-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[NFC][IR2Vec] Moving parseVocabSection()
to VocabStorage
#161711
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@llvm/pr-subscribers-llvm-analysis @llvm/pr-subscribers-mlgo Author: S. VenkataKeerthy (svkeerthy) ChangesFull diff: https://github.com/llvm/llvm-project/pull/161711.diff 2 Files Affected:
diff --git a/llvm/include/llvm/Analysis/IR2Vec.h b/llvm/include/llvm/Analysis/IR2Vec.h
index b7c301580a8a4..ed43f19b4a7d3 100644
--- a/llvm/include/llvm/Analysis/IR2Vec.h
+++ b/llvm/include/llvm/Analysis/IR2Vec.h
@@ -210,6 +210,13 @@ class VocabStorage {
const_iterator end() const {
return const_iterator(this, getNumSections(), 0);
}
+
+ using VocabMap = std::map<std::string, Embedding>;
+ /// Parse a vocabulary section from JSON and populate the target vocabulary
+ /// map.
+ static Error parseVocabSection(StringRef Key,
+ const json::Value &ParsedVocabValue,
+ VocabMap &TargetVocab, unsigned &Dim);
};
/// Class for storing and accessing the IR2Vec vocabulary.
@@ -600,8 +607,6 @@ class IR2VecVocabAnalysis : public AnalysisInfoMixin<IR2VecVocabAnalysis> {
Error readVocabulary(VocabMap &OpcVocab, VocabMap &TypeVocab,
VocabMap &ArgVocab);
- Error parseVocabSection(StringRef Key, const json::Value &ParsedVocabValue,
- VocabMap &TargetVocab, unsigned &Dim);
void generateVocabStorage(VocabMap &OpcVocab, VocabMap &TypeVocab,
VocabMap &ArgVocab);
void emitError(Error Err, LLVMContext &Ctx);
diff --git a/llvm/lib/Analysis/IR2Vec.cpp b/llvm/lib/Analysis/IR2Vec.cpp
index af30422b73759..295b6d33525d9 100644
--- a/llvm/lib/Analysis/IR2Vec.cpp
+++ b/llvm/lib/Analysis/IR2Vec.cpp
@@ -330,6 +330,43 @@ bool VocabStorage::const_iterator::operator!=(
return !(*this == Other);
}
+Error VocabStorage::parseVocabSection(StringRef Key,
+ const json::Value &ParsedVocabValue,
+ VocabMap &TargetVocab, unsigned &Dim) {
+ json::Path::Root Path("");
+ const json::Object *RootObj = ParsedVocabValue.getAsObject();
+ if (!RootObj)
+ return createStringError(errc::invalid_argument,
+ "JSON root is not an object");
+
+ const json::Value *SectionValue = RootObj->get(Key);
+ if (!SectionValue)
+ return createStringError(errc::invalid_argument,
+ "Missing '" + std::string(Key) +
+ "' section in vocabulary file");
+ if (!json::fromJSON(*SectionValue, TargetVocab, Path))
+ return createStringError(errc::illegal_byte_sequence,
+ "Unable to parse '" + std::string(Key) +
+ "' section from vocabulary");
+
+ Dim = TargetVocab.begin()->second.size();
+ if (Dim == 0)
+ return createStringError(errc::illegal_byte_sequence,
+ "Dimension of '" + std::string(Key) +
+ "' section of the vocabulary is zero");
+
+ if (!std::all_of(TargetVocab.begin(), TargetVocab.end(),
+ [Dim](const std::pair<StringRef, Embedding> &Entry) {
+ return Entry.second.size() == Dim;
+ }))
+ return createStringError(
+ errc::illegal_byte_sequence,
+ "All vectors in the '" + std::string(Key) +
+ "' section of the vocabulary are not of the same dimension");
+
+ return Error::success();
+}
+
// ==----------------------------------------------------------------------===//
// Vocabulary
//===----------------------------------------------------------------------===//
@@ -460,43 +497,6 @@ VocabStorage Vocabulary::createDummyVocabForTest(unsigned Dim) {
// IR2VecVocabAnalysis
//===----------------------------------------------------------------------===//
-Error IR2VecVocabAnalysis::parseVocabSection(
- StringRef Key, const json::Value &ParsedVocabValue, VocabMap &TargetVocab,
- unsigned &Dim) {
- json::Path::Root Path("");
- const json::Object *RootObj = ParsedVocabValue.getAsObject();
- if (!RootObj)
- return createStringError(errc::invalid_argument,
- "JSON root is not an object");
-
- const json::Value *SectionValue = RootObj->get(Key);
- if (!SectionValue)
- return createStringError(errc::invalid_argument,
- "Missing '" + std::string(Key) +
- "' section in vocabulary file");
- if (!json::fromJSON(*SectionValue, TargetVocab, Path))
- return createStringError(errc::illegal_byte_sequence,
- "Unable to parse '" + std::string(Key) +
- "' section from vocabulary");
-
- Dim = TargetVocab.begin()->second.size();
- if (Dim == 0)
- return createStringError(errc::illegal_byte_sequence,
- "Dimension of '" + std::string(Key) +
- "' section of the vocabulary is zero");
-
- if (!std::all_of(TargetVocab.begin(), TargetVocab.end(),
- [Dim](const std::pair<StringRef, Embedding> &Entry) {
- return Entry.second.size() == Dim;
- }))
- return createStringError(
- errc::illegal_byte_sequence,
- "All vectors in the '" + std::string(Key) +
- "' section of the vocabulary are not of the same dimension");
-
- return Error::success();
-}
-
// FIXME: Make this optional. We can avoid file reads
// by auto-generating a default vocabulary during the build time.
Error IR2VecVocabAnalysis::readVocabulary(VocabMap &OpcVocab,
@@ -513,16 +513,16 @@ Error IR2VecVocabAnalysis::readVocabulary(VocabMap &OpcVocab,
return ParsedVocabValue.takeError();
unsigned OpcodeDim = 0, TypeDim = 0, ArgDim = 0;
- if (auto Err =
- parseVocabSection("Opcodes", *ParsedVocabValue, OpcVocab, OpcodeDim))
+ if (auto Err = VocabStorage::parseVocabSection("Opcodes", *ParsedVocabValue,
+ OpcVocab, OpcodeDim))
return Err;
- if (auto Err =
- parseVocabSection("Types", *ParsedVocabValue, TypeVocab, TypeDim))
+ if (auto Err = VocabStorage::parseVocabSection("Types", *ParsedVocabValue,
+ TypeVocab, TypeDim))
return Err;
- if (auto Err =
- parseVocabSection("Arguments", *ParsedVocabValue, ArgVocab, ArgDim))
+ if (auto Err = VocabStorage::parseVocabSection("Arguments", *ParsedVocabValue,
+ ArgVocab, ArgDim))
return Err;
if (!(OpcodeDim == TypeDim && TypeDim == ArgDim))
|
mahesh-attarde
pushed a commit
to mahesh-attarde/llvm-project
that referenced
this pull request
Oct 3, 2025
MixedMatched
pushed a commit
to MixedMatched/llvm-project
that referenced
this pull request
Oct 3, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
No description provided.