Skip to content

Commit 5537ef8

Browse files
sitio-coutolanza
authored andcommitted
[CIR][IR] Refactor ScopeOp assembly format
This simplifies and modularizes the assembly format for ScopeOp by using the Tablegen assembly description and a new custom printer/parser that handles regions with omitted terminators. It also fixes an issue where the parser would not correctly handle `cir.scopes` with a return value. ghstack-source-id: c5b9be7 Pull Request resolved: #311
1 parent 1c2433c commit 5537ef8

File tree

3 files changed

+54
-32
lines changed

3 files changed

+54
-32
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -715,12 +715,14 @@ def ScopeOp : CIR_Op<"scope", [
715715
will be inserted implicitly.
716716
}];
717717

718-
let results = (outs Variadic<AnyType>:$results);
718+
let results = (outs Optional<AnyType>:$results);
719719
let regions = (region AnyRegion:$scopeRegion);
720720

721-
let hasCustomAssemblyFormat = 1;
722721
let hasVerifier = 1;
723722
let skipDefaultBuilders = 1;
723+
let assemblyFormat = [{
724+
custom<OmittedTerminatorRegion>($scopeRegion) (`:` type($results)^)? attr-dict
725+
}];
724726

725727
let builders = [
726728
// Scopes for yielding values.

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

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,28 @@ bool omitRegionTerm(mlir::Region &r) {
185185
return singleNonEmptyBlock && yieldsNothing();
186186
}
187187

188+
//===----------------------------------------------------------------------===//
189+
// CIR Custom Parsers/Printers
190+
//===----------------------------------------------------------------------===//
191+
192+
static mlir::ParseResult
193+
parseOmittedTerminatorRegion(mlir::OpAsmParser &parser, mlir::Region &region) {
194+
auto regionLoc = parser.getCurrentLocation();
195+
if (parser.parseRegion(region))
196+
return failure();
197+
if (ensureRegionTerm(parser, region, regionLoc).failed())
198+
return failure();
199+
return success();
200+
}
201+
202+
static void printOmittedTerminatorRegion(mlir::OpAsmPrinter &printer,
203+
mlir::cir::ScopeOp &op,
204+
mlir::Region &region) {
205+
printer.printRegion(region,
206+
/*printEntryBlockArgs=*/false,
207+
/*printBlockTerminators=*/!omitRegionTerm(region));
208+
}
209+
188210
//===----------------------------------------------------------------------===//
189211
// AllocaOp
190212
//===----------------------------------------------------------------------===//
@@ -581,35 +603,6 @@ LogicalResult IfOp::verify() { return success(); }
581603
// ScopeOp
582604
//===----------------------------------------------------------------------===//
583605

584-
ParseResult cir::ScopeOp::parse(OpAsmParser &parser, OperationState &result) {
585-
// Create one region within 'scope'.
586-
result.regions.reserve(1);
587-
Region *scopeRegion = result.addRegion();
588-
auto loc = parser.getCurrentLocation();
589-
590-
// Parse the scope region.
591-
if (parser.parseRegion(*scopeRegion, /*arguments=*/{}, /*argTypes=*/{}))
592-
return failure();
593-
594-
if (ensureRegionTerm(parser, *scopeRegion, loc).failed())
595-
return failure();
596-
597-
// Parse the optional attribute list.
598-
if (parser.parseOptionalAttrDict(result.attributes))
599-
return failure();
600-
return success();
601-
}
602-
603-
void cir::ScopeOp::print(OpAsmPrinter &p) {
604-
p << ' ';
605-
auto &scopeRegion = this->getScopeRegion();
606-
p.printRegion(scopeRegion,
607-
/*printEntryBlockArgs=*/false,
608-
/*printBlockTerminators=*/!omitRegionTerm(scopeRegion));
609-
610-
p.printOptionalAttrDict(getOperation()->getAttrs());
611-
}
612-
613606
/// Given the region at `index`, or the parent operation if `index` is None,
614607
/// return the successor regions. These are the regions that may be selected
615608
/// during the flow of control. `operands` is a set of optional attributes that
@@ -619,7 +612,7 @@ void ScopeOp::getSuccessorRegions(mlir::RegionBranchPoint point,
619612
SmallVectorImpl<RegionSuccessor> &regions) {
620613
// The only region always branch back to the parent operation.
621614
if (!point.isParent()) {
622-
regions.push_back(RegionSuccessor(getResults()));
615+
regions.push_back(RegionSuccessor(getODSResults(0)));
623616
return;
624617
}
625618

clang/test/CIR/IR/scope.cir

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// RUN: cir-opt %s -o %t.cir
2+
// RUN: FileCheck --input-file=%t.cir %s
3+
!u32i = !cir.int<u, 32>
4+
5+
module {
6+
// Should properly print/parse scope with implicit empty yield.
7+
cir.func @implicit_yield() {
8+
cir.scope {
9+
}
10+
// CHECK: cir.scope {
11+
// CHECK: }
12+
cir.return
13+
}
14+
15+
// Should properly print/parse scope with explicit yield.
16+
cir.func @explicit_yield() {
17+
%0 = cir.scope {
18+
%1 = cir.alloca !u32i, cir.ptr <!u32i>, ["a", init] {alignment = 4 : i64}
19+
cir.yield %1 : !cir.ptr<!u32i>
20+
} : !cir.ptr<!u32i>
21+
// CHECK: %0 = cir.scope {
22+
// [...]
23+
// CHECK: cir.yield %1 : !cir.ptr<!u32i>
24+
// CHECK: } : !cir.ptr<!u32i>
25+
cir.return
26+
}
27+
}

0 commit comments

Comments
 (0)