[flang-commits] [flang] [flang] OPTIONAL char dummy has no defining op; add null check (PR #182582)

via flang-commits flang-commits at lists.llvm.org
Fri Feb 20 11:56:18 PST 2026


https://github.com/tmjbios created https://github.com/llvm/llvm-project/pull/182582

size.getDefiningOp() returns nullptr for block arguments when a OPTIONAL character length generated the conditional "fir.if". Check for a nullptr before calling mlir::isa<> to avoid the crash.

Addresses: https://github.com/llvm/llvm-project/issues/182436
Passes check-flang, check-flang-rt, and llvm-test-suite (x86_64)

>From fa03f86bf7991d2e1ab568cf7cec3271bab72d8d Mon Sep 17 00:00:00 2001
From: Ted Johnson <tedmjohnson at protonmail.com>
Date: Fri, 20 Feb 2026 13:43:07 -0600
Subject: [PATCH] [flang] OPTIONAL char has no defining op; add null check

size.getDefiningOp() returns nullptr for block arguments when
a OPTIONAL character length generated the conditional "fir.if".
Check for a nullptr before calling mlir::isa<> to avoid the crash.
---
 flang/lib/Optimizer/CodeGen/CodeGen.cpp | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/flang/lib/Optimizer/CodeGen/CodeGen.cpp b/flang/lib/Optimizer/CodeGen/CodeGen.cpp
index 93f3806b18648..dc4f11d8ada8c 100644
--- a/flang/lib/Optimizer/CodeGen/CodeGen.cpp
+++ b/flang/lib/Optimizer/CodeGen/CodeGen.cpp
@@ -319,7 +319,10 @@ struct AllocaOpConversion : public fir::FIROpConversion<fir::AllocaOp> {
     unsigned allocaAs = getAllocaAddressSpace(rewriter);
     unsigned programAs = getProgramAddressSpace(rewriter);
 
-    if (mlir::isa<mlir::LLVM::ConstantOp>(size.getDefiningOp())) {
+    // A value defined by a block arg, such as fir.if for a
+    // optional assumed character dummy len, doesn't have a defining op.
+    if (auto *defOp = size.getDefiningOp();
+        defOp && mlir::isa<mlir::LLVM::ConstantOp>(size.getDefiningOp())) {
       // Set the Block in which the llvm alloca should be inserted.
       mlir::Operation *parentOp = rewriter.getInsertionBlock()->getParentOp();
       mlir::Region *parentRegion = rewriter.getInsertionBlock()->getParent();



More information about the flang-commits mailing list