-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Type-confusion when storing i32 into a stack slot #2839
Comments
Unrelated, I think. Really the matter was to not have a test for the explained behavior above. For what it's worth, I've got a test case now, which was a bit hard to handwrite, but that will make sure that we won't undo this later again. |
Fantastic find, @bnjbvr -- thank you for tracking this down! |
Fixes bytecodealliance#2839. See also the issue description and comments in this commits for details of what the fix is about here.
Looks like this patch now introduced a new regression on s390x. The patch changes store_spillslot but not load_spillslot, which means that some values may now be stored as i64 and then loaded as i32 from the same address. This does not work on big-endian platforms. Shouldn't load_spillslot be using the same type as store_spillslot? If this is not possible or desirable for some reason, then we need to update the offset where the value is loaded from on big-endian systems in such cases. (However, it is not immediately clear how we'd know the target endianness in load_spillsplot. Seems this would require a new ABIMachineSpec callback.) I'd be happy to come up with a patch to fix this, just let me know which option you prefer. |
Sorry about that! I agree that yeah, since the |
Hmm, yes, I think the straightforward fix is to do the same type-promotion logic in It might be worth documenting what invariants we're upholding, too, just to make this clearer for future readers: specifically, for integer registers, the lowering logic can always assume that the full register will be preserved across spills/reloads, while for floating-point and vector registers, only the used portion is preserved. The other side of this contract is that the lowering must be precise with types on floats/vecs, but is free to generate moves between integer registers holding types of different sizes. |
OK, #2843 implements this and fixes the regression for me. |
Fixes bytecodealliance#2839. See also the issue description and comments in this commits for details of what the fix is about here.
Fixes bytecodealliance#2839. See also the issue description and comments in this commits for details of what the fix is about here.
That was indeed a suspicious comment. I am not sure exactly what the original intent was but I double-checked just now that this
store
helper is not being used to codegen stores; those directly produce correctly-sized instructions. I suspect this originally came from spill/reload helpers in which case "always spill/reload the full reg" is a reasonable conservative approach but even then we have the invariant that upper bits (beyond a type's width) in a register are undefined and extended when needed by an op so this should be fine I think. Thanks for calling it out!Originally posted by @cfallin in #2806 (comment)
I guess I'm coming after the battle, but this particular change introduced a regression. See #2277 for context, which added an optimization for the following situation. Imagine we have the following CLIF:
Thus v1 is 32-bits, and v2 is 64-bits. During lowering, we pattern-match this situation: in theory, the
iadd.i32
is compiled to anaddl
, which clears out the upper bits, so the zero-extension is spurious, and can be redefined as a plain copy of the input (a move!). So this might be lowered into the following vcode:Now, from the point of view of register allocation, both vcode's values are using the
RegClass::i64
, so the two virtual registers can be coalesced and allocated to the same register. That's all right... unless the register is spilledThe line ending with
spill to a stack slot as an i32
results from the change Alex made. Before this, the value would be stored as an i64, so the high bits from the iaddl would be stored in the stack slot and correctly reloaded. The comment's wording didn't quite help in describing this situation. Also, there should have been a test case, so that part is on me.So the issue is that register allocation kind of loses the precise type, and that a store to a stack slot should store the full width of the stack slot all the time, so there are no undefined bits when storing and reloading values within a given
RegClass
(here,RegClass::I64
). Alternatively, we might remove theuextend
elimination optimization, but that would increase register pressure and slow down the decoding pipeline a bit, so I'd rather go with the first solution.The text was updated successfully, but these errors were encountered: