Skip to content

Commit 7615063

Browse files
Make more passes NewPM compatible (#44277)
* Make PropagateAddrspaces and RemoveAddrspaces NewPM compatible * Fix LateGCLower analysis invalidation * Fix missing return
1 parent 2714b92 commit 7615063

7 files changed

+132
-59
lines changed

src/aotcompile.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -895,6 +895,10 @@ static void registerCallbacks(PassBuilder &PB) {
895895
PM.addPass(AllocOptPass());
896896
return true;
897897
}
898+
if (Name == "PropagateJuliaAddrspaces") {
899+
PM.addPass(PropagateJuliaAddrspacesPass());
900+
return true;
901+
}
898902
return false;
899903
});
900904

@@ -917,6 +921,10 @@ static void registerCallbacks(PassBuilder &PB) {
917921
PM.addPass(FinalLowerGCPass());
918922
return true;
919923
}
924+
if (Name == "RemoveJuliaAddrspaces") {
925+
PM.addPass(RemoveJuliaAddrspacesPass());
926+
return true;
927+
}
920928
return false;
921929
});
922930

src/llvm-late-gc-lowering.cpp

+18-8
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ struct LateLowerGCFrame: private JuliaPassContext {
326326
LateLowerGCFrame(function_ref<DominatorTree &()> GetDT) : GetDT(GetDT) {}
327327

328328
public:
329-
bool runOnFunction(Function &F);
329+
bool runOnFunction(Function &F, bool *CFGModified = nullptr);
330330

331331
private:
332332
CallInst *pgcstack;
@@ -358,7 +358,7 @@ struct LateLowerGCFrame: private JuliaPassContext {
358358
void PlaceGCFrameStore(State &S, unsigned R, unsigned MinColorRoot, const std::vector<int> &Colors, Value *GCFrame, Instruction *InsertBefore);
359359
void PlaceGCFrameStores(State &S, unsigned MinColorRoot, const std::vector<int> &Colors, Value *GCFrame);
360360
void PlaceRootsAndUpdateCalls(std::vector<int> &Colors, State &S, std::map<Value *, std::pair<int, int>>);
361-
bool CleanupIR(Function &F, State *S=nullptr);
361+
bool CleanupIR(Function &F, State *S, bool *CFGModified);
362362
void NoteUseChain(State &S, BBState &BBS, User *TheUser);
363363
SmallVector<int, 1> GetPHIRefinements(PHINode *phi, State &S);
364364
void FixUpRefinements(ArrayRef<int> PHINumbers, State &S);
@@ -2234,7 +2234,7 @@ MDNode *createMutableTBAAAccessTag(MDNode *Tag) {
22342234
}
22352235

22362236

2237-
bool LateLowerGCFrame::CleanupIR(Function &F, State *S) {
2237+
bool LateLowerGCFrame::CleanupIR(Function &F, State *S, bool *CFGModified) {
22382238
bool ChangesMade = false;
22392239
// We create one alloca for all the jlcall frames that haven't been processed
22402240
// yet. LLVM would merge them anyway later, so might as well save it a bit
@@ -2460,6 +2460,9 @@ bool LateLowerGCFrame::CleanupIR(Function &F, State *S) {
24602460
CI->eraseFromParent();
24612461
continue;
24622462
}
2463+
if (CFGModified) {
2464+
*CFGModified = true;
2465+
}
24632466
IRBuilder<> builder(CI);
24642467
builder.SetCurrentDebugLocation(CI->getDebugLoc());
24652468
auto parBits = builder.CreateAnd(EmitLoadTag(builder, parent), 3);
@@ -2675,22 +2678,22 @@ void LateLowerGCFrame::PlaceRootsAndUpdateCalls(std::vector<int> &Colors, State
26752678
}
26762679
}
26772680

2678-
bool LateLowerGCFrame::runOnFunction(Function &F) {
2681+
bool LateLowerGCFrame::runOnFunction(Function &F, bool *CFGModified) {
26792682
initAll(*F.getParent());
26802683
LLVM_DEBUG(dbgs() << "GC ROOT PLACEMENT: Processing function " << F.getName() << "\n");
26812684
if (!pgcstack_getter)
2682-
return CleanupIR(F);
2685+
return CleanupIR(F, nullptr, CFGModified);
26832686

26842687
pgcstack = getPGCstack(F);
26852688
if (!pgcstack)
2686-
return CleanupIR(F);
2689+
return CleanupIR(F, nullptr, CFGModified);
26872690

26882691
State S = LocalScan(F);
26892692
ComputeLiveness(S);
26902693
std::vector<int> Colors = ColorRoots(S);
26912694
std::map<Value *, std::pair<int, int>> CallFrames; // = OptimizeCallFrames(S, Ordering);
26922695
PlaceRootsAndUpdateCalls(Colors, S, CallFrames);
2693-
CleanupIR(F, &S);
2696+
CleanupIR(F, &S, CFGModified);
26942697
return true;
26952698
}
26962699

@@ -2708,7 +2711,14 @@ PreservedAnalyses LateLowerGC::run(Function &F, FunctionAnalysisManager &AM)
27082711
return AM.getResult<DominatorTreeAnalysis>(F);
27092712
};
27102713
auto lateLowerGCFrame = LateLowerGCFrame(GetDT);
2711-
lateLowerGCFrame.runOnFunction(F);
2714+
bool CFGModified = false;
2715+
if (lateLowerGCFrame.runOnFunction(F, &CFGModified)) {
2716+
if (CFGModified) {
2717+
return PreservedAnalyses::none();
2718+
} else {
2719+
return PreservedAnalyses::allInSet<CFGAnalyses>();
2720+
}
2721+
}
27122722
return PreservedAnalyses::all();
27132723
}
27142724

src/llvm-propagate-addrspaces.cpp

+45-29
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
#include "codegen_shared.h"
2626
#include "julia.h"
27+
#include "passes.h"
2728

2829
#define DEBUG_TYPE "propagate_julia_addrspaces"
2930

@@ -40,16 +41,13 @@ using namespace llvm;
4041
optimizations.
4142
*/
4243

43-
struct PropagateJuliaAddrspaces : public FunctionPass, public InstVisitor<PropagateJuliaAddrspaces> {
44-
static char ID;
44+
struct PropagateJuliaAddrspacesVisitor : public InstVisitor<PropagateJuliaAddrspacesVisitor> {
4545
DenseMap<Value *, Value *> LiftingMap;
4646
SmallPtrSet<Value *, 4> Visited;
4747
std::vector<Instruction *> ToDelete;
4848
std::vector<std::pair<Instruction *, Instruction *>> ToInsert;
49-
PropagateJuliaAddrspaces() : FunctionPass(ID) {};
5049

5150
public:
52-
bool runOnFunction(Function &F) override;
5351
Value *LiftPointer(Value *V, Type *LocTy = nullptr, Instruction *InsertPt=nullptr);
5452
void visitMemop(Instruction &I, Type *T, unsigned OpIndex);
5553
void visitLoadInst(LoadInst &LI);
@@ -63,19 +61,6 @@ struct PropagateJuliaAddrspaces : public FunctionPass, public InstVisitor<Propag
6361
void PoisonValues(std::vector<Value *> &Worklist);
6462
};
6563

66-
bool PropagateJuliaAddrspaces::runOnFunction(Function &F) {
67-
visit(F);
68-
for (auto it : ToInsert)
69-
it.first->insertBefore(it.second);
70-
for (Instruction *I : ToDelete)
71-
I->eraseFromParent();
72-
ToInsert.clear();
73-
ToDelete.clear();
74-
LiftingMap.clear();
75-
Visited.clear();
76-
return true;
77-
}
78-
7964
static unsigned getValueAddrSpace(Value *V) {
8065
return cast<PointerType>(V->getType())->getAddressSpace();
8166
}
@@ -84,7 +69,7 @@ static bool isSpecialAS(unsigned AS) {
8469
return AddressSpace::FirstSpecial <= AS && AS <= AddressSpace::LastSpecial;
8570
}
8671

87-
void PropagateJuliaAddrspaces::PoisonValues(std::vector<Value *> &Worklist) {
72+
void PropagateJuliaAddrspacesVisitor::PoisonValues(std::vector<Value *> &Worklist) {
8873
while (!Worklist.empty()) {
8974
Value *CurrentV = Worklist.back();
9075
Worklist.pop_back();
@@ -97,7 +82,7 @@ void PropagateJuliaAddrspaces::PoisonValues(std::vector<Value *> &Worklist) {
9782
}
9883
}
9984

100-
Value *PropagateJuliaAddrspaces::LiftPointer(Value *V, Type *LocTy, Instruction *InsertPt) {
85+
Value *PropagateJuliaAddrspacesVisitor::LiftPointer(Value *V, Type *LocTy, Instruction *InsertPt) {
10186
SmallVector<Value *, 4> Stack;
10287
std::vector<Value *> Worklist;
10388
std::set<Value *> LocalVisited;
@@ -232,7 +217,7 @@ Value *PropagateJuliaAddrspaces::LiftPointer(Value *V, Type *LocTy, Instruction
232217
return CollapseCastsAndLift(V, InsertPt);
233218
}
234219

235-
void PropagateJuliaAddrspaces::visitMemop(Instruction &I, Type *T, unsigned OpIndex) {
220+
void PropagateJuliaAddrspacesVisitor::visitMemop(Instruction &I, Type *T, unsigned OpIndex) {
236221
Value *Original = I.getOperand(OpIndex);
237222
unsigned AS = Original->getType()->getPointerAddressSpace();
238223
if (!isSpecialAS(AS))
@@ -243,23 +228,23 @@ void PropagateJuliaAddrspaces::visitMemop(Instruction &I, Type *T, unsigned OpIn
243228
I.setOperand(OpIndex, Replacement);
244229
}
245230

246-
void PropagateJuliaAddrspaces::visitLoadInst(LoadInst &LI) {
231+
void PropagateJuliaAddrspacesVisitor::visitLoadInst(LoadInst &LI) {
247232
visitMemop(LI, LI.getType(), LoadInst::getPointerOperandIndex());
248233
}
249234

250-
void PropagateJuliaAddrspaces::visitStoreInst(StoreInst &SI) {
235+
void PropagateJuliaAddrspacesVisitor::visitStoreInst(StoreInst &SI) {
251236
visitMemop(SI, SI.getValueOperand()->getType(), StoreInst::getPointerOperandIndex());
252237
}
253238

254-
void PropagateJuliaAddrspaces::visitAtomicCmpXchgInst(AtomicCmpXchgInst &SI) {
239+
void PropagateJuliaAddrspacesVisitor::visitAtomicCmpXchgInst(AtomicCmpXchgInst &SI) {
255240
visitMemop(SI, SI.getNewValOperand()->getType(), AtomicCmpXchgInst::getPointerOperandIndex());
256241
}
257242

258-
void PropagateJuliaAddrspaces::visitAtomicRMWInst(AtomicRMWInst &SI) {
243+
void PropagateJuliaAddrspacesVisitor::visitAtomicRMWInst(AtomicRMWInst &SI) {
259244
visitMemop(SI, SI.getType(), AtomicRMWInst::getPointerOperandIndex());
260245
}
261246

262-
void PropagateJuliaAddrspaces::visitMemSetInst(MemSetInst &MI) {
247+
void PropagateJuliaAddrspacesVisitor::visitMemSetInst(MemSetInst &MI) {
263248
unsigned AS = MI.getDestAddressSpace();
264249
if (!isSpecialAS(AS))
265250
return;
@@ -272,7 +257,7 @@ void PropagateJuliaAddrspaces::visitMemSetInst(MemSetInst &MI) {
272257
MI.setArgOperand(0, Replacement);
273258
}
274259

275-
void PropagateJuliaAddrspaces::visitMemTransferInst(MemTransferInst &MTI) {
260+
void PropagateJuliaAddrspacesVisitor::visitMemTransferInst(MemTransferInst &MTI) {
276261
unsigned DestAS = MTI.getDestAddressSpace();
277262
unsigned SrcAS = MTI.getSourceAddressSpace();
278263
if (!isSpecialAS(DestAS) && !isSpecialAS(SrcAS))
@@ -299,11 +284,42 @@ void PropagateJuliaAddrspaces::visitMemTransferInst(MemTransferInst &MTI) {
299284
MTI.setArgOperand(1, Src);
300285
}
301286

302-
char PropagateJuliaAddrspaces::ID = 0;
303-
static RegisterPass<PropagateJuliaAddrspaces> X("PropagateJuliaAddrspaces", "Propagate (non-)rootedness information", false, false);
287+
bool propagateJuliaAddrspaces(Function &F) {
288+
PropagateJuliaAddrspacesVisitor visitor;
289+
visitor.visit(F);
290+
for (auto it : visitor.ToInsert)
291+
it.first->insertBefore(it.second);
292+
for (Instruction *I : visitor.ToDelete)
293+
I->eraseFromParent();
294+
visitor.ToInsert.clear();
295+
visitor.ToDelete.clear();
296+
visitor.LiftingMap.clear();
297+
visitor.Visited.clear();
298+
return true;
299+
}
300+
301+
struct PropagateJuliaAddrspacesLegacy : FunctionPass {
302+
static char ID;
303+
304+
PropagateJuliaAddrspacesLegacy() : FunctionPass(ID) {}
305+
bool runOnFunction(Function &F) override {
306+
return propagateJuliaAddrspaces(F);
307+
}
308+
};
309+
310+
char PropagateJuliaAddrspacesLegacy::ID = 0;
311+
static RegisterPass<PropagateJuliaAddrspacesLegacy> X("PropagateJuliaAddrspaces", "Propagate (non-)rootedness information", false, false);
304312

305313
Pass *createPropagateJuliaAddrspaces() {
306-
return new PropagateJuliaAddrspaces();
314+
return new PropagateJuliaAddrspacesLegacy();
315+
}
316+
317+
PreservedAnalyses PropagateJuliaAddrspacesPass::run(Function &F, FunctionAnalysisManager &AM) {
318+
if (propagateJuliaAddrspaces(F)) {
319+
return PreservedAnalyses::allInSet<CFGAnalyses>();
320+
} else {
321+
return PreservedAnalyses::all();
322+
}
307323
}
308324

309325
extern "C" JL_DLLEXPORT void LLVMExtraAddPropagateJuliaAddrspaces_impl(LLVMPassManagerRef PM)

src/llvm-remove-addrspaces.cpp

+40-22
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
#include "codegen_shared.h"
1616
#include "julia.h"
17+
#include "passes.h"
1718

1819
#define DEBUG_TYPE "remove_addrspaces"
1920

@@ -231,18 +232,7 @@ unsigned removeAllAddrspaces(unsigned AS)
231232
return AddressSpace::Generic;
232233
}
233234

234-
struct RemoveAddrspacesPass : public ModulePass {
235-
static char ID;
236-
AddrspaceRemapFunction ASRemapper;
237-
RemoveAddrspacesPass(
238-
AddrspaceRemapFunction ASRemapper = removeAllAddrspaces)
239-
: ModulePass(ID), ASRemapper(ASRemapper){};
240-
241-
public:
242-
bool runOnModule(Module &M) override;
243-
};
244-
245-
bool RemoveAddrspacesPass::runOnModule(Module &M)
235+
bool removeAddrspaces(Module &M, AddrspaceRemapFunction ASRemapper)
246236
{
247237
ValueToValueMapTy VMap;
248238
AddrspaceRemoveTypeRemapper TypeRemapper(ASRemapper);
@@ -457,8 +447,22 @@ bool RemoveAddrspacesPass::runOnModule(Module &M)
457447
return true;
458448
}
459449

460-
char RemoveAddrspacesPass::ID = 0;
461-
static RegisterPass<RemoveAddrspacesPass>
450+
451+
struct RemoveAddrspacesPassLegacy : public ModulePass {
452+
static char ID;
453+
AddrspaceRemapFunction ASRemapper;
454+
RemoveAddrspacesPassLegacy(
455+
AddrspaceRemapFunction ASRemapper = removeAllAddrspaces)
456+
: ModulePass(ID), ASRemapper(ASRemapper){};
457+
458+
public:
459+
bool runOnModule(Module &M) override {
460+
return removeAddrspaces(M, ASRemapper);
461+
}
462+
};
463+
464+
char RemoveAddrspacesPassLegacy::ID = 0;
465+
static RegisterPass<RemoveAddrspacesPassLegacy>
462466
X("RemoveAddrspaces",
463467
"Remove IR address space information.",
464468
false,
@@ -467,7 +471,17 @@ static RegisterPass<RemoveAddrspacesPass>
467471
Pass *createRemoveAddrspacesPass(
468472
AddrspaceRemapFunction ASRemapper = removeAllAddrspaces)
469473
{
470-
return new RemoveAddrspacesPass(ASRemapper);
474+
return new RemoveAddrspacesPassLegacy(ASRemapper);
475+
}
476+
477+
RemoveAddrspacesPass::RemoveAddrspacesPass() : RemoveAddrspacesPass(removeAllAddrspaces) {}
478+
479+
PreservedAnalyses RemoveAddrspacesPass::run(Module &M, ModuleAnalysisManager &AM) {
480+
if (removeAddrspaces(M, ASRemapper)) {
481+
return PreservedAnalyses::allInSet<CFGAnalyses>();
482+
} else {
483+
return PreservedAnalyses::all();
484+
}
471485
}
472486

473487

@@ -483,24 +497,28 @@ unsigned removeJuliaAddrspaces(unsigned AS)
483497
return AS;
484498
}
485499

486-
struct RemoveJuliaAddrspacesPass : public ModulePass {
500+
struct RemoveJuliaAddrspacesPassLegacy : public ModulePass {
487501
static char ID;
488-
RemoveAddrspacesPass Pass;
489-
RemoveJuliaAddrspacesPass() : ModulePass(ID), Pass(removeJuliaAddrspaces){};
502+
RemoveAddrspacesPassLegacy Pass;
503+
RemoveJuliaAddrspacesPassLegacy() : ModulePass(ID), Pass(removeJuliaAddrspaces){};
490504

491-
bool runOnModule(Module &M) { return Pass.runOnModule(M); }
505+
bool runOnModule(Module &M) override { return Pass.runOnModule(M); }
492506
};
493507

494-
char RemoveJuliaAddrspacesPass::ID = 0;
495-
static RegisterPass<RemoveJuliaAddrspacesPass>
508+
char RemoveJuliaAddrspacesPassLegacy::ID = 0;
509+
static RegisterPass<RemoveJuliaAddrspacesPassLegacy>
496510
Y("RemoveJuliaAddrspaces",
497511
"Remove IR address space information.",
498512
false,
499513
false);
500514

501515
Pass *createRemoveJuliaAddrspacesPass()
502516
{
503-
return new RemoveJuliaAddrspacesPass();
517+
return new RemoveJuliaAddrspacesPassLegacy();
518+
}
519+
520+
PreservedAnalyses RemoveJuliaAddrspacesPass::run(Module &M, ModuleAnalysisManager &AM) {
521+
return RemoveAddrspacesPass(removeJuliaAddrspaces).run(M, AM);
504522
}
505523

506524
extern "C" JL_DLLEXPORT void LLVMExtraAddRemoveJuliaAddrspacesPass_impl(LLVMPassManagerRef PM)

src/passes.h

+19
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ struct LateLowerGC : PassInfoMixin<LateLowerGC> {
2626
struct AllocOptPass : PassInfoMixin<AllocOptPass> {
2727
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
2828
};
29+
struct PropagateJuliaAddrspacesPass : PassInfoMixin<PropagateJuliaAddrspacesPass> {
30+
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
31+
static bool isRequired() { return true; }
32+
};
2933

3034
// Module Passes
3135
struct CPUFeatures : PassInfoMixin<CPUFeatures> {
@@ -46,6 +50,21 @@ struct FinalLowerGCPass : PassInfoMixin<LateLowerGC> {
4650
static bool isRequired() { return true; }
4751
};
4852

53+
struct RemoveJuliaAddrspacesPass : PassInfoMixin<RemoveJuliaAddrspacesPass> {
54+
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
55+
static bool isRequired() { return true; }
56+
};
57+
58+
struct RemoveAddrspacesPass : PassInfoMixin<RemoveAddrspacesPass> {
59+
60+
std::function<unsigned(unsigned)> ASRemapper;
61+
RemoveAddrspacesPass();
62+
RemoveAddrspacesPass(std::function<unsigned(unsigned)> ASRemapper) : ASRemapper(std::move(ASRemapper)) {}
63+
64+
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
65+
static bool isRequired() { return true; }
66+
};
67+
4968
// Loop Passes
5069
struct JuliaLICMPass : PassInfoMixin<JuliaLICMPass> {
5170
PreservedAnalyses run(Loop &L, LoopAnalysisManager &AM,

0 commit comments

Comments
 (0)