Skip to content

List the callers of a function when an IR error's backtrace reaches it - #1141

Open
maleadt wants to merge 1 commit into
mainfrom
tb/backtrace-callers
Open

maleadt wants to merge 1 commit into
mainfrom
tb/backtrace-callers

Conversation

@maleadt

@maleadt maleadt commented Dec 2, 2026

Copy link
Copy Markdown
Member

When an invalid instruction is in a function that isn't inlined and is called from several places, the IR backtrace of an InvalidIRError stops at that function with a "multiple call sites" frame. It doesn't say which code in the kernel reaches the error:

@noinline helper(x) = Base.inferencebarrier(sin)(x)::Float64
@noinline mid(p, x) = (unsafe_store!(p, helper(x)); nothing)
function kernel(p, x)
    unsafe_store!(p, helper(x))
    unsafe_store!(p, helper(x + 1))
    mid(p, x)
    return
end
# before
Reason: unsupported dynamic function invocation (call to sin)
Julia code (likely): the callee(::Float64), with the callee::Any
Stacktrace:
 [1] helper
   @ ./repro.jl:1
 [2] multiple call sites
   @ unknown:0

# after
Reason: unsupported dynamic function invocation (call to sin)
Julia code (likely): the callee(::Float64), with the callee::Any
Stacktrace:
 [1] helper
   @ ./repro.jl:1
Called from several places:
  mid
   @ ./repro.jl:2
  kernel
   @ ./repro.jl:6
or
  kernel
   @ ./repro.jl:4
or
  kernel
   @ ./repro.jl:5

While validating, the paths of calls into such a function are now recorded for each error, each followed out to the kernel, and shown as alternative continuations of the backtrace. Up to four are shown ("or elsewhere" when there are more); recursion is not followed.

The paths are stored in a new callers field of InvalidIRError, not in the backtraces. Other code takes the frames of a backtrace as one call chain: the grouping of errors by where they come from, Enzyme's error messages, and the frames that device-side exceptions report. Those are unchanged, as is backtrace itself.

Errors from the back-end validators (e.g. system-scope atomics on PTX, Float64 on Metal) get their callers too: validate_ir has a three-argument form that receives the record, and the in-tree back-ends implement it. The generic three-argument method calls the two-argument one, so back-ends that only implement that keep working, without callers. One caveat: an override of validate_ir(job, mod) for one of the in-tree targets (e.g. specialized on its own CompilerParams) is no longer called by validation; I found none in the JuliaGPU packages or Enzyme.

Tested with the native and PTX suites, which cover nested paths, recursion, mutual recursion and the cap, and a PTX atomic in a shared helper.

Fixes #637. Supersedes #1135, from which it takes the idea and test setup.

@codecov

codecov Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.18%. Comparing base (a867809) to head (aa0203c).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1141      +/-   ##
==========================================
+ Coverage   93.13%   93.18%   +0.05%     
==========================================
  Files          32       32              
  Lines        8574     8653      +79     
==========================================
+ Hits         7985     8063      +78     
- Misses        589      590       +1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

IR backtraces stop at a function with several call sites, ending in a
"multiple call sites" frame, so an InvalidIRError raised in a shared
non-inlined helper doesn't say which code in the kernel reaches it.

While validating, record for such errors up to five paths of calls into
that function, each followed outwards until a function that isn't
called (normally the kernel) or recursion, and show up to four of them
below the backtrace as alternative continuations. They are kept next
to the errors in InvalidIRError rather than in the backtraces, whose
frames other code takes as a single call chain: the grouping of errors
by origin, Enzyme, and the device-side exception frames.

Back-ends record them by implementing `validate_ir` with a third
argument, as the in-tree ones now do. Back-ends that only implement
the two-argument form keep working, without the callers, but
validation no longer calls a two-argument override for one of the
in-tree targets.

Co-authored-by: Nishagoswamii <nishagoswami369@gmail.com>
@maleadt
maleadt force-pushed the tb/backtrace-callers branch from aa0203c to 2cfe101 Compare December 2, 2026 19:54
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.

IR backtraces: expand multiple call frames

1 participant