Skip to content

Commit 0463e00

Browse files
authored
[mlir][ROCDL] Fix file leak in SeralizeToHsaco and its newer form (#67711)
SerializetToHsaco, as currently implemented, leaks the file descriptor of the .hsaco temporary file, which causes issues in long-running parallel compilation setups. See also ROCm/rocMLIR#1257
1 parent e83eb23 commit 0463e00

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

mlir/lib/Dialect/GPU/Transforms/SerializeToHsaco.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
//===----------------------------------------------------------------------===//
1313

1414
#include "mlir/Dialect/GPU/Transforms/Passes.h"
15-
#include "mlir/Dialect/LLVMIR/ROCDLDialect.h"
1615
#include "mlir/IR/Location.h"
1716
#include "mlir/IR/MLIRContext.h"
1817

@@ -43,6 +42,7 @@
4342
#include "llvm/MC/TargetRegistry.h"
4443

4544
#include "llvm/Support/CommandLine.h"
45+
#include "llvm/Support/FileSystem.h"
4646
#include "llvm/Support/FileUtilities.h"
4747
#include "llvm/Support/Path.h"
4848
#include "llvm/Support/Program.h"
@@ -402,9 +402,8 @@ SerializeToHsacoPass::createHsaco(const SmallVectorImpl<char> &isaBinary) {
402402
tempIsaBinaryOs.close();
403403

404404
// Create a temp file for HSA code object.
405-
int tempHsacoFD = -1;
406405
SmallString<128> tempHsacoFilename;
407-
if (llvm::sys::fs::createTemporaryFile("kernel", "hsaco", tempHsacoFD,
406+
if (llvm::sys::fs::createTemporaryFile("kernel", "hsaco",
408407
tempHsacoFilename)) {
409408
emitError(loc, "temporary file for HSA code object creation error");
410409
return {};
@@ -423,13 +422,14 @@ SerializeToHsacoPass::createHsaco(const SmallVectorImpl<char> &isaBinary) {
423422
}
424423

425424
// Load the HSA code object.
426-
auto hsacoFile = openInputFile(tempHsacoFilename);
425+
auto hsacoFile =
426+
llvm::MemoryBuffer::getFile(tempHsacoFilename, /*IsText=*/false);
427427
if (!hsacoFile) {
428428
emitError(loc, "read HSA code object from temp file error");
429429
return {};
430430
}
431431

432-
StringRef buffer = hsacoFile->getBuffer();
432+
StringRef buffer = (*hsacoFile)->getBuffer();
433433
return std::make_unique<std::vector<char>>(buffer.begin(), buffer.end());
434434
}
435435

mlir/lib/Target/LLVM/ROCDL/Target.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -377,9 +377,8 @@ AMDGPUSerializer::compileToBinary(const std::string &serializedISA) {
377377
}
378378

379379
// Create a temp file for HSA code object.
380-
int tempHsacoFD = -1;
381380
SmallString<128> tempHsacoFilename;
382-
if (llvm::sys::fs::createTemporaryFile("kernel", "hsaco", tempHsacoFD,
381+
if (llvm::sys::fs::createTemporaryFile("kernel", "hsaco",
383382
tempHsacoFilename)) {
384383
getOperation().emitError()
385384
<< "Failed to create a temporary file for the HSA code object.";
@@ -398,14 +397,15 @@ AMDGPUSerializer::compileToBinary(const std::string &serializedISA) {
398397
}
399398

400399
// Load the HSA code object.
401-
auto hsacoFile = openInputFile(tempHsacoFilename);
400+
auto hsacoFile =
401+
llvm::MemoryBuffer::getFile(tempHsacoFilename, /*IsText=*/false);
402402
if (!hsacoFile) {
403403
getOperation().emitError()
404404
<< "Failed to read the HSA code object from the temp file.";
405405
return std::nullopt;
406406
}
407407

408-
StringRef buffer = hsacoFile->getBuffer();
408+
StringRef buffer = (*hsacoFile)->getBuffer();
409409

410410
return SmallVector<char, 0>(buffer.begin(), buffer.end());
411411
}

0 commit comments

Comments
 (0)