Skip to content

Commit fc5ffe3

Browse files
committed
[Reduce] Add reduction that makes symbols private
Add the `MakeSymbolsPrivate` reduction pattern which changes symbol ops' visibility to private.
1 parent ff3aa23 commit fc5ffe3

File tree

3 files changed

+50
-35
lines changed

3 files changed

+50
-35
lines changed

lib/Reduce/GenericReductions.cpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include "circt/Reduce/GenericReductions.h"
1010
#include "circt/Reduce/ReductionUtils.h"
11+
#include "mlir/IR/SymbolTable.h"
1112
#include "mlir/Transforms/GreedyPatternRewriteDriver.h"
1213
#include "mlir/Transforms/Passes.h"
1314

@@ -66,6 +67,26 @@ struct UnusedSymbolPruner : public Reduction {
6667
std::unique_ptr<SymbolUserMap> symbolUsers;
6768
};
6869

70+
/// A reduction pattern that changes symbol visibility from "public" to
71+
/// "private".
72+
struct MakeSymbolsPrivate : public Reduction {
73+
uint64_t match(Operation *op) override {
74+
if (!op->getParentOp() || !isa<SymbolOpInterface>(op))
75+
return 0;
76+
return SymbolTable::getSymbolVisibility(op) !=
77+
SymbolTable::Visibility::Private;
78+
}
79+
80+
LogicalResult rewrite(Operation *op) override {
81+
// Change the visibility from public to private
82+
SymbolTable::setSymbolVisibility(op, SymbolTable::Visibility::Private);
83+
return success();
84+
}
85+
86+
std::string getName() const override { return "make-symbols-private"; }
87+
bool acceptSizeIncrease() const override { return true; }
88+
};
89+
6990
} // namespace
7091

7192
//===----------------------------------------------------------------------===//
@@ -85,6 +106,7 @@ void circt::populateGenericReducePatterns(MLIRContext *context,
85106
patterns.add<PassReduction, 103>(context, createSymbolDCEPass());
86107
patterns.add<PassReduction, 102>(context, createCSEPass());
87108
patterns.add<PassReduction, 101>(context, createSimpleCanonicalizerPass());
88-
patterns.add<UnusedSymbolPruner, 100>();
109+
patterns.add<MakeSymbolsPrivate, 100>();
110+
patterns.add<UnusedSymbolPruner, 99>();
89111
patterns.add<OperationPruner, 1>();
90112
}

test/Dialect/FIRRTL/Reduction/trivial.mlir

Lines changed: 0 additions & 34 deletions
This file was deleted.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// UNSUPPORTED: system-windows
2+
// See https://github.com/llvm/circt/issues/4129
3+
// RUN: circt-reduce %s --test /bin/true --include make-symbols-private | FileCheck %s
4+
5+
// This test verifies that the symbol visibility reducer changes public symbols to private
6+
7+
// CHECK-LABEL: func.func private @publicFunc
8+
func.func public @publicFunc() {
9+
return
10+
}
11+
12+
// CHECK-LABEL: func.func private @anotherPublicFunc
13+
func.func public @anotherPublicFunc() {
14+
return
15+
}
16+
17+
// This should remain unchanged as it's already private
18+
// CHECK-LABEL: func.func private @privateFunc
19+
func.func private @privateFunc() {
20+
return
21+
}
22+
23+
// This should be changed from default (public) to explicit private
24+
// CHECK-LABEL: func.func private @defaultFunc
25+
func.func @defaultFunc() {
26+
return
27+
}

0 commit comments

Comments
 (0)