kestred opened issue #15262:
.clifTest CaseA loop that calls a function and keeps three
f64values live across the call: the call's argumentv0, the factor
v1and the running sumv6. The function has no vector value.function %f(f64, f64, i64) -> f64 { sig0 = (f64) -> f64 fn0 = %g sig0 block0(v0: f64, v1: f64, v2: i64): v3 = iconst.i64 0 v4 = f64const 0.0 v12 = iconst.i64 1 jump block1(v3, v4) block1(v5: i64, v6: f64): v7 = call fn0(v0) v8 = fmul v7, v1 v9 = fadd v6, v8 v10 = iadd v5, v12 v11 = icmp ult v10, v2 brif v11, block1(v10, v9), block2 block2: return v9 }test run target aarch64 ; Writes whole q registers under high pressure, so that it uses v8 - v15 and, ; as AAPCS lets it, returns with only their low halves restored. function %clobber_vectors(f64) -> f64 { block0(v0: f64): v1 = splat.f64x2 v0 v2 = fadd v1, v1 v3 = fadd v2, v1 v4 = fadd v3, v2 v5 = fadd v4, v3 v6 = fadd v5, v4 v7 = fadd v6, v5 v8 = fadd v7, v6 v9 = fadd v8, v7 v10 = fadd v9, v8 v11 = fadd v10, v9 v12 = fadd v11, v10 v13 = fadd v12, v11 v14 = fadd v13, v12 v15 = fadd v14, v13 v16 = fadd v15, v14 v17 = fadd v16, v15 v18 = fadd v17, v16 v19 = fadd v18, v17 v20 = fadd v19, v18 v21 = fadd v20, v19 v22 = fadd v21, v20 v23 = fadd v22, v21 v24 = fadd v23, v22 v25 = fadd v24, v23 v26 = fadd v25, v24 v27 = fadd v26, v25 v28 = fadd v27, v26 v29 = fadd v28, v27 v30 = fadd v29, v28 v31 = fadd v30, v29 v32 = fadd v31, v30 v33 = fadd v32, v31 v40 = fadd v1, v2 v41 = fadd v40, v3 v42 = fadd v41, v4 v43 = fadd v42, v5 v44 = fadd v43, v6 v45 = fadd v44, v7 v46 = fadd v45, v8 v47 = fadd v46, v9 v48 = fadd v47, v10 v49 = fadd v48, v11 v50 = fadd v49, v12 v51 = fadd v50, v13 v52 = fadd v51, v14 v53 = fadd v52, v15 v54 = fadd v53, v16 v55 = fadd v54, v17 v56 = fadd v55, v18 v57 = fadd v56, v19 v58 = fadd v57, v20 v59 = fadd v58, v21 v60 = fadd v59, v22 v61 = fadd v60, v23 v62 = fadd v61, v24 v63 = fadd v62, v25 v64 = fadd v63, v26 v65 = fadd v64, v27 v66 = fadd v65, v28 v67 = fadd v66, v29 v68 = fadd v67, v30 v69 = fadd v68, v31 v70 = fadd v69, v32 v71 = fadd v70, v33 v72 = extractlane v71, 1 return v72 } ; No vector: the scalars stay in v8 - v15 across the call. function %scalar_caller(f64, f64, f64) -> f64 { fn0 = colocated %clobber_vectors(f64) -> f64 block0(v0: f64, v1: f64, v2: f64): v3 = call fn0(v0) v4 = fmul v1, v2 v5 = fadd v4, v1 v6 = fadd v5, v2 return v6 } ; run: %scalar_caller(0x1.0, 0x3.0, 0x5.0) == 0x17.0 ; A vector live across the call: v8 - v15 stay clobbered, so it is spilled. function %vector_caller(f64, f64) -> f64 { fn0 = colocated %clobber_vectors(f64) -> f64 block0(v0: f64, v1: f64): v2 = splat.f64x2 v1 v3 = call fn0(v0) v4 = extractlane v2, 1 v5 = fadd v4, v1 return v5 } ; run: %vector_caller(0x1.0, 0x3.0) == 0x6.0Steps to Reproduce
First, build
clif-utilfrom wasmtime v50.0.0, the release that ships cranelift-codegen 0.137.0:
curl -L https://github.com/bytecodealliance/wasmtime/archive/refs/tags/v50.0.0.tar.gz | tar xz cd wasmtime-50.0.0 cargo build --release -p cranelift-tools --bin clif-utilThen, compile the test case and print its disassembly:
target/release/clif-util compile --target aarch64 --set opt_level=speed -D 1-test.clif
clif-util compileonly compiles, so the output does not depend on the host. The listings below omit the
instruction bytes.Then, apply the patch under Extra Info, build again, and compile the test case again.
Expected Results
The loop keeps
v0,v1andv6in v8–v15 across the call, with no load or store per iteration. AAPCS64 preserves
the low 64 bits of those registers, which is all of anf64, and the prologue saves the ones that the function
uses. With the patch, the loop from0x2cto0x70moves the values between registers, and the prologue savesd8,
d9andd15once:Disassembly of 136 bytes <%f>: 0: stp x29, x30, [sp, #-0x10]! 4: mov x29, sp 8: stp x19, x20, [sp, #-0x10]! c: str d15, [sp, #-0x10]! 10: stp d8, d9, [sp, #-0x10]! 14: mov x20, x0 18: mov v8.16b, v0.16b 1c: mov v15.16b, v1.16b 20: mov x19, #0 24: movi v0.2s, #0 28: mov v9.16b, v0.16b 2c: ldr x13, #0x34 30: b #0x3c 34: udf #0 38: udf #0 3c: mov v0.16b, v8.16b 40: blr x13 44: add x19, x19, #1 48: mov v1.16b, v15.16b 4c: fmul d16, d0, d1 50: mov v0.16b, v9.16b 54: fadd d0, d0, d16 58: mov x0, x20 5c: cmp x19, x0 60: b.hs #0x74 64: mov x20, x0 68: mov v9.16b, v0.16b 6c: mov v15.16b, v1.16b 70: b #0x2c 74: ldp d8, d9, [sp], #0x10 78: ldr d15, [sp], #0x10 7c: ldp x19, x20, [sp], #0x10 80: ldp x29, x30, [sp], #0x10 84: retActual Results
Every iteration stores and reloads the floats: the loop from
0x28to0x6chas three 128-bit loads (ldur q) and
two 128-bit stores (stur q), to keep values that the callee would have preserved:Disassembly of 128 bytes <%f>: 0: stp x29, x30, [sp, #-0x10]! 4: mov x29, sp 8: stp x19, x20, [sp, #-0x10]! c: sub sp, sp, #0x30 10: mov x20, x0 14: stur q0, [sp] 18: stur q1, [sp, #0x10] 1c: mov x19, #0 20: movi v0.2s, #0 24: stur q0, [sp, #0x20] 28: ldr x13, #0x30 2c: b #0x38 30: udf #0 34: udf #0 38: ldur q0, [sp] 3c: blr x13 40: add x19, x19, #1 44: ldur q1, [sp, #0x10] 48: fmul d16, d0, d1 4c: ldur q0, [sp, #0x20] 50: fadd d0, d0, d16 54: mov x0, x20 58: cmp x19, x0 5c: b.hs #0x70 60: mov x20, x0 64: stur q1, [sp, #0x10] 68: stur q0, [sp, #0x20] 6c: b #0x28 70: add sp, sp, #0x30 74: ldp x19, x20, [sp], #0x10 78: ldp x29, x30, [sp], #0x10 7c: retVersions and Environment
Cranelift version or commit: cranelift-codegen 0.137.0, with regalloc2 0.15.2, built into
clif-utilfrom wasmtime
v50.0.0 (commit ebe5e8df1f4e12c5fc2f5954926881d8d61367d8) with rustc 1.98.0,--releaseand default features.Operating system: macOS 26.6.2 (build 25G83).
Architecture: arm64 (Apple M5 Pro), compiling for
--target aarch64.Extra Info
The limitation.
default_aapcs_clobbers()inisa/aarch64/abi.rssays:The upper 64 bits of v8 - v15 inclusive are also caller-saves. However, because we cannot currently represent
partial registers to regalloc2, we indicate here that every vector register is caller-save. Because this function
is used at callsites, approximating in this direction (save more than necessary) is conservative and thus safe.This stopgap was chosen in #2254 and #2267, where a follow-up was noted so that prologues and epilogues deal only with
the lower 64 bits. In regalloc2, partial registers are out of scope (bytecodealliance/regalloc2#247).The proposal. A function with no value wider than 64 bits loses nothing when a call clobbers only the upper
halves of v8–v15, so it can drop them from that call's clobbers without regalloc2 knowing about partial registers:
A new provided method,
ABIMachineSpec::get_regs_partially_clobbered_by_call(callee_conv, is_exception), returns
v8–v15 on aarch64 wherever the default AAPCS clobbers apply, and an empty set elsewhere and on other targets.
Callee::newcomputes ahas_wide_valuesflag from the types of the function'sdfgvalues, as it already scans
the dynamic types. A type that the backend cannot place in registers counts as wide.When the function has no wide value,
gen_call_infodropspartial(callee_conv) ∩ partial(caller_conv)from the
call's clobbers.There is no new setting and no call for the embedder to make. The other users of the static clobber sets are
unchanged and stay conservative. s390x carries the same comment and could opt in the same way.Soundness.
The intersection with the caller's own convention matters for a PreserveAll caller. A call's clobbers also decide
what the prologue saves, so such a caller still treats v8–v15 as clobbered and saves all 128 bits of them.An exception edge is narrowed only where the unwinder already restores callee-saves, that is, for a SystemV or
Appletry_call.Winch, PreserveAll and Tail exception edges are unchanged.
Evidence. With the patch, two aarch64 filetests change when blessed: in
call.clifanduser_stack_maps.clif,
the q-register spills becomemovto v8–v15, and the prologue savesd8,d11,d12ord15withstpor
str. Stockclif-utilfails both blessed files. With the patch and the blessed files,target/release/clif-util test cranelift/filetests/filetestsprinted
1322 testswith no failure. A runtime test,1-run.clifbeside this issue, calls a function that writes
all 128 bits of v8–v15 from a caller that keeps scalars in them across the call:target/release/clif-util test 1-run.clifIt printed
1 testswith no failure, with and without the patch. With its expected value changed from0x17.0to
0x18.0, it failed withactual: 0x1.7000000000000p4, which shows that the test runs.A precise alternative would be per-value partial clobbers in regalloc2.
<details><summary>Patch against cranelift-codegen 0.137.0</summary>
diff --git a/cranelift/codegen/src/isa/aarch64/abi.rs b/cranelift/codegen/src/isa/aarch64/abi.rs index 8659561..8c953bc 100644 --- a/cranelift/codegen/src/isa/aarch64/abi.rs +++ b/cranelift/codegen/src/isa/aarch64/abi.rs @@ -1210,6 +1210,20 @@ impl ABIMachineSpec for AArch64MachineDeps { } } + fn get_regs_partially_clobbered_by_call( + call_conv: isa::CallConv, + is_exception: bool, + ) -> PRegSet { + // A callee that follows AAPCS64 preserves the low 64 bits of v8 - v15, [message truncated]
kestred added the bug label to Issue #15262.
kestred added the cranelift label to Issue #15262.
alexcrichton added the cranelift:area:aarch64 label to Issue #15262.
kestred edited issue #15262:
.clifTest CaseA loop that calls a function and keeps three
f64values live across the call: the call's argumentv0, the factor
v1and the running sumv6. The function has no vector value.test.clif
function %f(f64, f64, i64) -> f64 { sig0 = (f64) -> f64 fn0 = %g sig0 block0(v0: f64, v1: f64, v2: i64): v3 = iconst.i64 0 v4 = f64const 0.0 v12 = iconst.i64 1 jump block1(v3, v4) block1(v5: i64, v6: f64): v7 = call fn0(v0) v8 = fmul v7, v1 v9 = fadd v6, v8 v10 = iadd v5, v12 v11 = icmp ult v10, v2 brif v11, block1(v10, v9), block2 block2: return v9 }run.clif
test run target aarch64 ; Writes whole q registers under high pressure, so that it uses v8 - v15 and, ; as AAPCS lets it, returns with only their low halves restored. function %clobber_vectors(f64) -> f64 { block0(v0: f64): v1 = splat.f64x2 v0 v2 = fadd v1, v1 v3 = fadd v2, v1 v4 = fadd v3, v2 v5 = fadd v4, v3 v6 = fadd v5, v4 v7 = fadd v6, v5 v8 = fadd v7, v6 v9 = fadd v8, v7 v10 = fadd v9, v8 v11 = fadd v10, v9 v12 = fadd v11, v10 v13 = fadd v12, v11 v14 = fadd v13, v12 v15 = fadd v14, v13 v16 = fadd v15, v14 v17 = fadd v16, v15 v18 = fadd v17, v16 v19 = fadd v18, v17 v20 = fadd v19, v18 v21 = fadd v20, v19 v22 = fadd v21, v20 v23 = fadd v22, v21 v24 = fadd v23, v22 v25 = fadd v24, v23 v26 = fadd v25, v24 v27 = fadd v26, v25 v28 = fadd v27, v26 v29 = fadd v28, v27 v30 = fadd v29, v28 v31 = fadd v30, v29 v32 = fadd v31, v30 v33 = fadd v32, v31 v40 = fadd v1, v2 v41 = fadd v40, v3 v42 = fadd v41, v4 v43 = fadd v42, v5 v44 = fadd v43, v6 v45 = fadd v44, v7 v46 = fadd v45, v8 v47 = fadd v46, v9 v48 = fadd v47, v10 v49 = fadd v48, v11 v50 = fadd v49, v12 v51 = fadd v50, v13 v52 = fadd v51, v14 v53 = fadd v52, v15 v54 = fadd v53, v16 v55 = fadd v54, v17 v56 = fadd v55, v18 v57 = fadd v56, v19 v58 = fadd v57, v20 v59 = fadd v58, v21 v60 = fadd v59, v22 v61 = fadd v60, v23 v62 = fadd v61, v24 v63 = fadd v62, v25 v64 = fadd v63, v26 v65 = fadd v64, v27 v66 = fadd v65, v28 v67 = fadd v66, v29 v68 = fadd v67, v30 v69 = fadd v68, v31 v70 = fadd v69, v32 v71 = fadd v70, v33 v72 = extractlane v71, 1 return v72 } ; No vector: the scalars stay in v8 - v15 across the call. function %scalar_caller(f64, f64, f64) -> f64 { fn0 = colocated %clobber_vectors(f64) -> f64 block0(v0: f64, v1: f64, v2: f64): v3 = call fn0(v0) v4 = fmul v1, v2 v5 = fadd v4, v1 v6 = fadd v5, v2 return v6 } ; run: %scalar_caller(0x1.0, 0x3.0, 0x5.0) == 0x17.0 ; A vector live across the call: v8 - v15 stay clobbered, so it is spilled. function %vector_caller(f64, f64) -> f64 { fn0 = colocated %clobber_vectors(f64) -> f64 block0(v0: f64, v1: f64): v2 = splat.f64x2 v1 v3 = call fn0(v0) v4 = extractlane v2, 1 v5 = fadd v4, v1 return v5 } ; run: %vector_caller(0x1.0, 0x3.0) == 0x6.0Steps to Reproduce
First, build
clif-utilfrom wasmtime v50.0.0, the release that ships cranelift-codegen 0.137.0:
curl -L https://github.com/bytecodealliance/wasmtime/archive/refs/tags/v50.0.0.tar.gz | tar xz cd wasmtime-50.0.0 cargo build --release -p cranelift-tools --bin clif-utilThen, compile the test case and print its disassembly:
target/release/clif-util compile --target aarch64 --set opt_level=speed -D 1-test.clif
clif-util compileonly compiles, so the output does not depend on the host. The listings below omit the
instruction bytes.Then, apply the patch under Extra Info, build again, and compile the test case again.
Expected Results
The loop keeps
v0,v1andv6in v8–v15 across the call, with no load or store per iteration. AAPCS64 preserves
the low 64 bits of those registers, which is all of anf64, and the prologue saves the ones that the function
uses. With the patch, the loop from0x2cto0x70moves the values between registers, and the prologue savesd8,
d9andd15once:Disassembly of 136 bytes <%f>: 0: stp x29, x30, [sp, #-0x10]! 4: mov x29, sp 8: stp x19, x20, [sp, #-0x10]! c: str d15, [sp, #-0x10]! 10: stp d8, d9, [sp, #-0x10]! 14: mov x20, x0 18: mov v8.16b, v0.16b 1c: mov v15.16b, v1.16b 20: mov x19, #0 24: movi v0.2s, #0 28: mov v9.16b, v0.16b 2c: ldr x13, #0x34 30: b #0x3c 34: udf #0 38: udf #0 3c: mov v0.16b, v8.16b 40: blr x13 44: add x19, x19, #1 48: mov v1.16b, v15.16b 4c: fmul d16, d0, d1 50: mov v0.16b, v9.16b 54: fadd d0, d0, d16 58: mov x0, x20 5c: cmp x19, x0 60: b.hs #0x74 64: mov x20, x0 68: mov v9.16b, v0.16b 6c: mov v15.16b, v1.16b 70: b #0x2c 74: ldp d8, d9, [sp], #0x10 78: ldr d15, [sp], #0x10 7c: ldp x19, x20, [sp], #0x10 80: ldp x29, x30, [sp], #0x10 84: retActual Results
Every iteration stores and reloads the floats: the loop from
0x28to0x6chas three 128-bit loads (ldur q) and
two 128-bit stores (stur q), to keep values that the callee would have preserved:Disassembly of 128 bytes <%f>: 0: stp x29, x30, [sp, #-0x10]! 4: mov x29, sp 8: stp x19, x20, [sp, #-0x10]! c: sub sp, sp, #0x30 10: mov x20, x0 14: stur q0, [sp] 18: stur q1, [sp, #0x10] 1c: mov x19, #0 20: movi v0.2s, #0 24: stur q0, [sp, #0x20] 28: ldr x13, #0x30 2c: b #0x38 30: udf #0 34: udf #0 38: ldur q0, [sp] 3c: blr x13 40: add x19, x19, #1 44: ldur q1, [sp, #0x10] 48: fmul d16, d0, d1 4c: ldur q0, [sp, #0x20] 50: fadd d0, d0, d16 54: mov x0, x20 58: cmp x19, x0 5c: b.hs #0x70 60: mov x20, x0 64: stur q1, [sp, #0x10] 68: stur q0, [sp, #0x20] 6c: b #0x28 70: add sp, sp, #0x30 74: ldp x19, x20, [sp], #0x10 78: ldp x29, x30, [sp], #0x10 7c: retVersions and Environment
Cranelift version or commit: cranelift-codegen 0.137.0, with regalloc2 0.15.2, built into
clif-utilfrom wasmtime
v50.0.0 (commit ebe5e8df1f4e12c5fc2f5954926881d8d61367d8) with rustc 1.98.0,--releaseand default features.Operating system: macOS 26.6.2 (build 25G83).
Architecture: arm64 (Apple M5 Pro), compiling for
--target aarch64.Extra Info
The limitation.
default_aapcs_clobbers()inisa/aarch64/abi.rssays:The upper 64 bits of v8 - v15 inclusive are also caller-saves. However, because we cannot currently represent
partial registers to regalloc2, we indicate here that every vector register is caller-save. Because this function
is used at callsites, approximating in this direction (save more than necessary) is conservative and thus safe.This stopgap was chosen in #2254 and #2267, where a follow-up was noted so that prologues and epilogues deal only with
the lower 64 bits. In regalloc2, partial registers are out of scope (bytecodealliance/regalloc2#247).The proposal. A function with no value wider than 64 bits loses nothing when a call clobbers only the upper
halves of v8–v15, so it can drop them from that call's clobbers without regalloc2 knowing about partial registers:
A new provided method,
ABIMachineSpec::get_regs_partially_clobbered_by_call(callee_conv, is_exception), returns
v8–v15 on aarch64 wherever the default AAPCS clobbers apply, and an empty set elsewhere and on other targets.
Callee::newcomputes ahas_wide_valuesflag from the types of the function'sdfgvalues, as it already scans
the dynamic types. A type that the backend cannot place in registers counts as wide.When the function has no wide value,
gen_call_infodropspartial(callee_conv) ∩ partial(caller_conv)from the
call's clobbers.There is no new setting and no call for the embedder to make. The other users of the static clobber sets are
unchanged and stay conservative. s390x carries the same comment and could opt in the same way.Soundness.
The intersection with the caller's own convention matters for a PreserveAll caller. A call's clobbers also decide
what the prologue saves, so such a caller still treats v8–v15 as clobbered and saves all 128 bits of them.An exception edge is narrowed only where the unwinder already restores callee-saves, that is, for a SystemV or
Appletry_call.Winch, PreserveAll and Tail exception edges are unchanged.
Evidence. With the patch, two aarch64 filetests change when blessed: in
call.clifanduser_stack_maps.clif,
the q-register spills becomemovto v8–v15, and the prologue savesd8,d11,d12ord15withstpor
str. Stockclif-utilfails both blessed files. With the patch and the blessed files,target/release/clif-util test cranelift/filetests/filetestsprinted
1322 testswith no failure. A runtime test,run.clifbeside this issue, calls a function that writes
all 128 bits of v8–v15 from a caller that keeps scalars in them across the call:target/release/clif-util test run.clifIt printed
1 testswith no failure, with and without the patch. With its expected value changed from0x17.0to
0x18.0, it failed withactual: 0x1.7000000000000p4, which shows that the test runs.A precise alternative would be per-value partial clobbers in regalloc2.
<details><summary>Patch against cranelift-codegen 0.137.0</summary>
diff --git a/cranelift/codegen/src/isa/aarch64/abi.rs b/cranelift/codegen/src/isa/aarch64/abi.rs index 8659561..8c953bc 100644 --- a/cranelift/codegen/src/isa/aarch64/abi.rs +++ b/cranelift/codegen/src/isa/aarch64/abi.rs @@ -1210,6 +1210,20 @@ impl ABIMachineSpec for AArch64MachineDeps { } } + fn get_regs_partially_clobbered_by_call( + call_conv: isa::CallConv, + is_exception: bool, + ) -> PRegSet { + // A callee that follows AAPCS64 preserves the [message truncated]
kestred edited issue #15262:
.clifTest CaseA loop that calls a function and keeps three
f64values live across the call: the call's argumentv0, the factor
v1and the running sumv6. The function has no vector value.test.clif
function %f(f64, f64, i64) -> f64 { sig0 = (f64) -> f64 fn0 = %g sig0 block0(v0: f64, v1: f64, v2: i64): v3 = iconst.i64 0 v4 = f64const 0.0 v12 = iconst.i64 1 jump block1(v3, v4) block1(v5: i64, v6: f64): v7 = call fn0(v0) v8 = fmul v7, v1 v9 = fadd v6, v8 v10 = iadd v5, v12 v11 = icmp ult v10, v2 brif v11, block1(v10, v9), block2 block2: return v9 }Steps to Reproduce
First, build
clif-utilfrom wasmtime v50.0.0, the release that ships cranelift-codegen 0.137.0:
curl -L https://github.com/bytecodealliance/wasmtime/archive/refs/tags/v50.0.0.tar.gz | tar xz cd wasmtime-50.0.0 cargo build --release -p cranelift-tools --bin clif-utilThen, compile the test case and print its disassembly:
target/release/clif-util compile --target aarch64 --set opt_level=speed -D 1-test.clif
clif-util compileonly compiles, so the output does not depend on the host. The listings below omit the
instruction bytes.Then, apply the patch under Extra Info, build again, and compile the test case again.
Expected Results
The loop keeps
v0,v1andv6in v8–v15 across the call, with no load or store per iteration. AAPCS64 preserves
the low 64 bits of those registers, which is all of anf64, and the prologue saves the ones that the function
uses. With the patch, the loop from0x2cto0x70moves the values between registers, and the prologue savesd8,
d9andd15once:Disassembly of 136 bytes <%f>: 0: stp x29, x30, [sp, #-0x10]! 4: mov x29, sp 8: stp x19, x20, [sp, #-0x10]! c: str d15, [sp, #-0x10]! 10: stp d8, d9, [sp, #-0x10]! 14: mov x20, x0 18: mov v8.16b, v0.16b 1c: mov v15.16b, v1.16b 20: mov x19, #0 24: movi v0.2s, #0 28: mov v9.16b, v0.16b 2c: ldr x13, #0x34 30: b #0x3c 34: udf #0 38: udf #0 3c: mov v0.16b, v8.16b 40: blr x13 44: add x19, x19, #1 48: mov v1.16b, v15.16b 4c: fmul d16, d0, d1 50: mov v0.16b, v9.16b 54: fadd d0, d0, d16 58: mov x0, x20 5c: cmp x19, x0 60: b.hs #0x74 64: mov x20, x0 68: mov v9.16b, v0.16b 6c: mov v15.16b, v1.16b 70: b #0x2c 74: ldp d8, d9, [sp], #0x10 78: ldr d15, [sp], #0x10 7c: ldp x19, x20, [sp], #0x10 80: ldp x29, x30, [sp], #0x10 84: retActual Results
Every iteration stores and reloads the floats: the loop from
0x28to0x6chas three 128-bit loads (ldur q) and
two 128-bit stores (stur q), to keep values that the callee would have preserved:Disassembly of 128 bytes <%f>: 0: stp x29, x30, [sp, #-0x10]! 4: mov x29, sp 8: stp x19, x20, [sp, #-0x10]! c: sub sp, sp, #0x30 10: mov x20, x0 14: stur q0, [sp] 18: stur q1, [sp, #0x10] 1c: mov x19, #0 20: movi v0.2s, #0 24: stur q0, [sp, #0x20] 28: ldr x13, #0x30 2c: b #0x38 30: udf #0 34: udf #0 38: ldur q0, [sp] 3c: blr x13 40: add x19, x19, #1 44: ldur q1, [sp, #0x10] 48: fmul d16, d0, d1 4c: ldur q0, [sp, #0x20] 50: fadd d0, d0, d16 54: mov x0, x20 58: cmp x19, x0 5c: b.hs #0x70 60: mov x20, x0 64: stur q1, [sp, #0x10] 68: stur q0, [sp, #0x20] 6c: b #0x28 70: add sp, sp, #0x30 74: ldp x19, x20, [sp], #0x10 78: ldp x29, x30, [sp], #0x10 7c: retVersions and Environment
Cranelift version or commit: cranelift-codegen 0.137.0, with regalloc2 0.15.2, built into
clif-utilfrom wasmtime
v50.0.0 (commit ebe5e8df1f4e12c5fc2f5954926881d8d61367d8) with rustc 1.98.0,--releaseand default features.Operating system: macOS 26.6.2 (build 25G83).
Architecture: arm64 (Apple M5 Pro), compiling for
--target aarch64.Extra Info
The limitation.
default_aapcs_clobbers()inisa/aarch64/abi.rssays:The upper 64 bits of v8 - v15 inclusive are also caller-saves. However, because we cannot currently represent
partial registers to regalloc2, we indicate here that every vector register is caller-save. Because this function
is used at callsites, approximating in this direction (save more than necessary) is conservative and thus safe.This stopgap was chosen in #2254 and #2267, where a follow-up was noted so that prologues and epilogues deal only with
the lower 64 bits. In regalloc2, partial registers are out of scope (bytecodealliance/regalloc2#247).The proposal. A function with no value wider than 64 bits loses nothing when a call clobbers only the upper
halves of v8–v15, so it can drop them from that call's clobbers without regalloc2 knowing about partial registers:
A new provided method,
ABIMachineSpec::get_regs_partially_clobbered_by_call(callee_conv, is_exception), returns
v8–v15 on aarch64 wherever the default AAPCS clobbers apply, and an empty set elsewhere and on other targets.
Callee::newcomputes ahas_wide_valuesflag from the types of the function'sdfgvalues, as it already scans
the dynamic types. A type that the backend cannot place in registers counts as wide.When the function has no wide value,
gen_call_infodropspartial(callee_conv) ∩ partial(caller_conv)from the
call's clobbers.There is no new setting and no call for the embedder to make. The other users of the static clobber sets are
unchanged and stay conservative. s390x carries the same comment and could opt in the same way.Soundness.
The intersection with the caller's own convention matters for a PreserveAll caller. A call's clobbers also decide
what the prologue saves, so such a caller still treats v8–v15 as clobbered and saves all 128 bits of them.An exception edge is narrowed only where the unwinder already restores callee-saves, that is, for a SystemV or
Appletry_call.Winch, PreserveAll and Tail exception edges are unchanged.
Evidence. With the patch, two aarch64 filetests change when blessed: in
call.clifanduser_stack_maps.clif,
the q-register spills becomemovto v8–v15, and the prologue savesd8,d11,d12ord15withstpor
str. Stockclif-utilfails both blessed files. With the patch and the blessed files,target/release/clif-util test cranelift/filetests/filetestsprinted
1322 testswith no failure. The following test,run.clifcalls a function that writes
all 128 bits of v8–v15 from a caller that keeps scalars in them across the call:<details><summary>run.clif</summary>
test run target aarch64 ; Writes whole q registers under high pressure, so that it uses v8 - v15 and, ; as AAPCS lets it, returns with only their low halves restored. function %clobber_vectors(f64) -> f64 { block0(v0: f64): v1 = splat.f64x2 v0 v2 = fadd v1, v1 v3 = fadd v2, v1 v4 = fadd v3, v2 v5 = fadd v4, v3 v6 = fadd v5, v4 v7 = fadd v6, v5 v8 = fadd v7, v6 v9 = fadd v8, v7 v10 = fadd v9, v8 v11 = fadd v10, v9 v12 = fadd v11, v10 v13 = fadd v12, v11 v14 = fadd v13, v12 v15 = fadd v14, v13 v16 = fadd v15, v14 v17 = fadd v16, v15 v18 = fadd v17, v16 v19 = fadd v18, v17 v20 = fadd v19, v18 v21 = fadd v20, v19 v22 = fadd v21, v20 v23 = fadd v22, v21 v24 = fadd v23, v22 v25 = fadd v24, v23 v26 = fadd v25, v24 v27 = fadd v26, v25 v28 = fadd v27, v26 v29 = fadd v28, v27 v30 = fadd v29, v28 v31 = fadd v30, v29 v32 = fadd v31, v30 v33 = fadd v32, v31 v40 = fadd v1, v2 v41 = fadd v40, v3 v42 = fadd v41, v4 v43 = fadd v42, v5 v44 = fadd v43, v6 v45 = fadd v44, v7 v46 = fadd v45, v8 v47 = fadd v46, v9 v48 = fadd v47, v10 v49 = fadd v48, v11 v50 = fadd v49, v12 v51 = fadd v50, v13 v52 = fadd v51, v14 v53 = fadd v52, v15 v54 = fadd v53, v16 v55 = fadd v54, v17 v56 = fadd v55, v18 v57 = fadd v56, v19 v58 = fadd v57, v20 v59 = fadd v58, v21 v60 = fadd v59, v22 v61 = fadd v60, v23 v62 = fadd v61, v24 v63 = fadd v62, v25 v64 = fadd v63, v26 v65 = fadd v64, v27 v66 = fadd v65, v28 v67 = fadd v66, v29 v68 = fadd v67, v30 v69 = fadd v68, v31 v70 = fadd v69, v32 v71 = fadd v70, v33 v72 = extractlane v71, 1 return v72 } ; No vector: the scalars stay in v8 - v15 across the call. function %scalar_caller(f64, f64, f64) -> f64 { fn0 = colocated %clobber_vectors(f64) -> f64 block0(v0: f64, v1: f64, v2: f64): v3 = call fn0(v0) v4 = fmul v1, v2 v5 = fadd v4, v1 v6 = fadd v5, v2 return v6 } ; run: %scalar_caller(0x1.0, 0x3.0, 0x5.0) == 0x17.0 ; A vector live across the call: v8 - v15 stay clobbered, so it is spilled. function %vector_caller(f64, f64) -> f64 { fn0 = colocated %clobber_vectors(f64) -> f64 block0(v0: f64, v1: f64): v2 = splat.f64x2 v1 v3 = call fn0(v0) v4 = extractlane v2, 1 v5 = fadd v4, v1 return v5 } ; run: %vector_caller(0x1.0, 0x3.0) == 0x6.0</details>
target/release/clif-util test run.clifIt printed
1 testswith no failure, with and without the patch. With its expected value changed from0x17.0to
0x18.0, it failed withactual: 0x1.7000000000000p4, which shows that the test runs.A precise alternative would be per-value partial clobbers in regalloc2.
<details><summary>Patch against cranelift-codegen 0.137.0</summary>
diff --git a/cranelift/codegen/src/isa/aarch64/abi.rs b/cranelift/codegen/src/isa/aarch64/abi.rs index 8659561..8c953bc 100644 --- a/cranelift/codegen/src/isa/aarch64/abi.rs +++ b/cranelift/codegen/src/isa/aarch64/abi.rs @@ -1210,6 +1210,20 @@ impl ABIMachineSpec for AArch64MachineDeps { } } + fn get_regs_partially_clobbered_by_call( + call_conv: isa::CallConv, + is_exception: bool, + ) -> PRegSet { + // A callee that follows [message truncated]
cfallin closed issue #15262:
.clifTest CaseA loop that calls a function and keeps three
f64values live across the call: the call's argumentv0, the factor
v1and the running sumv6. The function has no vector value.test.clif
function %f(f64, f64, i64) -> f64 { sig0 = (f64) -> f64 fn0 = %g sig0 block0(v0: f64, v1: f64, v2: i64): v3 = iconst.i64 0 v4 = f64const 0.0 v12 = iconst.i64 1 jump block1(v3, v4) block1(v5: i64, v6: f64): v7 = call fn0(v0) v8 = fmul v7, v1 v9 = fadd v6, v8 v10 = iadd v5, v12 v11 = icmp ult v10, v2 brif v11, block1(v10, v9), block2 block2: return v9 }Steps to Reproduce
First, build
clif-utilfrom wasmtime v50.0.0, the release that ships cranelift-codegen 0.137.0:
curl -L https://github.com/bytecodealliance/wasmtime/archive/refs/tags/v50.0.0.tar.gz | tar xz cd wasmtime-50.0.0 cargo build --release -p cranelift-tools --bin clif-utilThen, compile the test case and print its disassembly:
target/release/clif-util compile --target aarch64 --set opt_level=speed -D 1-test.clif
clif-util compileonly compiles, so the output does not depend on the host. The listings below omit the
instruction bytes.Then, apply the patch under Extra Info, build again, and compile the test case again.
Expected Results
The loop keeps
v0,v1andv6in v8–v15 across the call, with no load or store per iteration. AAPCS64 preserves
the low 64 bits of those registers, which is all of anf64, and the prologue saves the ones that the function
uses. With the patch, the loop from0x2cto0x70moves the values between registers, and the prologue savesd8,
d9andd15once:Disassembly of 136 bytes <%f>: 0: stp x29, x30, [sp, #-0x10]! 4: mov x29, sp 8: stp x19, x20, [sp, #-0x10]! c: str d15, [sp, #-0x10]! 10: stp d8, d9, [sp, #-0x10]! 14: mov x20, x0 18: mov v8.16b, v0.16b 1c: mov v15.16b, v1.16b 20: mov x19, #0 24: movi v0.2s, #0 28: mov v9.16b, v0.16b 2c: ldr x13, #0x34 30: b #0x3c 34: udf #0 38: udf #0 3c: mov v0.16b, v8.16b 40: blr x13 44: add x19, x19, #1 48: mov v1.16b, v15.16b 4c: fmul d16, d0, d1 50: mov v0.16b, v9.16b 54: fadd d0, d0, d16 58: mov x0, x20 5c: cmp x19, x0 60: b.hs #0x74 64: mov x20, x0 68: mov v9.16b, v0.16b 6c: mov v15.16b, v1.16b 70: b #0x2c 74: ldp d8, d9, [sp], #0x10 78: ldr d15, [sp], #0x10 7c: ldp x19, x20, [sp], #0x10 80: ldp x29, x30, [sp], #0x10 84: retActual Results
Every iteration stores and reloads the floats: the loop from
0x28to0x6chas three 128-bit loads (ldur q) and
two 128-bit stores (stur q), to keep values that the callee would have preserved:Disassembly of 128 bytes <%f>: 0: stp x29, x30, [sp, #-0x10]! 4: mov x29, sp 8: stp x19, x20, [sp, #-0x10]! c: sub sp, sp, #0x30 10: mov x20, x0 14: stur q0, [sp] 18: stur q1, [sp, #0x10] 1c: mov x19, #0 20: movi v0.2s, #0 24: stur q0, [sp, #0x20] 28: ldr x13, #0x30 2c: b #0x38 30: udf #0 34: udf #0 38: ldur q0, [sp] 3c: blr x13 40: add x19, x19, #1 44: ldur q1, [sp, #0x10] 48: fmul d16, d0, d1 4c: ldur q0, [sp, #0x20] 50: fadd d0, d0, d16 54: mov x0, x20 58: cmp x19, x0 5c: b.hs #0x70 60: mov x20, x0 64: stur q1, [sp, #0x10] 68: stur q0, [sp, #0x20] 6c: b #0x28 70: add sp, sp, #0x30 74: ldp x19, x20, [sp], #0x10 78: ldp x29, x30, [sp], #0x10 7c: retVersions and Environment
Cranelift version or commit: cranelift-codegen 0.137.0, with regalloc2 0.15.2, built into
clif-utilfrom wasmtime
v50.0.0 (commit ebe5e8df1f4e12c5fc2f5954926881d8d61367d8) with rustc 1.98.0,--releaseand default features.Operating system: macOS 26.6.2 (build 25G83).
Architecture: arm64 (Apple M5 Pro), compiling for
--target aarch64.Extra Info
The limitation.
default_aapcs_clobbers()inisa/aarch64/abi.rssays:The upper 64 bits of v8 - v15 inclusive are also caller-saves. However, because we cannot currently represent
partial registers to regalloc2, we indicate here that every vector register is caller-save. Because this function
is used at callsites, approximating in this direction (save more than necessary) is conservative and thus safe.This stopgap was chosen in #2254 and #2267, where a follow-up was noted so that prologues and epilogues deal only with
the lower 64 bits. In regalloc2, partial registers are out of scope (bytecodealliance/regalloc2#247).The proposal. A function with no value wider than 64 bits loses nothing when a call clobbers only the upper
halves of v8–v15, so it can drop them from that call's clobbers without regalloc2 knowing about partial registers:
A new provided method,
ABIMachineSpec::get_regs_partially_clobbered_by_call(callee_conv, is_exception), returns
v8–v15 on aarch64 wherever the default AAPCS clobbers apply, and an empty set elsewhere and on other targets.
Callee::newcomputes ahas_wide_valuesflag from the types of the function'sdfgvalues, as it already scans
the dynamic types. A type that the backend cannot place in registers counts as wide.When the function has no wide value,
gen_call_infodropspartial(callee_conv) ∩ partial(caller_conv)from the
call's clobbers.There is no new setting and no call for the embedder to make. The other users of the static clobber sets are
unchanged and stay conservative. s390x carries the same comment and could opt in the same way.Soundness.
The intersection with the caller's own convention matters for a PreserveAll caller. A call's clobbers also decide
what the prologue saves, so such a caller still treats v8–v15 as clobbered and saves all 128 bits of them.An exception edge is narrowed only where the unwinder already restores callee-saves, that is, for a SystemV or
Appletry_call.Winch, PreserveAll and Tail exception edges are unchanged.
Evidence. With the patch, two aarch64 filetests change when blessed: in
call.clifanduser_stack_maps.clif,
the q-register spills becomemovto v8–v15, and the prologue savesd8,d11,d12ord15withstpor
str. Stockclif-utilfails both blessed files. With the patch and the blessed files,target/release/clif-util test cranelift/filetests/filetestsprinted
1322 testswith no failure. The following test,run.clifcalls a function that writes
all 128 bits of v8–v15 from a caller that keeps scalars in them across the call:<details><summary>run.clif</summary>
test run target aarch64 ; Writes whole q registers under high pressure, so that it uses v8 - v15 and, ; as AAPCS lets it, returns with only their low halves restored. function %clobber_vectors(f64) -> f64 { block0(v0: f64): v1 = splat.f64x2 v0 v2 = fadd v1, v1 v3 = fadd v2, v1 v4 = fadd v3, v2 v5 = fadd v4, v3 v6 = fadd v5, v4 v7 = fadd v6, v5 v8 = fadd v7, v6 v9 = fadd v8, v7 v10 = fadd v9, v8 v11 = fadd v10, v9 v12 = fadd v11, v10 v13 = fadd v12, v11 v14 = fadd v13, v12 v15 = fadd v14, v13 v16 = fadd v15, v14 v17 = fadd v16, v15 v18 = fadd v17, v16 v19 = fadd v18, v17 v20 = fadd v19, v18 v21 = fadd v20, v19 v22 = fadd v21, v20 v23 = fadd v22, v21 v24 = fadd v23, v22 v25 = fadd v24, v23 v26 = fadd v25, v24 v27 = fadd v26, v25 v28 = fadd v27, v26 v29 = fadd v28, v27 v30 = fadd v29, v28 v31 = fadd v30, v29 v32 = fadd v31, v30 v33 = fadd v32, v31 v40 = fadd v1, v2 v41 = fadd v40, v3 v42 = fadd v41, v4 v43 = fadd v42, v5 v44 = fadd v43, v6 v45 = fadd v44, v7 v46 = fadd v45, v8 v47 = fadd v46, v9 v48 = fadd v47, v10 v49 = fadd v48, v11 v50 = fadd v49, v12 v51 = fadd v50, v13 v52 = fadd v51, v14 v53 = fadd v52, v15 v54 = fadd v53, v16 v55 = fadd v54, v17 v56 = fadd v55, v18 v57 = fadd v56, v19 v58 = fadd v57, v20 v59 = fadd v58, v21 v60 = fadd v59, v22 v61 = fadd v60, v23 v62 = fadd v61, v24 v63 = fadd v62, v25 v64 = fadd v63, v26 v65 = fadd v64, v27 v66 = fadd v65, v28 v67 = fadd v66, v29 v68 = fadd v67, v30 v69 = fadd v68, v31 v70 = fadd v69, v32 v71 = fadd v70, v33 v72 = extractlane v71, 1 return v72 } ; No vector: the scalars stay in v8 - v15 across the call. function %scalar_caller(f64, f64, f64) -> f64 { fn0 = colocated %clobber_vectors(f64) -> f64 block0(v0: f64, v1: f64, v2: f64): v3 = call fn0(v0) v4 = fmul v1, v2 v5 = fadd v4, v1 v6 = fadd v5, v2 return v6 } ; run: %scalar_caller(0x1.0, 0x3.0, 0x5.0) == 0x17.0 ; A vector live across the call: v8 - v15 stay clobbered, so it is spilled. function %vector_caller(f64, f64) -> f64 { fn0 = colocated %clobber_vectors(f64) -> f64 block0(v0: f64, v1: f64): v2 = splat.f64x2 v1 v3 = call fn0(v0) v4 = extractlane v2, 1 v5 = fadd v4, v1 return v5 } ; run: %vector_caller(0x1.0, 0x3.0) == 0x6.0</details>
target/release/clif-util test run.clifIt printed
1 testswith no failure, with and without the patch. With its expected value changed from0x17.0to
0x18.0, it failed withactual: 0x1.7000000000000p4, which shows that the test runs.A precise alternative would be per-value partial clobbers in regalloc2.
<details><summary>Patch against cranelift-codegen 0.137.0</summary>
diff --git a/cranelift/codegen/src/isa/aarch64/abi.rs b/cranelift/codegen/src/isa/aarch64/abi.rs index 8659561..8c953bc 100644 --- a/cranelift/codegen/src/isa/aarch64/abi.rs +++ b/cranelift/codegen/src/isa/aarch64/abi.rs @@ -1210,6 +1210,20 @@ impl ABIMachineSpec for AArch64MachineDeps { } } + fn get_regs_partially_clobbered_by_call( + call_conv: isa::CallConv, + is_exception: bool, + ) -> PRegSet { + // A callee that follows [message truncated]
cfallin commented on issue #15262:
Thanks. This is a known limitation, as you say. I don't think we want a function-global property computed (
has_wide_values); that is brittle in that it can cause significant performance shifts when unrelated code is changed. It's also a pretty subtle invariant that we have to be careful about.I'd rather see effort invested toward partial registers in RA2, if you're interested in pursuing this further. It might be possible to do something about this by recording "partialness" on both clobbers and on defs and detecting conflicts while accounting for that, without a full implementation of sub-registers. I'll go ahead and close this issue in favor of that because (i) the limitation is already tracked in RA2's issue tracker and (ii) the patch in this issue is not what I'd like to see pursued, as described above.
kestred commented on issue #15262:
@cfallin , I'll look into the complexity/cost/benefit of "partialness" in clobbers in regalloc!
kestred edited a comment on issue #15262:
Thanks @cfallin! I'll look into the complexity/cost/benefit of "partialness" in clobbers in regalloc2.
kestred edited a comment on issue #15262:
Thanks @cfallin! I'll look into the complexity/cost/benefit of "partialness" in clobbers in regalloc2.
This measures as a pretty big runtime win on the production programs I compile on MacOS
(w/o sacrificing compile time), so its something I intend to try and find a good solution for.
kestred edited a comment on issue #15262:
Thanks @cfallin! I'll look into the complexity/cost/benefit of "partialness" in clobbers in regalloc2.
This measures as a pretty big runtime win on the production programs I use crane lift to compile on MacOS
(w/o sacrificing compile time), so its something I intend to try and find a good solution for.
Last updated: Nov 19 2026 at 20:05 UTC