[llvm] [mlir] [mlir] Add Normalize pass (PR #162266)
Shourya Goel via llvm-commits
llvm-commits at lists.llvm.org
Tue Oct 28 10:12:47 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/31] 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/31] 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 ®ion : 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 ®ion : 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/31] 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 ®ion : 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 ®ion : 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/31] 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 ®ion : op.getRegions()) {
- for (Block &block : region) {
- collectOutputOperations(block, Outputs);
- }
- }
+ // for (Region ®ion : 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 ®ion : op.getRegions())
+ for (Block &block : region)
+ collectOutputOperations(block, Outputs);
- for (Region ®ion : 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/31] 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 ®ion : op.getRegions())
- // for (Block &block : region)
- // SetDeterministicNames(block);
-
SmallVector<Operation *, 16> Outputs;
for (Region ®ion : 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/31] 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/31] 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/31] 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/31] 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/31] 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 ®ion : op.getRegions())
- for (Block &block : region)
+ for (auto ®ion : 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 ®ion : 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/31] 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/31] 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/31] 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
+}
>From 89218e9e9d72c65f63a7a86e979752a20819a40a Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Fri, 10 Oct 2025 18:28:38 +0530
Subject: [PATCH 14/31] add infinite loop test and fix block/func arg naming
---
mlir/lib/Conversion/Normalize/Normalize.cpp | 62 +++---
.../Conversion/Normalize/infinite-loop.mlir | 202 ++++++++++++++++++
mlir/test/Conversion/Normalize/reorder.mlir | 4 +-
3 files changed, 235 insertions(+), 33 deletions(-)
create mode 100644 mlir/test/Conversion/Normalize/infinite-loop.mlir
diff --git a/mlir/lib/Conversion/Normalize/Normalize.cpp b/mlir/lib/Conversion/Normalize/Normalize.cpp
index c6da1f45562a7..31676bc4ec431 100644
--- a/mlir/lib/Conversion/Normalize/Normalize.cpp
+++ b/mlir/lib/Conversion/Normalize/Normalize.cpp
@@ -51,7 +51,7 @@ struct NormalizePass : public impl::NormalizeBase<NormalizePass> {
reorderOperation(mlir::Operation *used, mlir::Operation *user,
llvm::SmallPtrSet<const mlir::Operation *, 32> &visited);
- void RenameOperations(const SmallVector<Operation *, 16> &Outputs);
+ void renameOperations(const SmallVector<Operation *, 16> &Outputs);
void RenameOperation(mlir::Operation *op,
SmallPtrSet<const mlir::Operation *, 32> &visited);
@@ -83,21 +83,11 @@ void NormalizePass::runOnOperation() {
collectOutputOperations(block, Outputs);
reorderOperations(Outputs);
-
- RenameOperations(Outputs);
-
- for (auto ®ion : op.getRegions()) {
- for (auto &block : region) {
- for (auto &innerOp : block) {
- foldOperation(&innerOp);
- reorderOperationOperandsByName(&innerOp);
- }
- }
- }
+ renameOperations(Outputs);
}
}
-void NormalizePass::RenameOperations(
+void NormalizePass::renameOperations(
const SmallVector<Operation *, 16> &Outputs) {
llvm::SmallPtrSet<const mlir::Operation *, 32> visited;
@@ -115,6 +105,8 @@ void NormalizePass::RenameOperation(
} else {
nameAsRegularOperation(op, visited);
}
+ foldOperation(op);
+ reorderOperationOperandsByName(op);
}
}
@@ -189,14 +181,18 @@ void NormalizePass::nameAsInitialOperation(mlir::Operation *op) {
defOp->print(Stream, state);
std::string hash = to_string(strHash(split(Stream.str(), '=', 1)));
Operands.push_back(StringRef(hash));
- } else {
- 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);
- argNum = argNum.substr(1, argNum.size() - 1);
- Operands.push_back(StringRef(std::string("arg" + argNum)));
+ } else if (auto ba = dyn_cast<mlir::BlockArgument>(operand)) {
+ mlir::Block *ownerBlock = ba.getOwner();
+ unsigned argIndex = ba.getArgNumber();
+ if (auto func = dyn_cast<mlir::func::FuncOp>(ownerBlock->getParentOp())) {
+ if (&func.front() == ownerBlock) {
+ Operands.push_back(StringRef(std::string("funcArg" + std::to_string(argIndex))));
+ } else {
+ Operands.push_back(StringRef(std::string("blockArg" + std::to_string(argIndex))));
+ }
+ } else {
+ Operands.push_back(StringRef(std::string("blockArg" + std::to_string(argIndex))));
+ }
}
}
}
@@ -228,7 +224,7 @@ void NormalizePass::nameAsInitialOperation(mlir::Operation *op) {
Name.append(std::string(Operands[i]));
if (i < Operands.size() - 1)
- Name.append("--");
+ Name.append("-");
}
Name.append("$");
@@ -251,14 +247,18 @@ void NormalizePass::nameAsRegularOperation(
llvm::raw_string_ostream Stream(TextRepresentation);
defOp->print(Stream, state);
Operands.push_back(StringRef(split(Stream.str(), '=', 0)));
- } else {
- 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);
- argNum = argNum.substr(1, argNum.size() - 1);
- Operands.push_back(StringRef(std::string("arg" + argNum)));
+ } else if (auto ba = dyn_cast<mlir::BlockArgument>(operand)) {
+ mlir::Block *ownerBlock = ba.getOwner();
+ unsigned argIndex = ba.getArgNumber();
+ if (auto func = dyn_cast<mlir::func::FuncOp>(ownerBlock->getParentOp())) {
+ if (&func.front() == ownerBlock) {
+ Operands.push_back(StringRef(std::string("funcArg" + std::to_string(argIndex))));
+ } else {
+ Operands.push_back(StringRef(std::string("blockArg" + std::to_string(argIndex))));
+ }
+ } else {
+ Operands.push_back(StringRef(std::string("blockArg" + std::to_string(argIndex))));
+ }
}
}
@@ -295,7 +295,7 @@ void NormalizePass::nameAsRegularOperation(
Name.append(Operands[i]);
if (i < Operands.size() - 1)
- Name.append("--");
+ Name.append("-");
}
Name.append("$");
diff --git a/mlir/test/Conversion/Normalize/infinite-loop.mlir b/mlir/test/Conversion/Normalize/infinite-loop.mlir
new file mode 100644
index 0000000000000..969fbe4029e1b
--- /dev/null
+++ b/mlir/test/Conversion/Normalize/infinite-loop.mlir
@@ -0,0 +1,202 @@
+// RUN: mlir-opt %s --normalize --mlir-use-nameloc-as-prefix 2>&1 | FileCheck %s
+
+// CHECK-LABEL: module {
+// CHECK: func.func @test(%[[ARG0:.*]]: memref<?xi32>, %[[ARG1:.*]]: i32) {
+// CHECK: %[[VAL_0:.*]] = arith.constant 1 : i32
+// CHECK: %vl15390$e5677-funcArg1$ = arith.addi %[[ARG1:.*]], %[[VAL_0:.*]] : i32
+// CHECK: cf.br ^bb1(%vl15390$e5677-funcArg1$, %vl15390$e5677-funcArg1$ : i32, i32)
+// CHECK: ^bb1(%[[VAL_1:.*]]: i32, %[[VAL_2:.*]]: i32): // 2 preds: ^bb0, ^bb1
+// CHECK: %vl22288$20b04$ = arith.constant 0 : i32
+// CHECK: %vl13736$20b04-blockArg0$ = arith.muli %[[VAL_1:.*]], %vl22288$20b04$ : i32
+// CHECK: %vl22288$ded78$ = arith.constant -1 : i32
+// CHECK: %op51214$vl13736-vl22288$ = arith.xori %vl13736$20b04-blockArg0$, %vl22288$ded78$ : i32
+// CHECK: %op12693$op51214$ = arith.addi %[[VAL_1:.*]], %op51214$vl13736-vl22288$ : i32
+// CHECK: %vl15894$blockArg1-ded78$ = arith.addi %[[VAL_2:.*]], %vl22288$ded78$ : i32
+// CHECK: %op15672$op12693-vl15894$ = arith.addi %op12693$op51214$, %vl15894$blockArg1-ded78$ : i32
+// CHECK: %op97825$op15672-vl13736$ = arith.muli %op15672$op12693-vl15894$, %vl13736$20b04-blockArg0$ : i32
+// CHECK: %op51214$op97825-vl22288$ = arith.xori %op97825$op15672-vl13736$, %vl22288$ded78$ : i32
+// CHECK: %op12343$op15672-op51214$ = arith.addi %op15672$op12693-vl15894$, %op51214$op97825-vl22288$ : i32
+// CHECK: %op27844$op12343-vl22288$ = arith.addi %op12343$op15672-op51214$, %vl22288$20b04$ : i32
+// CHECK: %op97825$op27844-op97825$ = arith.muli %op27844$op12343-vl22288$, %op97825$op15672-vl13736$ : i32
+// CHECK: %op51214$op97825-vl22288$_0 = arith.xori %op97825$op27844-op97825$, %vl22288$ded78$ : i32
+// CHECK: %op12343$op27844-op51214$ = arith.addi %op27844$op12343-vl22288$, %op51214$op97825-vl22288$_0 : i32
+// CHECK: %op27844$op12343-vl22288$_1 = arith.addi %op12343$op27844-op51214$, %vl22288$20b04$ : i32
+// CHECK: %op27844$op27844-vl22288$ = arith.addi %op27844$op12343-vl22288$_1, %vl22288$20b04$ : i32
+// CHECK: %op97825$op27844-op97825$_3 = arith.muli %op27844$op12343-vl22288$_1, %op97825$op27844-op97825$ : i32
+// CHECK: %op97825$op27844-op97825$_4 = arith.muli %op27844$op27844-vl22288$_2, %op97825$op27844-op97825$_3 : i32
+// CHECK: %op51214$op97825-vl22288$_5 = arith.xori %op97825$op27844-op97825$_4, %vl22288$ded78$ : i32
+// CHECK: %op12343$op27844-op51214$_6 = arith.addi %op27844$op27844-vl22288$_2, %op51214$op97825-vl22288$_5 : i32
+// CHECK: %op27844$op12343-vl22288$_7 = arith.addi %op12343$op27844-op51214$_6, %vl22288$20b04$ : i32
+// CHECK: %op97825$op27844-op97825$_8 = arith.muli %op27844$op12343-vl22288$_7, %op97825$op27844-op97825$_4 : i32
+// CHECK: %op51214$op97825-vl22288$_9 = arith.xori %op97825$op27844-op97825$_8, %vl22288$ded78$ : i32
+// CHECK: %op12343$op27844-op51214$_10 = arith.addi %op27844$op12343-vl22288$_7, %op51214$op97825-vl22288$_9 : i32
+// CHECK: %op27844$op12343-vl22288$_11 = arith.addi %op12343$op27844-op51214$_10, %vl22288$20b04$ : i32
+// CHECK: %op97825$op27844-op97825$_12 = arith.muli %op27844$op12343-vl22288$_11, %op97825$op27844-op97825$_8 : i32
+// CHECK: %op51214$op97825-vl22288$_13 = arith.xori %op97825$op27844-op97825$_12, %vl22288$ded78$ : i32
+// CHECK: %op12343$op27844-op51214$_14 = arith.addi %op27844$op12343-vl22288$_11, %op51214$op97825-vl22288$_13 : i32
+// CHECK: %op27844$op12343-vl22288$_15 = arith.addi %op12343$op27844-op51214$_14, %vl22288$20b04$ : i32
+// CHECK: %op97825$op27844-op97825$_16 = arith.muli %op27844$op12343-vl22288$_15, %op97825$op27844-op97825$_12 : i32
+// CHECK: %op51214$op97825-vl22288$_17 = arith.xori %op97825$op27844-op97825$_16, %vl22288$ded78$ : i32
+// CHECK: %op12343$op27844-op51214$_18 = arith.addi %op27844$op12343-vl22288$_15, %op51214$op97825-vl22288$_17 : i32
+// CHECK: %op27844$op12343-vl22288$_19 = arith.addi %op12343$op27844-op51214$_18, %vl22288$20b04$ : i32
+// CHECK: %op97825$op27844-op97825$_20 = arith.muli %op27844$op12343-vl22288$_19, %op97825$op27844-op97825$_16 : i32
+// CHECK: %op51214$op97825-vl22288$_21 = arith.xori %op97825$op27844-op97825$_20, %vl22288$ded78$ : i32
+// CHECK: %op12343$op27844-op51214$_22 = arith.addi %op27844$op12343-vl22288$_19, %op51214$op97825-vl22288$_21 : i32
+// CHECK: %[[VAL_3:.*]] = arith.constant -9 : i32
+// CHECK: %vl15894$51850-blockArg1$ = arith.addi %[[VAL_2:.*]], %[[VAL_3:.*]] : i32
+// CHECK: %op15672$op12343-vl15894$ = arith.addi %op12343$op27844-op51214$_22, %vl15894$51850-blockArg1$ : i32
+// CHECK: %op97825$op15672-op97825$ = arith.muli %op15672$op12343-vl15894$, %op97825$op27844-op97825$_20 : i32
+// CHECK: %op51214$op97825-vl22288$_23 = arith.xori %op97825$op15672-op97825$, %vl22288$ded78$ : i32
+// CHECK: %op12343$op15672-op51214$_24 = arith.addi %op15672$op12343-vl15894$, %op51214$op97825-vl22288$_23 : i32
+// CHECK: %op27844$op12343-vl22288$_25 = arith.addi %op12343$op15672-op51214$_24, %vl22288$20b04$ : i32
+// CHECK: %op97825$op27844-op97825$_26 = arith.muli %op27844$op12343-vl22288$_25, %op97825$op15672-op97825$ : i32
+// CHECK: %op51214$op97825-vl22288$_27 = arith.xori %op97825$op27844-op97825$_26, %vl22288$ded78$ : i32
+// CHECK: %op12343$op27844-op51214$_28 = arith.addi %op27844$op12343-vl22288$_25, %op51214$op97825-vl22288$_27 : i32
+// CHECK: %op27844$op12343-vl22288$_29 = arith.addi %op12343$op27844-op51214$_28, %vl22288$20b04$ : i32
+// CHECK: %op97825$op27844-op97825$_30 = arith.muli %op27844$op12343-vl22288$_29, %op97825$op27844-op97825$_26 : i32
+// CHECK: %op51214$op97825-vl22288$_31 = arith.xori %op97825$op27844-op97825$_30, %vl22288$ded78$ : i32
+// CHECK: %op12343$op27844-op51214$_32 = arith.addi %op27844$op12343-vl22288$_29, %op51214$op97825-vl22288$_31 : i32
+// CHECK: %op27844$op12343-vl22288$_33 = arith.addi %op12343$op27844-op51214$_32, %vl22288$20b04$ : i32
+// CHECK: %op97825$op27844-op97825$_34 = arith.muli %op27844$op12343-vl22288$_33, %op97825$op27844-op97825$_30 : i32
+// CHECK: %op51214$op97825-vl22288$_35 = arith.xori %op97825$op27844-op97825$_34, %vl22288$ded78$ : i32
+// CHECK: %op12343$op27844-op51214$_36 = arith.addi %op27844$op12343-vl22288$_33, %op51214$op97825-vl22288$_35 : i32
+// CHECK: %op27844$op12343-vl22288$_37 = arith.addi %op12343$op27844-op51214$_36, %vl22288$20b04$ : i32
+// CHECK: %op97825$op27844-op97825$_38 = arith.muli %op27844$op12343-vl22288$_37, %op97825$op27844-op97825$_34 : i32
+// CHECK: %op51214$op97825-vl22288$_39 = arith.xori %op97825$op27844-op97825$_38, %vl22288$ded78$ : i32
+// CHECK: %op12343$op27844-op51214$_40 = arith.addi %op27844$op12343-vl22288$_37, %op51214$op97825-vl22288$_39 : i32
+// CHECK: %[[VAL_4:.*]] = arith.constant -14 : i32
+// CHECK: %vl15894$7b7de-blockArg1$ = arith.addi %[[VAL_2:.*]], %[[VAL_4:.*]] : i32
+// CHECK: %op15672$op12343-vl15894$_41 = arith.addi %op12343$op27844-op51214$_40, %vl15894$7b7de-blockArg1$ : i32
+// CHECK: %op97825$op15672-op97825$_42 = arith.muli %op15672$op12343-vl15894$_41, %op97825$op27844-op97825$_38 : i32
+// CHECK: %op51214$op97825-vl22288$_43 = arith.xori %op97825$op15672-op97825$_42, %vl22288$ded78$ : i32
+// CHECK: %op12343$op15672-op51214$_44 = arith.addi %op15672$op12343-vl15894$_41, %op51214$op97825-vl22288$_43 : i32
+// CHECK: %op27844$op12343-vl22288$_45 = arith.addi %op12343$op15672-op51214$_44, %vl22288$20b04$ : i32
+// CHECK: %op97825$op27844-op97825$_46 = arith.muli %op27844$op12343-vl22288$_45, %op97825$op15672-op97825$_42 : i32
+// CHECK: %op51214$op97825-vl22288$_47 = arith.xori %op97825$op27844-op97825$_46, %vl22288$ded78$ : i32
+// CHECK: %op12343$op27844-op51214$_48 = arith.addi %op27844$op12343-vl22288$_45, %op51214$op97825-vl22288$_47 : i32
+// CHECK: %op27844$op12343-vl22288$_49 = arith.addi %op12343$op27844-op51214$_48, %vl22288$20b04$ : i32
+// CHECK: %op97825$op27844-op97825$_50 = arith.muli %op27844$op12343-vl22288$_49, %op97825$op27844-op97825$_46 : i32
+// CHECK: %op51214$op97825-vl22288$_51 = arith.xori %op97825$op27844-op97825$_50, %vl22288$ded78$ : i32
+// CHECK: %op12343$op27844-op51214$_52 = arith.addi %op27844$op12343-vl22288$_49, %op51214$op97825-vl22288$_51 : i32
+// CHECK: %op27844$op12343-vl22288$_53 = arith.addi %op12343$op27844-op51214$_52, %vl22288$20b04$ : i32
+// CHECK: %op97825$op27844-op97825$_54 = arith.muli %op27844$op12343-vl22288$_53, %op97825$op27844-op97825$_50 : i32
+// CHECK: %op51214$op97825-vl22288$_55 = arith.xori %op97825$op27844-op97825$_54, %vl22288$ded78$ : i32
+// CHECK: %op12343$op27844-op51214$_56 = arith.addi %op27844$op12343-vl22288$_53, %op51214$op97825-vl22288$_55 : i32
+// CHECK: %op27844$op12343-vl22288$_57 = arith.addi %op12343$op27844-op51214$_56, %vl22288$20b04$ : i32
+// CHECK: %op97825$op27844-op97825$_58 = arith.muli %op27844$op12343-vl22288$_57, %op97825$op27844-op97825$_54 : i32
+// CHECK: %op51214$op97825-vl22288$_59 = arith.xori %op97825$op27844-op97825$_58, %vl22288$ded78$ : i32
+// CHECK: %op12343$op27844-op51214$_60 = arith.addi %op27844$op12343-vl22288$_57, %op51214$op97825-vl22288$_59 : i32
+// CHECK: %op27844$op12343-vl22288$_61 = arith.addi %op12343$op27844-op51214$_60, %vl22288$20b04$ : i32
+// CHECK: %op97825$op27844-op97825$_62 = arith.muli %op27844$op12343-vl22288$_61, %op97825$op27844-op97825$_58 : i32
+// CHECK: %op51214$op97825-vl22288$_63 = arith.xori %op97825$op27844-op97825$_62, %vl22288$ded78$ : i32
+// CHECK: %op12343$op27844-op51214$_64 = arith.addi %op27844$op12343-vl22288$_61, %op51214$op97825-vl22288$_63 : i32
+// CHECK: %op27844$op12343-vl22288$_65 = arith.addi %op12343$op27844-op51214$_64, %vl22288$20b04$ : i32
+// CHECK: %op27844$op27844-vl22288$_66 = arith.addi %op27844$op12343-vl22288$_65, %vl22288$20b04$ : i32
+// CHECK: %[[VAL_5:.*]] = arith.constant -21 : i32
+// CHECK: %vl15894$1e72e-blockArg1$ = arith.addi %[[VAL_2:.*]], %[[VAL_5:.*]] : i32
+// CHECK: %op15672$op27844-vl15894$ = arith.addi %op27844$op27844-vl22288$_66, %vl15894$1e72e-blockArg1$ : i32
+// CHECK: %vl48856$e527e$ = arith.constant 0 : index
+// CHECK: memref.store %op15672$op27844-vl15894$, %[[ARG0:.*]][%vl48856$e527e$] : memref<?xi32>
+// CHECK: cf.br ^bb1(%op15672$op27844-vl15894$, %vl15894$1e72e-blockArg1$ : i32, i32)
+// CHECK: }
+// CHECK: }
+func.func @test(%arg0: memref<?xi32>, %arg1: i32) {
+ %c1 = arith.constant 1 : i32
+ %a = arith.addi %arg1, %c1 : i32
+ cf.br ^bb1(%a, %a : i32, i32)
+
+ ^bb1(%tmp: i32, %tmp2: i32):
+ %c0 = arith.constant 0 : i32
+ %tmp3 = arith.muli %tmp, %c0 : i32
+ %cneg1 = arith.constant -1 : i32
+ %tmp4 = arith.xori %tmp3, %cneg1 : i32
+ %tmp5 = arith.addi %tmp, %tmp4 : i32
+ %tmp6 = arith.addi %tmp2, %cneg1 : i32
+ %tmp7 = arith.addi %tmp5, %tmp6 : i32
+ %tmp8 = arith.muli %tmp7, %tmp3 : i32
+ %tmp9 = arith.xori %tmp8, %cneg1 : i32
+ %tmp10 = arith.addi %tmp7, %tmp9 : i32
+ %tmp11 = arith.addi %tmp10, %c0 : i32
+ %tmp12 = arith.muli %tmp11, %tmp8 : i32
+ %tmp13 = arith.xori %tmp12, %cneg1 : i32
+ %tmp14 = arith.addi %tmp11, %tmp13 : i32
+ %tmp15 = arith.addi %tmp14, %c0 : i32
+ %tmp16 = arith.muli %tmp15, %tmp12 : i32
+ %tmp17 = arith.addi %tmp15, %c0 : i32
+ %tmp18 = arith.addi %tmp17, %c0 : i32
+ %tmp19 = arith.muli %tmp18, %tmp16 : i32
+ %tmp20 = arith.xori %tmp19, %cneg1 : i32
+ %tmp21 = arith.addi %tmp18, %tmp20 : i32
+ %tmp22 = arith.addi %tmp21, %c0 : i32
+ %tmp23 = arith.muli %tmp22, %tmp19 : i32
+ %tmp24 = arith.xori %tmp23, %cneg1 : i32
+ %tmp25 = arith.addi %tmp22, %tmp24 : i32
+ %tmp26 = arith.addi %tmp25, %c0 : i32
+ %tmp27 = arith.muli %tmp26, %tmp23 : i32
+ %tmp28 = arith.xori %tmp27, %cneg1 : i32
+ %tmp29 = arith.addi %tmp26, %tmp28 : i32
+ %tmp30 = arith.addi %tmp29, %c0 : i32
+ %tmp31 = arith.muli %tmp30, %tmp27 : i32
+ %tmp32 = arith.xori %tmp31, %cneg1 : i32
+ %tmp33 = arith.addi %tmp30, %tmp32 : i32
+ %tmp34 = arith.addi %tmp33, %c0 : i32
+ %tmp35 = arith.muli %tmp34, %tmp31 : i32
+ %tmp36 = arith.xori %tmp35, %cneg1 : i32
+ %tmp37 = arith.addi %tmp34, %tmp36 : i32
+ %cneg9 = arith.constant -9 : i32
+ %tmp38 = arith.addi %tmp2, %cneg9 : i32
+ %tmp39 = arith.addi %tmp37, %tmp38 : i32
+ %tmp40 = arith.muli %tmp39, %tmp35 : i32
+ %tmp41 = arith.xori %tmp40, %cneg1 : i32
+ %tmp42 = arith.addi %tmp39, %tmp41 : i32
+ %tmp43 = arith.addi %tmp42, %c0 : i32
+ %tmp44 = arith.muli %tmp43, %tmp40 : i32
+ %tmp45 = arith.xori %tmp44, %cneg1 : i32
+ %tmp46 = arith.addi %tmp43, %tmp45 : i32
+ %tmp47 = arith.addi %tmp46, %c0 : i32
+ %tmp48 = arith.muli %tmp47, %tmp44 : i32
+ %tmp49 = arith.xori %tmp48, %cneg1 : i32
+ %tmp50 = arith.addi %tmp47, %tmp49 : i32
+ %tmp51 = arith.addi %tmp50, %c0 : i32
+ %tmp52 = arith.muli %tmp51, %tmp48 : i32
+ %tmp53 = arith.xori %tmp52, %cneg1 : i32
+ %tmp54 = arith.addi %tmp51, %tmp53 : i32
+ %tmp55 = arith.addi %tmp54, %c0 : i32
+ %tmp56 = arith.muli %tmp55, %tmp52 : i32
+ %tmp57 = arith.xori %tmp56, %cneg1 : i32
+ %tmp58 = arith.addi %tmp55, %tmp57 : i32
+ %cneg14 = arith.constant -14 : i32
+ %tmp59 = arith.addi %tmp2, %cneg14 : i32
+ %tmp60 = arith.addi %tmp58, %tmp59 : i32
+ %tmp61 = arith.muli %tmp60, %tmp56 : i32
+ %tmp62 = arith.xori %tmp61, %cneg1 : i32
+ %tmp63 = arith.addi %tmp60, %tmp62 : i32
+ %tmp64 = arith.addi %tmp63, %c0 : i32
+ %tmp65 = arith.muli %tmp64, %tmp61 : i32
+ %tmp66 = arith.xori %tmp65, %cneg1 : i32
+ %tmp67 = arith.addi %tmp64, %tmp66 : i32
+ %tmp68 = arith.addi %tmp67, %c0 : i32
+ %tmp69 = arith.muli %tmp68, %tmp65 : i32
+ %tmp70 = arith.xori %tmp69, %cneg1 : i32
+ %tmp71 = arith.addi %tmp68, %tmp70 : i32
+ %tmp72 = arith.addi %tmp71, %c0 : i32
+ %tmp73 = arith.muli %tmp72, %tmp69 : i32
+ %tmp74 = arith.xori %tmp73, %cneg1 : i32
+ %tmp75 = arith.addi %tmp72, %tmp74 : i32
+ %tmp76 = arith.addi %tmp75, %c0 : i32
+ %tmp77 = arith.muli %tmp76, %tmp73 : i32
+ %tmp78 = arith.xori %tmp77, %cneg1 : i32
+ %tmp79 = arith.addi %tmp76, %tmp78 : i32
+ %tmp80 = arith.addi %tmp79, %c0 : i32
+ %tmp81 = arith.muli %tmp80, %tmp77 : i32
+ %tmp82 = arith.xori %tmp81, %cneg1 : i32
+ %tmp83 = arith.addi %tmp80, %tmp82 : i32
+ %tmp84 = arith.addi %tmp83, %c0 : i32
+ %tmp85 = arith.addi %tmp84, %c0 : i32
+ %cneg21 = arith.constant -21 : i32
+ %tmp86 = arith.addi %tmp2, %cneg21 : i32
+ %tmp87 = arith.addi %tmp85, %tmp86 : i32
+ %c0_idx = arith.constant 0 : index
+ memref.store %tmp87, %arg0[%c0_idx] : memref<?xi32>
+ cf.br ^bb1(%tmp87, %tmp86 : i32, i32)
+}
diff --git a/mlir/test/Conversion/Normalize/reorder.mlir b/mlir/test/Conversion/Normalize/reorder.mlir
index 8872f69ff495b..59c46a7834d32 100644
--- a/mlir/test/Conversion/Normalize/reorder.mlir
+++ b/mlir/test/Conversion/Normalize/reorder.mlir
@@ -3,9 +3,9 @@
// 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: %vl15831$51356-funcArg0$ = 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: %op27844$vl14084-vl15831$ = arith.addi %vl14084$187c2$, %vl15831$51356-funcArg0$ : 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
>From f4d0c63eedd07336834de05f2947ac3f9b78bc55 Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Fri, 10 Oct 2025 18:42:12 +0530
Subject: [PATCH 15/31] rename constants when used in an initial instruction
---
mlir/lib/Conversion/Normalize/Normalize.cpp | 36 ++++++++++++++-------
1 file changed, 24 insertions(+), 12 deletions(-)
diff --git a/mlir/lib/Conversion/Normalize/Normalize.cpp b/mlir/lib/Conversion/Normalize/Normalize.cpp
index 31676bc4ec431..1ec77c6e61399 100644
--- a/mlir/lib/Conversion/Normalize/Normalize.cpp
+++ b/mlir/lib/Conversion/Normalize/Normalize.cpp
@@ -56,7 +56,9 @@ struct NormalizePass : public impl::NormalizeBase<NormalizePass> {
SmallPtrSet<const mlir::Operation *, 32> &visited);
bool isInitialOperation(mlir::Operation *const op) const noexcept;
- void nameAsInitialOperation(mlir::Operation *op);
+ void nameAsInitialOperation(
+ mlir::Operation *op,
+ llvm::SmallPtrSet<const mlir::Operation *, 32> &visited);
void nameAsRegularOperation(
mlir::Operation *op,
llvm::SmallPtrSet<const mlir::Operation *, 32> &visited);
@@ -101,7 +103,7 @@ void NormalizePass::RenameOperation(
visited.insert(op);
if (isInitialOperation(op)) {
- nameAsInitialOperation(op);
+ nameAsInitialOperation(op, visited);
} else {
nameAsRegularOperation(op, visited);
}
@@ -158,7 +160,9 @@ std::string inline split(std::string_view str, const char &delimiter,
return nullptr;
}
-void NormalizePass::nameAsInitialOperation(mlir::Operation *op) {
+void NormalizePass::nameAsInitialOperation(
+ mlir::Operation *op,
+ llvm::SmallPtrSet<const mlir::Operation *, 32> &visited) {
SmallVector<SmallString<64>, 4> Operands;
if (op->getNumOperands() == 0) {
@@ -175,23 +179,28 @@ void NormalizePass::nameAsInitialOperation(mlir::Operation *op) {
} else {
for (mlir::Value operand : op->getOperands()) {
if (mlir::Operation *defOp = operand.getDefiningOp()) {
+ RenameOperation(defOp, visited);
+
std::string TextRepresentation;
mlir::AsmState state(defOp, flags);
llvm::raw_string_ostream Stream(TextRepresentation);
defOp->print(Stream, state);
- std::string hash = to_string(strHash(split(Stream.str(), '=', 1)));
- Operands.push_back(StringRef(hash));
+ Operands.push_back(StringRef(split(Stream.str(), '=', 0)));
} else if (auto ba = dyn_cast<mlir::BlockArgument>(operand)) {
mlir::Block *ownerBlock = ba.getOwner();
unsigned argIndex = ba.getArgNumber();
- if (auto func = dyn_cast<mlir::func::FuncOp>(ownerBlock->getParentOp())) {
+ if (auto func =
+ dyn_cast<mlir::func::FuncOp>(ownerBlock->getParentOp())) {
if (&func.front() == ownerBlock) {
- Operands.push_back(StringRef(std::string("funcArg" + std::to_string(argIndex))));
+ Operands.push_back(
+ StringRef(std::string("funcArg" + std::to_string(argIndex))));
} else {
- Operands.push_back(StringRef(std::string("blockArg" + std::to_string(argIndex))));
+ Operands.push_back(
+ StringRef(std::string("blockArg" + std::to_string(argIndex))));
}
} else {
- Operands.push_back(StringRef(std::string("blockArg" + std::to_string(argIndex))));
+ Operands.push_back(
+ StringRef(std::string("blockArg" + std::to_string(argIndex))));
}
}
}
@@ -252,12 +261,15 @@ void NormalizePass::nameAsRegularOperation(
unsigned argIndex = ba.getArgNumber();
if (auto func = dyn_cast<mlir::func::FuncOp>(ownerBlock->getParentOp())) {
if (&func.front() == ownerBlock) {
- Operands.push_back(StringRef(std::string("funcArg" + std::to_string(argIndex))));
+ Operands.push_back(
+ StringRef(std::string("funcArg" + std::to_string(argIndex))));
} else {
- Operands.push_back(StringRef(std::string("blockArg" + std::to_string(argIndex))));
+ Operands.push_back(
+ StringRef(std::string("blockArg" + std::to_string(argIndex))));
}
} else {
- Operands.push_back(StringRef(std::string("blockArg" + std::to_string(argIndex))));
+ Operands.push_back(
+ StringRef(std::string("blockArg" + std::to_string(argIndex))));
}
}
}
>From 9ed096df4392d1eefe2fd355301fcd9cfc4e5772 Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Fri, 10 Oct 2025 19:11:09 +0530
Subject: [PATCH 16/31] refactor repeated logic into foldOperations
---
mlir/lib/Conversion/Normalize/Normalize.cpp | 127 ++++++--------------
mlir/test/Conversion/Normalize/reorder.mlir | 6 +-
2 files changed, 38 insertions(+), 95 deletions(-)
diff --git a/mlir/lib/Conversion/Normalize/Normalize.cpp b/mlir/lib/Conversion/Normalize/Normalize.cpp
index 1ec77c6e61399..c803c158073ba 100644
--- a/mlir/lib/Conversion/Normalize/Normalize.cpp
+++ b/mlir/lib/Conversion/Normalize/Normalize.cpp
@@ -163,51 +163,10 @@ std::string inline split(std::string_view str, const char &delimiter,
void NormalizePass::nameAsInitialOperation(
mlir::Operation *op,
llvm::SmallPtrSet<const mlir::Operation *, 32> &visited) {
- SmallVector<SmallString<64>, 4> Operands;
- 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(strHash(split(Stream.str(), '=', 1)));
- Operands.push_back(StringRef(hash));
- }
- } else {
- for (mlir::Value operand : op->getOperands()) {
- if (mlir::Operation *defOp = operand.getDefiningOp()) {
- RenameOperation(defOp, visited);
-
- std::string TextRepresentation;
- mlir::AsmState state(defOp, flags);
- llvm::raw_string_ostream Stream(TextRepresentation);
- defOp->print(Stream, state);
- Operands.push_back(StringRef(split(Stream.str(), '=', 0)));
- } else if (auto ba = dyn_cast<mlir::BlockArgument>(operand)) {
- mlir::Block *ownerBlock = ba.getOwner();
- unsigned argIndex = ba.getArgNumber();
- if (auto func =
- dyn_cast<mlir::func::FuncOp>(ownerBlock->getParentOp())) {
- if (&func.front() == ownerBlock) {
- Operands.push_back(
- StringRef(std::string("funcArg" + std::to_string(argIndex))));
- } else {
- Operands.push_back(
- StringRef(std::string("blockArg" + std::to_string(argIndex))));
- }
- } else {
- Operands.push_back(
- StringRef(std::string("blockArg" + std::to_string(argIndex))));
- }
- }
- }
- }
-
- if (op->hasTrait<OpTrait::IsCommutative>())
- llvm::sort(Operands);
+ for (mlir::Value operand : op->getOperands())
+ if (mlir::Operation *defOp = operand.getDefiningOp())
+ RenameOperation(defOp, visited);
uint64_t Hash = MagicHashConstant;
@@ -228,14 +187,20 @@ void NormalizePass::nameAsInitialOperation(
Name.append(callee.str());
}
- Name.append("$");
- for (unsigned long i = 0; i < Operands.size(); ++i) {
- Name.append(std::string(Operands[i]));
-
- if (i < Operands.size() - 1)
- Name.append("-");
+ if (op->getNumOperands() == 0) {
+ Name.append("$");
+ if (auto call = mlir::dyn_cast<mlir::func::CallOp>(op)) {
+ Name.append("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(strHash(split(Stream.str(), '=', 1)));
+ Name.append(hash);
+ }
+ Name.append("$");
}
- Name.append("$");
mlir::OpBuilder b(op->getContext());
mlir::StringAttr sat = b.getStringAttr(Name);
@@ -246,36 +211,10 @@ void NormalizePass::nameAsInitialOperation(
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, flags);
- llvm::raw_string_ostream Stream(TextRepresentation);
- defOp->print(Stream, state);
- Operands.push_back(StringRef(split(Stream.str(), '=', 0)));
- } else if (auto ba = dyn_cast<mlir::BlockArgument>(operand)) {
- mlir::Block *ownerBlock = ba.getOwner();
- unsigned argIndex = ba.getArgNumber();
- if (auto func = dyn_cast<mlir::func::FuncOp>(ownerBlock->getParentOp())) {
- if (&func.front() == ownerBlock) {
- Operands.push_back(
- StringRef(std::string("funcArg" + std::to_string(argIndex))));
- } else {
- Operands.push_back(
- StringRef(std::string("blockArg" + std::to_string(argIndex))));
- }
- } else {
- Operands.push_back(
- StringRef(std::string("blockArg" + std::to_string(argIndex))));
- }
- }
- }
- if (op->hasTrait<OpTrait::IsCommutative>())
- llvm::sort(Operands);
+ for (mlir::Value operand : op->getOperands())
+ if (mlir::Operation *defOp = operand.getDefiningOp())
+ RenameOperation(defOp, visited);
uint64_t Hash = MagicHashConstant;
@@ -302,15 +241,6 @@ void NormalizePass::nameAsRegularOperation(
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());
@@ -324,7 +254,7 @@ bool inline starts_with(std::string_view base,
}
void NormalizePass::foldOperation(mlir::Operation *op) {
- if (isOutput(*op))
+ if (isOutput(*op) || op->getNumOperands() == 0)
return;
std::string TextRepresentation;
@@ -333,7 +263,7 @@ void NormalizePass::foldOperation(mlir::Operation *op) {
op->print(Stream, state);
auto opName = split(Stream.str(), '=', 0);
- if (!starts_with(opName, "%op"))
+ if (!starts_with(opName, "%op") && !starts_with(opName, "%vl"))
return;
SmallVector<std::string, 4> Operands;
@@ -354,7 +284,20 @@ void NormalizePass::foldOperation(mlir::Operation *op) {
} else {
Operands.push_back(name);
}
- }
+ } else if (auto ba = dyn_cast<mlir::BlockArgument>(operand)) {
+ mlir::Block *ownerBlock = ba.getOwner();
+ unsigned argIndex = ba.getArgNumber();
+ if (auto func =
+ dyn_cast<mlir::func::FuncOp>(ownerBlock->getParentOp())) {
+ if (&func.front() == ownerBlock) {
+ Operands.push_back(std::string("funcArg" + std::to_string(argIndex)));
+ } else {
+ Operands.push_back(std::string("blockArg" + std::to_string(argIndex)));
+ }
+ } else {
+ Operands.push_back(std::string("blockArg" + std::to_string(argIndex)));
+ }
+ }
}
if (op->hasTrait<OpTrait::IsCommutative>())
diff --git a/mlir/test/Conversion/Normalize/reorder.mlir b/mlir/test/Conversion/Normalize/reorder.mlir
index 59c46a7834d32..941c3e5aeaa08 100644
--- a/mlir/test/Conversion/Normalize/reorder.mlir
+++ b/mlir/test/Conversion/Normalize/reorder.mlir
@@ -2,10 +2,10 @@
// CHECK-LABEL: func.func @bar(
// CHECK-SAME: %[[ARG0:.*]]: i32) -> i32 {
-// CHECK: %[[VAL_0:.*]] = arith.constant 2 : i32
-// CHECK: %vl15831$51356-funcArg0$ = arith.addi %[[ARG0]], %[[VAL_0:.*]] : i32
+// CHECK: %vl14084$51356$ = arith.constant 2 : i32
+// CHECK: %vl15831$funcArg0-vl14084$ = arith.addi %[[ARG0]], %vl14084$51356$ : i32
// CHECK: %vl14084$187c2$ = arith.constant 6 : i32
-// CHECK: %op27844$vl14084-vl15831$ = arith.addi %vl14084$187c2$, %vl15831$51356-funcArg0$ : i32
+// CHECK: %op27844$vl14084-vl15831$ = arith.addi %vl14084$187c2$, %vl15831$funcArg0-vl14084$ : 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
>From bbbfe217d94d4f7a708eb80ae94f5bb188eab166 Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Fri, 10 Oct 2025 19:12:39 +0530
Subject: [PATCH 17/31] clang-format
---
mlir/lib/Conversion/Normalize/Normalize.cpp | 20 ++++++++++----------
1 file changed, 10 insertions(+), 10 deletions(-)
diff --git a/mlir/lib/Conversion/Normalize/Normalize.cpp b/mlir/lib/Conversion/Normalize/Normalize.cpp
index c803c158073ba..3cfe08b54b96e 100644
--- a/mlir/lib/Conversion/Normalize/Normalize.cpp
+++ b/mlir/lib/Conversion/Normalize/Normalize.cpp
@@ -285,19 +285,19 @@ void NormalizePass::foldOperation(mlir::Operation *op) {
Operands.push_back(name);
}
} else if (auto ba = dyn_cast<mlir::BlockArgument>(operand)) {
- mlir::Block *ownerBlock = ba.getOwner();
- unsigned argIndex = ba.getArgNumber();
- if (auto func =
- dyn_cast<mlir::func::FuncOp>(ownerBlock->getParentOp())) {
- if (&func.front() == ownerBlock) {
- Operands.push_back(std::string("funcArg" + std::to_string(argIndex)));
- } else {
- Operands.push_back(std::string("blockArg" + std::to_string(argIndex)));
- }
+ mlir::Block *ownerBlock = ba.getOwner();
+ unsigned argIndex = ba.getArgNumber();
+ if (auto func = dyn_cast<mlir::func::FuncOp>(ownerBlock->getParentOp())) {
+ if (&func.front() == ownerBlock) {
+ Operands.push_back(std::string("funcArg" + std::to_string(argIndex)));
} else {
- Operands.push_back(std::string("blockArg" + std::to_string(argIndex)));
+ Operands.push_back(
+ std::string("blockArg" + std::to_string(argIndex)));
}
+ } else {
+ Operands.push_back(std::string("blockArg" + std::to_string(argIndex)));
}
+ }
}
if (op->hasTrait<OpTrait::IsCommutative>())
>From 22cdd9182c7f3a95b8fed57cd63689a728420152 Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Fri, 10 Oct 2025 19:30:41 +0530
Subject: [PATCH 18/31] fix infinite-loop test
---
.../Conversion/Normalize/infinite-loop.mlir | 127 +++++++++---------
1 file changed, 64 insertions(+), 63 deletions(-)
diff --git a/mlir/test/Conversion/Normalize/infinite-loop.mlir b/mlir/test/Conversion/Normalize/infinite-loop.mlir
index 969fbe4029e1b..7b98e5c9e69b2 100644
--- a/mlir/test/Conversion/Normalize/infinite-loop.mlir
+++ b/mlir/test/Conversion/Normalize/infinite-loop.mlir
@@ -2,18 +2,18 @@
// CHECK-LABEL: module {
// CHECK: func.func @test(%[[ARG0:.*]]: memref<?xi32>, %[[ARG1:.*]]: i32) {
-// CHECK: %[[VAL_0:.*]] = arith.constant 1 : i32
-// CHECK: %vl15390$e5677-funcArg1$ = arith.addi %[[ARG1:.*]], %[[VAL_0:.*]] : i32
-// CHECK: cf.br ^bb1(%vl15390$e5677-funcArg1$, %vl15390$e5677-funcArg1$ : i32, i32)
-// CHECK: ^bb1(%[[VAL_1:.*]]: i32, %[[VAL_2:.*]]: i32): // 2 preds: ^bb0, ^bb1
+// CHECK: %vl15969$e5677$ = arith.constant 1 : i32
+// CHECK: %vl15390$funcArg1-vl15969$ = arith.addi %[[ARG1:.*]], %vl15969$e5677$ : i32
+// CHECK: cf.br ^bb1(%vl15390$funcArg1-vl15969$, %vl15390$funcArg1-vl15969$ : i32, i32)
+// CHECK: ^bb1(%[[VAL_0:.*]]: i32, %[[VAL_1:.*]]: i32): // 2 preds: ^bb0, ^bb1
// CHECK: %vl22288$20b04$ = arith.constant 0 : i32
-// CHECK: %vl13736$20b04-blockArg0$ = arith.muli %[[VAL_1:.*]], %vl22288$20b04$ : i32
+// CHECK: %vl13736$blockArg0-vl22288$ = arith.muli %[[VAL_0:.*]], %vl22288$20b04$ : i32
// CHECK: %vl22288$ded78$ = arith.constant -1 : i32
-// CHECK: %op51214$vl13736-vl22288$ = arith.xori %vl13736$20b04-blockArg0$, %vl22288$ded78$ : i32
-// CHECK: %op12693$op51214$ = arith.addi %[[VAL_1:.*]], %op51214$vl13736-vl22288$ : i32
-// CHECK: %vl15894$blockArg1-ded78$ = arith.addi %[[VAL_2:.*]], %vl22288$ded78$ : i32
-// CHECK: %op15672$op12693-vl15894$ = arith.addi %op12693$op51214$, %vl15894$blockArg1-ded78$ : i32
-// CHECK: %op97825$op15672-vl13736$ = arith.muli %op15672$op12693-vl15894$, %vl13736$20b04-blockArg0$ : i32
+// CHECK: %op51214$vl13736-vl22288$ = arith.xori %vl13736$blockArg0-vl22288$, %vl22288$ded78$ : i32
+// CHECK: %op12693$blockArg0-op51214$ = arith.addi %[[VAL_0:.*]], %op51214$vl13736-vl22288$ : i32
+// CHECK: %vl15894$blockArg1-vl22288$ = arith.addi %[[VAL_1:.*]], %vl22288$ded78$ : i32
+// CHECK: %op15672$op12693-vl15894$ = arith.addi %op12693$blockArg0-op51214$, %vl15894$blockArg1-vl22288$ : i32
+// CHECK: %op97825$op15672-vl13736$ = arith.muli %op15672$op12693-vl15894$, %vl13736$blockArg0-vl22288$ : i32
// CHECK: %op51214$op97825-vl22288$ = arith.xori %op97825$op15672-vl13736$, %vl22288$ded78$ : i32
// CHECK: %op12343$op15672-op51214$ = arith.addi %op15672$op12693-vl15894$, %op51214$op97825-vl22288$ : i32
// CHECK: %op27844$op12343-vl22288$ = arith.addi %op12343$op15672-op51214$, %vl22288$20b04$ : i32
@@ -22,6 +22,7 @@
// CHECK: %op12343$op27844-op51214$ = arith.addi %op27844$op12343-vl22288$, %op51214$op97825-vl22288$_0 : i32
// CHECK: %op27844$op12343-vl22288$_1 = arith.addi %op12343$op27844-op51214$, %vl22288$20b04$ : i32
// CHECK: %op27844$op27844-vl22288$ = arith.addi %op27844$op12343-vl22288$_1, %vl22288$20b04$ : i32
+// CHECK: %op27844$op27844-vl22288$_2 = arith.addi %op27844$op27844-vl22288$, %vl22288$20b04$ : i32
// CHECK: %op97825$op27844-op97825$_3 = arith.muli %op27844$op12343-vl22288$_1, %op97825$op27844-op97825$ : i32
// CHECK: %op97825$op27844-op97825$_4 = arith.muli %op27844$op27844-vl22288$_2, %op97825$op27844-op97825$_3 : i32
// CHECK: %op51214$op97825-vl22288$_5 = arith.xori %op97825$op27844-op97825$_4, %vl22288$ded78$ : i32
@@ -42,62 +43,62 @@
// CHECK: %op97825$op27844-op97825$_20 = arith.muli %op27844$op12343-vl22288$_19, %op97825$op27844-op97825$_16 : i32
// CHECK: %op51214$op97825-vl22288$_21 = arith.xori %op97825$op27844-op97825$_20, %vl22288$ded78$ : i32
// CHECK: %op12343$op27844-op51214$_22 = arith.addi %op27844$op12343-vl22288$_19, %op51214$op97825-vl22288$_21 : i32
-// CHECK: %[[VAL_3:.*]] = arith.constant -9 : i32
-// CHECK: %vl15894$51850-blockArg1$ = arith.addi %[[VAL_2:.*]], %[[VAL_3:.*]] : i32
-// CHECK: %op15672$op12343-vl15894$ = arith.addi %op12343$op27844-op51214$_22, %vl15894$51850-blockArg1$ : i32
+// CHECK: %vl22288$51850$ = arith.constant -9 : i32
+// CHECK: %vl15894$blockArg1-vl22288$_23 = arith.addi %[[VAL_1:.*]], %vl22288$51850$ : i32
+// CHECK: %op15672$op12343-vl15894$ = arith.addi %op12343$op27844-op51214$_22, %vl15894$blockArg1-vl22288$_23 : i32
// CHECK: %op97825$op15672-op97825$ = arith.muli %op15672$op12343-vl15894$, %op97825$op27844-op97825$_20 : i32
-// CHECK: %op51214$op97825-vl22288$_23 = arith.xori %op97825$op15672-op97825$, %vl22288$ded78$ : i32
-// CHECK: %op12343$op15672-op51214$_24 = arith.addi %op15672$op12343-vl15894$, %op51214$op97825-vl22288$_23 : i32
-// CHECK: %op27844$op12343-vl22288$_25 = arith.addi %op12343$op15672-op51214$_24, %vl22288$20b04$ : i32
-// CHECK: %op97825$op27844-op97825$_26 = arith.muli %op27844$op12343-vl22288$_25, %op97825$op15672-op97825$ : i32
-// CHECK: %op51214$op97825-vl22288$_27 = arith.xori %op97825$op27844-op97825$_26, %vl22288$ded78$ : i32
-// CHECK: %op12343$op27844-op51214$_28 = arith.addi %op27844$op12343-vl22288$_25, %op51214$op97825-vl22288$_27 : i32
-// CHECK: %op27844$op12343-vl22288$_29 = arith.addi %op12343$op27844-op51214$_28, %vl22288$20b04$ : i32
-// CHECK: %op97825$op27844-op97825$_30 = arith.muli %op27844$op12343-vl22288$_29, %op97825$op27844-op97825$_26 : i32
-// CHECK: %op51214$op97825-vl22288$_31 = arith.xori %op97825$op27844-op97825$_30, %vl22288$ded78$ : i32
-// CHECK: %op12343$op27844-op51214$_32 = arith.addi %op27844$op12343-vl22288$_29, %op51214$op97825-vl22288$_31 : i32
-// CHECK: %op27844$op12343-vl22288$_33 = arith.addi %op12343$op27844-op51214$_32, %vl22288$20b04$ : i32
-// CHECK: %op97825$op27844-op97825$_34 = arith.muli %op27844$op12343-vl22288$_33, %op97825$op27844-op97825$_30 : i32
-// CHECK: %op51214$op97825-vl22288$_35 = arith.xori %op97825$op27844-op97825$_34, %vl22288$ded78$ : i32
-// CHECK: %op12343$op27844-op51214$_36 = arith.addi %op27844$op12343-vl22288$_33, %op51214$op97825-vl22288$_35 : i32
-// CHECK: %op27844$op12343-vl22288$_37 = arith.addi %op12343$op27844-op51214$_36, %vl22288$20b04$ : i32
-// CHECK: %op97825$op27844-op97825$_38 = arith.muli %op27844$op12343-vl22288$_37, %op97825$op27844-op97825$_34 : i32
-// CHECK: %op51214$op97825-vl22288$_39 = arith.xori %op97825$op27844-op97825$_38, %vl22288$ded78$ : i32
-// CHECK: %op12343$op27844-op51214$_40 = arith.addi %op27844$op12343-vl22288$_37, %op51214$op97825-vl22288$_39 : i32
-// CHECK: %[[VAL_4:.*]] = arith.constant -14 : i32
-// CHECK: %vl15894$7b7de-blockArg1$ = arith.addi %[[VAL_2:.*]], %[[VAL_4:.*]] : i32
-// CHECK: %op15672$op12343-vl15894$_41 = arith.addi %op12343$op27844-op51214$_40, %vl15894$7b7de-blockArg1$ : i32
-// CHECK: %op97825$op15672-op97825$_42 = arith.muli %op15672$op12343-vl15894$_41, %op97825$op27844-op97825$_38 : i32
-// CHECK: %op51214$op97825-vl22288$_43 = arith.xori %op97825$op15672-op97825$_42, %vl22288$ded78$ : i32
-// CHECK: %op12343$op15672-op51214$_44 = arith.addi %op15672$op12343-vl15894$_41, %op51214$op97825-vl22288$_43 : i32
-// CHECK: %op27844$op12343-vl22288$_45 = arith.addi %op12343$op15672-op51214$_44, %vl22288$20b04$ : i32
-// CHECK: %op97825$op27844-op97825$_46 = arith.muli %op27844$op12343-vl22288$_45, %op97825$op15672-op97825$_42 : i32
-// CHECK: %op51214$op97825-vl22288$_47 = arith.xori %op97825$op27844-op97825$_46, %vl22288$ded78$ : i32
-// CHECK: %op12343$op27844-op51214$_48 = arith.addi %op27844$op12343-vl22288$_45, %op51214$op97825-vl22288$_47 : i32
-// CHECK: %op27844$op12343-vl22288$_49 = arith.addi %op12343$op27844-op51214$_48, %vl22288$20b04$ : i32
-// CHECK: %op97825$op27844-op97825$_50 = arith.muli %op27844$op12343-vl22288$_49, %op97825$op27844-op97825$_46 : i32
-// CHECK: %op51214$op97825-vl22288$_51 = arith.xori %op97825$op27844-op97825$_50, %vl22288$ded78$ : i32
-// CHECK: %op12343$op27844-op51214$_52 = arith.addi %op27844$op12343-vl22288$_49, %op51214$op97825-vl22288$_51 : i32
-// CHECK: %op27844$op12343-vl22288$_53 = arith.addi %op12343$op27844-op51214$_52, %vl22288$20b04$ : i32
-// CHECK: %op97825$op27844-op97825$_54 = arith.muli %op27844$op12343-vl22288$_53, %op97825$op27844-op97825$_50 : i32
-// CHECK: %op51214$op97825-vl22288$_55 = arith.xori %op97825$op27844-op97825$_54, %vl22288$ded78$ : i32
-// CHECK: %op12343$op27844-op51214$_56 = arith.addi %op27844$op12343-vl22288$_53, %op51214$op97825-vl22288$_55 : i32
-// CHECK: %op27844$op12343-vl22288$_57 = arith.addi %op12343$op27844-op51214$_56, %vl22288$20b04$ : i32
-// CHECK: %op97825$op27844-op97825$_58 = arith.muli %op27844$op12343-vl22288$_57, %op97825$op27844-op97825$_54 : i32
-// CHECK: %op51214$op97825-vl22288$_59 = arith.xori %op97825$op27844-op97825$_58, %vl22288$ded78$ : i32
-// CHECK: %op12343$op27844-op51214$_60 = arith.addi %op27844$op12343-vl22288$_57, %op51214$op97825-vl22288$_59 : i32
-// CHECK: %op27844$op12343-vl22288$_61 = arith.addi %op12343$op27844-op51214$_60, %vl22288$20b04$ : i32
-// CHECK: %op97825$op27844-op97825$_62 = arith.muli %op27844$op12343-vl22288$_61, %op97825$op27844-op97825$_58 : i32
-// CHECK: %op51214$op97825-vl22288$_63 = arith.xori %op97825$op27844-op97825$_62, %vl22288$ded78$ : i32
-// CHECK: %op12343$op27844-op51214$_64 = arith.addi %op27844$op12343-vl22288$_61, %op51214$op97825-vl22288$_63 : i32
-// CHECK: %op27844$op12343-vl22288$_65 = arith.addi %op12343$op27844-op51214$_64, %vl22288$20b04$ : i32
-// CHECK: %op27844$op27844-vl22288$_66 = arith.addi %op27844$op12343-vl22288$_65, %vl22288$20b04$ : i32
-// CHECK: %[[VAL_5:.*]] = arith.constant -21 : i32
-// CHECK: %vl15894$1e72e-blockArg1$ = arith.addi %[[VAL_2:.*]], %[[VAL_5:.*]] : i32
-// CHECK: %op15672$op27844-vl15894$ = arith.addi %op27844$op27844-vl22288$_66, %vl15894$1e72e-blockArg1$ : i32
+// CHECK: %op51214$op97825-vl22288$_24 = arith.xori %op97825$op15672-op97825$, %vl22288$ded78$ : i32
+// CHECK: %op12343$op15672-op51214$_25 = arith.addi %op15672$op12343-vl15894$, %op51214$op97825-vl22288$_24 : i32
+// CHECK: %op27844$op12343-vl22288$_26 = arith.addi %op12343$op15672-op51214$_25, %vl22288$20b04$ : i32
+// CHECK: %op97825$op27844-op97825$_27 = arith.muli %op27844$op12343-vl22288$_26, %op97825$op15672-op97825$ : i32
+// CHECK: %op51214$op97825-vl22288$_28 = arith.xori %op97825$op27844-op97825$_27, %vl22288$ded78$ : i32
+// CHECK: %op12343$op27844-op51214$_29 = arith.addi %op27844$op12343-vl22288$_26, %op51214$op97825-vl22288$_28 : i32
+// CHECK: %op27844$op12343-vl22288$_30 = arith.addi %op12343$op27844-op51214$_29, %vl22288$20b04$ : i32
+// CHECK: %op97825$op27844-op97825$_31 = arith.muli %op27844$op12343-vl22288$_30, %op97825$op27844-op97825$_27 : i32
+// CHECK: %op51214$op97825-vl22288$_32 = arith.xori %op97825$op27844-op97825$_31, %vl22288$ded78$ : i32
+// CHECK: %op12343$op27844-op51214$_33 = arith.addi %op27844$op12343-vl22288$_30, %op51214$op97825-vl22288$_32 : i32
+// CHECK: %op27844$op12343-vl22288$_34 = arith.addi %op12343$op27844-op51214$_33, %vl22288$20b04$ : i32
+// CHECK: %op97825$op27844-op97825$_35 = arith.muli %op27844$op12343-vl22288$_34, %op97825$op27844-op97825$_31 : i32
+// CHECK: %op51214$op97825-vl22288$_36 = arith.xori %op97825$op27844-op97825$_35, %vl22288$ded78$ : i32
+// CHECK: %op12343$op27844-op51214$_37 = arith.addi %op27844$op12343-vl22288$_34, %op51214$op97825-vl22288$_36 : i32
+// CHECK: %op27844$op12343-vl22288$_38 = arith.addi %op12343$op27844-op51214$_37, %vl22288$20b04$ : i32
+// CHECK: %op97825$op27844-op97825$_39 = arith.muli %op27844$op12343-vl22288$_38, %op97825$op27844-op97825$_35 : i32
+// CHECK: %op51214$op97825-vl22288$_40 = arith.xori %op97825$op27844-op97825$_39, %vl22288$ded78$ : i32
+// CHECK: %op12343$op27844-op51214$_41 = arith.addi %op27844$op12343-vl22288$_38, %op51214$op97825-vl22288$_40 : i32
+// CHECK: %vl22288$7b7de$ = arith.constant -14 : i32
+// CHECK: %vl15894$blockArg1-vl22288$_42 = arith.addi %[[VAL_1:.*]], %vl22288$7b7de$ : i32
+// CHECK: %op15672$op12343-vl15894$_43 = arith.addi %op12343$op27844-op51214$_41, %vl15894$blockArg1-vl22288$_42 : i32
+// CHECK: %op97825$op15672-op97825$_44 = arith.muli %op15672$op12343-vl15894$_43, %op97825$op27844-op97825$_39 : i32
+// CHECK: %op51214$op97825-vl22288$_45 = arith.xori %op97825$op15672-op97825$_44, %vl22288$ded78$ : i32
+// CHECK: %op12343$op15672-op51214$_46 = arith.addi %op15672$op12343-vl15894$_43, %op51214$op97825-vl22288$_45 : i32
+// CHECK: %op27844$op12343-vl22288$_47 = arith.addi %op12343$op15672-op51214$_46, %vl22288$20b04$ : i32
+// CHECK: %op97825$op27844-op97825$_48 = arith.muli %op27844$op12343-vl22288$_47, %op97825$op15672-op97825$_44 : i32
+// CHECK: %op51214$op97825-vl22288$_49 = arith.xori %op97825$op27844-op97825$_48, %vl22288$ded78$ : i32
+// CHECK: %op12343$op27844-op51214$_50 = arith.addi %op27844$op12343-vl22288$_47, %op51214$op97825-vl22288$_49 : i32
+// CHECK: %op27844$op12343-vl22288$_51 = arith.addi %op12343$op27844-op51214$_50, %vl22288$20b04$ : i32
+// CHECK: %op97825$op27844-op97825$_52 = arith.muli %op27844$op12343-vl22288$_51, %op97825$op27844-op97825$_48 : i32
+// CHECK: %op51214$op97825-vl22288$_53 = arith.xori %op97825$op27844-op97825$_52, %vl22288$ded78$ : i32
+// CHECK: %op12343$op27844-op51214$_54 = arith.addi %op27844$op12343-vl22288$_51, %op51214$op97825-vl22288$_53 : i32
+// CHECK: %op27844$op12343-vl22288$_55 = arith.addi %op12343$op27844-op51214$_54, %vl22288$20b04$ : i32
+// CHECK: %op97825$op27844-op97825$_56 = arith.muli %op27844$op12343-vl22288$_55, %op97825$op27844-op97825$_52 : i32
+// CHECK: %op51214$op97825-vl22288$_57 = arith.xori %op97825$op27844-op97825$_56, %vl22288$ded78$ : i32
+// CHECK: %op12343$op27844-op51214$_58 = arith.addi %op27844$op12343-vl22288$_55, %op51214$op97825-vl22288$_57 : i32
+// CHECK: %op27844$op12343-vl22288$_59 = arith.addi %op12343$op27844-op51214$_58, %vl22288$20b04$ : i32
+// CHECK: %op97825$op27844-op97825$_60 = arith.muli %op27844$op12343-vl22288$_59, %op97825$op27844-op97825$_56 : i32
+// CHECK: %op51214$op97825-vl22288$_61 = arith.xori %op97825$op27844-op97825$_60, %vl22288$ded78$ : i32
+// CHECK: %op12343$op27844-op51214$_62 = arith.addi %op27844$op12343-vl22288$_59, %op51214$op97825-vl22288$_61 : i32
+// CHECK: %op27844$op12343-vl22288$_63 = arith.addi %op12343$op27844-op51214$_62, %vl22288$20b04$ : i32
+// CHECK: %op97825$op27844-op97825$_64 = arith.muli %op27844$op12343-vl22288$_63, %op97825$op27844-op97825$_60 : i32
+// CHECK: %op51214$op97825-vl22288$_65 = arith.xori %op97825$op27844-op97825$_64, %vl22288$ded78$ : i32
+// CHECK: %op12343$op27844-op51214$_66 = arith.addi %op27844$op12343-vl22288$_63, %op51214$op97825-vl22288$_65 : i32
+// CHECK: %op27844$op12343-vl22288$_67 = arith.addi %op12343$op27844-op51214$_66, %vl22288$20b04$ : i32
+// CHECK: %op27844$op27844-vl22288$_68 = arith.addi %op27844$op12343-vl22288$_67, %vl22288$20b04$ : i32
+// CHECK: %vl22288$1e72e$ = arith.constant -21 : i32
+// CHECK: %vl15894$blockArg1-vl22288$_69 = arith.addi %[[VAL_1:.*]], %vl22288$1e72e$ : i32
+// CHECK: %op15672$op27844-vl15894$ = arith.addi %op27844$op27844-vl22288$_68, %vl15894$blockArg1-vl22288$_69 : i32
// CHECK: %vl48856$e527e$ = arith.constant 0 : index
// CHECK: memref.store %op15672$op27844-vl15894$, %[[ARG0:.*]][%vl48856$e527e$] : memref<?xi32>
-// CHECK: cf.br ^bb1(%op15672$op27844-vl15894$, %vl15894$1e72e-blockArg1$ : i32, i32)
+// CHECK: cf.br ^bb1(%op15672$op27844-vl15894$, %vl15894$blockArg1-vl22288$_69 : i32, i32)
// CHECK: }
// CHECK: }
func.func @test(%arg0: memref<?xi32>, %arg1: i32) {
>From 76df86866e3204ee0fb4911c460f387baa9828bc Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Fri, 10 Oct 2025 19:51:02 +0530
Subject: [PATCH 19/31] nit
---
mlir/include/mlir/Conversion/Normalize/Normalize.h | 4 +---
mlir/lib/Conversion/Normalize/Normalize.cpp | 2 +-
2 files changed, 2 insertions(+), 4 deletions(-)
diff --git a/mlir/include/mlir/Conversion/Normalize/Normalize.h b/mlir/include/mlir/Conversion/Normalize/Normalize.h
index fdb1dcd8f2d77..9d5703a13c0bc 100644
--- a/mlir/include/mlir/Conversion/Normalize/Normalize.h
+++ b/mlir/include/mlir/Conversion/Normalize/Normalize.h
@@ -1,4 +1,4 @@
-//===- Normalize.h - Math to outlined impl conversion -----------*- C++ -*-===//
+//===- Normalize.h - Conversion from MLIR to its canonical form -*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
@@ -9,8 +9,6 @@
#ifndef MLIR_CONVERSION_NORMALIZE_NORMALIZE_H
#define MLIR_CONVERSION_NORMALIZE_NORMALIZE_H
-#include <memory>
-
namespace mlir {
class Pass;
diff --git a/mlir/lib/Conversion/Normalize/Normalize.cpp b/mlir/lib/Conversion/Normalize/Normalize.cpp
index 3cfe08b54b96e..9cab5b3bd7edf 100644
--- a/mlir/lib/Conversion/Normalize/Normalize.cpp
+++ b/mlir/lib/Conversion/Normalize/Normalize.cpp
@@ -1,4 +1,4 @@
-//===- Normalize.cpp - IR to simplified IR conversion ---------------------===//
+//===- Normalize.cpp - Conversion from MLIR to its canonical form ---------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
>From 8387f1c48054bd4d022be6026a730e322945f653 Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Fri, 10 Oct 2025 19:51:46 +0530
Subject: [PATCH 20/31] nit
---
mlir/include/mlir/Conversion/Normalize/Normalize.h | 2 ++
1 file changed, 2 insertions(+)
diff --git a/mlir/include/mlir/Conversion/Normalize/Normalize.h b/mlir/include/mlir/Conversion/Normalize/Normalize.h
index 9d5703a13c0bc..650cfe734aeac 100644
--- a/mlir/include/mlir/Conversion/Normalize/Normalize.h
+++ b/mlir/include/mlir/Conversion/Normalize/Normalize.h
@@ -9,6 +9,8 @@
#ifndef MLIR_CONVERSION_NORMALIZE_NORMALIZE_H
#define MLIR_CONVERSION_NORMALIZE_NORMALIZE_H
+#include <memory>
+
namespace mlir {
class Pass;
>From 1823295e53c946ed5dbdb938c269cb7369db99ce Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Fri, 10 Oct 2025 19:54:59 +0530
Subject: [PATCH 21/31] test rename
---
mlir/test/Conversion/Normalize/infinite-loop.mlir | 4 ++--
mlir/test/Conversion/Normalize/reorder.mlir | 4 ++--
2 files changed, 4 insertions(+), 4 deletions(-)
diff --git a/mlir/test/Conversion/Normalize/infinite-loop.mlir b/mlir/test/Conversion/Normalize/infinite-loop.mlir
index 7b98e5c9e69b2..2ec50853046b2 100644
--- a/mlir/test/Conversion/Normalize/infinite-loop.mlir
+++ b/mlir/test/Conversion/Normalize/infinite-loop.mlir
@@ -1,7 +1,7 @@
// RUN: mlir-opt %s --normalize --mlir-use-nameloc-as-prefix 2>&1 | FileCheck %s
// CHECK-LABEL: module {
-// CHECK: func.func @test(%[[ARG0:.*]]: memref<?xi32>, %[[ARG1:.*]]: i32) {
+// CHECK: func.func @infinte_loop(%[[ARG0:.*]]: memref<?xi32>, %[[ARG1:.*]]: i32) {
// CHECK: %vl15969$e5677$ = arith.constant 1 : i32
// CHECK: %vl15390$funcArg1-vl15969$ = arith.addi %[[ARG1:.*]], %vl15969$e5677$ : i32
// CHECK: cf.br ^bb1(%vl15390$funcArg1-vl15969$, %vl15390$funcArg1-vl15969$ : i32, i32)
@@ -101,7 +101,7 @@
// CHECK: cf.br ^bb1(%op15672$op27844-vl15894$, %vl15894$blockArg1-vl22288$_69 : i32, i32)
// CHECK: }
// CHECK: }
-func.func @test(%arg0: memref<?xi32>, %arg1: i32) {
+func.func @infinte_loop(%arg0: memref<?xi32>, %arg1: i32) {
%c1 = arith.constant 1 : i32
%a = arith.addi %arg1, %c1 : i32
cf.br ^bb1(%a, %a : i32, i32)
diff --git a/mlir/test/Conversion/Normalize/reorder.mlir b/mlir/test/Conversion/Normalize/reorder.mlir
index 941c3e5aeaa08..aa698a5ae04d2 100644
--- a/mlir/test/Conversion/Normalize/reorder.mlir
+++ b/mlir/test/Conversion/Normalize/reorder.mlir
@@ -1,6 +1,6 @@
// RUN: mlir-opt %s --normalize --mlir-use-nameloc-as-prefix 2>&1 | FileCheck %s
-// CHECK-LABEL: func.func @bar(
+// CHECK-LABEL: func.func @reorder(
// CHECK-SAME: %[[ARG0:.*]]: i32) -> i32 {
// CHECK: %vl14084$51356$ = arith.constant 2 : i32
// CHECK: %vl15831$funcArg0-vl14084$ = arith.addi %[[ARG0]], %vl14084$51356$ : i32
@@ -10,7 +10,7 @@
// 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 {
+func.func @reorder(%a0: i32) -> i32 {
%c2 = arith.constant 2 : i32
%c6 = arith.constant 6 : i32
%c8 = arith.constant 8 : i32
>From 7b590b03b039d067047079f91a191c21d7eea2cd Mon Sep 17 00:00:00 2001
From: Anant jain <129408892+anant37289 at users.noreply.github.com>
Date: Fri, 10 Oct 2025 19:57:35 +0530
Subject: [PATCH 22/31] adding usedef chain test
---
mlir/test/Conversion/Normalize/usedef.mlir | 35 ++++++++++++++++++++++
1 file changed, 35 insertions(+)
create mode 100644 mlir/test/Conversion/Normalize/usedef.mlir
diff --git a/mlir/test/Conversion/Normalize/usedef.mlir b/mlir/test/Conversion/Normalize/usedef.mlir
new file mode 100644
index 0000000000000..2af5e55d64787
--- /dev/null
+++ b/mlir/test/Conversion/Normalize/usedef.mlir
@@ -0,0 +1,35 @@
+// RUN: mlir-opt %s --normalize --mlir-use-nameloc-as-prefix 2>&1 | FileCheck %s
+
+// CHECK-LABEL: func.func @use_def(
+// CHECK-SAME: %arg0: i32) -> i32 {
+// CHECK: %vl36495$eafb0$ = arith.constant 4 : i32
+// CHECK: %vl43392$funcArg0-vl36495$ = arith.addi %arg0, %vl36495$eafb0$ : i32
+// CHECK: %vl36495$20b04$ = arith.constant 0 : i32
+// CHECK: %op27844$vl36495-vl43392$ = arith.addi %vl36495$20b04$, %vl43392$funcArg0-vl36495$ : i32
+// CHECK: %op27844$op27844-vl36495$ = arith.addi %op27844$vl36495-vl43392$, %vl36495$eafb0$ : i32
+// CHECK: %op15672$op27844-op27844$ = arith.addi %op27844$op27844-vl36495$, %op27844$vl36495-vl43392$ : i32
+// CHECK: %op15672$op15672-op27844$ = arith.addi %op15672$op27844-op27844$, %op27844$op27844-vl36495$ : i32
+// CHECK: %op15672$op15672-op15672$ = arith.addi %op15672$op15672-op27844$, %op15672$op27844-op27844$ : i32
+// CHECK: %op15672$op15672-op15672$_0 = arith.addi %op15672$op15672-op15672$, %op15672$op15672-op27844$ : i32
+// CHECK: %op15672$op15672-op15672$_1 = arith.addi %op15672$op15672-op15672$, %op15672$op15672-op15672$_0 : i32
+// CHECK: %op15672$op15672-op15672$_2 = arith.addi %op15672$op15672-op15672$, %op15672$op15672-op15672$_1 : i32
+// CHECK: %op15672$op15672-op15672$_3 = arith.addi %op15672$op15672-op15672$_1, %op15672$op15672-op15672$_2 : i32
+// CHECK: return %op15672$op15672-op15672$_3 : i32
+// CHECK: }
+module {
+ func.func @use_def(%arg0: i32) -> i32 {
+ %c0 = arith.constant 4 : i32
+ %t = arith.addi %arg0, %c0 : i32
+ %zero = arith.constant 0 : i32
+ %t2 = arith.addi %t, %zero : i32
+ %t3 = arith.addi %t2, %c0 : i32
+ %t4 = arith.addi %t3, %t2 : i32
+ %t5 = arith.addi %t4, %t3 : i32
+ %t6 = arith.addi %t5, %t4 : i32
+ %t7 = arith.addi %t6, %t5 : i32
+ %t8 = arith.addi %t7, %t6 : i32
+ %t9 = arith.addi %t8, %t6 : i32
+ %t10 = arith.addi %t9, %t8 : i32
+ return %t10 : i32
+ }
+}
>From 9f86026096b89d1823ef880d23fff08ad81b88be Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Fri, 17 Oct 2025 15:48:25 +0530
Subject: [PATCH 23/31] nit
---
mlir/include/mlir/Conversion/Passes.td | 13 +-
mlir/lib/Conversion/Normalize/Normalize.cpp | 164 ++++++++++----------
2 files changed, 81 insertions(+), 96 deletions(-)
diff --git a/mlir/include/mlir/Conversion/Passes.td b/mlir/include/mlir/Conversion/Passes.td
index d00b212053177..6d5ed569eb514 100644
--- a/mlir/include/mlir/Conversion/Passes.td
+++ b/mlir/include/mlir/Conversion/Passes.td
@@ -947,18 +947,11 @@ def ConvertShardToMPIPass : Pass<"convert-shard-to-mpi"> {
// Normalize
//===----------------------------------------------------------------------===//
def Normalize : Pass<"normalize", "ModuleOp"> {
- let summary = "normalize.";
+ let summary = "convert MLIR input to its normalized form";
let description = [{
- just normalize bro
+ This pass produces reordered MLIR input with determinstic variable
+ names to reduce the diff between two semantically similar programs
}];
- let dependentDialects = [
- "arith::ArithDialect",
- "cf::ControlFlowDialect",
- "func::FuncDialect",
- "scf::SCFDialect",
- "vector::VectorDialect",
- "LLVM::LLVMDialect",
- ];
}
//===----------------------------------------------------------------------===//
diff --git a/mlir/lib/Conversion/Normalize/Normalize.cpp b/mlir/lib/Conversion/Normalize/Normalize.cpp
index 9cab5b3bd7edf..2a9999bd7badc 100644
--- a/mlir/lib/Conversion/Normalize/Normalize.cpp
+++ b/mlir/lib/Conversion/Normalize/Normalize.cpp
@@ -44,31 +44,27 @@ struct NormalizePass : public impl::NormalizeBase<NormalizePass> {
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);
-
+ bool isOutput(Operation &op) const noexcept;
+ void reorderOperations(const SmallVector<Operation *, 16> &Outputs);
+ void reorderOperation(Operation *used, Operation *user,
+ llvm::SmallPtrSet<const Operation *, 32> &visited);
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,
- llvm::SmallPtrSet<const mlir::Operation *, 32> &visited);
- 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) const;
- void foldOperation(mlir::Operation *op);
- void reorderOperationOperandsByName(mlir::Operation *op);
- mlir::OpPrintingFlags flags{};
+ void RenameOperation(Operation *op,
+ SmallPtrSet<const Operation *, 32> &visited);
+ bool isInitialOperation(Operation *const op) const noexcept;
+ void
+ nameAsInitialOperation(Operation *op,
+ llvm::SmallPtrSet<const Operation *, 32> &visited);
+ void
+ nameAsRegularOperation(Operation *op,
+ llvm::SmallPtrSet<const Operation *, 32> &visited);
+ bool hasOnlyImmediateOperands(Operation *const op) const noexcept;
+ llvm::SetVector<int>
+ getOutputFootprint(Operation *op,
+ llvm::SmallPtrSet<const Operation *, 32> &visited) const;
+ void foldOperation(Operation *op);
+ void reorderOperationOperandsByName(Operation *op);
+ OpPrintingFlags flags{};
};
} // namespace
@@ -91,14 +87,14 @@ void NormalizePass::runOnOperation() {
void NormalizePass::renameOperations(
const SmallVector<Operation *, 16> &Outputs) {
- llvm::SmallPtrSet<const mlir::Operation *, 32> visited;
+ llvm::SmallPtrSet<const Operation *, 32> visited;
for (auto *op : Outputs)
RenameOperation(op, visited);
}
void NormalizePass::RenameOperation(
- Operation *op, SmallPtrSet<const mlir::Operation *, 32> &visited) {
+ Operation *op, SmallPtrSet<const Operation *, 32> &visited) {
if (!visited.count(op)) {
visited.insert(op);
@@ -112,15 +108,14 @@ void NormalizePass::RenameOperation(
}
}
-bool NormalizePass::isInitialOperation(
- mlir::Operation *const op) const noexcept {
+bool NormalizePass::isInitialOperation(Operation *const op) const noexcept {
return !op->use_empty() and hasOnlyImmediateOperands(op);
}
bool NormalizePass::hasOnlyImmediateOperands(
- mlir::Operation *const op) const noexcept {
- for (mlir::Value operand : op->getOperands())
- if (mlir::Operation *defOp = operand.getDefiningOp())
+ Operation *const op) const noexcept {
+ for (Value operand : op->getOperands())
+ if (Operation *defOp = operand.getDefiningOp())
if (!(defOp->hasTrait<OpTrait::ConstantLike>()))
return false;
return true;
@@ -161,11 +156,10 @@ std::string inline split(std::string_view str, const char &delimiter,
}
void NormalizePass::nameAsInitialOperation(
- mlir::Operation *op,
- llvm::SmallPtrSet<const mlir::Operation *, 32> &visited) {
+ Operation *op, llvm::SmallPtrSet<const Operation *, 32> &visited) {
- for (mlir::Value operand : op->getOperands())
- if (mlir::Operation *defOp = operand.getDefiningOp())
+ for (Value operand : op->getOperands())
+ if (Operation *defOp = operand.getDefiningOp())
RenameOperation(defOp, visited);
uint64_t Hash = MagicHashConstant;
@@ -173,27 +167,27 @@ void NormalizePass::nameAsInitialOperation(
uint64_t opcodeHash = strHash(op->getName().getStringRef().str());
Hash = llvm::hashing::detail::hash_16_bytes(Hash, opcodeHash);
- SmallPtrSet<const mlir::Operation *, 32> Visited;
+ SmallPtrSet<const Operation *, 32> Visited;
SetVector<int> OutputFootprint = getOutputFootprint(op, Visited);
- for (const int &Output : OutputFootprint)
+ for (const auto &Output : OutputFootprint)
Hash = llvm::hashing::detail::hash_16_bytes(Hash, Output);
std::string Name{""};
Name.append("vl" + std::to_string(Hash).substr(0, 5));
- if (auto call = mlir::dyn_cast<mlir::func::CallOp>(op)) {
+ if (auto call = dyn_cast<func::CallOp>(op)) {
llvm::StringRef callee = call.getCallee();
Name.append(callee.str());
}
if (op->getNumOperands() == 0) {
Name.append("$");
- if (auto call = mlir::dyn_cast<mlir::func::CallOp>(op)) {
+ if (auto call = dyn_cast<func::CallOp>(op)) {
Name.append("void");
} else {
std::string TextRepresentation;
- mlir::AsmState state(op, flags);
+ AsmState state(op, flags);
llvm::raw_string_ostream Stream(TextRepresentation);
op->print(Stream, state);
std::string hash = to_string(strHash(split(Stream.str(), '=', 1)));
@@ -202,18 +196,17 @@ void NormalizePass::nameAsInitialOperation(
Name.append("$");
}
- mlir::OpBuilder b(op->getContext());
- mlir::StringAttr sat = b.getStringAttr(Name);
- mlir::Location newLoc = mlir::NameLoc::get(sat, op->getLoc());
+ OpBuilder b(op->getContext());
+ StringAttr sat = b.getStringAttr(Name);
+ Location newLoc = NameLoc::get(sat, op->getLoc());
op->setLoc(newLoc);
}
void NormalizePass::nameAsRegularOperation(
- mlir::Operation *op,
- llvm::SmallPtrSet<const mlir::Operation *, 32> &visited) {
+ Operation *op, llvm::SmallPtrSet<const Operation *, 32> &visited) {
- for (mlir::Value operand : op->getOperands())
- if (mlir::Operation *defOp = operand.getDefiningOp())
+ for (Value operand : op->getOperands())
+ if (Operation *defOp = operand.getDefiningOp())
RenameOperation(defOp, visited);
uint64_t Hash = MagicHashConstant;
@@ -223,8 +216,8 @@ void NormalizePass::nameAsRegularOperation(
SmallVector<uint64_t, 4> OperandsOpcodes;
- for (mlir::Value operand : op->getOperands())
- if (mlir::Operation *defOp = operand.getDefiningOp())
+ for (Value operand : op->getOperands())
+ if (Operation *defOp = operand.getDefiningOp())
OperandsOpcodes.push_back(strHash(defOp->getName().getStringRef().str()));
if (op->hasTrait<OpTrait::IsCommutative>())
@@ -236,14 +229,14 @@ void NormalizePass::nameAsRegularOperation(
SmallString<512> Name;
Name.append("op" + std::to_string(Hash).substr(0, 5));
- if (auto call = mlir::dyn_cast<mlir::func::CallOp>(op)) {
+ if (auto call = dyn_cast<func::CallOp>(op)) {
llvm::StringRef callee = call.getCallee();
Name.append(callee.str());
}
- mlir::OpBuilder b(op->getContext());
- mlir::StringAttr sat = b.getStringAttr(Name);
- mlir::Location newLoc = mlir::NameLoc::get(sat, op->getLoc());
+ OpBuilder b(op->getContext());
+ StringAttr sat = b.getStringAttr(Name);
+ Location newLoc = NameLoc::get(sat, op->getLoc());
op->setLoc(newLoc);
}
@@ -253,12 +246,12 @@ bool inline starts_with(std::string_view base,
std::equal(check.begin(), check.end(), base.begin());
}
-void NormalizePass::foldOperation(mlir::Operation *op) {
+void NormalizePass::foldOperation(Operation *op) {
if (isOutput(*op) || op->getNumOperands() == 0)
return;
std::string TextRepresentation;
- mlir::AsmState state(op, flags);
+ AsmState state(op, flags);
llvm::raw_string_ostream Stream(TextRepresentation);
op->print(Stream, state);
@@ -268,10 +261,10 @@ void NormalizePass::foldOperation(mlir::Operation *op) {
SmallVector<std::string, 4> Operands;
- for (mlir::Value operand : op->getOperands()) {
- if (mlir::Operation *defOp = operand.getDefiningOp()) {
+ for (Value operand : op->getOperands()) {
+ if (Operation *defOp = operand.getDefiningOp()) {
std::string TextRepresentation;
- mlir::AsmState state(defOp, flags);
+ AsmState state(defOp, flags);
llvm::raw_string_ostream Stream(TextRepresentation);
defOp->print(Stream, state);
auto name = split(Stream.str(), '=', 0);
@@ -284,10 +277,10 @@ void NormalizePass::foldOperation(mlir::Operation *op) {
} else {
Operands.push_back(name);
}
- } else if (auto ba = dyn_cast<mlir::BlockArgument>(operand)) {
- mlir::Block *ownerBlock = ba.getOwner();
+ } else if (auto ba = dyn_cast<BlockArgument>(operand)) {
+ Block *ownerBlock = ba.getOwner();
unsigned argIndex = ba.getArgNumber();
- if (auto func = dyn_cast<mlir::func::FuncOp>(ownerBlock->getParentOp())) {
+ if (auto func = dyn_cast<func::FuncOp>(ownerBlock->getParentOp())) {
if (&func.front() == ownerBlock) {
Operands.push_back(std::string("funcArg" + std::to_string(argIndex)));
} else {
@@ -307,27 +300,27 @@ void NormalizePass::foldOperation(mlir::Operation *op) {
Name.append(opName.substr(1, 7));
Name.append("$");
- for (unsigned long i = 0; i < Operands.size(); ++i) {
+ for (size_t i = 0, size_ = Operands.size(); i < size_; ++i) {
Name.append(Operands[i]);
- if (i < Operands.size() - 1)
+ if (i < 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());
+ OpBuilder b(op->getContext());
+ StringAttr sat = b.getStringAttr(Name);
+ Location newLoc = NameLoc::get(sat, op->getLoc());
op->setLoc(newLoc);
}
-void NormalizePass::reorderOperationOperandsByName(mlir::Operation *op) {
+void NormalizePass::reorderOperationOperandsByName(Operation *op) {
if (op->getNumOperands() == 0)
return;
- SmallVector<std::pair<std::string, mlir::Value>, 4> Operands;
+ SmallVector<std::pair<std::string, Value>, 4> Operands;
- for (mlir::Value operand : op->getOperands()) {
+ for (Value operand : op->getOperands()) {
std::string TextRepresentation;
llvm::raw_string_ostream Stream(TextRepresentation);
operand.printAsOperand(Stream, flags);
@@ -341,36 +334,36 @@ void NormalizePass::reorderOperationOperandsByName(mlir::Operation *op) {
});
}
- for (size_t i = 0; i < Operands.size(); i++) {
+ for (size_t i = 0, size_ = Operands.size(); i < size_; i++) {
op->setOperand(i, Operands[i].second);
}
}
void NormalizePass::reorderOperations(
const SmallVector<Operation *, 16> &Outputs) {
- llvm::SmallPtrSet<const mlir::Operation *, 32> visited;
+ llvm::SmallPtrSet<const Operation *, 32> visited;
for (auto *const op : Outputs)
- for (mlir::Value operand : op->getOperands())
- if (mlir::Operation *defOp = operand.getDefiningOp())
+ for (Value operand : op->getOperands())
+ if (Operation *defOp = operand.getDefiningOp())
reorderOperation(defOp, op, visited);
}
void NormalizePass::reorderOperation(
- mlir::Operation *used, mlir::Operation *user,
- llvm::SmallPtrSet<const mlir::Operation *, 32> &visited) {
+ Operation *used, Operation *user,
+ llvm::SmallPtrSet<const Operation *, 32> &visited) {
if (!visited.count(used)) {
visited.insert(used);
- mlir::Block *usedBlock = used->getBlock();
- mlir::Block *userBlock = user->getBlock();
+ Block *usedBlock = used->getBlock();
+ 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())
+ for (Value operand : used->getOperands())
+ if (Operation *defOp = operand.getDefiningOp())
reorderOperation(defOp, used, visited);
}
}
@@ -394,25 +387,24 @@ bool NormalizePass::isOutput(Operation &op) const noexcept {
return true;
}
- if (auto call = mlir::dyn_cast<mlir::func::CallOp>(op))
+ if (auto call = dyn_cast<func::CallOp>(op))
return true;
return false;
}
llvm::SetVector<int> NormalizePass::getOutputFootprint(
- mlir::Operation *op,
- llvm::SmallPtrSet<const mlir::Operation *, 32> &visited) const {
+ Operation *op, llvm::SmallPtrSet<const Operation *, 32> &visited) const {
llvm::SetVector<int> Outputs;
if (!visited.count(op)) {
visited.insert(op);
if (isOutput(*op)) {
- mlir::func::FuncOp func = op->getParentOfType<mlir::func::FuncOp>();
+ func::FuncOp func = op->getParentOfType<func::FuncOp>();
unsigned Count = 0;
for (Block &block : func.getRegion())
- for (mlir::Operation &innerOp : block) {
+ for (Operation &innerOp : block) {
if (&innerOp == op)
Outputs.insert(Count);
Count++;
@@ -421,8 +413,8 @@ llvm::SetVector<int> NormalizePass::getOutputFootprint(
return Outputs;
}
- for (mlir::OpOperand &use : op->getUses()) {
- mlir::Operation *useOp = use.getOwner();
+ for (OpOperand &use : op->getUses()) {
+ Operation *useOp = use.getOwner();
if (useOp) {
llvm::SetVector<int> OutputsUsingUop =
getOutputFootprint(useOp, visited);
>From 2313810b541c358e05d1ed8817e5c4979483cdf6 Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Fri, 17 Oct 2025 18:00:02 +0530
Subject: [PATCH 24/31] add docs
---
mlir/lib/Conversion/Normalize/Normalize.cpp | 101 +++++++++++++-------
1 file changed, 68 insertions(+), 33 deletions(-)
diff --git a/mlir/lib/Conversion/Normalize/Normalize.cpp b/mlir/lib/Conversion/Normalize/Normalize.cpp
index 2a9999bd7badc..890bf097a502a 100644
--- a/mlir/lib/Conversion/Normalize/Normalize.cpp
+++ b/mlir/lib/Conversion/Normalize/Normalize.cpp
@@ -34,12 +34,14 @@ using namespace mlir;
#define DEBUG_TYPE "normalize"
namespace {
+/// NormalizePass aims to transform MLIR into it's normal form
struct NormalizePass : public impl::NormalizeBase<NormalizePass> {
NormalizePass() = default;
void runOnOperation() override;
private:
+ // Random constant for hashing, so the state isn't zero.
const uint64_t MagicHashConstant = 0x6acaa36bef8325c5ULL;
void
collectOutputOperations(Block &block,
@@ -49,7 +51,7 @@ struct NormalizePass : public impl::NormalizeBase<NormalizePass> {
void reorderOperation(Operation *used, Operation *user,
llvm::SmallPtrSet<const Operation *, 32> &visited);
void renameOperations(const SmallVector<Operation *, 16> &Outputs);
- void RenameOperation(Operation *op,
+ void renameOperation(Operation *op,
SmallPtrSet<const Operation *, 32> &visited);
bool isInitialOperation(Operation *const op) const noexcept;
void
@@ -62,12 +64,13 @@ struct NormalizePass : public impl::NormalizeBase<NormalizePass> {
llvm::SetVector<int>
getOutputFootprint(Operation *op,
llvm::SmallPtrSet<const Operation *, 32> &visited) const;
- void foldOperation(Operation *op);
+ void appendRenamedOperands(Operation *op, SmallString<512> &opName);
void reorderOperationOperandsByName(Operation *op);
OpPrintingFlags flags{};
};
} // namespace
+/// Entry method to the NormalizePass
void NormalizePass::runOnOperation() {
flags.printNameLocAsPrefix(true);
@@ -90,10 +93,13 @@ void NormalizePass::renameOperations(
llvm::SmallPtrSet<const Operation *, 32> visited;
for (auto *op : Outputs)
- RenameOperation(op, visited);
+ renameOperation(op, visited);
}
-void NormalizePass::RenameOperation(
+/// Renames operations graphically (recursive) in accordance with the
+/// def-use tree, starting from the initial operations (defs), finishing at
+/// the output (top-most user) operations.
+void NormalizePass::renameOperation(
Operation *op, SmallPtrSet<const Operation *, 32> &visited) {
if (!visited.count(op)) {
visited.insert(op);
@@ -103,15 +109,19 @@ void NormalizePass::RenameOperation(
} else {
nameAsRegularOperation(op, visited);
}
- foldOperation(op);
- reorderOperationOperandsByName(op);
+ if (op->hasTrait<OpTrait::IsCommutative>())
+ reorderOperationOperandsByName(op);
}
}
+/// Helper method checking whether a given operation has users and only
+/// immediate operands.
bool NormalizePass::isInitialOperation(Operation *const op) const noexcept {
return !op->use_empty() and hasOnlyImmediateOperands(op);
}
+/// Helper method checking whether all operands of a given operation has a
+/// ConstantLike OpTrait
bool NormalizePass::hasOnlyImmediateOperands(
Operation *const op) const noexcept {
for (Value operand : op->getOperands())
@@ -155,12 +165,21 @@ std::string inline split(std::string_view str, const char &delimiter,
return nullptr;
}
+/// Names operation following the scheme:
+/// vl00000Callee$Operands$
+///
+/// Where 00000 is a hash calculated considering operation's opcode and output
+/// footprint. Callee's name is only included when operations's type is
+/// CallOp. If the operation has operands, the renaming is further handled
+/// in appendRenamedOperands, otherwise if it's a call operation with no
+/// arguments, void is appended, else a hash of the definition of the operation
+/// is appended.
void NormalizePass::nameAsInitialOperation(
Operation *op, llvm::SmallPtrSet<const Operation *, 32> &visited) {
for (Value operand : op->getOperands())
if (Operation *defOp = operand.getDefiningOp())
- RenameOperation(defOp, visited);
+ renameOperation(defOp, visited);
uint64_t Hash = MagicHashConstant;
@@ -173,7 +192,7 @@ void NormalizePass::nameAsInitialOperation(
for (const auto &Output : OutputFootprint)
Hash = llvm::hashing::detail::hash_16_bytes(Hash, Output);
- std::string Name{""};
+ SmallString<512> Name;
Name.append("vl" + std::to_string(Hash).substr(0, 5));
if (auto call = dyn_cast<func::CallOp>(op)) {
@@ -194,20 +213,31 @@ void NormalizePass::nameAsInitialOperation(
Name.append(hash);
}
Name.append("$");
+
+ OpBuilder b(op->getContext());
+ StringAttr sat = b.getStringAttr(Name);
+ Location newLoc = NameLoc::get(sat, op->getLoc());
+ op->setLoc(newLoc);
+
+ return;
}
- OpBuilder b(op->getContext());
- StringAttr sat = b.getStringAttr(Name);
- Location newLoc = NameLoc::get(sat, op->getLoc());
- op->setLoc(newLoc);
+ appendRenamedOperands(op, Name);
}
+/// Names operation following the scheme:
+/// op00000Callee$Operands$
+///
+/// Where 00000 is a hash calculated considering operation's opcode and its
+/// operands opcode. Callee's name is only included when operations's type is
+/// CallOp. A regular operation must have operands, thus the renaming is further
+/// handled in appendRenamedOperands.
void NormalizePass::nameAsRegularOperation(
Operation *op, llvm::SmallPtrSet<const Operation *, 32> &visited) {
for (Value operand : op->getOperands())
if (Operation *defOp = operand.getDefiningOp())
- RenameOperation(defOp, visited);
+ renameOperation(defOp, visited);
uint64_t Hash = MagicHashConstant;
@@ -234,10 +264,7 @@ void NormalizePass::nameAsRegularOperation(
Name.append(callee.str());
}
- OpBuilder b(op->getContext());
- StringAttr sat = b.getStringAttr(Name);
- Location newLoc = NameLoc::get(sat, op->getLoc());
- op->setLoc(newLoc);
+ appendRenamedOperands(op, Name);
}
bool inline starts_with(std::string_view base,
@@ -246,17 +273,15 @@ bool inline starts_with(std::string_view base,
std::equal(check.begin(), check.end(), base.begin());
}
-void NormalizePass::foldOperation(Operation *op) {
- if (isOutput(*op) || op->getNumOperands() == 0)
- return;
-
- std::string TextRepresentation;
- 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") && !starts_with(opName, "%vl"))
+/// This function serves a dual purpose of appending the operands name in the
+/// operation while at the same time shortening it. Because of the recursive
+/// def-use chain traversal, the operands should already have been renamed and
+/// if they were an initial / regular operation, we truncate them by taking the
+/// first 7 characters of the renamed operand. The operand could also have been
+/// a block/function argument which is handled separately.
+void NormalizePass::appendRenamedOperands(Operation *op,
+ SmallString<512> &Name) {
+ if (op->getNumOperands() == 0)
return;
SmallVector<std::string, 4> Operands;
@@ -296,9 +321,6 @@ void NormalizePass::foldOperation(Operation *op) {
if (op->hasTrait<OpTrait::IsCommutative>())
llvm::sort(Operands.begin(), Operands.end());
- SmallString<512> Name;
- Name.append(opName.substr(1, 7));
-
Name.append("$");
for (size_t i = 0, size_ = Operands.size(); i < size_; ++i) {
Name.append(Operands[i]);
@@ -309,11 +331,12 @@ void NormalizePass::foldOperation(Operation *op) {
Name.append("$");
OpBuilder b(op->getContext());
- StringAttr sat = b.getStringAttr(Name);
- Location newLoc = NameLoc::get(sat, op->getLoc());
+ Location newLoc = NameLoc::get(b.getStringAttr(Name), op->getLoc());
op->setLoc(newLoc);
}
+/// Reorders operation's operands alphabetically. This method assumes
+/// that passed operation is commutative.
void NormalizePass::reorderOperationOperandsByName(Operation *op) {
if (op->getNumOperands() == 0)
return;
@@ -339,6 +362,8 @@ void NormalizePass::reorderOperationOperandsByName(Operation *op) {
}
}
+/// Reorders operations by walking up the tree from each operand of an output
+/// operation and reducing the def-use distance.
void NormalizePass::reorderOperations(
const SmallVector<Operation *, 16> &Outputs) {
llvm::SmallPtrSet<const Operation *, 32> visited;
@@ -375,6 +400,13 @@ void NormalizePass::collectOutputOperations(
Outputs.emplace_back(&innerOp);
}
+/// The following Operations are termed as output:
+/// - Terminator operations are outputs
+/// - Any operation that implements MemoryEffectOpInterface and reports at
+/// least
+/// one MemoryEffects::Write effect is an output
+/// - func::CallOp is treated as an output (calls are conservatively assumed to
+/// possibly produce side effects).
bool NormalizePass::isOutput(Operation &op) const noexcept {
if (op.hasTrait<OpTrait::IsTerminator>())
return true;
@@ -393,6 +425,9 @@ bool NormalizePass::isOutput(Operation &op) const noexcept {
return false;
}
+/// Helper method returning indices (distance from the beginning of the basic
+/// block) of output operations using the given operation. Walks down the
+/// def-use tree recursively
llvm::SetVector<int> NormalizePass::getOutputFootprint(
Operation *op, llvm::SmallPtrSet<const Operation *, 32> &visited) const {
llvm::SetVector<int> Outputs;
>From 57c0292375d4dd8793f50d8d28d5daee369ce8a1 Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Mon, 20 Oct 2025 01:50:07 +0530
Subject: [PATCH 25/31] camelCase
---
mlir/lib/Conversion/Normalize/Normalize.cpp | 168 ++++++++++----------
1 file changed, 83 insertions(+), 85 deletions(-)
diff --git a/mlir/lib/Conversion/Normalize/Normalize.cpp b/mlir/lib/Conversion/Normalize/Normalize.cpp
index 890bf097a502a..b9d5f32a9179b 100644
--- a/mlir/lib/Conversion/Normalize/Normalize.cpp
+++ b/mlir/lib/Conversion/Normalize/Normalize.cpp
@@ -42,15 +42,15 @@ struct NormalizePass : public impl::NormalizeBase<NormalizePass> {
private:
// Random constant for hashing, so the state isn't zero.
- const uint64_t MagicHashConstant = 0x6acaa36bef8325c5ULL;
+ const uint64_t magicHashConstant = 0x6acaa36bef8325c5ULL;
void
collectOutputOperations(Block &block,
- SmallVector<Operation *, 16> &Output) const noexcept;
+ SmallVector<Operation *, 16> &outputs) const noexcept;
bool isOutput(Operation &op) const noexcept;
- void reorderOperations(const SmallVector<Operation *, 16> &Outputs);
+ void reorderOperations(const SmallVector<Operation *, 16> &outputs);
void reorderOperation(Operation *used, Operation *user,
llvm::SmallPtrSet<const Operation *, 32> &visited);
- void renameOperations(const SmallVector<Operation *, 16> &Outputs);
+ void renameOperations(const SmallVector<Operation *, 16> &outputs);
void renameOperation(Operation *op,
SmallPtrSet<const Operation *, 32> &visited);
bool isInitialOperation(Operation *const op) const noexcept;
@@ -64,7 +64,7 @@ struct NormalizePass : public impl::NormalizeBase<NormalizePass> {
llvm::SetVector<int>
getOutputFootprint(Operation *op,
llvm::SmallPtrSet<const Operation *, 32> &visited) const;
- void appendRenamedOperands(Operation *op, SmallString<512> &opName);
+ void appendRenamedOperands(Operation *op, SmallString<512> &name);
void reorderOperationOperandsByName(Operation *op);
OpPrintingFlags flags{};
};
@@ -77,22 +77,22 @@ void NormalizePass::runOnOperation() {
ModuleOp module = getOperation();
for (auto &op : module.getOps()) {
- SmallVector<Operation *, 16> Outputs;
+ SmallVector<Operation *, 16> outputs;
for (auto ®ion : op.getRegions())
for (auto &block : region)
- collectOutputOperations(block, Outputs);
+ collectOutputOperations(block, outputs);
- reorderOperations(Outputs);
- renameOperations(Outputs);
+ reorderOperations(outputs);
+ renameOperations(outputs);
}
}
void NormalizePass::renameOperations(
- const SmallVector<Operation *, 16> &Outputs) {
+ const SmallVector<Operation *, 16> &outputs) {
llvm::SmallPtrSet<const Operation *, 32> visited;
- for (auto *op : Outputs)
+ for (auto *op : outputs)
renameOperation(op, visited);
}
@@ -131,7 +131,7 @@ bool NormalizePass::hasOnlyImmediateOperands(
return true;
}
-std::string inline to_string(uint64_t const hash) noexcept {
+std::string inline toString(uint64_t const hash) noexcept {
std::ostringstream oss;
oss << std::hex << std::setw(5) << std::setfill('0') << hash;
std::string tmp = oss.str();
@@ -181,48 +181,48 @@ void NormalizePass::nameAsInitialOperation(
if (Operation *defOp = operand.getDefiningOp())
renameOperation(defOp, visited);
- uint64_t Hash = MagicHashConstant;
+ uint64_t hash = magicHashConstant;
uint64_t opcodeHash = strHash(op->getName().getStringRef().str());
- Hash = llvm::hashing::detail::hash_16_bytes(Hash, opcodeHash);
+ hash = llvm::hashing::detail::hash_16_bytes(hash, opcodeHash);
- SmallPtrSet<const Operation *, 32> Visited;
- SetVector<int> OutputFootprint = getOutputFootprint(op, Visited);
+ SmallPtrSet<const Operation *, 32> visitedLocal;
+ SetVector<int> outputFootprint = getOutputFootprint(op, visitedLocal);
- for (const auto &Output : OutputFootprint)
- Hash = llvm::hashing::detail::hash_16_bytes(Hash, Output);
+ for (const auto &output : outputFootprint)
+ hash = llvm::hashing::detail::hash_16_bytes(hash, output);
- SmallString<512> Name;
- Name.append("vl" + std::to_string(Hash).substr(0, 5));
+ SmallString<512> name;
+ name.append("vl" + std::to_string(hash).substr(0, 5));
if (auto call = dyn_cast<func::CallOp>(op)) {
llvm::StringRef callee = call.getCallee();
- Name.append(callee.str());
+ name.append(callee.str());
}
if (op->getNumOperands() == 0) {
- Name.append("$");
+ name.append("$");
if (auto call = dyn_cast<func::CallOp>(op)) {
- Name.append("void");
+ name.append("void");
} else {
- std::string TextRepresentation;
+ std::string textRepresentation;
AsmState state(op, flags);
- llvm::raw_string_ostream Stream(TextRepresentation);
- op->print(Stream, state);
- std::string hash = to_string(strHash(split(Stream.str(), '=', 1)));
- Name.append(hash);
+ llvm::raw_string_ostream stream(textRepresentation);
+ op->print(stream, state);
+ std::string hashStr = toString(strHash(split(stream.str(), '=', 1)));
+ name.append(hashStr);
}
- Name.append("$");
+ name.append("$");
OpBuilder b(op->getContext());
- StringAttr sat = b.getStringAttr(Name);
+ StringAttr sat = b.getStringAttr(name);
Location newLoc = NameLoc::get(sat, op->getLoc());
op->setLoc(newLoc);
return;
}
- appendRenamedOperands(op, Name);
+ appendRenamedOperands(op, name);
}
/// Names operation following the scheme:
@@ -239,36 +239,35 @@ void NormalizePass::nameAsRegularOperation(
if (Operation *defOp = operand.getDefiningOp())
renameOperation(defOp, visited);
- uint64_t Hash = MagicHashConstant;
+ uint64_t hash = magicHashConstant;
uint64_t opcodeHash = strHash(op->getName().getStringRef().str());
- Hash = llvm::hashing::detail::hash_16_bytes(Hash, opcodeHash);
+ hash = llvm::hashing::detail::hash_16_bytes(hash, opcodeHash);
- SmallVector<uint64_t, 4> OperandsOpcodes;
+ SmallVector<uint64_t, 4> operandOpcodes;
for (Value operand : op->getOperands())
if (Operation *defOp = operand.getDefiningOp())
- OperandsOpcodes.push_back(strHash(defOp->getName().getStringRef().str()));
+ operandOpcodes.push_back(strHash(defOp->getName().getStringRef().str()));
if (op->hasTrait<OpTrait::IsCommutative>())
- llvm::sort(OperandsOpcodes.begin(), OperandsOpcodes.end());
+ llvm::sort(operandOpcodes.begin(), operandOpcodes.end());
- for (const uint64_t Code : OperandsOpcodes)
- Hash = llvm::hashing::detail::hash_16_bytes(Hash, Code);
+ for (const uint64_t code : operandOpcodes)
+ hash = llvm::hashing::detail::hash_16_bytes(hash, code);
- SmallString<512> Name;
- Name.append("op" + std::to_string(Hash).substr(0, 5));
+ SmallString<512> name;
+ name.append("op" + std::to_string(hash).substr(0, 5));
if (auto call = dyn_cast<func::CallOp>(op)) {
llvm::StringRef callee = call.getCallee();
- Name.append(callee.str());
+ name.append(callee.str());
}
- appendRenamedOperands(op, Name);
+ appendRenamedOperands(op, name);
}
-bool inline starts_with(std::string_view base,
- std::string_view check) noexcept {
+bool inline startsWith(std::string_view base, std::string_view check) noexcept {
return base.size() >= check.size() &&
std::equal(check.begin(), check.end(), base.begin());
}
@@ -280,58 +279,58 @@ bool inline starts_with(std::string_view base,
/// first 7 characters of the renamed operand. The operand could also have been
/// a block/function argument which is handled separately.
void NormalizePass::appendRenamedOperands(Operation *op,
- SmallString<512> &Name) {
+ SmallString<512> &name) {
if (op->getNumOperands() == 0)
return;
- SmallVector<std::string, 4> Operands;
+ SmallVector<std::string, 4> operands;
for (Value operand : op->getOperands()) {
if (Operation *defOp = operand.getDefiningOp()) {
- std::string TextRepresentation;
+ std::string textRepresentation;
AsmState state(defOp, flags);
- llvm::raw_string_ostream Stream(TextRepresentation);
- defOp->print(Stream, state);
- auto name = split(Stream.str(), '=', 0);
+ llvm::raw_string_ostream stream(textRepresentation);
+ defOp->print(stream, state);
+ auto operandName = split(stream.str(), '=', 0);
bool hasNormalName =
- (starts_with(name, "%op") || starts_with(name, "%vl"));
+ (startsWith(operandName, "%op") || startsWith(operandName, "%vl"));
if (hasNormalName) {
- Operands.push_back(name.substr(1, 7));
+ operands.push_back(operandName.substr(1, 7));
} else {
- Operands.push_back(name);
+ operands.push_back(operandName);
}
} else if (auto ba = dyn_cast<BlockArgument>(operand)) {
Block *ownerBlock = ba.getOwner();
unsigned argIndex = ba.getArgNumber();
if (auto func = dyn_cast<func::FuncOp>(ownerBlock->getParentOp())) {
if (&func.front() == ownerBlock) {
- Operands.push_back(std::string("funcArg" + std::to_string(argIndex)));
+ operands.push_back(std::string("funcArg" + std::to_string(argIndex)));
} else {
- Operands.push_back(
+ operands.push_back(
std::string("blockArg" + std::to_string(argIndex)));
}
} else {
- Operands.push_back(std::string("blockArg" + std::to_string(argIndex)));
+ operands.push_back(std::string("blockArg" + std::to_string(argIndex)));
}
}
}
if (op->hasTrait<OpTrait::IsCommutative>())
- llvm::sort(Operands.begin(), Operands.end());
+ llvm::sort(operands.begin(), operands.end());
- Name.append("$");
- for (size_t i = 0, size_ = Operands.size(); i < size_; ++i) {
- Name.append(Operands[i]);
+ name.append("$");
+ for (size_t i = 0, size_ = operands.size(); i < size_; ++i) {
+ name.append(operands[i]);
if (i < size_ - 1)
- Name.append("-");
+ name.append("-");
}
- Name.append("$");
+ name.append("$");
OpBuilder b(op->getContext());
- Location newLoc = NameLoc::get(b.getStringAttr(Name), op->getLoc());
+ Location newLoc = NameLoc::get(b.getStringAttr(name), op->getLoc());
op->setLoc(newLoc);
}
@@ -341,33 +340,33 @@ void NormalizePass::reorderOperationOperandsByName(Operation *op) {
if (op->getNumOperands() == 0)
return;
- SmallVector<std::pair<std::string, Value>, 4> Operands;
+ SmallVector<std::pair<std::string, Value>, 4> operands;
for (Value operand : op->getOperands()) {
- std::string TextRepresentation;
- llvm::raw_string_ostream Stream(TextRepresentation);
- operand.printAsOperand(Stream, flags);
- Operands.push_back({Stream.str(), operand});
+ 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) {
+ 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, size_ = Operands.size(); i < size_; i++) {
- op->setOperand(i, Operands[i].second);
+ for (size_t i = 0, size_ = operands.size(); i < size_; i++) {
+ op->setOperand(i, operands[i].second);
}
}
/// Reorders operations by walking up the tree from each operand of an output
/// operation and reducing the def-use distance.
void NormalizePass::reorderOperations(
- const SmallVector<Operation *, 16> &Outputs) {
+ const SmallVector<Operation *, 16> &outputs) {
llvm::SmallPtrSet<const Operation *, 32> visited;
- for (auto *const op : Outputs)
+ for (auto *const op : outputs)
for (Value operand : op->getOperands())
if (Operation *defOp = operand.getDefiningOp())
reorderOperation(defOp, op, visited);
@@ -394,17 +393,16 @@ void NormalizePass::reorderOperation(
}
void NormalizePass::collectOutputOperations(
- Block &block, SmallVector<Operation *, 16> &Outputs) const noexcept {
+ Block &block, SmallVector<Operation *, 16> &outputs) const noexcept {
for (auto &innerOp : block)
if (isOutput(innerOp))
- Outputs.emplace_back(&innerOp);
+ outputs.emplace_back(&innerOp);
}
/// The following Operations are termed as output:
/// - Terminator operations are outputs
/// - Any operation that implements MemoryEffectOpInterface and reports at
-/// least
-/// one MemoryEffects::Write effect is an output
+/// least one MemoryEffects::Write effect is an output
/// - func::CallOp is treated as an output (calls are conservatively assumed to
/// possibly produce side effects).
bool NormalizePass::isOutput(Operation &op) const noexcept {
@@ -430,34 +428,34 @@ bool NormalizePass::isOutput(Operation &op) const noexcept {
/// def-use tree recursively
llvm::SetVector<int> NormalizePass::getOutputFootprint(
Operation *op, llvm::SmallPtrSet<const Operation *, 32> &visited) const {
- llvm::SetVector<int> Outputs;
+ llvm::SetVector<int> outputsVec;
if (!visited.count(op)) {
visited.insert(op);
if (isOutput(*op)) {
func::FuncOp func = op->getParentOfType<func::FuncOp>();
- unsigned Count = 0;
+ unsigned count = 0;
for (Block &block : func.getRegion())
for (Operation &innerOp : block) {
if (&innerOp == op)
- Outputs.insert(Count);
- Count++;
+ outputsVec.insert(count);
+ count++;
}
- return Outputs;
+ return outputsVec;
}
for (OpOperand &use : op->getUses()) {
Operation *useOp = use.getOwner();
if (useOp) {
- llvm::SetVector<int> OutputsUsingUop =
+ llvm::SetVector<int> outputsUsingUop =
getOutputFootprint(useOp, visited);
- Outputs.insert(OutputsUsingUop.begin(), OutputsUsingUop.end());
+ outputsVec.insert(outputsUsingUop.begin(), outputsUsingUop.end());
}
}
}
- return Outputs;
+ return outputsVec;
}
>From 9d8b682d37cdf66db76aaf7e1aa834ab615e499e Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Tue, 28 Oct 2025 22:09:28 +0530
Subject: [PATCH 26/31] fix redundant includes and namespacing
---
mlir/lib/Conversion/Normalize/Normalize.cpp | 56 +++++++++------------
1 file changed, 23 insertions(+), 33 deletions(-)
diff --git a/mlir/lib/Conversion/Normalize/Normalize.cpp b/mlir/lib/Conversion/Normalize/Normalize.cpp
index b9d5f32a9179b..9e4c1e1d25ba6 100644
--- a/mlir/lib/Conversion/Normalize/Normalize.cpp
+++ b/mlir/lib/Conversion/Normalize/Normalize.cpp
@@ -8,18 +8,12 @@
#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/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 "mlir/Support/LLVM.h"
#include "llvm/ADT/Hashing.h"
-#include "llvm/Support/FormatVariadic.h"
-#include "llvm/Support/raw_ostream.h"
#include <iomanip>
#include <sstream>
@@ -49,21 +43,19 @@ struct NormalizePass : public impl::NormalizeBase<NormalizePass> {
bool isOutput(Operation &op) const noexcept;
void reorderOperations(const SmallVector<Operation *, 16> &outputs);
void reorderOperation(Operation *used, Operation *user,
- llvm::SmallPtrSet<const Operation *, 32> &visited);
+ SmallPtrSet<const Operation *, 32> &visited);
void renameOperations(const SmallVector<Operation *, 16> &outputs);
void renameOperation(Operation *op,
SmallPtrSet<const Operation *, 32> &visited);
bool isInitialOperation(Operation *const op) const noexcept;
- void
- nameAsInitialOperation(Operation *op,
- llvm::SmallPtrSet<const Operation *, 32> &visited);
- void
- nameAsRegularOperation(Operation *op,
- llvm::SmallPtrSet<const Operation *, 32> &visited);
+ void nameAsInitialOperation(Operation *op,
+ SmallPtrSet<const Operation *, 32> &visited);
+ void nameAsRegularOperation(Operation *op,
+ SmallPtrSet<const Operation *, 32> &visited);
bool hasOnlyImmediateOperands(Operation *const op) const noexcept;
- llvm::SetVector<int>
+ SetVector<int>
getOutputFootprint(Operation *op,
- llvm::SmallPtrSet<const Operation *, 32> &visited) const;
+ SmallPtrSet<const Operation *, 32> &visited) const;
void appendRenamedOperands(Operation *op, SmallString<512> &name);
void reorderOperationOperandsByName(Operation *op);
OpPrintingFlags flags{};
@@ -90,7 +82,7 @@ void NormalizePass::runOnOperation() {
void NormalizePass::renameOperations(
const SmallVector<Operation *, 16> &outputs) {
- llvm::SmallPtrSet<const Operation *, 32> visited;
+ SmallPtrSet<const Operation *, 32> visited;
for (auto *op : outputs)
renameOperation(op, visited);
@@ -175,7 +167,7 @@ std::string inline split(std::string_view str, const char &delimiter,
/// arguments, void is appended, else a hash of the definition of the operation
/// is appended.
void NormalizePass::nameAsInitialOperation(
- Operation *op, llvm::SmallPtrSet<const Operation *, 32> &visited) {
+ Operation *op, SmallPtrSet<const Operation *, 32> &visited) {
for (Value operand : op->getOperands())
if (Operation *defOp = operand.getDefiningOp())
@@ -196,7 +188,7 @@ void NormalizePass::nameAsInitialOperation(
name.append("vl" + std::to_string(hash).substr(0, 5));
if (auto call = dyn_cast<func::CallOp>(op)) {
- llvm::StringRef callee = call.getCallee();
+ StringRef callee = call.getCallee();
name.append(callee.str());
}
@@ -233,7 +225,7 @@ void NormalizePass::nameAsInitialOperation(
/// CallOp. A regular operation must have operands, thus the renaming is further
/// handled in appendRenamedOperands.
void NormalizePass::nameAsRegularOperation(
- Operation *op, llvm::SmallPtrSet<const Operation *, 32> &visited) {
+ Operation *op, SmallPtrSet<const Operation *, 32> &visited) {
for (Value operand : op->getOperands())
if (Operation *defOp = operand.getDefiningOp())
@@ -260,7 +252,7 @@ void NormalizePass::nameAsRegularOperation(
name.append("op" + std::to_string(hash).substr(0, 5));
if (auto call = dyn_cast<func::CallOp>(op)) {
- llvm::StringRef callee = call.getCallee();
+ StringRef callee = call.getCallee();
name.append(callee.str());
}
@@ -318,7 +310,7 @@ void NormalizePass::appendRenamedOperands(Operation *op,
}
if (op->hasTrait<OpTrait::IsCommutative>())
- llvm::sort(operands.begin(), operands.end());
+ sort(operands.begin(), operands.end());
name.append("$");
for (size_t i = 0, size_ = operands.size(); i < size_; ++i) {
@@ -350,10 +342,9 @@ void NormalizePass::reorderOperationOperandsByName(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;
- });
+ sort(operands.begin(), operands.end(), [](const auto &a, const auto &b) {
+ return StringRef(a.first).compare_insensitive(b.first) < 0;
+ });
}
for (size_t i = 0, size_ = operands.size(); i < size_; i++) {
@@ -365,7 +356,7 @@ void NormalizePass::reorderOperationOperandsByName(Operation *op) {
/// operation and reducing the def-use distance.
void NormalizePass::reorderOperations(
const SmallVector<Operation *, 16> &outputs) {
- llvm::SmallPtrSet<const Operation *, 32> visited;
+ SmallPtrSet<const Operation *, 32> visited;
for (auto *const op : outputs)
for (Value operand : op->getOperands())
if (Operation *defOp = operand.getDefiningOp())
@@ -374,7 +365,7 @@ void NormalizePass::reorderOperations(
void NormalizePass::reorderOperation(
Operation *used, Operation *user,
- llvm::SmallPtrSet<const Operation *, 32> &visited) {
+ SmallPtrSet<const Operation *, 32> &visited) {
if (!visited.count(used)) {
visited.insert(used);
@@ -426,9 +417,9 @@ bool NormalizePass::isOutput(Operation &op) const noexcept {
/// Helper method returning indices (distance from the beginning of the basic
/// block) of output operations using the given operation. Walks down the
/// def-use tree recursively
-llvm::SetVector<int> NormalizePass::getOutputFootprint(
- Operation *op, llvm::SmallPtrSet<const Operation *, 32> &visited) const {
- llvm::SetVector<int> outputsVec;
+SetVector<int> NormalizePass::getOutputFootprint(
+ Operation *op, SmallPtrSet<const Operation *, 32> &visited) const {
+ SetVector<int> outputsVec;
if (!visited.count(op)) {
visited.insert(op);
@@ -449,8 +440,7 @@ llvm::SetVector<int> NormalizePass::getOutputFootprint(
for (OpOperand &use : op->getUses()) {
Operation *useOp = use.getOwner();
if (useOp) {
- llvm::SetVector<int> outputsUsingUop =
- getOutputFootprint(useOp, visited);
+ SetVector<int> outputsUsingUop = getOutputFootprint(useOp, visited);
outputsVec.insert(outputsUsingUop.begin(), outputsUsingUop.end());
}
>From 319bcae4c7a89e34984c0a786d49839dd6c6a974 Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Tue, 28 Oct 2025 22:17:45 +0530
Subject: [PATCH 27/31] nit
---
mlir/include/mlir/Conversion/Passes.td | 8 +++++---
1 file changed, 5 insertions(+), 3 deletions(-)
diff --git a/mlir/include/mlir/Conversion/Passes.td b/mlir/include/mlir/Conversion/Passes.td
index 6d5ed569eb514..a3e8f51173260 100644
--- a/mlir/include/mlir/Conversion/Passes.td
+++ b/mlir/include/mlir/Conversion/Passes.td
@@ -947,10 +947,12 @@ def ConvertShardToMPIPass : Pass<"convert-shard-to-mpi"> {
// Normalize
//===----------------------------------------------------------------------===//
def Normalize : Pass<"normalize", "ModuleOp"> {
- let summary = "convert MLIR input to its normalized form";
+ let summary = "Convert MLIR input to its normalized form";
let description = [{
- This pass produces reordered MLIR input with determinstic variable
- names to reduce the diff between two semantically similar programs
+ This pass aims to transform MLIR inputs into a normal form by reordering
+ and renaming instructions while preserving the same semantics. The pass
+ makes it easier to spot semantic differences while diffing two modules
+ which have undergone different passes.
}];
}
>From 137f96c1f732949045c3a28ad680e289f5914a06 Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Tue, 28 Oct 2025 22:29:48 +0530
Subject: [PATCH 28/31] nit
---
mlir/lib/Conversion/Normalize/Normalize.cpp | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/mlir/lib/Conversion/Normalize/Normalize.cpp b/mlir/lib/Conversion/Normalize/Normalize.cpp
index 9e4c1e1d25ba6..00330b7a0d5e5 100644
--- a/mlir/lib/Conversion/Normalize/Normalize.cpp
+++ b/mlir/lib/Conversion/Normalize/Normalize.cpp
@@ -352,8 +352,6 @@ void NormalizePass::reorderOperationOperandsByName(Operation *op) {
}
}
-/// Reorders operations by walking up the tree from each operand of an output
-/// operation and reducing the def-use distance.
void NormalizePass::reorderOperations(
const SmallVector<Operation *, 16> &outputs) {
SmallPtrSet<const Operation *, 32> visited;
@@ -363,6 +361,8 @@ void NormalizePass::reorderOperations(
reorderOperation(defOp, op, visited);
}
+/// Reorders operations by walking up the tree from each operand of an output
+/// operation and reducing the def-use distance.
void NormalizePass::reorderOperation(
Operation *used, Operation *user,
SmallPtrSet<const Operation *, 32> &visited) {
>From 9049f11da84819e769779da743af0fc2febe7329 Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Tue, 28 Oct 2025 22:37:33 +0530
Subject: [PATCH 29/31] 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 00330b7a0d5e5..c8072b2ae45d7 100644
--- a/mlir/lib/Conversion/Normalize/Normalize.cpp
+++ b/mlir/lib/Conversion/Normalize/Normalize.cpp
@@ -394,8 +394,8 @@ void NormalizePass::collectOutputOperations(
/// - Terminator operations are outputs
/// - Any operation that implements MemoryEffectOpInterface and reports at
/// least one MemoryEffects::Write effect is an output
-/// - func::CallOp is treated as an output (calls are conservatively assumed to
-/// possibly produce side effects).
+/// - Any operation that implements CallOpInterface is treated as an output
+/// (calls are conservatively assumed to possibly produce side effects).
bool NormalizePass::isOutput(Operation &op) const noexcept {
if (op.hasTrait<OpTrait::IsTerminator>())
return true;
@@ -408,7 +408,7 @@ bool NormalizePass::isOutput(Operation &op) const noexcept {
return true;
}
- if (auto call = dyn_cast<func::CallOp>(op))
+ if (dyn_cast<CallOpInterface>(&op))
return true;
return false;
>From 759e798b20c1eaf923a31b41022e2bffca872164 Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Tue, 28 Oct 2025 22:39:06 +0530
Subject: [PATCH 30/31] nit
---
mlir/lib/Conversion/Normalize/Normalize.cpp | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/mlir/lib/Conversion/Normalize/Normalize.cpp b/mlir/lib/Conversion/Normalize/Normalize.cpp
index c8072b2ae45d7..ce94c9481e9ee 100644
--- a/mlir/lib/Conversion/Normalize/Normalize.cpp
+++ b/mlir/lib/Conversion/Normalize/Normalize.cpp
@@ -415,8 +415,8 @@ bool NormalizePass::isOutput(Operation &op) const noexcept {
}
/// Helper method returning indices (distance from the beginning of the basic
-/// block) of output operations using the given operation. Walks down the
-/// def-use tree recursively
+/// block) of output operations using the given operation. It Walks down the
+/// def-use tree recursively.
SetVector<int> NormalizePass::getOutputFootprint(
Operation *op, SmallPtrSet<const Operation *, 32> &visited) const {
SetVector<int> outputsVec;
>From 4cfc8eb070a88833d367269ae989c9c66e14e611 Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Tue, 28 Oct 2025 22:42:14 +0530
Subject: [PATCH 31/31] nit
---
mlir/lib/Conversion/Normalize/Normalize.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/mlir/lib/Conversion/Normalize/Normalize.cpp b/mlir/lib/Conversion/Normalize/Normalize.cpp
index ce94c9481e9ee..c2b1fdf859d29 100644
--- a/mlir/lib/Conversion/Normalize/Normalize.cpp
+++ b/mlir/lib/Conversion/Normalize/Normalize.cpp
@@ -416,7 +416,7 @@ bool NormalizePass::isOutput(Operation &op) const noexcept {
/// Helper method returning indices (distance from the beginning of the basic
/// block) of output operations using the given operation. It Walks down the
-/// def-use tree recursively.
+/// def-use tree recursively.
SetVector<int> NormalizePass::getOutputFootprint(
Operation *op, SmallPtrSet<const Operation *, 32> &visited) const {
SetVector<int> outputsVec;
More information about the llvm-commits
mailing list