[Mlir-commits] [mlir] 0ee4875 - [mlir][bytecode] Error if requested bytecode version is unsupported

Mehdi Amini llvmlistbot at llvm.org
Wed May 31 19:20:54 PDT 2023


Author: Kevin Gleason
Date: 2023-05-31T19:20:42-07:00
New Revision: 0ee4875ddff08ba1cdc96bc85a72a51727eb88f6

URL: https://github.com/llvm/llvm-project/commit/0ee4875ddff08ba1cdc96bc85a72a51727eb88f6
DIFF: https://github.com/llvm/llvm-project/commit/0ee4875ddff08ba1cdc96bc85a72a51727eb88f6.diff

LOG: [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

Added: 
    

Modified: 
    mlir/include/mlir/Bytecode/BytecodeWriter.h
    mlir/lib/Bytecode/Writer/BytecodeWriter.cpp
    mlir/test/Bytecode/versioning/versioned_bytecode.mlir

Removed: 
    


################################################################################
diff  --git a/mlir/include/mlir/Bytecode/BytecodeWriter.h b/mlir/include/mlir/Bytecode/BytecodeWriter.h
index 4a4cec822ff0e..c6df1a21a55bb 100644
--- a/mlir/include/mlir/Bytecode/BytecodeWriter.h
+++ b/mlir/include/mlir/Bytecode/BytecodeWriter.h
@@ -40,10 +40,9 @@ class BytecodeWriterConfig {
   /// Return an instance of the internal implementation.
   const Impl &getImpl() const { return *impl; }
 
-  /// Set the desired bytecode version to emit. This function clamps the version
-  /// to the existing version if larger than existing. The desired version may
-  /// not be used depending on the features used and the actual version required
-  /// is returned by bytecode writer entry point.
+  /// Set the desired bytecode version to emit. This method does not validate
+  /// the desired version. The bytecode writer entry point will return failure
+  /// if it cannot emit the desired version.
   void setDesiredBytecodeVersion(int64_t bytecodeVersion);
 
   /// Get the set desired bytecode version to emit.

diff  --git a/mlir/lib/Bytecode/Writer/BytecodeWriter.cpp b/mlir/lib/Bytecode/Writer/BytecodeWriter.cpp
index 515391d5634c1..3be342b363548 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.

diff  --git a/mlir/test/Bytecode/versioning/versioned_bytecode.mlir b/mlir/test/Bytecode/versioning/versioned_bytecode.mlir
index bf08a23c03ae0..6fcc3832eec28 100644
--- a/mlir/test/Bytecode/versioning/versioned_bytecode.mlir
+++ b/mlir/test/Bytecode/versioning/versioned_bytecode.mlir
@@ -12,3 +12,14 @@
 // RUN: mlir-opt %S/versioned-op-1.12.mlirbc -o %t.2 && \
 // RUN: 
diff  %t.1 %t.2
 
+//===--------------------------------------------------------------------===//
+// Test invalid versions
+//===--------------------------------------------------------------------===//
+
+// RUN: not mlir-opt %S/versioned-op-1.12.mlirbc -emit-bytecode \
+// RUN:   -emit-bytecode-version=-1 2>&1 | FileCheck %s --check-prefix=ERR_VERSION_NEGATIVE
+// ERR_VERSION_NEGATIVE: unsupported version requested -1, must be in range [{{[0-9]+}}, {{[0-9]+}}]
+
+// RUN: not mlir-opt %S/versioned-op-1.12.mlirbc -emit-bytecode \
+// RUN:   -emit-bytecode-version=999 2>&1 | FileCheck %s --check-prefix=ERR_VERSION_FUTURE
+// ERR_VERSION_FUTURE: unsupported version requested 999, must be in range [{{[0-9]+}}, {{[0-9]+}}]


        


More information about the Mlir-commits mailing list