[llvm] Ret (PR #104839)

Kun Liu via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 19 12:41:00 PDT 2024


https://github.com/Ryan-hub-bit created https://github.com/llvm/llvm-project/pull/104839

None

>From 68db1a578dce4a0cda2335f24a348a43ab24e72f Mon Sep 17 00:00:00 2001
From: ryan <liu864579887 at gmail.com>
Date: Mon, 19 Aug 2024 12:09:19 -0500
Subject: [PATCH 1/2] error

---
 .../Transforms/IPO/InterProceduralGraph.h     |  59 +++++++++
 llvm/lib/Passes/PassBuilder.cpp               |   1 +
 llvm/lib/Passes/PassRegistry.def              |   1 +
 llvm/lib/Transforms/IPO/CMakeLists.txt        |   1 +
 .../Transforms/IPO/InterProceduralGraph.cpp   | 119 ++++++++++++++++++
 5 files changed, 181 insertions(+)
 create mode 100644 llvm/include/llvm/Transforms/IPO/InterProceduralGraph.h
 create mode 100644 llvm/lib/Transforms/IPO/InterProceduralGraph.cpp

diff --git a/llvm/include/llvm/Transforms/IPO/InterProceduralGraph.h b/llvm/include/llvm/Transforms/IPO/InterProceduralGraph.h
new file mode 100644
index 00000000000000..38fbcf9f3c74ad
--- /dev/null
+++ b/llvm/include/llvm/Transforms/IPO/InterProceduralGraph.h
@@ -0,0 +1,59 @@
+#ifndef INTERPROCEDURALGRAPH_PASS_H
+#define INTERPROCEDURALGRAPH_PASS_H
+
+#include "llvm/IR/Module.h"
+#include "llvm/IR/Function.h"
+#include "llvm/IR/Instructions.h"
+#include "llvm/Analysis/CallGraph.h"
+#include "llvm/Passes/PassBuilder.h"
+#include "llvm/Passes/CallGraphWrapperPass.h"
+#include "llvm/Support/raw_ostream.h"
+#include <map>
+#include <vector>
+#include <set>
+
+namespace llvm {
+
+// Forward declaration of the pass
+class InterproceduralGraphPass;
+
+// Node structure to represent graph nodes
+struct Node {
+    std::string name;
+    // Additional properties can be added here
+};
+
+// Edge structure to represent graph edges
+struct Edge {
+    std::string from;
+    std::string to;
+};
+
+// InterproceduralGraph class to manage graph nodes and edges
+struct InterproceduralGraph {
+    std::map<std::string, Node> nodes;
+    std::vector<Edge> edges;
+    std::set<std::string> functionsWithIndirectCalls;
+
+    void addNode(Node node);
+    void addEdge(const std::string &from, const std::string &to);
+    void addIntraproceduralEdges(Function &F);
+    void addInterproceduralEdges(CallGraph &CG);
+    void addIndirectCallEdges();
+    std::set<std::string> getPossibleIndirectTargets(const std::string &FuncName);
+    void outputGraph();
+};
+
+// InterproceduralGraphPass class to define the pass
+class InterproceduralGraphPass : public PassInfoMixin<InterproceduralGraphPass> {
+public:
+    // Function to run the pass
+    PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
+
+    // Function to check if the pass is required
+    static bool isRequired();
+};
+
+} // namespace llvm
+
+#endif // INTERPROCEDURALGRAPH_PASS_H
diff --git a/llvm/lib/Passes/PassBuilder.cpp b/llvm/lib/Passes/PassBuilder.cpp
index 17eed97fd950c9..eb1178ac692803 100644
--- a/llvm/lib/Passes/PassBuilder.cpp
+++ b/llvm/lib/Passes/PassBuilder.cpp
@@ -181,6 +181,7 @@
 #include "llvm/Transforms/IPO/StripSymbols.h"
 #include "llvm/Transforms/IPO/SyntheticCountsPropagation.h"
 #include "llvm/Transforms/IPO/WholeProgramDevirt.h"
+#include "llvm/Transforms/IPO/InterProceduralGraph.h"
 #include "llvm/Transforms/InstCombine/InstCombine.h"
 #include "llvm/Transforms/Instrumentation.h"
 #include "llvm/Transforms/Instrumentation/AddressSanitizer.h"
diff --git a/llvm/lib/Passes/PassRegistry.def b/llvm/lib/Passes/PassRegistry.def
index 6b5e1cf83c4698..9f51f9d1098a67 100644
--- a/llvm/lib/Passes/PassRegistry.def
+++ b/llvm/lib/Passes/PassRegistry.def
@@ -44,6 +44,7 @@ MODULE_ALIAS_ANALYSIS("globals-aa", GlobalsAA())
 #ifndef MODULE_PASS
 #define MODULE_PASS(NAME, CREATE_PASS)
 #endif
+MODULE_PASS("inter-procedural-graph", InterproceduralGraphPass())
 MODULE_PASS("always-inline", AlwaysInlinerPass())
 MODULE_PASS("annotation2metadata", Annotation2MetadataPass())
 MODULE_PASS("assign-guid", AssignGUIDPass())
diff --git a/llvm/lib/Transforms/IPO/CMakeLists.txt b/llvm/lib/Transforms/IPO/CMakeLists.txt
index 92a9697720efd4..f42c0bce6a9993 100644
--- a/llvm/lib/Transforms/IPO/CMakeLists.txt
+++ b/llvm/lib/Transforms/IPO/CMakeLists.txt
@@ -27,6 +27,7 @@ add_llvm_component_library(LLVMipo
   InferFunctionAttrs.cpp
   Inliner.cpp
   Internalize.cpp
+  InterProceduralGraph.cpp
   LoopExtractor.cpp
   LowerTypeTests.cpp
   MemProfContextDisambiguation.cpp
diff --git a/llvm/lib/Transforms/IPO/InterProceduralGraph.cpp b/llvm/lib/Transforms/IPO/InterProceduralGraph.cpp
new file mode 100644
index 00000000000000..df0f5e658bfeb4
--- /dev/null
+++ b/llvm/lib/Transforms/IPO/InterProceduralGraph.cpp
@@ -0,0 +1,119 @@
+#include "llvm/Transforms/IPO/InterProceduralGraph.h"
+#include "llvm/IR/Module.h"
+#include "llvm/IR/Function.h"
+#include "llvm/IR/Instructions.h"
+#include "llvm/Analysis/CallGraph.h"
+#include "llvm/Passes/PassBuilder.h"
+#include "llvm/Passes/CallGraphWrapperPass.h"
+#include "llvm/Support/raw_ostream.h"
+#include <map>
+#include <vector>
+#include <set>
+
+using namespace llvm;
+
+namespace {
+
+struct Node {
+    std::string name;
+    // Additional properties can be added here
+};
+
+struct Edge {
+    std::string from;
+    std::string to;
+};
+
+struct InterproceduralGraph {
+    std::map<std::string, Node> nodes;
+    std::vector<Edge> edges;
+    std::set<std::string> functionsWithIndirectCalls;
+
+    void addNode(Node node) {
+        nodes[node.name] = node;
+    }
+
+    void addEdge(const std::string &from, const std::string &to) {
+        edges.push_back({from, to});
+    }
+
+    void addIntraproceduralEdges(Function &F) {
+        for (BasicBlock &BB : F) {
+            for (Instruction &I : BB) {
+                if (auto *CI = dyn_cast<CallInst>(&I)) {
+                    if (auto *Callee = dyn_cast<Function>(CI->getCalledFunction())) {
+                        if (Callee && !Callee->isDeclaration()) {
+                            addEdge(F.getName().str(), Callee->getName().str());
+                            functionsWithIndirectCalls.insert(Callee->getName().str());
+                        }
+                    }
+                }
+            }
+        }
+    }
+
+    void addInterproceduralEdges(CallGraph &CG) {
+        for (auto &nodePair : CG) {
+            const Function *caller = nodePair.first;
+            CallGraphNode *cgn = nodePair.second.get();
+
+            for (auto it = cgn->begin(); it != cgn->end(); ++it) {
+                CallGraphNode::CallRecord callRecord = *it;
+                const Function *callee = callRecord.second->getFunction();
+                if (callee) {
+                    addEdge(caller->getName().str(), callee->getName().str());
+                }
+            }
+        }
+    }
+
+    void addIndirectCallEdges() {
+        for (const auto &FuncName : functionsWithIndirectCalls) {
+            for (const auto &TargetFuncName : getPossibleIndirectTargets(FuncName)) {
+                addEdge(FuncName, TargetFuncName);
+            }
+        }
+    }
+
+    std::set<std::string> getPossibleIndirectTargets(const std::string &FuncName) {
+        return std::set<std::string>();
+    }
+
+    void outputGraph() {
+        errs() << "Nodes:\n";
+        for (const auto &NodePair : nodes) {
+            errs() << "  " << NodePair.first << "\n";
+        }
+
+        errs() << "Edges:\n";
+        for (const auto &Edge : edges) {
+            errs() << "  " << Edge.from << " -> " << Edge.to << "\n";
+        }
+    }
+};
+
+class InterproceduralGraphPass : public PassInfoMixin<InterproceduralGraphPass> {
+public:
+    PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM) {
+        InterproceduralGraph IPG;
+
+        CallGraph &CG = AM.getResult<CallGraphAnalysis>(M);
+
+        for (Function &F : M) {
+            Node functionNode;
+            functionNode.name = F.getName();
+            IPG.addNode(functionNode);
+            IPG.addIntraproceduralEdges(F);
+        }
+
+        IPG.addInterproceduralEdges(CG);
+        IPG.addIndirectCallEdges();
+        IPG.outputGraph();
+
+        return PreservedAnalyses::none();
+    }
+
+    static bool isRequired() { return true; }
+};
+
+} // end of anonymous namespace

>From de44837d2c4f32603c6e4a0fffc0821edb5beee1 Mon Sep 17 00:00:00 2001
From: ryan <liu864579887 at gmail.com>
Date: Mon, 19 Aug 2024 14:39:40 -0500
Subject: [PATCH 2/2] IPO

---
 llvm/include/llvm/Transforms/IPO/InterProceduralGraph.h | 5 ++---
 llvm/lib/Transforms/IPO/InterProceduralGraph.cpp        | 5 ++---
 2 files changed, 4 insertions(+), 6 deletions(-)

diff --git a/llvm/include/llvm/Transforms/IPO/InterProceduralGraph.h b/llvm/include/llvm/Transforms/IPO/InterProceduralGraph.h
index 38fbcf9f3c74ad..d34813717b68a6 100644
--- a/llvm/include/llvm/Transforms/IPO/InterProceduralGraph.h
+++ b/llvm/include/llvm/Transforms/IPO/InterProceduralGraph.h
@@ -4,9 +4,8 @@
 #include "llvm/IR/Module.h"
 #include "llvm/IR/Function.h"
 #include "llvm/IR/Instructions.h"
-#include "llvm/Analysis/CallGraph.h"
 #include "llvm/Passes/PassBuilder.h"
-#include "llvm/Passes/CallGraphWrapperPass.h"
+#include "llvm/Analysis/CallGraph.h"
 #include "llvm/Support/raw_ostream.h"
 #include <map>
 #include <vector>
@@ -51,7 +50,7 @@ class InterproceduralGraphPass : public PassInfoMixin<InterproceduralGraphPass>
     PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
 
     // Function to check if the pass is required
-    static bool isRequired();
+    //static bool isRequired();
 };
 
 } // namespace llvm
diff --git a/llvm/lib/Transforms/IPO/InterProceduralGraph.cpp b/llvm/lib/Transforms/IPO/InterProceduralGraph.cpp
index df0f5e658bfeb4..c0f41b71eacd56 100644
--- a/llvm/lib/Transforms/IPO/InterProceduralGraph.cpp
+++ b/llvm/lib/Transforms/IPO/InterProceduralGraph.cpp
@@ -4,7 +4,6 @@
 #include "llvm/IR/Instructions.h"
 #include "llvm/Analysis/CallGraph.h"
 #include "llvm/Passes/PassBuilder.h"
-#include "llvm/Passes/CallGraphWrapperPass.h"
 #include "llvm/Support/raw_ostream.h"
 #include <map>
 #include <vector>
@@ -94,7 +93,7 @@ struct InterproceduralGraph {
 
 class InterproceduralGraphPass : public PassInfoMixin<InterproceduralGraphPass> {
 public:
-    PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM) {
+     PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM) {
         InterproceduralGraph IPG;
 
         CallGraph &CG = AM.getResult<CallGraphAnalysis>(M);
@@ -113,7 +112,7 @@ class InterproceduralGraphPass : public PassInfoMixin<InterproceduralGraphPass>
         return PreservedAnalyses::none();
     }
 
-    static bool isRequired() { return true; }
+    //static bool isRequired() { return true; }
 };
 
 } // end of anonymous namespace



More information about the llvm-commits mailing list