Skip to content

Commit 7e25b24

Browse files
authored
IRNormalizer: Replace cl::opts with pass parameters (#133874)
Not sure why the "fold-all" option naming didn't match the variable "FoldPreOutputs", but I've preserved the difference. More annoyingly, the pass name "normalize" does not match the pass name IRNormalizer and should probably be fixed one way or the other. Also the existing test coverage for the flags is lacking. I've added a test that shows they parse, but we should have tests that they do something.
1 parent 105c8c3 commit 7e25b24

File tree

6 files changed

+86
-36
lines changed

6 files changed

+86
-36
lines changed

llvm/include/llvm/Transforms/Utils/IRNormalizer.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,29 @@
55

66
namespace llvm {
77

8+
struct IRNormalizerOptions {
9+
/// Preserves original instruction order.
10+
bool PreserveOrder = false;
11+
12+
/// Renames all instructions (including user-named)
13+
bool RenameAll = true;
14+
15+
/// Folds all regular instructions (including pre-outputs)
16+
bool FoldPreOutputs = true;
17+
18+
/// Sorts and reorders operands in commutative instructions
19+
bool ReorderOperands = true;
20+
};
21+
822
/// IRNormalizer aims to transform LLVM IR into normal form.
923
struct IRNormalizerPass : public PassInfoMixin<IRNormalizerPass> {
24+
private:
25+
const IRNormalizerOptions Options;
26+
27+
public:
28+
IRNormalizerPass(IRNormalizerOptions Options = IRNormalizerOptions())
29+
: Options(Options) {}
30+
1031
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM) const;
1132
};
1233

llvm/lib/Passes/PassBuilder.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -793,6 +793,31 @@ Expected<bool> parseLowerMatrixIntrinsicsPassOptions(StringRef Params) {
793793
"LowerMatrixIntrinsics");
794794
}
795795

796+
Expected<IRNormalizerOptions> parseIRNormalizerPassOptions(StringRef Params) {
797+
IRNormalizerOptions Result;
798+
while (!Params.empty()) {
799+
StringRef ParamName;
800+
std::tie(ParamName, Params) = Params.split(';');
801+
802+
bool Enable = !ParamName.consume_front("no-");
803+
if (ParamName == "preserve-order")
804+
Result.PreserveOrder = Enable;
805+
else if (ParamName == "rename-all")
806+
Result.RenameAll = Enable;
807+
else if (ParamName == "fold-all") // FIXME: Name mismatch
808+
Result.FoldPreOutputs = Enable;
809+
else if (ParamName == "reorder-operands")
810+
Result.ReorderOperands = Enable;
811+
else {
812+
return make_error<StringError>(
813+
formatv("invalid normalize pass parameter '{0}' ", ParamName).str(),
814+
inconvertibleErrorCode());
815+
}
816+
}
817+
818+
return Result;
819+
}
820+
796821
Expected<AddressSanitizerOptions> parseASanPassOptions(StringRef Params) {
797822
AddressSanitizerOptions Result;
798823
while (!Params.empty()) {

llvm/lib/Passes/PassRegistry.def

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,6 @@ FUNCTION_PASS("move-auto-init", MoveAutoInitPass())
422422
FUNCTION_PASS("nary-reassociate", NaryReassociatePass())
423423
FUNCTION_PASS("newgvn", NewGVNPass())
424424
FUNCTION_PASS("no-op-function", NoOpFunctionPass())
425-
FUNCTION_PASS("normalize", IRNormalizerPass())
426425
FUNCTION_PASS("objc-arc", ObjCARCOptPass())
427426
FUNCTION_PASS("objc-arc-contract", ObjCARCContractPass())
428427
FUNCTION_PASS("objc-arc-expand", ObjCARCExpandPass())
@@ -568,6 +567,11 @@ FUNCTION_PASS_WITH_PARAMS(
568567
"lower-matrix-intrinsics", "LowerMatrixIntrinsicsPass",
569568
[](bool Minimal) { return LowerMatrixIntrinsicsPass(Minimal); },
570569
parseLowerMatrixIntrinsicsPassOptions, "minimal")
570+
FUNCTION_PASS_WITH_PARAMS(
571+
"normalize", "IRNormalizerPass",
572+
[](IRNormalizerOptions Options) { return IRNormalizerPass(Options); },
573+
parseIRNormalizerPassOptions,
574+
"no-preserve-order;preserve-order;no-rename-all;rename-all;no-fold-all;fold-all;no-reorder-operands;reorder-operands")
571575
FUNCTION_PASS_WITH_PARAMS(
572576
"mldst-motion", "MergedLoadStoreMotionPass",
573577
[](MergedLoadStoreMotionOptions Opts) {

llvm/lib/Transforms/Utils/IRNormalizer.cpp

Lines changed: 13 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -40,21 +40,13 @@ namespace {
4040
/// IRNormalizer aims to transform LLVM IR into normal form.
4141
class IRNormalizer {
4242
public:
43-
/// \name Normalizer flags.
44-
/// @{
45-
/// Preserves original order of instructions.
46-
static cl::opt<bool> PreserveOrder;
47-
/// Renames all instructions (including user-named).
48-
static cl::opt<bool> RenameAll; // TODO: Don't rename on empty name
49-
/// Folds all regular instructions (including pre-outputs).
50-
static cl::opt<bool> FoldPreOutputs;
51-
/// Sorts and reorders operands in commutative instructions.
52-
static cl::opt<bool> ReorderOperands;
53-
/// @}
54-
5543
bool runOnFunction(Function &F);
5644

45+
IRNormalizer(IRNormalizerOptions Options) : Options(Options) {}
46+
5747
private:
48+
const IRNormalizerOptions Options;
49+
5850
// Random constant for hashing, so the state isn't zero.
5951
const uint64_t MagicHashConstant = 0x6acaa36bef8325c5ULL;
6052
DenseSet<const Instruction *> NamedInstructions;
@@ -96,19 +88,6 @@ class IRNormalizer {
9688
};
9789
} // namespace
9890

99-
cl::opt<bool> IRNormalizer::PreserveOrder(
100-
"norm-preserve-order", cl::Hidden, cl::init(false),
101-
cl::desc("Preserves original instruction order"));
102-
cl::opt<bool> IRNormalizer::RenameAll(
103-
"norm-rename-all", cl::Hidden, cl::init(true),
104-
cl::desc("Renames all instructions (including user-named)"));
105-
cl::opt<bool> IRNormalizer::FoldPreOutputs(
106-
"norm-fold-all", cl::Hidden, cl::init(true),
107-
cl::desc("Folds all regular instructions (including pre-outputs)"));
108-
cl::opt<bool> IRNormalizer::ReorderOperands(
109-
"norm-reorder-operands", cl::Hidden, cl::init(true),
110-
cl::desc("Sorts and reorders operands in commutative instructions"));
111-
11291
/// Entry method to the IRNormalizer.
11392
///
11493
/// \param F Function to normalize.
@@ -118,7 +97,7 @@ bool IRNormalizer::runOnFunction(Function &F) {
11897

11998
Outputs = collectOutputInstructions(F);
12099

121-
if (!PreserveOrder)
100+
if (!Options.PreserveOrder)
122101
reorderInstructions(F);
123102

124103
// TODO: Reorder basic blocks via a topological sort.
@@ -127,8 +106,8 @@ bool IRNormalizer::runOnFunction(Function &F) {
127106
nameInstruction(I);
128107

129108
for (auto &I : instructions(F)) {
130-
if (!PreserveOrder) {
131-
if (ReorderOperands)
109+
if (!Options.PreserveOrder) {
110+
if (Options.ReorderOperands)
132111
reorderInstructionOperandsByNames(&I);
133112

134113
if (auto *Phi = dyn_cast<PHINode>(&I))
@@ -146,7 +125,7 @@ bool IRNormalizer::runOnFunction(Function &F) {
146125
void IRNormalizer::nameFunctionArguments(Function &F) const {
147126
int ArgumentCounter = 0;
148127
for (auto &A : F.args()) {
149-
if (RenameAll || A.getName().empty()) {
128+
if (Options.RenameAll || A.getName().empty()) {
150129
A.setName("a" + Twine(ArgumentCounter));
151130
ArgumentCounter += 1;
152131
}
@@ -167,7 +146,7 @@ void IRNormalizer::nameBasicBlocks(Function &F) const {
167146
if (isOutput(&I))
168147
Hash = hashing::detail::hash_16_bytes(Hash, I.getOpcode());
169148

170-
if (RenameAll || B.getName().empty()) {
149+
if (Options.RenameAll || B.getName().empty()) {
171150
// Name basic block. Substring hash to make diffs more readable.
172151
B.setName("bb" + std::to_string(Hash).substr(0, 5));
173152
}
@@ -219,7 +198,7 @@ void IRNormalizer::sortCommutativeOperands(Instruction *I, T &Operands) const {
219198
void IRNormalizer::nameAsInitialInstruction(Instruction *I) const {
220199
if (I->getType()->isVoidTy())
221200
return;
222-
if (!(I->getName().empty() || RenameAll))
201+
if (!(I->getName().empty() || Options.RenameAll))
223202
return;
224203
LLVM_DEBUG(dbgs() << "Naming initial instruction: " << *I << "\n");
225204

@@ -359,7 +338,7 @@ void IRNormalizer::nameAsRegularInstruction(Instruction *I) {
359338
}
360339
Name.append(")");
361340

362-
if ((I->getName().empty() || RenameAll) && !I->getType()->isVoidTy())
341+
if ((I->getName().empty() || Options.RenameAll) && !I->getType()->isVoidTy())
363342
I->setName(Name);
364343
}
365344

@@ -379,7 +358,7 @@ void IRNormalizer::nameAsRegularInstruction(Instruction *I) {
379358
void IRNormalizer::foldInstructionName(Instruction *I) const {
380359
// If this flag is raised, fold all regular
381360
// instructions (including pre-outputs).
382-
if (!FoldPreOutputs) {
361+
if (!Options.FoldPreOutputs) {
383362
// Don't fold if one of the users is an output instruction.
384363
for (auto *U : I->users())
385364
if (auto *IU = dyn_cast<Instruction>(U))
@@ -690,7 +669,7 @@ SetVector<int> IRNormalizer::getOutputFootprint(
690669

691670
PreservedAnalyses IRNormalizerPass::run(Function &F,
692671
FunctionAnalysisManager &AM) const {
693-
IRNormalizer{}.runOnFunction(F);
672+
IRNormalizer(Options).runOnFunction(F);
694673
PreservedAnalyses PA;
695674
PA.preserveSet<CFGAnalyses>();
696675
return PA;
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
; RUN: not opt -S -passes='normalize<invalid>' %s 2>&1 | FileCheck -check-prefix=ERR %s
2+
; RUN: opt -S -passes=normalize < %s | FileCheck %s
3+
; RUN: opt -S -passes='normalize<>' < %s | FileCheck %s
4+
; RUN: opt -S -passes='normalize<preserve-order;rename-all;fold-all;reorder-operands>' < %s | FileCheck %s
5+
; RUN: opt -S -passes='normalize<no-preserve-order;no-rename-all;no-fold-all;no-reorder-operands>' < %s | FileCheck %s
6+
7+
; FIXME: This verifies all the pass parameter names parse, but not
8+
; that they work as expected.
9+
10+
; ERR: invalid normalize pass parameter 'invalid'
11+
12+
; CHECK: define i32 @0(i32 %a0, i32 %a1) {
13+
; CHECK-NEXT: bb17254:
14+
; CHECK-NEXT: %"vl12603(%a0, %a1)" = add i32 %a0, %a1
15+
; CHECK-NEXT: ret i32 %"vl12603(%a0, %a1)"
16+
; CHECK-NEXT: }
17+
define i32 @0(i32, i32) {
18+
%3 = add i32 %0, %1
19+
ret i32 %3
20+
}
21+

llvm/test/Transforms/IRNormalizer/reordering.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 4
2-
; RUN: opt -S -passes=normalize -verify-each -norm-rename-all=false < %s | FileCheck %s
2+
; RUN: opt -S -passes='normalize<no-rename-all>' -verify-each < %s | FileCheck %s
33

44
define void @foo() {
55
; CHECK-LABEL: define void @foo() {

0 commit comments

Comments
 (0)