Skip to content

Commit 093da29

Browse files
[mlir][Parser][NFC] Make parseFloatFromIntegerLiteral a standalone function
1 parent 73bb022 commit 093da29

File tree

4 files changed

+57
-55
lines changed

4 files changed

+57
-55
lines changed

mlir/lib/AsmParser/AsmParserImpl.h

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -287,13 +287,13 @@ class AsmParserImpl : public BaseT {
287287
APFloat &result) override {
288288
bool isNegative = parser.consumeIf(Token::minus);
289289
Token curTok = parser.getToken();
290-
SMLoc loc = curTok.getLoc();
290+
auto emitErrorAtTok = [&]() { return emitError(curTok.getLoc(), ""); };
291291

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

304304
// Check for a hexadecimal float value.
305305
if (curTok.is(Token::integer)) {
306-
std::optional<APFloat> apResult;
307-
if (failed(parser.parseFloatFromIntegerLiteral(
308-
apResult, curTok, isNegative, semantics,
309-
APFloat::semanticsSizeInBits(semantics))))
306+
FailureOr<APFloat> apResult = parseFloatFromIntegerLiteral(
307+
emitErrorAtTok, curTok, isNegative, semantics);
308+
if (failed(apResult))
310309
return failure();
311310

312311
result = *apResult;
313312
parser.consumeToken(Token::integer);
314313
return success();
315314
}
316315

317-
return emitError(loc, "expected floating point literal");
316+
return emitErrorAtTok() << "expected floating point literal";
318317
}
319318

320319
/// Parse a floating point value from the stream.

mlir/lib/AsmParser/AttributeParser.cpp

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -422,10 +422,10 @@ Attribute Parser::parseDecOrHexAttr(Type type, bool isNegative) {
422422
}
423423

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

662662
// Handle hexadecimal float literals.
663663
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())))
664+
auto emitErrorAtTok = [&]() { return p.emitError(token.getLoc()); };
665+
FailureOr<APFloat> result = parseFloatFromIntegerLiteral(
666+
emitErrorAtTok, token, isNegative, eltTy.getFloatSemantics());
667+
if (failed(result))
668668
return failure();
669669

