Repository navigation
Conversation
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>
Member
Author
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.GenClosureCallwrite 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:An llvm
callhas a static function type, socall_invoke_indirectturns a CG indirect call that may take a static link into two calls: one with thenestparameter and one without. To choose between them it relied on what the front end used to emit:pop_static_linkwas in the call's basic block, it called withnest;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_linkcomes after the join.Gen_Call_indirect'sCheck_nilthen starts a new block (cm3cfg.commonpasses-check_procsfor 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 withnest. Both calls use the same code address and join in a phi, as before.After #1244 this condition is exact:
GenClosureCallpasses the closure's frame or NIL.Marker.CallFinallyHandler, the only otherPop_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
-NoChecksthere is noCheck_nil, soGenClosureCall's pop and call share a block while the link may be NIL. The shortcut would then call a top-level procedure with anestargument it doesn't declare.The
staticLinkBBfield andGetBBLastInstructionare removed, since nothing else uses them.Only
llvm22is changed, because it is the version CI builds.llvm13andllvm18have the same code and would need the same change if anyone still builds them.Testing
llvm22bindings. I checked this on ARM64_DARWIN with LLVM 23 headers.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