Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[mlir][scf] Implement getSingle... of LoopLikeOpInterface for scf::ForallOp #67883

Merged
merged 2 commits into from
Oct 20, 2023

Conversation

ubfx
Copy link
Member

@ubfx ubfx commented Sep 30, 2023

The getSingle(IterationVar|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.

…rallOp

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`.
@llvmbot
Copy link
Member

llvmbot commented Sep 30, 2023

@llvm/pr-subscribers-mlir-scf

@llvm/pr-subscribers-mlir

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/67883.diff

2 Files Affected:

  • (modified) mlir/include/mlir/Dialect/SCF/IR/SCFOps.td (+2-1)
  • (modified) mlir/lib/Dialect/SCF/IR/SCF.cpp (+21)
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)

@ubfx ubfx merged commit 833a8db into llvm:main Oct 20, 2023
ubfx added a commit to ubfx/llvm-project that referenced this pull request Oct 20, 2023
…rallelOp

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: llvm#67883
ubfx added a commit that referenced this pull request Oct 20, 2023
…rallelOp (#68511)

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: #67883
@ubfx ubfx deleted the forall-looplike-single branch October 21, 2023 07:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants