[unittests/CodeGen] Remove unique_ptr from the result of createTargetMachine
authorGuozhi Wei <carrot@google.com>
Tue, 16 Aug 2022 22:06:50 +0000 (22:06 +0000)
committerGuozhi Wei <carrot@google.com>
Tue, 16 Aug 2022 22:06:50 +0000 (22:06 +0000)
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.

Differential Revision: https://reviews.llvm.org/D131790

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

index 32e1d20..3c83268 100644 (file)
@@ -116,8 +116,9 @@ private:
   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 @@ std::unique_ptr<MachineFunction> createMachineFunction(LLVMContext &Ctx,
 
   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);
index 9044b6e..165e168 100644 (file)
@@ -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"
 #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, ChangeToTargetIndexTest) {
 }
 
 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.