Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 6 additions & 17 deletions llvm/include/llvm/Support/Alignment.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,8 @@ struct Align {
friend unsigned encode(struct MaybeAlign A);
friend struct MaybeAlign decodeMaybeAlign(unsigned Value);

/// A trivial type to allow construction of constexpr Align.
/// This is currently needed to workaround a bug in GCC 5.3 which prevents
/// definition of constexpr assign operators.
/// https://stackoverflow.com/questions/46756288/explicitly-defaulted-function-cannot-be-declared-as-constexpr-because-the-implic
/// FIXME: Remove this, make all assign operators constexpr and introduce user
/// defined literals when we don't have to support GCC 5.3 anymore.
/// https://llvm.org/docs/GettingStarted.html#getting-a-modern-host-c-toolchain
struct LogValue {
uint8_t Log;
};
struct FromShiftValue {};
constexpr Align(FromShiftValue, uint8_t Shift) : ShiftValue(Shift) {}

public:
/// Default is byte-aligned.
Expand All @@ -70,8 +62,8 @@ struct Align {
/// checks have been performed when building `Other`.
constexpr Align(const Align &Other) = default;
constexpr Align(Align &&Other) = default;
Align &operator=(const Align &Other) = default;
Align &operator=(Align &&Other) = default;
constexpr Align &operator=(const Align &Other) = default;
constexpr Align &operator=(Align &&Other) = default;

explicit Align(uint64_t Value) {
assert(Value > 0 && "Value must not be 0");
Expand All @@ -82,7 +74,7 @@ struct Align {

/// This is a hole in the type system and should not be abused.
/// Needed to interact with C for instance.
uint64_t value() const { return uint64_t(1) << ShiftValue; }
constexpr uint64_t value() const { return uint64_t(1) << ShiftValue; }

// Returns the previous alignment.
Align previous() const {
Expand All @@ -94,17 +86,14 @@ struct Align {

/// Allow constructions of constexpr Align.
template <size_t kValue> constexpr static Align Constant() {
return LogValue{static_cast<uint8_t>(ConstantLog2<kValue>())};
return Align(FromShiftValue{}, ConstantLog2<kValue>());
}

/// Allow constructions of constexpr Align from types.
/// Compile time equivalent to Align(alignof(T)).
template <typename T> constexpr static Align Of() {
return Constant<std::alignment_of_v<T>>();
}

/// Constexpr constructor from LogValue type.
constexpr Align(LogValue CA) : ShiftValue(CA.Log) {}
};

/// Treats the value 0 as a 1, so Align is always at least 1.
Expand Down
10 changes: 10 additions & 0 deletions llvm/unittests/Support/AlignmentTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ TEST(AlignmentTest, AlignConstexprConstant) {
EXPECT_EQ(Align(alignof(uint64_t)), kConstantAlign);
}

TEST(AlignmentTest, ConstexprAssign) {
constexpr auto assignAndGet = []() constexpr {
Align A = Align::Constant<8>();
Align B = Align::Constant<16>();
A = B;
return A.value();
};
static_assert(assignAndGet() == 16);
}

std::vector<uint64_t> getValidAlignments() {
std::vector<uint64_t> Out;
for (size_t Shift = 0; Shift < 64; ++Shift)
Expand Down