diff options
| author | Kevin Gleason <gleasonk@google.com> | 2023-05-31 18:10:42 -0700 |
|---|---|---|
| committer | Mehdi Amini <joker.eph@gmail.com> | 2023-05-31 19:20:42 -0700 |
| commit | 0ee4875ddff08ba1cdc96bc85a72a51727eb88f6 (patch) | |
| tree | 9ec1232c1fad2506697e82bab18ac0423a8c9eb8 /mlir/lib/Bytecode | |
| parent | 21dfaf60a763795e3834d67def48fc2ba5214e59 (diff) | |
[mlir][bytecode] Error if requested bytecode version is unsupported
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
Diffstat (limited to 'mlir/lib/Bytecode')
| -rw-r--r-- | mlir/lib/Bytecode/Writer/BytecodeWriter.cpp | 11 |
1 files changed, 8 insertions, 3 deletions
diff --git a/mlir/lib/Bytecode/Writer/BytecodeWriter.cpp b/mlir/lib/Bytecode/Writer/BytecodeWriter.cpp index 515391d5634c..3be342b36354 100644 --- a/mlir/lib/Bytecode/Writer/BytecodeWriter.cpp +++ b/mlir/lib/Bytecode/Writer/BytecodeWriter.cpp @@ -65,9 +65,7 @@ void BytecodeWriterConfig::attachResourcePrinter( } void BytecodeWriterConfig::setDesiredBytecodeVersion(int64_t bytecodeVersion) { - // Clamp to current version. - impl->bytecodeVersion = - std::min<int64_t>(bytecodeVersion, bytecode::kVersion); + impl->bytecodeVersion = bytecodeVersion; } int64_t BytecodeWriterConfig::getDesiredBytecodeVersion() const { @@ -630,6 +628,13 @@ LogicalResult BytecodeWriter::write(Operation *rootOp, raw_ostream &os) { emitter.emitString("ML\xefR"); // Emit the bytecode version. + if (config.bytecodeVersion < bytecode::kMinSupportedVersion || + config.bytecodeVersion > bytecode::kVersion) + return rootOp->emitError() + << "unsupported version requested " << config.bytecodeVersion + << ", must be in range [" + << static_cast<int64_t>(bytecode::kMinSupportedVersion) << ", " + << static_cast<int64_t>(bytecode::kVersion) << ']'; emitter.emitVarInt(config.bytecodeVersion); // Emit the producer. |
