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

Arseniy Obolenskiy via llvm-commits llvm-commits at lists.llvm.org
Thu May 7 08:07:09 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();
----------------
aobolensk wrote:

Slot.reset() only destroys the MachineFunction, the test (to be bore precise the part that is below this line) exposes it because std::optional in-place buffer makes the second emplace() reuse the same address, so the stale pointer aliases the new MF. As for without the fix, the second finalizeLowering early-returns on the ProcessedMF lookup, freezeReservedRegs never runs, and EXPECT_TRUE(reservedRegsFrozen()) fails

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


More information about the llvm-commits mailing list