Skip to content

Commit cf670d5

Browse files
authored
[flang][openacc] Accept scalar integer expression in the if clause (#69381)
Relax the parser to accept scalar integer expression in addition to scalar logical expression. The parser now accepts scalar expression and the semantic checks its type.
1 parent 1981b1b commit cf670d5

File tree

6 files changed

+30
-2
lines changed

6 files changed

+30
-2
lines changed

flang/docs/OpenACC.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,5 @@ local:
2121
* `!$acc end loop` does not trigger a parsing error and is just ignored.
2222
* The restriction on `!$acc data` required clauses is emitted as a portability
2323
warning instead of an error as other compiler accepts it.
24+
* The `if` clause accepts scalar integer expression in addition to scalar
25+
logical expression.

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,6 @@ CHECK_SIMPLE_CLAUSE(DeviceNum, ACCC_device_num)
376376
CHECK_SIMPLE_CLAUSE(Finalize, ACCC_finalize)
377377
CHECK_SIMPLE_CLAUSE(Firstprivate, ACCC_firstprivate)
378378
CHECK_SIMPLE_CLAUSE(Host, ACCC_host)
379-
CHECK_SIMPLE_CLAUSE(If, ACCC_if)
380379
CHECK_SIMPLE_CLAUSE(IfPresent, ACCC_if_present)
381380
CHECK_SIMPLE_CLAUSE(Independent, ACCC_independent)
382381
CHECK_SIMPLE_CLAUSE(NoCreate, ACCC_no_create)
@@ -660,6 +659,20 @@ void AccStructureChecker::Enter(const parser::AccClause::Link &x) {
660659
CheckMultipleOccurrenceInDeclare(x.v, llvm::acc::Clause::ACCC_link);
661660
}
662661

662+
void AccStructureChecker::Enter(const parser::AccClause::If &x) {
663+
CheckAllowed(llvm::acc::Clause::ACCC_if);
664+
if (const auto *expr{GetExpr(x.v)}) {
665+
if (auto type{expr->GetType()}) {
666+
if (type->category() == TypeCategory::Integer ||
667+
type->category() == TypeCategory::Logical) {
668+
return; // LOGICAL and INTEGER type supported for the if clause.
669+
}
670+
}
671+
}
672+
context_.Say(
673+
GetContext().clauseSource, "Must have LOGICAL or INTEGER type"_err_en_US);
674+
}
675+
663676
void AccStructureChecker::Enter(const parser::Module &) {
664677
declareSymbols.clear();
665678
}

flang/test/Lower/OpenACC/acc-init.f90

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
subroutine acc_init
77
logical :: ifCondition = .TRUE.
8+
integer :: ifInt = 1
89

910
!$acc init
1011
!CHECK: acc.init{{ *}}{{$}}
@@ -28,4 +29,9 @@ subroutine acc_init
2829
!CHECK: [[DEVTYPE2:%.*]] = arith.constant 2 : i32
2930
!CHECK: acc.init device_type([[DEVTYPE1]], [[DEVTYPE2]] : i32, i32) device_num([[DEVNUM]] : i32){{$}}
3031

32+
!$acc init if(ifInt)
33+
!CHECK: %[[IFINT:.*]] = fir.load %{{.*}} : !fir.ref<i32>
34+
!CHECK: %[[CONV:.*]] = fir.convert %[[IFINT]] : (i32) -> i1
35+
!CHECK: acc.init if(%[[CONV]])
36+
3137
end subroutine acc_init

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,14 @@ program openacc_init_validity
1010
integer :: i, j
1111
integer, parameter :: N = 256
1212
logical :: ifCondition = .TRUE.
13+
integer :: ifInt
14+
real :: ifReal
1315
real(8), dimension(N) :: a
1416

1517
!$acc init
1618
!$acc init if(.TRUE.)
1719
!$acc init if(ifCondition)
20+
!$acc init if(ifInt)
1821
!$acc init device_num(1)
1922
!$acc init device_num(i)
2023
!$acc init device_type(i)
@@ -93,4 +96,7 @@ program openacc_init_validity
9396
!ERROR: At most one DEVICE_TYPE clause can appear on the INIT directive
9497
!$acc init device_type(2) device_type(i, j)
9598

99+
!ERROR: Must have LOGICAL or INTEGER type
100+
!$acc init if(ifReal)
101+
96102
end program openacc_init_validity

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ def ACCC_Host : Clause<"host"> {
159159

160160
// 2.5.6
161161
def ACCC_If : Clause <"if"> {
162-
let flangClass = "ScalarLogicalExpr";
162+
let flangClass = "ScalarExpr";
163163
}
164164

165165
// 2.14.4

llvm/utils/TableGen/DirectiveEmitter.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,7 @@ static void GenerateFlangClausesParser(const DirectiveLanguage &DirLang,
736736
.Case("Name", "name")
737737
.Case("ScalarIntConstantExpr", "scalarIntConstantExpr")
738738
.Case("ScalarIntExpr", "scalarIntExpr")
739+
.Case("ScalarExpr", "scalarExpr")
739740
.Case("ScalarLogicalExpr", "scalarLogicalExpr")
740741
.Default(("Parser<" + Clause.getFlangClass() + ">{}")
741742
.toStringRef(Scratch));

0 commit comments

Comments
 (0)