Skip to content

[IR2Vec] Support for lazy computation of BB Embeddings #142033

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 1 commit into from
May 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions llvm/include/llvm/Analysis/IR2Vec.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ class Embedder {
/// the embeddings is specific to the kind of embeddings being computed.
virtual void computeEmbeddings() const = 0;

/// Helper function to compute the embedding for a given basic block.
/// Specific to the kind of embeddings being computed.
virtual void computeEmbeddings(const BasicBlock &BB) const = 0;

/// Lookup vocabulary for a given Key. If the key is not found, it returns a
/// zero vector.
Embedding lookupVocab(const std::string &Key) const;
Expand Down Expand Up @@ -121,6 +125,11 @@ class Embedder {
/// for the function and returns the map.
const BBEmbeddingsMap &getBBVecMap() const;

/// Returns the embedding for a given basic block in the function F if it has
/// been computed. If not, it computes the embedding for the basic block and
/// returns it.
const Embedding &getBBVector(const BasicBlock &BB) const;

/// Computes and returns the embedding for the current function.
const Embedding &getFunctionVector() const;
};
Expand All @@ -130,16 +139,14 @@ class Embedder {
/// representations obtained from the Vocabulary.
class SymbolicEmbedder : public Embedder {
private:
/// Utility function to compute the embedding for a given basic block.
Embedding computeBB2Vec(const BasicBlock &BB) const;

/// Utility function to compute the embedding for a given type.
Embedding getTypeEmbedding(const Type *Ty) const;

/// Utility function to compute the embedding for a given operand.
Embedding getOperandEmbedding(const Value *Op) const;

void computeEmbeddings() const override;
void computeEmbeddings(const BasicBlock &BB) const override;

public:
SymbolicEmbedder(const Function &F, const Vocab &Vocabulary,
Expand Down
31 changes: 19 additions & 12 deletions llvm/lib/Analysis/IR2Vec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,14 @@ const BBEmbeddingsMap &Embedder::getBBVecMap() const {
return BBVecMap;
}

const Embedding &Embedder::getBBVector(const BasicBlock &BB) const {
auto It = BBVecMap.find(&BB);
if (It != BBVecMap.end())
return It->second;
computeEmbeddings(BB);
return BBVecMap[&BB];
}

const Embedding &Embedder::getFunctionVector() const {
// Currently, we always (re)compute the embeddings for the function.
// This is cheaper than caching the vector.
Expand Down Expand Up @@ -152,17 +160,7 @@ Embedding SymbolicEmbedder::getOperandEmbedding(const Value *Op) const {

#undef RETURN_LOOKUP_IF

void SymbolicEmbedder::computeEmbeddings() const {
if (F.isDeclaration())
return;
for (const auto &BB : F) {
auto [It, WasInserted] = BBVecMap.try_emplace(&BB, computeBB2Vec(BB));
assert(WasInserted && "Basic block already exists in the map");
addVectors(FuncVector, It->second);
}
}

Embedding SymbolicEmbedder::computeBB2Vec(const BasicBlock &BB) const {
void SymbolicEmbedder::computeEmbeddings(const BasicBlock &BB) const {
Embedding BBVector(Dimension, 0);

for (const auto &I : BB) {
Expand All @@ -184,7 +182,16 @@ Embedding SymbolicEmbedder::computeBB2Vec(const BasicBlock &BB) const {
InstVecMap[&I] = InstVector;
addVectors(BBVector, InstVector);
}
return BBVector;
BBVecMap[&BB] = BBVector;
}

void SymbolicEmbedder::computeEmbeddings() const {
if (F.isDeclaration())
return;
for (const auto &BB : F) {
computeEmbeddings(BB);
addVectors(FuncVector, BBVecMap[&BB]);
}
}

// ==----------------------------------------------------------------------===//
Expand Down
10 changes: 10 additions & 0 deletions llvm/unittests/Analysis/IR2VecTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class TestableEmbedder : public Embedder {
TestableEmbedder(const Function &F, const Vocab &V, unsigned Dim)
: Embedder(F, V, Dim) {}
void computeEmbeddings() const override {}
void computeEmbeddings(const BasicBlock &BB) const override {}
using Embedder::lookupVocab;
static void addVectors(Embedding &Dst, const Embedding &Src) {
Embedder::addVectors(Dst, Src);
Expand Down Expand Up @@ -229,6 +230,15 @@ TEST(IR2VecTest, GetBBVecMap) {
ElementsAre(DoubleNear(1.29, 1e-6), DoubleNear(2.31, 1e-6)));
}

TEST(IR2VecTest, GetBBVector) {
GetterTestEnv Env;
const auto &BBVec = Env.Emb->getBBVector(*Env.BB);

EXPECT_EQ(BBVec.size(), 2u);
EXPECT_THAT(BBVec,
ElementsAre(DoubleNear(1.29, 1e-6), DoubleNear(2.31, 1e-6)));
}

TEST(IR2VecTest, GetFunctionVector) {
GetterTestEnv Env;
const auto &FuncVec = Env.Emb->getFunctionVector();
Expand Down
Loading