[flang-commits] [flang] b1a34b2 - [flang] Fix element indexing in derived type default initialization

Valentin Clement via flang-commits flang-commits at lists.llvm.org
Wed Jan 25 09:39:10 PST 2023


Author: Valentin Clement
Date: 2023-01-25T18:39:03+01:00
New Revision: b1a34b242fb96ddedf34045d415cbb807534d733

URL: https://github.com/llvm/llvm-project/commit/b1a34b242fb96ddedf34045d415cbb807534d733
DIFF: https://github.com/llvm/llvm-project/commit/b1a34b242fb96ddedf34045d415cbb807534d733.diff

LOG: [flang] Fix element indexing in derived type default initialization

Derived type default initialization was not taking the step into
consideration.

```
module dt_init
  type p1
    integer :: a
  end type
  type, extends(p1) :: p2
    integer :: b = 10
  end type
contains
  subroutine init_dt(z)
    class(p1), intent(out) :: z(:)
    select type(z)
    type is (p2)
      print*,z
    end select
  end subroutine
end module
program test
  use dt_init
  type(p2) :: t(6) = [ p2(1,2),p2(3,4),p2(5,6),p2(7,8),p2(9,10),p2(11,12) ]
  print*,t
  call init_dt(t(::2))
  print*,t
end program
```

Without the fix, the three first elements are initialized

```
 1 2 3 4 5 6 7 8 9 10 11 12
 1 10 5 10 9 10
 1 10 3 10 5 10 7 8 9 10 11 12
```

Where it should be element number 1,3,5

```
 1 2 3 4 5 6 7 8 9 10 11 12
 1 10 5 10 9 10
 1 10 3 4 5 10 7 8 9 10 11 12
```

Reviewed By: klausler

Differential Revision: https://reviews.llvm.org/D142527

Added: 
    

Modified: 
    flang/runtime/derived.cpp

Removed: 
    


################################################################################
diff  --git a/flang/runtime/derived.cpp b/flang/runtime/derived.cpp
index bb47f6b7a3292..05c5b5264b9d4 100644
--- a/flang/runtime/derived.cpp
+++ b/flang/runtime/derived.cpp
@@ -48,7 +48,7 @@ int Initialize(const Descriptor &instance, const typeInfo::DerivedType &derived,
       // non-allocatable non-automatic components
       std::size_t bytes{comp.SizeInBytes(instance)};
       for (std::size_t j{0}; j < elements; ++j) {
-        char *ptr{instance.OffsetElement<char>(j * byteStride + comp.offset())};
+        char *ptr{instance.ZeroBasedIndexedElement<char>(j) + comp.offset()};
         std::memcpy(ptr, init, bytes);
       }
     } else if (comp.genre() == typeInfo::Component::Genre::Data &&


        


More information about the flang-commits mailing list