[llvm] [mlir] [WIP][mlir] Add Normalize pass (PR #162266)

Shourya Goel via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 10 02:52:34 PDT 2025


https://github.com/Sh0g0-1758 updated https://github.com/llvm/llvm-project/pull/162266

>From 38973424260e1e1371d3113ef34643af3848d038 Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Sat, 20 Sep 2025 16:58:13 +0530
Subject: [PATCH 01/13] setup the normalize pass

---
 .../mlir/Conversion/Normalize/Normalize.h     | 22 +++++++++
 mlir/include/mlir/Conversion/Passes.h         |  1 +
 mlir/include/mlir/Conversion/Passes.td        | 18 +++++++
 mlir/lib/Conversion/CMakeLists.txt            |  1 +
 mlir/lib/Conversion/Normalize/CMakeLists.txt  | 24 +++++++++
 mlir/lib/Conversion/Normalize/Normalize.cpp   | 49 +++++++++++++++++++
 6 files changed, 115 insertions(+)
 create mode 100644 mlir/include/mlir/Conversion/Normalize/Normalize.h
 create mode 100644 mlir/lib/Conversion/Normalize/CMakeLists.txt
 create mode 100644 mlir/lib/Conversion/Normalize/Normalize.cpp

diff --git a/mlir/include/mlir/Conversion/Normalize/Normalize.h b/mlir/include/mlir/Conversion/Normalize/Normalize.h
new file mode 100644
index 0000000000000..fdb1dcd8f2d77
--- /dev/null
+++ b/mlir/include/mlir/Conversion/Normalize/Normalize.h
@@ -0,0 +1,22 @@
+//===- Normalize.h - Math to outlined impl conversion -----------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef MLIR_CONVERSION_NORMALIZE_NORMALIZE_H
+#define MLIR_CONVERSION_NORMALIZE_NORMALIZE_H
+
+#include <memory>
+
+namespace mlir {
+class Pass;
+
+#define GEN_PASS_DECL_NORMALIZE
+#include "mlir/Conversion/Passes.h.inc"
+
+} // namespace mlir
+
+#endif // MLIR_CONVERSION_NORMALIZE_NORMALIZE_H
diff --git a/mlir/include/mlir/Conversion/Passes.h b/mlir/include/mlir/Conversion/Passes.h
index da061b269daf7..442ad55e77204 100644
--- a/mlir/include/mlir/Conversion/Passes.h
+++ b/mlir/include/mlir/Conversion/Passes.h
@@ -52,6 +52,7 @@
 #include "mlir/Conversion/MemRefToEmitC/MemRefToEmitCPass.h"
 #include "mlir/Conversion/MemRefToLLVM/MemRefToLLVM.h"
 #include "mlir/Conversion/MemRefToSPIRV/MemRefToSPIRVPass.h"
+#include "mlir/Conversion/Normalize/Normalize.h"
 #include "mlir/Conversion/NVGPUToNVVM/NVGPUToNVVM.h"
 #include "mlir/Conversion/NVVMToLLVM/NVVMToLLVM.h"
 #include "mlir/Conversion/OpenACCToSCF/ConvertOpenACCToSCF.h"
diff --git a/mlir/include/mlir/Conversion/Passes.td b/mlir/include/mlir/Conversion/Passes.td
index 1a37d057776e2..a161ec3ce0a0f 100644
--- a/mlir/include/mlir/Conversion/Passes.td
+++ b/mlir/include/mlir/Conversion/Passes.td
@@ -943,6 +943,24 @@ def ConvertShardToMPIPass : Pass<"convert-shard-to-mpi"> {
   ];
 }
 
+//===----------------------------------------------------------------------===//
+// Normalize
+//===----------------------------------------------------------------------===//
+def Normalize : Pass<"normalize", "ModuleOp"> {
+  let summary = "normalize.";
+  let description = [{
+    just normalize bro
+  }];
+  let dependentDialects = [
+    "arith::ArithDialect",
+    "cf::ControlFlowDialect",
+    "func::FuncDialect",
+    "scf::SCFDialect",
+    "vector::VectorDialect",
+    "LLVM::LLVMDialect",
+  ];
+}
+
 //===----------------------------------------------------------------------===//
 // NVVMToLLVM
 //===----------------------------------------------------------------------===//
diff --git a/mlir/lib/Conversion/CMakeLists.txt b/mlir/lib/Conversion/CMakeLists.txt
index 71986f83c4870..759e0a092e2d3 100644
--- a/mlir/lib/Conversion/CMakeLists.txt
+++ b/mlir/lib/Conversion/CMakeLists.txt
@@ -45,6 +45,7 @@ add_subdirectory(MemRefToLLVM)
 add_subdirectory(MemRefToSPIRV)
 add_subdirectory(ShardToMPI)
 add_subdirectory(MPIToLLVM)
+add_subdirectory(Normalize)
 add_subdirectory(NVGPUToNVVM)
 add_subdirectory(NVVMToLLVM)
 add_subdirectory(OpenACCToSCF)
diff --git a/mlir/lib/Conversion/Normalize/CMakeLists.txt b/mlir/lib/Conversion/Normalize/CMakeLists.txt
new file mode 100644
index 0000000000000..a9944349714e3
--- /dev/null
+++ b/mlir/lib/Conversion/Normalize/CMakeLists.txt
@@ -0,0 +1,24 @@
+add_mlir_conversion_library(MLIRNormalize
+  Normalize.cpp
+
+  ADDITIONAL_HEADER_DIRS
+  ${MLIR_MAIN_INCLUDE_DIR}/mlir/Conversion/Normalize
+
+  DEPENDS
+  MLIRConversionPassIncGen
+
+  LINK_COMPONENTS
+  Core
+
+  LINK_LIBS PUBLIC
+  MLIRArithDialect
+  MLIRControlFlowDialect
+  MLIRFuncDialect
+  MLIRLLVMDialect
+  MLIRMathDialect
+  MLIRPass
+  MLIRSCFDialect
+  MLIRTransforms
+  MLIRVectorDialect
+  MLIRVectorUtils
+)
diff --git a/mlir/lib/Conversion/Normalize/Normalize.cpp b/mlir/lib/Conversion/Normalize/Normalize.cpp
new file mode 100644
index 0000000000000..7db70da1699d9
--- /dev/null
+++ b/mlir/lib/Conversion/Normalize/Normalize.cpp
@@ -0,0 +1,49 @@
+//===- Normalize.cpp - IR to simplified IR conversion ---------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "mlir/Conversion/Normalize/Normalize.h"
+
+#include "mlir/Dialect/Arith/IR/Arith.h"
+#include "mlir/Dialect/ControlFlow/IR/ControlFlowOps.h"
+#include "mlir/Dialect/Func/IR/FuncOps.h"
+#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
+#include "mlir/Dialect/Math/IR/Math.h"
+#include "mlir/Dialect/SCF/IR/SCF.h"
+#include "mlir/Dialect/Utils/IndexingUtils.h"
+#include "mlir/Dialect/Vector/IR/VectorOps.h"
+#include "mlir/Dialect/Vector/Utils/VectorUtils.h"
+#include "mlir/IR/TypeUtilities.h"
+#include "mlir/Pass/Pass.h"
+#include "mlir/Transforms/DialectConversion.h"
+#include "llvm/ADT/DenseMap.h"
+#include "llvm/ADT/TypeSwitch.h"
+#include "llvm/Support/DebugLog.h"
+
+namespace mlir {
+#define GEN_PASS_DEF_NORMALIZE
+#include "mlir/Conversion/Passes.h.inc"
+} // namespace mlir
+
+using namespace mlir;
+
+#define DEBUG_TYPE "normalize"
+
+namespace {
+struct NormalizePass: public impl::NormalizeBase<NormalizePass> {
+  NormalizePass() = default;
+
+  void runOnOperation() override;
+};
+} // namespace
+
+#include <iostream>
+
+void NormalizePass::runOnOperation() {
+  std::cout << "OH HEY MAN!!" << std::endl;
+  ModuleOp module = getOperation();
+}

>From 5aa301b5b032700198ffb044c69b82d22d511536 Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Wed, 24 Sep 2025 21:45:44 +0530
Subject: [PATCH 02/13] reorder instructions

---
 mlir/lib/Conversion/Normalize/Normalize.cpp | 112 +++++++++++++++++++-
 1 file changed, 108 insertions(+), 4 deletions(-)

diff --git a/mlir/lib/Conversion/Normalize/Normalize.cpp b/mlir/lib/Conversion/Normalize/Normalize.cpp
index 7db70da1699d9..f87a9c8216f30 100644
--- a/mlir/lib/Conversion/Normalize/Normalize.cpp
+++ b/mlir/lib/Conversion/Normalize/Normalize.cpp
@@ -23,6 +23,7 @@
 #include "llvm/ADT/DenseMap.h"
 #include "llvm/ADT/TypeSwitch.h"
 #include "llvm/Support/DebugLog.h"
+#include "llvm/ADT/Hashing.h"
 
 namespace mlir {
 #define GEN_PASS_DEF_NORMALIZE
@@ -35,15 +36,118 @@ using namespace mlir;
 
 namespace {
 struct NormalizePass: public impl::NormalizeBase<NormalizePass> {
-  NormalizePass() = default;
+    NormalizePass() = default;
+
+    void runOnOperation() override;
+private:
+    const uint64_t MagicHashConstant = 0x6acaa36bef8325c5ULL;
+    void collectOutputOperations(Block &block, SmallVector<Operation *, 16>& Output);
+    bool isOutput(Operation& op);
+    void reorderOperations(SmallVector<Operation*, 16>& Outputs);
+    void reorderOperation(
+        mlir::Operation *used,
+        mlir::Operation *user,
+        llvm::SmallPtrSet<const mlir::Operation *, 32> &visited);
+    void RenameOperations(Block &block, SmallVector<Operation*, 16>& Outputs);
 
-  void runOnOperation() override;
 };
 } // namespace
 
 #include <iostream>
 
 void NormalizePass::runOnOperation() {
-  std::cout << "OH HEY MAN!!" << std::endl;
-  ModuleOp module = getOperation();
+    ModuleOp module = getOperation();
+
+    for (Operation &op : module.getOps()) {
+        llvm::outs() << "Operation name: " << op.getName() << "\n";
+        SmallVector<Operation *, 16> Outputs;
+
+        for (Region &region : op.getRegions()) {
+            for (Block &block : region) {
+                collectOutputOperations(block, Outputs);
+            }
+        }
+        for (auto& op : Outputs) llvm::outs() << op->getName() << "\n";
+
+        reorderOperations(Outputs);
+    }
+
+    for (Operation &op : module.getOps()) {
+        llvm::outs() << "Operation name: " << op.getName() << "\n";
+        SmallVector<Operation *, 16> Outputs;
+
+        for (Region &region : op.getRegions()) {
+            for (Block &block : region) {
+              RenameOperations(block, Outputs);
+            }
+        }
+    }
+}
+
+void NormalizePass::RenameOperations(Block &block, SmallVector<Operation*, 16>& Outputs) {
+  static size_t VarCounter = 0;
+  for(Operation& innerOp : block) {
+    /**
+     * TODO: Renaming scheme
+     */
+  }
+}
+
+
+void NormalizePass::reorderOperations(SmallVector<Operation*, 16>& Outputs) {
+  llvm::SmallPtrSet<const mlir::Operation *, 32> visited;
+  for (auto *op : Outputs) {
+    for (mlir::Value operand : op->getOperands()) {
+      if (mlir::Operation *defOp = operand.getDefiningOp()) {
+        reorderOperation(defOp, op, visited);
+      }
+    }
+  }
+}
+
+void NormalizePass::reorderOperation(
+    mlir::Operation *used, mlir::Operation *user,
+    llvm::SmallPtrSet<const mlir::Operation *, 32> &visited) {
+
+  if (!visited.count(used)) {
+    visited.insert(used);
+
+    mlir::Block *usedBlock = used->getBlock();
+    mlir::Block *userBlock = user->getBlock();
+
+    if (usedBlock == userBlock) {
+      used->moveBefore(user);
+    } else {
+      used->moveBefore(&usedBlock->back());
+    }
+
+    for (mlir::Value operand : used->getOperands()) {
+      if (mlir::Operation *defOp = operand.getDefiningOp()) {
+        reorderOperation(defOp, used, visited);
+      }
+    }
+  }
+}
+
+void NormalizePass::collectOutputOperations(Block &block, SmallVector<Operation*, 16>& Outputs) {
+  for(Operation& innerOp : block) {
+    if(isOutput(innerOp)) {
+        Outputs.emplace_back(&innerOp);
+    }
+  }
+}
+
+bool NormalizePass::isOutput(Operation& op) {
+    if (op.hasTrait<OpTrait::IsTerminator>())
+        return true;
+
+    if (auto memOp = dyn_cast<MemoryEffectOpInterface>(&op)) {
+        SmallVector<MemoryEffects::EffectInstance, 4> effects;
+        memOp.getEffects(effects);
+        for (auto &effect : effects) {
+            if (isa<MemoryEffects::Write>( effect.getEffect() )) return true;
+        }
+    }
+
+    return false;
 }

>From dda4e0b7b36c62e2910d02f3cad0e059dc846d25 Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Sat, 4 Oct 2025 14:19:28 +0530
Subject: [PATCH 03/13] rename op results

---
 mlir/lib/Conversion/Normalize/Normalize.cpp | 31 +++++++--------------
 1 file changed, 10 insertions(+), 21 deletions(-)

diff --git a/mlir/lib/Conversion/Normalize/Normalize.cpp b/mlir/lib/Conversion/Normalize/Normalize.cpp
index f87a9c8216f30..d6b24c17775bf 100644
--- a/mlir/lib/Conversion/Normalize/Normalize.cpp
+++ b/mlir/lib/Conversion/Normalize/Normalize.cpp
@@ -12,18 +12,11 @@
 #include "mlir/Dialect/ControlFlow/IR/ControlFlowOps.h"
 #include "mlir/Dialect/Func/IR/FuncOps.h"
 #include "mlir/Dialect/LLVMIR/LLVMDialect.h"
-#include "mlir/Dialect/Math/IR/Math.h"
 #include "mlir/Dialect/SCF/IR/SCF.h"
-#include "mlir/Dialect/Utils/IndexingUtils.h"
-#include "mlir/Dialect/Vector/IR/VectorOps.h"
 #include "mlir/Dialect/Vector/Utils/VectorUtils.h"
 #include "mlir/IR/TypeUtilities.h"
 #include "mlir/Pass/Pass.h"
-#include "mlir/Transforms/DialectConversion.h"
-#include "llvm/ADT/DenseMap.h"
-#include "llvm/ADT/TypeSwitch.h"
-#include "llvm/Support/DebugLog.h"
-#include "llvm/ADT/Hashing.h"
+#include "llvm/Support/FormatVariadic.h"
 
 namespace mlir {
 #define GEN_PASS_DEF_NORMALIZE
@@ -48,18 +41,15 @@ struct NormalizePass: public impl::NormalizeBase<NormalizePass> {
         mlir::Operation *used,
         mlir::Operation *user,
         llvm::SmallPtrSet<const mlir::Operation *, 32> &visited);
-    void RenameOperations(Block &block, SmallVector<Operation*, 16>& Outputs);
+    void RenameOperations(Block &block);
 
 };
 } // namespace
 
-#include <iostream>
-
 void NormalizePass::runOnOperation() {
     ModuleOp module = getOperation();
 
     for (Operation &op : module.getOps()) {
-        llvm::outs() << "Operation name: " << op.getName() << "\n";
         SmallVector<Operation *, 16> Outputs;
 
         for (Region &region : op.getRegions()) {
@@ -67,33 +57,32 @@ void NormalizePass::runOnOperation() {
                 collectOutputOperations(block, Outputs);
             }
         }
-        for (auto& op : Outputs) llvm::outs() << op->getName() << "\n";
 
         reorderOperations(Outputs);
     }
 
     for (Operation &op : module.getOps()) {
-        llvm::outs() << "Operation name: " << op.getName() << "\n";
         SmallVector<Operation *, 16> Outputs;
 
         for (Region &region : op.getRegions()) {
             for (Block &block : region) {
-              RenameOperations(block, Outputs);
+              RenameOperations(block);
             }
         }
     }
 }
 
