summaryrefslogtreecommitdiff
path: root/mlir/lib/Bytecode
AgeCommit message (Collapse)Author
2025-09-11[MLIR][Bytecode] Followup 8106c81 (#157136)Nikhil Kalra
Addressed code review feedback: - Fixed some issues in the unit test - Adjusted line wrapping in the docs - Clarified comments in the bytecode reader
2025-09-04[MLIR][Bytecode] Enforce alignment requirements (#157004)Nikhil Kalra
Adds a check that the bytecode buffer is aligned to any section alignment requirements. Without this check, if the source buffer is not sufficiently aligned, we may return early when aligning the data pointer. In that case, we may end up trying to read successive sections from an incorrect offset, giving the appearance of invalid bytecode. This requirement is documented in the bytecode unit tests, but is not otherwise documented in the code or bytecode reference.
2025-08-25Revert "[MLIR] Fix duplicated attribute nodes in MLIR bytecode ↵Christian Ulmann
deserialization (#151267) (#155214) This reverts commit c075fb8c37856365fb76d986ad3aefa2400b3240. This commit introduces a caching bug that causes undesired collisions.
2025-08-20[MLIR] Adopt LDBG() debug macro in BytecodeWriter.cpp (NFC) (#154642)Mehdi Amini
2025-08-20[MLIR] Fix duplicated attribute nodes in MLIR bytecode deserialization (#151267)Hank
Fixes #150163 MLIR bytecode does not preserve alias definitions, so each attribute encountered during deserialization is treated as a new one. This can generate duplicate `DISubprogram` nodes during deserialization. The patch adds a `StringMap` cache that records attributes and fetches them when encountered again.
2025-06-09[mlir] Use *Map::try_emplace (NFC) (#143341)Kazu Hirata
- 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-05-30[mlir][Value] Add getNumUses, hasNUses, and hasNUsesOrMore to Value (#142084)Michael Maitland
We already have hasOneUse. Like llvm::Value we provide helper methods to query the number of uses of a Value. Add unittests for Value, because that was missing. --------- Co-authored-by: Michael Maitland <michaelmaitland@meta.com>
2025-04-15[mlir] Use llvm::make_first_range (NFC) (#135900)Kazu Hirata
2025-04-14[mlir] Use llvm::append_range (NFC) (#135722)Kazu Hirata
2025-03-31[MLIR][NFC] Fix incomplete boundary comments. (#133516)Han-Chung Wang
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 Kalra
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 Nosseir
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 Hirata
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-11-01[llvm][NFC] Fix typos: replace “avaliable” with “available” across ↵Wang Qiang
various files (#114524) This pull request corrects multiple occurrences of the typo "avaliable" to "available" across the LLVM and Clang codebase. These changes improve the clarity and accuracy of comments and documentation. Specific modifications are in the following files: 1. clang-tools-extra/clang-tidy/readability/FunctionCognitiveComplexityCheck.cpp: Updated comments in readability checks for cognitive complexity. 2. llvm/include/llvm/ExecutionEngine/Orc/ExecutionUtils.h: Corrected documentation for JITDylib responsibilities. 3. llvm/include/llvm/Target/TargetMacroFusion.td: Fixed descriptions for FusionPredicate variables. 4. llvm/lib/CodeGen/SafeStack.cpp: Improved comments on DominatorTree availability. 5. llvm/lib/Target/RISCV/RISCVSchedSiFive7.td: Enhanced resource usage descriptions for vector units. 6. llvm/lib/Transforms/Scalar/LoopIdiomRecognize.cpp: Updated invariant description in shift-detect idiom logic. 7. llvm/test/MC/ARM/mve-fp-registers.s: Amended ARM MVE register availability notes. 8. mlir/lib/Bytecode/Reader/BytecodeReader.cpp: Adjusted forward reference descriptions for bytecode reader operations. These changes have no impact on code functionality, focusing solely on documentation clarity. Co-authored-by: wangqiang <wangqiang1@kylinos.cn>
2024-09-12[Bytecode] Avoid repeated hash lookups (NFC) (#108320)Kazu Hirata
2024-07-20Add logging for emit functions in BytecodeWriter.cpp (#99558)Kevin Gleason
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-18[mlir][BytecodeReader] Const qualify *SectionReader, NFC (#99376)Hideto Ueno
`StringSectionReader`, `ResourceSectionReader` and `PropertiesSectionReader` are immutable after `initialize` so this PR adds const to their parsing functions and references in `AttrTypeReader` and `DialectReader`.
2024-07-15[mlir] Remove bytecode reader & writer header from interface. (#98920)Jacques Pienaar
Flagged some additional headers missing in process. Inspired by #98676
2024-07-03[mlir][bytecode] Fix external resource bytecode parsing (#97650)Jeff Niu
The key was being dropped for external resources because they aren't present in the dialect resource name mapper.
2024-07-02mlir/LogicalResult: move into llvm (#97309)Ramkumar Ramachandra
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 Niu
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-02-25Fix bytecode roundtrip of unregistered ops (#82932)Matteo Franciolini
When roundtripping to bytecode an unregistered operation name that does not contain any '.' separator, the bytecode writer will emit an op encoding without a proper opName. In this case, the string just becomes a possibly unknown dialect name. At parsing, this dialect name is used as a proper operation name. However, when the unregistered operation name coincidentally matches that of a dialect, the parser would fail. That means we can't roundtrip an unregistered op with a name that matches one of the registered dialect names. For example, ``` "index"() : () -> () ``` can be emitted but cannot be parsed, because its name is coincidentally the same as that of the Index dialect. The patch removes such inconsistency. This patch specifically fixes the bytecode roundtrip of `mlir/test/IR/parser.mlir`.
2024-02-20Fix pipeline-invalid.mlir bytecode roundtrip test (#82366)Matteo Franciolini
If an op was not contained in a region when was written to bytecode, we don't have an initialized valueScope with forward references to define.
2024-01-04[mlir] don't use magic numbers in IRNumbering.cppAlex Zinenko
Bytecode versions have named constants that should be used instead of magic numbers.
2024-01-04[mlir] fix bytecode writer after c1eab57673ef3eb28Alex Zinenko
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-12-13[mlir] Use StringRef::{starts,ends}_with (NFC)Kazu Hirata
This patch replaces uses of StringRef::{starts,ends}with with StringRef::{starts,ends}_with for consistency with std::{string,string_view}::{starts,ends}_with in C++20. I'm planning to deprecate and eventually remove StringRef::{starts,ends}with.
2023-11-13[mlir][bytecode] Add bytecode writer config API to skip serialization of ↵Matteo Franciolini
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 Franciolini
(#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 Amini
2023-09-29[mlir] Fix bytecode reading of resource sectionsMogball
This partially reverts #66380. The assertion that the underlying buffer of an EncodingReader is aligned to any required alignments for resource sections. Resources know their own alignment and pad their buffers accordingly, but the bytecode reader doesn't know that ahead of time. Consequently, it cannot give the resource EncodingReader a base buffer aligned to the maximum required alignment. A simple example from the test fails without this: ```mlir module @TestDialectResources attributes { bytecode.test = dense_resource<resource> : tensor<4xi32> } {} {-# dialect_resources: { builtin: { resource: "0x2000000001000000020000000300000004000000", resource_2: "0x2000000001000000020000000300000004000000" } } ```
2023-09-17[mlir][bytecode] Check that bytecode source buffer is sufficiently aligned. ↵Christian Sigg
(#66380) Before this change, the `ByteCode` test failed on CentOS 7 with devtoolset-9, because strings happen to be only 8 byte aligned. In general though, strings have no alignment guarantee. Increase resource alignment in test to 32 bytes. Adjust test to sufficiently align buffer. Add test to check error when buffer is insufficiently aligned.
2023-08-07[mlir] Apply ClangTidy fix (NFC)Adrian Kuegel
redundant get() call on smart pointer.
2023-08-02[MLIR][Bytecode] Add missing field initializer in constructor initializer listMehdi Amini
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 Franciolini
[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 Amini
This reverts commit b299ec16661f653df66cdaf161cdc5441bc9803c. The authorship informations were incorrect.
2023-07-28Expose callbacks for encoding of types/attributesMehdi Amini
[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 Riddle
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 Riddle
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 Riddle
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 Amini
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 Amini
Properties" This reverts commit 20b93abca6516bbb23689c3777536fea04e46e14. One python test is broken, WIP.
2023-07-24Update ODS variadic segments "magic" attributes to use native PropertiesMehdi Amini
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 Warzynski
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-26[mlir][bytecode] Fix lazy loading of non-isolated regionsRiver Riddle
The bytecode reader currently assumes all regions can be lazy loaded, which breaks reading any non-isolated region. This patch fixes that by properly handling nested non-lazy regions, and only considers isolated regions as lazy. Differential Revision: https://reviews.llvm.org/D153795
2023-06-23Fix bytecode reader/writer on big-endian platformsUlrich Weigand
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 Amini
Reviewed By: jpienaar, burmako Differential Revision: https://reviews.llvm.org/D151621
2023-05-31[mlir][bytecode] Error if requested bytecode version is unsupportedKevin Gleason
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 Wu
2023-05-27[mlir] Fix non-const lvalue reference to type 'uint64_t' cannot bind to type ↵Jie Fu
'size_t' error (NFC) /Users/jiefu/llvm-project/mlir/lib/Bytecode/Reader/BytecodeReader.cpp:1007:39: error: non-const lvalue reference to type 'uint64_t' (aka 'unsigned long long') cannot bind to a value of unrelated type 'size_t' (aka 'unsigned long') if (failed(propReader.parseVarInt(count))) ^~~~~ /Users/jiefu/llvm-project/mlir/lib/Bytecode/Reader/BytecodeReader.cpp:191:39: note: passing argument to parameter 'result' here LogicalResult parseVarInt(uint64_t &result) { ^ /Users/jiefu/llvm-project/mlir/lib/Bytecode/Reader/BytecodeReader.cpp:1020:44: error: non-const lvalue reference to type 'uint64_t' (aka 'unsigned long long') cannot bind to a value of unrelated type 'size_t' (aka 'unsigned long') if (failed(offsetsReader.parseVarInt(dataSize)) || ^~~~~~~~ /Users/jiefu/llvm-project/mlir/lib/Bytecode/Reader/BytecodeReader.cpp:191:39: note: passing argument to parameter 'result' here LogicalResult parseVarInt(uint64_t &result) { ^ 2 errors generated.
2023-05-26[MLIR] Add native Bytecode support for propertiesMehdi Amini
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