[Mlir-commits] [llvm] [mlir] [WIP][mlir] Add Normalize pass (PR #162266)
llvmlistbot at llvm.org
llvmlistbot at llvm.org
Tue Oct 7 04:49:26 PDT 2025
github-actions[bot] wrote:
<!--LLVM CODE FORMAT COMMENT: {clang-format}-->
:warning: C/C++ code formatter, clang-format found issues in your code. :warning:
<details>
<summary>
You can test this locally with the following command:
</summary>
``````````bash
git-clang-format --diff origin/main HEAD --extensions cpp,h -- mlir/include/mlir/Conversion/Normalize/Normalize.h mlir/lib/Conversion/Normalize/Normalize.cpp mlir/include/mlir/Conversion/Passes.h
``````````
:warning:
The reproduction instructions above might return results for more than one PR
in a stack if you are using a stacked PR workflow. You can limit the results by
changing `origin/main` to the base branch/commit you want to compare against.
:warning:
</details>
<details>
<summary>
View the diff from clang-format here.
</summary>
``````````diff
diff --git a/mlir/include/mlir/Conversion/Passes.h b/mlir/include/mlir/Conversion/Passes.h
index 442ad55e7..aa20e97bf 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 635eb3842..79f3ac185 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
@@ -49,14 +49,19 @@ private:
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 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);
- llvm::SetVector<int> getOutputFootprint(mlir::Operation* op, llvm::SmallPtrSet<const mlir::Operation *, 32> &visited);
- void foldOperation(mlir::Operation* op);
+ llvm::SetVector<int>
+ getOutputFootprint(mlir::Operation *op,
+ llvm::SmallPtrSet<const mlir::Operation *, 32> &visited);
+ void foldOperation(mlir::Operation *op);
mlir::OpPrintingFlags flags{};
};
} // namespace
@@ -77,7 +82,7 @@ void NormalizePass::runOnOperation() {
RenameOperations(Outputs);
- for (Region& region : op.getRegions())
+ for (Region ®ion : op.getRegions())
for (Block &block : region)
for (Operation &innerOp : block)
foldOperation(&innerOp);
@@ -99,11 +104,12 @@ void NormalizePass::SetDeterministicNames(Block &block) {
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) {
+void NormalizePass::RenameOperation(
+ Operation *op, SmallPtrSet<const mlir::Operation *, 32> &visited) {
if (!visited.count(op)) {
visited.insert(op);
@@ -115,11 +121,11 @@ void NormalizePass::RenameOperation(Operation *op, SmallPtrSet<const mlir::Opera
}
}
-bool NormalizePass::isInitialOperation(mlir::Operation* op) {
+bool NormalizePass::isInitialOperation(mlir::Operation *op) {
return !op->use_empty() and hasOnlyImmediateOperands(op);
}
-bool NormalizePass::hasOnlyImmediateOperands(mlir::Operation* op) {
+bool NormalizePass::hasOnlyImmediateOperands(mlir::Operation *op) {
for (mlir::Value operand : op->getOperands())
if (mlir::Operation *defOp = operand.getDefiningOp())
if (!(defOp->hasTrait<OpTrait::ConstantLike>()))
@@ -127,52 +133,50 @@ bool NormalizePass::hasOnlyImmediateOperands(mlir::Operation* op) {
return true;
}
-std::string to_string(uint64_t const hash)
-{
+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;
- uint64_t hash = FNV_OFFSET;
- for (unsigned char c : data)
- {
- hash ^= static_cast<uint64_t>(c);
- hash *= FNV_PRIME;
- }
- return hash;
+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;
}
-void replace(std::string& str, char from, char to) {
- for(auto& it : str) {
- if(it == from) it = to;
+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::stringstream ss{std::string{str}};
std::string item;
- while(std::getline(ss, item, delimiter)) {
+ while (std::getline(ss, item, delimiter)) {
replace(item, ':', '_');
outs.emplace_back(item);
}
return outs;
}
-void NormalizePass::nameAsInitialOperation(mlir::Operation* op) {
+void NormalizePass::nameAsInitialOperation(mlir::Operation *op) {
SmallVector<SmallString<64>, 4> Operands;
- if(op->getNumOperands() == 0) {
+ if (op->getNumOperands() == 0) {
/**
* INFO: Constant operations like arith.constant
*/
- if(auto call = mlir::dyn_cast<mlir::func::CallOp>(op)) {
+ if (auto call = mlir::dyn_cast<mlir::func::CallOp>(op)) {
Operands.push_back(StringRef(std::string{"void"}));
} else {
std::string TextRepresentation;
@@ -209,7 +213,8 @@ void NormalizePass::nameAsInitialOperation(mlir::Operation* op) {
}
}
- if (op->hasTrait<OpTrait::IsCommutative>()) llvm::sort(Operands);
+ if (op->hasTrait<OpTrait::IsCommutative>())
+ llvm::sort(Operands);
uint64_t Hash = MagicHashConstant;
@@ -245,7 +250,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()) {
@@ -270,7 +277,8 @@ void NormalizePass::nameAsRegularOperation(mlir::Operation* op, llvm::SmallPtrSe
}
}
- if (op->hasTrait<OpTrait::IsCommutative>()) llvm::sort(Operands);
+ if (op->hasTrait<OpTrait::IsCommutative>())
+ llvm::sort(Operands);
uint64_t Hash = MagicHashConstant;
@@ -281,9 +289,11 @@ void NormalizePass::nameAsRegularOperation(mlir::Operation* op, llvm::SmallPtrSe
for (mlir::Value operand : op->getOperands())
if (mlir::Operation *defOp = operand.getDefiningOp())
- OperandsOpcodes.push_back(kernel_hash(defOp->getName().getStringRef().str()));
+ OperandsOpcodes.push_back(
+ kernel_hash(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);
@@ -312,21 +322,26 @@ void NormalizePass::nameAsRegularOperation(mlir::Operation* op, llvm::SmallPtrSe
}
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;
+ 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;
+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;
+ if (!starts_with(opName, "%op"))
+ return;
SmallVector<std::string, 4> Operands;
@@ -338,9 +353,10 @@ void NormalizePass::foldOperation(mlir::Operation* op) {
defOp->print(Stream, state);
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);
@@ -348,7 +364,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));
@@ -397,7 +414,8 @@ void NormalizePass::reorderOperation(
}
}
-void NormalizePass::collectOutputOperations(Block &block, SmallVector<Operation *, 16> &Outputs) {
+void NormalizePass::collectOutputOperations(
+ Block &block, SmallVector<Operation *, 16> &Outputs) {
for (Operation &innerOp : block)
if (isOutput(innerOp))
Outputs.emplace_back(&innerOp);
@@ -415,12 +433,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);
@@ -429,11 +450,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;
@@ -442,7 +463,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());
}
``````````
</details>
https://github.com/llvm/llvm-project/pull/162266
More information about the Mlir-commits
mailing list