Skip to content

Commit 9c1e558

Browse files
committed
Use symbolic name for previous MLIR Bytecode versions
Reviewed By: jpienaar, burmako Differential Revision: https://reviews.llvm.org/D151621
1 parent 1d511e1 commit 9c1e558

File tree

3 files changed

+51
-31
lines changed

3 files changed

+51
-31
lines changed

mlir/include/mlir/Bytecode/Encoding.h

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,27 @@ namespace bytecode {
2424
// General constants
2525
//===----------------------------------------------------------------------===//
2626

27-
enum {
27+
enum BytecodeVersion {
2828
/// The minimum supported version of the bytecode.
2929
kMinSupportedVersion = 0,
3030

31+
/// Dialects versioning was added in version 1.
32+
kDialectVersioning = 1,
33+
34+
/// Support for lazy-loading of isolated region was added in version 2.
35+
kLazyLoading = 2,
36+
37+
/// Use-list ordering started to be encoded in version 3.
38+
kUseListOrdering = 3,
39+
40+
/// Avoid recording unknown locations on block arguments (compression) started
41+
/// in version 4.
42+
kElideUnknownBlockArgLocation = 4,
43+
44+
/// Support for encoding properties natively in bytecode instead of merged
45+
/// with the discardable attributes.
46+
kNativePropertiesEncoding = 5,
47+
3148
/// The current bytecode version.
3249
kVersion = 5,
3350

mlir/lib/Bytecode/Reader/BytecodeReader.cpp

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ static bool isSectionOptional(bytecode::Section::ID sectionID, int version) {
8080
case bytecode::Section::kDialectVersions:
8181
return true;
8282
case bytecode::Section::kProperties:
83-
return version < 5;
83+
return version < bytecode::kNativePropertiesEncoding;
8484
default:
8585
llvm_unreachable("unknown section ID");
8686
}
@@ -492,7 +492,7 @@ struct BytecodeOperationName {
492492
StringRef name;
493493

494494
/// Whether this operation was registered when the bytecode was produced.
495-
/// This flag is populated when bytecode version >=5.
495+
/// This flag is populated when bytecode version >=kNativePropertiesEncoding.
496496
std::optional<bool> wasRegistered;
497497
};
498498
} // namespace
@@ -1647,7 +1647,7 @@ LogicalResult BytecodeReader::Impl::parseVersion(EncodingReader &reader) {
16471647
currentVersion);
16481648
}
16491649
// Override any request to lazy-load if the bytecode version is too old.
1650-
if (version < 2)
1650+
if (version < bytecode::kLazyLoading)
16511651
lazyLoading = false;
16521652
return success();
16531653
}
@@ -1699,9 +1699,9 @@ BytecodeReader::Impl::parseDialectSection(ArrayRef<uint8_t> sectionData) {
16991699

17001700
// Parse each of the dialects.
17011701
for (uint64_t i = 0; i < numDialects; ++i) {
1702-
/// Before version 1, there wasn't any versioning available for dialects,
1703-
/// and the entryIdx represent the string itself.
1704-
if (version == 0) {
1702+
/// Before version kDialectVersioning, there wasn't any versioning available
1703+
/// for dialects, and the entryIdx represent the string itself.
1704+
if (version < bytecode::kDialectVersioning) {
17051705
if (failed(stringReader.parseString(sectionReader, dialects[i].name)))
17061706
return failure();
17071707
continue;
@@ -1731,9 +1731,9 @@ BytecodeReader::Impl::parseDialectSection(ArrayRef<uint8_t> sectionData) {
17311731
auto parseOpName = [&](BytecodeDialect *dialect) {
17321732
StringRef opName;
17331733
std::optional<bool> wasRegistered;
1734-
// Prior to version 5, the information about wheter an op was registered or
1735-
// not wasn't encoded.
1736-
if (version < 5) {
1734+
// Prior to version kNativePropertiesEncoding, the information about wheter
1735+
// an op was registered or not wasn't encoded.
1736+
if (version < bytecode::kNativePropertiesEncoding) {
17371737
if (failed(stringReader.parseString(sectionReader, opName)))
17381738
return failure();
17391739
} else {
@@ -1746,9 +1746,9 @@ BytecodeReader::Impl::parseDialectSection(ArrayRef<uint8_t> sectionData) {
17461746
opNames.emplace_back(dialect, opName, wasRegistered);
17471747
return success();
17481748
};
1749-
// Avoid re-allocation in bytecode version > 3 where the number of ops are
1750-
// known.
1751-
if (version > 3) {
1749+
// Avoid re-allocation in bytecode version >=kElideUnknownBlockArgLocation
1750+
// where the number of ops are known.
1751+
if (version >= bytecode::kElideUnknownBlockArgLocation) {
17521752
uint64_t numOps;
17531753
if (failed(sectionReader.parseVarInt(numOps)))
17541754
return failure();
@@ -2078,7 +2078,7 @@ BytecodeReader::Impl::parseRegions(std::vector<RegionReadState> &regionStack,
20782078
RegionReadState childState(*op, &reader, isIsolatedFromAbove);
20792079

20802080
// Isolated regions are encoded as a section in version 2 and above.
2081-
if (version >= 2 && isIsolatedFromAbove) {
2081+
if (version >= bytecode::kLazyLoading && isIsolatedFromAbove) {
20822082
bytecode::Section::ID sectionID;
20832083
ArrayRef<uint8_t> sectionData;
20842084
if (failed(reader.parseSection(sectionID, sectionData)))
@@ -2229,7 +2229,8 @@ BytecodeReader::Impl::parseOpWithoutRegions(EncodingReader &reader,
22292229
/// Parse the use-list orders for the results of the operation. Use-list
22302230
/// orders are available since version 3 of the bytecode.
22312231
std::optional<UseListMapT> resultIdxToUseListMap = std::nullopt;
2232-
if (version > 2 && (opMask & bytecode::OpEncodingMask::kHasUseListOrders)) {
2232+
if (version >= bytecode::kUseListOrdering &&
2233+
(opMask & bytecode::OpEncodingMask::kHasUseListOrders)) {
22332234
size_t numResults = opState.types.size();
22342235
auto parseResult = parseUseListOrderForRange(reader, numResults);
22352236
if (failed(parseResult))
@@ -2316,7 +2317,7 @@ BytecodeReader::Impl::parseBlockHeader(EncodingReader &reader,
23162317
return failure();
23172318

23182319
// Uselist orders are available since version 3 of the bytecode.
2319-
if (version < 3)
2320+
if (version < bytecode::kUseListOrdering)
23202321
return success();
23212322

23222323
uint8_t hasUseListOrders = 0;
@@ -2357,7 +2358,7 @@ LogicalResult BytecodeReader::Impl::parseBlockArguments(EncodingReader &reader,
23572358
while (numArgs--) {
23582359
Type argType;
23592360
LocationAttr argLoc = unknownLoc;
2360-
if (version > 3) {
2361+
if (version >= bytecode::kElideUnknownBlockArgLocation) {
23612362
// Parse the type with hasLoc flag to determine if it has type.
23622363
uint64_t typeIdx;
23632364
bool hasLoc;

mlir/lib/Bytecode/Writer/BytecodeWriter.cpp

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -657,7 +657,7 @@ LogicalResult BytecodeWriter::write(Operation *rootOp, raw_ostream &os) {
657657
writeStringSection(emitter);
658658

659659
// Emit the properties section.
660-
if (config.bytecodeVersion >= 5)
660+
if (config.bytecodeVersion >= bytecode::kNativePropertiesEncoding)
661661
writePropertiesSection(emitter);
662662
else if (!propertiesSection.empty())
663663
return rootOp->emitError(
@@ -708,7 +708,7 @@ void BytecodeWriter::writeDialectSection(EncodingEmitter &emitter) {
708708
// Write the string section and get the ID.
709709
size_t nameID = stringSection.insert(dialect.name);
710710

711-
if (config.bytecodeVersion == 0) {
711+
if (config.bytecodeVersion < bytecode::kDialectVersioning) {
712712
dialectEmitter.emitVarInt(nameID);
713713
continue;
714714
}
@@ -732,13 +732,13 @@ void BytecodeWriter::writeDialectSection(EncodingEmitter &emitter) {
732732
std::move(versionEmitter));
733733
}
734734

735-
if (config.bytecodeVersion > 3)
735+
if (config.bytecodeVersion >= bytecode::kElideUnknownBlockArgLocation)
736736
dialectEmitter.emitVarInt(size(numberingState.getOpNames()));
737737

738738
// Emit the referenced operation names grouped by dialect.
739739
auto emitOpName = [&](OpNameNumbering &name) {
740740
size_t stringId = stringSection.insert(name.name.stripDialect());
741-
if (config.bytecodeVersion < 5)
741+
if (config.bytecodeVersion < bytecode::kNativePropertiesEncoding)
742742
dialectEmitter.emitVarInt(stringId);
743743
else
744744
dialectEmitter.emitVarIntWithFlag(stringId, name.name.isRegistered());
@@ -826,7 +826,7 @@ LogicalResult BytecodeWriter::writeBlock(EncodingEmitter &emitter,
826826
emitter.emitVarInt(args.size());
827827
for (BlockArgument arg : args) {
828828
Location argLoc = arg.getLoc();
829-
if (config.bytecodeVersion > 3) {
829+
if (config.bytecodeVersion >= bytecode::kElideUnknownBlockArgLocation) {
830830
emitter.emitVarIntWithFlag(numberingState.getNumber(arg.getType()),
831831
!isa<UnknownLoc>(argLoc));
832832
if (!isa<UnknownLoc>(argLoc))
@@ -836,7 +836,7 @@ LogicalResult BytecodeWriter::writeBlock(EncodingEmitter &emitter,
836836
emitter.emitVarInt(numberingState.getNumber(argLoc));
837837
}
838838
}
839-
if (config.bytecodeVersion > 2) {
839+
if (config.bytecodeVersion >= bytecode::kUseListOrdering) {
840840
uint64_t maskOffset = emitter.size();
841841
uint8_t encodingMask = 0;
842842
emitter.emitByte(0);
@@ -868,18 +868,19 @@ LogicalResult BytecodeWriter::writeOp(EncodingEmitter &emitter, Operation *op) {
868868

869869
// Emit the attributes of this operation.
870870
DictionaryAttr attrs = op->getDiscardableAttrDictionary();
871-
// Allow deployment to version <5 by merging inherent attribute with the
872-
// discardable ones. We should fail if there are any conflicts.
873-
if (config.bytecodeVersion < 5)
871+
// Allow deployment to version <kNativePropertiesEncoding by merging inherent
872+
// attribute with the discardable ones. We should fail if there are any
873+
// conflicts.
874+
if (config.bytecodeVersion < bytecode::kNativePropertiesEncoding)
874875
attrs = op->getAttrDictionary();
875876
if (!attrs.empty()) {
876877
opEncodingMask |= bytecode::OpEncodingMask::kHasAttrs;
877878
emitter.emitVarInt(numberingState.getNumber(attrs));
878879
}
879880

880881
// Emit the properties of this operation, for now we still support deployment
881-
// to version <5.
882-
if (config.bytecodeVersion >= 5) {
882+
// to version <kNativePropertiesEncoding.
883+
if (config.bytecodeVersion >= bytecode::kNativePropertiesEncoding) {
883884
std::optional<ssize_t> propertiesId = propertiesSection.emit(op);
884885
if (propertiesId.has_value()) {
885886
opEncodingMask |= bytecode::OpEncodingMask::kHasProperties;
@@ -913,7 +914,7 @@ LogicalResult BytecodeWriter::writeOp(EncodingEmitter &emitter, Operation *op) {
913914

914915
// Emit the use-list orders to bytecode, so we can reconstruct the same order
915916
// at parsing.
916-
if (config.bytecodeVersion > 2)
917+
if (config.bytecodeVersion >= bytecode::kUseListOrdering)
917918
writeUseListOrders(emitter, opEncodingMask, ValueRange(op->getResults()));
918919

919920
// Check for regions.
@@ -934,8 +935,9 @@ LogicalResult BytecodeWriter::writeOp(EncodingEmitter &emitter, Operation *op) {
934935

935936
for (Region &region : op->getRegions()) {
936937
// If the region is not isolated from above, or we are emitting bytecode
937-
// targeting version <2, we don't use a section.
938-
if (!isIsolatedFromAbove || config.bytecodeVersion < 2) {
938+
// targeting version <kLazyLoading, we don't use a section.
939+
if (!isIsolatedFromAbove ||
940+
config.bytecodeVersion < bytecode::kLazyLoading) {
939941
if (failed(writeRegion(emitter, &region)))
940942
return failure();
941943
continue;

0 commit comments

Comments
 (0)