[llvm] 337f0ed - [RISCV] Fix memory leak in RISCVInstrInfoTest.cpp unittest

Craig Topper via llvm-commits llvm-commits at lists.llvm.org
Thu Nov 16 11:48:33 PST 2023


Author: Craig Topper
Date: 2023-11-16T11:48:27-08:00
New Revision: 337f0ededc2201bf1c367dfe6c96177772571105

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

LOG: [RISCV] Fix memory leak in RISCVInstrInfoTest.cpp unittest

We need to make sure the RISCVTargetMachine is also deleted.

Added: 
    

Modified: 
    llvm/unittests/Target/RISCV/RISCVInstrInfoTest.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/unittests/Target/RISCV/RISCVInstrInfoTest.cpp b/llvm/unittests/Target/RISCV/RISCVInstrInfoTest.cpp
index 1e9a259a6b3f110..fd5f17e0185e4ab 100644
--- a/llvm/unittests/Target/RISCV/RISCVInstrInfoTest.cpp
+++ b/llvm/unittests/Target/RISCV/RISCVInstrInfoTest.cpp
@@ -12,6 +12,7 @@
 #include "llvm/CodeGen/MachineModuleInfo.h"
 #include "llvm/MC/TargetRegistry.h"
 #include "llvm/Support/TargetSelect.h"
+#include "llvm/Target/TargetLoweringObjectFile.h"
 #include "llvm/Target/TargetMachine.h"
 #include "llvm/Target/TargetOptions.h"
 
@@ -25,6 +26,7 @@ namespace {
 
 class RISCVInstrInfoTest : public testing::TestWithParam<const char *> {
 protected:
+  std::unique_ptr<RISCVTargetMachine> TM;
   std::unique_ptr<LLVMContext> Ctx;
   std::unique_ptr<RISCVSubtarget> ST;
   std::unique_ptr<MachineModuleInfo> MMI;
@@ -42,16 +44,16 @@ class RISCVInstrInfoTest : public testing::TestWithParam<const char *> {
     const Target *TheTarget = TargetRegistry::lookupTarget(TT, Error);
     TargetOptions Options;
 
-    RISCVTargetMachine *TM = static_cast<RISCVTargetMachine *>(
-        TheTarget->createTargetMachine(TT, "generic", "", Options, std::nullopt,
-                                       std::nullopt, CodeGenOptLevel::Default));
+    TM.reset(static_cast<RISCVTargetMachine *>(TheTarget->createTargetMachine(
+        TT, "generic", "", Options, std::nullopt, std::nullopt,
+        CodeGenOptLevel::Default)));
 
     Ctx = std::make_unique<LLVMContext>();
     Module M("Module", *Ctx);
     M.setDataLayout(TM->createDataLayout());
     auto *FType = FunctionType::get(Type::getVoidTy(*Ctx), false);
     auto *F = Function::Create(FType, GlobalValue::ExternalLinkage, "Test", &M);
-    MMI = std::make_unique<MachineModuleInfo>(TM);
+    MMI = std::make_unique<MachineModuleInfo>(TM.get());
 
     ST = std::make_unique<RISCVSubtarget>(
         TM->getTargetTriple(), TM->getTargetCPU(), TM->getTargetCPU(),


        


More information about the llvm-commits mailing list