[Mlir-commits] [llvm] [mlir] [WIP][mlir] Add Normalize pass (PR #162266)
Shourya Goel
llvmlistbot at llvm.org
Tue Oct 7 04:45:58 PDT 2025
https://github.com/Sh0g0-1758 created https://github.com/llvm/llvm-project/pull/162266
PR to add an [llvm-canon](https://llvm.org/devmtg/2019-10/slides/Paszkowski-LLVMCanon.pdf) kind of tool for MLIR.
>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 1/8] 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 2/8] 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 3/8] 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 4/8] 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 5/8] 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 6/8] 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 7/8] 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 8/8] 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);
}
}
}
More information about the Mlir-commits
mailing list