[clang] [CIR] Add if statement support (PR #134333)
via cfe-commits
cfe-commits at lists.llvm.org
Sun Apr 6 11:19:00 PDT 2025
================
@@ -447,6 +448,133 @@ mlir::LogicalResult cir::ReturnOp::verify() {
return success();
}
+//===----------------------------------------------------------------------===//
+// IfOp
+//===----------------------------------------------------------------------===//
+
+ParseResult cir::IfOp::parse(OpAsmParser &parser, OperationState &result) {
+ // create the regions for 'then'.
+ result.regions.reserve(2);
+ Region *thenRegion = result.addRegion();
+ Region *elseRegion = result.addRegion();
+
+ mlir::Builder &builder = parser.getBuilder();
+ OpAsmParser::UnresolvedOperand cond;
+ Type boolType = cir::BoolType::get(builder.getContext());
+
+ if (parser.parseOperand(cond) ||
+ parser.resolveOperand(cond, boolType, result.operands))
+ return failure();
+
+ // Parse 'then' region.
+ mlir::SMLoc parseThenLoc = parser.getCurrentLocation();
+ if (parser.parseRegion(*thenRegion, /*arguments=*/{}, /*argTypes=*/{}))
+ return failure();
+
+ if (ensureRegionTerm(parser, *thenRegion, parseThenLoc).failed())
+ return failure();
+
+ // If we find an 'else' keyword, parse the 'else' region.
+ if (!parser.parseOptionalKeyword("else")) {
+ mlir::SMLoc parseElseLoc = parser.getCurrentLocation();
+ if (parser.parseRegion(*elseRegion, /*arguments=*/{}, /*argTypes=*/{}))
+ return failure();
+ if (ensureRegionTerm(parser, *elseRegion, parseElseLoc).failed())
+ return failure();
+ }
+
+ // Parse the optional attribute list.
+ if (parser.parseOptionalAttrDict(result.attributes))
+ return failure();
+ return success();
+}
+
+void cir::IfOp::print(OpAsmPrinter &p) {
+
----------------
Andres-Salamanca wrote:
Changes applied Let me know if it looks good now
https://github.com/llvm/llvm-project/pull/134333
More information about the cfe-commits
mailing list