llvm / llvm/circt

[FIRRTL] Don't Clone in ModuleInliner Unless Necessary

Open
#2,586 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

FIRRTL
Dominant language
C++
Stars
2.2k
Forks
524
Avg merge
3d 2h
Merged PRs (30d)
46

Description

Change FIRRTL Dialect's `ModuleInliner` pass to not clone operations when a move is sufficient. Currently, the `ModuleInliner` works by copying operations and then garbage collecting dead modules.

Consider the following circuit where `Bar` is going to be inlined into `Foo`:

```mlir
firrtl.circuit "Foo" {
firrtl.module @Bar() attributes {annotations = [{class = "firrtl.passes.InlineAnnotation"}]} {
%w = firrtl.wire : !firrtl.uint<1>
}
firrtl.module @Foo() {
firrtl.instance bar @Bar()
}
}
```

This performs one clone of `%w = firrtl.wire` even though a move would suffice. You can see the single clone happen with the following patch:

```diff
diff --git a/lib/Dialect/FIRRTL/Transforms/ModuleInliner.cpp b/lib/Dialect/FIRRTL/Transforms/ModuleInliner.cpp
index a6f6d16a..9fe9935c 100644
--- a/lib/Dialect/FIRRTL/Transforms/ModuleInliner.cpp
+++ b/lib/Dialect/FIRRTL/Transforms/ModuleInliner.cpp
@@ -598,6 +598,7 @@ void Inliner::cloneAndRename(
}

// Clone and rename.
+ llvm::errs() << "// Cloning: " << op << "\n";
auto *newOp = b.clone(op, mapper);
rename(prefix, newOp, moduleNamespace);

```

Running this then produces (circt-opt Foo.mlir -pass-pipeline='firrtl.circuit(firrtl-inliner)':
```mlir
// Cloning: %w = firrtl.wire : !firrtl.uint<1>
module {
firrtl.circuit "Foo" {
firrtl.module @Foo() {
%bar_w = firrtl.wire : !firrtl.uint<1>
}
}
}
```

This issue is based off a @darthscsi comment: https://github.com/llvm/circt/pull/2544#discussion_r799642682.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in lib/Dialect/FIRRTL/Transforms/ModuleInliner.cpp, especially Inliner::cloneAndRename, and inspect the firrtl-inliner pass behavior using the circuit example in the issue. Run circt-opt Foo.mlir with -pass-pipeline='firrtl.circuit(firrtl-inliner)' and verify that operations are moved when cloning is unnecessary while the output remains correct.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers
Issue type
Refactor
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.