[clang] [CIR] Add if statement support (PR #134333)
Andy Kaylor via cfe-commits
cfe-commits at lists.llvm.org
Fri Apr 4 17:34:42 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) {
+
+ p << " " << getCondition() << " ";
+ mlir::Region &thenRegion = this->getThenRegion();
+ p.printRegion(thenRegion,
+ /*printEntryBlockArgs=*/false,
+ /*printBlockTerminators=*/!omitRegionTerm(thenRegion));
+
+ // Print the 'else' regions if it exists and has a block.
+ mlir::Region &elseRegion = this->getElseRegion();
+ if (!elseRegion.empty()) {
+ p << " else ";
+ p.printRegion(elseRegion,
+ /*printEntryBlockArgs=*/false,
+ /*printBlockTerminators=*/!omitRegionTerm(elseRegion));
+ }
+
+ p.printOptionalAttrDict(getOperation()->getAttrs());
+}
+
+/// Default callback for IfOp builders.
+void cir::buildTerminatedBody(OpBuilder &builder, Location loc) {
+ // add cir.yield to end of the block
+ builder.create<cir::YieldOp>(loc);
+}
+
+/// Given the region at `index`, or the parent operation if `index` is None,
+/// return the successor regions. These are the regions that may be selected
+/// during the flow of control. `operands` is a set of optional attributes that
+/// correspond to a constant value for each operand, or null if that operand is
+/// not a constant.
+void cir::IfOp::getSuccessorRegions(mlir::RegionBranchPoint point,
+ SmallVectorImpl<RegionSuccessor> ®ions) {
+ // The `then` and the `else` region branch back to the parent operation.
+ if (!point.isParent()) {
+ regions.push_back(RegionSuccessor());
+ return;
+ }
+
+ // Don't consider the else region if it is empty.
+ Region *elseRegion = &this->getElseRegion();
+ if (elseRegion->empty())
+ elseRegion = nullptr;
+
+ // Otherwise, the successor is dependent on the condition.
+ // bool condition;
+ // if (auto condAttr = operands.front().dyn_cast_or_null<IntegerAttr>()) {
----------------
andykaylor wrote:
I'm not sure what this code was doing or when it was commented out. It should be removed.
https://github.com/llvm/llvm-project/pull/134333
More information about the cfe-commits
mailing list