Skip to content

Commit 36d8f98

Browse files
committed
Address unrecheable block concerns
1 parent 38215a6 commit 36d8f98

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed

mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2302,19 +2302,31 @@ static LogicalResult verifyComdat(Operation *op,
23022302
static LogicalResult verifyBlockTags(LLVMFuncOp funcOp) {
23032303
llvm::DenseSet<BlockTagAttr> blockTags;
23042304
BlockTagOp badBlockTagOp;
2305+
enum { DupTag, UnrecheableBlock } errorMsgType;
23052306
if (funcOp
23062307
.walk([&](BlockTagOp blockTagOp) {
2308+
mlir::Block *block = blockTagOp->getBlock();
2309+
if (!block->isEntryBlock() && block->use_empty()) {
2310+
badBlockTagOp = blockTagOp;
2311+
errorMsgType = UnrecheableBlock;
2312+
return WalkResult::interrupt();
2313+
}
2314+
23072315
if (blockTags.contains(blockTagOp.getTag())) {
23082316
badBlockTagOp = blockTagOp;
2317+
errorMsgType = DupTag;
23092318
return WalkResult::interrupt();
23102319
}
23112320
blockTags.insert(blockTagOp.getTag());
23122321
return WalkResult::advance();
23132322
})
23142323
.wasInterrupted()) {
2315-
badBlockTagOp.emitError()
2316-
<< "duplicate block tag '" << badBlockTagOp.getTag().getId()
2317-
<< "' in the same function: ";
2324+
if (errorMsgType == DupTag)
2325+
badBlockTagOp.emitError()
2326+
<< "duplicate block tag '" << badBlockTagOp.getTag().getId()
2327+
<< "' in the same function: ";
2328+
else
2329+
badBlockTagOp.emitError() << "not allowed in unrecheable blocks";
23182330
return failure();
23192331
}
23202332

mlir/test/Dialect/LLVMIR/blockaddress-canonicalize.mlir

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: mlir-opt %s -pass-pipeline='builtin.module(llvm.func(canonicalize{region-simplify=aggressive}))' -split-input-file | FileCheck %s
1+
// RUN: mlir-opt %s -pass-pipeline='builtin.module(llvm.func(canonicalize{region-simplify=aggressive}))' -verify-diagnostics -split-input-file | FileCheck %s
22

33
llvm.mlir.global private @x() {addr_space = 0 : i32, dso_local} : !llvm.ptr {
44
%0 = llvm.blockaddress <function = @ba, tag = <id = 2>> : !llvm.ptr
@@ -46,3 +46,27 @@ llvm.func @fn(%cond : i1, %arg0 : i32, %arg1 : i32) -> i32 {
4646
llvm.blocktag <id = 1>
4747
llvm.return %arg1 : i32
4848
}
49+
50+
// -----
51+
52+
llvm.mlir.global private @g() {addr_space = 0 : i32, dso_local} : !llvm.ptr {
53+
%0 = llvm.blockaddress <function = @fn, tag = <id = 1>> : !llvm.ptr
54+
llvm.return %0 : !llvm.ptr
55+
}
56+
57+
// The Canonicalizer's region simplify pass can be hazardous when dealing
58+
// with indirect branches, as there is currently no mechanism to convey
59+
// dialect-specific block constraints.
60+
61+
llvm.func @fn(%dest : !llvm.ptr, %arg0 : i32, %arg1 : i32) -> i32 {
62+
llvm.indirectbr %dest : !llvm.ptr, [
63+
^head
64+
]
65+
^head:
66+
llvm.blocktag <id = 0>
67+
llvm.return %arg0 : i32
68+
^tail:
69+
// expected-error@+1 {{not allowed in unrecheable blocks}}
70+
llvm.blocktag <id = 1>
71+
llvm.return %arg1 : i32
72+
}

0 commit comments

Comments
 (0)