Skip to content

[NVPTX] Add support for "blocksareclusters" kernel attr #152265

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions llvm/docs/NVPTXUsage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ Function Attributes
dimension. Specifying a different cluster dimension at launch will result in
a runtime error or kernel launch failure. Only supported for Hopper+.

``"nvvm.blocksareclusters"``
This attribute implies that the grid launch configuration for the corresponding
kernel function is specifying the number of clusters instead of the number of thread
blocks. This attribute is only allowed for kernel functions and requires
``nvvm.reqntid`` and ``nvvm.cluster_dim`` attributes.

.. _address_spaces:

Address Spaces
Expand Down
8 changes: 4 additions & 4 deletions llvm/lib/Target/NVPTX/NVPTX.td
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,10 @@ foreach sm = [20, 21, 30, 32, 35, 37, 50, 52, 53,
def SM#sm#a : FeatureSM<""#sm#"a", !add(!mul(sm, 10), 3)>;
}

foreach version = [32, 40, 41, 42, 43, 50, 60, 61, 62, 63, 64, 65,
70, 71, 72, 73, 74, 75, 76, 77, 78,
80, 81, 82, 83, 84, 85, 86, 87, 88] in
def PTX#version: FeaturePTX<version>;
foreach version = [32, 40, 41, 42, 43, 50, 60, 61, 62, 63, 64, 65, 70, 71, 72,
73, 74, 75, 76, 77, 78, 80, 81, 82, 83, 84, 85, 86, 87, 88,
90] in
def PTX#version : FeaturePTX<version>;

//===----------------------------------------------------------------------===//
// NVPTX supported processors.
Expand Down
19 changes: 18 additions & 1 deletion llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -436,9 +436,13 @@ void NVPTXAsmPrinter::emitKernelFunctionDirectives(const Function &F,

if (STI->getSmVersion() >= 90) {
const auto ClusterDim = getClusterDim(F);
const bool BlocksAreClusters = hasBlocksAreClusters(F);

if (!ClusterDim.empty()) {
O << ".explicitcluster\n";

if (!BlocksAreClusters)
O << ".explicitcluster\n";

if (ClusterDim[0] != 0) {
assert(llvm::all_of(ClusterDim, [](unsigned D) { return D != 0; }) &&
"cluster_dim_x != 0 implies cluster_dim_y and cluster_dim_z "
Expand All @@ -452,6 +456,19 @@ void NVPTXAsmPrinter::emitKernelFunctionDirectives(const Function &F,
"should be 0 as well");
}
}

if (BlocksAreClusters) {
LLVMContext &Ctx = F.getContext();
if (ReqNTID.empty() || ClusterDim.empty()) {
Ctx.emitError(
"blocksareclusters requires reqntid and cluster_dim attributes");
} else if (STI->getPTXVersion() < 90) {
Ctx.emitError("blocksareclusters requires PTX version >= 9.0");
} else {
O << ".blocksareclusters\n";
}
}

if (const auto Maxclusterrank = getMaxClusterRank(F))
O << ".maxclusterrank " << *Maxclusterrank << "\n";
}
Expand Down
4 changes: 4 additions & 0 deletions llvm/lib/Target/NVPTX/NVPTXUtilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,10 @@ std::optional<unsigned> getMaxNReg(const Function &F) {
return getFnAttrParsedInt(F, "nvvm.maxnreg");
}

bool hasBlocksAreClusters(const Function &F) {
return F.hasFnAttribute("nvvm.blocksareclusters");
}

MaybeAlign getAlign(const CallInst &I, unsigned Index) {
// First check the alignstack metadata
if (MaybeAlign StackAlign =
Expand Down
2 changes: 2 additions & 0 deletions llvm/lib/Target/NVPTX/NVPTXUtilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ std::optional<unsigned> getMaxClusterRank(const Function &);
std::optional<unsigned> getMinCTASm(const Function &);
std::optional<unsigned> getMaxNReg(const Function &);

bool hasBlocksAreClusters(const Function &);

inline bool isKernelFunction(const Function &F) {
return F.getCallingConv() == CallingConv::PTX_Kernel;
}
Expand Down
60 changes: 60 additions & 0 deletions llvm/test/CodeGen/NVPTX/blocksareclusters-kernel-attr.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 5
; RUN: llc < %s -mcpu=sm_90 -mattr=+ptx90 | FileCheck %s

target triple = "nvptx64-nvidia-cuda"

; Test "blocksareclusters" attribute with full "reqntid" and "cluster_dim"
; attributes.
define ptx_kernel void @kernel1(ptr %input, ptr %output) #0 #1 #2 {
; CHECK-LABEL: kernel1(
; CHECK: .reqntid 1024, 1, 1
; CHECK-NEXT: .reqnctapercluster 2, 2, 2
; CHECK-NEXT: .blocksareclusters
; CHECK-NEXT: {
; CHECK-EMPTY:
; CHECK-EMPTY:
; CHECK-NEXT: // %bb.0:
; CHECK-NEXT: ret;
ret void
}

; Test "blocksareclusters" attribute with single dimension "reqntid" and
; "cluster_dim" attributes.
define ptx_kernel void @kernel2(ptr %input, ptr %output) #0 #3 #4 {
; CHECK-LABEL: kernel2(
; CHECK: .reqntid 1024
; CHECK-NEXT: .reqnctapercluster 2
; CHECK-NEXT: .blocksareclusters // @kernel2
; CHECK-NEXT: {
; CHECK-EMPTY:
; CHECK-EMPTY:
; CHECK-NEXT: // %bb.0:
; CHECK-NEXT: ret;
ret void
}

; Test "blocksareclusters" attribute with two dimensions(not z dimension)
; "reqntid" and "cluster_dim" attributes.
define ptx_kernel void @kernel3(ptr %input, ptr %output) #0 #5 #6 {
; CHECK-LABEL: kernel3(
; CHECK: .reqntid 512, 2
; CHECK-NEXT: .reqnctapercluster 2, 2
; CHECK-NEXT: .blocksareclusters // @kernel3
; CHECK-NEXT: {
; CHECK-EMPTY:
; CHECK-EMPTY:
; CHECK-NEXT: // %bb.0:
; CHECK-NEXT: ret;
ret void
}

attributes #0 = { "nvvm.blocksareclusters" }

attributes #1 = { "nvvm.reqntid"="1024,1,1" }
attributes #2 = { "nvvm.cluster_dim"="2,2,2" }

attributes #3 = { "nvvm.reqntid"="1024" }
attributes #4 = { "nvvm.cluster_dim"="2" }

attributes #5 = { "nvvm.reqntid"="512,2" }
attributes #6 = { "nvvm.cluster_dim"="2,2" }