Skip to content

Commit 8ca6401

Browse files
authored
[flang][openacc] parse and ignore non-standard shortloop clause (#106564)
shortloop is a non standard OpenACC extension (https://docs.nvidia.com/hpc-sdk/pgi-compilers/2015/pgirn157.pdf) that can be found on loop directives. f18 parser was choking when seeing it. Since it can be found in existing apps and is mainly an optimization hint, parse it on loop directives and ignore it with a warning. For the records, here is shortloop meaning according to the manual linked above: "If the shortloop clause appears on a loop directive with the vector clause, it tells the compiler that the loop trip count is less than or equal to the number of vector lanes created for that loop. This means the value of the vector() clause on the loop directive in a kernels region, or the value of the vector_length() clause on the parallel directive in a parallel region will be greater than or equal to the loop trip count. This allows the compiler to generate more efficient code for the loop"
1 parent d68059b commit 8ca6401

File tree

5 files changed

+88
-5
lines changed

5 files changed

+88
-5
lines changed

flang/lib/Semantics/check-acc-structure.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,15 @@ void AccStructureChecker::Enter(const parser::AccClause::Link &x) {
769769
CheckMultipleOccurrenceInDeclare(x.v, llvm::acc::Clause::ACCC_link);
770770
}
771771

772+
void AccStructureChecker::Enter(const parser::AccClause::Shortloop &x) {
773+
if (CheckAllowed(llvm::acc::Clause::ACCC_shortloop) &&
774+
context_.languageFeatures().ShouldWarn(
775+
common::UsageWarning::OpenAccUsage)) {
776+
context_.Say(GetContext().clauseSource,
777+
"Non-standard shortloop clause ignored"_warn_en_US);
778+
}
779+
}
780+
772781
void AccStructureChecker::Enter(const parser::AccClause::If &x) {
773782
CheckAllowed(llvm::acc::Clause::ACCC_if);
774783
if (const auto *expr{GetExpr(x.v)}) {

flang/lib/Semantics/check-directive-structure.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,9 @@ class DirectiveStructureChecker : public virtual BaseChecker {
354354

355355
void CheckRequireAtLeastOneOf(bool warnInsteadOfError = false);
356356

357-
void CheckAllowed(C clause, bool warnInsteadOfError = false);
357+
// Check if a clause is allowed on a directive. Returns true if is and
358+
// false otherwise.
359+
bool CheckAllowed(C clause, bool warnInsteadOfError = false);
358360

359361
// Check that the clause appears only once. The counter is reset when the
360362
// separator clause appears.
@@ -484,7 +486,7 @@ std::string DirectiveStructureChecker<D, C, PC,
484486

485487
// Check that clauses present on the directive are allowed clauses.
486488
template <typename D, typename C, typename PC, std::size_t ClauseEnumSize>
487-
void DirectiveStructureChecker<D, C, PC, ClauseEnumSize>::CheckAllowed(
489+
bool DirectiveStructureChecker<D, C, PC, ClauseEnumSize>::CheckAllowed(
488490
C clause, bool warnInsteadOfError) {
489491
if (!GetContext().allowedClauses.test(clause) &&
490492
!GetContext().allowedOnceClauses.test(clause) &&
@@ -504,7 +506,7 @@ void DirectiveStructureChecker<D, C, PC, ClauseEnumSize>::CheckAllowed(
504506
parser::ToUpperCaseLetters(getClauseName(clause).str()),
505507
parser::ToUpperCaseLetters(GetContext().directiveSource.ToString()));
506508
}
507-
return;
509+
return false;
508510
}
509511
if ((GetContext().allowedOnceClauses.test(clause) ||
510512
GetContext().allowedExclusiveClauses.test(clause)) &&
@@ -513,7 +515,7 @@ void DirectiveStructureChecker<D, C, PC, ClauseEnumSize>::CheckAllowed(
513515
"At most one %s clause can appear on the %s directive"_err_en_US,
514516
parser::ToUpperCaseLetters(getClauseName(clause).str()),
515517
parser::ToUpperCaseLetters(GetContext().directiveSource.ToString()));
516-
return;
518+
return false;
517519
}
518520
if (GetContext().allowedExclusiveClauses.test(clause)) {
519521
std::vector<C> others;
@@ -531,12 +533,13 @@ void DirectiveStructureChecker<D, C, PC, ClauseEnumSize>::CheckAllowed(
531533
parser::ToUpperCaseLetters(GetContext().directiveSource.ToString()));
532534
}
533535
if (!others.empty()) {
534-
return;
536+
return false;
535537
}
536538
}
537539
SetContextClauseInfo(clause);
538540
AddClauseToCrtContext(clause);
539541
AddClauseToCrtGroupInContext(clause);
542+
return true;
540543
}
541544

542545
// Enforce restriction where clauses in the given set are not allowed if the
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
! Test that non-standard shortloop clause is accepted and ignored with a
2+
! warning.
3+
4+
! RUN: %flang_fc1 -fopenacc -emit-hlfir %s -o - 2>&1 | FileCheck %s
5+
6+
! CHECK: warning: Non-standard shortloop clause ignored
7+
! CHECK: warning: Non-standard shortloop clause ignored
8+
! CHECK: warning: Non-standard shortloop clause ignored
9+
! CHECK: warning: Non-standard shortloop clause ignored
10+
11+
subroutine test_loop(a, b, c)
12+
implicit none
13+
real, dimension(100) :: a,b,c
14+
integer :: i
15+
!$acc loop vector shortloop
16+
do i=1,100
17+
a(i) = b(i) + c(i)
18+
enddo
19+
end subroutine
20+
! CHECK-LABEL: test_loop
21+
! CHECK: acc.loop vector
22+
23+
subroutine test_kernels_loop(a, b, c)
24+
implicit none
25+
real, dimension(100) :: a,b,c
26+
integer :: i
27+
!$acc kernels loop vector shortloop
28+
do i=1,100
29+
a(i) = b(i) + c(i)
30+
enddo
31+
end subroutine
32+
! CHECK-LABEL: test_kernels_loop
33+
! CHECK: acc.loop combined(kernels) vector
34+
35+
subroutine test_parallel_loop(a, b, c)
36+
implicit none
37+
real, dimension(100) :: a,b,c
38+
integer :: i
39+
!$acc parallel loop vector shortloop
40+
do i=1,100
41+
a(i) = b(i) + c(i)
42+
enddo
43+
end subroutine
44+
! CHECK-LABEL: test_parallel_loop
45+
! CHECK: acc.loop combined(parallel) vector
46+
47+
subroutine test_serial_loop(a, b, c)
48+
implicit none
49+
real, dimension(100) :: a,b,c
50+
integer :: i
51+
!$acc serial loop vector shortloop
52+
do i=1,100
53+
a(i) = b(i) + c(i)
54+
enddo
55+
end subroutine
56+
! CHECK-LABEL: test_serial_loop
57+
! CHECK: acc.loop combined(serial) vector

flang/test/Semantics/OpenACC/acc-routine-validity.f90

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,11 @@ subroutine sub6(a)
7272
!$acc routine seq bind(dummy_sub)
7373
end subroutine sub6
7474

75+
subroutine sub7(a)
76+
real :: a(:)
77+
!ERROR: SHORTLOOP clause is not allowed on the KERNELS directive
78+
!$acc kernels shortloop
79+
!$acc end kernels
80+
end subroutine sub7
81+
7582
end module openacc_routine_validity

llvm/include/llvm/Frontend/OpenACC/ACC.td

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,9 @@ def ACCC_Self : Clause<"self"> {
229229
// 2.9.5
230230
def ACCC_Seq : Clause<"seq"> {}
231231

232+
// Non-standard extension
233+
def ACCC_ShortLoop : Clause<"shortloop"> {}
234+
232235
// 2.9.4
233236
def ACCC_Vector : Clause<"vector"> {
234237
let flangClass = "ScalarIntExpr";
@@ -407,6 +410,7 @@ def ACC_Loop : Directive<"loop"> {
407410
VersionedClause<ACCC_Reduction>,
408411
VersionedClause<ACCC_Collapse>,
409412
VersionedClause<ACCC_Gang>,
413+
VersionedClause<ACCC_ShortLoop>,
410414
VersionedClause<ACCC_Tile>,
411415
VersionedClause<ACCC_Vector>,
412416
VersionedClause<ACCC_Worker>
@@ -583,6 +587,7 @@ def ACC_KernelsLoop : Directive<"kernels loop"> {
583587
VersionedClause<ACCC_Present>,
584588
VersionedClause<ACCC_Private>,
585589
VersionedClause<ACCC_Reduction>,
590+
VersionedClause<ACCC_ShortLoop>,
586591
VersionedClause<ACCC_Tile>,
587592
VersionedClause<ACCC_Vector>,
588593
VersionedClause<ACCC_VectorLength>,
@@ -623,6 +628,7 @@ def ACC_ParallelLoop : Directive<"parallel loop"> {
623628
VersionedClause<ACCC_Present>,
624629
VersionedClause<ACCC_Private>,
625630
VersionedClause<ACCC_Reduction>,
631+
VersionedClause<ACCC_ShortLoop>,
626632
VersionedClause<ACCC_Tile>,
627633
VersionedClause<ACCC_Vector>,
628634
VersionedClause<ACCC_VectorLength>,
@@ -661,6 +667,7 @@ def ACC_SerialLoop : Directive<"serial loop"> {
661667
VersionedClause<ACCC_Present>,
662668
VersionedClause<ACCC_Private>,
663669
VersionedClause<ACCC_Reduction>,
670+
VersionedClause<ACCC_ShortLoop>,
664671
VersionedClause<ACCC_Tile>,
665672
VersionedClause<ACCC_Vector>,
666673
VersionedClause<ACCC_Wait>,

0 commit comments

Comments
 (0)