[Mlir-commits] [mlir] [mlir][scf] Implement getSingle... of LoopLikeOpinterface for scf::ParallelOp (PR #68511)

Felix Schneider llvmlistbot at llvm.org
Sun Oct 8 01:18:40 PDT 2023


https://github.com/ubfx updated https://github.com/llvm/llvm-project/pull/68511

>From 342834f6f4139186564f0680584771632641c1de Mon Sep 17 00:00:00 2001
From: Felix Schneider <fx.schn at gmail.com>
Date: Sun, 8 Oct 2023 08:16:06 +0000
Subject: [PATCH] [mlir][scf] Implement getSingle... of LoopLikeOpinterface for
 scf::ParallelOp

This adds implementations for `getSingleIterationVar`, `getSingleLowerBound`,
`getSingleUpperBound`, `getSingleStep` of `LoopLikeOpInterface` to
`scf::ParallelOp`. Until now, the implementations for these methods defaulted
to returning `std::nullopt`, even in the special case where the parallel
Op only has one dimension.

Related: https://github.com/llvm/llvm-project/pull/67883
---
 mlir/include/mlir/Dialect/SCF/IR/SCFOps.td |  3 ++-
 mlir/lib/Dialect/SCF/IR/SCF.cpp            | 24 ++++++++++++++++++++++
 2 files changed, 26 insertions(+), 1 deletion(-)

diff --git a/mlir/include/mlir/Dialect/SCF/IR/SCFOps.td b/mlir/include/mlir/Dialect/SCF/IR/SCFOps.td
index e1a604a88715f0e..21847592aebfdcd 100644
--- a/mlir/include/mlir/Dialect/SCF/IR/SCFOps.td
+++ b/mlir/include/mlir/Dialect/SCF/IR/SCFOps.td
@@ -788,7 +788,8 @@ def IfOp : SCF_Op<"if", [DeclareOpInterfaceMethods<RegionBranchOpInterface, [
 def ParallelOp : SCF_Op<"parallel",
     [AutomaticAllocationScope,
      AttrSizedOperandSegments,
-     DeclareOpInterfaceMethods<LoopLikeOpInterface>,
+     DeclareOpInterfaceMethods<LoopLikeOpInterface, ["getSingleInductionVar",
+          "getSingleLowerBound", "getSingleUpperBound", "getSingleStep"]>,
      RecursiveMemoryEffects,
      DeclareOpInterfaceMethods<RegionBranchOpInterface>,
      SingleBlockImplicitTerminator<"scf::YieldOp">]> {
diff --git a/mlir/lib/Dialect/SCF/IR/SCF.cpp b/mlir/lib/Dialect/SCF/IR/SCF.cpp
index 8d8481421e18d57..3cc1943d66c3c83 100644
--- a/mlir/lib/Dialect/SCF/IR/SCF.cpp
+++ b/mlir/lib/Dialect/SCF/IR/SCF.cpp
@@ -2911,6 +2911,30 @@ void ParallelOp::print(OpAsmPrinter &p) {
 
 SmallVector<Region *> ParallelOp::getLoopRegions() { return {&getRegion()}; }
 
+std::optional<Value> ParallelOp::getSingleInductionVar() {
+  if (getNumLoops() != 1)
+    return std::nullopt;
+  return getBody()->getArgument(0);
+}
+
+std::optional<OpFoldResult> ParallelOp::getSingleLowerBound() {
+  if (getNumLoops() != 1)
+    return std::nullopt;
+  return getLowerBound()[0];
+}
+
+std::optional<OpFoldResult> ParallelOp::getSingleUpperBound() {
+  if (getNumLoops() != 1)
+    return std::nullopt;
+  return getUpperBound()[0];
+}
+
+std::optional<OpFoldResult> ParallelOp::getSingleStep() {
+  if (getNumLoops() != 1)
+    return std::nullopt;
+  return getStep()[0];
+}
+
 ParallelOp mlir::scf::getParallelForInductionVarOwner(Value val) {
   auto ivArg = llvm::dyn_cast<BlockArgument>(val);
   if (!ivArg)



More information about the Mlir-commits mailing list