Skip to content

[mlir][gpu] Add vector::CombiningKind conversion helper - #107134

Closed
andfau-amd wants to merge 1 commit into
llvm:mainfrom
andfau-amd:18142-convertReductionKind-reverse
Closed

andfau-amd wants to merge 1 commit into
llvm:mainfrom
andfau-amd:18142-convertReductionKind-reverse

Conversation

@andfau-amd

Copy link
Copy Markdown
Contributor

This mirrors the existing conversion in the other direction.

This mirrors the existing conversion in the other direction.
@llvmbot

llvmbot commented Sep 3, 2024

Copy link
Copy Markdown
Member

@llvm/pr-subscribers-mlir-gpu

Author: Andrea Faulds (andfau-amd)

Changes

This mirrors the existing conversion in the other direction.


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

2 Files Affected:

  • (modified) mlir/include/mlir/Dialect/GPU/Transforms/Utils.h (+2)
  • (modified) mlir/lib/Dialect/GPU/Transforms/Utils.cpp (+26)
diff --git a/mlir/include/mlir/Dialect/GPU/Transforms/Utils.h b/mlir/include/mlir/Dialect/GPU/Transforms/Utils.h
index 073493971e6b7c..349b2cff20ce8e 100644
--- a/mlir/include/mlir/Dialect/GPU/Transforms/Utils.h
+++ b/mlir/include/mlir/Dialect/GPU/Transforms/Utils.h
@@ -29,6 +29,8 @@ class LaunchOp;
 
 /// Returns the matching vector combining kind.
 vector::CombiningKind convertReductionKind(gpu::AllReduceOperation mode);
+/// Returns the matching reduce operation.
+gpu::AllReduceOperation convertReductionKind(vector::CombiningKind mode);
 } // namespace gpu
 
 /// Get a gpu.func created from outlining the region of a gpu.launch op with the
diff --git a/mlir/lib/Dialect/GPU/Transforms/Utils.cpp b/mlir/lib/Dialect/GPU/Transforms/Utils.cpp
index e91aa18128c7b9..7ebb0c23d3e64f 100644
--- a/mlir/lib/Dialect/GPU/Transforms/Utils.cpp
+++ b/mlir/lib/Dialect/GPU/Transforms/Utils.cpp
@@ -41,4 +41,30 @@ vector::CombiningKind convertReductionKind(gpu::AllReduceOperation mode) {
   llvm_unreachable("Vector and GPU reduction kinds should match 1:1");
 }
 
+gpu::AllReduceOperation convertReductionKind(vector::CombiningKind op) {
+  switch (unsigned(op)) {
+#define MAP_CASE(X)                                                            \
+  case unsigned(vector::CombiningKind::X):                                     \
+    return gpu::AllReduceOperation::X
+
+    MAP_CASE(ADD);
+    MAP_CASE(MUL);
+    MAP_CASE(MINUI);
+    MAP_CASE(MINSI);
+    MAP_CASE(MINNUMF);
+    MAP_CASE(MAXSI);
+    MAP_CASE(MAXUI);
+    MAP_CASE(MAXNUMF);
+    MAP_CASE(AND);
+    MAP_CASE(OR);
+    MAP_CASE(XOR);
+    MAP_CASE(MINIMUMF);
+    MAP_CASE(MAXIMUMF);
+
+#undef MAP_CASE
+  }
+
+  llvm_unreachable("Vector and GPU reduction kinds should match 1:1");
+}
+
 } // namespace mlir::gpu

@llvmbot

llvmbot commented Sep 3, 2024

Copy link
Copy Markdown
Member

@llvm/pr-subscribers-mlir

Author: Andrea Faulds (andfau-amd)

Changes

This mirrors the existing conversion in the other direction.


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

2 Files Affected:

  • (modified) mlir/include/mlir/Dialect/GPU/Transforms/Utils.h (+2)
  • (modified) mlir/lib/Dialect/GPU/Transforms/Utils.cpp (+26)
diff --git a/mlir/include/mlir/Dialect/GPU/Transforms/Utils.h b/mlir/include/mlir/Dialect/GPU/Transforms/Utils.h
index 073493971e6b7c..349b2cff20ce8e 100644
--- a/mlir/include/mlir/Dialect/GPU/Transforms/Utils.h
+++ b/mlir/include/mlir/Dialect/GPU/Transforms/Utils.h
@@ -29,6 +29,8 @@ class LaunchOp;
 
 /// Returns the matching vector combining kind.
 vector::CombiningKind convertReductionKind(gpu::AllReduceOperation mode);
+/// Returns the matching reduce operation.
+gpu::AllReduceOperation convertReductionKind(vector::CombiningKind mode);
 } // namespace gpu
 
 /// Get a gpu.func created from outlining the region of a gpu.launch op with the
diff --git a/mlir/lib/Dialect/GPU/Transforms/Utils.cpp b/mlir/lib/Dialect/GPU/Transforms/Utils.cpp
index e91aa18128c7b9..7ebb0c23d3e64f 100644
--- a/mlir/lib/Dialect/GPU/Transforms/Utils.cpp
+++ b/mlir/lib/Dialect/GPU/Transforms/Utils.cpp
@@ -41,4 +41,30 @@ vector::CombiningKind convertReductionKind(gpu::AllReduceOperation mode) {
   llvm_unreachable("Vector and GPU reduction kinds should match 1:1");
 }
 
+gpu::AllReduceOperation convertReductionKind(vector::CombiningKind op) {
+  switch (unsigned(op)) {
+#define MAP_CASE(X)                                                            \
+  case unsigned(vector::CombiningKind::X):                                     \
+    return gpu::AllReduceOperation::X
+
+    MAP_CASE(ADD);
+    MAP_CASE(MUL);
+    MAP_CASE(MINUI);
+    MAP_CASE(MINSI);
+    MAP_CASE(MINNUMF);
+    MAP_CASE(MAXSI);
+    MAP_CASE(MAXUI);
+    MAP_CASE(MAXNUMF);
+    MAP_CASE(AND);
+    MAP_CASE(OR);
+    MAP_CASE(XOR);
+    MAP_CASE(MINIMUMF);
+    MAP_CASE(MAXIMUMF);
+
+#undef MAP_CASE
+  }
+
+  llvm_unreachable("Vector and GPU reduction kinds should match 1:1");
+}
+
 } // namespace mlir::gpu

@andfau-amd

Copy link
Copy Markdown
Contributor Author

I don't know if it has an immediate application in this repo, but it will be used for iree-org/iree#18142. In order to replace manual building of arith reduction + shuffles with the upstream gpu subgroup reduction, it's necessary to do this conversion. I don't mind if merging this is delayed until the other pieces are there, but I think it is a trivial and straightforward improvement.

@andfau-amd

Copy link
Copy Markdown
Contributor Author

Abandoning in favor of downstream implementation (iree-org/iree#18443).

@andfau-amd andfau-amd closed this Sep 5, 2024
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.

2 participants