Skip to content

Commit b207a0b

Browse files
nikicakiramenai
authored andcommitted
[APInt] Fix APInt constructions where value does not fit bitwidth (NFCI) (#80309)
This fixes all the places that hit the new assertion added in llvm/llvm-project#106524 in tests. That is, cases where the value passed to the APInt constructor is not an N-bit signed/unsigned integer, where N is the bit width and signedness is determined by the isSigned flag. The fixes either set the correct value for isSigned, set the implicitTrunc flag, or perform more calculations inside APInt. Note that the assertion is currently still disabled by default, so this patch is mostly NFC.
1 parent c97aade commit b207a0b

File tree

30 files changed

+101
-63
lines changed

30 files changed

+101
-63
lines changed

clang/include/clang/Sema/Sema.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6792,7 +6792,7 @@ class Sema final : public SemaBase {
67926792

67936793
ExprResult BuildPredefinedExpr(SourceLocation Loc, PredefinedIdentKind IK);
67946794
ExprResult ActOnPredefinedExpr(SourceLocation Loc, tok::TokenKind Kind);
6795-
ExprResult ActOnIntegerConstant(SourceLocation Loc, uint64_t Val);
6795+
ExprResult ActOnIntegerConstant(SourceLocation Loc, int64_t Val);
67966796

67976797
bool CheckLoopHintExpr(Expr *E, SourceLocation Loc, bool AllowZero);
67986798

clang/lib/AST/Interp/IntegralAP.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ template <bool Signed> class IntegralAP final {
6161

6262
IntegralAP(APInt V) : V(V) {}
6363
/// Arbitrary value for uninitialized variables.
64-
IntegralAP() : IntegralAP(-1, 3) {}
64+
IntegralAP() : IntegralAP(Signed ? -1 : 7, 3) {}
6565

6666
IntegralAP operator-() const { return IntegralAP(-V); }
6767
IntegralAP operator-(const IntegralAP &Other) const {
@@ -112,7 +112,10 @@ template <bool Signed> class IntegralAP final {
112112

113113
template <unsigned Bits, bool InputSigned>
114114
static IntegralAP from(Integral<Bits, InputSigned> I, unsigned BitWidth) {
115-
APInt Copy = APInt(BitWidth, static_cast<uint64_t>(I), InputSigned);
115+
// TODO: Avoid implicit trunc?
116+
// See https://github.com/llvm/llvm-project/issues/112510.
117+
APInt Copy = APInt(BitWidth, static_cast<uint64_t>(I), InputSigned,
118+
/*implicitTrunc=*/true);
116119

117120
return IntegralAP<Signed>(Copy);
118121
}

clang/lib/CodeGen/CGVTT.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,9 @@ CodeGenVTables::EmitVTTDefinition(llvm::GlobalVariable *VTT,
8585
cast<llvm::StructType>(VTable->getValueType())
8686
->getElementType(AddressPoint.VTableIndex));
8787
unsigned Offset = ComponentSize * AddressPoint.AddressPointIndex;
88-
llvm::ConstantRange InRange(llvm::APInt(32, -Offset, true),
89-
llvm::APInt(32, VTableSize - Offset, true));
88+
llvm::ConstantRange InRange(
89+
llvm::APInt(32, (int)-Offset, true),
90+
llvm::APInt(32, (int)(VTableSize - Offset), true));
9091
llvm::Constant *Init = llvm::ConstantExpr::getGetElementPtr(
9192
VTable->getValueType(), VTable, Idxs, /*InBounds=*/true, InRange);
9293

clang/lib/CodeGen/ItaniumCXXABI.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2101,8 +2101,9 @@ ItaniumCXXABI::getVTableAddressPoint(BaseSubobject Base,
21012101
unsigned VTableSize =
21022102
ComponentSize * Layout.getVTableSize(AddressPoint.VTableIndex);
21032103
unsigned Offset = ComponentSize * AddressPoint.AddressPointIndex;
2104-
llvm::ConstantRange InRange(llvm::APInt(32, -Offset, true),
2105-
llvm::APInt(32, VTableSize - Offset, true));
2104+
llvm::ConstantRange InRange(
2105+
llvm::APInt(32, (int)-Offset, true),
2106+
llvm::APInt(32, (int)(VTableSize - Offset), true));
21062107
return llvm::ConstantExpr::getGetElementPtr(
21072108
VTable->getValueType(), VTable, Indices, /*InBounds=*/true, InRange);
21082109
}

clang/lib/Parse/ParseInit.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -436,9 +436,9 @@ ExprResult Parser::createEmbedExpr() {
436436
ASTContext &Context = Actions.getASTContext();
437437
SourceLocation StartLoc = ConsumeAnnotationToken();
438438
if (Data->BinaryData.size() == 1) {
439-
Res = IntegerLiteral::Create(Context,
440-
llvm::APInt(CHAR_BIT, Data->BinaryData.back()),
441-
Context.UnsignedCharTy, StartLoc);
439+
Res = IntegerLiteral::Create(
440+
Context, llvm::APInt(CHAR_BIT, (unsigned char)Data->BinaryData.back()),
441+
Context.UnsignedCharTy, StartLoc);
442442
} else {
443443
auto CreateStringLiteralFromStringRef = [&](StringRef Str, QualType Ty) {
444444
llvm::APSInt ArraySize =

clang/lib/Sema/SemaExpr.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3575,9 +3575,10 @@ ExprResult Sema::ActOnCharacterConstant(const Token &Tok, Scope *UDLScope) {
35753575
Lit, Tok.getLocation());
35763576
}
35773577

3578-
ExprResult Sema::ActOnIntegerConstant(SourceLocation Loc, uint64_t Val) {
3578+
ExprResult Sema::ActOnIntegerConstant(SourceLocation Loc, int64_t Val) {
35793579
unsigned IntSize = Context.getTargetInfo().getIntWidth();
3580-
return IntegerLiteral::Create(Context, llvm::APInt(IntSize, Val),
3580+
return IntegerLiteral::Create(Context,
3581+
llvm::APInt(IntSize, Val, /*isSigned=*/true),
35813582
Context.IntTy, Loc);
35823583
}
35833584

clang/lib/Sema/SemaOpenMP.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5712,7 +5712,9 @@ StmtResult SemaOpenMP::ActOnOpenMPCanonicalLoop(Stmt *AStmt) {
57125712
llvm_unreachable("unhandled unary increment operator");
57135713
}
57145714
Step = IntegerLiteral::Create(
5715-
Ctx, llvm::APInt(Ctx.getIntWidth(LogicalTy), Direction), LogicalTy, {});
5715+
Ctx,
5716+
llvm::APInt(Ctx.getIntWidth(LogicalTy), Direction, /*isSigned=*/true),
5717+
LogicalTy, {});
57165718
} else if (auto *IncBin = dyn_cast<BinaryOperator>(Inc)) {
57175719
if (IncBin->getOpcode() == BO_AddAssign) {
57185720
Step = IncBin->getRHS();

lldb/source/Expression/DWARFExpression.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -859,10 +859,12 @@ llvm::Expected<Value> DWARFExpression::Evaluate(
859859
// TODO: Implement a real typed stack, and store the genericness of the value
860860
// there.
861861
auto to_generic = [&](auto v) {
862+
// TODO: Avoid implicit trunc?
863+
// See https://github.com/llvm/llvm-project/issues/112510.
862864
bool is_signed = std::is_signed<decltype(v)>::value;
863-
return Scalar(llvm::APSInt(
864-
llvm::APInt(8 * opcodes.GetAddressByteSize(), v, is_signed),
865-
!is_signed));
865+
return Scalar(llvm::APSInt(llvm::APInt(8 * opcodes.GetAddressByteSize(), v,
866+
is_signed, /*implicitTrunc=*/true),
867+
!is_signed));
866868
};
867869

868870
// The default kind is a memory location. This is updated by any

llvm/include/llvm/ADT/APFixedPoint.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,9 @@ class APFixedPoint {
160160
}
161161

162162
APFixedPoint(uint64_t Val, const FixedPointSemantics &Sema)
163-
: APFixedPoint(APInt(Sema.getWidth(), Val, Sema.isSigned()), Sema) {}
163+
: APFixedPoint(APInt(Sema.getWidth(), Val, Sema.isSigned(),
164+
/*implicitTrunc=*/true),
165+
Sema) {}
164166

165167
// Zero initialization.
166168
APFixedPoint(const FixedPointSemantics &Sema) : APFixedPoint(0, Sema) {}

llvm/lib/Analysis/ConstantFolding.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -889,7 +889,8 @@ Constant *SymbolicallyEvaluateGEP(const GEPOperator *GEP,
889889
APInt Offset = APInt(
890890
BitWidth,
891891
DL.getIndexedOffsetInType(
892-
SrcElemTy, ArrayRef((Value *const *)Ops.data() + 1, Ops.size() - 1)));
892+
SrcElemTy, ArrayRef((Value *const *)Ops.data() + 1, Ops.size() - 1)),
893+
/*isSigned=*/true, /*implicitTrunc=*/true);
893894

894895
std::optional<ConstantRange> InRange = GEP->getInRange();
895896
if (InRange)

0 commit comments

Comments
 (0)