Skip to content

Commit 970b041

Browse files
committed
Use smart pointer for context to get automatic de-allocation.
Signed-off-by: Julian Oppermann <[email protected]>
1 parent 84adf59 commit 970b041

File tree

3 files changed

+9
-16
lines changed

3 files changed

+9
-16
lines changed

sycl-jit/jit-compiler/lib/KernelFusion.cpp

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -244,27 +244,21 @@ extern "C" JITResult compileSYCL(InMemoryFile SourceFile,
244244
return errorToFusionResult(ModuleOrErr.takeError(),
245245
"Device compilation failed");
246246
}
247-
std::unique_ptr<llvm::Module> Module = std::move(*ModuleOrErr);
248247

249-
auto FreeModuleAndContext = [&Module]() {
250-
auto *LLVMCtx = &Module->getContext();
251-
Module.reset();
252-
delete LLVMCtx;
253-
};
248+
std::unique_ptr<llvm::LLVMContext> Context;
249+
std::unique_ptr<llvm::Module> Module = std::move(*ModuleOrErr);
250+
Context.reset(&Module->getContext());
254251

255-
if (auto Error = linkDefaultDeviceLibraries(Module.get(), UserArgs)) {
256-
FreeModuleAndContext();
252+
if (auto Error = linkDefaultDeviceLibraries(*Module, UserArgs)) {
257253
return errorToFusionResult(std::move(Error), "Device linking failed");
258254
}
259255

260256
SYCLKernelInfo Kernel;
261257
if (auto Error = translation::KernelTranslator::translateKernel(
262258
Kernel, *Module, JITContext::getInstance(), BinaryFormat::SPIRV)) {
263-
FreeModuleAndContext();
264259
return errorToFusionResult(std::move(Error), "SPIR-V translation failed");
265260
}
266261

267-
FreeModuleAndContext();
268262
return JITResult{Kernel};
269263
}
270264

sycl-jit/jit-compiler/lib/rtc/DeviceCompilation.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ jit_compiler::compileDeviceCode(InMemoryFile SourceFile,
150150
return createStringError("Unable to obtain LLVM module");
151151
}
152152

153-
Error jit_compiler::linkDefaultDeviceLibraries(llvm::Module *Module,
153+
Error jit_compiler::linkDefaultDeviceLibraries(llvm::Module &Module,
154154
View<const char *> UserArgs) {
155155
// This function mimics the device library selection process
156156
// `clang::driver::tools::SYCL::getDeviceLibraries`, assuming a SPIR-V target
@@ -195,22 +195,21 @@ Error jit_compiler::linkDefaultDeviceLibraries(llvm::Module *Module,
195195
"libsycl-itt-user-wrappers", "libsycl-itt-compiler-wrappers",
196196
"libsycl-itt-stubs"};
197197

198-
LLVMContext &Ctx = Module->getContext();
198+
LLVMContext &Context = Module.getContext();
199199
auto Link = [&](ArrayRef<llvm::StringLiteral> LibNames) -> Error {
200200
for (const auto &LibName : LibNames) {
201201
std::string LibPath = (DPCPPRoot + "/lib/" + LibName + ".bc").str();
202202

203203
SMDiagnostic Diag;
204-
std::unique_ptr<llvm::Module> Lib = parseIRFile(LibPath, Diag, Ctx);
204+
std::unique_ptr<llvm::Module> Lib = parseIRFile(LibPath, Diag, Context);
205205
if (!Lib) {
206206
std::string DiagMsg;
207207
raw_string_ostream SOS(DiagMsg);
208208
Diag.print(/*ProgName=*/nullptr, SOS);
209209
return createStringError(DiagMsg);
210210
}
211211

212-
if (Linker::linkModules(*Module, std::move(Lib),
213-
Linker::LinkOnlyNeeded)) {
212+
if (Linker::linkModules(Module, std::move(Lib), Linker::LinkOnlyNeeded)) {
214213
// TODO: `linkModules` always prints errors to the console.
215214
return createStringError("Unable to link device library: %s",
216215
LibPath.c_str());

sycl-jit/jit-compiler/lib/rtc/DeviceCompilation.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ llvm::Expected<std::unique_ptr<llvm::Module>>
2323
compileDeviceCode(InMemoryFile SourceFile, View<InMemoryFile> IncludeFiles,
2424
View<const char *> UserArgs);
2525

26-
llvm::Error linkDefaultDeviceLibraries(llvm::Module *Module,
26+
llvm::Error linkDefaultDeviceLibraries(llvm::Module &Module,
2727
View<const char *> UserArgs);
2828

2929
} // namespace jit_compiler

0 commit comments

Comments
 (0)