Skip to content

[mlir][Parser][NFC] Make parseFloatFromIntegerLiteral a standalone function #116171

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions mlir/lib/AsmParser/AsmParserImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -287,13 +287,13 @@ class AsmParserImpl : public BaseT {
APFloat &result) override {
bool isNegative = parser.consumeIf(Token::minus);
Token curTok = parser.getToken();
SMLoc loc = curTok.getLoc();
auto emitErrorAtTok = [&]() { return emitError(curTok.getLoc(), ""); };

// Check for a floating point value.
if (curTok.is(Token::floatliteral)) {
auto val = curTok.getFloatingPointValue();
if (!val)
return emitError(loc, "floating point value too large");
return emitErrorAtTok() << "floating point value too large";
parser.consumeToken(Token::floatliteral);
result = APFloat(isNegative ? -*val : *val);
bool losesInfo;
Expand All @@ -303,18 +303,17 @@ class AsmParserImpl : public BaseT {

// Check for a hexadecimal float value.
if (curTok.is(Token::integer)) {
std::optional<APFloat> apResult;
if (failed(parser.parseFloatFromIntegerLiteral(
apResult, curTok, isNegative, semantics,
APFloat::semanticsSizeInBits(semantics))))
FailureOr<APFloat> apResult = parseFloatFromIntegerLiteral(
emitErrorAtTok, curTok, isNegative, semantics);
if (failed(apResult))
return failure();

result = *apResult;
parser.consumeToken(Token::integer);
return success();
}

return emitError(loc, "expected floating point literal");
return emitErrorAtTok() << "expected floating point literal";
}

/// Parse a floating point value from the stream.
Expand Down
24 changes: 13 additions & 11 deletions mlir/lib/AsmParser/AttributeParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -422,10 +422,10 @@ Attribute Parser::parseDecOrHexAttr(Type type, bool isNegative) {
}

if (auto floatType = dyn_cast<FloatType>(type)) {
std::optional<APFloat> result;
if (failed(parseFloatFromIntegerLiteral(result, tok, isNegative,
floatType.getFloatSemantics(),
floatType.getWidth())))
auto emitErrorAtTok = [&]() { return emitError(tok.getLoc()); };
FailureOr<APFloat> result = parseFloatFromIntegerLiteral(
emitErrorAtTok, tok, isNegative, floatType.getFloatSemantics());
if (failed(result))
return Attribute();
return FloatAttr::get(floatType, *result);
}
Expand Down Expand Up @@ -661,10 +661,10 @@ TensorLiteralParser::getFloatAttrElements(SMLoc loc, FloatType eltTy,

// Handle hexadecimal float literals.
if (token.is(Token::integer) && token.getSpelling().starts_with("0x")) {
std::optional<APFloat> result;
if (failed(p.parseFloatFromIntegerLiteral(result, token, isNegative,
eltTy.getFloatSemantics(),
eltTy.getWidth())))
auto emitErrorAtTok = [&]() { return p.emitError(token.getLoc()); };
FailureOr<APFloat> result = parseFloatFromIntegerLiteral(
emitErrorAtTok, token, isNegative, eltTy.getFloatSemantics());
if (failed(result))
return failure();

floatValues.push_back(*result);
Expand Down Expand Up @@ -911,10 +911,12 @@ ParseResult DenseArrayElementParser::parseFloatElement(Parser &p) {
auto floatType = cast<FloatType>(type);
if (p.consumeIf(Token::integer)) {
// Parse an integer literal as a float.
if (p.parseFloatFromIntegerLiteral(result, token, isNegative,
floatType.getFloatSemantics(),
floatType.getWidth()))
auto emitErrorAtTok = [&]() { return p.emitError(token.getLoc()); };
FailureOr<APFloat> fromIntLit = parseFloatFromIntegerLiteral(
emitErrorAtTok, token, isNegative, floatType.getFloatSemantics());
if (failed(fromIntLit))
return failure();
result = *fromIntLit;
} else if (p.consumeIf(Token::floatliteral)) {
// Parse a floating point literal.
std::optional<double> val = token.getFloatingPointValue();
Expand Down
63 changes: 32 additions & 31 deletions mlir/lib/AsmParser/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,38 @@
using namespace mlir;
using namespace mlir::detail;

/// Parse a floating point value from an integer literal token.
FailureOr<APFloat> detail::parseFloatFromIntegerLiteral(
function_ref<InFlightDiagnostic()> emitError, const Token &tok,
bool isNegative, const llvm::fltSemantics &semantics) {
StringRef spelling = tok.getSpelling();
bool isHex = spelling.size() > 1 && spelling[1] == 'x';
if (!isHex) {
auto error = emitError();
error << "unexpected decimal integer literal for a "
"floating point value";
error.attachNote() << "add a trailing dot to make the literal a float";
return failure();
}
if (isNegative) {
emitError() << "hexadecimal float literal should not have a "
"leading minus";
return failure();
}

APInt intValue;
tok.getSpelling().getAsInteger(isHex ? 0 : 10, intValue);
auto typeSizeInBits = APFloat::semanticsSizeInBits(semantics);
if (intValue.getActiveBits() > typeSizeInBits) {
return emitError() << "hexadecimal float constant out of range for type";
return failure();
}

APInt truncatedValue(typeSizeInBits, intValue.getNumWords(),
intValue.getRawData());
return APFloat(semantics, truncatedValue);
}

//===----------------------------------------------------------------------===//
// CodeComplete
//===----------------------------------------------------------------------===//
Expand Down Expand Up @@ -347,37 +379,6 @@ OptionalParseResult Parser::parseOptionalDecimalInteger(APInt &result) {
return success();
}

/// Parse a floating point value from an integer literal token.
ParseResult Parser::parseFloatFromIntegerLiteral(
std::optional<APFloat> &result, const Token &tok, bool isNegative,
const llvm::fltSemantics &semantics, size_t typeSizeInBits) {
SMLoc loc = tok.getLoc();
StringRef spelling = tok.getSpelling();
bool isHex = spelling.size() > 1 && spelling[1] == 'x';
if (!isHex) {
return emitError(loc, "unexpected decimal integer literal for a "
"floating point value")
.attachNote()
<< "add a trailing dot to make the literal a float";
}
if (isNegative) {
return emitError(loc, "hexadecimal float literal should not have a "
"leading minus");
}

APInt intValue;
tok.getSpelling().getAsInteger(isHex ? 0 : 10, intValue);
if (intValue.getActiveBits() > typeSizeInBits)
return emitError(loc, "hexadecimal float constant out of range for type");

APInt truncatedValue(typeSizeInBits, intValue.getNumWords(),
intValue.getRawData());

result.emplace(semantics, truncatedValue);

return success();
}

ParseResult Parser::parseOptionalKeyword(StringRef *keyword) {
// Check that the current token is a keyword.
if (!isCurrentTokenAKeyword())
Expand Down
12 changes: 6 additions & 6 deletions mlir/lib/AsmParser/Parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@

namespace mlir {
namespace detail {
/// Parse a floating point value from an integer literal token.
FailureOr<APFloat>
parseFloatFromIntegerLiteral(function_ref<InFlightDiagnostic()> emitError,
const Token &tok, bool isNegative,
const llvm::fltSemantics &semantics);

//===----------------------------------------------------------------------===//
// Parser
//===----------------------------------------------------------------------===//
Expand Down Expand Up @@ -151,12 +157,6 @@ class Parser {
/// Parse an optional integer value only in decimal format from the stream.
OptionalParseResult parseOptionalDecimalInteger(APInt &result);

/// Parse a floating point value from an integer literal token.
ParseResult parseFloatFromIntegerLiteral(std::optional<APFloat> &result,
const Token &tok, bool isNegative,
const llvm::fltSemantics &semantics,
size_t typeSizeInBits);

/// Returns true if the current token corresponds to a keyword.
bool isCurrentTokenAKeyword() const {
return getToken().isAny(Token::bare_identifier, Token::inttype) ||
Expand Down
Loading