Skip to content

[MLIR][LLVM] Fix inlining of a single block ending with unreachable #122646

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
Jan 13, 2025
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: 10 additions & 0 deletions mlir/include/mlir/Transforms/InliningUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,13 @@ class DialectInlinerInterface
/// is invoked before inlined terminator operations have been processed.
virtual void processInlinedCallBlocks(
Operation *call, iterator_range<Region::iterator> inlinedBlocks) const {}

/// Returns true if the inliner can assume a fast path of not creating a new
/// block, if there is only one block.
virtual bool allowSingleBlockOptimization(
iterator_range<Region::iterator> inlinedBlocks) const {
return true;
}
};

/// This interface provides the hooks into the inlining interface.
Expand Down Expand Up @@ -223,6 +230,9 @@ class InlinerInterface

virtual void processInlinedCallBlocks(
Operation *call, iterator_range<Region::iterator> inlinedBlocks) const;

virtual bool allowSingleBlockOptimization(
iterator_range<Region::iterator> inlinedBlocks) const;
};

//===----------------------------------------------------------------------===//
Expand Down
8 changes: 8 additions & 0 deletions mlir/lib/Dialect/LLVMIR/Transforms/InlinerInterfaceImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -743,6 +743,14 @@ struct LLVMInlinerInterface : public DialectInlinerInterface {
op->erase();
}

bool allowSingleBlockOptimization(
iterator_range<Region::iterator> inlinedBlocks) const final {
if (!inlinedBlocks.empty() &&
isa<LLVM::UnreachableOp>(inlinedBlocks.begin()->getTerminator()))
return false;
return true;
}

/// Handle the given inlined return by replacing the uses of the call with the
/// operands of the return. This overload is called when the inlined region
/// only contains one block.
Expand Down
16 changes: 15 additions & 1 deletion mlir/lib/Transforms/Utils/InliningUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,18 @@ void InlinerInterface::handleTerminator(Operation *op,
handler->handleTerminator(op, valuesToRepl);
}

/// Returns true if the inliner can assume a fast path of not creating a
/// new block, if there is only one block.
bool InlinerInterface::allowSingleBlockOptimization(
iterator_range<Region::iterator> inlinedBlocks) const {
if (inlinedBlocks.empty()) {
return true;
}
auto *handler = getInterfaceFor(inlinedBlocks.begin()->getParentOp());
assert(handler && "expected valid dialect handler");
return handler->allowSingleBlockOptimization(inlinedBlocks);
}

Value InlinerInterface::handleArgument(OpBuilder &builder, Operation *call,
Operation *callable, Value argument,
DictionaryAttr argumentAttrs) const {
Expand Down Expand Up @@ -294,8 +306,10 @@ inlineRegionImpl(InlinerInterface &interface, Region *src, Block *inlineBlock,
interface.processInlinedCallBlocks(call, newBlocks);
interface.processInlinedBlocks(newBlocks);

bool singleBlockFastPath = interface.allowSingleBlockOptimization(newBlocks);

// Handle the case where only a single block was inlined.
if (std::next(newBlocks.begin()) == newBlocks.end()) {
if (singleBlockFastPath && std::next(newBlocks.begin()) == newBlocks.end()) {
// Run the result attribute handler on the terminator operands.
Operation *firstBlockTerminator = firstNewBlock->getTerminator();
builder.setInsertionPoint(firstBlockTerminator);
Expand Down
16 changes: 16 additions & 0 deletions mlir/test/Dialect/LLVMIR/inlining.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -676,3 +676,19 @@ llvm.func @caller(%x : i32) -> i32 {
%z = llvm.call @private_func(%x) : (i32) -> (i32)
llvm.return %z : i32
}

// -----

llvm.func @unreachable_func(%a : i32) -> i32 {
"llvm.intr.trap"() : () -> ()
llvm.unreachable
}

// CHECK-LABEL: func @caller
llvm.func @caller(%x : i32) -> i32 {
// CHECK-NOT: llvm.call @unreachable_func
// CHECK: llvm.intr.trap
// CHECK: llvm.unreachable
%z = llvm.call @unreachable_func(%x) : (i32) -> (i32)
llvm.return %z : i32
}
Loading