-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[flang] Add ability to have special allocator for descriptor data #100690
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
Changes from all commits
7118b77
4b10a9c
247e91b
e033565
e8fa5b5
b3dff1b
9639fe9
ecc4fd7
e778bbc
944999c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -92,8 +92,8 @@ class Dimension { | |
// The storage for this object follows the last used dim[] entry in a | ||
// Descriptor (CFI_cdesc_t) generic descriptor. Space matters here, since | ||
// descriptors serve as POINTER and ALLOCATABLE components of derived type | ||
// instances. The presence of this structure is implied by the flag | ||
// CFI_cdesc_t.f18Addendum, and the number of elements in the len_[] | ||
// instances. The presence of this structure is encoded in the | ||
// CFI_cdesc_t.extra field, and the number of elements in the len_[] | ||
// array is determined by derivedType_->LenParameters(). | ||
class DescriptorAddendum { | ||
public: | ||
|
@@ -339,14 +339,14 @@ class Descriptor { | |
const SubscriptValue *, const int *permutation = nullptr) const; | ||
|
||
RT_API_ATTRS DescriptorAddendum *Addendum() { | ||
if (raw_.f18Addendum != 0) { | ||
if (HasAddendum()) { | ||
return reinterpret_cast<DescriptorAddendum *>(&GetDimension(rank())); | ||
} else { | ||
return nullptr; | ||
} | ||
} | ||
RT_API_ATTRS const DescriptorAddendum *Addendum() const { | ||
if (raw_.f18Addendum != 0) { | ||
if (HasAddendum()) { | ||
return reinterpret_cast<const DescriptorAddendum *>( | ||
&GetDimension(rank())); | ||
} else { | ||
|
@@ -420,6 +420,27 @@ class Descriptor { | |
|
||
void Dump(FILE * = stdout) const; | ||
|
||
// Value of the addendum presence flag. | ||
#define _CFI_ADDENDUM_FLAG 1 | ||
// Number of bits needed to be shifted when manipulating the allocator index. | ||
#define _CFI_ALLOCATOR_IDX_SHIFT 1 | ||
// Allocator index mask. | ||
#define _CFI_ALLOCATOR_IDX_MASK 0b00001110 | ||
|
||
RT_API_ATTRS inline bool HasAddendum() const { | ||
return raw_.extra & _CFI_ADDENDUM_FLAG; | ||
} | ||
RT_API_ATTRS inline void SetHasAddendum() { | ||
raw_.extra |= _CFI_ADDENDUM_FLAG; | ||
} | ||
RT_API_ATTRS inline int GetAllocIdx() const { | ||
return (raw_.extra & _CFI_ALLOCATOR_IDX_MASK) >> _CFI_ALLOCATOR_IDX_SHIFT; | ||
} | ||
RT_API_ATTRS inline void SetAllocIdx(int pos) { | ||
raw_.extra &= ~_CFI_ALLOCATOR_IDX_MASK; // Clear the allocator index bits. | ||
raw_.extra |= (pos << _CFI_ALLOCATOR_IDX_SHIFT); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This line causes the build failure in my setup, as the following:
Edit: I use Can you please check? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Which compiler are you using? All build bots are working fine with this change. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was using GNU 11. Then, I tested this using Clang and it works. Should we only use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With:
I get the error. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's hard to have I'm often building with gcc 9.3.0 and there is tons of warnings. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, got it. Thank you! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I use gcc 9.3.0 with -Werror enabled for flang. What warnings are you seeing? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It doesn't throw the error for gcc 9. I will use gcc 9 only. Thank you! |
||
} | ||
|
||
private: | ||
ISO::CFI_cdesc_t raw_; | ||
}; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
//===-- runtime/allocator-registry.cpp ------------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "allocator-registry.h" | ||
#include "terminator.h" | ||
|
||
namespace Fortran::runtime { | ||
|
||
#ifndef FLANG_RUNTIME_NO_GLOBAL_VAR_DEFS | ||
RT_OFFLOAD_VAR_GROUP_BEGIN | ||
RT_VAR_ATTRS AllocatorRegistry allocatorRegistry; | ||
RT_OFFLOAD_VAR_GROUP_END | ||
#endif // FLANG_RUNTIME_NO_GLOBAL_VAR_DEFS | ||
|
||
RT_OFFLOAD_API_GROUP_BEGIN | ||
RT_API_ATTRS void AllocatorRegistry::Register(int pos, Allocator_t allocator) { | ||
// pos 0 is reserved for the default allocator and is registered in the | ||
// struct ctor. | ||
INTERNAL_CHECK(pos > 0 && pos < MAX_ALLOCATOR); | ||
allocators[pos] = allocator; | ||
} | ||
|
||
RT_API_ATTRS AllocFct AllocatorRegistry::GetAllocator(int pos) { | ||
INTERNAL_CHECK(pos >= 0 && pos < MAX_ALLOCATOR); | ||
AllocFct f{allocators[pos].alloc}; | ||
INTERNAL_CHECK(f != nullptr); | ||
return f; | ||
} | ||
|
||
RT_API_ATTRS FreeFct AllocatorRegistry::GetDeallocator(int pos) { | ||
INTERNAL_CHECK(pos >= 0 && pos < MAX_ALLOCATOR); | ||
FreeFct f{allocators[pos].free}; | ||
INTERNAL_CHECK(f != nullptr); | ||
return f; | ||
} | ||
RT_OFFLOAD_API_GROUP_END | ||
} // namespace Fortran::runtime |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
//===-- runtime/allocator-registry.h ----------------------------*- C++ -*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef FORTRAN_RUNTIME_ALLOCATOR_H_ | ||
#define FORTRAN_RUNTIME_ALLOCATOR_H_ | ||
|
||
#include "flang/Common/api-attrs.h" | ||
#include <cstdlib> | ||
#include <vector> | ||
|
||
#define MAX_ALLOCATOR 5 | ||
|
||
namespace Fortran::runtime { | ||
|
||
using AllocFct = void *(*)(std::size_t); | ||
using FreeFct = void (*)(void *); | ||
|
||
typedef struct Allocator_t { | ||
AllocFct alloc{nullptr}; | ||
FreeFct free{nullptr}; | ||
} Allocator_t; | ||
|
||
#ifdef RT_DEVICE_COMPILATION | ||
static RT_API_ATTRS void *MallocWrapper(std::size_t size) { | ||
return std::malloc(size); | ||
} | ||
static RT_API_ATTRS void FreeWrapper(void *p) { return std::free(p); } | ||
#endif | ||
|
||
struct AllocatorRegistry { | ||
#ifdef RT_DEVICE_COMPILATION | ||
RT_API_ATTRS constexpr AllocatorRegistry() | ||
: allocators{{&MallocWrapper, &FreeWrapper}} {} | ||
#else | ||
constexpr AllocatorRegistry() { allocators[0] = {&std::malloc, &std::free}; }; | ||
#endif | ||
RT_API_ATTRS void Register(int, Allocator_t); | ||
RT_API_ATTRS AllocFct GetAllocator(int pos); | ||
RT_API_ATTRS FreeFct GetDeallocator(int pos); | ||
|
||
Allocator_t allocators[MAX_ALLOCATOR]; | ||
}; | ||
|
||
RT_OFFLOAD_VAR_GROUP_BEGIN | ||
extern RT_VAR_ATTRS AllocatorRegistry allocatorRegistry; | ||
RT_OFFLOAD_VAR_GROUP_END | ||
|
||
} // namespace Fortran::runtime | ||
|
||
#endif // FORTRAN_RUNTIME_ALLOCATOR_H_ |
Uh oh!
There was an error while loading. Please reload this page.