Skip to content

Commit 54f8572

Browse files
committed
[AMDGPU][Attributor] Add a pass parameter closed-world for AMDGPUAttributor pass
1 parent 602fcdf commit 54f8572

File tree

5 files changed

+51
-14
lines changed

5 files changed

+51
-14
lines changed

llvm/lib/Target/AMDGPU/AMDGPU.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -283,17 +283,22 @@ class AMDGPULowerKernelArgumentsPass
283283
PreservedAnalyses run(Function &, FunctionAnalysisManager &);
284284
};
285285

286+
struct AMDGPUAttributorOptions {
287+
bool IsClosedWorld = false;
288+
};
289+
286290
class AMDGPUAttributorPass : public PassInfoMixin<AMDGPUAttributorPass> {
287291
private:
288292
TargetMachine &TM;
289293

294+
AMDGPUAttributorOptions Options;
295+
290296
/// Asserts whether we can assume whole program visibility.
291297
bool HasWholeProgramVisibility = false;
292298

293299
public:
294-
AMDGPUAttributorPass(TargetMachine &TM,
295-
bool HasWholeProgramVisibility = false)
296-
: TM(TM), HasWholeProgramVisibility(HasWholeProgramVisibility) {};
300+
AMDGPUAttributorPass(TargetMachine &TM, AMDGPUAttributorOptions Options = {})
301+
: TM(TM), Options(Options) {};
297302
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
298303
};
299304

llvm/lib/Target/AMDGPU/AMDGPUAttributor.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,7 +1025,7 @@ static void addPreloadKernArgHint(Function &F, TargetMachine &TM) {
10251025
}
10261026

10271027
static bool runImpl(Module &M, AnalysisGetter &AG, TargetMachine &TM,
1028-
bool HasWholeProgramVisibility) {
1028+
AMDGPUAttributorOptions Options) {
10291029
SetVector<Function *> Functions;
10301030
for (Function &F : M) {
10311031
if (!F.isIntrinsic())
@@ -1043,7 +1043,7 @@ static bool runImpl(Module &M, AnalysisGetter &AG, TargetMachine &TM,
10431043
&AAUnderlyingObjects::ID, &AAIndirectCallInfo::ID, &AAInstanceInfo::ID});
10441044

10451045
AttributorConfig AC(CGUpdater);
1046-
AC.IsClosedWorldModule = HasWholeProgramVisibility;
1046+
AC.IsClosedWorldModule = Options.IsClosedWorld;
10471047
AC.Allowed = &Allowed;
10481048
AC.IsModulePass = true;
10491049
AC.DefaultInitializeLiveInternals = false;
@@ -1102,7 +1102,7 @@ class AMDGPUAttributorLegacy : public ModulePass {
11021102

11031103
bool runOnModule(Module &M) override {
11041104
AnalysisGetter AG(this);
1105-
return runImpl(M, AG, *TM, /*HasWholeProgramVisibility=*/false);
1105+
return runImpl(M, AG, *TM, /*Options=*/{});
11061106
}
11071107

11081108
void getAnalysisUsage(AnalysisUsage &AU) const override {
@@ -1123,9 +1123,8 @@ PreservedAnalyses llvm::AMDGPUAttributorPass::run(Module &M,
11231123
AnalysisGetter AG(FAM);
11241124

11251125
// TODO: Probably preserves CFG
1126-
return runImpl(M, AG, TM, HasWholeProgramVisibility)
1127-
? PreservedAnalyses::none()
1128-
: PreservedAnalyses::all();
1126+
return runImpl(M, AG, TM, Options) ? PreservedAnalyses::none()
1127+
: PreservedAnalyses::all();
11291128
}
11301129

11311130
char AMDGPUAttributorLegacy::ID = 0;

llvm/lib/Target/AMDGPU/AMDGPUPassRegistry.def

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
#define MODULE_PASS(NAME, CREATE_PASS)
1818
#endif
1919
MODULE_PASS("amdgpu-always-inline", AMDGPUAlwaysInlinePass())
20-
MODULE_PASS("amdgpu-attributor", AMDGPUAttributorPass(*this))
2120
MODULE_PASS("amdgpu-lower-buffer-fat-pointers",
2221
AMDGPULowerBufferFatPointersPass(*this))
2322
MODULE_PASS("amdgpu-lower-ctor-dtor", AMDGPUCtorDtorLoweringPass())
@@ -26,6 +25,17 @@ MODULE_PASS("amdgpu-printf-runtime-binding", AMDGPUPrintfRuntimeBindingPass())
2625
MODULE_PASS("amdgpu-unify-metadata", AMDGPUUnifyMetadataPass())
2726
#undef MODULE_PASS
2827

28+
#ifndef MODULE_PASS_WITH_PARAMS
29+
#define MODULE_PASS_WITH_PARAMS(NAME, CLASS, CREATE_PASS, PARSER, PARAMS)
30+
#endif
31+
MODULE_PASS_WITH_PARAMS(
32+
"amdgpu-attributor", "AMDGPUAttributorPass",
33+
[=](AMDGPUAttributorOptions Options) {
34+
return AMDGPUAttributorPass(*this, Options);
35+
},
36+
parseAMDGPUAttributorPassOptions, "closed-world")
37+
#undef MODULE_PASS_WITH_PARAMS
38+
2939
#ifndef FUNCTION_PASS
3040
#define FUNCTION_PASS(NAME, CREATE_PASS)
3141
#endif

llvm/lib/Target/AMDGPU/AMDGPUTargetMachine.cpp

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
#include "llvm/InitializePasses.h"
5555
#include "llvm/MC/TargetRegistry.h"
5656
#include "llvm/Passes/PassBuilder.h"
57+
#include "llvm/Support/FormatVariadic.h"
5758
#include "llvm/Transforms/HipStdPar/HipStdPar.h"
5859
#include "llvm/Transforms/IPO.h"
5960
#include "llvm/Transforms/IPO/AlwaysInliner.h"
@@ -661,6 +662,24 @@ Error AMDGPUTargetMachine::buildCodeGenPipeline(
661662
return CGPB.buildPipeline(MPM, Out, DwoOut, FileType);
662663
}
663664

665+
Expected<AMDGPUAttributorOptions>
666+
parseAMDGPUAttributorPassOptions(StringRef Params) {
667+
AMDGPUAttributorOptions Result;
668+
while (!Params.empty()) {
669+
StringRef ParamName;
670+
std::tie(ParamName, Params) = Params.split(';');
671+
if (ParamName == "closed-world") {
672+
Result.IsClosedWorld = true;
673+
} else {
674+
return make_error<StringError>(
675+
formatv("invalid AMDGPUAttributor pass parameter '{0}' ", ParamName)
676+
.str(),
677+
inconvertibleErrorCode());
678+
}
679+
}
680+
return Result;
681+
}
682+
664683
void AMDGPUTargetMachine::registerPassBuilderCallbacks(PassBuilder &PB) {
665684

666685
#define GET_PASS_REGISTRY "AMDGPUPassRegistry.def"
@@ -739,9 +758,13 @@ void AMDGPUTargetMachine::registerPassBuilderCallbacks(PassBuilder &PB) {
739758
OptimizationLevel Level,
740759
ThinOrFullLTOPhase Phase) {
741760
if (Level != OptimizationLevel::O0) {
742-
MPM.addPass(AMDGPUAttributorPass(
743-
*this, Phase == ThinOrFullLTOPhase::FullLTOPostLink ||
744-
Phase == ThinOrFullLTOPhase::ThinLTOPostLink));
761+
{
762+
AMDGPUAttributorOptions Options;
763+
Options.IsClosedWorld =
764+
(Phase == ThinOrFullLTOPhase::FullLTOPostLink) ||
765+
(Phase == ThinOrFullLTOPhase::ThinLTOPostLink);
766+
MPM.addPass(AMDGPUAttributorPass(*this, Options));
767+
}
745768
}
746769
});
747770

llvm/test/CodeGen/AMDGPU/simple-indirect-call-2.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --function-signature --check-globals
22
; RUN: opt -S -mtriple=amdgcn-amd-amdhsa -passes=amdgpu-attributor %s | FileCheck --check-prefixes=CHECK,OW %s
3-
; RUN: opt -S -mtriple=amdgcn-amd-amdhsa -passes=amdgpu-attributor -attributor-assume-closed-world=1 %s | FileCheck --check-prefixes=CHECK,CW %s
3+
; RUN: opt -S -mtriple=amdgcn-amd-amdhsa -passes='amdgpu-attributor<closed-world>' %s | FileCheck --check-prefixes=CHECK,CW %s
44

55
target datalayout = "A5"
66

0 commit comments

Comments
 (0)