[clang] [lld] [llvm] [Clang][WebAssembly] Add wasm_global declaration attribute (PR #221909)
Changqing Jing via cfe-commits
cfe-commits at lists.llvm.org
Sat Oct 10 00:41:25 PDT 2026
https://github.com/Changqing-JING updated https://github.com/llvm/llvm-project/pull/221909
>From c4932a4981b7e3cedbc771cfef16c03c47732091 Mon Sep 17 00:00:00 2001
From: Changqing Jing <changqing.jing at bmw.com>
Date: Tue, 8 Sep 2026 15:42:28 +0800
Subject: [PATCH 1/5] [Clang][WebAssembly] Add wasm_global declaration
attribute
---
clang/include/clang/Basic/Attr.td | 6 ++++++
clang/include/clang/Basic/AttrDocs.td | 12 ++++++++++++
clang/include/clang/Sema/SemaWasm.h | 1 +
clang/lib/CodeGen/Targets/WebAssembly.cpp | 9 +++++++++
clang/lib/Sema/SemaDeclAttr.cpp | 3 +++
clang/lib/Sema/SemaWasm.cpp | 10 ++++++++++
.../WebAssembly/wasm-global-attribute.cpp | 16 ++++++++++++++++
.../llvm/Support}/WasmAddressSpaces.h | 14 ++++++--------
.../WebAssembly/Utils/WebAssemblyTypeUtilities.h | 2 +-
.../Target/WebAssembly/WebAssemblyFastISel.cpp | 2 +-
.../WebAssembly/WebAssemblyRefTypeMem2Local.cpp | 2 +-
11 files changed, 66 insertions(+), 11 deletions(-)
create mode 100644 clang/test/CodeGen/WebAssembly/wasm-global-attribute.cpp
rename llvm/{lib/Target/WebAssembly/Utils => include/llvm/Support}/WasmAddressSpaces.h (73%)
diff --git a/clang/include/clang/Basic/Attr.td b/clang/include/clang/Basic/Attr.td
index 61ef3fb6124405..f11dcc77e84831 100644
--- a/clang/include/clang/Basic/Attr.td
+++ b/clang/include/clang/Basic/Attr.td
@@ -2602,6 +2602,12 @@ def WebAssemblyImportName : InheritableAttr,
let Subjects = SubjectList<[Function, GlobalVar], ErrorDiag>;
}
+def WebAssemblyGlobal : InheritableAttr, TargetSpecificAttr<TargetWebAssembly> {
+ let Spellings = [Clang<"wasm_global">];
+ let Documentation = [WebAssemblyGlobalDocs];
+ let Subjects = SubjectList<[GlobalVar], ErrorDiag>;
+}
+
def NoSplitStack : InheritableAttr {
let Spellings = [GCC<"no_split_stack">];
let Subjects = SubjectList<[Function], ErrorDiag>;
diff --git a/clang/include/clang/Basic/AttrDocs.td b/clang/include/clang/Basic/AttrDocs.td
index 1bbaae8b211064..6140c827c24b08 100644
--- a/clang/include/clang/Basic/AttrDocs.td
+++ b/clang/include/clang/Basic/AttrDocs.td
@@ -7337,6 +7337,18 @@ request a specific field name be used instead.
}];
}
+def WebAssemblyGlobalDocs : Documentation {
+ let Category = DocCatVariable;
+ let Content = [{
+Clang supports the `[[clang::wasm_global]]` attribute for the WebAssembly
+target. This attribute declares that a variable is stored as a WebAssembly
+global rather than in linear memory.
+
+WebAssembly globals can be imported or exported using the
+`import_module`, `import_name`, and `export_name` attributes.
+ }];
+}
+
def ArtificialDocs : Documentation {
let Category = DocCatFunction;
let Content = [{
diff --git a/clang/include/clang/Sema/SemaWasm.h b/clang/include/clang/Sema/SemaWasm.h
index c70cda259029e2..41245ef649e841 100644
--- a/clang/include/clang/Sema/SemaWasm.h
+++ b/clang/include/clang/Sema/SemaWasm.h
@@ -50,6 +50,7 @@ class SemaWasm : public SemaBase {
void handleWebAssemblyExportNameAttr(Decl *D, const ParsedAttr &AL);
void handleWebAssemblyImportModuleAttr(Decl *D, const ParsedAttr &AL);
void handleWebAssemblyImportNameAttr(Decl *D, const ParsedAttr &AL);
+ void handleWebAssemblyGlobalAttr(Decl *D, const ParsedAttr &AL);
};
} // namespace clang
diff --git a/clang/lib/CodeGen/Targets/WebAssembly.cpp b/clang/lib/CodeGen/Targets/WebAssembly.cpp
index 8f6f58bfb807e8..f51edfc620baa4 100644
--- a/clang/lib/CodeGen/Targets/WebAssembly.cpp
+++ b/clang/lib/CodeGen/Targets/WebAssembly.cpp
@@ -9,6 +9,7 @@
#include "ABIInfoImpl.h"
#include "TargetInfo.h"
#include "clang/Basic/DiagnosticFrontend.h"
+#include "llvm/Support/WasmAddressSpaces.h"
using namespace clang;
using namespace clang::CodeGen;
@@ -55,6 +56,14 @@ class WebAssemblyTargetCodeGenInfo final : public TargetCodeGenInfo {
std::make_unique<SwiftABIInfo>(CGT, /*SwiftErrorInRegister=*/false);
}
+ LangAS getGlobalVarAddressSpace(CodeGenModule &CGM,
+ const VarDecl *D) const override {
+ if (D && D->hasAttr<WebAssemblyGlobalAttr>())
+ return getLangASFromTargetAS(
+ llvm::WebAssembly::WASM_ADDRESS_SPACE_VAR);
+ return TargetCodeGenInfo::getGlobalVarAddressSpace(CGM, D);
+ }
+
void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
CodeGen::CodeGenModule &CGM) const override {
TargetCodeGenInfo::setTargetAttributes(D, GV, CGM);
diff --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp
index eb4a8c2ab9ae01..6bc06c8f24b2bf 100644
--- a/clang/lib/Sema/SemaDeclAttr.cpp
+++ b/clang/lib/Sema/SemaDeclAttr.cpp
@@ -7750,6 +7750,9 @@ ProcessDeclAttribute(Sema &S, Decl *D, const ParsedAttr &AL,
case ParsedAttr::AT_WebAssemblyImportName:
S.Wasm().handleWebAssemblyImportNameAttr(D, AL);
break;
+ case ParsedAttr::AT_WebAssemblyGlobal:
+ S.Wasm().handleWebAssemblyGlobalAttr(D, AL);
+ break;
case ParsedAttr::AT_IBOutlet:
S.ObjC().handleIBOutlet(D, AL);
break;
diff --git a/clang/lib/Sema/SemaWasm.cpp b/clang/lib/Sema/SemaWasm.cpp
index cfc0a08a50e5a7..d0fae91fa47279 100644
--- a/clang/lib/Sema/SemaWasm.cpp
+++ b/clang/lib/Sema/SemaWasm.cpp
@@ -20,6 +20,7 @@
#include "clang/Basic/TargetInfo.h"
#include "clang/Sema/Attr.h"
#include "clang/Sema/Sema.h"
+#include "llvm/Support/WasmAddressSpaces.h"
namespace clang {
@@ -457,4 +458,13 @@ void SemaWasm::handleWebAssemblyExportNameAttr(Decl *D, const ParsedAttr &AL) {
D->addAttr(UsedAttr::CreateImplicit(Context));
}
+void SemaWasm::handleWebAssemblyGlobalAttr(Decl *D, const ParsedAttr &AL) {
+ ASTContext &Context = getASTContext();
+ auto *VD = cast<VarDecl>(D);
+ VD->setType(Context.getAddrSpaceQualType(
+ VD->getType(), getLangASFromTargetAS(
+ llvm::WebAssembly::WASM_ADDRESS_SPACE_VAR)));
+ D->addAttr(::new (Context) WebAssemblyGlobalAttr(Context, AL));
+}
+
} // namespace clang
diff --git a/clang/test/CodeGen/WebAssembly/wasm-global-attribute.cpp b/clang/test/CodeGen/WebAssembly/wasm-global-attribute.cpp
new file mode 100644
index 00000000000000..e61056d9ba8481
--- /dev/null
+++ b/clang/test/CodeGen/WebAssembly/wasm-global-attribute.cpp
@@ -0,0 +1,16 @@
+// RUN: %clang_cc1 -triple wasm32-unknown-unknown-wasm -emit-llvm -o - %s | FileCheck %s
+
+extern "C" {
+int exported_g [[clang::wasm_global]] = 42;
+
+extern const int imported_g [[clang::wasm_global]]
+ __attribute__((import_module("env"), import_name("imported_g")));
+
+int get_import(void) { return imported_g; }
+}
+
+// CHECK: @exported_g = addrspace(1) global i32 42, align 4
+// CHECK: @imported_g = external addrspace(1) constant i32, align 4 #0
+// CHECK: define{{.*}} @get_import()
+// CHECK: load i32, ptr addrspace(1) @imported_g
+// CHECK: attributes #0 = { "wasm-import-module"="env" "wasm-import-name"="imported_g" }
\ No newline at end of file
diff --git a/llvm/lib/Target/WebAssembly/Utils/WasmAddressSpaces.h b/llvm/include/llvm/Support/WasmAddressSpaces.h
similarity index 73%
rename from llvm/lib/Target/WebAssembly/Utils/WasmAddressSpaces.h
rename to llvm/include/llvm/Support/WasmAddressSpaces.h
index d2ab2c0f9e7777..8738b4e88520d7 100644
--- a/llvm/lib/Target/WebAssembly/Utils/WasmAddressSpaces.h
+++ b/llvm/include/llvm/Support/WasmAddressSpaces.h
@@ -1,4 +1,4 @@
-//===--- llvm/CodeGen/WasmAddressSpaces.h -----------------------*- C++ -*-===//
+//===---------------- WasmAddressSpaces.h ----------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
@@ -6,22 +6,21 @@
//
//===----------------------------------------------------------------------===//
//
-// Address Spaces for WebAssembly Type Handling
+// Address spaces for WebAssembly IR and type handling.
//
//===----------------------------------------------------------------------===//
-#ifndef LLVM_LIB_TARGET_WEBASSEMBLY_UTILS_WASMADDRESSSPACES_H
-#define LLVM_LIB_TARGET_WEBASSEMBLY_UTILS_WASMADDRESSSPACES_H
+#ifndef LLVM_SUPPORT_WASMADDRESSSPACES_H
+#define LLVM_SUPPORT_WASMADDRESSSPACES_H
namespace llvm {
-
namespace WebAssembly {
enum WasmAddressSpace : unsigned {
// Default address space, for pointers to linear memory (stack, heap, data).
WASM_ADDRESS_SPACE_DEFAULT = 0,
// A non-integral address space for pointers to named objects outside of
- // linear memory: WebAssembly globals or WebAssembly locals. Loads and stores
+ // linear memory: WebAssembly globals or WebAssembly locals. Loads and stores
// to these pointers are lowered to global.get / global.set or local.get /
// local.set, as appropriate.
WASM_ADDRESS_SPACE_VAR = 1
@@ -38,7 +37,6 @@ inline bool isValidAddressSpace(unsigned AS) {
}
} // namespace WebAssembly
-
} // namespace llvm
-#endif // LLVM_LIB_TARGET_WEBASSEMBLY_UTILS_WASMADDRESSSPACES_H
+#endif // LLVM_SUPPORT_WASMADDRESSSPACES_H
\ No newline at end of file
diff --git a/llvm/lib/Target/WebAssembly/Utils/WebAssemblyTypeUtilities.h b/llvm/lib/Target/WebAssembly/Utils/WebAssemblyTypeUtilities.h
index 47ba91df811615..72342393e410a2 100644
--- a/llvm/lib/Target/WebAssembly/Utils/WebAssemblyTypeUtilities.h
+++ b/llvm/lib/Target/WebAssembly/Utils/WebAssemblyTypeUtilities.h
@@ -16,11 +16,11 @@
#define LLVM_LIB_TARGET_WEBASSEMBLY_UTILS_WEBASSEMBLYTYPEUTILITIES_H
#include "MCTargetDesc/WebAssemblyMCTypeUtilities.h"
-#include "WasmAddressSpaces.h"
#include "llvm/BinaryFormat/Wasm.h"
#include "llvm/CodeGenTypes/MachineValueType.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/MC/MCSymbolWasm.h"
+#include "llvm/Support/WasmAddressSpaces.h"
namespace llvm {
diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyFastISel.cpp b/llvm/lib/Target/WebAssembly/WebAssemblyFastISel.cpp
index db03f5874234c7..075330081b5ca2 100644
--- a/llvm/lib/Target/WebAssembly/WebAssemblyFastISel.cpp
+++ b/llvm/lib/Target/WebAssembly/WebAssemblyFastISel.cpp
@@ -16,7 +16,6 @@
//===----------------------------------------------------------------------===//
#include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
-#include "Utils/WasmAddressSpaces.h"
#include "WebAssemblyMachineFunctionInfo.h"
#include "WebAssemblySubtarget.h"
#include "WebAssemblyUtilities.h"
@@ -33,6 +32,7 @@
#include "llvm/IR/Function.h"
#include "llvm/IR/GetElementPtrTypeIterator.h"
#include "llvm/IR/GlobalVariable.h"
+#include "llvm/Support/WasmAddressSpaces.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/IntrinsicsWebAssembly.h"
#include "llvm/IR/Operator.h"
diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyRefTypeMem2Local.cpp b/llvm/lib/Target/WebAssembly/WebAssemblyRefTypeMem2Local.cpp
index 2c162308700af2..6fdb04760514ed 100644
--- a/llvm/lib/Target/WebAssembly/WebAssemblyRefTypeMem2Local.cpp
+++ b/llvm/lib/Target/WebAssembly/WebAssemblyRefTypeMem2Local.cpp
@@ -12,7 +12,6 @@
///
//===----------------------------------------------------------------------===//
-#include "Utils/WasmAddressSpaces.h"
#include "Utils/WebAssemblyTypeUtilities.h"
#include "WebAssembly.h"
#include "llvm/IR/Analysis.h"
@@ -22,6 +21,7 @@
#include "llvm/IR/PassManager.h"
#include "llvm/IR/ValueHandle.h"
#include "llvm/Pass.h"
+#include "llvm/Support/WasmAddressSpaces.h"
using namespace llvm;
#define DEBUG_TYPE "wasm-ref-type-mem2local"
>From 1e26388f0b91a97e7888ee16de9c2a929a34a0b7 Mon Sep 17 00:00:00 2001
From: Changqing Jing <changqing.jing at bmw.com>
Date: Tue, 8 Sep 2026 15:56:09 +0800
Subject: [PATCH 2/5] Fix
---
clang/lib/CodeGen/Targets/WebAssembly.cpp | 3 +--
clang/lib/Sema/SemaWasm.cpp | 4 ++--
llvm/lib/Target/WebAssembly/WebAssemblyFastISel.cpp | 2 +-
3 files changed, 4 insertions(+), 5 deletions(-)
diff --git a/clang/lib/CodeGen/Targets/WebAssembly.cpp b/clang/lib/CodeGen/Targets/WebAssembly.cpp
index f51edfc620baa4..05f2391dc7e381 100644
--- a/clang/lib/CodeGen/Targets/WebAssembly.cpp
+++ b/clang/lib/CodeGen/Targets/WebAssembly.cpp
@@ -59,8 +59,7 @@ class WebAssemblyTargetCodeGenInfo final : public TargetCodeGenInfo {
LangAS getGlobalVarAddressSpace(CodeGenModule &CGM,
const VarDecl *D) const override {
if (D && D->hasAttr<WebAssemblyGlobalAttr>())
- return getLangASFromTargetAS(
- llvm::WebAssembly::WASM_ADDRESS_SPACE_VAR);
+ return getLangASFromTargetAS(llvm::WebAssembly::WASM_ADDRESS_SPACE_VAR);
return TargetCodeGenInfo::getGlobalVarAddressSpace(CGM, D);
}
diff --git a/clang/lib/Sema/SemaWasm.cpp b/clang/lib/Sema/SemaWasm.cpp
index d0fae91fa47279..42c8957585ea07 100644
--- a/clang/lib/Sema/SemaWasm.cpp
+++ b/clang/lib/Sema/SemaWasm.cpp
@@ -462,8 +462,8 @@ void SemaWasm::handleWebAssemblyGlobalAttr(Decl *D, const ParsedAttr &AL) {
ASTContext &Context = getASTContext();
auto *VD = cast<VarDecl>(D);
VD->setType(Context.getAddrSpaceQualType(
- VD->getType(), getLangASFromTargetAS(
- llvm::WebAssembly::WASM_ADDRESS_SPACE_VAR)));
+ VD->getType(),
+ getLangASFromTargetAS(llvm::WebAssembly::WASM_ADDRESS_SPACE_VAR)));
D->addAttr(::new (Context) WebAssemblyGlobalAttr(Context, AL));
}
diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyFastISel.cpp b/llvm/lib/Target/WebAssembly/WebAssemblyFastISel.cpp
index 075330081b5ca2..3eea32411a808a 100644
--- a/llvm/lib/Target/WebAssembly/WebAssemblyFastISel.cpp
+++ b/llvm/lib/Target/WebAssembly/WebAssemblyFastISel.cpp
@@ -32,10 +32,10 @@
#include "llvm/IR/Function.h"
#include "llvm/IR/GetElementPtrTypeIterator.h"
#include "llvm/IR/GlobalVariable.h"
-#include "llvm/Support/WasmAddressSpaces.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/IntrinsicsWebAssembly.h"
#include "llvm/IR/Operator.h"
+#include "llvm/Support/WasmAddressSpaces.h"
using namespace llvm;
>From 7a185fc6415cb47b46b5514a3f6262d99f61ca7f Mon Sep 17 00:00:00 2001
From: Changqing Jing <changqing.jing at bmw.com>
Date: Tue, 8 Sep 2026 17:14:35 +0800
Subject: [PATCH 3/5] Fix
---
clang/test/Misc/pragma-attribute-supported-attributes-list.test | 1 +
1 file changed, 1 insertion(+)
diff --git a/clang/test/Misc/pragma-attribute-supported-attributes-list.test b/clang/test/Misc/pragma-attribute-supported-attributes-list.test
index 8f8c1aa3d3f9cd..2285f3602b863d 100644
--- a/clang/test/Misc/pragma-attribute-supported-attributes-list.test
+++ b/clang/test/Misc/pragma-attribute-supported-attributes-list.test
@@ -236,6 +236,7 @@
// CHECK-NEXT: Weak (SubjectMatchRule_variable, SubjectMatchRule_function, SubjectMatchRule_record)
// CHECK-NEXT: WeakRef (SubjectMatchRule_variable, SubjectMatchRule_function)
// CHECK-NEXT: WebAssemblyExportName (SubjectMatchRule_function, SubjectMatchRule_variable_is_global)
+// CHECK-NEXT: WebAssemblyGlobal (SubjectMatchRule_variable_is_global)
// CHECK-NEXT: WebAssemblyImportModule (SubjectMatchRule_function, SubjectMatchRule_variable_is_global)
// CHECK-NEXT: WebAssemblyImportName (SubjectMatchRule_function, SubjectMatchRule_variable_is_global)
// CHECK-NEXT: WorkGroupSizeHint (SubjectMatchRule_function)
>From 0da0bbc3d8a831df0cdc68164eddeb1319b9dcd1 Mon Sep 17 00:00:00 2001
From: Changqing Jing <changqing.jing at bmw.com>
Date: Sat, 10 Oct 2026 15:33:52 +0800
Subject: [PATCH 4/5] Fix review
---
clang/include/clang/Basic/DiagnosticSemaKinds.td | 2 ++
clang/lib/CodeGen/Targets/WebAssembly.cpp | 8 --------
clang/lib/Sema/SemaWasm.cpp | 4 ++++
clang/test/CodeGen/WebAssembly/wasm-global-attribute.cpp | 8 ++++++--
4 files changed, 12 insertions(+), 10 deletions(-)
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 70f25c78a6f4a9..f2cae681610fd3 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -14023,6 +14023,8 @@ def err_builtin_pass_in_regs_non_class : Error<
// WebAssembly reference type and table diagnostics.
+def err_wasm_global_with_address_space : Error<
+ "'wasm_global' attribute cannot be used with an explicit address space">;
def err_wasm_reference_pr : Error<
"%select{pointer|reference}0 to WebAssembly reference type is not allowed">;
def err_wasm_ca_reference : Error<
diff --git a/clang/lib/CodeGen/Targets/WebAssembly.cpp b/clang/lib/CodeGen/Targets/WebAssembly.cpp
index 05f2391dc7e381..8f6f58bfb807e8 100644
--- a/clang/lib/CodeGen/Targets/WebAssembly.cpp
+++ b/clang/lib/CodeGen/Targets/WebAssembly.cpp
@@ -9,7 +9,6 @@
#include "ABIInfoImpl.h"
#include "TargetInfo.h"
#include "clang/Basic/DiagnosticFrontend.h"
-#include "llvm/Support/WasmAddressSpaces.h"
using namespace clang;
using namespace clang::CodeGen;
@@ -56,13 +55,6 @@ class WebAssemblyTargetCodeGenInfo final : public TargetCodeGenInfo {
std::make_unique<SwiftABIInfo>(CGT, /*SwiftErrorInRegister=*/false);
}
- LangAS getGlobalVarAddressSpace(CodeGenModule &CGM,
- const VarDecl *D) const override {
- if (D && D->hasAttr<WebAssemblyGlobalAttr>())
- return getLangASFromTargetAS(llvm::WebAssembly::WASM_ADDRESS_SPACE_VAR);
- return TargetCodeGenInfo::getGlobalVarAddressSpace(CGM, D);
- }
-
void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
CodeGen::CodeGenModule &CGM) const override {
TargetCodeGenInfo::setTargetAttributes(D, GV, CGM);
diff --git a/clang/lib/Sema/SemaWasm.cpp b/clang/lib/Sema/SemaWasm.cpp
index 42c8957585ea07..67853b72c75c91 100644
--- a/clang/lib/Sema/SemaWasm.cpp
+++ b/clang/lib/Sema/SemaWasm.cpp
@@ -461,6 +461,10 @@ void SemaWasm::handleWebAssemblyExportNameAttr(Decl *D, const ParsedAttr &AL) {
void SemaWasm::handleWebAssemblyGlobalAttr(Decl *D, const ParsedAttr &AL) {
ASTContext &Context = getASTContext();
auto *VD = cast<VarDecl>(D);
+ if (VD->getType().getAddressSpace() != LangAS::Default) {
+ Diag(AL.getLoc(), diag::err_wasm_global_with_address_space);
+ return;
+ }
VD->setType(Context.getAddrSpaceQualType(
VD->getType(),
getLangASFromTargetAS(llvm::WebAssembly::WASM_ADDRESS_SPACE_VAR)));
diff --git a/clang/test/CodeGen/WebAssembly/wasm-global-attribute.cpp b/clang/test/CodeGen/WebAssembly/wasm-global-attribute.cpp
index e61056d9ba8481..e83cfdd1855bfb 100644
--- a/clang/test/CodeGen/WebAssembly/wasm-global-attribute.cpp
+++ b/clang/test/CodeGen/WebAssembly/wasm-global-attribute.cpp
@@ -1,7 +1,10 @@
// RUN: %clang_cc1 -triple wasm32-unknown-unknown-wasm -emit-llvm -o - %s | FileCheck %s
extern "C" {
-int exported_g [[clang::wasm_global]] = 42;
+int exported_g [[clang::wasm_global]] = 0;
+
+extern int redeclared_g [[clang::wasm_global]];
+int redeclared_g = 0;
extern const int imported_g [[clang::wasm_global]]
__attribute__((import_module("env"), import_name("imported_g")));
@@ -9,7 +12,8 @@ extern const int imported_g [[clang::wasm_global]]
int get_import(void) { return imported_g; }
}
-// CHECK: @exported_g = addrspace(1) global i32 42, align 4
+// CHECK: @exported_g = addrspace(1) global i32 0, align 4
+// CHECK: @redeclared_g = addrspace(1) global i32 0, align 4
// CHECK: @imported_g = external addrspace(1) constant i32, align 4 #0
// CHECK: define{{.*}} @get_import()
// CHECK: load i32, ptr addrspace(1) @imported_g
>From 79c6dd29a17b80464eccfec211fccad6d174176f Mon Sep 17 00:00:00 2001
From: Changqing Jing <changqing.jing at bmw.com>
Date: Sat, 10 Oct 2026 15:41:02 +0800
Subject: [PATCH 5/5] fix review
---
lld/test/wasm/global-symbol-type-mismatch.ll | 29 ++++++++++++++++++++
1 file changed, 29 insertions(+)
create mode 100644 lld/test/wasm/global-symbol-type-mismatch.ll
diff --git a/lld/test/wasm/global-symbol-type-mismatch.ll b/lld/test/wasm/global-symbol-type-mismatch.ll
new file mode 100644
index 00000000000000..a83c5e2bb43dcb
--- /dev/null
+++ b/lld/test/wasm/global-symbol-type-mismatch.ll
@@ -0,0 +1,29 @@
+; RUN: split-file %s %t
+; RUN: llc -mtriple=wasm32-unknown-unknown -filetype=obj %t/use-global.ll -o %t/use-global.o
+; RUN: llc -mtriple=wasm32-unknown-unknown -filetype=obj %t/define-global.ll -o %t/define-global.o
+; RUN: llc -mtriple=wasm32-unknown-unknown -filetype=obj %t/define-data.ll -o %t/define-data.o
+; RUN: wasm-ld --no-entry --export=read_global %t/use-global.o %t/define-global.o -o %t/matching.wasm
+; RUN: not wasm-ld --no-entry --export=read_global %t/use-global.o %t/define-data.o -o %t/mismatch.wasm 2>&1 | FileCheck %s --check-prefix=GLOBAL-FIRST
+; RUN: not wasm-ld --no-entry --export=read_global %t/define-data.o %t/use-global.o -o %t/mismatch.wasm 2>&1 | FileCheck %s --check-prefix=DATA-FIRST
+
+; GLOBAL-FIRST: error: symbol type mismatch: storage_global
+; GLOBAL-FIRST-NEXT: >>> defined as WASM_SYMBOL_TYPE_GLOBAL in {{.*}}use-global.o
+; GLOBAL-FIRST-NEXT: >>> defined as WASM_SYMBOL_TYPE_DATA in {{.*}}define-data.o
+
+; DATA-FIRST: error: symbol type mismatch: storage_global
+; DATA-FIRST-NEXT: >>> defined as WASM_SYMBOL_TYPE_DATA in {{.*}}define-data.o
+; DATA-FIRST-NEXT: >>> defined as WASM_SYMBOL_TYPE_GLOBAL in {{.*}}use-global.o
+
+;--- use-global.ll
+ at storage_global = external addrspace(1) global i32
+
+define i32 @read_global() {
+ %value = load i32, ptr addrspace(1) @storage_global, align 4
+ ret i32 %value
+}
+
+;--- define-global.ll
+ at storage_global = addrspace(1) global i32 0, align 4
+
+;--- define-data.ll
+ at storage_global = global i32 0, align 4
\ No newline at end of file
More information about the cfe-commits
mailing list