cranelift: Add newtype wrappers for x64 register classes#3752
Conversation
Subscribe to Label ActionDetailsThis issue or pull request has been labeled: "cranelift", "cranelift:area:aarch64", "cranelift:area:machinst", "cranelift:area:x64", "isle"Thus the following users have been cc'd because of the following labels:
To subscribe or unsubscribe from this label, edit the |
cfallin
left a comment
There was a problem hiding this comment.
Thanks for doing all of this -- it's really quite tedious!
I admit to harboring a little doubt as I look at the number of conversions (to_writable_reg, from_writable_reg, etc) and type-tetris this code adds, though this has to be traded off against the correctness benefits. The Writable<T> technique has likely saved us a few times (judged by the several regalloc metadata sort of bugs we've had) and this will probably have similar yields.
I guess a good answer to that is that implicit conversions (#3753) should make almost all of these go away; we should probably prioritize that work (I can take a look at it soon-ish probably, once I'm back from Wasmtime-land).
| ;; Construct a new `XmmMem` from the given `RegMem`. | ||
| ;; | ||
| ;; Asserts that the `RegMem`'s register, if any, is an XMM register. | ||
| (decl xmm_mem_new (RegMem) XmmMem) |
There was a problem hiding this comment.
Would these constructors be clearer without the _new suffix, I wonder? I'm mostly thinking in terms of symmetry with e.g. instruction constructors and the other utility types -- we have (value_regs ...), not (value_regs_new ...). (Or maybe there's a name conflict from elsewhere I'm missing?)
There was a problem hiding this comment.
I can do reg_mem_to_xmm_mem which is more similar to the other constructors. Thoughts?
There was a problem hiding this comment.
That seems reasonable to me! I guess as a general principle the x_to_y explicitness is good and is a sort of direct indication that we can register all such constructors as implicit conversions later.
Yes, I think this the type conversions mechanism will make all of this much better. |
94e2beb to
61cfd3c
Compare
I need this to move the x64 `Inst` definition into ISLE without losing its custom `Debug` implementation that prints the assembly for the `Inst`.
This primary motivation of this large commit (apologies for its size!) is to
introduce `Gpr` and `Xmm` newtypes over `Reg`. This should help catch
difficult-to-diagnose register class mixup bugs in x64 lowerings.
But having a newtype for `Gpr` and `Xmm` themselves isn't enough to catch all of
our operand-with-wrong-register-class bugs, because about 50% of operands on x64
aren't just a register, but a register or memory address or even an
immediate! So we have `{Gpr,Xmm}Mem[Imm]` newtypes as well.
Unfortunately, `GprMem` et al can't be `enum`s and are therefore a little bit
noisier to work with from ISLE. They need to maintain the invariant that their
registers really are of the claimed register class, so they need to encapsulate
the inner data. If they exposed the underlying `enum` variants, then anyone
could just change register classes or construct a `GprMem` that holds an XMM
register, defeating the whole point of these newtypes. So when working with
these newtypes from ISLE, we rely on external constructors like `(gpr_to_gpr_mem
my_gpr)` instead of `(GprMem.Gpr my_gpr)`.
A bit of extra lines of code are included to add support for register mapping
for all of these newtypes as well. Ultimately this is all a bit wordier than I'd
hoped it would be when I first started authoring this commit, but I think it is
all worth it nonetheless!
In the process of adding these newtypes, I didn't want to have to update both
the ISLE `extern` type definition of `MInst` and the Rust definition, so I move
the definition fully into ISLE, similar as aarch64.
Finally, this process isn't complete. I've introduced the newtypes here, and
I've made most XMM-using instructions switch from `Reg` to `Xmm`, as well as
register class-converting instructions, but I haven't moved all of the GPR-using
instructions over to the newtypes yet. I figured this commit was big enough as
it was, and I can continue the adoption of these newtypes in follow up commits.
Part of bytecodealliance#3685.
61cfd3c to
2c77cf8
Compare
This primary motivation of this large commit (apologies for its size!) is to
introduce
GprandXmmnewtypes overReg. This should help catchdifficult-to-diagnose register class mixup bugs in x64 lowerings.
But having a newtype for
GprandXmmthemselves isn't enough to catch all ofour operand-with-wrong-register-class bugs, because about 50% of operands on x64
aren't just a register, but a register or memory address or even an
immediate! So we have
{Gpr,Xmm}Mem[Imm]newtypes as well.Unfortunately,
GprMemet al can't beenums and are therefore a little bitnoisier to work with from ISLE. They need to maintain the invariant that their
registers really are of the claimed register class, so they need to encapsulate
the inner data. If they exposed the underlying
enumvariants, then anyonecould just change register classes or construct a
GprMemthat holds an XMMregister, defeating the whole point of these newtypes. So when working with
these newtypes from ISLE, we rely on external constructors like
(gpr_to_gpr_mem my_gpr)instead of(GprMem.Gpr my_gpr).A bit of extra lines of code are included to add support for register mapping
for all of these newtypes as well. Ultimately this is all a bit wordier than I'd
hoped it would be when I first started authoring this commit, but I think it is
all worth it nonetheless!
In the process of adding these newtypes, I didn't want to have to update both
the ISLE
externtype definition ofMInstand the Rust definition, so I movethe definition fully into ISLE, similar as aarch64.
Finally, this process isn't complete. I've introduced the newtypes here, and
I've made most XMM-using instructions switch from
RegtoXmm, as well asregister class-converting instructions, but I haven't moved all of the GPR-using
instructions over to the newtypes yet. I figured this commit was big enough as
it was, and I can continue the adoption of these newtypes in follow up commits.
Part of #3685.