Skip to content

Commit e03fac1

Browse files
[X86][NewPM] Port X86PartialReduction to NewPM
There are no tests that specifically stop/start at x86-partial-reduction, so no test cases have been updated for this patch. Reviewers: phoebewang, paperchalice, RKSimon, arsenm Reviewed By: arsenm, RKSimon Pull Request: #166048
1 parent 4776451 commit e03fac1

File tree

4 files changed

+60
-31
lines changed

4 files changed

+60
-31
lines changed

llvm/lib/Target/X86/X86.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,16 @@ FunctionPass *createX86InsertX87waitPass();
158158
/// This pass optimizes arithmetic based on knowledge that is only used by
159159
/// a reduction sequence and is therefore safe to reassociate in interesting
160160
/// ways.
161-
FunctionPass *createX86PartialReductionPass();
161+
class X86PartialReductionPass : public PassInfoMixin<X86PartialReductionPass> {
162+
private:
163+
const X86TargetMachine *TM;
164+
165+
public:
166+
X86PartialReductionPass(const X86TargetMachine *TM) : TM(TM) {}
167+
PreservedAnalyses run(Function &F, FunctionAnalysisManager &FAM);
168+
};
169+
170+
FunctionPass *createX86PartialReductionLegacyPass();
162171

163172
/// // Analyzes and emits pseudos to support Win x64 Unwind V2.
164173
FunctionPass *createX86WinEHUnwindV2Pass();
@@ -231,7 +240,7 @@ void initializeX86LowerAMXIntrinsicsLegacyPassPass(PassRegistry &);
231240
void initializeX86LowerAMXTypeLegacyPassPass(PassRegistry &);
232241
void initializeX86LowerTileCopyPass(PassRegistry &);
233242
void initializeX86OptimizeLEAPassPass(PassRegistry &);
234-
void initializeX86PartialReductionPass(PassRegistry &);
243+
void initializeX86PartialReductionLegacyPass(PassRegistry &);
235244
void initializeX86PreTileConfigPass(PassRegistry &);
236245
void initializeX86ReturnThunksPass(PassRegistry &);
237246
void initializeX86SpeculativeExecutionSideEffectSuppressionPass(PassRegistry &);

llvm/lib/Target/X86/X86PartialReduction.cpp

Lines changed: 46 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@
1616
#include "X86TargetMachine.h"
1717
#include "llvm/Analysis/ValueTracking.h"
1818
#include "llvm/CodeGen/TargetPassConfig.h"
19+
#include "llvm/IR/Analysis.h"
1920
#include "llvm/IR/Constants.h"
2021
#include "llvm/IR/IRBuilder.h"
2122
#include "llvm/IR/Instructions.h"
2223
#include "llvm/IR/IntrinsicsX86.h"
24+
#include "llvm/IR/PassManager.h"
2325
#include "llvm/IR/PatternMatch.h"
2426
#include "llvm/Pass.h"
2527
#include "llvm/Support/KnownBits.h"
@@ -30,39 +32,44 @@ using namespace llvm;
3032

3133
namespace {
3234

33-
class X86PartialReduction : public FunctionPass {
35+
class X86PartialReduction {
36+
const X86TargetMachine *TM;
3437
const DataLayout *DL = nullptr;
3538
const X86Subtarget *ST = nullptr;
3639

40+
public:
41+
X86PartialReduction(const X86TargetMachine *TM) : TM(TM) {}
42+
bool run(Function &F);
43+
44+
private:
45+
bool tryMAddReplacement(Instruction *Op, bool ReduceInOneBB);
46+
bool trySADReplacement(Instruction *Op);
47+
};
48+
49+
class X86PartialReductionLegacy : public FunctionPass {
3750
public:
3851
static char ID; // Pass identification, replacement for typeid.
3952

40-
X86PartialReduction() : FunctionPass(ID) { }
53+
X86PartialReductionLegacy() : FunctionPass(ID) {}
4154

42-
bool runOnFunction(Function &Fn) override;
55+
bool runOnFunction(Function &F) override;
4356

4457
void getAnalysisUsage(AnalysisUsage &AU) const override {
4558
AU.setPreservesCFG();
4659
}
4760

48-
StringRef getPassName() const override {
49-
return "X86 Partial Reduction";
50-
}
51-
52-
private:
53-
bool tryMAddReplacement(Instruction *Op, bool ReduceInOneBB);
54-
bool trySADReplacement(Instruction *Op);
61+
StringRef getPassName() const override { return "X86 Partial Reduction"; }
5562
};
5663
}
5764

58-
FunctionPass *llvm::createX86PartialReductionPass() {
59-
return new X86PartialReduction();
65+
FunctionPass *llvm::createX86PartialReductionLegacyPass() {
66+
return new X86PartialReductionLegacy();
6067
}
6168

62-
char X86PartialReduction::ID = 0;
69+
char X86PartialReductionLegacy::ID = 0;
6370

64-
INITIALIZE_PASS(X86PartialReduction, DEBUG_TYPE,
65-
"X86 Partial Reduction", false, false)
71+
INITIALIZE_PASS(X86PartialReductionLegacy, DEBUG_TYPE, "X86 Partial Reduction",
72+
false, false)
6673

6774
// This function should be aligned with detectExtMul() in X86ISelLowering.cpp.
6875
static bool matchVPDPBUSDPattern(const X86Subtarget *ST, BinaryOperator *Mul,
@@ -494,17 +501,8 @@ static void collectLeaves(Value *Root, SmallVectorImpl<Instruction *> &Leaves) {
494501
}
495502
}
496503

497-
bool X86PartialReduction::runOnFunction(Function &F) {
498-
if (skipFunction(F))
499-
return false;
500-
501-
auto *TPC = getAnalysisIfAvailable<TargetPassConfig>();
502-
if (!TPC)
503-
return false;
504-
505-
auto &TM = TPC->getTM<X86TargetMachine>();
506-
ST = TM.getSubtargetImpl(F);
507-
504+
bool X86PartialReduction::run(Function &F) {
505+
ST = TM->getSubtargetImpl(F);
508506
DL = &F.getDataLayout();
509507

510508
bool MadeChange = false;
@@ -540,3 +538,25 @@ bool X86PartialReduction::runOnFunction(Function &F) {
540538

541539
return MadeChange;
542540
}
541+
542+
bool X86PartialReductionLegacy::runOnFunction(Function &F) {
543+
if (skipFunction(F))
544+
return false;
545+
546+
auto *TPC = getAnalysisIfAvailable<TargetPassConfig>();
547+
if (!TPC)
548+
return false;
549+
550+
return X86PartialReduction(&TPC->getTM<X86TargetMachine>()).run(F);
551+
}
552+
553+
PreservedAnalyses X86PartialReductionPass::run(Function &F,
554+
FunctionAnalysisManager &FAM) {
555+
bool Changed = X86PartialReduction(TM).run(F);
556+
if (!Changed)
557+
return PreservedAnalyses::all();
558+
559+
PreservedAnalyses PA = PreservedAnalyses::none();
560+
PA.preserveSet<CFGAnalyses>();
561+
return PA;
562+
}

llvm/lib/Target/X86/X86PassRegistry.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@
1717
#endif
1818
FUNCTION_PASS("x86-lower-amx-intrinsics", X86LowerAMXIntrinsicsPass(this))
1919
FUNCTION_PASS("x86-lower-amx-type", X86LowerAMXTypePass(this))
20+
FUNCTION_PASS("x86-partial-reduction", X86PartialReductionPass(this))
2021
#undef FUNCTION_PASS
2122

2223
#ifndef DUMMY_FUNCTION_PASS
2324
#define DUMMY_FUNCTION_PASS(NAME, CREATE_PASS)
2425
#endif
25-
DUMMY_FUNCTION_PASS("x86-partial-reduction", X86PartialReduction())
2626
DUMMY_FUNCTION_PASS("x86-winehstate", WinEHStatePass())
2727
#undef DUMMY_FUNCTION_PASS
2828

llvm/lib/Target/X86/X86TargetMachine.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ extern "C" LLVM_C_ABI void LLVMInitializeX86Target() {
9797
initializeX86LoadValueInjectionLoadHardeningPassPass(PR);
9898
initializeX86LoadValueInjectionRetHardeningPassPass(PR);
9999
initializeX86OptimizeLEAPassPass(PR);
100-
initializeX86PartialReductionPass(PR);
100+
initializeX86PartialReductionLegacyPass(PR);
101101
initializePseudoProbeInserterPass(PR);
102102
initializeX86ReturnThunksPass(PR);
103103
initializeX86DAGToDAGISelLegacyPass(PR);
@@ -429,7 +429,7 @@ void X86PassConfig::addIRPasses() {
429429

430430
if (TM->getOptLevel() != CodeGenOptLevel::None) {
431431
addPass(createInterleavedAccessPass());
432-
addPass(createX86PartialReductionPass());
432+
addPass(createX86PartialReductionLegacyPass());
433433
}
434434

435435
// Add passes that handle indirect branch removal and insertion of a retpoline

0 commit comments

Comments
 (0)