[Mlir-commits] [mlir] [mlir][emitc] Add 'emitc.switch' op to the dialect (PR #102331)

Simon Camphausen llvmlistbot at llvm.org
Tue Aug 13 03:27:45 PDT 2024


================
@@ -1096,6 +1096,197 @@ GetGlobalOp::verifySymbolUses(SymbolTableCollection &symbolTable) {
   return success();
 }
 
+//===----------------------------------------------------------------------===//
+// SwitchOp
+//===----------------------------------------------------------------------===//
+
+/// Parse the case regions and values.
+static ParseResult
+parseSwitchCases(OpAsmParser &parser, DenseI64ArrayAttr &cases,
+                 SmallVectorImpl<std::unique_ptr<Region>> &caseRegions) {
+  SmallVector<int64_t> caseValues;
+  while (succeeded(parser.parseOptionalKeyword("case"))) {
+    int64_t value;
+    Region &region = *caseRegions.emplace_back(std::make_unique<Region>());
+
+    if (parser.parseInteger(value) || parser.parseColon() ||
+        parser.parseRegion(region, /*arguments=*/{}))
+      return failure();
+    caseValues.push_back(value);
+  }
+  cases = parser.getBuilder().getDenseI64ArrayAttr(caseValues);
+  return success();
+}
+
+/// Print the case regions and values.
+static void printSwitchCases(OpAsmPrinter &p, Operation *op,
+                             DenseI64ArrayAttr cases, RegionRange caseRegions) {
+  for (auto [value, region] : llvm::zip(cases.asArrayRef(), caseRegions)) {
+    p.printNewline();
+    p << "case " << value << ": ";
+    p.printRegion(*region, /*printEntryBlockArgs=*/false);
+  }
+}
+
+ParseResult SwitchOp::parse(OpAsmParser &parser, OperationState &result) {
+  OpAsmParser::UnresolvedOperand arg;
+  DenseI64ArrayAttr casesAttr;
+  SmallVector<std::unique_ptr<Region>, 2> caseRegionsRegions;
+  std::unique_ptr<Region> defaultRegionRegion = std::make_unique<Region>();
+
+  if (parser.parseOperand(arg))
+    return failure();
+
+  Type argType;
+  // Parse the case's type.
+  if (parser.parseColon() || parser.parseType(argType))
+    return failure();
+
+  auto loc = parser.getCurrentLocation();
+  if (parser.parseOptionalAttrDict(result.attributes))
+    return failure();
+
+  if (failed(verifyInherentAttrs(result.name, result.attributes, [&]() {
+        return parser.emitError(loc)
+               << "'" << result.name.getStringRef() << "' op ";
+      })))
+    return failure();
+
+  auto odsResult = parseSwitchCases(parser, casesAttr, caseRegionsRegions);
+  if (odsResult)
+    return failure();
+
+  result.getOrAddProperties<SwitchOp::Properties>().cases = casesAttr;
+
+  if (parser.parseKeyword("default") || parser.parseColon())
+    return failure();
+
+  if (parser.parseRegion(*defaultRegionRegion))
+    return failure();
+
+  result.addRegion(std::move(defaultRegionRegion));
+  result.addRegions(caseRegionsRegions);
+
+  if (parser.resolveOperand(arg, argType, result.operands))
+    return failure();
+
+  return success();
+}
+
+void SwitchOp::print(OpAsmPrinter &p) {
+  p << ' ' << getArg();
+  SmallVector<StringRef, 2> elidedAttrs;
+  elidedAttrs.push_back("cases");
+  p.printOptionalAttrDict((*this)->getAttrs(), elidedAttrs);
+  p << ' ';
+  printSwitchCases(p, *this, getCasesAttr(), getCaseRegions());
+  p.printNewline();
+  p << "default ";
----------------
simon-camp wrote:

I've checked out the code and ran a few tests.

You are currently expecting a colon after the default keyword in the parser. Can you then please remove  the colons from `parseSwitchCases` and `printSwitchCases`, so these functions are a 1:1 copy of the scf index switch version.

Additionally you are parsing `: type` after the argument, but you are not printing that, so the custom syntax does not roundtrip.

Therefore I suggest switching to custom assembly in tablegen similar to the scf dialect:
```
let assemblyFormat = [{
    $arg `:` type($arg) attr-dict custom<SwitchCases>($cases, $caseRegions) `\n`
    `` `default` $defaultRegion
  }];
```

You can then delete `SwitchOp::parse` and `SwitchOp::print` as these will be auto generated.

Additionally you need to add a valid switch op in `test/Dialect/EmitC/ops.mlir`; this file tests roundtripping of the custom assembly format.

https://github.com/llvm/llvm-project/pull/102331


More information about the Mlir-commits mailing list