Skip to content

Commit a47b28c

Browse files
[mlir][UB] Add ub.unreachable operation
1 parent 0c6d7a4 commit a47b28c

File tree

6 files changed

+88
-8
lines changed

6 files changed

+88
-8
lines changed

mlir/include/mlir/Dialect/UB/IR/UBOps.td

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,24 @@ def PoisonOp : UB_Op<"poison", [ConstantLike, Pure]> {
6666
let hasFolder = 1;
6767
}
6868

69+
//===----------------------------------------------------------------------===//
70+
// UnreachableOp
71+
//===----------------------------------------------------------------------===//
72+
73+
def UnreachableOp : UB_Op<"unreachable", [Terminator]> {
74+
let summary = "Unreachable operation.";
75+
let description = [{
76+
The `unreachable` operation has no defined semantics. This operation
77+
indicates that its enclosing basic block is not reachable.
78+
79+
Example:
80+
81+
```
82+
ub.unreachable
83+
```
84+
}];
85+
86+
let assemblyFormat = "attr-dict";
87+
}
88+
6989
#endif // MLIR_DIALECT_UB_IR_UBOPS_TD

mlir/lib/Conversion/UBToLLVM/UBToLLVM.cpp

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,20 @@ namespace mlir {
2323

2424
using namespace mlir;
2525

26-
namespace {
26+
//===----------------------------------------------------------------------===//
27+
// PoisonOpLowering
28+
//===----------------------------------------------------------------------===//
2729

30+
namespace {
2831
struct PoisonOpLowering : public ConvertOpToLLVMPattern<ub::PoisonOp> {
2932
using ConvertOpToLLVMPattern::ConvertOpToLLVMPattern;
3033

3134
LogicalResult
3235
matchAndRewrite(ub::PoisonOp op, OpAdaptor adaptor,
3336
ConversionPatternRewriter &rewriter) const override;
3437
};
35-
3638
} // namespace
3739

38-
//===----------------------------------------------------------------------===//
39-
// PoisonOpLowering
40-
//===----------------------------------------------------------------------===//
41-
4240
LogicalResult
4341
PoisonOpLowering::matchAndRewrite(ub::PoisonOp op, OpAdaptor adaptor,
4442
ConversionPatternRewriter &rewriter) const {
@@ -60,6 +58,29 @@ PoisonOpLowering::matchAndRewrite(ub::PoisonOp op, OpAdaptor adaptor,
6058
return success();
6159
}
6260

61+
//===----------------------------------------------------------------------===//
62+
// UnreachableOpLowering
63+
//===----------------------------------------------------------------------===//
64+
65+
namespace {
66+
struct UnreachableOpLowering
67+
: public ConvertOpToLLVMPattern<ub::UnreachableOp> {
68+
using ConvertOpToLLVMPattern::ConvertOpToLLVMPattern;
69+
70+
LogicalResult
71+
matchAndRewrite(ub::UnreachableOp op, OpAdaptor adaptor,
72+
ConversionPatternRewriter &rewriter) const override;
73+
};
74+
} // namespace
75+
LogicalResult
76+
77+
UnreachableOpLowering::matchAndRewrite(
78+
ub::UnreachableOp op, OpAdaptor adaptor,
79+
ConversionPatternRewriter &rewriter) const {
80+
rewriter.replaceOpWithNewOp<LLVM::UnreachableOp>(op);
81+
return success();
82+
}
83+
6384
//===----------------------------------------------------------------------===//
6485
// Pass Definition
6586
//===----------------------------------------------------------------------===//
@@ -93,7 +114,7 @@ struct UBToLLVMConversionPass
93114

94115
void mlir::ub::populateUBToLLVMConversionPatterns(
95116
const LLVMTypeConverter &converter, RewritePatternSet &patterns) {
96-
patterns.add<PoisonOpLowering>(converter);
117+
patterns.add<PoisonOpLowering, UnreachableOpLowering>(converter);
97118
}
98119

99120
//===----------------------------------------------------------------------===//

mlir/lib/Conversion/UBToSPIRV/UBToSPIRV.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,17 @@ struct PoisonOpLowering final : OpConversionPattern<ub::PoisonOp> {
4040
}
4141
};
4242

43+
struct UnreachableOpLowering final : OpConversionPattern<ub::UnreachableOp> {
44+
using Base::Base;
45+
46+
LogicalResult
47+
matchAndRewrite(ub::UnreachableOp op, OpAdaptor,
48+
ConversionPatternRewriter &rewriter) const override {
49+
rewriter.replaceOpWithNewOp<spirv::UnreachableOp>(op);
50+
return success();
51+
}
52+
};
53+
4354
} // namespace
4455

4556
//===----------------------------------------------------------------------===//
@@ -75,5 +86,6 @@ struct UBToSPIRVConversionPass final
7586

7687
void mlir::ub::populateUBToSPIRVConversionPatterns(
7788
const SPIRVTypeConverter &converter, RewritePatternSet &patterns) {
78-
patterns.add<PoisonOpLowering>(converter, patterns.getContext());
89+
patterns.add<PoisonOpLowering, UnreachableOpLowering>(converter,
90+
patterns.getContext());
7991
}

mlir/test/Conversion/UBToLLVM/ub-to-llvm.mlir

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,9 @@ func.func @check_poison() {
1717
%3 = ub.poison : !llvm.ptr
1818
return
1919
}
20+
21+
// CHECK-LABEL: @check_unrechable
22+
func.func @check_unrechable() {
23+
// CHECK: llvm.unreachable
24+
ub.unreachable
25+
}

mlir/test/Conversion/UBToSPIRV/ub-to-spirv.mlir

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,18 @@ func.func @check_poison() {
1919
}
2020

2121
}
22+
23+
// -----
24+
25+
// No successful test because the dialect conversion framework does not convert
26+
// unreachable blocks.
27+
28+
module attributes {
29+
spirv.target_env = #spirv.target_env<
30+
#spirv.vce<v1.0, [Int8, Int16, Int64, Float16, Float64, Shader], []>, #spirv.resource_limits<>>
31+
} {
32+
func.func @check_unrechable() {
33+
// expected-error@+1{{cannot be used in reachable block}}
34+
spirv.Unreachable
35+
}
36+
}

mlir/test/Dialect/UB/ops.mlir

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,9 @@ func.func @poison_tensor() -> tensor<8x?xf64> {
3838
%0 = ub.poison : tensor<8x?xf64>
3939
return %0 : tensor<8x?xf64>
4040
}
41+
42+
// CHECK-LABEL: func @unreachable()
43+
// CHECK: ub.unreachable
44+
func.func @unreachable() {
45+
ub.unreachable
46+
}

0 commit comments

Comments
 (0)