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

Carl Ritson via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 17 23:49:45 PDT 2023


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

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.

>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] [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);
   }
 };



More information about the llvm-commits mailing list