[flang-commits] [flang] [flang][cuda] Preserve array lower bounds in implicit device-to-host transfer (PR #227824)

via flang-commits flang-commits at lists.llvm.org
Thu Oct 1 06:11:07 PDT 2026


================
@@ -5694,9 +5557,32 @@ class FirConverter : public Fortran::lower::AbstractConverter {
             else
               temps.push_back(temp);
           }
-          addSymbol(sym,
-                    hlfir::translateToExtendedValue(loc, builder, temp).first,
-                    /*forced=*/true);
+
+          // Translate the temporary to an ExtendedValue and rebind it with
+          // the original lower bounds.  fir::updateRuntimeLBounds covers all
+          // array representations (ArrayBoxValue, BoxValue, MutableBoxValue,
+          // CharArrayBoxValue) while preserving the temp's storage/extents and
+          // dropping the CUDA device data attribute.
+          auto [tempExv, tempCleanup] =
+              hlfir::translateToExtendedValue(loc, builder, temp);
+          assert(!tempCleanup && "temp should not yield cleanup");
+          fir::ExtendedValue reboundExv =
+              fir::updateRuntimeLBounds(tempExv, origLbounds);
+
+          // Declare the host temporary under the symbol's name but without
+          // any CUDA data attribute (empty dataAttr = host storage).
+          auto symName = mangleName(sym);
+          fir::FortranVariableFlagsAttr hostAttrs = {};
+          cuf::DataAttributeAttr noDataAttr = {};
+          auto reboundDecl =
+              hlfir::genDeclare(loc, builder, reboundExv, symName, hostAttrs,
+                                /*dummyScope=*/mlir::Value{},
+                                /*storage=*/mlir::Value{},
+                                /*storageOffset=*/0, noDataAttr,
+                                /*argNo=*/0);
+          localSymbols.addVariableDefinition(sym, reboundDecl,
+                                             /*force=*/true);
----------------
jeanPerier wrote:

While this predates your change since `addSymbol` is creating a declare with the symbol name, I think we should not use the variable name in the declare since this is a temporary storage.

Using the variable name could lead to weird things from a TBAA perspective since we will end up assigning the same TAG to different storages inside the same scope if there are several data transfer.

You may want to just name it ".tmp.data_transfer."+the unmangled symbol name for instance. That is enough to help IR readers and will prevent messing with the TBAA and debug info.

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


More information about the flang-commits mailing list