github-actions[bot] wrote: <!--LLVM CODE FORMAT COMMENT: {clang-format}-->
:warning: C/C++ code formatter, clang-format found issues in your code. :warning: <details> <summary> You can test this locally with the following command: </summary> ``````````bash git-clang-format --diff 545e0593f8c59376a7ec8c6eb558babf6c9f93c1 6679082fa13048516a2b1ea27edf8de4cecb6f07 --extensions ,cpp,h -- libcxx/include/__memory/tagged_ptr.h clang/include/clang/AST/APValue.h clang/lib/AST/APValue.cpp clang/lib/AST/ExprConstant.cpp clang/lib/CodeGen/CGBuiltin.cpp clang/lib/CodeGen/CodeGenFunction.h libcxx/include/memory `````````` </details> <details> <summary> View the diff from clang-format here. </summary> ``````````diff diff --git a/clang/include/clang/AST/APValue.h b/clang/include/clang/AST/APValue.h index 278cf61dd5..da6f23d36a 100644 --- a/clang/include/clang/AST/APValue.h +++ b/clang/include/clang/AST/APValue.h @@ -198,6 +198,7 @@ public: /// The QualType, if this is a DynamicAllocLValue. void *DynamicAllocType; }; + public: uint64_t Metadata{0}; }; @@ -530,7 +531,7 @@ public: const LValueBase getLValueBase() const; uint64_t getLValueMetadata() const; - uint64_t & getLValueMetadata(); + uint64_t &getLValueMetadata(); CharUnits &getLValueOffset(); const CharUnits &getLValueOffset() const { return const_cast<APValue*>(this)->getLValueOffset(); diff --git a/clang/lib/AST/APValue.cpp b/clang/lib/AST/APValue.cpp index 02ba5cf51d..72704459a9 100644 --- a/clang/lib/AST/APValue.cpp +++ b/clang/lib/AST/APValue.cpp @@ -980,7 +980,7 @@ uint64_t APValue::getLValueMetadata() const { return ((const LV *)(const void *)&Data)->Base.Metadata; } -uint64_t & APValue::getLValueMetadata() { +uint64_t &APValue::getLValueMetadata() { assert(isLValue() && "Invalid accessor"); return ((LV *)(void *)&Data)->Base.Metadata; } diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp index ed7eeeba20..3a83aedff1 100644 --- a/clang/lib/AST/ExprConstant.cpp +++ b/clang/lib/AST/ExprConstant.cpp @@ -4498,7 +4498,7 @@ handleLValueToRValueConversion(EvalInfo &Info, const Expr *Conv, QualType Type, bool WantObjectRepresentation = false) { if (LVal.Designator.Invalid) return false; - + if (LVal.Base.Metadata != 0) { Info.FFDiag(Conv, diag::note_constexpr_dereferencing_tagged_pointer); return false; @@ -9741,27 +9741,29 @@ bool PointerExprEvaluator::VisitBuiltinCallExpr(const CallExpr *E, switch (BuiltinOp) { // emulation of pointer tagging without actually touching pointer value - // as there is no such thing as address here, so tag is stored as a metadata in lvalue base - case Builtin::BI__builtin_tag_pointer_mask_or: { - APSInt Value, Mask; - if (!evaluatePointer(E->getArg(0), Result)) - return Error(E); - - if (!EvaluateInteger(E->getArg(1), Value, Info)) - return Error(E); - - if (!EvaluateInteger(E->getArg(2), Mask, Info)) - return Error(E); - - if (Mask.getLimitedValue() == 0) { - CCEDiag(E->getArg(2), diag::note_constexpr_tagging_with_empty_mask); - return false; - } - - Result.Base.Metadata = (Result.Base.Metadata & ~Mask.getLimitedValue()) | (Value.getLimitedValue() & Mask.getLimitedValue()); - return true; + // as there is no such thing as address here, so tag is stored as a metadata + // in lvalue base + case Builtin::BI__builtin_tag_pointer_mask_or: { + APSInt Value, Mask; + if (!evaluatePointer(E->getArg(0), Result)) + return Error(E); + + if (!EvaluateInteger(E->getArg(1), Value, Info)) + return Error(E); + + if (!EvaluateInteger(E->getArg(2), Mask, Info)) + return Error(E); + + if (Mask.getLimitedValue() == 0) { + CCEDiag(E->getArg(2), diag::note_constexpr_tagging_with_empty_mask); + return false; } + Result.Base.Metadata = (Result.Base.Metadata & ~Mask.getLimitedValue()) | + (Value.getLimitedValue() & Mask.getLimitedValue()); + return true; + } + // alternative approach to tagging which shifts pointer // here we are only shifting metadata case Builtin::BI__builtin_tag_pointer_shift_or: { @@ -9780,45 +9782,50 @@ bool PointerExprEvaluator::VisitBuiltinCallExpr(const CallExpr *E, return false; } - const uint64_t Mask = (1ull << static_cast<uint64_t>(Shift.getLimitedValue())) - 1ull; - Result.Base.Metadata = (Result.Base.Metadata << static_cast<uint64_t>(Shift.getLimitedValue())) | (Value.getLimitedValue() & Mask); + const uint64_t Mask = + (1ull << static_cast<uint64_t>(Shift.getLimitedValue())) - 1ull; + Result.Base.Metadata = (Result.Base.Metadata + << static_cast<uint64_t>(Shift.getLimitedValue())) | + (Value.getLimitedValue() & Mask); return true; } - + // recover pointer by masking metadata // exprconstant allows dereferencing only metadata == 0 pointer case Builtin::BI__builtin_tag_pointer_mask: { APSInt Mask; if (!evaluatePointer(E->getArg(0), Result)) - return Error(E); - + return Error(E); + if (!EvaluateInteger(E->getArg(1), Mask, Info)) return Error(E); - + if (Mask.getLimitedValue() == 0) { CCEDiag(E->getArg(2), diag::note_constexpr_tagging_with_empty_mask); return false; } - + Result.Base.Metadata = (Result.Base.Metadata & Mask.getLimitedValue()); return true; } - // shifting back pointer (also can convert tagged pointer back to normal pointer) + // shifting back pointer (also can convert tagged pointer back to normal + // pointer) case Builtin::BI__builtin_tag_pointer_unshift: { APSInt Shift; if (!evaluatePointer(E->getArg(0), Result)) - return Error(E); - + return Error(E); + if (!EvaluateInteger(E->getArg(1), Shift, Info)) return Error(E); - + if (Shift.getLimitedValue() == 0) { CCEDiag(E->getArg(2), diag::note_constexpr_tagging_with_shift_zero); return false; } - - Result.Base.Metadata = (Result.Base.Metadata >> static_cast<uint64_t>(Shift.getLimitedValue())); + + Result.Base.Metadata = (Result.Base.Metadata >> + static_cast<uint64_t>(Shift.getLimitedValue())); return true; } case Builtin::BIaddressof: @@ -12751,19 +12758,20 @@ bool IntExprEvaluator::VisitBuiltinCallExpr(const CallExpr *E, case Builtin::BI__builtin_tag_pointer_mask_as_int: { LValue Pointer; APSInt Mask; - + if (!EvaluatePointer(E->getArg(0), Pointer, Info)) return Error(E); if (!EvaluateInteger(E->getArg(1), Mask, Info)) return Error(E); - + if (Mask.getLimitedValue() == 0) { CCEDiag(E->getArg(2), diag::note_constexpr_tagging_with_empty_mask); return false; } - - const uint64_t Result = Pointer.Base.Metadata & (static_cast<uint64_t>(Mask.getLimitedValue())); + + const uint64_t Result = + Pointer.Base.Metadata & (static_cast<uint64_t>(Mask.getLimitedValue())); return Success(Result, E); } @@ -14324,13 +14332,13 @@ EvaluateComparisonBinaryOperator(EvalInfo &Info, const BinaryOperator *E, return Success(CmpResult::Less, E); if (CompareLHS > CompareRHS) return Success(CmpResult::Greater, E); - + // this makes tagged pointer not equal to original pointer if (LHSValue.Base.Metadata < RHSValue.Base.Metadata) return Success(CmpResult::Less, E); if (LHSValue.Base.Metadata > RHSValue.Base.Metadata) return Success(CmpResult::Greater, E); - + return Success(CmpResult::Equal, E); } diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp index 4ac09e7a31..14519d0ecd 100644 --- a/clang/lib/CodeGen/CGBuiltin.cpp +++ b/clang/lib/CodeGen/CGBuiltin.cpp @@ -5320,7 +5320,7 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID, return RValue::get(Carry); } - + // support for pointer tagging case Builtin::BI__builtin_tag_pointer_mask_or: return EmitBuiltinTagPointerMaskOr(E); @@ -21260,93 +21260,112 @@ Value *CodeGenFunction::EmitNVPTXBuiltinExpr(unsigned BuiltinID, /// Generate (x & ~mask) | (value & mask). RValue CodeGenFunction::EmitBuiltinTagPointerMaskOr(const CallExpr *E) { - llvm::Value * Ptr = EmitScalarExpr(E->getArg(0)); - llvm::Value * Value = EmitScalarExpr(E->getArg(1)); - llvm::Value * Mask = EmitScalarExpr(E->getArg(2)); - - llvm::IntegerType * IntType = IntegerType::get(getLLVMContext(), CGM.getDataLayout().getIndexTypeSizeInBits(Ptr->getType())); - + llvm::Value *Ptr = EmitScalarExpr(E->getArg(0)); + llvm::Value *Value = EmitScalarExpr(E->getArg(1)); + llvm::Value *Mask = EmitScalarExpr(E->getArg(2)); + + llvm::IntegerType *IntType = IntegerType::get( + getLLVMContext(), + CGM.getDataLayout().getIndexTypeSizeInBits(Ptr->getType())); + // TODO: avoid using bitcast and go path of ptr.tag (mirror to ptr.mask) - // to keep pointer's provenance, but this turns out a bit harder to do as it touches - // a lot of places in llvm - llvm::Value * PointerInt = Builder.CreateBitOrPointerCast(Ptr, IntType, "pointer_int"); - llvm::Value * InvertedMask = Builder.CreateNot(Mask, "inverted_mask"); - - llvm::Value * MaskedPtr = Builder.CreateAnd(PointerInt, InvertedMask, "masked_ptr"); - llvm::Value * MaskedValue = Builder.CreateAnd(Value, Mask, "masked_value"); - - llvm::Value * ResultInt = Builder.CreateOr(MaskedPtr, MaskedValue, "result_int"); - llvm::Value * Result = Builder.CreateBitOrPointerCast(ResultInt, Ptr->getType(), "result_ptr"); - + // to keep pointer's provenance, but this turns out a bit harder to do as it + // touches a lot of places in llvm + llvm::Value *PointerInt = + Builder.CreateBitOrPointerCast(Ptr, IntType, "pointer_int"); + llvm::Value *InvertedMask = Builder.CreateNot(Mask, "inverted_mask"); + + llvm::Value *MaskedPtr = + Builder.CreateAnd(PointerInt, InvertedMask, "masked_ptr"); + llvm::Value *MaskedValue = Builder.CreateAnd(Value, Mask, "masked_value"); + + llvm::Value *ResultInt = + Builder.CreateOr(MaskedPtr, MaskedValue, "result_int"); + llvm::Value *Result = + Builder.CreateBitOrPointerCast(ResultInt, Ptr->getType(), "result_ptr"); + return RValue::get(Result); } /// Generate (x << shift) | (value & ((1 << shift) - 1)). RValue CodeGenFunction::EmitBuiltinTagPointerShiftOr(const CallExpr *E) { - llvm::Value * Ptr = EmitScalarExpr(E->getArg(0)); - llvm::Value * Value = EmitScalarExpr(E->getArg(1)); - llvm::Value * Shift = EmitScalarExpr(E->getArg(2)); - - llvm::IntegerType * IntType = IntegerType::get(getLLVMContext(), CGM.getDataLayout().getIndexTypeSizeInBits(Ptr->getType())); - + llvm::Value *Ptr = EmitScalarExpr(E->getArg(0)); + llvm::Value *Value = EmitScalarExpr(E->getArg(1)); + llvm::Value *Shift = EmitScalarExpr(E->getArg(2)); + + llvm::IntegerType *IntType = IntegerType::get( + getLLVMContext(), + CGM.getDataLayout().getIndexTypeSizeInBits(Ptr->getType())); + // TODO: again, for now a bitcast, later ptr.shift_tag - llvm::Value * PointerInt = Builder.CreateBitOrPointerCast(Ptr, IntType, "pointer_int"); - llvm::Value * ShiftedPointerInt = Builder.CreateShl(PointerInt, Shift); - + llvm::Value *PointerInt = + Builder.CreateBitOrPointerCast(Ptr, IntType, "pointer_int"); + llvm::Value *ShiftedPointerInt = Builder.CreateShl(PointerInt, Shift); + auto *One = llvm::ConstantInt::get(IntType, 1); - - llvm::Value * Mask = Builder.CreateSub(Builder.CreateShl(One, Shift), One, "mask"); - llvm::Value * MaskedValue = Builder.CreateAdd(Value, Mask, "masked_value"); - llvm::Value * PointerWithTag = Builder.CreateOr(ShiftedPointerInt, MaskedValue, "pointer_with_tag_int"); - - llvm::Value * Result = Builder.CreateBitOrPointerCast(PointerWithTag, Ptr->getType(), "result_ptr"); + + llvm::Value *Mask = + Builder.CreateSub(Builder.CreateShl(One, Shift), One, "mask"); + llvm::Value *MaskedValue = Builder.CreateAdd(Value, Mask, "masked_value"); + llvm::Value *PointerWithTag = + Builder.CreateOr(ShiftedPointerInt, MaskedValue, "pointer_with_tag_int"); + + llvm::Value *Result = Builder.CreateBitOrPointerCast( + PointerWithTag, Ptr->getType(), "result_ptr"); return RValue::get(Result); } /// Generate (x >> shift) RValue CodeGenFunction::EmitBuiltinTagPointerUnshift(const CallExpr *E) { - llvm::Value * Ptr = EmitScalarExpr(E->getArg(0)); - llvm::Value * Shift = EmitScalarExpr(E->getArg(1)); - - llvm::IntegerType * IntType = IntegerType::get(getLLVMContext(), CGM.getDataLayout().getIndexTypeSizeInBits(Ptr->getType())); - + llvm::Value *Ptr = EmitScalarExpr(E->getArg(0)); + llvm::Value *Shift = EmitScalarExpr(E->getArg(1)); + + llvm::IntegerType *IntType = IntegerType::get( + getLLVMContext(), + CGM.getDataLayout().getIndexTypeSizeInBits(Ptr->getType())); + // for now I'm going path of bitcast - llvm::Value * PointerInt = Builder.CreateBitOrPointerCast(Ptr, IntType, "pointer_int"); - llvm::Value * UnShiftedPointerInt = Builder.CreateAShr(PointerInt, Shift, "unshifted_pointer_int"); - - llvm::Value * Result = Builder.CreateBitOrPointerCast(UnShiftedPointerInt, Ptr->getType(), "result_ptr"); + llvm::Value *PointerInt = + Builder.CreateBitOrPointerCast(Ptr, IntType, "pointer_int"); + llvm::Value *UnShiftedPointerInt = + Builder.CreateAShr(PointerInt, Shift, "unshifted_pointer_int"); + + llvm::Value *Result = Builder.CreateBitOrPointerCast( + UnShiftedPointerInt, Ptr->getType(), "result_ptr"); return RValue::get(Result); } /// Generate (x & mask). RValue CodeGenFunction::EmitBuiltinTagPointerMask(const CallExpr *E) { - llvm::Value * Ptr = EmitScalarExpr(E->getArg(0)); - llvm::Value * Mask = EmitScalarExpr(E->getArg(1)); - + llvm::Value *Ptr = EmitScalarExpr(E->getArg(0)); + llvm::Value *Mask = EmitScalarExpr(E->getArg(1)); + llvm::Value *Result = Builder.CreateIntrinsic( - Intrinsic::ptrmask, {Ptr->getType(), Mask->getType()}, - {Ptr, Mask}, nullptr, "result"); - + Intrinsic::ptrmask, {Ptr->getType(), Mask->getType()}, {Ptr, Mask}, + nullptr, "result"); + return RValue::get(Result); } /// Generate (x & mask) (but return it as number). RValue CodeGenFunction::EmitBuiltinTagPointerMaskAsInt(const CallExpr *E) { - llvm::Value * Ptr = EmitScalarExpr(E->getArg(0)); - llvm::Value * Mask = EmitScalarExpr(E->getArg(1)); - - llvm::IntegerType * IntType = IntegerType::get(getLLVMContext(), CGM.getDataLayout().getIndexTypeSizeInBits(Ptr->getType())); - + llvm::Value *Ptr = EmitScalarExpr(E->getArg(0)); + llvm::Value *Mask = EmitScalarExpr(E->getArg(1)); + + llvm::IntegerType *IntType = IntegerType::get( + getLLVMContext(), + CGM.getDataLayout().getIndexTypeSizeInBits(Ptr->getType())); + llvm::Value *Result = Builder.CreateIntrinsic( - Intrinsic::ptrmask, {Ptr->getType(), Mask->getType()}, - {Ptr, Mask}, nullptr, "result"); + Intrinsic::ptrmask, {Ptr->getType(), Mask->getType()}, {Ptr, Mask}, + nullptr, "result"); + + llvm::Value *IntResult = + Builder.CreateBitOrPointerCast(Result, IntType, "int_result"); - llvm::Value * IntResult = Builder.CreateBitOrPointerCast(Result, IntType, "int_result"); - return RValue::get(IntResult); } - namespace { struct BuiltinAlignArgs { llvm::Value *Src = nullptr; diff --git a/libcxx/include/__memory/tagged_ptr.h b/libcxx/include/__memory/tagged_ptr.h index 7c99d0e929..c11d9dacae 100644 --- a/libcxx/include/__memory/tagged_ptr.h +++ b/libcxx/include/__memory/tagged_ptr.h @@ -15,122 +15,142 @@ #endif #if _LIBCPP_STD_VER >= 26 - -#include <__config> -#include <__type_traits/is_trivially_copyable.h> -#include <__assert> -#include "__bit/has_single_bit.h" -#include <__type_traits/rank.h> -#include "pointer_traits.h" -#include <compare> + +# include "__bit/has_single_bit.h" +# include "pointer_traits.h" +# include <__assert> +# include <__config> +# include <__type_traits/is_trivially_copyable.h> +# include <__type_traits/rank.h> +# include <compare> _LIBCPP_BEGIN_NAMESPACE_STD -template <typename T, typename Y> concept convertible_to_from = std::convertible_to<Y, T> && std::convertible_to<T, Y>; - -template <typename T> concept pointer_tagging_schema = requires(T::dirty_pointer payload, T::clean_pointer clean, T::tag_type tag) { - //requires convertible_to_from<typename T::tag_type, uintptr_t>; +template <typename T, typename Y> +concept convertible_to_from = std::convertible_to<Y, T> && std::convertible_to<T, Y>; + +template <typename T> +concept pointer_tagging_schema = requires(T::dirty_pointer payload, T::clean_pointer clean, T::tag_type tag) { + // requires convertible_to_from<typename T::tag_type, uintptr_t>; requires std::is_pointer_v<typename T::clean_pointer>; - + { T::encode_pointer_with_tag(clean, tag) } noexcept -> std::same_as<typename T::dirty_pointer>; { T::recover_pointer(payload) } noexcept -> std::same_as<typename T::clean_pointer>; { T::recover_value(payload) } noexcept -> std::same_as<typename T::tag_type>; }; -template <typename T> concept pointer_tagging_schema_with_aliasing = pointer_tagging_schema<T> && requires(T::dirty_pointer payload) { +template <typename T> +concept pointer_tagging_schema_with_aliasing = pointer_tagging_schema<T> && requires(T::dirty_pointer payload) { { T::recover_aliasing_pointer(payload) } noexcept -> std::same_as<typename T::clean_pointer>; }; struct no_tag { - template <typename T, typename Tag> struct schema { - using clean_pointer = T *; - using dirty_pointer = void *; - using tag_type = Tag; - - [[clang::always_inline]] static constexpr dirty_pointer encode_pointer_with_tag(clean_pointer _ptr, tag_type) noexcept { + template <typename T, typename Tag> + struct schema { + using clean_pointer = T*; + using dirty_pointer = void*; + using tag_type = Tag; + + [[clang::always_inline]] static constexpr dirty_pointer + encode_pointer_with_tag(clean_pointer _ptr, tag_type) noexcept { return (dirty_pointer)_ptr; } [[clang::always_inline]] static constexpr clean_pointer recover_pointer(dirty_pointer _ptr) noexcept { return (clean_pointer)_ptr; } - [[clang::always_inline]] static constexpr tag_type recover_value(dirty_pointer) noexcept { - return {}; - } + [[clang::always_inline]] static constexpr tag_type recover_value(dirty_pointer) noexcept { return {}; } }; }; -template <uintptr_t Mask> struct bitmask_tag { +template <uintptr_t Mask> +struct bitmask_tag { static constexpr uintptr_t _mask = Mask; - template <typename T, typename Tag> struct schema { - using clean_pointer = T *; - using dirty_pointer = void *; - using tag_type = Tag; + template <typename T, typename Tag> + struct schema { + using clean_pointer = T*; + using dirty_pointer = void*; + using tag_type = Tag; - [[clang::always_inline]] static constexpr dirty_pointer encode_pointer_with_tag(clean_pointer _ptr, tag_type _value) noexcept { - return static_cast<dirty_pointer>(__builtin_tag_pointer_mask_or((void *)(_ptr), static_cast<uintptr_t>(_value), _mask)); + [[clang::always_inline]] static constexpr dirty_pointer + encode_pointer_with_tag(clean_pointer _ptr, tag_type _value) noexcept { + return static_cast<dirty_pointer>( + __builtin_tag_pointer_mask_or((void*)(_ptr), static_cast<uintptr_t>(_value), _mask)); } [[clang::always_inline]] static constexpr clean_pointer recover_pointer(dirty_pointer _ptr) noexcept { - return static_cast<clean_pointer>(__builtin_tag_pointer_mask((void *)_ptr, ~_mask)); + return static_cast<clean_pointer>(__builtin_tag_pointer_mask((void*)_ptr, ~_mask)); } [[clang::always_inline]] static constexpr tag_type recover_value(dirty_pointer _ptr) noexcept { - return static_cast<tag_type>(__builtin_tag_pointer_mask_as_int((void *)_ptr, _mask)); + return static_cast<tag_type>(__builtin_tag_pointer_mask_as_int((void*)_ptr, _mask)); } }; }; -template <unsigned Alignment> struct custom_alignment_tag { +template <unsigned Alignment> +struct custom_alignment_tag { static constexpr uintptr_t mask = (static_cast<uintptr_t>(1u) << static_cast<uintptr_t>(Alignment)) - 1ull; - template <typename T, typename Tag> using schema = typename bitmask_tag<mask>::template schema<T, Tag>; + template <typename T, typename Tag> + using schema = typename bitmask_tag<mask>::template schema<T, Tag>; }; struct alignment_low_bits_tag { - template <typename T> static constexpr unsigned alignment = alignof(T); - template <typename T, typename Tag> using schema = typename custom_alignment_tag<alignment<T>>::template schema<T, Tag>; + template <typename T> + static constexpr unsigned alignment = alignof(T); + template <typename T, typename Tag> + using schema = typename custom_alignment_tag<alignment<T>>::template schema<T, Tag>; }; -template <unsigned Bits> struct shift_tag { +template <unsigned Bits> +struct shift_tag { static constexpr unsigned _shift = Bits; static constexpr uintptr_t _mask = (uintptr_t{1u} << _shift) - 1u; - template <typename T, typename Tag> struct schema { - using clean_pointer = T *; - using dirty_pointer = void *; - using tag_type = Tag; + template <typename T, typename Tag> + struct schema { + using clean_pointer = T*; + using dirty_pointer = void*; + using tag_type = Tag; - [[clang::always_inline]] static constexpr dirty_pointer encode_pointer_with_tag(clean_pointer _ptr, tag_type _value) noexcept { - return static_cast<dirty_pointer>(__builtin_tag_pointer_shift_or((void *)(_ptr), (uintptr_t)_value, _shift)); + [[clang::always_inline]] static constexpr dirty_pointer + encode_pointer_with_tag(clean_pointer _ptr, tag_type _value) noexcept { + return static_cast<dirty_pointer>(__builtin_tag_pointer_shift_or((void*)(_ptr), (uintptr_t)_value, _shift)); } [[clang::always_inline]] static constexpr clean_pointer recover_pointer(dirty_pointer _ptr) noexcept { - return static_cast<clean_pointer>(__builtin_tag_pointer_unshift((void *)_ptr, _shift)); + return static_cast<clean_pointer>(__builtin_tag_pointer_unshift((void*)_ptr, _shift)); } [[clang::always_inline]] static constexpr tag_type recover_value(dirty_pointer _ptr) noexcept { - return static_cast<tag_type>(__builtin_tag_pointer_mask_as_int((void *)_ptr, _mask)); + return static_cast<tag_type>(__builtin_tag_pointer_mask_as_int((void*)_ptr, _mask)); } }; }; struct low_byte_tag { - template <typename T, typename Tag> using schema = typename shift_tag<8>::template schema<T, Tag>; + template <typename T, typename Tag> + using schema = typename shift_tag<8>::template schema<T, Tag>; }; struct upper_byte_tag { - template <typename T> static constexpr unsigned _shift = sizeof(T *) * 8ull - 8ull; - template <typename T> static constexpr uintptr_t _mask = 0b1111'1111ull << _shift<T>; - - template <typename T, typename Tag> using schema = typename bitmask_tag<_mask<T>>::template schema<T, Tag>; + template <typename T> + static constexpr unsigned _shift = sizeof(T*) * 8ull - 8ull; + template <typename T> + static constexpr uintptr_t _mask = 0b1111'1111ull << _shift<T>; + + template <typename T, typename Tag> + using schema = typename bitmask_tag<_mask<T>>::template schema<T, Tag>; }; -struct upper_byte_shifted_tag: upper_byte_tag { - template <typename T, typename Tag> struct schema { - using _underlying_schema = typename upper_byte_tag::template schema<T, uintptr_t>; +struct upper_byte_shifted_tag : upper_byte_tag { + template <typename T, typename Tag> + struct schema { + using _underlying_schema = typename upper_byte_tag::template schema<T, uintptr_t>; static constexpr unsigned _shift = upper_byte_tag::template _shift<T>; - - using clean_pointer = T *; - using dirty_pointer = void *; - using tag_type = Tag; - - [[clang::always_inline]] static constexpr dirty_pointer encode_pointer_with_tag(clean_pointer _ptr, tag_type _value) noexcept { + + using clean_pointer = T*; + using dirty_pointer = void*; + using tag_type = Tag; + + [[clang::always_inline]] static constexpr dirty_pointer + encode_pointer_with_tag(clean_pointer _ptr, tag_type _value) noexcept { return _underlying_schema::encode_pointer_with_tag(_ptr, static_cast<uintptr_t>(_value) << _shift); } [[clang::always_inline]] static constexpr clean_pointer recover_pointer(dirty_pointer _ptr) noexcept { @@ -142,213 +162,218 @@ struct upper_byte_shifted_tag: upper_byte_tag { }; }; - - // forward declaration -template <typename _T, typename _Tag = uintptr_t, typename _Schema = alignment_low_bits_tag> class tagged_ptr; - +template <typename _T, typename _Tag = uintptr_t, typename _Schema = alignment_low_bits_tag> +class tagged_ptr; -template <typename _Schema, typename _T, typename _Tag = uintptr_t> constexpr auto tag_ptr(_T * _ptr, _Tag _tag = {}) noexcept { +template <typename _Schema, typename _T, typename _Tag = uintptr_t> +constexpr auto tag_ptr(_T* _ptr, _Tag _tag = {}) noexcept { return tagged_ptr<_T, _Tag, _Schema>{_ptr, _tag}; } -template <typename _T, typename _Tag, typename _Schema = alignment_low_bits_tag> constexpr auto tagged_pointer_cast(typename _Schema::template schema<_T, _Tag>::dirty_pointer _ptr) noexcept -> tagged_ptr<_T, _Tag, _Schema> { +template <typename _T, typename _Tag, typename _Schema = alignment_low_bits_tag> +constexpr auto tagged_pointer_cast(typename _Schema::template schema<_T, _Tag>::dirty_pointer _ptr) noexcept + -> tagged_ptr<_T, _Tag, _Schema> { using result_type = tagged_ptr<_T, _Tag, _Schema>; return result_type{typename result_type::already_tagged_tag{_ptr}}; } -template <typename _Schema2, typename _T, typename _Tag, typename _Schema> constexpr auto scheme_pointer_cast(tagged_ptr<_T, _Tag, _Schema> in) noexcept { +template <typename _Schema2, typename _T, typename _Tag, typename _Schema> +constexpr auto scheme_pointer_cast(tagged_ptr<_T, _Tag, _Schema> in) noexcept { return tagged_ptr<_T, _Tag, _Schema2>{in.pointer(), in.tag()}; } -template <typename _Y, typename _T, typename _Tag, typename _Schema> constexpr auto const_pointer_cast(tagged_ptr<_T, _Tag, _Schema> in) noexcept { +template <typename _Y, typename _T, typename _Tag, typename _Schema> +constexpr auto const_pointer_cast(tagged_ptr<_T, _Tag, _Schema> in) noexcept { // TODO we can just use native pointer here return tagged_ptr<_Y, _Tag, _Schema>{const_cast<_Y*>(in.pointer()), in.tag()}; } -template <typename _Y, typename _T, typename _Tag, typename _Schema> constexpr auto static_pointer_cast(tagged_ptr<_T, _Tag, _Schema> in) noexcept { +template <typename _Y, typename _T, typename _Tag, typename _Schema> +constexpr auto static_pointer_cast(tagged_ptr<_T, _Tag, _Schema> in) noexcept { return tagged_ptr<_Y, _Tag, _Schema>{static_cast<_Y*>(in.pointer()), in.tag()}; } -template <typename _Y, typename _T, typename _Tag, typename _Schema> constexpr auto dynamic_pointer_cast(tagged_ptr<_T, _Tag, _Schema> in) noexcept { +template <typename _Y, typename _T, typename _Tag, typename _Schema> +constexpr auto dynamic_pointer_cast(tagged_ptr<_T, _Tag, _Schema> in) noexcept { return tagged_ptr<_Y, _Tag, _Schema>{dynamic_cast<_Y*>(in.pointer()), in.tag()}; } -template <typename _Y, typename _T, typename _Tag, typename _Schema> auto reinterpret_pointer_cast(tagged_ptr<_T, _Tag, _Schema> in) noexcept { +template <typename _Y, typename _T, typename _Tag, typename _Schema> +auto reinterpret_pointer_cast(tagged_ptr<_T, _Tag, _Schema> in) noexcept { return tagged_ptr<_Y, _Tag, _Schema>{reinterpret_cast<_Y*>(in.pointer()), in.tag()}; } - // wrapper class containing the pointer value and provides access -template <typename _T, typename _Tag, typename _Schema> class tagged_ptr { +template <typename _T, typename _Tag, typename _Schema> +class tagged_ptr { public: - using schema = typename _Schema::template schema<_T, _Tag>; + using schema = typename _Schema::template schema<_T, _Tag>; using dirty_pointer = typename schema::dirty_pointer; using clean_pointer = typename schema::clean_pointer; - using tag_type = typename schema::tag_type; - - using value_type = std::remove_cvref_t<decltype(*std::declval<clean_pointer>())>; + using tag_type = typename schema::tag_type; + + using value_type = std::remove_cvref_t<decltype(*std::declval<clean_pointer>())>; using difference_type = typename std::pointer_traits<clean_pointer>::difference_type; - - - template <typename _Y> using rebind = tagged_ptr<_Y, _Tag, _Schema>; - + + template <typename _Y> + using rebind = tagged_ptr<_Y, _Tag, _Schema>; + private: - dirty_pointer _pointer{nullptr}; - - friend constexpr auto tagged_pointer_cast<_T, _Tag, _Schema>(typename _Schema::template schema<_T, _Tag>::dirty_pointer ptr) noexcept -> tagged_ptr<_T, _Tag, _Schema>; - + + friend constexpr auto tagged_pointer_cast<_T, _Tag, _Schema>( + typename _Schema::template schema<_T, _Tag>::dirty_pointer ptr) noexcept -> tagged_ptr<_T, _Tag, _Schema>; + struct already_tagged_tag { dirty_pointer _ptr; }; - + // special hidden constructor to allow constructing unsafely - [[clang::always_inline]] constexpr tagged_ptr(already_tagged_tag _in) noexcept: _pointer{_in._ptr} { } - - template <typename _Y, typename _T2, typename _Tag2, typename _Schema2> constexpr auto const_pointer_cast(tagged_ptr<_T2, _Tag2, _Schema2> in) noexcept -> rebind<_T>; - + [[clang::always_inline]] constexpr tagged_ptr(already_tagged_tag _in) noexcept : _pointer{_in._ptr} {} + + template <typename _Y, typename _T2, typename _Tag2, typename _Schema2> + constexpr auto const_pointer_cast(tagged_ptr<_T2, _Tag2, _Schema2> in) noexcept -> rebind<_T>; + public: tagged_ptr() = default; - consteval tagged_ptr(nullptr_t) noexcept: _pointer{nullptr} { } - tagged_ptr(const tagged_ptr &) = default; - tagged_ptr(tagged_ptr &&) = default; - ~tagged_ptr() = default; - tagged_ptr & operator=(const tagged_ptr &) = default; - tagged_ptr & operator=(tagged_ptr &&) = default; - - [[clang::always_inline]] explicit constexpr tagged_ptr(clean_pointer _ptr, tag_type _tag = {}) noexcept: _pointer{schema::encode_pointer_with_tag(_ptr, _tag)} { + consteval tagged_ptr(nullptr_t) noexcept : _pointer{nullptr} {} + tagged_ptr(const tagged_ptr&) = default; + tagged_ptr(tagged_ptr&&) = default; + ~tagged_ptr() = default; + tagged_ptr& operator=(const tagged_ptr&) = default; + tagged_ptr& operator=(tagged_ptr&&) = default; + + [[clang::always_inline]] explicit constexpr tagged_ptr(clean_pointer _ptr, tag_type _tag = {}) noexcept + : _pointer{schema::encode_pointer_with_tag(_ptr, _tag)} { _LIBCPP_ASSERT_SEMANTIC_REQUIREMENT(pointer() == _ptr, "pointer must be recoverable after untagging"); - _LIBCPP_ASSERT_SEMANTIC_REQUIREMENT(tag() == _tag, "stored tag must be recoverable and within schema provided bit capacity"); - } + _LIBCPP_ASSERT_SEMANTIC_REQUIREMENT( + tag() == _tag, "stored tag must be recoverable and within schema provided bit capacity"); + } // accessors - [[clang::always_inline]] constexpr decltype(auto) operator*() const noexcept { - return *pointer(); - } - - [[clang::always_inline]] constexpr clean_pointer operator->() const noexcept { - return pointer(); - } - - template <typename...Ts> [[clang::always_inline]] [[clang::always_inline]] constexpr decltype(auto) operator[](Ts... args) const noexcept requires std::is_array_v<value_type> && (sizeof...(Ts) == std::rank_v<value_type>) { + [[clang::always_inline]] constexpr decltype(auto) operator*() const noexcept { return *pointer(); } + + [[clang::always_inline]] constexpr clean_pointer operator->() const noexcept { return pointer(); } + + template <typename... Ts> + [[clang::always_inline]] [[clang::always_inline]] constexpr decltype(auto) operator[](Ts... args) const noexcept + requires std::is_array_v<value_type> && (sizeof...(Ts) == std::rank_v<value_type>) + { return (*pointer())[args...]; } - - [[clang::always_inline]] constexpr decltype(auto) operator[](difference_type diff) const noexcept requires (!std::is_array_v<value_type>) { + + [[clang::always_inline]] constexpr decltype(auto) operator[](difference_type diff) const noexcept + requires(!std::is_array_v<value_type>) + { return *(pointer() + diff); } - + // swap - [[clang::always_inline]] friend constexpr void swap(tagged_ptr & lhs, tagged_ptr & rhs) noexcept { + [[clang::always_inline]] friend constexpr void swap(tagged_ptr& lhs, tagged_ptr& rhs) noexcept { std::swap(lhs._pointer, rhs._pointer); } - + // modifiers for tag - [[clang::always_inline]] constexpr auto & set(tag_type new_tag) noexcept { + [[clang::always_inline]] constexpr auto& set(tag_type new_tag) noexcept { // this is here so I can avoid checks // TODO we should be able to check what bits available _pointer = schema::encode_pointer_with_tag(pointer(), new_tag); return *this; } - - [[clang::always_inline]] constexpr auto & set_union(tag_type addition) noexcept { - return set(tag() | addition); - } - - [[clang::always_inline]] constexpr auto & set_difference(tag_type mask) noexcept { + + [[clang::always_inline]] constexpr auto& set_union(tag_type addition) noexcept { return set(tag() | addition); } + + [[clang::always_inline]] constexpr auto& set_difference(tag_type mask) noexcept { return set(tag() & (~static_cast<uintptr_t>(mask))); } - - [[clang::always_inline]] constexpr auto & set_intersection(tag_type mask) noexcept { - return set(tag() & mask); - } - - [[clang::always_inline]] constexpr auto & set_all() noexcept { + + [[clang::always_inline]] constexpr auto& set_intersection(tag_type mask) noexcept { return set(tag() & mask); } + + [[clang::always_inline]] constexpr auto& set_all() noexcept { return set(static_cast<tag_type>(0xFFFFFFFF'FFFFFFFFull)); } // modifiers for pointer - [[clang::always_inline]] constexpr auto & operator++() noexcept { - _pointer = tagged_ptr{pointer()+1u, tag()}._pointer; + [[clang::always_inline]] constexpr auto& operator++() noexcept { + _pointer = tagged_ptr{pointer() + 1u, tag()}._pointer; return *this; } - + [[clang::always_inline]] constexpr auto operator++(int) noexcept { auto copy = auto(*this); this->operator++(); return copy; } - - [[clang::always_inline]] constexpr auto & operator+=(difference_type diff) noexcept { - _pointer = tagged_ptr{pointer()+diff, tag()}._pointer; + + [[clang::always_inline]] constexpr auto& operator+=(difference_type diff) noexcept { + _pointer = tagged_ptr{pointer() + diff, tag()}._pointer; return *this; } - + [[clang::always_inline]] friend constexpr auto operator+(tagged_ptr lhs, difference_type diff) noexcept { lhs += diff; return lhs; } - + [[clang::always_inline]] friend constexpr auto operator+(difference_type diff, tagged_ptr rhs) noexcept { rhs += diff; return rhs; } - + [[clang::always_inline]] friend constexpr auto operator-(tagged_ptr lhs, difference_type diff) noexcept { lhs -= diff; return lhs; } - + [[clang::always_inline]] friend constexpr auto operator-(difference_type diff, tagged_ptr rhs) noexcept { rhs -= diff; return rhs; } - - [[clang::always_inline]] constexpr auto & operator-=(difference_type diff) noexcept { - _pointer = tagged_ptr{pointer()-diff, tag()}._pointer; + + [[clang::always_inline]] constexpr auto& operator-=(difference_type diff) noexcept { + _pointer = tagged_ptr{pointer() - diff, tag()}._pointer; return *this; } - - [[clang::always_inline]] constexpr auto & operator--() noexcept { - _pointer = tagged_ptr{pointer()-1u, tag()}._pointer; + + [[clang::always_inline]] constexpr auto& operator--() noexcept { + _pointer = tagged_ptr{pointer() - 1u, tag()}._pointer; return *this; } - + [[clang::always_inline]] constexpr auto operator--(int) noexcept { auto copy = auto(*this); this->operator--(); return copy; } - + // observers constexpr dirty_pointer unsafe_dirty_pointer() const noexcept { // this function is not intentionally constexpr, as it is needed only to interact with // existing runtime code return _pointer; - } - + } + static constexpr bool support_aliasing_masking = pointer_tagging_schema_with_aliasing<schema>; - + [[clang::always_inline]] constexpr clean_pointer aliasing_pointer() const noexcept { if constexpr (support_aliasing_masking) { if !consteval { return schema::recover_aliasing_pointer(_pointer); } } - + return schema::recover_pointer(_pointer); } - + [[clang::always_inline]] constexpr clean_pointer pointer() const noexcept { return schema::recover_pointer(_pointer); } - - [[clang::always_inline]] constexpr tag_type tag() const noexcept { - return schema::recover_value(_pointer); - } - - template <std::size_t I> [[nodiscard, clang::always_inline]] friend constexpr decltype(auto) get(tagged_ptr _pair) noexcept { + + [[clang::always_inline]] constexpr tag_type tag() const noexcept { return schema::recover_value(_pointer); } + + template <std::size_t I> + [[nodiscard, clang::always_inline]] friend constexpr decltype(auto) get(tagged_ptr _pair) noexcept { static_assert(I < 3); if constexpr (I == 0) { return _pair.pointer(); @@ -356,25 +381,23 @@ public: return _pair.tag(); } } - - [[clang::always_inline]] constexpr explicit operator bool() const noexcept { - return pointer() != nullptr; - } - + + [[clang::always_inline]] constexpr explicit operator bool() const noexcept { return pointer() != nullptr; } + [[clang::always_inline]] friend constexpr ptrdiff_t operator-(tagged_ptr lhs, tagged_ptr rhs) noexcept { return lhs.pointer() - rhs.pointer(); } - + // comparison operators [[clang::always_inline]] friend bool operator==(tagged_ptr, tagged_ptr) = default; - + struct _compare_object { clean_pointer pointer; tag_type tag; - + friend auto operator<=>(_compare_object, _compare_object) = default; }; - + [[clang::always_inline]] friend constexpr auto operator<=>(tagged_ptr lhs, tagged_ptr rhs) noexcept { return _compare_object{lhs.pointer(), lhs.tag()} <=> _compare_object{rhs.pointer(), rhs.tag()}; } @@ -390,7 +413,9 @@ public: }; // to_address specialization -template <typename _T, typename _Tag, typename _Schema> static constexpr auto to_address(tagged_ptr<_T, _Tag, _Schema> p) noexcept -> tagged_ptr<_T, _Tag, _Schema>::element_type * { +template <typename _T, typename _Tag, typename _Schema> +static constexpr auto +to_address(tagged_ptr<_T, _Tag, _Schema> p) noexcept -> tagged_ptr<_T, _Tag, _Schema>::element_type* { return p.pointer(); } @@ -398,46 +423,47 @@ template <typename _T, typename _Tag, typename _Schema> static constexpr auto to template <typename _T, typename _Tag, typename _Schema> struct _LIBCPP_TEMPLATE_VIS iterator_traits<tagged_ptr<_T, _Tag, _Schema>> { using _tagged_ptr = tagged_ptr<_T, _Tag, _Schema>; - + using iterator_category = std::random_access_iterator_tag; - using iterator_concept = std::contiguous_iterator_tag; - - using value_type = _tagged_ptr::value_type; - using reference = value_type &; - using pointer = _tagged_ptr::clean_pointer; + using iterator_concept = std::contiguous_iterator_tag; + + using value_type = _tagged_ptr::value_type; + using reference = value_type&; + using pointer = _tagged_ptr::clean_pointer; using difference_type = _tagged_ptr::difference_type; }; // pointer traits template <typename _T, typename _Tag, typename _Schema> struct _LIBCPP_TEMPLATE_VIS pointer_traits<tagged_ptr<_T, _Tag, _Schema>> { - using _tagged_ptr = tagged_ptr<_T, _Tag, _Schema>; - using pointer = _tagged_ptr::clean_pointer; - using element_type = _tagged_ptr::value_type; + using _tagged_ptr = tagged_ptr<_T, _Tag, _Schema>; + using pointer = _tagged_ptr::clean_pointer; + using element_type = _tagged_ptr::value_type; using difference_type = _tagged_ptr::difference_type; - + // what to do with this? - template <typename _Up> using rebind = typename _tagged_ptr::template rebind<_Up>; + template <typename _Up> + using rebind = typename _tagged_ptr::template rebind<_Up>; public: - _LIBCPP_HIDE_FROM_ABI constexpr static pointer pointer_to(pointer ptr) _NOEXCEPT { - return _tagged_ptr{ptr}; - } + _LIBCPP_HIDE_FROM_ABI constexpr static pointer pointer_to(pointer ptr) _NOEXCEPT { return _tagged_ptr{ptr}; } }; // we are defaulting always to low_bits schema -template <typename _T> tagged_ptr(_T *) -> tagged_ptr<_T>; -template <typename _T, typename _Tag> tagged_ptr(_T *, _Tag) -> tagged_ptr<_T, _Tag>; +template <typename _T> +tagged_ptr(_T*) -> tagged_ptr<_T>; +template <typename _T, typename _Tag> +tagged_ptr(_T*, _Tag) -> tagged_ptr<_T, _Tag>; // support for tuple protocol so we can split tagged pointer to structured bindings: // auto [ptr, tag] = tagged_ptr template <typename _T, typename _Tag, typename _Schema> -struct tuple_size<tagged_ptr<_T, _Tag, _Schema>>: std::integral_constant<std::size_t, 2> {}; +struct tuple_size<tagged_ptr<_T, _Tag, _Schema>> : std::integral_constant<std::size_t, 2> {}; template <std::size_t I, typename _T, typename _Tag, typename _Schema> struct tuple_element<I, tagged_ptr<_T, _Tag, _Schema>> { using _pair_type = tagged_ptr<_T, _Tag, _Schema>; - using type = std::conditional_t<I == 0, typename _pair_type::clean_pointer, typename _pair_type::tag_type>; + using type = std::conditional_t<I == 0, typename _pair_type::clean_pointer, typename _pair_type::tag_type>; }; _LIBCPP_END_NAMESPACE_STD `````````` </details> https://github.com/llvm/llvm-project/pull/111861 _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits