Skip to content

Commit 45a6c02

Browse files
[flang] Control alignment of constant folded reals (#149381)
When REAL types are constant folded, the underneath implementation uses arrays of integers. Ensure that these arrays are properly aligned. This matters when building flang with clang. In some cases, the resulting code for flang compiler ended up using SSE2 aligned load instructions for REAL(16) constant folding on x86_64, and these instructions require that the values are loaded from the aligned addresses.
1 parent 2a78c6d commit 45a6c02

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

flang/include/flang/Evaluate/integer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ class Integer {
7474
static_assert(std::is_unsigned_v<BigPart>);
7575
static_assert(CHAR_BIT * sizeof(BigPart) >= 2 * partBits);
7676
static constexpr bool littleEndian{IS_LITTLE_ENDIAN};
77+
static constexpr int alignment{ALIGNMENT};
7778

7879
private:
7980
static constexpr int maxPartBits{CHAR_BIT * sizeof(Part)};

flang/include/flang/Evaluate/real.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,10 @@ template <typename WORD, int PREC> class Real {
490490
bool isNegative, int exponent, const Fraction &, Rounding, RoundingBits,
491491
bool multiply = false);
492492

493-
Word word_{}; // an Integer<>
493+
// Require alignment, in case code generation on x86_64 decides that our
494+
// Real object is suitable for SSE2 instructions and then gets surprised
495+
// by unaligned address.
496+
alignas(Word::alignment / 8) Word word_{}; // an Integer<>
494497
};
495498

496499
extern template class Real<Integer<16>, 11>; // IEEE half format

0 commit comments

Comments
 (0)