-
Notifications
You must be signed in to change notification settings - Fork 15.4k
[clang][bytecode] Optimize classify() further #140735
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Member
|
@llvm/pr-subscribers-clang Author: Timm Baeder (tbaederr) ChangesTry 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:
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.
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.