[llvm] [SPIR-V] Ensure that Module resource is managed locally wrt. a unit test case (PR #123725)

via llvm-commits llvm-commits at lists.llvm.org
Tue Jan 21 02:51:09 PST 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-backend-spir-v

Author: Vyacheslav Levytskyy (VyacheslavLevytskyy)

<details>
<summary>Changes</summary>

Adding SPIRV to LLVM_ALL_TARGETS (https://github.com/llvm/llvm-project/pull/119653) revealed a series of minor compilation problems and sanitizer complaints. This PR is to move unit tests resources (a Module ptr) from the class-scope to a local scope of the class member function to be sure that before the test env is teared down the ptr is released.


---
Full diff: https://github.com/llvm/llvm-project/pull/123725.diff


2 Files Affected:

- (modified) llvm/lib/Target/SPIRV/SPIRVAPI.cpp (+5-6) 
- (modified) llvm/unittests/Target/SPIRV/SPIRVAPITest.cpp (+3-4) 


``````````diff
diff --git a/llvm/lib/Target/SPIRV/SPIRVAPI.cpp b/llvm/lib/Target/SPIRV/SPIRVAPI.cpp
index a1ee4aada853bc..4c806fd7c98882 100644
--- a/llvm/lib/Target/SPIRV/SPIRVAPI.cpp
+++ b/llvm/lib/Target/SPIRV/SPIRVAPI.cpp
@@ -134,9 +134,8 @@ SPIRVTranslateModule(Module *M, std::string &SpirvObj, std::string &ErrMsg,
   TargetOptions Options;
   std::optional<Reloc::Model> RM;
   std::optional<CodeModel::Model> CM;
-  std::unique_ptr<TargetMachine> Target =
-      std::unique_ptr<TargetMachine>(TheTarget->createTargetMachine(
-          TargetTriple.getTriple(), "", "", Options, RM, CM, OLevel));
+  std::unique_ptr<TargetMachine> Target(TheTarget->createTargetMachine(
+      TargetTriple.getTriple(), "", "", Options, RM, CM, OLevel));
   if (!Target) {
     ErrMsg = "Could not allocate target machine!";
     return false;
@@ -158,10 +157,10 @@ SPIRVTranslateModule(Module *M, std::string &SpirvObj, std::string &ErrMsg,
   TargetLibraryInfoImpl TLII(Triple(M->getTargetTriple()));
   legacy::PassManager PM;
   PM.add(new TargetLibraryInfoWrapperPass(TLII));
-  MachineModuleInfoWrapperPass *MMIWP =
-      new MachineModuleInfoWrapperPass(Target.get());
+  std::unique_ptr<MachineModuleInfoWrapperPass> MMIWP(
+      new MachineModuleInfoWrapperPass(Target.get()));
   const_cast<TargetLoweringObjectFile *>(Target->getObjFileLowering())
-      ->Initialize(MMIWP->getMMI().getContext(), *Target);
+      ->Initialize(MMIWP.get()->getMMI().getContext(), *Target);
 
   SmallString<4096> OutBuffer;
   raw_svector_ostream OutStream(OutBuffer);
diff --git a/llvm/unittests/Target/SPIRV/SPIRVAPITest.cpp b/llvm/unittests/Target/SPIRV/SPIRVAPITest.cpp
index 27ea8b8cf06e8d..149db48c190a09 100644
--- a/llvm/unittests/Target/SPIRV/SPIRVAPITest.cpp
+++ b/llvm/unittests/Target/SPIRV/SPIRVAPITest.cpp
@@ -36,7 +36,9 @@ class SPIRVAPITest : public testing::Test {
                const std::vector<std::string> &AllowExtNames,
                const std::vector<std::string> &Opts) {
     SMDiagnostic ParseError;
-    M = parseAssemblyString(Assembly, ParseError, Context);
+    LLVMContext Context;
+    std::unique_ptr<Module> M =
+        parseAssemblyString(Assembly, ParseError, Context);
     if (!M) {
       ParseError.print("IR parsing failed: ", errs());
       report_fatal_error("Can't parse input assembly.");
@@ -48,9 +50,6 @@ class SPIRVAPITest : public testing::Test {
     return Status;
   }
 
-  LLVMContext Context;
-  std::unique_ptr<Module> M;
-
   static constexpr StringRef ExtensionAssembly = R"(
     define dso_local spir_func void @test1() {
     entry:

``````````

</details>


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


More information about the llvm-commits mailing list