[llvm] [MachineBasicBlock] Fix SlotIndexUpdater for insertion order (PR #69424)

Carl Ritson via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 18 20:23:44 PDT 2023


https://github.com/perlfu updated https://github.com/llvm/llvm-project/pull/69424

>From e6162cf3ef2fa66d9f67deb1f2246597c4c0224a Mon Sep 17 00:00:00 2001
From: Carl Ritson <carl.ritson at amd.com>
Date: Wed, 18 Oct 2023 15:37:26 +0900
Subject: [PATCH 1/2] [MachineBasicBlock] Fix SlotIndexUpdater for insertion
 order

Follow up fix for #68786 to address that MachineFunction
handleInsertion is actually called before a new instruction has
been inserted into the block. Hence new instructions must be
recorded and SlotIndex updates performed after the delegate call.
---
 llvm/lib/CodeGen/MachineBasicBlock.cpp | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)

diff --git a/llvm/lib/CodeGen/MachineBasicBlock.cpp b/llvm/lib/CodeGen/MachineBasicBlock.cpp
index 14d9bb292ddf2e8..f0b404c8553b930 100644
--- a/llvm/lib/CodeGen/MachineBasicBlock.cpp
+++ b/llvm/lib/CodeGen/MachineBasicBlock.cpp
@@ -1101,6 +1101,7 @@ class SlotIndexUpdateDelegate : public MachineFunction::Delegate {
 private:
   MachineFunction &MF;
   SlotIndexes *Indexes;
+  SmallSetVector<MachineInstr *, 2> Insertions;
 
 public:
   SlotIndexUpdateDelegate(MachineFunction &MF, SlotIndexes *Indexes)
@@ -1108,15 +1109,23 @@ class SlotIndexUpdateDelegate : public MachineFunction::Delegate {
     MF.setDelegate(this);
   }
 
-  ~SlotIndexUpdateDelegate() { MF.resetDelegate(this); }
+  ~SlotIndexUpdateDelegate() {
+    MF.resetDelegate(this);
+    if (Indexes) {
+      for (auto MI : Insertions)
+        Indexes->insertMachineInstrInMaps(*MI);
+    }
+  }
 
   void MF_HandleInsertion(MachineInstr &MI) override {
-    if (Indexes)
-      Indexes->insertMachineInstrInMaps(MI);
+    // This is called before MI is inserted into block so defer index update.
+    Insertions.insert(&MI);
   }
 
   void MF_HandleRemoval(MachineInstr &MI) override {
-    if (Indexes)
+    if (Insertions.count(&MI))
+      Insertions.remove(&MI);
+    else if (Indexes)
       Indexes->removeMachineInstrFromMaps(MI);
   }
 };

>From fce8962b06edd6131b34b5bde1fb652f30854c92 Mon Sep 17 00:00:00 2001
From: Carl Ritson <carl.ritson at amd.com>
Date: Thu, 19 Oct 2023 12:22:42 +0900
Subject: [PATCH 2/2] Address reviewer feedback.

---
 llvm/lib/CodeGen/MachineBasicBlock.cpp | 13 +++++--------
 1 file changed, 5 insertions(+), 8 deletions(-)

diff --git a/llvm/lib/CodeGen/MachineBasicBlock.cpp b/llvm/lib/CodeGen/MachineBasicBlock.cpp
index f0b404c8553b930..5f9e4a66c0d22ed 100644
--- a/llvm/lib/CodeGen/MachineBasicBlock.cpp
+++ b/llvm/lib/CodeGen/MachineBasicBlock.cpp
@@ -1111,21 +1111,18 @@ class SlotIndexUpdateDelegate : public MachineFunction::Delegate {
 
   ~SlotIndexUpdateDelegate() {
     MF.resetDelegate(this);
-    if (Indexes) {
-      for (auto MI : Insertions)
-        Indexes->insertMachineInstrInMaps(*MI);
-    }
+    for (auto MI : Insertions)
+      Indexes->insertMachineInstrInMaps(*MI);
   }
 
   void MF_HandleInsertion(MachineInstr &MI) override {
     // This is called before MI is inserted into block so defer index update.
-    Insertions.insert(&MI);
+    if (Indexes)
+      Insertions.insert(&MI);
   }
 
   void MF_HandleRemoval(MachineInstr &MI) override {
-    if (Insertions.count(&MI))
-      Insertions.remove(&MI);
-    else if (Indexes)
+    if (Indexes && !Insertions.remove(&MI))
       Indexes->removeMachineInstrFromMaps(MI);
   }
 };



More information about the llvm-commits mailing list