Skip to content

[mlir][scf]: Expose emitNormalizedLoopBounds/denormalizeInductionVariable util functions (NFC) #94429

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

Merged
merged 1 commit into from
Jun 14, 2024

Conversation

AviadCo
Copy link
Contributor

@AviadCo AviadCo commented Jun 5, 2024

No description provided.

@llvmbot
Copy link
Member

llvmbot commented Jun 5, 2024

@llvm/pr-subscribers-mlir-arith
@llvm/pr-subscribers-mlir-affine
@llvm/pr-subscribers-mlir-scf

@llvm/pr-subscribers-mlir

Author: Aviad Cohen (AviadCo)

Changes

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

2 Files Affected:

  • (modified) mlir/include/mlir/Dialect/SCF/Utils/Utils.h (+25)
  • (modified) mlir/lib/Dialect/SCF/Utils/Utils.cpp (+5-25)
diff --git a/mlir/include/mlir/Dialect/SCF/Utils/Utils.h b/mlir/include/mlir/Dialect/SCF/Utils/Utils.h
index bc09cc7f7fa5e..dde2daddb81e0 100644
--- a/mlir/include/mlir/Dialect/SCF/Utils/Utils.h
+++ b/mlir/include/mlir/Dialect/SCF/Utils/Utils.h
@@ -120,6 +120,31 @@ LogicalResult loopUnrollByFactor(
     scf::ForOp forOp, uint64_t unrollFactor,
     function_ref<void(unsigned, Operation *, OpBuilder)> annotateFn = nullptr);
 
+/// This structure is to pass and return sets of loop parameters without
+/// confusing the order.
+struct LoopParams {
+  Value lowerBound;
+  Value upperBound;
+  Value step;
+};
+
+/// Transform a loop with a strictly positive step
+///   for %i = %lb to %ub step %s
+/// into a 0-based loop with step 1
+///   for %ii = 0 to ceildiv(%ub - %lb, %s) step 1 {
+///     %i = %ii * %s + %lb
+/// Insert the induction variable remapping in the body of `inner`, which is
+/// expected to be either `loop` or another loop perfectly nested under `loop`.
+/// Insert the definition of new bounds immediate before `outer`, which is
+/// expected to be either `loop` or its parent in the loop nest.
+LoopParams emitNormalizedLoopBounds(RewriterBase &rewriter, Location loc,
+                                    Value lb, Value ub, Value step);
+
+/// Get back the original induction variable values after loop normalization.
+void denormalizeInductionVariable(RewriterBase &rewriter, Location loc,
+                                  Value normalizedIv, Value origLb,
+                                  Value origStep);
+
 /// Tile a nest of standard for loops rooted at `rootForOp` by finding such
 /// parametric tile sizes that the outer loops have a fixed number of iterations
 /// as defined in `sizes`.
diff --git a/mlir/lib/Dialect/SCF/Utils/Utils.cpp b/mlir/lib/Dialect/SCF/Utils/Utils.cpp
index 6658cca03eba7..dd8a650cf1089 100644
--- a/mlir/lib/Dialect/SCF/Utils/Utils.cpp
+++ b/mlir/lib/Dialect/SCF/Utils/Utils.cpp
@@ -29,16 +29,6 @@
 
 using namespace mlir;
 
