[llvm] [WebAssembly] Preserve constness for imported globals (PR #199507)

Changqing Jing via llvm-commits llvm-commits at lists.llvm.org
Sat Jun 6 23:55:05 PDT 2026


https://github.com/Changqing-JING updated https://github.com/llvm/llvm-project/pull/199507

>From ebe54a3fbb9e326216c1d09cb366733bad95635b Mon Sep 17 00:00:00 2001
From: Changqing Jing <changqing.jing at bmw.com>
Date: Mon, 25 May 2026 18:05:45 +0800
Subject: [PATCH 1/6] [WebAssembly] Preserve constness for imported globals

---
 .../Utils/WebAssemblyTypeUtilities.cpp        |  4 +-
 .../Utils/WebAssemblyTypeUtilities.h          |  2 +-
 .../WebAssembly/WebAssemblyAsmPrinter.cpp     |  3 +-
 .../WebAssembly/WebAssemblyMCInstLower.cpp    | 45 ++++++++++---------
 .../WebAssembly/imported-const-global.ll      | 28 ++++++++++++
 5 files changed, 58 insertions(+), 24 deletions(-)
 create mode 100644 llvm/test/CodeGen/WebAssembly/imported-const-global.ll

diff --git a/llvm/lib/Target/WebAssembly/Utils/WebAssemblyTypeUtilities.cpp b/llvm/lib/Target/WebAssembly/Utils/WebAssemblyTypeUtilities.cpp
index f9293460e701a..57502c82f6c26 100644
--- a/llvm/lib/Target/WebAssembly/Utils/WebAssemblyTypeUtilities.cpp
+++ b/llvm/lib/Target/WebAssembly/Utils/WebAssemblyTypeUtilities.cpp
@@ -67,7 +67,7 @@ wasm::ValType WebAssembly::toValType(MVT Type) {
 }
 
 void WebAssembly::wasmSymbolSetType(MCSymbolWasm *Sym, const Type *GlobalVT,
-                                    ArrayRef<MVT> VTs) {
+                                    ArrayRef<MVT> VTs, bool Mutable) {
   assert(!Sym->getType());
 
   // Tables are represented as Arrays in LLVM IR therefore
@@ -94,6 +94,6 @@ void WebAssembly::wasmSymbolSetType(MCSymbolWasm *Sym, const Type *GlobalVT,
     Sym->setTableType(ValTy);
   } else {
     Sym->setType(wasm::WASM_SYMBOL_TYPE_GLOBAL);
-    Sym->setGlobalType(wasm::WasmGlobalType{uint8_t(ValTy), /*Mutable=*/true});
+    Sym->setGlobalType(wasm::WasmGlobalType{uint8_t(ValTy), Mutable});
   }
 }
diff --git a/llvm/lib/Target/WebAssembly/Utils/WebAssemblyTypeUtilities.h b/llvm/lib/Target/WebAssembly/Utils/WebAssemblyTypeUtilities.h
index 87660947e7de1..c55e773980eaf 100644
--- a/llvm/lib/Target/WebAssembly/Utils/WebAssemblyTypeUtilities.h
+++ b/llvm/lib/Target/WebAssembly/Utils/WebAssemblyTypeUtilities.h
@@ -60,7 +60,7 @@ wasm::ValType toValType(MVT Type);
 
 /// Sets a Wasm Symbol Type.
 void wasmSymbolSetType(MCSymbolWasm *Sym, const Type *GlobalVT,
-                       ArrayRef<MVT> VTs);
+                       ArrayRef<MVT> VTs, bool Mutable = true);
 
 } // end namespace WebAssembly
 } // end namespace llvm
diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp b/llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp
index edea99e629407..8c0e866f38bf1 100644
--- a/llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp
+++ b/llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp
@@ -206,7 +206,8 @@ void WebAssemblyAsmPrinter::emitGlobalVariable(const GlobalVariable *GV) {
     computeLegalValueVTs(TLI, GV->getParent()->getContext(),
                          GV->getDataLayout(), GlobalVT, VTs);
 
-    WebAssembly::wasmSymbolSetType(Sym, GlobalVT, VTs);
+    WebAssembly::wasmSymbolSetType(Sym, GlobalVT, VTs,
+                                   /*Mutable=*/!GV->isConstant());
   }
 
   emitVisibility(Sym, GV->getVisibility(), !GV->isDeclaration());
diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyMCInstLower.cpp b/llvm/lib/Target/WebAssembly/WebAssemblyMCInstLower.cpp
index e48283aadb437..b4c0f7fc5b802 100644
--- a/llvm/lib/Target/WebAssembly/WebAssemblyMCInstLower.cpp
+++ b/llvm/lib/Target/WebAssembly/WebAssemblyMCInstLower.cpp
@@ -28,6 +28,7 @@
 #include "llvm/CodeGen/MachineFunction.h"
 #include "llvm/CodeGen/MachineOperand.h"
 #include "llvm/IR/Constants.h"
+#include "llvm/IR/GlobalVariable.h"
 #include "llvm/MC/MCAsmInfo.h"
 #include "llvm/MC/MCContext.h"
 #include "llvm/MC/MCExpr.h"
@@ -65,7 +66,11 @@ WebAssemblyMCInstLower::GetGlobalAddressSymbol(const MachineOperand &MO) const {
       SmallVector<MVT, 1> VTs;
       computeLegalValueVTs(CurrentFunc, TM, GlobalVT, VTs);
 
-      WebAssembly::wasmSymbolSetType(WasmSym, GlobalVT, VTs);
+      bool Mutable = true;
+      if (const auto *GV = dyn_cast<GlobalVariable>(Global))
+        Mutable = !GV->isConstant();
+
+      WebAssembly::wasmSymbolSetType(WasmSym, GlobalVT, VTs, Mutable);
     }
     return WasmSym;
   }
