[Mlir-commits] [mlir] [mlir][scf] Implement getSingle... of LoopLikeOpInterface for scf::ForallOp (PR #67883)

Felix Schneider llvmlistbot at llvm.org
Sat Sep 30 06:53:30 PDT 2023


https://github.com/ubfx created https://github.com/llvm/llvm-project/pull/67883

The `getSingle(UpperBound|LowerBound|Step)` methods of `LoopLikeOpInterface` are
useful to quickly query the iteration space of unidimensional loops. Until now,
`scf::ForallOp` always fell back to the default implementation of these methods,
returning `std::nullopt`.

This patch implements those methods, returning the respective bounds or steps
in the special case of `rank == 1`.

>From 8f1378be30b8dcbc738232187002a130403e8bee Mon Sep 17 00:00:00 2001
From: Felix Schneider <fx.schn at gmail.com>
Date: Sat, 30 Sep 2023 13:40:29 +0000
Subject: [PATCH] [mlir][scf] Implement getSingle... of LoopLikeOpInterface for
 scf::ForallOp

The `getSingle(UpperBound|LowerBound|Step)` methods of `LoopLikeOpInterface` are
useful to quickly query the iteration space of unidimensional loops. Until now,
`scf::ForallOp` always fell back to the default implementation of these methods,
returning `std::nullopt`.

This patch implements those methods, returning the respective bounds or steps
in the special case of `rank == 1`.
---
 mlir/include/mlir/Dialect/SCF/IR/SCFOps.td |  3 ++-
 mlir/lib/Dialect/SCF/IR/SCF.cpp            | 21 +++++++++++++++++++++
 2 files changed, 23 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..218d3b09d96fb05 100644
--- a/mlir/include/mlir/Dialect/SCF/IR/SCFOps.td
+++ b/mlir/include/mlir/Dialect/SCF/IR/SCFOps.td
@@ -331,7 +331,8 @@ def ForallOp : SCF_Op<"forall", [
        AttrSizedOperandSegments,
        AutomaticAllocationScope,
        DeclareOpInterfaceMethods<LoopLikeOpInterface,
-          ["promoteIfSingleIteration"]>,
+          ["promoteIfSingleIteration", "getSingleLowerBound",
+          "getSingleUpperBound", "getSingleStep"]>,
        RecursiveMemoryEffects,
        SingleBlockImplicitTerminator<"scf::InParallelOp">,
        DeclareOpInterfaceMethods<RegionBranchOpInterface>,
diff --git a/mlir/lib/Dialect/SCF/IR/SCF.cpp b/mlir/lib/Dialect/SCF/IR/SCF.cpp
index 8d8481421e18d57..bfe6ccd7b1169df 100644
--- a/mlir/lib/Dialect/SCF/IR/SCF.cpp
+++ b/mlir/lib/Dialect/SCF/IR/SCF.cpp
@@ -1526,6 +1526,27 @@ InParallelOp ForallOp::getTerminator() {
   return cast<InParallelOp>(getBody()->getTerminator());
 }
 
+std::optional<OpFoldResult> ForallOp::getSingleLowerBound() {
+  if (getRank() != 1)
+    return std::nullopt;
+
+  return getMixedLowerBound()[0];
+}
+
+std::optional<OpFoldResult> ForallOp::getSingleUpperBound() {
+  if (getRank() != 1)
+    return std::nullopt;
+
+  return getMixedUpperBound()[0];
+}
+
+std::optional<OpFoldResult> ForallOp::getSingleStep() {
+  if (getRank() != 1)
+    return std::nullopt;
+
+  return getMixedStep()[0];
+}
+
 ForallOp mlir::scf::getForallOpThreadIndexOwner(Value val) {
   auto tidxArg = llvm::dyn_cast<BlockArgument>(val);
   if (!tidxArg)



More information about the Mlir-commits mailing list