Skip to content

Atomic builtins checked at the call + atomicload/atomicstore; literal vs generic T; new through an alias; JIT read-only section layout - #767

Merged
ASDAlexander77 merged 5 commits into
mainfrom
atomics-builtins-and-fixes
Nov 5, 2026
Merged

ASDAlexander77 merged 5 commits into
mainfrom
atomics-builtins-and-fixes

Conversation

@ASDAlexander77

Copy link
Copy Markdown
Owner

Compiler issues found while adding Atomics to the default library (ASDAlexander77/TypeScriptCompilerDefaultLib#64), where they were worked around. One commit per fix.

Atomic builtins (MLIRCodeLogic.h, TypeScriptOps.td, LowerToLLVM.cpp)

  1. A variable op code, ordering, syncscope, alignment or flag crashed tslang (0xC0000005): the op was built with the null attribute getIntegerAttr returned. Now an error at the call: atomicrmw: the op code must be a constant integer.
  2. Constants LLVM rejects (op code 99, an atomicrmw ordering below monotonic, a cmpxchg failure ordering of release/acq_rel, the ordering hole 3, a fence weaker than acquire) were reported by LLVM's verifier at <unknown>:0. They're checked in MLIRGen and reported at the call with the allowed values, e.g. atomicrmw: the ordering 1 (unordered) is not allowed here; expected 2 (monotonic), 4 (acquire), 5 (release), 6 (acq_rel) or 7 (seq_cst). Also checked: the reference's element type against the operation (integer ops, float ops, cmpxchg integer or pointer, no boolean), a non-reference first argument, and the alignment (a power of two).
  3. A wrong argument count said only "failed statement". It now names the signature: expected atomicrmw(op, ref, value, ordering[, syncscope[, alignment[, volatile]]]), but got 3 arguments.
  4. New builtins atomicload(ref, ordering[, syncscope[, alignment[, volatile]]]) and atomicstore(ref, value, ordering[, ...]), lowering to LLVM load atomic / store atomic, aligned naturally unless an alignment is given. Atomics.load / Atomics.store in the default lib can use them instead of or 0 / xchg.
  5. volatile and weak are optional trailing boolean arguments (a syncscope of "" and an alignment of 0 mean the defaults, for a call that only wants a flag).

Also: the value (and cmpxchg's expected value) is now cast to the type the reference points at. Before, atomicrmw(1, ReferenceOf(u16var), 1, 7) was a 32-bit atomicrmw over the u16 and its neighbour.

Type checking

  1. A literal argument no longer overrides the T another argument fixed (MLIRGenTypes.cpp zipTypeParameterWithArgument, MLIRGenGenerics.cpp): with add<T>(ta: TA<T>, i: int, v: T), add(new TA<u16>([65535]), 0, 2) is T = u16 with 2 cast to it, in either parameter order. A literal the type can't hold (300 for a u8) merges as before.
  2. new through a specialising alias (MLIRGenExpressions.cpp): type Uint8Array = TypedArray<u8>; new Uint8Array([250]) built a TypedArray<s32>; the alias's type arguments are now kept, and the constructor's arguments and the receiver don't override them. Constructor overloads (MLIRGenAccessCall.cpp): an array literal argument fits a T[] parameter as its elements fit T, so new TypedArray<u8>([250]) of a class with a second constructor(length: int) finds constructor(data: T[]).

JIT

  1. "IMAGE_REL_AMD64_ADDR32NB relocation requires an ordered section layout" (about 3 in 10 runs of the concatenated DefaultLib atomics tests under rc). JitSectionMemoryManager allocated read-only sections from the code pool, which RTDyld's reservation sizes for the code alone. An object whose read-only data outgrew the page-rounding slack spilled into a fresh mapping wherever the OS put it, and below the reserved block an ADDR32NB relocation to it fell below the image base (seen with instrumentation: the reservation at …2400000, the spilled .pdata at …2010000). Read-only sections now come from the read-only pool (Code < RO < RW inside the reserved block, as RTDyld expects), and finalizeMemory makes their pages read+execute for the .xdata personality thunks. Before: 9/30 failing runs; after: 60/60 passing.

The JIT also warns when a section lies outside the reserved block. A spill is always outside, but only a spill below crashed, and that depended on the address space, so the crash itself couldn't be reproduced by any in-tree program (I tried five shapes, 0 failures each). The warning appears every run under the old routing (checked on three programs and both memory models), so test-jit-rodata-layout fails deterministically if read-only sections ever go back to the code pool.

Tests

  • tests/00atomic_builtins.ts: every builtin and flag, the u16 cast, a weak cmpxchg retried in a loop. Compile and JIT under gc, own, rc and none.
  • atomic-errors/*.ts (18 cases): each message at its file:line:col, failing on Stack dump, Assertion failed or <unknown>.
  • tests/00generic_literal_inference.ts and tests/00new_alias_and_ctor_overloads.ts: all four models.
  • test-jit-rodata-layout (tests/00jit_rodata_layout.ts).

Verification

  • Release ctest -C Release: 6630 tests, all passed (3 disabled as before).
  • DefaultLib (main, with Merge private #64) rebuilt with this compiler, tests.ps1 release compile + jit: gc 184/184; rc, none and own 183/184 (1 gc-only skip). lib.generics.ts still passes <T>value to atomicrmw, so the new cast is a no-op there.
  • Debug ctest -C Debug --timeout 300: 161 of 6629 fail. None of the new tests is among them. The failures fall into classes in code this PR doesn't touch: inlinable function call ... must have a debug location / "Failed to emit LLVM IR" (--di), cast<Ty>() argument of incompatible type asserts, an APSInt "Too many bits for int64_t" assert in 64-bit tests (00int64_remainder, 00throw_bool_bigint), and -shared export/import link errors. I didn't build a Debug tree of main to confirm they predate this branch: Debug wasn't green before (38 known failures on 2026-10-01).

Not in this PR

  • Issue 9 (no parallel user code: await offloads but blocks, an unawaited async call is synchronous): investigated only, write-up in the session notes.
  • Follow-up for DefaultLib: Atomics.load/store can use atomicload/atomicstore, and the const x: Uint8Array = … annotations and <T> casts in its tests and lib are no longer needed.

🤖 Generated with Claude Code

ASDAlexander77 and others added 5 commits November 5, 2026 15:27
…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 <unknown>: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 <noreply@anthropic.com>
…n it holds the value

With `add<T>(ta: TA<T>, i: int, v: T)`, `add(new TA<u16>([65535]), 0, 2)` merged T = u16 with the
literal's s32 into s32, and failed with "type TA<ui16> is not assignable to type TA<si32>". 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 <noreply@anthropic.com>
… literals fit overloaded T[] constructors

`type Uint8Array = TypedArray<u8>; new Uint8Array([250])` inferred the class's type arguments
from the constructor's arguments, which made a TypedArray<s32>. 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<u8>([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 <noreply@anthropic.com>
…m 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 <noreply@anthropic.com>
…t regression tests

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ASDAlexander77
ASDAlexander77 merged commit f7883fc into main Nov 5, 2026
2 checks passed
@ASDAlexander77
ASDAlexander77 deleted the atomics-builtins-and-fixes branch November 5, 2026 17:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant