[mlir][Parser] Small optimization to parsing

* Use function_ref instead of std::function in several methods
* Use ::get instead of ::getChecked for IntegerType.
  - It is already fully verified and constructing a mlir::Location can be extremely costly during parsing.
This commit is contained in:
River Riddle 2020-11-03 12:31:24 -08:00
parent daa127d77e
commit 3e1390090f
3 changed files with 12 additions and 13 deletions

View File

@ -35,8 +35,8 @@ using llvm::SourceMgr;
/// Parse a comma separated list of elements that must have at least one entry
/// in it.
ParseResult Parser::parseCommaSeparatedList(
const std::function<ParseResult()> &parseElement) {
ParseResult
Parser::parseCommaSeparatedList(function_ref<ParseResult()> parseElement) {
// Non-empty case starts with an element.
if (parseElement())
return failure();
@ -55,9 +55,10 @@ ParseResult Parser::parseCommaSeparatedList(
/// abstract-list ::= rightToken // if allowEmptyList == true
/// abstract-list ::= element (',' element)* rightToken
///
ParseResult Parser::parseCommaSeparatedListUntil(
Token::Kind rightToken, const std::function<ParseResult()> &parseElement,
bool allowEmptyList) {
ParseResult
Parser::parseCommaSeparatedListUntil(Token::Kind rightToken,
function_ref<ParseResult()> parseElement,
bool allowEmptyList) {
// Handle the empty case.
if (getToken().is(rightToken)) {
if (!allowEmptyList)
@ -145,8 +146,8 @@ public:
/// returns null on failure.
Value resolveSSAUse(SSAUseInfo useInfo, Type type);
ParseResult parseSSADefOrUseAndType(
const std::function<ParseResult(SSAUseInfo, Type)> &action);
ParseResult
parseSSADefOrUseAndType(function_ref<ParseResult(SSAUseInfo, Type)> action);
ParseResult parseOptionalSSAUseAndTypeList(SmallVectorImpl<Value> &results);
@ -506,7 +507,7 @@ Value OperationParser::resolveSSAUse(SSAUseInfo useInfo, Type type) {
///
/// ssa-use-and-type ::= ssa-use `:` type
ParseResult OperationParser::parseSSADefOrUseAndType(
const std::function<ParseResult(SSAUseInfo, Type)> &action) {
function_ref<ParseResult(SSAUseInfo, Type)> action) {
SSAUseInfo useInfo;
if (parseSSAUse(useInfo) ||
parseToken(Token::colon, "expected ':' and type for SSA operand"))

View File

@ -36,13 +36,12 @@ public:
/// Parse a comma-separated list of elements up until the specified end token.
ParseResult
parseCommaSeparatedListUntil(Token::Kind rightToken,
const std::function<ParseResult()> &parseElement,
function_ref<ParseResult()> parseElement,
bool allowEmptyList = true);
/// Parse a comma separated list of elements that must have at least one entry
/// in it.
ParseResult
parseCommaSeparatedList(const std::function<ParseResult()> &parseElement);
ParseResult parseCommaSeparatedList(function_ref<ParseResult()> parseElement);
ParseResult parsePrettyDialectSymbolName(StringRef &prettyName);

View File

@ -337,9 +337,8 @@ Type Parser::parseNonFunctionType() {
if (Optional<bool> signedness = getToken().getIntTypeSignedness())
signSemantics = *signedness ? IntegerType::Signed : IntegerType::Unsigned;
auto loc = getEncodedSourceLocation(getToken().getLoc());
consumeToken(Token::inttype);
return IntegerType::getChecked(width.getValue(), signSemantics, loc);
return IntegerType::get(width.getValue(), signSemantics, getContext());
}
// float-type