Skip to content

Commit 2b2d79f

Browse files
authored
[flang][runtime] Establish derived type desc properly. (#67623)
Example: ``` module types type t real,allocatable :: c end type t contains function h(x) class(t),allocatable :: h ... end function h subroutine test type(t),allocatable :: b(:) allocate(b(2),source=h(2.5)) end subroutine test7 end module type ``` `DoFromSourceAssign` creates two descriptors for initializing `b(1)` and `b(2)` from the result of `h`. This Create call creates a descriptor without properly initialized addendum, so the Assign just does shallow copies of the descriptor representing result of `h` into `b(1)` and `b(2)`. I modified Create code to properly establish the descriptor for derived type case. I had to keep the `addendum` argument to keep the testing in `flang/unittests/Runtime/TemporaryStack.cpp`.
1 parent da28593 commit 2b2d79f

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

flang/include/flang/Runtime/descriptor.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,12 +180,16 @@ class Descriptor {
180180
const SubscriptValue *extent = nullptr,
181181
ISO::CFI_attribute_t attribute = CFI_attribute_other);
182182

183-
// CUDA_TODO: Clang does not support unique_ptr on device.
183+
// To create a descriptor for a derived type the caller
184+
// must provide non-null dt argument.
185+
// The addendum argument is only used for testing purposes,
186+
// and it may force a descriptor with an addendum while
187+
// dt may be null.
184188
static RT_API_ATTRS OwningPtr<Descriptor> Create(TypeCode t,
185189
std::size_t elementBytes, void *p = nullptr, int rank = maxRank,
186190
const SubscriptValue *extent = nullptr,
187191
ISO::CFI_attribute_t attribute = CFI_attribute_other,
188-
int derivedTypeLenParameters = 0);
192+
bool addendum = false, const typeInfo::DerivedType *dt = nullptr);
189193
static RT_API_ATTRS OwningPtr<Descriptor> Create(TypeCategory, int kind,
190194
void *p = nullptr, int rank = maxRank,
191195
const SubscriptValue *extent = nullptr,

flang/runtime/descriptor.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,19 @@ RT_API_ATTRS void Descriptor::Establish(const typeInfo::DerivedType &dt,
9999

100100
RT_API_ATTRS OwningPtr<Descriptor> Descriptor::Create(TypeCode t,
101101
std::size_t elementBytes, void *p, int rank, const SubscriptValue *extent,
102-
ISO::CFI_attribute_t attribute, int derivedTypeLenParameters) {
103-
std::size_t bytes{SizeInBytes(rank, true, derivedTypeLenParameters)};
102+
ISO::CFI_attribute_t attribute, bool addendum,
103+
const typeInfo::DerivedType *dt) {
104104
Terminator terminator{__FILE__, __LINE__};
105+
RUNTIME_CHECK(terminator, t.IsDerived() == (dt != nullptr));
106+
int derivedTypeLenParameters = dt ? dt->LenParameters() : 0;
107+
std::size_t bytes{SizeInBytes(rank, addendum, derivedTypeLenParameters)};
105108
Descriptor *result{
106109
reinterpret_cast<Descriptor *>(AllocateMemoryOrCrash(terminator, bytes))};
107-
result->Establish(t, elementBytes, p, rank, extent, attribute, true);
110+
if (dt) {
111+
result->Establish(*dt, p, rank, extent, attribute);
112+
} else {
113+
result->Establish(t, elementBytes, p, rank, extent, attribute, addendum);
114+
}
108115
return OwningPtr<Descriptor>{result};
109116
}
110117

@@ -126,7 +133,7 @@ RT_API_ATTRS OwningPtr<Descriptor> Descriptor::Create(
126133
const typeInfo::DerivedType &dt, void *p, int rank,
127134
const SubscriptValue *extent, ISO::CFI_attribute_t attribute) {
128135
return Create(TypeCode{TypeCategory::Derived, 0}, dt.sizeInBytes(), p, rank,
129-
extent, attribute, dt.LenParameters());
136+
extent, attribute, /*addendum=*/true, &dt);
130137
}
131138

132139
RT_API_ATTRS std::size_t Descriptor::SizeInBytes() const {

0 commit comments

Comments
 (0)