[flang-commits] [flang] [Flang][OpenMP] Fix crash on invalid atomic variable expressions (PR #173068)

Krish Gupta via flang-commits flang-commits at lists.llvm.org
Fri Dec 19 11:13:21 PST 2025


https://github.com/KrxGu updated https://github.com/llvm/llvm-project/pull/173068

>From c7d304c5b99fd9bad2a45681731036b40e903277 Mon Sep 17 00:00:00 2001
From: KrxGu <krishom70 at gmail.com>
Date: Sat, 20 Dec 2025 00:42:35 +0530
Subject: [PATCH] [Flang][OpenMP] Fix crash on invalid atomic variable
 expressions

Fixes #169484

The compiler crashed with an assertion failure when CheckAtomicVariable
encountered expressions without designators (e.g., function references).

The assertion assumed GetAllDesignators() always returns exactly one
designator, but it returns an empty vector for function references.

This patch replaces the assertion with an early return when dsgs.size()
!= 1, allowing existing semantic checks to diagnose actual errors.
Procedure references are valid in atomic constructs if they return a
pointer to a scalar, so we avoid emitting premature diagnostics.

Added regression test to verify the crash is fixed.
---
 flang/lib/Semantics/check-omp-atomic.cpp      | 12 ++++++-
 .../OpenMP/atomic-invalid-variable.f90        | 32 +++++++++++++++++++
 2 files changed, 43 insertions(+), 1 deletion(-)
 create mode 100644 flang/test/Semantics/OpenMP/atomic-invalid-variable.f90

diff --git a/flang/lib/Semantics/check-omp-atomic.cpp b/flang/lib/Semantics/check-omp-atomic.cpp
index b9e34ca6e74df..9a4cb84ad3c2c 100644
--- a/flang/lib/Semantics/check-omp-atomic.cpp
+++ b/flang/lib/Semantics/check-omp-atomic.cpp
@@ -587,8 +587,18 @@ void OmpStructureChecker::CheckAtomicVariable(
   }
 
   std::vector<SomeExpr> dsgs{GetAllDesignators(atom)};
-  assert(dsgs.size() == 1 && "Should have a single top-level designator");
+
+  // Procedure references are valid if they return a pointer to a scalar.
+  // Just return if we don't have exactly one designator - other checks will
+  // diagnose any actual errors.
+  if (dsgs.size() != 1) {
+    return;
+  }
+
   evaluate::SymbolVector syms{evaluate::GetSymbolVector(dsgs.front())};
+  if (syms.empty()) {
+    return;
+  }
 
   CheckAtomicType(syms.back(), source, atom.AsFortran(), checkTypeOnPointer);
 
diff --git a/flang/test/Semantics/OpenMP/atomic-invalid-variable.f90 b/flang/test/Semantics/OpenMP/atomic-invalid-variable.f90
new file mode 100644
index 0000000000000..32527d3cfd51d
--- /dev/null
+++ b/flang/test/Semantics/OpenMP/atomic-invalid-variable.f90
@@ -0,0 +1,32 @@
+! RUN: %python %S/../test_errors.py %s %flang_fc1 -fopenmp
+! Test for issue #169484: Flang should not crash on invalid atomic variables
+! This test verifies that proper diagnostics are emitted for invalid atomic
+! constructs instead of crashing the compiler.
+
+subroutine test_atomic_invalid(a, b, i)
+  integer :: i, a, b
+  interface
+    function baz(i) result(res)
+      integer :: i, res
+    end function
+  end interface
+  
+  ! Valid atomic update - should work fine
+  !$omp atomic
+    b = b + a
+  
+  ! Invalid: z is undeclared, so z(1) is treated as a function reference
+  ! This should emit an error, not crash
+  !$omp atomic
+    !ERROR: Left-hand side of assignment is not definable
+    !ERROR: 'z(1_4)' is not a variable or pointer
+    z(1) = z(1) + 1
+  
+  ! Invalid: baz(i) is a function reference, not a variable
+  ! This should emit an error, not crash
+  !$omp atomic
+    !ERROR: Left-hand side of assignment is not definable
+    !ERROR: 'baz(i)' is not a variable or pointer
+    !ERROR: This is not a valid ATOMIC UPDATE operation
+    baz(i) = 1
+end subroutine



More information about the flang-commits mailing list