<html><head><style type='text/css'>p { margin: 0; }</style></head><body><div style='font-family: arial,helvetica,sans-serif; font-size: 10pt; color: #000000'><br><hr id="zwchr"><blockquote style="border-left: 2px solid rgb(16, 16, 255); margin-left: 5px; padding-left: 5px; color: rgb(0, 0, 0); font-weight: normal; font-style: normal; text-decoration: none; font-family: Helvetica,Arial,sans-serif; font-size: 12pt;"><b>From: </b>"Nirav Rana" <nirav076@gmail.com><br><b>To: </b>hfinkel@anl.gov, llvm-dev@lists.llvm.org<br><b>Cc: </b>"Pandya Vivek" <h2015078@pilani.bits-pilani.ac.in>, h2015089@pilani.bits-pilani.ac.in, h2015172@pilani.bits-pilani.ac.in<br><b>Sent: </b>Sunday, November 27, 2016 2:37:14 PM<br><b>Subject: </b>Extending Register Rematerialization<br><br><div dir="ltr">Hello LLVM Developers,<div><br></div><div>We are working on extending currently available register rematerialization to include cases where sequence of multiple instructions is required to rematerialize a value.</div><div><br></div><div>We had a discussion on this in community mailing list and link is here: </div><div><a href="http://lists.llvm.org/pipermail/llvm-dev/2016-September/subject.html#104777" target="_blank">http://lists.llvm.org/pipermail/llvm-dev/2016-September/subject.html#104777</a><br></div><div><br></div><div>From the above discussion and studying the code we believe that extension can be implemented in same flow as current remat is implemented. What we unterstood is RegAlloc<>.cpp will try to allocate register to live-range, and if not possible, will call InlineSpiller.cpp to spill the live range. InlineSpiller.cpp will try to first rematerialize the register value if possible with help of LiveRangeEdit.cpp which provides various methods for checking if value is rematable or not.</div><div><br></div><div>So we have added a new function in LiveRangeEdit that traverses sequence of instruction in use-def chain recursively (instead of only current instruction in consideration) upto depth 6 (arbitrarily taken for testing) to check if value can be rematerialized with the sequence of instruction or not.</div><div><br></div><div>Here is the code:</div><div>//New function added for checking complex multi-instruction-sequence rematerializable</div><div>bool LiveRangeEdit::checkComplexRematerializable(VNInfo *VNI,</div><div> const MachineInstr *DefMI,</div><div> unsigned int depth,</div><div> AliasAnalysis *aa) {</div><div> if(TII.isReMaterializablePossible(*DefMI, aa))</div><div> return false;</div><div> DEBUG(dbgs() << " ComplexRemat MI: " << *DefMI);</div><div> for (unsigned i = 0, e = DefMI->getNumOperands(); i != e; ++i) {</div><div> const MachineOperand &MO = DefMI->getOperand(i);</div><div><br></div><div> if (!MO.isReg() || !MO.getReg() || !MO.readsReg())</div><div> continue;</div><div> if (TargetRegisterInfo::isPhysicalRegister(MO.getReg())) {</div><div> if (MRI.isConstantPhysReg(MO.getReg(), *DefMI->getParent()->getParent()))</div><div> continue;</div><div> //If not constant then check its def</div><div> if(depth > 6)</div><div> return false;</div><div><br></div><div> LiveInterval &li = LIS.getInterval(MO.getReg());</div><div> SlotIndex UseIdx = LIS.getInstructionIndex(*DefMI);</div><div> VNInfo *UseVNInfo = li.getVNInfoAt(UseIdx);</div><div><br></div><div> MachineInstr *NewDefMI = LIS.getInstructionFromIndex(UseVNInfo->def);</div><div> if(!checkComplexRematerializable(UseVNInfo, NewDefMI, depth+1, aa))</div><div> return false;</div><div> }</div><div> }</div><div> Remattable.insert(VNI); //May have to add new data structure</div><div> return true;</div><div>} </div><div><br></div><div>In above function we are calling a new function TII.isReMaterializablePossible(*DefMI, aa) which will act as early heuristic and return false by checking if instruction is definitely not rematerialize. We have found some cases from TargetInstrInfo::isReallyTriviallyReMaterializableGeneric and code for same is here:</div><div><br></div><div><div>bool TargetInstrInfo::isReMaterializablePossible(</div><div> const MachineInstr &MI, AliasAnalysis *AA) const {</div><div> const MachineFunction &MF = *MI.getParent()->getParent();</div><div> const MachineRegisterInfo &MRI = MF.getRegInfo();</div><div><br></div><div> // Remat clients assume operand 0 is the defined register.</div><div> if (!MI.getNumOperands() || !MI.getOperand(0).isReg())</div><div> return false;</div><div> unsigned DefReg = MI.getOperand(0).getReg();</div><div><br></div><div> // A sub-register definition can only be rematerialized if the instruction</div><div> // doesn't read the other parts of the register. Otherwise it is really a</div><div> // read-modify-write operation on the full virtual register which cannot be</div><div> // moved safely.</div><div> if (TargetRegisterInfo::isVirtualRegister(DefReg) &&</div><div> MI.getOperand(0).getSubReg() && MI.readsVirtualRegister(DefReg))</div><div> return false;</div><div><br></div><div> // Avoid instructions obviously unsafe for remat.</div><div> if (MI.isNotDuplicable() || MI.mayStore() || MI.hasUnmodeledSideEffects())</div><div> return false;</div><div><br></div><div> // Don't remat inline asm. We have no idea how expensive it is</div><div> // even if it's side effect free.</div><div> if (MI.isInlineAsm())</div><div> return false;</div><div>}</div></div><div><br></div><div>We have following doubts and require guidance and suggestion to move ahead:</div><div>1. Is the approach we are following feasible?</div><div>2. What will be the suitable method to store the sequence of instruction for recomputing value which will be used during transformation.</div><div>3. Suggestion for deciding termination condition for checking use-def chain as it should be terminated when remat will be costly that spill. </div><div>4. What other cases or instruction could be included in isReMaterializablePossible() function. Some suggestions for direction to look in.</div><div><br></div><div id="DWT4155">Any other suggestions will also be helpful for us to move in right direction.</div></div></blockquote>I think sounds feasible. Regarding the second question, I'm not sure what you're asking.<br><br>Regarding isReMaterializablePossible(), if you're allowing instructions that read from memory, and many of the use cases fall into that category, you'll need to consider whether you'd be moving any potential load past some aliasing store. We have code that does these kinds of checks in the instruction scheduler (in ScheduleDAGInstrs). The instruction scheduler builds a graph structure detailing these dependencies; maybe we should just keep it around for RA?<br><br>One of the issues we obviously need to deal with is the relative cost of rematerialization vs. the spill/restore. We have code that does this kind of comparison today in the MachineCombiner, and I think that the same kind of comparison is needed here.<br><br> -Hal<br><blockquote style="border-left: 2px solid rgb(16, 16, 255); margin-left: 5px; padding-left: 5px; color: rgb(0, 0, 0); font-weight: normal; font-style: normal; text-decoration: none; font-family: Helvetica,Arial,sans-serif; font-size: 12pt;"><div dir="ltr"><div></div><div><br></div><div>- Nirav</div></div>
</blockquote><br><br><br>-- <br><div><span name="x"></span>Hal Finkel<br>Lead, Compiler Technology and Programming Languages<br>Leadership Computing Facility<br>Argonne National Laboratory<span name="x"></span><br></div></div></body></html>