Expand description
Linking StableHLO modules at the text level.
A module exported from JAX (jax.export) defines a public function named
main and possibly private helper functions. To call it from a program
generated by ruxla, import renames every symbol it defines with a
prefix (@main becomes @policy_main, for example) and makes the
functions private. link then places the imported functions and the
caller’s functions in one module, where the caller reaches them through
func.call. XLA inlines the callees, so the whole program is optimized
together.
The functions work on the MLIR text format without parsing it fully. They
understand symbol references (@name), attribute and type aliases
(#name, !name), string literals, and the nesting of braces, which is
enough for modules produced by JAX.
§Examples
use ruxla_stablehlo::link;
let exported = r#"
module @jit_f attributes {mhlo.num_replicas = 1 : i32} {
func.func public @main(%x: tensor<f32>) -> tensor<f32> {
%0 = func.call @helper(%x) : (tensor<f32>) -> tensor<f32>
return %0 : tensor<f32>
}
func.func private @helper(%x: tensor<f32>) -> tensor<f32> {
return %x : tensor<f32>
}
}
"#;
let f = link::import(exported, "f")?;
assert_eq!(f.symbol("main"), Some("f_main"));
let main = r#"
func.func public @main(%x: tensor<f32>) -> tensor<f32> {
%0 = func.call @f_main(%x) : (tensor<f32>) -> tensor<f32>
return %0 : tensor<f32>
}
"#;
let linked = link::link(main, &[f]);
assert!(linked.contains("func.func private @f_helper"));