[llvm] NFC - Refactoring llvm-ir2vec.cpp for better separation of concerns in the Tooling classes (PR #170078)

Nishant Sachdeva via llvm-commits llvm-commits at lists.llvm.org
Fri Dec 12 23:01:47 PST 2025


================
@@ -249,23 +254,57 @@ class IR2VecTool {
           ++ArgIndex;
         }
         // Only update MaxRelation if there were operands
-        if (ArgIndex > 0) {
+        if (ArgIndex > 0)
           MaxRelation = std::max(MaxRelation, ArgRelation + ArgIndex - 1);
-        }
         PrevOpcode = Opcode;
         HasPrevOpcode = true;
       }
     }
 
-    return MaxRelation;
+    Result.MaxRelation = MaxRelation;
+    return Result;
   }
 
-  /// Dump entity ID to string mappings
-  static void generateEntityMappings(raw_ostream &OS) {
+  /// Get triplets for the entire module
+  TripletResult generateTriplets() const {
+    TripletResult Result;
+    Result.MaxRelation = NextRelation;
+
+    for (const Function &F : M.getFunctionDefs()) {
+      TripletResult FuncResult = generateTriplets(F);
+      Result.MaxRelation = std::max(Result.MaxRelation, FuncResult.MaxRelation);
+      Result.Triplets.insert(Result.Triplets.end(), FuncResult.Triplets.begin(),
+                             FuncResult.Triplets.end());
+    }
+
+    return Result;
+  }
+
+  /// Collect triplets for the module and dump output to stream
+  /// Output format: MAX_RELATION=N header followed by relationships
+  void generateTriplets(raw_ostream &OS) const {
----------------
nishant-sachdeva wrote:

This sounds like a good idea. At the moment, we have a common function name `generate_XYZ` that covers all purposes. 

I'm thinking I'll change it to
`streamTriplets(..., raw_ostream &Os)` -> `generateTriplets(...)` 
`streamEntities(..., raw_ostream &Os)` -> generateEntities(...)`
`streamEmbeddings(..., raw_ostream &Os)` 

I'll push a debug commit with this change, and if required, we can iterate on top of that

https://github.com/llvm/llvm-project/pull/170078


More information about the llvm-commits mailing list