[flang-commits] [flang] 45a6c02 - [flang] Control alignment of constant folded reals (#149381)

via flang-commits flang-commits at lists.llvm.org
Mon Jul 21 13:51:55 PDT 2025


Author: Eugene Epshteyn
Date: 2025-07-21T16:51:52-04:00
New Revision: 45a6c02c2123f1d4764a8ad981193b15851df744

URL: https://github.com/llvm/llvm-project/commit/45a6c02c2123f1d4764a8ad981193b15851df744
DIFF: https://github.com/llvm/llvm-project/commit/45a6c02c2123f1d4764a8ad981193b15851df744.diff

LOG: [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.

Added: 
    

Modified: 
    flang/include/flang/Evaluate/integer.h
    flang/include/flang/Evaluate/real.h

Removed: 
    


################################################################################
diff  --git a/flang/include/flang/Evaluate/integer.h b/flang/include/flang/Evaluate/integer.h
index fccc2ad774a8f..5953fc81cb111 100644
--- a/flang/include/flang/Evaluate/integer.h
+++ b/flang/include/flang/Evaluate/integer.h
@@ -74,6 +74,7 @@ class Integer {
   static_assert(std::is_unsigned_v<BigPart>);
   static_assert(CHAR_BIT * sizeof(BigPart) >= 2 * partBits);
   static constexpr bool littleEndian{IS_LITTLE_ENDIAN};
+  static constexpr int alignment{ALIGNMENT};
 
 private:
   static constexpr int maxPartBits{CHAR_BIT * sizeof(Part)};

diff  --git a/flang/include/flang/Evaluate/real.h b/flang/include/flang/Evaluate/real.h
index 03294881850a1..76d25d9fe2670 100644
--- a/flang/include/flang/Evaluate/real.h
+++ b/flang/include/flang/Evaluate/real.h
@@ -490,7 +490,10 @@ template <typename WORD, int PREC> class Real {
       bool isNegative, int exponent, const Fraction &, Rounding, RoundingBits,
       bool multiply = false);
 
-  Word word_{}; // an Integer<>
+  // Require alignment, in case code generation on x86_64 decides that our
+  // Real object is suitable for SSE2 instructions and then gets surprised
+  // by unaligned address.
+  alignas(Word::alignment / 8) Word word_{}; // an Integer<>
 };
 
 extern template class Real<Integer<16>, 11>; // IEEE half format


        


More information about the flang-commits mailing list