-void NormalizePass::RenameOperations(Block &block, SmallVector<Operation*, 16>& Outputs) {
+void NormalizePass::RenameOperations(Block &block) {
   static size_t VarCounter = 0;
-  for(Operation& innerOp : block) {
-    /**
-     * TODO: Renaming scheme
-     */
+
+  for (Operation &innerOp : block) {
+    mlir::OpBuilder  b(innerOp.getContext());
+    mlir::StringAttr sat = b.getStringAttr(llvm::formatv("v{0}", VarCounter++).str());
+    mlir::Location newLoc = mlir::NameLoc::get(sat, innerOp.getLoc());
+    innerOp.setLoc(newLoc);
   }
 }
 
-
 void NormalizePass::reorderOperations(SmallVector<Operation*, 16>& Outputs) {
   llvm::SmallPtrSet<const mlir::Operation *, 32> visited;
   for (auto *op : Outputs) {

>From fe9a6fa4c6feca3bf1b46c32cd1a8b535d97534c Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Sat, 4 Oct 2025 18:31:40 +0530
Subject: [PATCH 04/13] naming scheme

---
 llvm/lib/Transforms/Utils/IRNormalizer.cpp  |   3 +
 mlir/lib/Conversion/Normalize/Normalize.cpp | 242 +++++++++++++++-----
 2 files changed, 185 insertions(+), 60 deletions(-)

diff --git a/llvm/lib/Transforms/Utils/IRNormalizer.cpp b/llvm/lib/Transforms/Utils/IRNormalizer.cpp
index fefa49f68c8da..7d4b02ded9560 100644
--- a/llvm/lib/Transforms/Utils/IRNormalizer.cpp
+++ b/llvm/lib/Transforms/Utils/IRNormalizer.cpp
@@ -324,7 +324,10 @@ void IRNormalizer::nameAsRegularInstruction(Instruction *I) {
       Name.append(F->getName());
 
   Name.append("(");
+  llvm::outs() << "BROOSKO SIZEO HEHEHE >>> " << Operands.size() << "\n";
+  llvm::outs() << "OPERANDO >> \n";
   for (size_t i = 0; i < Operands.size(); ++i) {
+    llvm::outs() << Operands[i] << "\n";
     Name.append(Operands[i]);
 
     if (i < Operands.size() - 1)
diff --git a/mlir/lib/Conversion/Normalize/Normalize.cpp b/mlir/lib/Conversion/Normalize/Normalize.cpp
index d6b24c17775bf..97d45d1dfb6bd 100644
--- a/mlir/lib/Conversion/Normalize/Normalize.cpp
+++ b/mlir/lib/Conversion/Normalize/Normalize.cpp
@@ -17,6 +17,11 @@
 #include "mlir/IR/TypeUtilities.h"
 #include "mlir/Pass/Pass.h"
 #include "llvm/Support/FormatVariadic.h"
+#include "llvm/Support/raw_ostream.h"
+#include "mlir/IR/AsmState.h"
+#include "llvm/ADT/Hashing.h"
+
+#include <sstream>
 
 namespace mlir {
 #define GEN_PASS_DEF_NORMALIZE
@@ -28,70 +33,188 @@ using namespace mlir;
 #define DEBUG_TYPE "normalize"
 
 namespace {
-struct NormalizePass: public impl::NormalizeBase<NormalizePass> {
-    NormalizePass() = default;
+struct NormalizePass : public impl::NormalizeBase<NormalizePass> {
+  NormalizePass() = default;
 
-    void runOnOperation() override;
-private:
-    const uint64_t MagicHashConstant = 0x6acaa36bef8325c5ULL;
-    void collectOutputOperations(Block &block, SmallVector<Operation *, 16>& Output);
-    bool isOutput(Operation& op);
-    void reorderOperations(SmallVector<Operation*, 16>& Outputs);
-    void reorderOperation(
-        mlir::Operation *used,
-        mlir::Operation *user,
-        llvm::SmallPtrSet<const mlir::Operation *, 32> &visited);
-    void RenameOperations(Block &block);
+  void runOnOperation() override;
 
+private:
+  const uint64_t MagicHashConstant = 0x6acaa36bef8325c5ULL;
+  void collectOutputOperations(Block &block,
+                               SmallVector<Operation *, 16> &Output);
+  bool isOutput(mlir::Operation &op);
+  void reorderOperations(SmallVector<mlir::Operation *, 16> &Outputs);
+  void
+  reorderOperation(mlir::Operation *used, mlir::Operation *user,
+                   llvm::SmallPtrSet<const mlir::Operation *, 32> &visited);
+  void RenameOperations(SmallVector<Operation *, 16> &Outputs);
+  void RenameOperation(mlir::Operation* op, SmallPtrSet<const mlir::Operation *, 32> &visited);
+  bool isInitialOperation(mlir::Operation* op);
+  void nameAsInitialOperation(mlir::Operation* op);
+  void nameAsRegularOperation(mlir::Operation* op, llvm::SmallPtrSet<const mlir::Operation *, 32> &visited);
+  bool hasOnlyImmediateOperands(mlir::Operation* op);
+  void SetDeterministicNames(Block &block);
 };
 } // namespace
 
 void NormalizePass::runOnOperation() {
-    ModuleOp module = getOperation();
+  ModuleOp module = getOperation();
 
-    for (Operation &op : module.getOps()) {
-        SmallVector<Operation *, 16> Outputs;
+  for (Operation &op : module.getOps()) {
 
-        for (Region &region : op.getRegions()) {
-            for (Block &block : region) {
-                collectOutputOperations(block, Outputs);
-            }
-        }
+    // for (Region &region : op.getRegions())
+    //   for (Block &block : region)
+    //     SetDeterministicNames(block);
 
-        reorderOperations(Outputs);
-    }
+    SmallVector<Operation *, 16> Outputs;
 
-    for (Operation &op : module.getOps()) {
-        SmallVector<Operation *, 16> Outputs;
+    for (Region &region : op.getRegions())
+      for (Block &block : region)
+        collectOutputOperations(block, Outputs);
 
-        for (Region &region : op.getRegions()) {
-            for (Block &block : region) {
-              RenameOperations(block);
-            }
-        }
-    }
+    reorderOperations(Outputs);
+
+    // RenameOperations(Outputs);
+  }
 }
 
-void NormalizePass::RenameOperations(Block &block) {
+void NormalizePass::SetDeterministicNames(Block &block) {
   static size_t VarCounter = 0;
 
   for (Operation &innerOp : block) {
-    mlir::OpBuilder  b(innerOp.getContext());
-    mlir::StringAttr sat = b.getStringAttr(llvm::formatv("v{0}", VarCounter++).str());
+    mlir::OpBuilder b(innerOp.getContext());
+    mlir::StringAttr sat =
+        b.getStringAttr(llvm::formatv("v{0}", VarCounter++).str());
     mlir::Location newLoc = mlir::NameLoc::get(sat, innerOp.getLoc());
     innerOp.setLoc(newLoc);
   }
 }
 
-void NormalizePass::reorderOperations(SmallVector<Operation*, 16>& Outputs) {
+void NormalizePass::RenameOperations(SmallVector<Operation *, 16> &Outputs) {
   llvm::SmallPtrSet<const mlir::Operation *, 32> visited;
-  for (auto *op : Outputs) {
+
+  for(auto *op : Outputs)
+    RenameOperation(op, visited);
+}
+
+void NormalizePass::RenameOperation(Operation *op, SmallPtrSet<const mlir::Operation *, 32> &visited) {
+  if (!visited.count(op)) {
+    visited.insert(op);
+
+    llvm::outs() << op->getName() << " --> ";
+  
+    if (isInitialOperation(op)) {
+      llvm::outs() <<" INITIAL\n";
+      nameAsInitialOperation(op);
+    } else {
+      llvm::outs() << " REGULAR\n";
+      nameAsRegularOperation(op, visited);
+    }
+  }
+}
+
+bool NormalizePass::isInitialOperation(mlir::Operation* op) {
+  return !op->use_empty() and hasOnlyImmediateOperands(op);
+}
+
+bool NormalizePass::hasOnlyImmediateOperands(mlir::Operation* op) {
+  for (mlir::Value operand : op->getOperands())
+    if (mlir::Operation *defOp = operand.getDefiningOp())
+      if (!(defOp->hasTrait<OpTrait::ConstantLike>()))
+        return false;
+  return true;
+}
+
+uint64_t kernel_hash(std::string data)
+{
+    const uint64_t FNV_OFFSET = 0xcbf29ce484222325ULL;
+    const uint64_t FNV_PRIME = 0x100000001b3ULL;
+    uint64_t hash = FNV_OFFSET;
+    for (unsigned char c : data)
+    {
+        hash ^= static_cast<uint64_t>(c);
+        hash *= FNV_PRIME;
+    }
+    return hash;
+}
+
+void NormalizePass::nameAsInitialOperation(mlir::Operation* op) {
+  SmallVector<SmallString<64>, 4> Operands;
+
+  if(op->getNumOperands() == 0) {
+    std::string TextRepresentation;
+    mlir::AsmState state(op);
+    llvm::raw_string_ostream Stream(TextRepresentation);
+    op->print(Stream, state);
+    Operands.push_back(StringRef(Stream.str()));
+  } else {
     for (mlir::Value operand : op->getOperands()) {
       if (mlir::Operation *defOp = operand.getDefiningOp()) {
-        reorderOperation(defOp, op, visited);
+        std::string TextRepresentation;
+        mlir::AsmState state(defOp);
+        llvm::raw_string_ostream Stream(TextRepresentation);
+        defOp->print(Stream, state);
+        Operands.push_back(StringRef(Stream.str()));
+      } else {
+        std::string TextRepresentation;
+        mlir::AsmState state(op);
+        llvm::raw_string_ostream Stream(TextRepresentation);
+        operand.print(Stream, state);
+        Operands.push_back(StringRef(Stream.str()));
       }
     }
   }
+
+  if (op->hasTrait<OpTrait::IsCommutative>()) llvm::sort(Operands);
+
+  uint64_t Hash = MagicHashConstant;
+
+  uint64_t opcodeHash = kernel_hash(op->getName().getStringRef().str());
+  Hash = llvm::hashing::detail::hash_16_bytes(Hash, opcodeHash);
+
+  SmallPtrSet<const Instruction *, 32> Visited;
+  // Get output footprint for I.
+  SetVector<int> OutputFootprint = getOutputFootprint(I, Visited);
+
+  // Consider output footprint in the hash.
+  for (const int &Output : OutputFootprint)
+    Hash = llvm::hashing::detail::hash_16_bytes(Hash, Output);
+
+  // Base instruction name.
+  SmallString<256> Name;
+  Name.append("vl" + std::to_string(Hash).substr(0, 5));
+
+  // In case of CallInst, consider callee in the instruction name.
+  if (const auto *CI = dyn_cast<CallInst>(I)) {
+    Function *F = CI->getCalledFunction();
+
+    if (F != nullptr) {
+      Name.append(F->getName());
+    }
+  }
+
+  Name.append("(");
+  for (unsigned long i = 0; i < Operands.size(); ++i) {
+    Name.append(Operands[i]);
+
+    if (i < Operands.size() - 1)
+      Name.append(", ");
+  }
+  Name.append(")");
+
+  I->setName(Name);
+}
+
+void NormalizePass::nameAsRegularOperation(mlir::Operation* op, llvm::SmallPtrSet<const mlir::Operation *, 32> &visited) {
+
+}
+
+void NormalizePass::reorderOperations(SmallVector<Operation *, 16> &Outputs) {
+  llvm::SmallPtrSet<const mlir::Operation *, 32> visited;
+  for (auto *op : Outputs)
+    for (mlir::Value operand : op->getOperands())
+      if (mlir::Operation *defOp = operand.getDefiningOp())
+        reorderOperation(defOp, op, visited);
 }
 
 void NormalizePass::reorderOperation(
@@ -104,39 +227,38 @@ void NormalizePass::reorderOperation(
     mlir::Block *usedBlock = used->getBlock();
     mlir::Block *userBlock = user->getBlock();
 
-    if (usedBlock == userBlock) {
+    if (usedBlock == userBlock)
       used->moveBefore(user);
-    } else {
+    else
       used->moveBefore(&usedBlock->back());
-    }
 
-    for (mlir::Value operand : used->getOperands()) {
-      if (mlir::Operation *defOp = operand.getDefiningOp()) {
+    for (mlir::Value operand : used->getOperands())
+      if (mlir::Operation *defOp = operand.getDefiningOp())
         reorderOperation(defOp, used, visited);
-      }
-    }
   }
 }
 
-void NormalizePass::collectOutputOperations(Block &block, SmallVector<Operation*, 16>& Outputs) {
-  for(Operation& innerOp : block) {
-    if(isOutput(innerOp)) {
-        Outputs.emplace_back(&innerOp);
-    }
+void NormalizePass::collectOutputOperations(
+    Block &block, SmallVector<Operation *, 16> &Outputs) {
+  llvm::SmallPtrSet<const mlir::Operation *, 32> visited;
+  for (Operation &innerOp : block) {
+    RenameOperation(&innerOp, visited);
+    if (isOutput(innerOp))
+      Outputs.emplace_back(&innerOp);
   }
 }
 
-bool NormalizePass::isOutput(Operation& op) {
-    if (op.hasTrait<OpTrait::IsTerminator>())
-        return true;
+bool NormalizePass::isOutput(Operation &op) {
+  if (op.hasTrait<OpTrait::IsTerminator>())
+    return true;
 
-    if (auto memOp = dyn_cast<MemoryEffectOpInterface>(&op)) {
-        SmallVector<MemoryEffects::EffectInstance, 4> effects;
-        memOp.getEffects(effects);
-        for (auto &effect : effects) {
-            if (isa<MemoryEffects::Write>( effect.getEffect() )) return true;
-        }
-    }
+  if (auto memOp = dyn_cast<MemoryEffectOpInterface>(&op)) {
+    SmallVector<MemoryEffects::EffectInstance, 4> effects;
+    memOp.getEffects(effects);
+    for (auto &effect : effects)
+      if (isa<MemoryEffects::Write>(effect.getEffect()))
+        return true;
+  }
 
-    return false;
+  return false;
 }

>From 1a8ef2f6191f753f31e8d66f3ec81ae8d84f6803 Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Sat, 4 Oct 2025 23:08:13 +0530
Subject: [PATCH 05/13] initial and regular operand renaming

---
 mlir/lib/Conversion/Normalize/Normalize.cpp | 127 ++++++++++++++++----
 1 file changed, 102 insertions(+), 25 deletions(-)

diff --git a/mlir/lib/Conversion/Normalize/Normalize.cpp b/mlir/lib/Conversion/Normalize/Normalize.cpp
index 97d45d1dfb6bd..d57b19a0aa303 100644
--- a/mlir/lib/Conversion/Normalize/Normalize.cpp
+++ b/mlir/lib/Conversion/Normalize/Normalize.cpp
@@ -54,6 +54,7 @@ struct NormalizePass : public impl::NormalizeBase<NormalizePass> {
   void nameAsRegularOperation(mlir::Operation* op, llvm::SmallPtrSet<const mlir::Operation *, 32> &visited);
   bool hasOnlyImmediateOperands(mlir::Operation* op);
   void SetDeterministicNames(Block &block);
+  llvm::SetVector<int> getOutputFootprint(mlir::Operation* op, llvm::SmallPtrSet<const mlir::Operation *, 32> &visited);
 };
 } // namespace
 
@@ -61,11 +62,6 @@ void NormalizePass::runOnOperation() {
   ModuleOp module = getOperation();
 
   for (Operation &op : module.getOps()) {
-
-    // for (Region &region : op.getRegions())
-    //   for (Block &block : region)
-    //     SetDeterministicNames(block);
-
     SmallVector<Operation *, 16> Outputs;
 
     for (Region &region : op.getRegions())
@@ -74,7 +70,7 @@ void NormalizePass::runOnOperation() {
 
     reorderOperations(Outputs);
 
-    // RenameOperations(Outputs);
+    RenameOperations(Outputs);
   }
 }
 
@@ -172,25 +168,18 @@ void NormalizePass::nameAsInitialOperation(mlir::Operation* op) {
   uint64_t opcodeHash = kernel_hash(op->getName().getStringRef().str());
   Hash = llvm::hashing::detail::hash_16_bytes(Hash, opcodeHash);
 
-  SmallPtrSet<const Instruction *, 32> Visited;
-  // Get output footprint for I.
-  SetVector<int> OutputFootprint = getOutputFootprint(I, Visited);
+  SmallPtrSet<const mlir::Operation *, 32> Visited;
+  SetVector<int> OutputFootprint = getOutputFootprint(op, Visited);
 
-  // Consider output footprint in the hash.
   for (const int &Output : OutputFootprint)
     Hash = llvm::hashing::detail::hash_16_bytes(Hash, Output);
 
-  // Base instruction name.
   SmallString<256> Name;
   Name.append("vl" + std::to_string(Hash).substr(0, 5));
 
-  // In case of CallInst, consider callee in the instruction name.
-  if (const auto *CI = dyn_cast<CallInst>(I)) {
-    Function *F = CI->getCalledFunction();
-
-    if (F != nullptr) {
-      Name.append(F->getName());
-    }
+  if (auto call = mlir::dyn_cast<mlir::func::CallOp>(op)) {
+    llvm::StringRef callee = call.getCallee();
+    Name.append(callee.str());
   }
 
   Name.append("(");
@@ -202,11 +191,71 @@ void NormalizePass::nameAsInitialOperation(mlir::Operation* op) {
   }
   Name.append(")");
 
-  I->setName(Name);
+  mlir::OpBuilder b(op->getContext());
+  mlir::StringAttr sat = b.getStringAttr(Name);
+  mlir::Location newLoc = mlir::NameLoc::get(sat, op->getLoc());
+  op->setLoc(newLoc);
 }
 
 void NormalizePass::nameAsRegularOperation(mlir::Operation* op, llvm::SmallPtrSet<const mlir::Operation *, 32> &visited) {
+  SmallVector<SmallString<64>, 4> Operands;
+  for (mlir::Value operand : op->getOperands()) {
+    if (mlir::Operation *defOp = operand.getDefiningOp()) {
+      RenameOperation(defOp, visited);
+
+      std::string TextRepresentation;
+      mlir::AsmState state(defOp);
+      llvm::raw_string_ostream Stream(TextRepresentation);
+      defOp->print(Stream, state);
+      Operands.push_back(StringRef(Stream.str()));
+    } else {
+      std::string TextRepresentation;
+      mlir::AsmState state(op);
+      llvm::raw_string_ostream Stream(TextRepresentation);
+      operand.print(Stream, state);
+      Operands.push_back(StringRef(Stream.str()));
+    }
+  }
+
+  if (op->hasTrait<OpTrait::IsCommutative>()) llvm::sort(Operands);
+
+  uint64_t Hash = MagicHashConstant;
+
+  uint64_t opcodeHash = kernel_hash(op->getName().getStringRef().str());
+  Hash = llvm::hashing::detail::hash_16_bytes(Hash, opcodeHash);
+
+  SmallVector<uint64_t, 4> OperandsOpcodes;
+
+  for (mlir::Value operand : op->getOperands())
+    if (mlir::Operation *defOp = operand.getDefiningOp())
+      OperandsOpcodes.push_back(kernel_hash(defOp->getName().getStringRef().str()));
+
+  if (op->hasTrait<OpTrait::IsCommutative>()) llvm::sort(OperandsOpcodes.begin(), OperandsOpcodes.end());
+
+  for (const uint64_t Code : OperandsOpcodes)
+    Hash = llvm::hashing::detail::hash_16_bytes(Hash, Code);
+
+  SmallString<512> Name;
+  Name.append("op" + std::to_string(Hash).substr(0, 5));
+
+  if (auto call = mlir::dyn_cast<mlir::func::CallOp>(op)) {
+    llvm::StringRef callee = call.getCallee();
+    Name.append(callee.str());
+  }
+
+  Name.append("(");
+  for (unsigned long i = 0; i < Operands.size(); ++i) {
+    Name.append(Operands[i]);
 
+    if (i < Operands.size() - 1)
+      Name.append(", ");
+  }
+  Name.append(")");
+
+  mlir::OpBuilder b(op->getContext());
+  mlir::StringAttr sat = b.getStringAttr(Name);
+  mlir::Location newLoc = mlir::NameLoc::get(sat, op->getLoc());
+  op->setLoc(newLoc);
 }
 
 void NormalizePass::reorderOperations(SmallVector<Operation *, 16> &Outputs) {
@@ -238,14 +287,10 @@ void NormalizePass::reorderOperation(
   }
 }
 
-void NormalizePass::collectOutputOperations(
-    Block &block, SmallVector<Operation *, 16> &Outputs) {
-  llvm::SmallPtrSet<const mlir::Operation *, 32> visited;
-  for (Operation &innerOp : block) {
-    RenameOperation(&innerOp, visited);
+void NormalizePass::collectOutputOperations(Block &block, SmallVector<Operation *, 16> &Outputs) {
+  for (Operation &innerOp : block)
     if (isOutput(innerOp))
       Outputs.emplace_back(&innerOp);
-  }
 }
 
 bool NormalizePass::isOutput(Operation &op) {
@@ -262,3 +307,35 @@ bool NormalizePass::isOutput(Operation &op) {
 
   return false;
 }
+
+llvm::SetVector<int> NormalizePass::getOutputFootprint(mlir::Operation* op, llvm::SmallPtrSet<const mlir::Operation *, 32> &visited) {
+  llvm::SetVector<int> Outputs;
+  if (!visited.count(op)) {
+    visited.insert(op);
+
+    if (isOutput(*op)) {
+      mlir::func::FuncOp func = op->getParentOfType<mlir::func::FuncOp>();
+
+      unsigned Count = 0;
+        for (Block &block : func.getRegion())
+          for(mlir::Operation &innerOp : block){
+            if(&innerOp==op)
+              Outputs.insert(Count);
+            Count++;
+        }
+
+      return Outputs;
+    }
+
+    for (mlir::OpOperand &use : op->getUses()) {
+      mlir::Operation *useOp = use.getOwner();
+      if (useOp) {
+        llvm::SetVector<int> OutputsUsingUop = getOutputFootprint(useOp, visited);
+
+        Outputs.insert(OutputsUsingUop.begin(), OutputsUsingUop.end());
+      }
+    }
+  }
+
+  return Outputs;
+}

>From 48aa67b8d81a7d4e779aa299e7c42fe334ba3002 Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Sun, 5 Oct 2025 01:36:19 +0530
Subject: [PATCH 06/13] rename without folding with hashing

---
 llvm/lib/Transforms/Utils/IRNormalizer.cpp  |  3 -
 mlir/lib/Conversion/Normalize/Normalize.cpp | 97 ++++++++++++++++-----
 2 files changed, 75 insertions(+), 25 deletions(-)

diff --git a/llvm/lib/Transforms/Utils/IRNormalizer.cpp b/llvm/lib/Transforms/Utils/IRNormalizer.cpp
index 7d4b02ded9560..fefa49f68c8da 100644
--- a/llvm/lib/Transforms/Utils/IRNormalizer.cpp
+++ b/llvm/lib/Transforms/Utils/IRNormalizer.cpp
@@ -324,10 +324,7 @@ void IRNormalizer::nameAsRegularInstruction(Instruction *I) {
       Name.append(F->getName());
 
   Name.append("(");
-  llvm::outs() << "BROOSKO SIZEO HEHEHE >>> " << Operands.size() << "\n";
-  llvm::outs() << "OPERANDO >> \n";
   for (size_t i = 0; i < Operands.size(); ++i) {
-    llvm::outs() << Operands[i] << "\n";
     Name.append(Operands[i]);
 
     if (i < Operands.size() - 1)
diff --git a/mlir/lib/Conversion/Normalize/Normalize.cpp b/mlir/lib/Conversion/Normalize/Normalize.cpp
index d57b19a0aa303..ef79ff907f6f6 100644
--- a/mlir/lib/Conversion/Normalize/Normalize.cpp
+++ b/mlir/lib/Conversion/Normalize/Normalize.cpp
@@ -22,6 +22,7 @@
 #include "llvm/ADT/Hashing.h"
 
 #include <sstream>
+#include <iomanip>
 
 namespace mlir {
 #define GEN_PASS_DEF_NORMALIZE
@@ -55,10 +56,13 @@ struct NormalizePass : public impl::NormalizeBase<NormalizePass> {
   bool hasOnlyImmediateOperands(mlir::Operation* op);
   void SetDeterministicNames(Block &block);
   llvm::SetVector<int> getOutputFootprint(mlir::Operation* op, llvm::SmallPtrSet<const mlir::Operation *, 32> &visited);
+  mlir::OpPrintingFlags flags{};
 };
 } // namespace
 
 void NormalizePass::runOnOperation() {
+  flags.printNameLocAsPrefix(true);
+
   ModuleOp module = getOperation();
 
   for (Operation &op : module.getOps()) {
@@ -121,7 +125,15 @@ bool NormalizePass::hasOnlyImmediateOperands(mlir::Operation* op) {
   return true;
 }
 
-uint64_t kernel_hash(std::string data)
+std::string to_string(uint64_t const hash)
+{
+  std::ostringstream oss;
+  oss << std::hex << std::setw(16) << std::setfill('0') << hash;
+  std::string tmp = oss.str();
+  return tmp.substr(11, 5);
+}
+
+uint64_t kernel_hash(std::string_view data)
 {
     const uint64_t FNV_OFFSET = 0xcbf29ce484222325ULL;
     const uint64_t FNV_PRIME = 0x100000001b3ULL;
@@ -134,29 +146,63 @@ uint64_t kernel_hash(std::string data)
     return hash;
 }
 
+void replace(std::string& str, char from, char to) {
+  for(auto& it : str) {
+    if(it == from) it = to;
+  }
+}
+
+std::vector<std::string> split(std::string_view str, char delimiter) {
+  std::vector<std::string> outs{};
+  std::stringstream ss{ std::string {str} };
+  std::string item;
+  while(std::getline(ss, item, delimiter)) {
+    replace(item, ':', '_');
+    outs.emplace_back(item);
+  }
+  return outs;
+}
+
 void NormalizePass::nameAsInitialOperation(mlir::Operation* op) {
   SmallVector<SmallString<64>, 4> Operands;
 
   if(op->getNumOperands() == 0) {
-    std::string TextRepresentation;
-    mlir::AsmState state(op);
-    llvm::raw_string_ostream Stream(TextRepresentation);
-    op->print(Stream, state);
-    Operands.push_back(StringRef(Stream.str()));
+    /**
+     * INFO: Constant operations like arith.constant
+     */
+    if(auto call = mlir::dyn_cast<mlir::func::CallOp>(op)) {
+      Operands.push_back(StringRef(std::string{"void"}));
+    } else {
+      std::string TextRepresentation;
+      mlir::AsmState state(op, flags);
+      llvm::raw_string_ostream Stream(TextRepresentation);
+      op->print(Stream, state);
+      std::string hash = to_string(kernel_hash(split(Stream.str(), '=')[1]));
+      Operands.push_back(StringRef(hash));
+    }
   } else {
     for (mlir::Value operand : op->getOperands()) {
       if (mlir::Operation *defOp = operand.getDefiningOp()) {
+        /**
+         * INFO: Constant arguments like arith.constant
+         */
         std::string TextRepresentation;
-        mlir::AsmState state(defOp);
+        mlir::AsmState state(defOp, flags);
         llvm::raw_string_ostream Stream(TextRepresentation);
         defOp->print(Stream, state);
-        Operands.push_back(StringRef(Stream.str()));
+        std::string hash = to_string(kernel_hash(split(Stream.str(), '=')[1]));
+        Operands.push_back(StringRef(hash));
       } else {
+        /**
+         * INFO: Function Arguments
+         */
         std::string TextRepresentation;
-        mlir::AsmState state(op);
+        mlir::AsmState state(op, flags);
         llvm::raw_string_ostream Stream(TextRepresentation);
         operand.print(Stream, state);
-        Operands.push_back(StringRef(Stream.str()));
+        std::string argNum = split(Stream.str(), ':')[1];
+        argNum = argNum.substr(1, argNum.size() - 1);
+        Operands.push_back(StringRef(std::string("arg" + argNum)));
       }
     }
   }
@@ -174,7 +220,7 @@ void NormalizePass::nameAsInitialOperation(mlir::Operation* op) {
   for (const int &Output : OutputFootprint)
     Hash = llvm::hashing::detail::hash_16_bytes(Hash, Output);
 
-  SmallString<256> Name;
+  std::string Name{""};
   Name.append("vl" + std::to_string(Hash).substr(0, 5));
 
   if (auto call = mlir::dyn_cast<mlir::func::CallOp>(op)) {
@@ -182,14 +228,14 @@ void NormalizePass::nameAsInitialOperation(mlir::Operation* op) {
     Name.append(callee.str());
   }
 
-  Name.append("(");
+  Name.append(" $ ");
   for (unsigned long i = 0; i < Operands.size(); ++i) {
-    Name.append(Operands[i]);
+    Name.append(std::string(Operands[i]));
 
     if (i < Operands.size() - 1)
-      Name.append(", ");
+      Name.append(" -- ");
   }
-  Name.append(")");
+  Name.append(" $.$ ");
 
   mlir::OpBuilder b(op->getContext());
   mlir::StringAttr sat = b.getStringAttr(Name);
@@ -204,16 +250,21 @@ void NormalizePass::nameAsRegularOperation(mlir::Operation* op, llvm::SmallPtrSe
       RenameOperation(defOp, visited);
 
       std::string TextRepresentation;
-      mlir::AsmState state(defOp);
+      mlir::AsmState state(defOp, flags);
       llvm::raw_string_ostream Stream(TextRepresentation);
       defOp->print(Stream, state);
-      Operands.push_back(StringRef(Stream.str()));
+      Operands.push_back(StringRef(split(Stream.str(), '=')[0]));
     } else {
+      /**
+       * INFO: Function Arguments
+       */
       std::string TextRepresentation;
-      mlir::AsmState state(op);
+      mlir::AsmState state(op, flags);
       llvm::raw_string_ostream Stream(TextRepresentation);
       operand.print(Stream, state);
-      Operands.push_back(StringRef(Stream.str()));
+      std::string argNum = split(Stream.str(), ':')[1];
+      argNum = argNum.substr(1, argNum.size() - 1);
+      Operands.push_back(StringRef(std::string("arg" + argNum)));
     }
   }
 
@@ -243,14 +294,14 @@ void NormalizePass::nameAsRegularOperation(mlir::Operation* op, llvm::SmallPtrSe
     Name.append(callee.str());
   }
 
-  Name.append("(");
+  Name.append(" $ ");
   for (unsigned long i = 0; i < Operands.size(); ++i) {
     Name.append(Operands[i]);
 
     if (i < Operands.size() - 1)
-      Name.append(", ");
+      Name.append(" -- ");
   }
-  Name.append(")");
+  Name.append(" $.$ ");
 
   mlir::OpBuilder b(op->getContext());
   mlir::StringAttr sat = b.getStringAttr(Name);
@@ -305,6 +356,8 @@ bool NormalizePass::isOutput(Operation &op) {
         return true;
   }
 
+  if (auto call = mlir::dyn_cast<mlir::func::CallOp>(op)) return true;
+
   return false;
 }
 

>From 8a9efd1d656afd461961be96be881a1b6cd95c36 Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Tue, 7 Oct 2025 16:50:57 +0530
Subject: [PATCH 07/13] operation folding

---
 mlir/lib/Conversion/Normalize/Normalize.cpp | 79 ++++++++++++++++++---
 1 file changed, 69 insertions(+), 10 deletions(-)

diff --git a/mlir/lib/Conversion/Normalize/Normalize.cpp b/mlir/lib/Conversion/Normalize/Normalize.cpp
index ef79ff907f6f6..0c0aebd3fd264 100644
--- a/mlir/lib/Conversion/Normalize/Normalize.cpp
+++ b/mlir/lib/Conversion/Normalize/Normalize.cpp
@@ -56,6 +56,7 @@ struct NormalizePass : public impl::NormalizeBase<NormalizePass> {
   bool hasOnlyImmediateOperands(mlir::Operation* op);
   void SetDeterministicNames(Block &block);
   llvm::SetVector<int> getOutputFootprint(mlir::Operation* op, llvm::SmallPtrSet<const mlir::Operation *, 32> &visited);
+  void foldOperation(mlir::Operation* op);
   mlir::OpPrintingFlags flags{};
 };
 } // namespace
@@ -75,6 +76,11 @@ void NormalizePass::runOnOperation() {
     reorderOperations(Outputs);
 
     RenameOperations(Outputs);
+
+    for (Region& region : op.getRegions())
+      for (Block &block : region)
+        for (Operation &innerOp : block)
+          foldOperation(&innerOp);
   }
 }
 
@@ -101,13 +107,9 @@ void NormalizePass::RenameOperation(Operation *op, SmallPtrSet<const mlir::Opera
   if (!visited.count(op)) {
     visited.insert(op);
 
-    llvm::outs() << op->getName() << " --> ";
-  
     if (isInitialOperation(op)) {
-      llvm::outs() <<" INITIAL\n";
       nameAsInitialOperation(op);
     } else {
-      llvm::outs() << " REGULAR\n";
       nameAsRegularOperation(op, visited);
     }
   }
@@ -228,14 +230,14 @@ void NormalizePass::nameAsInitialOperation(mlir::Operation* op) {
     Name.append(callee.str());
   }
 
-  Name.append(" $ ");
+  Name.append("$");
   for (unsigned long i = 0; i < Operands.size(); ++i) {
     Name.append(std::string(Operands[i]));
 
     if (i < Operands.size() - 1)
-      Name.append(" -- ");
+      Name.append("--");
   }
-  Name.append(" $.$ ");
+  Name.append("$");
 
   mlir::OpBuilder b(op->getContext());
   mlir::StringAttr sat = b.getStringAttr(Name);
@@ -294,14 +296,71 @@ void NormalizePass::nameAsRegularOperation(mlir::Operation* op, llvm::SmallPtrSe
     Name.append(callee.str());
   }
 
-  Name.append(" $ ");
+  Name.append("$");
+  for (unsigned long i = 0; i < Operands.size(); ++i) {
+    Name.append(Operands[i]);
+
+    if (i < Operands.size() - 1)
+      Name.append("--");
+  }
+  Name.append("$");
+
+  mlir::OpBuilder b(op->getContext());
+  mlir::StringAttr sat = b.getStringAttr(Name);
+  mlir::Location newLoc = mlir::NameLoc::get(sat, op->getLoc());
+  op->setLoc(newLoc);
+}
+
+bool starts_with(std::string_view base, std::string_view check) {
+  if(base.size() < check.size()) return false;
+  for(int i = 0; i < check.size(); i++) if(base[i] != check[i]) return false;
+  return true;
+}
+
+void NormalizePass::foldOperation(mlir::Operation* op) {
+  if(isOutput(*op)) return;
+
+  std::string TextRepresentation;
+  mlir::AsmState state(op, flags);
+  llvm::raw_string_ostream Stream(TextRepresentation);
+  op->print(Stream, state);
+  
+  auto opName = split(Stream.str(), '=')[0];
+  if(!starts_with(opName, "%op")) return;
+
+  SmallVector<SmallString<64>, 4> Operands;
+
+  for (mlir::Value operand : op->getOperands()) {
+    if (mlir::Operation *defOp = operand.getDefiningOp()) {
+      std::string TextRepresentation;
+      mlir::AsmState state(defOp, flags);
+      llvm::raw_string_ostream Stream(TextRepresentation);
+      defOp->print(Stream, state);
+      auto name = split(Stream.str(), '=')[0];
+
+      bool hasNormalName = (starts_with(name, "%op") || starts_with(name, "%vl"));
+
+      if(hasNormalName) {
+        Operands.push_back(StringRef(name.substr(1, 7)));
+      } else {
+        Operands.push_back(StringRef(name));
+      }
+    }
+  }
+
+  if (op->hasTrait<OpTrait::IsCommutative>()) llvm::sort(Operands.begin(), Operands.end());
+
+  SmallString<512> Name;
+  Name.append(opName.substr(1, 7));
+
+  Name.append("$");
   for (unsigned long i = 0; i < Operands.size(); ++i) {
     Name.append(Operands[i]);
 
     if (i < Operands.size() - 1)
-      Name.append(" -- ");
+      Name.append("-");
   }
-  Name.append(" $.$ ");
+  Name.append("$");
 
   mlir::OpBuilder b(op->getContext());
   mlir::StringAttr sat = b.getStringAttr(Name);

>From c94e6e6f0f0c4b0f858ed121e4a861d6c27792b3 Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Tue, 7 Oct 2025 17:09:43 +0530
Subject: [PATCH 08/13] nit

---
 mlir/lib/Conversion/Normalize/Normalize.cpp | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/mlir/lib/Conversion/Normalize/Normalize.cpp b/mlir/lib/Conversion/Normalize/Normalize.cpp
index 0c0aebd3fd264..635eb38427eca 100644
--- a/mlir/lib/Conversion/Normalize/Normalize.cpp
+++ b/mlir/lib/Conversion/Normalize/Normalize.cpp
@@ -328,7 +328,7 @@ void NormalizePass::foldOperation(mlir::Operation* op) {
   auto opName = split(Stream.str(), '=')[0];
   if(!starts_with(opName, "%op")) return;
 
-  SmallVector<SmallString<64>, 4> Operands;
+  SmallVector<std::string, 4> Operands;
 
   for (mlir::Value operand : op->getOperands()) {
     if (mlir::Operation *defOp = operand.getDefiningOp()) {
@@ -341,9 +341,9 @@ void NormalizePass::foldOperation(mlir::Operation* op) {
       bool hasNormalName = (starts_with(name, "%op") || starts_with(name, "%vl"));
 
       if(hasNormalName) {
-        Operands.push_back(StringRef(name.substr(1, 7)));
+        Operands.push_back(name.substr(1, 7));
       } else {
-        Operands.push_back(StringRef(name));
+        Operands.push_back(name);
       }
     }
   }

>From 8c487ff64e72d371e1eb05183981073d9587cfe4 Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Tue, 7 Oct 2025 17:48:00 +0530
Subject: [PATCH 09/13] operand reordering in alphabetical order

---
 mlir/lib/Conversion/Normalize/Normalize.cpp | 41 +++++++++++++++------
 1 file changed, 29 insertions(+), 12 deletions(-)

diff --git a/mlir/lib/Conversion/Normalize/Normalize.cpp b/mlir/lib/Conversion/Normalize/Normalize.cpp
index 635eb38427eca..d5d7490870eaf 100644
--- a/mlir/lib/Conversion/Normalize/Normalize.cpp
+++ b/mlir/lib/Conversion/Normalize/Normalize.cpp
@@ -54,9 +54,9 @@ struct NormalizePass : public impl::NormalizeBase<NormalizePass> {
   void nameAsInitialOperation(mlir::Operation* op);
   void nameAsRegularOperation(mlir::Operation* op, llvm::SmallPtrSet<const mlir::Operation *, 32> &visited);
   bool hasOnlyImmediateOperands(mlir::Operation* op);
-  void SetDeterministicNames(Block &block);
   llvm::SetVector<int> getOutputFootprint(mlir::Operation* op, llvm::SmallPtrSet<const mlir::Operation *, 32> &visited);
   void foldOperation(mlir::Operation* op);
+  void reorderOperationOperandsByName(mlir::Operation* op);
   mlir::OpPrintingFlags flags{};
 };
 } // namespace
@@ -81,18 +81,11 @@ void NormalizePass::runOnOperation() {
       for (Block &block : region)
         for (Operation &innerOp : block)
           foldOperation(&innerOp);
-  }
-}
 
-void NormalizePass::SetDeterministicNames(Block &block) {
-  static size_t VarCounter = 0;
-
-  for (Operation &innerOp : block) {
-    mlir::OpBuilder b(innerOp.getContext());
-    mlir::StringAttr sat =
-        b.getStringAttr(llvm::formatv("v{0}", VarCounter++).str());
-    mlir::Location newLoc = mlir::NameLoc::get(sat, innerOp.getLoc());
-    innerOp.setLoc(newLoc);
+    for (Region& region : op.getRegions())
+      for (Block &block : region)
+        for (Operation &innerOp : block)
+          reorderOperationOperandsByName(&innerOp);
   }
 }
 
@@ -368,6 +361,30 @@ void NormalizePass::foldOperation(mlir::Operation* op) {
   op->setLoc(newLoc);
 }
 
+void NormalizePass::reorderOperationOperandsByName(mlir::Operation* op) {
+  if(op->getNumOperands() == 0) return;
+
+  SmallVector<std::pair<std::string, mlir::Value>, 4> Operands;
+
+  for (mlir::Value operand : op->getOperands()) {
+    std::string TextRepresentation;
+    llvm::raw_string_ostream Stream(TextRepresentation);
+    operand.printAsOperand(Stream, flags);
+    Operands.push_back({Stream.str(), operand});
+  }
+
+  if (op->hasTrait<OpTrait::IsCommutative>()) {
+    llvm::sort(Operands.begin(), Operands.end(),
+                [](const auto &a, const auto &b) {
+                  return llvm::StringRef(a.first).compare_insensitive(b.first) < 0;
+                });
+  }
+
+  for(size_t i = 0; i < Operands.size(); i++) {
+    op->setOperand(i, Operands[i].second);
+  }
+}
+
 void NormalizePass::reorderOperations(SmallVector<Operation *, 16> &Outputs) {
   llvm::SmallPtrSet<const mlir::Operation *, 32> visited;
   for (auto *op : Outputs)

>From 9c0e8669fe250faec17a177968b1631160ccabcb Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Fri, 10 Oct 2025 07:19:57 +0530
Subject: [PATCH 10/13] refactor impl

---
 mlir/lib/Conversion/Normalize/Normalize.cpp | 248 ++++++++++----------
 1 file changed, 129 insertions(+), 119 deletions(-)

diff --git a/mlir/lib/Conversion/Normalize/Normalize.cpp b/mlir/lib/Conversion/Normalize/Normalize.cpp
index d5d7490870eaf..98bb855cfd985 100644
--- a/mlir/lib/Conversion/Normalize/Normalize.cpp
+++ b/mlir/lib/Conversion/Normalize/Normalize.cpp
@@ -14,15 +14,15 @@
 #include "mlir/Dialect/LLVMIR/LLVMDialect.h"
 #include "mlir/Dialect/SCF/IR/SCF.h"
 #include "mlir/Dialect/Vector/Utils/VectorUtils.h"
+#include "mlir/IR/AsmState.h"
 #include "mlir/IR/TypeUtilities.h"
 #include "mlir/Pass/Pass.h"
+#include "llvm/ADT/Hashing.h"
 #include "llvm/Support/FormatVariadic.h"
 #include "llvm/Support/raw_ostream.h"
-#include "mlir/IR/AsmState.h"
-#include "llvm/ADT/Hashing.h"
 
-#include <sstream>
 #include <iomanip>
+#include <sstream>
 
 namespace mlir {
 #define GEN_PASS_DEF_NORMALIZE
@@ -41,22 +41,31 @@ struct NormalizePass : public impl::NormalizeBase<NormalizePass> {
 
 private:
   const uint64_t MagicHashConstant = 0x6acaa36bef8325c5ULL;
-  void collectOutputOperations(Block &block,
-                               SmallVector<Operation *, 16> &Output);
-  bool isOutput(mlir::Operation &op);
-  void reorderOperations(SmallVector<mlir::Operation *, 16> &Outputs);
+  void
+  collectOutputOperations(Block &block,
+                          SmallVector<Operation *, 16> &Output) const noexcept;
+  bool isOutput(mlir::Operation &op) const noexcept;
+
+  void reorderOperations(const SmallVector<mlir::Operation *, 16> &Outputs);
   void
   reorderOperation(mlir::Operation *used, mlir::Operation *user,
                    llvm::SmallPtrSet<const mlir::Operation *, 32> &visited);
-  void RenameOperations(SmallVector<Operation *, 16> &Outputs);
-  void RenameOperation(mlir::Operation* op, SmallPtrSet<const mlir::Operation *, 32> &visited);
-  bool isInitialOperation(mlir::Operation* op);
-  void nameAsInitialOperation(mlir::Operation* op);
-  void nameAsRegularOperation(mlir::Operation* op, llvm::SmallPtrSet<const mlir::Operation *, 32> &visited);
-  bool hasOnlyImmediateOperands(mlir::Operation* op);
-  llvm::SetVector<int> getOutputFootprint(mlir::Operation* op, llvm::SmallPtrSet<const mlir::Operation *, 32> &visited);
-  void foldOperation(mlir::Operation* op);
-  void reorderOperationOperandsByName(mlir::Operation* op);
+
+  void RenameOperations(const SmallVector<Operation *, 16> &Outputs);
+  void RenameOperation(mlir::Operation *op,
+                       SmallPtrSet<const mlir::Operation *, 32> &visited);
+
+  bool isInitialOperation(mlir::Operation *const op) const noexcept;
+  void nameAsInitialOperation(mlir::Operation *op);
+  void nameAsRegularOperation(
+      mlir::Operation *op,
+      llvm::SmallPtrSet<const mlir::Operation *, 32> &visited);
+  bool hasOnlyImmediateOperands(mlir::Operation *const op) const noexcept;
+  llvm::SetVector<int>
+  getOutputFootprint(mlir::Operation *op,
+                     llvm::SmallPtrSet<const mlir::Operation *, 32> &visited);
+  void foldOperation(mlir::Operation *op);
+  void reorderOperationOperandsByName(mlir::Operation *op);
   mlir::OpPrintingFlags flags{};
 };
 } // namespace
@@ -66,37 +75,38 @@ void NormalizePass::runOnOperation() {
 
   ModuleOp module = getOperation();
 
-  for (Operation &op : module.getOps()) {
+  for (auto &op : module.getOps()) {
     SmallVector<Operation *, 16> Outputs;
 
-    for (Region &region : op.getRegions())
-      for (Block &block : region)
+    for (auto &region : op.getRegions())
+      for (auto &block : region)
         collectOutputOperations(block, Outputs);
 
     reorderOperations(Outputs);
 
     RenameOperations(Outputs);
 
-    for (Region& region : op.getRegions())
-      for (Block &block : region)
-        for (Operation &innerOp : block)
+    for (auto &region : op.getRegions()) {
+      for (auto &block : region) {
+        for (auto &innerOp : block) {
           foldOperation(&innerOp);
-
-    for (Region& region : op.getRegions())
-      for (Block &block : region)
-        for (Operation &innerOp : block)
           reorderOperationOperandsByName(&innerOp);
+        }
+      }
+    }
   }
 }
 
-void NormalizePass::RenameOperations(SmallVector<Operation *, 16> &Outputs) {
+void NormalizePass::RenameOperations(
+    const SmallVector<Operation *, 16> &Outputs) {
   llvm::SmallPtrSet<const mlir::Operation *, 32> visited;
 
-  for(auto *op : Outputs)
+  for (auto *op : Outputs)
     RenameOperation(op, visited);
 }
 
-void NormalizePass::RenameOperation(Operation *op, SmallPtrSet<const mlir::Operation *, 32> &visited) {
+void NormalizePass::RenameOperation(
+    Operation *op, SmallPtrSet<const mlir::Operation *, 32> &visited) {
   if (!visited.count(op)) {
     visited.insert(op);
 
@@ -108,11 +118,13 @@ void NormalizePass::RenameOperation(Operation *op, SmallPtrSet<const mlir::Opera
   }
 }
 
-bool NormalizePass::isInitialOperation(mlir::Operation* op) {
+bool NormalizePass::isInitialOperation(
+    mlir::Operation *const op) const noexcept {
   return !op->use_empty() and hasOnlyImmediateOperands(op);
 }
 
-bool NormalizePass::hasOnlyImmediateOperands(mlir::Operation* op) {
+bool NormalizePass::hasOnlyImmediateOperands(
+    mlir::Operation *const op) const noexcept {
   for (mlir::Value operand : op->getOperands())
     if (mlir::Operation *defOp = operand.getDefiningOp())
       if (!(defOp->hasTrait<OpTrait::ConstantLike>()))
@@ -120,93 +132,80 @@ bool NormalizePass::hasOnlyImmediateOperands(mlir::Operation* op) {
   return true;
 }
 
-std::string to_string(uint64_t const hash)
-{
+std::string inline to_string(uint64_t const hash) noexcept {
   std::ostringstream oss;
-  oss << std::hex << std::setw(16) << std::setfill('0') << hash;
+  oss << std::hex << std::setw(5) << std::setfill('0') << hash;
   std::string tmp = oss.str();
-  return tmp.substr(11, 5);
-}
-
-uint64_t kernel_hash(std::string_view data)
-{
-    const uint64_t FNV_OFFSET = 0xcbf29ce484222325ULL;
-    const uint64_t FNV_PRIME = 0x100000001b3ULL;
-    uint64_t hash = FNV_OFFSET;
-    for (unsigned char c : data)
-    {
-        hash ^= static_cast<uint64_t>(c);
-        hash *= FNV_PRIME;
-    }
-    return hash;
+  return tmp.size() > 5 ? tmp.substr(tmp.size() - 5, 5) : tmp;
 }
 
-void replace(std::string& str, char from, char to) {
-  for(auto& it : str) {
-    if(it == from) it = to;
+uint64_t inline strHash(std::string_view data) noexcept {
+  const static uint64_t FNV_OFFSET = 0xcbf29ce484222325ULL;
+  const static uint64_t FNV_PRIME = 0x100000001b3ULL;
+  uint64_t hash = FNV_OFFSET;
+  for (const auto &c : data) {
+    hash ^= static_cast<uint64_t>(c);
+    hash *= FNV_PRIME;
   }
+  return hash;
 }
 
-std::vector<std::string> split(std::string_view str, char delimiter) {
-  std::vector<std::string> outs{};
-  std::stringstream ss{ std::string {str} };
+std::string inline split(std::string_view str, const char &delimiter,
+                         int indx = 0) noexcept {
+  std::stringstream ss{std::string{str}};
   std::string item;
-  while(std::getline(ss, item, delimiter)) {
-    replace(item, ':', '_');
-    outs.emplace_back(item);
+  int cnt = 0;
+  while (std::getline(ss, item, delimiter)) {
+    if (cnt == indx) {
+      std::replace(item.begin(), item.end(), ':', '_');
+      return item;
+    } else {
+      cnt++;
+    }
   }
-  return outs;
 }
 
-void NormalizePass::nameAsInitialOperation(mlir::Operation* op) {
+void NormalizePass::nameAsInitialOperation(mlir::Operation *op) {
   SmallVector<SmallString<64>, 4> Operands;
 
-  if(op->getNumOperands() == 0) {
-    /**
-     * INFO: Constant operations like arith.constant
-     */
-    if(auto call = mlir::dyn_cast<mlir::func::CallOp>(op)) {
+  if (op->getNumOperands() == 0) {
+    if (auto call = mlir::dyn_cast<mlir::func::CallOp>(op)) {
       Operands.push_back(StringRef(std::string{"void"}));
     } else {
       std::string TextRepresentation;
       mlir::AsmState state(op, flags);
       llvm::raw_string_ostream Stream(TextRepresentation);
       op->print(Stream, state);
-      std::string hash = to_string(kernel_hash(split(Stream.str(), '=')[1]));
+      std::string hash = to_string(strHash(split(Stream.str(), '=', 1)));
       Operands.push_back(StringRef(hash));
     }
   } else {
     for (mlir::Value operand : op->getOperands()) {
       if (mlir::Operation *defOp = operand.getDefiningOp()) {
-        /**
-         * INFO: Constant arguments like arith.constant
-         */
         std::string TextRepresentation;
         mlir::AsmState state(defOp, flags);
         llvm::raw_string_ostream Stream(TextRepresentation);
         defOp->print(Stream, state);
-        std::string hash = to_string(kernel_hash(split(Stream.str(), '=')[1]));
+        std::string hash = to_string(strHash(split(Stream.str(), '=', 1)));
         Operands.push_back(StringRef(hash));
       } else {
-        /**
-         * INFO: Function Arguments
-         */
         std::string TextRepresentation;
         mlir::AsmState state(op, flags);
         llvm::raw_string_ostream Stream(TextRepresentation);
         operand.print(Stream, state);
-        std::string argNum = split(Stream.str(), ':')[1];
+        std::string argNum = split(Stream.str(), ':', 1);
         argNum = argNum.substr(1, argNum.size() - 1);
         Operands.push_back(StringRef(std::string("arg" + argNum)));
       }
     }
   }
 
-  if (op->hasTrait<OpTrait::IsCommutative>()) llvm::sort(Operands);
+  if (op->hasTrait<OpTrait::IsCommutative>())
+    llvm::sort(Operands);
 
   uint64_t Hash = MagicHashConstant;
 
-  uint64_t opcodeHash = kernel_hash(op->getName().getStringRef().str());
+  uint64_t opcodeHash = strHash(op->getName().getStringRef().str());
   Hash = llvm::hashing::detail::hash_16_bytes(Hash, opcodeHash);
 
   SmallPtrSet<const mlir::Operation *, 32> Visited;
@@ -238,7 +237,9 @@ void NormalizePass::nameAsInitialOperation(mlir::Operation* op) {
   op->setLoc(newLoc);
 }
 
-void NormalizePass::nameAsRegularOperation(mlir::Operation* op, llvm::SmallPtrSet<const mlir::Operation *, 32> &visited) {
+void NormalizePass::nameAsRegularOperation(
+    mlir::Operation *op,
+    llvm::SmallPtrSet<const mlir::Operation *, 32> &visited) {
   SmallVector<SmallString<64>, 4> Operands;
   for (mlir::Value operand : op->getOperands()) {
     if (mlir::Operation *defOp = operand.getDefiningOp()) {
@@ -248,35 +249,34 @@ void NormalizePass::nameAsRegularOperation(mlir::Operation* op, llvm::SmallPtrSe
       mlir::AsmState state(defOp, flags);
       llvm::raw_string_ostream Stream(TextRepresentation);
       defOp->print(Stream, state);
-      Operands.push_back(StringRef(split(Stream.str(), '=')[0]));
+      Operands.push_back(StringRef(split(Stream.str(), '=', 0)));
     } else {
-      /**
-       * INFO: Function Arguments
-       */
       std::string TextRepresentation;
       mlir::AsmState state(op, flags);
       llvm::raw_string_ostream Stream(TextRepresentation);
       operand.print(Stream, state);
-      std::string argNum = split(Stream.str(), ':')[1];
+      std::string argNum = split(Stream.str(), ':', 1);
       argNum = argNum.substr(1, argNum.size() - 1);
       Operands.push_back(StringRef(std::string("arg" + argNum)));
     }
   }
 
-  if (op->hasTrait<OpTrait::IsCommutative>()) llvm::sort(Operands);
+  if (op->hasTrait<OpTrait::IsCommutative>())
+    llvm::sort(Operands);
 
   uint64_t Hash = MagicHashConstant;
 
-  uint64_t opcodeHash = kernel_hash(op->getName().getStringRef().str());
+  uint64_t opcodeHash = strHash(op->getName().getStringRef().str());
   Hash = llvm::hashing::detail::hash_16_bytes(Hash, opcodeHash);
 
   SmallVector<uint64_t, 4> OperandsOpcodes;
 
   for (mlir::Value operand : op->getOperands())
     if (mlir::Operation *defOp = operand.getDefiningOp())
-      OperandsOpcodes.push_back(kernel_hash(defOp->getName().getStringRef().str()));
+      OperandsOpcodes.push_back(strHash(defOp->getName().getStringRef().str()));
 
-  if (op->hasTrait<OpTrait::IsCommutative>()) llvm::sort(OperandsOpcodes.begin(), OperandsOpcodes.end());
+  if (op->hasTrait<OpTrait::IsCommutative>())
+    llvm::sort(OperandsOpcodes.begin(), OperandsOpcodes.end());
 
   for (const uint64_t Code : OperandsOpcodes)
     Hash = llvm::hashing::detail::hash_16_bytes(Hash, Code);
@@ -304,22 +304,24 @@ void NormalizePass::nameAsRegularOperation(mlir::Operation* op, llvm::SmallPtrSe
   op->setLoc(newLoc);
 }
 
-bool starts_with(std::string_view base, std::string_view check) {
-  if(base.size() < check.size()) return false;
-  for(int i = 0; i < check.size(); i++) if(base[i] != check[i]) return false;
-  return true;
+bool inline starts_with(std::string_view base,
+                        std::string_view check) noexcept {
+  return base.size() >= check.size() &&
+         std::equal(check.begin(), check.end(), base.begin());
 }
 
-void NormalizePass::foldOperation(mlir::Operation* op) {
-  if(isOutput(*op)) return;
+void NormalizePass::foldOperation(mlir::Operation *op) {
+  if (isOutput(*op))
+    return;
 
   std::string TextRepresentation;
   mlir::AsmState state(op, flags);
   llvm::raw_string_ostream Stream(TextRepresentation);
   op->print(Stream, state);
-  
-  auto opName = split(Stream.str(), '=')[0];
-  if(!starts_with(opName, "%op")) return;
+
+  auto opName = split(Stream.str(), '=', 0);
+  if (!starts_with(opName, "%op"))
+    return;
 
   SmallVector<std::string, 4> Operands;
 
@@ -329,11 +331,12 @@ void NormalizePass::foldOperation(mlir::Operation* op) {
       mlir::AsmState state(defOp, flags);
       llvm::raw_string_ostream Stream(TextRepresentation);
       defOp->print(Stream, state);
-      auto name = split(Stream.str(), '=')[0];
+      auto name = split(Stream.str(), '=', 0);
 
-      bool hasNormalName = (starts_with(name, "%op") || starts_with(name, "%vl"));
+      bool hasNormalName =
+          (starts_with(name, "%op") || starts_with(name, "%vl"));
 
-      if(hasNormalName) {
+      if (hasNormalName) {
         Operands.push_back(name.substr(1, 7));
       } else {
         Operands.push_back(name);
@@ -341,7 +344,8 @@ void NormalizePass::foldOperation(mlir::Operation* op) {
     }
   }
 
-  if (op->hasTrait<OpTrait::IsCommutative>()) llvm::sort(Operands.begin(), Operands.end());
+  if (op->hasTrait<OpTrait::IsCommutative>())
+    llvm::sort(Operands.begin(), Operands.end());
 
   SmallString<512> Name;
   Name.append(opName.substr(1, 7));
@@ -361,8 +365,9 @@ void NormalizePass::foldOperation(mlir::Operation* op) {
   op->setLoc(newLoc);
 }
 
-void NormalizePass::reorderOperationOperandsByName(mlir::Operation* op) {
-  if(op->getNumOperands() == 0) return;
+void NormalizePass::reorderOperationOperandsByName(mlir::Operation *op) {
+  if (op->getNumOperands() == 0)
+    return;
 
   SmallVector<std::pair<std::string, mlir::Value>, 4> Operands;
 
@@ -374,20 +379,21 @@ void NormalizePass::reorderOperationOperandsByName(mlir::Operation* op) {
   }
 
   if (op->hasTrait<OpTrait::IsCommutative>()) {
-    llvm::sort(Operands.begin(), Operands.end(),
-                [](const auto &a, const auto &b) {
-                  return llvm::StringRef(a.first).compare_insensitive(b.first) < 0;
-                });
+    llvm::sort(
+        Operands.begin(), Operands.end(), [](const auto &a, const auto &b) {
+          return llvm::StringRef(a.first).compare_insensitive(b.first) < 0;
+        });
   }
 
-  for(size_t i = 0; i < Operands.size(); i++) {
+  for (size_t i = 0; i < Operands.size(); i++) {
     op->setOperand(i, Operands[i].second);
   }
 }
 
-void NormalizePass::reorderOperations(SmallVector<Operation *, 16> &Outputs) {
+void NormalizePass::reorderOperations(
+    const SmallVector<Operation *, 16> &Outputs) {
   llvm::SmallPtrSet<const mlir::Operation *, 32> visited;
-  for (auto *op : Outputs)
+  for (auto *const op : Outputs)
     for (mlir::Value operand : op->getOperands())
       if (mlir::Operation *defOp = operand.getDefiningOp())
         reorderOperation(defOp, op, visited);
@@ -396,7 +402,6 @@ void NormalizePass::reorderOperations(SmallVector<Operation *, 16> &Outputs) {
 void NormalizePass::reorderOperation(
     mlir::Operation *used, mlir::Operation *user,
     llvm::SmallPtrSet<const mlir::Operation *, 32> &visited) {
-
   if (!visited.count(used)) {
     visited.insert(used);
 
@@ -414,13 +419,14 @@ void NormalizePass::reorderOperation(
   }
 }
 
-void NormalizePass::collectOutputOperations(Block &block, SmallVector<Operation *, 16> &Outputs) {
-  for (Operation &innerOp : block)
+void NormalizePass::collectOutputOperations(
+    Block &block, SmallVector<Operation *, 16> &Outputs) const noexcept {
+  for (auto &innerOp : block)
     if (isOutput(innerOp))
       Outputs.emplace_back(&innerOp);
 }
 
-bool NormalizePass::isOutput(Operation &op) {
+bool NormalizePass::isOutput(Operation &op) const noexcept {
   if (op.hasTrait<OpTrait::IsTerminator>())
     return true;
 
@@ -432,12 +438,15 @@ bool NormalizePass::isOutput(Operation &op) {
         return true;
   }
 
-  if (auto call = mlir::dyn_cast<mlir::func::CallOp>(op)) return true;
+  if (auto call = mlir::dyn_cast<mlir::func::CallOp>(op))
+    return true;
 
   return false;
 }
 
-llvm::SetVector<int> NormalizePass::getOutputFootprint(mlir::Operation* op, llvm::SmallPtrSet<const mlir::Operation *, 32> &visited) {
+llvm::SetVector<int> NormalizePass::getOutputFootprint(
+    mlir::Operation *op,
+    llvm::SmallPtrSet<const mlir::Operation *, 32> &visited) {
   llvm::SetVector<int> Outputs;
   if (!visited.count(op)) {
     visited.insert(op);
@@ -446,11 +455,11 @@ llvm::SetVector<int> NormalizePass::getOutputFootprint(mlir::Operation* op, llvm
       mlir::func::FuncOp func = op->getParentOfType<mlir::func::FuncOp>();
 
       unsigned Count = 0;
-        for (Block &block : func.getRegion())
-          for(mlir::Operation &innerOp : block){
-            if(&innerOp==op)
-              Outputs.insert(Count);
-            Count++;
+      for (Block &block : func.getRegion())
+        for (mlir::Operation &innerOp : block) {
+          if (&innerOp == op)
+            Outputs.insert(Count);
+          Count++;
         }
 
       return Outputs;
@@ -459,7 +468,8 @@ llvm::SetVector<int> NormalizePass::getOutputFootprint(mlir::Operation* op, llvm
     for (mlir::OpOperand &use : op->getUses()) {
       mlir::Operation *useOp = use.getOwner();
       if (useOp) {
-        llvm::SetVector<int> OutputsUsingUop = getOutputFootprint(useOp, visited);
+        llvm::SetVector<int> OutputsUsingUop =
+            getOutputFootprint(useOp, visited);
 
         Outputs.insert(OutputsUsingUop.begin(), OutputsUsingUop.end());
       }

>From 0836dad2d2e03defdd1cf9735414e4eef57ea18b Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Fri, 10 Oct 2025 07:28:49 +0530
Subject: [PATCH 11/13] clang-format

---
 mlir/include/mlir/Conversion/Passes.h       | 2 +-
 mlir/lib/Conversion/Normalize/Normalize.cpp | 8 ++++----
 2 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/mlir/include/mlir/Conversion/Passes.h b/mlir/include/mlir/Conversion/Passes.h
index 442ad55e77204..aa20e97bf31fe 100644
--- a/mlir/include/mlir/Conversion/Passes.h
+++ b/mlir/include/mlir/Conversion/Passes.h
@@ -52,9 +52,9 @@
 #include "mlir/Conversion/MemRefToEmitC/MemRefToEmitCPass.h"
 #include "mlir/Conversion/MemRefToLLVM/MemRefToLLVM.h"
 #include "mlir/Conversion/MemRefToSPIRV/MemRefToSPIRVPass.h"
-#include "mlir/Conversion/Normalize/Normalize.h"
 #include "mlir/Conversion/NVGPUToNVVM/NVGPUToNVVM.h"
 #include "mlir/Conversion/NVVMToLLVM/NVVMToLLVM.h"
+#include "mlir/Conversion/Normalize/Normalize.h"
 #include "mlir/Conversion/OpenACCToSCF/ConvertOpenACCToSCF.h"
 #include "mlir/Conversion/OpenMPToLLVM/ConvertOpenMPToLLVM.h"
 #include "mlir/Conversion/PDLToPDLInterp/PDLToPDLInterp.h"
diff --git a/mlir/lib/Conversion/Normalize/Normalize.cpp b/mlir/lib/Conversion/Normalize/Normalize.cpp
index 98bb855cfd985..c77fa850e70dd 100644
--- a/mlir/lib/Conversion/Normalize/Normalize.cpp
+++ b/mlir/lib/Conversion/Normalize/Normalize.cpp
@@ -61,9 +61,9 @@ struct NormalizePass : public impl::NormalizeBase<NormalizePass> {
       mlir::Operation *op,
       llvm::SmallPtrSet<const mlir::Operation *, 32> &visited);
   bool hasOnlyImmediateOperands(mlir::Operation *const op) const noexcept;
-  llvm::SetVector<int>
-  getOutputFootprint(mlir::Operation *op,
-                     llvm::SmallPtrSet<const mlir::Operation *, 32> &visited);
+  llvm::SetVector<int> getOutputFootprint(
+      mlir::Operation *op,
+      llvm::SmallPtrSet<const mlir::Operation *, 32> &visited) const;
   void foldOperation(mlir::Operation *op);
   void reorderOperationOperandsByName(mlir::Operation *op);
   mlir::OpPrintingFlags flags{};
@@ -446,7 +446,7 @@ bool NormalizePass::isOutput(Operation &op) const noexcept {
 
 llvm::SetVector<int> NormalizePass::getOutputFootprint(
     mlir::Operation *op,
-    llvm::SmallPtrSet<const mlir::Operation *, 32> &visited) {
+    llvm::SmallPtrSet<const mlir::Operation *, 32> &visited) const {
   llvm::SetVector<int> Outputs;
   if (!visited.count(op)) {
     visited.insert(op);

>From ad150397c7a64e0ce531ec07bb201db371a59508 Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Fri, 10 Oct 2025 07:36:29 +0530
Subject: [PATCH 12/13] linux build fix

---
 mlir/lib/Conversion/Normalize/Normalize.cpp | 1 +
 1 file changed, 1 insertion(+)

diff --git a/mlir/lib/Conversion/Normalize/Normalize.cpp b/mlir/lib/Conversion/Normalize/Normalize.cpp
index c77fa850e70dd..c6da1f45562a7 100644
--- a/mlir/lib/Conversion/Normalize/Normalize.cpp
+++ b/mlir/lib/Conversion/Normalize/Normalize.cpp
@@ -163,6 +163,7 @@ std::string inline split(std::string_view str, const char &delimiter,
       cnt++;
     }
   }
+  return nullptr;
 }
 
 void NormalizePass::nameAsInitialOperation(mlir::Operation *op) {

>From 1256ff17a01663d7c7ec2bc10b4a767373bf069f Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Fri, 10 Oct 2025 15:22:12 +0530
Subject: [PATCH 13/13] add reordering test

---
 mlir/test/Conversion/Normalize/reorder.mlir | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
 create mode 100644 mlir/test/Conversion/Normalize/reorder.mlir

diff --git a/mlir/test/Conversion/Normalize/reorder.mlir b/mlir/test/Conversion/Normalize/reorder.mlir
new file mode 100644
index 0000000000000..8872f69ff495b
--- /dev/null
+++ b/mlir/test/Conversion/Normalize/reorder.mlir
@@ -0,0 +1,21 @@
+// RUN: mlir-opt %s --normalize --mlir-use-nameloc-as-prefix 2>&1 | FileCheck %s
+
+// CHECK-LABEL:   func.func @bar(
+// CHECK-SAME:                   %[[ARG0:.*]]: i32) -> i32 {
+// CHECK:           %[[VAL_0:.*]] = arith.constant 2 : i32
+// CHECK:           %vl15831$51356--arg0$ = arith.addi %[[ARG0]], %[[VAL_0:.*]] : i32
+// CHECK:           %vl14084$187c2$ = arith.constant 6 : i32
+// CHECK:           %op27844$vl14084-vl15831$ = arith.addi %vl14084$187c2$, %vl15831$51356--arg0$ : i32
+// CHECK:           %vl14084$4c6ac$ = arith.constant 8 : i32
+// CHECK:           %op27844$op27844-vl14084$ = arith.addi %op27844$vl14084-vl15831$, %vl14084$4c6ac$ : i32
+// CHECK:           return %op27844$op27844-vl14084$ : i32
+// CHECK:         }
+func.func @bar(%a0: i32) -> i32 {
+  %c2 = arith.constant 2 : i32
+  %c6 = arith.constant 6 : i32
+  %c8 = arith.constant 8 : i32
+  %a = arith.addi %a0, %c2 : i32
+  %b = arith.addi %a, %c6 : i32
+  %c = arith.addi %b, %c8 : i32
+  func.return %c : i32
+}



More information about the llvm-commits mailing list