Hi, I've just started working on cranelift and I'm trying to do some pattern matching which involves inspecting values and not just the input opcode. From the instruction I'm lowering, I want to look at the two inputs and figure out if each of them is using the same input value (same virtual register?) Is this possible, or is lowering just not the place where this can happen? I'm coming from LLVM, so any description is those terms of how instructions are represented and can be queried would be most welcome.
This topic was moved here from #general > Inspecting inputs during cranelift lowering by Till Schneidereit
Hi Sam, and welcome! I moved this topic to the #cranelift stream for more visibility to folks focusing on Cranelift. Many of them will probably only be around a bit later in the day, though :smile:
I think the Shuffle
operation is doing that in the x64 backend: it checks whether its two operands live in the same register and emits a different lowering if that is the case.
Right, we can do the comparisons on registers once they've been 'put', but can I inspect the inputs to the inputs (which I assumed haven't been visited yet)? From my extremely limited understanding, it looks like lowering is done with a per-instruction context, which is why I'm suspecting I can't look at much but the current instruction.
And the answer is yes... put_input_in_reg can be used on the input's inputs :)
Hi @Sam Parker , there are several lowering patterns in the current backends that look beyond the inputs to the currently-being-lowered instruction; see e.g. lower_to_amode
in aarch64 (looks through extends and a tree of adds to find all addends), or xext/uext handling in aarch64, or uext handling in x64
and, indeed, once you have an input further up the operand tree you can get it in a reg directly, as you've found!
note that some care should be taken when merging effectful ops -- see the sink
method on LowerCtx
if, e.g., you happen to merge a load
Last updated: Nov 22 2024 at 17:03 UTC