Skip to content

[AffineParallelize] expose options when creating pass #124959

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

Conversation

rscottmanley
Copy link
Contributor

Add a createAffineParallelizePass() that takes AffineParallelizeOptions
so it can be customized in a pass pipeline.

@llvmbot
Copy link
Member

llvmbot commented Jan 29, 2025

@llvm/pr-subscribers-mlir-affine

@llvm/pr-subscribers-mlir

Author: Scott Manley (rscottmanley)

Changes

Add a createAffineParallelizePass() that takes AffineParallelizeOptions
so it can be customized in a pass pipeline.


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

2 Files Affected:

  • (modified) mlir/include/mlir/Dialect/Affine/Passes.h (+4)
  • (modified) mlir/lib/Dialect/Affine/Transforms/AffineParallelize.cpp (+13)
diff --git a/mlir/include/mlir/Dialect/Affine/Passes.h b/mlir/include/mlir/Dialect/Affine/Passes.h
index e152101236dc7a9..2d2b5f461e813e3 100644
--- a/mlir/include/mlir/Dialect/Affine/Passes.h
+++ b/mlir/include/mlir/Dialect/Affine/Passes.h
@@ -48,6 +48,10 @@ createAffineLoopInvariantCodeMotionPass();
 /// ops.
 std::unique_ptr<OperationPass<func::FuncOp>> createAffineParallelizePass();
 
+/// Creates a pass to convert all parallel affine.for's into 1-d affine.parallel
+/// ops using the specified parallelize options.
+std::unique_ptr<OperationPass<func::FuncOp>> createAffineParallelizePass(const AffineParallelizeOptions &options);
+
 /// Apply normalization transformations to affine loop-like ops. If
 /// `promoteSingleIter` is true, single iteration loops are promoted (i.e., the
 /// loop is replaced by its loop body).
diff --git a/mlir/lib/Dialect/Affine/Transforms/AffineParallelize.cpp b/mlir/lib/Dialect/Affine/Transforms/AffineParallelize.cpp
index 64f2bc6e745c374..f841b83c5aecde0 100644
--- a/mlir/lib/Dialect/Affine/Transforms/AffineParallelize.cpp
+++ b/mlir/lib/Dialect/Affine/Transforms/AffineParallelize.cpp
@@ -42,6 +42,14 @@ namespace {
 /// Convert all parallel affine.for op into 1-D affine.parallel op.
 struct AffineParallelize
     : public affine::impl::AffineParallelizeBase<AffineParallelize> {
+
+  AffineParallelize() = default;
+
+  explicit AffineParallelize(const AffineParallelizeOptions &options) {
+    maxNested = std::move(options.maxNested);
+    parallelReductions = std::move(options.parallelReductions);
+  }
+
   void runOnOperation() override;
 };
 
@@ -95,3 +103,8 @@ std::unique_ptr<OperationPass<func::FuncOp>>
 mlir::affine::createAffineParallelizePass() {
   return std::make_unique<AffineParallelize>();
 }
+
+std::unique_ptr<OperationPass<func::FuncOp>>
+mlir::affine::createAffineParallelizePass(const AffineParallelizeOptions &options) {
+  return std::make_unique<AffineParallelize>(options);
+}

@rscottmanley rscottmanley force-pushed the rscottmanley/affine-parallelize-options branch from afface6 to f5c04c8 Compare January 29, 2025 17:48
@@ -42,6 +42,14 @@ namespace {
/// Convert all parallel affine.for op into 1-D affine.parallel op.
struct AffineParallelize
: public affine::impl::AffineParallelizeBase<AffineParallelize> {

AffineParallelize() = default;

Copy link
Contributor

Choose a reason for hiding this comment

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

These constructors should have been auto-generated in AffineParallelizeBase. You probably just need to make it visible here with using AffineParallelizeBase<AffineParallelize>::AffineParallelizeBase...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agree -- will change. Thanks for the review Diego!

@@ -95,3 +103,9 @@ std::unique_ptr<OperationPass<func::FuncOp>>
mlir::affine::createAffineParallelizePass() {
return std::make_unique<AffineParallelize>();
}

std::unique_ptr<OperationPass<func::FuncOp>>
mlir::affine::createAffineParallelizePass(
Copy link
Contributor

Choose a reason for hiding this comment

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

There is also a way to have this auto-generated. I think if you go to Affine/Passes.td and remove the line let constructor = "mlir::affine::createAffineParallelizePass()";, tablegen should generate the respective createAffineParallelizePass constructors.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ironically had done it this way before seeing how OneShotBufferize did it and then did it that way instead! I definitely prefer this way so I'll make that change.

Use the constructors auto generated in AffineParallelizeBase so
AffineParallelizeOptions can be used when adding it to a pass pipeline.

Also remove the constructor from the def in the pass table.
@rscottmanley rscottmanley force-pushed the rscottmanley/affine-parallelize-options branch from f5c04c8 to db1f642 Compare January 30, 2025 03:42
Copy link
Contributor

@dcaballe dcaballe left a comment

Choose a reason for hiding this comment

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

Cool, thanks for modernizing this code!

@jeanPerier jeanPerier merged commit 29371d3 into llvm:main Jan 30, 2025
8 checks passed
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