Skip to content

[AMDGPU][Attributor] Add ThinOrFullLTOPhase as an argument #123994

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions llvm/include/llvm/Pass.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,23 @@ enum class ThinOrFullLTOPhase {
FullLTOPostLink
};

#ifndef NDEBUG
constexpr const char *to_string(ThinOrFullLTOPhase Phase) {
switch (Phase) {
case ThinOrFullLTOPhase::None:
return "None";
case ThinOrFullLTOPhase::ThinLTOPreLink:
return "ThinLTOPreLink";
case ThinOrFullLTOPhase::ThinLTOPostLink:
return "ThinLTOPostLink";
case ThinOrFullLTOPhase::FullLTOPreLink:
return "FullLTOPreLink";
case ThinOrFullLTOPhase::FullLTOPostLink:
return "FullLTOPostLink";
}
}
#endif

//===----------------------------------------------------------------------===//
/// Pass interface - Implemented by all 'passes'. Subclass this if you are an
/// interprocedural optimization or you do not fit into any of the more
Expand Down
7 changes: 5 additions & 2 deletions llvm/lib/Target/AMDGPU/AMDGPU.h
Original file line number Diff line number Diff line change
Expand Up @@ -336,9 +336,12 @@ class AMDGPUAttributorPass : public PassInfoMixin<AMDGPUAttributorPass> {

AMDGPUAttributorOptions Options;

const ThinOrFullLTOPhase LTOPhase;

public:
AMDGPUAttributorPass(TargetMachine &TM, AMDGPUAttributorOptions Options = {})
: TM(TM), Options(Options) {};
AMDGPUAttributorPass(TargetMachine &TM, AMDGPUAttributorOptions Options,
ThinOrFullLTOPhase LTOPhase = ThinOrFullLTOPhase::None)
: TM(TM), Options(Options), LTOPhase(LTOPhase) {};
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
};

Expand Down
20 changes: 13 additions & 7 deletions llvm/lib/Target/AMDGPU/AMDGPUAttributor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1343,7 +1343,8 @@ static void addPreloadKernArgHint(Function &F, TargetMachine &TM) {
}

static bool runImpl(Module &M, AnalysisGetter &AG, TargetMachine &TM,
AMDGPUAttributorOptions Options) {
AMDGPUAttributorOptions Options,
ThinOrFullLTOPhase LTOPhase) {
SetVector<Function *> Functions;
for (Function &F : M) {
if (!F.isIntrinsic())
Expand Down Expand Up @@ -1378,9 +1379,13 @@ static bool runImpl(Module &M, AnalysisGetter &AG, TargetMachine &TM,

Attributor A(Functions, InfoCache, AC);

LLVM_DEBUG(dbgs() << "[AMDGPUAttributor] Module " << M.getName() << " is "
<< (AC.IsClosedWorldModule ? "" : "not ")
<< "assumed to be a closed world.\n");
LLVM_DEBUG({
StringRef LTOPhaseStr = to_string(LTOPhase);
dbgs() << "[AMDGPUAttributor] Running at phase " << LTOPhaseStr << '\n'
<< "[AMDGPUAttributor] Module " << M.getName() << " is "
<< (AC.IsClosedWorldModule ? "" : "not ")
<< "assumed to be a closed world.\n";
});

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

bool runOnModule(Module &M) override {
AnalysisGetter AG(this);
return runImpl(M, AG, *TM, /*Options=*/{});
return runImpl(M, AG, *TM, /*Options=*/{},
/*LTOPhase=*/ThinOrFullLTOPhase::None);
}

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

// TODO: Probably preserves CFG
return runImpl(M, AG, TM, Options) ? PreservedAnalyses::none()
: PreservedAnalyses::all();
return runImpl(M, AG, TM, Options, LTOPhase) ? PreservedAnalyses::none()
: PreservedAnalyses::all();
}

char AMDGPUAttributorLegacy::ID = 0;
Expand Down
9 changes: 6 additions & 3 deletions llvm/lib/Target/AMDGPU/AMDGPUTargetMachine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -884,8 +884,10 @@ void AMDGPUTargetMachine::registerPassBuilderCallbacks(PassBuilder &PB) {
OptimizationLevel Level,
ThinOrFullLTOPhase Phase) {
if (Level != OptimizationLevel::O0) {
if (!isLTOPreLink(Phase))
MPM.addPass(AMDGPUAttributorPass(*this));
if (!isLTOPreLink(Phase)) {
AMDGPUAttributorOptions Opts;
MPM.addPass(AMDGPUAttributorPass(*this, Opts, Phase));
}
}
});

Expand Down Expand Up @@ -914,7 +916,8 @@ void AMDGPUTargetMachine::registerPassBuilderCallbacks(PassBuilder &PB) {
AMDGPUAttributorOptions Opt;
if (HasClosedWorldAssumption)
Opt.IsClosedWorld = true;
PM.addPass(AMDGPUAttributorPass(*this, Opt));
PM.addPass(AMDGPUAttributorPass(
*this, Opt, ThinOrFullLTOPhase::FullLTOPostLink));
}
}
if (!NoKernelInfoEndLTO) {
Expand Down
8 changes: 5 additions & 3 deletions llvm/test/LTO/AMDGPU/closed-world-assumption.ll
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
; RUN: opt -S -mtriple=amdgcn-amd-amdhsa -O3 -debug-only=amdgpu-attributor -o - %s 2>&1 | FileCheck %s --check-prefix=NO-CW
; RUN: opt -S -mtriple=amdgcn-amd-amdhsa -passes="lto<O3>" -debug-only=amdgpu-attributor -o - %s 2>&1 | FileCheck %s --check-prefix=NO-CW
; 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
; RUN: opt -S -mtriple=amdgcn-amd-amdhsa -O3 -debug-only=amdgpu-attributor -o - %s 2>&1 | FileCheck %s --check-prefixes=NO-CW,NO-LTO
; 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
; 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

; REQUIRES: amdgpu-registered-target
; REQUIRES: asserts

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