[Mlir-commits] [mlir] [MLIR] Don't check for key before inserting in map in GreedyPatternRewriteDriver worklist (NFC) (PR #88148)

Mehdi Amini llvmlistbot at llvm.org
Tue Apr 9 09:19:49 PDT 2024


https://github.com/joker-eph created https://github.com/llvm/llvm-project/pull/88148

This is a common anti-pattern (any volunteer for a clang-tidy check?).

Amazingly a micro-benchmark of the GreedyPatternRewriteDriver shows a 2.5x speedup of the entire GreedyPatternRewriteDriver execution with this patch.

>From 2054ff4eec222717458ba19badafe086c48aa737 Mon Sep 17 00:00:00 2001
From: Mehdi Amini <joker.eph at gmail.com>
Date: Tue, 9 Apr 2024 09:16:33 -0700
Subject: [PATCH] [MLIR] Don't check for key before inserting in map in
 GreedyPatternRewriteDriver worklist (NFC)

This is a common anti-pattern (any volunteer for a clang-tidy check?).

Amazingly a micro-benchmark of the GreedyPatternRewriteDriver shows
a 2.5x speedup of the entire GreedyPatternRewriteDriver execution
with this patch.
---
 mlir/lib/Transforms/Utils/GreedyPatternRewriteDriver.cpp | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/mlir/lib/Transforms/Utils/GreedyPatternRewriteDriver.cpp b/mlir/lib/Transforms/Utils/GreedyPatternRewriteDriver.cpp
index bbecbdb8566935..604d8628fae755 100644
--- a/mlir/lib/Transforms/Utils/GreedyPatternRewriteDriver.cpp
+++ b/mlir/lib/Transforms/Utils/GreedyPatternRewriteDriver.cpp
@@ -243,9 +243,8 @@ bool Worklist::empty() const {
 void Worklist::push(Operation *op) {
   assert(op && "cannot push nullptr to worklist");
   // Check to see if the worklist already contains this op.
-  if (map.count(op))
+  if (map.insert({op, list.size()}).second)
     return;
-  map[op] = list.size();
   list.push_back(op);
 }
 



More information about the Mlir-commits mailing list