@@ -424,8 +424,7 @@ Attribute Parser::parseDecOrHexAttr(Type type, bool isNegative) {
424424 if (auto floatType = dyn_cast<FloatType>(type)) {
425425 std::optional<APFloat> result;
426426 if (failed (parseFloatFromIntegerLiteral (result, tok, isNegative,
427- floatType.getFloatSemantics (),
428- floatType.getWidth ())))
427+ floatType.getFloatSemantics ())))
429428 return Attribute ();
430429 return FloatAttr::get (floatType, *result);
431430 }
@@ -658,36 +657,11 @@ TensorLiteralParser::getFloatAttrElements(SMLoc loc, FloatType eltTy,
658657 for (const auto &signAndToken : storage) {
659658 bool isNegative = signAndToken.first ;
660659 const Token &token = signAndToken.second ;
661-
662- // Handle hexadecimal float literals.
663- if (token.is (Token::integer) && token.getSpelling ().starts_with (" 0x" )) {
664- std::optional<APFloat> result;
665- if (failed (p.parseFloatFromIntegerLiteral (result, token, isNegative,
666- eltTy.getFloatSemantics (),
667- eltTy.getWidth ())))
668- return failure ();
669-
670- floatValues.push_back (*result);
671- continue ;
672- }
673-
674- // Check to see if any decimal integers or booleans were parsed.
675- if (!token.is (Token::floatliteral))
676- return p.emitError ()
677- << " expected floating-point elements, but parsed integer" ;
678-
679- // Build the float values from tokens.
680- auto val = token.getFloatingPointValue ();
681- if (!val)
682- return p.emitError (" floating point value too large for attribute" );
683-
684- APFloat apVal (isNegative ? -*val : *val);
685- if (!eltTy.isF64 ()) {
686- bool unused;
687- apVal.convert (eltTy.getFloatSemantics (), APFloat::rmNearestTiesToEven,
688- &unused);
689- }
690- floatValues.push_back (apVal);
660+ std::optional<APFloat> result;
661+ if (failed (p.parseFloatFromLiteral (result, token, isNegative,
662+ eltTy.getFloatSemantics ())))
663+ return failure ();
664+ floatValues.push_back (*result);
691665 }
692666 return success ();
693667}
@@ -905,32 +879,14 @@ ParseResult DenseArrayElementParser::parseIntegerElement(Parser &p) {
905879
906880ParseResult DenseArrayElementParser::parseFloatElement (Parser &p) {
907881 bool isNegative = p.consumeIf (Token::minus);
908-
909882 Token token = p.getToken ();
910- std::optional<APFloat> result;
911- auto floatType = cast<FloatType>(type);
912- if (p.consumeIf (Token::integer)) {
913- // Parse an integer literal as a float.
914- if (p.parseFloatFromIntegerLiteral (result, token, isNegative,
915- floatType.getFloatSemantics (),
916- floatType.getWidth ()))
917- return failure ();
918- } else if (p.consumeIf (Token::floatliteral)) {
919- // Parse a floating point literal.
920- std::optional<double > val = token.getFloatingPointValue ();
921- if (!val)
922- return failure ();
923- result = APFloat (isNegative ? -*val : *val);
924- if (!type.isF64 ()) {
925- bool unused;
926- result->convert (floatType.getFloatSemantics (),
927- APFloat::rmNearestTiesToEven, &unused);
928- }
929- } else {
930- return p.emitError (" expected integer or floating point literal" );
931- }
932-
933- append (result->bitcastToAPInt ());
883+ std::optional<APFloat> fromIntLit;
884+ if (failed (
885+ p.parseFloatFromLiteral (fromIntLit, token, isNegative,
886+ cast<FloatType>(type).getFloatSemantics ())))
887+ return failure ();
888+ p.consumeToken ();
889+ append (fromIntLit->bitcastToAPInt ());
934890 return success ();
935891}
936892
0 commit comments