Skip to content

Conversation

eugeneepshteyn
Copy link
Contributor

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".

…nt 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".
@eugeneepshteyn eugeneepshteyn marked this pull request as ready for review September 29, 2025 12:38
@llvmbot llvmbot added flang Flang issues not falling into any other category flang:semantics labels Sep 29, 2025
@llvmbot
Copy link
Member

llvmbot commented Sep 29, 2025

@llvm/pr-subscribers-flang-semantics

Author: Eugene Epshteyn (eugeneepshteyn)

Changes

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".


Full diff: https://github.com/llvm/llvm-project/pull/161084.diff

3 Files Affected:

  • (modified) flang/include/flang/Support/Fortran-features.h (+1-1)
  • (modified) flang/lib/Evaluate/fold-logical.cpp (+8)
  • (added) flang/test/Semantics/contiguous-warn.f90 (+6)
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..4bb1dc99ebec4 100644
--- a/flang/lib/Evaluate/fold-logical.cpp
+++ b/flang/lib/Evaluate/fold-logical.cpp
@@ -799,12 +799,20 @@ 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};
         }
       }
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

}
} else if (name == "is_contiguous") {
if (args.at(0)) {
auto warnContiguous = [&]() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please always use braced initialization in everything before lowering or in the runtime.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The text of this message isn't really actionable; what should the programmer do?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Message updated.

@eugeneepshteyn eugeneepshteyn merged commit 4064c0e into llvm:main Sep 30, 2025
9 checks passed
@eugeneepshteyn eugeneepshteyn deleted the is-contiguous-const branch September 30, 2025 18:40
mahesh-attarde pushed a commit to mahesh-attarde/llvm-project that referenced this pull request Oct 3, 2025
…nt values (llvm#161084)

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".
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang:semantics flang Flang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants