[flang-commits] [flang] [flang] Fail fir.global conversion when the type has no LLVM equivalent (PR #232610)
Keshav Vinayak Jha via flang-commits
flang-commits at lists.llvm.org
Thu Oct 15 23:25:07 PDT 2026
https://github.com/keshavvinayak01 created https://github.com/llvm/llvm-project/pull/232610
`GlobalOpConversion` passed the result of `convertType` straight to `LLVM::GlobalOp::create`. For a global whose type the FIR type converter cannot lower, such as tensor<10xi32>, that result is null and TypeAttr::get segfaults.
To reproduce:
Run: `fir-opt test.mlir --canonicalize --lower-hlfir-ordered-assignments --lower-hlfir-intrinsics --bufferize-hlfir --convert-hlfir-to-fir --cfg-conversion --fir-to-llvm-ir -o /dev/null`
Input contains:
```
fir.global internal @_QFEnonvolatile_array : tensor<10xi32> {
%0 = fir.zero_bits !fir.array<10xi32>
fir.has_value %0 : !fir.array<10xi32>
}
```
Signal: SIGSEGV. Stack: `GlobalOpConversion` → `LLVM::GlobalOp::create` → `TypeAttr::get` → `Type::getContext`.
Return a match failure instead so the conversion reports that `fir.global` could not be legalized.
Assisted-by: Claude code
>From 581a6be6ced236c2691f731a34250cfdf44fb71f Mon Sep 17 00:00:00 2001
From: Keshav Vinayak Jha <keshavvinayakjha at gmail.com>
Date: Fri, 16 Oct 2026 05:29:25 +0000
Subject: [PATCH] [flang] Fail fir.global conversion when the type has no LLVM
equivalent
GlobalOpConversion passed the result of convertType straight to
LLVM::GlobalOp::create. For a global whose type the FIR type converter
cannot lower, such as tensor<10xi32>, that result is null and
TypeAttr::get segfaults. Return a match failure instead so the
conversion reports that fir.global could not be legalized.
Co-authored-by: Claude Opus 5.5 <noreply at anthropic.com>
Signed-off-by: Keshav Vinayak Jha <keshavvinayakjha at gmail.com>
---
flang/lib/Optimizer/CodeGen/CodeGen.cpp | 3 +++
flang/test/Fir/convert-to-llvm-invalid.fir | 8 ++++++++
2 files changed, 11 insertions(+)
diff --git a/flang/lib/Optimizer/CodeGen/CodeGen.cpp b/flang/lib/Optimizer/CodeGen/CodeGen.cpp
index d72b1fb4a1d783..444abce0e173c9 100644
--- a/flang/lib/Optimizer/CodeGen/CodeGen.cpp
+++ b/flang/lib/Optimizer/CodeGen/CodeGen.cpp
@@ -3875,6 +3875,9 @@ struct GlobalOpConversion : public fir::FIROpConversion<fir::GlobalOp> {
auto tyAttr = convertType(global.getType());
if (auto boxType = mlir::dyn_cast<fir::BaseBoxType>(global.getType()))
tyAttr = this->lowerTy().convertBoxTypeAsStruct(boxType);
+ if (!tyAttr)
+ return rewriter.notifyMatchFailure(
+ global, "global type cannot be converted to an LLVM type");
auto loc = global.getLoc();
mlir::Attribute initAttr = global.getInitVal().value_or(mlir::Attribute());
assert(attributeTypeIsCompatible(global.getContext(), initAttr, tyAttr));
diff --git a/flang/test/Fir/convert-to-llvm-invalid.fir b/flang/test/Fir/convert-to-llvm-invalid.fir
index c7249eed0b4b39..bdab3332d5a386 100644
--- a/flang/test/Fir/convert-to-llvm-invalid.fir
+++ b/flang/test/Fir/convert-to-llvm-invalid.fir
@@ -118,3 +118,11 @@ func.func @coordinate_of_dynamic_array(%arg0: !fir.ref<!fir.array<1x!fir.char<4,
%p = fir.coordinate_of %arg0, %arg1 : (!fir.ref<!fir.array<1x!fir.char<4,?>>>, index) -> !fir.ref<f32>
return
}
+
+// -----
+
+// Test `fir.global` conversion failure because the global type (tensor) has no
+// LLVM equivalent.
+
+// expected-error at +1{{failed to legalize operation 'fir.global'}}
+fir.global @tensor_global : tensor<10xi32>
More information about the flang-commits
mailing list