aboutsummaryrefslogtreecommitdiff
path: root/mlir/lib/Bytecode/Writer
AgeCommit message (Collapse)AuthorFilesLines
2025-08-20[MLIR] Adopt LDBG() debug macro in BytecodeWriter.cpp (NFC) (#154642)Mehdi Amini1-9/+6
2025-06-09[mlir] Use *Map::try_emplace (NFC) (#143341)Kazu Hirata1-2/+2
- try_emplace(Key) is shorter than insert({Key, nullptr}). - try_emplace performs value initialization without value parameters. - We overwrite values on successful insertion anyway.
2025-04-14[mlir] Use llvm::append_range (NFC) (#135722)Kazu Hirata1-3/+2
2025-03-31[MLIR][NFC] Fix incomplete boundary comments. (#133516)Han-Chung Wang1-0/+6
I observed that we have the boundary comments in the codebase like: ``` //===----------------------------------------------------------------------===// // ... //===----------------------------------------------------------------------===// ``` I also observed that there are incomplete boundary comments. The revision is generated by a script that completes the boundary comments. ``` //===----------------------------------------------------------------------===// // ... ... ``` Signed-off-by: hanhanW <hanhan0912@gmail.com>
2025-02-12[mlir] BytecodeWriter: invoke `reserveExtraSpace` (#126953)Nikhil Kalra1-0/+3
Update `BytecodeWriter` to invoke `reserveExtraSpace` on the stream before writing to it. This will give clients implementing custom output streams the opportunity to allocate an appropriately sized buffer for the write.
2025-02-06[MLIR] Add move constructor to BytecodeWriterConfig (#126130)Karim Nosseir1-0/+3
The config is currently not movable and because there are constructors the default move won't be generated, which prevents it from being moved. Also, it is not copyable because of the unique_ptr. This PR adds move constructor to allow moving it.
2025-01-11[mlir] Migrate away from PointerUnion::{is,get} (NFC) (#122591)Kazu Hirata1-2/+2
Note that PointerUnion::{is,get} have been soft deprecated in PointerUnion.h: // FIXME: Replace the uses of is(), get() and dyn_cast() with // isa<T>, cast<T> and the llvm::dyn_cast<T> I'm not touching PointerUnion::dyn_cast for now because it's a bit complicated; we could blindly migrate it to dyn_cast_if_present, but we should probably use dyn_cast when the operand is known to be non-null.
2024-07-20Add logging for emit functions in BytecodeWriter.cpp (#99558)Kevin Gleason1-113/+153
Recently there was a change to materializing unrealized conversion casts, which inserted conversion that previously did not exist during legalization (https://github.com/llvm/llvm-project/pull/97903), after these cases are inserted and then washed away after transformation completes, it caused the use-list ordering of an op to change in some cases: `my.add %arg0(use1), %arg0(use2) --> my.add %arg0(use2), %arg0(use1)`, which subtly changes the bytecode emitted since this is considered a custom use-list. When investigating why the bytecode had changed I added the following logging which helped track down the difference, in my case it showed extra bytes with "use-list section". With `-debug-only=mlir-bytecode-writer` emits logs like the following, detailing the source of written bytes: ``` emitBytes(4b) bytecode header emitVarInt(6) bytecode version emitByte(13) bytecode version emitBytes(17b) bytecode producer emitByte(0) null terminator emitVarInt(2) dialects count ... emitByte(5) dialect version emitVarInt(4) op names count emitByte(9) op names count emitVarInt(0) dialect number ... emitVarInt(2) dialect writer emitByte(5) dialect writer emitVarInt(9259963783827161088) dialect APInt ... emitVarInt(3) attr/type offset emitByte(7) attr/type offset emitByte(3) section code emitVarInt(18) section size ... ``` Note: this uses string constants and `StringLiteral`, I'm not sure if these are washed away during compilation / OK to have these around for debuggin, or if there's a better way to do this? Alternative was adding many braces and `LLVM_DEBUG` calls at each callsite, but this felt more error prone / likely to miss some callsites.
2024-07-15[mlir] Remove bytecode reader & writer header from interface. (#98920)Jacques Pienaar1-0/+1
Flagged some additional headers missing in process. Inspired by #98676
2024-07-02mlir/LogicalResult: move into llvm (#97309)Ramkumar Ramachandra1-1/+0
This patch is part of a project to move the Presburger library into LLVM.
2024-04-10[mlir] Slightly optimize bytecode op numbering (#88310)Jeff Niu1-7/+7
If the bytecode encoding supports properties, then the dictionary attribute is always the raw dictionary attribute of the operation, regardless of what it contains. Otherwise, get the dictionary attribute from the op: if the op does not have properties, then it returns the raw dictionary, otherwise it returns the combined inherent and discardable attributes.
2024-01-04[mlir] don't use magic numbers in IRNumbering.cppAlex Zinenko1-5/+9
Bytecode versions have named constants that should be used instead of magic numbers.
2024-01-04[mlir] fix bytecode writer after c1eab57673ef3eb28Alex Zinenko2-5/+9
The change in c1eab57 fixed the behavior of `getDiscardableAttrDictionary` for ops that are not using properties to only return discardable attributes. Bytecode writer was relying on the wrong behavior and would assume all attributes are discardable, without appropriate testing. Fix that and add a test.
2023-11-13[mlir][bytecode] Add bytecode writer config API to skip serialization of ↵Matteo Franciolini1-6/+20
resources (#71991) When serializing to bytecode, users can select the option to elide resources from the bytecode file. This will instruct the bytecode writer to serialize only the key and resource kind, while skipping serialization of the data buffer. At parsing, the IR is built in memory with valid (but empty) resource handlers.
2023-10-31[mlir][bytecode] Implements back deployment capability for MLIR dialects ↵Matteo Franciolini2-12/+57
(#70724) When emitting bytecode, clients can specify a target dialect version to emit in `BytecodeWriterConfig`. This exposes a target dialect version to the DialectBytecodeWriter, which can be queried by name and used to back-deploy attributes, types, and properties.
2023-10-21Apply clang-tidy fixes for llvm-qualified-auto in IRNumbering.cpp (NFC)Mehdi Amini1-1/+1
2023-08-02[MLIR][Bytecode] Add missing field initializer in constructor initializer listMehdi Amini1-2/+2
Leaving this field unitialized could led to crashes when it'll diverge from the IRNumbering phase. Differential Revision: https://reviews.llvm.org/D156965
2023-07-28Expose callbacks for encoding of types/attributesMatteo Franciolini2-32/+99
[mlir] Expose a mechanism to provide a callback for encoding types and attributes in MLIR bytecode. Two callbacks are exposed, respectively, to the BytecodeWriterConfig and to the ParserConfig. At bytecode parsing/printing, clients have the ability to specify a callback to be used to optionally read/write the encoding. On failure, fallback path will execute the default parsers and printers for the dialect. Testing shows how to leverage this functionality to support back-deployment and backward-compatibility usecases when roundtripping to bytecode a client dialect with type/attributes dependencies on upstream. Reviewed By: rriddle Differential Revision: https://reviews.llvm.org/D153383
2023-07-28Revert "Expose callbacks for encoding of types/attributes"Mehdi Amini2-99/+32
This reverts commit b299ec16661f653df66cdaf161cdc5441bc9803c. The authorship informations were incorrect.
2023-07-28Expose callbacks for encoding of types/attributesMehdi Amini2-32/+99
[mlir] Expose a mechanism to provide a callback for encoding types and attributes in MLIR bytecode. Two callbacks are exposed, respectively, to the BytecodeWriterConfig and to the ParserConfig. At bytecode parsing/printing, clients have the ability to specify a callback to be used to optionally read/write the encoding. On failure, fallback path will execute the default parsers and printers for the dialect. Testing shows how to leverage this functionality to support back-deployment and backward-compatibility usecases when roundtripping to bytecode a client dialect with type/attributes dependencies on upstream. Reviewed By: rriddle Differential Revision: https://reviews.llvm.org/D153383
2023-07-25[mlir:bytecode] Only visit the all regions path if the op has regionsRiver Riddle1-1/+1
Zero region operations return true for both isBeforeAllRegions and isAfterAllRegions when using WalkStage. The bytecode walk only expects region holding operations in the after regions path, so guard against that.
2023-07-25[mlir:bytecode] Support lazy loading dynamically isolated regionsRiver Riddle3-22/+162
We currently only support lazy loading for regions that statically implement the IsolatedFromAbove trait, but that limits the amount of operations that can be lazily loaded. This review lifts that restriction by computing which operations have isolated regions when numbering, allowing any operation to be lazily loaded as long as it doesn't use values defined above. Differential Revision: https://reviews.llvm.org/D156199
2023-07-25[mlir:bytecode] Fix bytecode lazy loading for ops with multiple regionsRiver Riddle1-11/+15
We currently encode each region as a separate section, but the reader expects all of the regions to be in the same section. This updates the writer to match the behavior that the reader expects. Differential Revision: https://reviews.llvm.org/D156198
2023-07-24Update ODS variadic segments "magic" attributes to use native PropertiesMehdi Amini2-1/+8
The operand_segment_sizes and result_segment_sizes Attributes are now inlined in the operation as native propertie. We continue to support building an Attribute on the fly for `getAttr("operand_segment_sizes")` and setting the property from an attribute with `setAttr("operand_segment_sizes", attr)`. A new bytecode version is introduced to support backward compatibility and backdeployments. Differential Revision: https://reviews.llvm.org/D155919
2023-07-24Revert "Update ODS variadic segments "magic" attributes to use native ↵Mehdi Amini2-8/+1
Properties" This reverts commit 20b93abca6516bbb23689c3777536fea04e46e14. One python test is broken, WIP.
2023-07-24Update ODS variadic segments "magic" attributes to use native PropertiesMehdi Amini2-1/+8
The operand_segment_sizes and result_segment_sizes Attributes are now inlined in the operation as native propertie. We continue to support building an Attribute on the fly for `getAttr("operand_segment_sizes")` and setting the property from an attribute with `setAttr("operand_segment_sizes", attr)`. A new bytecode version is introduced to support backward compatibility and backdeployments. Differential Revision: https://reviews.llvm.org/D155919
2023-06-27[mlir][VectorType] Allow arbitrary dimensions to be scalableAndrzej Warzynski2-0/+3
At the moment, only the trailing dimensions in the vector type can be scalable, i.e. this is supported: vector<2x[4]xf32> and this is not allowed: vector<[2]x4xf32> This patch extends the vector type so that arbitrary dimensions can be scalable. To this end, an array of bool values is added to every vector type to denote whether the corresponding dimensions are scalable or not. For example, for this vector: vector<[2]x[3]x4xf32> the following array would be created: {true, true, false}. Additionally, the current syntax: vector<[2x3]x4xf32> is replaced with: vector<[2]x[3]x4xf32> This is primarily to simplify parsing (this way, the parser can easily process one dimension at a time rather than e.g. tracking whether "scalable block" has been entered/left). NOTE: The `isScalableDim` parameter of `VectorType` (introduced in this patch) makes `numScalableDims` redundant. For the time being, `numScalableDims` is preserved to facilitate the transition between the two parameters. `numScalableDims` will be removed in one of the subsequent patches. This change is a part of a larger effort to enable scalable vectorisation in Linalg. See this RFC for more context: * https://discourse.llvm.org/t/rfc-scalable-vectorisation-in-linalg/ Differential Revision: https://reviews.llvm.org/D153372
2023-06-23Fix bytecode reader/writer on big-endian platformsUlrich Weigand1-2/+5
This makes the bytecode reader/writer work on big-endian platforms. The only problem was related to encoding of multi-byte integers, where both reader and writer code make implicit assumptions about endianness of the host platform. This fixes the current test failures on s390x, and in addition allows to remove the UNSUPPORTED markers from all other bytecode-related test cases - they now also all pass on s390x. Also adding a GFAIL_SKIP to the MultiModuleWithResource unit test, as this still fails due to an unrelated endian bug regarding decoding of external resources. Differential Revision: https://reviews.llvm.org/D153567 Reviewed By: mehdi_amini, jpienaar, rriddle
2023-06-06Use symbolic name for previous MLIR Bytecode versionsMehdi Amini1-14/+16
Reviewed By: jpienaar, burmako Differential Revision: https://reviews.llvm.org/D151621
2023-05-31[mlir][bytecode] Error if requested bytecode version is unsupportedKevin Gleason1-3/+8
Currently desired bytecode version is clamped to the maximum. This allows requesting bytecode versions that do not exist. We have added callsite validation for this in StableHLO to ensure we don't pass an invalid version number, probably better if this is managed upstream. If a user wants to use the current version, then omitting `setDesiredBytecodeVersion` is the best way to do that (as opposed to providing a large number). Adding this check will also properly error on older version numbers as we increment the minimum supported version. Silently claming on minimum version would likely lead to unintentional forward incompatibilities. Separately, due to bytecode version being `int64_t` and using methods to read/write uints, we can generate payloads with invalid version numbers: ``` mlir-opt file.mlir --emit-bytecode --emit-bytecode-version=-1 | mlir-opt <stdin>:0:0: error: bytecode version 18446744073709551615 is newer than the current version 5 ``` This is fixed with version bounds checking as well. Reviewed By: mehdi_amini Differential Revision: https://reviews.llvm.org/D151838
2023-05-27[mlir][bazel] Port for 660f714e26999d266232a1fbb02712bb879bd34eHaojian Wu1-1/+0
2023-05-26[MLIR] Add native Bytecode support for propertiesMehdi Amini4-30/+245
This is adding a new interface (`BytecodeOpInterface`) to allow operations to opt-in skipping conversion to attribute and serializing properties to native bytecode. The scheme relies on a new section where properties are stored in sequence { size, serialize_properties }, ... The operations are storing the index of a properties, a table of offset is built when loading the properties section the first time. This is a re-commit of 837d1ce0dc which conflicted with another patch upgrading the bytecode and the collision wasn't properly resolved before. Differential Revision: https://reviews.llvm.org/D151065
2023-05-25Revert "[MLIR] Add native Bytecode support for properties"Mehdi Amini4-244/+30
This reverts commit ca5a12fd69d4acf70c08f797cbffd714dd548348 and follow-up fixes: df34c288c428eb4b867c8075def48b3d1727d60b 07dc906883af660780cf6d0cc1044f7e74dab83e ab80ad0095083fda062c23ac90df84c40b4332c8 837d1ce0dc8eec5b17255291b3462e6296cb369b The first commit was incomplete and broken, I'll prepare a new version later, in the meantime pull this work out of tree.
2023-05-25Fix MLIR Bytecode backward deploymentMehdi Amini1-1/+1
The condition for guarding the properties section was reversed.
2023-05-25Fix MLIR back-deployment to version < 5 ; properties section should not be ↵Eugene Burmako1-1/+9
emitted. This was an oversight in the development of bytecode version 5, which was caught by downstream StableHLO compatibility tests. Differential revision: https://reviews.llvm.org/D151531
2023-05-25[MLIR] Add native Bytecode support for propertiesMehdi Amini4-30/+236
This is adding a new interface (`BytecodeOpInterface`) to allow operations to opt-in skipping conversion to attribute and serializing properties to native bytecode. The scheme relies on a new section where properties are stored in sequence { size, serialize_properties }, ... The operations are storing the index of a properties, a table of offset is built when loading the properties section the first time. Back-deployment to version prior to 4 are relying on getAttrDictionnary() which we intend to deprecate and remove: that is putting a de-factor end-of-support horizon for supporting deployments to version older than 4. Differential Revision: https://reviews.llvm.org/D151065
2023-05-25[mlir][bytecode] Avoid recording null arglocs & realloc opnames.Jacques Pienaar1-3/+14
For block arg locs a common case is no/uknown location (where the producer signifies they don't care about blockarg location). Also avoid needing to dynamically resize opnames during parsing. Assumed to be post lazy loading change, so chose version 3. Differential Revision: https://reviews.llvm.org/D151038
2023-05-21Preserve use-list orders in mlir bytecodeMatteo Franciolini3-4/+120
This patch implements a mechanism to read/write use-list orders from/to the mlir bytecode format. When producing bytecode, use-list orders are appended to each value of the IR. When reading bytecode, use-lists orders are loaded in memory and used at the end of parsing to sort the existing use-list chains. Reviewed By: mehdi_amini Differential Revision: https://reviews.llvm.org/D149755
2023-05-20Add support for Lazyloading to the MLIR bytecodeMehdi Amini1-2/+12
IsolatedRegions are emitted in sections in order for the reader to be able to skip over them. A new class is exposed to manage the state and allow the readers to load these IsolatedRegions on-demand. Differential Revision: https://reviews.llvm.org/D149515
2023-05-12[mlir] Move casting calls from methods to function callsTres Popp1-2/+2
The MLIR classes Type/Attribute/Operation/Op/Value support cast/dyn_cast/isa/dyn_cast_or_null functionality through llvm's doCast functionality in addition to defining methods with the same name. This change begins the migration of uses of the method to the corresponding function call as has been decided as more consistent. Note that there still exist classes that only define methods directly, such as AffineExpr, and this does not include work currently to support a functional cast/isa call. Caveats include: - This clang-tidy script probably has more problems. - This only touches C++ code, so nothing that is being generated. Context: - https://mlir.llvm.org/deprecation/ at "Use the free function variants for dyn_cast/cast/isa/…" - Original discussion at https://discourse.llvm.org/t/preferred-casting-style-going-forward/68443 Implementation: This first patch was created with the following steps. The intention is to only do automated changes at first, so I waste less time if it's reverted, and so the first mass change is more clear as an example to other teams that will need to follow similar steps. Steps are described per line, as comments are removed by git: 0. Retrieve the change from the following to build clang-tidy with an additional check: https://github.com/llvm/llvm-project/compare/main...tpopp:llvm-project:tidy-cast-check 1. Build clang-tidy 2. Run clang-tidy over your entire codebase while disabling all checks and enabling the one relevant one. Run on all header files also. 3. Delete .inc files that were also modified, so the next build rebuilds them to a pure state. 4. Some changes have been deleted for the following reasons: - Some files had a variable also named cast - Some files had not included a header file that defines the cast functions - Some files are definitions of the classes that have the casting methods, so the code still refers to the method instead of the function without adding a prefix or removing the method declaration at the same time. ``` ninja -C $BUILD_DIR clang-tidy run-clang-tidy -clang-tidy-binary=$BUILD_DIR/bin/clang-tidy -checks='-*,misc-cast-functions'\ -header-filter=mlir/ mlir/* -fix rm -rf $BUILD_DIR/tools/mlir/**/*.inc git restore mlir/lib/IR mlir/lib/Dialect/DLTI/DLTI.cpp\ mlir/lib/Dialect/Complex/IR/ComplexDialect.cpp\ mlir/lib/**/IR/\ mlir/lib/Dialect/SparseTensor/Transforms/SparseVectorization.cpp\ mlir/lib/Dialect/Vector/Transforms/LowerVectorMultiReduction.cpp\ mlir/test/lib/Dialect/Test/TestTypes.cpp\ mlir/test/lib/Dialect/Transform/TestTransformDialectExtension.cpp\ mlir/test/lib/Dialect/Test/TestAttributes.cpp\ mlir/unittests/TableGen/EnumsGenTest.cpp\ mlir/test/python/lib/PythonTestCAPI.cpp\ mlir/include/mlir/IR/ ``` Differential Revision: https://reviews.llvm.org/D150123
2023-04-30[mlir][bytecode] Return error instead of min versionJacques Pienaar1-6/+4
Can't return a well-formed IR output while enabling version to be bumped up during emission. Previously it would return min version but potentially invalid IR which was confusing, instead make it return error and abort immediately instead. Differential Revision: https://reviews.llvm.org/D149569
2023-04-29[mlir][bytecode] Allow client to specify a desired version.Jacques Pienaar2-21/+50
Add method to set a desired bytecode file format to generate. Change write method to be able to return status including the minimum bytecode version needed by reader. This enables generating an older version of the bytecode (not dialect ops, attributes or types). But this does not guarantee that an older version can always be generated, e.g., if a dialect uses a new encoding only available at later bytecode version. This clamps setting to at most current version. Differential Revision: https://reviews.llvm.org/D146555
2023-03-15[ADT][mlir][NFCI] Do not use non-const lvalue-refs with enumerateJakub Kuderski1-5/+5
Replace references to enumerate results with either result_pairs (reference wrapper type) or structured bindings. I did not use structured bindings everywhere as it wasn't clear to me it would improve readability. This is in preparation to the switch to zip semantics which won't support non-const lvalue reference to elements: https://reviews.llvm.org/D144503. I chose to use values instead of const lvalue-refs because MLIR is biased towards avoiding `const` local variables. This won't degrade performance because currently `result_pair` is cheap to copy (size_t + iterator), and in the future, the enumerator iterator dereference will return temporaries anyway. Reviewed By: dblaikie Differential Revision: https://reviews.llvm.org/D146006
2023-03-10Implements MLIR Bytecode versioning capabilityMatteo Franciolini1-117/+132
A dialect can opt-in to handle versioning through the `BytecodeDialectInterface`. Few hooks are exposed to the dialect to allow managing a version encoded into the bytecode file. The version is loaded lazily and allows to retrieve the version information while parsing the input IR, and gives an opportunity to each dialect for which a version is present to perform IR upgrades post-parsing through the `upgradeFromVersion` method. Custom Attribute and Type encodings can also be upgraded according to the dialect version using readAttribute and readType methods. There is no restriction on what kind of information a dialect is allowed to encode to model its versioning. Currently, versioning is supported only for bytecode formats. Reviewed By: rriddle, mehdi_amini Differential Revision: https://reviews.llvm.org/D143647
2023-02-07[mlir][IRNumbering] Fix the dialect comparator to be strictRiver Riddle1-0/+2
Check if rhs is the dialect to be ordered first, ensuring that we don't inadvertantly order something before it by falling back to pure number comparison. This only shows up depending on the implementation of stable_sort. This was hit in a build of MSVC that was checking for strict ordering.
2023-01-16[llvm][ADT] Replace uses of `makeMutableArrayRef` with deduction guidesJoe Loser1-3/+3
Similar to how `makeArrayRef` is deprecated in favor of deduction guides, do the same for `makeMutableArrayRef`. Once all of the places in-tree are using the deduction guides for `MutableArrayRef`, we can mark `makeMutableArrayRef` as deprecated. Differential Revision: https://reviews.llvm.org/D141814
2022-09-13[mlir] Add bytecode encodings for the builtin ElementsAttr attributesRiver Riddle2-0/+7
This adds bytecode support for DenseArrayAttr, DenseIntOrFpElementsAttr, DenseStringElementsAttr, and SparseElementsAttr. Differential Revision: https://reviews.llvm.org/D133744
2022-09-13[mlir] Add fallback support for parsing/printing unknown external resourcesRiver Riddle1-0/+5
This is necessary/useful for building generic tooling that can roundtrip external resources without needing to explicitly handle them. For example, this allows for viewing the resources encoded within a bytecode file without having to explicitly know how to process them (e.g. making it easier to interact with a reproducer encoded in bytecode). Differential Revision: https://reviews.llvm.org/D133460
2022-09-13[mlir:Bytecode] Add support for encoding resourcesRiver Riddle3-18/+381
Resources are encoded in two separate sections similarly to attributes/types, one for the actual data and one for the data offsets. Unlike other sections, the resource sections are optional given that in many cases they won't be present. For testing, bytecode serialization is added for DenseResourceElementsAttr. Differential Revision: https://reviews.llvm.org/D132729
2022-08-30Apply clang-tidy fixes for readability-identifier-naming in ↵Mehdi Amini1-3/+3
BytecodeWriter.cpp (NFC)