@@ -99,25 +104,25 @@ MCOperand WebAssemblyMCInstLower::lowerSymbolOperand(const MachineOperand &MO,
   unsigned TargetFlags = MO.getTargetFlags();
 
   switch (TargetFlags) {
-    case WebAssemblyII::MO_NO_FLAG:
-      break;
-    case WebAssemblyII::MO_GOT_TLS:
-      Spec = WebAssembly::S_GOT_TLS;
-      break;
-    case WebAssemblyII::MO_GOT:
-      Spec = WebAssembly::S_GOT;
-      break;
-    case WebAssemblyII::MO_MEMORY_BASE_REL:
-      Spec = WebAssembly::S_MBREL;
-      break;
-    case WebAssemblyII::MO_TLS_BASE_REL:
-      Spec = WebAssembly::S_TLSREL;
-      break;
-    case WebAssemblyII::MO_TABLE_BASE_REL:
-      Spec = WebAssembly::S_TBREL;
-      break;
-    default:
-      llvm_unreachable("Unknown target flag on GV operand");
+  case WebAssemblyII::MO_NO_FLAG:
+    break;
+  case WebAssemblyII::MO_GOT_TLS:
+    Spec = WebAssembly::S_GOT_TLS;
+    break;
+  case WebAssemblyII::MO_GOT:
+    Spec = WebAssembly::S_GOT;
+    break;
+  case WebAssemblyII::MO_MEMORY_BASE_REL:
+    Spec = WebAssembly::S_MBREL;
+    break;
+  case WebAssemblyII::MO_TLS_BASE_REL:
+    Spec = WebAssembly::S_TLSREL;
+    break;
+  case WebAssemblyII::MO_TABLE_BASE_REL:
+    Spec = WebAssembly::S_TBREL;
+    break;
+  default:
+    llvm_unreachable("Unknown target flag on GV operand");
   }
 
   const MCExpr *Expr = MCSymbolRefExpr::create(Sym, Spec, Ctx);
diff --git a/llvm/test/CodeGen/WebAssembly/imported-const-global.ll b/llvm/test/CodeGen/WebAssembly/imported-const-global.ll
new file mode 100644
index 0000000000000..3a29cd042228e
--- /dev/null
+++ b/llvm/test/CodeGen/WebAssembly/imported-const-global.ll
@@ -0,0 +1,28 @@
+; RUN: llc < %s --mtriple=wasm32-unknown-unknown -asm-verbose=false | FileCheck %s --check-prefix=ASM
+; RUN: llc < %s --mtriple=wasm32-unknown-unknown --filetype=obj | obj2yaml | FileCheck %s --check-prefix=OBJ
+
+ at imported_g = external addrspace(1) constant i32
+
+define i32 @goo() {
+; ASM-LABEL: goo:
+; ASM-NEXT:  functype       goo () -> (i32)
+; ASM-NEXT:  global.get imported_g
+; ASM-NEXT:  end_function
+  %v = load i32, ptr addrspace(1) @imported_g
+  ret i32 %v
+}
+
+; ASM:       .globaltype imported_g, i32, immutable
+
+; OBJ:       --- !WASM
+; OBJ:       FileHeader:
+; OBJ:         Version:         0x1
+; OBJ:       Sections:
+; OBJ:         - Type:            TYPE
+; OBJ:         - Type:            IMPORT
+; OBJ:           Imports:
+; OBJ:             - Module:          env
+; OBJ:               Field:           imported_g
+; OBJ:               Kind:            GLOBAL
+; OBJ:               GlobalType:      I32
+; OBJ:               GlobalMutable:   false
\ No newline at end of file

>From 8006929e6d3442eb2514b93cb8aaf44dc722013d Mon Sep 17 00:00:00 2001
From: Changqing Jing <changqing.jing at bmw.com>
Date: Fri, 29 May 2026 15:49:35 +0800
Subject: [PATCH 2/6] Fix  review

---
 llvm/lib/Target/WebAssembly/Utils/WebAssemblyTypeUtilities.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/lib/Target/WebAssembly/Utils/WebAssemblyTypeUtilities.h b/llvm/lib/Target/WebAssembly/Utils/WebAssemblyTypeUtilities.h
index c55e773980eaf..c5e0dcfffef9c 100644
--- a/llvm/lib/Target/WebAssembly/Utils/WebAssemblyTypeUtilities.h
+++ b/llvm/lib/Target/WebAssembly/Utils/WebAssemblyTypeUtilities.h
@@ -60,7 +60,7 @@ wasm::ValType toValType(MVT Type);
 
 /// Sets a Wasm Symbol Type.
 void wasmSymbolSetType(MCSymbolWasm *Sym, const Type *GlobalVT,
-                       ArrayRef<MVT> VTs, bool Mutable = true);
+                       ArrayRef<MVT> VTs, bool Mutable);
 
 } // end namespace WebAssembly
 } // end namespace llvm

>From e07807a2ca1b2f98c115bdda0b57f0d3a691c108 Mon Sep 17 00:00:00 2001
From: Changqing Jing <changqing.jing at bmw.com>
Date: Sun, 31 May 2026 18:10:43 +0800
Subject: [PATCH 3/6] Fix review

---
 .../WebAssembly/WebAssemblyMCInstLower.cpp       | 16 ++++++++++++----
 .../WebAssembly/immutable-global-alias.ll        | 16 ++++++++++++++++
 .../invalid-wasm-global-function-alias.ll        | 16 ++++++++++++++++
 .../WebAssembly/invalid-wasm-global-symbol.ll    | 12 ++++++++++++
 4 files changed, 56 insertions(+), 4 deletions(-)
 create mode 100644 llvm/test/CodeGen/WebAssembly/immutable-global-alias.ll
 create mode 100644 llvm/test/CodeGen/WebAssembly/invalid-wasm-global-function-alias.ll
 create mode 100644 llvm/test/CodeGen/WebAssembly/invalid-wasm-global-symbol.ll

diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyMCInstLower.cpp b/llvm/lib/Target/WebAssembly/WebAssemblyMCInstLower.cpp
index b4c0f7fc5b802..2726c94898156 100644
--- a/llvm/lib/Target/WebAssembly/WebAssemblyMCInstLower.cpp
+++ b/llvm/lib/Target/WebAssembly/WebAssemblyMCInstLower.cpp
@@ -47,6 +47,15 @@ static cl::opt<bool>
                                " instruction output for test purposes only."),
                       cl::init(false));
 
