Skip to content

m3front: define the static link on both arms of a closure call; cm3: pass debug/optimize to the C back end (fixes #1243) - #1244

Merged
hosking merged 3 commits into
modula3:masterfrom
mikan-cerebras:fix-undefined-static-link
Nov 22, 2026
Merged

hosking merged 3 commits into
modula3:masterfrom
mikan-cerebras:fix-undefined-static-link

Conversation

@mikan-cerebras

@mikan-cerebras mikan-cerebras commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

GenClosureCall emits Pop_static_link only 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:

CG.If_closure (p_temp, CG.No_label, skip, CG.Maybe);
CG.Push (p_temp);
CG.Closure_frame ();
CG.Pop_static_link ();      (* the only write -- inside the branch *)
CG.Push (p_temp);
CG.Closure_proc ();
CG.Store_temp (p_temp);
CG.Set_label (skip);        (* join *)
CG.Push (p_temp);
...
CG.Gen_Call_indirect (result, cc);   (* reads the static link on both arms *)

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 becomes undef:

%20 = phi ptr [ undef, %3 ], [ %15, %13 ]

undef means "any value I like", so the two arms become interchangeable and SimplifyCFGPass folds the branch, keeping the CL_marker comparison only as an assumption:

%12 = icmp eq i64 %11, -1
call void @llvm.assume(i1 %12)     ; <- was a branch
; CL_proc / CL_frame loads now unconditional

After that, every suitably aligned non-NIL procedure value takes the closure path, loads CL_proc and CL_frame out of the target function's own instruction stream, and branches to the result. The resulting pc decodes to a real instruction — in our case stp 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:

int fields target proc address & 7 result
1 0x100001244 4 clean
2 0x100001288 0 crash
4 0x1000012f0 0 crash
18 0x1000015bc 4 clean

Adding an unrelated field can move a program between the two.

Why the fix is here and not in the C back end

M3C.m3's loadBeforeStore analysis 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_LLVM and M3x86 have no equivalent mitigation at all, and the undefined read originates in the front end regardless.

Note also that Pop_static_link spills 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.

  • A 27-line reproducer that passes a procedure as a parameter crashes at -O2 before and prints the correct answer after, with the target procedure forced to 8-byte alignment so the failing arm is the one exercised.
  • Nested procedures still get the correct static link. A closure reading a captured local returns identical values under patched -O2 and stock -O1 (1407, 407), so the environment is still delivered correctly — this was the obvious regression risk.
  • A ~20k-line numerical model builds at -O2 and reproduces its reference output bit-for-bit (99,999 rows, matching SHA-256), and a checkpoint-restart that segfaulted at -O2 before now completes.
  • -O1 behaviour is unchanged before and after.

Related, but not this bug

The CL_marker test 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. The undef is 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 -O2 reachable for the C back end in the first place — and therefore what exposes the bug fixed in the first commit.

Builder.m3 hardcoded TRUE, FALSE for debug, optimize at the DoRunC call site:

IF ok AND DoRunC THEN
  ok := RunCC (s, CCodeName, u.object, TRUE, FALSE, s.include_path_empty);
END;

so with the C back end, m3_optimize in an m3makefile and -O on the command line were silently ignored — every build was -O0 with debug forced on. Every other back end on the same path already passes u.debug, u.optimize, as do the other two RunCC call sites in the same file. This fixes #1243.

Measured on ARM64_DARWIN with a config carrying the current Unix.common:

flag seen by compile_c args to cc Callee_m.o
before optimize= -g -O0 f4e0ea4a…
after optimize=TRUE -g -O2 ec98460b…

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

Please take these together, or this one second

Landing this commit 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, on whichever procedures the linker happens to place at a multiple of 8. The static-link fix is what makes -O2 safe here. With both commits applied, the same test program compiles at -O2 and 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.

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>
@mikan-cerebras mikan-cerebras changed the title m3front: define the static link on both arms of a closure call m3front: define the static link on both arms of a closure call; cm3: pass debug/optimize to the C back end (fixes #1243) Oct 28, 2026
@VictorMiasnikov

Copy link
Copy Markdown
Contributor

@mikan-cerebras

Copy link
Copy Markdown
Contributor Author

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?

@VictorMiasnikov

VictorMiasnikov commented Oct 28, 2026 via email

Copy link
Copy Markdown
Contributor

@VictorMiasnikov

Copy link
Copy Markdown
Contributor

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?

I can't

You can. Yes: first logon, and...

I don't know details -(

@VictorMiasnikov

VictorMiasnikov commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

( Empty msg. May will be deleted later by me)

@VictorMiasnikov

Copy link
Copy Markdown
Contributor

(
I see:
https://github.com/modula3/cm3/actions/runs/40608654077
It have been run.

Thank!
)

Have you seen this?

AMD64_LINUX / build-and-test (llvm)
failed 10 hours ago in 3m 39s

  . . .

buildship m3-libs/m3core m3-libs/libm3 m3-libs/set m3-libs/sysutils m3-sys/m3middle m3-sys/m3objfile m3-sys/m3linker m3-sys/m3back m3-sys/m3cggen m3-sys/m3quake m3-sys/m3front m3-sys/m3cgcat m3-tools/m3bundle m3-sys/mklib m3-sys/cm3

cd /home/runner/work/cm3/cm3/m3-libs/m3core 

/home/runner/work/cm3/install/bin/cm3 -build -DBUILD_DIR=AMD64_LINUX -DROOT=/home/runner/work/cm3/cm3 -DTARGET=AMD64_LINUX -DM3_BACKEND_MODE=StAloneLlvmObj -g0
--- building in AMD64_LINUX ---

ignoring ../src/m3overrides

clang -I../src -S -emit-llvm -o../src/proc.ll ../src/Csupport/hand.c
; ModuleID = '../src/Csupport/hand.c'
source_filename = "../src/Csupport/hand.c"
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-pc-linux-gnu"

; Function Attrs: noinline nounwind optnone uwtable
define dso_local i64 @m3_dnew source -> compiling RTHooks.i3
new source -> compiling RT0.i3
new source -> compiling RuntimeError.i3
new source -> compiling WordRep.i3

   . . .


new source -> compiling FloatModeC.c
new source -> compiling DatePosixC.c
new source -> compiling TimePosixC.c
***  PARALLEL BACK-END (LLVM) BUILD, M3_PARALLEL_BACK = 20


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

Aborted (core dumped)
llc: error: llc: RTLinker.mb: error: Could not open input file: No such file or directory


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

Aborted (core dumped)
llc: error: llc: RTutils.mb: error: Could not open input file: No such file or directory


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

Aborted (core dumped)
llc: error: llc: RTExFrame.mb: error: Could not open input file: No such file or directory


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

Aborted (core dumped)
llc: error: llc: Text.mb: error: Could not open input file: No such file or directory
new exporters -> recompiling RTHooks.i3
new exporters -> recompiling RTAllocCnts.i3
new exporters -> recompiling RTHeapRep.i3

   . . .

new exporters -> recompiling TimePosix.i3
new exporters -> recompiling DatePosix.i3
new exporters -> recompiling Text.i3
 -> archiving libm3core.a
ar: RTLinker_m.o: No such file or directory
  make_lib => 256
librarian failed building: m3core
Fatal Error: package build failed

@VictorMiasnikov

VictorMiasnikov commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

( Empty msg on 2026-11-19. May will be deleted later by me)

@demoitem

Copy link
Copy Markdown
Contributor

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.

@VictorMiasnikov

Copy link
Copy Markdown
Contributor

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.

Ok

@RodneyBates

RodneyBates commented Nov 18, 2026 via email

Copy link
Copy Markdown
Contributor

@demoitem

demoitem commented Nov 19, 2026 via email

Copy link
Copy Markdown
Contributor

@hosking

hosking commented Nov 19, 2026

Copy link
Copy Markdown
Member

@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 fix-undefined-static-link, from @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). 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 thinks M3CG_LLVM has a workaround that depends on the old front-end output for closure calls, which now needs adjusting. The AMD64_MINGW / build job also fails; I haven't checked whether that's related to this PR.

hosking pushed a commit that referenced this pull request Nov 19, 2026
- 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>
@VictorMiasnikov

VictorMiasnikov commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

( Empty msg N2 on 2026-11-19. May will be deleted later by me)

@VictorMiasnikov

VictorMiasnikov commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

The AMD64_MINGW / build job also fails; I haven't checked whether that's related to this PR.

clang -I../src -S -emit-llvm -o../src/proc.ll ../src/Csupport/hand.c
'clang' is not recognized as an internal or external command,
operable program or batch file.

I've fix this in my branch

https://github.com/VictorMiasnikov/cm3/commit/b0922712f623c867c1ff87a6ed7e8772f83f5b7d

        install: >-
+        mingw-w64-ucrt-x86_64-clang
          mingw-w64-ucrt-x86_64-cmake

Of course, please use current prefix of pkg ( mingw-w64-ucrt- -- used in my case).

@RodneyBates

RodneyBates commented Nov 19, 2026 via email

Copy link
Copy Markdown
Contributor

@hosking

hosking commented Nov 21, 2026

Copy link
Copy Markdown
Member

@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.

@VictorMiasnikov

VictorMiasnikov commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

AMD64_MINGW lacking clang

This micro-problem: please see detail near

@hosking

hosking commented Nov 21, 2026

Copy link
Copy Markdown
Member

Correction to my comment above: I misread the checks. #1244's failures are AMD64_LINUX (llvm), the M3CG_LLVM.m3:5323 assertion from my Nov 19 comment, and AMD64_MINGW (clang was missing at the time; master no longer needs it). I386_LINUX isn't involved. So the llvm failure is real: once this merges, M3CG_LLVM's workaround has to go at the same time, or the llvm job will fail. @RodneyBates, would you be able to remove it in step with this?

…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 merged commit 2b5d52e into modula3:master Nov 22, 2026
11 checks passed
@hosking

hosking commented Nov 22, 2026

Copy link
Copy Markdown
Member

@mikan-cerebras, @RodneyBates: this is merged. I added a third commit to the branch before merging, because the front-end change tripped the AMD64_LINUX (llvm) job (M3CG_LLVM.m3:5323).

M3CG_LLVM handled an indirect call that might or might not take a static link by finding the pop_static_link's basic block and taking apart the load/store/branch the old front end emitted in the closure arm. With the static link now written on both arms (NIL for a top-level procedure) and popped after the join, that sequence is gone. The commit replaces it with a run-time test: a NIL static link means a top-level procedure, so it gets the call without nest; anything else gets the call with it. Marker.CallFinallyHandler, the only other Pop_static_link, always passes a frame address, and llvm folds the test away there.

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 llvm22. llvm13 and llvm18 still have the old code, if anyone builds them. When you rebase rodney-new onto master you'll get the front-end change and the new call_invoke_indirect together.

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.

C backend has code generation optimization disabled

5 participants