-namespace {
-// This structure is to pass and return sets of loop parameters without
-// confusing the order.
-struct LoopParams {
-  Value lowerBound;
-  Value upperBound;
-  Value step;
-};
-} // namespace
-
 SmallVector<scf::ForOp> mlir::replaceLoopNestWithNewYields(
     RewriterBase &rewriter, MutableArrayRef<scf::ForOp> loopNest,
     ValueRange newIterOperands, const NewYieldValuesFn &newYieldValuesFn,
@@ -473,17 +463,8 @@ LogicalResult mlir::loopUnrollByFactor(
   return success();
 }
 
-/// Transform a loop with a strictly positive step
-///   for %i = %lb to %ub step %s
-/// into a 0-based loop with step 1
-///   for %ii = 0 to ceildiv(%ub - %lb, %s) step 1 {
-///     %i = %ii * %s + %lb
-/// Insert the induction variable remapping in the body of `inner`, which is
-/// expected to be either `loop` or another loop perfectly nested under `loop`.
-/// Insert the definition of new bounds immediate before `outer`, which is
-/// expected to be either `loop` or its parent in the loop nest.
-static LoopParams emitNormalizedLoopBounds(RewriterBase &rewriter, Location loc,
-                                           Value lb, Value ub, Value step) {
+LoopParams mlir::emitNormalizedLoopBounds(RewriterBase &rewriter, Location loc,
+                                          Value lb, Value ub, Value step) {
   // For non-index types, generate `arith` instructions
   // Check if the loop is already known to have a constant zero lower bound or
   // a constant one step.
@@ -517,10 +498,9 @@ static LoopParams emitNormalizedLoopBounds(RewriterBase &rewriter, Location loc,
   return {newLowerBound, newUpperBound, newStep};
 }
 
-/// Get back the original induction variable values after loop normalization
-static void denormalizeInductionVariable(RewriterBase &rewriter, Location loc,
-                                         Value normalizedIv, Value origLb,
-                                         Value origStep) {
+void mlir::denormalizeInductionVariable(RewriterBase &rewriter, Location loc,
+                                        Value normalizedIv, Value origLb,
+                                        Value origStep) {
   Value denormalizedIv;
   SmallPtrSet<Operation *, 2> preserve;
   bool isStepOne = isConstantIntValue(origStep, 1);

@AviadCo AviadCo force-pushed the scf/expose-norm-funcs branch 2 times, most recently from 5bc850b to 0436d9f Compare June 6, 2024 04:05
@AviadCo AviadCo force-pushed the scf/expose-norm-funcs branch from 0436d9f to 3ffd895 Compare June 7, 2024 10:41
Copy link
Contributor Author

@AviadCo AviadCo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the code review!

@AviadCo AviadCo force-pushed the scf/expose-norm-funcs branch from 3ffd895 to 34feac8 Compare June 7, 2024 10:48
Copy link
Contributor

@MaheshRavishankar MaheshRavishankar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One more comment, but looks good to me!

@AviadCo AviadCo force-pushed the scf/expose-norm-funcs branch from 34feac8 to 79eb2a8 Compare June 9, 2024 06:07
@AviadCo
Copy link
Contributor Author

AviadCo commented Jun 12, 2024

@MaheshRavishankar can please take a look at the last revision? thanks!

Copy link
Contributor

@MaheshRavishankar MaheshRavishankar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If changing the function to take OpFoldResult arguments works without issues, this looks good to me.

@AviadCo AviadCo force-pushed the scf/expose-norm-funcs branch from 79eb2a8 to ef30bfc Compare June 13, 2024 03:30
@AviadCo
Copy link
Contributor Author

AviadCo commented Jun 13, 2024

If changing the function to take OpFoldResult arguments works without issues, this looks good to me.

I refactored the func args, it added a little more wrapping functions but works the same.

@AviadCo AviadCo force-pushed the scf/expose-norm-funcs branch from ef30bfc to 8191de2 Compare June 13, 2024 05:21
@AviadCo AviadCo force-pushed the scf/expose-norm-funcs branch from 8191de2 to 237db65 Compare June 13, 2024 09:24
…able util functions

* Also updated normarlize/denormalize loop bounds to be folded if
possible.
@AviadCo AviadCo force-pushed the scf/expose-norm-funcs branch from 237db65 to fe5c071 Compare June 13, 2024 09:27
@AviadCo
Copy link
Contributor Author

AviadCo commented Jun 13, 2024

@MaheshRavishankar can u please take another look?

Copy link
Contributor

@MaheshRavishankar MaheshRavishankar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks fine to me! I already approved. Are you wait for me to land this?

@MaheshRavishankar
Copy link
Contributor

Actually I just realized that LoopParams here is same as Range here . I should have pointed it out earlier. Fine to do as a follow up, but would appreciate just using Range instead of LoopParams. If not, I can do as follow up.

@AviadCo
Copy link
Contributor Author

AviadCo commented Jun 14, 2024

Looks fine to me! I already approved. Are you wait for me to land this?

I thought I need reapproval after new changes, will land this one

@AviadCo AviadCo merged commit 85e8d62 into llvm:main Jun 14, 2024
7 checks passed
@AviadCo
Copy link
Contributor Author

AviadCo commented Jun 14, 2024

Actually I just realized that LoopParams here is same as Range here . I should have pointed it out earlier. Fine to do as a follow up, but would appreciate just using Range instead of LoopParams. If not, I can do as follow up.

I will do the follow up PR it is o.k.

@AviadCo
Copy link
Contributor Author

AviadCo commented Jun 14, 2024

@MaheshRavishankar Please review the follow up PR: #95501
@kuhar thanks for the review here also :)

@AviadCo AviadCo deleted the scf/expose-norm-funcs branch June 14, 2024 04:10
EthanLuisMcDonough pushed a commit to EthanLuisMcDonough/llvm-project that referenced this pull request Aug 13, 2024
…able util functions (llvm#94429)

Also adjusted `LoopParams` to use OpFoldResult instead of Value.
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.

4 participants