[Mlir-commits] [mlir] [mlir][gpu] Eliminate redundant gpu.barrier ops (PR #71575)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Tue Nov 7 11:12:44 PST 2023


https://github.com/spaceotter created https://github.com/llvm/llvm-project/pull/71575

Adds a canonicalizer for gpu.barrier that gets rid of duplicates.

>From fd295a87f13bcf46dc6dd5f11ff17f09c64f4d69 Mon Sep 17 00:00:00 2001
From: Eric Eaton <eric at nod-labs.com>
Date: Tue, 7 Nov 2023 11:07:23 -0800
Subject: [PATCH] [mlir][gpu] Eliminate redundant gpu.barrier ops

---
 mlir/include/mlir/Dialect/GPU/IR/GPUOps.td |  1 +
 mlir/lib/Dialect/GPU/IR/GPUDialect.cpp     | 28 ++++++++++++++++++++++
 2 files changed, 29 insertions(+)

diff --git a/mlir/include/mlir/Dialect/GPU/IR/GPUOps.td b/mlir/include/mlir/Dialect/GPU/IR/GPUOps.td
index 6375d35f4311295..632cdd96c6d4c2b 100644
--- a/mlir/include/mlir/Dialect/GPU/IR/GPUOps.td
+++ b/mlir/include/mlir/Dialect/GPU/IR/GPUOps.td
@@ -1010,6 +1010,7 @@ def GPU_BarrierOp : GPU_Op<"barrier"> {
     in convergence.
   }];
   let assemblyFormat = "attr-dict";
+  let hasCanonicalizer = 1;
 }
 
 def GPU_GPUModuleOp : GPU_Op<"module", [
diff --git a/mlir/lib/Dialect/GPU/IR/GPUDialect.cpp b/mlir/lib/Dialect/GPU/IR/GPUDialect.cpp
index 5eb2cadc884e151..d9ffacfd0d54f59 100644
--- a/mlir/lib/Dialect/GPU/IR/GPUDialect.cpp
+++ b/mlir/lib/Dialect/GPU/IR/GPUDialect.cpp
@@ -1139,6 +1139,34 @@ void ShuffleOp::build(OpBuilder &builder, OperationState &result, Value value,
         mode);
 }
 
+//===----------------------------------------------------------------------===//
+// BarrierOp
+//===----------------------------------------------------------------------===//
+
+namespace {
+
+/// Remove gpu.barrier after gpu.barrier, the threads are already synchronized!
+struct EraseRedundantGpuBarrierOpPairs : public OpRewritePattern<BarrierOp> {
+public:
+  using OpRewritePattern::OpRewritePattern;
+
+  LogicalResult matchAndRewrite(BarrierOp op,
+                                PatternRewriter &rewriter) const final {
+    if (isa<BarrierOp>(op->getNextNode())) {
+      rewriter.eraseOp(op->getNextNode());
+      return success();
+    }
+    return failure();
+  }
+};
+
+} // end anonymous namespace
+
+void BarrierOp::getCanonicalizationPatterns(RewritePatternSet &results,
+                                            MLIRContext *context) {
+  results.add<EraseRedundantGpuBarrierOpPairs>(context);
+}
+
 //===----------------------------------------------------------------------===//
 // GPUFuncOp
 //===----------------------------------------------------------------------===//



More information about the Mlir-commits mailing list