Skip to content

Commit 11872e0

Browse files
committed
[AMDGPU][Attributor] Add a pass parameter closed-world for AMDGPUAttributor pass
1 parent 841327d commit 11872e0

File tree

6 files changed

+117
-6
lines changed

6 files changed

+117
-6
lines changed

llvm/lib/Target/AMDGPU/AMDGPU.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,12 +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+
296+
/// Asserts whether we can assume whole program visibility.
297+
bool HasWholeProgramVisibility = false;
298+
290299
public:
291-
AMDGPUAttributorPass(TargetMachine &TM) : TM(TM){};
300+
AMDGPUAttributorPass(TargetMachine &TM, AMDGPUAttributorOptions Options = {})
301+
: TM(TM), Options(Options) {};
292302
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
293303
};
294304

llvm/lib/Target/AMDGPU/AMDGPUAttributor.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1023,7 +1023,8 @@ static void addPreloadKernArgHint(Function &F, TargetMachine &TM) {
10231023
}
10241024
}
10251025

1026-
static bool runImpl(Module &M, AnalysisGetter &AG, TargetMachine &TM) {
1026+
static bool runImpl(Module &M, AnalysisGetter &AG, TargetMachine &TM,
1027+
AMDGPUAttributorOptions Options) {
10271028
SetVector<Function *> Functions;
10281029
for (Function &F : M) {
10291030
if (!F.isIntrinsic())
@@ -1041,6 +1042,7 @@ static bool runImpl(Module &M, AnalysisGetter &AG, TargetMachine &TM) {
10411042
&AAUnderlyingObjects::ID, &AAAddressSpace::ID});
10421043

10431044
AttributorConfig AC(CGUpdater);
1045+
AC.IsClosedWorldModule = Options.IsClosedWorld;
10441046
AC.Allowed = &Allowed;
10451047
AC.IsModulePass = true;
10461048
AC.DefaultInitializeLiveInternals = false;
@@ -1098,7 +1100,7 @@ class AMDGPUAttributorLegacy : public ModulePass {
10981100

10991101
bool runOnModule(Module &M) override {
11001102
AnalysisGetter AG(this);
1101-
return runImpl(M, AG, *TM);
1103+
return runImpl(M, AG, *TM, /*Options=*/{});
11021104
}
11031105

11041106
void getAnalysisUsage(AnalysisUsage &AU) const override {
@@ -1119,8 +1121,8 @@ PreservedAnalyses llvm::AMDGPUAttributorPass::run(Module &M,
11191121
AnalysisGetter AG(FAM);
11201122

11211123
// TODO: Probably preserves CFG
1122-
return runImpl(M, AG, TM) ? PreservedAnalyses::none()
1123-
: PreservedAnalyses::all();
1124+
return runImpl(M, AG, TM, Options) ? PreservedAnalyses::none()
1125+
: PreservedAnalyses::all();
11241126
}
11251127

11261128
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: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
#include "llvm/InitializePasses.h"
5656
#include "llvm/MC/TargetRegistry.h"
5757
#include "llvm/Passes/PassBuilder.h"
58+
#include "llvm/Support/FormatVariadic.h"
5859
#include "llvm/Transforms/HipStdPar/HipStdPar.h"
5960
#include "llvm/Transforms/IPO.h"
6061
#include "llvm/Transforms/IPO/AlwaysInliner.h"
@@ -662,6 +663,24 @@ Error AMDGPUTargetMachine::buildCodeGenPipeline(
662663
return CGPB.buildPipeline(MPM, Out, DwoOut, FileType);
663664
}
664665

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

667686
#define GET_PASS_REGISTRY "AMDGPUPassRegistry.def"
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --function-signature --check-globals
2+
; 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<closed-world>' %s | FileCheck --check-prefixes=CHECK,CW %s
4+
5+
target datalayout = "A5"
6+
7+
@G = global i32 0, align 4
8+
9+
;.
10+
; CHECK: @G = global i32 0, align 4
11+
;.
12+
define void @bar() {
13+
; CHECK-LABEL: define {{[^@]+}}@bar
14+
; CHECK-SAME: () #[[ATTR0:[0-9]+]] {
15+
; CHECK-NEXT: entry:
16+
; CHECK-NEXT: store i32 1, ptr @G, align 4
17+
; CHECK-NEXT: ret void
18+
;
19+
entry:
20+
store i32 1, ptr @G, align 4
21+
ret void
22+
}
23+
24+
define ptr @helper() {
25+
; CHECK-LABEL: define {{[^@]+}}@helper
26+
; CHECK-SAME: () #[[ATTR0]] {
27+
; CHECK-NEXT: entry:
28+
; CHECK-NEXT: ret ptr @bar
29+
;
30+
entry:
31+
ret ptr @bar
32+
}
33+
34+
define amdgpu_kernel void @foo(ptr noundef %fp) {
35+
; CHECK-LABEL: define {{[^@]+}}@foo
36+
; CHECK-SAME: (ptr noundef [[FP:%.*]]) #[[ATTR1:[0-9]+]] {
37+
; CHECK-NEXT: entry:
38+
; CHECK-NEXT: [[FP_ADDR:%.*]] = alloca ptr, align 8, addrspace(5)
39+
; CHECK-NEXT: store ptr [[FP]], ptr addrspace(5) [[FP_ADDR]], align 8
40+
; CHECK-NEXT: [[LOAD:%.*]] = load ptr, ptr addrspace(5) [[FP_ADDR]], align 8
41+
; CHECK-NEXT: call void [[LOAD]]()
42+
; CHECK-NEXT: ret void
43+
;
44+
entry:
45+
%fp.addr = alloca ptr, addrspace(5)
46+
store ptr %fp, ptr addrspace(5) %fp.addr
47+
%load = load ptr, ptr addrspace(5) %fp.addr
48+
call void %load()
49+
ret void
50+
}
51+
52+
;.
53+
; CHECK: attributes #[[ATTR0]] = { "amdgpu-no-agpr" "amdgpu-no-completion-action" "amdgpu-no-default-queue" "amdgpu-no-dispatch-id" "amdgpu-no-dispatch-ptr" "amdgpu-no-heap-ptr" "amdgpu-no-hostcall-ptr" "amdgpu-no-implicitarg-ptr" "amdgpu-no-lds-kernel-id" "amdgpu-no-multigrid-sync-arg" "amdgpu-no-queue-ptr" "amdgpu-no-workgroup-id-x" "amdgpu-no-workgroup-id-y" "amdgpu-no-workgroup-id-z" "amdgpu-no-workitem-id-x" "amdgpu-no-workitem-id-y" "amdgpu-no-workitem-id-z" "amdgpu-waves-per-eu"="4,10" "uniform-work-group-size"="false" }
54+
; CHECK: attributes #[[ATTR1]] = { "uniform-work-group-size"="false" }
55+
;.
56+
;; NOTE: These prefixes are unused and the list is autogenerated. Do not add tests below this line:
57+
; CW: {{.*}}
58+
; OW: {{.*}}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
; REQUIRES: amdgpu-registered-target
2+
3+
; RUN: not opt -S -mtriple=amdgcn-amd-amdhsa -passes='amdgpu-attributor<random>' -disable-output %s 2>&1 | FileCheck %s
4+
5+
; CHECK: amdgpu-attributor: invalid AMDGPUAttributor pass parameter 'random'
6+
7+
define void @f() {
8+
entry:
9+
br label %loop
10+
loop:
11+
br label %loop
12+
}

0 commit comments

Comments
 (0)