-
Notifications
You must be signed in to change notification settings - Fork 14.6k
[SPIR-V] Ensure that Module resource is managed locally wrt. a unit test case and fix a memory leak #123725
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[SPIR-V] Ensure that Module resource is managed locally wrt. a unit test case and fix a memory leak #123725
Conversation
@llvm/pr-subscribers-backend-spir-v Author: Vyacheslav Levytskyy (VyacheslavLevytskyy) ChangesAdding SPIRV to LLVM_ALL_TARGETS (#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:
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:
|
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/88/builds/7043 Here is the relevant piece of the build log for the reference
|
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/73/builds/12085 Here is the relevant piece of the build log for the reference
|
Adding SPIRV to LLVM_ALL_TARGETS (#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.