[llvm-dev] Adding FP environment register modeling for constrained FP nodes

Kaylor, Andrew via llvm-dev llvm-dev at lists.llvm.org
Tue Feb 14 10:25:24 PST 2017

Hi Hal,

Thanks for the guidance.  I hope you don’t mind that I’m adding LLVMDev to this e-mail thread, as it seems as though it may be of general interest.

I agree that duplicating the FP opcodes should be our goal.  I just wasn’t sure that was entirely possible.  I’ll try adding implicit defs in the way you’ve suggested, but I’m concerned that there may be code that relies on the TII for that kind of thing -- for instance, InstrEmitter::EmitMachineNode() does this:

  bool HasPhysRegOuts = NumResults > NumDefs && II.getImplicitDefs()!=nullptr;

where “NumDefs” comes from TII and “NumResults” comes from the node.  Obviously we can fix that up as needed, but it seems like a weak point in the design.  Perhaps it is still better than trying to maintain a duplicate set of opcodes though.

I’m still trying to piece together how to get the set of nodes to be updated from the SelectionDAG to the InstrEmitter.  I’m still learning my way around this code.

In any event, I can confirm that for X86 targets the control register uses are not currently modeled.  I just committed a patch yesterday adding the MXCSR register and updating the instructions that directly read and write it (but still implicitly so).  I suppose you are correct that there is no reason not to add uses of that register to the instructions that derive their rounding behavior from it and then the constrained FP intrinsics will just need to add implicit defs where needed.  I’ll also need to add the x87 control register as that isn’t modeled at all right now.


From: Hal Finkel [mailto:hfinkel at anl.gov]
Sent: Friday, February 10, 2017 4:58 PM
To: Kaylor, Andrew <andrew.kaylor at intel.com>
Subject: Re: Adding FP environment register modeling for constrained FP nodes

On 02/09/2017 06:41 PM, Kaylor, Andrew wrote:
Hi Hal,

I’ve been trying to figure out enough of the instruction selection code to be able to attach implicit use/def information for DAG nodes that have been selected from constrained FP pseudo-ops.  You seemed to have a clear idea in your mind of how this could be done.  I had an idea for what I intended, but I didn’t really know how to do it.

As I’ve been digging in to this, it seems that my idea that the implicit register use was something I could just append to an existing node was incorrect.  If I understand correctly, the implicit register use often (if not always) comes from TargetInstrInfo queries based on the opcode.  If this is right, then I’d need to introduce a new machine opcode for any instruction that I wanted to have an implicit register use.  For instance, if we just mutated ISD::STRICT_FADD to ISD::FADD and Select changed that to X86::MULSDrm, then to attach implicit MXCSR use to that I’d need a new opcode (X86::MULSDrm_Strict) that behaves just like X86::MULSDrm but has the implicit MXCSR use/def information associated with it.

Does it sound like I’m understanding this correctly?

You're correct that implicit operands normally come from the instruction definitions, but you can add whatever other implicit operands you'd like. We often do this to handle calls, returns, etc. - we add implicit operands corresponding to parameters and return values.

My thought was that, in SelectionDAGISel::DoInstructionSelection, after the call to Select, Node should have been mutated to be the associated machine instruction. We can then have a target hook which then updates the node operands to also have the implicit register dependencies. However, that does not explain how to actually do that: At the MI level, you can always call addOperand to add another implicit operand. Adding implicit uses at the SDAG level is also straightforward (because you can mutate the node to have additional operands that you get by calling CurDAG->getRegister(...).

I think the easiest way to do this is to add a map of the nodes to which we need to add the extra operands, and then we can just add the additional operands at the MI level from InstrEmitter.cpp after we otherwise build the MI representation for the instruction.

If so, is there a simple way to create these duplicate opcodes?  I’m looking at .td files trying to figure it out and it’s looking like a can of worms.  Is there a way to do this from TableGen’s

I specifically want to do this without duplicating the opcodes if possible. Duplicating the opcodes requires a lot more code changes to the backends that I'd like to avoid.


I’m also not sure I know what I’m doing with finding the nodes that need to be updated.  In the simplest case the FP node gets updated directly into a single machine opcode node, but I don’t think that always happens and I’m not sure I can even count on the original node not being deleted.  I’ve experimented with using a DAGUpdateListener, but I’m not sure that does everything I need it to.  Am I overthinking this?

I think you're on the right track. DAGUpdateListener seems like the right approach. ISel already uses these for other purposes, and this will allow to do track what has happened to the node being instruction-selected. You're correct that there might be more than one instruction created as a result, but I don't think that should be a problem because you should only need to modify the final instruction.

At least on PowerPC, all of the floating-point instructions are already given implicit (use) operands of RM (which stands for 'rounding mode', but is used to represent all of the FP-environment state). None of these instructions, however, define RM, so there are no dependencies created. I think that the target callback on PowerPC would just need to add an implicit register def of 'RM' to the "final" instruction representing any operation to create the necessary dependency relationships.

X86 might not have these implicit uses already?

I’m starting to wonder if it might be better to back out what I did in the ISel and just require target-specific pattern matching and whatever other implementation bits are needed.

On the good news side of things, I wrote a simple function pass to translate all FP operations into the constrained intrinsics and with that pass being run as early as possible I can successfully run some real-world programs with extensive FP usage.

Neat :-)

I imagined that the way we'd do this is by putting a mode into the IRBuilder where it will generate the intrinsics instead of the regular instructions (kind of like it has a mode in which it will add fast-math flags by default).

Thanks again,

Anyway, if you can offer any guidance and tell me whether or not I’m heading in the right direction I would be very grateful.



Hal Finkel

Lead, Compiler Technology and Programming Languages

Leadership Computing Facility

Argonne National Laboratory
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20170214/b9b01f63/attachment.html>

More information about the llvm-dev mailing list