Skip to content

Commit 0ef2313

Browse files
committed
[AMDGPU] Re-enable closed-world assumption as an opt-out feature
Although the ABI (if any exists) doesn’t explicitly prohibit cross-device-image function calls, especially since our loader can handle them, for all officially supported programming models, this is not actually allowed. Given this, assuming a closed-world model at link time is safe. However, there are certain cases, such as the GPU libc project, that use non-standard approaches which could break this assumption. This PR introduces an option to disable this assumption when needed.
1 parent 09fb01a commit 0ef2313

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

llvm/lib/Target/AMDGPU/AMDGPUAttributor.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,6 +1068,10 @@ static bool runImpl(Module &M, AnalysisGetter &AG, TargetMachine &TM,
10681068

10691069
Attributor A(Functions, InfoCache, AC);
10701070

1071+
LLVM_DEBUG(dbgs() << "[AMDGPUAttributor] Module " << M.getName() << " is "
1072+
<< (AC.IsClosedWorldModule ? "" : "not ")
1073+
<< "assumed to be a closed world.\n");
1074+
10711075
for (auto *F : Functions) {
10721076
A.getOrCreateAAFor<AAAMDAttributes>(IRPosition::function(*F));
10731077
A.getOrCreateAAFor<AAUniformWorkGroupSize>(IRPosition::function(*F));

llvm/lib/Target/AMDGPU/AMDGPUTargetMachine.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,11 @@ static cl::opt<bool>
449449
cl::desc("Enable AMDGPUAttributorPass"),
450450
cl::init(true), cl::Hidden);
451451

452+
static cl::opt<bool> HasClosedWorldAssumption(
453+
"amdgpu-link-time-closed-world",
454+
cl::desc("Whether has closed-world assumption at link time"),
455+
cl::init(true), cl::Hidden);
456+
452457
extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeAMDGPUTarget() {
453458
// Register the target
454459
RegisterTargetMachine<R600TargetMachine> X(getTheR600Target());
@@ -836,8 +841,12 @@ void AMDGPUTargetMachine::registerPassBuilderCallbacks(PassBuilder &PB) {
836841
PM.addPass(InternalizePass(mustPreserveGV));
837842
PM.addPass(GlobalDCEPass());
838843
}
839-
if (EnableAMDGPUAttributor)
840-
PM.addPass(AMDGPUAttributorPass(*this));
844+
if (EnableAMDGPUAttributor) {
845+
AMDGPUAttributorOptions Opt;
846+
if (HasClosedWorldAssumption)
847+
Opt.IsClosedWorld = true;
848+
PM.addPass(AMDGPUAttributorPass(*this, Opt));
849+
}
841850
}
842851
});
843852

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +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=CW
3+
; RUN: opt -S -mtriple=amdgcn-amd-amdhsa -passes="lto<O3>" -debug-only=amdgpu-attributor -amdgpu-link-time-closed-world=0 -o - %s 2>&1 | FileCheck %s --check-prefix=NO-CW
4+
5+
; REQUIRES: amdgpu-registered-target
6+
; REQUIRES: asserts
7+
8+
; NO-CW: Module {{.*}} is not assumed to be a closed world.
9+
; CW: Module {{.*}} is assumed to be a closed world.
10+
define hidden noundef i32 @_Z3foov() {
11+
ret i32 1
12+
}

0 commit comments

Comments
 (0)