[PATCH] D65452: [Support] Workaround a GCC 4.8 bug on constant expression evaluation.

Michael Liao via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 30 07:50:56 PDT 2019


hliao created this revision.
hliao added a reviewer: jfb.
Herald added subscribers: llvm-commits, dexonsmith.
Herald added a project: LLVM.

- The following stripped code trigger a gcc-4.8 bug. To work that around, move the alignment evaluation into template parameter.

  ``` // https://godbolt.org/z/58p5_X // #include <cstddef> #include <cstdint>

  enum { aligned = 0, unaligned = 1 };

  template <typename T, int alignment> struct PickAlignment { enum { value = alignment == 0 ? alignof(T) : alignment }; };

  template <typename ValueType, std::size_t Alignment> struct packed { private: struct { alignas( PickAlignment<ValueType, Alignment>::value) char buffer[sizeof(int)]; } Value; };

  using ule16_t = packed<uint16_t, unaligned>;

  ule16_t x; ```

- Also, replace `alignas` with `LLVMALIGN_AS` to improve the compiler compatibility.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D65452

Files:
  llvm/include/llvm/Support/Endian.h


Index: llvm/include/llvm/Support/Endian.h
===================================================================
--- llvm/include/llvm/Support/Endian.h
+++ llvm/include/llvm/Support/Endian.h
@@ -205,7 +205,8 @@
 
 template<typename ValueType,
          endianness Endian,
-         std::size_t Alignment>
+         std::size_t Alignment,
+         std::size_t ALIGN = PickAlignment<ValueType, Alignment>::value>
 struct packed_endian_specific_integral {
   using value_type = ValueType;
   static constexpr endianness endian = Endian;
@@ -247,8 +248,7 @@
 
 private:
   struct {
-    alignas(PickAlignment<value_type,
-                          alignment>::value) char buffer[sizeof(value_type)];
+    LLVM_ALIGNAS(ALIGN) char buffer[sizeof(value_type)];
   } Value;
 
 public:


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D65452.212346.patch
Type: text/x-patch
Size: 775 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190730/e433c0fc/attachment.bin>


More information about the llvm-commits mailing list