Hi Chris, Gordon,<div><br></div><div>Here's a patch to allow a GCStrategy to customize the places where it wants to insert safe points. I'm not sure who maintains the GC code today in LLVM (I'd be happy to take ownership, if needed).</div>
<div><br></div><div>The patch just adds up a custom safepoints flag, similar to the way the GCStrategy can customize intrinsics lowering, or roots initialization. It works pretty well, as I've tested it on VMKit. So the patch in itself should not be controversial.</div>
<div><br></div><div>Chris, I'd like to discuss with you on how we can improve the current GC framework to take advantage of metadata. The reason I add this custom safe point creation method is because it's difficult for a front-end to pass custom information to the GC framework. And I think metadata could help us in this situation. Ideally, I'd like to get to a point where we can write:</div>
<div><br></div><div>15 = load %MyPointer* %2 !safepoint</div><div><br></div><div>And the GCStrategy would find this metadata information in the MachineInstruction (because the safe points are created based on MachineInstruction). This solution looks very much like the !nontemporal metadata.</div>
<div><br></div><div>Unfortunately, it looks like metadata are not passed down to MachineInstruction. How could we achieve that? Should we write custom code in the Instruction -> MachineInstruction transformation to pass down that !safepoint metadata?</div>
<div><br></div><div>Thanks for any input!</div><div>Nicolas</div><div><br></div><div><br></div><div><div>Index: include/llvm/CodeGen/GCStrategy.h</div><div>===================================================================</div>
<div>--- include/llvm/CodeGen/GCStrategy.h<span class="Apple-tab-span" style="white-space:pre"> </span>(revision 141409)</div><div>+++ include/llvm/CodeGen/GCStrategy.h<span class="Apple-tab-span" style="white-space:pre"> </span>(working copy)</div>
<div>@@ -37,6 +37,7 @@</div><div> #define LLVM_CODEGEN_GCSTRATEGY_H</div><div> </div><div> #include "llvm/CodeGen/GCMetadata.h"</div><div>+#include "llvm/CodeGen/MachineFunction.h"</div><div> #include "llvm/Support/Registry.h"</div>
<div> #include <string></div><div> </div><div>@@ -68,6 +69,8 @@</div><div> bool CustomReadBarriers; //< Default is to insert loads.</div><div> bool CustomWriteBarriers; //< Default is to insert stores.</div>
<div> bool CustomRoots; //< Default is to pass through to backend.</div><div>+ bool CustomSafePoints; //< Default is to use NeededSafePoints</div><div>+ // to find safe points.</div>
<div> bool InitRoots; //< If set, roots are nulled during lowering.</div><div> bool UsesMetadata; //< If set, backend must emit metadata tables.</div><div> </div><div>@@ -87,7 +90,9 @@</div>
<div> </div><div> /// needsSafePoitns - True if safe points of any kind are required. By</div><div> // default, none are recorded.</div><div>- bool needsSafePoints() const { return NeededSafePoints != 0; }</div>
<div>+ bool needsSafePoints() const {</div><div>+ return CustomSafePoints || NeededSafePoints != 0;</div><div>+ }</div><div> </div><div> /// needsSafePoint(Kind) - True if the given kind of safe point is</div>
<div> // required. By default, none are recorded.</div><div>@@ -109,6 +114,11 @@</div><div> /// can generate a stack map. If true, then</div><div> // performCustomLowering must delete them.</div>
<div> bool customRoots() const { return CustomRoots; }</div><div>+</div><div>+ /// customSafePoints - By default, the GC analysis will find safe</div><div>+ /// points according to NeededSafePoints. If true,</div>
<div>+ /// then findCustomSafePoints must create them.</div><div>+ bool customSafePoints() const { return CustomSafePoints; }</div><div> </div><div> /// initializeRoots - If set, gcroot intrinsics should initialize their</div>
<div> // allocas to null before the first use. This is</div><div>@@ -135,6 +145,7 @@</div><div> /// which the LLVM IR can be modified.</div><div> virtual bool initializeCustomLowering(Module &F);</div>
<div> virtual bool performCustomLowering(Function &F);</div><div>+ virtual bool findCustomSafePoints(GCFunctionInfo& FI, MachineFunction& MF);</div><div> };</div><div> </div><div> }</div><div>Index: lib/CodeGen/GCStrategy.cpp</div>
<div>===================================================================</div><div>--- lib/CodeGen/GCStrategy.cpp<span class="Apple-tab-span" style="white-space:pre"> </span>(revision 141409)</div><div>+++ lib/CodeGen/GCStrategy.cpp<span class="Apple-tab-span" style="white-space:pre"> </span>(working copy)</div>
<div>@@ -97,6 +97,7 @@</div><div> CustomReadBarriers(false),</div><div> CustomWriteBarriers(false),</div><div> CustomRoots(false),</div><div>+ CustomSafePoints(false),</div><div> InitRoots(true),</div><div> UsesMetadata(false)</div>
<div> {}</div><div>@@ -116,6 +117,14 @@</div><div> return 0;</div><div> }</div><div> </div><div>+</div><div>+bool GCStrategy::findCustomSafePoints(GCFunctionInfo& FI, MachineFunction &F) {</div><div>+ dbgs() << "gc " << getName() << " must override findCustomSafePoints.\n";</div>
<div>+ llvm_unreachable(0);</div><div>+ return 0;</div><div>+}</div><div>+</div><div>+</div><div> GCFunctionInfo *GCStrategy::insertFunctionInfo(const Function &F) {</div><div> GCFunctionInfo *FI = new GCFunctionInfo(F, *this);</div>
<div> Functions.push_back(FI);</div><div>@@ -370,6 +379,7 @@</div><div> MCSymbol* Label = InsertLabel(*CI->getParent(), RAI, CI->getDebugLoc());</div><div> FI->addSafePoint(GC::PostCall, Label, CI->getDebugLoc());</div>
<div> }</div><div>+</div><div> }</div><div> </div><div> void MachineCodeAnalysis::FindSafePoints(MachineFunction &MF) {</div><div>@@ -405,9 +415,13 @@</div><div> </div><div> // Find the size of the stack frame.</div>
<div> FI->setFrameSize(MF.getFrameInfo()->getStackSize());</div><div>- </div><div>+ </div><div> // Find all safe points.</div><div>- FindSafePoints(MF);</div><div>+ if (FI->getStrategy().customSafePoints()) {</div>
<div>+ FI->getStrategy().findCustomSafePoints(*FI, MF);</div><div>+ } else {</div><div>+ FindSafePoints(MF);</div><div>+ }</div><div> </div><div> // Find the stack offsets for all roots.</div><div> FindStackOffsets(MF);</div>
</div>