Skip to content

Conversation

@tbaederr
Copy link
Contributor

Try to do as few checks as possible. Check for builtin types only once, then look at the BuiltinType Kind. For integers, we cache the int and long size, since those are used a lot and the ASTContext::getIntWidth() call is costly.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" clang:bytecode Issues for the clang bytecode constexpr interpreter labels May 20, 2025
@llvmbot
Copy link
Member

llvmbot commented May 20, 2025

@llvm/pr-subscribers-clang

Author: Timm Baeder (tbaederr)

Changes

Try to do as few checks as possible. Check for builtin types only once, then look at the BuiltinType Kind. For integers, we cache the int and long size, since those are used a lot and the ASTContext::getIntWidth() call is costly.


Full diff: https://github.com/llvm/llvm-project/pull/140735.diff

2 Files Affected:

  • (modified) clang/lib/AST/ByteCode/Context.cpp (+84-44)
  • (modified) clang/lib/AST/ByteCode/Context.h (+3)
diff --git a/clang/lib/AST/ByteCode/Context.cpp b/clang/lib/AST/ByteCode/Context.cpp
index c70a5259b77e2..ed973dfbffcde 100644
--- a/clang/lib/AST/ByteCode/Context.cpp
+++ b/clang/lib/AST/ByteCode/Context.cpp
@@ -21,7 +21,12 @@
 using namespace clang;
 using namespace clang::interp;
 
-Context::Context(ASTContext &Ctx) : Ctx(Ctx), P(new Program(*this)) {}
+Context::Context(ASTContext &Ctx) : Ctx(Ctx), P(new Program(*this)) {
+  this->IntWidth = Ctx.getTargetInfo().getIntWidth();
+  this->LongWidth = Ctx.getTargetInfo().getLongWidth();
+  assert(Ctx.getTargetInfo().getCharWidth() == 8 &&
+         "We're assuming 8 bit chars");
+}
 
 Context::~Context() {}
 
