Skip to content

M3CG_LLVM: test the static link at run time (companion to #1244) - #1288

Closed
hosking wants to merge 3 commits into
masterfrom
llvm-static-link
Closed

hosking wants to merge 3 commits into
masterfrom
llvm-static-link

Conversation

@hosking

@hosking hosking commented Nov 21, 2026 •

Copy link
Copy Markdown
Member

Companion to #1244, so that it can merge without breaking the AMD64_LINUX / build-and-test (llvm) job.

This branch is #1244's two commits, rebased onto current master, plus one M3CG_LLVM commit. It's a draft so that CI runs against the combination. If it passes, either the last commit goes onto #1244 or this PR replaces it.

The failure

#1244 makes UserProc.GenClosureCall write the static link on both arms of the closure test, NIL for a top-level procedure, and pop it once after the join. The llvm job then fails with:

<*ASSERT*> failed: LLVM.GetInstructionOpcode(loadInst) = LLVM.Opcode.LLVMStore
file "../src/M3CG_LLVM.m3", line 5323

An llvm call has a static function type, so call_invoke_indirect turns a CG indirect call that may take a static link into two calls: one with the nest parameter and one without. To choose between them it relied on what the front end used to emit:

  • if the pop_static_link was in the call's basic block, it called with nest;
  • otherwise it went back to the pop_static_link's block (the closure arm), deleted its branch, and took the code address from the load two instructions before the end.

After #1244 the pop_static_link comes after the join. Gen_Call_indirect's Check_nil then starts a new block (cm3cfg.common passes -check_procs for every llvm mode), so the call falls into the second case, and the expected load/store/branch sequence isn't there.

The change

Test the static link at run time. A NIL link means a top-level procedure, so call it without nest; otherwise call with nest. Both calls use the same code address and join in a phi, as before.

After #1244 this condition is exact:

  • GenClosureCall passes the closure's frame or NIL.
  • Marker.CallFinallyHandler, the only other Pop_static_link, passes the address of a frame. That is never NIL, so llvm folds the test away.

The old same-block shortcut also had to go. With -NoChecks there is no Check_nil, so GenClosureCall's pop and call share a block while the link may be NIL. The shortcut would then call a top-level procedure with a nest argument it doesn't declare.

The staticLinkBB field and GetBBLastInstruction are removed, since nothing else uses them.

Only llvm22 is changed, because it is the version CI builds. llvm13 and llvm18 have the same code and would need the same change if anyone still builds them.

Testing

  • M3CG_LLVM compiles cleanly against llvm22bindings. I checked this on ARM64_DARWIN with LLVM 23 headers.
  • I have no LLVM 22 machine to run the backend on. The AMD64_LINUX (llvm) job on this PR, which builds the system and runs m3tests, is the real test.

@RodneyBates: this removes the workaround you'd otherwise have to adjust for #1244. A review would be welcome, especially whether anything else depends on the old CG sequence.

🤖 Generated with Claude Code

mikan-cerebras and others added 3 commits November 22, 2026 10:55
GenClosureCall emitted Pop_static_link only inside the arm taken when
the procedure value is a closure, while the indirect call after the
join reads the static link on both arms.  For a top-level procedure
the static link was therefore read having never been written.

The value is genuinely a don't-care there -- the callee ignores it --
but "don't care" and "undefined" are not the same thing to an
optimizer.  With the C back end and clang -O2 the never-written local
becomes undef, which lets SimplifyCFG treat the two arms as
interchangeable and fold the closure test away: the CL_marker
comparison is demoted to llvm.assume, and every suitably aligned
non-NIL procedure value then takes the closure path.  It loads
CL_proc and CL_frame out of the target function's own instruction
stream and branches to the result.

Whether a given program crashes depends on where the linker places
the target procedure, because the "not 8-aligned, so not a closure"
pre-check survives the fold.  A procedure landing at 4 mod 8 escapes
through it and is called correctly; one landing at 0 mod 8 is called
through its own code.  Adding an unrelated field to a record can move
a program between the two.

Pop_static_link spills the stack top to a fresh temporary each time
it is called, so calling it a second time before the branch does not
help -- it allocates a second temp and the first store is dead.  This
uses one temp, written on both arms, popped once after the join.

Fixed in the front end rather than in the C back end's
loadBeforeStore analysis.  That analysis is branch-insensitive by
design ("This knowingly imagines the IR runs in order, without
branches around") and so does not mark this temp, but more
importantly M3CG_LLVM and M3x86 have no equivalent mitigation at all,
and the undefined read is the front end's to begin with.

Tested on ARM64_DARWIN, C back end:

  - a 27-line reproducer passing a procedure as a parameter crashes
    at -O2 before, prints the right answer after, with the target
    procedure forced to 8-byte alignment so the arm is the failing one
  - nested procedures still receive the correct static link: a closure
    reading a captured local returns identical values under patched
    -O2 and stock -O1
  - a 20k-line numerical model builds at -O2 and reproduces its
    reference output bit-for-bit, and a checkpoint-restart that
    segfaulted at -O2 before now completes
  - -O1 was unaffected throughout, before and after

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Builder.m3 hardcoded TRUE, FALSE for debug and optimize at the DoRunC
call site, so with the C back end m3_optimize in an m3makefile and -O
on the command line were silently ignored: every build was compiled
-O0 with debug forced on, whatever the m3makefile asked for.

Every other back end on the same path already passes u.debug and
u.optimize, as do the other two RunCC call sites in the same file.
Only this one did not.

The flag does reach compile_c once this is fixed, measured on
ARM64_DARWIN with a config carrying the current Unix.common:

  before:  optimize=      args=-g -O0
  after:   optimize=TRUE  args=-g -O2

and the resulting objects differ, as expected.

_m3main.cpp is unaffected: it is compiled through a separate call at
the bottom of this file that passes debug := TRUE, optimize := FALSE
deliberately, and still reports optimize= after this change.

ORDERING NOTE: this change is what exposes the undefined static link
fixed in the preceding commit.  Taking this one alone turns on -O2
for the C back end and will produce programs that call procedure
parameters through a word read out of their own instruction stream,
depending on where the linker places the target.  The two belong
together, or this one belongs after.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
…e static link

An llvm call has a static function type, so an indirect call that may or
may not take a static link becomes two calls, one with the 'nest'
parameter and one without.  M3CG_LLVM chose between them by finding the
pop_static_link's basic block and taking apart the load/store/branch the
front end emitted in the closure arm.  With the front end now writing the
static link on both arms of GenClosureCall (NIL for a top-level
procedure), the pop_static_link is after the join and that sequence is
gone, so the build asserted at M3CG_LLVM.m3:5323.

Test the static link at run time instead: NIL means a top-level
procedure.  Marker.CallFinallyHandler passes a frame address, which llvm
folds as non-NIL.  This no longer depends on the shape of CG's output.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@hosking

hosking commented Nov 22, 2026

Copy link
Copy Markdown
Member Author

Folded into #1244: the M3CG_LLVM commit was pushed onto that PR's branch and #1244 has merged. This PR's CI run tested the identical tree (all jobs green, including AMD64_LINUX (llvm) with m3tests).

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.

2 participants