diff --git a/mlir/include/mlir/Bytecode/BytecodeWriter.h b/mlir/include/mlir/Bytecode/BytecodeWriter.h --- a/mlir/include/mlir/Bytecode/BytecodeWriter.h +++ b/mlir/include/mlir/Bytecode/BytecodeWriter.h @@ -40,10 +40,9 @@ /// 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 --- a/mlir/lib/Bytecode/Writer/BytecodeWriter.cpp +++ b/mlir/lib/Bytecode/Writer/BytecodeWriter.cpp @@ -65,9 +65,7 @@ } void BytecodeWriterConfig::setDesiredBytecodeVersion(int64_t bytecodeVersion) { - // Clamp to current version. - impl->bytecodeVersion = - std::min(bytecodeVersion, bytecode::kVersion); + impl->bytecodeVersion = bytecodeVersion; } int64_t BytecodeWriterConfig::getDesiredBytecodeVersion() const { @@ -630,6 +628,13 @@ 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(bytecode::kMinSupportedVersion) << ", " + << static_cast(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 --- 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]+}}]