Skip to content

Commit 3ff9368

Browse files
[SPIR-V] Ensure that Module resource is managed locally wrt. a unit test case and fix a memory leak (#123725)
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.
1 parent 6dc356d commit 3ff9368

File tree

2 files changed

+8
-10
lines changed

2 files changed

+8
-10
lines changed

llvm/lib/Target/SPIRV/SPIRVAPI.cpp

+5-6
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,8 @@ SPIRVTranslateModule(Module *M, std::string &SpirvObj, std::string &ErrMsg,
134134
TargetOptions Options;
135135
std::optional<Reloc::Model> RM;
136136
std::optional<CodeModel::Model> CM;
137-
std::unique_ptr<TargetMachine> Target =
138-
std::unique_ptr<TargetMachine>(TheTarget->createTargetMachine(
139-
TargetTriple.getTriple(), "", "", Options, RM, CM, OLevel));
137+
std::unique_ptr<TargetMachine> Target(TheTarget->createTargetMachine(
138+
TargetTriple.getTriple(), "", "", Options, RM, CM, OLevel));
140139
if (!Target) {
141140
ErrMsg = "Could not allocate target machine!";
142141
return false;
@@ -158,10 +157,10 @@ SPIRVTranslateModule(Module *M, std::string &SpirvObj, std::string &ErrMsg,
158157
TargetLibraryInfoImpl TLII(Triple(M->getTargetTriple()));
159158
legacy::PassManager PM;
160159
PM.add(new TargetLibraryInfoWrapperPass(TLII));
161-
MachineModuleInfoWrapperPass *MMIWP =
162-
new MachineModuleInfoWrapperPass(Target.get());
160+
std::unique_ptr<MachineModuleInfoWrapperPass> MMIWP(
161+
new MachineModuleInfoWrapperPass(Target.get()));
163162
const_cast<TargetLoweringObjectFile *>(Target->getObjFileLowering())
164-
->Initialize(MMIWP->getMMI().getContext(), *Target);
163+
->Initialize(MMIWP.get()->getMMI().getContext(), *Target);
165164

166165
SmallString<4096> OutBuffer;
167166
raw_svector_ostream OutStream(OutBuffer);

llvm/unittests/Target/SPIRV/SPIRVAPITest.cpp

+3-4
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ class SPIRVAPITest : public testing::Test {
3636
const std::vector<std::string> &AllowExtNames,
3737
const std::vector<std::string> &Opts) {
3838
SMDiagnostic ParseError;
39-
M = parseAssemblyString(Assembly, ParseError, Context);
39+
LLVMContext Context;
40+
std::unique_ptr<Module> M =
41+
parseAssemblyString(Assembly, ParseError, Context);
4042
if (!M) {
4143
ParseError.print("IR parsing failed: ", errs());
4244
report_fatal_error("Can't parse input assembly.");
@@ -48,9 +50,6 @@ class SPIRVAPITest : public testing::Test {
4850
return Status;
4951
}
5052

51-
LLVMContext Context;
52-
std::unique_ptr<Module> M;
53-
5453
static constexpr StringRef ExtensionAssembly = R"(
5554
define dso_local spir_func void @test1() {
5655
entry:

0 commit comments

Comments
 (0)