LLZK 0.1.0
Veridise's ZK Language IR
Loading...
Searching...
No Matches
Debug.h
Go to the documentation of this file.
1//===-- Debug.h -------------------------------------------------*- 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#pragma once
11
12#include <mlir/IR/Attributes.h>
13#include <mlir/IR/Operation.h>
14#include <mlir/IR/SymbolTable.h>
15#include <mlir/IR/Value.h>
16#include <mlir/Interfaces/MemorySlotInterfaces.h>
17
18#include <llvm/ADT/DenseMap.h>
19#include <llvm/Support/raw_ostream.h>
20
21#include <optional>
22#include <string>
23
24namespace llzk::debug {
25
26namespace {
27
28// Define this concept instead of `std::ranges::range` because certain classes (like OperandRange)
29// do not work with `std::ranges::range`.
30template <typename T>
31concept Iterable = requires(T t) {
32 std::begin(t);
33 std::end(t);
34};
35
36struct Appender {
37 llvm::raw_string_ostream stream;
38 Appender(std::string &out) : stream(out) {}
39
40 void append(const mlir::MemorySlot &a);
41 void append(const mlir::DestructurableMemorySlot &a);
42 void append(const mlir::OpOperand &a);
43 void append(const mlir::NamedAttribute &a);
44 void append(const mlir::SymbolTable::SymbolUse &a);
45 template <typename T> void append(const std::optional<T> &a);
46 template <typename Any> void append(const Any &value);
47 template <typename A, typename B> void append(const std::pair<A, B> &a);
48 template <typename A, typename B> void append(const llvm::detail::DenseMapPair<A, B> &a);
49 template <Iterable InputIt> void append(const InputIt &collection);
50 template <typename InputIt> void appendList(InputIt begin, InputIt end);
51 template <typename Any> Appender &operator<<(const Any &v);
52};
53
54[[maybe_unused]]
55void Appender::append(const mlir::MemorySlot &a) {
56 stream << "ptr: " << a.ptr << "; type: " << a.elemType;
57}
58
59[[maybe_unused]]
60void Appender::append(const mlir::DestructurableMemorySlot &a) {
61 stream << "ptr: " << a.ptr << "; type: " << a.elemType << "; subelementTypes:\n";
62 for (auto &p : a.subelementTypes) {
63 stream.indent(2);
64 append(p);
65 stream << '\n';
66 }
67}
68
69[[maybe_unused]]
70void Appender::append(const mlir::OpOperand &a) {
71 stream << a.get();
72}
73
74[[maybe_unused]]
75void Appender::append(const mlir::NamedAttribute &a) {
76 stream << a.getName() << '=' << a.getValue();
77}
78
79[[maybe_unused]]
80void Appender::append(const mlir::SymbolTable::SymbolUse &a) {
81 stream << a.getUser()->getName();
82}
83
84template <typename T>
85[[maybe_unused]]
86inline void Appender::append(const std::optional<T> &a) {
87 if (a.has_value()) {
88 append(a.value());
89 } else {
90 stream << "NONE";
91 }
92}
93
94template <typename Any>
95[[maybe_unused]]
96void Appender::append(const Any &value) {
97 stream << value;
98}
99
100template <typename A, typename B>
101[[maybe_unused]]
102void Appender::append(const std::pair<A, B> &a) {
103 stream << '(';
104 append(a.first);
105 stream << ',';
106 append(a.second);
107 stream << ')';
108}
109
110template <typename A, typename B>
111[[maybe_unused]]
112void Appender::append(const llvm::detail::DenseMapPair<A, B> &a) {
113 stream << '(';
114 append(a.first);
115 stream << ',';
116 append(a.second);
117 stream << ')';
118}
119
120template <Iterable InputIt>
121[[maybe_unused]]
122inline void Appender::append(const InputIt &collection) {
123 appendList(std::begin(collection), std::end(collection));
124}
125
126template <typename InputIt>
127[[maybe_unused]]
128void Appender::appendList(InputIt begin, InputIt end) {
129 stream << '[';
130 llvm::interleave(begin, end, [this](const auto &n) { this->append(n); }, [this] {
131 this->stream << ", ";
132 });
133 stream << ']';
134}
135
136template <typename Any>
137[[maybe_unused]]
138Appender &Appender::operator<<(const Any &v) {
139 append(v);
140 return *this;
141}
142
143} // namespace
144
147template <typename InputIt>
148[[maybe_unused]]
149inline std::string toStringList(InputIt begin, InputIt end) {
150 std::string output;
151 Appender(output).appendList(begin, end);
152 return output;
153}
154
157template <typename InputIt>
158[[maybe_unused]]
159inline std::string toStringList(const InputIt &collection) {
160 return toStringList(collection.begin(), collection.end());
161}
162
163template <typename InputIt>
164[[maybe_unused]]
165inline std::string toStringList(const std::optional<InputIt> &optionalCollection) {
166 if (optionalCollection.has_value()) {
167 return toStringList(optionalCollection.value());
168 } else {
169 return "NONE";
170 }
171}
172
173template <typename T>
174[[maybe_unused]]
175inline std::string toStringOne(const T &value) {
176 std::string output;
177 Appender(output).append(value);
178 return output;
179}
180
181[[maybe_unused]]
182void dumpSymbolTableWalk(mlir::Operation *symbolTableOp);
183
184[[maybe_unused]]
185void dumpSymbolTable(llvm::raw_ostream &stream, mlir::SymbolTable &symTab, unsigned indent = 0);
186
187[[maybe_unused]]
188void dumpSymbolTable(mlir::SymbolTable &symTab);
189
190[[maybe_unused]]
191void dumpSymbolTables(llvm::raw_ostream &stream, mlir::SymbolTableCollection &tables);
192
193[[maybe_unused]]
194void dumpSymbolTables(mlir::SymbolTableCollection &tables);
195
196[[maybe_unused]]
197void dumpToFile(mlir::Operation *op, llvm::StringRef filename);
198
199} // namespace llzk::debug
std::string toStringOne(const T &value)
Definition Debug.h:175
std::string toStringList(InputIt begin, InputIt end)
Generate a comma-separated string representation by traversing elements from begin to end where the e...
Definition Debug.h:149
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
Interval operator<<(const Interval &lhs, const Interval &rhs)