Skip to content

Commit 05fa923

Browse files
authored
Fix SmallVector usage in SerailzeToHsaco (#71702)
Enable merging #71439 by removing a definitely-wrong usage of std::unique_ptr<SmallVectorImpl<char>> as a return value with passing in a SmallVectorImpl<char>& Also change the following function to take ArrayRef<char> instead of const SmalVectorImpl<char>& .
1 parent 955dd88 commit 05fa923

File tree

1 file changed

+14
-19
lines changed

1 file changed

+14
-19
lines changed

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

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,9 @@ class SerializeToHsacoPass
9595
std::unique_ptr<std::vector<char>>
9696
serializeISA(const std::string &isa) override;
9797

98-
std::unique_ptr<SmallVectorImpl<char>> assembleIsa(const std::string &isa);
99-
std::unique_ptr<std::vector<char>>
100-
createHsaco(const SmallVectorImpl<char> &isaBinary);
98+
LogicalResult assembleIsa(const std::string &isa,
99+
SmallVectorImpl<char> &result);
100+
std::unique_ptr<std::vector<char>> createHsaco(ArrayRef<char> isaBinary);
101101

102102
std::string getRocmPath();
103103
};
@@ -318,21 +318,18 @@ SerializeToHsacoPass::translateToLLVMIR(llvm::LLVMContext &llvmContext) {
318318
return ret;
319319
}
320320

321-
std::unique_ptr<SmallVectorImpl<char>>
322-
SerializeToHsacoPass::assembleIsa(const std::string &isa) {
321+
LogicalResult SerializeToHsacoPass::assembleIsa(const std::string &isa,
322+
SmallVectorImpl<char> &result) {
323323
auto loc = getOperation().getLoc();
324324

325-
SmallVector<char, 0> result;
326325
llvm::raw_svector_ostream os(result);
327326

328327
llvm::Triple triple(llvm::Triple::normalize(this->triple));
329328
std::string error;
330329
const llvm::Target *target =
331330
llvm::TargetRegistry::lookupTarget(triple.normalize(), error);
332-
if (!target) {
333-
emitError(loc, Twine("failed to lookup target: ") + error);
334-
return {};
335-
}
331+
if (!target)
332+
return emitError(loc, Twine("failed to lookup target: ") + error);
336333

337334
llvm::SourceMgr srcMgr;
338335
srcMgr.AddNewSourceBuffer(llvm::MemoryBuffer::getMemBuffer(isa), SMLoc());
@@ -373,19 +370,17 @@ SerializeToHsacoPass::assembleIsa(const std::string &isa) {
373370
std::unique_ptr<llvm::MCTargetAsmParser> tap(
374371
target->createMCAsmParser(*sti, *parser, *mcii, mcOptions));
375372

376-
if (!tap) {
377-
emitError(loc, "assembler initialization error");
378-
return {};
379-
}
373+
if (!tap)
374+
return emitError(loc, "assembler initialization error");
380375

381376
parser->setTargetParser(*tap);
382377
parser->Run(false);
383378

384-
return std::make_unique<SmallVector<char, 0>>(std::move(result));
379+
return success();
385380
}
386381

387382
std::unique_ptr<std::vector<char>>
388-
SerializeToHsacoPass::createHsaco(const SmallVectorImpl<char> &isaBinary) {
383+
SerializeToHsacoPass::createHsaco(ArrayRef<char> isaBinary) {
389384
auto loc = getOperation().getLoc();
390385

391386
// Save the ISA binary to a temp file.
@@ -435,10 +430,10 @@ SerializeToHsacoPass::createHsaco(const SmallVectorImpl<char> &isaBinary) {
435430

436431
std::unique_ptr<std::vector<char>>
437432
SerializeToHsacoPass::serializeISA(const std::string &isa) {
438-
auto isaBinary = assembleIsa(isa);
439-
if (!isaBinary)
433+
SmallVector<char, 0> isaBinary;
434+
if (failed(assembleIsa(isa, isaBinary)))
440435
return {};
441-
return createHsaco(*isaBinary);
436+
return createHsaco(isaBinary);
442437
}
443438

444439
// Register pass to serialize GPU kernel functions to a HSACO binary annotation.

0 commit comments

Comments
 (0)