[PATCH] D131790: [unittests/CodeGen] Remove unique_ptr from the result of createTargetMachine

Guozhi Wei via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 12 10:09:58 PDT 2022


Carrot created this revision.
Herald added a project: All.
Carrot requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

The object contained in unique_ptr will be automatically deleted at the end of the current scope. In createMachineFunction,

  auto TM = createTargetMachine();

creates a TM contained in unique_ptr, a reference of the TM is stored in a MachineFunction object, but at the end of the function, the TM is deleted, so later access to the TM(and contained STI, TRI ...) through MachineFunction object is invalid.

So we should not use unique_ptr<BogusTargetMachine> in functions createMachineFunction and createTargetMachine.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D131790

Files:
  llvm/unittests/CodeGen/MFCommon.inc
  llvm/unittests/CodeGen/MachineOperandTest.cpp


Index: llvm/unittests/CodeGen/MachineOperandTest.cpp
===================================================================
--- llvm/unittests/CodeGen/MachineOperandTest.cpp
+++ llvm/unittests/CodeGen/MachineOperandTest.cpp
@@ -7,6 +7,11 @@
 //===----------------------------------------------------------------------===//
 
 #include "llvm/CodeGen/MachineOperand.h"
+#include "llvm/CodeGen/MachineFunction.h"
+#include "llvm/CodeGen/MachineModuleInfo.h"
+#include "llvm/CodeGen/TargetFrameLowering.h"
+#include "llvm/CodeGen/TargetInstrInfo.h"
+#include "llvm/CodeGen/TargetLowering.h"
 #include "llvm/ADT/ilist_node.h"
 #include "llvm/IR/Constants.h"
 #include "llvm/IR/InstrTypes.h"
@@ -15,14 +20,19 @@
 #include "llvm/IR/ModuleSlotTracker.h"
 #include "llvm/MC/MCAsmInfo.h"
 #include "llvm/MC/MCContext.h"
+#include "llvm/MC/TargetRegistry.h"
 #include "llvm/Support/LowLevelTypeImpl.h"
 #include "llvm/Support/raw_ostream.h"
+#include "llvm/Target/TargetMachine.h"
 #include "gtest/gtest.h"
 
 using namespace llvm;
 
 namespace {
 
+// Include helper functions to ease the manipulation of MachineFunctions.
+#include "MFCommon.inc"
+
 TEST(MachineOperandTest, ChangeToTargetIndexTest) {
   // Creating a MachineOperand to change it to TargetIndex
   MachineOperand MO = MachineOperand::CreateImm(50);
@@ -46,13 +56,17 @@
 }
 
 TEST(MachineOperandTest, PrintRegisterMask) {
-  uint32_t Dummy;
-  MachineOperand MO = MachineOperand::CreateRegMask(&Dummy);
+  LLVMContext Ctx;
+  Module Mod("Module", Ctx);
+  auto MF = createMachineFunction(Ctx, Mod);
+
+  uint32_t *Dummy = MF->allocateRegMask();
+  MachineOperand MO = MachineOperand::CreateRegMask(Dummy);
 
   // Checking some preconditions on the newly created
   // MachineOperand.
   ASSERT_TRUE(MO.isRegMask());
-  ASSERT_TRUE(MO.getRegMask() == &Dummy);
+  ASSERT_TRUE(MO.getRegMask() == Dummy);
 
   // Print a MachineOperand containing a RegMask. Here we check that without a
   // TRI and IntrinsicInfo we still print a less detailed regmask.
Index: llvm/unittests/CodeGen/MFCommon.inc
===================================================================
--- llvm/unittests/CodeGen/MFCommon.inc
+++ llvm/unittests/CodeGen/MFCommon.inc
@@ -116,8 +116,9 @@
   BogusSubtarget ST;
 };
 
-std::unique_ptr<BogusTargetMachine> createTargetMachine() {
-  return std::make_unique<BogusTargetMachine>();
+BogusTargetMachine *createTargetMachine() {
+  static BogusTargetMachine BogusTM;
+  return &BogusTM;
 }
 
 std::unique_ptr<MachineFunction> createMachineFunction(LLVMContext &Ctx,
@@ -127,7 +128,7 @@
 
   auto TM = createTargetMachine();
   unsigned FunctionNum = 42;
-  MachineModuleInfo MMI(TM.get());
+  MachineModuleInfo MMI(TM);
   const TargetSubtargetInfo &STI = *TM->getSubtargetImpl(*F);
 
   return std::make_unique<MachineFunction>(*F, *TM, STI, FunctionNum, MMI);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D131790.452211.patch
Type: text/x-patch
Size: 2834 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220812/5cc79b9c/attachment.bin>


More information about the llvm-commits mailing list