[flang-commits] [flang] [flang] Implemented the RANK clause of an attr-spec, per the Fortran 2023 Standard (PR #176979)
via flang-commits
flang-commits at lists.llvm.org
Tue Jan 20 09:35:35 PST 2026
https://github.com/kwyatt-ext created https://github.com/llvm/llvm-project/pull/176979
This implements the RANK clause per the Fortran 2023 Standard. This includes both the parsing/semantics and un-parsing functionality. It is a fairly straight-forward change.
An executable test was added that tests the examples from the Fortran 2023 What's New document.
check-flang, check-flang-rt, and llvm-lit tests have been executed against this change.
>From d95c7aeb2f8ed66563498e41ae41643164930949 Mon Sep 17 00:00:00 2001
From: Kevin Wyatt <kwyatt at hpe.com>
Date: Fri, 16 Jan 2026 15:52:09 -0600
Subject: [PATCH 1/2] Implemented the RANK clause of an attr-spec, per the
Fortran 2023 Standard
---
flang/include/flang/Parser/dump-parse-tree.h | 1 +
flang/include/flang/Parser/parse-tree.h | 9 ++-
flang/lib/Parser/Fortran-parsers.cpp | 5 +-
flang/lib/Parser/unparse.cpp | 2 +
flang/lib/Semantics/resolve-names.cpp | 32 ++++++++++
flang/test/Semantics/rank-clause01.f90 | 64 ++++++++++++++++++++
6 files changed, 110 insertions(+), 3 deletions(-)
create mode 100644 flang/test/Semantics/rank-clause01.f90
diff --git a/flang/include/flang/Parser/dump-parse-tree.h b/flang/include/flang/Parser/dump-parse-tree.h
index f6e4cce241ad3..84c7b8d2a5349 100644
--- a/flang/include/flang/Parser/dump-parse-tree.h
+++ b/flang/include/flang/Parser/dump-parse-tree.h
@@ -843,6 +843,7 @@ class ParseTreeDumper {
NODE(parser, ProgramUnit)
NODE(parser, Protected)
NODE(parser, ProtectedStmt)
+ NODE(parser, RankClause)
NODE(parser, ReadStmt)
NODE(parser, RealLiteralConstant)
NODE(RealLiteralConstant, Real)
diff --git a/flang/include/flang/Parser/parse-tree.h b/flang/include/flang/Parser/parse-tree.h
index 37c0f699361eb..1a53591e4f89f 100644
--- a/flang/include/flang/Parser/parse-tree.h
+++ b/flang/include/flang/Parser/parse-tree.h
@@ -1353,12 +1353,17 @@ struct IntentSpec {
WRAPPER_CLASS_BOILERPLATE(IntentSpec, Intent);
};
+// F2023_R829 rank-clause ->
+// scalar-int-constant-expr
+WRAPPER_CLASS(RankClause, ScalarIntConstantExpr);
+
// R802 attr-spec ->
// access-spec | ALLOCATABLE | ASYNCHRONOUS |
// CODIMENSION lbracket coarray-spec rbracket | CONTIGUOUS |
// DIMENSION ( array-spec ) | EXTERNAL | INTENT ( intent-spec ) |
// INTRINSIC | language-binding-spec | OPTIONAL | PARAMETER | POINTER |
-// PROTECTED | SAVE | TARGET | VALUE | VOLATILE |
+// PROTECTED | RANK ( scalar-int-constant-expr ) | SAVE | TARGET |
+// VALUE | VOLATILE |
// (CUDA) CONSTANT | DEVICE | MANAGED | PINNED | SHARED | TEXTURE
EMPTY_CLASS(Asynchronous);
EMPTY_CLASS(External);
@@ -1374,7 +1379,7 @@ struct AttrSpec {
UNION_CLASS_BOILERPLATE(AttrSpec);
std::variant<AccessSpec, Allocatable, Asynchronous, CoarraySpec, Contiguous,
ArraySpec, External, IntentSpec, Intrinsic, LanguageBindingSpec, Optional,
- Parameter, Pointer, Protected, Save, Target, Value, Volatile,
+ Parameter, Pointer, Protected, RankClause, Save, Target, Value, Volatile,
common::CUDADataAttr>
u;
};
diff --git a/flang/lib/Parser/Fortran-parsers.cpp b/flang/lib/Parser/Fortran-parsers.cpp
index 988db5450abc9..2a89d43b8d4f6 100644
--- a/flang/lib/Parser/Fortran-parsers.cpp
+++ b/flang/lib/Parser/Fortran-parsers.cpp
@@ -700,7 +700,8 @@ TYPE_PARSER(
// CODIMENSION lbracket coarray-spec rbracket | CONTIGUOUS |
// DIMENSION ( array-spec ) | EXTERNAL | INTENT ( intent-spec ) |
// INTRINSIC | language-binding-spec | OPTIONAL | PARAMETER | POINTER |
-// PROTECTED | SAVE | TARGET | VALUE | VOLATILE |
+// PROTECTED | RANK ( scalar-int-constant-expr ) | SAVE | TARGET |
+// VALUE | VOLATILE |
// CUDA-data-attr
TYPE_PARSER(construct<AttrSpec>(accessSpec) ||
construct<AttrSpec>(allocatable) ||
@@ -714,6 +715,8 @@ TYPE_PARSER(construct<AttrSpec>(accessSpec) ||
construct<AttrSpec>(languageBindingSpec) || construct<AttrSpec>(optional) ||
construct<AttrSpec>(construct<Parameter>("PARAMETER"_tok)) ||
construct<AttrSpec>(pointer) || construct<AttrSpec>(protectedAttr) ||
+ construct<AttrSpec>("RANK" >> construct<RankClause>(
+ parenthesized(scalarIntConstantExpr))) ||
construct<AttrSpec>(save) ||
construct<AttrSpec>(construct<Target>("TARGET"_tok)) ||
construct<AttrSpec>(construct<Value>("VALUE"_tok)) ||
diff --git a/flang/lib/Parser/unparse.cpp b/flang/lib/Parser/unparse.cpp
index 9b31454537df5..c88cb323e820f 100644
--- a/flang/lib/Parser/unparse.cpp
+++ b/flang/lib/Parser/unparse.cpp
@@ -508,6 +508,7 @@ class UnparseVisitor {
common::visit(common::visitors{
[&](const CoarraySpec &) { Word("CODIMENSION["); },
[&](const ArraySpec &) { Word("DIMENSION("); },
+ [&](const RankClause &) { Word("RANK("); },
[](const auto &) {},
},
x.u);
@@ -516,6 +517,7 @@ class UnparseVisitor {
common::visit(common::visitors{
[&](const CoarraySpec &) { Put(']'); },
[&](const ArraySpec &) { Put(')'); },
+ [&](const RankClause &) { Put(')'); },
[](const auto &) {},
},
x.u);
diff --git a/flang/lib/Semantics/resolve-names.cpp b/flang/lib/Semantics/resolve-names.cpp
index 310f7ab97e9a0..6301ff21a7c5d 100644
--- a/flang/lib/Semantics/resolve-names.cpp
+++ b/flang/lib/Semantics/resolve-names.cpp
@@ -14,6 +14,7 @@
#include "resolve-directives.h"
#include "resolve-names-utils.h"
#include "rewrite-parse-tree.h"
+#include "flang/Common/Fortran-consts.h"
#include "flang/Common/indirection.h"
#include "flang/Common/restorer.h"
#include "flang/Common/visit.h"
@@ -445,6 +446,7 @@ class ImplicitRulesVisitor : public DeclTypeSpecVisitor {
// 6. POINTER(p,x(10))
class ArraySpecVisitor : public virtual BaseVisitor {
public:
+ void Post(const parser::RankClause &); // Should this be a void?
void Post(const parser::ArraySpec &);
void Post(const parser::ComponentArraySpec &);
void Post(const parser::CoarraySpec &);
@@ -2723,6 +2725,36 @@ bool ImplicitRulesVisitor::HandleImplicitNone(
// ArraySpecVisitor implementation
+void ArraySpecVisitor::Post(const parser::RankClause &x) {
+ // RANK(n) is equivalent to DIMENSION with n deferred shape specs (:)
+ CHECK(arraySpec_.empty());
+ // Evaluate the rank value (must be a constant expression)
+ // if (auto rank{evaluate::ToInt64(EvaluateInt64(context(), x.v))}) {
+ if (auto rank{EvaluateInt64(context(), x.v)}) {
+ if (*rank < 0 || *rank > common::maxRank) {
+
+ // Say(x.v.thing.thing.value().source,
+ // Say(x.v.source,
+ // "RANK value (%lld) must be between 0 and %d"_err_en_US,
+ // static_cast<long long>(*rank), common::maxRank);
+ // currStmtSource().value()
+ Say("RANK value (%lld) must be between 0 and %d"_err_en_US);
+ // static_cast<long long>(*rank), common::maxRank);
+ } else { // KKW: I'm not sure about this pushback... it might be ok since it is Pre, but I'm suspect...
+ // Create n deferred shape specs (:)
+ for (int i = 0; i < *rank; ++i) {
+ arraySpec_.push_back(ShapeSpec::MakeDeferred());
+ }
+ }
+ } else {
+ Say("RANK value must be a constant expression"_err_en_US);
+ // Say(x.source,
+ // "RANK value must be a constant expression"_err_en_US);
+ // Say(x.v.thing.thing.value().source,
+ // "RANK value must be a constant expression"_err_en_US);
+ }
+}
+
void ArraySpecVisitor::Post(const parser::ArraySpec &x) {
CHECK(arraySpec_.empty());
arraySpec_ = AnalyzeArraySpec(context(), x);
diff --git a/flang/test/Semantics/rank-clause01.f90 b/flang/test/Semantics/rank-clause01.f90
new file mode 100644
index 0000000000000..40ba470cb4168
--- /dev/null
+++ b/flang/test/Semantics/rank-clause01.f90
@@ -0,0 +1,64 @@
+! Test the new RANK clause. This uses the examples from the F2023 Standard and
+! related explanation documents.
+!
+! RUN: %flang %s -o %t
+! RUN: env LD_LIBRARY_PATH="$LD_LIBRARY_PATH:%libdir" %t | FileCheck %s
+! CHECK-NOT: FAIL
+
+program rank_clause01
+ implicit none
+
+ logical :: X0(10,10,10)
+ integer :: array1(10,10)
+
+ interface
+
+ subroutine sub02(arg1)
+ integer, rank(2) :: arg1
+ end subroutine
+
+ subroutine new(arg1)
+ integer, rank(2) :: arg1
+ end subroutine
+
+ end interface
+
+ call sub01(X0)
+
+ call sub02(array1)
+
+ call new(array1)
+
+ contains
+
+ subroutine sub01(X3)
+
+ integer :: X0(10,10,10)
+ logical, rank(rank(X0)), allocatable :: X1 ! Rank 3, deferred shape
+ complex, rank(2), pointer :: X2 ! Rank 2, deferred-shape
+ logical, rank(rank(X0)) :: X3 ! Rank 3, assumed-shape
+ real, rank(0) :: X4 ! Scalar
+ allocatable :: X4
+
+ if (rank(X1) == 3 .and. rank(X2) == 2 .and. rank(X3) == 3 .and. &
+ rank(X4) == 0) then
+ print *, "PASS"
+ else
+ print *, "FAIL"
+ endif
+
+ end subroutine
+
+end program
+
+subroutine sub02(A)
+ integer, rank(2) :: A, B
+ entry new(B)
+
+ if (rank(A) == rank(B)) then
+ print *, "PASS"
+ else
+ print *, "FAIL"
+ endif
+
+end subroutine
>From 782248234650941f7d152174c95e78c0bc060dc9 Mon Sep 17 00:00:00 2001
From: Kevin Wyatt <kwyatt at hpe.com>
Date: Tue, 20 Jan 2026 11:21:02 -0600
Subject: [PATCH 2/2] [flang] Added the RANK clause from the F2023 Standard
---
flang/lib/Semantics/resolve-names.cpp | 21 ++++++---------------
1 file changed, 6 insertions(+), 15 deletions(-)
diff --git a/flang/lib/Semantics/resolve-names.cpp b/flang/lib/Semantics/resolve-names.cpp
index 6301ff21a7c5d..5bf690c10c064 100644
--- a/flang/lib/Semantics/resolve-names.cpp
+++ b/flang/lib/Semantics/resolve-names.cpp
@@ -446,7 +446,7 @@ class ImplicitRulesVisitor : public DeclTypeSpecVisitor {
// 6. POINTER(p,x(10))
class ArraySpecVisitor : public virtual BaseVisitor {
public:
- void Post(const parser::RankClause &); // Should this be a void?
+ void Post(const parser::RankClause &);
void Post(const parser::ArraySpec &);
void Post(const parser::ComponentArraySpec &);
void Post(const parser::CoarraySpec &);
@@ -2728,19 +2728,14 @@ bool ImplicitRulesVisitor::HandleImplicitNone(
void ArraySpecVisitor::Post(const parser::RankClause &x) {
// RANK(n) is equivalent to DIMENSION with n deferred shape specs (:)
CHECK(arraySpec_.empty());
+
// Evaluate the rank value (must be a constant expression)
- // if (auto rank{evaluate::ToInt64(EvaluateInt64(context(), x.v))}) {
if (auto rank{EvaluateInt64(context(), x.v)}) {
if (*rank < 0 || *rank > common::maxRank) {
-
- // Say(x.v.thing.thing.value().source,
- // Say(x.v.source,
- // "RANK value (%lld) must be between 0 and %d"_err_en_US,
- // static_cast<long long>(*rank), common::maxRank);
- // currStmtSource().value()
- Say("RANK value (%lld) must be between 0 and %d"_err_en_US);
- // static_cast<long long>(*rank), common::maxRank);
- } else { // KKW: I'm not sure about this pushback... it might be ok since it is Pre, but I'm suspect...
+ Say(currStmtSource().value(),
+ "RANK value (%lld) must be between 0 and %d"_err_en_US,
+ static_cast<long long>(*rank), common::maxRank);
+ } else {
// Create n deferred shape specs (:)
for (int i = 0; i < *rank; ++i) {
arraySpec_.push_back(ShapeSpec::MakeDeferred());
@@ -2748,10 +2743,6 @@ void ArraySpecVisitor::Post(const parser::RankClause &x) {
}
} else {
Say("RANK value must be a constant expression"_err_en_US);
- // Say(x.source,
- // "RANK value must be a constant expression"_err_en_US);
- // Say(x.v.thing.thing.value().source,
- // "RANK value must be a constant expression"_err_en_US);
}
}
More information about the flang-commits
mailing list