Skip to content

Commit 02ffa41

Browse files
committed
Support cleanup region in the parser/printer
1 parent 6cd47ee commit 02ffa41

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

clang/include/clang/CIR/Dialect/IR/CIROps.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3017,6 +3017,8 @@ def CIR_CallOp : CIR_CallOpBase<"call", [NoRegionArguments]> {
30173017
let arguments = commonArgs;
30183018
let regions = (region AnyRegion:$cleanup);
30193019

3020+
let skipDefaultBuilders = 1;
3021+
30203022
let builders = [
30213023
OpBuilder<(ins "mlir::SymbolRefAttr":$callee, "mlir::Type":$resType,
30223024
"mlir::ValueRange":$operands), [{

clang/lib/CIR/Dialect/IR/CIRDialect.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -847,6 +847,16 @@ static mlir::ParseResult parseCallCommon(mlir::OpAsmParser &parser,
847847
if (parser.resolveOperands(ops, opsFnTy.getInputs(), opsLoc, result.operands))
848848
return mlir::failure();
849849

850+
// If exception is present and there are cleanups, this should be latest thing
851+
// present (after all attributes, etc).
852+
if (!hasDestinationBlocks) {
853+
mlir::Region *cleanupRegion = result.addRegion();
854+
if (parser.parseOptionalKeyword("cleanup").succeeded()) {
855+
if (parser.parseRegion(*cleanupRegion))
856+
return failure();
857+
}
858+
}
859+
850860
return mlir::success();
851861
}
852862

@@ -899,6 +909,14 @@ static void printCallCommon(mlir::Operation *op,
899909
printer << " : ";
900910
printer.printFunctionalType(op->getOperands().getTypes(),
901911
op->getResultTypes());
912+
913+
// If exception is present and there are cleanups, this should be latest thing
914+
// present (after all attributes, etc).
915+
auto call = dyn_cast<cir::CallOp>(op);
916+
if (call && !call.getCleanup().empty()) {
917+
printer << " cleanup ";
918+
printer.printRegion(call.getCleanup());
919+
}
902920
}
903921

904922
mlir::ParseResult cir::CallOp::parse(mlir::OpAsmParser &parser,

clang/test/CIR/IR/try-catch.cir

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// RUN: cir-opt %s --verify-roundtrip | FileCheck %s
22

33
!u8i = !cir.int<u, 8>
4+
!s32i = !cir.int<s, 32>
45

56
module {
67

@@ -103,4 +104,37 @@ cir.func @empty_try_block_with_catch_unwind_contains_resume() {
103104
// CHECK: cir.return
104105
// CHECK: }
105106

107+
108+
cir.func private @function_with_cleanup() -> !s32i
109+
110+
// CHECK: cir.func private @function_with_cleanup() -> !s32i
111+
112+
cir.func @try_catch_with_call_that_has_cleanup() {
113+
cir.scope {
114+
cir.try {
115+
cir.call @function_with_cleanup() : () -> !s32i cleanup {
116+
cir.yield
117+
}
118+
cir.yield
119+
} unwind {
120+
cir.resume
121+
}
122+
}
123+
cir.return
124+
}
125+
126+
// CHECK: cir.func @try_catch_with_call_that_has_cleanup() {
127+
// CHECK: cir.scope {
128+
// CHECK: cir.try {
129+
// CHECK: cir.call @function_with_cleanup() : () -> !s32i cleanup {
130+
// CHECK: cir.yield
131+
// CHECK: }
132+
// CHECK: cir.yield
133+
// CHECK: } unwind {
134+
// CHECK: cir.resume
135+
// CHECK: }
136+
// CHECK: }
137+
// CHECK: cir.return
138+
// CHECK: }
139+
106140
}

0 commit comments

Comments
 (0)