Skip to content

Commit b4650af

Browse files
committed
Support cleanup region in the parser/printer
1 parent 1629f20 commit b4650af

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
@@ -2958,6 +2958,8 @@ def CIR_CallOp : CIR_CallOpBase<"call", [NoRegionArguments]> {
29582958
let arguments = commonArgs;
29592959
let regions = (region AnyRegion:$cleanup);
29602960

2961+
let skipDefaultBuilders = 1;
2962+
29612963
let builders = [
29622964
OpBuilder<(ins "mlir::SymbolRefAttr":$callee, "mlir::Type":$resType,
29632965
"mlir::ValueRange":$operands), [{

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

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

844+
// If exception is present and there are cleanups, this should be latest thing
845+
// present (after all attributes, etc).
846+
if (!hasDestinationBlocks) {
847+
mlir::Region *cleanupRegion = result.addRegion();
848+
if (parser.parseOptionalKeyword("cleanup").succeeded()) {
849+
if (parser.parseRegion(*cleanupRegion))
850+
return failure();
851+
}
852+
}
853+
844854
return mlir::success();
845855
}
846856

@@ -893,6 +903,14 @@ static void printCallCommon(mlir::Operation *op,
893903
printer << " : ";
894904
printer.printFunctionalType(op->getOperands().getTypes(),
895905
op->getResultTypes());
906+
907+
// If exception is present and there are cleanups, this should be latest thing
908+
// present (after all attributes, etc).
909+
auto call = dyn_cast<cir::CallOp>(op);
910+
if (call && !call.getCleanup().empty()) {
911+
printer << " cleanup ";
912+
printer.printRegion(call.getCleanup());
913+
}
896914
}
897915

898916
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)