Skip to content

Commit d6dbe77

Browse files
authored
[AMDGPU][Attributor] Add ThinOrFullLTOPhase as an argument (#123994)
1 parent 52d2b58 commit d6dbe77

File tree

5 files changed

+46
-15
lines changed

5 files changed

+46
-15
lines changed

llvm/include/llvm/Pass.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,23 @@ enum class ThinOrFullLTOPhase {
8686
FullLTOPostLink
8787
};
8888

89+
#ifndef NDEBUG
90+
constexpr const char *to_string(ThinOrFullLTOPhase Phase) {
91+
switch (Phase) {
92+
case ThinOrFullLTOPhase::None:
93+
return "None";
94+
case ThinOrFullLTOPhase::ThinLTOPreLink:
95+
return "ThinLTOPreLink";
96+
case ThinOrFullLTOPhase::ThinLTOPostLink:
97+
return "ThinLTOPostLink";
98+
case ThinOrFullLTOPhase::FullLTOPreLink:
99+
return "FullLTOPreLink";
100+
case ThinOrFullLTOPhase::FullLTOPostLink:
101+
return "FullLTOPostLink";
102+
}
103+
}
104+
#endif
105+
89106
//===----------------------------------------------------------------------===//
90107
/// Pass interface - Implemented by all 'passes'. Subclass this if you are an
91108
/// interprocedural optimization or you do not fit into any of the more

llvm/lib/Target/AMDGPU/AMDGPU.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -336,9 +336,12 @@ class AMDGPUAttributorPass : public PassInfoMixin<AMDGPUAttributorPass> {
336336

337337
AMDGPUAttributorOptions Options;
338338

339+
const ThinOrFullLTOPhase LTOPhase;
340+
339341
public:
340-
AMDGPUAttributorPass(TargetMachine &TM, AMDGPUAttributorOptions Options = {})
341-
: TM(TM), Options(Options) {};
342+
AMDGPUAttributorPass(TargetMachine &TM, AMDGPUAttributorOptions Options,
343+
ThinOrFullLTOPhase LTOPhase = ThinOrFullLTOPhase::None)
344+
: TM(TM), Options(Options), LTOPhase(LTOPhase) {};
342345
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
343346
};
344347

llvm/lib/Target/AMDGPU/AMDGPUAttributor.cpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1343,7 +1343,8 @@ static void addPreloadKernArgHint(Function &F, TargetMachine &TM) {
13431343
}
13441344

13451345
static bool runImpl(Module &M, AnalysisGetter &AG, TargetMachine &TM,
1346-
AMDGPUAttributorOptions Options) {
1346+
AMDGPUAttributorOptions Options,
1347+
ThinOrFullLTOPhase LTOPhase) {
13471348
SetVector<Function *> Functions;
13481349
for (Function &F : M) {
13491350
if (!F.isIntrinsic())
@@ -1378,9 +1379,13 @@ static bool runImpl(Module &M, AnalysisGetter &AG, TargetMachine &TM,
13781379

13791380
Attributor A(Functions, InfoCache, AC);
13801381

1381-
LLVM_DEBUG(dbgs() << "[AMDGPUAttributor] Module " << M.getName() << " is "
1382-
<< (AC.IsClosedWorldModule ? "" : "not ")
1383-
<< "assumed to be a closed world.\n");
1382+
LLVM_DEBUG({
1383+
StringRef LTOPhaseStr = to_string(LTOPhase);
1384+
dbgs() << "[AMDGPUAttributor] Running at phase " << LTOPhaseStr << '\n'
1385+
<< "[AMDGPUAttributor] Module " << M.getName() << " is "
1386+
<< (AC.IsClosedWorldModule ? "" : "not ")
1387+
<< "assumed to be a closed world.\n";
1388+
});
13841389

13851390
for (auto *F : Functions) {
13861391
A.getOrCreateAAFor<AAAMDAttributes>(IRPosition::function(*F));
@@ -1433,7 +1438,8 @@ class AMDGPUAttributorLegacy : public ModulePass {
14331438

14341439
bool runOnModule(Module &M) override {
14351440
AnalysisGetter AG(this);
1436-
return runImpl(M, AG, *TM, /*Options=*/{});
1441+
return runImpl(M, AG, *TM, /*Options=*/{},
1442+
/*LTOPhase=*/ThinOrFullLTOPhase::None);
14371443
}
14381444

14391445
void getAnalysisUsage(AnalysisUsage &AU) const override {
@@ -1454,8 +1460,8 @@ PreservedAnalyses llvm::AMDGPUAttributorPass::run(Module &M,
14541460
AnalysisGetter AG(FAM);
14551461

14561462
// TODO: Probably preserves CFG
1457-
return runImpl(M, AG, TM, Options) ? PreservedAnalyses::none()
1458-
: PreservedAnalyses::all();
1463+
return runImpl(M, AG, TM, Options, LTOPhase) ? PreservedAnalyses::none()
1464+
: PreservedAnalyses::all();
14591465
}
14601466

14611467
char AMDGPUAttributorLegacy::ID = 0;

llvm/lib/Target/AMDGPU/AMDGPUTargetMachine.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -884,8 +884,10 @@ void AMDGPUTargetMachine::registerPassBuilderCallbacks(PassBuilder &PB) {
884884
OptimizationLevel Level,
885885
ThinOrFullLTOPhase Phase) {
886886
if (Level != OptimizationLevel::O0) {
887-
if (!isLTOPreLink(Phase))
888-
MPM.addPass(AMDGPUAttributorPass(*this));
887+
if (!isLTOPreLink(Phase)) {
888+
AMDGPUAttributorOptions Opts;
889+
MPM.addPass(AMDGPUAttributorPass(*this, Opts, Phase));
890+
}
889891
}
890892
});
891893

@@ -914,7 +916,8 @@ void AMDGPUTargetMachine::registerPassBuilderCallbacks(PassBuilder &PB) {
914916
AMDGPUAttributorOptions Opt;
915917
if (HasClosedWorldAssumption)
916918
Opt.IsClosedWorld = true;
917-
PM.addPass(AMDGPUAttributorPass(*this, Opt));
919+
PM.addPass(AMDGPUAttributorPass(
920+
*this, Opt, ThinOrFullLTOPhase::FullLTOPostLink));
918921
}
919922
}
920923
if (!NoKernelInfoEndLTO) {

llvm/test/LTO/AMDGPU/closed-world-assumption.ll

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
; RUN: opt -S -mtriple=amdgcn-amd-amdhsa -O3 -debug-only=amdgpu-attributor -o - %s 2>&1 | FileCheck %s --check-prefix=NO-CW
2-
; RUN: opt -S -mtriple=amdgcn-amd-amdhsa -passes="lto<O3>" -debug-only=amdgpu-attributor -o - %s 2>&1 | FileCheck %s --check-prefix=NO-CW
3-
; RUN: opt -S -mtriple=amdgcn-amd-amdhsa -passes="lto<O3>" -debug-only=amdgpu-attributor -amdgpu-link-time-closed-world=1 -o - %s 2>&1 | FileCheck %s --check-prefix=CW
1+
; RUN: opt -S -mtriple=amdgcn-amd-amdhsa -O3 -debug-only=amdgpu-attributor -o - %s 2>&1 | FileCheck %s --check-prefixes=NO-CW,NO-LTO
2+
; RUN: opt -S -mtriple=amdgcn-amd-amdhsa -passes="lto<O3>" -debug-only=amdgpu-attributor -o - %s 2>&1 | FileCheck %s --check-prefixes=NO-CW,LTO
3+
; RUN: opt -S -mtriple=amdgcn-amd-amdhsa -passes="lto<O3>" -debug-only=amdgpu-attributor -amdgpu-link-time-closed-world=1 -o - %s 2>&1 | FileCheck %s --check-prefixes=CW,LTO
44

55
; REQUIRES: amdgpu-registered-target
66
; REQUIRES: asserts
77

8+
; NO-LTO: Running at phase None
9+
; LTO: Running at phase FullLTOPostLink
810
; NO-CW: Module {{.*}} is not assumed to be a closed world.
911
; CW: Module {{.*}} is assumed to be a closed world.
1012
define hidden noundef i32 @_Z3foov() {

0 commit comments

Comments
 (0)