[Mlir-commits] [mlir] [mlir][emitc] Add op modelling C expressions (PR #71631)
Simon Camphausen
llvmlistbot at llvm.org
Wed Nov 8 06:49:48 PST 2023
================
@@ -189,6 +189,82 @@ LogicalResult emitc::ConstantOp::verify() {
OpFoldResult emitc::ConstantOp::fold(FoldAdaptor adaptor) { return getValue(); }
+//===----------------------------------------------------------------------===//
+// ExpressionOp
+//===----------------------------------------------------------------------===//
+
+Operation *ExpressionOp::getRootOp() {
+ auto yieldOp = cast<YieldOp>(getRegion().front().getTerminator());
+ Value yieldedValue = yieldOp.getResult();
+ Operation *rootOp = yieldedValue.getDefiningOp();
+ assert(rootOp && "Yielded value not defined within expression");
+ return rootOp;
+}
+
+ParseResult ExpressionOp::parse(OpAsmParser &parser, OperationState &result) {
+ // Parse the optional attribute list.
+ if (parser.parseOptionalAttrDict(result.attributes))
+ return failure();
+
+ // Parse results type.
+ Type expressionType;
+ if (parser.parseColonType(expressionType))
+ return failure();
+ result.addTypes(expressionType);
+
+ // Create the expression's body region.
+ result.regions.reserve(1);
+ Region *region = result.addRegion();
+
+ // Parse the region.
+ if (parser.parseRegion(*region, /*arguments=*/{}, /*argTypes=*/{}))
+ return failure();
+
+ return success();
+}
+
+void ExpressionOp::print(OpAsmPrinter &p) {
+ p.printOptionalAttrDict((*this)->getAttrs());
+
+ p << " : " << getResult().getType() << ' ';
+
+ p.printRegion(getRegion(),
+ /*printEntryBlockArgs=*/false,
+ /*printBlockTerminators=*/true);
+}
----------------
simon-camp wrote:
```suggestion
```
See assembly syntax in tablegen definition.
https://github.com/llvm/llvm-project/pull/71631
More information about the Mlir-commits
mailing list