Skip to content

Conversation

Sh0g0-1758
Copy link
Member

PR to add an llvm-canon kind of tool for MLIR.

Copy link

github-actions bot commented Oct 7, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

@Sh0g0-1758 Sh0g0-1758 added the mlir label Oct 7, 2025
@Sh0g0-1758 Sh0g0-1758 self-assigned this Oct 7, 2025
@Sh0g0-1758 Sh0g0-1758 marked this pull request as ready for review October 10, 2025 14:27
@Sh0g0-1758 Sh0g0-1758 changed the title [WIP][mlir] Add Normalize pass [mlir] Add Normalize pass Oct 10, 2025
@Sh0g0-1758 Sh0g0-1758 requested a review from jpienaar October 10, 2025 14:41
Copy link
Member

@jpienaar jpienaar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you expand the PR desciption as well as pass and function comments to capture the goals, what's working etc?

def Normalize : Pass<"normalize", "ModuleOp"> {
let summary = "normalize.";
let description = [{
just normalize bro
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you populate these?

"func::FuncDialect",
"scf::SCFDialect",
"vector::VectorDialect",
"LLVM::LLVMDialect",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are these needed as dependent dialects?


bool isInitialOperation(mlir::Operation *const op) const noexcept;
void nameAsInitialOperation(
mlir::Operation *op,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't need mlir:: as you have using mlir namespace above.

const uint64_t MagicHashConstant = 0x6acaa36bef8325c5ULL;
void
collectOutputOperations(Block &block,
SmallVector<Operation *, 16> &Output) const noexcept;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MLIR has style guide around variable names https://mlir.llvm.org/getting_started/DeveloperGuide/#style-guide


uint64_t Hash = MagicHashConstant;

uint64_t opcodeHash = strHash(op->getName().getStringRef().str());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

StringRef has hash_value , could that be used?

@@ -0,0 +1,436 @@
//===- Normalize.cpp - Conversion from MLIR to its canonical form ---------===//
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not make this pass local to a tool?

if (op.hasTrait<OpTrait::IsTerminator>())
return true;

if (auto memOp = dyn_cast<MemoryEffectOpInterface>(&op)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you comment as to why?

Operands.push_back({Stream.str(), operand});
}

if (op->hasTrait<OpTrait::IsCommutative>()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

commutative operations are naturally reordered while considering constants, would this be considered here too giving naming conventions?

std::equal(check.begin(), check.end(), base.begin());
}

void NormalizePass::foldOperation(mlir::Operation *op) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In MLIR there is OpFolder and folding, I think this is doing something different, could you document it?

Name.append(opName.substr(1, 7));

Name.append("$");
for (unsigned long i = 0; i < Operands.size(); ++i) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://llvm.org/docs/CodingStandards.html#don-t-evaluate-end-every-time-through-a-loop , although in this case the range based should be easy to use and preferred.


// CHECK-LABEL: module {
// CHECK: func.func @infinte_loop(%[[ARG0:.*]]: memref<?xi32>, %[[ARG1:.*]]: i32) {
// CHECK: %vl15969$e5677$ = arith.constant 1 : i32
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's up with these cryptic SSA variable names?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's the point of llvm-canon, it gives everything predictable "canonical" names so when you compare two files textually, they don't differ because of SSA value numbers.

}

uint64_t inline strHash(std::string_view data) noexcept {
const static uint64_t FNV_OFFSET = 0xcbf29ce484222325ULL;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the meaning of these magic numbers?

private:
const uint64_t MagicHashConstant = 0x6acaa36bef8325c5ULL;
void
collectOutputOperations(Block &block,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All of the functions here should have documentation.

Copy link
Member

@ftynse ftynse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your patch! Please provide a clear commit description, explaining why you make changes, not only what they are: https://mlir.llvm.org/getting_started/Contributing/#commit-messages.

Two large items that will need addressing is (1) this does not belong to lib/Conversion because it is not a conversion and it will have to operate generally on all MLIR operations regardless of dialects and (2) we need to find a way of testing the desired goal of the tool which is minizing textual difference between files to "semantically meaningful" parts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants