From b3d3311826b660f68088605f2965e17fe5915377 Mon Sep 17 00:00:00 2001
From: Cheng Shao <terrorjack@type.dance>
Date: Tue, 1 Oct 2024 19:04:06 +0000
Subject: [PATCH] compiler: fix Cmm dynamic CLabels for wasm

This commit fixes the handling of dynamic CLabels for the wasm
backend. Just do the simplest handling: preserve the original CLabel,
both unreg/NCG backends can handle them properly without issue.

(cherry picked from commit 744114618678ed1eac7a699c738ffa3223d1b41a)
---
 compiler/GHC/CmmToAsm/PIC.hs | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/compiler/GHC/CmmToAsm/PIC.hs b/compiler/GHC/CmmToAsm/PIC.hs
index 5bda90ab455..104d72d61eb 100644
--- a/compiler/GHC/CmmToAsm/PIC.hs
+++ b/compiler/GHC/CmmToAsm/PIC.hs
@@ -137,6 +137,11 @@ cmmMakeDynamicReference config referenceKind lbl
               addImport symbolPtr
               return $ cmmLoadBWord platform (cmmMakePicReference config symbolPtr)
 
+        -- On wasm, always preserve the original CLabel, the backends
+        -- will handle dynamic references properly
+        AccessDirectly | ArchWasm32 <- platformArch platform ->
+              pure $ CmmLit $ CmmLabel lbl
+
         AccessDirectly -> case referenceKind of
                 -- for data, we might have to make some calculations:
               DataReference -> return $ cmmMakePicReference config lbl
@@ -414,6 +419,11 @@ howToAccessLabel config _arch os _kind lbl
             then AccessViaSymbolPtr
             else AccessDirectly
 
+-- On wasm, always keep the original CLabel and let the backend decide
+-- how to handle dynamic references
+howToAccessLabel _ ArchWasm32 _ _ _
+        = AccessDirectly
+
 -- all other platforms
 howToAccessLabel config _arch _os _kind _lbl
         | not (ncgPIC config)
-- 
GitLab