CIRCT  19.0.0git
Utility.h
Go to the documentation of this file.
1 //===-- Utility.h - collection of utility functions and macros --*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 ///
9 /// This header provides a variety of utility functions and macros for use
10 /// throughout the tool.
11 ///
12 //===----------------------------------------------------------------------===//
13 
14 // NOLINTNEXTLINE
15 #ifndef TOOLS_CIRCT_LEC_UTILITY_H
16 #define TOOLS_CIRCT_LEC_UTILITY_H
17 
18 #include "mlir/IR/Value.h"
19 #include "mlir/Support/IndentedOstream.h"
20 #include "llvm/ADT/APInt.h"
21 #include "llvm/Support/Debug.h"
22 #include "llvm/Support/raw_ostream.h"
23 #include <z3++.h>
24 
25 namespace lec {
26 // Defining persistent output streams such that text will be printed in
27 // accordance with the globally set indentation level.
28 inline mlir::raw_indented_ostream &dbgs() {
29  static auto stream = mlir::raw_indented_ostream(llvm::dbgs());
30  return stream;
31 }
32 
33 inline mlir::raw_indented_ostream &errs() {
34  static auto stream = mlir::raw_indented_ostream(llvm::errs());
35  return stream;
36 }
37 
38 inline mlir::raw_indented_ostream &outs() {
39  static auto stream = mlir::raw_indented_ostream(llvm::outs());
40  return stream;
41 }
42 
43 /// RAII struct to indent the output streams.
44 struct Scope {
45  mlir::raw_indented_ostream::DelimitedScope indentDbgs = lec::dbgs().scope();
46  mlir::raw_indented_ostream::DelimitedScope indentErrs = lec::errs().scope();
47  mlir::raw_indented_ostream::DelimitedScope indentOuts = lec::outs().scope();
48 };
49 
50 /// Helper function to provide a common debug formatting for z3 expressions.
51 inline void printExpr(const z3::expr &expr) {
52  lec::dbgs() << "symbol: " << expr.to_string() << "\n";
53  lec::dbgs() << "sort: " << expr.get_sort().to_string() << "\n";
54  lec::dbgs() << "expression id: " << expr.id() << "\n";
55  lec::dbgs() << "expression hash: " << expr.hash() << "\n";
56 }
57 
58 /// Helper function to provide a common debug formatting for MLIR values.
59 inline void printValue(const mlir::Value &value) {
60  lec::dbgs() << "value: " << value << "\n";
61  lec::dbgs() << "type: " << value.getType() << "\n";
62  lec::dbgs() << "value hash: " << mlir::hash_value(value) << "\n";
63 }
64 
65 /// Helper function to provide a common debug formatting for MLIR APInt'egers.
66 inline void printAPInt(const mlir::APInt &value) {
67  lec::dbgs() << "APInt: " << value.getZExtValue() << "\n";
68 }
69 } // namespace lec
70 
71 #endif // TOOLS_CIRCT_LEC_UTILITY_H
This header provides a variety of utility functions and macros for use throughout the tool.
Definition: Utility.h:25
void printExpr(const z3::expr &expr)
Helper function to provide a common debug formatting for z3 expressions.
Definition: Utility.h:51
void printValue(const mlir::Value &value)
Helper function to provide a common debug formatting for MLIR values.
Definition: Utility.h:59
mlir::raw_indented_ostream & outs()
Definition: Utility.h:38
mlir::raw_indented_ostream & errs()
Definition: Utility.h:33
void printAPInt(const mlir::APInt &value)
Helper function to provide a common debug formatting for MLIR APInt'egers.
Definition: Utility.h:66
mlir::raw_indented_ostream & dbgs()
Definition: Utility.h:28
llvm::hash_code hash_value(const T &e)
RAII struct to indent the output streams.
Definition: Utility.h:44
mlir::raw_indented_ostream::DelimitedScope indentDbgs
Definition: Utility.h:45
mlir::raw_indented_ostream::DelimitedScope indentOuts
Definition: Utility.h:47
mlir::raw_indented_ostream::DelimitedScope indentErrs
Definition: Utility.h:46