[clang] [llvm] [AMDGPU] Pad INST_PREF_SIZE by extra cache lines past the kernel (PR #245591)
Vikram Hegde via cfe-commits
cfe-commits at lists.llvm.org
Tue Nov 24 20:11:42 PST 2026
https://github.com/vikramRH updated https://github.com/llvm/llvm-project/pull/245591
>From 541095ea644aa7f752c99674bb21494009425f5b Mon Sep 17 00:00:00 2001
From: vikhegde <vikram.hegde at amd.com>
Date: Tue, 24 Nov 2026 17:13:43 +0530
Subject: [PATCH] [AMDGPU] Pad INST_PREF_SIZE by extra cache lines past the
kernel
---
clang/include/clang/Basic/Attr.td | 7 ++
clang/include/clang/Basic/AttrDocs.td | 17 +++++
clang/include/clang/Sema/SemaAMDGPU.h | 1 +
clang/lib/CIR/CodeGen/Targets/AMDGPU.cpp | 15 ++++
clang/lib/CodeGen/Targets/AMDGPU.cpp | 5 ++
clang/lib/Sema/SemaAMDGPU.cpp | 11 +++
clang/lib/Sema/SemaDeclAttr.cpp | 7 ++
clang/test/CIR/CodeGenHIP/amdgpu-attrs.hip | 15 ++++
.../amdgpu-inst-pref-pad-lines.cl | 12 +++
...a-attribute-supported-attributes-list.test | 1 +
.../SemaCUDA/amdgpu-inst-pref-pad-lines.cu | 11 +++
.../SemaOpenCL/amdgpu-inst-pref-pad-lines.cl | 14 ++++
llvm/docs/AMDGPUUsage.md | 7 ++
llvm/lib/Target/AMDGPU/AMDGPUAsmPrinter.cpp | 26 +++++++
.../CodeGen/AMDGPU/inst-pref-pad-lines.ll | 74 +++++++++++++++++++
15 files changed, 223 insertions(+)
create mode 100644 clang/test/CodeGenOpenCL/amdgpu-inst-pref-pad-lines.cl
create mode 100644 clang/test/SemaCUDA/amdgpu-inst-pref-pad-lines.cu
create mode 100644 clang/test/SemaOpenCL/amdgpu-inst-pref-pad-lines.cl
create mode 100644 llvm/test/CodeGen/AMDGPU/inst-pref-pad-lines.ll
diff --git a/clang/include/clang/Basic/Attr.td b/clang/include/clang/Basic/Attr.td
index 35afd9d197cfde..e79703bab9085b 100644
--- a/clang/include/clang/Basic/Attr.td
+++ b/clang/include/clang/Basic/Attr.td
@@ -2555,6 +2555,13 @@ def AMDGPUNumVGPR : InheritableAttr {
let Subjects = SubjectList<[Function], ErrorDiag, "kernel functions">;
}
+def AMDGPUInstPrefPadLines : InheritableAttr {
+ let Spellings = [Clang<"amdgpu_inst_pref_pad_lines", 0>];
+ let Args = [UnsignedArgument<"Lines">];
+ let Documentation = [AMDGPUInstPrefPadLinesDocs];
+ let Subjects = SubjectList<[Function], ErrorDiag, "kernel functions">;
+}
+
def AMDGPUMaxNumWorkGroups : InheritableAttr {
let Spellings = [Clang<"amdgpu_max_num_work_groups", 0>];
let Args = [ExprArgument<"MaxNumWorkGroupsX">, ExprArgument<"MaxNumWorkGroupsY", 1>, ExprArgument<"MaxNumWorkGroupsZ", 1>];
diff --git a/clang/include/clang/Basic/AttrDocs.td b/clang/include/clang/Basic/AttrDocs.td
index bd9a40835a567e..f1e0586601c0bb 100644
--- a/clang/include/clang/Basic/AttrDocs.td
+++ b/clang/include/clang/Basic/AttrDocs.td
@@ -3763,6 +3763,23 @@ An error will be given if:
}];
}
+def AMDGPUInstPrefPadLinesDocs : Documentation {
+ let Category = DocCatAMDGPUAttributes;
+ let Content = [{
+Clang supports the
+`__attribute__((amdgpu_inst_pref_pad_lines(<N>)))` attribute for the AMDGPU
+target. The attribute may be attached to a kernel function definition and
+lowers to the `"amdgpu-inst-pref-pad-lines"` LLVM function attribute.
+
+`<N>` is the number of extra instruction-cache lines added to
+`COMPUTE_PGM_RSRC3.INST_PREF_SIZE` beyond the kernel's code size. The kernel
+code itself is left unchanged. The value overrides the
+`-mllvm -amdgpu-inst-pref-pad-lines=<N>` option for that kernel, and an
+explicit `0` keeps the unpadded size. The backend clamps the resulting field
+to its width (255 lines on GFX12 and newer, 63 lines on GFX11).
+ }];
+}
+
def AMDGPUMaxNumWorkGroupsDocs : Documentation {
let Category = DocCatAMDGPUAttributes;
let Content = [{
diff --git a/clang/include/clang/Sema/SemaAMDGPU.h b/clang/include/clang/Sema/SemaAMDGPU.h
index 2f8e6225872e7b..eb4000455b6f1a 100644
--- a/clang/include/clang/Sema/SemaAMDGPU.h
+++ b/clang/include/clang/Sema/SemaAMDGPU.h
@@ -79,6 +79,7 @@ class SemaAMDGPU : public SemaBase {
void handleAMDGPUWavesPerEUAttr(Decl *D, const ParsedAttr &AL);
void handleAMDGPUNumSGPRAttr(Decl *D, const ParsedAttr &AL);
void handleAMDGPUNumVGPRAttr(Decl *D, const ParsedAttr &AL);
+ void handleAMDGPUInstPrefPadLinesAttr(Decl *D, const ParsedAttr &AL);
void handleAMDGPUMaxNumWorkGroupsAttr(Decl *D, const ParsedAttr &AL);
void handleAMDGPUFlatWorkGroupSizeAttr(Decl *D, const ParsedAttr &AL);
diff --git a/clang/lib/CIR/CodeGen/Targets/AMDGPU.cpp b/clang/lib/CIR/CodeGen/Targets/AMDGPU.cpp
index d527e2981ddc6a..576a43153887b1 100644
--- a/clang/lib/CIR/CodeGen/Targets/AMDGPU.cpp
+++ b/clang/lib/CIR/CodeGen/Targets/AMDGPU.cpp
@@ -144,6 +144,20 @@ static void handleAMDGPUNumVGPRAttr(const FunctionDecl *fd, cir::FuncOp func,
}
}
+/// Handle amdgpu-inst-pref-pad-lines attribute.
+static void handleAMDGPUInstPrefPadLinesAttr(const FunctionDecl *fd,
+ cir::FuncOp func,
+ CIRGenModule &cgm,
+ CIRGenBuilderTy &builder) {
+ const auto *attr = fd->getAttr<AMDGPUInstPrefPadLinesAttr>();
+ if (!attr)
+ return;
+
+ // Emit 0 as well: an explicit zero overrides -amdgpu-inst-pref-pad-lines.
+ func->setAttr("cir.amdgpu-inst-pref-pad-lines",
+ builder.getStringAttr(llvm::utostr(attr->getLines())));
+}
+
/// Handle amdgpu-max-num-workgroups attribute.
static void handleAMDGPUMaxNumWorkGroupsAttr(const FunctionDecl *fd,
cir::FuncOp func,
@@ -257,6 +271,7 @@ void clang::CIRGen::setAMDGPUTargetFunctionAttributes(const Decl *decl,
handleAMDGPUWavesPerEUAttr(fd, func, cgm, builder, lbMinWaves);
handleAMDGPUNumSGPRAttr(fd, func, cgm, builder);
handleAMDGPUNumVGPRAttr(fd, func, cgm, builder);
+ handleAMDGPUInstPrefPadLinesAttr(fd, func, cgm, builder);
handleAMDGPUMaxNumWorkGroupsAttr(fd, func, cgm, builder);
handleAMDGPUClusterDimsAttr(fd, func, cgm, builder, isOpenCLKernel);
}
diff --git a/clang/lib/CodeGen/Targets/AMDGPU.cpp b/clang/lib/CodeGen/Targets/AMDGPU.cpp
index fd9182adda47db..d4bd975a83cf03 100644
--- a/clang/lib/CodeGen/Targets/AMDGPU.cpp
+++ b/clang/lib/CodeGen/Targets/AMDGPU.cpp
@@ -395,6 +395,11 @@ void AMDGPUTargetCodeGenInfo::setFunctionDeclAttributes(
F->addFnAttr("amdgpu-num-vgpr", llvm::utostr(NumVGPR));
}
+ if (const auto *Attr = FD->getAttr<AMDGPUInstPrefPadLinesAttr>()) {
+ // Emit 0 as well: an explicit zero overrides -amdgpu-inst-pref-pad-lines.
+ F->addFnAttr("amdgpu-inst-pref-pad-lines", llvm::utostr(Attr->getLines()));
+ }
+
if (const auto *Attr = FD->getAttr<AMDGPUMaxNumWorkGroupsAttr>()) {
uint32_t X = Attr->getMaxNumWorkGroupsX()
->EvaluateKnownConstInt(M.getContext())
diff --git a/clang/lib/Sema/SemaAMDGPU.cpp b/clang/lib/Sema/SemaAMDGPU.cpp
index 0afbf3c3291b15..9df453ae05345e 100644
--- a/clang/lib/Sema/SemaAMDGPU.cpp
+++ b/clang/lib/Sema/SemaAMDGPU.cpp
@@ -723,6 +723,17 @@ void SemaAMDGPU::handleAMDGPUNumVGPRAttr(Decl *D, const ParsedAttr &AL) {
AMDGPUNumVGPRAttr(getASTContext(), AL, NumVGPR));
}
+void SemaAMDGPU::handleAMDGPUInstPrefPadLinesAttr(Decl *D,
+ const ParsedAttr &AL) {
+ uint32_t Lines = 0;
+ Expr *LinesExpr = AL.getArgAsExpr(0);
+ if (!SemaRef.checkUInt32Argument(AL, LinesExpr, Lines))
+ return;
+
+ D->addAttr(::new (getASTContext())
+ AMDGPUInstPrefPadLinesAttr(getASTContext(), AL, Lines));
+}
+
static bool
checkAMDGPUMaxNumWorkGroupsArguments(Sema &S, Expr *XExpr, Expr *YExpr,
Expr *ZExpr,
diff --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp
index 3d16a1d0e9bbeb..99b4ebaee1afbb 100644
--- a/clang/lib/Sema/SemaDeclAttr.cpp
+++ b/clang/lib/Sema/SemaDeclAttr.cpp
@@ -7817,6 +7817,9 @@ ProcessDeclAttribute(Sema &S, Decl *D, const ParsedAttr &AL,
case ParsedAttr::AT_AMDGPUNumVGPR:
S.AMDGPU().handleAMDGPUNumVGPRAttr(D, AL);
break;
+ case ParsedAttr::AT_AMDGPUInstPrefPadLines:
+ S.AMDGPU().handleAMDGPUInstPrefPadLinesAttr(D, AL);
+ break;
case ParsedAttr::AT_AMDGPUMaxNumWorkGroups:
S.AMDGPU().handleAMDGPUMaxNumWorkGroupsAttr(D, AL);
break;
@@ -8713,6 +8716,10 @@ void Sema::ProcessDeclAttributeList(
Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
<< A << A->isRegularKeywordAttribute() << ExpectedKernelFunction;
D->setInvalidDecl();
+ } else if (const auto *A = D->getAttr<AMDGPUInstPrefPadLinesAttr>()) {
+ Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
+ << A << A->isRegularKeywordAttribute() << ExpectedKernelFunction;
+ D->setInvalidDecl();
}
}
checkAMDGPUReqdWorkGroupSize(*this, D);
diff --git a/clang/test/CIR/CodeGenHIP/amdgpu-attrs.hip b/clang/test/CIR/CodeGenHIP/amdgpu-attrs.hip
index 9f50a15ba72536..e5020e7865beca 100644
--- a/clang/test/CIR/CodeGenHIP/amdgpu-attrs.hip
+++ b/clang/test/CIR/CodeGenHIP/amdgpu-attrs.hip
@@ -92,6 +92,17 @@ __attribute__((amdgpu_waves_per_eu(3)))
__launch_bounds__(128, 4)
__global__ void kernel_lb_override() {}
+// Test: Instruction-prefetch pad. Zero is emitted so it can override the flag.
+// CIR: cir.func{{.*}} @_Z15kernel_pref_padv(){{.*}}"cir.amdgpu-inst-pref-pad-lines" = "2"
+// LLVM: define{{.*}} void @_Z15kernel_pref_padv(){{.*}} #[[PREF_PAD_ATTR:[0-9]+]]
+__attribute__((amdgpu_inst_pref_pad_lines(2)))
+__global__ void kernel_pref_pad() {}
+
+// CIR: cir.func{{.*}} @_Z17kernel_pref_pad_0v(){{.*}}"cir.amdgpu-inst-pref-pad-lines" = "0"
+// LLVM: define{{.*}} void @_Z17kernel_pref_pad_0v(){{.*}} #[[PREF_PAD0_ATTR:[0-9]+]]
+__attribute__((amdgpu_inst_pref_pad_lines(0)))
+__global__ void kernel_pref_pad_0() {}
+
// Test: Device function should NOT have kernel attributes
// CIR: cir.func{{.*}} @_Z9device_fnv()
// CIR-NOT: cir.amdgpu-flat-work-group-size
@@ -105,6 +116,8 @@ __device__ void device_fn() {}
// LLVM-DAG: attributes #[[WAVES_MM_ATTR]] = {{.*}}"amdgpu-waves-per-eu"="2,4"
// LLVM-DAG: attributes #[[SGPR_ATTR]] = {{.*}}"amdgpu-num-sgpr"="32"
// LLVM-DAG: attributes #[[VGPR_ATTR]] = {{.*}}"amdgpu-num-vgpr"="64"
+// LLVM-DAG: attributes #[[PREF_PAD_ATTR]] = {{.*}}"amdgpu-inst-pref-pad-lines"="2"
+// LLVM-DAG: attributes #[[PREF_PAD0_ATTR]] = {{.*}}"amdgpu-inst-pref-pad-lines"="0"
// LLVM-DAG: attributes #[[MAX_WG_ATTR]] = {{.*}}"amdgpu-max-num-workgroups"="8,4,2"
// LLVM-DAG: attributes #[[LB_THREAD_ATTR]] = {{.*}}"amdgpu-flat-work-group-size"="1,256"
// LLVM-DAG: attributes #[[LB_WAVES_ATTR]] = {{.*}}"amdgpu-flat-work-group-size"="1,128"{{.*}}"amdgpu-waves-per-eu"="4"
@@ -120,4 +133,6 @@ __device__ void device_fn() {}
// PROF: cir.func{{.*}} @_Z15kernel_num_vgprv(){{.*}}"cir.amdgpu-expand-waitcnt-profiling"
// PROF: cir.func{{.*}} @_Z22kernel_max_num_wgroupsv(){{.*}}"cir.amdgpu-expand-waitcnt-profiling"
// PROF: cir.func{{.*}} @_Z15kernel_combinedv(){{.*}}"cir.amdgpu-expand-waitcnt-profiling"
+// PROF: cir.func{{.*}} @_Z15kernel_pref_padv(){{.*}}"cir.amdgpu-expand-waitcnt-profiling"
+// PROF: cir.func{{.*}} @_Z17kernel_pref_pad_0v(){{.*}}"cir.amdgpu-expand-waitcnt-profiling"
// PROF: cir.func{{.*}} @_Z9device_fnv(){{.*}}"cir.amdgpu-expand-waitcnt-profiling"
diff --git a/clang/test/CodeGenOpenCL/amdgpu-inst-pref-pad-lines.cl b/clang/test/CodeGenOpenCL/amdgpu-inst-pref-pad-lines.cl
new file mode 100644
index 00000000000000..e2163056cce887
--- /dev/null
+++ b/clang/test/CodeGenOpenCL/amdgpu-inst-pref-pad-lines.cl
@@ -0,0 +1,12 @@
+// RUN: %clang_cc1 -triple amdgpu12.00-amd-amdhsa -O0 -emit-llvm -o - %s | FileCheck %s
+
+__attribute__((amdgpu_inst_pref_pad_lines(2)))
+kernel void pad_two() {}
+// CHECK: define{{.*}} amdgpu_kernel void @pad_two() [[PAD2:#[0-9]+]]
+
+__attribute__((amdgpu_inst_pref_pad_lines(0)))
+kernel void pad_zero() {}
+// CHECK: define{{.*}} amdgpu_kernel void @pad_zero() [[PAD0:#[0-9]+]]
+
+// CHECK-DAG: attributes [[PAD2]] = {{.*}}"amdgpu-inst-pref-pad-lines"="2"
+// CHECK-DAG: attributes [[PAD0]] = {{.*}}"amdgpu-inst-pref-pad-lines"="0"
diff --git a/clang/test/Misc/pragma-attribute-supported-attributes-list.test b/clang/test/Misc/pragma-attribute-supported-attributes-list.test
index 2b5c4741dc2166..15c122330f5a20 100644
--- a/clang/test/Misc/pragma-attribute-supported-attributes-list.test
+++ b/clang/test/Misc/pragma-attribute-supported-attributes-list.test
@@ -4,6 +4,7 @@
// CHECK: #pragma clang attribute supports the following attributes:
// CHECK-NEXT: AMDGPUFlatWorkGroupSize (SubjectMatchRule_function)
+// CHECK-NEXT: AMDGPUInstPrefPadLines (SubjectMatchRule_function)
// CHECK-NEXT: AMDGPUMaxNumWorkGroups (SubjectMatchRule_function)
// CHECK-NEXT: AMDGPUNumSGPR (SubjectMatchRule_function)
// CHECK-NEXT: AMDGPUNumVGPR (SubjectMatchRule_function)
diff --git a/clang/test/SemaCUDA/amdgpu-inst-pref-pad-lines.cu b/clang/test/SemaCUDA/amdgpu-inst-pref-pad-lines.cu
new file mode 100644
index 00000000000000..4a223e92864810
--- /dev/null
+++ b/clang/test/SemaCUDA/amdgpu-inst-pref-pad-lines.cu
@@ -0,0 +1,11 @@
+// RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s
+#include "Inputs/cuda.h"
+
+__attribute__((amdgpu_inst_pref_pad_lines(2)))
+__global__ void pad_two() {}
+
+__attribute__((amdgpu_inst_pref_pad_lines(0)))
+__global__ void pad_zero() {}
+
+__attribute__((amdgpu_inst_pref_pad_lines(2)))
+__device__ void not_a_kernel() {} // expected-error {{'amdgpu_inst_pref_pad_lines' attribute only applies to kernel functions}}
diff --git a/clang/test/SemaOpenCL/amdgpu-inst-pref-pad-lines.cl b/clang/test/SemaOpenCL/amdgpu-inst-pref-pad-lines.cl
new file mode 100644
index 00000000000000..6720976a73b3ee
--- /dev/null
+++ b/clang/test/SemaOpenCL/amdgpu-inst-pref-pad-lines.cl
@@ -0,0 +1,14 @@
+// RUN: %clang_cc1 -triple amdgpu-- -verify -fsyntax-only %s
+
+typedef __attribute__((amdgpu_inst_pref_pad_lines(2))) struct struct_pad { // expected-error {{'amdgpu_inst_pref_pad_lines' attribute only applies to kernel functions}}
+ int x;
+} struct_pad;
+
+__attribute__((amdgpu_inst_pref_pad_lines(2))) void not_a_kernel() {} // expected-error {{'amdgpu_inst_pref_pad_lines' attribute only applies to kernel functions}}
+
+__attribute__((amdgpu_inst_pref_pad_lines("ABC"))) kernel void not_an_int() {} // expected-error {{'amdgpu_inst_pref_pad_lines' attribute requires an integer constant}}
+
+__attribute__((amdgpu_inst_pref_pad_lines(4294967296))) kernel void too_wide() {} // expected-error {{integer constant expression evaluates to value 4294967296 that cannot be represented in a 32-bit unsigned integer type}}
+
+__attribute__((amdgpu_inst_pref_pad_lines(0))) kernel void zero() {}
+__attribute__((amdgpu_inst_pref_pad_lines(2))) kernel void two() {}
diff --git a/llvm/docs/AMDGPUUsage.md b/llvm/docs/AMDGPUUsage.md
index 6aa5b66020e341..d2b254837cd9cb 100644
--- a/llvm/docs/AMDGPUUsage.md
+++ b/llvm/docs/AMDGPUUsage.md
@@ -3361,6 +3361,13 @@ The AMDGPU backend supports the following LLVM IR attributes.
This is an optimization hint, and the backend may not be able to satisfy the request.
If the specified range is incompatible with the function's "amdgpu-flat-work-group-size" value, the occupancy bounds implied by the workgroup size take precedence.
Under object linking, this hint cannot lower occupancy below the ABI occupancy floor (and therefore cannot raise the register cap); see {ref}`amdgpu-abi-occupancy`.
+* - "amdgpu-inst-pref-pad-lines"="n"
+ - Extra instruction-cache lines added to `COMPUTE_PGM_RSRC3.INST_PREF_SIZE` beyond the kernel code size.
+ Generated by the `amdgpu_inst_pref_pad_lines` CLANG attribute \[{ref}`CLANG-ATTR <clang-attr>`\].
+ The kernel code itself is not changed.
+ `n` overrides `-amdgpu-inst-pref-pad-lines` for that kernel, and an explicit `0` leaves the size unpadded.
+ The emitted field is `min(ceil(code_size / line_size) + n, field maximum)`: 63 lines on GFX11 and 255 lines on GFX12 and newer.
+ Applies to GFX11 and newer kernels. `.amdhsa_inst_pref_size` shows the padded value.
* - "amdgpu-ieee" true/false.
- GFX6-GFX11 (Except GFX11.7) Only Specify whether the function expects the IEEE field of the mode register to be set on entry.
Overrides the default for the calling convention.
diff --git a/llvm/lib/Target/AMDGPU/AMDGPUAsmPrinter.cpp b/llvm/lib/Target/AMDGPU/AMDGPUAsmPrinter.cpp
index 97a8a2464126bf..06b1a0d0480aea 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPUAsmPrinter.cpp
+++ b/llvm/lib/Target/AMDGPU/AMDGPUAsmPrinter.cpp
@@ -47,6 +47,7 @@
#include "llvm/MC/MCValue.h"
#include "llvm/MC/TargetRegistry.h"
#include "llvm/Support/AMDHSAKernelDescriptor.h"
+#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Target/TargetLoweringObjectFile.h"
#include "llvm/Target/TargetMachine.h"
@@ -55,6 +56,14 @@
using namespace llvm;
using namespace llvm::AMDGPU;
+// Extra instruction-cache lines added to COMPUTE_PGM_RSRC3.INST_PREF_SIZE.
+// The per-kernel "amdgpu-inst-pref-pad-lines" attribute overrides this.
+static cl::opt<unsigned> InstPrefPadLines(
+ "amdgpu-inst-pref-pad-lines",
+ cl::desc("Pad COMPUTE_PGM_RSRC3.INST_PREF_SIZE by N instruction-cache "
+ "lines beyond the kernel code size"),
+ cl::init(0), cl::Hidden);
+
// This should get the default rounding mode from the kernel. We just set the
// default here, but this could change if the OpenCL rounding mode pragmas are
// used.
@@ -259,6 +268,23 @@ void AMDGPUAsmPrinter::endFunction(const MachineFunction *MF) {
uint32_t Mask, Shift, Width, CacheLineSize;
STM.getInstPrefSizeArgs(Mask, Shift, Width, CacheLineSize);
+
+ // INST_PREF_SIZE = min(ceil(code_size / line_size) + N, field max).
+ // Adding N lines of bytes before the ceil is the same as adding N to the
+ // line count. evaluateInstPrefSize clamps to the field width. A kernel
+ // attribute overrides the global option, including an explicit 0.
+ uint64_t PadLines = MF->getFunction().getFnAttributeAsParsedInteger(
+ "amdgpu-inst-pref-pad-lines", InstPrefPadLines);
+ uint64_t MaxLines = (1ull << Width) - 1;
+ if (PadLines > MaxLines)
+ PadLines = MaxLines;
+ if (PadLines) {
+ const MCExpr *PadBytes = MCConstantExpr::create(
+ static_cast<int64_t>(PadLines * CacheLineSize), OutContext);
+ CodeSizeExpr =
+ MCBinaryExpr::createAdd(CodeSizeExpr, PadBytes, OutContext);
+ }
+
const MCExpr *InstPrefSize =
AMDGPUMCExpr::createInstPrefSize(CodeSizeExpr, Ctx);
KD.compute_pgm_rsrc3 =
diff --git a/llvm/test/CodeGen/AMDGPU/inst-pref-pad-lines.ll b/llvm/test/CodeGen/AMDGPU/inst-pref-pad-lines.ll
new file mode 100644
index 00000000000000..e4050364deed1e
--- /dev/null
+++ b/llvm/test/CodeGen/AMDGPU/inst-pref-pad-lines.ll
@@ -0,0 +1,74 @@
+; RUN: llc -mtriple=amdgpu12.00-amd-amdhsa < %s | FileCheck --check-prefixes=NOPAD,NOPAD-GFX12 %s
+; RUN: llc -mtriple=amdgpu11.00-amd-amdhsa < %s | FileCheck --check-prefixes=NOPAD,NOPAD-GFX11 %s
+; RUN: llc -mtriple=amdgpu12.00-amd-amdhsa -amdgpu-inst-pref-pad-lines=2 < %s | FileCheck --check-prefixes=PAD2,PAD2-GFX12 %s
+; RUN: llc -mtriple=amdgpu11.00-amd-amdhsa -amdgpu-inst-pref-pad-lines=2 < %s | FileCheck --check-prefixes=PAD2,PAD2-GFX11 %s
+; RUN: llc -mtriple=amdgpu12.50-amd-amdhsa -amdgpu-inst-pref-pad-lines=2 < %s | FileCheck --check-prefixes=PAD2,PAD2-GFX12 %s
+
+;; Resolved kernel descriptors. COMPUTE_PGM_RSRC3 is at +0x2C in each
+;; 64-byte descriptor. inst_pref_size is bits [11:4] (GFX12+) or [9:4] (GFX11).
+;; An empty kernel occupies 1 cache line. Cache lines are 128 bytes.
+; RUN: llc -mtriple=amdgpu12.00-amd-amdhsa -amdgpu-inst-pref-pad-lines=2 -filetype=obj < %s -o %t.pad2.gfx12.o
+; RUN: llvm-objdump -s -j .rodata %t.pad2.gfx12.o | FileCheck --check-prefix=OBJ-PAD2 %s
+; RUN: llc -mtriple=amdgpu12.00-amd-amdhsa -amdgpu-inst-pref-pad-lines=2 < %s | llvm-mc -triple amdgpu12.00-amd-amdhsa -mcpu=gfx1200 -filetype=obj -o %t.rt.o
+; RUN: llvm-objdump -s -j .rodata %t.rt.o | FileCheck --check-prefix=OBJ-PAD2 %s
+; RUN: llc -mtriple=amdgpu12.00-amd-amdhsa -amdgpu-inst-pref-pad-lines=1000 -filetype=obj < %s -o %t.clamp.gfx12.o
+; RUN: llvm-objdump -s -j .rodata %t.clamp.gfx12.o | FileCheck --check-prefix=OBJ-CLAMP12 %s
+; RUN: llc -mtriple=amdgpu11.00-amd-amdhsa -amdgpu-inst-pref-pad-lines=1000 -filetype=obj < %s -o %t.clamp.gfx11.o
+; RUN: llvm-objdump -s -j .rodata %t.clamp.gfx11.o | FileCheck --check-prefix=OBJ-CLAMP11 %s
+
+; The global option adds 2 lines (256 bytes) to the code-size expression.
+; The kernel body stays a single s_endpgm.
+; PAD2-LABEL: small:
+; PAD2: s_endpgm
+
+; NOPAD-LABEL: .amdhsa_kernel small
+; NOPAD-GFX12: .amdhsa_inst_pref_size ((instprefsize(.Lfunc_end0-small)<<4)&4080)>>4
+; NOPAD-GFX11: .amdhsa_inst_pref_size ((instprefsize(.Lfunc_end0-small)<<4)&1008)>>4
+; PAD2-LABEL: .amdhsa_kernel small
+; PAD2-GFX12: .amdhsa_inst_pref_size ((instprefsize(.Lfunc_end0-small+256)<<4)&4080)>>4
+; PAD2-GFX11: .amdhsa_inst_pref_size ((instprefsize(.Lfunc_end0-small+256)<<4)&1008)>>4
+;; pref = min(1 + 2, max) = 3 → 3 << 4 = 0x30
+; OBJ-PAD2: 0020 {{.*}}30000000
+;; pref = min(1 + 1000, 255) = 255 → 255 << 4 = 0xff0
+; OBJ-CLAMP12: 0020 {{.*}}f00f0000
+;; pref = min(1 + 1000, 63) = 63 → 63 << 4 = 0x3f0
+; OBJ-CLAMP11: 0020 {{.*}}f0030000
+define amdgpu_kernel void @small() {
+bb:
+ ret void
+}
+
+; A kernel attribute replaces the global option. 4 lines = 512 bytes.
+; NOPAD-LABEL: .amdhsa_kernel attr_pad
+; NOPAD-GFX12: .amdhsa_inst_pref_size ((instprefsize(.Lfunc_end1-attr_pad+512)<<4)&4080)>>4
+; NOPAD-GFX11: .amdhsa_inst_pref_size ((instprefsize(.Lfunc_end1-attr_pad+512)<<4)&1008)>>4
+; PAD2-LABEL: .amdhsa_kernel attr_pad
+; PAD2-GFX12: .amdhsa_inst_pref_size ((instprefsize(.Lfunc_end1-attr_pad+512)<<4)&4080)>>4
+; PAD2-GFX11: .amdhsa_inst_pref_size ((instprefsize(.Lfunc_end1-attr_pad+512)<<4)&1008)>>4
+;; pref = 1 + 4 = 5 → 5 << 4 = 0x50. Descriptor at 0x40, field at 0x6c.
+; OBJ-PAD2: 0060 {{.*}}50000000
+; OBJ-CLAMP12: 0060 {{.*}}50000000
+; OBJ-CLAMP11: 0060 {{.*}}50000000
+define amdgpu_kernel void @attr_pad() #0 {
+bb:
+ ret void
+}
+
+; An explicit 0 keeps today's unpadded size even when the global option is set.
+; NOPAD-LABEL: .amdhsa_kernel attr_zero
+; NOPAD-GFX12: .amdhsa_inst_pref_size ((instprefsize(.Lfunc_end2-attr_zero)<<4)&4080)>>4
+; NOPAD-GFX11: .amdhsa_inst_pref_size ((instprefsize(.Lfunc_end2-attr_zero)<<4)&1008)>>4
+; PAD2-LABEL: .amdhsa_kernel attr_zero
+; PAD2-GFX12: .amdhsa_inst_pref_size ((instprefsize(.Lfunc_end2-attr_zero)<<4)&4080)>>4
+; PAD2-GFX11: .amdhsa_inst_pref_size ((instprefsize(.Lfunc_end2-attr_zero)<<4)&1008)>>4
+;; pref = 1 → 0x10. Descriptor at 0x80, field at 0xac.
+; OBJ-PAD2: 00a0 {{.*}}10000000
+; OBJ-CLAMP12: 00a0 {{.*}}10000000
+; OBJ-CLAMP11: 00a0 {{.*}}10000000
+define amdgpu_kernel void @attr_zero() #1 {
+bb:
+ ret void
+}
+
+attributes #0 = { "amdgpu-inst-pref-pad-lines"="4" }
+attributes #1 = { "amdgpu-inst-pref-pad-lines"="0" }
More information about the cfe-commits
mailing list