Skip to content

Commit 1826fad

Browse files
committed
[mlir][bytecode] Avoid recording null arglocs & realloc opnames.
For block arg locs a common case is no/uknown location (where the producer signifies they don't care about blockarg location). Also avoid needing to dynamically resize opnames during parsing. Assumed to be post lazy loading change, so chose version 3. Differential Revision: https://reviews.llvm.org/D151038
1 parent 12ccc59 commit 1826fad

File tree

6 files changed

+45
-13
lines changed

6 files changed

+45
-13
lines changed

mlir/docs/BytecodeFormat.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ dialects that were also referenced.
154154
dialect_section {
155155
numDialects: varint,
156156
dialectNames: varint[],
157+
numTotalOpNames: varint,
157158
opNames: op_name_group[]
158159
}
159160
@@ -444,8 +445,8 @@ block_arguments {
444445
}
445446
446447
block_argument {
447-
typeIndex: varint,
448-
location: varint
448+
typeAndLocation: varint, // (type << 1) | (hasLocation)
449+
location: varint? // Optional, else unknown location
449450
}
450451
```
451452

mlir/include/mlir/Bytecode/Encoding.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ enum {
2929
kMinSupportedVersion = 0,
3030

3131
/// The current bytecode version.
32-
kVersion = 3,
32+
kVersion = 4,
3333

3434
/// An arbitrary value used to fill alignment padding.
3535
kAlignmentByte = 0xCB,

mlir/lib/Bytecode/Reader/BytecodeReader.cpp

+25-5
Original file line numberDiff line numberDiff line change
@@ -1603,6 +1603,14 @@ BytecodeReader::Impl::parseDialectSection(ArrayRef<uint8_t> sectionData) {
16031603
opNames.emplace_back(dialect, opName);
16041604
return success();
16051605
};
1606+
// Avoid re-allocation in bytecode version > 3 where the number of ops are
1607+
// known.
1608+
if (version > 3) {
1609+
uint64_t numOps;
1610+
if (failed(sectionReader.parseVarInt(numOps)))
1611+
return failure();
1612+
opNames.reserve(numOps);
1613+
}
16061614
while (!sectionReader.empty())
16071615
if (failed(parseDialectGrouping(sectionReader, dialects, parseOpName)))
16081616
return failure();
@@ -2175,13 +2183,25 @@ LogicalResult BytecodeReader::Impl::parseBlockArguments(EncodingReader &reader,
21752183
argTypes.reserve(numArgs);
21762184
argLocs.reserve(numArgs);
21772185

2186+
Location unknownLoc = UnknownLoc::get(config.getContext());
21782187
while (numArgs--) {
21792188
Type argType;
2180-
LocationAttr argLoc;
2181-
if (failed(parseType(reader, argType)) ||
2182-
failed(parseAttribute(reader, argLoc)))
2183-
return failure();
2184-
2189+
LocationAttr argLoc = unknownLoc;
2190+
if (version > 3) {
2191+
// Parse the type with hasLoc flag to determine if it has type.
2192+
uint64_t typeIdx;
2193+
bool hasLoc;
2194+
if (failed(reader.parseVarIntWithFlag(typeIdx, hasLoc)) ||
2195+
!(argType = attrTypeReader.resolveType(typeIdx)))
2196+
return failure();
2197+
if (hasLoc && failed(parseAttribute(reader, argLoc)))
2198+
return failure();
2199+
} else {
2200+
// All args has type and location.
2201+
if (failed(parseType(reader, argType)) ||
2202+
failed(parseAttribute(reader, argLoc)))
2203+
return failure();
2204+
}
21852205
argTypes.push_back(argType);
21862206
argLocs.push_back(argLoc);
21872207
}

mlir/lib/Bytecode/Writer/BytecodeWriter.cpp

+14-3
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,9 @@ void BytecodeWriter::writeDialectSection(EncodingEmitter &emitter) {
585585
std::move(versionEmitter));
586586
}
587587

588+
if (config.bytecodeVersion > 3)
589+
dialectEmitter.emitVarInt(size(numberingState.getOpNames()));
590+
588591
// Emit the referenced operation names grouped by dialect.
589592
auto emitOpName = [&](OpNameNumbering &name) {
590593
dialectEmitter.emitVarInt(stringSection.insert(name.name.stripDialect()));
@@ -670,8 +673,16 @@ void BytecodeWriter::writeBlock(EncodingEmitter &emitter, Block *block) {
670673
if (hasArgs) {
671674
emitter.emitVarInt(args.size());
672675
for (BlockArgument arg : args) {
673-
emitter.emitVarInt(numberingState.getNumber(arg.getType()));
674-
emitter.emitVarInt(numberingState.getNumber(arg.getLoc()));
676+
Location argLoc = arg.getLoc();
677+
if (config.bytecodeVersion > 3) {
678+
emitter.emitVarIntWithFlag(numberingState.getNumber(arg.getType()),
679+
!isa<UnknownLoc>(argLoc));
680+
if (!isa<UnknownLoc>(argLoc))
681+
emitter.emitVarInt(numberingState.getNumber(argLoc));
682+
} else {
683+
emitter.emitVarInt(numberingState.getNumber(arg.getType()));
684+
emitter.emitVarInt(numberingState.getNumber(argLoc));
685+
}
675686
}
676687
if (config.bytecodeVersion > 2) {
677688
uint64_t maskOffset = emitter.size();
@@ -755,7 +766,7 @@ void BytecodeWriter::writeOp(EncodingEmitter &emitter, Operation *op) {
755766

756767
for (Region &region : op->getRegions()) {
757768
// If the region is not isolated from above, or we are emitting bytecode
758-
// targetting version <2, we don't use a section.
769+
// targeting version <2, we don't use a section.
759770
if (!isIsolatedFromAbove || config.bytecodeVersion < 2) {
760771
writeRegion(emitter, &region);
761772
continue;

mlir/test/Bytecode/general.mlir

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
}
3333
"bytecode.branch"()[^secondBlock] : () -> ()
3434

35-
^secondBlock(%arg1: i32, %arg2: !bytecode.int, %arg3: !pdl.operation):
35+
^secondBlock(%arg1: i32 loc(unknown), %arg2: !bytecode.int, %arg3: !pdl.operation loc(unknown)):
3636
"bytecode.regions"() ({
3737
"bytecode.operands"(%arg1, %arg2, %arg3) : (i32, !bytecode.int, !pdl.operation) -> ()
3838
"bytecode.return"() : () -> ()

mlir/test/Bytecode/invalid/invalid-structure.mlir

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
//===--------------------------------------------------------------------===//
1010

1111
// RUN: not mlir-opt %S/invalid-structure-version.mlirbc 2>&1 | FileCheck %s --check-prefix=VERSION
12-
// VERSION: bytecode version 127 is newer than the current version 3
12+
// VERSION: bytecode version 127 is newer than the current version
1313

1414
//===--------------------------------------------------------------------===//
1515
// Producer

0 commit comments

Comments
 (0)