Skip to content

Commit 0ffaf40

Browse files
clementvalGroverkss
authored andcommitted
[fang][cuda] Allow * in call chevron syntax (llvm#115381)
Using `*` in call chevron syntax should be allowed. This patch updates the parser to allow this usage. ``` call sub<<<*,nbBlock>>>() ```
1 parent f6aecb0 commit 0ffaf40

File tree

8 files changed

+31
-11
lines changed

8 files changed

+31
-11
lines changed

flang/include/flang/Parser/dump-parse-tree.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ class ParseTreeDumper {
177177
NODE(parser, Call)
178178
NODE(parser, CallStmt)
179179
NODE(CallStmt, Chevrons)
180+
NODE(CallStmt, StarOrExpr)
180181
NODE(parser, CaseConstruct)
181182
NODE(CaseConstruct, Case)
182183
NODE(parser, CaseSelector)

flang/include/flang/Parser/parse-tree.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3247,13 +3247,14 @@ struct FunctionReference {
32473247

32483248
// R1521 call-stmt -> CALL procedure-designator [ chevrons ]
32493249
// [( [actual-arg-spec-list] )]
3250-
// (CUDA) chevrons -> <<< scalar-expr, scalar-expr [,
3250+
// (CUDA) chevrons -> <<< * | scalar-expr, scalar-expr [,
32513251
// scalar-int-expr [, scalar-int-expr ] ] >>>
32523252
struct CallStmt {
32533253
BOILERPLATE(CallStmt);
3254+
WRAPPER_CLASS(StarOrExpr, std::optional<ScalarExpr>);
32543255
struct Chevrons {
32553256
TUPLE_CLASS_BOILERPLATE(Chevrons);
3256-
std::tuple<ScalarExpr, ScalarExpr, std::optional<ScalarIntExpr>,
3257+
std::tuple<StarOrExpr, ScalarExpr, std::optional<ScalarIntExpr>,
32573258
std::optional<ScalarIntExpr>>
32583259
t;
32593260
};

flang/lib/Parser/program-parsers.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -474,10 +474,13 @@ TYPE_CONTEXT_PARSER("function reference"_en_US,
474474

475475
// R1521 call-stmt -> CALL procedure-designator [chevrons]
476476
/// [( [actual-arg-spec-list] )]
477-
// (CUDA) chevrons -> <<< scalar-expr, scalar-expr [, scalar-int-expr
477+
// (CUDA) chevrons -> <<< * | scalar-expr, scalar-expr [, scalar-int-expr
478478
// [, scalar-int-expr ] ] >>>
479+
constexpr auto starOrExpr{
480+
construct<CallStmt::StarOrExpr>("*" >> pure<std::optional<ScalarExpr>>() ||
481+
applyFunction(presentOptional<ScalarExpr>, scalarExpr))};
479482
TYPE_PARSER(extension<LanguageFeature::CUDA>(
480-
"<<<" >> construct<CallStmt::Chevrons>(scalarExpr, "," >> scalarExpr,
483+
"<<<" >> construct<CallStmt::Chevrons>(starOrExpr, ", " >> scalarExpr,
481484
maybe("," >> scalarIntExpr), maybe("," >> scalarIntExpr)) /
482485
">>>"))
483486
constexpr auto actualArgSpecList{optionalList(actualArgSpec)};

flang/lib/Parser/unparse.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1703,6 +1703,13 @@ class UnparseVisitor {
17031703
void Unparse(const IntrinsicStmt &x) { // R1519
17041704
Word("INTRINSIC :: "), Walk(x.v, ", ");
17051705
}
1706+
void Unparse(const CallStmt::StarOrExpr &x) {
1707+
if (x.v) {
1708+
Walk(*x.v);
1709+
} else {
1710+
Word("*");
1711+
}
1712+
}
17061713
void Unparse(const CallStmt::Chevrons &x) { // CUDA
17071714
Walk(std::get<0>(x.t)); // grid
17081715
Word(","), Walk(std::get<1>(x.t)); // block

flang/lib/Semantics/expression.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3066,11 +3066,17 @@ std::optional<Chevrons> ExpressionAnalyzer::AnalyzeChevrons(
30663066
return false;
30673067
}};
30683068
if (const auto &chevrons{call.chevrons}) {
3069-
if (auto expr{Analyze(std::get<0>(chevrons->t))};
3070-
expr && checkLaunchArg(*expr, "grid")) {
3071-
result.emplace_back(*expr);
3069+
auto &starOrExpr{std::get<0>(chevrons->t)};
3070+
if (starOrExpr.v) {
3071+
if (auto expr{Analyze(*starOrExpr.v)};
3072+
expr && checkLaunchArg(*expr, "grid")) {
3073+
result.emplace_back(*expr);
3074+
} else {
3075+
return std::nullopt;
3076+
}
30723077
} else {
3073-
return std::nullopt;
3078+
result.emplace_back(
3079+
AsGenericExpr(evaluate::Constant<evaluate::CInteger>{-1}));
30743080
}
30753081
if (auto expr{Analyze(std::get<1>(chevrons->t))};
30763082
expr && checkLaunchArg(*expr, "block")) {

flang/test/Parser/cuf-sanity-common

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ module m
4040
call globalsub<<<1, 2>>>
4141
call globalsub<<<1, 2, 3>>>
4242
call globalsub<<<1, 2, 3, 4>>>
43+
call globalsub<<<*,5>>>
4344
allocate(pa(32), pinned = isPinned)
4445
end subroutine
4546
end module

flang/test/Parser/cuf-sanity-tree.CUF

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,15 +166,15 @@ include "cuf-sanity-common"
166166
!CHECK: | | | | | Call
167167
!CHECK: | | | | | | ProcedureDesignator -> Name = 'globalsub'
168168
!CHECK: | | | | | Chevrons
169-
!CHECK: | | | | | | Scalar -> Expr = '1_4'
169+
!CHECK: | | | | | | StarOrExpr -> Scalar -> Expr = '1_4'
170170
!CHECK: | | | | | | | LiteralConstant -> IntLiteralConstant = '1'
171171
!CHECK: | | | | | | Scalar -> Expr = '2_4'
172172
!CHECK: | | | | | | | LiteralConstant -> IntLiteralConstant = '2'
173173
!CHECK: | | | | ExecutionPartConstruct -> ExecutableConstruct -> ActionStmt -> CallStmt = 'CALL globalsub<<<1_4,2_4,3_4>>>()'
174174
!CHECK: | | | | | Call
175175
!CHECK: | | | | | | ProcedureDesignator -> Name = 'globalsub'
176176
!CHECK: | | | | | Chevrons
177-
!CHECK: | | | | | | Scalar -> Expr = '1_4'
177+
!CHECK: | | | | | | StarOrExpr -> Scalar -> Expr = '1_4'
178178
!CHECK: | | | | | | | LiteralConstant -> IntLiteralConstant = '1'
179179
!CHECK: | | | | | | Scalar -> Expr = '2_4'
180180
!CHECK: | | | | | | | LiteralConstant -> IntLiteralConstant = '2'
@@ -184,7 +184,7 @@ include "cuf-sanity-common"
184184
!CHECK: | | | | | Call
185185
!CHECK: | | | | | | ProcedureDesignator -> Name = 'globalsub'
186186
!CHECK: | | | | | Chevrons
187-
!CHECK: | | | | | | Scalar -> Expr = '1_4'
187+
!CHECK: | | | | | | StarOrExpr -> Scalar -> Expr = '1_4'
188188
!CHECK: | | | | | | | LiteralConstant -> IntLiteralConstant = '1'
189189
!CHECK: | | | | | | Scalar -> Expr = '2_4'
190190
!CHECK: | | | | | | | LiteralConstant -> IntLiteralConstant = '2'

flang/test/Parser/cuf-sanity-unparse.CUF

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ include "cuf-sanity-common"
4343
!CHECK: CALL globalsub<<<1_4,2_4>>>()
4444
!CHECK: CALL globalsub<<<1_4,2_4,3_4>>>()
4545
!CHECK: CALL globalsub<<<1_4,2_4,3_4,4_4>>>()
46+
!CHECK: CALL globalsub<<<-1_4,5_4>>>()
4647
!CHECK: ALLOCATE(pa(32_4), PINNED=ispinned)
4748
!CHECK: END SUBROUTINE
4849
!CHECK: END MODULE

0 commit comments

Comments
 (0)