[llvm] d88ddad - [SPIR-V] Fix dangling MachineFunction pointers in finalizeLowering (#196050)

via llvm-commits llvm-commits at lists.llvm.org
Fri May 8 08:24:22 PDT 2026


Author: Arseniy Obolenskiy
Date: 2026-05-08T17:24:17+02:00
New Revision: d88ddad05c0d91fa0a66ca81b7a19af186ae9992

URL: https://github.com/llvm/llvm-project/commit/d88ddad05c0d91fa0a66ca81b7a19af186ae9992
DIFF: https://github.com/llvm/llvm-project/commit/d88ddad05c0d91fa0a66ca81b7a19af186ae9992.diff

LOG: [SPIR-V] Fix dangling MachineFunction pointers in finalizeLowering (#196050)

Added: 
    

Modified: 
    llvm/lib/Target/SPIRV/SPIRVISelLowering.cpp
    llvm/lib/Target/SPIRV/SPIRVISelLowering.h
    llvm/unittests/Target/SPIRV/SPIRVGlobalRegistryTests.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Target/SPIRV/SPIRVISelLowering.cpp b/llvm/lib/Target/SPIRV/SPIRVISelLowering.cpp
index 493192b068658..eb16c9a314a23 100644
--- a/llvm/lib/Target/SPIRV/SPIRVISelLowering.cpp
+++ b/llvm/lib/Target/SPIRV/SPIRVISelLowering.cpp
@@ -429,7 +429,7 @@ void validateAccessChain(const SPIRVSubtarget &STI, MachineRegisterInfo *MRI,
 void SPIRVTargetLowering::finalizeLowering(MachineFunction &MF) const {
   // finalizeLowering() is called twice (see GlobalISel/InstructionSelect.cpp)
   // We'd like to avoid the needless second processing pass.
-  if (ProcessedMF.find(&MF) != ProcessedMF.end())
+  if (MF.getRegInfo().reservedRegsFrozen())
     return;
 
   MachineRegisterInfo *MRI = &MF.getRegInfo();
@@ -604,7 +604,6 @@ void SPIRVTargetLowering::finalizeLowering(MachineFunction &MF) const {
       }
     }
   }
-  ProcessedMF.insert(&MF);
   TargetLowering::finalizeLowering(MF);
 }
 

diff  --git a/llvm/lib/Target/SPIRV/SPIRVISelLowering.h b/llvm/lib/Target/SPIRV/SPIRVISelLowering.h
index 3561745c1b9e0..6af0bbcc9818d 100644
--- a/llvm/lib/Target/SPIRV/SPIRVISelLowering.h
+++ b/llvm/lib/Target/SPIRV/SPIRVISelLowering.h
@@ -16,7 +16,6 @@
 
 #include "SPIRVGlobalRegistry.h"
 #include "llvm/CodeGen/TargetLowering.h"
-#include <set>
 
 namespace llvm {
 class SPIRVSubtarget;
@@ -24,9 +23,6 @@ class SPIRVSubtarget;
 class SPIRVTargetLowering : public TargetLowering {
   const SPIRVSubtarget &STI;
 
-  // Record of already processed machine functions
-  mutable std::set<const MachineFunction *> ProcessedMF;
-
 public:
   explicit SPIRVTargetLowering(const TargetMachine &TM,
                                const SPIRVSubtarget &ST);

diff  --git a/llvm/unittests/Target/SPIRV/SPIRVGlobalRegistryTests.cpp b/llvm/unittests/Target/SPIRV/SPIRVGlobalRegistryTests.cpp
index c7d4072cbf32e..618edab01f91b 100644
--- a/llvm/unittests/Target/SPIRV/SPIRVGlobalRegistryTests.cpp
+++ b/llvm/unittests/Target/SPIRV/SPIRVGlobalRegistryTests.cpp
@@ -101,3 +101,25 @@ TEST_F(SPIRVGlobalRegistryTest, PrepareFunctionsClearsStalePointers) {
 
   EXPECT_EQ(GR->findDeducedElementType(F), nullptr);
 }
+
+TEST_F(SPIRVGlobalRegistryTest, FinalizeLoweringFreezesAcrossAliasedMFs) {
+  auto *STM = static_cast<SPIRVTargetMachine *>(TM.get());
+  Function *F = Mod->getFunction("f");
+  ASSERT_NE(F, nullptr);
+  const auto *Sub = STM->getSubtargetImpl(*F);
+  const SPIRVTargetLowering *TLI = Sub->getTargetLowering();
+
+  MF.reset();
+  std::optional<MachineFunction> Slot;
+
+  Slot.emplace(*F, *TM, *Sub, MMI->getContext(), 0);
+  Slot->push_back(Slot->CreateMachineBasicBlock());
+  TLI->finalizeLowering(*Slot);
+  ASSERT_TRUE(Slot->getRegInfo().reservedRegsFrozen());
+
+  Slot.reset();
+  Slot.emplace(*F, *TM, *Sub, MMI->getContext(), 0);
+  Slot->push_back(Slot->CreateMachineBasicBlock());
+  TLI->finalizeLowering(*Slot);
+  EXPECT_TRUE(Slot->getRegInfo().reservedRegsFrozen());
+}


        


More information about the llvm-commits mailing list