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

Jay Foad via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 18 05:18:50 PDT 2023


================
@@ -1101,22 +1101,31 @@ class SlotIndexUpdateDelegate : public MachineFunction::Delegate {
 private:
   MachineFunction &MF;
   SlotIndexes *Indexes;
+  SmallSetVector<MachineInstr *, 2> Insertions;
 
 public:
   SlotIndexUpdateDelegate(MachineFunction &MF, SlotIndexes *Indexes)
       : MF(MF), Indexes(Indexes) {
     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);
----------------
jayfoad wrote:

Nit: testing `if (Indexes)` here instead of in the destructor might be infinitesimally more efficient?

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


More information about the llvm-commits mailing list