+static bool getWasmGlobalMutable(const GlobalValue *Global) {
+  const auto *BaseObject = Global->getAliaseeObject();
+  const auto *GV = dyn_cast_or_null<GlobalVariable>(BaseObject);
+  if (!GV)
+    report_fatal_error(
+        "wasm_var address space symbol must resolve to a GlobalVariable");
+  return !GV->isConstant();
+}
+
 static void removeRegisterOperands(const MachineInstr *MI, MCInst &OutMI);
 
 MCSymbol *
@@ -62,14 +71,13 @@ WebAssemblyMCInstLower::GetGlobalAddressSymbol(const MachineOperand &MO) const {
       const MachineFunction &MF = *MO.getParent()->getParent()->getParent();
       const TargetMachine &TM = MF.getTarget();
       const Function &CurrentFunc = MF.getFunction();
+
+      bool Mutable = getWasmGlobalMutable(Global);
+
       Type *GlobalVT = Global->getValueType();
       SmallVector<MVT, 1> VTs;
       computeLegalValueVTs(CurrentFunc, TM, GlobalVT, VTs);
 
-      bool Mutable = true;
-      if (const auto *GV = dyn_cast<GlobalVariable>(Global))
-        Mutable = !GV->isConstant();
-
       WebAssembly::wasmSymbolSetType(WasmSym, GlobalVT, VTs, Mutable);
     }
     return WasmSym;
diff --git a/llvm/test/CodeGen/WebAssembly/immutable-global-alias.ll b/llvm/test/CodeGen/WebAssembly/immutable-global-alias.ll
new file mode 100644
index 0000000000000..0953010b50125
--- /dev/null
+++ b/llvm/test/CodeGen/WebAssembly/immutable-global-alias.ll
@@ -0,0 +1,16 @@
+; RUN: llc < %s --mtriple=wasm32-unknown-unknown -asm-verbose=false | FileCheck %s
+
+ at defined_g = addrspace(1) constant i32 42
+ at defined_g_alias = alias i32, ptr addrspace(1) @defined_g
+
+define i32 @foo() {
+; CHECK-LABEL: foo:
+; CHECK-NEXT:  .functype       foo () -> (i32)
+; CHECK-NEXT:  global.get      defined_g_alias
+; CHECK-NEXT:  end_function
+  %v = load i32, ptr addrspace(1) @defined_g_alias
+  ret i32 %v
+}
+
+; CHECK:       .globaltype     defined_g, i32, immutable
+; CHECK:       defined_g_alias = defined_g
\ No newline at end of file
diff --git a/llvm/test/CodeGen/WebAssembly/invalid-wasm-global-function-alias.ll b/llvm/test/CodeGen/WebAssembly/invalid-wasm-global-function-alias.ll
new file mode 100644
index 0000000000000..56a8239dfc7d9
--- /dev/null
+++ b/llvm/test/CodeGen/WebAssembly/invalid-wasm-global-function-alias.ll
@@ -0,0 +1,16 @@
+; RUN: not --crash llc -mtriple=wasm32-unknown-unknown -filetype=asm %s -o - 2>&1 | FileCheck %s
+
+; CHECK: LLVM ERROR: wasm_var address space symbol must resolve to a GlobalVariable
+
+target triple = "wasm32-unknown-unknown"
+
+ at f_alias = alias i32 (), ptr addrspace(1) @f
+
+define i32 @f() addrspace(1) {
+  ret i32 0
+}
+
+define i32 @use() {
+  %v = load i32, ptr addrspace(1) @f_alias
+  ret i32 %v
+}
\ No newline at end of file
diff --git a/llvm/test/CodeGen/WebAssembly/invalid-wasm-global-symbol.ll b/llvm/test/CodeGen/WebAssembly/invalid-wasm-global-symbol.ll
new file mode 100644
index 0000000000000..7353af10850b6
--- /dev/null
+++ b/llvm/test/CodeGen/WebAssembly/invalid-wasm-global-symbol.ll
@@ -0,0 +1,12 @@
+; RUN: not --crash llc -mtriple=wasm32-unknown-unknown -filetype=asm %s -o - 2>&1 | FileCheck %s
+
+; CHECK: LLVM ERROR: wasm_var address space symbol must resolve to a GlobalVariable
+
+target triple = "wasm32-unknown-unknown"
+
+ at bad = alias i32, inttoptr(i32 42 to ptr addrspace(1))
+
+define i32 @foo() {
+  %v = load i32, ptr addrspace(1) @bad
+  ret i32 %v
+}
\ No newline at end of file

