Skip to content

Commit

Permalink
[flang][cuda] Force default allocator in device code (#102238)
Browse files Browse the repository at this point in the history
  • Loading branch information
clementval authored Aug 9, 2024
1 parent f4fb735 commit 5c016bf
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions flang/runtime/descriptor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,15 @@ RT_API_ATTRS std::size_t Descriptor::Elements() const {
return elements;
}

RT_API_ATTRS static inline int MapAllocIdx(const Descriptor &desc) {
#ifdef RT_DEVICE_COMPILATION
// Force default allocator in device code.
return kDefaultAllocator;
#else
return desc.GetAllocIdx();
#endif
}

RT_API_ATTRS int Descriptor::Allocate() {
std::size_t elementBytes{ElementBytes()};
if (static_cast<std::int64_t>(elementBytes) < 0) {
Expand All @@ -162,11 +171,10 @@ RT_API_ATTRS int Descriptor::Allocate() {
elementBytes = raw_.elem_len = 0;
}
std::size_t byteSize{Elements() * elementBytes};
AllocFct alloc{allocatorRegistry.GetAllocator(MapAllocIdx(*this))};
// Zero size allocation is possible in Fortran and the resulting
// descriptor must be allocated/associated. Since std::malloc(0)
// result is implementation defined, always allocate at least one byte.

AllocFct alloc{allocatorRegistry.GetAllocator(GetAllocIdx())};
void *p{alloc(byteSize ? byteSize : 1)};
if (!p) {
return CFI_ERROR_MEM_ALLOCATION;
Expand Down Expand Up @@ -209,7 +217,7 @@ RT_API_ATTRS int Descriptor::Deallocate() {
if (!descriptor.base_addr) {
return CFI_ERROR_BASE_ADDR_NULL;
} else {
FreeFct free{allocatorRegistry.GetDeallocator(GetAllocIdx())};
FreeFct free{allocatorRegistry.GetDeallocator(MapAllocIdx(*this))};
free(descriptor.base_addr);
descriptor.base_addr = nullptr;
return CFI_SUCCESS;
Expand Down

0 comments on commit 5c016bf

Please sign in to comment.