From 38973424260e1e1371d3113ef34643af3848d038 Mon Sep 17 00:00:00 2001 From: Sh0g0-1758 Date: Sat, 20 Sep 2025 16:58:13 +0530 Subject: [PATCH 01/36] 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 + +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() = default; + + void runOnOperation() override; +}; +} // namespace + +#include + +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 Date: Wed, 24 Sep 2025 21:45:44 +0530 Subject: [PATCH 02/36] 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() = default; + NormalizePass() = default; + + void runOnOperation() override; +private: + const uint64_t MagicHashConstant = 0x6acaa36bef8325c5ULL; + void collectOutputOperations(Block &block, SmallVector& Output); + bool isOutput(Operation& op); + void reorderOperations(SmallVector& Outputs); + void reorderOperation( + mlir::Operation *used, + mlir::Operation *user, + llvm::SmallPtrSet &visited); + void RenameOperations(Block &block, SmallVector& Outputs); - void runOnOperation() override; }; } // namespace #include 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 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 Outputs; + + for (Region ®ion : op.getRegions()) { + for (Block &block : region) { + RenameOperations(block, Outputs); + } + } + } +} + +void NormalizePass::RenameOperations(Block &block, SmallVector& Outputs) { + static size_t VarCounter = 0; + for(Operation& innerOp : block) { + /** + * TODO: Renaming scheme + */ + } +} + + +void NormalizePass::reorderOperations(SmallVector& Outputs) { + llvm::SmallPtrSet 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 &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& Outputs) { + for(Operation& innerOp : block) { + if(isOutput(innerOp)) { + Outputs.emplace_back(&innerOp); + } + } +} + +bool NormalizePass::isOutput(Operation& op) { + if (op.hasTrait()) + return true; + + if (auto memOp = dyn_cast(&op)) { + SmallVector effects; + memOp.getEffects(effects); + for (auto &effect : effects) { + if (isa( effect.getEffect() )) return true; + } + } + + return false; } From dda4e0b7b36c62e2910d02f3cad0e059dc846d25 Mon Sep 17 00:00:00 2001 From: Sh0g0-1758 Date: Sat, 4 Oct 2025 14:19:28 +0530 Subject: [PATCH 03/36] 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 { mlir::Operation *used, mlir::Operation *user, llvm::SmallPtrSet &visited); - void RenameOperations(Block &block, SmallVector& Outputs); + void RenameOperations(Block &block); }; } // namespace -#include - void NormalizePass::runOnOperation() { ModuleOp module = getOperation(); for (Operation &op : module.getOps()) { - llvm::outs() << "Operation name: " << op.getName() << "\n"; SmallVector 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 Outputs; for (Region ®ion : op.getRegions()) { for (Block &block : region) { - RenameOperations(block, Outputs); + RenameOperations(block); } } } } -void NormalizePass::RenameOperations(Block &block, SmallVector& 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& Outputs) { llvm::SmallPtrSet visited; for (auto *op : Outputs) { From fe9a6fa4c6feca3bf1b46c32cd1a8b535d97534c Mon Sep 17 00:00:00 2001 From: Sh0g0-1758 Date: Sat, 4 Oct 2025 18:31:40 +0530 Subject: [PATCH 04/36] 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 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() = default; +struct NormalizePass : public impl::NormalizeBase { + NormalizePass() = default; - void runOnOperation() override; -private: - const uint64_t MagicHashConstant = 0x6acaa36bef8325c5ULL; - void collectOutputOperations(Block &block, SmallVector& Output); - bool isOutput(Operation& op); - void reorderOperations(SmallVector& Outputs); - void reorderOperation( - mlir::Operation *used, - mlir::Operation *user, - llvm::SmallPtrSet &visited); - void RenameOperations(Block &block); + void runOnOperation() override; +private: + const uint64_t MagicHashConstant = 0x6acaa36bef8325c5ULL; + void collectOutputOperations(Block &block, + SmallVector &Output); + bool isOutput(mlir::Operation &op); + void reorderOperations(SmallVector &Outputs); + void + reorderOperation(mlir::Operation *used, mlir::Operation *user, + llvm::SmallPtrSet &visited); + void RenameOperations(SmallVector &Outputs); + void RenameOperation(mlir::Operation* op, SmallPtrSet &visited); + bool isInitialOperation(mlir::Operation* op); + void nameAsInitialOperation(mlir::Operation* op); + void nameAsRegularOperation(mlir::Operation* op, llvm::SmallPtrSet &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 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 Outputs; - for (Operation &op : module.getOps()) { - SmallVector 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& Outputs) { +void NormalizePass::RenameOperations(SmallVector &Outputs) { llvm::SmallPtrSet visited; - for (auto *op : Outputs) { + + for(auto *op : Outputs) + RenameOperation(op, visited); +} + +void NormalizePass::RenameOperation(Operation *op, SmallPtrSet &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())) + 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(c); + hash *= FNV_PRIME; + } + return hash; +} + +void NormalizePass::nameAsInitialOperation(mlir::Operation* op) { + SmallVector, 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()) 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 Visited; + // Get output footprint for I. + SetVector 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(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 &visited) { + +} + +void NormalizePass::reorderOperations(SmallVector &Outputs) { + llvm::SmallPtrSet 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& Outputs) { - for(Operation& innerOp : block) { - if(isOutput(innerOp)) { - Outputs.emplace_back(&innerOp); - } +void NormalizePass::collectOutputOperations( + Block &block, SmallVector &Outputs) { + llvm::SmallPtrSet visited; + for (Operation &innerOp : block) { + RenameOperation(&innerOp, visited); + if (isOutput(innerOp)) + Outputs.emplace_back(&innerOp); } } -bool NormalizePass::isOutput(Operation& op) { - if (op.hasTrait()) - return true; +bool NormalizePass::isOutput(Operation &op) { + if (op.hasTrait()) + return true; - if (auto memOp = dyn_cast(&op)) { - SmallVector effects; - memOp.getEffects(effects); - for (auto &effect : effects) { - if (isa( effect.getEffect() )) return true; - } - } + if (auto memOp = dyn_cast(&op)) { + SmallVector effects; + memOp.getEffects(effects); + for (auto &effect : effects) + if (isa(effect.getEffect())) + return true; + } - return false; + return false; } From 1a8ef2f6191f753f31e8d66f3ec81ae8d84f6803 Mon Sep 17 00:00:00 2001 From: Sh0g0-1758 Date: Sat, 4 Oct 2025 23:08:13 +0530 Subject: [PATCH 05/36] 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 { void nameAsRegularOperation(mlir::Operation* op, llvm::SmallPtrSet &visited); bool hasOnlyImmediateOperands(mlir::Operation* op); void SetDeterministicNames(Block &block); + llvm::SetVector getOutputFootprint(mlir::Operation* op, llvm::SmallPtrSet &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 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 Visited; - // Get output footprint for I. - SetVector OutputFootprint = getOutputFootprint(I, Visited); + SmallPtrSet Visited; + SetVector 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(I)) { - Function *F = CI->getCalledFunction(); - - if (F != nullptr) { - Name.append(F->getName()); - } + if (auto call = mlir::dyn_cast(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 &visited) { + SmallVector, 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()) 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 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()) 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(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 &Outputs) { @@ -238,14 +287,10 @@ void NormalizePass::reorderOperation( } } -void NormalizePass::collectOutputOperations( - Block &block, SmallVector &Outputs) { - llvm::SmallPtrSet visited; - for (Operation &innerOp : block) { - RenameOperation(&innerOp, visited); +void NormalizePass::collectOutputOperations(Block &block, SmallVector &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 NormalizePass::getOutputFootprint(mlir::Operation* op, llvm::SmallPtrSet &visited) { + llvm::SetVector Outputs; + if (!visited.count(op)) { + visited.insert(op); + + if (isOutput(*op)) { + mlir::func::FuncOp func = op->getParentOfType(); + + 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 OutputsUsingUop = getOutputFootprint(useOp, visited); + + Outputs.insert(OutputsUsingUop.begin(), OutputsUsingUop.end()); + } + } + } + + return Outputs; +} From 48aa67b8d81a7d4e779aa299e7c42fe334ba3002 Mon Sep 17 00:00:00 2001 From: Sh0g0-1758 Date: Sun, 5 Oct 2025 01:36:19 +0530 Subject: [PATCH 06/36] 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 +#include namespace mlir { #define GEN_PASS_DEF_NORMALIZE @@ -55,10 +56,13 @@ struct NormalizePass : public impl::NormalizeBase { bool hasOnlyImmediateOperands(mlir::Operation* op); void SetDeterministicNames(Block &block); llvm::SetVector getOutputFootprint(mlir::Operation* op, llvm::SmallPtrSet &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 split(std::string_view str, char delimiter) { + std::vector 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, 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(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(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(op)) return true; + return false; } From 8a9efd1d656afd461961be96be881a1b6cd95c36 Mon Sep 17 00:00:00 2001 From: Sh0g0-1758 Date: Tue, 7 Oct 2025 16:50:57 +0530 Subject: [PATCH 07/36] 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 { bool hasOnlyImmediateOperands(mlir::Operation* op); void SetDeterministicNames(Block &block); llvm::SetVector getOutputFootprint(mlir::Operation* op, llvm::SmallPtrSet &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, SmallPtrSetgetName() << " --> "; - 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, 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()) 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 Date: Tue, 7 Oct 2025 17:09:43 +0530 Subject: [PATCH 08/36] 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, 4> Operands; + SmallVector 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 Date: Tue, 7 Oct 2025 17:48:00 +0530 Subject: [PATCH 09/36] 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 { void nameAsInitialOperation(mlir::Operation* op); void nameAsRegularOperation(mlir::Operation* op, llvm::SmallPtrSet &visited); bool hasOnlyImmediateOperands(mlir::Operation* op); - void SetDeterministicNames(Block &block); llvm::SetVector getOutputFootprint(mlir::Operation* op, llvm::SmallPtrSet &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, 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()) { + 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 &Outputs) { llvm::SmallPtrSet visited; for (auto *op : Outputs) From 9c0e8669fe250faec17a177968b1631160ccabcb Mon Sep 17 00:00:00 2001 From: Sh0g0-1758 Date: Fri, 10 Oct 2025 07:19:57 +0530 Subject: [PATCH 10/36] 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 #include +#include namespace mlir { #define GEN_PASS_DEF_NORMALIZE @@ -41,22 +41,31 @@ struct NormalizePass : public impl::NormalizeBase { private: const uint64_t MagicHashConstant = 0x6acaa36bef8325c5ULL; - void collectOutputOperations(Block &block, - SmallVector &Output); - bool isOutput(mlir::Operation &op); - void reorderOperations(SmallVector &Outputs); + void + collectOutputOperations(Block &block, + SmallVector &Output) const noexcept; + bool isOutput(mlir::Operation &op) const noexcept; + + void reorderOperations(const SmallVector &Outputs); void reorderOperation(mlir::Operation *used, mlir::Operation *user, llvm::SmallPtrSet &visited); - void RenameOperations(SmallVector &Outputs); - void RenameOperation(mlir::Operation* op, SmallPtrSet &visited); - bool isInitialOperation(mlir::Operation* op); - void nameAsInitialOperation(mlir::Operation* op); - void nameAsRegularOperation(mlir::Operation* op, llvm::SmallPtrSet &visited); - bool hasOnlyImmediateOperands(mlir::Operation* op); - llvm::SetVector getOutputFootprint(mlir::Operation* op, llvm::SmallPtrSet &visited); - void foldOperation(mlir::Operation* op); - void reorderOperationOperandsByName(mlir::Operation* op); + + void RenameOperations(const SmallVector &Outputs); + void RenameOperation(mlir::Operation *op, + SmallPtrSet &visited); + + bool isInitialOperation(mlir::Operation *const op) const noexcept; + void nameAsInitialOperation(mlir::Operation *op); + void nameAsRegularOperation( + mlir::Operation *op, + llvm::SmallPtrSet &visited); + bool hasOnlyImmediateOperands(mlir::Operation *const op) const noexcept; + llvm::SetVector + getOutputFootprint(mlir::Operation *op, + llvm::SmallPtrSet &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 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 &Outputs) { +void NormalizePass::RenameOperations( + const SmallVector &Outputs) { llvm::SmallPtrSet visited; - for(auto *op : Outputs) + for (auto *op : Outputs) RenameOperation(op, visited); } -void NormalizePass::RenameOperation(Operation *op, SmallPtrSet &visited) { +void NormalizePass::RenameOperation( + Operation *op, SmallPtrSet &visited) { if (!visited.count(op)) { visited.insert(op); @@ -108,11 +118,13 @@ void NormalizePass::RenameOperation(Operation *op, SmallPtrSetuse_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())) @@ -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(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(c); + hash *= FNV_PRIME; } + return hash; } -std::vector split(std::string_view str, char delimiter) { - std::vector 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, 4> Operands; - if(op->getNumOperands() == 0) { - /** - * INFO: Constant operations like arith.constant - */ - if(auto call = mlir::dyn_cast(op)) { + if (op->getNumOperands() == 0) { + if (auto call = mlir::dyn_cast(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()) llvm::sort(Operands); + if (op->hasTrait()) + 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 Visited; @@ -238,7 +237,9 @@ void NormalizePass::nameAsInitialOperation(mlir::Operation* op) { op->setLoc(newLoc); } -void NormalizePass::nameAsRegularOperation(mlir::Operation* op, llvm::SmallPtrSet &visited) { +void NormalizePass::nameAsRegularOperation( + mlir::Operation *op, + llvm::SmallPtrSet &visited) { SmallVector, 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()) llvm::sort(Operands); + if (op->hasTrait()) + 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 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()) llvm::sort(OperandsOpcodes.begin(), OperandsOpcodes.end()); + if (op->hasTrait()) + 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 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()) llvm::sort(Operands.begin(), Operands.end()); + if (op->hasTrait()) + 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, 4> Operands; @@ -374,20 +379,21 @@ void NormalizePass::reorderOperationOperandsByName(mlir::Operation* op) { } if (op->hasTrait()) { - 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 &Outputs) { +void NormalizePass::reorderOperations( + const SmallVector &Outputs) { llvm::SmallPtrSet 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 &Outputs) { void NormalizePass::reorderOperation( mlir::Operation *used, mlir::Operation *user, llvm::SmallPtrSet &visited) { - if (!visited.count(used)) { visited.insert(used); @@ -414,13 +419,14 @@ void NormalizePass::reorderOperation( } } -void NormalizePass::collectOutputOperations(Block &block, SmallVector &Outputs) { - for (Operation &innerOp : block) +void NormalizePass::collectOutputOperations( + Block &block, SmallVector &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()) return true; @@ -432,12 +438,15 @@ bool NormalizePass::isOutput(Operation &op) { return true; } - if (auto call = mlir::dyn_cast(op)) return true; + if (auto call = mlir::dyn_cast(op)) + return true; return false; } -llvm::SetVector NormalizePass::getOutputFootprint(mlir::Operation* op, llvm::SmallPtrSet &visited) { +llvm::SetVector NormalizePass::getOutputFootprint( + mlir::Operation *op, + llvm::SmallPtrSet &visited) { llvm::SetVector Outputs; if (!visited.count(op)) { visited.insert(op); @@ -446,11 +455,11 @@ llvm::SetVector NormalizePass::getOutputFootprint(mlir::Operation* op, llvm mlir::func::FuncOp func = op->getParentOfType(); 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 NormalizePass::getOutputFootprint(mlir::Operation* op, llvm for (mlir::OpOperand &use : op->getUses()) { mlir::Operation *useOp = use.getOwner(); if (useOp) { - llvm::SetVector OutputsUsingUop = getOutputFootprint(useOp, visited); + llvm::SetVector OutputsUsingUop = + getOutputFootprint(useOp, visited); Outputs.insert(OutputsUsingUop.begin(), OutputsUsingUop.end()); } From 0836dad2d2e03defdd1cf9735414e4eef57ea18b Mon Sep 17 00:00:00 2001 From: Sh0g0-1758 Date: Fri, 10 Oct 2025 07:28:49 +0530 Subject: [PATCH 11/36] 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 { mlir::Operation *op, llvm::SmallPtrSet &visited); bool hasOnlyImmediateOperands(mlir::Operation *const op) const noexcept; - llvm::SetVector - getOutputFootprint(mlir::Operation *op, - llvm::SmallPtrSet &visited); + llvm::SetVector getOutputFootprint( + mlir::Operation *op, + llvm::SmallPtrSet &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 NormalizePass::getOutputFootprint( mlir::Operation *op, - llvm::SmallPtrSet &visited) { + llvm::SmallPtrSet &visited) const { llvm::SetVector Outputs; if (!visited.count(op)) { visited.insert(op); From ad150397c7a64e0ce531ec07bb201db371a59508 Mon Sep 17 00:00:00 2001 From: Sh0g0-1758 Date: Fri, 10 Oct 2025 07:36:29 +0530 Subject: [PATCH 12/36] 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 Date: Fri, 10 Oct 2025 15:22:12 +0530 Subject: [PATCH 13/36] 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 Date: Fri, 10 Oct 2025 18:28:38 +0530 Subject: [PATCH 14/36] 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 { reorderOperation(mlir::Operation *used, mlir::Operation *user, llvm::SmallPtrSet &visited); - void RenameOperations(const SmallVector &Outputs); + void renameOperations(const SmallVector &Outputs); void RenameOperation(mlir::Operation *op, SmallPtrSet &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 &Outputs) { llvm::SmallPtrSet 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(operand)) { + mlir::Block *ownerBlock = ba.getOwner(); + unsigned argIndex = ba.getArgNumber(); + if (auto func = dyn_cast(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(operand)) { + mlir::Block *ownerBlock = ba.getOwner(); + unsigned argIndex = ba.getArgNumber(); + if (auto func = dyn_cast(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, %[[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 +// CHECK: cf.br ^bb1(%op15672$op27844-vl15894$, %vl15894$1e72e-blockArg1$ : i32, i32) +// CHECK: } +// CHECK: } +func.func @test(%arg0: memref, %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 + 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 Date: Fri, 10 Oct 2025 18:42:12 +0530 Subject: [PATCH 15/36] 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 { SmallPtrSet &visited); bool isInitialOperation(mlir::Operation *const op) const noexcept; - void nameAsInitialOperation(mlir::Operation *op); + void nameAsInitialOperation( + mlir::Operation *op, + llvm::SmallPtrSet &visited); void nameAsRegularOperation( mlir::Operation *op, llvm::SmallPtrSet &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 &visited) { SmallVector, 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(operand)) { mlir::Block *ownerBlock = ba.getOwner(); unsigned argIndex = ba.getArgNumber(); - if (auto func = dyn_cast(ownerBlock->getParentOp())) { + if (auto func = + dyn_cast(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(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 Date: Fri, 10 Oct 2025 19:11:09 +0530 Subject: [PATCH 16/36] 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 &visited) { - SmallVector, 4> Operands; - if (op->getNumOperands() == 0) { - if (auto call = mlir::dyn_cast(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(operand)) { - mlir::Block *ownerBlock = ba.getOwner(); - unsigned argIndex = ba.getArgNumber(); - if (auto func = - dyn_cast(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()) - 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(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 &visited) { - SmallVector, 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(operand)) { - mlir::Block *ownerBlock = ba.getOwner(); - unsigned argIndex = ba.getArgNumber(); - if (auto func = dyn_cast(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()) - 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 Operands; @@ -354,7 +284,20 @@ void NormalizePass::foldOperation(mlir::Operation *op) { } else { Operands.push_back(name); } - } + } else if (auto ba = dyn_cast(operand)) { + mlir::Block *ownerBlock = ba.getOwner(); + unsigned argIndex = ba.getArgNumber(); + if (auto func = + dyn_cast(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()) 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 Date: Fri, 10 Oct 2025 19:12:39 +0530 Subject: [PATCH 17/36] 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(operand)) { - mlir::Block *ownerBlock = ba.getOwner(); - unsigned argIndex = ba.getArgNumber(); - if (auto func = - dyn_cast(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(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()) From 22cdd9182c7f3a95b8fed57cd63689a728420152 Mon Sep 17 00:00:00 2001 From: Sh0g0-1758 Date: Fri, 10 Oct 2025 19:30:41 +0530 Subject: [PATCH 18/36] 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, %[[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 -// 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, %arg1: i32) { From 76df86866e3204ee0fb4911c460f387baa9828bc Mon Sep 17 00:00:00 2001 From: Sh0g0-1758 Date: Fri, 10 Oct 2025 19:51:02 +0530 Subject: [PATCH 19/36] 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 - 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 Date: Fri, 10 Oct 2025 19:51:46 +0530 Subject: [PATCH 20/36] 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 + namespace mlir { class Pass; From 1823295e53c946ed5dbdb938c269cb7369db99ce Mon Sep 17 00:00:00 2001 From: Sh0g0-1758 Date: Fri, 10 Oct 2025 19:54:59 +0530 Subject: [PATCH 21/36] 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, %[[ARG1:.*]]: i32) { +// CHECK: func.func @infinte_loop(%[[ARG0:.*]]: memref, %[[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, %arg1: i32) { +func.func @infinte_loop(%arg0: memref, %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@users.noreply.github.com> Date: Fri, 10 Oct 2025 19:57:35 +0530 Subject: [PATCH 22/36] 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 Date: Fri, 17 Oct 2025 15:48:25 +0530 Subject: [PATCH 23/36] 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 { void collectOutputOperations(Block &block, SmallVector &Output) const noexcept; - bool isOutput(mlir::Operation &op) const noexcept; - - void reorderOperations(const SmallVector &Outputs); - void - reorderOperation(mlir::Operation *used, mlir::Operation *user, - llvm::SmallPtrSet &visited); - + bool isOutput(Operation &op) const noexcept; + void reorderOperations(const SmallVector &Outputs); + void reorderOperation(Operation *used, Operation *user, + llvm::SmallPtrSet &visited); void renameOperations(const SmallVector &Outputs); - void RenameOperation(mlir::Operation *op, - SmallPtrSet &visited); - - bool isInitialOperation(mlir::Operation *const op) const noexcept; - void nameAsInitialOperation( - mlir::Operation *op, - llvm::SmallPtrSet &visited); - void nameAsRegularOperation( - mlir::Operation *op, - llvm::SmallPtrSet &visited); - bool hasOnlyImmediateOperands(mlir::Operation *const op) const noexcept; - llvm::SetVector getOutputFootprint( - mlir::Operation *op, - llvm::SmallPtrSet &visited) const; - void foldOperation(mlir::Operation *op); - void reorderOperationOperandsByName(mlir::Operation *op); - mlir::OpPrintingFlags flags{}; + void RenameOperation(Operation *op, + SmallPtrSet &visited); + bool isInitialOperation(Operation *const op) const noexcept; + void + nameAsInitialOperation(Operation *op, + llvm::SmallPtrSet &visited); + void + nameAsRegularOperation(Operation *op, + llvm::SmallPtrSet &visited); + bool hasOnlyImmediateOperands(Operation *const op) const noexcept; + llvm::SetVector + getOutputFootprint(Operation *op, + llvm::SmallPtrSet &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 &Outputs) { - llvm::SmallPtrSet visited; + llvm::SmallPtrSet visited; for (auto *op : Outputs) RenameOperation(op, visited); } void NormalizePass::RenameOperation( - Operation *op, SmallPtrSet &visited) { + Operation *op, SmallPtrSet &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())) 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 &visited) { + Operation *op, llvm::SmallPtrSet &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 Visited; + SmallPtrSet Visited; SetVector 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(op)) { + if (auto call = dyn_cast(op)) { llvm::StringRef callee = call.getCallee(); Name.append(callee.str()); } if (op->getNumOperands() == 0) { Name.append("$"); - if (auto call = mlir::dyn_cast(op)) { + if (auto call = dyn_cast(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 &visited) { + Operation *op, llvm::SmallPtrSet &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 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()) @@ -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(op)) { + if (auto call = dyn_cast(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 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(operand)) { - mlir::Block *ownerBlock = ba.getOwner(); + } else if (auto ba = dyn_cast(operand)) { + Block *ownerBlock = ba.getOwner(); unsigned argIndex = ba.getArgNumber(); - if (auto func = dyn_cast(ownerBlock->getParentOp())) { + if (auto func = dyn_cast(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, 4> Operands; + SmallVector, 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 &Outputs) { - llvm::SmallPtrSet visited; + llvm::SmallPtrSet 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 &visited) { + Operation *used, Operation *user, + llvm::SmallPtrSet &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(op)) + if (auto call = dyn_cast(op)) return true; return false; } llvm::SetVector NormalizePass::getOutputFootprint( - mlir::Operation *op, - llvm::SmallPtrSet &visited) const { + Operation *op, llvm::SmallPtrSet &visited) const { llvm::SetVector Outputs; if (!visited.count(op)) { visited.insert(op); if (isOutput(*op)) { - mlir::func::FuncOp func = op->getParentOfType(); + func::FuncOp func = op->getParentOfType(); 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 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 OutputsUsingUop = getOutputFootprint(useOp, visited); From 2313810b541c358e05d1ed8817e5c4979483cdf6 Mon Sep 17 00:00:00 2001 From: Sh0g0-1758 Date: Fri, 17 Oct 2025 18:00:02 +0530 Subject: [PATCH 24/36] 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() = 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 { void reorderOperation(Operation *used, Operation *user, llvm::SmallPtrSet &visited); void renameOperations(const SmallVector &Outputs); - void RenameOperation(Operation *op, + void renameOperation(Operation *op, SmallPtrSet &visited); bool isInitialOperation(Operation *const op) const noexcept; void @@ -62,12 +64,13 @@ struct NormalizePass : public impl::NormalizeBase { llvm::SetVector getOutputFootprint(Operation *op, llvm::SmallPtrSet &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 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 &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()) + 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 &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(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 &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 Operands; @@ -296,9 +321,6 @@ void NormalizePass::foldOperation(Operation *op) { if (op->hasTrait()) 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 &Outputs) { llvm::SmallPtrSet 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()) 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 NormalizePass::getOutputFootprint( Operation *op, llvm::SmallPtrSet &visited) const { llvm::SetVector Outputs; From 57c0292375d4dd8793f50d8d28d5daee369ce8a1 Mon Sep 17 00:00:00 2001 From: Sh0g0-1758 Date: Mon, 20 Oct 2025 01:50:07 +0530 Subject: [PATCH 25/36] 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 { 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 &Output) const noexcept; + SmallVector &outputs) const noexcept; bool isOutput(Operation &op) const noexcept; - void reorderOperations(const SmallVector &Outputs); + void reorderOperations(const SmallVector &outputs); void reorderOperation(Operation *used, Operation *user, llvm::SmallPtrSet &visited); - void renameOperations(const SmallVector &Outputs); + void renameOperations(const SmallVector &outputs); void renameOperation(Operation *op, SmallPtrSet &visited); bool isInitialOperation(Operation *const op) const noexcept; @@ -64,7 +64,7 @@ struct NormalizePass : public impl::NormalizeBase { llvm::SetVector getOutputFootprint(Operation *op, llvm::SmallPtrSet &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 Outputs; + SmallVector 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 &Outputs) { + const SmallVector &outputs) { llvm::SmallPtrSet 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 Visited; - SetVector OutputFootprint = getOutputFootprint(op, Visited); + SmallPtrSet visitedLocal; + SetVector 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(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(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 OperandsOpcodes; + SmallVector 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()) - 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(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 Operands; + SmallVector 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(operand)) { Block *ownerBlock = ba.getOwner(); unsigned argIndex = ba.getArgNumber(); if (auto func = dyn_cast(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()) - 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, 4> Operands; + SmallVector, 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()) { 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 &Outputs) { + const SmallVector &outputs) { llvm::SmallPtrSet 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 &Outputs) const noexcept { + Block &block, SmallVector &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 NormalizePass::getOutputFootprint( Operation *op, llvm::SmallPtrSet &visited) const { - llvm::SetVector Outputs; + llvm::SetVector outputsVec; if (!visited.count(op)) { visited.insert(op); if (isOutput(*op)) { func::FuncOp func = op->getParentOfType(); - 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 OutputsUsingUop = + llvm::SetVector 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 Date: Tue, 28 Oct 2025 22:09:28 +0530 Subject: [PATCH 26/36] 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 #include @@ -49,21 +43,19 @@ struct NormalizePass : public impl::NormalizeBase { bool isOutput(Operation &op) const noexcept; void reorderOperations(const SmallVector &outputs); void reorderOperation(Operation *used, Operation *user, - llvm::SmallPtrSet &visited); + SmallPtrSet &visited); void renameOperations(const SmallVector &outputs); void renameOperation(Operation *op, SmallPtrSet &visited); bool isInitialOperation(Operation *const op) const noexcept; - void - nameAsInitialOperation(Operation *op, - llvm::SmallPtrSet &visited); - void - nameAsRegularOperation(Operation *op, - llvm::SmallPtrSet &visited); + void nameAsInitialOperation(Operation *op, + SmallPtrSet &visited); + void nameAsRegularOperation(Operation *op, + SmallPtrSet &visited); bool hasOnlyImmediateOperands(Operation *const op) const noexcept; - llvm::SetVector + SetVector getOutputFootprint(Operation *op, - llvm::SmallPtrSet &visited) const; + SmallPtrSet &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 &outputs) { - llvm::SmallPtrSet visited; + SmallPtrSet 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 &visited) { + Operation *op, SmallPtrSet &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(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 &visited) { + Operation *op, SmallPtrSet &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(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()) - 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()) { - 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 &outputs) { - llvm::SmallPtrSet visited; + SmallPtrSet 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 &visited) { + SmallPtrSet &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 NormalizePass::getOutputFootprint( - Operation *op, llvm::SmallPtrSet &visited) const { - llvm::SetVector outputsVec; +SetVector NormalizePass::getOutputFootprint( + Operation *op, SmallPtrSet &visited) const { + SetVector outputsVec; if (!visited.count(op)) { visited.insert(op); @@ -449,8 +440,7 @@ llvm::SetVector NormalizePass::getOutputFootprint( for (OpOperand &use : op->getUses()) { Operation *useOp = use.getOwner(); if (useOp) { - llvm::SetVector outputsUsingUop = - getOutputFootprint(useOp, visited); + SetVector outputsUsingUop = getOutputFootprint(useOp, visited); outputsVec.insert(outputsUsingUop.begin(), outputsUsingUop.end()); } From 319bcae4c7a89e34984c0a786d49839dd6c6a974 Mon Sep 17 00:00:00 2001 From: Sh0g0-1758 Date: Tue, 28 Oct 2025 22:17:45 +0530 Subject: [PATCH 27/36] 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 Date: Tue, 28 Oct 2025 22:29:48 +0530 Subject: [PATCH 28/36] 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 &outputs) { SmallPtrSet 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 &visited) { From 9049f11da84819e769779da743af0fc2febe7329 Mon Sep 17 00:00:00 2001 From: Sh0g0-1758 Date: Tue, 28 Oct 2025 22:37:33 +0530 Subject: [PATCH 29/36] 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()) return true; @@ -408,7 +408,7 @@ bool NormalizePass::isOutput(Operation &op) const noexcept { return true; } - if (auto call = dyn_cast(op)) + if (dyn_cast(&op)) return true; return false; From 759e798b20c1eaf923a31b41022e2bffca872164 Mon Sep 17 00:00:00 2001 From: Sh0g0-1758 Date: Tue, 28 Oct 2025 22:39:06 +0530 Subject: [PATCH 30/36] 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 NormalizePass::getOutputFootprint( Operation *op, SmallPtrSet &visited) const { SetVector outputsVec; From 4cfc8eb070a88833d367269ae989c9c66e14e611 Mon Sep 17 00:00:00 2001 From: Sh0g0-1758 Date: Tue, 28 Oct 2025 22:42:14 +0530 Subject: [PATCH 31/36] 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 NormalizePass::getOutputFootprint( Operation *op, SmallPtrSet &visited) const { SetVector outputsVec; From f663b14db15dfcd928ef05d0a63288ec32c6a270 Mon Sep 17 00:00:00 2001 From: Sh0g0-1758 Date: Tue, 28 Oct 2025 22:53:01 +0530 Subject: [PATCH 32/36] early break --- mlir/lib/Conversion/Normalize/Normalize.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/mlir/lib/Conversion/Normalize/Normalize.cpp b/mlir/lib/Conversion/Normalize/Normalize.cpp index c2b1fdf859d29..ea7068d6d58b9 100644 --- a/mlir/lib/Conversion/Normalize/Normalize.cpp +++ b/mlir/lib/Conversion/Normalize/Normalize.cpp @@ -427,14 +427,15 @@ SetVector NormalizePass::getOutputFootprint( func::FuncOp func = op->getParentOfType(); unsigned count = 0; - for (Block &block : func.getRegion()) + for (Block &block : func.getRegion()) { for (Operation &innerOp : block) { - if (&innerOp == op) + if (&innerOp == op) { outputsVec.insert(count); + return outputsVec; + } count++; } - - return outputsVec; + } } for (OpOperand &use : op->getUses()) { From 195f88e780ad31d22b060259feadad9a7f490287 Mon Sep 17 00:00:00 2001 From: Sh0g0-1758 Date: Tue, 28 Oct 2025 23:22:17 +0530 Subject: [PATCH 33/36] use std::distance --- mlir/lib/Conversion/Normalize/Normalize.cpp | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/mlir/lib/Conversion/Normalize/Normalize.cpp b/mlir/lib/Conversion/Normalize/Normalize.cpp index ea7068d6d58b9..382fb24b538ef 100644 --- a/mlir/lib/Conversion/Normalize/Normalize.cpp +++ b/mlir/lib/Conversion/Normalize/Normalize.cpp @@ -15,6 +15,7 @@ #include "mlir/Support/LLVM.h" #include "llvm/ADT/Hashing.h" +#include #include #include @@ -428,14 +429,19 @@ SetVector NormalizePass::getOutputFootprint( unsigned count = 0; for (Block &block : func.getRegion()) { - for (Operation &innerOp : block) { - if (&innerOp == op) { - outputsVec.insert(count); - return outputsVec; - } - count++; + if (auto it = std::find_if( + block.begin(), block.end(), + [op](Operation &innerOp) { return &innerOp == op; }); + it != block.end()) { + count += std::distance(block.begin(), it); + outputsVec.insert(count); + break; + } else { + count += std::distance(block.begin(), block.end()); } } + + return outputsVec; } for (OpOperand &use : op->getUses()) { From ac6ce809922d096c432ef57a21f71d1c024e0e1f Mon Sep 17 00:00:00 2001 From: Sh0g0-1758 Date: Tue, 28 Oct 2025 23:48:53 +0530 Subject: [PATCH 34/36] walk the module --- mlir/lib/Conversion/Normalize/Normalize.cpp | 26 ++++++--------------- 1 file changed, 7 insertions(+), 19 deletions(-) diff --git a/mlir/lib/Conversion/Normalize/Normalize.cpp b/mlir/lib/Conversion/Normalize/Normalize.cpp index 382fb24b538ef..33bedcd1cb783 100644 --- a/mlir/lib/Conversion/Normalize/Normalize.cpp +++ b/mlir/lib/Conversion/Normalize/Normalize.cpp @@ -38,9 +38,6 @@ struct NormalizePass : public impl::NormalizeBase { private: // Random constant for hashing, so the state isn't zero. const uint64_t magicHashConstant = 0x6acaa36bef8325c5ULL; - void - collectOutputOperations(Block &block, - SmallVector &outputs) const noexcept; bool isOutput(Operation &op) const noexcept; void reorderOperations(const SmallVector &outputs); void reorderOperation(Operation *used, Operation *user, @@ -69,16 +66,14 @@ void NormalizePass::runOnOperation() { ModuleOp module = getOperation(); - for (auto &op : module.getOps()) { - SmallVector outputs; + SmallVector outputs; + module.walk([&](Operation *op) { + if (isOutput(*op)) + outputs.push_back(op); + }); - for (auto ®ion : op.getRegions()) - for (auto &block : region) - collectOutputOperations(block, outputs); - - reorderOperations(outputs); - renameOperations(outputs); - } + reorderOperations(outputs); + renameOperations(outputs); } void NormalizePass::renameOperations( @@ -384,13 +379,6 @@ void NormalizePass::reorderOperation( } } -void NormalizePass::collectOutputOperations( - Block &block, SmallVector &outputs) const noexcept { - for (auto &innerOp : block) - if (isOutput(innerOp)) - outputs.emplace_back(&innerOp); -} - /// The following Operations are termed as output: /// - Terminator operations are outputs /// - Any operation that implements MemoryEffectOpInterface and reports at From d1373818c0df3cf48206e8bd25a597e610fe48c3 Mon Sep 17 00:00:00 2001 From: Anant jain <129408892+anant37289@users.noreply.github.com> Date: Wed, 29 Oct 2025 01:26:43 +0530 Subject: [PATCH 35/36] Add side-effect testcases --- .../Conversion/Normalize/side-effect.mlir | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 mlir/test/Conversion/Normalize/side-effect.mlir diff --git a/mlir/test/Conversion/Normalize/side-effect.mlir b/mlir/test/Conversion/Normalize/side-effect.mlir new file mode 100644 index 0000000000000..ef5ae17e29ae0 --- /dev/null +++ b/mlir/test/Conversion/Normalize/side-effect.mlir @@ -0,0 +1,66 @@ +// RUN: mlir-opt %s --normalize --mlir-use-nameloc-as-prefix 2>&1 | FileCheck %s + +module { + // CHECK-LABEL: func.func @foo( + // CHECK-SAME: %arg0: i32) -> i32 { + // CHECK: %vl13600$funcArg0-funcArg0$ = arith.addi %arg0, %arg0 : i32 + // CHECK: return %vl13600$funcArg0-funcArg0$ : i32 + // CHECK: } + func.func @foo(%x: i32) -> i32 { + %y = arith.addi %x, %x : i32 + return %y : i32 + } + // CHECK-LABEL: func.func @bar() -> i32 { + // CHECK: %vl10237$20b04$ = arith.constant 0 : i32 + // CHECK: %vl15008$eafb0$ = arith.constant 4 : i32 + // CHECK: %vl70789$vl10237-vl15008$ = arith.addi %vl10237$20b04$, %vl15008$eafb0$ : i32 + // CHECK: %op27844$vl10237-vl70789$ = arith.addi %vl10237$20b04$, %vl70789$vl10237-vl15008$ : i32 + // CHECK: %op27844$op27844-vl15008$ = arith.addi %op27844$vl10237-vl70789$, %vl15008$eafb0$ : i32 + // CHECK: %vl16656$bf67a$ = memref.alloc() : memref<4xi32> + // CHECK: %vl77401$e527e$ = arith.constant 0 : index + // CHECK: memref.store %op27844$op27844-vl15008$, %vl16656$bf67a$[%vl77401$e527e$] : memref<4xi32> + // CHECK: %op15672$op27844-op27844$ = arith.addi %op27844$op27844-vl15008$, %op27844$vl10237-vl70789$ : i32 + // CHECK: %vl70265$62631$ = arith.constant 1 : index + // CHECK: memref.store %op15672$op27844-op27844$, %vl16656$bf67a$[%vl70265$62631$] : memref<4xi32> + // CHECK: %op27844$vl10237-vl70789$_0 = arith.addi %vl10237$20b04$, %vl70789$vl10237-vl15008$ : i32 + // CHECK: %op27844$op27844-vl15008$_1 = arith.addi %op27844$vl10237-vl70789$_0, %vl15008$eafb0$ : i32 + // CHECK: %op15672$op27844-op27844$_2 = arith.addi %op27844$op27844-vl15008$_1, %op27844$vl10237-vl70789$_0 : i32 + // CHECK: %op43149$vl16656-vl77401$ = memref.load %vl16656$bf67a$[%vl77401$e527e$] : memref<4xi32> + // CHECK: %op43149$vl16656-vl70265$ = memref.load %vl16656$bf67a$[%vl70265$62631$] : memref<4xi32> + // CHECK: %op12004$op43149-op43149$ = arith.addi %op43149$vl16656-vl70265$, %op43149$vl16656-vl77401$ : i32 + // CHECK: %op52433foo$op15672$ = call @foo(%op15672$op27844-op27844$_2) : (i32) -> i32 + // CHECK: %op91727$op12004-op52433$ = arith.addi %op12004$op43149-op43149$, %op52433foo$op15672$ : i32 + // CHECK: return %op91727$op12004-op52433$ : i32 + // CHECK: } + + func.func @bar() -> i32{ + %m0 = memref.alloc() : memref<4xi32> + + %c0 = arith.constant 4 : i32 + %zero = arith.constant 0 : i32 + + %idx0 = arith.constant 0 : index + %idx1 = arith.constant 1 : index + + %t = arith.addi %zero, %c0 : i32 + + %t2 = arith.addi %t, %zero : i32 + %t3 = arith.addi %t2, %c0 : i32 + %t4 = arith.addi %t3, %t2 : i32 + + memref.store %t3, %m0[%idx0] : memref<4xi32> + memref.store %t4, %m0[%idx1] : memref<4xi32> + + %a = memref.load %m0[%idx0] : memref<4xi32> + %b = memref.load %m0[%idx1] : memref<4xi32> + + %t5 = arith.addi %t, %zero : i32 + %t6 = arith.addi %t5, %c0 : i32 + %t7 = arith.addi %t6, %t5 : i32 + %t8 = call @foo(%t7) : (i32) -> i32 + + %t9 = arith.addi %a, %b : i32 + %t10 = arith.addi %t9, %t8 : i32 + return %t10 : i32 + } +} From bb41dc42db38aa95f774771c892732ddcfe4747a Mon Sep 17 00:00:00 2001 From: Sh0g0-1758 Date: Wed, 29 Oct 2025 16:24:27 +0530 Subject: [PATCH 36/36] modify infinite-loop test into 2 variants with tree-like dependencies that will produce the same canonical form after the normalize pass reorders them --- .../Conversion/Normalize/infinite-loop.mlir | 866 +++++++++++++++--- 1 file changed, 755 insertions(+), 111 deletions(-) diff --git a/mlir/test/Conversion/Normalize/infinite-loop.mlir b/mlir/test/Conversion/Normalize/infinite-loop.mlir index 2ec50853046b2..1ec6b4551363e 100644 --- a/mlir/test/Conversion/Normalize/infinite-loop.mlir +++ b/mlir/test/Conversion/Normalize/infinite-loop.mlir @@ -1,138 +1,773 @@ // RUN: mlir-opt %s --normalize --mlir-use-nameloc-as-prefix 2>&1 | FileCheck %s // CHECK-LABEL: module { -// CHECK: func.func @infinte_loop(%[[ARG0:.*]]: memref, %[[ARG1:.*]]: i32) { + +// CHECK: func.func @infinte_loop_v1(%[[ARG0:.*]]: memref, %[[ARG1:.*]]: i32) { // CHECK: %vl15969$e5677$ = arith.constant 1 : i32 -// CHECK: %vl15390$funcArg1-vl15969$ = arith.addi %[[ARG1:.*]], %vl15969$e5677$ : 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$blockArg0-vl22288$ = arith.muli %[[VAL_0:.*]], %vl22288$20b04$ : i32 -// CHECK: %vl22288$ded78$ = arith.constant -1 : 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 -// 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: %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 -// 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: %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$_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 -// CHECK: cf.br ^bb1(%op15672$op27844-vl15894$, %vl15894$blockArg1-vl22288$_69 : i32, i32) +// CHECK: ^bb1(%0: i32, %1: i32): +// CHECK: %vl85743$20b04$ = arith.constant 0 : i32 +// CHECK: %vl73800$blockArg0-vl85743$ = arith.muli %0, %vl85743$20b04$ : i32 +// CHECK: %vl85743$ded78$ = arith.constant -1 : i32 +// CHECK: %op51214$vl73800-vl85743$ = arith.xori %vl73800$blockArg0-vl85743$, %vl85743$ded78$ : i32 +// CHECK: %op12693$blockArg0-op51214$ = arith.addi %0, %op51214$vl73800-vl85743$ : i32 +// CHECK: %vl34407$blockArg1-vl85743$ = arith.addi %1, %vl85743$ded78$ : i32 +// CHECK: %op15672$op12693-vl34407$ = arith.addi %op12693$blockArg0-op51214$, %vl34407$blockArg1-vl85743$ : i32 +// CHECK: %op97825$op15672-vl73800$ = arith.muli %op15672$op12693-vl34407$, %vl73800$blockArg0-vl85743$ : i32 +// CHECK: %op51214$op97825-vl85743$ = arith.xori %op97825$op15672-vl73800$, %vl85743$ded78$ : i32 +// CHECK: %op12343$op15672-op51214$ = arith.addi %op15672$op12693-vl34407$, %op51214$op97825-vl85743$ : i32 +// CHECK: %op27844$op12343-vl85743$ = arith.addi %op12343$op15672-op51214$, %vl85743$20b04$ : i32 +// CHECK: %op97825$op27844-op97825$ = arith.muli %op27844$op12343-vl85743$, %op97825$op15672-vl73800$ : i32 +// CHECK: %op51214$op97825-vl85743$_0 = arith.xori %op97825$op27844-op97825$, %vl85743$ded78$ : i32 +// CHECK: %op12343$op27844-op51214$ = arith.addi %op27844$op12343-vl85743$, %op51214$op97825-vl85743$_0 : i32 +// CHECK: %op27844$op12343-vl85743$_1 = arith.addi %op12343$op27844-op51214$, %vl85743$20b04$ : i32 +// CHECK: %op27844$op27844-vl85743$ = arith.addi %op27844$op12343-vl85743$_1, %vl85743$20b04$ : i32 +// CHECK: %op27844$op27844-vl85743$_2 = arith.addi %op27844$op27844-vl85743$, %vl85743$20b04$ : i32 +// CHECK: %op97825$op27844-op97825$_3 = arith.muli %op27844$op12343-vl85743$_1, %op97825$op27844-op97825$ : i32 +// CHECK: %op97825$op27844-op97825$_4 = arith.muli %op27844$op27844-vl85743$_2, %op97825$op27844-op97825$_3 : i32 +// CHECK: %op51214$op97825-vl85743$_5 = arith.xori %op97825$op27844-op97825$_4, %vl85743$ded78$ : i32 +// CHECK: %op12343$op27844-op51214$_6 = arith.addi %op27844$op27844-vl85743$_2, %op51214$op97825-vl85743$_5 : i32 +// CHECK: %op27844$op12343-vl85743$_7 = arith.addi %op12343$op27844-op51214$_6, %vl85743$20b04$ : i32 +// CHECK: %op97825$op27844-op97825$_8 = arith.muli %op27844$op12343-vl85743$_7, %op97825$op27844-op97825$_4 : i32 +// CHECK: %op51214$op97825-vl85743$_9 = arith.xori %op97825$op27844-op97825$_8, %vl85743$ded78$ : i32 +// CHECK: %op12343$op27844-op51214$_10 = arith.addi %op27844$op12343-vl85743$_7, %op51214$op97825-vl85743$_9 : i32 +// CHECK: %op27844$op12343-vl85743$_11 = arith.addi %op12343$op27844-op51214$_10, %vl85743$20b04$ : i32 +// CHECK: %op97825$op27844-op97825$_12 = arith.muli %op27844$op12343-vl85743$_11, %op97825$op27844-op97825$_8 : i32 +// CHECK: %op51214$op97825-vl85743$_13 = arith.xori %op97825$op27844-op97825$_12, %vl85743$ded78$ : i32 +// CHECK: %op12343$op27844-op51214$_14 = arith.addi %op27844$op12343-vl85743$_11, %op51214$op97825-vl85743$_13 : i32 +// CHECK: %op27844$op12343-vl85743$_15 = arith.addi %op12343$op27844-op51214$_14, %vl85743$20b04$ : i32 +// CHECK: %op97825$op27844-op97825$_16 = arith.muli %op27844$op12343-vl85743$_15, %op97825$op27844-op97825$_12 : i32 +// CHECK: %op51214$op97825-vl85743$_17 = arith.xori %op97825$op27844-op97825$_16, %vl85743$ded78$ : i32 +// CHECK: %op12343$op27844-op51214$_18 = arith.addi %op27844$op12343-vl85743$_15, %op51214$op97825-vl85743$_17 : i32 +// CHECK: %op27844$op12343-vl85743$_19 = arith.addi %op12343$op27844-op51214$_18, %vl85743$20b04$ : i32 +// CHECK: %op97825$op27844-op97825$_20 = arith.muli %op27844$op12343-vl85743$_19, %op97825$op27844-op97825$_16 : i32 +// CHECK: %op51214$op97825-vl85743$_21 = arith.xori %op97825$op27844-op97825$_20, %vl85743$ded78$ : i32 +// CHECK: %op12343$op27844-op51214$_22 = arith.addi %op27844$op12343-vl85743$_19, %op51214$op97825-vl85743$_21 : i32 +// CHECK: %vl85743$51850$ = arith.constant -9 : i32 +// CHECK: %vl34407$blockArg1-vl85743$_23 = arith.addi %1, %vl85743$51850$ : i32 +// CHECK: %op17008$vl34407-vl85743$ = arith.muli %vl34407$blockArg1-vl85743$_23, %vl85743$20b04$ : i32 +// CHECK: %op51214$op17008-vl85743$ = arith.xori %op17008$vl34407-vl85743$, %vl85743$ded78$ : i32 +// CHECK: %op12343$op51214-vl34407$ = arith.addi %op51214$op17008-vl85743$, %vl34407$blockArg1-vl85743$_23 : i32 +// CHECK: %op15672$op12343-op12343$ = arith.addi %op12343$op27844-op51214$_22, %op12343$op51214-vl34407$ : i32 +// CHECK: %op97825$op15672-op97825$ = arith.muli %op15672$op12343-op12343$, %op97825$op27844-op97825$_20 : i32 +// CHECK: %op51214$op97825-vl85743$_24 = arith.xori %op97825$op15672-op97825$, %vl85743$ded78$ : i32 +// CHECK: %op12343$op15672-op51214$_25 = arith.addi %op15672$op12343-op12343$, %op51214$op97825-vl85743$_24 : i32 +// CHECK: %op17008$vl34407-vl85743$_26 = arith.muli %vl34407$blockArg1-vl85743$, %vl85743$20b04$ : i32 +// CHECK: %op51214$op17008-vl85743$_27 = arith.xori %op17008$vl34407-vl85743$_26, %vl85743$ded78$ : i32 +// CHECK: %op12343$op51214-vl34407$_28 = arith.addi %op51214$op17008-vl85743$_27, %vl34407$blockArg1-vl85743$ : i32 +// CHECK: %op97825$op12343-op17008$ = arith.muli %op12343$op51214-vl34407$_28, %op17008$vl34407-vl85743$_26 : i32 +// CHECK: %op51214$op97825-vl85743$_29 = arith.xori %op97825$op12343-op17008$, %vl85743$ded78$ : i32 +// CHECK: %op12343$op12343-op51214$ = arith.addi %op12343$op51214-vl34407$_28, %op51214$op97825-vl85743$_29 : i32 +// CHECK: %op27844$op12343-vl85743$_30 = arith.addi %op12343$op12343-op51214$, %vl85743$20b04$ : i32 +// CHECK: %op97825$op27844-op97825$_31 = arith.muli %op27844$op12343-vl85743$_30, %op97825$op12343-op17008$ : i32 +// CHECK: %op51214$op97825-vl85743$_32 = arith.xori %op97825$op27844-op97825$_31, %vl85743$ded78$ : i32 +// CHECK: %op12343$op27844-op51214$_33 = arith.addi %op27844$op12343-vl85743$_30, %op51214$op97825-vl85743$_32 : i32 +// CHECK: %op15672$op12343-op12343$_34 = arith.addi %op12343$op15672-op51214$_25, %op12343$op27844-op51214$_33 : i32 +// CHECK: %op97825$op15672-op97825$_35 = arith.muli %op15672$op12343-op12343$_34, %op97825$op15672-op97825$ : i32 +// CHECK: %op51214$op97825-vl85743$_36 = arith.xori %op97825$op15672-op97825$_35, %vl85743$ded78$ : i32 +// CHECK: %op12343$op15672-op51214$_37 = arith.addi %op15672$op12343-op12343$_34, %op51214$op97825-vl85743$_36 : i32 +// CHECK: %op27844$op12343-vl85743$_38 = arith.addi %op12343$op15672-op51214$_37, %vl85743$20b04$ : i32 +// CHECK: %op97825$op27844-op97825$_39 = arith.muli %op27844$op12343-vl85743$_38, %op97825$op15672-op97825$_35 : i32 +// CHECK: %op51214$op97825-vl85743$_40 = arith.xori %op97825$op27844-op97825$_39, %vl85743$ded78$ : i32 +// CHECK: %op12343$op27844-op51214$_41 = arith.addi %op27844$op12343-vl85743$_38, %op51214$op97825-vl85743$_40 : i32 +// CHECK: %op27844$op12343-vl85743$_42 = arith.addi %op12343$op27844-op51214$_41, %vl85743$20b04$ : i32 +// CHECK: %op97825$op27844-op97825$_43 = arith.muli %op27844$op12343-vl85743$_42, %op97825$op27844-op97825$_39 : i32 +// CHECK: %op51214$op97825-vl85743$_44 = arith.xori %op97825$op27844-op97825$_43, %vl85743$ded78$ : i32 +// CHECK: %op12343$op27844-op51214$_45 = arith.addi %op27844$op12343-vl85743$_42, %op51214$op97825-vl85743$_44 : i32 +// CHECK: %vl85743$7b7de$ = arith.constant -14 : i32 +// CHECK: %vl34407$blockArg1-vl85743$_46 = arith.addi %1, %vl85743$7b7de$ : i32 +// CHECK: %op17008$vl34407-vl85743$_47 = arith.muli %vl34407$blockArg1-vl85743$_46, %vl85743$20b04$ : i32 +// CHECK: %op51214$op17008-vl85743$_48 = arith.xori %op17008$vl34407-vl85743$_47, %vl85743$ded78$ : i32 +// CHECK: %op12343$op51214-vl34407$_49 = arith.addi %op51214$op17008-vl85743$_48, %vl34407$blockArg1-vl85743$_46 : i32 +// CHECK: %op97825$op12343-op17008$_50 = arith.muli %op12343$op51214-vl34407$_49, %op17008$vl34407-vl85743$_47 : i32 +// CHECK: %op51214$op97825-vl85743$_51 = arith.xori %op97825$op12343-op17008$_50, %vl85743$ded78$ : i32 +// CHECK: %op12343$op12343-op51214$_52 = arith.addi %op12343$op51214-vl34407$_49, %op51214$op97825-vl85743$_51 : i32 +// CHECK: %op27844$op12343-vl85743$_53 = arith.addi %op12343$op12343-op51214$_52, %vl85743$20b04$ : i32 +// CHECK: %op97825$op27844-op97825$_54 = arith.muli %op27844$op12343-vl85743$_53, %op97825$op12343-op17008$_50 : i32 +// CHECK: %op51214$op97825-vl85743$_55 = arith.xori %op97825$op27844-op97825$_54, %vl85743$ded78$ : i32 +// CHECK: %op12343$op27844-op51214$_56 = arith.addi %op27844$op12343-vl85743$_53, %op51214$op97825-vl85743$_55 : i32 +// CHECK: %op27844$op12343-vl85743$_57 = arith.addi %op12343$op27844-op51214$_56, %vl85743$20b04$ : i32 +// CHECK: %op97825$op27844-op97825$_58 = arith.muli %op27844$op12343-vl85743$_57, %op97825$op27844-op97825$_54 : i32 +// CHECK: %op51214$op97825-vl85743$_59 = arith.xori %op97825$op27844-op97825$_58, %vl85743$ded78$ : i32 +// CHECK: %op12343$op27844-op51214$_60 = arith.addi %op27844$op12343-vl85743$_57, %op51214$op97825-vl85743$_59 : i32 +// CHECK: %op27844$op12343-vl85743$_61 = arith.addi %op12343$op27844-op51214$_60, %vl85743$20b04$ : i32 +// CHECK: %op97825$op27844-op97825$_62 = arith.muli %op27844$op12343-vl85743$_61, %op97825$op27844-op97825$_58 : i32 +// CHECK: %op51214$op97825-vl85743$_63 = arith.xori %op97825$op27844-op97825$_62, %vl85743$ded78$ : i32 +// CHECK: %op12343$op27844-op51214$_64 = arith.addi %op27844$op12343-vl85743$_61, %op51214$op97825-vl85743$_63 : i32 +// CHECK: %op27844$op12343-vl85743$_65 = arith.addi %op12343$op27844-op51214$_64, %vl85743$20b04$ : i32 +// CHECK: %op15672$op12343-op27844$ = arith.addi %op12343$op27844-op51214$_45, %op27844$op12343-vl85743$_65 : i32 +// CHECK: %op97825$op15672-op97825$_66 = arith.muli %op15672$op12343-op27844$, %op97825$op27844-op97825$_43 : i32 +// CHECK: %op51214$op97825-vl85743$_67 = arith.xori %op97825$op15672-op97825$_66, %vl85743$ded78$ : i32 +// CHECK: %op12343$op15672-op51214$_68 = arith.addi %op15672$op12343-op27844$, %op51214$op97825-vl85743$_67 : i32 +// CHECK: %op15672$op12343-vl34407$ = arith.addi %op12343$op15672-op51214$_68, %vl34407$blockArg1-vl85743$_46 : i32 +// CHECK: %op97825$op15672-op97825$_69 = arith.muli %op15672$op12343-vl34407$, %op97825$op15672-op97825$_66 : i32 +// CHECK: %op51214$op97825-vl85743$_70 = arith.xori %op97825$op15672-op97825$_69, %vl85743$ded78$ : i32 +// CHECK: %op12343$op15672-op51214$_71 = arith.addi %op15672$op12343-vl34407$, %op51214$op97825-vl85743$_70 : i32 +// CHECK: %vl85743$1e72e$ = arith.constant -21 : i32 +// CHECK: %vl34407$blockArg1-vl85743$_72 = arith.addi %1, %vl85743$1e72e$ : i32 +// CHECK: %op17008$vl34407-vl85743$_73 = arith.muli %vl34407$blockArg1-vl85743$_72, %vl85743$20b04$ : i32 +// CHECK: %op51214$op17008-vl85743$_74 = arith.xori %op17008$vl34407-vl85743$_73, %vl85743$ded78$ : i32 +// CHECK: %op12343$op51214-vl34407$_75 = arith.addi %op51214$op17008-vl85743$_74, %vl34407$blockArg1-vl85743$_72 : i32 +// CHECK: %op97825$op12343-op17008$_76 = arith.muli %op12343$op51214-vl34407$_75, %op17008$vl34407-vl85743$_73 : i32 +// CHECK: %op51214$op97825-vl85743$_77 = arith.xori %op97825$op12343-op17008$_76, %vl85743$ded78$ : i32 +// CHECK: %op12343$op12343-op51214$_78 = arith.addi %op12343$op51214-vl34407$_75, %op51214$op97825-vl85743$_77 : i32 +// CHECK: %op27844$op12343-vl85743$_79 = arith.addi %op12343$op12343-op51214$_78, %vl85743$20b04$ : i32 +// CHECK: %op97825$op27844-op97825$_80 = arith.muli %op27844$op12343-vl85743$_79, %op97825$op12343-op17008$_76 : i32 +// CHECK: %op51214$op97825-vl85743$_81 = arith.xori %op97825$op27844-op97825$_80, %vl85743$ded78$ : i32 +// CHECK: %op12343$op27844-op51214$_82 = arith.addi %op27844$op12343-vl85743$_79, %op51214$op97825-vl85743$_81 : i32 +// CHECK: %op27844$op12343-vl85743$_83 = arith.addi %op12343$op27844-op51214$_82, %vl85743$20b04$ : i32 +// CHECK: %op97825$op27844-op97825$_84 = arith.muli %op27844$op12343-vl85743$_83, %op97825$op27844-op97825$_80 : i32 +// CHECK: %op51214$op97825-vl85743$_85 = arith.xori %op97825$op27844-op97825$_84, %vl85743$ded78$ : i32 +// CHECK: %op12343$op27844-op51214$_86 = arith.addi %op27844$op12343-vl85743$_83, %op51214$op97825-vl85743$_85 : i32 +// CHECK: %op27844$op12343-vl85743$_87 = arith.addi %op12343$op27844-op51214$_86, %vl85743$20b04$ : i32 +// CHECK: %op97825$op27844-op97825$_88 = arith.muli %op27844$op12343-vl85743$_87, %op97825$op27844-op97825$_84 : i32 +// CHECK: %op51214$op97825-vl85743$_89 = arith.xori %op97825$op27844-op97825$_88, %vl85743$ded78$ : i32 +// CHECK: %op12343$op27844-op51214$_90 = arith.addi %op27844$op12343-vl85743$_87, %op51214$op97825-vl85743$_89 : i32 +// CHECK: %op27844$op12343-vl85743$_91 = arith.addi %op12343$op27844-op51214$_90, %vl85743$20b04$ : i32 +// CHECK: %op97825$op27844-op97825$_92 = arith.muli %op27844$op12343-vl85743$_91, %op97825$op27844-op97825$_88 : i32 +// CHECK: %op13782$op12343-op97825$ = arith.addi %op12343$op15672-op51214$_71, %op97825$op27844-op97825$_92 : i32 +// CHECK: %op97825$op13782-op97825$ = arith.muli %op13782$op12343-op97825$, %op97825$op15672-op97825$_69 : i32 +// CHECK: %op51214$op97825-vl85743$_93 = arith.xori %op97825$op13782-op97825$, %vl85743$ded78$ : i32 +// CHECK: %op12343$op13782-op51214$ = arith.addi %op13782$op12343-op97825$, %op51214$op97825-vl85743$_93 : i32 +// CHECK: %op27844$op12343-vl85743$_94 = arith.addi %op12343$op13782-op51214$, %vl85743$20b04$ : i32 +// CHECK: %op97825$op27844-op97825$_95 = arith.muli %op27844$op12343-vl85743$_94, %op97825$op13782-op97825$ : i32 +// CHECK: %op51214$op97825-vl85743$_96 = arith.xori %op97825$op27844-op97825$_95, %vl85743$ded78$ : i32 +// CHECK: %op12343$op27844-op51214$_97 = arith.addi %op27844$op12343-vl85743$_94, %op51214$op97825-vl85743$_96 : i32 +// CHECK: %op27844$op12343-vl85743$_98 = arith.addi %op12343$op27844-op51214$_97, %vl85743$20b04$ : i32 +// CHECK: %op97825$op27844-op97825$_99 = arith.muli %op27844$op12343-vl85743$_98, %op97825$op27844-op97825$_95 : i32 +// CHECK: %op51214$op97825-vl85743$_100 = arith.xori %op97825$op27844-op97825$_99, %vl85743$ded78$ : i32 +// CHECK: %op12343$op27844-op51214$_101 = arith.addi %op27844$op12343-vl85743$_98, %op51214$op97825-vl85743$_100 : i32 +// CHECK: %op27844$op12343-vl85743$_102 = arith.addi %op12343$op27844-op51214$_101, %vl85743$20b04$ : i32 +// CHECK: %op97825$op27844-op97825$_103 = arith.muli %op27844$op12343-vl85743$_102, %op97825$op27844-op97825$_99 : i32 +// CHECK: %op51214$op97825-vl85743$_104 = arith.xori %op97825$op27844-op97825$_103, %vl85743$ded78$ : i32 +// CHECK: %op12343$op27844-op51214$_105 = arith.addi %op27844$op12343-vl85743$_102, %op51214$op97825-vl85743$_104 : i32 +// CHECK: %op27844$op12343-vl85743$_106 = arith.addi %op12343$op27844-op51214$_105, %vl85743$20b04$ : i32 +// CHECK: %op97825$op27844-op97825$_107 = arith.muli %op27844$op12343-vl85743$_106, %op97825$op27844-op97825$_103 : i32 +// CHECK: %op51214$op97825-vl85743$_108 = arith.xori %op97825$op27844-op97825$_107, %vl85743$ded78$ : i32 +// CHECK: %op12343$op27844-op51214$_109 = arith.addi %op27844$op12343-vl85743$_106, %op51214$op97825-vl85743$_108 : i32 +// CHECK: %op27844$op12343-vl85743$_110 = arith.addi %op12343$op27844-op51214$_109, %vl85743$20b04$ : i32 +// CHECK: %op27844$op27844-vl85743$_111 = arith.addi %op27844$op12343-vl85743$_110, %vl85743$20b04$ : i32 +// CHECK: %op15672$op27844-vl34407$ = arith.addi %op27844$op27844-vl85743$_111, %vl34407$blockArg1-vl85743$_72 : i32 +// CHECK: %vl16744$e527e$ = arith.constant 0 : index +// CHECK: memref.store %op15672$op27844-vl34407$, %[[ARG0]][%vl16744$e527e$] : memref +// CHECK: cf.br ^bb1(%op15672$op27844-vl34407$, %vl34407$blockArg1-vl85743$_72 : i32, i32) // CHECK: } -// CHECK: } -func.func @infinte_loop(%arg0: memref, %arg1: i32) { +func.func @infinte_loop_v1(%arg0: memref, %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 + %cneg1 = arith.constant -1 : i32 + %cneg9 = arith.constant -9 : i32 + %cneg14 = arith.constant -14 : i32 + %cneg21 = arith.constant -21 : i32 + + // Branch E: Start with tmp2-based constants + %e1 = arith.addi %tmp2, %cneg14 : i32 + + // Branch A: First main chain %tmp3 = arith.muli %tmp, %c0 : i32 + %tmp4 = arith.xori %tmp3, %cneg1 : i32 + + // Branch D: Another tmp2-based chain + %d1 = arith.addi %tmp2, %cneg21 : i32 + %d2 = arith.muli %d1, %c0 : i32 + + // Branch B: tmp2-cneg1 chain + %tmp6 = arith.addi %tmp2, %cneg1 : i32 + + // Branch A continued + %tmp5 = arith.addi %tmp, %tmp4 : i32 + + // Branch C: tmp2-cneg9 chain + %base1 = arith.addi %tmp2, %cneg9 : i32 + + // Branch E continued + %e2 = arith.muli %e1, %c0 : i32 + %e3 = arith.xori %e2, %cneg1 : i32 + + // Branch A continued + %tmp7 = arith.addi %tmp5, %tmp6 : i32 + + // Branch D continued + %d3 = arith.xori %d2, %cneg1 : i32 + + // Branch B continued + %alt1 = arith.muli %tmp6, %c0 : i32 + %alt2 = arith.xori %alt1, %cneg1 : i32 + + // Branch C continued + %base2 = arith.muli %base1, %c0 : i32 + + // Branch A continued + %tmp8 = arith.muli %tmp7, %tmp3 : i32 + %tmp9 = arith.xori %tmp8, %cneg1 : i32 + + // Branch E continued + %e4 = arith.addi %e1, %e3 : i32 + + // Branch B continued + %alt3 = arith.addi %tmp6, %alt2 : i32 + + // Branch D continued + %d4 = arith.addi %d1, %d3 : i32 + %d5 = arith.muli %d4, %d2 : i32 + + // Branch A continued + %tmp10 = arith.addi %tmp7, %tmp9 : i32 + + // Branch C continued + %base3 = arith.xori %base2, %cneg1 : i32 + %base4 = arith.addi %base1, %base3 : i32 + + // Branch B continued + %alt4 = arith.muli %alt3, %alt1 : i32 + %alt5 = arith.xori %alt4, %cneg1 : i32 + + // Branch A continued + %tmp11 = arith.addi %tmp10, %c0 : i32 + %tmp12 = arith.muli %tmp11, %tmp8 : i32 + + // Branch E continued + %e5 = arith.muli %e4, %e2 : i32 + %e6 = arith.xori %e5, %cneg1 : i32 + + // Branch D continued + %d6 = arith.xori %d5, %cneg1 : i32 + + // Branch A continued + %tmp13 = arith.xori %tmp12, %cneg1 : i32 + %tmp14 = arith.addi %tmp11, %tmp13 : i32 + + // Branch B continued + %alt6 = arith.addi %alt3, %alt5 : i32 + %alt7 = arith.addi %alt6, %c0 : i32 + + // Branch E continued + %e7 = arith.addi %e4, %e6 : i32 + + // Branch A continued + %tmp15 = arith.addi %tmp14, %c0 : i32 + %tmp16 = arith.muli %tmp15, %tmp12 : i32 + + // Branch D continued + %d7 = arith.addi %d4, %d6 : i32 + %d8 = arith.addi %d7, %c0 : i32 + + // Branch B continued + %alt8 = arith.muli %alt7, %alt4 : i32 + + // Branch A continued + %tmp17 = arith.addi %tmp15, %c0 : i32 + %tmp18 = arith.addi %tmp17, %c0 : i32 + + // Branch E continued + %e8 = arith.addi %e7, %c0 : i32 + %e9 = arith.muli %e8, %e5 : i32 + + // Branch A continued + %tmp19 = arith.muli %tmp18, %tmp16 : i32 + %tmp20 = arith.xori %tmp19, %cneg1 : i32 + + // Branch D continued + %d9 = arith.muli %d8, %d5 : i32 + + // Branch B continued + %alt9 = arith.xori %alt8, %cneg1 : i32 + %alt10 = arith.addi %alt7, %alt9 : i32 + + // Branch A continued + %tmp21 = arith.addi %tmp18, %tmp20 : i32 + + // Branch E continued + %e10 = arith.xori %e9, %cneg1 : i32 + + // Branch A continued + %tmp22 = arith.addi %tmp21, %c0 : i32 + %tmp23 = arith.muli %tmp22, %tmp19 : i32 + + // Branch D continued + %d10 = arith.xori %d9, %cneg1 : i32 + %d11 = arith.addi %d8, %d10 : i32 + + // Branch E continued + %e11 = arith.addi %e8, %e10 : i32 + + // Branch A continued + %tmp24 = arith.xori %tmp23, %cneg1 : i32 + %tmp25 = arith.addi %tmp22, %tmp24 : i32 + + // Branch D continued + %d12 = arith.addi %d11, %c0 : i32 + + // Branch A continued + %tmp26 = arith.addi %tmp25, %c0 : i32 + %tmp27 = arith.muli %tmp26, %tmp23 : i32 + + // Branch E continued + %e12 = arith.addi %e11, %c0 : i32 + + // Branch A continued + %tmp28 = arith.xori %tmp27, %cneg1 : i32 + %tmp29 = arith.addi %tmp26, %tmp28 : i32 + + // Branch D continued + %d13 = arith.muli %d12, %d9 : i32 + %d14 = arith.xori %d13, %cneg1 : i32 + + // Branch A continued + %tmp30 = arith.addi %tmp29, %c0 : i32 + %tmp31 = arith.muli %tmp30, %tmp27 : i32 + + // Branch E continued + %e13 = arith.muli %e12, %e9 : i32 + + // Branch A continued + %tmp32 = arith.xori %tmp31, %cneg1 : i32 + %tmp33 = arith.addi %tmp30, %tmp32 : i32 + + // Branch D continued + %d15 = arith.addi %d12, %d14 : i32 + + // Branch A continued + %tmp34 = arith.addi %tmp33, %c0 : i32 + %tmp35 = arith.muli %tmp34, %tmp31 : i32 + + // Branch E continued + %e14 = arith.xori %e13, %cneg1 : i32 + %e15 = arith.addi %e12, %e14 : i32 + + // Branch A continued + %tmp36 = arith.xori %tmp35, %cneg1 : i32 + %tmp37 = arith.addi %tmp34, %tmp36 : i32 + + // Branch D continued + %d16 = arith.addi %d15, %c0 : i32 + + // First merge: A + C + %tmp39 = arith.addi %tmp37, %base4 : i32 + + // Branch E continued + %e16 = arith.addi %e15, %c0 : i32 + + // Continue merged chain + %tmp40 = arith.muli %tmp39, %tmp35 : i32 + + // Branch D continued + %d17 = arith.muli %d16, %d13 : i32 + + // Continue merged chain + %tmp41 = arith.xori %tmp40, %cneg1 : i32 + %tmp42 = arith.addi %tmp39, %tmp41 : i32 + + // Branch E continued + %e17 = arith.muli %e16, %e13 : i32 + %e18 = arith.xori %e17, %cneg1 : i32 + + // Second merge: AB + B + %tmp43 = arith.addi %tmp42, %alt10 : i32 + + // Branch D continued + %d18 = arith.xori %d17, %cneg1 : i32 + %d19 = arith.addi %d16, %d18 : i32 + + // Continue merged chain + %tmp44 = arith.muli %tmp43, %tmp40 : i32 + + // Branch E continued + %e19 = arith.addi %e16, %e18 : i32 + + // Continue merged chain + %tmp45 = arith.xori %tmp44, %cneg1 : i32 + %tmp46 = arith.addi %tmp43, %tmp45 : i32 + + // Branch D continued + %d20 = arith.addi %d19, %c0 : i32 + + // Continue merged chain + %tmp47 = arith.addi %tmp46, %c0 : i32 + + // Branch E final + %e20 = arith.addi %e19, %c0 : i32 + + // Continue merged chain + %tmp48 = arith.muli %tmp47, %tmp44 : i32 + %tmp49 = arith.xori %tmp48, %cneg1 : i32 + + // Branch D final + %d21 = arith.muli %d20, %d17 : i32 + + // Continue merged chain + %tmp50 = arith.addi %tmp47, %tmp49 : i32 + %tmp51 = arith.addi %tmp50, %c0 : i32 + + // Merge in E + %tmp52 = arith.muli %tmp51, %tmp48 : i32 + %tmp53 = arith.xori %tmp52, %cneg1 : i32 + %tmp54 = arith.addi %tmp51, %tmp53 : i32 + + // Continue with E merge + %tmp55 = arith.addi %tmp54, %e20 : i32 + + // Continue chain + %tmp56 = arith.muli %tmp55, %tmp52 : i32 + %tmp57 = arith.xori %tmp56, %cneg1 : i32 + %tmp58 = arith.addi %tmp55, %tmp57 : i32 + + // Merge in E branch value + %tmp60 = arith.addi %tmp58, %e1 : i32 + + // Continue chain + %tmp61 = arith.muli %tmp60, %tmp56 : i32 + %tmp62 = arith.xori %tmp61, %cneg1 : i32 + %tmp63 = arith.addi %tmp60, %tmp62 : i32 + + // Merge in D + %tmp64 = arith.addi %tmp63, %d21 : i32 + + // Continue chain + %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 + + // Final merge with D + %tmp87 = arith.addi %tmp85, %d1 : i32 + + %c0_idx = arith.constant 0 : index + memref.store %tmp87, %arg0[%c0_idx] : memref + cf.br ^bb1(%tmp87, %d1 : i32, i32) +} + +// CHECK: func.func @infinte_loop_v2(%[[ARG0:.*]]: memref, %[[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) +// CHECK: ^bb1(%0: i32, %1: i32): +// CHECK: %vl85743$20b04$ = arith.constant 0 : i32 +// CHECK: %vl73800$blockArg0-vl85743$ = arith.muli %0, %vl85743$20b04$ : i32 +// CHECK: %vl85743$ded78$ = arith.constant -1 : i32 +// CHECK: %op51214$vl73800-vl85743$ = arith.xori %vl73800$blockArg0-vl85743$, %vl85743$ded78$ : i32 +// CHECK: %op12693$blockArg0-op51214$ = arith.addi %0, %op51214$vl73800-vl85743$ : i32 +// CHECK: %vl34407$blockArg1-vl85743$ = arith.addi %1, %vl85743$ded78$ : i32 +// CHECK: %op15672$op12693-vl34407$ = arith.addi %op12693$blockArg0-op51214$, %vl34407$blockArg1-vl85743$ : i32 +// CHECK: %op97825$op15672-vl73800$ = arith.muli %op15672$op12693-vl34407$, %vl73800$blockArg0-vl85743$ : i32 +// CHECK: %op51214$op97825-vl85743$ = arith.xori %op97825$op15672-vl73800$, %vl85743$ded78$ : i32 +// CHECK: %op12343$op15672-op51214$ = arith.addi %op15672$op12693-vl34407$, %op51214$op97825-vl85743$ : i32 +// CHECK: %op27844$op12343-vl85743$ = arith.addi %op12343$op15672-op51214$, %vl85743$20b04$ : i32 +// CHECK: %op97825$op27844-op97825$ = arith.muli %op27844$op12343-vl85743$, %op97825$op15672-vl73800$ : i32 +// CHECK: %op51214$op97825-vl85743$_0 = arith.xori %op97825$op27844-op97825$, %vl85743$ded78$ : i32 +// CHECK: %op12343$op27844-op51214$ = arith.addi %op27844$op12343-vl85743$, %op51214$op97825-vl85743$_0 : i32 +// CHECK: %op27844$op12343-vl85743$_1 = arith.addi %op12343$op27844-op51214$, %vl85743$20b04$ : i32 +// CHECK: %op27844$op27844-vl85743$ = arith.addi %op27844$op12343-vl85743$_1, %vl85743$20b04$ : i32 +// CHECK: %op27844$op27844-vl85743$_2 = arith.addi %op27844$op27844-vl85743$, %vl85743$20b04$ : i32 +// CHECK: %op97825$op27844-op97825$_3 = arith.muli %op27844$op12343-vl85743$_1, %op97825$op27844-op97825$ : i32 +// CHECK: %op97825$op27844-op97825$_4 = arith.muli %op27844$op27844-vl85743$_2, %op97825$op27844-op97825$_3 : i32 +// CHECK: %op51214$op97825-vl85743$_5 = arith.xori %op97825$op27844-op97825$_4, %vl85743$ded78$ : i32 +// CHECK: %op12343$op27844-op51214$_6 = arith.addi %op27844$op27844-vl85743$_2, %op51214$op97825-vl85743$_5 : i32 +// CHECK: %op27844$op12343-vl85743$_7 = arith.addi %op12343$op27844-op51214$_6, %vl85743$20b04$ : i32 +// CHECK: %op97825$op27844-op97825$_8 = arith.muli %op27844$op12343-vl85743$_7, %op97825$op27844-op97825$_4 : i32 +// CHECK: %op51214$op97825-vl85743$_9 = arith.xori %op97825$op27844-op97825$_8, %vl85743$ded78$ : i32 +// CHECK: %op12343$op27844-op51214$_10 = arith.addi %op27844$op12343-vl85743$_7, %op51214$op97825-vl85743$_9 : i32 +// CHECK: %op27844$op12343-vl85743$_11 = arith.addi %op12343$op27844-op51214$_10, %vl85743$20b04$ : i32 +// CHECK: %op97825$op27844-op97825$_12 = arith.muli %op27844$op12343-vl85743$_11, %op97825$op27844-op97825$_8 : i32 +// CHECK: %op51214$op97825-vl85743$_13 = arith.xori %op97825$op27844-op97825$_12, %vl85743$ded78$ : i32 +// CHECK: %op12343$op27844-op51214$_14 = arith.addi %op27844$op12343-vl85743$_11, %op51214$op97825-vl85743$_13 : i32 +// CHECK: %op27844$op12343-vl85743$_15 = arith.addi %op12343$op27844-op51214$_14, %vl85743$20b04$ : i32 +// CHECK: %op97825$op27844-op97825$_16 = arith.muli %op27844$op12343-vl85743$_15, %op97825$op27844-op97825$_12 : i32 +// CHECK: %op51214$op97825-vl85743$_17 = arith.xori %op97825$op27844-op97825$_16, %vl85743$ded78$ : i32 +// CHECK: %op12343$op27844-op51214$_18 = arith.addi %op27844$op12343-vl85743$_15, %op51214$op97825-vl85743$_17 : i32 +// CHECK: %op27844$op12343-vl85743$_19 = arith.addi %op12343$op27844-op51214$_18, %vl85743$20b04$ : i32 +// CHECK: %op97825$op27844-op97825$_20 = arith.muli %op27844$op12343-vl85743$_19, %op97825$op27844-op97825$_16 : i32 +// CHECK: %op51214$op97825-vl85743$_21 = arith.xori %op97825$op27844-op97825$_20, %vl85743$ded78$ : i32 +// CHECK: %op12343$op27844-op51214$_22 = arith.addi %op27844$op12343-vl85743$_19, %op51214$op97825-vl85743$_21 : i32 +// CHECK: %vl85743$51850$ = arith.constant -9 : i32 +// CHECK: %vl34407$blockArg1-vl85743$_23 = arith.addi %1, %vl85743$51850$ : i32 +// CHECK: %op17008$vl34407-vl85743$ = arith.muli %vl34407$blockArg1-vl85743$_23, %vl85743$20b04$ : i32 +// CHECK: %op51214$op17008-vl85743$ = arith.xori %op17008$vl34407-vl85743$, %vl85743$ded78$ : i32 +// CHECK: %op12343$op51214-vl34407$ = arith.addi %op51214$op17008-vl85743$, %vl34407$blockArg1-vl85743$_23 : i32 +// CHECK: %op15672$op12343-op12343$ = arith.addi %op12343$op27844-op51214$_22, %op12343$op51214-vl34407$ : i32 +// CHECK: %op97825$op15672-op97825$ = arith.muli %op15672$op12343-op12343$, %op97825$op27844-op97825$_20 : i32 +// CHECK: %op51214$op97825-vl85743$_24 = arith.xori %op97825$op15672-op97825$, %vl85743$ded78$ : i32 +// CHECK: %op12343$op15672-op51214$_25 = arith.addi %op15672$op12343-op12343$, %op51214$op97825-vl85743$_24 : i32 +// CHECK: %op17008$vl34407-vl85743$_26 = arith.muli %vl34407$blockArg1-vl85743$, %vl85743$20b04$ : i32 +// CHECK: %op51214$op17008-vl85743$_27 = arith.xori %op17008$vl34407-vl85743$_26, %vl85743$ded78$ : i32 +// CHECK: %op12343$op51214-vl34407$_28 = arith.addi %op51214$op17008-vl85743$_27, %vl34407$blockArg1-vl85743$ : i32 +// CHECK: %op97825$op12343-op17008$ = arith.muli %op12343$op51214-vl34407$_28, %op17008$vl34407-vl85743$_26 : i32 +// CHECK: %op51214$op97825-vl85743$_29 = arith.xori %op97825$op12343-op17008$, %vl85743$ded78$ : i32 +// CHECK: %op12343$op12343-op51214$ = arith.addi %op12343$op51214-vl34407$_28, %op51214$op97825-vl85743$_29 : i32 +// CHECK: %op27844$op12343-vl85743$_30 = arith.addi %op12343$op12343-op51214$, %vl85743$20b04$ : i32 +// CHECK: %op97825$op27844-op97825$_31 = arith.muli %op27844$op12343-vl85743$_30, %op97825$op12343-op17008$ : i32 +// CHECK: %op51214$op97825-vl85743$_32 = arith.xori %op97825$op27844-op97825$_31, %vl85743$ded78$ : i32 +// CHECK: %op12343$op27844-op51214$_33 = arith.addi %op27844$op12343-vl85743$_30, %op51214$op97825-vl85743$_32 : i32 +// CHECK: %op15672$op12343-op12343$_34 = arith.addi %op12343$op15672-op51214$_25, %op12343$op27844-op51214$_33 : i32 +// CHECK: %op97825$op15672-op97825$_35 = arith.muli %op15672$op12343-op12343$_34, %op97825$op15672-op97825$ : i32 +// CHECK: %op51214$op97825-vl85743$_36 = arith.xori %op97825$op15672-op97825$_35, %vl85743$ded78$ : i32 +// CHECK: %op12343$op15672-op51214$_37 = arith.addi %op15672$op12343-op12343$_34, %op51214$op97825-vl85743$_36 : i32 +// CHECK: %op27844$op12343-vl85743$_38 = arith.addi %op12343$op15672-op51214$_37, %vl85743$20b04$ : i32 +// CHECK: %op97825$op27844-op97825$_39 = arith.muli %op27844$op12343-vl85743$_38, %op97825$op15672-op97825$_35 : i32 +// CHECK: %op51214$op97825-vl85743$_40 = arith.xori %op97825$op27844-op97825$_39, %vl85743$ded78$ : i32 +// CHECK: %op12343$op27844-op51214$_41 = arith.addi %op27844$op12343-vl85743$_38, %op51214$op97825-vl85743$_40 : i32 +// CHECK: %op27844$op12343-vl85743$_42 = arith.addi %op12343$op27844-op51214$_41, %vl85743$20b04$ : i32 +// CHECK: %op97825$op27844-op97825$_43 = arith.muli %op27844$op12343-vl85743$_42, %op97825$op27844-op97825$_39 : i32 +// CHECK: %op51214$op97825-vl85743$_44 = arith.xori %op97825$op27844-op97825$_43, %vl85743$ded78$ : i32 +// CHECK: %op12343$op27844-op51214$_45 = arith.addi %op27844$op12343-vl85743$_42, %op51214$op97825-vl85743$_44 : i32 +// CHECK: %vl85743$7b7de$ = arith.constant -14 : i32 +// CHECK: %vl34407$blockArg1-vl85743$_46 = arith.addi %1, %vl85743$7b7de$ : i32 +// CHECK: %op17008$vl34407-vl85743$_47 = arith.muli %vl34407$blockArg1-vl85743$_46, %vl85743$20b04$ : i32 +// CHECK: %op51214$op17008-vl85743$_48 = arith.xori %op17008$vl34407-vl85743$_47, %vl85743$ded78$ : i32 +// CHECK: %op12343$op51214-vl34407$_49 = arith.addi %op51214$op17008-vl85743$_48, %vl34407$blockArg1-vl85743$_46 : i32 +// CHECK: %op97825$op12343-op17008$_50 = arith.muli %op12343$op51214-vl34407$_49, %op17008$vl34407-vl85743$_47 : i32 +// CHECK: %op51214$op97825-vl85743$_51 = arith.xori %op97825$op12343-op17008$_50, %vl85743$ded78$ : i32 +// CHECK: %op12343$op12343-op51214$_52 = arith.addi %op12343$op51214-vl34407$_49, %op51214$op97825-vl85743$_51 : i32 +// CHECK: %op27844$op12343-vl85743$_53 = arith.addi %op12343$op12343-op51214$_52, %vl85743$20b04$ : i32 +// CHECK: %op97825$op27844-op97825$_54 = arith.muli %op27844$op12343-vl85743$_53, %op97825$op12343-op17008$_50 : i32 +// CHECK: %op51214$op97825-vl85743$_55 = arith.xori %op97825$op27844-op97825$_54, %vl85743$ded78$ : i32 +// CHECK: %op12343$op27844-op51214$_56 = arith.addi %op27844$op12343-vl85743$_53, %op51214$op97825-vl85743$_55 : i32 +// CHECK: %op27844$op12343-vl85743$_57 = arith.addi %op12343$op27844-op51214$_56, %vl85743$20b04$ : i32 +// CHECK: %op97825$op27844-op97825$_58 = arith.muli %op27844$op12343-vl85743$_57, %op97825$op27844-op97825$_54 : i32 +// CHECK: %op51214$op97825-vl85743$_59 = arith.xori %op97825$op27844-op97825$_58, %vl85743$ded78$ : i32 +// CHECK: %op12343$op27844-op51214$_60 = arith.addi %op27844$op12343-vl85743$_57, %op51214$op97825-vl85743$_59 : i32 +// CHECK: %op27844$op12343-vl85743$_61 = arith.addi %op12343$op27844-op51214$_60, %vl85743$20b04$ : i32 +// CHECK: %op97825$op27844-op97825$_62 = arith.muli %op27844$op12343-vl85743$_61, %op97825$op27844-op97825$_58 : i32 +// CHECK: %op51214$op97825-vl85743$_63 = arith.xori %op97825$op27844-op97825$_62, %vl85743$ded78$ : i32 +// CHECK: %op12343$op27844-op51214$_64 = arith.addi %op27844$op12343-vl85743$_61, %op51214$op97825-vl85743$_63 : i32 +// CHECK: %op27844$op12343-vl85743$_65 = arith.addi %op12343$op27844-op51214$_64, %vl85743$20b04$ : i32 +// CHECK: %op15672$op12343-op27844$ = arith.addi %op12343$op27844-op51214$_45, %op27844$op12343-vl85743$_65 : i32 +// CHECK: %op97825$op15672-op97825$_66 = arith.muli %op15672$op12343-op27844$, %op97825$op27844-op97825$_43 : i32 +// CHECK: %op51214$op97825-vl85743$_67 = arith.xori %op97825$op15672-op97825$_66, %vl85743$ded78$ : i32 +// CHECK: %op12343$op15672-op51214$_68 = arith.addi %op15672$op12343-op27844$, %op51214$op97825-vl85743$_67 : i32 +// CHECK: %op15672$op12343-vl34407$ = arith.addi %op12343$op15672-op51214$_68, %vl34407$blockArg1-vl85743$_46 : i32 +// CHECK: %op97825$op15672-op97825$_69 = arith.muli %op15672$op12343-vl34407$, %op97825$op15672-op97825$_66 : i32 +// CHECK: %op51214$op97825-vl85743$_70 = arith.xori %op97825$op15672-op97825$_69, %vl85743$ded78$ : i32 +// CHECK: %op12343$op15672-op51214$_71 = arith.addi %op15672$op12343-vl34407$, %op51214$op97825-vl85743$_70 : i32 +// CHECK: %vl85743$1e72e$ = arith.constant -21 : i32 +// CHECK: %vl34407$blockArg1-vl85743$_72 = arith.addi %1, %vl85743$1e72e$ : i32 +// CHECK: %op17008$vl34407-vl85743$_73 = arith.muli %vl34407$blockArg1-vl85743$_72, %vl85743$20b04$ : i32 +// CHECK: %op51214$op17008-vl85743$_74 = arith.xori %op17008$vl34407-vl85743$_73, %vl85743$ded78$ : i32 +// CHECK: %op12343$op51214-vl34407$_75 = arith.addi %op51214$op17008-vl85743$_74, %vl34407$blockArg1-vl85743$_72 : i32 +// CHECK: %op97825$op12343-op17008$_76 = arith.muli %op12343$op51214-vl34407$_75, %op17008$vl34407-vl85743$_73 : i32 +// CHECK: %op51214$op97825-vl85743$_77 = arith.xori %op97825$op12343-op17008$_76, %vl85743$ded78$ : i32 +// CHECK: %op12343$op12343-op51214$_78 = arith.addi %op12343$op51214-vl34407$_75, %op51214$op97825-vl85743$_77 : i32 +// CHECK: %op27844$op12343-vl85743$_79 = arith.addi %op12343$op12343-op51214$_78, %vl85743$20b04$ : i32 +// CHECK: %op97825$op27844-op97825$_80 = arith.muli %op27844$op12343-vl85743$_79, %op97825$op12343-op17008$_76 : i32 +// CHECK: %op51214$op97825-vl85743$_81 = arith.xori %op97825$op27844-op97825$_80, %vl85743$ded78$ : i32 +// CHECK: %op12343$op27844-op51214$_82 = arith.addi %op27844$op12343-vl85743$_79, %op51214$op97825-vl85743$_81 : i32 +// CHECK: %op27844$op12343-vl85743$_83 = arith.addi %op12343$op27844-op51214$_82, %vl85743$20b04$ : i32 +// CHECK: %op97825$op27844-op97825$_84 = arith.muli %op27844$op12343-vl85743$_83, %op97825$op27844-op97825$_80 : i32 +// CHECK: %op51214$op97825-vl85743$_85 = arith.xori %op97825$op27844-op97825$_84, %vl85743$ded78$ : i32 +// CHECK: %op12343$op27844-op51214$_86 = arith.addi %op27844$op12343-vl85743$_83, %op51214$op97825-vl85743$_85 : i32 +// CHECK: %op27844$op12343-vl85743$_87 = arith.addi %op12343$op27844-op51214$_86, %vl85743$20b04$ : i32 +// CHECK: %op97825$op27844-op97825$_88 = arith.muli %op27844$op12343-vl85743$_87, %op97825$op27844-op97825$_84 : i32 +// CHECK: %op51214$op97825-vl85743$_89 = arith.xori %op97825$op27844-op97825$_88, %vl85743$ded78$ : i32 +// CHECK: %op12343$op27844-op51214$_90 = arith.addi %op27844$op12343-vl85743$_87, %op51214$op97825-vl85743$_89 : i32 +// CHECK: %op27844$op12343-vl85743$_91 = arith.addi %op12343$op27844-op51214$_90, %vl85743$20b04$ : i32 +// CHECK: %op97825$op27844-op97825$_92 = arith.muli %op27844$op12343-vl85743$_91, %op97825$op27844-op97825$_88 : i32 +// CHECK: %op13782$op12343-op97825$ = arith.addi %op12343$op15672-op51214$_71, %op97825$op27844-op97825$_92 : i32 +// CHECK: %op97825$op13782-op97825$ = arith.muli %op13782$op12343-op97825$, %op97825$op15672-op97825$_69 : i32 +// CHECK: %op51214$op97825-vl85743$_93 = arith.xori %op97825$op13782-op97825$, %vl85743$ded78$ : i32 +// CHECK: %op12343$op13782-op51214$ = arith.addi %op13782$op12343-op97825$, %op51214$op97825-vl85743$_93 : i32 +// CHECK: %op27844$op12343-vl85743$_94 = arith.addi %op12343$op13782-op51214$, %vl85743$20b04$ : i32 +// CHECK: %op97825$op27844-op97825$_95 = arith.muli %op27844$op12343-vl85743$_94, %op97825$op13782-op97825$ : i32 +// CHECK: %op51214$op97825-vl85743$_96 = arith.xori %op97825$op27844-op97825$_95, %vl85743$ded78$ : i32 +// CHECK: %op12343$op27844-op51214$_97 = arith.addi %op27844$op12343-vl85743$_94, %op51214$op97825-vl85743$_96 : i32 +// CHECK: %op27844$op12343-vl85743$_98 = arith.addi %op12343$op27844-op51214$_97, %vl85743$20b04$ : i32 +// CHECK: %op97825$op27844-op97825$_99 = arith.muli %op27844$op12343-vl85743$_98, %op97825$op27844-op97825$_95 : i32 +// CHECK: %op51214$op97825-vl85743$_100 = arith.xori %op97825$op27844-op97825$_99, %vl85743$ded78$ : i32 +// CHECK: %op12343$op27844-op51214$_101 = arith.addi %op27844$op12343-vl85743$_98, %op51214$op97825-vl85743$_100 : i32 +// CHECK: %op27844$op12343-vl85743$_102 = arith.addi %op12343$op27844-op51214$_101, %vl85743$20b04$ : i32 +// CHECK: %op97825$op27844-op97825$_103 = arith.muli %op27844$op12343-vl85743$_102, %op97825$op27844-op97825$_99 : i32 +// CHECK: %op51214$op97825-vl85743$_104 = arith.xori %op97825$op27844-op97825$_103, %vl85743$ded78$ : i32 +// CHECK: %op12343$op27844-op51214$_105 = arith.addi %op27844$op12343-vl85743$_102, %op51214$op97825-vl85743$_104 : i32 +// CHECK: %op27844$op12343-vl85743$_106 = arith.addi %op12343$op27844-op51214$_105, %vl85743$20b04$ : i32 +// CHECK: %op97825$op27844-op97825$_107 = arith.muli %op27844$op12343-vl85743$_106, %op97825$op27844-op97825$_103 : i32 +// CHECK: %op51214$op97825-vl85743$_108 = arith.xori %op97825$op27844-op97825$_107, %vl85743$ded78$ : i32 +// CHECK: %op12343$op27844-op51214$_109 = arith.addi %op27844$op12343-vl85743$_106, %op51214$op97825-vl85743$_108 : i32 +// CHECK: %op27844$op12343-vl85743$_110 = arith.addi %op12343$op27844-op51214$_109, %vl85743$20b04$ : i32 +// CHECK: %op27844$op27844-vl85743$_111 = arith.addi %op27844$op12343-vl85743$_110, %vl85743$20b04$ : i32 +// CHECK: %op15672$op27844-vl34407$ = arith.addi %op27844$op27844-vl85743$_111, %vl34407$blockArg1-vl85743$_72 : i32 +// CHECK: %vl16744$e527e$ = arith.constant 0 : index +// CHECK: memref.store %op15672$op27844-vl34407$, %[[ARG0]][%vl16744$e527e$] : memref +// CHECK: cf.br ^bb1(%op15672$op27844-vl34407$, %vl34407$blockArg1-vl85743$_72 : i32, i32) +// CHECK: } +func.func @infinte_loop_v2(%arg0: memref, %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 %cneg1 = arith.constant -1 : i32 + %cneg9 = arith.constant -9 : i32 + %cneg14 = arith.constant -14 : i32 + %cneg21 = arith.constant -21 : i32 + + // Branch D: Start with cneg21 + %d1 = arith.addi %tmp2, %cneg21 : i32 + + // Branch C: tmp2-cneg9 chain + %base1 = arith.addi %tmp2, %cneg9 : i32 + %base2 = arith.muli %base1, %c0 : i32 + + // Branch A: First main chain + %tmp3 = arith.muli %tmp, %c0 : i32 + + // Branch D continued + %d2 = arith.muli %d1, %c0 : i32 + %d3 = arith.xori %d2, %cneg1 : i32 + %d4 = arith.addi %d1, %d3 : i32 + + // Branch E: cneg14 chain + %e1 = arith.addi %tmp2, %cneg14 : i32 + %e2 = arith.muli %e1, %c0 : i32 + + // Branch A continued %tmp4 = arith.xori %tmp3, %cneg1 : i32 %tmp5 = arith.addi %tmp, %tmp4 : i32 + + // Branch B: tmp6 chain %tmp6 = arith.addi %tmp2, %cneg1 : i32 + %alt1 = arith.muli %tmp6, %c0 : i32 + + // Branch C continued + %base3 = arith.xori %base2, %cneg1 : i32 + + // Branch D continued + %d5 = arith.muli %d4, %d2 : i32 + %d6 = arith.xori %d5, %cneg1 : i32 + %d7 = arith.addi %d4, %d6 : i32 + + // Branch E continued + %e3 = arith.xori %e2, %cneg1 : i32 + %e4 = arith.addi %e1, %e3 : i32 + %e5 = arith.muli %e4, %e2 : i32 + + // Branch A continued %tmp7 = arith.addi %tmp5, %tmp6 : i32 %tmp8 = arith.muli %tmp7, %tmp3 : i32 + + // Branch B continued + %alt2 = arith.xori %alt1, %cneg1 : i32 + %alt3 = arith.addi %tmp6, %alt2 : i32 + %alt4 = arith.muli %alt3, %alt1 : i32 + + // Branch C continued + %base4 = arith.addi %base1, %base3 : i32 + + // Branch D continued + %d8 = arith.addi %d7, %c0 : i32 + %d9 = arith.muli %d8, %d5 : i32 + + // Branch A continued %tmp9 = arith.xori %tmp8, %cneg1 : i32 %tmp10 = arith.addi %tmp7, %tmp9 : i32 %tmp11 = arith.addi %tmp10, %c0 : i32 + + // Branch E continued + %e6 = arith.xori %e5, %cneg1 : i32 + %e7 = arith.addi %e4, %e6 : i32 + %e8 = arith.addi %e7, %c0 : i32 + + // Branch B continued + %alt5 = arith.xori %alt4, %cneg1 : i32 + %alt6 = arith.addi %alt3, %alt5 : i32 + + // Branch A continued %tmp12 = arith.muli %tmp11, %tmp8 : i32 %tmp13 = arith.xori %tmp12, %cneg1 : i32 + + // Branch D continued + %d10 = arith.xori %d9, %cneg1 : i32 + %d11 = arith.addi %d8, %d10 : i32 + %d12 = arith.addi %d11, %c0 : i32 + + // Branch E continued + %e9 = arith.muli %e8, %e5 : i32 + %e10 = arith.xori %e9, %cneg1 : i32 + + // Branch B continued + %alt7 = arith.addi %alt6, %c0 : i32 + %alt8 = arith.muli %alt7, %alt4 : i32 + %alt9 = arith.xori %alt8, %cneg1 : i32 + + // Branch A continued %tmp14 = arith.addi %tmp11, %tmp13 : i32 %tmp15 = arith.addi %tmp14, %c0 : i32 %tmp16 = arith.muli %tmp15, %tmp12 : i32 + + // Branch D continued + %d13 = arith.muli %d12, %d9 : i32 + %d14 = arith.xori %d13, %cneg1 : i32 + %d15 = arith.addi %d12, %d14 : i32 + + // Branch E continued + %e11 = arith.addi %e8, %e10 : i32 + %e12 = arith.addi %e11, %c0 : i32 + %e13 = arith.muli %e12, %e9 : i32 + + // Branch A continued %tmp17 = arith.addi %tmp15, %c0 : i32 %tmp18 = arith.addi %tmp17, %c0 : i32 %tmp19 = arith.muli %tmp18, %tmp16 : i32 + + // Branch B continued + %alt10 = arith.addi %alt7, %alt9 : i32 + + // Branch D continued + %d16 = arith.addi %d15, %c0 : i32 + %d17 = arith.muli %d16, %d13 : i32 + %d18 = arith.xori %d17, %cneg1 : i32 + + // Branch A continued %tmp20 = arith.xori %tmp19, %cneg1 : i32 %tmp21 = arith.addi %tmp18, %tmp20 : i32 %tmp22 = arith.addi %tmp21, %c0 : i32 + + // Branch E continued + %e14 = arith.xori %e13, %cneg1 : i32 + %e15 = arith.addi %e12, %e14 : i32 + %e16 = arith.addi %e15, %c0 : i32 + + // Branch A continued %tmp23 = arith.muli %tmp22, %tmp19 : i32 %tmp24 = arith.xori %tmp23, %cneg1 : i32 + + // Branch D continued + %d19 = arith.addi %d16, %d18 : i32 + %d20 = arith.addi %d19, %c0 : i32 + %d21 = arith.muli %d20, %d17 : i32 + + // Branch A continued %tmp25 = arith.addi %tmp22, %tmp24 : i32 %tmp26 = arith.addi %tmp25, %c0 : i32 + + // Branch E continued + %e17 = arith.muli %e16, %e13 : i32 + %e18 = arith.xori %e17, %cneg1 : i32 + %e19 = arith.addi %e16, %e18 : i32 + %e20 = arith.addi %e19, %c0 : i32 + + // Branch A continued %tmp27 = arith.muli %tmp26, %tmp23 : i32 %tmp28 = arith.xori %tmp27, %cneg1 : i32 %tmp29 = arith.addi %tmp26, %tmp28 : i32 @@ -144,13 +779,15 @@ func.func @infinte_loop(%arg0: memref, %arg1: 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 + + // Merge A + C + %tmp39 = arith.addi %tmp37, %base4 : 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 + + // Merge + B + %tmp43 = arith.addi %tmp42, %alt10 : i32 %tmp44 = arith.muli %tmp43, %tmp40 : i32 %tmp45 = arith.xori %tmp44, %cneg1 : i32 %tmp46 = arith.addi %tmp43, %tmp45 : i32 @@ -159,20 +796,24 @@ func.func @infinte_loop(%arg0: memref, %arg1: i32) { %tmp49 = arith.xori %tmp48, %cneg1 : i32 %tmp50 = arith.addi %tmp47, %tmp49 : i32 %tmp51 = arith.addi %tmp50, %c0 : i32 + + // Merge + E %tmp52 = arith.muli %tmp51, %tmp48 : i32 %tmp53 = arith.xori %tmp52, %cneg1 : i32 %tmp54 = arith.addi %tmp51, %tmp53 : i32 - %tmp55 = arith.addi %tmp54, %c0 : i32 + %tmp55 = arith.addi %tmp54, %e20 : 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 + + // Merge E source value + %tmp60 = arith.addi %tmp58, %e1 : 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 + + // Merge + D + %tmp64 = arith.addi %tmp63, %d21 : i32 %tmp65 = arith.muli %tmp64, %tmp61 : i32 %tmp66 = arith.xori %tmp65, %cneg1 : i32 %tmp67 = arith.addi %tmp64, %tmp66 : i32 @@ -194,10 +835,13 @@ func.func @infinte_loop(%arg0: memref, %arg1: 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 + + // Final merge with D source value + %tmp87 = arith.addi %tmp85, %d1 : i32 + %c0_idx = arith.constant 0 : index memref.store %tmp87, %arg0[%c0_idx] : memref - cf.br ^bb1(%tmp87, %tmp86 : i32, i32) + cf.br ^bb1(%tmp87, %d1 : i32, i32) } + +// CHECK: }