[flang-commits] [flang] [flang] Recognize SIMPLE prefix in purity checks (PR #232175)

Šárka Holendová via flang-commits flang-commits at lists.llvm.org
Sun Oct 25 14:48:48 PDT 2026


https://github.com/mlir-maiden updated https://github.com/llvm/llvm-project/pull/232175

>From 4b4435d649d6e0e7e1ecc825a25c515e8a20b127 Mon Sep 17 00:00:00 2001
From: Sarka Holendova <sarka.holendova at gmail.com>
Date: Wed, 14 Oct 2026 21:21:29 -0700
Subject: [PATCH 1/2] [flang] Recognize SIMPLE prefix in purity checks

Treat `SIMPLE` as a `PURE` prefix in `PurityChecker::HasPurePrefix`,
as required by F2023 15.8 p2.

Add regression tests for image control statements (C15109)
and internal subprogram purity (C15102).

Fixes #231353
---
 flang/lib/Semantics/check-purity.cpp          |  1 +
 flang/test/Semantics/simple-purity-checks.f90 | 28 +++++++++++++++++++
 .../Semantics/simple-purity-image-control.f90 |  7 +++++
 3 files changed, 36 insertions(+)
 create mode 100644 flang/test/Semantics/simple-purity-checks.f90
 create mode 100644 flang/test/Semantics/simple-purity-image-control.f90

diff --git a/flang/lib/Semantics/check-purity.cpp b/flang/lib/Semantics/check-purity.cpp
index b327282f390e51..59bf48aba93d23 100644
--- a/flang/lib/Semantics/check-purity.cpp
+++ b/flang/lib/Semantics/check-purity.cpp
@@ -48,6 +48,7 @@ bool PurityChecker::HasPurePrefix(
     if (std::holds_alternative<parser::PrefixSpec::Impure>(prefix.u)) {
       return false;
     } else if (std::holds_alternative<parser::PrefixSpec::Pure>(prefix.u) ||
+        std::holds_alternative<parser::PrefixSpec::Simple>(prefix.u) ||
         std::holds_alternative<parser::PrefixSpec::Elemental>(prefix.u)) {
       result = true;
     }
diff --git a/flang/test/Semantics/simple-purity-checks.f90 b/flang/test/Semantics/simple-purity-checks.f90
new file mode 100644
index 00000000000000..92ba1e3c5e3b03
--- /dev/null
+++ b/flang/test/Semantics/simple-purity-checks.f90
@@ -0,0 +1,28 @@
+! RUN: %python %S/test_errors.py %s %flang_fc1
+
+! F2023 15.8 p2: A SIMPLE procedure is also PURE.
+! F2023 C15102: An internal subprogram of a pure subprogram must be pure
+
+! A SIMPLE subprogram may not contain an impure internal subprogram
+! C15113 additionally requires the internal subprogram to be SIMPLE
+simple subroutine s(x)
+  integer, intent(inout) :: x
+  x = x + 1
+contains
+  !ERROR: An internal subprogram of a pure subprogram must also be pure
+  subroutine inner(y)
+    integer, intent(inout) :: y
+    y = y + 1
+  end subroutine
+end subroutine
+
+! A SIMPLE internal subprogram satisfies the PURE requirement
+pure subroutine t(x)
+  integer, intent(inout) :: x
+  call inner(x)
+contains
+  simple subroutine inner(y)
+    integer, intent(inout) :: y
+    y = y + 1
+  end subroutine
+end subroutine
\ No newline at end of file
diff --git a/flang/test/Semantics/simple-purity-image-control.f90 b/flang/test/Semantics/simple-purity-image-control.f90
new file mode 100644
index 00000000000000..c5b9390b16002d
--- /dev/null
+++ b/flang/test/Semantics/simple-purity-image-control.f90
@@ -0,0 +1,7 @@
+! RUN: %python %S/test_errors.py %s %flang_fc1 -fcoarray
+
+! F2023 15.8 p2: A SIMPLE procedure is also PURE
+simple subroutine s()
+  !ERROR: An image control statement may not appear in a pure subprogram
+  sync all
+end subroutine
\ No newline at end of file

>From b9bfda5ef4aa79803601b083f0e23f44fd31b56b Mon Sep 17 00:00:00 2001
From: Sarka Holendova <sarka.holendova at gmail.com>
Date: Sun, 25 Oct 2026 14:48:18 -0700
Subject: [PATCH 2/2] [flang] Address review feedback on SIMPLE purity tests

---
 flang/test/Semantics/simple-purity-checks.f90 | 26 ++++++++++++++++++-
 .../Semantics/simple-purity-image-control.f90 |  7 -----
 2 files changed, 25 insertions(+), 8 deletions(-)
 delete mode 100644 flang/test/Semantics/simple-purity-image-control.f90

diff --git a/flang/test/Semantics/simple-purity-checks.f90 b/flang/test/Semantics/simple-purity-checks.f90
index 92ba1e3c5e3b03..f7934a3bc29f90 100644
--- a/flang/test/Semantics/simple-purity-checks.f90
+++ b/flang/test/Semantics/simple-purity-checks.f90
@@ -25,4 +25,28 @@ simple subroutine inner(y)
     integer, intent(inout) :: y
     y = y + 1
   end subroutine
-end subroutine
\ No newline at end of file
+end subroutine
+
+! A SIMPLE internal function of an impure host is pure (C15109 applies inside
+! it only); its impure sibling and the following external subprogram are not
+subroutine host()
+contains
+  simple integer function f()
+    !ERROR: An image control statement may not appear in a pure subprogram
+    sync all
+    f = 0
+  end function
+  subroutine sibling()
+    sync all
+  end subroutine
+end subroutine
+
+subroutine following()
+  sync all
+end subroutine
+
+! F2023 C15109: An image control statement may not appear in a SIMPLE subprogram
+simple subroutine simple_image_control()
+  !ERROR: An image control statement may not appear in a pure subprogram
+  sync all
+end subroutine
diff --git a/flang/test/Semantics/simple-purity-image-control.f90 b/flang/test/Semantics/simple-purity-image-control.f90
deleted file mode 100644
index c5b9390b16002d..00000000000000
--- a/flang/test/Semantics/simple-purity-image-control.f90
+++ /dev/null
@@ -1,7 +0,0 @@
-! RUN: %python %S/test_errors.py %s %flang_fc1 -fcoarray
-
-! F2023 15.8 p2: A SIMPLE procedure is also PURE
-simple subroutine s()
-  !ERROR: An image control statement may not appear in a pure subprogram
-  sync all
-end subroutine
\ No newline at end of file



More information about the flang-commits mailing list