@@ -276,22 +276,41 @@ void parseVisibilityAttr(OpAsmParser &parser, cir::VisibilityAttr &visibility) {
276
276
// CIR Custom Parsers/Printers
277
277
// ===----------------------------------------------------------------------===//
278
278
279
- static mlir::ParseResult parseOmittedTerminatorRegion (mlir::OpAsmParser &parser,
280
- mlir::Region ®ion) {
279
+ static mlir::ParseResult
280
+ parseOmittedTerminatorRegion (mlir::OpAsmParser &parser,
281
+ mlir::Region &scopeRegion,
282
+ mlir::Region &cleanupRegion) {
281
283
auto regionLoc = parser.getCurrentLocation ();
282
- if (parser.parseRegion (region ))
284
+ if (parser.parseRegion (scopeRegion ))
283
285
return failure ();
284
- if (ensureRegionTerm (parser, region , regionLoc).failed ())
286
+ if (ensureRegionTerm (parser, scopeRegion , regionLoc).failed ())
285
287
return failure ();
288
+
289
+ // Parse optional cleanup region.
290
+ if (parser.parseOptionalKeyword (" cleanup" ).succeeded ()) {
291
+ if (parser.parseRegion (cleanupRegion, /* arguments=*/ {}, /* argTypes=*/ {}))
292
+ return failure ();
293
+ if (ensureRegionTerm (parser, cleanupRegion, regionLoc).failed ())
294
+ return failure ();
295
+ }
296
+
286
297
return success ();
287
298
}
288
299
289
300
static void printOmittedTerminatorRegion (mlir::OpAsmPrinter &printer,
290
301
cir::ScopeOp &op,
291
- mlir::Region ®ion) {
292
- printer.printRegion (region,
302
+ mlir::Region &scopeRegion,
303
+ mlir::Region &cleanupRegion) {
304
+ printer.printRegion (scopeRegion,
293
305
/* printEntryBlockArgs=*/ false ,
294
- /* printBlockTerminators=*/ !omitRegionTerm (region));
306
+ /* printBlockTerminators=*/ !omitRegionTerm (scopeRegion));
307
+ if (!op.getCleanupRegion ().empty ()) {
308
+ printer << " cleanup " ;
309
+ printer.printRegion (
310
+ cleanupRegion,
311
+ /* printEntryBlockArgs=*/ false ,
312
+ /* printBlockTerminators=*/ !omitRegionTerm (cleanupRegion));
313
+ }
295
314
}
296
315
297
316
// ===----------------------------------------------------------------------===//
@@ -1251,6 +1270,7 @@ void cir::ScopeOp::getSuccessorRegions(
1251
1270
1252
1271
// If the condition isn't constant, both regions may be executed.
1253
1272
regions.push_back (RegionSuccessor (&getScopeRegion ()));
1273
+ regions.push_back (RegionSuccessor (&getCleanupRegion ()));
1254
1274
}
1255
1275
1256
1276
void cir::ScopeOp::build (
@@ -1261,6 +1281,8 @@ void cir::ScopeOp::build(
1261
1281
OpBuilder::InsertionGuard guard (builder);
1262
1282
Region *scopeRegion = result.addRegion ();
1263
1283
builder.createBlock (scopeRegion);
1284
+ // cleanup region, do not eagarly create blocks, do it upon demand.
1285
+ (void )result.addRegion ();
1264
1286
1265
1287
mlir::Type yieldTy;
1266
1288
scopeBuilder (builder, yieldTy, result.location );
@@ -1276,17 +1298,22 @@ void cir::ScopeOp::build(
1276
1298
OpBuilder::InsertionGuard guard (builder);
1277
1299
Region *scopeRegion = result.addRegion ();
1278
1300
builder.createBlock (scopeRegion);
1301
+ // cleanup region, do not eagarly create blocks, do it upon demand.
1302
+ (void )result.addRegion ();
1279
1303
scopeBuilder (builder, result.location );
1280
1304
}
1281
1305
1282
1306
LogicalResult cir::ScopeOp::verify () {
1283
- if (getRegion ().empty ()) {
1307
+ if (getScopeRegion ().empty ()) {
1284
1308
return emitOpError () << " cir.scope must not be empty since it should "
1285
1309
" include at least an implicit cir.yield " ;
1286
1310
}
1287
1311
1288
- if (getRegion ().back ().empty () ||
1289
- !getRegion ().back ().getTerminator ()->hasTrait <OpTrait::IsTerminator>())
1312
+ if (getScopeRegion ().back ().empty () ||
1313
+ !getScopeRegion ()
1314
+ .back ()
1315
+ .getTerminator ()
1316
+ ->hasTrait <OpTrait::IsTerminator>())
1290
1317
return emitOpError () << " last block of cir.scope must be terminated" ;
1291
1318
return success ();
1292
1319
}
0 commit comments