https://github.com/tbaederr created https://github.com/llvm/llvm-project/pull/237860
Add `getActiveBits()` to primitive integral types so we don't have to go through `APSInt`. >From 06df08295333d07011c7ec3966653cac9487d24f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timm=20B=C3=A4der?= <[email protected]> Date: Mon, 2 Nov 2026 08:33:08 +0100 Subject: [PATCH] [clang][bytecode] Resolve FIXME comment in CheckArraySize --- clang/lib/AST/ByteCode/Boolean.h | 1 + clang/lib/AST/ByteCode/Char.h | 1 + clang/lib/AST/ByteCode/Integral.h | 5 +++++ clang/lib/AST/ByteCode/IntegralAP.h | 1 + clang/lib/AST/ByteCode/Interp.h | 5 ++--- clang/lib/AST/ByteCode/InterpHelpers.h | 25 ++++++++++++------------- 6 files changed, 22 insertions(+), 16 deletions(-) diff --git a/clang/lib/AST/ByteCode/Boolean.h b/clang/lib/AST/ByteCode/Boolean.h index 39510ae94b35f..28dd8b0c55f06 100644 --- a/clang/lib/AST/ByteCode/Boolean.h +++ b/clang/lib/AST/ByteCode/Boolean.h @@ -67,6 +67,7 @@ class Boolean final { } unsigned countLeadingZeros() const { return V ? 0 : 1; } + unsigned getActiveBits() const { return V; } Boolean truncate(unsigned TruncBits) const { return *this; } diff --git a/clang/lib/AST/ByteCode/Char.h b/clang/lib/AST/ByteCode/Char.h index 79219f2a3adb6..6b32a9dc5841d 100644 --- a/clang/lib/AST/ByteCode/Char.h +++ b/clang/lib/AST/ByteCode/Char.h @@ -136,6 +136,7 @@ template <bool Signed> class Char final { static_cast<typename AsUnsigned::ReprT>(V)); llvm_unreachable("Don't call countLeadingZeros() on negative values."); } + unsigned getActiveBits() const { return 8 - countLeadingZeros(); } static bool increment(Char A, Char *R) { return add(A, Char(ReprT(1)), A.bitWidth(), R); diff --git a/clang/lib/AST/ByteCode/Integral.h b/clang/lib/AST/ByteCode/Integral.h index 40ab7ab7841c6..46267315e0458 100644 --- a/clang/lib/AST/ByteCode/Integral.h +++ b/clang/lib/AST/ByteCode/Integral.h @@ -283,6 +283,11 @@ template <unsigned Bits, bool Signed> class Integral final { llvm_unreachable("Don't call countLeadingZeros() on negative values."); } + unsigned getActiveBits() const { + assert(isNumber()); + return Bits - countLeadingZeros(); + } + Integral truncate(unsigned TruncBits) const { assert(TruncBits >= 1); if (TruncBits >= Bits) diff --git a/clang/lib/AST/ByteCode/IntegralAP.h b/clang/lib/AST/ByteCode/IntegralAP.h index a35ab4d77c8fa..056c9024a6ff6 100644 --- a/clang/lib/AST/ByteCode/IntegralAP.h +++ b/clang/lib/AST/ByteCode/IntegralAP.h @@ -177,6 +177,7 @@ template <bool Signed> class IntegralAP final { bool isMinusOne() const { return Signed && getValue().isAllOnes(); } unsigned countLeadingZeros() const { return getValue().countl_zero(); } + unsigned getActiveBits() const { return getValue().getActiveBits(); } void print(llvm::raw_ostream &OS) const { getValue().print(OS, Signed); } std::string toDiagnosticString(const ASTContext &Ctx) const { diff --git a/clang/lib/AST/ByteCode/Interp.h b/clang/lib/AST/ByteCode/Interp.h index a365df655179f..412857cce5eaf 100644 --- a/clang/lib/AST/ByteCode/Interp.h +++ b/clang/lib/AST/ByteCode/Interp.h @@ -2960,12 +2960,11 @@ bool CastAPS(InterpState &S, uint32_t BitWidth) { // (i.e. truncation would change the value). template <PrimType Name, class T = typename PrimConv<Name>::T> bool CastAPToOffsetIndex(InterpState &S, CodePtr OpPC) { - T Source = S.Stk.pop<T>(); - APSInt Val = Source.toAPSInt(); + T Val = S.Stk.pop<T>(); if (Val.isNegative() || Val.getActiveBits() > 63) return Invalid(S, OpPC); S.Stk.push<Integral<64, true>>( - Integral<64, true>::from((int64_t)Val.getZExtValue())); + Integral<64, true>::from(static_cast<uint64_t>(Val))); return true; } diff --git a/clang/lib/AST/ByteCode/InterpHelpers.h b/clang/lib/AST/ByteCode/InterpHelpers.h index 8a99406eb1b67..ae34fca812c5e 100644 --- a/clang/lib/AST/ByteCode/InterpHelpers.h +++ b/clang/lib/AST/ByteCode/InterpHelpers.h @@ -157,33 +157,32 @@ bool CheckArraySize(InterpState &S, CodePtr OpPC, SizeT NumElements, if (ElemSize == 0) return true; - // FIXME: Both the SizeT::from() as well as the - // NumElements.toAPSInt() in this function are rather expensive. - // Can't be too many elements if the bitwidth of NumElements is lower than // that of Descriptor::MaxArrayElemBytes. if ((NumElements.bitWidth() - NumElements.isSigned()) < (sizeof(Descriptor::MaxArrayElemBytes) * 8)) return true; + // Negative sizes aren't ok. + if (NumElements.isNegative()) { + if (!IsNoThrow) + S.FFDiag(S.Current->getSource(OpPC), diag::note_constexpr_new_negative) + << NumElements.toDiagnosticString(S.getASTContext()); + return false; + } + // FIXME: GH63562 // APValue stores array extents as unsigned, // so anything that is greater that unsigned would overflow when // constructing the array, we catch this here. SizeT MaxElements = SizeT::from(Descriptor::MaxArrayElemBytes / ElemSize); assert(MaxElements.isPositive()); - if (NumElements.toAPSInt().getActiveBits() > + if (NumElements.getActiveBits() > ConstantArrayType::getMaxSizeBits(S.getASTContext()) || NumElements > MaxElements) { - if (!IsNoThrow) { - if (NumElements.isSigned() && NumElements.isNegative()) { - S.FFDiag(S.Current->getSource(OpPC), diag::note_constexpr_new_negative) - << NumElements.toDiagnosticString(S.getASTContext()); - } else { - S.FFDiag(S.Current->getSource(OpPC), diag::note_constexpr_new_too_large) - << NumElements.toDiagnosticString(S.getASTContext()); - } - } + if (!IsNoThrow) + S.FFDiag(S.Current->getSource(OpPC), diag::note_constexpr_new_too_large) + << NumElements.toDiagnosticString(S.getASTContext()); return false; } return true; _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
