Lobsters - 02 Dec 2026
Page 2 of 2
compilation of function on line 4 does not match
the text expectation
--- expected
+++ actual
@@ -0,0 +1,13 @@
+VCode:
+block0:
+ mov x5, x0
+ load_acquire_128 x0, x1, [x5], scratch=x4
+ ret
+
+Disassembled:
+block0: ; offset 0x0
+ mov x5, x0
+ ldaxp x0, x1, [x5] ; trap: heap_oob
+ stxp w4, x0, x1, [x5] ; trap: heap_oob
+ cbnz x4, #4
+ ret
This test assertion can be automatically updated by setting the
CRANELIFT_TEST_BLESS=1 environment variable when running this test.
1 tests
Error: 1 failure
It lowers the atomic_load.i128 to the LoadAcquire128 instruction and emits a sequence of machine instructions that looks correct. However, the test fails because we didn't actually add this as the expected compilation output. Let's bless it:
$ CRANELIFT_TEST_BLESS=1 cargo run -p cranelift-tools -- test filetests/filetests/isa/aarch64/atomic_load.clif
1 tests
The only thing missing now is to actually verify whether this generated code does the right thing. Fortunately, Cranelift also has runtests, which compile the IR with a JIT and execute it.
The runtest for 128-bit atomics is filetests/filetests/runtests/atomic-128.clif, which tests whether load, store, CAS and RMW all exhibit correct semantics. However, only one access is performed, so we cannot verify whether we have implemented the atomic semantics correctly. It would only catch egregious logic errors, like loading a wrong value.
function %atomic_load(i128) -> i128 {
ss0 = explicit_slot 16, align=16
block0(v0: i128):
v3 = stack_addr.i64 ss0
store.i128 notrap v0, v3
v1 = stack_addr.i64 ss0
v2 = atomic_load.i128 v1
return v2
}
; run: %atomic_load(0) == 0
; run: %atomic_load(-1) == -1
; run: %atomic_load(0x00000000_00000000_FFFFFFFF_FFFFFFFF) == 0x00000000_00000000_FFFFFFFF_FFFFFFFF
; run: %atomic_load(0xFFFFFFFF_FFFFFFFF_00000000_00000000) == 0xFFFFFFFF_FFFFFFFF_00000000_00000000
; run: %atomic_load(0xFEDCBA98_76543210_F7E6D5C4_B3A29180) == 0xFEDCBA98_76543210_F7E6D5C4_B3A29180
; run: %atomic_load(0xA00A00A0_0A00A00A_00A00A00_A00A00A0) == 0xA00A00A0_0A00A00A_00A00A00_A00A00A0
; run: %atomic_load(0xC0FFEEEE_ABCDEF01_DECAFFFF_12345678) == 0xC0FFEEEE_ABCDEF01_DECAFFFF_12345678
How the runtest for the atomic load works is that it stores a value to memory non-atomically and then performs the atomic load from the location, asserting that it is the same value that was stored.
Since we don't implement atomic store, CAS or RMW at this point, we can temporarily remove those from the test and run it in QEMU after adding target aarch64 at the top:
$ cargo run --target aarch64-unknown-linux-gnu -p cranelift-tools -- test filetests/filetests/runtests/atomic-128.clif
1 tests
As expected, it passes. The runtest is not very spectacular for load and store, but we will see how it can be particularly useful for troubleshooting logic errors.
Debugging
Unfortunately, there are times where not everything goes right the first time. Alex found that the following snippet would crash:
test run
set enable_multi_ret_implicit_sret
target aarch64
function %atomic_load_crashes_callee(i64) -> i128 {
block0(v0: i64):
v1 = atomic_load.i128 v0
return v1
}
function %atomic_load_crashes(i128) -> i128 {
ss0 = explicit_slot 16, align=16
fn0 = %atomic_load_crashes_callee(i64) -> i128
block0(v0: i128):
v1 = stack_addr.i64 ss0
store.i128 notrap v0, v1
v2 = call fn0(v1)
return v2
}
; run: %atomic_load_crashes(0) == 0
; run: %atomic_load_crashes(0x1234) == 0x1234
; run: %atomic_load_crashes(0xC0FFEEEE_ABCDEF01_DECAFFFF_12345678) == 0xC0FFEEEE_ABCDEF01_DECAFFFF_12345678
Running this test results in a crash with SIGSEGV. In order to understand what's going on here, it'd be great if I could just attach a debugger and see which instructions get executed. That sounds easier than it is, but fortunately, bjorn3 was able to help me do just that.
Setting PERF_BUILDID_DIR causes Cranelift's JIT to emit a map file /tmp/perf-$PID.map which contains a list of functions, their addresses in memory and their sizes.
$ PERF_BUILDID_DIR=1 gdb -q --args ../../target/debug/clif-util test ../filetests/filetests/runtests/load_crash.clif
Reading symbols from ../../target/debug/clif-util...
warning: Missing auto-load script at offset 0 in section .debug_gdb_scripts
of file /home/aelin/wasmtime/target/debug/clif-util.
Use `info auto-load python-scripts [REGEXP]' to list them.
Let's run the program:
(gdb) r
Starting program: /home/aelin/wasmtime/target/debug/clif-util test ../filetests/filetests/runtests/load_crash.clif
[New LWP 11025]
[New LWP 11026]
[New LWP 11027]
[New LWP 11028]
[New LWP 11029]
[New LWP 11030]
[New LWP 11031]
[New LWP 11032]
[New LWP 11033]
[New LWP 11034]
[New LWP 11035]
[New LWP 11036]
[New LWP 11037]
[LWP 11036 exited]
[LWP 11029 exited]
[LWP 11028 exited]
[LWP 11027 exited]
[LWP 11031 exited]
[LWP 11030 exited]
[LWP 11033 exited]
[LWP 11032 exited]
[LWP 11034 exited]
[LWP 11037 exited]
[LWP 11035 exited]
Thread 3 "worker #0" received signal SIGSEGV, Segmentation fault.
[Switching to LWP 11026]
0x0000fffff75cf004 in ?? ()
At this point, the map file has been created. Let's take a look:
(gdb) !cat /tmp/perf-11019.map
fffff75cf000 10 %atomic_load_crashes_callee
fffff75cf010 30 u0:2
fffff75cf040 38 %atomic_load_crashes
fffff75cf078 34 u0:3
Judging by our program counter, we're crashing inside of %atomic_load_crashes_callee. Now, with the help of the output from the script, we can inspect which instructions Cranelift has generated for this method:
(gdb) disas 0xfffff75cf000, 0xfffff75cf000+0x10
Dump of assembler code from 0xfffff75cf000 to 0xfffff75cf010:
0x0000fffff75cf000: ldaxp x0, x1, [x0]
=> 0x0000fffff75cf004: stxp w4, x0, x1, [x0]
0x0000fffff75cf008: cbnz x4, 0xfffff75cf000
0x0000fffff75cf00c: ret
End of assembler dump.
We're crashing on the stxp. That is because ldaxp uses x0 both as the address to load from, but also immediately reuses the register as one of the two destination GPRs to load the value into, which means it no longer contains a valid address for stxp.
Remember what we told regalloc about the instruction:
collector.reg_use(rn);
collector.reg_def(rt1);
collector.reg_def(rt2);
collector.reg_def(scratch);
This is actually not entirely correct. The way the register allocator in Cranelift works is that it assumes a reg_def is not followed by any uses, i.e. it happens at the very end of the instruction. Therefore, with the current constraints, it would be able to reuse the register assigned to rn for any of rt1, rt2 or scratch.
The solution here is to mark them as reg_early_def instead, which means that the register may be written to before all uses are read. That way it won't overwrite the use of rn. With this change (which Alex made in this commit, thanks!), this will not overwrite the address register anymore and the test case will pass.
Upstreaming
Once I was done with the implementation of all the operations, I opened a pull request to Cranelift with the changes. Timing-wise, this lined up conveniently with the Cranelift weekly meeting, which I was able to attend thanks to bjorn3, who came up with the idea and shared the meeting link with me shortly before.
This turned out to be immensely helpful, because review capacity in wasmtime was very scarce due to a plethora of security issues reported at the time. Fortunately, in the meeting, Jimmy volunteered to review my PR and it was merged shortly afterwards.
There's more to do
Quite early while working on this, I realized that AArch64 was not the only backend that was missing a 128-bit atomic implementation; RISC-V and s390x also lacked one. Fortunately, none of the rustc targets for RISC-V set max_atomic_width to 128, so the standard library does not expose 128-bit atomic types on these targets.
The s390x targets however do, so I also implemented the necessary changes for the s390x backend (1, 2, 3). For various reasons,[2] this turned out to be more complicated than I had anticipated, but in the end and after another very helpful Cranelift weekly meeting, we managed to find a solution that works.
The result
128-bit atomic support for AArch64 and s390x shipped in Cranelift 0.138, and rustc_codegen_cranelift is now finally able to compile the standard library without patches.
I'd like to thank the bytecodealliance folks for being so welcoming to new contributors - I was able to spontaneously hop in a meeting with a bunch of people, ask various questions, get valuable feedback and found a very kind reviewer for my changes. And all of this even though they were flooded with work. Cranelift is an excellent project to hack on and get started with!
- VCode is a control-flow graph of lowered Cranelift instructions, which are quite similar to machine instructions and usually map 1:1 to them, but not always. A notable exception would be the LoadAcquire128 instruction that we add later, which maps to multiple instructions on machine level.
- For those curious: Contrary to the other targets, the s390x ABI passes 128-bit integers indirectly. When such a parameter is used, Cranelift automatically emits a load. Due to Cranelift's API, this load can only be performed into a vector register. We opted to keep 128-bit integers entirely in vector registers to simplify things and make it more performant. More details can be found here.
Previous page | More Lobsters | Headlines
Original: https://trifectatech.org/blog/implementing-128-bit-atomics-in-cranelift/