LLZK 2.1.1
An open-source IR for Zero Knowledge (ZK) circuits
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>
27
28namespace llzk {
29#define GEN_PASS_DEF_CALLGRAPHPRINTERPASS
30#define GEN_PASS_DEF_CALLGRAPHSCCSPRINTERPASS
32} // namespace llzk
33
34namespace {
35
36class PassImpl : public llzk::impl::CallGraphPrinterPassBase<PassImpl> {
37 using Base = CallGraphPrinterPassBase<PassImpl>;
38 using Base::Base;
40 void runOnOperation() override {
41 markAllAnalysesPreserved();
42
43 auto &cga = getAnalysis<llzk::CallGraphAnalysis>();
44 cga.getCallGraph().print(llzk::toStream(outputStream));
45 }
46};
48class SCCPassImpl : public llzk::impl::CallGraphSCCsPrinterPassBase<SCCPassImpl> {
50 using Base::Base;
52 void runOnOperation() override {
53 markAllAnalysesPreserved();
55 auto &os = llzk::toStream(outputStream);
56 auto &CG = getAnalysis<llzk::CallGraphAnalysis>();
57 unsigned sccNum = 0;
58 os << "SCCs for the program in PostOrder:";
59 for (auto SCCI = llvm::scc_begin<const llzk::CallGraph *>(&CG.getCallGraph()); !SCCI.isAtEnd();
60 ++SCCI) {
61 const std::vector<const llzk::CallGraphNode *> &nextSCC = *SCCI;
62 os << "\nSCC #" << ++sccNum << ": ";
63 bool First = true;
64 for (const llzk::CallGraphNode *CGN : nextSCC) {
65 if (First) {
66 First = false;
67 } else {
68 os << ", ";
69 }
70 if (CGN->isExternal()) {
71 os << "external node";
72 } else {
73 os << CGN->getCalledFunction().getFullyQualifiedName();
74 }
75 }
76
77 if (nextSCC.size() == 1 && SCCI.hasCycle()) {
78 os << " (Has self-loop).";
79 }
80 }
81 os << '\n';
82 }
83};
84
85} // namespace
This is a simple port of the mlir::CallGraphNode with llzk::CallGraph as a friend class,...
Definition CallGraph.h:39
::mlir::Pass::Option<::llzk::OutputStream > outputStream
llvm::raw_ostream & toStream(OutputStream val)