Skip to content

[Flang-RT] Change alignment for allocatable and pointer arrays to 64 …#206525

Merged
Jason-Van-Beusekom merged 3 commits into
llvm:mainfrom
Jason-Van-Beusekom:runtime-alginment
Jul 1, 2026
Merged

[Flang-RT] Change alignment for allocatable and pointer arrays to 64 …#206525
Jason-Van-Beusekom merged 3 commits into
llvm:mainfrom
Jason-Van-Beusekom:runtime-alginment

Conversation

@Jason-Van-Beusekom

@Jason-Van-Beusekom Jason-Van-Beusekom commented Jun 29, 2026

Copy link
Copy Markdown
Contributor

…bytes

in #194969 the default array alignment was changed to 64bytes for Fortran globals. In this PR the malloc wrapper in the flang runtime is modified to accept an alignment argument (0 default) and for cases above 16 byte (std::max_align_t) uses aligned_alloc instead of malloc.

The issue of alignment was discussed in this RFC: https://discourse.llvm.org/t/rfc-alignment-of-global-arrays/90397/13

Assisted-by: Opus 4.6

@llvmorg-github-actions llvmorg-github-actions Bot added flang Flang issues not falling into any other category flang-rt labels Jun 29, 2026
@github-actions

github-actions Bot commented Jun 29, 2026

Copy link
Copy Markdown

✅ With the latest revision this PR passed the C/C++ code formatter.

@eugeneepshteyn eugeneepshteyn removed the request for review from klausler June 29, 2026 16:55
static constexpr unsigned kDeviceAllocatorPos = 2;
static constexpr unsigned kManagedAllocatorPos = 3;
static constexpr unsigned kUnifiedAllocatorPos = 4;
static constexpr unsigned kDefaultArrayAlignment = 64;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add a blank line of a comment to separate this from the CUF allocator positions.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 24826e2

@eugeneepshteyn eugeneepshteyn left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

Note: for this flang-rt change, please ensure that both static and shared library builds of flang work.

Should the new alignment for global, allocatable, and pointer arrays be documented somewhere? Should definitely be mentioned in release notes, but maybe also in another more permanent doc?

@eugeneepshteyn

eugeneepshteyn commented Jun 29, 2026

Copy link
Copy Markdown
Contributor

@Jason-Van-Beusekom , when building with gcc 9.3.0:

.../llvm-project/flang-rt/include/flang-rt/runtime/allocator-registry.h:35:27: error: no member named 'max_align_t' in namespace 'std'; did you mean simply 'max_align_t'?
   35 |   if (alignment > alignof(std::max_align_t)) {
      |                           ^    ~~~~~~~~~~~
.../build/release/llvm/lib/clang/23/include/__stddef_max_align_t.h:24:3: note: 'max_align_t' declared here
   24 | } max_align_t;
      |   ^

I think you need to explicitly include <cstddef> for max_align_t.

Comment thread flang-rt/include/flang-rt/runtime/allocator-registry.h
Comment thread flang-rt/include/flang-rt/runtime/allocator-registry.h Outdated
@eugeneepshteyn

Copy link
Copy Markdown
Contributor

Assisted with Opus 4.6

Nit: please use Assisted-by: ... tag, per process (and also some people search for this particular string).

@Jason-Van-Beusekom

Copy link
Copy Markdown
Contributor Author

Assisted with Opus 4.6

Nit: please use Assisted-by: ... tag, per process (and also some people search for this particular string).

Updated Pr message and will make sure to have the commit with correct tag.

@Jason-Van-Beusekom

Copy link
Copy Markdown
Contributor Author

@Jason-Van-Beusekom , when building with gcc 9.3.0:

.../llvm-project/flang-rt/include/flang-rt/runtime/allocator-registry.h:35:27: error: no member named 'max_align_t' in namespace 'std'; did you mean simply 'max_align_t'?
   35 |   if (alignment > alignof(std::max_align_t)) {
      |                           ^    ~~~~~~~~~~~
.../build/release/llvm/lib/clang/23/include/__stddef_max_align_t.h:24:3: note: 'max_align_t' declared here
   24 | } max_align_t;
      |   ^

I think you need to explicitly include <cstddef> for max_align_t.

Updated in 3c7d7fb

@Jason-Van-Beusekom

Jason-Van-Beusekom commented Jun 30, 2026

Copy link
Copy Markdown
Contributor Author

Will hold off on merging till tomorrow for any last minute feedback, also this does not cover all cases as simple allocatable arrays are not handled in the runtime and instead in llvm-project/flang/lib/Lower/Allocatable.cpp via inlineAllocation. Will have that change be in a seperate PR

@Jason-Van-Beusekom Jason-Van-Beusekom merged commit a675a66 into llvm:main Jul 1, 2026
12 checks passed
@ceseo

ceseo commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

This broke recent versions of macOS. I created a PR to fix it: #207248


## Non-comprehensive list of changes in this release

- Global, allocatable, and pointer arrays are now aligned to 64 bytes. The

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a special wording to indicate that binaries compiled with this release are not compatible with binaries built by the previous one? We should probably stress that in the release notes.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I cannot find notes on it but in the discussion for the RFC for this change: https://discourse.llvm.org/t/rfc-alignment-of-global-arrays/90397/14 if I remember correctly this was mentioned in the meeting, and if I recall correctly flang makes no guarantees that new releases work with binaries compiled with older versions.

I can add a mention of it in the release notes if desired however.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since Flang is part of LLVM, I believe there is some comptatibility guarantees within the same major version. @tstellar @tru do you have an advice about documenting compatibility breaking change within one major release?

Jason-Van-Beusekom added a commit that referenced this pull request Jul 13, 2026
…ys (#208781)

In #206525 the runtime was set
to default alloctable and pointer arrays to 64byte alignment. In this commit
the inlineallocation case is handled.

The same approach was taken and an alignment attribute was added,
with logic to promote to aligned_alloc or posixMemalign for apple
targets, when requested alignment is greater than 16.

Of note alignof(std::max_align_t)) was not used as the runtime target
may be different than the compile target, so 16 was hardcoded as when to
use aligned_alloc

Assisted-by: Opus 4.8
llvm-upstreamsync Bot pushed a commit to qualcomm/cpullvm-toolchain that referenced this pull request Jul 13, 2026
…on for arrays (#208781)

In llvm/llvm-project#206525 the runtime was set
to default alloctable and pointer arrays to 64byte alignment. In this commit
the inlineallocation case is handled.

The same approach was taken and an alignment attribute was added,
with logic to promote to aligned_alloc or posixMemalign for apple
targets, when requested alignment is greater than 16.

Of note alignof(std::max_align_t)) was not used as the runtime target
may be different than the compile target, so 16 was hardcoded as when to
use aligned_alloc

Assisted-by: Opus 4.8
llvm-sync Bot pushed a commit to arm/arm-toolchain that referenced this pull request Jul 13, 2026
…on for arrays (#208781)

In llvm/llvm-project#206525 the runtime was set
to default alloctable and pointer arrays to 64byte alignment. In this commit
the inlineallocation case is handled.

The same approach was taken and an alignment attribute was added,
with logic to promote to aligned_alloc or posixMemalign for apple
targets, when requested alignment is greater than 16.

Of note alignof(std::max_align_t)) was not used as the runtime target
may be different than the compile target, so 16 was hardcoded as when to
use aligned_alloc

Assisted-by: Opus 4.8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

flang Flang issues not falling into any other category flang-rt

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants