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

Vyacheslav Levytskyy via llvm-commits llvm-commits at lists.llvm.org
Tue Jan 21 02:23:37 PST 2025


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

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.


>From 3d81ed8f15899baf9413c472995d14f1b8be40c5 Mon Sep 17 00:00:00 2001
From: "Levytskyy, Vyacheslav" <vyacheslav.levytskyy at intel.com>
Date: Tue, 21 Jan 2025 02:20:13 -0800
Subject: [PATCH] Ensure that Module resource is managed locally wrt. a unit
 test case

---
 llvm/unittests/Target/SPIRV/SPIRVAPITest.cpp | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

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:



More information about the llvm-commits mailing list