670670
floatValues.push_back(*result);
@@ -911,10 +911,12 @@ ParseResult DenseArrayElementParser::parseFloatElement(Parser &p) {
911911
auto floatType = cast<FloatType>(type);
912912
if (p.consumeIf(Token::integer)) {
913913
// Parse an integer literal as a float.
914-
if (p.parseFloatFromIntegerLiteral(result, token, isNegative,
915-
floatType.getFloatSemantics(),
916-
floatType.getWidth()))
914+
auto emitErrorAtTok = [&]() { return p.emitError(token.getLoc()); };
915+
FailureOr<APFloat> fromIntLit = parseFloatFromIntegerLiteral(
916+
emitErrorAtTok, token, isNegative, floatType.getFloatSemantics());
917+
if (failed(fromIntLit))
917918
return failure();
919+
result = *fromIntLit;
918920
} else if (p.consumeIf(Token::floatliteral)) {
919921
// Parse a floating point literal.
920922
std::optional<double> val = token.getFloatingPointValue();

mlir/lib/AsmParser/Parser.cpp

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,38 @@
6767
using namespace mlir;
6868
using namespace mlir::detail;
6969

70+
/// Parse a floating point value from an integer literal token.
71+
FailureOr<APFloat> detail::parseFloatFromIntegerLiteral(
72+
function_ref<InFlightDiagnostic()> emitError, const Token &tok,
73+
bool isNegative, const llvm::fltSemantics &semantics) {
74+
StringRef spelling = tok.getSpelling();
75+
bool isHex = spelling.size() > 1 && spelling[1] == 'x';
76+
if (!isHex) {
77+
auto error = emitError();
78+
error << "unexpected decimal integer literal for a "
79+
"floating point value";
80+
error.attachNote() << "add a trailing dot to make the literal a float";
81+
return failure();
82+
}
83+
if (isNegative) {
84+
emitError() << "hexadecimal float literal should not have a "
85+
"leading minus";
86+
return failure();
87+
}
88+
89+
APInt intValue;
90+
tok.getSpelling().getAsInteger(isHex ? 0 : 10, intValue);
91+
auto typeSizeInBits = APFloat::semanticsSizeInBits(semantics);
92+
if (intValue.getActiveBits() > typeSizeInBits) {
93+
return emitError() << "hexadecimal float constant out of range for type";
94+
return failure();
95+
}
96+
97+
APInt truncatedValue(typeSizeInBits, intValue.getNumWords(),
98+
intValue.getRawData());
99+
return APFloat(semantics, truncatedValue);
100+
}
101+
70102
//===----------------------------------------------------------------------===//
71103
// CodeComplete
72104
//===----------------------------------------------------------------------===//
@@ -347,37 +379,6 @@ OptionalParseResult Parser::parseOptionalDecimalInteger(APInt &result) {
347379
return success();
348380
}
349381

350-
/// Parse a floating point value from an integer literal token.
351-
ParseResult Parser::parseFloatFromIntegerLiteral(
352-
std::optional<APFloat> &result, const Token &tok, bool isNegative,
353-
const llvm::fltSemantics &semantics, size_t typeSizeInBits) {
354-
SMLoc loc = tok.getLoc();
355-
StringRef spelling = tok.getSpelling();
356-
bool isHex = spelling.size() > 1 && spelling[1] == 'x';
357-
if (!isHex) {
358-
return emitError(loc, "unexpected decimal integer literal for a "
359-
"floating point value")
360-
.attachNote()
361-
<< "add a trailing dot to make the literal a float";
362-
}
363-
if (isNegative) {
364-
return emitError(loc, "hexadecimal float literal should not have a "
365-
"leading minus");
366-
}
367-
368-
APInt intValue;
369-
tok.getSpelling().getAsInteger(isHex ? 0 : 10, intValue);
370-
if (intValue.getActiveBits() > typeSizeInBits)
371-
return emitError(loc, "hexadecimal float constant out of range for type");
372-
373-
APInt truncatedValue(typeSizeInBits, intValue.getNumWords(),
374-
intValue.getRawData());
375-
376-
result.emplace(semantics, truncatedValue);
377-
378-
return success();
379-
}
380-
381382
ParseResult Parser::parseOptionalKeyword(StringRef *keyword) {
382383
// Check that the current token is a keyword.
383384
if (!isCurrentTokenAKeyword())

mlir/lib/AsmParser/Parser.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@
1616

1717
namespace mlir {
1818
namespace detail {
19+
/// Parse a floating point value from an integer literal token.
20+
FailureOr<APFloat>
21+
parseFloatFromIntegerLiteral(function_ref<InFlightDiagnostic()> emitError,
22+
const Token &tok, bool isNegative,
23+
const llvm::fltSemantics &semantics);
24+
1925
//===----------------------------------------------------------------------===//
2026
// Parser
2127
//===----------------------------------------------------------------------===//
@@ -151,12 +157,6 @@ class Parser {
151157
/// Parse an optional integer value only in decimal format from the stream.
152158
OptionalParseResult parseOptionalDecimalInteger(APInt &result);
153159

154-
/// Parse a floating point value from an integer literal token.
155-
ParseResult parseFloatFromIntegerLiteral(std::optional<APFloat> &result,
156-
const Token &tok, bool isNegative,
157-
const llvm::fltSemantics &semantics,
158-
size_t typeSizeInBits);
159-
160160
/// Returns true if the current token corresponds to a keyword.
161161
bool isCurrentTokenAKeyword() const {
162162
return getToken().isAny(Token::bare_identifier, Token::inttype) ||

0 commit comments

Comments
 (0)