Stream: cranelift

Topic: docs for StructArgument/StructReturn


view this post on Zulip Noah Lev (camelid) (Jul 01 2024 at 02:16):

Is there any documentation for how StructArgument/StructReturn work and should be used? I'm a little unclear about when to use multiple arguments and multiple returns vs these aggregate AbiParams. Thanks! I've been really enjoying using cranelift so far :)

view this post on Zulip bjorn3 (Jul 01 2024 at 12:29):

You will have to look up the abi specification document for your target and see if the type in question needs to be returned using a return pointer (=> use pointerStructReturn as first argument and write the return value there) or using multiple registers (=> use multiple returns). And similar for struct arguments. You can also look at how rustc lowers your type to llvm ir and reproduce this in clif ir.

view this post on Zulip bjorn3 (Jul 01 2024 at 12:29):

Or alternatively you can take the relevant file at https://github.com/rust-lang/rust/tree/master/compiler/rustc_target/src/abi/call and try to reproduce the logic in there.

Empowering everyone to build reliable and efficient software. - rust-lang/rust

view this post on Zulip Noah Lev (camelid) (Jul 01 2024 at 21:23):

Thanks!

view this post on Zulip Noah Lev (camelid) (Jul 01 2024 at 21:24):

Not fully related, but are zero-sized stack slots allowed by cranelift? If not of course I can just not emit them, but I wanted to see if I can leave my code as-is

view this post on Zulip bjorn3 (Jul 02 2024 at 09:51):

AFAIK Cranelift doesn't have any issues with zero-sized stack slots. If it does we should either fix it or add a verifier check for it.

view this post on Zulip Noah Lev (camelid) (Jul 02 2024 at 19:44):

bjorn3 said:

You will have to look up the abi specification document for your target and see if the type in question needs to be returned using a return pointer (=> use pointerStructReturn as first argument and write the return value there) or using multiple registers (=> use multiple returns). And similar for struct arguments. You can also look at how rustc lowers your type to llvm ir and reproduce this in clif ir.

Oh following up about this: Why does StructArgument require a size but StructReturn does not? And for both of them, I should use a pointer for the AbiParam's type?

view this post on Zulip bjorn3 (Jul 03 2024 at 09:21):

StructReturn only changes the register in which the pointer is stored. StructArgument doesn't actually pass a pointer around, instead it copies an area of the given size pointed to by the pointer you pass as user to a specific location on the stack where the callee expects it.

view this post on Zulip Noah Lev (camelid) (Jul 03 2024 at 19:38):

Oh, so I should put the type of the StructArgument as a pointer but then it will automatically handle the copying? Do I then access the data through a load on the argument?

view this post on Zulip Noah Lev (camelid) (Jul 03 2024 at 19:40):

I'm just a bit unclear where the copied data ends up

view this post on Zulip bjorn3 (Jul 03 2024 at 19:43):

Yes, the type of a StructArgument should be a pointer. Cranelift handles the copying automatically as part of the call instruction. The copied data ends up somewhere on the stack which is kept alive until the callee returns.


Last updated: Nov 22 2024 at 17:03 UTC