>From 6cd22e536707f401925f619003c4f48e88780eaf Mon Sep 17 00:00:00 2001
From: Changqing Jing <changqing.jing at bmw.com>
Date: Sat, 6 Jun 2026 20:40:44 +0800
Subject: [PATCH 4/6] fix review

---
 .../WebAssembly/WebAssemblyMCInstLower.cpp    | 19 ++++++++++++++-----
 .../invalid-wasm-global-function-alias.ll     |  5 +++--
 .../WebAssembly/invalid-wasm-global-symbol.ll |  5 +++--
 3 files changed, 20 insertions(+), 9 deletions(-)

diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyMCInstLower.cpp b/llvm/lib/Target/WebAssembly/WebAssemblyMCInstLower.cpp
index 2726c94898156..27fd4cf133db3 100644
--- a/llvm/lib/Target/WebAssembly/WebAssemblyMCInstLower.cpp
+++ b/llvm/lib/Target/WebAssembly/WebAssemblyMCInstLower.cpp
@@ -28,6 +28,7 @@
 #include "llvm/CodeGen/MachineFunction.h"
 #include "llvm/CodeGen/MachineOperand.h"
 #include "llvm/IR/Constants.h"
+#include "llvm/IR/DiagnosticInfo.h"
 #include "llvm/IR/GlobalVariable.h"
 #include "llvm/MC/MCAsmInfo.h"
 #include "llvm/MC/MCContext.h"
@@ -47,12 +48,18 @@ static cl::opt<bool>
                                " instruction output for test purposes only."),
                       cl::init(false));
 
