Skip to content

Commit 77a7208

Browse files
committed
[SYCL] Fix memory leak in online compiler
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 3205368 commit 77a7208

File tree

1 file changed

+4
-7
lines changed

1 file changed

+4
-7
lines changed

sycl/source/detail/online_compiler/online_compiler.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -160,16 +160,13 @@ 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+
SpirV = std::vector<byte>(Outputs[I], Outputs[I] + OutputLengths[I]);
173170
} else if (!strcmp(OutputNames[I], "stdout.log")) {
174171
CompileLog = std::string(reinterpret_cast<const char *>(Outputs[I]));
175172
}
@@ -184,13 +181,13 @@ compileToSPIRV(const std::string &Source, sycl::info::device_type DeviceType,
184181
if (CompileError)
185182
throw online_compile_error("ocloc reported compilation errors: {\n" +
186183
CompileLog + "\n}");
187-
if (!SpirV)
184+
if (SpirV.empty())
188185
throw online_compile_error(
189186
"Unexpected output: ocloc did not return SPIR-V");
190187
if (MemFreeError)
191188
throw online_compile_error("ocloc cannot safely free resources");
192189

193-
return std::vector<byte>(SpirV, SpirV + SpirVSize);
190+
return SpirV;
194191
}
195192
} // namespace detail
196193

0 commit comments

Comments
 (0)