[flang-commits] [flang] [flang] Fix TRANSFER into derived type with tail padding zeroing pad bytes (PR #223814)

via flang-commits flang-commits at lists.llvm.org
Wed Sep 16 20:37:05 PDT 2026


================
@@ -27,3 +25,17 @@ func.func @test_with_final(%a: !fir.ref<!t_with_final>, %b: !fir.ref<!t_with_fin
 }
 // CHECK-LABEL: func.func @test_with_final(
 // CHECK:  fir.call @_FortranAAssign
+
+// Derived type with two fields of different sizes: the LLVM struct will have
+// tail padding.  The assignment must use fir.copy (full allocated size) rather
+// than field-by-field copy so that tail-padding bytes are preserved.
+!t_tail_pad = !fir.type<tail_pad,sequence{a:i32,b:i8}>
+fir.type_info @tail_pad noinit nodestroy nofinal : !t_tail_pad
+
+func.func @test_tail_pad(%a: !fir.ref<!t_tail_pad>, %b: !fir.ref<!t_tail_pad>) {
+  hlfir.assign %b to %a : !fir.ref<!t_tail_pad>, !fir.ref<!t_tail_pad>
+  return
+}
+// CHECK-LABEL: func.func @test_tail_pad(
+// CHECK-NOT: fir.coordinate_of
+// CHECK:  fir.copy %{{.*}} to %{{.*}} no_overlap : !fir.ref<!fir.type<tail_pad,sequence{a:i32,b:i8}>>, !fir.ref<!fir.type<tail_pad,sequence{a:i32,b:i8}>>
----------------
MattPD wrote:

This CHECK line verifies routing to `fir.copy` but not the copy size changed by the patch. In-tree tests don't check an 8-byte copy of `!fir.type<tail_pad,sequence{a:i32,b:i8}>`. `flang/test/Fir/copy-codegen.fir` only covers a 36-byte record without tail padding. `fir-opt --fir-to-llvm-ir` on a `fir.copy` of this type produces `llvm.mlir.constant(8 : i64)` as the `llvm.intr.memcpy` length. A matching case in `copy-codegen.fir` would cover the 8-byte size.

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


More information about the flang-commits mailing list