From 0e42c475dc5aba813bf752696f6055bcd3c8eca0 Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Thu, 5 Nov 2026 15:27:32 +0000 Subject: [PATCH 1/5] Atomic builtins: constant arguments checked at the call; atomicload, atomicstore, volatile and weak The op code, orderings, syncscope, alignment and flags of atomicrmw, cmpxchg and fence become op attributes, so each has to be a constant. A variable one crashed tslang (the op was built with a null attribute), a constant LLVM doesn't take (op code 99, an atomicrmw ordering below monotonic, a cmpxchg failure ordering of release) was reported only by LLVM's verifier at :0, and a wrong argument count said just "failed statement". Each is now an error at the call, naming what the argument may be or the builtin's signature. New builtins atomicload(ref, ordering[, syncscope[, alignment[, volatile]]]) and atomicstore(ref, value, ordering[, ...]) lower to LLVM `load atomic` / `store atomic`, aligned naturally unless an alignment is given. atomicrmw and the new two take a trailing volatile flag, cmpxchg weak and volatile ones. The value (and cmpxchg's expected value) is cast to the type the reference points at: a literal added to a u16 is a 16-bit atomicrmw, not a 32-bit one over the element and its neighbour. The reference's type is checked against the operation (integer ops, float ops, cmpxchg integer or pointer, no boolean). Tests: tests/00atomic_builtins.ts, atomic-errors/*.ts. Co-Authored-By: Claude Opus 5.5 --- .../TypeScript/MLIRLogic/MLIRCodeLogic.h | 535 ++++++++++++++++-- tslang/include/TypeScript/TypeScriptOps.td | 22 + tslang/lib/TypeScript/LowerToAffineLoops.cpp | 2 +- tslang/lib/TypeScript/LowerToLLVM.cpp | 60 +- tslang/test/tester/atomic-errors/alignment.ts | 8 + .../tester/atomic-errors/boolean_reference.ts | 8 + .../tester/atomic-errors/cmpxchg_arguments.ts | 8 + .../atomic-errors/cmpxchg_failure_ordering.ts | 8 + .../tester/atomic-errors/cmpxchg_float.ts | 8 + .../tester/atomic-errors/fence_ordering.ts | 7 + .../tester/atomic-errors/fence_variable.ts | 7 + .../tester/atomic-errors/load_ordering.ts | 8 + .../tester/atomic-errors/not_a_reference.ts | 8 + tslang/test/tester/atomic-errors/op_range.ts | 8 + .../test/tester/atomic-errors/op_variable.ts | 8 + .../tester/atomic-errors/ordering_hole.ts | 8 + .../tester/atomic-errors/rmw_arguments.ts | 8 + .../atomic-errors/rmw_float_op_on_int.ts | 8 + .../atomic-errors/rmw_int_op_on_float.ts | 8 + .../test/tester/atomic-errors/rmw_ordering.ts | 8 + .../tester/atomic-errors/store_ordering.ts | 8 + .../tester/atomic-errors/volatile_variable.ts | 8 + tslang/test/tester/tests/00atomic_builtins.ts | 70 +++ 23 files changed, 790 insertions(+), 41 deletions(-) create mode 100644 tslang/test/tester/atomic-errors/alignment.ts create mode 100644 tslang/test/tester/atomic-errors/boolean_reference.ts create mode 100644 tslang/test/tester/atomic-errors/cmpxchg_arguments.ts create mode 100644 tslang/test/tester/atomic-errors/cmpxchg_failure_ordering.ts create mode 100644 tslang/test/tester/atomic-errors/cmpxchg_float.ts create mode 100644 tslang/test/tester/atomic-errors/fence_ordering.ts create mode 100644 tslang/test/tester/atomic-errors/fence_variable.ts create mode 100644 tslang/test/tester/atomic-errors/load_ordering.ts create mode 100644 tslang/test/tester/atomic-errors/not_a_reference.ts create mode 100644 tslang/test/tester/atomic-errors/op_range.ts create mode 100644 tslang/test/tester/atomic-errors/op_variable.ts create mode 100644 tslang/test/tester/atomic-errors/ordering_hole.ts create mode 100644 tslang/test/tester/atomic-errors/rmw_arguments.ts create mode 100644 tslang/test/tester/atomic-errors/rmw_float_op_on_int.ts create mode 100644 tslang/test/tester/atomic-errors/rmw_int_op_on_float.ts create mode 100644 tslang/test/tester/atomic-errors/rmw_ordering.ts create mode 100644 tslang/test/tester/atomic-errors/store_ordering.ts create mode 100644 tslang/test/tester/atomic-errors/volatile_variable.ts create mode 100644 tslang/test/tester/tests/00atomic_builtins.ts diff --git a/tslang/include/TypeScript/MLIRLogic/MLIRCodeLogic.h b/tslang/include/TypeScript/MLIRLogic/MLIRCodeLogic.h index 88a980e77..e5c17a566 100644 --- a/tslang/include/TypeScript/MLIRLogic/MLIRCodeLogic.h +++ b/tslang/include/TypeScript/MLIRLogic/MLIRCodeLogic.h @@ -403,7 +403,7 @@ class MLIRCustomMethods static std::map m { {"print", true}, {"convertf", true}, {"assert", true}, {"parseInt", true}, {"parseFloat", true}, {"isNaN", true}, {"sizeof", true}, {GENERATOR_SWITCHSTATE, true}, {"LoadLibraryPermanently", true}, { "SearchForAddressOfSymbol", true }, { "LoadReference", true }, { "ReferenceOf", true }, { "Deref", true }, { "Ref", true }, - {"atomicrmw", true}, {"cmpxchg", true}, {"fence", true}, {"inline_asm", true}, {"call_intrinsic", true}, {"linker_options", true} + {"atomicrmw", true}, {"cmpxchg", true}, {"atomicload", true}, {"atomicstore", true}, {"fence", true}, {"inline_asm", true}, {"call_intrinsic", true}, {"linker_options", true} }; return m[functionName.str()]; } @@ -413,7 +413,7 @@ class MLIRCustomMethods static std::map m { {"print", true}, {"convertf", true}, {"assert", true}, {"sizeof", true}, {GENERATOR_SWITCHSTATE, true}, {"LoadLibraryPermanently", true}, { "SearchForAddressOfSymbol", true }, { "LoadReference", true }, { "ReferenceOf", true }, { "Deref", true }, { "Ref", true }, - {"atomicrmw", true}, {"cmpxchg", true}, {"fence", true}, {"inline_asm", true}, {"call_intrinsic", true}, {"linker_options", true} + {"atomicrmw", true}, {"cmpxchg", true}, {"atomicload", true}, {"atomicstore", true}, {"fence", true}, {"inline_asm", true}, {"call_intrinsic", true}, {"linker_options", true} }; return m[functionName.str()]; } @@ -499,12 +499,20 @@ class MLIRCustomMethods } else if (functionName == "atomicrmw") { - return mlirGenAtomicRMW(location, operands); - } + return mlirGenAtomicRMW(location, operands, castFn, genContext); + } else if (functionName == "cmpxchg") { - return mlirGenCmpXchg(location, operands); - } + return mlirGenCmpXchg(location, operands, castFn, genContext); + } + else if (functionName == "atomicload") + { + return mlirGenAtomicLoad(location, operands); + } + else if (functionName == "atomicstore") + { + return mlirGenAtomicStore(location, operands, castFn, genContext); + } else if (functionName == "fence") { return mlirGenFence(location, operands); @@ -1199,32 +1207,405 @@ class MLIRCustomMethods return V(refValue); } - ValueOrLogicalResult mlirGenAtomicRMW(const mlir::Location &location, ArrayRef operands) + // The atomic builtins. Their op codes, orderings, alignments and flags become attributes of the + // op, so each has to be a constant: a variable used to give the op a null attribute, and + // tslang crashed building it. A constant LLVM does not accept was only caught by LLVM's + // verifier, reported at `:0`. Both are reported here, at the call. + // + // atomicrmw(op, ref, value, ordering[, syncscope[, alignment[, volatile]]]) + // cmpxchg(ref, expected, value, successOrdering, failureOrdering[, syncscope[, alignment[, weak[, volatile]]]]) + // atomicload(ref, ordering[, syncscope[, alignment[, volatile]]]) + // atomicstore(ref, value, ordering[, syncscope[, alignment[, volatile]]]) + // fence(ordering[, syncscope]) + // + // The orderings are LLVM's: 0 not_atomic, 1 unordered, 2 monotonic, 4 acquire, 5 release, + // 6 acq_rel, 7 seq_cst (3 is not one). A syncscope of "" and an alignment of 0 mean the default + // (the natural alignment of the value), so a call that wants a trailing flag passes them. The + // value (and cmpxchg's expected value) is cast to the type the reference points at, which is + // the type the atomic reads and writes. + using AtomicCastFn = std::function; + + static constexpr const char *atomicRMWSignature = "atomicrmw(op, ref, value, ordering[, syncscope[, alignment[, volatile]]])"; + static constexpr const char *cmpXchgSignature = + "cmpxchg(ref, expected, value, successOrdering, failureOrdering[, syncscope[, alignment[, weak[, volatile]]]])"; + static constexpr const char *atomicLoadSignature = "atomicload(ref, ordering[, syncscope[, alignment[, volatile]]])"; + static constexpr const char *atomicStoreSignature = "atomicstore(ref, value, ordering[, syncscope[, alignment[, volatile]]])"; + static constexpr const char *fenceSignature = "fence(ordering[, syncscope])"; + + mlir::Attribute getConstantAttr(mlir::Value oper) + { + if (auto constantOp = oper.getDefiningOp()) + { + return constantOp.getValue(); + } + + if (auto literalType = dyn_cast(oper.getType())) + { + return literalType.getValue(); + } + + return mlir::Attribute(); + } + + bool checkAtomicArgCount(const mlir::Location &location, StringRef signature, size_t size, size_t min, size_t max) + { + if (size >= min && size <= max) + { + return true; + } + + emitError(location) << "expected " << signature << ", but got " << size << " argument" << (size == 1 ? "" : "s"); + return false; + } + + std::optional getAtomicConstInt(const mlir::Location &location, StringRef builtin, StringRef argName, mlir::Value oper) + { + auto value = getConstantAttr(oper); + if (auto intAttr = dyn_cast_or_null(value); intAttr && !isa(value)) + { + return intAttr.getValue().getSExtValue(); + } + + if (auto floatAttr = dyn_cast_or_null(value)) + { + auto floatValue = floatAttr.getValueAsDouble(); + if (floatValue >= -1e9 && floatValue <= 1e9 && floatValue == static_cast(static_cast(floatValue))) + { + return static_cast(floatValue); + } + } + + emitError(location) << builtin << ": the " << argName << " must be a constant integer"; + return std::nullopt; + } + + std::optional getAtomicConstFlag(const mlir::Location &location, StringRef builtin, StringRef argName, mlir::Value oper) + { + if (auto boolAttr = dyn_cast_or_null(getConstantAttr(oper))) + { + return boolAttr.getValue(); + } + + emitError(location) << builtin << ": the " << argName << " flag must be a constant boolean"; + return std::nullopt; + } + + static StringRef atomicOrderingName(int64_t ordering) + { + switch (ordering) + { + case 0: return "not_atomic"; + case 1: return "unordered"; + case 2: return "monotonic"; + case 4: return "acquire"; + case 5: return "release"; + case 6: return "acq_rel"; + case 7: return "seq_cst"; + default: return ""; + } + } + + // `allowed` lists the orderings this argument accepts, in LLVM's numbering + std::optional getAtomicOrdering(const mlir::Location &location, StringRef builtin, StringRef argName, mlir::Value oper, + ArrayRef allowed) + { + auto ordering = getAtomicConstInt(location, builtin, argName, oper); + if (!ordering) + { + return std::nullopt; + } + + if (llvm::is_contained(allowed, *ordering)) + { + return ordering; + } + + auto diag = emitError(location) << builtin << ": the " << argName << " " << *ordering; + auto name = atomicOrderingName(*ordering); + if (!name.empty()) + { + diag << " (" << name << ")"; + } + + diag << " is not allowed here; expected "; + for (auto [index, value] : llvm::enumerate(allowed)) + { + diag << (index == 0 ? "" : index + 1 == allowed.size() ? " or " : ", ") << value << " (" << atomicOrderingName(value) << ")"; + } + + return std::nullopt; + } + + // the optional syncscope ("" is the default, system scope) and alignment (0 is the natural one) + mlir::LogicalResult getAtomicScopeAndAlignment(const mlir::Location &location, StringRef builtin, ArrayRef operands, + size_t scopeIndex, bool hasAlignment, mlir::StringAttr &syncscope, mlir::IntegerAttr &alignment) + { + if (operands.size() > scopeIndex) + { + auto scope = dyn_cast_or_null(getConstantAttr(operands[scopeIndex])); + if (!scope) + { + emitError(location) << builtin << ": the syncscope must be a constant string"; + return mlir::failure(); + } + + if (!scope.getValue().empty()) + { + syncscope = scope; + } + } + + if (hasAlignment && operands.size() > scopeIndex + 1) + { + auto align = getAtomicConstInt(location, builtin, "alignment", operands[scopeIndex + 1]); + if (!align) + { + return mlir::failure(); + } + + if (*align < 0 || (*align & (*align - 1)) != 0) + { + emitError(location) << builtin << ": the alignment " << *align << " is not a power of two (0 is the natural one)"; + return mlir::failure(); + } + + if (*align > 0) + { + alignment = builder.getI64IntegerAttr(*align); + } + } + + return mlir::success(); + } + + mlir::LogicalResult getAtomicFlag(const mlir::Location &location, StringRef builtin, StringRef argName, ArrayRef operands, + size_t index, mlir::UnitAttr &flag) + { + if (operands.size() > index) + { + auto value = getAtomicConstFlag(location, builtin, argName, operands[index]); + if (!value) + { + return mlir::failure(); + } + + if (*value) + { + flag = builder.getUnitAttr(); + } + } + + return mlir::success(); + } + + enum class AtomicValueKind + { + // xchg, atomicload, atomicstore: an integer, a float or a pointer + Any, + // add, sub, and, nand, or, xor, max, min, umax, umin, uinc_wrap, udec_wrap, usub_cond, usub_sat + Integer, + // fadd, fsub, fmax, fmin, fmaximum, fminimum + Float, + // cmpxchg: an integer or a pointer + IntegerOrPointer + }; + + static bool isAtomicIntegerType(mlir::Type type) + { + if (auto intType = dyn_cast(type)) + { + auto width = intType.getWidth(); + return width == 8 || width == 16 || width == 32 || width == 64; + } + + return false; + } + + static bool isAtomicFloatType(mlir::Type type) + { + return isa(type) || type.isF16() || type.isBF16() || type.isF32() || type.isF64(); + } + + // the type the reference points at, which is the type the atomic reads and writes + mlir::Type getAtomicElementType(const mlir::Location &location, StringRef builtin, mlir::Value ref, AtomicValueKind kind) + { + mlir::Type elementType; + if (auto refType = dyn_cast(ref.getType())) + { + elementType = refType.getElementType(); + } + else if (auto valueRefType = dyn_cast(ref.getType())) + { + elementType = valueRefType.getElementType(); + } + else + { + emitError(location) << builtin << ": expected a reference (ReferenceOf(x)), but got " << ref.getType(); + return mlir::Type(); + } + + auto isInteger = isAtomicIntegerType(elementType); + auto isFloat = isAtomicFloatType(elementType); + // a boolean is an i1, which no atomic takes; any other type is a pointer once lowered + auto isOther = !isInteger && !isFloat && !isa(elementType); + auto ok = false; + StringRef expected; + switch (kind) + { + case AtomicValueKind::Any: + ok = isInteger || isFloat || isOther; + expected = "an 8, 16, 32 or 64-bit integer, a float or a reference"; + break; + case AtomicValueKind::Integer: + ok = isInteger; + expected = "an 8, 16, 32 or 64-bit integer"; + break; + case AtomicValueKind::Float: + ok = isFloat; + expected = "a float"; + break; + case AtomicValueKind::IntegerOrPointer: + ok = isInteger || isOther; + expected = "an 8, 16, 32 or 64-bit integer or a reference"; + break; + } + + if (!ok) + { + emitError(location) << builtin << ": the reference points at " << elementType << ", expected " << expected; + return mlir::Type(); + } + + return elementType; + } + + mlir::Value castAtomicValue(const mlir::Location &location, mlir::Type elementType, mlir::Value value, AtomicCastFn castFn, + const GenContext &genContext) { - auto size = operands.size(); - if (size < 4 || size > 6) + if (value.getType() == elementType) + { + return value; + } + + auto result = castFn(location, elementType, value, genContext, false); + if (mlir::failed(result) || !result.value) + { + return mlir::Value(); + } + + return result; + } + + ValueOrLogicalResult mlirGenAtomicRMW(const mlir::Location &location, ArrayRef operands, AtomicCastFn castFn, + const GenContext &genContext) + { + const StringRef builtin = "atomicrmw"; + if (!checkAtomicArgCount(location, atomicRMWSignature, operands.size(), 4, 7)) { return mlir::failure(); } - MLIRCodeLogic mcl(builder, compileOptions); - return V(builder.create(location, - operands[2].getType(), - mcl.getIntegerAttr(location, operands[0]), operands[1], operands[2], mcl.getIntegerAttr(location, operands[3]), - size > 4 ? mcl.getStringAttr(location, operands[4]) : mlir::StringAttr(), - size > 5 ? mcl.getIntegerAttr(location, operands[5], 64) : mlir::IntegerAttr(), - mlir::UnitAttr()/*isVolatile*/)); - } + auto binOp = getAtomicConstInt(location, builtin, "op code", operands[0]); + if (!binOp) + { + return mlir::failure(); + } + + // LLVM's AtomicRMWInst::BinOp + if (*binOp < 0 || *binOp > 20) + { + emitError(location) << builtin << ": the op code " << *binOp + << " is not one of 0 (xchg), 1 (add), 2 (sub), 3 (and), 4 (nand), 5 (or), 6 (xor), 7 (max), " + "8 (min), 9 (umax), 10 (umin), 11 (fadd), 12 (fsub), 13 (fmax), 14 (fmin), 15 (uinc_wrap), " + "16 (udec_wrap), 17 (usub_cond), 18 (usub_sat), 19 (fmaximum) or 20 (fminimum)"; + return mlir::failure(); + } + + auto isFloatOp = (*binOp >= 11 && *binOp <= 14) || *binOp >= 19; + auto kind = *binOp == 0 ? AtomicValueKind::Any : isFloatOp ? AtomicValueKind::Float : AtomicValueKind::Integer; + auto elementType = getAtomicElementType(location, builtin, operands[1], kind); + if (!elementType) + { + return mlir::failure(); + } + + auto ordering = getAtomicOrdering(location, builtin, "ordering", operands[3], {2, 4, 5, 6, 7}); + if (!ordering) + { + return mlir::failure(); + } + + mlir::StringAttr syncscope; + mlir::IntegerAttr alignment; + mlir::UnitAttr isVolatile; + if (mlir::failed(getAtomicScopeAndAlignment(location, builtin, operands, 4, true, syncscope, alignment)) + || mlir::failed(getAtomicFlag(location, builtin, "volatile", operands, 6, isVolatile))) + { + return mlir::failure(); + } + + auto value = castAtomicValue(location, elementType, operands[2], castFn, genContext); + if (!value) + { + return mlir::failure(); + } + + return V(builder.create(location, elementType, builder.getI32IntegerAttr(*binOp), operands[1], value, + builder.getI32IntegerAttr(*ordering), syncscope, alignment, isVolatile)); + } mlir::Type getTupleType(mlir::SmallVector &fieldInfos) { return mlir_ts::TupleType::get(builder.getContext(), fieldInfos); } - ValueOrLogicalResult mlirGenCmpXchg(const mlir::Location &location, ArrayRef operands) + ValueOrLogicalResult mlirGenCmpXchg(const mlir::Location &location, ArrayRef operands, AtomicCastFn castFn, + const GenContext &genContext) { - auto size = operands.size(); - if (size < 5 || size > 7) + const StringRef builtin = "cmpxchg"; + if (!checkAtomicArgCount(location, cmpXchgSignature, operands.size(), 5, 9)) + { + return mlir::failure(); + } + + auto elementType = getAtomicElementType(location, builtin, operands[0], AtomicValueKind::IntegerOrPointer); + if (!elementType) + { + return mlir::failure(); + } + + auto successOrdering = getAtomicOrdering(location, builtin, "success ordering", operands[3], {2, 4, 5, 6, 7}); + if (!successOrdering) + { + return mlir::failure(); + } + + // the failure ordering is that of a load: it can't release + auto failureOrdering = getAtomicOrdering(location, builtin, "failure ordering", operands[4], {2, 4, 7}); + if (!failureOrdering) + { + return mlir::failure(); + } + + mlir::StringAttr syncscope; + mlir::IntegerAttr alignment; + mlir::UnitAttr isWeak; + mlir::UnitAttr isVolatile; + if (mlir::failed(getAtomicScopeAndAlignment(location, builtin, operands, 5, true, syncscope, alignment)) + || mlir::failed(getAtomicFlag(location, builtin, "weak", operands, 7, isWeak)) + || mlir::failed(getAtomicFlag(location, builtin, "volatile", operands, 8, isVolatile))) + { + return mlir::failure(); + } + + auto expected = castAtomicValue(location, elementType, operands[1], castFn, genContext); + if (!expected) + { + return mlir::failure(); + } + + auto value = castAtomicValue(location, elementType, operands[2], castFn, genContext); + if (!value) { return mlir::failure(); } @@ -1233,11 +1614,11 @@ class MLIRCustomMethods fields.push_back( mlir_ts::FieldInfo{ mlir::Attribute(), - operands[2].getType(), + elementType, false, mlir_ts::AccessLevel::Public }); - + fields.push_back( mlir_ts::FieldInfo{ mlir::Attribute(), @@ -1248,36 +1629,112 @@ class MLIRCustomMethods auto resultTuple = getTupleType(fields); - MLIRCodeLogic mcl(builder, compileOptions); - return V(builder.create(location, - resultTuple, operands[0], - operands[1], operands[2], mcl.getIntegerAttr(location, operands[3]), mcl.getIntegerAttr(location, operands[4]), - size > 5 ? mcl.getStringAttr(location, operands[5]) : mlir::StringAttr(), - size > 6 ? mcl.getIntegerAttr(location, operands[6], 64) : mlir::IntegerAttr(), - mlir::UnitAttr()/*Weak*/, - mlir::UnitAttr()/*isVolatile*/)); - } + return V(builder.create(location, resultTuple, operands[0], expected, value, + builder.getI32IntegerAttr(*successOrdering), builder.getI32IntegerAttr(*failureOrdering), syncscope, alignment, + isWeak, isVolatile)); + } + + ValueOrLogicalResult mlirGenAtomicLoad(const mlir::Location &location, ArrayRef operands) + { + const StringRef builtin = "atomicload"; + if (!checkAtomicArgCount(location, atomicLoadSignature, operands.size(), 2, 5)) + { + return mlir::failure(); + } + + auto elementType = getAtomicElementType(location, builtin, operands[0], AtomicValueKind::Any); + if (!elementType) + { + return mlir::failure(); + } + + // a load can't release + auto ordering = getAtomicOrdering(location, builtin, "ordering", operands[1], {1, 2, 4, 7}); + if (!ordering) + { + return mlir::failure(); + } + + mlir::StringAttr syncscope; + mlir::IntegerAttr alignment; + mlir::UnitAttr isVolatile; + if (mlir::failed(getAtomicScopeAndAlignment(location, builtin, operands, 2, true, syncscope, alignment)) + || mlir::failed(getAtomicFlag(location, builtin, "volatile", operands, 4, isVolatile))) + { + return mlir::failure(); + } + + return V(builder.create(location, elementType, operands[0], builder.getI32IntegerAttr(*ordering), + syncscope, alignment, isVolatile)); + } + + ValueOrLogicalResult mlirGenAtomicStore(const mlir::Location &location, ArrayRef operands, AtomicCastFn castFn, + const GenContext &genContext) + { + const StringRef builtin = "atomicstore"; + if (!checkAtomicArgCount(location, atomicStoreSignature, operands.size(), 3, 6)) + { + return mlir::failure(); + } + + auto elementType = getAtomicElementType(location, builtin, operands[0], AtomicValueKind::Any); + if (!elementType) + { + return mlir::failure(); + } + + // a store can't acquire + auto ordering = getAtomicOrdering(location, builtin, "ordering", operands[2], {1, 2, 5, 7}); + if (!ordering) + { + return mlir::failure(); + } + + mlir::StringAttr syncscope; + mlir::IntegerAttr alignment; + mlir::UnitAttr isVolatile; + if (mlir::failed(getAtomicScopeAndAlignment(location, builtin, operands, 3, true, syncscope, alignment)) + || mlir::failed(getAtomicFlag(location, builtin, "volatile", operands, 5, isVolatile))) + { + return mlir::failure(); + } + + auto value = castAtomicValue(location, elementType, operands[1], castFn, genContext); + if (!value) + { + return mlir::failure(); + } + + builder.create(location, operands[0], value, builder.getI32IntegerAttr(*ordering), syncscope, + alignment, isVolatile); + return mlir::success(); + } ValueOrLogicalResult mlirGenFence(const mlir::Location &location, ArrayRef operands) { - auto size = operands.size(); - if (size == 0 || size > 2) + const StringRef builtin = "fence"; + if (!checkAtomicArgCount(location, fenceSignature, operands.size(), 1, 2)) { return mlir::failure(); } - MLIRCodeLogic mcl(builder, compileOptions); - if (size > 1) + // a fence orders: acquire, release, acq_rel or seq_cst + auto ordering = getAtomicOrdering(location, builtin, "ordering", operands[0], {4, 5, 6, 7}); + if (!ordering) { - builder.create(location, mcl.getIntegerAttr(location, operands[0]), mcl.getStringAttr(location, operands[1])); + return mlir::failure(); } - else + + mlir::StringAttr syncscope; + mlir::IntegerAttr alignment; + if (mlir::failed(getAtomicScopeAndAlignment(location, builtin, operands, 1, false, syncscope, alignment))) { - builder.create(location, mcl.getIntegerAttr(location, operands[0]), mlir::StringAttr()); + return mlir::failure(); } + builder.create(location, builder.getI32IntegerAttr(*ordering), syncscope); return mlir::success(); - } + } ValueOrLogicalResult mlirGenInlineAsm(const mlir::Location &location, mlir::SmallVector typeArgs, ArrayRef operands) { diff --git a/tslang/include/TypeScript/TypeScriptOps.td b/tslang/include/TypeScript/TypeScriptOps.td index 57b5ff232..ac7a6fd36 100644 --- a/tslang/include/TypeScript/TypeScriptOps.td +++ b/tslang/include/TypeScript/TypeScriptOps.td @@ -2608,6 +2608,28 @@ def TypeScript_AtomicCmpXchgOp : TypeScript_Op<"CmpXchg"> { let results = (outs AnyType:$res); } +def TypeScript_AtomicLoadOp : TypeScript_Op<"AtomicLoad"> { + let summary = "atomicload command"; + let description = [{ + The "atomicload" builtin operation: an LLVM `load atomic` of the value $ptr points at. An + alignment of none is the natural alignment of the value. + }]; + + let arguments = (ins TypeScript_AnyRefLike:$ptr, I32Attr:$ordering, OptionalAttr:$syncscope, OptionalAttr:$alignment, UnitAttr:$volatile_); + let results = (outs AnyType:$res); +} + +def TypeScript_AtomicStoreOp : TypeScript_Op<"AtomicStore"> { + let summary = "atomicstore command"; + let description = [{ + The "atomicstore" builtin operation: an LLVM `store atomic` of $value where $ptr points. An + alignment of none is the natural alignment of the value. + }]; + + let arguments = (ins TypeScript_AnyRefLike:$ptr, AnyType:$value, I32Attr:$ordering, OptionalAttr:$syncscope, OptionalAttr:$alignment, UnitAttr:$volatile_); + let results = (outs ); +} + def TypeScript_FenceOp : TypeScript_Op<"Fence"> { let summary = "fence command"; let description = [{ diff --git a/tslang/lib/TypeScript/LowerToAffineLoops.cpp b/tslang/lib/TypeScript/LowerToAffineLoops.cpp index b7bfe2caf..ee13f9cc6 100644 --- a/tslang/lib/TypeScript/LowerToAffineLoops.cpp +++ b/tslang/lib/TypeScript/LowerToAffineLoops.cpp @@ -3124,7 +3124,7 @@ void AddTsAffineLegalOps(ConversionTarget &target) mlir_ts::CreateUnionInstanceOp, mlir_ts::GetValueFromUnionOp, mlir_ts::GetTypeInfoFromUnionOp, mlir_ts::OptionalOp, mlir_ts::OptionalValueOp, mlir_ts::OptionalUndefOp, mlir_ts::LoadLibraryPermanentlyOp, mlir_ts::SearchForAddressOfSymbolOp, - mlir_ts::AtomicRMWOp, mlir_ts::AtomicCmpXchgOp, mlir_ts::FenceOp, mlir_ts::InlineAsmOp, + mlir_ts::AtomicRMWOp, mlir_ts::AtomicCmpXchgOp, mlir_ts::AtomicLoadOp, mlir_ts::AtomicStoreOp, mlir_ts::FenceOp, mlir_ts::InlineAsmOp, mlir_ts::CallIntrinsicOp, mlir_ts::LinkerOptionsOp>(); #ifdef ENABLE_TYPED_GC target.addLegalOp< diff --git a/tslang/lib/TypeScript/LowerToLLVM.cpp b/tslang/lib/TypeScript/LowerToLLVM.cpp index 5d2f52930..a1a4b81c8 100644 --- a/tslang/lib/TypeScript/LowerToLLVM.cpp +++ b/tslang/lib/TypeScript/LowerToLLVM.cpp @@ -8548,6 +8548,64 @@ struct AtomicCmpXchgOpLowering : public TsLlvmPattern } }; +// LLVM wants an atomic load or store aligned; none given is the natural alignment of the value +static uint64_t atomicAccessAlignment(const mlir::TypeConverter *typeConverter, mlir::Type llvmType, std::optional alignment) +{ + if (alignment.has_value() && *alignment > 0) + { + return *alignment; + } + + LLVMTypeConverterHelper llvmtch(static_cast(typeConverter)); + return llvmtch.getTypeAllocSizeInBytes(llvmType); +} + +struct AtomicLoadOpLowering : public TsLlvmPattern +{ + using TsLlvmPattern::TsLlvmPattern; + + LogicalResult matchAndRewrite(mlir_ts::AtomicLoadOp op, Adaptor transformed, + ConversionPatternRewriter &rewriter) const final + { + TypeConverterHelper tch(getTypeConverter()); + auto llvmType = tch.convertType(op.getType()); + rewriter.replaceOpWithNewOp( + op, + llvmType, + transformed.getPtr(), + atomicAccessAlignment(getTypeConverter(), llvmType, transformed.getAlignment()), + transformed.getVolatile_(), + /*isNonTemporal=*/false, + /*isInvariant=*/false, + /*isInvariantGroup=*/false, + (LLVM::AtomicOrdering)transformed.getOrdering(), + transformed.getSyncscope().value_or(StringRef())); + return success(); + } +}; + +struct AtomicStoreOpLowering : public TsLlvmPattern +{ + using TsLlvmPattern::TsLlvmPattern; + + LogicalResult matchAndRewrite(mlir_ts::AtomicStoreOp op, Adaptor transformed, + ConversionPatternRewriter &rewriter) const final + { + auto value = transformed.getValue(); + rewriter.replaceOpWithNewOp( + op, + value, + transformed.getPtr(), + atomicAccessAlignment(getTypeConverter(), value.getType(), transformed.getAlignment()), + transformed.getVolatile_(), + /*isNonTemporal=*/false, + /*isInvariantGroup=*/false, + (LLVM::AtomicOrdering)transformed.getOrdering(), + transformed.getSyncscope().value_or(StringRef())); + return success(); + } +}; + struct FenceOpLowering : public TsLlvmPattern { using TsLlvmPattern::TsLlvmPattern; @@ -9673,7 +9731,7 @@ void TypeScriptToLLVMLoweringPass::runOnOperation() #endif , SwitchStateInternalOpLowering, LoadLibraryPermanentlyOpLowering, SearchForAddressOfSymbolOpLowering, - AtomicRMWOpLowering, AtomicCmpXchgOpLowering, FenceOpLowering, InlineAsmOpLowering, CallIntrinsicOpLowering, LinkerOptionsOpLowering>( + AtomicRMWOpLowering, AtomicCmpXchgOpLowering, AtomicLoadOpLowering, AtomicStoreOpLowering, FenceOpLowering, InlineAsmOpLowering, CallIntrinsicOpLowering, LinkerOptionsOpLowering>( typeConverter, &getContext(), &tsLlvmContext); if (tsLlvmContext.compileOptions.isWindows) diff --git a/tslang/test/tester/atomic-errors/alignment.ts b/tslang/test/tester/atomic-errors/alignment.ts new file mode 100644 index 000000000..2dc890c78 --- /dev/null +++ b/tslang/test/tester/atomic-errors/alignment.ts @@ -0,0 +1,8 @@ +function f() { + let a: i32 = 1; + return atomicload(ReferenceOf(a), 7, "", 3); +} + +function main() { + print("unreachable"); +} diff --git a/tslang/test/tester/atomic-errors/boolean_reference.ts b/tslang/test/tester/atomic-errors/boolean_reference.ts new file mode 100644 index 000000000..4c1454dfc --- /dev/null +++ b/tslang/test/tester/atomic-errors/boolean_reference.ts @@ -0,0 +1,8 @@ +function f() { + let b = true; + return atomicload(ReferenceOf(b), 7); +} + +function main() { + print("unreachable"); +} diff --git a/tslang/test/tester/atomic-errors/cmpxchg_arguments.ts b/tslang/test/tester/atomic-errors/cmpxchg_arguments.ts new file mode 100644 index 000000000..e43df3f1a --- /dev/null +++ b/tslang/test/tester/atomic-errors/cmpxchg_arguments.ts @@ -0,0 +1,8 @@ +function f() { + let a: i32 = 1; + return cmpxchg(ReferenceOf(a), 1, 2, 7); +} + +function main() { + print("unreachable"); +} diff --git a/tslang/test/tester/atomic-errors/cmpxchg_failure_ordering.ts b/tslang/test/tester/atomic-errors/cmpxchg_failure_ordering.ts new file mode 100644 index 000000000..70415dafd --- /dev/null +++ b/tslang/test/tester/atomic-errors/cmpxchg_failure_ordering.ts @@ -0,0 +1,8 @@ +function f() { + let a: i32 = 1; + return cmpxchg(ReferenceOf(a), 1, 2, 7, 5); +} + +function main() { + print("unreachable"); +} diff --git a/tslang/test/tester/atomic-errors/cmpxchg_float.ts b/tslang/test/tester/atomic-errors/cmpxchg_float.ts new file mode 100644 index 000000000..2d6564436 --- /dev/null +++ b/tslang/test/tester/atomic-errors/cmpxchg_float.ts @@ -0,0 +1,8 @@ +function f() { + let n: number = 1; + return cmpxchg(ReferenceOf(n), 1, 2, 7, 7); +} + +function main() { + print("unreachable"); +} diff --git a/tslang/test/tester/atomic-errors/fence_ordering.ts b/tslang/test/tester/atomic-errors/fence_ordering.ts new file mode 100644 index 000000000..61d7d54bb --- /dev/null +++ b/tslang/test/tester/atomic-errors/fence_ordering.ts @@ -0,0 +1,7 @@ +function f() { + fence(2); +} + +function main() { + print("unreachable"); +} diff --git a/tslang/test/tester/atomic-errors/fence_variable.ts b/tslang/test/tester/atomic-errors/fence_variable.ts new file mode 100644 index 000000000..d3da5633f --- /dev/null +++ b/tslang/test/tester/atomic-errors/fence_variable.ts @@ -0,0 +1,7 @@ +function f(order: int) { + fence(order); +} + +function main() { + print("unreachable"); +} diff --git a/tslang/test/tester/atomic-errors/load_ordering.ts b/tslang/test/tester/atomic-errors/load_ordering.ts new file mode 100644 index 000000000..c6d4f558b --- /dev/null +++ b/tslang/test/tester/atomic-errors/load_ordering.ts @@ -0,0 +1,8 @@ +function f() { + let a: i32 = 1; + return atomicload(ReferenceOf(a), 5); +} + +function main() { + print("unreachable"); +} diff --git a/tslang/test/tester/atomic-errors/not_a_reference.ts b/tslang/test/tester/atomic-errors/not_a_reference.ts new file mode 100644 index 000000000..e04bae6ec --- /dev/null +++ b/tslang/test/tester/atomic-errors/not_a_reference.ts @@ -0,0 +1,8 @@ +function f() { + let a: i32 = 1; + return atomicload(a, 7); +} + +function main() { + print("unreachable"); +} diff --git a/tslang/test/tester/atomic-errors/op_range.ts b/tslang/test/tester/atomic-errors/op_range.ts new file mode 100644 index 000000000..ef9a19657 --- /dev/null +++ b/tslang/test/tester/atomic-errors/op_range.ts @@ -0,0 +1,8 @@ +function f() { + let a: i32 = 1; + return atomicrmw(99, ReferenceOf(a), 2, 7); +} + +function main() { + print("unreachable"); +} diff --git a/tslang/test/tester/atomic-errors/op_variable.ts b/tslang/test/tester/atomic-errors/op_variable.ts new file mode 100644 index 000000000..80fcff9bb --- /dev/null +++ b/tslang/test/tester/atomic-errors/op_variable.ts @@ -0,0 +1,8 @@ +function f(op: int) { + let a: i32 = 1; + return atomicrmw(op, ReferenceOf(a), 2, 7); +} + +function main() { + print("unreachable"); +} diff --git a/tslang/test/tester/atomic-errors/ordering_hole.ts b/tslang/test/tester/atomic-errors/ordering_hole.ts new file mode 100644 index 000000000..d4502c95f --- /dev/null +++ b/tslang/test/tester/atomic-errors/ordering_hole.ts @@ -0,0 +1,8 @@ +function f() { + let a: i32 = 1; + return atomicrmw(1, ReferenceOf(a), 2, 3); +} + +function main() { + print("unreachable"); +} diff --git a/tslang/test/tester/atomic-errors/rmw_arguments.ts b/tslang/test/tester/atomic-errors/rmw_arguments.ts new file mode 100644 index 000000000..bf16273a5 --- /dev/null +++ b/tslang/test/tester/atomic-errors/rmw_arguments.ts @@ -0,0 +1,8 @@ +function f() { + let a: i32 = 1; + return atomicrmw(1, ReferenceOf(a), 2); +} + +function main() { + print("unreachable"); +} diff --git a/tslang/test/tester/atomic-errors/rmw_float_op_on_int.ts b/tslang/test/tester/atomic-errors/rmw_float_op_on_int.ts new file mode 100644 index 000000000..4fae7ba4c --- /dev/null +++ b/tslang/test/tester/atomic-errors/rmw_float_op_on_int.ts @@ -0,0 +1,8 @@ +function f() { + let a: i32 = 1; + return atomicrmw(11, ReferenceOf(a), 2, 7); +} + +function main() { + print("unreachable"); +} diff --git a/tslang/test/tester/atomic-errors/rmw_int_op_on_float.ts b/tslang/test/tester/atomic-errors/rmw_int_op_on_float.ts new file mode 100644 index 000000000..e612e77b5 --- /dev/null +++ b/tslang/test/tester/atomic-errors/rmw_int_op_on_float.ts @@ -0,0 +1,8 @@ +function f() { + let n: number = 1; + return atomicrmw(1, ReferenceOf(n), 2, 7); +} + +function main() { + print("unreachable"); +} diff --git a/tslang/test/tester/atomic-errors/rmw_ordering.ts b/tslang/test/tester/atomic-errors/rmw_ordering.ts new file mode 100644 index 000000000..9ab5458bb --- /dev/null +++ b/tslang/test/tester/atomic-errors/rmw_ordering.ts @@ -0,0 +1,8 @@ +function f() { + let a: i32 = 1; + return atomicrmw(1, ReferenceOf(a), 2, 1); +} + +function main() { + print("unreachable"); +} diff --git a/tslang/test/tester/atomic-errors/store_ordering.ts b/tslang/test/tester/atomic-errors/store_ordering.ts new file mode 100644 index 000000000..f5f12a55c --- /dev/null +++ b/tslang/test/tester/atomic-errors/store_ordering.ts @@ -0,0 +1,8 @@ +function f() { + let a: i32 = 1; + atomicstore(ReferenceOf(a), 1, 4); +} + +function main() { + print("unreachable"); +} diff --git a/tslang/test/tester/atomic-errors/volatile_variable.ts b/tslang/test/tester/atomic-errors/volatile_variable.ts new file mode 100644 index 000000000..a780721ae --- /dev/null +++ b/tslang/test/tester/atomic-errors/volatile_variable.ts @@ -0,0 +1,8 @@ +function f(flag: boolean) { + let a: i32 = 1; + return atomicload(ReferenceOf(a), 7, "", 0, flag); +} + +function main() { + print("unreachable"); +} diff --git a/tslang/test/tester/tests/00atomic_builtins.ts b/tslang/test/tester/tests/00atomic_builtins.ts new file mode 100644 index 000000000..0afad46a2 --- /dev/null +++ b/tslang/test/tester/tests/00atomic_builtins.ts @@ -0,0 +1,70 @@ +// The atomic builtins: atomicrmw, cmpxchg, atomicload, atomicstore and fence, with their optional +// syncscope, alignment and volatile / weak arguments. The value is cast to the type the reference +// points at, so a literal does not make a 16-bit element a 32-bit store. Orderings are LLVM's: +// 2 monotonic, 4 acquire, 5 release, 6 acq_rel, 7 seq_cst. + +enum Order { + Monotonic = 2, + Acquire = 4, + Release = 5, + AcquireRelease = 6, + SequentiallyConsistent = 7 +} + +class Counter { + value: i32 = 0; + other: u16 = 7; +} + +function main() { + // atomicrmw returns the value before the operation + let a: i32 = 20; + assert(atomicrmw(1, ReferenceOf(a), 2, Order.SequentiallyConsistent) == 20 && a == 22, "add"); + assert(atomicrmw(2, ReferenceOf(a), 2, Order.Monotonic, "", 4) == 22 && a == 20, "sub, syncscope and alignment"); + assert(atomicrmw(0, ReferenceOf(a), 5, 7, "", 0, true) == 20 && a == 5, "volatile xchg"); + + // the literal is cast to u16: the add wraps at 16 bits and the neighbour is untouched + const c = new Counter(); + c.other = 65535; + assert(atomicrmw(1, ReferenceOf(c.other), 1, 7) == 65535 && c.other == 0, "u16 wraps"); + assert(c.value == 0, "the field before it untouched"); + + let f: number = 1.5; + assert(atomicrmw(11, ReferenceOf(f), 2, 7) == 1.5 && f == 3.5, "fadd"); + + // atomicload / atomicstore + let b: i64 = 5; + assert(atomicload(ReferenceOf(b), Order.Acquire) == 5, "load"); + atomicstore(ReferenceOf(b), 9, Order.Release); + assert(b == 9, "store"); + assert(atomicload(ReferenceOf(b), 7, "", 8, true) == 9, "volatile load"); + atomicstore(ReferenceOf(b), 10, 7, "", 0, true); + assert(atomicload(ReferenceOf(b), 1) == 10, "volatile store, unordered load"); + + let n: number = 0.25; + atomicstore(ReferenceOf(n), 0.5, 7); + assert(atomicload(ReferenceOf(n), 7) == 0.5, "a float"); + + // cmpxchg returns [the value before, whether it was the expected one] + let x: i32 = 22; + const hit = cmpxchg(ReferenceOf(x), 22, 30, 7, 7); + assert(hit[0] == 22 && hit[1] && x == 30, "cmpxchg hit"); + const miss = cmpxchg(ReferenceOf(x), 22, 40, Order.AcquireRelease, Order.Acquire); + assert(miss[0] == 30 && !miss[1] && x == 30, "cmpxchg miss"); + + // a weak cmpxchg may fail spuriously: retried until it succeeds + let tries = 0; + let done = false; + while (!done && tries < 1000) { + const weak = cmpxchg(ReferenceOf(x), 30, 31, 7, 7, "", 0, true, true); + done = weak[1]; + tries++; + } + + assert(done && x == 31, "weak volatile cmpxchg"); + + fence(Order.SequentiallyConsistent); + fence(4, ""); + + print("done."); +} From a3b3592523d7414239386d29acb873bb2413f41a Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Thu, 5 Nov 2026 15:28:56 +0000 Subject: [PATCH 2/5] A number literal argument yields to the T another argument fixed, when it holds the value With `add(ta: TA, i: int, v: T)`, `add(new TA([65535]), 0, 2)` merged T = u16 with the literal's s32 into s32, and failed with "type TA is not assignable to type TA". A number literal now yields to a candidate it fits (u16 holds 2), in either parameter order, and is cast to it; a literal the other type can't hold merges as before. Test: tests/00generic_literal_inference.ts. Co-Authored-By: Claude Opus 5.5 --- .../TypeScript/MLIRLogic/MLIRTypeHelper.h | 53 +++++++++++++++++++ tslang/lib/TypeScript/MLIRGenGenerics.cpp | 16 +++++- tslang/lib/TypeScript/MLIRGenTypes.cpp | 16 +++++- .../tests/00generic_literal_inference.ts | 43 +++++++++++++++ 4 files changed, 126 insertions(+), 2 deletions(-) create mode 100644 tslang/test/tester/tests/00generic_literal_inference.ts diff --git a/tslang/include/TypeScript/MLIRLogic/MLIRTypeHelper.h b/tslang/include/TypeScript/MLIRLogic/MLIRTypeHelper.h index 72de7b9d9..5a0e59336 100644 --- a/tslang/include/TypeScript/MLIRLogic/MLIRTypeHelper.h +++ b/tslang/include/TypeScript/MLIRLogic/MLIRTypeHelper.h @@ -4083,6 +4083,59 @@ class MLIRTypeHelper return mergeType(location, existType, currentType, merged); } + // whether `literalType` is a number literal whose value `type`, a number type, holds as it is + bool numberLiteralFits(mlir::Type literalType, mlir::Type type) + { + auto literal = dyn_cast(literalType); + if (!literal || isa(type)) + { + return false; + } + + auto value = literal.getValue(); + if (auto intType = dyn_cast(type)) + { + llvm::APSInt intValue; + if (auto intAttr = dyn_cast(value); intAttr && !isa(value)) + { + auto literalInt = dyn_cast(intAttr.getType()); + intValue = llvm::APSInt(intAttr.getValue(), literalInt && literalInt.isUnsigned()); + } + else if (auto floatAttr = dyn_cast(value)) + { + auto isExact = false; + intValue = llvm::APSInt(128, false); + if (floatAttr.getValue().convertToInteger(intValue, llvm::APFloat::rmTowardZero, &isExact) != llvm::APFloat::opOK || + !isExact) + { + return false; + } + } + else + { + return false; + } + + auto width = intType.getWidth(); + if (!intValue.isRepresentableByInt64() || width > 64) + { + return false; + } + + auto number = intValue.getExtValue(); + auto fitsUnsigned = number >= 0 && (width == 64 || static_cast(number) < (uint64_t(1) << width)); + auto fitsSigned = width == 64 || (number >= -(int64_t(1) << (width - 1)) && number < (int64_t(1) << (width - 1))); + return intType.isUnsigned() ? fitsUnsigned : intType.isSigned() ? fitsSigned : fitsSigned || fitsUnsigned; + } + + if (isa(type) || type.isF64()) + { + return isa(value) && !isa(value); + } + + return false; + } + mlir::Type mergeType(mlir::Location location, mlir::Type existType, mlir::Type currentType, bool& merged) { merged = false; diff --git a/tslang/lib/TypeScript/MLIRGenGenerics.cpp b/tslang/lib/TypeScript/MLIRGenGenerics.cpp index 3d6d2f639..0f4c25f73 100644 --- a/tslang/lib/TypeScript/MLIRGenGenerics.cpp +++ b/tslang/lib/TypeScript/MLIRGenGenerics.cpp @@ -21,7 +21,21 @@ namespace mlirgen auto currentType = concreteType; auto name = namedGenType.getName().getValue(); auto existType = results.lookup(name); - if (existType) + if (existType && mth.numberLiteralFits(currentType, existType)) + { + // a number literal yields to the type T already has, when it holds its value: in + // `add(ta: TA, v: T)`, `add(new TA(...), 2)` is T = u16 with 2 cast to it, where + // merging the two made T the literal's s32, and TA no longer passed for TA + LLVM_DEBUG(llvm::dbgs() << "\n!! literal " << currentType << " yields to: " << existType << "\n";); + currentType = existType; + } + else if (existType && mth.numberLiteralFits(existType, currentType)) + { + // ...in either order: `add2(v: T, ta: TA)` + LLVM_DEBUG(llvm::dbgs() << "\n!! literal " << existType << " yields to: " << currentType << "\n";); + results[name] = currentType; + } + else if (existType) { auto merged = false; currentType = mth.mergeType(location, existType, currentType, merged); diff --git a/tslang/lib/TypeScript/MLIRGenTypes.cpp b/tslang/lib/TypeScript/MLIRGenTypes.cpp index 4d2427dd9..4d3548028 100644 --- a/tslang/lib/TypeScript/MLIRGenTypes.cpp +++ b/tslang/lib/TypeScript/MLIRGenTypes.cpp @@ -382,7 +382,21 @@ namespace mlirgen LLVM_DEBUG(llvm::dbgs() << "\n!! replacing existing type for: " << name << " exist type: " << existType.second << " new type: " << type << "\n";); - if (!isa(existType.second) && mergeTypes) + if (!isa(existType.second) && mergeTypes && !arrayMerge && + mth.numberLiteralFits(type, existType.second)) + { + // a number literal argument yields to the type T already has from another one, when + // it holds the literal's value: `add(ta: TA, i: int, v: T)` called as + // `add(new TA(...), 0, 2)` is T = u16, the literal cast to it. Merged, T was the + // literal's s32, and TA did not pass for TA. + type = existType.second; + } + else if (!isa(existType.second) && mergeTypes && !arrayMerge && + mth.numberLiteralFits(existType.second, type)) + { + // ...in either order: `add2(v: T, ta: TA)` + } + else if (!isa(existType.second) && mergeTypes) { auto merged = false; if (arrayMerge) diff --git a/tslang/test/tester/tests/00generic_literal_inference.ts b/tslang/test/tester/tests/00generic_literal_inference.ts new file mode 100644 index 000000000..a4b71e760 --- /dev/null +++ b/tslang/test/tester/tests/00generic_literal_inference.ts @@ -0,0 +1,43 @@ +// A number literal argument does not decide a generic's T that another argument already fixed: +// with `add(ta: TA, i: int, v: T)`, `add(new TA(...), 0, 2)` is T = u16 and 2 is cast to +// it, in either parameter order. Merging the two used to make T the literal's s32, and +// "type TA is not assignable to type TA". A literal T can't hold still merges as before. + +class TA { + constructor(public data: T[]) {} +} + +function add(ta: TA, i: int, v: T): T { + const old = ta.data[i]; + ta.data[i] = v; + return old; +} + +function addFirst(v: T, ta: TA, i: int): T { + const old = ta.data[i]; + ta.data[i] = v; + return old; +} + +function same(a: T, b: T): T { + return b; +} + +function main() { + const t = new TA([65535]); + assert(add(t, 0, 2) == 65535 && t.data[0] == 2, "the class's T first"); + assert(addFirst(3, t, 0) == 2 && t.data[0] == 3, "the literal first"); + assert(sizeof() == 2, "u16"); + + const s = new TA([-1]); + assert(add(s, 0, -128) == -1 && s.data[0] == -128, "a negative literal an i8 holds"); + + const u: u8 = 200; + const r = same(u, 100); + assert(r == 100 && sizeof() == 1, "T = u8"); + + // 300 doesn't fit a u8: T is merged as before + assert(same(u, 300) == 300, "a literal u8 can't hold"); + + print("done."); +} From 7c7eb74f76c789783b06b626551bc7e04b696260 Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Thu, 5 Nov 2026 15:28:57 +0000 Subject: [PATCH 3/5] new through a specialising type alias keeps its type arguments; array literals fit overloaded T[] constructors `type Uint8Array = TypedArray; new Uint8Array([250])` inferred the class's type arguments from the constructor's arguments, which made a TypedArray. A `new` naming an alias that already specialises the class no longer infers them, from the arguments or the receiver. Constructor overload resolution scored `[250]` by its evaluated type, s32[], which fits no u8[] parameter, so `new TypedArray([250])` of a class with a second constructor found none. An array literal argument now fits a T[] parameter as its elements fit T, as it is generated. Test: tests/00new_alias_and_ctor_overloads.ts. Co-Authored-By: Claude Opus 5.5 --- tslang/lib/TypeScript/MLIRGenAccessCall.cpp | 30 ++++++++++- tslang/lib/TypeScript/MLIRGenExpressions.cpp | 41 +++++++++++++- .../tests/00new_alias_and_ctor_overloads.ts | 54 +++++++++++++++++++ 3 files changed, 121 insertions(+), 4 deletions(-) create mode 100644 tslang/test/tester/tests/00new_alias_and_ctor_overloads.ts diff --git a/tslang/lib/TypeScript/MLIRGenAccessCall.cpp b/tslang/lib/TypeScript/MLIRGenAccessCall.cpp index b2525d4ce..7bd569881 100644 --- a/tslang/lib/TypeScript/MLIRGenAccessCall.cpp +++ b/tslang/lib/TypeScript/MLIRGenAccessCall.cpp @@ -2128,6 +2128,32 @@ namespace mlirgen argumentTypes.push_back(type); } + // an array literal is generated for its parameter's type: `[250]` evaluates s32[], and fits + // `data: u8[]` as its elements fit u8 (`new TypedArray([250])` of a class with a second + // constructor found none that took s32[]); an empty one fits any array + auto argumentFit = [&](size_t index, mlir::Type param) { + auto fit = overloadArgumentFit(location, argumentTypes[index], param); + auto arrayParam = dyn_cast(mth.stripOptionalType(param)); + if (fit > 0 || !arrayParam || arguments[index] != SyntaxKind::ArrayLiteralExpression) + { + return fit; + } + + auto elementsFit = 3; + for (auto element : arguments[index].as()->elements) + { + auto elementType = element == SyntaxKind::SpreadElement ? mlir::Type() : evaluate(element, genContext); + if (!elementType) + { + return 0; + } + + elementsFit = std::min(elementsFit, overloadArgumentFit(location, elementType, arrayParam.getElementType())); + } + + return elementsFit; + }; + auto bestScore = -1; auto best = -1; for (auto candidate : candidates) @@ -2154,7 +2180,7 @@ namespace mlirgen continue; } - auto argFit = overloadArgumentFit(location, argumentTypes[index], param); + auto argFit = argumentFit(index, param); fits &= argFit > 0; score += argFit; } @@ -2169,7 +2195,7 @@ namespace mlirgen for (auto index = fixedParams.size(); index < argumentTypes.size(); index++) { - auto argFit = overloadArgumentFit(location, argumentTypes[index], restType); + auto argFit = argumentFit(index, restType); fits &= argFit > 0; score += argFit; } diff --git a/tslang/lib/TypeScript/MLIRGenExpressions.cpp b/tslang/lib/TypeScript/MLIRGenExpressions.cpp index 0e952dfbc..c38d284b3 100644 --- a/tslang/lib/TypeScript/MLIRGenExpressions.cpp +++ b/tslang/lib/TypeScript/MLIRGenExpressions.cpp @@ -1584,10 +1584,47 @@ namespace mlirgen MLIRValueGuard sharedElementsRefusedGuard(sharedElementsRefused); sharedElementsRefused = false; + // `type Uint8Array = TypedArray`: `new Uint8Array([250])` names a class its alias already + // gave type arguments, which neither the constructor's arguments nor the receiver override. + // Inferred from the arguments, it was a TypedArray. + auto namesSpecializedAlias = [&](mlir_ts::ClassType classType) { + if (getGenericClassInfoByFullName(classType.getName().getValue())) + { + // the generic class itself + return false; + } + + auto classInfo = getClassInfoByFullName(classType.getName().getValue()); + if (!classInfo || !classInfo->originClassType) + { + return false; + } + + auto genericClassInfo = getGenericClassInfoByFullName(classInfo->originClassType.getName().getValue()); + StringRef name; + if (typeExpression == SyntaxKind::Identifier) + { + name = MLIRHelper::getName(typeExpression.as(), stringAllocator); + } + else if (typeExpression == SyntaxKind::PropertyAccessExpression) + { + name = MLIRHelper::getName(typeExpression.as()->name, stringAllocator); + } + + return genericClassInfo && !name.empty() && name != genericClassInfo->name; + }; + + auto classOfType = dyn_cast(value.getType()); + auto inferTypeArguments = newExpression->typeArguments.size() == 0 && !(classOfType && namesSpecializedAlias(classOfType)); + if (!inferTypeArguments) + { + unspecialized = mlir::Value(); + } + // `new Set([2, 4, 6, 8])`: a generic class written without type arguments takes them from // its constructor's arguments (see inferClassTypeFromConstructorArguments). Ahead of the // receiver below, which overrides it: `const s: Set = new Set([1, 2])` is Set. - if (newExpression->typeArguments.size() == 0 && newExpression->arguments.size() > 0) + if (inferTypeArguments && newExpression->arguments.size() > 0) { if (auto classType = dyn_cast(value.getType())) { @@ -1625,7 +1662,7 @@ namespace mlirgen // arguments takes them from the type it is given to, when that is the same class. Its // type parameter defaults (`Map`) made Map otherwise, a // different layout, and the cast to the declared type crashed at run time. - if (newExpression->typeArguments.size() == 0 && genContext.receiverType) + if (inferTypeArguments && genContext.receiverType) { if (auto classType = dyn_cast(value.getType())) { diff --git a/tslang/test/tester/tests/00new_alias_and_ctor_overloads.ts b/tslang/test/tester/tests/00new_alias_and_ctor_overloads.ts new file mode 100644 index 000000000..5a35bdb29 --- /dev/null +++ b/tslang/test/tester/tests/00new_alias_and_ctor_overloads.ts @@ -0,0 +1,54 @@ +// `new` through a type alias that gives a generic class its type arguments keeps them: +// `type U8 = Buf; new U8([250])` is a Buf, where the constructor's arguments made it a +// Buf. And an array literal argument fits an overloaded constructor's `T[]` parameter as its +// elements fit T: `new Buf([250])` of a class with a second constructor found none for s32[]. + +class Buf { + data: T[]; + + constructor(data: T[] = []) { + this.data = data; + } + + constructor(length: int) { + this.data = []; + for (let i = 0; i < length; i++) { + this.data.push(0); + } + } +} + +type U8 = Buf; +type I16 = Buf; + +class One { + constructor(public data: T[]) {} +} + +type OneU8 = One; + +function main() { + const a = new Buf([250]); + assert(sizeof() == 1 && a.data[0] == 250, "an overloaded constructor takes [250] as u8[]"); + + const b = new U8([250]); + assert(sizeof() == 1 && b.data[0] == 250, "the alias's u8"); + + const c = new I16([-2, 3]); + assert(sizeof() == 2 && c.data[0] == -2, "the alias's i16"); + + const d = new U8(3); + assert(d.data.length == 3, "the other constructor"); + + const e = new Buf([]); + assert(e.data.length == 0, "an empty literal"); + + const f = new OneU8([7]); + assert(sizeof() == 1, "one constructor"); + + // without an alias, the arguments still give T + const g = new One([1, 2]); + assert(sizeof() == 4, "inferred s32"); + + print("done."); +} From 03e2de02fce9cd0eccd0fd50e32a9e78a1a50563 Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Thu, 5 Nov 2026 15:28:57 +0000 Subject: [PATCH 4/5] JIT: read-only sections from their own pool, made executable, not from the code pool On Win64 the JIT allocated read-only sections from the code pool, so they could hold the personality thunks RTDyld puts in .xdata. RTDyld's reservation sizes the code pool for the code alone, so an object whose read-only data outgrew the page-rounding slack spilled into a mapping of its own wherever the OS put it. Below the reserved block (about a third of runs of the DefaultLib Atomics tests under rc), an IMAGE_REL_AMD64_ADDR32NB relocation to it fell below the image base: "LLVM ERROR: IMAGE_REL_AMD64_ADDR32NB relocation requires an ordered section layout". Read-only sections now come from the read-only pool, in the reserved block between code and data as RTDyld lays it out, and finalizeMemory makes their pages read+execute. The JIT also warns when a section is outside the reserved block, which a spill always is, so the regression test fails in every run rather than in a few: the old routing warned on every run of tests/00jit_rodata_layout.ts. Test: test-jit-rodata-layout (tests/00jit_rodata_layout.ts). Co-Authored-By: Claude Opus 5.5 --- .../test/tester/tests/00jit_rodata_layout.ts | 1502 +++++++++++++++++ tslang/tslang/jit.cpp | 90 +- 2 files changed, 1587 insertions(+), 5 deletions(-) create mode 100644 tslang/test/tester/tests/00jit_rodata_layout.ts diff --git a/tslang/test/tester/tests/00jit_rodata_layout.ts b/tslang/test/tester/tests/00jit_rodata_layout.ts new file mode 100644 index 000000000..8d5d9c406 --- /dev/null +++ b/tslang/test/tester/tests/00jit_rodata_layout.ts @@ -0,0 +1,1502 @@ +// A JIT'd object with more read-only data (strings, .xdata, .pdata) than its code's reservation +// has room to spare. The JIT used to allocate read-only sections from the code pool, which RTDyld +// reserves for the code alone: the rest spilled into a mapping of its own, below the reserved block +// in some runs, and an IMAGE_REL_AMD64_ADDR32NB relocation (.pdata, .xdata) to it failed with +// "LLVM ERROR: IMAGE_REL_AMD64_ADDR32NB relocation requires an ordered section layout". Every run +// spilled, though: the JIT now warns of a section outside the reserved block, which the test fails +// on (test-jit-rodata-layout). The try/catch in each function is the read-only data that must be +// executable: the personality thunk RTDyld puts in .xdata. + +function f0(x: number): number { + let r = x; + try { + if (x < 0) { + throw "negative 0"; + } + + r = x * 1 + 0; + } catch (e) { + r = -1; + } + + return r; +} + +function f1(x: number): number { + let r = x; + try { + if (x < 0) { + throw "negative 1"; + } + + r = x * 2 + 1; + } catch (e) { + r = -1; + } + + return r; +} + +function f2(x: number): number { + let r = x; + try { + if (x < 0) { + throw "negative 2"; + } + + r = x * 3 + 2; + } catch (e) { + r = -1; + } + + return r; +} + +function f3(x: number): number { + let r = x; + try { + if (x < 0) { + throw "negative 3"; + } + + r = x * 4 + 3; + } catch (e) { + r = -1; + } + + return r; +} + +function f4(x: number): number { + let r = x; + try { + if (x < 0) { + throw "negative 4"; + } + + r = x * 5 + 4; + } catch (e) { + r = -1; + } + + return r; +} + +function f5(x: number): number { + let r = x; + try { + if (x < 0) { + throw "negative 5"; + } + + r = x * 6 + 5; + } catch (e) { + r = -1; + } + + return r; +} + +function f6(x: number): number { + let r = x; + try { + if (x < 0) { + throw "negative 6"; + } + + r = x * 7 + 6; + } catch (e) { + r = -1; + } + + return r; +} + +function f7(x: number): number { + let r = x; + try { + if (x < 0) { + throw "negative 7"; + } + + r = x * 8 + 7; + } catch (e) { + r = -1; + } + + return r; +} + +function f8(x: number): number { + let r = x; + try { + if (x < 0) { + throw "negative 8"; + } + + r = x * 9 + 8; + } catch (e) { + r = -1; + } + + return r; +} + +function f9(x: number): number { + let r = x; + try { + if (x < 0) { + throw "negative 9"; + } + + r = x * 10 + 9; + } catch (e) { + r = -1; + } + + return r; +} + +function f10(x: number): number { + let r = x; + try { + if (x < 0) { + throw "negative 10"; + } + + r = x * 11 + 10; + } catch (e) { + r = -1; + } + + return r; +} + +function f11(x: number): number { + let r = x; + try { + if (x < 0) { + throw "negative 11"; + } + + r = x * 12 + 11; + } catch (e) { + r = -1; + } + + return r; +} + +function f12(x: number): number { + let r = x; + try { + if (x < 0) { + throw "negative 12"; + } + + r = x * 13 + 12; + } catch (e) { + r = -1; + } + + return r; +} + +function f13(x: number): number { + let r = x; + try { + if (x < 0) { + throw "negative 13"; + } + + r = x * 14 + 13; + } catch (e) { + r = -1; + } + + return r; +} + +function f14(x: number): number { + let r = x; + try { + if (x < 0) { + throw "negative 14"; + } + + r = x * 15 + 14; + } catch (e) { + r = -1; + } + + return r; +} + +function f15(x: number): number { + let r = x; + try { + if (x < 0) { + throw "negative 15"; + } + + r = x * 16 + 15; + } catch (e) { + r = -1; + } + + return r; +} + +function f16(x: number): number { + let r = x; + try { + if (x < 0) { + throw "negative 16"; + } + + r = x * 17 + 16; + } catch (e) { + r = -1; + } + + return r; +} + +function f17(x: number): number { + let r = x; + try { + if (x < 0) { + throw "negative 17"; + } + + r = x * 18 + 17; + } catch (e) { + r = -1; + } + + return r; +} + +function f18(x: number): number { + let r = x; + try { + if (x < 0) { + throw "negative 18"; + } + + r = x * 19 + 18; + } catch (e) { + r = -1; + } + + return r; +} + +function f19(x: number): number { + let r = x; + try { + if (x < 0) { + throw "negative 19"; + } + + r = x * 20 + 19; + } catch (e) { + r = -1; + } + + return r; +} + +function f20(x: number): number { + let r = x; + try { + if (x < 0) { + throw "negative 20"; + } + + r = x * 21 + 20; + } catch (e) { + r = -1; + } + + return r; +} + +function f21(x: number): number { + let r = x; + try { + if (x < 0) { + throw "negative 21"; + } + + r = x * 22 + 21; + } catch (e) { + r = -1; + } + + return r; +} + +function f22(x: number): number { + let r = x; + try { + if (x < 0) { + throw "negative 22"; + } + + r = x * 23 + 22; + } catch (e) { + r = -1; + } + + return r; +} + +function f23(x: number): number { + let r = x; + try { + if (x < 0) { + throw "negative 23"; + } + + r = x * 24 + 23; + } catch (e) { + r = -1; + } + + return r; +} + +function f24(x: number): number { + let r = x; + try { + if (x < 0) { + throw "negative 24"; + } + + r = x * 25 + 24; + } catch (e) { + r = -1; + } + + return r; +} + +function f25(x: number): number { + let r = x; + try { + if (x < 0) { + throw "negative 25"; + } + + r = x * 26 + 25; + } catch (e) { + r = -1; + } + + return r; +} + +function f26(x: number): number { + let r = x; + try { + if (x < 0) { + throw "negative 26"; + } + + r = x * 27 + 26; + } catch (e) { + r = -1; + } + + return r; +} + +function f27(x: number): number { + let r = x; + try { + if (x < 0) { + throw "negative 27"; + } + + r = x * 28 + 27; + } catch (e) { + r = -1; + } + + return r; +} + +function f28(x: number): number { + let r = x; + try { + if (x < 0) { + throw "negative 28"; + } + + r = x * 29 + 28; + } catch (e) { + r = -1; + } + + return r; +} + +function f29(x: number): number { + let r = x; + try { + if (x < 0) { + throw "negative 29"; + } + + r = x * 30 + 29; + } catch (e) { + r = -1; + } + + return r; +} + +function f30(x: number): number { + let r = x; + try { + if (x < 0) { + throw "negative 30"; + } + + r = x * 31 + 30; + } catch (e) { + r = -1; + } + + return r; +} + +function f31(x: number): number { + let r = x; + try { + if (x < 0) { + throw "negative 31"; + } + + r = x * 32 + 31; + } catch (e) { + r = -1; + } + + return r; +} + +function f32(x: number): number { + let r = x; + try { + if (x < 0) { + throw "negative 32"; + } + + r = x * 33 + 32; + } catch (e) { + r = -1; + } + + return r; +} + +function f33(x: number): number { + let r = x; + try { + if (x < 0) { + throw "negative 33"; + } + + r = x * 34 + 33; + } catch (e) { + r = -1; + } + + return r; +} + +function f34(x: number): number { + let r = x; + try { + if (x < 0) { + throw "negative 34"; + } + + r = x * 35 + 34; + } catch (e) { + r = -1; + } + + return r; +} + +function f35(x: number): number { + let r = x; + try { + if (x < 0) { + throw "negative 35"; + } + + r = x * 36 + 35; + } catch (e) { + r = -1; + } + + return r; +} + +function f36(x: number): number { + let r = x; + try { + if (x < 0) { + throw "negative 36"; + } + + r = x * 37 + 36; + } catch (e) { + r = -1; + } + + return r; +} + +function f37(x: number): number { + let r = x; + try { + if (x < 0) { + throw "negative 37"; + } + + r = x * 38 + 37; + } catch (e) { + r = -1; + } + + return r; +} + +function f38(x: number): number { + let r = x; + try { + if (x < 0) { + throw "negative 38"; + } + + r = x * 39 + 38; + } catch (e) { + r = -1; + } + + return r; +} + +function f39(x: number): number { + let r = x; + try { + if (x < 0) { + throw "negative 39"; + } + + r = x * 40 + 39; + } catch (e) { + r = -1; + } + + return r; +} + +function f40(x: number): number { + let r = x; + try { + if (x < 0) { + throw "negative 40"; + } + + r = x * 41 + 40; + } catch (e) { + r = -1; + } + + return r; +} + +function f41(x: number): number { + let r = x; + try { + if (x < 0) { + throw "negative 41"; + } + + r = x * 42 + 41; + } catch (e) { + r = -1; + } + + return r; +} + +function f42(x: number): number { + let r = x; + try { + if (x < 0) { + throw "negative 42"; + } + + r = x * 43 + 42; + } catch (e) { + r = -1; + } + + return r; +} + +function f43(x: number): number { + let r = x; + try { + if (x < 0) { + throw "negative 43"; + } + + r = x * 44 + 43; + } catch (e) { + r = -1; + } + + return r; +} + +function f44(x: number): number { + let r = x; + try { + if (x < 0) { + throw "negative 44"; + } + + r = x * 45 + 44; + } catch (e) { + r = -1; + } + + return r; +} + +function f45(x: number): number { + let r = x; + try { + if (x < 0) { + throw "negative 45"; + } + + r = x * 46 + 45; + } catch (e) { + r = -1; + } + + return r; +} + +function f46(x: number): number { + let r = x; + try { + if (x < 0) { + throw "negative 46"; + } + + r = x * 47 + 46; + } catch (e) { + r = -1; + } + + return r; +} + +function f47(x: number): number { + let r = x; + try { + if (x < 0) { + throw "negative 47"; + } + + r = x * 48 + 47; + } catch (e) { + r = -1; + } + + return r; +} + +function f48(x: number): number { + let r = x; + try { + if (x < 0) { + throw "negative 48"; + } + + r = x * 49 + 48; + } catch (e) { + r = -1; + } + + return r; +} + +function f49(x: number): number { + let r = x; + try { + if (x < 0) { + throw "negative 49"; + } + + r = x * 50 + 49; + } catch (e) { + r = -1; + } + + return r; +} + +function f50(x: number): number { + let r = x; + try { + if (x < 0) { + throw "negative 50"; + } + + r = x * 51 + 50; + } catch (e) { + r = -1; + } + + return r; +} + +function f51(x: number): number { + let r = x; + try { + if (x < 0) { + throw "negative 51"; + } + + r = x * 52 + 51; + } catch (e) { + r = -1; + } + + return r; +} + +function f52(x: number): number { + let r = x; + try { + if (x < 0) { + throw "negative 52"; + } + + r = x * 53 + 52; + } catch (e) { + r = -1; + } + + return r; +} + +function f53(x: number): number { + let r = x; + try { + if (x < 0) { + throw "negative 53"; + } + + r = x * 54 + 53; + } catch (e) { + r = -1; + } + + return r; +} + +function f54(x: number): number { + let r = x; + try { + if (x < 0) { + throw "negative 54"; + } + + r = x * 55 + 54; + } catch (e) { + r = -1; + } + + return r; +} + +function f55(x: number): number { + let r = x; + try { + if (x < 0) { + throw "negative 55"; + } + + r = x * 56 + 55; + } catch (e) { + r = -1; + } + + return r; +} + +function f56(x: number): number { + let r = x; + try { + if (x < 0) { + throw "negative 56"; + } + + r = x * 57 + 56; + } catch (e) { + r = -1; + } + + return r; +} + +function f57(x: number): number { + let r = x; + try { + if (x < 0) { + throw "negative 57"; + } + + r = x * 58 + 57; + } catch (e) { + r = -1; + } + + return r; +} + +function f58(x: number): number { + let r = x; + try { + if (x < 0) { + throw "negative 58"; + } + + r = x * 59 + 58; + } catch (e) { + r = -1; + } + + return r; +} + +function f59(x: number): number { + let r = x; + try { + if (x < 0) { + throw "negative 59"; + } + + r = x * 60 + 59; + } catch (e) { + r = -1; + } + + return r; +} + +function f60(x: number): number { + let r = x; + try { + if (x < 0) { + throw "negative 60"; + } + + r = x * 61 + 60; + } catch (e) { + r = -1; + } + + return r; +} + +function f61(x: number): number { + let r = x; + try { + if (x < 0) { + throw "negative 61"; + } + + r = x * 62 + 61; + } catch (e) { + r = -1; + } + + return r; +} + +function f62(x: number): number { + let r = x; + try { + if (x < 0) { + throw "negative 62"; + } + + r = x * 63 + 62; + } catch (e) { + r = -1; + } + + return r; +} + +function f63(x: number): number { + let r = x; + try { + if (x < 0) { + throw "negative 63"; + } + + r = x * 64 + 63; + } catch (e) { + r = -1; + } + + return r; +} + +function f64(x: number): number { + let r = x; + try { + if (x < 0) { + throw "negative 64"; + } + + r = x * 65 + 64; + } catch (e) { + r = -1; + } + + return r; +} + +function f65(x: number): number { + let r = x; + try { + if (x < 0) { + throw "negative 65"; + } + + r = x * 66 + 65; + } catch (e) { + r = -1; + } + + return r; +} + +function f66(x: number): number { + let r = x; + try { + if (x < 0) { + throw "negative 66"; + } + + r = x * 67 + 66; + } catch (e) { + r = -1; + } + + return r; +} + +function f67(x: number): number { + let r = x; + try { + if (x < 0) { + throw "negative 67"; + } + + r = x * 68 + 67; + } catch (e) { + r = -1; + } + + return r; +} + +function f68(x: number): number { + let r = x; + try { + if (x < 0) { + throw "negative 68"; + } + + r = x * 69 + 68; + } catch (e) { + r = -1; + } + + return r; +} + +function f69(x: number): number { + let r = x; + try { + if (x < 0) { + throw "negative 69"; + } + + r = x * 70 + 69; + } catch (e) { + r = -1; + } + + return r; +} + +function f70(x: number): number { + let r = x; + try { + if (x < 0) { + throw "negative 70"; + } + + r = x * 71 + 70; + } catch (e) { + r = -1; + } + + return r; +} + +function f71(x: number): number { + let r = x; + try { + if (x < 0) { + throw "negative 71"; + } + + r = x * 72 + 71; + } catch (e) { + r = -1; + } + + return r; +} + +function f72(x: number): number { + let r = x; + try { + if (x < 0) { + throw "negative 72"; + } + + r = x * 73 + 72; + } catch (e) { + r = -1; + } + + return r; +} + +function f73(x: number): number { + let r = x; + try { + if (x < 0) { + throw "negative 73"; + } + + r = x * 74 + 73; + } catch (e) { + r = -1; + } + + return r; +} + +function f74(x: number): number { + let r = x; + try { + if (x < 0) { + throw "negative 74"; + } + + r = x * 75 + 74; + } catch (e) { + r = -1; + } + + return r; +} + +function f75(x: number): number { + let r = x; + try { + if (x < 0) { + throw "negative 75"; + } + + r = x * 76 + 75; + } catch (e) { + r = -1; + } + + return r; +} + +function f76(x: number): number { + let r = x; + try { + if (x < 0) { + throw "negative 76"; + } + + r = x * 77 + 76; + } catch (e) { + r = -1; + } + + return r; +} + +function f77(x: number): number { + let r = x; + try { + if (x < 0) { + throw "negative 77"; + } + + r = x * 78 + 77; + } catch (e) { + r = -1; + } + + return r; +} + +function f78(x: number): number { + let r = x; + try { + if (x < 0) { + throw "negative 78"; + } + + r = x * 79 + 78; + } catch (e) { + r = -1; + } + + return r; +} + +function f79(x: number): number { + let r = x; + try { + if (x < 0) { + throw "negative 79"; + } + + r = x * 80 + 79; + } catch (e) { + r = -1; + } + + return r; +} + +function table(): string[] { + return [ + "row 000 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", + "row 001 bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", + "row 002 cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc", + "row 003 dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd", + "row 004 eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee", + "row 005 ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + "row 006 gggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg", + "row 007 hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh", + "row 008 iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii", + "row 009 jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj", + "row 010 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", + "row 011 bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", + "row 012 cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc", + "row 013 dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd", + "row 014 eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee", + "row 015 ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + "row 016 gggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg", + "row 017 hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh", + "row 018 iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii", + "row 019 jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj", + "row 020 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", + "row 021 bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", + "row 022 cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc", + "row 023 dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd", + "row 024 eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee", + "row 025 ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + "row 026 gggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg", + "row 027 hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh", + "row 028 iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii", + "row 029 jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj", + "row 030 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", + "row 031 bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", + "row 032 cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc", + "row 033 dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd", + "row 034 eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee", + "row 035 ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + "row 036 gggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg", + "row 037 hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh", + "row 038 iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii", + "row 039 jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj", + "row 040 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", + "row 041 bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", + "row 042 cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc", + "row 043 dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd", + "row 044 eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee", + "row 045 ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + "row 046 gggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg", + "row 047 hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh", + "row 048 iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii", + "row 049 jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj", + "row 050 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", + "row 051 bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", + "row 052 cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc", + "row 053 dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd", + "row 054 eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee", + "row 055 ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + "row 056 gggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg", + "row 057 hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh", + "row 058 iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii", + "row 059 jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj", + "row 060 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", + "row 061 bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", + "row 062 cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc", + "row 063 dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd", + "row 064 eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee", + "row 065 ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + "row 066 gggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg", + "row 067 hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh", + "row 068 iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii", + "row 069 jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj", + "row 070 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", + "row 071 bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", + "row 072 cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc", + "row 073 dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd", + "row 074 eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee", + "row 075 ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + "row 076 gggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg", + "row 077 hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh", + "row 078 iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii", + "row 079 jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj", + "row 080 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", + "row 081 bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", + "row 082 cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc", + "row 083 dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd", + "row 084 eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee", + "row 085 ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + "row 086 gggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg", + "row 087 hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh", + "row 088 iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii", + "row 089 jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj", + "row 090 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", + "row 091 bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", + "row 092 cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc", + "row 093 dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd", + "row 094 eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee", + "row 095 ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + "row 096 gggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg", + "row 097 hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh", + "row 098 iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii", + "row 099 jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj", + "row 100 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", + "row 101 bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", + "row 102 cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc", + "row 103 dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd", + "row 104 eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee", + "row 105 ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + "row 106 gggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg", + "row 107 hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh", + "row 108 iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii", + "row 109 jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj", + "row 110 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", + "row 111 bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", + "row 112 cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc", + "row 113 dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd", + "row 114 eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee", + "row 115 ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + "row 116 gggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg", + "row 117 hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh", + "row 118 iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii", + "row 119 jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj", + "row 120 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", + "row 121 bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", + "row 122 cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc", + "row 123 dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd", + "row 124 eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee", + "row 125 ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + "row 126 gggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg", + "row 127 hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh", + "row 128 iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii", + "row 129 jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj", + "row 130 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", + "row 131 bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", + "row 132 cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc", + "row 133 dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd", + "row 134 eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee", + "row 135 ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + "row 136 gggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg", + "row 137 hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh", + "row 138 iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii", + "row 139 jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj", + "row 140 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", + "row 141 bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", + "row 142 cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc", + "row 143 dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd", + "row 144 eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee", + "row 145 ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + "row 146 gggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg", + "row 147 hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh", + "row 148 iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii", + "row 149 jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj", + "row 150 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", + "row 151 bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", + "row 152 cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc", + "row 153 dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd", + "row 154 eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee", + "row 155 ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + "row 156 gggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg", + "row 157 hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh", + "row 158 iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii", + "row 159 jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj", + "row 160 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", + "row 161 bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", + "row 162 cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc", + "row 163 dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd", + "row 164 eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee", + "row 165 ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + "row 166 gggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg", + "row 167 hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh", + "row 168 iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii", + "row 169 jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj", + "row 170 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", + "row 171 bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", + "row 172 cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc", + "row 173 dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd", + "row 174 eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee", + "row 175 ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + "row 176 gggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg", + "row 177 hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh", + "row 178 iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii", + "row 179 jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj", + "row 180 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", + "row 181 bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", + "row 182 cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc", + "row 183 dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd", + "row 184 eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee", + "row 185 ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + "row 186 gggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg", + "row 187 hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh", + "row 188 iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii", + "row 189 jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj", + "row 190 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", + "row 191 bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", + "row 192 cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc", + "row 193 dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd", + "row 194 eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee", + "row 195 ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + "row 196 gggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg", + "row 197 hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh", + "row 198 iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii", + "row 199 jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj", + ]; +} + +function main() { + let total: number = 0; + total += f0(1); + total += f1(1); + total += f2(1); + total += f3(1); + total += f4(1); + total += f5(1); + total += f6(1); + total += f7(1); + total += f8(1); + total += f9(1); + total += f10(1); + total += f11(1); + total += f12(1); + total += f13(1); + total += f14(1); + total += f15(1); + total += f16(1); + total += f17(1); + total += f18(1); + total += f19(1); + total += f20(1); + total += f21(1); + total += f22(1); + total += f23(1); + total += f24(1); + total += f25(1); + total += f26(1); + total += f27(1); + total += f28(1); + total += f29(1); + total += f30(1); + total += f31(1); + total += f32(1); + total += f33(1); + total += f34(1); + total += f35(1); + total += f36(1); + total += f37(1); + total += f38(1); + total += f39(1); + total += f40(1); + total += f41(1); + total += f42(1); + total += f43(1); + total += f44(1); + total += f45(1); + total += f46(1); + total += f47(1); + total += f48(1); + total += f49(1); + total += f50(1); + total += f51(1); + total += f52(1); + total += f53(1); + total += f54(1); + total += f55(1); + total += f56(1); + total += f57(1); + total += f58(1); + total += f59(1); + total += f60(1); + total += f61(1); + total += f62(1); + total += f63(1); + total += f64(1); + total += f65(1); + total += f66(1); + total += f67(1); + total += f68(1); + total += f69(1); + total += f70(1); + total += f71(1); + total += f72(1); + total += f73(1); + total += f74(1); + total += f75(1); + total += f76(1); + total += f77(1); + total += f78(1); + total += f79(1); + + assert(total == 6400, "the functions"); + assert(f0(-1) == -1, "the catch"); + assert(table().length == 200, "the strings"); + print("done."); +} diff --git a/tslang/tslang/jit.cpp b/tslang/tslang/jit.cpp index 388e9873b..f05dfea13 100644 --- a/tslang/tslang/jit.cpp +++ b/tslang/tslang/jit.cpp @@ -17,8 +17,10 @@ #include "llvm/Support/ConvertUTF.h" #include "llvm/Support/DynamicLibrary.h" #include "llvm/Support/FileSystem.h" +#include "llvm/Support/MathExtras.h" #include "llvm/Support/Memory.h" #include "llvm/Support/Path.h" +#include "llvm/Support/Process.h" #include #include @@ -161,7 +163,15 @@ static std::deque jitImages; // __CxxFrameHandler3 personality referenced from .xdata — RTDyld materializes a // jump thunk in the *referencing* section's stub area, and the OS exception // dispatcher calls through it. Read-only sections therefore must be mapped -// executable; route them through the code allocator. +// executable: they are allocated from the read-only pool like any, and their pages +// made read+execute in finalizeMemory. +// +// They used to be routed through the code allocator instead. The reservation (4) sizes +// the code pool for the code alone, so a program whose read-only data outgrew the +// page-rounding slack overflowed it, and the rest went to a fresh mapping wherever the OS +// put it - below the reserved block in about a third of runs, where an ADDR32NB +// relocation to it falls below the image base: "LLVM ERROR: IMAGE_REL_AMD64_ADDR32NB +// relocation requires an ordered section layout". class JitSectionMemoryManager : public llvm::SectionMemoryManager { using GCRootsFn = void (*)(void *, void *); @@ -188,6 +198,28 @@ class JitSectionMemoryManager : public llvm::SectionMemoryManager { } + // The size of the block the base reserves, as it computes it (SectionMemoryManager.cpp), for + // the check in finalizeMemory: a manager serves one object, so this is called once. + void reserveAllocationSpace(uintptr_t codeSize, llvm::Align codeAlign, uintptr_t roDataSize, llvm::Align roDataAlign, + uintptr_t rwDataSize, llvm::Align rwDataAlign) override + { + static const uint64_t pageSize = llvm::sys::Process::getPageSizeEstimate(); + auto required = [&](uintptr_t size, llvm::Align align) -> uint64_t { + if (size == 0) + { + return 0; + } + + auto alignment = llvm::Align(std::max(align.value(), 8)); + return llvm::alignTo(llvm::alignTo(size, alignment) + alignment.value(), pageSize); + }; + + reservedSize = reservedSize == 0 && sectionsEnd == 0 + ? required(codeSize, codeAlign) + required(roDataSize, roDataAlign) + required(rwDataSize, rwDataAlign) + : 0; + llvm::SectionMemoryManager::reserveAllocationSpace(codeSize, codeAlign, roDataSize, roDataAlign, rwDataSize, rwDataAlign); + } + uint8_t *allocateCodeSection(uintptr_t size, unsigned alignment, unsigned sectionID, llvm::StringRef sectionName) override { @@ -199,16 +231,16 @@ class JitSectionMemoryManager : public llvm::SectionMemoryManager uint8_t *allocateDataSection(uintptr_t size, unsigned alignment, unsigned sectionID, llvm::StringRef sectionName, bool isReadOnly) override { + auto *addr = llvm::SectionMemoryManager::allocateDataSection(size, alignment, sectionID, sectionName, isReadOnly); + noteSectionAddress(addr, size); #ifdef _WIN64 - if (isReadOnly) + if (isReadOnly && addr != nullptr) { // see (3): unwind personality thunks live in read-only sections - return allocateCodeSection(size, alignment, sectionID, sectionName); + readOnlySections.push_back({addr, size}); } #endif - auto *addr = llvm::SectionMemoryManager::allocateDataSection(size, alignment, sectionID, sectionName, isReadOnly); - noteSectionAddress(addr, size); if (!isReadOnly && addr != nullptr) { if (auto addRoots = reinterpret_cast( @@ -223,6 +255,49 @@ class JitSectionMemoryManager : public llvm::SectionMemoryManager } #ifdef _WIN64 + // see (3): the base makes the read-only sections read-only; their pages hold nothing else (the + // pool is page-aligned, in the reservation (4) as in a mapping of its own), so they become + // read+execute as they are + bool finalizeMemory(std::string *errMsg = nullptr) override + { + if (llvm::SectionMemoryManager::finalizeMemory(errMsg)) + { + return true; + } + + // Every section is meant to come from the reserved block (4). One that did not was given a + // mapping of its own wherever the OS put it, and one below the block falls below the image + // base its ADDR32NB relocations are resolved against - the failure (3) describes, in some + // runs only. Its spans and the block's are page-aligned, and the block starts at its first + // section, so a span wider than the block always means one went outside it: this says so + // in every run, as a warning, rather than failing in a few. + if (reservedSize > 0 && sectionsEnd - imageBase > reservedSize) + { + llvm::errs() << "warning: a JIT'd section is outside the block reserved for its object (" << (sectionsEnd - imageBase) + << " bytes used, " << reservedSize << " reserved)\n"; + } + + static const auto pageSize = llvm::sys::Process::getPageSizeEstimate(); + for (auto §ion : readOnlySections) + { + auto start = llvm::alignDown(reinterpret_cast(section.first), pageSize); + auto end = llvm::alignTo(reinterpret_cast(section.first) + section.second, pageSize); + llvm::sys::MemoryBlock block(reinterpret_cast(start), end - start); + if (auto ec = llvm::sys::Memory::protectMappedMemory(block, llvm::sys::Memory::MF_READ | llvm::sys::Memory::MF_EXEC)) + { + if (errMsg) + { + *errMsg = ec.message(); + } + + return true; + } + } + + readOnlySections.clear(); + return false; + } + void registerEHFrames(uint8_t *addr, uint64_t loadAddr, size_t size) override { auto entryCount = size / sizeof(RUNTIME_FUNCTION); @@ -273,6 +348,8 @@ class JitSectionMemoryManager : public llvm::SectionMemoryManager imageBase = reinterpret_cast(addr); } + sectionsEnd = (std::max)(sectionsEnd, reinterpret_cast(addr) + size); + #ifdef _WIN64 // a memory manager serves one object: its image is the span of its sections if (image == nullptr) @@ -287,12 +364,15 @@ class JitSectionMemoryManager : public llvm::SectionMemoryManager } uint64_t imageBase = 0; + uint64_t sectionsEnd = 0; + uint64_t reservedSize = 0; #ifdef _WIN64 JitImage *image = nullptr; #endif mlir::SmallVector> gcRootSections; #ifdef _WIN64 mlir::SmallVector functionTables; + mlir::SmallVector> readOnlySections; #endif }; From 77e89fe776502886b777f721de914ae3c84ab394 Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Thu, 5 Nov 2026 15:28:58 +0000 Subject: [PATCH 5/5] Tester: register the atomics, inference, alias/overload and JIT layout regression tests Co-Authored-By: Claude Opus 5.5 --- tslang/test/tester/CMakeLists.txt | 66 +++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/tslang/test/tester/CMakeLists.txt b/tslang/test/tester/CMakeLists.txt index 28cfd8b04..3b35cca7c 100644 --- a/tslang/test/tester/CMakeLists.txt +++ b/tslang/test/tester/CMakeLists.txt @@ -917,6 +917,12 @@ tslang_add_test(NAME test-compile-nbody COMMAND test-runner "${PROJECT_SOURCE_DI tslang_add_test(NAME test-compile-no-main COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/no_main.ts") tslang_add_test(NAME test-compile-parser COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/parser.ts") tslang_add_test(NAME test-compile-internals COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/internals.ts") +tslang_add_test(NAME test-compile-00-atomic-builtins COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00atomic_builtins.ts") +tslang_add_test(NAME test-compile-00-generic-literal-inference COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00generic_literal_inference.ts") +tslang_add_test(NAME test-compile-00-new-alias-and-ctor-overloads COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00new_alias_and_ctor_overloads.ts") +tslang_add_test(NAME test-compile-own-00-atomic-builtins COMMAND test-runner -mm=own "${PROJECT_SOURCE_DIR}/test/tester/tests/00atomic_builtins.ts") +tslang_add_test(NAME test-compile-own-00-generic-literal-inference COMMAND test-runner -mm=own "${PROJECT_SOURCE_DIR}/test/tester/tests/00generic_literal_inference.ts") +tslang_add_test(NAME test-compile-own-00-new-alias-and-ctor-overloads COMMAND test-runner -mm=own "${PROJECT_SOURCE_DIR}/test/tester/tests/00new_alias_and_ctor_overloads.ts") tslang_add_test(NAME test-compile-load-store-decorators COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/load_store_decorators.ts") tslang_add_test(NAME test-compile-reference-null-bug COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00reference_null_bug.ts") tslang_add_test(NAME test-compile-reference-index-bug COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00reference_index_bug.ts") @@ -1562,6 +1568,12 @@ tslang_add_test(NAME test-jit-nbody COMMAND test-runner -jit "${PROJECT_SOURCE_D tslang_add_test(NAME test-jit-no-main COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/no_main.ts") tslang_add_test(NAME test-jit-parser COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/parser.ts") tslang_add_test(NAME test-jit-internals COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/internals.ts") +tslang_add_test(NAME test-jit-00-atomic-builtins COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00atomic_builtins.ts") +tslang_add_test(NAME test-jit-00-generic-literal-inference COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00generic_literal_inference.ts") +tslang_add_test(NAME test-jit-00-new-alias-and-ctor-overloads COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00new_alias_and_ctor_overloads.ts") +tslang_add_test(NAME test-jit-own-00-atomic-builtins COMMAND test-runner -jit -mm=own "${PROJECT_SOURCE_DIR}/test/tester/tests/00atomic_builtins.ts") +tslang_add_test(NAME test-jit-own-00-generic-literal-inference COMMAND test-runner -jit -mm=own "${PROJECT_SOURCE_DIR}/test/tester/tests/00generic_literal_inference.ts") +tslang_add_test(NAME test-jit-own-00-new-alias-and-ctor-overloads COMMAND test-runner -jit -mm=own "${PROJECT_SOURCE_DIR}/test/tester/tests/00new_alias_and_ctor_overloads.ts") tslang_add_test(NAME test-jit-load-store-decorators COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/load_store_decorators.ts") tslang_add_test(NAME test-jit-reference-null-bug COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00reference_null_bug.ts") tslang_add_test(NAME test-jit-reference-index-bug COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00reference_index_bug.ts") @@ -2153,6 +2165,9 @@ tslang_add_test(NAME test-jit-none-strings COMMAND test-runner -jit -mm=none "${ # every single-file test the default model runs, in either tier set(TSLANG_CORPUS + 00atomic_builtins.ts + 00generic_literal_inference.ts + 00new_alias_and_ctor_overloads.ts 00any_truthiness_objects.ts 00add_promotes_both_operands.ts 00conditional_owned_result.ts @@ -3272,6 +3287,57 @@ foreach(call_arity_case ${call_arity_cases}) FAIL_REGULAR_EXPRESSION "Stack dump|Assertion failed") endforeach() +# The atomic builtins (atomicrmw, cmpxchg, atomicload, atomicstore, fence) take their op code, +# orderings, alignment and flags as constants. A variable one crashed tslang, and a constant LLVM +# doesn't take was reported by LLVM's verifier at `:0`; a wrong argument count said only +# "failed statement". Each file in atomic-errors/ makes one bad call; the test passes when the +# compiler reports it at the call. Plain add_test: these compile nothing. +set(atomic_error_cases + "op_variable|op_variable\\.ts:3:12: error: atomicrmw: the op code must be a constant integer" + "op_range|op_range\\.ts:3:12: error: atomicrmw: the op code 99 is not one of 0 \\(xchg\\)" + "rmw_ordering|rmw_ordering\\.ts:3:12: error: atomicrmw: the ordering 1 \\(unordered\\) is not allowed here. expected 2 \\(monotonic\\)" + "ordering_hole|ordering_hole\\.ts:3:12: error: atomicrmw: the ordering 3 is not allowed here" + "rmw_arguments|rmw_arguments\\.ts:3:12: error: expected atomicrmw\\(op, ref, value, ordering., syncscope., alignment., volatile...\\), but got 3 arguments" + "rmw_float_op_on_int|rmw_float_op_on_int\\.ts:3:12: error: atomicrmw: the reference points at 'i32', expected a float" + "rmw_int_op_on_float|rmw_int_op_on_float\\.ts:3:12: error: atomicrmw: the reference points at '!ts\\.number', expected an 8, 16, 32 or 64-bit integer" + "cmpxchg_failure_ordering|cmpxchg_failure_ordering\\.ts:3:12: error: cmpxchg: the failure ordering 5 \\(release\\) is not allowed here" + "cmpxchg_float|cmpxchg_float\\.ts:3:12: error: cmpxchg: the reference points at '!ts\\.number'" + "cmpxchg_arguments|cmpxchg_arguments\\.ts:3:12: error: expected cmpxchg\\(ref, expected, value, successOrdering, failureOrdering.*but got 4 arguments" + "fence_ordering|fence_ordering\\.ts:2:5: error: fence: the ordering 2 \\(monotonic\\) is not allowed here" + "fence_variable|fence_variable\\.ts:2:5: error: fence: the ordering must be a constant integer" + "load_ordering|load_ordering\\.ts:3:12: error: atomicload: the ordering 5 \\(release\\) is not allowed here" + "store_ordering|store_ordering\\.ts:3:5: error: atomicstore: the ordering 4 \\(acquire\\) is not allowed here" + "alignment|alignment\\.ts:3:12: error: atomicload: the alignment 3 is not a power of two" + "volatile_variable|volatile_variable\\.ts:3:12: error: atomicload: the volatile flag must be a constant boolean" + "not_a_reference|not_a_reference\\.ts:3:12: error: atomicload: expected a reference \\(ReferenceOf\\(x\\)\\), but got 'i32'" + "boolean_reference|boolean_reference\\.ts:3:12: error: atomicload: the reference points at '!ts\\.boolean'") +foreach(atomic_error_case ${atomic_error_cases}) + string(FIND "${atomic_error_case}" "|" atomic_error_sep) + string(SUBSTRING "${atomic_error_case}" 0 ${atomic_error_sep} atomic_error_name) + math(EXPR atomic_error_sep "${atomic_error_sep} + 1") + string(SUBSTRING "${atomic_error_case}" ${atomic_error_sep} -1 atomic_error_message) + string(REPLACE "_" "-" atomic_error_slug "${atomic_error_name}") + add_test(NAME test-compile-atomic-errors-${atomic_error_slug} + COMMAND $ --emit=obj --no-default-lib -mm=none + "${PROJECT_SOURCE_DIR}/test/tester/atomic-errors/${atomic_error_name}.ts" + -o "${CMAKE_CURRENT_BINARY_DIR}/atomic-errors-${atomic_error_slug}.obj") + set_tests_properties(test-compile-atomic-errors-${atomic_error_slug} + PROPERTIES PASS_REGULAR_EXPRESSION "${atomic_error_message}" + FAIL_REGULAR_EXPRESSION "Stack dump|Assertion failed|") +endforeach() + +# A JIT'd object with more read-only data than its code reservation has room to spare (see +# tests/00jit_rodata_layout.ts). Read-only sections came from the code pool and spilled out of the +# reserved block, below it in some runs: "IMAGE_REL_AMD64_ADDR32NB relocation requires an ordered +# section layout". The JIT warns of a section outside the block in every run that spills, so this +# fails every time rather than in a few. --jit-cache=false: one object, compiled each run. +add_test(NAME test-jit-rodata-layout + COMMAND $ --emit=jit -mm=none --no-default-lib --jit-cache=false + "${PROJECT_SOURCE_DIR}/test/tester/tests/00jit_rodata_layout.ts") +set_tests_properties(test-jit-rodata-layout + PROPERTIES PASS_REGULAR_EXPRESSION "done\\." + FAIL_REGULAR_EXPRESSION "outside the block reserved|LLVM ERROR|Stack dump") + # `@final` (docs/superpowers/specs/2026-10-19-final-classes-design.md): a class nobody may extend. # Each file in final/ breaks one rule; the test passes when the compiler prints the message. set(final_error_cases