Skip to content

Commit 04269ea

Browse files
authored
[AMDGPU] Re-enable closed-world assumption as an opt-in feature (#115371)
Although the ABI (if one exists) doesn’t explicitly prohibit cross-code-object function calls—particularly since our loader can handle them—such calls are not actually allowed in any of the officially supported programming models. However, this limitation has some nuances. For instance, the loader can handle cross-code-object global variables, which complicates the situation further. Given this complexity, assuming a closed-world model at link time isn’t always safe. To address this, this PR introduces an option that enables this assumption, providing end users the flexibility to enable it for improved compiler optimizations. However, it is the user’s responsibility to ensure they do not violate this assumption.
1 parent f3c675f commit 04269ea

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)