-static bool getWasmGlobalMutable(const GlobalValue *Global) {
+static bool getWasmGlobalMutable(const GlobalValue *Global,
+                                 const Function &CurrentFunc,
+                                 const DiagnosticLocation &DL) {
   const auto *BaseObject = Global->getAliaseeObject();
   const auto *GV = dyn_cast_or_null<GlobalVariable>(BaseObject);
-  if (!GV)
-    report_fatal_error(
-        "wasm_var address space symbol must resolve to a GlobalVariable");
+  if (!GV) {
+    CurrentFunc.getContext().diagnose(DiagnosticInfoUnsupported(
+        CurrentFunc, "wasm_var address space symbol must resolve to a "
+                     "GlobalVariable",
+        DL));
+    return false;
+  }
   return !GV->isConstant();
 }
 
@@ -68,11 +75,13 @@ WebAssemblyMCInstLower::GetGlobalAddressSymbol(const MachineOperand &MO) const {
     // WASM_SYMBOL_TYPE_GLOBAL.
     if (WebAssembly::isWasmVarAddressSpace(Global->getAddressSpace()) &&
         !WasmSym->getType()) {
+      const MachineInstr &MI = *MO.getParent();
       const MachineFunction &MF = *MO.getParent()->getParent()->getParent();
       const TargetMachine &TM = MF.getTarget();
       const Function &CurrentFunc = MF.getFunction();
 
-      bool Mutable = getWasmGlobalMutable(Global);
+      bool Mutable = getWasmGlobalMutable(Global, CurrentFunc,
+                                          MI.getDebugLoc());
 
       Type *GlobalVT = Global->getValueType();
       SmallVector<MVT, 1> VTs;
diff --git a/llvm/test/CodeGen/WebAssembly/invalid-wasm-global-function-alias.ll b/llvm/test/CodeGen/WebAssembly/invalid-wasm-global-function-alias.ll
index 56a8239dfc7d9..b0a2b8d82d33f 100644
--- a/llvm/test/CodeGen/WebAssembly/invalid-wasm-global-function-alias.ll
+++ b/llvm/test/CodeGen/WebAssembly/invalid-wasm-global-function-alias.ll
@@ -1,6 +1,7 @@
-; RUN: not --crash llc -mtriple=wasm32-unknown-unknown -filetype=asm %s -o - 2>&1 | FileCheck %s
+; RUN: not llc -mtriple=wasm32-unknown-unknown -filetype=asm %s -o - 2>&1 | FileCheck %s
 
-; CHECK: LLVM ERROR: wasm_var address space symbol must resolve to a GlobalVariable
+; CHECK-NOT: LLVM ERROR
+; CHECK: in function use i32 (): wasm_var address space symbol must resolve to a GlobalVariable
 
 target triple = "wasm32-unknown-unknown"
 
diff --git a/llvm/test/CodeGen/WebAssembly/invalid-wasm-global-symbol.ll b/llvm/test/CodeGen/WebAssembly/invalid-wasm-global-symbol.ll
index 7353af10850b6..a1fa95f8c302e 100644
--- a/llvm/test/CodeGen/WebAssembly/invalid-wasm-global-symbol.ll
+++ b/llvm/test/CodeGen/WebAssembly/invalid-wasm-global-symbol.ll
@@ -1,6 +1,7 @@
-; RUN: not --crash llc -mtriple=wasm32-unknown-unknown -filetype=asm %s -o - 2>&1 | FileCheck %s
+; RUN: not llc -mtriple=wasm32-unknown-unknown -filetype=asm %s -o - 2>&1 | FileCheck %s
 
-; CHECK: LLVM ERROR: wasm_var address space symbol must resolve to a GlobalVariable
+; CHECK-NOT: LLVM ERROR
+; CHECK: in function foo i32 (): wasm_var address space symbol must resolve to a GlobalVariable
 
 target triple = "wasm32-unknown-unknown"
 

>From c8a80f000c1e424fbadd82e27f87beb9cc0ff58c Mon Sep 17 00:00:00 2001
From: Changqing Jing <changqing.jing at bmw.com>
Date: Sat, 6 Jun 2026 20:51:30 +0800
Subject: [PATCH 5/6] Fix review

---
 llvm/lib/Target/WebAssembly/WebAssemblyMCInstLower.cpp   | 9 +++++----
 .../WebAssembly/invalid-wasm-global-function-alias.ll    | 2 +-
 .../CodeGen/WebAssembly/invalid-wasm-global-symbol.ll    | 2 +-
 3 files changed, 7 insertions(+), 6 deletions(-)

diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyMCInstLower.cpp b/llvm/lib/Target/WebAssembly/WebAssemblyMCInstLower.cpp
index 27fd4cf133db3..8860c03ce13e3 100644
--- a/llvm/lib/Target/WebAssembly/WebAssemblyMCInstLower.cpp
+++ b/llvm/lib/Target/WebAssembly/WebAssemblyMCInstLower.cpp
@@ -55,8 +55,9 @@ static bool getWasmGlobalMutable(const GlobalValue *Global,
   const auto *GV = dyn_cast_or_null<GlobalVariable>(BaseObject);
   if (!GV) {
     CurrentFunc.getContext().diagnose(DiagnosticInfoUnsupported(
-        CurrentFunc, "wasm_var address space symbol must resolve to a "
-                     "GlobalVariable",
+        CurrentFunc,
+        "wasm_var address space symbol must resolve to a "
+        "GlobalVariable",
         DL));
     return false;
   }
@@ -80,8 +81,8 @@ WebAssemblyMCInstLower::GetGlobalAddressSymbol(const MachineOperand &MO) const {
       const TargetMachine &TM = MF.getTarget();
       const Function &CurrentFunc = MF.getFunction();
 
-      bool Mutable = getWasmGlobalMutable(Global, CurrentFunc,
-                                          MI.getDebugLoc());
+      bool Mutable =
+          getWasmGlobalMutable(Global, CurrentFunc, MI.getDebugLoc());
 
       Type *GlobalVT = Global->getValueType();
       SmallVector<MVT, 1> VTs;
diff --git a/llvm/test/CodeGen/WebAssembly/invalid-wasm-global-function-alias.ll b/llvm/test/CodeGen/WebAssembly/invalid-wasm-global-function-alias.ll
index b0a2b8d82d33f..4c01d0b9d1829 100644
--- a/llvm/test/CodeGen/WebAssembly/invalid-wasm-global-function-alias.ll
+++ b/llvm/test/CodeGen/WebAssembly/invalid-wasm-global-function-alias.ll
@@ -14,4 +14,4 @@ define i32 @f() addrspace(1) {
 define i32 @use() {
   %v = load i32, ptr addrspace(1) @f_alias
   ret i32 %v
-}
\ No newline at end of file
+}
diff --git a/llvm/test/CodeGen/WebAssembly/invalid-wasm-global-symbol.ll b/llvm/test/CodeGen/WebAssembly/invalid-wasm-global-symbol.ll
index a1fa95f8c302e..308029ee57d81 100644
--- a/llvm/test/CodeGen/WebAssembly/invalid-wasm-global-symbol.ll
+++ b/llvm/test/CodeGen/WebAssembly/invalid-wasm-global-symbol.ll
@@ -10,4 +10,4 @@ target triple = "wasm32-unknown-unknown"
 define i32 @foo() {
   %v = load i32, ptr addrspace(1) @bad
   ret i32 %v
-}
\ No newline at end of file
+}

>From d327cfc5cd53e38d38067955d91c785204ffe96c Mon Sep 17 00:00:00 2001
From: Changqing Jing <changqing.jing at bmw.com>
Date: Sun, 7 Jun 2026 12:56:36 +0800
Subject: [PATCH 6/6] Fix review

---
 .../WebAssembly/WebAssemblyMCInstLower.cpp    | 53 ++++++++++---------
 .../invalid-wasm-global-function-alias.ll     |  2 +-
 .../WebAssembly/invalid-wasm-global-symbol.ll |  2 +-
 3 files changed, 30 insertions(+), 27 deletions(-)

diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyMCInstLower.cpp b/llvm/lib/Target/WebAssembly/WebAssemblyMCInstLower.cpp
index 8860c03ce13e3..b3b55e3091d5c 100644
--- a/llvm/lib/Target/WebAssembly/WebAssemblyMCInstLower.cpp
+++ b/llvm/lib/Target/WebAssembly/WebAssemblyMCInstLower.cpp
@@ -37,6 +37,7 @@
 #include "llvm/MC/MCSymbolWasm.h"
 #include "llvm/Support/ErrorHandling.h"
 #include "llvm/Support/raw_ostream.h"
+#include <optional>
 
 using namespace llvm;
 
@@ -48,9 +49,9 @@ static cl::opt<bool>
                                " instruction output for test purposes only."),
                       cl::init(false));
 
-static bool getWasmGlobalMutable(const GlobalValue *Global,
-                                 const Function &CurrentFunc,
-                                 const DiagnosticLocation &DL) {
+static std::optional<bool> getWasmGlobalMutable(const GlobalValue *Global,
+                                                const Function &CurrentFunc,
+                                                const DiagnosticLocation &DL) {
   const auto *BaseObject = Global->getAliaseeObject();
   const auto *GV = dyn_cast_or_null<GlobalVariable>(BaseObject);
   if (!GV) {
@@ -59,7 +60,7 @@ static bool getWasmGlobalMutable(const GlobalValue *Global,
         "wasm_var address space symbol must resolve to a "
         "GlobalVariable",
         DL));
-    return false;
+    return std::nullopt;
   }
   return !GV->isConstant();
 }
@@ -81,14 +82,16 @@ WebAssemblyMCInstLower::GetGlobalAddressSymbol(const MachineOperand &MO) const {
       const TargetMachine &TM = MF.getTarget();
       const Function &CurrentFunc = MF.getFunction();
 
-      bool Mutable =
+      std::optional<bool> Mutable =
           getWasmGlobalMutable(Global, CurrentFunc, MI.getDebugLoc());
+      if (!Mutable)
+        return WasmSym;
 
       Type *GlobalVT = Global->getValueType();
       SmallVector<MVT, 1> VTs;
       computeLegalValueVTs(CurrentFunc, TM, GlobalVT, VTs);
 
-      WebAssembly::wasmSymbolSetType(WasmSym, GlobalVT, VTs, Mutable);
+      WebAssembly::wasmSymbolSetType(WasmSym, GlobalVT, VTs, *Mutable);
     }
     return WasmSym;
   }
@@ -122,25 +125,25 @@ MCOperand WebAssemblyMCInstLower::lowerSymbolOperand(const MachineOperand &MO,
   unsigned TargetFlags = MO.getTargetFlags();
 
   switch (TargetFlags) {
-  case WebAssemblyII::MO_NO_FLAG:
-    break;
-  case WebAssemblyII::MO_GOT_TLS:
-    Spec = WebAssembly::S_GOT_TLS;
-    break;
-  case WebAssemblyII::MO_GOT:
-    Spec = WebAssembly::S_GOT;
-    break;
-  case WebAssemblyII::MO_MEMORY_BASE_REL:
-    Spec = WebAssembly::S_MBREL;
-    break;
-  case WebAssemblyII::MO_TLS_BASE_REL:
-    Spec = WebAssembly::S_TLSREL;
-    break;
-  case WebAssemblyII::MO_TABLE_BASE_REL:
-    Spec = WebAssembly::S_TBREL;
-    break;
-  default:
-    llvm_unreachable("Unknown target flag on GV operand");
+    case WebAssemblyII::MO_NO_FLAG:
+      break;
+    case WebAssemblyII::MO_GOT_TLS:
+      Spec = WebAssembly::S_GOT_TLS;
+      break;
+    case WebAssemblyII::MO_GOT:
+      Spec = WebAssembly::S_GOT;
+      break;
+    case WebAssemblyII::MO_MEMORY_BASE_REL:
+      Spec = WebAssembly::S_MBREL;
+      break;
+    case WebAssemblyII::MO_TLS_BASE_REL:
+      Spec = WebAssembly::S_TLSREL;
+      break;
+    case WebAssemblyII::MO_TABLE_BASE_REL:
+      Spec = WebAssembly::S_TBREL;
+      break;
+    default:
+      llvm_unreachable("Unknown target flag on GV operand");
   }
 
   const MCExpr *Expr = MCSymbolRefExpr::create(Sym, Spec, Ctx);
diff --git a/llvm/test/CodeGen/WebAssembly/invalid-wasm-global-function-alias.ll b/llvm/test/CodeGen/WebAssembly/invalid-wasm-global-function-alias.ll
index 4c01d0b9d1829..2a14d47397847 100644
--- a/llvm/test/CodeGen/WebAssembly/invalid-wasm-global-function-alias.ll
+++ b/llvm/test/CodeGen/WebAssembly/invalid-wasm-global-function-alias.ll
@@ -1,4 +1,4 @@
-; RUN: not llc -mtriple=wasm32-unknown-unknown -filetype=asm %s -o - 2>&1 | FileCheck %s
+; RUN: not llc -mtriple=wasm32-unknown-unknown -filetype=asm %s -o %t 2>&1 | FileCheck %s
 
 ; CHECK-NOT: LLVM ERROR
 ; CHECK: in function use i32 (): wasm_var address space symbol must resolve to a GlobalVariable
diff --git a/llvm/test/CodeGen/WebAssembly/invalid-wasm-global-symbol.ll b/llvm/test/CodeGen/WebAssembly/invalid-wasm-global-symbol.ll
index 308029ee57d81..0fae07737476f 100644
--- a/llvm/test/CodeGen/WebAssembly/invalid-wasm-global-symbol.ll
+++ b/llvm/test/CodeGen/WebAssembly/invalid-wasm-global-symbol.ll
@@ -1,4 +1,4 @@
-; RUN: not llc -mtriple=wasm32-unknown-unknown -filetype=asm %s -o - 2>&1 | FileCheck %s
+; RUN: not llc -mtriple=wasm32-unknown-unknown -filetype=asm %s -o %t 2>&1 | FileCheck %s
 
 ; CHECK-NOT: LLVM ERROR
 ; CHECK: in function foo i32 (): wasm_var address space symbol must resolve to a GlobalVariable



More information about the llvm-commits mailing list