[flang-commits] [flang] [flang] Implemented a warning about contiguity of compile time constant values (PR #161084)

Eugene Epshteyn via flang-commits flang-commits at lists.llvm.org
Sun Sep 28 07:46:11 PDT 2025


https://github.com/eugeneepshteyn updated https://github.com/llvm/llvm-project/pull/161084

>From b380c1486ee262c78726d0f3f3c769a7a4ed3dfe Mon Sep 17 00:00:00 2001
From: Eugene Epshteyn <eepshteyn at nvidia.com>
Date: Sat, 27 Sep 2025 23:33:30 -0400
Subject: [PATCH 1/3] [flang] Implemented a warning about contiguity of compile
 time constant values

Implemented common::UsageWarning::ConstantIsContiguous to warn about the
following case:
```
integer, parameter :: num = 3
integer, parameter :: arr(num)=[(i, i=1,num)]
logical, parameter :: result=is_contiguous(arr(num:1:-1))
end
```
Here, while array section is discontiguous, `arr` is a compile time constant,
so array section created at compile time will end up being contiguous and
`result` will be "true". If `arr` wasn't a constant, the result at runtime
would have been "false".
---
 flang/include/flang/Support/Fortran-features.h | 2 +-
 flang/lib/Evaluate/fold-logical.cpp            | 9 +++++++++
 2 files changed, 10 insertions(+), 1 deletion(-)

diff --git a/flang/include/flang/Support/Fortran-features.h b/flang/include/flang/Support/Fortran-features.h
index 2bbc2385777da..51364d552be64 100644
--- a/flang/include/flang/Support/Fortran-features.h
+++ b/flang/include/flang/Support/Fortran-features.h
@@ -76,7 +76,7 @@ ENUM_CLASS(UsageWarning, Portability, PointerToUndefinable,
     IndexVarRedefinition, IncompatibleImplicitInterfaces, CdefinedInit,
     VectorSubscriptFinalization, UndefinedFunctionResult, UselessIomsg,
     MismatchingDummyProcedure, SubscriptedEmptyArray, UnsignedLiteralTruncation,
-    CompatibleDeclarationsFromDistinctModules,
+    CompatibleDeclarationsFromDistinctModules, ConstantIsContiguous,
     NullActualForDefaultIntentAllocatable, UseAssociationIntoSameNameSubprogram,
     HostAssociatedIntentOutInSpecExpr, NonVolatilePointerToVolatile,
     RealConstantWidening, VolatileOrAsynchronousTemporary)
diff --git a/flang/lib/Evaluate/fold-logical.cpp b/flang/lib/Evaluate/fold-logical.cpp
index c64f79e06a8ac..705902aa3d8ed 100644
--- a/flang/lib/Evaluate/fold-logical.cpp
+++ b/flang/lib/Evaluate/fold-logical.cpp
@@ -799,12 +799,21 @@ Expr<Type<TypeCategory::Logical, KIND>> FoldIntrinsicFunction(
     }
   } else if (name == "is_contiguous") {
     if (args.at(0)) {
+      auto warnContiguous = [&]() {
+        if (auto source{args[0]->sourceLocation()}) {
+          context.Warn(common::UsageWarning::ConstantIsContiguous,
+              *source,
+              "constant values constructed at compile time are likely to be contiguous"_warn_en_US);
+        }
+      };
       if (auto *expr{args[0]->UnwrapExpr()}) {
         if (auto contiguous{IsContiguous(*expr, context)}) {
+          warnContiguous();
           return Expr<T>{*contiguous};
         }
       } else if (auto *assumedType{args[0]->GetAssumedTypeDummy()}) {
         if (auto contiguous{IsContiguous(*assumedType, context)}) {
+          warnContiguous();
           return Expr<T>{*contiguous};
         }
       }

>From fb8269dc65c31b9b148b3ada11ea8e5c765cc4f6 Mon Sep 17 00:00:00 2001
From: Eugene Epshteyn <eepshteyn at nvidia.com>
Date: Sat, 27 Sep 2025 23:43:29 -0400
Subject: [PATCH 2/3] clang-format

---
 flang/lib/Evaluate/fold-logical.cpp | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/flang/lib/Evaluate/fold-logical.cpp b/flang/lib/Evaluate/fold-logical.cpp
index 705902aa3d8ed..4bb1dc99ebec4 100644
--- a/flang/lib/Evaluate/fold-logical.cpp
+++ b/flang/lib/Evaluate/fold-logical.cpp
@@ -801,8 +801,7 @@ Expr<Type<TypeCategory::Logical, KIND>> FoldIntrinsicFunction(
     if (args.at(0)) {
       auto warnContiguous = [&]() {
         if (auto source{args[0]->sourceLocation()}) {
-          context.Warn(common::UsageWarning::ConstantIsContiguous,
-              *source,
+          context.Warn(common::UsageWarning::ConstantIsContiguous, *source,
               "constant values constructed at compile time are likely to be contiguous"_warn_en_US);
         }
       };

>From a74f608dd2648252432089f4b2d8f6da101fe709 Mon Sep 17 00:00:00 2001
From: Eugene Epshteyn <eepshteyn at nvidia.com>
Date: Sat, 27 Sep 2025 23:47:50 -0400
Subject: [PATCH 3/3] added test

---
 flang/test/Semantics/contiguous-warn.f90 | 6 ++++++
 1 file changed, 6 insertions(+)
 create mode 100644 flang/test/Semantics/contiguous-warn.f90

diff --git a/flang/test/Semantics/contiguous-warn.f90 b/flang/test/Semantics/contiguous-warn.f90
new file mode 100644
index 0000000000000..5d87089a36778
--- /dev/null
+++ b/flang/test/Semantics/contiguous-warn.f90
@@ -0,0 +1,6 @@
+! RUN: %python %S/test_errors.py %s %flang_fc1 -pedantic -Werror
+integer, parameter :: num = 3
+integer, parameter :: arr(num)=[(i, i=1,num)]
+!WARNING: constant values constructed at compile time are likely to be contiguous [-Wconstant-is-contiguous]
+logical, parameter :: result=is_contiguous(arr(num:1:-1))
+end



More information about the flang-commits mailing list