[flang-commits] [flang] [flang][debug] Add fake use ops for dynamic array dimension variables (PR #200061)

Abid Qadeer via flang-commits flang-commits at lists.llvm.org
Wed Jun 17 10:51:08 PDT 2026


================
@@ -331,6 +331,60 @@ void AddDebugInfoPass::handleLocalVariable(Op declOp, llvm::StringRef name,
   auto tyAttr =
       typeGen.convertType(typeToConvert, fileAttr, scopeAttr, typeGenDeclOp);
 
+  // Check if tyAttr represents a dynamic Fortran array. If so, create
+  // FakeUseOps to preserve the automatic variables that represent the lower
+  // bound, count, and stride.
+  if (emitFakeUseForDebugVars) {
+    if (auto arrayTy =
+            mlir::dyn_cast<mlir::LLVM::DICompositeTypeAttr>(tyAttr)) {
+      if (arrayTy.getTag() == llvm::dwarf::DW_TAG_array_type) {
+        bool isDynamic = llvm::any_of(
+            arrayTy.getElements(), [](mlir::Attribute element) {
+              auto subrange =
+                  mlir::dyn_cast<mlir::LLVM::DISubrangeAttr>(element);
+              if (!subrange)
+                return false;
+              return static_cast<bool>(
+                  mlir::dyn_cast_if_present<
+                      mlir::LLVM::DILocalVariableAttr>(subrange.getCount()) ||
+                  mlir::dyn_cast_if_present<
+                      mlir::LLVM::DILocalVariableAttr>(
+                      subrange.getLowerBound()) ||
+                  mlir::dyn_cast_if_present<
+                      mlir::LLVM::DILocalVariableAttr>(
+                      subrange.getStride()));
+            });
+        if (isDynamic) {
+          if constexpr (std::is_same_v<Op, fir::cg::XDeclareOp>) {
+            if (auto funcOp =
+                    declOp->template getParentOfType<mlir::func::FuncOp>()) {
+              if (declOp->getBlock() == &funcOp.getBody().front()) {
+                for (mlir::Block &block : funcOp.getBody()) {
+                  if (auto returnOp = mlir::dyn_cast<mlir::func::ReturnOp>(
----------------
abidh wrote:

As you mentioned, we can probably try to find the terminating instruction once. This is not really a blocker for me but would be good to do.

https://github.com/llvm/llvm-project/pull/200061


More information about the flang-commits mailing list