[clang] [lld] [llvm] [WebAssembly] Emit constant initializers for addrspace(1) globals (PR #212007)

Yuta Saito via cfe-commits cfe-commits at lists.llvm.org
Tue Jul 28 02:31:19 PDT 2026


================
@@ -185,6 +186,26 @@ MCSymbolWasm *WebAssemblyAsmPrinter::getMCSymbolForFunction(
   return WasmSym;
 }
 
+static std::optional<uint64_t> getWasmGlobalInitBits(const Constant *C) {
+  // Sub-i32 ints and half/bfloat legalize to a single i32/f32 MVT and so pass
+  // wasmSymbolSetType(), but their bits are not an i32/f32 init value; gate on
+  // the exact IR type so they fall back to the default 0 rather than emit a
+  // garbage init expr.
+  if (const auto *CI = dyn_cast<ConstantInt>(C)) {
+    unsigned BW = CI->getType()->getIntegerBitWidth();
+    if (BW == 32 || BW == 64)
+      return CI->getValue().getZExtValue();
+    return std::nullopt;
+  }
+  if (const auto *CF = dyn_cast<ConstantFP>(C)) {
+    Type *T = CF->getType();
+    if (T->isFloatTy() || T->isDoubleTy())
+      return CF->getValueAPF().bitcastToAPInt().getZExtValue();
+    return std::nullopt;
+  }
+  return std::nullopt;
----------------
kateinoigakukun wrote:

If the expr is not supported form, we should not silently ignore it and just fallback to zero (writer side emits 0 when nullopt regardless if the nullopt comes from no expr or invalid expr).
I think we need to emit hard diagnostics. 

https://github.com/llvm/llvm-project/pull/212007


More information about the cfe-commits mailing list