From e24a2f2fab77db030e7a3f84fa2f0a0df22d136f Mon Sep 17 00:00:00 2001 From: Mark Mroz Date: Sun, 21 Jan 2018 09:30:10 -0500 Subject: [PATCH 1/6] Support for Float literals --- include/swift/WALASupport/InstrKindInfoGetter.h | 1 + lib/WALASupport/InstrKindInfoGetter.cpp | 15 ++++++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/include/swift/WALASupport/InstrKindInfoGetter.h b/include/swift/WALASupport/InstrKindInfoGetter.h index 511bead31d4ed..2ddee148cf8ef 100644 --- a/include/swift/WALASupport/InstrKindInfoGetter.h +++ b/include/swift/WALASupport/InstrKindInfoGetter.h @@ -47,6 +47,7 @@ class InstrKindInfoGetter { jobject handleIntegerLiteralInst(); jobject handleStringLiteralInst(); jobject handleConstStringLiteralInst(); + jobject handleFloatLiteralInst(); jobject handleProjectBoxInst(); jobject handleDebugValueInst(); jobject handleFunctionRefInst(); diff --git a/lib/WALASupport/InstrKindInfoGetter.cpp b/lib/WALASupport/InstrKindInfoGetter.cpp index 0ad5b79ba100e..abbc04f3ed9ff 100644 --- a/lib/WALASupport/InstrKindInfoGetter.cpp +++ b/lib/WALASupport/InstrKindInfoGetter.cpp @@ -225,6 +225,19 @@ jobject InstrKindInfoGetter::handleIntegerLiteralInst() { return node; } +jobject InstrKindInfoGetter::handleFloatLiteralInst() { + if (outs != NULL) { + *outs << "<< FloatLiteralInst >>" << "\n"; + } + FloatLiteralInst* castInst = cast(instr); + APFloat value = castInst->getValue(); + bool APFLosesInfo; + value.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven, &APFLosesInfo); + jobject node = (*wala)->makeConstant(value.convertToDouble()); + nodeMap->insert(std::make_pair(castInst, node)); + return node; +} + jobject InstrKindInfoGetter::handleStringLiteralInst() { // ValueKind indentifier if (outs != NULL) { @@ -813,7 +826,7 @@ SILInstructionKind InstrKindInfoGetter::get() { } case SILInstructionKind::FloatLiteralInst: { - *outs << "<< FloatLiteralInst >>" << "\n"; + node = handleFloatLiteralInst(); break; } From cd466442549b9b485c3e0f84a13b60f69ced8ad3 Mon Sep 17 00:00:00 2001 From: Bryan Tam Date: Mon, 29 Jan 2018 16:38:00 -0700 Subject: [PATCH 2/6] Handle Float Literal using BigDecimal --- include/swift/WALASupport/WALAWalker.h | 2 ++ lib/WALASupport/InstrKindInfoGetter.cpp | 22 ++++++++++++++++++---- lib/WALASupport/WALAWalker.cpp | 15 +++++++++++++++ 3 files changed, 35 insertions(+), 4 deletions(-) diff --git a/include/swift/WALASupport/WALAWalker.h b/include/swift/WALASupport/WALAWalker.h index ed20d7aec6d9d..45e01068807e0 100644 --- a/include/swift/WALASupport/WALAWalker.h +++ b/include/swift/WALASupport/WALAWalker.h @@ -55,6 +55,8 @@ class WALAIntegration { void print(jobject obj); jobject makePosition(int, int, int, int); + + jobject makeBigDecimal(const char *, int); WALAIntegration(JNIEnv *, Exceptions &, const char *); }; diff --git a/lib/WALASupport/InstrKindInfoGetter.cpp b/lib/WALASupport/InstrKindInfoGetter.cpp index abbc04f3ed9ff..232b6fec6569b 100644 --- a/lib/WALASupport/InstrKindInfoGetter.cpp +++ b/lib/WALASupport/InstrKindInfoGetter.cpp @@ -229,12 +229,26 @@ jobject InstrKindInfoGetter::handleFloatLiteralInst() { if (outs != NULL) { *outs << "<< FloatLiteralInst >>" << "\n"; } + jobject node = nullptr; FloatLiteralInst* castInst = cast(instr); APFloat value = castInst->getValue(); - bool APFLosesInfo; - value.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven, &APFLosesInfo); - jobject node = (*wala)->makeConstant(value.convertToDouble()); - nodeMap->insert(std::make_pair(castInst, node)); + + if (value.isFinite()) { + // To BigDecimal + SmallVector buf; + value.toString(buf); + jobject bigDecimal = (*wala).makeBigDecimal(buf.data(), buf.size()); + node = (*wala)->makeConstant(bigDecimal); + nodeMap->insert(std::make_pair(castInst, node)); + } + else { + // Infinity of NaN, convert to double + // as BigDecimal constructor cannot accept strings of these + bool APFLosesInfo; + value.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven, &APFLosesInfo); + node = (*wala)->makeConstant(value.convertToDouble()); + nodeMap->insert(std::make_pair(castInst, node)); + } return node; } diff --git a/lib/WALASupport/WALAWalker.cpp b/lib/WALASupport/WALAWalker.cpp index 803ae1620220a..10ab1aa491fab 100644 --- a/lib/WALASupport/WALAWalker.cpp +++ b/lib/WALASupport/WALAWalker.cpp @@ -54,6 +54,21 @@ jobject WALAIntegration::makePosition(int fl, int fc, int ll, int lc) { return result; } +jobject WALAIntegration::makeBigDecimal(const char *strData, int strLen) { + char *safeData = strndup(strData, strLen); + jobject val = java_env->NewStringUTF(safeData); + delete safeData; + jclass bigDecimalCls = java_env->FindClass("java/math/BigDecimal"); + THROW_ANY_EXCEPTION(cpp_ex); + jmethodID bigDecimalInit = java_env->GetMethodID(bigDecimalCls, + "", "(Ljava/lang/String;)V"); + THROW_ANY_EXCEPTION(cpp_ex); + jobject bigDecimal = java_env->NewObject(bigDecimalCls, bigDecimalInit, val); + THROW_ANY_EXCEPTION(cpp_ex); + java_env->DeleteLocalRef(val); + return bigDecimal; +} + void WALAIntegration::print(jobject obj) { print_object(java_env, obj); THROW_ANY_EXCEPTION(cpp_ex); From 88c30e7a546a4ed58eb4e8a6eb9f8158f1fc656c Mon Sep 17 00:00:00 2001 From: Bryan Tam Date: Tue, 6 Feb 2018 15:07:36 -0500 Subject: [PATCH 3/6] Fix typo in comment Accidentally made a type in the comment explaining Infinity and NaN, fixed that in this commit --- lib/WALASupport/InstrKindInfoGetter.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/WALASupport/InstrKindInfoGetter.cpp b/lib/WALASupport/InstrKindInfoGetter.cpp index 232b6fec6569b..010a5e63a9c8b 100644 --- a/lib/WALASupport/InstrKindInfoGetter.cpp +++ b/lib/WALASupport/InstrKindInfoGetter.cpp @@ -242,7 +242,7 @@ jobject InstrKindInfoGetter::handleFloatLiteralInst() { nodeMap->insert(std::make_pair(castInst, node)); } else { - // Infinity of NaN, convert to double + // Infinity or NaN, convert to double // as BigDecimal constructor cannot accept strings of these bool APFLosesInfo; value.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven, &APFLosesInfo); @@ -1391,4 +1391,4 @@ SILInstructionKind InstrKindInfoGetter::get() { *outs << *instr << "\n"; return instrKind; -} \ No newline at end of file +} From dbcbe3abf4249bee86425ba06cee7a3b401a29b0 Mon Sep 17 00:00:00 2001 From: Bryan Tam Date: Tue, 6 Mar 2018 09:59:41 -0700 Subject: [PATCH 4/6] added check for float and double comparing pointers to fltSemantics --- lib/WALASupport/InstrKindInfoGetter.cpp | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/WALASupport/InstrKindInfoGetter.cpp b/lib/WALASupport/InstrKindInfoGetter.cpp index 010a5e63a9c8b..bd9d9fbd2739d 100644 --- a/lib/WALASupport/InstrKindInfoGetter.cpp +++ b/lib/WALASupport/InstrKindInfoGetter.cpp @@ -233,7 +233,21 @@ jobject InstrKindInfoGetter::handleFloatLiteralInst() { FloatLiteralInst* castInst = cast(instr); APFloat value = castInst->getValue(); - if (value.isFinite()) { + if (&value.getSemantics() == &APFloat::IEEEsingle()) { + // To Float + bool APFLosesInfo; + value.convert(APFloat::IEEEsingle(), APFloat::rmNearestTiesToEven, &APFLosesInfo); + node = (*wala)->makeConstant(value.convertToFloat()); + nodeMap->insert(std::make_pair(castInst, node)); + } + else if (&value.getSemantics() == &APFloat::IEEEdouble()) { + // To Double + bool APFLosesInfo; + value.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven, &APFLosesInfo); + node = (*wala)->makeConstant(value.convertToDouble()); + nodeMap->insert(std::make_pair(castInst, node)); + } + else if (value.isFinite()) { // To BigDecimal SmallVector buf; value.toString(buf); From acc8e376b6f733868df9234b4f019aabfd441387 Mon Sep 17 00:00:00 2001 From: Bryan Tam Date: Fri, 9 Mar 2018 08:45:04 -0700 Subject: [PATCH 5/6] remove manual convert --- lib/WALASupport/InstrKindInfoGetter.cpp | 4 ---- 1 file changed, 4 deletions(-) diff --git a/lib/WALASupport/InstrKindInfoGetter.cpp b/lib/WALASupport/InstrKindInfoGetter.cpp index bd9d9fbd2739d..3dab6f04599a9 100644 --- a/lib/WALASupport/InstrKindInfoGetter.cpp +++ b/lib/WALASupport/InstrKindInfoGetter.cpp @@ -235,15 +235,11 @@ jobject InstrKindInfoGetter::handleFloatLiteralInst() { if (&value.getSemantics() == &APFloat::IEEEsingle()) { // To Float - bool APFLosesInfo; - value.convert(APFloat::IEEEsingle(), APFloat::rmNearestTiesToEven, &APFLosesInfo); node = (*wala)->makeConstant(value.convertToFloat()); nodeMap->insert(std::make_pair(castInst, node)); } else if (&value.getSemantics() == &APFloat::IEEEdouble()) { // To Double - bool APFLosesInfo; - value.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven, &APFLosesInfo); node = (*wala)->makeConstant(value.convertToDouble()); nodeMap->insert(std::make_pair(castInst, node)); } From 859aff224b9dc8e40388ef69d698a4dcd02c9038 Mon Sep 17 00:00:00 2001 From: Bryan Tam Date: Fri, 9 Mar 2018 09:38:39 -0700 Subject: [PATCH 6/6] remove repeated code by moving outside if --- lib/WALASupport/InstrKindInfoGetter.cpp | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/lib/WALASupport/InstrKindInfoGetter.cpp b/lib/WALASupport/InstrKindInfoGetter.cpp index 3dab6f04599a9..a8d9a11c5b4f9 100644 --- a/lib/WALASupport/InstrKindInfoGetter.cpp +++ b/lib/WALASupport/InstrKindInfoGetter.cpp @@ -236,12 +236,10 @@ jobject InstrKindInfoGetter::handleFloatLiteralInst() { if (&value.getSemantics() == &APFloat::IEEEsingle()) { // To Float node = (*wala)->makeConstant(value.convertToFloat()); - nodeMap->insert(std::make_pair(castInst, node)); } else if (&value.getSemantics() == &APFloat::IEEEdouble()) { // To Double node = (*wala)->makeConstant(value.convertToDouble()); - nodeMap->insert(std::make_pair(castInst, node)); } else if (value.isFinite()) { // To BigDecimal @@ -249,7 +247,6 @@ jobject InstrKindInfoGetter::handleFloatLiteralInst() { value.toString(buf); jobject bigDecimal = (*wala).makeBigDecimal(buf.data(), buf.size()); node = (*wala)->makeConstant(bigDecimal); - nodeMap->insert(std::make_pair(castInst, node)); } else { // Infinity or NaN, convert to double @@ -257,8 +254,8 @@ jobject InstrKindInfoGetter::handleFloatLiteralInst() { bool APFLosesInfo; value.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven, &APFLosesInfo); node = (*wala)->makeConstant(value.convertToDouble()); - nodeMap->insert(std::make_pair(castInst, node)); } + nodeMap->insert(std::make_pair(castInst, node)); return node; }