Skip to content

Conversation

@ceseo
Copy link
Contributor

@ceseo ceseo commented Jul 31, 2025

Properly terminate the StatementContext cleanup when a function return value is undefined.

Fixes #126452

@ceseo ceseo requested a review from tblah July 31, 2025 18:59
@llvmbot llvmbot added flang Flang issues not falling into any other category flang:fir-hlfir labels Jul 31, 2025
@llvmbot
Copy link
Member

llvmbot commented Jul 31, 2025

@llvm/pr-subscribers-flang-fir-hlfir

Author: Carlos Seo (ceseo)

Changes

Properly terminate the StatementContext cleanup when a function return value is undefined.

Fixes #126452


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

2 Files Affected:

  • (modified) flang/lib/Lower/Bridge.cpp (+7)
  • (added) flang/test/Lower/undef-func-result.f90 (+8)
diff --git a/flang/lib/Lower/Bridge.cpp b/flang/lib/Lower/Bridge.cpp
index 1adfb96ab9a98..802bdae9a5f05 100644
--- a/flang/lib/Lower/Bridge.cpp
+++ b/flang/lib/Lower/Bridge.cpp
@@ -1733,6 +1733,13 @@ class FirConverter : public Fortran::lower::AbstractConverter {
     mlir::Location loc = toLocation();
     if (!resultSymBox) {
       mlir::emitError(loc, "internal error when processing function return");
+      // Create a dummy undefined value of the expected return type.
+      // This prevents improper cleanup of StatementContext, which would lead
+      // to a crash due to a block with no terminator. See issue #126452.
+      mlir::FunctionType funcType = builder->getFunction().getFunctionType();
+      mlir::Type resultType = funcType.getResult(0);
+      mlir::Value undefResult = builder->create<fir::UndefOp>(loc, resultType);
+      genExitRoutine(false, undefResult);
       return;
     }
     mlir::Value resultVal = resultSymBox.match(
diff --git a/flang/test/Lower/undef-func-result.f90 b/flang/test/Lower/undef-func-result.f90
new file mode 100644
index 0000000000000..e8f3c8d2cad7d
--- /dev/null
+++ b/flang/test/Lower/undef-func-result.f90
@@ -0,0 +1,8 @@
+! RUN: %python %S/test_errors.py %s %flang_fc1
+! XFAIL: *
+function s(x) result(i)
+!ERROR: internal error when processing function return
+integer::x
+procedure():: i
+end function
+end

Copy link
Contributor

@tblah tblah left a comment

Choose a reason for hiding this comment

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

Thanks for all of the bug fixes.

Properly terminate the StatementContext cleanup when a function return value is
undefined.

Fixes llvm#126452
@ceseo ceseo requested a review from tblah August 5, 2025 13:17
Copy link
Contributor

@tblah tblah left a comment

Choose a reason for hiding this comment

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

Thank you

@ceseo ceseo merged commit 47ef3d0 into llvm:main Aug 5, 2025
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

flang:fir-hlfir flang Flang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Flang] Compilation abnormally terminates when result-name of RESULT is not defined in function subprogram

3 participants