Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions docs/how/wasm/test/wasm_skips.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 6 additions & 1 deletion tslang/include/TypeScript/Defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
15 changes: 9 additions & 6 deletions tslang/lib/TypeScript/LowerToLLVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ struct InterfaceAccessorCell
mlir::Value thisVal;
mlir::Value slotValue;
mlir::Type intPtrType;
unsigned sizeBits;
llvm::APInt tag;

static std::optional<InterfaceAccessorCell> of(mlir::Value reference, ConversionPatternRewriter &rewriter,
const CompileOptions &compileOptions)
Expand All @@ -621,9 +621,13 @@ struct InterfaceAccessorCell
auto sizeBits = compileOptions.sizeBits();
auto intPtrType = rewriter.getIntegerType(sizeBits);
auto slotValue = rewriter.create<LLVM::PtrToIntOp>(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<LLVM::ConstantOp>(loc, intPtrType, rewriter.getIntegerAttr(intPtrType, tag));
auto zero = rewriter.create<LLVM::ConstantOp>(loc, intPtrType, rewriter.getIntegerAttr(intPtrType, 0));
auto isCell = rewriter.create<LLVM::ICmpOp>(loc, LLVM::ICmpPredicate::slt, slotValue, zero);
return InterfaceAccessorCell{isCell, thisVal, slotValue, intPtrType, static_cast<unsigned>(sizeBits)};
auto tagBit = rewriter.create<LLVM::AndOp>(loc, intPtrType, slotValue, tagValue);
auto isCell = rewriter.create<LLVM::ICmpOp>(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
Expand Down Expand Up @@ -665,9 +669,8 @@ struct InterfaceAccessorCell
mlir::Value accessor(mlir::Location loc, ConversionPatternRewriter &rewriter, int index)
{
TypeHelper th(rewriter);
auto tag = rewriter.create<LLVM::ConstantOp>(
loc, intPtrType, rewriter.getIntegerAttr(intPtrType, llvm::APInt::getSignMask(sizeBits)));
auto cellInt = rewriter.create<LLVM::SubOp>(loc, intPtrType, slotValue, tag);
auto tagValue = rewriter.create<LLVM::ConstantOp>(loc, intPtrType, rewriter.getIntegerAttr(intPtrType, tag));
auto cellInt = rewriter.create<LLVM::SubOp>(loc, intPtrType, slotValue, tagValue);
auto cellAddr = rewriter.create<LLVM::IntToPtrOp>(loc, th.getPtrType(), cellInt.getResult());
auto cellLLVMType = LLVM::LLVMStructType::getLiteral(rewriter.getContext(), {th.getPtrType(), th.getPtrType()}, false);
auto accessorPtr = rewriter.create<LLVM::GEPOp>(loc, th.getPtrType(), cellLLVMType, cellAddr,
Expand Down
4 changes: 3 additions & 1 deletion tslang/lib/TypeScript/MLIRGenClasses.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4015,7 +4015,9 @@ genContext);
auto cellAddress = builder.create<mlir_ts::CastOp>(location, ptrIntType, cellRef);
auto tagValue = builder.create<mlir_ts::ConstantOp>(
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<mlir_ts::ArithmeticBinaryOp>(
location, ptrIntType, builder.getI32IntegerAttr((int)SyntaxKind::PlusToken), cellAddress, tagValue);
auto slotValue = builder.create<mlir_ts::CastOp>(
Expand Down