[llvm] [AMDGPU] Set preferred function alignment based on icache geometry (PR #183064)
via llvm-commits
llvm-commits at lists.llvm.org
Wed Mar 11 03:52:19 PDT 2026
https://github.com/michaelselehov updated https://github.com/llvm/llvm-project/pull/183064
>From a167977a5f33d1e67426593437f4229625a5103c Mon Sep 17 00:00:00 2001
From: Michael Selehov <michael.selehov at amd.com>
Date: Tue, 24 Feb 2026 07:45:55 -0600
Subject: [PATCH 1/3] [AMDGPU] Set preferred function alignment based on
instruction cache geometry
Non-entry functions were unconditionally aligned to 4 bytes with no
architecture-specific preferred alignment, and setAlignment() was used
instead of ensureAlignment(), overwriting any explicit IR attributes.
Add instruction cache line size and fetch alignment data to GCNSubtarget
for each generation (GFX9: 64B/32B, GFX10: 64B/4B, GFX11+: 128B/4B).
Use this to call setPrefFunctionAlignment() in SITargetLowering, aligning
non-entry functions to the cache line size by default. Change setAlignment
to ensureAlignment in AMDGPUAsmPrinter so explicit IR align attributes
are respected.
Empirical thread trace analysis on gfx942, gfx1030, gfx1100, and gfx1200
showed that only GFX9 exhibits measurable fetch stalls when functions cross
the 32-byte fetch window boundary. GFX10+ showed no alignment sensitivity.
A hidden option -amdgpu-align-functions-for-fetch-only is provided to use
the fetch granularity instead of cache line size.
Assisted-by: Claude Opus
---
llvm/lib/Target/AMDGPU/AMDGPUAsmPrinter.cpp | 2 +-
llvm/lib/Target/AMDGPU/GCNSubtarget.cpp | 34 ++++++
llvm/lib/Target/AMDGPU/GCNSubtarget.h | 11 ++
llvm/lib/Target/AMDGPU/SIISelLowering.cpp | 12 ++
.../test/CodeGen/AMDGPU/function-alignment.ll | 106 ++++++++++++++++++
llvm/test/CodeGen/AMDGPU/hsa-func-align.ll | 4 +-
llvm/test/CodeGen/AMDGPU/hsa-func.ll | 6 +-
llvm/test/CodeGen/AMDGPU/s_code_end.ll | 8 +-
8 files changed, 173 insertions(+), 10 deletions(-)
create mode 100644 llvm/test/CodeGen/AMDGPU/function-alignment.ll
diff --git a/llvm/lib/Target/AMDGPU/AMDGPUAsmPrinter.cpp b/llvm/lib/Target/AMDGPU/AMDGPUAsmPrinter.cpp
index 7d2df427ddd60..b8a02faa0247b 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPUAsmPrinter.cpp
+++ b/llvm/lib/Target/AMDGPU/AMDGPUAsmPrinter.cpp
@@ -686,7 +686,7 @@ bool AMDGPUAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
// The starting address of all shader programs must be 256 bytes aligned.
// Regular functions just need the basic required instruction alignment.
- MF.setAlignment(MFI->isEntryFunction() ? Align(256) : Align(4));
+ MF.ensureAlignment(MFI->isEntryFunction() ? Align(256) : Align(4));
SetupMachineFunction(MF);
diff --git a/llvm/lib/Target/AMDGPU/GCNSubtarget.cpp b/llvm/lib/Target/AMDGPU/GCNSubtarget.cpp
index 9b6592a7da4bc..ed071ec2ae8f3 100644
--- a/llvm/lib/Target/AMDGPU/GCNSubtarget.cpp
+++ b/llvm/lib/Target/AMDGPU/GCNSubtarget.cpp
@@ -145,6 +145,40 @@ GCNSubtarget &GCNSubtarget::initializeSubtargetDependencies(const Triple &TT,
HasFminFmaxLegacy = getGeneration() < AMDGPUSubtarget::VOLCANIC_ISLANDS;
HasSMulHi = getGeneration() >= AMDGPUSubtarget::GFX9;
+ // Different generations have different instruction cache line sizes and fetch
+ // granularities. Empirical testing (thread trace analysis on gfx942, gfx1030,
+ // gfx1100, gfx1200) showed that only GFX9 exhibits measurable fetch stalls
+ // when functions are not aligned to the 32-byte fetch window boundary.
+ // GFX10+ showed no alignment sensitivity in any test.
+ switch (Gen) {
+ case AMDGPUSubtarget::GFX9:
+ InstCacheLineSize = 64;
+ InstFetchAlignment = 32;
+ break;
+ case AMDGPUSubtarget::GFX10:
+ InstCacheLineSize = 64;
+ InstFetchAlignment = 4;
+ break;
+ case AMDGPUSubtarget::GFX11:
+ InstCacheLineSize = 128;
+ InstFetchAlignment = 4;
+ break;
+ case AMDGPUSubtarget::GFX12:
+ InstCacheLineSize = 128;
+ InstFetchAlignment = 4;
+ break;
+ default:
+ // Covers legacy pre-GFX9 and any future post-GFX12 architectures.
+ InstCacheLineSize = Gen >= AMDGPUSubtarget::GFX11 ? 128 : 64;
+ InstFetchAlignment = 4;
+ break;
+ }
+
+ assert(llvm::isPowerOf2_32(InstCacheLineSize) &&
+ "InstCacheLineSize must be a power of 2");
+ assert(llvm::isPowerOf2_32(InstFetchAlignment) &&
+ "InstFetchAlignment must be a power of 2");
+
TargetID.setTargetIDFromFeaturesString(FS);
LLVM_DEBUG(dbgs() << "xnack setting for subtarget: "
diff --git a/llvm/lib/Target/AMDGPU/GCNSubtarget.h b/llvm/lib/Target/AMDGPU/GCNSubtarget.h
index 5dac0c5dd9bd7..1acbd161d6f0c 100644
--- a/llvm/lib/Target/AMDGPU/GCNSubtarget.h
+++ b/llvm/lib/Target/AMDGPU/GCNSubtarget.h
@@ -66,6 +66,11 @@ class GCNSubtarget final : public AMDGPUGenSubtargetInfo,
int LDSBankCount = 0;
unsigned MaxPrivateElementSize = 0;
+ // Instruction cache parameters, set per-generation in
+ // initializeSubtargetDependencies.
+ unsigned InstCacheLineSize = 64;
+ unsigned InstFetchAlignment = 4;
+
// Dynamically set bits that enable features.
bool DynamicVGPR = false;
bool DynamicVGPRBlockSize32 = false;
@@ -170,6 +175,12 @@ class GCNSubtarget final : public AMDGPUGenSubtargetInfo,
int getLDSBankCount() const { return LDSBankCount; }
+ /// Instruction cache line size in bytes (64 for pre-GFX11, 128 for GFX11+).
+ unsigned getInstCacheLineSize() const { return InstCacheLineSize; }
+
+ /// Minimum function alignment needed to avoid instruction fetch stalls.
+ unsigned getInstFetchAlignment() const { return InstFetchAlignment; }
+
unsigned getMaxPrivateElementSize(bool ForBufferRSrc = false) const {
return (ForBufferRSrc || !hasFlatScratchEnabled()) ? MaxPrivateElementSize
: 16;
diff --git a/llvm/lib/Target/AMDGPU/SIISelLowering.cpp b/llvm/lib/Target/AMDGPU/SIISelLowering.cpp
index 2f993f4232766..1d173e5529a5b 100644
--- a/llvm/lib/Target/AMDGPU/SIISelLowering.cpp
+++ b/llvm/lib/Target/AMDGPU/SIISelLowering.cpp
@@ -61,6 +61,13 @@ static cl::opt<bool>
cl::desc("Do not align and prefetch loops"),
cl::init(false));
+static cl::opt<bool> AlignFunctionsForFetchOnly(
+ "amdgpu-align-functions-for-fetch-only", cl::Hidden,
+ cl::desc("Align non-entry functions to instruction fetch granularity "
+ "instead of cache line size (less padding, sufficient for "
+ "measured architectures)"),
+ cl::init(false));
+
static cl::opt<bool> UseDivergentRegisterIndexing(
"amdgpu-use-divergent-register-indexing", cl::Hidden,
cl::desc("Use indirect register addressing for divergent indexes"),
@@ -196,6 +203,11 @@ SITargetLowering::SITargetLowering(const TargetMachine &TM,
computeRegisterProperties(Subtarget->getRegisterInfo());
+ setMinFunctionAlignment(Align(4));
+ unsigned PrefAlign = AlignFunctionsForFetchOnly ? STI.getInstFetchAlignment()
+ : STI.getInstCacheLineSize();
+ setPrefFunctionAlignment(Align(PrefAlign));
+
// The boolean content concept here is too inflexible. Compares only ever
// really produce a 1-bit result. Any copy/extend from these will turn into a
// select, and zext/1 or sext/-1 are equally cheap. Arbitrarily choose 0/1, as
diff --git a/llvm/test/CodeGen/AMDGPU/function-alignment.ll b/llvm/test/CodeGen/AMDGPU/function-alignment.ll
new file mode 100644
index 0000000000000..8bb217e1f3006
--- /dev/null
+++ b/llvm/test/CodeGen/AMDGPU/function-alignment.ll
@@ -0,0 +1,106 @@
+; Test preferred alignment of non-entry functions on different AMDGPU
+; architectures. Cache-line alignment (default) and fetch-only alignment
+; (via -amdgpu-align-functions-for-fetch-only) are both tested.
+;
+; GFX9: cache line = 64B (.p2align 6), fetch = 32B (.p2align 5)
+; GFX10: cache line = 64B (.p2align 6), fetch = 4B (.p2align 2)
+; GFX11: cache line = 128B (.p2align 7), fetch = 4B (.p2align 2)
+; GFX12: cache line = 128B (.p2align 7), fetch = 4B (.p2align 2)
+
+; --- Default (cache line alignment) ---
+
+; RUN: llc -mtriple=amdgcn -mcpu=gfx900 < %s | FileCheck -check-prefix=GFX9-CACHE %s
+; RUN: llc -mtriple=amdgcn -mcpu=gfx1030 < %s | FileCheck -check-prefix=GFX10-CACHE %s
+; RUN: llc -mtriple=amdgcn -mcpu=gfx1100 < %s | FileCheck -check-prefix=GFX11-CACHE %s
+; RUN: llc -mtriple=amdgcn -mcpu=gfx1200 < %s | FileCheck -check-prefix=GFX12-CACHE %s
+
+; --- Fetch-only alignment ---
+
+; RUN: llc -mtriple=amdgcn -mcpu=gfx900 -amdgpu-align-functions-for-fetch-only < %s | FileCheck -check-prefix=GFX9-FETCH %s
+; RUN: llc -mtriple=amdgcn -mcpu=gfx1030 -amdgpu-align-functions-for-fetch-only < %s | FileCheck -check-prefix=GFX10-FETCH %s
+; RUN: llc -mtriple=amdgcn -mcpu=gfx1100 -amdgpu-align-functions-for-fetch-only < %s | FileCheck -check-prefix=GFX11-FETCH %s
+; RUN: llc -mtriple=amdgcn -mcpu=gfx1200 -amdgpu-align-functions-for-fetch-only < %s | FileCheck -check-prefix=GFX12-FETCH %s
+
+; --- Optsize: alignment drops to minimum (Align(4) = .p2align 2) ---
+
+; RUN: llc -mtriple=amdgcn -mcpu=gfx900 < %s | FileCheck -check-prefix=OPTSIZE %s
+
+; --- IR align attribute: ensureAlignment must not lower explicit alignment ---
+
+; RUN: llc -mtriple=amdgcn -mcpu=gfx900 < %s | FileCheck -check-prefix=EXPLICIT-ALIGN %s
+
+; --- -align-all-functions=1 with optsize: verify floor at Align(4) ---
+
+; RUN: llc -mtriple=amdgcn -mcpu=gfx900 -align-all-functions=1 < %s | FileCheck -check-prefix=ALIGN-ALL %s
+
+; --- Entry function: 256B alignment unchanged ---
+
+; RUN: llc -mtriple=amdgcn -mcpu=gfx900 < %s | FileCheck -check-prefix=ENTRY %s
+
+
+; Non-entry function: alignment depends on architecture and option.
+define void @non_entry_func() {
+; GFX9-CACHE: .p2align 6{{$}}
+; GFX9-CACHE: non_entry_func:
+
+; GFX10-CACHE: .p2align 6{{$}}
+; GFX10-CACHE: non_entry_func:
+
+; GFX11-CACHE: .p2align 7{{$}}
+; GFX11-CACHE: non_entry_func:
+
+; GFX12-CACHE: .p2align 7{{$}}
+; GFX12-CACHE: non_entry_func:
+
+; GFX9-FETCH: .p2align 5{{$}}
+; GFX9-FETCH: non_entry_func:
+
+; GFX10-FETCH: .p2align 2{{$}}
+; GFX10-FETCH: non_entry_func:
+
+; GFX11-FETCH: .p2align 2{{$}}
+; GFX11-FETCH: non_entry_func:
+
+; GFX12-FETCH: .p2align 2{{$}}
+; GFX12-FETCH: non_entry_func:
+ ret void
+}
+
+; Non-entry function with optsize: must still be at least Align(4).
+define void @optsize_func() optsize {
+; OPTSIZE: .globl optsize_func
+; OPTSIZE-NEXT: .p2align 2{{$}}
+ ret void
+}
+
+; Non-entry function with explicit IR align 128: ensureAlignment must not lower
+; it. On GFX9 default is 64 (cache line), so 128 from IR must be preserved.
+define void @explicit_align_func() align 128 {
+; EXPLICIT-ALIGN: .globl explicit_align_func
+; EXPLICIT-ALIGN-NEXT: .p2align 7{{$}}
+ ret void
+}
+
+; Non-entry function with explicit IR align 32 on GFX9: lower than preferred
+; (64), so preferred alignment wins. Result: .p2align 6.
+define void @low_align_func() align 32 {
+; GFX9-CACHE: .globl low_align_func
+; GFX9-CACHE-NEXT: .p2align 6{{$}}
+ ret void
+}
+
+; Optsize + -align-all-functions=1: MachineFunction::init sets Align(2), but
+; ensureAlignment(4) in AsmPrinter restores the floor. With optsize,
+; getPreferredAlignment returns max(Align(1), Align(4)) = Align(4).
+define void @align_all_optsize_func() optsize {
+; ALIGN-ALL: .globl align_all_optsize_func
+; ALIGN-ALL-NEXT: .p2align 2{{$}}
+ ret void
+}
+
+; Entry function: must be 256B aligned regardless of our changes.
+define amdgpu_kernel void @entry_func() {
+; ENTRY: .globl entry_func
+; ENTRY-NEXT: .p2align 8{{$}}
+ ret void
+}
diff --git a/llvm/test/CodeGen/AMDGPU/hsa-func-align.ll b/llvm/test/CodeGen/AMDGPU/hsa-func-align.ll
index 0ac6ca2904074..e10c6934765e1 100644
--- a/llvm/test/CodeGen/AMDGPU/hsa-func-align.ll
+++ b/llvm/test/CodeGen/AMDGPU/hsa-func-align.ll
@@ -5,11 +5,11 @@
; ELF: Name: .text
; ELF: SHF_ALLOC (0x2)
; ELF: SHF_EXECINSTR (0x4)
-; ELF: AddressAlignment: 32
+; ELF: AddressAlignment: 64
; ELF: }
; HSA: .globl simple_align16
-; HSA: .p2align 5
+; HSA: .p2align 6
define void @simple_align16(ptr addrspace(4) %ptr.out) align 32 {
entry:
%out = load ptr addrspace(1), ptr addrspace(4) %ptr.out
diff --git a/llvm/test/CodeGen/AMDGPU/hsa-func.ll b/llvm/test/CodeGen/AMDGPU/hsa-func.ll
index 4ef3c994d0622..b8bdacf450fb3 100644
--- a/llvm/test/CodeGen/AMDGPU/hsa-func.ll
+++ b/llvm/test/CodeGen/AMDGPU/hsa-func.ll
@@ -14,7 +14,7 @@
; ELF: Flags [ (0x6)
; ELF: SHF_ALLOC (0x2)
; ELF: SHF_EXECINSTR (0x4)
-; ELF: AddressAlignment: 4
+; ELF: AddressAlignment: 64
; ELF: }
; ELF: SHT_NOTE
@@ -38,7 +38,7 @@
; HSA-NOT: .amdgpu_hsa_kernel simple
; HSA: .globl simple
-; HSA: .p2align 2
+; HSA: .p2align 6
; HSA: {{^}}simple:
; HSA-NOT: amd_kernel_code_t
; HSA: flat_load_dwordx2 v{{\[[0-9]+:[0-9]+\]}}, v[0:1]
@@ -60,7 +60,7 @@ entry:
; Ignore explicit alignment that is too low.
; HSA: .globl simple_align2
-; HSA: .p2align 2
+; HSA: .p2align 6
define void @simple_align2(ptr addrspace(4) %ptr.out) align 2 {
entry:
%out = load ptr addrspace(1), ptr addrspace(4) %ptr.out
diff --git a/llvm/test/CodeGen/AMDGPU/s_code_end.ll b/llvm/test/CodeGen/AMDGPU/s_code_end.ll
index ad7d8a14babc1..2579807ac9862 100644
--- a/llvm/test/CodeGen/AMDGPU/s_code_end.ll
+++ b/llvm/test/CodeGen/AMDGPU/s_code_end.ll
@@ -33,7 +33,7 @@ define amdgpu_kernel void @a_kernel2() #0 {
}
; GCN-ASM: .globl a_function
-; GCN-ASM-NEXT: .p2align 2
+; GCN-ASM-NEXT: .p2align {{[67]}}
; GCN-ASM-NEXT: .type a_function, at function
; GCN-NEXT: a_function{{>?}}:
@@ -55,11 +55,11 @@ define amdgpu_kernel void @a_kernel2() #0 {
; GFX11END-OBJ-NEXT: s_code_end
; GFX90AEND-OBJ-NEXT: s_nop 0
-; GFX10END-OBJ: s_code_end // 000000000140:
+; GFX10END-OBJ: s_code_end // {{[0-9A-F]+}}:
; GFX10END-OBJ-COUNT-47: s_code_end
-; GFX11END-OBJ: s_code_end // 000000000140:
+; GFX11END-OBJ: s_code_end // {{[0-9A-F]+}}:
; GFX11END-OBJ-COUNT-47: s_code_end
-; GFX90AEND-OBJ: s_nop 0 // 000000000140:
+; GFX90AEND-OBJ: s_nop 0 // {{[0-9A-F]+}}:
; GFX90AEND-OBJ-COUNT-255: s_nop 0
define void @a_function() #0 {
>From 898251ac6733a3dc251d77bf8b0b63c073c769b3 Mon Sep 17 00:00:00 2001
From: Michael Selehov <michael.selehov at amd.com>
Date: Fri, 27 Feb 2026 03:25:39 -0600
Subject: [PATCH 2/3] [AMDGPU] Add prefalign test coverage to
function-alignment.ll
Test that the prefalign IR attribute overrides the target's preferred
function alignment. prefalign(16) on GFX9 gives .p2align 4 (not the
target's cache-line 64), and prefalign(256) gives .p2align 8.
Made-with: Cursor
---
.../test/CodeGen/AMDGPU/function-alignment.ll | 21 +++++++++++++++++++
1 file changed, 21 insertions(+)
diff --git a/llvm/test/CodeGen/AMDGPU/function-alignment.ll b/llvm/test/CodeGen/AMDGPU/function-alignment.ll
index 8bb217e1f3006..10e8fcfdc2712 100644
--- a/llvm/test/CodeGen/AMDGPU/function-alignment.ll
+++ b/llvm/test/CodeGen/AMDGPU/function-alignment.ll
@@ -33,6 +33,10 @@
; RUN: llc -mtriple=amdgcn -mcpu=gfx900 -align-all-functions=1 < %s | FileCheck -check-prefix=ALIGN-ALL %s
+; --- prefalign attribute: overrides target preferred alignment ---
+
+; RUN: llc -mtriple=amdgcn -mcpu=gfx900 < %s | FileCheck -check-prefix=PREFALIGN %s
+
; --- Entry function: 256B alignment unchanged ---
; RUN: llc -mtriple=amdgcn -mcpu=gfx900 < %s | FileCheck -check-prefix=ENTRY %s
@@ -98,6 +102,23 @@ define void @align_all_optsize_func() optsize {
ret void
}
+; prefalign(16) on GFX9: overrides target preferred (64) with 16.
+; getPreferredAlignment uses prefalign directly instead of getPrefFunctionAlignment.
+; Result: max(16, 4) = 16 → .p2align 4.
+define void @prefalign_low_func() prefalign(16) {
+; PREFALIGN: .globl prefalign_low_func
+; PREFALIGN-NEXT: .p2align 4{{$}}
+ ret void
+}
+
+; prefalign(256) on GFX9: higher than target preferred (64).
+; Result: max(256, 4) = 256 → .p2align 8.
+define void @prefalign_high_func() prefalign(256) {
+; PREFALIGN: .globl prefalign_high_func
+; PREFALIGN-NEXT: .p2align 8{{$}}
+ ret void
+}
+
; Entry function: must be 256B aligned regardless of our changes.
define amdgpu_kernel void @entry_func() {
; ENTRY: .globl entry_func
>From 41ccd6f6c53daab3d321776f7118256366dbc2df Mon Sep 17 00:00:00 2001
From: Michael Selehov <michael.selehov at amd.com>
Date: Fri, 27 Feb 2026 03:38:28 -0600
Subject: [PATCH 3/3] [AMDGPU] Move InstCacheLineSize to TableGen subtarget
feature
Address review feedback: define instruction cache line size as a
TableGen subtarget feature (FeatureInstCacheLineSize64/128) instead of
a C++ switch/case. Remove the hidden -amdgpu-align-functions-for-fetch-only
cl::opt and InstFetchAlignment -- preferred function alignment now
always uses the instruction cache line size directly.
Made-with: Cursor
---
llvm/lib/Target/AMDGPU/AMDGPU.td | 32 ++++++--
llvm/lib/Target/AMDGPU/GCNSubtarget.cpp | 33 +-------
llvm/lib/Target/AMDGPU/GCNSubtarget.h | 9 +--
llvm/lib/Target/AMDGPU/SIISelLowering.cpp | 11 +--
.../test/CodeGen/AMDGPU/function-alignment.ll | 81 ++++++++-----------
5 files changed, 67 insertions(+), 99 deletions(-)
diff --git a/llvm/lib/Target/AMDGPU/AMDGPU.td b/llvm/lib/Target/AMDGPU/AMDGPU.td
index 07fb32173c2a3..86402ce518725 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPU.td
+++ b/llvm/lib/Target/AMDGPU/AMDGPU.td
@@ -377,6 +377,16 @@ class SubtargetFeatureLDSBankCount <int Value> : SubtargetFeature <
def FeatureLDSBankCount16 : SubtargetFeatureLDSBankCount<16>;
def FeatureLDSBankCount32 : SubtargetFeatureLDSBankCount<32>;
+class SubtargetFeatureInstCacheLineSize <int Value> : SubtargetFeature <
+ "instcachelinesize"#Value,
+ "InstCacheLineSize",
+ !cast<string>(Value),
+ "Instruction cache line size in bytes."
+>;
+
+def FeatureInstCacheLineSize64 : SubtargetFeatureInstCacheLineSize<64>;
+def FeatureInstCacheLineSize128 : SubtargetFeatureInstCacheLineSize<128>;
+
defm GCN3Encoding : AMDGPUSubtargetFeature<"gcn3-encoding",
"Encoding format for VI",
/*GenPredicate=*/0
@@ -1325,6 +1335,7 @@ def FeatureSouthernIslands : GCNSubtargetFeatureGeneration<"SOUTHERN_ISLANDS",
FeatureWavefrontSize64, FeatureSMemTimeInst, FeatureMadMacF32Insts,
FeatureDsSrc2Insts, FeatureLDSBankCount32, FeatureMovrel,
FeatureTrigReducedRange, FeatureExtendedImageInsts, FeatureImageInsts,
+ FeatureInstCacheLineSize64,
FeatureGDS, FeatureGWS, FeatureDefaultComponentZero,
FeatureAtomicFMinFMaxF32GlobalInsts, FeatureAtomicFMinFMaxF64GlobalInsts,
FeatureVmemWriteVgprInOrder, FeatureCubeInsts, FeatureLerpInst,
@@ -1343,7 +1354,8 @@ def FeatureSeaIslands : GCNSubtargetFeatureGeneration<"SEA_ISLANDS",
FeatureAtomicFMinFMaxF32GlobalInsts, FeatureAtomicFMinFMaxF64GlobalInsts,
FeatureAtomicFMinFMaxF32FlatInsts, FeatureAtomicFMinFMaxF64FlatInsts,
FeatureVmemWriteVgprInOrder, FeatureCubeInsts, FeatureLerpInst,
- FeatureSadInsts, FeatureQsadInsts, FeatureCvtPkNormVOP2Insts
+ FeatureSadInsts, FeatureQsadInsts, FeatureCvtPkNormVOP2Insts,
+ FeatureInstCacheLineSize64
]
>;
@@ -1362,7 +1374,8 @@ def FeatureVolcanicIslands : GCNSubtargetFeatureGeneration<"VOLCANIC_ISLANDS",
FeatureUnalignedBufferAccess, FeatureImageInsts, FeatureGDS, FeatureGWS,
FeatureDefaultComponentZero, FeatureVmemWriteVgprInOrder, FeatureCubeInsts,
FeatureLerpInst, FeatureSadInsts, FeatureQsadInsts,
- FeatureCvtPkNormVOP2Insts
+ FeatureCvtPkNormVOP2Insts,
+ FeatureInstCacheLineSize64
]
>;
@@ -1384,7 +1397,8 @@ def FeatureGFX9 : GCNSubtargetFeatureGeneration<"GFX9",
FeatureDefaultComponentZero,FeatureVmemWriteVgprInOrder, FeatureVMemToLDSLoad,
FeatureCubeInsts, FeatureLerpInst, FeatureSadInsts, FeatureQsadInsts,
FeatureCvtNormInsts, FeatureCvtPkNormVOP2Insts,
- FeatureCvtPkNormVOP3Insts
+ FeatureCvtPkNormVOP3Insts,
+ FeatureInstCacheLineSize64
]
>;
@@ -1411,7 +1425,8 @@ def FeatureGFX10 : GCNSubtargetFeatureGeneration<"GFX10",
FeatureVmemWriteVgprInOrder, FeatureVMemToLDSLoad, FeatureCubeInsts,
FeatureLerpInst, FeatureSadInsts, FeatureQsadInsts,
FeatureCvtNormInsts, FeatureCvtPkNormVOP2Insts,
- FeatureCvtPkNormVOP3Insts
+ FeatureCvtPkNormVOP3Insts,
+ FeatureInstCacheLineSize64
]
>;
@@ -1436,7 +1451,8 @@ def FeatureGFX11 : GCNSubtargetFeatureGeneration<"GFX11",
FeatureAtomicFMinFMaxF32GlobalInsts, FeatureAtomicFMinFMaxF32FlatInsts,
FeatureVmemWriteVgprInOrder, FeatureCubeInsts, FeatureLerpInst,
FeatureSadInsts, FeatureQsadInsts, FeatureCvtNormInsts,
- FeatureCvtPkNormVOP2Insts, FeatureCvtPkNormVOP3Insts
+ FeatureCvtPkNormVOP2Insts, FeatureCvtPkNormVOP3Insts,
+ FeatureInstCacheLineSize128
]
>;
@@ -1460,7 +1476,8 @@ def FeatureGFX12 : GCNSubtargetFeatureGeneration<"GFX12",
FeatureDefaultComponentBroadcast, FeatureMaxHardClauseLength32,
FeatureAtomicFMinFMaxF32GlobalInsts, FeatureAtomicFMinFMaxF32FlatInsts,
FeatureIEEEMinimumMaximumInsts, FeatureMinimum3Maximum3F32,
- FeatureMinimum3Maximum3F16, FeatureAgentScopeFineGrainedRemoteMemoryAtomics
+ FeatureMinimum3Maximum3F16, FeatureAgentScopeFineGrainedRemoteMemoryAtomics,
+ FeatureInstCacheLineSize128
]
>;
@@ -1484,7 +1501,8 @@ def FeatureGFX13 : GCNSubtargetFeatureGeneration<"GFX13",
FeatureDefaultComponentBroadcast, FeatureMaxHardClauseLength32,
FeatureAtomicFMinFMaxF32GlobalInsts, FeatureAtomicFMinFMaxF32FlatInsts,
FeatureIEEEMinimumMaximumInsts, FeatureMinimum3Maximum3F32,
- FeatureMinimum3Maximum3F16, FeatureAgentScopeFineGrainedRemoteMemoryAtomics
+ FeatureMinimum3Maximum3F16, FeatureAgentScopeFineGrainedRemoteMemoryAtomics,
+ FeatureInstCacheLineSize128
]
>;
//===----------------------------------------------------------------------===//
diff --git a/llvm/lib/Target/AMDGPU/GCNSubtarget.cpp b/llvm/lib/Target/AMDGPU/GCNSubtarget.cpp
index ed071ec2ae8f3..96f3bd536b11c 100644
--- a/llvm/lib/Target/AMDGPU/GCNSubtarget.cpp
+++ b/llvm/lib/Target/AMDGPU/GCNSubtarget.cpp
@@ -145,39 +145,14 @@ GCNSubtarget &GCNSubtarget::initializeSubtargetDependencies(const Triple &TT,
HasFminFmaxLegacy = getGeneration() < AMDGPUSubtarget::VOLCANIC_ISLANDS;
HasSMulHi = getGeneration() >= AMDGPUSubtarget::GFX9;
- // Different generations have different instruction cache line sizes and fetch
- // granularities. Empirical testing (thread trace analysis on gfx942, gfx1030,
- // gfx1100, gfx1200) showed that only GFX9 exhibits measurable fetch stalls
- // when functions are not aligned to the 32-byte fetch window boundary.
- // GFX10+ showed no alignment sensitivity in any test.
- switch (Gen) {
- case AMDGPUSubtarget::GFX9:
+ // InstCacheLineSize is set from TableGen subtarget features
+ // (FeatureInstCacheLineSize64 / FeatureInstCacheLineSize128).
+ // Fall back to 64 if no feature was specified (e.g. generic targets).
+ if (InstCacheLineSize == 0)
InstCacheLineSize = 64;
- InstFetchAlignment = 32;
- break;
- case AMDGPUSubtarget::GFX10:
- InstCacheLineSize = 64;
- InstFetchAlignment = 4;
- break;
- case AMDGPUSubtarget::GFX11:
- InstCacheLineSize = 128;
- InstFetchAlignment = 4;
- break;
- case AMDGPUSubtarget::GFX12:
- InstCacheLineSize = 128;
- InstFetchAlignment = 4;
- break;
- default:
- // Covers legacy pre-GFX9 and any future post-GFX12 architectures.
- InstCacheLineSize = Gen >= AMDGPUSubtarget::GFX11 ? 128 : 64;
- InstFetchAlignment = 4;
- break;
- }
assert(llvm::isPowerOf2_32(InstCacheLineSize) &&
"InstCacheLineSize must be a power of 2");
- assert(llvm::isPowerOf2_32(InstFetchAlignment) &&
- "InstFetchAlignment must be a power of 2");
TargetID.setTargetIDFromFeaturesString(FS);
diff --git a/llvm/lib/Target/AMDGPU/GCNSubtarget.h b/llvm/lib/Target/AMDGPU/GCNSubtarget.h
index 1acbd161d6f0c..7b85fcadc650d 100644
--- a/llvm/lib/Target/AMDGPU/GCNSubtarget.h
+++ b/llvm/lib/Target/AMDGPU/GCNSubtarget.h
@@ -66,10 +66,8 @@ class GCNSubtarget final : public AMDGPUGenSubtargetInfo,
int LDSBankCount = 0;
unsigned MaxPrivateElementSize = 0;
- // Instruction cache parameters, set per-generation in
- // initializeSubtargetDependencies.
- unsigned InstCacheLineSize = 64;
- unsigned InstFetchAlignment = 4;
+ // Instruction cache line size in bytes; set from TableGen subtarget features.
+ unsigned InstCacheLineSize = 0;
// Dynamically set bits that enable features.
bool DynamicVGPR = false;
@@ -178,9 +176,6 @@ class GCNSubtarget final : public AMDGPUGenSubtargetInfo,
/// Instruction cache line size in bytes (64 for pre-GFX11, 128 for GFX11+).
unsigned getInstCacheLineSize() const { return InstCacheLineSize; }
- /// Minimum function alignment needed to avoid instruction fetch stalls.
- unsigned getInstFetchAlignment() const { return InstFetchAlignment; }
-
unsigned getMaxPrivateElementSize(bool ForBufferRSrc = false) const {
return (ForBufferRSrc || !hasFlatScratchEnabled()) ? MaxPrivateElementSize
: 16;
diff --git a/llvm/lib/Target/AMDGPU/SIISelLowering.cpp b/llvm/lib/Target/AMDGPU/SIISelLowering.cpp
index 1d173e5529a5b..d1cef0e31cdbb 100644
--- a/llvm/lib/Target/AMDGPU/SIISelLowering.cpp
+++ b/llvm/lib/Target/AMDGPU/SIISelLowering.cpp
@@ -61,13 +61,6 @@ static cl::opt<bool>
cl::desc("Do not align and prefetch loops"),
cl::init(false));
-static cl::opt<bool> AlignFunctionsForFetchOnly(
- "amdgpu-align-functions-for-fetch-only", cl::Hidden,
- cl::desc("Align non-entry functions to instruction fetch granularity "
- "instead of cache line size (less padding, sufficient for "
- "measured architectures)"),
- cl::init(false));
-
static cl::opt<bool> UseDivergentRegisterIndexing(
"amdgpu-use-divergent-register-indexing", cl::Hidden,
cl::desc("Use indirect register addressing for divergent indexes"),
@@ -204,9 +197,7 @@ SITargetLowering::SITargetLowering(const TargetMachine &TM,
computeRegisterProperties(Subtarget->getRegisterInfo());
setMinFunctionAlignment(Align(4));
- unsigned PrefAlign = AlignFunctionsForFetchOnly ? STI.getInstFetchAlignment()
- : STI.getInstCacheLineSize();
- setPrefFunctionAlignment(Align(PrefAlign));
+ setPrefFunctionAlignment(Align(STI.getInstCacheLineSize()));
// The boolean content concept here is too inflexible. Compares only ever
// really produce a 1-bit result. Any copy/extend from these will turn into a
diff --git a/llvm/test/CodeGen/AMDGPU/function-alignment.ll b/llvm/test/CodeGen/AMDGPU/function-alignment.ll
index 10e8fcfdc2712..078d662a38b58 100644
--- a/llvm/test/CodeGen/AMDGPU/function-alignment.ll
+++ b/llvm/test/CodeGen/AMDGPU/function-alignment.ll
@@ -1,25 +1,17 @@
; Test preferred alignment of non-entry functions on different AMDGPU
-; architectures. Cache-line alignment (default) and fetch-only alignment
-; (via -amdgpu-align-functions-for-fetch-only) are both tested.
+; architectures. Preferred alignment matches the instruction cache line size:
;
-; GFX9: cache line = 64B (.p2align 6), fetch = 32B (.p2align 5)
-; GFX10: cache line = 64B (.p2align 6), fetch = 4B (.p2align 2)
-; GFX11: cache line = 128B (.p2align 7), fetch = 4B (.p2align 2)
-; GFX12: cache line = 128B (.p2align 7), fetch = 4B (.p2align 2)
+; GFX9 - cache line = 64B (.p2align 6)
+; GFX10 - cache line = 64B (.p2align 6)
+; GFX11 - cache line = 128B (.p2align 7)
+; GFX12 - cache line = 128B (.p2align 7)
; --- Default (cache line alignment) ---
-; RUN: llc -mtriple=amdgcn -mcpu=gfx900 < %s | FileCheck -check-prefix=GFX9-CACHE %s
-; RUN: llc -mtriple=amdgcn -mcpu=gfx1030 < %s | FileCheck -check-prefix=GFX10-CACHE %s
-; RUN: llc -mtriple=amdgcn -mcpu=gfx1100 < %s | FileCheck -check-prefix=GFX11-CACHE %s
-; RUN: llc -mtriple=amdgcn -mcpu=gfx1200 < %s | FileCheck -check-prefix=GFX12-CACHE %s
-
-; --- Fetch-only alignment ---
-
-; RUN: llc -mtriple=amdgcn -mcpu=gfx900 -amdgpu-align-functions-for-fetch-only < %s | FileCheck -check-prefix=GFX9-FETCH %s
-; RUN: llc -mtriple=amdgcn -mcpu=gfx1030 -amdgpu-align-functions-for-fetch-only < %s | FileCheck -check-prefix=GFX10-FETCH %s
-; RUN: llc -mtriple=amdgcn -mcpu=gfx1100 -amdgpu-align-functions-for-fetch-only < %s | FileCheck -check-prefix=GFX11-FETCH %s
-; RUN: llc -mtriple=amdgcn -mcpu=gfx1200 -amdgpu-align-functions-for-fetch-only < %s | FileCheck -check-prefix=GFX12-FETCH %s
+; RUN: llc -mtriple=amdgcn -mcpu=gfx900 < %s | FileCheck -check-prefix=GFX9 %s
+; RUN: llc -mtriple=amdgcn -mcpu=gfx1030 < %s | FileCheck -check-prefix=GFX10 %s
+; RUN: llc -mtriple=amdgcn -mcpu=gfx1100 < %s | FileCheck -check-prefix=GFX11 %s
+; RUN: llc -mtriple=amdgcn -mcpu=gfx1200 < %s | FileCheck -check-prefix=GFX12 %s
; --- Optsize: alignment drops to minimum (Align(4) = .p2align 2) ---
@@ -42,31 +34,19 @@
; RUN: llc -mtriple=amdgcn -mcpu=gfx900 < %s | FileCheck -check-prefix=ENTRY %s
-; Non-entry function: alignment depends on architecture and option.
+; Non-entry function: alignment matches instruction cache line size.
define void @non_entry_func() {
-; GFX9-CACHE: .p2align 6{{$}}
-; GFX9-CACHE: non_entry_func:
-
-; GFX10-CACHE: .p2align 6{{$}}
-; GFX10-CACHE: non_entry_func:
-
-; GFX11-CACHE: .p2align 7{{$}}
-; GFX11-CACHE: non_entry_func:
-
-; GFX12-CACHE: .p2align 7{{$}}
-; GFX12-CACHE: non_entry_func:
+; GFX9: .p2align 6{{$}}
+; GFX9: non_entry_func:
-; GFX9-FETCH: .p2align 5{{$}}
-; GFX9-FETCH: non_entry_func:
+; GFX10: .p2align 6{{$}}
+; GFX10: non_entry_func:
-; GFX10-FETCH: .p2align 2{{$}}
-; GFX10-FETCH: non_entry_func:
+; GFX11: .p2align 7{{$}}
+; GFX11: non_entry_func:
-; GFX11-FETCH: .p2align 2{{$}}
-; GFX11-FETCH: non_entry_func:
-
-; GFX12-FETCH: .p2align 2{{$}}
-; GFX12-FETCH: non_entry_func:
+; GFX12: .p2align 7{{$}}
+; GFX12: non_entry_func:
ret void
}
@@ -85,11 +65,11 @@ define void @explicit_align_func() align 128 {
ret void
}
-; Non-entry function with explicit IR align 32 on GFX9: lower than preferred
-; (64), so preferred alignment wins. Result: .p2align 6.
+; Non-entry function with explicit IR align 32 on gfx900 -- lower than
+; preferred (64), so preferred alignment wins. Result: .p2align 6.
define void @low_align_func() align 32 {
-; GFX9-CACHE: .globl low_align_func
-; GFX9-CACHE-NEXT: .p2align 6{{$}}
+; GFX9: .globl low_align_func
+; GFX9-NEXT: .p2align 6{{$}}
ret void
}
@@ -102,23 +82,32 @@ define void @align_all_optsize_func() optsize {
ret void
}
-; prefalign(16) on GFX9: overrides target preferred (64) with 16.
+; prefalign(16) on gfx900 overrides target preferred (64) with 16.
; getPreferredAlignment uses prefalign directly instead of getPrefFunctionAlignment.
-; Result: max(16, 4) = 16 → .p2align 4.
+; Result: max(16, 4) = 16 -> .p2align 4.
define void @prefalign_low_func() prefalign(16) {
; PREFALIGN: .globl prefalign_low_func
; PREFALIGN-NEXT: .p2align 4{{$}}
ret void
}
-; prefalign(256) on GFX9: higher than target preferred (64).
-; Result: max(256, 4) = 256 → .p2align 8.
+; prefalign(256) on gfx900 -- higher than target preferred (64).
+; Result: max(256, 4) = 256 -> .p2align 8.
define void @prefalign_high_func() prefalign(256) {
; PREFALIGN: .globl prefalign_high_func
; PREFALIGN-NEXT: .p2align 8{{$}}
ret void
}
+; prefalign(2) on gfx900 -- below the 4-byte instruction alignment floor.
+; ensureAlignment(4) in AsmPrinter guarantees the minimum.
+; Result: max(2, 4) = 4 -> .p2align 2.
+define void @prefalign_floor_func() prefalign(2) {
+; PREFALIGN: .globl prefalign_floor_func
+; PREFALIGN-NEXT: .p2align 2{{$}}
+ ret void
+}
+
; Entry function: must be 256B aligned regardless of our changes.
define amdgpu_kernel void @entry_func() {
; ENTRY: .globl entry_func
More information about the llvm-commits
mailing list