Feature Request: add a way in rustc for generating Struct type llvm IR without paddings

I am improving the support of arm(aarch64) mods in the stdarch project. There are currently 500+ arm instructions being blocked due to the issue about llvm IR. We should have a way to generate Struct type llvm IR without paddings.

I studied the mechanism of generating llvm IR in rustc. At present, the compiler will automatically add paddings when generating a Struct type llvm IR.(in this fuction)

I can think of two ways to tell the compiler not to add paddings: (1) Extend the #[repr] attribute, such as #[repr(no_padding)] (2) Add a new attribute separately. Either of them should be unstable, since I cannot think of a reliable method to ensure the safety of IR. I can do the corresponding implementation, if agreed.

May I have some suggestion here and wether it may be necessary to submit an rfc?

3 Likes

@bjorn3 @Amanieu

Isn't this just #[repr(packed)]?

I tried it, but using #[repr(packed)] will still make the compiler generate IR with paddings: godbolt

In the function that generates IR, the packed variable does not affect the addition of paddings in the IR result

Does it need to be a new attribute? Isn't it simply an implementation deficiency that useless padding is generated for repr(C, packed)?

I have thought about it too, and tried to modify the IR generating function so that it does not generate paddings when all the padding sizes are 0. But in this way, parameter indices errors have raised in many places during testing, and these errors all come from the check function of llvm. I don’t know the specific reason why they reported the error.

In other words, I am not sure when these paddings should exist, which is why I think the new attribute can only be unstable

I think the field projection code in rustc_codegen_llvm needs to be changed for the lack of the padding fields. This would still be the case if it was a new attribute.

2 Likes

Just a heads-up: I have a proof-of-concept implementation working which removes zero-sized padding from the emitted LLVM IR regardless of the ABI - see PR. It just needs some optimization.

5 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.