[PATCH] D142446: [MC] Disable copying and moving of MCInstrDescs the C++20 way
David Blaikie via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Wed Jan 25 15:51:39 PST 2023
dblaikie added a comment.
In D142446#4079439 <https://reviews.llvm.org/D142446#4079439>, @ilya-biryukov wrote:
> In D142446#4078204 <https://reviews.llvm.org/D142446#4078204>, @dblaikie wrote:
>
>> Alternatively, how bad would it be if aggregate initialization were removed and these objects were initialized with ctor calls? (bad for an -O0 build, no doubt... (how bad?) calling all those ctors to build the tables, but does clang optimize all the ctor calls away to the equivalent of aggregate init when using optimizations?)
>
> The alternative suggestion would be to add some library support, roughly this:
>
> #include <cstdint>
>
> struct NonCopyableNonMovable {
> NonCopyableNonMovable() = default;
> NonCopyableNonMovable(const NonCopyableNonMovable&) = delete;
> NonCopyableNonMovable(NonCopyableNonMovable&&) = delete;
> NonCopyableNonMovable &operator=(const NonCopyableNonMovable &) = delete;
> NonCopyableNonMovable &operator=(NonCopyableNonMovable &&) = delete;
> };
>
> #if defined(__has_cpp_attribute) && __has_cpp_attribute(no_unique_address)
> #define LLVM_NO_UNIQUE_ADDRESS [[no_unique_address]]
> #elif defined(__has_cpp_attribute) && __has_cpp_attribute(msvc::no_unique_address)
> #define LLVM_NO_UNIQUE_ADDRESS [[msvc::no_unique_address]]
> #else
> #error "WTF"
> #define LLVM_NO_UNIQUE_ADDRESS
> #endif
>
> #if __cplusplus >= 202002L
> // Put as the last member in the aggregate struct.
> #define LLVM_DISABLE_COPY_AND_MOVE(X)\
> LLVM_NO_UNIQUE_ADDRESS NonCopyableNonMovable DisableCopy##X;
> #else
> #define LLVM_DISABLE_COPY_AND_MOVE(X)\
> X(X const&) = delete;\
> X(X&&) = delete;\
> X& operator=(X const&) = delete;\
> X& operator=(X&&) = delete;
> #endif
>
> And use it like this:
>
> struct Y {
> uint64_t a;
> uint64_t b;
>
> LLVM_DISABLE_COPY_AND_MOVE(Y);
> };
> auto y = Y{1, 2};
> static_assert(sizeof(Y) == 16);
>
> Seem to work in both C++17 and C++20, except in the latest MSVC (looks like a bug, but I'm not entirely certain):
> https://gcc.godbolt.org/z/8nsooKz8c
> I suspect this could be worked around, but I did not dig any further.
>
> The question is: how useful is it and does it carry its weight?
Sounds OK to me, but yeah, might be overkill.
@foad any sense of how important this is? whether it's worth the above compelxity?
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D142446/new/
https://reviews.llvm.org/D142446
More information about the llvm-commits
mailing list