From 1826fadb0d2bc5b61d6c028d8006b2a7d1249ec0 Mon Sep 17 00:00:00 2001 From: Jacques Pienaar Date: Thu, 25 May 2023 09:24:50 -0700 Subject: [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 --- mlir/lib/Bytecode/Reader/BytecodeReader.cpp | 30 ++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) (limited to 'mlir/lib/Bytecode/Reader/BytecodeReader.cpp') diff --git a/mlir/lib/Bytecode/Reader/BytecodeReader.cpp b/mlir/lib/Bytecode/Reader/BytecodeReader.cpp index 8ff48ad..ca05eac 100644 --- a/mlir/lib/Bytecode/Reader/BytecodeReader.cpp +++ b/mlir/lib/Bytecode/Reader/BytecodeReader.cpp @@ -1603,6 +1603,14 @@ BytecodeReader::Impl::parseDialectSection(ArrayRef sectionData) { opNames.emplace_back(dialect, opName); return success(); }; + // Avoid re-allocation in bytecode version > 3 where the number of ops are + // known. + if (version > 3) { + uint64_t numOps; + if (failed(sectionReader.parseVarInt(numOps))) + return failure(); + opNames.reserve(numOps); + } while (!sectionReader.empty()) if (failed(parseDialectGrouping(sectionReader, dialects, parseOpName))) return failure(); @@ -2175,13 +2183,25 @@ LogicalResult BytecodeReader::Impl::parseBlockArguments(EncodingReader &reader, argTypes.reserve(numArgs); argLocs.reserve(numArgs); + Location unknownLoc = UnknownLoc::get(config.getContext()); while (numArgs--) { Type argType; - LocationAttr argLoc; - if (failed(parseType(reader, argType)) || - failed(parseAttribute(reader, argLoc))) - return failure(); - + LocationAttr argLoc = unknownLoc; + if (version > 3) { + // Parse the type with hasLoc flag to determine if it has type. + uint64_t typeIdx; + bool hasLoc; + if (failed(reader.parseVarIntWithFlag(typeIdx, hasLoc)) || + !(argType = attrTypeReader.resolveType(typeIdx))) + return failure(); + if (hasLoc && failed(parseAttribute(reader, argLoc))) + return failure(); + } else { + // All args has type and location. + if (failed(parseType(reader, argType)) || + failed(parseAttribute(reader, argLoc))) + return failure(); + } argTypes.push_back(argType); argLocs.push_back(argLoc); } -- cgit v1.1