Unverified Commit 4cc791bc authored by philnik777's avatar philnik777 Committed by GitHub
Browse files

[Clang] Add __datasizeof (#67805)

The data size is required for implementing the `memmove` optimization
for `std::copy`, `std::move` etc. correctly as well as replacing
`__compressed_pair` with `[[no_unique_address]]` in libc++. Since the
compiler already knows the data size, we can avoid some complexity by
exposing that information.
parent 0515ccc0
Loading
Loading
Loading
Loading
+12 −0
Original line number Diff line number Diff line
@@ -424,6 +424,18 @@ Builtin Macros
  "UTF-16" or "UTF-32" (but may change in the future if the
  ``-fwide-exec-charset="Encoding-Name"`` option is implemented.)

Implementation-defined keywords
===============================

__datasizeof
------------

``__datasizeof`` behaves like ``sizeof``, except that it returns the size of the
type ignoring tail padding.

..
  FIXME: This should list all the keyword extensions

.. _langext-vectors:

Vectors and Extended Vectors
+2 −0
Original line number Diff line number Diff line
@@ -217,6 +217,8 @@ Non-comprehensive list of changes in this release
  (e.g., ``uint16x8_t``), this returns the constant number of elements at compile-time.
  For scalable vectors, e.g., SVE or RISC-V V, the number of elements is not known at compile-time and is
  determined at runtime.
* The ``__datasizeof`` keyword has been added. It is similar to ``sizeof``
  except that it returns the size of a type ignoring tail padding.

New Compiler Flags
------------------
+1 −0
Original line number Diff line number Diff line
@@ -277,6 +277,7 @@ EXTENSION(gnu_asm_goto_with_outputs_full, LangOpts.GNUAsm)
EXTENSION(matrix_types, LangOpts.MatrixTypes)
EXTENSION(matrix_types_scalar_division, true)
EXTENSION(cxx_attributes_on_using_declarations, LangOpts.CPlusPlus11)
EXTENSION(datasizeof, LangOpts.CPlusPlus)

FEATURE(builtin_headers_in_system_modules, LangOpts.BuiltinHeadersInSystemModules)
FEATURE(cxx_abi_relative_vtable, LangOpts.CPlusPlus && LangOpts.RelativeCXXABIVTables)
+1 −0
Original line number Diff line number Diff line
@@ -310,6 +310,7 @@ KEYWORD(return , KEYALL)
KEYWORD(short                       , KEYALL)
KEYWORD(signed                      , KEYALL)
UNARY_EXPR_OR_TYPE_TRAIT(sizeof, SizeOf, KEYALL)
UNARY_EXPR_OR_TYPE_TRAIT(__datasizeof, DataSizeOf, KEYCXX)
KEYWORD(static                      , KEYALL)
KEYWORD(struct                      , KEYALL)
KEYWORD(switch                      , KEYALL)
+16 −4
Original line number Diff line number Diff line
@@ -3184,9 +3184,14 @@ static bool HandleLValueIndirectMember(EvalInfo &Info, const Expr *E,
  return true;
}
enum class SizeOfType {
  SizeOf,
  DataSizeOf,
};
/// Get the size of the given type in char units.
static bool HandleSizeof(EvalInfo &Info, SourceLocation Loc,
                         QualType Type, CharUnits &Size) {
static bool HandleSizeof(EvalInfo &Info, SourceLocation Loc, QualType Type,
                         CharUnits &Size, SizeOfType SOT = SizeOfType::SizeOf) {
  // sizeof(void), __alignof__(void), sizeof(function) = 1 as a gcc
  // extension.
  if (Type->isVoidType() || Type->isFunctionType()) {
@@ -3206,7 +3211,10 @@ static bool HandleSizeof(EvalInfo &Info, SourceLocation Loc,
    return false;
  }
  if (SOT == SizeOfType::SizeOf)
    Size = Info.Ctx.getTypeSizeInChars(Type);
  else
    Size = Info.Ctx.getTypeInfoDataSizeInChars(Type).Width;
  return true;
}
@@ -13689,6 +13697,7 @@ bool IntExprEvaluator::VisitUnaryExprOrTypeTraitExpr(
      return Success(1, E);
  }
  case UETT_DataSizeOf:
  case UETT_SizeOf: {
    QualType SrcTy = E->getTypeOfArgument();
    // C++ [expr.sizeof]p2: "When applied to a reference or a reference type,
@@ -13697,8 +13706,11 @@ bool IntExprEvaluator::VisitUnaryExprOrTypeTraitExpr(
      SrcTy = Ref->getPointeeType();
    CharUnits Sizeof;
    if (!HandleSizeof(Info, E->getExprLoc(), SrcTy, Sizeof))
    if (!HandleSizeof(Info, E->getExprLoc(), SrcTy, Sizeof,
                      E->getKind() == UETT_DataSizeOf ? SizeOfType::DataSizeOf
                                                      : SizeOfType::SizeOf)) {
      return false;
    }
    return Success(Sizeof, E);
  }
  case UETT_OpenMPRequiredSimdAlign:
Loading