4 ms·
(I'm the author) As part of lifting you convert the x86 instructions into an IR so you can do optimization passes. This is how I was using Cranelift as a codege
by chc4 5y ago
(I'm the author)
As part of lifting you convert the x86 instructions into an IR so you can do optimization passes. This is how I was using Cranelift as a codegen backend; there is essentially an abstract interpreter over x86 that feeds into their function builder API. Cranelift then has optimizations for constant folding and things, along with redoing register allocation from the virtual registers that their IR uses internally before lowering to x86 again.
This is how qemu and DynamoRIO and things work too, where they have an IR which allows them to more abstract transform the code than having to keep it normalize to x86 or some other assembly the entire time.
As part of switching to my own IR I'm going to have to write my own optimization passes for it as well instead of being able to piggyback off Cranelift.
- celeritascelery 5y agoWhy was cranelift not a good fit as an optimizer?
- chc4 5y agoI go into a bit in https://github.com/chc4/lineiform/issues/19 https://github.com/chc4/lineiform/issues/19, but it's less a problem with its optimizer and more a problem with its IR constraints. Cranelift specifically has some rules about `iflags`, the type they use to conceptualize processor flags effects (e.g. add's carryout or overflow). You can only have one `iflags` value live at a time, and it can't overlap with any other math operation. This is a problem because the x86 we're lifting doesn't always follow that rule, so if we just emit Cranelift as we go it will panic and say we built an invalid function. The iflags design in general is kinda awkward too, and was being rethought a few months ago when I was first getting this working; I think they're planning on redesigning the add carryout interface and things to be slightly more streamlined. I suspect that any redesigned interface will have similar problems with mismatch between what I want from Cranelift and what 90% of other uses of Cranelfit want, though, and so I decided to just make my own IR instead.