Repository navigation
m3front: define the static link on both arms of a closure call; cm3: pass debug/optimize to the C back end (fixes #1243) - #1244
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>
|
This is not |
sorry I don't know how this works. Do I need to log in with my maintainer credentials and approve something? Can you approve it? |
|
I - can't. I don't have _any_ rights on cm3 repo.
This is annoying -(
- - -
Yes: you can logon and press something.
( In fact I don't know what exactly - see Ok part 1 of msg.)
…________________________________
From: Mika Nystrom ***@***.***>
Sent: Wednesday, October 28, 2026 8:37 PM
To: modula3/cm3 ***@***.***>
Cc: VictorMiasnikov ***@***.***>; Comment ***@***.***>
Subject: Re: [modula3/cm3] m3front: define the static link on both arms of a closure call; cm3: pass debug/optimize to the C back end (fixes #1243) (PR #1244)
[https://avatars.githubusercontent.com/u/254522122?s=20&v=4]mikan-cerebras left a comment (modula3/cm3#1244)<#1244 (comment)>
This is not https://github.com/modula3/cm3/actions/runs/40608654077 runned?
sorry I don't know how this works. Do I need to log in with my maintainer credentials and approve something? Can you approve it?
—
Reply to this email directly, view it on GitHub<#1244?email_source=notifications&email_token=ALUI7HGHSGS2ECLDPE7PHML5WIVETA5CNFSNUABFM5UWIORPF5TWS5BNNB2WEL2JONZXKZKDN5WW2ZLOOQXTMNBXGYYTIOBUGMZ2M4TFMFZW63VHMNXW23LFNZ2KKZLWMVXHJLDGN5XXIZLSL5RWY2LDNM#issuecomment-6476148433>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/ALUI7HHX4BYFXV2E7BOCTT35WIVETAVCNFSNUABDKJSXA33TNF2G64TZHM4TKOBTHE4DMO2JONZXKZJ3GYYDOMBQGI3TEMZQUF3AE>.
You are receiving this because you commented.Message ID: ***@***.***>
|
I can't You can. Yes: first logon, and... I don't know details -( |
|
( Empty msg. May will be deleted later by me) |
|
( Thank! Have you seen this? |
|
( Empty msg on 2026-11-19. May will be deleted later by me) |
|
It is best if Rodney fixes this problem. In the llvm backend there is some tenuous code to remove |
Ok |
|
The messaging systems, m3-devel, github, etc. are leaving me a bit lost as to what I
need to address. For example, I can't find a #1244, mentioned several times on github.
The open issues there skip from #1243 to #1248 and closed from #1239 to @ 1246.
Is there some other numbering I need to look at?
…On 10/29/26 03:56, peter mckinna wrote:
*demoitem* left a comment (modula3/cm3#1244) <#1244 (comment)>
It is best if Rodney fixes this problem. In the llvm backend there is some tenuous code to remove
instructions in the call invoke direct procedure based on knowledge of the emission of code from the
front end. In fact a comment recommends review of the front end in this case of a closure call.
Mika has in effect fixed the front end and now we have to fix any backends that had work arounds for
this situation. Its best to leave this to an expert. I'm not sure how it impacts the integrated backend
but the gcc backend seems unaffected.
—
Reply to this email directly, view it on GitHub <#1244?email_source=notifications&email_token=ABSVZNFXKVRMMYU4VRVB4235WMA3TA5CNFSNUABFM5UWIORPF5TWS5BNNB2WEL2JONZXKZKDN5WW2ZLOOQXTMNBZGIZTEMJUGM3KM4TFMFZW63VKON2WE43DOJUWEZLEUVSXMZLOOSWGM33PORSXEX3DNRUWG2Y#issuecomment-6492321436>, or unsubscribe <https://github.com/notifications/unsubscribe-auth/ABSVZNERMNX66AJGHEYDWAL5WMA3TAVCNFSNUABDKJSXA33TNF2G64TZHM4TKOBTHE4DMO2JONZXKZJ3GYYDOMBQGI3TEMZQUF3AE>.
You are receiving this because you are subscribed to this thread.Message ID: ***@***.***>
|
|
If you go to github and click pull requests Mika had one #1244 about 3
weeks ago on a branch
of his trying to get optimisations working on the C backend. A fair bit of
Claude or one of those ai's is involved I think.
Its all explained in the descriptions. There is also a fairly
lengthy discussion from Victor which is slightly
confusing. Anyway the fix makes changes to UserProc.m3 in m3front/types and
pulls the pop_static_link out
of the closure arm of the GenClosure call and puts it after the label which
I guess means its in the common
area and is called for non-closure as well. Apparently C backend
optimisations now work with the change.
But I tested it and the llvm backend would need some changes itself if the
change was merged.
On Thu, 19 Nov 2026 at 10:21, Rodney M. Bates ***@***.***>
wrote:
… *RodneyBates* left a comment (modula3/cm3#1244)
<#1244 (comment)>
The messaging systems, m3-devel, github, etc. are leaving me a bit lost as
to what I
need to address. For example, I can't find a #1244, mentioned several
times on github.
The open issues there skip from #1243 to #1248 and closed from #1239 to @
1246.
Is there some other numbering I need to look at?
On 10/29/26 03:56, peter mckinna wrote:
> *demoitem* left a comment (modula3/cm3#1244) <
#1244 (comment)>
>
> It is best if Rodney fixes this problem. In the llvm backend there is
some tenuous code to remove
> instructions in the call invoke direct procedure based on knowledge of
the emission of code from the
> front end. In fact a comment recommends review of the front end in this
case of a closure call.
> Mika has in effect fixed the front end and now we have to fix any
backends that had work arounds for
> this situation. Its best to leave this to an expert. I'm not sure how it
impacts the integrated backend
> but the gcc backend seems unaffected.
>
> —
> Reply to this email directly, view it on GitHub <
#1244?email_source=notifications&email_token=ABSVZNFXKVRMMYU4VRVB4235WMA3TA5CNFSNUABFM5UWIORPF5TWS5BNNB2WEL2JONZXKZKDN5WW2ZLOOQXTMNBZGIZTEMJUGM3KM4TFMFZW63VKON2WE43DOJUWEZLEUVSXMZLOOSWGM33PORSXEX3DNRUWG2Y#issuecomment-6492321436>,
or unsubscribe <
https://github.com/notifications/unsubscribe-auth/ABSVZNERMNX66AJGHEYDWAL5WMA3TAVCNFSNUABDKJSXA33TNF2G64TZHM4TKOBTHE4DMO2JONZXKZJ3GYYDOMBQGI3TEMZQUF3AE>.
> You are receiving this because you are subscribed to this thread.Message
ID: ***@***.***>
>
—
Reply to this email directly, view it on GitHub
<#1244?email_source=notifications&email_token=ABALZX7MFAT324JCYWLAEWD5ZYVGJA5CNFSNUABFM5UWIORPF5TWS5BNNB2WEL2JONZXKZKDN5WW2ZLOOQXTMOJZGQ2TINZZGU2KM4TFMFZW63VHMNXW23LFNZ2KKZLWMVXHJLDGN5XXIZLSL5RWY2LDNM#issuecomment-6994547954>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ABALZX54Y46M5QBWBFZ5OIT5ZYVGJAVCNFSNUABDKJSXA33TNF2G64TZHM4TKOBTHE4DMO2JONZXKZJ3GYYDOMBQGI3TEMZQUF3AE>
.
You are receiving this because you commented.Message ID:
***@***.***>
|
|
@RodneyBates, #1244 is a pull request rather than an issue, so it's listed under the Pull requests tab, not under Issues. Issues and PRs share one numbering sequence, which is why the issue list jumps from #1243 to #1248. You're on it now: #1244 (branch Summary: in What needs you: the |
- Status table: rows for the master merge (in progress), optimised m3tests for MSIRObj (not done; #1243 only showed at -O2), and EXTENDED on AMD64_LINUX (unvalidated fp128 lowering). - O18: closure-call lowering loads the procedure's first word to test CL_marker. Not the #1244 defect, but it reads code bytes and skips CG's NIL/alignment pre-checks; options are CG-style pre-checks or a tagged closure pointer. - O19: target triple and data layout are hard-coded in MSIREmit.BeginUnit; propose one definition shared with the m3cg LLVM backend. - New "Merge from master (2026-11)" section: merge base and the two expected-output conflicts, files changed on both sides with the MSIR paths at risk, other relevant master changes, and a before/after results table to fill in. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
( Empty msg N2 on 2026-11-19. May will be deleted later by me) |
I've fix this in my branch
Of course, please use current prefix of pkg ( |
|
On 11/18/26 20:23, Antony Hosking wrote:
*hosking* left a comment (modula3/cm3#1244) <#1244 (comment)>
@RodneyBates <https://github.com/RodneyBates>, #1244 <#1244> is a pull request rather than an issue, so it's listed under the *Pull requests* tab, not under Issues. Issues and PRs share one numbering sequence, which is why the issue list jumps from #1243 <#1243> to #1248 <#1248>. You're on it now: #1244 <#1244> (branch |fix-undefined-static-link|, from @mikan-cerebras <https://github.com/mikan-cerebras>).
Summary: in |UserProc.GenClosureCall|, |Pop_static_link| is only emitted on the closure arm, but the indirect call after the join reads the static link on both arms. Clang at |-O2| treats the unwritten value as |undef| and folds the closure test away, so plain procedure values get called as closures. The fix defines the static link on both arms (details in #1243 <#1243>). A second commit passes the unit's debug/optimize flags through to the C back end instead of hard-coding them.
What needs you: the |AMD64_LINUX / build-and-test (llvm)| job fails with |<*ASSERT*> failed: LLVM.GetInstructionOpcode(loadInst) = LLVM.Opcode.LLVMStore| at |M3CG_LLVM.m3:5323|. @demoitem <https://github.com/demoitem> thinks |M3CG_LLVM| has a workaround that depends on the old front-end output for closure calls, which now needs adjusting.
So Mika fixed the front end and now M3CG_LLVM needs its workaround removed?
I think I need to rebase rodney-new to get the front end fix and the gold/ld thing.
… The |AMD64_MINGW / build| job also fails; I haven't checked whether that's related to this PR.
—
Reply to this email directly, view it on GitHub <#1244?email_source=notifications&email_token=ABSVZNFWI73P2UNAESWGAB35ZZKSLA5CNFSNUABFM5UWIORPF5TWS5BNNB2WEL2JONZXKZKDN5WW2ZLOOQXTMOJZG43TGOJUHA4KM4TFMFZW63VHNVSW45DJN5XKKZLWMVXHJLDGN5XXIZLSL5RWY2LDNM#issuecomment-6997739488>, or unsubscribe <https://github.com/notifications/unsubscribe-auth/ABSVZNA3JDLW2JUU43YE73T5ZZKSLAVCNFSNUABDKJSXA33TNF2G64TZHM4TKOBTHE4DMO2JONZXKZJ3GYYDOMBQGI3TEMZQUF3AE>.
You are receiving this because you were mentioned.Message ID: ***@***.***>
|
|
@RodneyBates #1244 isn't merged yet, so master doesn't have the front-end fix. Its two CI failures are infrastructure (I386_LINUX's CMake toolchain, fixed by #1275, and AMD64_MINGW lacking clang), not the PR. Once it's merged, M3CG_LLVM can drop its static-link workaround. The gold/ld change (#1253) is already on master, so rebasing rodney-new onto master picks that up now. |
This micro-problem: please see detail near |
|
Correction to my comment above: I misread the checks. #1244's failures are |
…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>
|
@mikan-cerebras, @RodneyBates: this is merged. I added a third commit to the branch before merging, because the front-end change tripped the M3CG_LLVM handled an indirect call that might or might not take a static link by finding the Before merging, the combination ran green on every CI job, including the llvm build and m3tests; that was #1288, now closed. Rodney: the workaround you'd have had to adjust is gone from |
GenClosureCallemitsPop_static_linkonly inside the arm taken when the procedure value is a closure, but the indirect call after the join reads the static link on both arms. When the value is a top-level procedure the static link is therefore read having never been written.m3-sys/m3front/src/types/UserProc.m3, before:The value really is a don't-care for a top-level procedure, since the callee ignores it. But don't care and undefined are not the same thing to an optimizer.
What goes wrong
With the C back end and clang
-O2, the never-written local becomesundef:undefmeans "any value I like", so the two arms become interchangeable andSimplifyCFGPassfolds the branch, keeping theCL_markercomparison only as an assumption:After that, every suitably aligned non-NIL procedure value takes the closure path, loads
CL_procandCL_frameout of the target function's own instruction stream, and branches to the result. The resultingpcdecodes to a real instruction — in our casestp x29, x30, [sp, #0x16], the prologue of the very function that should have been called.Bisected with
-opt-bisect-limit: pass #132,SimplifyCFGPass.Why it is intermittent
The "not 8-aligned, so not a closure" pre-check survives the fold, so whether a program crashes depends on where the linker puts the target procedure. Four variants of one test differing only in a record's field count:
& 70x1000012440x1000012880x1000012f00x1000015bcAdding an unrelated field can move a program between the two.
Why the fix is here and not in the C back end
M3C.m3'sloadBeforeStoreanalysis would normally emit={0}for a read-before-write temp, but it is branch-insensitive by design — its own comment says "This knowingly imagines the IR runs in order, without branches around." Patching it would fix one back end.M3CG_LLVMandM3x86have no equivalent mitigation at all, and the undefined read originates in the front end regardless.Note also that
Pop_static_linkspills the stack top to a fresh temporary on each call, so simply calling it again before the branch does not work — it allocates a second temp and the first store is dead. The fix uses one temp, written on both arms, popped once after the join.Testing
ARM64_DARWIN, C back end, Apple clang.
-O2before and prints the correct answer after, with the target procedure forced to 8-byte alignment so the failing arm is the one exercised.-O2and stock-O1(1407,407), so the environment is still delivered correctly — this was the obvious regression risk.-O2and reproduces its reference output bit-for-bit (99,999 rows, matching SHA-256), and a checkpoint-restart that segfaulted at-O2before now completes.-O1behaviour is unchanged before and after.Related, but not this bug
The
CL_markertest itself dereferences a function pointer as data, which is undefined in C (a function is not an object, so there is nothing there to read). That is worth addressing separately, but it is not what is happening here: under opaque pointers LLVM cannot tell the pointer designates a function, and a hand transcription of the dispatch sequence into plain C does not reproduce at any optimization level. Theundefis the whole trigger.Second commit: pass the unit's debug/optimize flags to the C back end
Added after the above, because it is what makes
-O2reachable for the C back end in the first place — and therefore what exposes the bug fixed in the first commit.Builder.m3hardcodedTRUE, FALSEfordebug, optimizeat theDoRunCcall site:so with the C back end,
m3_optimizein an m3makefile and-Oon the command line were silently ignored — every build was-O0with debug forced on. Every other back end on the same path already passesu.debug, u.optimize, as do the other twoRunCCcall sites in the same file. This fixes #1243.Measured on ARM64_DARWIN with a config carrying the current
Unix.common:compile_cccCallee_m.ooptimize=-g -O0f4e0ea4a…optimize=TRUE-g -O2ec98460b…_m3main.cppis unaffected — it goes through a separate call at the bottom of the file that passesdebug := TRUE, optimize := FALSEdeliberately, and still reportsoptimize=after the change.Please take these together, or this one second
Landing this commit alone turns on
-O2for the C back end and will produce programs that call procedure parameters through a word read out of their own instruction stream, on whichever procedures the linker happens to place at a multiple of 8. The static-link fix is what makes-O2safe here. With both commits applied, the same test program compiles at-O2and produces correct results for both top-level and nested procedures.Happy to split this into two PRs if you'd prefer to review them separately — the ordering constraint is the only reason they are together.