[llvm] [WebAssembly] Fix lowering of (extending) loads from addrspace(1) globals (PR #155937)
Demetrius Kanios via llvm-commits
llvm-commits at lists.llvm.org
Thu Apr 9 16:35:11 PDT 2026
================
@@ -1850,16 +1879,94 @@ SDValue WebAssemblyTargetLowering::LowerLoad(SDValue Op,
LoadSDNode *LN = cast<LoadSDNode>(Op.getNode());
const SDValue &Base = LN->getBasePtr();
const SDValue &Offset = LN->getOffset();
+ ISD::LoadExtType ExtType = LN->getExtensionType();
+ EVT ResultType = LN->getValueType(0);
if (IsWebAssemblyGlobal(Base)) {
if (!Offset->isUndef())
report_fatal_error(
"unexpected offset when loading from webassembly global", false);
- SDVTList Tys = DAG.getVTList(LN->getValueType(0), MVT::Other);
- SDValue Ops[] = {LN->getChain(), Base};
- return DAG.getMemIntrinsicNode(WebAssemblyISD::GLOBAL_GET, DL, Tys, Ops,
- LN->getMemoryVT(), LN->getMemOperand());
+ if (!ResultType.isInteger() && !ResultType.isFloatingPoint()) {
+ SDVTList Tys = DAG.getVTList(ResultType, MVT::Other);
+ SDValue Ops[] = {LN->getChain(), Base};
+ SDValue GlobalGetNode =
+ DAG.getMemIntrinsicNode(WebAssemblyISD::GLOBAL_GET, DL, Tys, Ops,
+ LN->getMemoryVT(), LN->getMemOperand());
+ return GlobalGetNode;
+ }
+
+ EVT GT = MVT::INVALID_SIMPLE_VALUE_TYPE;
+
+ if (auto *GA = dyn_cast<GlobalAddressSDNode>(
+ Base->getOpcode() == WebAssemblyISD::Wrapper ? Base->getOperand(0)
+ : Base))
+ GT = EVT::getEVT(GA->getGlobal()->getValueType());
+
+ if (GT != MVT::i8 && GT != MVT::i16 && GT != MVT::i32 && GT != MVT::i64 &&
+ GT != MVT::f32 && GT != MVT::f64)
+ report_fatal_error("encountered unexpected global type for Base when "
+ "loading from webassembly global",
+ false);
+
+ EVT PromotedGT = getTypeToTransformTo(*DAG.getContext(), GT);
+
+ switch (ExtType) {
+ case ISD::NON_EXTLOAD: {
+ // A normal, non-extending load may try to load more or less than the
+ // underlying global, which is invalid. We lower this to a load of the
+ // global (i32 or i64) then truncate or extend as needed
+
+ // Modify the MMO to load the full global
+ // This is assumed to be safe without copy/dup, as the original load will
+ // be removed
+ MachineMemOperand *MMO = LN->getMemOperand();
+ MMO->setType(LLT(PromotedGT.getSimpleVT()));
+
+ SDVTList Tys = DAG.getVTList(PromotedGT, MVT::Other);
+ SDValue Ops[] = {LN->getChain(), Base};
+ SDValue GlobalGetNode = DAG.getMemIntrinsicNode(
+ WebAssemblyISD::GLOBAL_GET, DL, Tys, Ops, PromotedGT, MMO);
+
+ if (ResultType.bitsEq(PromotedGT)) {
+ return GlobalGetNode;
+ }
+
+ SDValue ValRes;
+ if (ResultType.isFloatingPoint())
+ ValRes = DAG.getFPExtendOrRound(GlobalGetNode, DL, ResultType);
----------------
QuantumSegfault wrote:
> What could cause an i16 load from an i64 global? Is this just because LLVM IR itself allows loading an amount of bits that mismatches the size of the LLVM global?
Not just LLVM IR. You can also abuse casts in Clang.
```
typedef short int16_t;
typedef long long int64_t;
#define WASM_GLOBAL __attribute__((__address_space__(1)))
WASM_GLOBAL int64_t myGlobal = 0;
int16_t test() {
return *(WASM_GLOBAL int16_t*)&myGlobal;
}
```
> Yes, bag of bits is what I'm suggesting.
See the latest commit. I already made the change.
> Trying to load more bits than the size of the global would be invalid IR, right? I think we should assert or something there rather than trying to handle that.
Not at all invalid IR. Wasm globals aside, this is perfectly valid for linear memory access:
```ll
@myGlobal = hidden global i32 0, align 4
; Function Attrs: noinline nounwind optnone
define hidden i64 @test() #0 {
%1 = load i64, ptr @myGlobal, align 4
ret i64 %1
}
```
Ill advised and UB, but possible. Same applies to address space 1 (or any others).
Since it's UB, I arbitrarily decided to pad it out with zeros. Though an argument can be made to just disallow it (including trapping on attempts to do so).
> If we left the case (e.g. of trying to load 64 bits from an i32 global) unhandled specifically, we might end up e.g. generating an i32-typed global.get in a stack context that wanted an i64 and fail validation.
Which is the exact reason why I'm comparing the loads against the type of the global and making sure to always load the actual type of the global (whatever it expands to in transformation to Wasm), and then coerce it to the desired type.
https://github.com/llvm/llvm-project/pull/155937
More information about the llvm-commits
mailing list