Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[flang][cuda] Force default allocator in device code #102238

Merged
merged 4 commits into from
Aug 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading