Skip to content

[flang] Parse !$CUF KERNEL DO <<< (*) #85338

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 1 commit into from
Mar 15, 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
1 change: 1 addition & 0 deletions flang/include/flang/Parser/dump-parse-tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ class ParseTreeDumper {
NODE(parser, CriticalStmt)
NODE(parser, CUDAAttributesStmt)
NODE(parser, CUFKernelDoConstruct)
NODE(CUFKernelDoConstruct, StarOrExpr)
NODE(CUFKernelDoConstruct, Directive)
NODE(parser, CycleStmt)
NODE(parser, DataComponentDefStmt)
Expand Down
10 changes: 6 additions & 4 deletions flang/include/flang/Parser/parse-tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -4297,16 +4297,18 @@ struct OpenACCConstruct {
// CUF-kernel-do-construct ->
// !$CUF KERNEL DO [ (scalar-int-constant-expr) ] <<< grid, block [, stream]
// >>> do-construct
// grid -> * | scalar-int-expr | ( scalar-int-expr-list )
// block -> * | scalar-int-expr | ( scalar-int-expr-list )
// star-or-expr -> * | scalar-int-expr
// grid -> * | scalar-int-expr | ( star-or-expr-list )
// block -> * | scalar-int-expr | ( star-or-expr-list )
// stream -> 0, scalar-int-expr | STREAM = scalar-int-expr
struct CUFKernelDoConstruct {
TUPLE_CLASS_BOILERPLATE(CUFKernelDoConstruct);
WRAPPER_CLASS(StarOrExpr, std::optional<ScalarIntExpr>);
struct Directive {
TUPLE_CLASS_BOILERPLATE(Directive);
CharBlock source;
std::tuple<std::optional<ScalarIntConstantExpr>, std::list<ScalarIntExpr>,
std::list<ScalarIntExpr>, std::optional<ScalarIntExpr>>
std::tuple<std::optional<ScalarIntConstantExpr>, std::list<StarOrExpr>,
std::list<StarOrExpr>, std::optional<ScalarIntExpr>>
t;
};
std::tuple<Directive, std::optional<DoConstruct>> t;
Expand Down
29 changes: 21 additions & 8 deletions flang/lib/Lower/Bridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2508,19 +2508,32 @@ class FirConverter : public Fortran::lower::AbstractConverter {
if (nestedLoops > 1)
n = builder->getIntegerAttr(builder->getI64Type(), nestedLoops);

const std::list<Fortran::parser::ScalarIntExpr> &grid = std::get<1>(dir.t);
const std::list<Fortran::parser::ScalarIntExpr> &block = std::get<2>(dir.t);
const std::list<Fortran::parser::CUFKernelDoConstruct::StarOrExpr> &grid =
std::get<1>(dir.t);
const std::list<Fortran::parser::CUFKernelDoConstruct::StarOrExpr> &block =
std::get<2>(dir.t);
const std::optional<Fortran::parser::ScalarIntExpr> &stream =
std::get<3>(dir.t);

llvm::SmallVector<mlir::Value> gridValues;
for (const Fortran::parser::ScalarIntExpr &expr : grid)
gridValues.push_back(fir::getBase(
genExprValue(*Fortran::semantics::GetExpr(expr), stmtCtx)));
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: '*'
}
}
llvm::SmallVector<mlir::Value> blockValues;
for (const Fortran::parser::ScalarIntExpr &expr : block)
blockValues.push_back(fir::getBase(
genExprValue(*Fortran::semantics::GetExpr(expr), stmtCtx)));
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: '*'
}
}
mlir::Value streamValue;
if (stream)
streamValue = fir::getBase(
Expand Down
20 changes: 10 additions & 10 deletions flang/lib/Parser/executable-parsers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -542,19 +542,19 @@ TYPE_CONTEXT_PARSER("UNLOCK statement"_en_US,
// CUF-kernel-do-directive ->
// !$CUF KERNEL DO [ (scalar-int-constant-expr) ] <<< grid, block [, stream]
// >>> do-construct
// grid -> * | scalar-int-expr | ( scalar-int-expr-list )
// block -> * | scalar-int-expr | ( scalar-int-expr-list )
// star-or-expr -> * | scalar-int-expr
// grid -> * | scalar-int-expr | ( star-or-expr-list )
// block -> * | scalar-int-expr | ( star-or-expr-list )
// stream -> ( 0, | STREAM = ) scalar-int-expr
constexpr auto starOrExpr{construct<CUFKernelDoConstruct::StarOrExpr>(
"*" >> pure<std::optional<ScalarIntExpr>>() ||
applyFunction(presentOptional<ScalarIntExpr>, scalarIntExpr))};
constexpr auto gridOrBlock{parenthesized(nonemptyList(starOrExpr)) ||
applyFunction(singletonList<CUFKernelDoConstruct::StarOrExpr>, starOrExpr)};
TYPE_PARSER(sourced(beginDirective >> "$CUF KERNEL DO"_tok >>
construct<CUFKernelDoConstruct::Directive>(
maybe(parenthesized(scalarIntConstantExpr)),
"<<<" >>
("*" >> pure<std::list<ScalarIntExpr>>() ||
parenthesized(nonemptyList(scalarIntExpr)) ||
applyFunction(singletonList<ScalarIntExpr>, scalarIntExpr)),
"," >> ("*" >> pure<std::list<ScalarIntExpr>>() ||
parenthesized(nonemptyList(scalarIntExpr)) ||
applyFunction(singletonList<ScalarIntExpr>, scalarIntExpr)),
maybe(parenthesized(scalarIntConstantExpr)), "<<<" >> gridOrBlock,
"," >> gridOrBlock,
maybe((", 0 ,"_tok || ", STREAM ="_tok) >> scalarIntExpr) / ">>>" /
endDirective)))
TYPE_CONTEXT_PARSER("!$CUF KERNEL DO construct"_en_US,
Expand Down
5 changes: 5 additions & 0 deletions flang/lib/Parser/misc-parsers.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,10 @@ template <typename A> common::IfNoLvalue<std::list<A>, A> singletonList(A &&x) {
result.emplace_back(std::move(x));
return result;
}

template <typename A>
common::IfNoLvalue<std::optional<A>, A> presentOptional(A &&x) {
return std::make_optional(std::move(x));
}
} // namespace Fortran::parser
#endif
7 changes: 7 additions & 0 deletions flang/lib/Parser/unparse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2729,6 +2729,13 @@ class UnparseVisitor {
WALK_NESTED_ENUM(OmpOrderModifier, Kind) // OMP order-modifier
#undef WALK_NESTED_ENUM

void Unparse(const CUFKernelDoConstruct::StarOrExpr &x) {
if (x.v) {
Walk(*x.v);
} else {
Word("*");
}
}
void Unparse(const CUFKernelDoConstruct::Directive &x) {
Word("!$CUF KERNEL DO");
Walk(" (", std::get<std::optional<ScalarIntConstantExpr>>(x.t), ")");
Expand Down
5 changes: 1 addition & 4 deletions flang/test/Lower/CUDA/cuda-kernel-loop-directive.cuf
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,7 @@ subroutine sub1()
! CHECK: fir.cuda_kernel<<<%c1{{.*}}, (%c256{{.*}}, %c1{{.*}})>>> (%{{.*}} : index, %{{.*}} : index) = (%{{.*}}, %{{.*}} : index, index) to (%{{.*}}, %{{.*}} : index, index) step (%{{.*}}, %{{.*}} : index, index)
! CHECK: {n = 2 : i64}

! TODO: currently these trigger error in the parser
! TODO: lowering for these cases
! !$cuf kernel do(2) <<< (1,*), (256,1) >>>
! !$cuf kernel do(2) <<< (*,*), (32,4) >>>
end



6 changes: 3 additions & 3 deletions flang/test/Parser/cuf-sanity-tree.CUF
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,11 @@ include "cuf-sanity-common"
!CHECK: | | | | | | EndDoStmt ->
!CHECK: | | | | ExecutionPartConstruct -> ExecutableConstruct -> CUFKernelDoConstruct
!CHECK: | | | | | Directive
!CHECK: | | | | | | Scalar -> Integer -> Expr = '1_4'
!CHECK: | | | | | | StarOrExpr -> Scalar -> Integer -> Expr = '1_4'
!CHECK: | | | | | | | LiteralConstant -> IntLiteralConstant = '1'
!CHECK: | | | | | | Scalar -> Integer -> Expr = '2_4'
!CHECK: | | | | | | StarOrExpr -> Scalar -> Integer -> Expr = '2_4'
!CHECK: | | | | | | | LiteralConstant -> IntLiteralConstant = '2'
!CHECK: | | | | | | Scalar -> Integer -> Expr = '3_4'
!CHECK: | | | | | | StarOrExpr -> Scalar -> Integer -> Expr = '3_4'
!CHECK: | | | | | | | LiteralConstant -> IntLiteralConstant = '3'
!CHECK: | | | | | | Scalar -> Integer -> Expr = '1_4'
!CHECK: | | | | | | | LiteralConstant -> IntLiteralConstant = '1'
Expand Down
9 changes: 9 additions & 0 deletions flang/test/Semantics/cuf09.cuf
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ module m
end

program main
!$cuf kernel do <<< *, * >>> ! ok
do j = 1, 0
end do
!$cuf kernel do <<< (*), (*) >>> ! ok
do j = 1, 0
end do
!$cuf kernel do <<< (1,*), (2,*) >>> ! ok
do j = 1, 0
end do
!ERROR: !$CUF KERNEL DO (1) must be followed by a DO construct with tightly nested outer levels of counted DO loops
!$cuf kernel do <<< 1, 2 >>>
do while (.false.)
Expand Down