@@ -216,55 +221,91 @@ bool Context::evaluateCharRange(State &Parent, const Expr *SizeExpr,
 
 const LangOptions &Context::getLangOpts() const { return Ctx.getLangOpts(); }
 
-std::optional<PrimType> Context::classify(QualType T) const {
-  if (T->isBooleanType())
-    return PT_Bool;
-
-  if (T->isSignedIntegerOrEnumerationType()) {
-    switch (Ctx.getIntWidth(T)) {
-    case 64:
-      return PT_Sint64;
-    case 32:
-      return PT_Sint32;
-    case 16:
-      return PT_Sint16;
-    case 8:
-      return PT_Sint8;
-    default:
-      return PT_IntAPS;
-    }
+static PrimType integralTypeToPrimTypeS(unsigned BitWidth) {
+  switch (BitWidth) {
+  case 64:
+    return PT_Sint64;
+  case 32:
+    return PT_Sint32;
+  case 16:
+    return PT_Sint16;
+  case 8:
+    return PT_Sint8;
+  default:
+    return PT_IntAPS;
   }
+  llvm_unreachable("Unhandled BitWidth");
+}
+
+static PrimType integralTypeToPrimTypeU(unsigned BitWidth) {
+  switch (BitWidth) {
+  case 64:
+    return PT_Uint64;
+  case 32:
+    return PT_Uint32;
+  case 16:
+    return PT_Uint16;
+  case 8:
+    return PT_Uint8;
+  default:
+    return PT_IntAP;
+  }
+  llvm_unreachable("Unhandled BitWidth");
+}
+
+std::optional<PrimType> Context::classify(QualType T) const {
 
-  if (T->isUnsignedIntegerOrEnumerationType()) {
-    switch (Ctx.getIntWidth(T)) {
-    case 64:
-      return PT_Uint64;
-    case 32:
-      return PT_Uint32;
-    case 16:
-      return PT_Uint16;
-    case 8:
-      return PT_Uint8;
-    case 1:
-      // Might happen for enum types.
+  if (const auto *BT = dyn_cast<BuiltinType>(T.getCanonicalType())) {
+    auto Kind = BT->getKind();
+    if (Kind == BuiltinType::Bool)
       return PT_Bool;
-    default:
-      return PT_IntAP;
-    }
+    if (Kind == BuiltinType::NullPtr)
+      return PT_Ptr;
+    if (Kind == BuiltinType::BoundMember)
+      return PT_MemberPtr;
+
+    // Just trying to avoid the ASTContext::getIntWidth call below.
+    if (Kind == BuiltinType::Int)
+      return integralTypeToPrimTypeS(this->IntWidth);
+    if (Kind == BuiltinType::UInt)
+      return integralTypeToPrimTypeU(this->IntWidth);
+    if (Kind == BuiltinType::Long)
+      return integralTypeToPrimTypeS(this->LongWidth);
+    if (Kind == BuiltinType::ULong)
+      return integralTypeToPrimTypeU(this->LongWidth);
+    if (Kind == BuiltinType::SChar || Kind == BuiltinType::Char_S ||
+        Kind == BuiltinType::Char8)
+      return integralTypeToPrimTypeS(8);
+    if (Kind == BuiltinType::UChar || Kind == BuiltinType::Char_U)
+      return integralTypeToPrimTypeU(8);
+
+    if (BT->isSignedInteger())
+      return integralTypeToPrimTypeS(Ctx.getIntWidth(T));
+    if (BT->isUnsignedInteger())
+      return integralTypeToPrimTypeU(Ctx.getIntWidth(T));
+
+    if (BT->isFloatingPoint())
+      return PT_Float;
   }
 
-  if (T->isNullPtrType())
+  if (T->isPointerOrReferenceType())
     return PT_Ptr;
 
-  if (T->isRealFloatingType())
-    return PT_Float;
+  if (T->isMemberPointerType())
+    return PT_MemberPtr;
 
-  if (T->isFunctionPointerType() || T->isFunctionReferenceType() ||
-      T->isFunctionType() || T->isBlockPointerType())
-    return PT_Ptr;
+  if (const auto *BT = T->getAs<BitIntType>()) {
+    if (BT->isSigned())
+      return integralTypeToPrimTypeS(BT->getNumBits());
+    return integralTypeToPrimTypeU(BT->getNumBits());
+  }
 
-  if (T->isPointerOrReferenceType() || T->isObjCObjectPointerType())
-    return PT_Ptr;
+  if (const auto *ET = T->getAs<EnumType>()) {
+    const auto *D = ET->getDecl();
+    if (!D->isComplete())
+      return std::nullopt;
+    return classify(D->getIntegerType());
+  }
 
   if (const auto *AT = T->getAs<AtomicType>())
     return classify(AT->getValueType());
@@ -272,9 +313,8 @@ std::optional<PrimType> Context::classify(QualType T) const {
   if (const auto *DT = dyn_cast<DecltypeType>(T))
     return classify(DT->getUnderlyingType());
 
-  if (T->isSpecificBuiltinType(BuiltinType::BoundMember) ||
-      T->isMemberPointerType())
-    return PT_MemberPtr;
+  if (T->isObjCObjectPointerType() || T->isBlockPointerType())
+    return PT_Ptr;
 
   if (T->isFixedPointType())
     return PT_FixedPoint;
diff --git a/clang/lib/AST/ByteCode/Context.h b/clang/lib/AST/ByteCode/Context.h
index fc778f9796a79..9a604ce8ebbe9 100644
--- a/clang/lib/AST/ByteCode/Context.h
+++ b/clang/lib/AST/ByteCode/Context.h
@@ -137,6 +137,9 @@ class Context final {
   std::unique_ptr<Program> P;
   /// ID identifying an evaluation.
   unsigned EvalID = 0;
+  /// Cached widths (in bits) of common types, for a faster classify().
+  unsigned IntWidth;
+  unsigned LongWidth;
 };
 
 } // namespace interp

Try to do as few checks as possible. Check for builtin types only once,
then look at the BuiltinType Kind. For integers, we cache the int and
long size, since those are used a lot and the ASTContext::getIntWidth()
call is costly.
@tbaederr tbaederr merged commit 3c8a6bc into llvm:main May 21, 2025
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:bytecode Issues for the clang bytecode constexpr interpreter clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants