LLZK 0.1.0
Veridise's ZK Language IR
Loading...
Searching...
No Matches
CallGraphPasses.cpp
Go to the documentation of this file.
1//===-- CallGraphPasses.cpp -------------------------------------*- C++ -*-===//
2//
3// Part of the LLZK Project, under the Apache License v2.0.
4// See LICENSE.txt for license information.
5// Copyright 2025 Veridise Inc.
6// SPDX-License-Identifier: Apache-2.0
7//
8// The contents of this file are adapted from llvm/lib/Analysis/CallGraph.cpp
9// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
10// See https://llvm.org/LICENSE.txt for license information.
11// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
12//
13//===----------------------------------------------------------------------===//
19//===----------------------------------------------------------------------===//
20
24
25#include <llvm/ADT/SmallVector.h>
26#include <llvm/Support/ErrorHandling.h>
28namespace llzk {
30#define GEN_PASS_DEF_CALLGRAPHPRINTERPASS
31#define GEN_PASS_DEF_CALLGRAPHSCCSPRINTERPASS
34class CallGraphPrinterPass : public impl::CallGraphPrinterPassBase<CallGraphPrinterPass> {
35 llvm::raw_ostream &os;
37public:
38 explicit CallGraphPrinterPass(llvm::raw_ostream &ostream)
40
41protected:
42 void runOnOperation() override {
43 markAllAnalysesPreserved();
45 auto &cga = getAnalysis<CallGraphAnalysis>();
46 cga.getCallGraph().print(os);
47 }
48};
49
50std::unique_ptr<mlir::Pass> createCallGraphPrinterPass(llvm::raw_ostream &os = llvm::errs()) {
51 return std::make_unique<CallGraphPrinterPass>(os);
53
55 : public impl::CallGraphSCCsPrinterPassBase<CallGraphSCCsPrinterPass> {
56 llvm::raw_ostream &os;
58public:
59 explicit CallGraphSCCsPrinterPass(llvm::raw_ostream &ostream)
61
62protected:
63 void runOnOperation() override {
64 markAllAnalysesPreserved();
65
66 auto &CG = getAnalysis<CallGraphAnalysis>();
67 unsigned sccNum = 0;
68 os << "SCCs for the program in PostOrder:";
69 for (auto SCCI = llvm::scc_begin<const llzk::CallGraph *>(&CG.getCallGraph()); !SCCI.isAtEnd();
70 ++SCCI) {
71 const std::vector<const CallGraphNode *> &nextSCC = *SCCI;
72 os << "\nSCC #" << ++sccNum << ": ";
73 bool First = true;
74 for (const CallGraphNode *CGN : nextSCC) {
75 if (First) {
76 First = false;
77 } else {
78 os << ", ";
79 }
80 if (CGN->isExternal()) {
81 os << "external node";
82 } else {
83 os << CGN->getCalledFunction().getFullyQualifiedName();
84 }
85 }
86
87 if (nextSCC.size() == 1 && SCCI.hasCycle()) {
88 os << " (Has self-loop).";
89 }
90 }
91 os << '\n';
92 }
93};
94
95std::unique_ptr<mlir::Pass> createCallGraphSCCsPrinterPass(llvm::raw_ostream &os = llvm::errs()) {
96 return std::make_unique<CallGraphSCCsPrinterPass>(os);
97}
99} // namespace llzk
This is a simple port of the mlir::CallGraphNode with llzk::CallGraph as a friend class,...
Definition CallGraph.h:39
CallGraphPrinterPass(llvm::raw_ostream &ostream)
CallGraphSCCsPrinterPass(llvm::raw_ostream &ostream)
std::unique_ptr< mlir::Pass > createCallGraphSCCsPrinterPass(llvm::raw_ostream &os=llvm::errs())
std::unique_ptr< mlir::Pass > createCallGraphPrinterPass(llvm::raw_ostream &os=llvm::errs())