Skip to content

Commit 1f81180

Browse files
address comments 2
1 parent 6a59007 commit 1f81180

File tree

4 files changed

+40
-38
lines changed

4 files changed

+40
-38
lines changed

mlir/lib/AsmParser/AsmParserImpl.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -287,9 +287,9 @@ class AsmParserImpl : public BaseT {
287287
APFloat &result) override {
288288
bool isNegative = parser.consumeIf(Token::minus);
289289
Token curTok = parser.getToken();
290-
FailureOr<APFloat> apResult =
291-
parser.parseFloatFromLiteral(curTok, isNegative, semantics);
292-
if (failed(apResult))
290+
std::optional<APFloat> apResult;
291+
if (failed(parser.parseFloatFromLiteral(apResult, curTok, isNegative,
292+
semantics)))
293293
return failure();
294294
parser.consumeToken();
295295
result = *apResult;

mlir/lib/AsmParser/AttributeParser.cpp

+10-9
Original file line numberDiff line numberDiff line change
@@ -422,9 +422,9 @@ Attribute Parser::parseDecOrHexAttr(Type type, bool isNegative) {
422422
}
423423

424424
if (auto floatType = dyn_cast<FloatType>(type)) {
425-
FailureOr<APFloat> result = parseFloatFromIntegerLiteral(
426-
tok, isNegative, floatType.getFloatSemantics());
427-
if (failed(result))
425+
std::optional<APFloat> result;
426+
if (failed(parseFloatFromIntegerLiteral(result, tok, isNegative,
427+
floatType.getFloatSemantics())))
428428
return Attribute();
429429
return FloatAttr::get(floatType, *result);
430430
}
@@ -657,9 +657,9 @@ TensorLiteralParser::getFloatAttrElements(SMLoc loc, FloatType eltTy,
657657
for (const auto &signAndToken : storage) {
658658
bool isNegative = signAndToken.first;
659659
const Token &token = signAndToken.second;
660-
FailureOr<APFloat> result =
661-
p.parseFloatFromLiteral(token, isNegative, eltTy.getFloatSemantics());
662-
if (failed(result))
660+
std::optional<APFloat> result;
661+
if (failed(p.parseFloatFromLiteral(result, token, isNegative,
662+
eltTy.getFloatSemantics())))
663663
return failure();
664664
floatValues.push_back(*result);
665665
}
@@ -880,9 +880,10 @@ ParseResult DenseArrayElementParser::parseIntegerElement(Parser &p) {
880880
ParseResult DenseArrayElementParser::parseFloatElement(Parser &p) {
881881
bool isNegative = p.consumeIf(Token::minus);
882882
Token token = p.getToken();
883-
FailureOr<APFloat> fromIntLit = p.parseFloatFromLiteral(
884-
token, isNegative, cast<FloatType>(type).getFloatSemantics());
885-
if (failed(fromIntLit))
883+
std::optional<APFloat> fromIntLit;
884+
if (failed(
885+
p.parseFloatFromLiteral(fromIntLit, token, isNegative,
886+
cast<FloatType>(type).getFloatSemantics())))
886887
return failure();
887888
p.consumeToken();
888889
append(fromIntLit->bitcastToAPInt());

mlir/lib/AsmParser/Parser.cpp

+21-21
Original file line numberDiff line numberDiff line change
@@ -347,59 +347,59 @@ OptionalParseResult Parser::parseOptionalDecimalInteger(APInt &result) {
347347
return success();
348348
}
349349

350-
FailureOr<APFloat>
351-
Parser::parseFloatFromLiteral(const Token &tok, bool isNegative,
352-
const llvm::fltSemantics &semantics) {
350+
ParseResult Parser::parseFloatFromLiteral(std::optional<APFloat> &result,
351+
const Token &tok, bool isNegative,
352+
const llvm::fltSemantics &semantics) {
353353
// Check for a floating point value.
354354
if (tok.is(Token::floatliteral)) {
355355
auto val = tok.getFloatingPointValue();
356356
if (!val)
357357
return emitError(tok.getLoc()) << "floating point value too large";
358358

359-
APFloat result(isNegative ? -*val : *val);
359+
result.emplace(isNegative ? -*val : *val);
360360
bool unused;
361-
result.convert(semantics, APFloat::rmNearestTiesToEven, &unused);
362-
return result;
361+
result->convert(semantics, APFloat::rmNearestTiesToEven, &unused);
362+
return success();
363363
}
364364

365365
// Check for a hexadecimal float value.
366366
if (tok.is(Token::integer))
367-
return parseFloatFromIntegerLiteral(tok, isNegative, semantics);
367+
return parseFloatFromIntegerLiteral(result, tok, isNegative, semantics);
368368

369369
return emitError(tok.getLoc()) << "expected floating point literal";
370370
}
371371

372372
/// Parse a floating point value from an integer literal token.
373-
FailureOr<APFloat>
374-
Parser::parseFloatFromIntegerLiteral(const Token &tok, bool isNegative,
373+
ParseResult
374+
Parser::parseFloatFromIntegerLiteral(std::optional<APFloat> &result,
375+
const Token &tok, bool isNegative,
375376
const llvm::fltSemantics &semantics) {
376377
StringRef spelling = tok.getSpelling();
377378
bool isHex = spelling.size() > 1 && spelling[1] == 'x';
378379
if (!isHex) {
379-
auto error = emitError(tok.getLoc());
380-
error << "unexpected decimal integer literal for a "
381-
"floating point value";
382-
error.attachNote() << "add a trailing dot to make the literal a float";
383-
return failure();
380+
return emitError(tok.getLoc(), "unexpected decimal integer literal for a "
381+
"floating point value")
382+
.attachNote()
383+
<< "add a trailing dot to make the literal a float";
384384
}
385385
if (isNegative) {
386-
emitError(tok.getLoc()) << "hexadecimal float literal should not have a "
387-
"leading minus";
388-
return failure();
386+
return emitError(tok.getLoc(),
387+
"hexadecimal float literal should not have a "
388+
"leading minus");
389389
}
390390

391391
APInt intValue;
392392
tok.getSpelling().getAsInteger(isHex ? 0 : 10, intValue);
393393
auto typeSizeInBits = APFloat::semanticsSizeInBits(semantics);
394394
if (intValue.getActiveBits() > typeSizeInBits) {
395-
return emitError(tok.getLoc())
396-
<< "hexadecimal float constant out of range for type";
397-
return failure();
395+
return emitError(tok.getLoc(),
396+
"hexadecimal float constant out of range for type");
398397
}
399398

400399
APInt truncatedValue(typeSizeInBits, intValue.getNumWords(),
401400
intValue.getRawData());
402-
return APFloat(semantics, truncatedValue);
401+
result.emplace(semantics, truncatedValue);
402+
return success();
403403
}
404404

405405
ParseResult Parser::parseOptionalKeyword(StringRef *keyword) {

mlir/lib/AsmParser/Parser.h

+6-5
Original file line numberDiff line numberDiff line change
@@ -153,13 +153,14 @@ class Parser {
153153
OptionalParseResult parseOptionalDecimalInteger(APInt &result);
154154

155155
/// Parse a floating point value from a literal.
156-
FailureOr<APFloat> parseFloatFromLiteral(const Token &tok, bool isNegative,
157-
const llvm::fltSemantics &semantics);
156+
ParseResult parseFloatFromLiteral(std::optional<APFloat> &result,
157+
const Token &tok, bool isNegative,
158+
const llvm::fltSemantics &semantics);
158159

159160
/// Parse a floating point value from an integer literal token.
160-
FailureOr<APFloat>
161-
parseFloatFromIntegerLiteral(const Token &tok, bool isNegative,
162-
const llvm::fltSemantics &semantics);
161+
ParseResult parseFloatFromIntegerLiteral(std::optional<APFloat> &result,
162+
const Token &tok, bool isNegative,
163+
const llvm::fltSemantics &semantics);
163164

164165
/// Returns true if the current token corresponds to a keyword.
165166
bool isCurrentTokenAKeyword() const {

0 commit comments

Comments
 (0)