Skip to content

Metal: add_input_arguments! drops the input arguments on self-recursive calls #1090

Closed

Description

@christiangnrd

🤖:

add_input_arguments! puts value_map[f] = new_f in the clone map (src/irgen.jl:1412), so a self-recursive call is retargeted to new_f with its old argument list. rewrite_input_uses! only visits uses of the old f, so that call never gets the extra arguments.

using Metal

@noinline rec(n) = n <= 1 ? Int(thread_position_in_grid().x) : rec(n
- 1) + rec(n - 2)
function kernel(a)
    @inbounds a[1] = rec(a[1])
    return
end

Metal.code_llvm(kernel, Tuple{MtlDeviceVector{Int, 1}}; kernel =
true, dump_module = true)
define internal fastcc i64 @julia_rec_378(i64 signext %"n::Int64",
<3 x i32> %thread_position_in_grid) {
  ...
  %4 = call fastcc i64 @julia_rec_378(i64 signext %3, <3 x i32>
zeroinitializer)
  %6 = call fastcc i64 @julia_rec_378(i64 signext %5, <3 x i32>
zeroinitializer)

Two outcomes, depending on the function:

  • Wrong value: as above, the missing argument is later padded with zero, so recursive invocations see thread_position_in_grid == 0.

  • Segfault: if the function also needs the kernel state, AddKernelStatePass rebuilds the call with the new function type but the short argument list ("Incorrect number of arguments passed to called function!"), and the inliner in the Metal optimize_module! crashes in CallAnalyzer::analyze. This is the LLVM crash behind Use GPUToolbox's shared throw overrides Metal.jl#1138: on Julia 1.10, Rational arithmetic pulls Base's recursive print_fullname/modulesof! into the kernel. That PR removes the trigger, not this bug; the user should have gotten an InvalidIRError.

Removing the value_map[f] = new_f line fixes both for me: self-calls keep pointing at the old f and are rewritten like any other call site.

GPUCompiler 2.22.2 (line still present on main), reproduced on Julia 1.10.13 and 1.13.3.

Activity

  1. added a commit that references this issue on Nov 18, 2026
    068bf22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions