Skip to content

Commit e126fcd

Browse files
authored
[LLVM][PM] Make LowerSIMDLoop NewPM compatible (#40884)
* [LLVM][PM] Make LowerSIMDLoop NewPM compatible * [LLVM][PM] Make DemoteFloat16 NewPM compatible * [LLVM][PM] Make MulAdd NewPM compatible * [LLVM][PM] Make RemoveNI NewPM compatible
1 parent 86184db commit e126fcd

File tree

4 files changed

+170
-91
lines changed

4 files changed

+170
-91
lines changed

src/llvm-demote-float16.cpp

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,15 @@
2020

2121
#include <llvm/IR/IRBuilder.h>
2222
#include <llvm/IR/LegacyPassManager.h>
23+
#include <llvm/IR/PassManager.h>
2324
#include <llvm/IR/Module.h>
2425
#include <llvm/Support/Debug.h>
2526

2627
using namespace llvm;
2728

2829
namespace {
2930

30-
struct DemoteFloat16Pass : public FunctionPass {
31-
static char ID;
32-
DemoteFloat16Pass() : FunctionPass(ID){};
33-
34-
private:
35-
bool runOnFunction(Function &F) override;
36-
};
37-
38-
bool DemoteFloat16Pass::runOnFunction(Function &F)
31+
static bool demoteFloat16(Function &F)
3932
{
4033
auto &ctx = F.getContext();
4134
auto T_float16 = Type::getHalfTy(ctx);
@@ -132,17 +125,41 @@ bool DemoteFloat16Pass::runOnFunction(Function &F)
132125
return false;
133126
}
134127

135-
char DemoteFloat16Pass::ID = 0;
136-
static RegisterPass<DemoteFloat16Pass>
128+
} // end anonymous namespace
129+
130+
struct DemoteFloat16 : PassInfoMixin<DemoteFloat16> {
131+
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
132+
};
133+
134+
PreservedAnalyses DemoteFloat16::run(Function &F, FunctionAnalysisManager &AM)
135+
{
136+
demoteFloat16(F);
137+
return PreservedAnalyses::all();
138+
}
139+
140+
namespace {
141+
142+
struct DemoteFloat16Legacy : public FunctionPass {
143+
static char ID;
144+
DemoteFloat16Legacy() : FunctionPass(ID){};
145+
146+
private:
147+
bool runOnFunction(Function &F) override {
148+
return demoteFloat16(F);
149+
}
150+
};
151+
152+
char DemoteFloat16Legacy::ID = 0;
153+
static RegisterPass<DemoteFloat16Legacy>
137154
Y("DemoteFloat16",
138155
"Demote Float16 operations to Float32 equivalents.",
139156
false,
140157
false);
141-
}
158+
} // end anonymous namespac
142159

143160
Pass *createDemoteFloat16Pass()
144161
{
145-
return new DemoteFloat16Pass();
162+
return new DemoteFloat16Legacy();
146163
}
147164

148165
extern "C" JL_DLLEXPORT void LLVMExtraAddDemoteFloat16Pass(LLVMPassManagerRef PM)

src/llvm-muladd.cpp

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include <llvm/IR/Value.h>
1111
#include <llvm/IR/LegacyPassManager.h>
12+
#include <llvm/IR/PassManager.h>
1213
#include <llvm/IR/Function.h>
1314
#include <llvm/IR/Instructions.h>
1415
#include <llvm/IR/IntrinsicInst.h>
@@ -34,15 +35,6 @@ using namespace llvm;
3435
* when `%v0` has no other use
3536
*/
3637

37-
struct CombineMulAdd : public FunctionPass {
38-
static char ID;
39-
CombineMulAdd() : FunctionPass(ID)
40-
{}
41-
42-
private:
43-
bool runOnFunction(Function &F) override;
44-
};
45-
4638
// Return true if this function shouldn't be called again on the other operand
4739
// This will always return false on LLVM 5.0+
4840
static bool checkCombine(Module *m, Instruction *addOp, Value *maybeMul, Value *addend,
@@ -60,7 +52,7 @@ static bool checkCombine(Module *m, Instruction *addOp, Value *maybeMul, Value *
6052
return false;
6153
}
6254

63-
bool CombineMulAdd::runOnFunction(Function &F)
55+
static bool combineMulAdd(Function &F)
6456
{
6557
Module *m = F.getParent();
6658
for (auto &BB: F) {
@@ -90,14 +82,36 @@ bool CombineMulAdd::runOnFunction(Function &F)
9082
return true;
9183
}
9284

93-
char CombineMulAdd::ID = 0;
94-
static RegisterPass<CombineMulAdd> X("CombineMulAdd", "Combine mul and add to muladd",
85+
struct CombineMulAdd : PassInfoMixin<CombineMulAdd> {
86+
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
87+
};
88+
89+
PreservedAnalyses CombineMulAdd::run(Function &F, FunctionAnalysisManager &AM)
90+
{
91+
combineMulAdd(F);
92+
return PreservedAnalyses::all();
93+
}
94+
95+
96+
struct CombineMulAddLegacy : public FunctionPass {
97+
static char ID;
98+
CombineMulAddLegacy() : FunctionPass(ID)
99+
{}
100+
101+
private:
102+
bool runOnFunction(Function &F) override {
103+
return combineMulAdd(F);
104+
}
105+
};
106+
107+
char CombineMulAddLegacy::ID = 0;
108+
static RegisterPass<CombineMulAddLegacy> X("CombineMulAdd", "Combine mul and add to muladd",
95109
false /* Only looks at CFG */,
96110
false /* Analysis Pass */);
97111

98112
Pass *createCombineMulAddPass()
99113
{
100-
return new CombineMulAdd();
114+
return new CombineMulAddLegacy();
101115
}
102116

103117
extern "C" JL_DLLEXPORT void LLVMExtraAddCombineMulAddPass(LLVMPassManagerRef PM)

src/llvm-remove-ni.cpp

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "llvm-version.h"
44

55
#include <llvm/IR/Module.h>
6+
#include <llvm/IR/PassManager.h>
67
#include <llvm/IR/LegacyPassManager.h>
78
#include <llvm/Support/Debug.h>
89

@@ -14,31 +15,48 @@ using namespace llvm;
1415

1516
namespace {
1617

17-
struct RemoveNIPass : public ModulePass {
18+
static bool removeNI(Module &M)
19+
{
20+
auto dlstr = M.getDataLayoutStr();
21+
auto nistart = dlstr.find("-ni:");
22+
if (nistart == std::string::npos)
23+
return false;
24+
auto len = dlstr.size();
25+
auto niend = nistart + 1;
26+
for (; niend < len; niend++) {
27+
if (dlstr[niend] == '-') {
28+
break;
29+
}
30+
}
31+
dlstr.erase(nistart, niend - nistart);
32+
M.setDataLayout(dlstr);
33+
return true;
34+
}
35+
}
36+
37+
struct RemoveNI : PassInfoMixin<RemoveNI> {
38+
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
39+
};
40+
41+
PreservedAnalyses RemoveNI::run(Module &M, ModuleAnalysisManager &AM)
42+
{
43+
removeNI(M);
44+
return PreservedAnalyses::all();
45+
}
46+
47+
namespace {
48+
struct RemoveNILegacy : public ModulePass {
1849
static char ID;
19-
RemoveNIPass() : ModulePass(ID) {};
50+
RemoveNILegacy() : ModulePass(ID) {};
2051

2152
bool runOnModule(Module &M)
2253
{
23-
auto dlstr = M.getDataLayoutStr();
24-
auto nistart = dlstr.find("-ni:");
25-
if (nistart == std::string::npos)
26-
return false;
27-
auto len = dlstr.size();
28-
auto niend = nistart + 1;
29-
for (; niend < len; niend++) {
30-
if (dlstr[niend] == '-') {
31-
break;
32-
}
33-
}
34-
dlstr.erase(nistart, niend - nistart);
35-
M.setDataLayout(dlstr);
36-
return true;
54+
return removeNI(M);
3755
}
3856
};
3957

40-
char RemoveNIPass::ID = 0;
41-
static RegisterPass<RemoveNIPass>
58+
char RemoveNILegacy::ID = 0;
59+
static RegisterPass<RemoveNILegacy>
4260
Y("RemoveNI",
4361
"Remove non-integral address space.",
4462
false,
@@ -47,7 +65,7 @@ static RegisterPass<RemoveNIPass>
4765

4866
Pass *createRemoveNIPass()
4967
{
50-
return new RemoveNIPass();
68+
return new RemoveNILegacy();
5169
}
5270

5371
extern "C" JL_DLLEXPORT void LLVMExtraAddRemoveNIPass(LLVMPassManagerRef PM)

src/llvm-simdloop.cpp

Lines changed: 76 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -30,35 +30,7 @@
3030

3131
namespace llvm {
3232

33-
34-
/// This pass should run after reduction variables have been converted to phi nodes,
35-
/// otherwise floating-point reductions might not be recognized as such and
36-
/// prevent SIMDization.
37-
struct LowerSIMDLoop : public ModulePass {
38-
static char ID;
39-
LowerSIMDLoop() : ModulePass(ID)
40-
{
41-
}
42-
43-
protected:
44-
void getAnalysisUsage(AnalysisUsage &AU) const override
45-
{
46-
ModulePass::getAnalysisUsage(AU);
47-
AU.addRequired<LoopInfoWrapperPass>();
48-
AU.addPreserved<LoopInfoWrapperPass>();
49-
AU.setPreservesCFG();
50-
}
51-
52-
private:
53-
bool runOnModule(Module &M) override;
54-
55-
bool markLoopInfo(Module &M, Function *marker);
56-
57-
/// If Phi is part of a reduction cycle of FAdd, FSub, FMul or FDiv,
58-
/// mark the ops as permitting reassociation/commuting.
59-
/// As of LLVM 4.0, FDiv is not handled by the loop vectorizer
60-
void enableUnsafeAlgebraIfReduction(PHINode *Phi, Loop *L) const;
61-
};
33+
namespace {
6234

6335
static unsigned getReduceOpcode(Instruction *J, Instruction *operand)
6436
{
@@ -80,7 +52,10 @@ static unsigned getReduceOpcode(Instruction *J, Instruction *operand)
8052
}
8153
}
8254

83-
void LowerSIMDLoop::enableUnsafeAlgebraIfReduction(PHINode *Phi, Loop *L) const
55+
/// If Phi is part of a reduction cycle of FAdd, FSub, FMul or FDiv,
56+
/// mark the ops as permitting reassociation/commuting.
57+
/// As of LLVM 4.0, FDiv is not handled by the loop vectorizer
58+
static void enableUnsafeAlgebraIfReduction(PHINode *Phi, Loop *L)
8459
{
8560
typedef SmallVector<Instruction*, 8> chainVector;
8661
chainVector chain;
@@ -130,26 +105,15 @@ void LowerSIMDLoop::enableUnsafeAlgebraIfReduction(PHINode *Phi, Loop *L) const
130105
}
131106
}
132107

133-
bool LowerSIMDLoop::runOnModule(Module &M)
134-
{
135-
Function *loopinfo_marker = M.getFunction("julia.loopinfo_marker");
136-
137-
bool Changed = false;
138-
if (loopinfo_marker)
139-
Changed |= markLoopInfo(M, loopinfo_marker);
140-
141-
return Changed;
142-
}
143-
144-
bool LowerSIMDLoop::markLoopInfo(Module &M, Function *marker)
108+
static bool markLoopInfo(Module &M, Function *marker, function_ref<LoopInfo &(Function &)> GetLI)
145109
{
146110
bool Changed = false;
147111
std::vector<Instruction*> ToDelete;
148112
for (User *U : marker->users()) {
149113
Instruction *I = cast<Instruction>(U);
150114
ToDelete.push_back(I);
151115

152-
LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>(*I->getParent()->getParent()).getLoopInfo();
116+
LoopInfo &LI = GetLI(*I->getParent()->getParent());
153117
Loop *L = LI.getLoopFor(I->getParent());
154118
I->removeFromParent();
155119
if (!L)
@@ -243,15 +207,81 @@ bool LowerSIMDLoop::markLoopInfo(Module &M, Function *marker)
243207
return Changed;
244208
}
245209

246-
char LowerSIMDLoop::ID = 0;
210+
} // end anonymous namespace
211+
212+
213+
/// This pass should run after reduction variables have been converted to phi nodes,
214+
/// otherwise floating-point reductions might not be recognized as such and
215+
/// prevent SIMDization.
216+
struct LowerSIMDLoop : PassInfoMixin<LowerSIMDLoop> {
217+
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
218+
};
219+
220+
221+
PreservedAnalyses LowerSIMDLoop::run(Module &M, ModuleAnalysisManager &AM)
222+
{
223+
Function *loopinfo_marker = M.getFunction("julia.loopinfo_marker");
224+
225+
if (!loopinfo_marker)
226+
return PreservedAnalyses::all();
227+
228+
FunctionAnalysisManager &FAM =
229+
AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
230+
231+
auto GetLI = [&FAM](Function &F) -> LoopInfo & {
232+
return FAM.getResult<LoopAnalysis>(F);
233+
};
234+
235+
markLoopInfo(M, loopinfo_marker, GetLI);
236+
237+
return PreservedAnalyses::all();
238+
}
239+
240+
namespace {
241+
class LowerSIMDLoopLegacy : public ModulePass {
242+
LowerSIMDLoop Impl;
243+
244+
public:
245+
static char ID;
246+
247+
LowerSIMDLoopLegacy() : ModulePass(ID) {
248+
}
249+
250+
bool runOnModule(Module &M) override {
251+
bool Changed = false;
252+
253+
Function *loopinfo_marker = M.getFunction("julia.loopinfo_marker");
254+
255+
auto GetLI = [this](Function &F) -> LoopInfo & {
256+
return getAnalysis<LoopInfoWrapperPass>(F).getLoopInfo();
257+
};
258+
259+
if (loopinfo_marker)
260+
Changed |= markLoopInfo(M, loopinfo_marker, GetLI);
261+
262+
return Changed;
263+
}
264+
265+
void getAnalysisUsage(AnalysisUsage &AU) const override
266+
{
267+
ModulePass::getAnalysisUsage(AU);
268+
AU.addRequired<LoopInfoWrapperPass>();
269+
AU.addPreserved<LoopInfoWrapperPass>();
270+
AU.setPreservesCFG();
271+
}
272+
};
273+
274+
} // end anonymous namespace
275+
276+
char LowerSIMDLoopLegacy::ID = 0;
247277

248-
static RegisterPass<LowerSIMDLoop> X("LowerSIMDLoop", "LowerSIMDLoop Pass",
278+
static RegisterPass<LowerSIMDLoopLegacy> X("LowerSIMDLoop", "LowerSIMDLoop Pass",
249279
false /* Only looks at CFG */,
250280
false /* Analysis Pass */);
251281

252282
JL_DLLEXPORT Pass *createLowerSimdLoopPass()
253283
{
254-
return new LowerSIMDLoop();
284+
return new LowerSIMDLoopLegacy();
255285
}
256286

257287
extern "C" JL_DLLEXPORT void LLVMExtraAddLowerSimdLoopPass(LLVMPassManagerRef PM)

0 commit comments

Comments
 (0)