Skip to content

Commit d1ce09d

Browse files
committed
Fixed regressions in tests caused by changes upstream.
The relevant PR is #20200
1 parent cab9142 commit d1ce09d

File tree

2 files changed

+32
-11
lines changed

2 files changed

+32
-11
lines changed

lib/SILOptimizer/Mandatory/TFConstExpr.cpp

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -457,17 +457,38 @@ ConstExprFunctionState::computeConstantValueBuiltin(BuiltinInst *inst) {
457457
auto dstBitWidth =
458458
builtin.Types[1]->castTo<BuiltinIntegerType>()->getGreatestWidth();
459459

460-
APInt result = operandVal.trunc(dstBitWidth);
460+
// Get source type and bit width.
461+
auto SrcTy = builtin.Types[0]->castTo<AnyBuiltinIntegerType>();
462+
assert((srcSigned || !isa<BuiltinIntegerLiteralType>(SrcTy)) &&
463+
"only the signed intrinsics can be used with integer literals");
464+
465+
assert((dstBitWidth < srcBitWidth || !SrcTy->getWidth().isFixedWidth()) &&
466+
"preconditions on builtin trunc operations should prevent"
467+
"fixed-width truncations that actually extend");
468+
APInt result;
469+
bool overflowed;
470+
if (dstBitWidth > srcBitWidth) {
471+
// The only way a true extension can overflow is if the value is
472+
// negative and the result is unsigned.
473+
overflowed = (srcSigned && !dstSigned && operandVal.isNegative());
474+
result =
475+
srcSigned ? operandVal.sext(dstBitWidth) : operandVal.zext(dstBitWidth);
476+
} else if (dstBitWidth == srcBitWidth) {
477+
// A same-width change can overflow if the top bit disagrees.
478+
overflowed = (srcSigned != dstSigned && operandVal.isNegative());
479+
} else {
480+
// Truncate the result and check for overflow.
481+
result = operandVal.trunc(dstBitWidth);
461482

462-
// Compute the overflow by re-extending the value back to its source and
463-
// checking for loss of value.
464-
APInt reextended =
483+
// Compute the overflow by re-extending the value back to its source and
484+
// checking for loss of value.
485+
APInt reextended =
465486
dstSigned ? result.sext(srcBitWidth) : result.zext(srcBitWidth);
466-
bool overflowed = (operandVal != reextended);
467-
468-
if (builtin.ID == BuiltinValueKind::UToSCheckedTrunc)
469-
overflowed |= result.isSignBitSet();
487+
overflowed = (operandVal != reextended);
470488

489+
if (builtin.ID == BuiltinValueKind::UToSCheckedTrunc)
490+
overflowed |= result.isSignBitSet();
491+
}
471492
if (overflowed)
472493
return evaluator.getUnknown(SILValue(inst), UnknownReason::Overflow);
473494

lib/SILOptimizer/Mandatory/TFDifferentiation.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1619,9 +1619,9 @@ static void convertIntToIndirectExpressible(intmax_t scalar,
16191619
auto *intLitTypeDecl = intLitTy->getAnyNominal();
16201620
assert(intLitTypeDecl);
16211621
// %1 = float_literal $Builtin.FPIEEE80, <value>
1622-
auto builtinIntegerTy = SILType::getBuiltinIntegerType(2048, astCtx);
1623-
auto builtinInteger = builder.createIntegerLiteral(
1624-
loc, builtinIntegerTy, APInt(2048, scalar));
1622+
auto builtinIntegerLiteralTy = SILType::getBuiltinIntegerLiteralType(astCtx);
1623+
auto builtinInteger =
1624+
builder.createIntegerLiteral(loc, builtinIntegerLiteralTy, scalar);
16251625
// %2 = metatype $@thin <target type>.IntegerLiteralType.Type
16261626
auto intLitMetatypeTy = SILType::getPrimitiveObjectType(
16271627
CanMetatypeType::get(intLitTy, MetatypeRepresentation::Thick));

0 commit comments

Comments
 (0)