LLZK 0.1.0
Veridise's ZK Language IR
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"
12
13using namespace mlir;
14
15namespace llzk {
16namespace debug {
17
18void dumpSymbolTableWalk(Operation *symbolTableOp) {
19 std::string output; // buffer to avoid multi-threaded mess
20 llvm::raw_string_ostream oss(output);
21 oss << "Dumping symbol walk (self = [" << symbolTableOp << "]): \n";
22 auto walkFn = [&](Operation *op, bool allUsesVisible) {
23 oss << " found op [" << op << "] " << op->getName() << " named "
24 << op->getAttrOfType<StringAttr>(SymbolTable::getSymbolAttrName()) << '\n';
25 };
26 SymbolTable::walkSymbolTables(symbolTableOp, /*allSymUsesVisible=*/true, walkFn);
27 llvm::outs() << output;
28}
29
30void dumpSymbolTable(llvm::raw_ostream &stream, SymbolTable &symTab, unsigned indent) {
31 indent *= 2;
32 stream.indent(indent) << "Dumping SymbolTable [" << &symTab << "]: \n";
33 auto *rawSymbolTablePtr = reinterpret_cast<char *>(&symTab);
34 auto *privateFieldPtr =
35 reinterpret_cast<llvm::DenseMap<Attribute, Operation *> *>(rawSymbolTablePtr + 8);
36 indent += 2;
37 for (llvm::detail::DenseMapPair<Attribute, Operation *> &p : *privateFieldPtr) {
38 Operation *op = p.second;
39 stream.indent(indent) << p.first << " -> [" << op << "] " << op->getName() << '\n';
40 }
41}
42
43void dumpSymbolTable(SymbolTable &symTab) {
44 // Buffer to a string then print to avoid multi-threaded mess
45 llvm::outs() << buildStringViaCallback([&symTab](llvm::raw_ostream &stream) {
46 dumpSymbolTable(stream, symTab);
47 });
48}
49
50void dumpSymbolTables(llvm::raw_ostream &stream, SymbolTableCollection &tables) {
51 stream << "Dumping SymbolTableCollection [" << &tables << "]: \n";
52 auto *rawObjectPtr = reinterpret_cast<char *>(&tables);
53 auto *privateFieldPtr =
54 reinterpret_cast<llvm::DenseMap<Operation *, std::unique_ptr<SymbolTable>> *>(
55 rawObjectPtr + 0
56 );
57 for (llvm::detail::DenseMapPair<Operation *, std::unique_ptr<SymbolTable>> &p :
58 *privateFieldPtr) {
59 stream << " [" << p.first << "] " << p.first->getName() << " -> " << '\n';
60 dumpSymbolTable(stream, *p.second.get(), 2);
61 }
62}
63
64void dumpSymbolTables(SymbolTableCollection &tables) {
65 // Buffer to a string then print to avoid multi-threaded mess
66 llvm::outs() << buildStringViaCallback([&tables](llvm::raw_ostream &stream) {
67 dumpSymbolTables(stream, tables);
68 });
69}
70
71void dumpToFile(Operation *op, llvm::StringRef filename) {
72 std::error_code err;
73 llvm::raw_fd_stream stream(filename, err);
74 if (!err) {
75 auto options = OpPrintingFlags().assumeVerified().useLocalScope();
76 op->print(stream, options);
77 stream << '\n';
78 }
79}
80
81} // namespace debug
82} // namespace llzk
void dumpSymbolTable(llvm::raw_ostream &stream, SymbolTable &symTab, unsigned indent)
Definition Debug.cpp:30
void dumpSymbolTables(llvm::raw_ostream &stream, SymbolTableCollection &tables)
Definition Debug.cpp:50
void dumpSymbolTableWalk(Operation *symbolTableOp)
Definition Debug.cpp:18
void dumpToFile(Operation *op, llvm::StringRef filename)
Definition Debug.cpp:71
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...