[llvm] r367329 - [Support] Workaround a GCC 4.8 bug on constant expression evaluation.

Michael Liao via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 30 09:11:48 PDT 2019


Author: hliao
Date: Tue Jul 30 09:11:48 2019
New Revision: 367329

URL: http://llvm.org/viewvc/llvm-project?rev=367329&view=rev
Log:
[Support] Workaround a GCC 4.8 bug on constant expression evaluation.

Summary:
- 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
//

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.

Reviewers: jfb

Subscribers: dexonsmith, llvm-commits

Tags: #llvm

Differential Revision: https://reviews.llvm.org/D65452

Modified:
    llvm/trunk/include/llvm/Support/Endian.h

Modified: llvm/trunk/include/llvm/Support/Endian.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/Endian.h?rev=367329&r1=367328&r2=367329&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/Endian.h (original)
+++ llvm/trunk/include/llvm/Support/Endian.h Tue Jul 30 09:11:48 2019
@@ -205,7 +205,8 @@ namespace detail {
 
 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 @@ struct packed_endian_specific_integral {
 
 private:
   struct {
-    alignas(PickAlignment<value_type,
-                          alignment>::value) char buffer[sizeof(value_type)];
+    LLVM_ALIGNAS(ALIGN) char buffer[sizeof(value_type)];
   } Value;
 
 public:




More information about the llvm-commits mailing list