Skip to content

Commit 80b8e6b

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 0469bb9 commit 80b8e6b

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
@@ -1286,6 +1286,10 @@ static bool runImpl(Module &M, AnalysisGetter &AG, TargetMachine &TM,
12861286

12871287
Attributor A(Functions, InfoCache, AC);
12881288

1289+
LLVM_DEBUG(dbgs() << "[AMDGPUAttributor] Module " << M.getName() << " is "
1290+
<< (AC.IsClosedWorldModule ? "" : "not ")
1291+
<< "assumed to be a closed world.\n");
1292+
12891293
for (auto *F : Functions) {
12901294
A.getOrCreateAAFor<AAAMDAttributes>(IRPosition::function(*F));
12911295
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
@@ -454,6 +454,11 @@ static cl::opt<bool> NewRegBankSelect(
454454
"regbankselect"),
455455
cl::init(false), cl::Hidden);
456456

457+
static cl::opt<bool> HasClosedWorldAssumption(
458+
"amdgpu-link-time-closed-world",
459+
cl::desc("Whether has closed-world assumption at link time"),
460+
cl::init(false), cl::Hidden);
461+
457462
extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeAMDGPUTarget() {
458463
// Register the target
459464
RegisterTargetMachine<R600TargetMachine> X(getTheR600Target());
@@ -859,8 +864,12 @@ void AMDGPUTargetMachine::registerPassBuilderCallbacks(PassBuilder &PB) {
859864
PM.addPass(InternalizePass(mustPreserveGV));
860865
PM.addPass(GlobalDCEPass());
861866
}
862-
if (EnableAMDGPUAttributor)
863-
PM.addPass(AMDGPUAttributorPass(*this));
867+
if (EnableAMDGPUAttributor) {
868+
AMDGPUAttributorOptions Opt;
869+
if (HasClosedWorldAssumption)
870+
Opt.IsClosedWorld = true;
871+
PM.addPass(AMDGPUAttributorPass(*this, Opt));
872+
}
864873
}
865874
});
866875

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=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
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)