[llvm] Refactoring llvm-ir2vec.cpp for better separation of concerns in the Tooling classes (PR #170078)
via llvm-commits
llvm-commits at lists.llvm.org
Mon Dec 1 00:14:00 PST 2025
github-actions[bot] wrote:
<!--LLVM CODE FORMAT COMMENT: {clang-format}-->
:warning: C/C++ code formatter, clang-format found issues in your code. :warning:
<details>
<summary>
You can test this locally with the following command:
</summary>
``````````bash
git-clang-format --diff origin/main HEAD --extensions cpp -- llvm/tools/llvm-ir2vec/llvm-ir2vec.cpp --diff_from_common_commit
``````````
:warning:
The reproduction instructions above might return results for more than one PR
in a stack if you are using a stacked PR workflow. You can limit the results by
changing `origin/main` to the base branch/commit you want to compare against.
:warning:
</details>
<details>
<summary>
View the diff from clang-format here.
</summary>
``````````diff
diff --git a/llvm/tools/llvm-ir2vec/llvm-ir2vec.cpp b/llvm/tools/llvm-ir2vec/llvm-ir2vec.cpp
index e0058cbc8..f82759ecb 100644
--- a/llvm/tools/llvm-ir2vec/llvm-ir2vec.cpp
+++ b/llvm/tools/llvm-ir2vec/llvm-ir2vec.cpp
@@ -58,13 +58,13 @@
#include "llvm/Analysis/IR2Vec.h"
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/Function.h"
+#include "llvm/IR/InstIterator.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/PassInstrumentation.h"
#include "llvm/IR/PassManager.h"
#include "llvm/IR/Type.h"
-#include "llvm/IR/InstIterator.h"
#include "llvm/IRReader/IRReader.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Debug.h"
@@ -225,16 +225,16 @@ public:
if (HasPrevOpcode) {
Result.Triplets.push_back({PrevOpcode, Opcode, NextRelation});
LLVM_DEBUG(dbgs()
- << Vocabulary::getVocabKeyForOpcode(PrevOpcode + 1) << '\t'
- << Vocabulary::getVocabKeyForOpcode(Opcode + 1) << '\t'
- << "Next\n");
+ << Vocabulary::getVocabKeyForOpcode(PrevOpcode + 1) << '\t'
+ << Vocabulary::getVocabKeyForOpcode(Opcode + 1) << '\t'
+ << "Next\n");
}
Result.Triplets.push_back({Opcode, TypeID, TypeRelation});
LLVM_DEBUG(
dbgs() << Vocabulary::getVocabKeyForOpcode(Opcode + 1) << '\t'
- << Vocabulary::getVocabKeyForTypeID(I.getType()->getTypeID())
- << '\t' << "Type\n");
+ << Vocabulary::getVocabKeyForTypeID(I.getType()->getTypeID())
+ << '\t' << "Type\n");
unsigned ArgIndex = 0;
for (const Use &U : I.operands()) {
@@ -246,7 +246,7 @@ public:
StringRef OperandStr = Vocabulary::getVocabKeyForOperandKind(
Vocabulary::getOperandKind(U.get()));
dbgs() << Vocabulary::getVocabKeyForOpcode(Opcode + 1) << '\t'
- << OperandStr << '\t' << "Arg" << ArgIndex << '\n';
+ << OperandStr << '\t' << "Arg" << ArgIndex << '\n';
});
++ArgIndex;
@@ -272,9 +272,8 @@ public:
for (const Function &F : M) {
TripletResult FuncResult = getTriplets(F);
Result.MaxRelation = std::max(Result.MaxRelation, FuncResult.MaxRelation);
- Result.Triplets.insert(Result.Triplets.end(),
- FuncResult.Triplets.begin(),
- FuncResult.Triplets.end());
+ Result.Triplets.insert(Result.Triplets.end(), FuncResult.Triplets.begin(),
+ FuncResult.Triplets.end());
}
return Result;
@@ -379,23 +378,23 @@ public:
OS << "Function: " << F.getName() << "\n";
- auto printListLevel = [&](const auto& list, const char* suffix_str) {
- for (const auto& [name, embedding] : list) {
+ auto printListLevel = [&](const auto &list, const char *suffix_str) {
+ for (const auto &[name, embedding] : list) {
OS << name << suffix_str;
embedding.print(OS);
}
};
switch (Level) {
- case EmbeddingLevel::FunctionLevel:
- getFunctionEmbedding(F).print(OS);
- break;
- case EmbeddingLevel::BasicBlockLevel:
- printListLevel(getBBEmbeddings(F), ":");
- break;
- case EmbeddingLevel::InstructionLevel:
- printListLevel(getInstEmbeddings(F), "");
- break;
+ case EmbeddingLevel::FunctionLevel:
+ getFunctionEmbedding(F).print(OS);
+ break;
+ case EmbeddingLevel::BasicBlockLevel:
+ printListLevel(getBBEmbeddings(F), ":");
+ break;
+ case EmbeddingLevel::InstructionLevel:
+ printListLevel(getInstEmbeddings(F), "");
+ break;
}
}
};
@@ -499,7 +498,7 @@ public:
<< "No machine functions found to initialize vocabulary\n";
return false;
}
-
+
/// Get triplets for a single machine function
/// Returns TripletResult containing MaxRelation and vector of Triplets
TripletResult getTriplets(const MachineFunction &MF) const {
@@ -549,8 +548,8 @@ public:
// Update MaxRelation if there were operands
if (ArgIndex > 0)
- Result.MaxRelation = std::max(Result.MaxRelation,
- MIRArgRelation + ArgIndex - 1);
+ Result.MaxRelation =
+ std::max(Result.MaxRelation, MIRArgRelation + ArgIndex - 1);
PrevOpcode = OpcodeID;
HasPrevOpcode = true;
@@ -559,7 +558,7 @@ public:
return Result;
}
-
+
/// Get triplets for the entire module
/// Returns TripletResult containing aggregated MaxRelation and all Triplets
TripletResult getTriplets(const Module &M) const {
@@ -579,9 +578,8 @@ public:
TripletResult FuncResult = getTriplets(*MF);
Result.MaxRelation = std::max(Result.MaxRelation, FuncResult.MaxRelation);
- Result.Triplets.insert(Result.Triplets.end(),
- FuncResult.Triplets.begin(),
- FuncResult.Triplets.end());
+ Result.Triplets.insert(Result.Triplets.end(), FuncResult.Triplets.begin(),
+ FuncResult.Triplets.end());
}
return Result;
@@ -715,9 +713,10 @@ public:
OS << "MIR2Vec embeddings for machine function " << MF.getName() << ":\n";
- auto printListLevel = [&](const auto& list, const char* label, const char* prefix_str, const char* suffix_str) {
+ auto printListLevel = [&](const auto &list, const char *label,
+ const char *prefix_str, const char *suffix_str) {
OS << label << ":\n";
- for (const auto& [name, embedding] : list) {
+ for (const auto &[name, embedding] : list) {
OS << prefix_str << name << suffix_str;
embedding.print(OS);
}
``````````
</details>
https://github.com/llvm/llvm-project/pull/170078
More information about the llvm-commits
mailing list