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

Dmitry Sidorov via llvm-commits llvm-commits at lists.llvm.org
Thu May 7 05:57:43 PDT 2026


================
@@ -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();
----------------
MrSidims wrote:

So previously, we had a storage of `MachineFunction` - Slot.reset was not resetting it, right? How it was exposed by this test?

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


More information about the llvm-commits mailing list