Skip to content

Conversation

eugeneepshteyn
Copy link
Contributor

Added extra checks to fix the crash.

Fixes #156167

@eugeneepshteyn eugeneepshteyn marked this pull request as ready for review September 2, 2025 01:42
@llvmbot llvmbot added flang Flang issues not falling into any other category flang:semantics labels Sep 2, 2025
@llvmbot
Copy link
Member

llvmbot commented Sep 2, 2025

@llvm/pr-subscribers-flang-semantics

Author: Eugene Epshteyn (eugeneepshteyn)

Changes

Added extra checks to fix the crash.

Fixes #156167


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

2 Files Affected:

  • (modified) flang/lib/Semantics/check-call.cpp (+3-2)
  • (modified) flang/test/Semantics/reduce01.f90 (+4)
diff --git a/flang/lib/Semantics/check-call.cpp b/flang/lib/Semantics/check-call.cpp
index a9cfe4d059982..f0078fda3600c 100644
--- a/flang/lib/Semantics/check-call.cpp
+++ b/flang/lib/Semantics/check-call.cpp
@@ -1985,8 +1985,9 @@ static void CheckReduce(
       }
     }
   }
-  const auto *result{
-      procChars ? procChars->functionResult->GetTypeAndShape() : nullptr};
+  const auto *result{procChars && procChars->functionResult
+          ? procChars->functionResult->GetTypeAndShape()
+          : nullptr};
   if (!procChars || !procChars->IsPure() ||
       procChars->dummyArguments.size() != 2 || !procChars->functionResult) {
     messages.Say(
diff --git a/flang/test/Semantics/reduce01.f90 b/flang/test/Semantics/reduce01.f90
index ad63a42d73cae..3384366db812f 100644
--- a/flang/test/Semantics/reduce01.f90
+++ b/flang/test/Semantics/reduce01.f90
@@ -71,6 +71,8 @@ pure function f11(x,y) result(res)
     type(pdt(max(x%len, y%len))) :: res
     res%ch = x%ch // y%ch
   end function
+  subroutine bad_reduce
+  end subroutine
 
   subroutine errors
     real :: a(10,10), b, c(10)
@@ -115,6 +117,8 @@ subroutine errors
     !ERROR: MASK= has no .TRUE. element, so IDENTITY= must be present
     b = reduce(a, f, reshape([(j > 100, j=1, 100)], shape(a)))
     b = reduce(a, f, reshape([(j == 50, j=1, 100)], shape(a))) ! ok
+    !ERROR: OPERATION= argument of REDUCE() must be a pure function of two data arguments
+    b = reduce(a, bad_reduce)
   end subroutine
   subroutine not_errors
     type(pdt(10)) :: a(10), b

@eugeneepshteyn eugeneepshteyn merged commit 2364736 into llvm:main Sep 2, 2025
13 checks passed
@eugeneepshteyn eugeneepshteyn deleted the reduce-crash-fix branch September 2, 2025 17:54
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.

[Flang] crashes with assertion failure when compiling reduce intrinsic
3 participants