Skip to content

Commit 2af6ccd

Browse files
[SYCL] Fix memory leak in online compiler (#4963)
The experimental online compiler may leak memory in compileToSPIRV. These changes address this leak by storing the SPIR-V binary information directly in the vector that will later be returned. Signed-off-by: Steffen Larsen <[email protected]>
1 parent f074774 commit 2af6ccd

File tree

1 file changed

+5
-7
lines changed

1 file changed

+5
-7
lines changed

sycl/source/detail/online_compiler/online_compiler.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -160,16 +160,14 @@ compileToSPIRV(const std::string &Source, sycl::info::device_type DeviceType,
160160
&SourceName, 0, nullptr, nullptr, nullptr, &NumOutputs,
161161
&Outputs, &OutputLengths, &OutputNames);
162162

163-
byte *SpirV = nullptr;
163+
std::vector<byte> SpirV;
164164
std::string CompileLog;
165-
size_t SpirVSize = 0;
166165
for (uint32_t I = 0; I < NumOutputs; I++) {
167166
size_t NameLen = strlen(OutputNames[I]);
168167
if (NameLen >= 4 && strstr(OutputNames[I], ".spv") != nullptr &&
169168
Outputs[I] != nullptr) {
170-
SpirVSize = OutputLengths[I];
171-
SpirV = new byte[SpirVSize];
172-
std::memcpy(SpirV, Outputs[I], SpirVSize);
169+
assert(SpirV.size() == 0 && "More than one SPIR-V output found.");
170+
SpirV = std::vector<byte>(Outputs[I], Outputs[I] + OutputLengths[I]);
173171
} else if (!strcmp(OutputNames[I], "stdout.log")) {
174172
CompileLog = std::string(reinterpret_cast<const char *>(Outputs[I]));
175173
}
@@ -184,13 +182,13 @@ compileToSPIRV(const std::string &Source, sycl::info::device_type DeviceType,
184182
if (CompileError)
185183
throw online_compile_error("ocloc reported compilation errors: {\n" +
186184
CompileLog + "\n}");
187-
if (!SpirV)
185+
if (SpirV.empty())
188186
throw online_compile_error(
189187
"Unexpected output: ocloc did not return SPIR-V");
190188
if (MemFreeError)
191189
throw online_compile_error("ocloc cannot safely free resources");
192190

193-
return std::vector<byte>(SpirV, SpirV + SpirVSize);
191+
return SpirV;
194192
}
195193
} // namespace detail
196194

0 commit comments

Comments
 (0)