Open NotAFlyingGoose opened 1 week ago
You may be interested in the topic at https://bytecodealliance.zulipchat.com/#narrow/stream/217117-cranelift/topic/.E2.9C.94.20How.20to.20properly.20use.20ArgumentPurpose.3A.3AStructArgument.3F
tl;dr: StructArgument
should not be used on arm64. Also
Just like LLVM, Cranelift only handles the lower half of the calling convention. The upper half which lowers C types is required to be implemented by the frontend. You can find the rust implementations of this for various architectures at https://github.com/rust-lang/rust/blob/master/compiler/rustc_target/src/abi/call This code is shared by the LLVM, GCC and Cranelift backends of rustc.
Also if you control the calling convention on both sides, I strongly suggest avoiding StructArgument
entirely and just passing pointers if you need to pass a struct as argument. StructArgument
primarily exists for calling with native C functions that take a struct as argument on architectures where this is done by passing the struct at a fixed stack offset rather than by passing a pointer.
Edit: Seems like you actually want compatibility with native C functions. In that case you do actually need to reimplement the target specific lowerings as found at https://github.com/rust-lang/rust/blob/master/compiler/rustc_target/src/abi/call yourself.
Thank you! This is very helpful!
In any case, if StructArgument
isn't meant to be used on a64 then there should probably be a better error message than an assertion failure. Either that or something else should be done. The code does end up compiling when using cranelift_object, so the panic in cranelift_jit should probably be fixed.
I just noticed that the assertion failure is unrelated to StructArgument
. It is the assertion at https://github.com/bytecodealliance/wasmtime/blob/81efaa738419c6e2078e4ad6d14df1e1461d415f/cranelift/jit/src/compiled_blob.rs#L90 I'm not sure when this assertion fires though.
.clif
Test CaseSteps to Reproduce
I've created a MRE here
The basic issue is caused when declaring a function with
AbiParam::special(ptr_ty, ArgumentPurpose::StructArgument(aggr_pointee_size))
as a parameter.This is not an issue with
cranelift_object
. This assertion failure specifically happens incranelift_jit
Expected Results
I expected the above MRE to compile and print the number 42 to the screen. This happens when using x86,
cranelift_object
, or not usingArgumentPurpose::StructArgument
. When using A64,cranelift_jit
, andArgumentPurpose::StructArgument
, cranelift panicsActual Results
When calling
module.finalize_definitions()
, the MRE reaches an assertion failureVersions and Environment
Cranelift version or commit: I just updated the MRE to 0.109 this morning and the error is still present
Operating system: macOS 14.3.1
Architecture: Apple M2 (Arm64) It's important to note that this ONLY occurs on Arm64, which makes sense as that's where the assertion failure is. The MRE should work as expected on x86.
Extra Info
The exact line in the MRE to look at is main.rs:152