Skip to content

[flang][cuda] Handle lowering of stars in cuf kernel launch parameters #85695

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

Merged
merged 3 commits into from
Mar 19, 2024
Merged
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
10 changes: 10 additions & 0 deletions flang/include/flang/Optimizer/Dialect/FIROps.td
Original file line number Diff line number Diff line change
Expand Up @@ -3131,6 +3131,16 @@ def fir_BoxOffsetOp : fir_Op<"box_offset", [NoMemoryEffect]> {
def fir_CUDAKernelOp : fir_Op<"cuda_kernel", [AttrSizedOperandSegments,
DeclareOpInterfaceMethods<LoopLikeOpInterface>]> {

let description = [{
Represent the CUDA Fortran kernel directive. The operation is a loop like
operation that represents the iteration range of the embedded loop nest.

When grid or block variadic operands are empty, a `*` only syntax was used
in the Fortran code.
If the `*` is mixed with values for either grid or block, these are
represented by a 0 constant value.
}];

let arguments = (ins
Variadic<I32>:$grid, // empty means `*`
Variadic<I32>:$block, // empty means `*`
Expand Down
45 changes: 32 additions & 13 deletions flang/lib/Lower/Bridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2529,23 +2529,42 @@ class FirConverter : public Fortran::lower::AbstractConverter {
const std::optional<Fortran::parser::ScalarIntExpr> &stream =
std::get<3>(dir.t);

auto isOnlyStars =
[&](const std::list<Fortran::parser::CUFKernelDoConstruct::StarOrExpr>
&list) -> bool {
for (const Fortran::parser::CUFKernelDoConstruct::StarOrExpr &expr :
list) {
if (expr.v)
return false;
}
return true;
};

mlir::Value zero =
builder->createIntegerConstant(loc, builder->getI32Type(), 0);

llvm::SmallVector<mlir::Value> gridValues;
for (const Fortran::parser::CUFKernelDoConstruct::StarOrExpr &expr : grid) {
if (expr.v) {
gridValues.push_back(fir::getBase(
genExprValue(*Fortran::semantics::GetExpr(*expr.v), stmtCtx)));
} else {
// TODO: '*'
if (!isOnlyStars(grid)) {
for (const Fortran::parser::CUFKernelDoConstruct::StarOrExpr &expr :
grid) {
if (expr.v) {
gridValues.push_back(fir::getBase(
genExprValue(*Fortran::semantics::GetExpr(*expr.v), stmtCtx)));
} else {
gridValues.push_back(zero);
}
}
}
llvm::SmallVector<mlir::Value> blockValues;
for (const Fortran::parser::CUFKernelDoConstruct::StarOrExpr &expr :
block) {
if (expr.v) {
blockValues.push_back(fir::getBase(
genExprValue(*Fortran::semantics::GetExpr(*expr.v), stmtCtx)));
} else {
// TODO: '*'
if (!isOnlyStars(block)) {
for (const Fortran::parser::CUFKernelDoConstruct::StarOrExpr &expr :
block) {
if (expr.v) {
blockValues.push_back(fir::getBase(
genExprValue(*Fortran::semantics::GetExpr(*expr.v), stmtCtx)));
} else {
blockValues.push_back(zero);
}
}
}
mlir::Value streamValue;
Expand Down
19 changes: 16 additions & 3 deletions flang/test/Lower/CUDA/cuda-kernel-loop-directive.cuf
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,20 @@ subroutine sub1()
! CHECK: fir.cuda_kernel<<<%c1{{.*}}, (%c256{{.*}}, %c1{{.*}})>>> (%{{.*}} : index, %{{.*}} : index) = (%{{.*}}, %{{.*}} : index, index) to (%{{.*}}, %{{.*}} : index, index) step (%{{.*}}, %{{.*}} : index, index)
! CHECK: {n = 2 : i64}

! TODO: lowering for these cases
! !$cuf kernel do(2) <<< (1,*), (256,1) >>>
! !$cuf kernel do(2) <<< (*,*), (32,4) >>>
!$cuf kernel do(2) <<< (1,*), (256,1) >>>
do i = 1, n
do j = 1, n
c(i,j) = c(i,j) * d(i,j)
end do
end do
! CHECK: fir.cuda_kernel<<<(%c1{{.*}}, %c0{{.*}}), (%c256{{.*}}, %c1{{.*}})>>> (%{{.*}} : index, %{{.*}} : index) = (%{{.*}}, %{{.*}} : index, index) to (%{{.*}}, %{{.*}} : index, index) step (%{{.*}}, %{{.*}} : index, index)

!$cuf kernel do(2) <<< (*,*), (32,4) >>>
do i = 1, n
do j = 1, n
c(i,j) = c(i,j) * d(i,j)
end do
end do

! CHECK: fir.cuda_kernel<<<*, (%c32{{.*}}, %c4{{.*}})>>> (%{{.*}} : index, %{{.*}} : index) = (%{{.*}}, %{{.*}} : index, index) to (%{{.*}}, %{{.*}} : index, index) step (%{{.*}}, %{{.*}} : index, index)
end