From 744114618678ed1eac7a699c738ffa3223d1b41a 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.
---
 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 5bd6edbb6b4..01692f800d5 100644
--- a/compiler/GHC/CmmToAsm/PIC.hs
+++ b/compiler/GHC/CmmToAsm/PIC.hs
@@ -142,6 +142,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
@@ -413,6 +418,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