[llvm] [SandboxVec][DAG] MemDGNode for memory-dependency candidate nodes (PR #109684)

via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 1 15:39:43 PDT 2024


================
@@ -29,35 +29,61 @@
 
 namespace llvm::sandboxir {
 
+class DependencyGraph;
+class MemDGNode;
+
+/// SubclassIDs for isa/dyn_cast etc.
+enum class DGNodeID {
+  DGNode,
+  MemDGNode,
+};
+
 /// A DependencyGraph Node that points to an Instruction and contains memory
 /// dependency edges.
 class DGNode {
+protected:
   Instruction *I;
+  // TODO: Use a PointerIntPair for SubclassID and I.
+  /// For isa/dyn_cast etc.
+  DGNodeID SubclassID;
   /// Memory predecessors.
-  DenseSet<DGNode *> MemPreds;
-  /// This is true if this may read/write memory, or if it has some ordering
-  /// constraints, like with stacksave/stackrestore and alloca/inalloca.
-  bool IsMem;
+  DenseSet<MemDGNode *> MemPreds;
+
+  DGNode(Instruction *I, DGNodeID ID) : I(I), SubclassID(ID) {}
+  friend class MemDGNode; // For constructor.
 
 public:
-  DGNode(Instruction *I) : I(I) {
-    IsMem = I->isMemDepCandidate() ||
-            (isa<AllocaInst>(I) && cast<AllocaInst>(I)->isUsedWithInAlloca()) ||
-            I->isStackSaveOrRestoreIntrinsic();
+  DGNode(Instruction *I) : I(I), SubclassID(DGNodeID::DGNode) {
+    assert(!isMemDepCandidate(I) && "Expected Non-Mem instruction, ");
+  }
+  DGNode(const DGNode &Other) = delete;
+  virtual ~DGNode() = default;
+  /// \Returns true if this is before \p Other in program order.
+  bool comesBefore(const DGNode *Other) { return I->comesBefore(Other->I); }
+  /// \Returns true if \p I is a memory dependency candidate instruction.
+  static bool isMemDepCandidate(Instruction *I) {
+    AllocaInst *Alloca;
+    return I->isMemDepCandidate() ||
+           ((Alloca = dyn_cast<AllocaInst>(I)) &&
+            Alloca->isUsedWithInAlloca()) ||
+           I->isStackSaveOrRestoreIntrinsic();
----------------
vporpo wrote:

The reason why `isMemDepCandidate()` and `isStackSaveOrRestoreIntrinsic()` are in `sandboxir::Instruction` is that they need to access LLVM IR, but the combined check does not need to. Also I don't think this check will be used in other places.

https://github.com/llvm/llvm-project/pull/109684


More information about the llvm-commits mailing list