Skip to content

Commit 95ff425

Browse files
committed
Revert "[Diagnostics & Tests] Add diagnostics for detecting invalid conversions form Floating point to integers"
This reverts commit 957dc37. Reason: Float80 doesn't exist on all non-x86 architectures.
1 parent 9d08b8c commit 95ff425

File tree

5 files changed

+0
-503
lines changed

5 files changed

+0
-503
lines changed

include/swift/AST/DiagnosticsSIL.def

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -312,14 +312,6 @@ ERROR(non_borrowed_indirect_addressof,none,
312312
REMARK(opt_remark_passed, none, "%0", (StringRef))
313313
REMARK(opt_remark_missed, none, "%0", (StringRef))
314314

315-
// Float-point to integer conversions
316-
ERROR(float_to_int_overflow, none,
317-
"invalid%select{| implicit}2 conversion: '%0' overflows %1", (StringRef, Type, bool))
318-
319-
ERROR(negative_fp_literal_overflow_unsigned, none,
320-
"negative literal '%0' cannot be converted to %select{|unsigned }2%1",
321-
(StringRef, Type, bool))
322-
323315
#ifndef DIAG_NO_UNDEF
324316
# if defined(DIAG)
325317
# undef DIAG

lib/SILOptimizer/Utils/ConstantFolding.cpp

Lines changed: 0 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
#include "swift/SILOptimizer/Utils/Local.h"
2121
#include "llvm/ADT/Statistic.h"
2222
#include "llvm/Support/Debug.h"
23-
#include "llvm/ADT/APSInt.h"
2423

2524
#define DEBUG_TYPE "constant-folding"
2625

@@ -860,116 +859,6 @@ constantFoldAndCheckIntegerConversions(BuiltinInst *BI,
860859

861860
}
862861

863-
/// A utility function that extracts the literal text corresponding
864-
/// to a given FloatLiteralInst the way it appears in the AST.
865-
/// This function can be used on FloatLiteralInsts generated by the
866-
/// constant folding phase.
867-
/// If the extraction is successful, the function returns true and
868-
/// 'fpStr' contains the literal the way it appears in the AST.
869-
/// If the extraction is unsuccessful, e.g. because there is no AST
870-
/// for the FloatLiteralInst, the function returns false.
871-
template<unsigned N>
872-
static bool tryExtractLiteralText(FloatLiteralInst *flitInst,
873-
SmallString<N> &fpStr) {
874-
875-
Expr *expr = flitInst->getLoc().getAsASTNode<Expr>();
876-
if (!expr)
877-
return false;
878-
879-
// 'expr' may not be a FloatLiteralExpr since 'flitInst' could have been
880-
// created by the ConstantFolder by folding floating-point constructor calls.
881-
// So we iterate through the sequence of folded constructors if any, and
882-
// try to extract the FloatLiteralExpr.
883-
while (auto *callExpr = dyn_cast<CallExpr>(expr)) {
884-
if (callExpr->getNumArguments() != 1 ||
885-
!dyn_cast<ConstructorRefCallExpr>(callExpr->getFn()))
886-
break;
887-
888-
auto *tupleExpr = dyn_cast<TupleExpr>(callExpr->getArg());
889-
if (!tupleExpr)
890-
break;
891-
892-
expr = tupleExpr->getElement(0);
893-
}
894-
895-
auto *flitExpr = dyn_cast<FloatLiteralExpr>(expr);
896-
if (!flitExpr)
897-
return false;
898-
899-
if (flitExpr->isNegative())
900-
fpStr += '-';
901-
fpStr += flitExpr->getDigitsText();
902-
return true;
903-
}
904-
905-
static SILValue foldFPToIntConversion(BuiltinInst *BI,
906-
const BuiltinInfo &Builtin, Optional<bool> &ResultsInError) {
907-
908-
assert(Builtin.ID == BuiltinValueKind::FPToSI ||
909-
Builtin.ID == BuiltinValueKind::FPToUI);
910-
911-
OperandValueArrayRef Args = BI->getArguments();
912-
bool conversionToUnsigned = (Builtin.ID == BuiltinValueKind::FPToUI);
913-
914-
auto *flitInst = dyn_cast<FloatLiteralInst>(Args[0]);
915-
if (!flitInst)
916-
return nullptr;
917-
APFloat fpVal = flitInst->getValue();
918-
auto *destTy = Builtin.Types[1]->castTo<BuiltinIntegerType>();
919-
920-
// Check non-negativeness of 'fpVal' for conversion to unsigned int.
921-
if (conversionToUnsigned && fpVal.isNegative() && !fpVal.isZero()) {
922-
// Stop folding and emit diagnostics if enabled.
923-
if (ResultsInError.hasValue()) {
924-
SILModule &M = BI->getModule();
925-
const ApplyExpr *CE = BI->getLoc().getAsASTNode<ApplyExpr>();
926-
927-
SmallString<10> fpStr;
928-
if (!tryExtractLiteralText(flitInst, fpStr))
929-
flitInst->getValue().toString(fpStr);
930-
931-
diagnose(M.getASTContext(), BI->getLoc().getSourceLoc(),
932-
diag::negative_fp_literal_overflow_unsigned, fpStr,
933-
CE ? CE->getType() : destTy,
934-
CE ? false : conversionToUnsigned);
935-
ResultsInError = Optional<bool>(true);
936-
}
937-
return nullptr;
938-
}
939-
940-
llvm::APSInt resInt(destTy->getFixedWidth(), conversionToUnsigned);
941-
bool isExact = false;
942-
APFloat::opStatus status =
943-
fpVal.convertToInteger(resInt, APFloat::rmTowardZero, &isExact);
944-
945-
if (status & APFloat::opStatus::opInvalidOp) {
946-
// Stop folding and emit diagnostics if enabled.
947-
if (ResultsInError.hasValue()) {
948-
SILModule &M = BI->getModule();
949-
const ApplyExpr *CE = BI->getLoc().getAsASTNode<ApplyExpr>();
950-
951-
SmallString<10> fpStr;
952-
if (!tryExtractLiteralText(flitInst, fpStr))
953-
flitInst->getValue().toString(fpStr);
954-
955-
diagnose(M.getASTContext(), BI->getLoc().getSourceLoc(),
956-
diag::float_to_int_overflow, fpStr,
957-
CE ? CE->getType() : destTy,
958-
CE ? CE->isImplicit() : false);
959-
ResultsInError = Optional<bool>(true);
960-
}
961-
return nullptr;
962-
}
963-
964-
if (status != APFloat::opStatus::opOK &&
965-
status != APFloat::opStatus::opInexact) {
966-
return nullptr;
967-
}
968-
// The call to the builtin should be replaced with the constant value.
969-
SILBuilderWithScope B(BI);
970-
return B.createIntegerLiteral(BI->getLoc(), BI->getType(), resInt);
971-
}
972-
973862
static SILValue constantFoldBuiltin(BuiltinInst *BI,
974863
Optional<bool> &ResultsInError) {
975864
const IntrinsicInfo &Intrinsic = BI->getIntrinsicInfo();
@@ -1100,12 +989,6 @@ case BuiltinValueKind::id:
1100989
return B.createFloatLiteral(Loc, BI->getType(), TruncVal);
1101990
}
1102991

1103-
// Conversions from floating point to integer,
1104-
case BuiltinValueKind::FPToSI:
1105-
case BuiltinValueKind::FPToUI: {
1106-
return foldFPToIntConversion(BI, Builtin, ResultsInError);
1107-
}
1108-
1109992
case BuiltinValueKind::AssumeNonNegative: {
1110993
auto *V = dyn_cast<IntegerLiteralInst>(Args[0]);
1111994
if (!V)

test/SILOptimizer/constant_propagation_floats.sil

Lines changed: 0 additions & 155 deletions
This file was deleted.

0 commit comments

Comments
 (0)