Skip to content

[mlir][ROCDL] Fix file leak in SeralizeToHsaco and its newer form #67711

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 1 commit into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
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
10 changes: 5 additions & 5 deletions mlir/lib/Dialect/GPU/Transforms/SerializeToHsaco.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
//===----------------------------------------------------------------------===//

#include "mlir/Dialect/GPU/Transforms/Passes.h"
#include "mlir/Dialect/LLVMIR/ROCDLDialect.h"
#include "mlir/IR/Location.h"
#include "mlir/IR/MLIRContext.h"

Expand Down Expand Up @@ -43,6 +42,7 @@
#include "llvm/MC/TargetRegistry.h"

#include "llvm/Support/CommandLine.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/FileUtilities.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/Program.h"
Expand Down Expand Up @@ -402,9 +402,8 @@ SerializeToHsacoPass::createHsaco(const SmallVectorImpl<char> &isaBinary) {
tempIsaBinaryOs.close();

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

// Load the HSA code object.
auto hsacoFile = openInputFile(tempHsacoFilename);
auto hsacoFile =
llvm::MemoryBuffer::getFile(tempHsacoFilename, /*IsText=*/false);
if (!hsacoFile) {
emitError(loc, "read HSA code object from temp file error");
return {};
}

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

Expand Down
8 changes: 4 additions & 4 deletions mlir/lib/Target/LLVM/ROCDL/Target.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -377,9 +377,8 @@ AMDGPUSerializer::compileToBinary(const std::string &serializedISA) {
}

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

// Load the HSA code object.
auto hsacoFile = openInputFile(tempHsacoFilename);
auto hsacoFile =
llvm::MemoryBuffer::getFile(tempHsacoFilename, /*IsText=*/false);
if (!hsacoFile) {
getOperation().emitError()
<< "Failed to read the HSA code object from the temp file.";
return std::nullopt;
}

StringRef buffer = hsacoFile->getBuffer();
StringRef buffer = (*hsacoFile)->getBuffer();

return SmallVector<char, 0>(buffer.begin(), buffer.end());
}
Expand Down