aboutsummaryrefslogtreecommitdiff
path: root/mlir/lib/Parser/Parser.cpp
AgeCommit message (Collapse)AuthorFilesLines
2025-07-31[mlir] Set implicit operation loc to start of split. (#151499)Jacques Pienaar1-6/+24
2023-08-30[mlir] Relax requirement on memory buffer creation.Jacques Pienaar1-1/+3
parseSourceString does not require null-terminated string, hence requirement on memory buffer can be relaxed. Differential Revision: https://reviews.llvm.org/D159214
2023-08-30fix unused variable warnings in conditionalsMikhail Goncharov1-1/+1
warning was updated in 92023b15099012a657da07ebf49dd7d94a260f84
2023-03-01[mlir][python] Add generic operation parse APIsrkayaith1-1/+2
Currently the bindings only allow for parsing IR with a top-level `builtin.module` op, since the parse APIs insert an implicit module op. This change adds `Operation.parse`, which returns whatever top-level op is actually in the source. To simplify parsing of specific operations, `OpView.parse` is also added, which handles the error checking for `OpView` subclasses. Reviewed By: ftynse, stellaraccident Differential Revision: https://reviews.llvm.org/D143352
2022-12-11[mlir:Bytecode] Add shared_ptr<SourceMgr> overloads to allow safe mmap of dataRiver Riddle1-7/+37
The bytecode reader currently has no mechanism that allows for directly referencing data from the input buffer safely. This commit adds shared_ptr<SourceMgr> overloads that provide an explicit and safe way of extending the lifetime of the input. The usage of these new overloads is adopted in all of our tooling, and is implicitly used in the filename only parser methods. Differential Revision: https://reviews.llvm.org/D139366
2022-08-22[mlir] Add initial support for a binary serialization formatRiver Riddle1-0/+3
This commit adds a new bytecode serialization format for MLIR. The actual serialization of MLIR to binary is relatively straightforward, given the very very general structure of MLIR. The underlying basis for this format is a variable-length encoding for integers, which gets heavily used for nearly all aspects of the encoding (given that most of the encoding is just indexing into lists). The format currently does not provide support for custom attribute/type serialization, and thus always uses an assembly format fallback. It also doesn't provide support for resources. These will be added in followups, the intention for this patch is to provide something that supports the basic cases, and can be built on top of. https://discourse.llvm.org/t/rfc-a-binary-serialization-format-for-mlir/63518 Differential Revision: https://reviews.llvm.org/D131747
2022-07-25[mlir] Refactor the Parser library in preparation for an MLIR binary formatRiver Riddle1-2597/+15
The current Parser library is solely focused on providing API for the textual MLIR format, but MLIR will soon also provide a binary format. This commit renames the current Parser library to AsmParser to better correspond to what the library is actually intended for. A new Parser library is added which will act as a unified parser interface between both text and binary formats. Most parser clients are unaffected, given that the unified interface is essentially the same as the current interface. Only clients that rely on utilizing the AsmParserState, or those that want to parse Attributes/Types need to be updated to point to the AsmParser library. Differential Revision: https://reviews.llvm.org/D129605
2022-07-14[mlir] Use value instead of getValue (NFC)Kazu Hirata1-1/+1
2022-07-13[mlir] Use has_value instead of hasValue (NFC)Kazu Hirata1-1/+1
2022-07-12Read/write external resource alignment tag in little-endianUlrich Weigand1-1/+2
https://reviews.llvm.org/D126446 added support for encoding binary blobs in MLIR assembly. To enable cross-architecture compatibility, these need to be encoded in little-endian format. This patch is a first step in that direction by reading and writing the alignment tag that those blobs are prefixed by in little-endian format. This fixes assertion failures in several test cases on big-endian platforms. The actual content of the blob is not yet handled here. Differential Revision: https://reviews.llvm.org/D129483
2022-07-08[mlir:LSP] Add support for code completing attributes and typesRiver Riddle1-0/+20
This required changing a bit of how attributes/types are parsed. A new `KeywordSwitch` class was added to AsmParser that provides a StringSwitch like API for parsing keywords with a set of potential matches. It intends to both provide a cleaner API, and enable injection for code completion. This required changing the API of `generated(Attr|Type)Parser` to handle the parsing of the keyword, instead of having the user do it. Most upstream dialects use the autogenerated handling and didn't require a direct update. Differential Revision: https://reviews.llvm.org/D129267
2022-07-08[mlir:LSP] Add support for keyword code completionsRiver Riddle1-0/+9
This commit adds code completion results to the MLIR LSP when parsing keywords. Keyword support is currently limited to the case where the expected keyword is provided, but a followup will work on expanding the set of keyword cases we handle (e.g. to allow capturing attribute/type mnemonics). Differential Revision: https://reviews.llvm.org/D129184
2022-07-07[mlir:LSP] Add support for MLIR code completionsRiver Riddle1-13/+157
This commit adds code completion results to the MLIR LSP using a new code completion context in the MLIR parser. This commit adds initial completion for dialect, operation, SSA value, and block names. Differential Revision: https://reviews.llvm.org/D129183
2022-06-29[mlir] Allow for attaching external resources to .mlir filesRiver Riddle1-21/+255
This commit enables support for providing and processing external resources within MLIR assembly formats. This is a mechanism with which dialects, and external clients, may attach additional information when printing IR without that information being encoded in the IR itself. External resources are not uniqued within the MLIR context, are not attached directly to any operation, and are solely intended to live and be processed outside of the immediate IR. There are many potential uses of this functionality, for example MLIR's pass crash reproducer could utilize this to attach the pass resource executing when a crash occurs. Other types of uses may be embedding large amounts of binary data, such as weights in ML applications, that shouldn't be copied directly into the MLIR context, but need to be kept adjacent to the IR. External resources are encoded using a key-value pair nested within a dictionary anchored by name either on a dialect, or an externally registered entity. The key is an identifier used to disambiguate the data. The value may be stored in various limited forms, but general encodings use a string (human readable) or blob format (binary). Within the textual format, an example may be of the form: ```mlir {-# // The `dialect_resources` section within the file-level metadata // dictionary is used to contain any dialect resource entries. dialect_resources: { // Here is a dictionary anchored on "foo_dialect", which is a dialect // namespace. foo_dialect: { // `some_dialect_resource` is a key to be interpreted by the dialect, // and used to initialize/configure/etc. some_dialect_resource: "Some important resource value" } }, // The `external_resources` section within the file-level metadata // dictionary is used to contain any non-dialect resource entries. external_resources: { // Here is a dictionary anchored on "mlir_reproducer", which is an // external entity representing MLIR's crash reproducer functionality. mlir_reproducer: { // `pipeline` is an entry that holds a crash reproducer pipeline // resource. pipeline: "func.func(canonicalize,cse)" } } ``` Differential Revision: https://reviews.llvm.org/D126446
2022-06-25Revert "Don't use Optional::hasValue (NFC)"Kazu Hirata1-2/+2
This reverts commit aa8feeefd3ac6c78ee8f67bf033976fc7d68bc6d.
2022-06-25Don't use Optional::hasValue (NFC)Kazu Hirata1-2/+2
2022-06-20[mlir] Don't use Optional::getValue (NFC)Kazu Hirata1-1/+1
2022-06-20[mlir] Don't use Optional::hasValue (NFC)Kazu Hirata1-3/+3
2022-05-23Apply clang-tidy fixes for modernize-use-bool-literals in Parser.cpp (NFC)Mehdi Amini1-1/+1
2022-05-16[mlir] Remove the `type` keyword from type alias definitionsRiver Riddle1-5/+3
This was carry over from LLVM IR where the alias definition can be ambiguous, but MLIR type aliases have no such problems. Having the `type` keyword is superfluous and doesn't add anything. This commit drops it, which also nicely aligns with the syntax for attribute aliases (which doesn't have a keyword). Differential Revision: https://reviews.llvm.org/D125501
2022-05-11[mlir:Parser] Emit a better diagnostic when a custom operation is unknownRiver Riddle1-32/+21
When a custom operation is unknown and does not have a dialect prefix, we currently emit an error using the name of the operation with the default dialect prefix. This leads to a confusing error message, especially when operations get moved between dialects. For example, `func` was recently moved out of `builtin` and to the `func` dialect. The current error message we get is: ``` func @foo() ^ custom op 'builtin.func' is unknown ``` This could lead users to believe that there is supposed to be a `builtin.func`, because there used to be. This commit adds a better error message that does not assume that the operation is supposed to be in the default dialect: ``` func @foo() ^ custom op 'func' is unknown (tried 'builtin.func' as well) ``` Differential Revision: https://reviews.llvm.org/D125351
2022-05-11[AsmParser] Improve error recovery again.Chris Lattner1-10/+22
Change the parsing logic to use StringRef instead of lower level char* logic. Also, if emitting a diagnostic on the first token in the file, we make sure to use that position instead of the very start of the file. Differential Revision: https://reviews.llvm.org/D125353
2022-05-10[MLIR Parser] Improve QoI for "expected token" errorsChris Lattner1-16/+65
A typical problem with missing a token is that the missing token is at the end of a line. The problem with this is that the error message gets reported on the start of the following line (which is where the next / invalid token is) which can be confusing. Handle this by noticing this case and backing up to the end of the previous line. Differential Revision: https://reviews.llvm.org/D125295
2022-05-06[mlir] Remove special case parsing/printing of `func` operationsRiver Riddle1-5/+0
This was leftover from when the standard dialect was destroyed, and when FuncOp moved to the func dialect. Now that these transitions have settled a bit we can drop these. Most updates were handled using a simple regex: replace `^( *)func` with `$1func.func` Differential Revision: https://reviews.llvm.org/D124146
2022-04-29[AsmParser] Introduce a new "Argument" abstraction + supporting logicChris Lattner1-83/+80
MLIR has a common pattern for "arguments" that uses syntax like `%x : i32 {attrs} loc("sourceloc")` which is implemented in adhoc ways throughout the codebase. The approach this uses is verbose (because it is implemented with parallel arrays) and inconsistent (e.g. lots of things drop source location info). Solve this by introducing OpAsmParser::Argument and make addRegion (which sets up BlockArguments for the region) take it. Convert the world to propagating this down. This means that we correctly capture and propagate source location information in a lot more cases (e.g. see the affine.for testcase example), and it also simplifies much code. Differential Revision: https://reviews.llvm.org/D124649
2022-04-28[OpAsmParser] Simplify logic for requiredOperandCount in parseOperandList.Chris Lattner1-19/+2
I would ideally like to eliminate 'requiredOperandCount' as a bit of verification that should be in the client side, but it is much more widely used than I expected. Just tidy some pieces up around it given we can't drop it immediately. NFC. Differential Revision: https://reviews.llvm.org/D124629
2022-04-28[AsmParser] Rework logic around "region argument parsing"Chris Lattner1-46/+24
The asm parser had a notional distinction between parsing an operand (like "%foo" or "%4#3") and parsing a region argument (which isn't supposed to allow a result number like #3). Unfortunately the implementation has two problems: 1) It didn't actually check for the result number and reject it. parseRegionArgument and parseOperand were identical. 2) It had a lot of machinery built up around it that paralleled operand parsing. This also was functionally identical, but also had some subtle differences (e.g. the parseOptional stuff had a different result type). I thought about just removing all of this, but decided that the missing error checking was important, so I reimplemented it with a `allowResultNumber` flag on parseOperand. This keeps the codepaths unified and adds the missing error checks. Differential Revision: https://reviews.llvm.org/D124470
2022-04-21[AsmParser/Printer] Rework sourceloc support for function arguments.Chris Lattner1-30/+20
When Location tracking support for block arguments was added, we discussed various approaches to threading support for this through function-like argument parsing. At the time, we added a parallel array of locations that could hold this. It turns out that that approach was verbose and error prone, roughly no one adopted it. This patch takes a different approach, adding an optional source locator to the UnresolvedOperand class. This fits much more naturally into the standard structure we use for representing locators, and gives all the function like dialects locator support for free (e.g. see the test adding an example for the LLVM dialect). Differential Revision: https://reviews.llvm.org/D124188
2022-04-06[mlir:Parser][NFC] Replace SSAUseInfo with OpAsmParser::UnresolvedOperandRiver Riddle1-83/+59
These are functionally identical, and merging the two removes the number of redundant conversions within the parser.
2022-04-04[mlir] Rework the implementation of TypeIDRiver Riddle1-4/+7
This commit restructures how TypeID is implemented to ideally avoid the current problems related to shared libraries. This is done by changing the "implicit" fallback path to use the name of the type, instead of using a static template variable (which breaks shared libraries). The major downside to this is that it adds some additional initialization costs for the implicit path. Given the use of type names for uniqueness in the fallback, we also no longer allow types defined in anonymous namespaces to have an implicit TypeID. To simplify defining an ID for these classes, a new `MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID` macro was added to allow for explicitly defining a TypeID directly on an internal class. To help identify when types are using the fallback, `-debug-only=typeid` can be used to log which types are using implicit ids. This change generally only requires changes to the test passes, which are all defined in anonymous namespaces, and thus can't use the fallback any longer. Differential Revision: https://reviews.llvm.org/D122775
2022-03-28[mlir] Fix leak in case of failed parseJacques Pienaar1-34/+34
A Block is optionally allocated & leaks in case of failed parse. Inline the function and ensure Block gets freed unless parse is successful. Differential Revision: https://reviews.llvm.org/D122112
2022-03-23[mlir] Make OpBuilder::createOperation to accept raw inputsChia-hung Duan1-2/+2
This provides a way to create an operation without manipulating OperationState directly. This is useful for creating unregistered ops. Reviewed By: rriddle, mehdi_amini Differential Revision: https://reviews.llvm.org/D120787
2022-03-21[mlir] Rename `OpAsmParser::OperandType` to `OpAsmParser::UnresolvedOperand`Markus Böck1-48/+50
I am not sure about the meaning of Type in the name (was it meant be interpreted as Kind?), and given the importance and meaning of Type in the context of MLIR, its probably better to rename it. Given the comment in the source code, the suggestion in the GitHub issue and the final discussions in the review, this patch renames the OperandType to UnresolvedOperand. Fixes https://github.com/llvm/llvm-project/issues/54446 Differential Revision: https://reviews.llvm.org/D122142
2022-03-12[MLIR] Fix block label parsing bugUday Bondhugula1-7/+7
Fix bug in `Block` label parsing: https://github.com/llvm/llvm-project/issues/54343 The `parseOptionalBlockArgList` method was doing the wrong thing (contrary to its doc comment) and its calling context was also incorrect. This led to a parse failure for something like "^bb0()". Fixes #54343 Differential Revision: https://reviews.llvm.org/D121503
2022-03-07[mlir][NFC] Move Parser.h to Parser/River Riddle1-1/+1
There is no reason for this file to be at the top-level, and its current placement predates the Parser/ folder's existence. Differential Revision: https://reviews.llvm.org/D121024
2022-03-01[mlir] Rename the Standard dialect to the Func dialectRiver Riddle1-2/+3
The last remaining operations in the standard dialect all revolve around FuncOp/function related constructs. This patch simply handles the initial renaming (which by itself is already huge), but there are a large number of cleanups unlocked/necessary afterwards: * Removing a bunch of unnecessary dependencies on Func * Cleaning up the From/ToStandard conversion passes * Preparing for the move of FuncOp to the Func dialect See the discussion at https://discourse.llvm.org/t/standard-dialect-the-final-chapter/6061 Differential Revision: https://reviews.llvm.org/D120624
2022-01-26[mlir][NFC] Add a using for llvm::SMLoc/llvm::SMRange to LLVM.hRiver Riddle1-8/+7
These are used pervasively during parsing. Differential Revision: https://reviews.llvm.org/D118291
2022-01-19[mlir] Make locations required when adding/creating block argumentsRiver Riddle1-17/+17
BlockArguments gained the ability to have locations attached a while ago, but they have always been optional. This goes against the core tenant of MLIR where location information is a requirement, so this commit updates the API to require locations. Fixes #53279 Differential Revision: https://reviews.llvm.org/D117633
2022-01-20Preserve function argument locations.Dominik Grewe1-14/+28
Previously the optional locations of function arguments were dropped in `parseFunctionArgumentList`. This CL adds another output argument to the function through which they are now returned. The values are then plumbed through as an array of optional locations in the various places. Reviewed By: rriddle Differential Revision: https://reviews.llvm.org/D117604
2022-01-19Fix GCC 5 MLIR Build (NFC)Mehdi Amini1-3/+3
Try to fix the error: mlir/lib/Parser/Parser.cpp:553:14: error: cannot call member function 'mlir::InFlightDiagnostic mlir::detail::Parser::emitError(llvm::SMLoc, const llvm::Twine&)' without object << "operation location alias was never defined";
2022-01-18Allows deferred location attribute in `parseOptionalLocationSpecifier`Mehdi Amini1-26/+63
Before this patch, deferred location in operation like `test.pretty_printed_region` would break the parser, and so the IR round-trip. Depends On D117088 Reviewed By: rriddle Differential Revision: https://reviews.llvm.org/D117413
2022-01-18Use Opaque location for handling deferred references to aliases internally ↵Mehdi Amini1-25/+45
to the parser (NFC) This will allow to return to the client of `parseLocationInstance` a location that is resolved later. Reviewed By: rriddle Differential Revision: https://reviews.llvm.org/D117088
2021-12-22Fix more clang-tidy cleanups in mlir/ (NFC)Mehdi Amini1-2/+1
2021-12-08Adjust "end namespace" comment in MLIR to match new agree'd coding styleMehdi Amini1-3/+3
See D115115 and this mailing list discussion: https://lists.llvm.org/pipermail/llvm-dev/2021-December/154199.html Differential Revision: https://reviews.llvm.org/D115309
2021-11-23[mlir] Refactoring a few Parser APIsSandeep Dasgupta1-74/+174
Refactored two new parser APIs parseGenericOperationAfterOperands and parseCustomOperationName out of parseGenericOperation and parseCustomOperation. Motivation: Sometimes an op can be printed in a special way if certain criteria is met. While parsing, we need to handle all the versions. `parseGenericOperationAfterOperands` is handy in situation where we already parsed the operands and decide to fall back to default parsing. `parseCustomOperationName` is useful when we need to know details (dialect, operation name etc.) about a parsed token meant to be an mlir operation. Reviewed By: rriddle Differential Revision: https://reviews.llvm.org/D113719
2021-11-18[mlir] Convert NamedAttribute to be a classRiver Riddle1-1/+1
NamedAttribute is currently represented as an std::pair, but this creates an extremely clunky .first/.second API. This commit converts it to a class, with better accessors (getName/getValue) and also opens the door for more convenient API in the future. Differential Revision: https://reviews.llvm.org/D113956
2021-11-17[mlir] Refactor AbstractOperation and OperationNameRiver Riddle1-32/+28
The current implementation is quite clunky; OperationName stores either an Identifier or an AbstractOperation that corresponds to an operation. This has several problems: * OperationNames created before and after an operation are registered are different * Accessing the identifier name/dialect/etc. from an OperationName are overly branchy - they need to dyn_cast a PointerUnion to check the state This commit refactors this such that we create a single information struct for every operation name, even operations that aren't registered yet. When an OperationName is created for an unregistered operation, we only populate the name field. When the operation is registered, we populate the remaining fields. With this we now have two new classes: OperationName and RegisteredOperationName. These both point to the same underlying operation information struct, but only RegisteredOperationName can assume that the operation is actually registered. This leads to a much cleaner API, and we can also move some AbstractOperation functionality directly to OperationName. Differential Revision: https://reviews.llvm.org/D114049
2021-11-11[mlir] Replace usages of Identifier with StringAttrRiver Riddle1-1/+1
Identifier and StringAttr essentially serve the same purpose, i.e. to hold a string value. Keeping these seemingly identical pieces of functionality separate has caused problems in certain situations: * Identifier has nice accessors that StringAttr doesn't * Identifier can't be used as an Attribute, meaning strings are often duplicated between Identifier/StringAttr (e.g. in PDL) The only thing that Identifier has that StringAttr doesn't is support for caching a dialect that is referenced by the string (e.g. dialect.foo). This functionality is added to StringAttr, as this is useful for StringAttr in generally the same ways it was useful for Identifier. Differential Revision: https://reviews.llvm.org/D113536
2021-10-14[MLIR] Fix assert crash when an unregistered dialect op is encounteredUday Bondhugula1-10/+19
Fix assert crash when an unregistered dialect op is encountered during parsing and `-allow-unregistered-dialect' isn't on. Instead, emit an error. While on this, clean up "registered" vs "loaded" on `getDialect()` and local clang-tidy warnings. https://llvm.discourse.group/t/assert-behavior-on-unregistered-dialect-ops/4402 Differential Revision: https://reviews.llvm.org/D111628
2021-09-29AsmParser::getContext() - there can be only one. This should unbreak the build.Chris Lattner1-2/+0