Skip to content

[SYCL] Fix memory leak in online compiler #4963

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

Merged
merged 2 commits into from
Nov 20, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions sycl/source/detail/online_compiler/online_compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,16 +160,14 @@ compileToSPIRV(const std::string &Source, sycl::info::device_type DeviceType,
&SourceName, 0, nullptr, nullptr, nullptr, &NumOutputs,
&Outputs, &OutputLengths, &OutputNames);

byte *SpirV = nullptr;
std::vector<byte> SpirV;
std::string CompileLog;
size_t SpirVSize = 0;
for (uint32_t I = 0; I < NumOutputs; I++) {
size_t NameLen = strlen(OutputNames[I]);
if (NameLen >= 4 && strstr(OutputNames[I], ".spv") != nullptr &&
Outputs[I] != nullptr) {
SpirVSize = OutputLengths[I];
SpirV = new byte[SpirVSize];
std::memcpy(SpirV, Outputs[I], SpirVSize);
Comment on lines -170 to -172
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@v-klochkov - Can there only be at most one .spv in OutputNames?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@v-klochkov, ping, can you answer the question above?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changes look good to me.
Regarding the number of output spirv files. It is assumed to be 1 at most, but I did not find any strict wordings about that in ocloc API https://github.com/intel/compute-runtime/blob/master/shared/offline_compiler/source/ocloc_api.h

It may be a good idea to add an assert here checking that there is only 1 spv output.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea! I have added such an assertion.

assert(SpirV.size() == 0 && "More than one SPIR-V output found.");
SpirV = std::vector<byte>(Outputs[I], Outputs[I] + OutputLengths[I]);
} else if (!strcmp(OutputNames[I], "stdout.log")) {
CompileLog = std::string(reinterpret_cast<const char *>(Outputs[I]));
}
Expand All @@ -184,13 +182,13 @@ compileToSPIRV(const std::string &Source, sycl::info::device_type DeviceType,
if (CompileError)
throw online_compile_error("ocloc reported compilation errors: {\n" +
CompileLog + "\n}");
if (!SpirV)
if (SpirV.empty())
throw online_compile_error(
"Unexpected output: ocloc did not return SPIR-V");
if (MemFreeError)
throw online_compile_error("ocloc cannot safely free resources");

return std::vector<byte>(SpirV, SpirV + SpirVSize);
return SpirV;
}
} // namespace detail

Expand Down