Skip to content

Commit 63880ae

Browse files
authored
[FIRRTL][circt-reduce] Add LayerEnableRemover to strip enabled layers (#10539)
1 parent 15080c2 commit 63880ae

2 files changed

Lines changed: 51 additions & 0 deletions

File tree

lib/Dialect/FIRRTL/FIRRTLReductions.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2559,6 +2559,34 @@ struct LayerDisable : public OpReduction<CircuitOp> {
25592559
DenseMap<uint64_t, SymbolRefAttr> symbolRefAttrMap;
25602560
};
25612561

2562+
/// A reduction pattern that removes initialized layer entries
2563+
/// from the FIRRTL module operations.
2564+
struct LayerEnableRemover : public OpReduction<FModuleOp> {
2565+
void matches(FModuleOp moduleOp,
2566+
llvm::function_ref<void(uint64_t, uint64_t)> addMatch) override {
2567+
2568+
auto layers = moduleOp.getLayersAttr();
2569+
for (size_t i = 0, e = layers.size(); i != e; ++i)
2570+
addMatch(1, i);
2571+
}
2572+
2573+
LogicalResult rewriteMatches(FModuleOp moduleOp,
2574+
ArrayRef<uint64_t> matches) override {
2575+
2576+
llvm::SmallDenseSet<uint64_t, 4> removed(matches.begin(), matches.end());
2577+
SmallVector<Attribute> newLayers;
2578+
auto layers = moduleOp.getLayersAttr();
2579+
for (size_t i = 0, e = layers.size(); i != e; ++i)
2580+
if (!removed.contains(i))
2581+
newLayers.push_back(layers[i]);
2582+
2583+
moduleOp.setLayersAttr(ArrayAttr::get(moduleOp.getContext(), newLayers));
2584+
return success();
2585+
}
2586+
2587+
std::string getName() const override { return "firrtl-layer-enable-remover"; }
2588+
};
2589+
25622590
} // namespace
25632591

25642592
/// A reduction pattern that removes elements from FIRRTL list create
@@ -2652,6 +2680,7 @@ void firrtl::FIRRTLReducePatternDialectInterface::populateReducePatterns(
26522680
patterns.add<AnnotationRemover, 32>();
26532681
patterns.add<ModuleSwapper, 31>();
26542682
patterns.add<LayerDisable, 30>(getContext());
2683+
patterns.add<LayerEnableRemover, 30>();
26552684
patterns.add<PassReduction, 29>(
26562685
getContext(),
26572686
firrtl::createDropName({/*preserveMode=*/PreserveValues::None}), false,
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// UNSUPPORTED: system-windows
2+
// See https://github.com/llvm/circt/issues/4129
3+
// RUN: circt-reduce %s --test /usr/bin/env --test-arg grep --test-arg -q --test-arg '@Foo' --include firrtl-layer-enable-remover --include firrtl-layer-disable --keep-best=0 | FileCheck %s --check-prefix=ALL-LAYERS --implicit-check-not='firrtl.layer' --implicit-check-not='layers ='
4+
// RUN: circt-reduce %s --test /usr/bin/env --test-arg grep --test-arg -q --test-arg '@A::@B' --include firrtl-layer-enable-remover --keep-best=0 | FileCheck %s --check-prefix=KEEP-LAYER-B
5+
6+
// ALL-LAYERS-LABEL: firrtl.circuit "Foo"
7+
// ALL-LAYERS: firrtl.module @Foo()
8+
// ALL-LAYERS-NEXT: }
9+
10+
// KEEP-LAYER-B-LABEL: firrtl.circuit "Foo"
11+
// KEEP-LAYER-B: firrtl.module @Foo() attributes {layers = [@A::@B]}
12+
// KEEP-LAYER-B-NEXT: }
13+
firrtl.circuit "Foo" {
14+
firrtl.layer @A bind attributes {output_file = #hw.output_file<"a/">, sym_visibility = "private"} {
15+
firrtl.layer @B bind attributes {output_file = #hw.output_file<"a/b/">} {
16+
}
17+
firrtl.layer @C bind attributes {output_file = #hw.output_file<"a/c/">} {
18+
}
19+
}
20+
firrtl.module @Foo() attributes {layers = [@A, @A::@B, @A::@C]} {
21+
}
22+
}

0 commit comments

Comments
 (0)