LLZK 2.0.0
An open-source IR for Zero Knowledge (ZK) circuits
Loading...
Searching...
No Matches
Debug.cpp
Go to the documentation of this file.
1//===-- Debug.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//===----------------------------------------------------------------------===//
9
10#include "llzk/Util/Debug.h"
11
13
14using namespace mlir;
15
16namespace llzk {
17namespace debug {
18
19void dumpSymbolTableWalk(Operation *symbolTableOp) {
20 std::string output; // buffer to avoid multi-threaded mess
21 llvm::raw_string_ostream oss(output);
22 oss << "Dumping symbol walk (self = [" << symbolTableOp << "]): \n";
23 auto walkFn = [&](Operation *op, bool /*allUsesVisible*/) {
24 oss << " found op [" << op << "] " << op->getName() << " named "
25 << op->getAttrOfType<StringAttr>(SymbolTable::getSymbolAttrName()) << '\n';
26 };
27 SymbolTable::walkSymbolTables(symbolTableOp, /*allSymUsesVisible=*/true, walkFn);
28 llvm::dbgs() << output;
29}
30
31void dumpSymbolTable(llvm::raw_ostream &stream, SymbolTable &symTab, unsigned indent) {
32 indent *= 2;
33 stream.indent(indent) << "Dumping SymbolTable [" << &symTab << "]: \n";
34 auto *rawSymbolTablePtr = reinterpret_cast<char *>(&symTab);
35 auto *privateMemberPtr =
36 reinterpret_cast<llvm::DenseMap<Attribute, Operation *> *>(rawSymbolTablePtr + 8);
37 indent += 2;
38 for (llvm::detail::DenseMapPair<Attribute, Operation *> &p : *privateMemberPtr) {
39 Operation *op = p.second;
40 stream.indent(indent) << p.first << " -> [" << op << "] " << op->getName() << '\n';
41 }
42}
43
44void dumpSymbolTable(SymbolTable &symTab) {
45 // Buffer to a string then print to avoid multi-threaded mess
46 llvm::dbgs() << buildStringViaCallback([&symTab](llvm::raw_ostream &stream) {
47 dumpSymbolTable(stream, symTab);
48 });
49}
50
51void dumpSymbolTables(llvm::raw_ostream &stream, SymbolTableCollection &tables) {
52 stream << "Dumping SymbolTableCollection [" << &tables << "]: \n";
53 auto *rawObjectPtr = reinterpret_cast<char *>(&tables);
54 auto *privateMemberPtr =
55 reinterpret_cast<llvm::DenseMap<Operation *, std::unique_ptr<SymbolTable>> *>(
56 rawObjectPtr + 0
57 );
58 for (llvm::detail::DenseMapPair<Operation *, std::unique_ptr<SymbolTable>> &p :
59 *privateMemberPtr) {
60 stream << " [" << p.first << "] " << p.first->getName() << " -> " << '\n';
61 dumpSymbolTable(stream, *p.second, 2);
62 }
63}
64
65void dumpSymbolTables(SymbolTableCollection &tables) {
66 // Buffer to a string then print to avoid multi-threaded mess
67 llvm::dbgs() << buildStringViaCallback([&tables](llvm::raw_ostream &stream) {
68 dumpSymbolTables(stream, tables);
69 });
70}
71
72void dumpToFile(Operation *op, llvm::StringRef filename) {
73 std::error_code err;
74 llvm::raw_fd_stream stream(filename, err);
75 if (!err) {
76 auto options = OpPrintingFlags().assumeVerified().useLocalScope();
77 op->print(stream, options);
78 stream << '\n';
79 }
80}
81
82} // namespace debug
83} // namespace llzk
void dumpSymbolTable(llvm::raw_ostream &stream, SymbolTable &symTab, unsigned indent)
Definition Debug.cpp:31
void dumpSymbolTables(llvm::raw_ostream &stream, SymbolTableCollection &tables)
Definition Debug.cpp:51
void dumpSymbolTableWalk(Operation *symbolTableOp)
Definition Debug.cpp:19
void dumpToFile(Operation *op, llvm::StringRef filename)
Definition Debug.cpp:72
std::string buildStringViaCallback(Func &&appendFn, Args &&...args)
Generate a string by calling the given appendFn with an llvm::raw_ostream & as the first argument fol...