[flang-commits] [flang] [flang][runtime] Establish derived type desc properly. (PR #67623)

Slava Zakharin via flang-commits flang-commits at lists.llvm.org
Thu Sep 28 12:05:24 PDT 2023


================
@@ -96,12 +96,19 @@ void Descriptor::Establish(const typeInfo::DerivedType &dt, void *p, int rank,
 
 OwningPtr<Descriptor> Descriptor::Create(TypeCode t, std::size_t elementBytes,
     void *p, int rank, const SubscriptValue *extent,
-    ISO::CFI_attribute_t attribute, int derivedTypeLenParameters) {
-  std::size_t bytes{SizeInBytes(rank, true, derivedTypeLenParameters)};
+    ISO::CFI_attribute_t attribute, bool addendum,
+    const typeInfo::DerivedType *dt) {
   Terminator terminator{__FILE__, __LINE__};
+  RUNTIME_CHECK(terminator, t.IsDerived() == (dt != nullptr));
+  int derivedTypeLenParameters = dt ? dt->LenParameters() : 0;
+  std::size_t bytes{SizeInBytes(rank, addendum, derivedTypeLenParameters)};
   Descriptor *result{
       reinterpret_cast<Descriptor *>(AllocateMemoryOrCrash(terminator, bytes))};
-  result->Establish(t, elementBytes, p, rank, extent, attribute, true);
----------------
vzakhari wrote:

Jean, I am not sure what use-case you thought about.

I guess it would be a runtime error if Create/Establish is called for an unlimited polymorphic, and it ends up creating a descriptor without an addendum.

>From the compiler side, I think, we cover all cases well.  I tried this case with a "dynamic" descriptor:
```
module types
  type t1
     class(*), allocatable :: c
  end type t1
end module types
program main
  use types
  call test()
contains
  subroutine test()
    type(t1), allocatable :: x
    allocate(x)
    allocate(x%c, source=7)
    select type (s => x%c)
    type is (integer)
       print *, s
    end select
  end subroutine test
end program main
```

The `allocate(x)` is allocating the memory based on the element size provided by the compiler, and the compiler correctly provides size of the descriptor with an addendum.  Then, the establishing of the component descriptor properly sets the `f18addendum` flag, based on the component's unlimited polymorphic type.

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


More information about the flang-commits mailing list