SROA will drop the explicit alignment on allocas when the ABI guarantees enough alignment. Because the alignment on new load/store instructions are set base on the alloca's alignment, that means SROA would end up dropping the alignment from atomic loads and stores, which is not allowed (see bug). For those, make sure to always carry over the alignment from the previous instruction.
Details
Diff Detail
Event Timeline
I would prefer to just avoid creating unaligned allocas in the first place. https://github.com/llvm/llvm-project/blob/740ed617f7d4d16e7883636c5eff994f8be7eba4/llvm/lib/Transforms/Scalar/SROA.cpp#L4163 has some code which intentionally uses an unspecified alignment... but really, there's no reason to do that. allocas with no specified alignment are implicitly aligned using the ABI type alignment. So it's semantically equivalent to std::max(Alignment, DL.getABITypeAlignment(SliceTy), just more annoying to deal with.
Really, the alignment of load/store/alloca shouldn't be a MaybeAlign in the first place, but someone needs to go through and clean up a bunch of places to make that happen.
The current behaviour is from https://github.com/llvm/llvm-project/commit/903790eff54f473fca8fbf6915713751a070b5df
Changing that seems like a riskier change (I want this for 10.x) whereas copying over the alignment from the store/load seems straight-forward to me.
llvm/test/Transforms/SROA/alignment.ll | ||
---|---|---|
242 | From the code, it looks like you want a test where the load is volatile only as well? Does this affect other memory operations, i.e. the atomic ones? |
Changing that seems like a riskier change (I want this for 10.x) whereas copying over the alignment from the store/load seems straight-forward to me.
It's not completely obvious at first glance that the alloca is actually sufficiently aligned. But looking a little more carefully, it should be; we're preserving the alignment of the original alloca. So this is fine, I guess. LGTM
llvm/test/Transforms/SROA/alignment.ll | ||
---|---|---|
242 | I don't think the volatile-only case is interesting. My code doesn't change anything for that case. I've made the test cover stores too. I'm not sure what you mean by "other memory operations, i.e. the atomic ones". This change affects exactly the atomic memory operations.. |
I know, but if it grows that capability then the author will thank you for adding a test that prevents them from regressing correctness.
NewLI->setAlignment(LI.getAlign());