================
@@ -1087,6 +1083,139 @@ LogicalResult cir::ScopeOp::verify() {
   return success();
 }
 
+//===----------------------------------------------------------------------===//
+// TryOp
+//===----------------------------------------------------------------------===//
+
+void cir::TryOp::build(
+    OpBuilder &builder, OperationState &result,
+    function_ref<void(OpBuilder &, Location)> tryBodyBuilder,
+    function_ref<void(OpBuilder &, Location, OperationState &)> catchBuilder) {
+  assert(tryBodyBuilder && "expected builder callback for 'cir.try' body");
+
+  OpBuilder::InsertionGuard guard(builder);
+
+  // Try body region
+  Region *tryBodyRegion = result.addRegion();
+
+  // Create try body region and set insertion point
+  builder.createBlock(tryBodyRegion);
+  tryBodyBuilder(builder, result.location);
+  catchBuilder(builder, result.location, result);
+}
+
+void cir::TryOp::getSuccessorRegions(
+    mlir::RegionBranchPoint point, SmallVectorImpl<RegionSuccessor> &regions) {
+  // If any index all the underlying regions branch back to the parent
+  // operation.
+  if (!point.isParent()) {
+    regions.push_back(RegionSuccessor());
+    return;
+  }
+
+  // If the condition isn't constant, both regions may be executed.
+  regions.push_back(RegionSuccessor(&getTryRegion()));
+
+  // FIXME: optimize, ideas include:
+  // - If we know a target function never throws a specific type, we can
+  //   remove the catch handler.
+  for (auto &r : this->getCatchRegions())
+    regions.push_back(RegionSuccessor(&r));
+}
+
+void printCatchRegions(OpAsmPrinter &p, cir::TryOp op,
+                       mlir::MutableArrayRef<::mlir::Region> regions,
+                       mlir::ArrayAttr catchList) {
+
+  int currCatchIdx = 0;
+  if (!catchList)
+    return;
+  p << "catch [";
+  llvm::interleaveComma(catchList, p, [&](const Attribute &a) {
+    auto exRtti = a;
+
+    if (mlir::isa<cir::CatchUnwindAttr>(a)) {
+      p.printAttribute(a);
+      p << " ";
+    } else if (!exRtti) {
+      p << "all";
+    } else {
+      p << "type ";
+      p.printAttribute(exRtti);
+      p << " ";
+    }
+    p.printRegion(regions[currCatchIdx], /*printEntryBLockArgs=*/false,
+                  /*printBlockTerminators=*/true);
+    currCatchIdx++;
+  });
+  p << "]";
+}
+
+ParseResult parseCatchRegions(
----------------
andykaylor wrote:

Can you add an IR test for the printing and parsing?

https://github.com/llvm/llvm-project/pull/162528
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to