From 814cf304356b38aca149f774fd7f7c8d0dd2fce6 Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Wed, 11 Nov 2026 20:34:13 +0000 Subject: [PATCH] wasm32: the interface accessor cell tag is bit 30, so the vtable's relocation fits A field a class fills with accessors has, in an interface's vtable, the address of a cell of accessors plus a tag - the sign bit, which an offset never has. A wasm object keeps a relocation's addend as a signed 32-bit LEB, and an address plus 2^31 is out of its range: wasm-ld (and any reader of the object, llvm-objdump too) stopped with "LEB is outside Varint32 range". On wasm32 the tag is now bit 30: a wasm32 program's static data is at the bottom of its linear memory, far below 1 GB, and no offset into an object reaches 1 GB either. A slot is a cell when the tag bit is set - on every other target the sign bit, as before. 00class_accessor_implements_field, 00interface_field_getter_owned, 00class_accessor_increment and 00class_redeclared_field now run on wasm32. Co-Authored-By: Claude Opus 5.5 --- docs/how/wasm/test/wasm_skips.txt | 4 ---- tslang/include/TypeScript/Defines.h | 7 ++++++- tslang/lib/TypeScript/LowerToLLVM.cpp | 15 +++++++++------ tslang/lib/TypeScript/MLIRGenClasses.cpp | 4 +++- 4 files changed, 18 insertions(+), 12 deletions(-) diff --git a/docs/how/wasm/test/wasm_skips.txt b/docs/how/wasm/test/wasm_skips.txt index bdd46a316..c39d4a6f6 100644 --- a/docs/how/wasm/test/wasm_skips.txt +++ b/docs/how/wasm/test/wasm_skips.txt @@ -11,10 +11,6 @@ internals.ts # inline_asm with x86 instructions fetch.ts # (default library) fetch(): the host has no network, so the request throws # compiler gaps on wasm, each to be fixed and taken off this list -00class_accessor_implements_field.ts wasm32 # interface accessor cell tag (address + 0x80000000) folded into an i32.const: wasm-ld "LEB is outside Varint32 range" -00interface_field_getter_owned.ts wasm32 # interface accessor cell tag (address + 0x80000000) folded into an i32.const: wasm-ld "LEB is outside Varint32 range" -00class_accessor_increment.ts wasm32 # the same accessor cell tag (--di --opt_level=0 programs) -00class_redeclared_field.ts wasm32 # the same accessor cell tag (--di --opt_level=0 programs) 00generator3.ts wasm64 # wasm-ld: invalid data relocation for an object literal's generator method `next` 01iterator.ts wasm64 # wasm-ld: invalid data relocation for an object literal's generator method `next` 00owned_generator_locals.ts wasm64 # wasm-ld: invalid data relocation for a generator's `next` (under none) diff --git a/tslang/include/TypeScript/Defines.h b/tslang/include/TypeScript/Defines.h index e73aae994..e9ecd17b3 100644 --- a/tslang/include/TypeScript/Defines.h +++ b/tslang/include/TypeScript/Defines.h @@ -253,7 +253,12 @@ // the class has none, plus this bit - the sign bit, which an offset never has (a symbol plus a constant, so it // is relocated as any address). A load through it calls the getter with `this`, and a store the setter // (LoadOpLowering, StoreOpLowering). On a 32-bit target this holds for a cell below 2 GB. -#define INTERFACE_ACCESSOR_CELL_TAG(sizeBits) (1ULL << ((sizeBits) - 1)) +// On wasm32 it is bit 30 instead: a wasm object keeps a relocation's addend as a signed 32-bit LEB, and the +// sign bit added to an address is out of its range ("LEB is outside Varint32 range", in wasm-ld and in any +// reader of the object). A wasm32 program's static data is at the bottom of its linear memory, far below 1 GB, +// and no offset into an object reaches 1 GB either. +#define INTERFACE_ACCESSOR_CELL_TAG(sizeBits, isWasm) \ + ((isWasm) && (sizeBits) == 32 ? (1ULL << 30) : (1ULL << ((sizeBits) - 1))) // on an InterfaceSymbolRefOp of a `v?: T` field that is stored to: true sets the flag of a tagged slot's // field, false (a store of undefined) clears it #define INTERFACE_OPTIONAL_WRITE_ATTR "__optional_write" diff --git a/tslang/lib/TypeScript/LowerToLLVM.cpp b/tslang/lib/TypeScript/LowerToLLVM.cpp index 205a82d7e..c6d06933d 100644 --- a/tslang/lib/TypeScript/LowerToLLVM.cpp +++ b/tslang/lib/TypeScript/LowerToLLVM.cpp @@ -597,7 +597,7 @@ struct InterfaceAccessorCell mlir::Value thisVal; mlir::Value slotValue; mlir::Type intPtrType; - unsigned sizeBits; + llvm::APInt tag; static std::optional of(mlir::Value reference, ConversionPatternRewriter &rewriter, const CompileOptions &compileOptions) @@ -621,9 +621,13 @@ struct InterfaceAccessorCell auto sizeBits = compileOptions.sizeBits(); auto intPtrType = rewriter.getIntegerType(sizeBits); auto slotValue = rewriter.create(loc, intPtrType, slotPtr); + // tagged: the tag bit set (on most targets the sign bit, so this is `slot < 0`) + auto tag = llvm::APInt(sizeBits, INTERFACE_ACCESSOR_CELL_TAG(sizeBits, compileOptions.isWasm)); + auto tagValue = rewriter.create(loc, intPtrType, rewriter.getIntegerAttr(intPtrType, tag)); auto zero = rewriter.create(loc, intPtrType, rewriter.getIntegerAttr(intPtrType, 0)); - auto isCell = rewriter.create(loc, LLVM::ICmpPredicate::slt, slotValue, zero); - return InterfaceAccessorCell{isCell, thisVal, slotValue, intPtrType, static_cast(sizeBits)}; + auto tagBit = rewriter.create(loc, intPtrType, slotValue, tagValue); + auto isCell = rewriter.create(loc, LLVM::ICmpPredicate::ne, tagBit, zero); + return InterfaceAccessorCell{isCell, thisVal, slotValue, intPtrType, tag}; } // the block a load or a store in a `try` unwinds to: the AccessorUnwindEdgeOp after it says @@ -665,9 +669,8 @@ struct InterfaceAccessorCell mlir::Value accessor(mlir::Location loc, ConversionPatternRewriter &rewriter, int index) { TypeHelper th(rewriter); - auto tag = rewriter.create( - loc, intPtrType, rewriter.getIntegerAttr(intPtrType, llvm::APInt::getSignMask(sizeBits))); - auto cellInt = rewriter.create(loc, intPtrType, slotValue, tag); + auto tagValue = rewriter.create(loc, intPtrType, rewriter.getIntegerAttr(intPtrType, tag)); + auto cellInt = rewriter.create(loc, intPtrType, slotValue, tagValue); auto cellAddr = rewriter.create(loc, th.getPtrType(), cellInt.getResult()); auto cellLLVMType = LLVM::LLVMStructType::getLiteral(rewriter.getContext(), {th.getPtrType(), th.getPtrType()}, false); auto accessorPtr = rewriter.create(loc, th.getPtrType(), cellLLVMType, cellAddr, diff --git a/tslang/lib/TypeScript/MLIRGenClasses.cpp b/tslang/lib/TypeScript/MLIRGenClasses.cpp index a9a724081..1293cabdb 100644 --- a/tslang/lib/TypeScript/MLIRGenClasses.cpp +++ b/tslang/lib/TypeScript/MLIRGenClasses.cpp @@ -4015,7 +4015,9 @@ genContext); auto cellAddress = builder.create(location, ptrIntType, cellRef); auto tagValue = builder.create( location, ptrIntType, - builder.getIntegerAttr(ptrIntType, llvm::APInt::getSignMask(compileOptions.sizeBits()))); + builder.getIntegerAttr(ptrIntType, llvm::APInt(compileOptions.sizeBits(), + INTERFACE_ACCESSOR_CELL_TAG(compileOptions.sizeBits(), + compileOptions.isWasm)))); auto taggedValue = builder.create( location, ptrIntType, builder.getI32IntegerAttr((int)SyntaxKind::PlusToken), cellAddress, tagValue); auto slotValue = builder.create(