diff options
| author | Mehdi Amini <joker.eph@gmail.com> | 2023-05-24 21:52:05 -0700 |
|---|---|---|
| committer | Mehdi Amini <joker.eph@gmail.com> | 2023-05-25 00:27:59 -0700 |
| commit | cfd90939f70805f9824ac6b99bbe3ef4e50c7e8b (patch) | |
| tree | b1e041122728636e6a41e659e0fd0b598a713175 /mlir/lib/Bytecode/Reader/BytecodeReader.cpp | |
| parent | e84589c9cce0915b223d1c6eda3320f498b6a598 (diff) | |
| download | llvm-cfd90939f70805f9824ac6b99bbe3ef4e50c7e8b.zip llvm-cfd90939f70805f9824ac6b99bbe3ef4e50c7e8b.tar.gz llvm-cfd90939f70805f9824ac6b99bbe3ef4e50c7e8b.tar.bz2 | |
Fix MLIR bytecode loading of resources
The bytecode reader didn't handle properly the case where resource names
conflicted and were renamed, leading to orphan handles in the IR as well
as overwriting the exiting resources.
Differential Revision: https://reviews.llvm.org/D151408
Diffstat (limited to 'mlir/lib/Bytecode/Reader/BytecodeReader.cpp')
| -rw-r--r-- | mlir/lib/Bytecode/Reader/BytecodeReader.cpp | 15 |
1 files changed, 14 insertions, 1 deletions
diff --git a/mlir/lib/Bytecode/Reader/BytecodeReader.cpp b/mlir/lib/Bytecode/Reader/BytecodeReader.cpp index 05a1d33..8ff48ad 100644 --- a/mlir/lib/Bytecode/Reader/BytecodeReader.cpp +++ b/mlir/lib/Bytecode/Reader/BytecodeReader.cpp @@ -24,6 +24,7 @@ #include "llvm/ADT/ScopeExit.h" #include "llvm/ADT/SmallString.h" #include "llvm/ADT/StringExtras.h" +#include "llvm/ADT/StringRef.h" #include "llvm/Support/MemoryBufferRef.h" #include "llvm/Support/SaveAndRestore.h" #include "llvm/Support/SourceMgr.h" @@ -516,6 +517,7 @@ public: private: /// The table of dialect resources within the bytecode file. SmallVector<AsmDialectResourceHandle> dialectResources; + llvm::StringMap<std::string> dialectResourceHandleRenamingMap; }; class ParsedResourceEntry : public AsmParsedResourceEntry { @@ -604,6 +606,7 @@ parseResourceGroup(Location fileLoc, bool allowEmpty, EncodingReader &offsetReader, EncodingReader &resourceReader, StringSectionReader &stringReader, T *handler, const std::shared_ptr<llvm::SourceMgr> &bufferOwnerRef, + function_ref<StringRef(StringRef)> remapKey = {}, function_ref<LogicalResult(StringRef)> processKeyFn = {}) { uint64_t numResources; if (failed(offsetReader.parseVarInt(numResources))) @@ -635,6 +638,7 @@ parseResourceGroup(Location fileLoc, bool allowEmpty, // Otherwise, parse the resource value. EncodingReader entryReader(data, fileLoc); + key = remapKey(key); ParsedResourceEntry entry(key, kind, entryReader, stringReader, bufferOwnerRef); if (failed(handler->parseResource(entry))) @@ -665,8 +669,16 @@ LogicalResult ResourceSectionReader::initialize( // provides most of the arguments. auto parseGroup = [&](auto *handler, bool allowEmpty = false, function_ref<LogicalResult(StringRef)> keyFn = {}) { + auto resolveKey = [&](StringRef key) -> StringRef { + auto it = dialectResourceHandleRenamingMap.find(key); + if (it == dialectResourceHandleRenamingMap.end()) + return ""; + return it->second; + }; + return parseResourceGroup(fileLoc, allowEmpty, offsetReader, resourceReader, - stringReader, handler, bufferOwnerRef, keyFn); + stringReader, handler, bufferOwnerRef, resolveKey, + keyFn); }; // Read the external resources from the bytecode. @@ -714,6 +726,7 @@ LogicalResult ResourceSectionReader::initialize( << "unknown 'resource' key '" << key << "' for dialect '" << dialect->name << "'"; } + dialectResourceHandleRenamingMap[key] = handler->getResourceKey(*handle); dialectResources.push_back(*handle); return success(); }; |
