Skip to content

Support io_call and runtime_call inside a block body in EXLA - #1910

Merged
polvalente merged 3 commits into
elixir-nx:mainfrom
blasphemetheus:fix/exla-io-call-in-block
Nov 27, 2026
Merged

polvalente merged 3 commits into
elixir-nx:mainfrom
blasphemetheus:fix/exla-io-call-in-block

Conversation

@blasphemetheus

Copy link
Copy Markdown
Contributor

An io_call or runtime_call inside the body of an Nx.block fails to compile on EXLA. This passes the callback pid into block functions so they compile and run.

The problem

Nx.block/4 wraps a group of operations under a struct, so a compiler can either replace the whole group with its own implementation or run the body as written. io_call runs an Elixir function on a value while a compiled function is executing, for example to print or log it.

Each works on its own with EXLA. Together they do not:

defmodule Doubled do
  import Nx.Defn

  defstruct []

  deftransform double(a) do
    Nx.block(%Doubled{}, [a], nil, fn _struct, a ->
      Nx.io_call(Nx.multiply(a, 2), &IO.inspect/1)
    end)
  end

  defn in_block(a), do: double(a)

  defn at_top_level(a), do: io_call(a * 2, &IO.inspect/1)
end

Nx.Defn.jit(&Doubled.in_block/1, compiler: Nx.Defn.Evaluator).(3)
# prints the tensor 6 and returns it

EXLA.jit(&Doubled.at_top_level/1).(3)
# prints the tensor 6 and returns it

EXLA.jit(&Doubled.in_block/1).(3)
# ** (FunctionClauseError) no function clause matching in anonymous fn/1 in EXLA.MLIR.Value.op/5

With this change the last call prints the tensor 6 and returns it.

runtime_call inside a block body fails with the same error.

Where it fails and where it does not

On main, with the host client:

Where the io_call or runtime_call is EXLA
At the top level of a defn works
Inside a while body or a cond branch works
Before a block, or applied to a block's result works
Inside a block body fails to compile
Inside a block body, with the block inside while or cond fails to compile

Only blocks that EXLA runs from their body are affected. The blocks built into Nx have no side effects in their bodies, so today this is reached by calling Nx.block/4 directly.

Cause

EXLA compiles the body of a block as a separate function and calls it. io_call and runtime_call need the pid of the callback server as their first operand, and that pid is an argument of the main function only. The lowering inside the block function used the main function's pid, and EXLA.MLIR.Value.op/5 rejects an operand that belongs to another function.

while bodies and cond branches are regions of the function that contains them, so they can use its pid directly.

The change

The pid becomes the first argument of every block function. default_block_implementation/5 passes it at the call:

call_args = [state.callback_pid_value | call_args]

block_computation/5 declares it in the signature and makes it the pid for the body:

callback_pid_typespec = EXLA.Executable.callback_server_pid_typespec()
arg_typespecs = [callback_pid_typespec | Enum.map(args, &Value.get_typespec/1)]

# ...

[callback_pid_value | args] = args

state = %{
  state
  | builder: function,
    params: Map.new(params),
    scope_ids: Tree.scope_ids(expr),
    callback_pid_value: callback_pid_value
}

Block functions whose body has no side effect receive the argument and do not use it. Passing it only where it is needed would mean walking the body before building the function.

Not covered

Nx.Defn.Evaluator treats an io_call that names a hook differently when it is inside a block body:

defn named(a), do: io_call(a * 2, :seen)

deftransform double_named(a) do
  Nx.block(%Doubled{}, [a], nil, fn _struct, a -> named(a) end)
end

defn in_block_named(a), do: double_named(a)

hooks = %{seen: &IO.inspect/1}

Nx.Defn.jit(&Doubled.in_block_named/1, hooks: hooks, compiler: Nx.Defn.Evaluator).(3)
# returns 6 and prints nothing: the hook is not called

EXLA.jit(&Doubled.in_block_named/1, hooks: hooks).(3)
# with this change: prints the tensor 6 and returns it

The evaluator runs a block body on concrete tensors, outside the traced expression, so the hooks given to jit are not looked up there. That is a separate problem in nx and is not changed here.

Tests

Six tests in the io_calls describe block of exla/test/exla/defn/api_test.exs. Each fails on main:

  • an io_call inside a block
  • io_calls before, inside and after a block, received in program order
  • one io_call in each of two blocks
  • an io_call inside a block inside while
  • an io_call inside a block inside the taken branch of cond
  • a runtime_call inside a block

🤖 Generated with Claude Code

An io_call or runtime_call inside the body of a block failed to compile.
The body of a block is lowered as its own function, and the callback pid
those calls need is an argument of the main function, so the call was
built from a value of another function. The pid is now an argument of
every block function.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@blasphemetheus

Copy link
Copy Markdown
Contributor Author

ran into this while writing tests for a per-use recomputation version of checkpointing

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@polvalente
polvalente merged commit 1211ef1 into elixir-nx:main Nov 27, 2026
9 checks passed
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.

2 participants