[flang-commits] [flang] [flang] Fail fir.global conversion when the type has no LLVM equivalent (PR #232610)
via flang-commits
flang-commits at lists.llvm.org
Thu Oct 15 23:25:44 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-flang-codegen
Author: Keshav Vinayak Jha (keshavvinayak01)
<details>
<summary>Changes</summary>
`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
---
Full diff: https://github.com/llvm/llvm-project/pull/232610.diff
2 Files Affected:
- (modified) flang/lib/Optimizer/CodeGen/CodeGen.cpp (+3)
- (modified) flang/test/Fir/convert-to-llvm-invalid.fir (+8)
``````````diff
diff --git a/flang/lib/Optimizer/CodeGen/CodeGen.cpp b/flang/lib/Optimizer/CodeGen/CodeGen.cpp
index d72b1fb4a1d78..444abce0e173c 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 c7249eed0b4b3..bdab3332d5a38 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>
``````````
</details>
https://github.com/llvm/llvm-project/pull/232610
More information about the flang-commits
mailing list