Halide
CompilerLogger.h
Go to the documentation of this file.
1 #ifndef HALIDE_COMPILER_LOGGER_H_
2 #define HALIDE_COMPILER_LOGGER_H_
3 
4 /** \file
5  * Defines an interface used to gather and log compile-time information, stats, etc
6  * for use in evaluating internal Halide compilation rules and efficiency.
7  *
8  * The 'standard' implementation simply logs all gathered data to
9  * a local file (in JSON form), but the entire implementation can be
10  * replaced by custom definitions if you have unusual logging needs.
11  */
12 
13 #include <iostream>
14 #include <map>
15 #include <memory>
16 #include <set>
17 #include <string>
18 #include <utility>
19 
20 #include "Expr.h"
21 #include "Target.h"
22 
23 namespace Halide {
24 namespace Internal {
25 
27 public:
28  /** The "Phase" of compilation, used for some calls */
29  enum class Phase {
31  LLVM,
32  };
33 
34  CompilerLogger() = default;
35  virtual ~CompilerLogger() = default;
36 
37  /** Record when a particular simplifier rule matches.
38  */
39  virtual void record_matched_simplifier_rule(const std::string &rulename, Expr expr) = 0;
40 
41  /** Record when an expression is non-monotonic in a loop variable.
42  */
43  virtual void record_non_monotonic_loop_var(const std::string &loop_var, Expr expr) = 0;
44 
45  /** Record when can_prove() fails, but cannot find a counterexample.
46  */
47  virtual void record_failed_to_prove(Expr failed_to_prove, Expr original_expr) = 0;
48 
49  /** Record total size (in bytes) of final generated object code (e.g., file size of .o output).
50  */
51  virtual void record_object_code_size(uint64_t bytes) = 0;
52 
53  /** Record the compilation time (in seconds) for a given phase.
54  */
55  virtual void record_compilation_time(Phase phase, double duration) = 0;
56 
57  /**
58  * Emit all the gathered data to the given stream. This may be called multiple times.
59  */
60  virtual std::ostream &emit_to_stream(std::ostream &o) = 0;
61 };
62 
63 /** Set the active CompilerLogger object, replacing any existing one.
64  * It is legal to pass in a nullptr (which means "don't do any compiler logging").
65  * Returns the previous CompilerLogger (if any). */
66 std::unique_ptr<CompilerLogger> set_compiler_logger(std::unique_ptr<CompilerLogger> compiler_logger);
67 
68 /** Return the currently active CompilerLogger object. If set_compiler_logger()
69  * has never been called, a nullptr implementation will be returned.
70  * Do not save the pointer returned! It is intended to be used for immediate
71  * calls only. */
72 CompilerLogger *get_compiler_logger();
73 
74 /** JSONCompilerLogger is a basic implementation of the CompilerLogger interface
75  * that saves logged data, then logs it all in JSON format in emit_to_stream().
76  */
78 public:
79  JSONCompilerLogger() = default;
80 
82  const std::string &generator_name,
83  const std::string &function_name,
84  const std::string &autoscheduler_name,
85  const Target &target,
86  const std::string &generator_args,
87  bool obfuscate_exprs);
88 
89  void record_matched_simplifier_rule(const std::string &rulename, Expr expr) override;
90  void record_non_monotonic_loop_var(const std::string &loop_var, Expr expr) override;
91  void record_failed_to_prove(Expr failed_to_prove, Expr original_expr) override;
92  void record_object_code_size(uint64_t bytes) override;
93  void record_compilation_time(Phase phase, double duration) override;
94 
95  std::ostream &emit_to_stream(std::ostream &o) override;
96 
97 protected:
98  const std::string generator_name;
99  const std::string function_name;
100  const std::string autoscheduler_name;
101  const Target target;
102  const std::string generator_args;
103  const bool obfuscate_exprs{false};
104 
105  // Maps from string representing rewrite rule -> list of Exprs that matched that rule
106  std::map<std::string, std::vector<Expr>> matched_simplifier_rules;
107 
108  // Maps loop_var -> list of Exprs that were nonmonotonic for that loop_var
109  std::map<std::string, std::vector<Expr>> non_monotonic_loop_vars;
110 
111  // List of (unprovable simplified Expr, original version of that Expr passed to can_prove()).
112  std::vector<std::pair<Expr, Expr>> failed_to_prove_exprs;
113 
114  // Total code size generated, in bytes.
116 
117  // Map of the time take for each phase of compilation.
118  std::map<Phase, double> compilation_time;
119 
120  void obfuscate();
121  void emit();
122 };
123 
124 } // namespace Internal
125 } // namespace Halide
126 
127 #endif // HALIDE_COMPILER_LOGGER_H_
Halide::Internal::JSONCompilerLogger::JSONCompilerLogger
JSONCompilerLogger()=default
Halide::Internal::JSONCompilerLogger::record_matched_simplifier_rule
void record_matched_simplifier_rule(const std::string &rulename, Expr expr) override
Record when a particular simplifier rule matches.
Halide::Internal::set_compiler_logger
std::unique_ptr< CompilerLogger > set_compiler_logger(std::unique_ptr< CompilerLogger > compiler_logger)
Set the active CompilerLogger object, replacing any existing one.
Halide::Internal::CompilerLogger::Phase::LLVM
@ LLVM
Halide::Internal::get_compiler_logger
CompilerLogger * get_compiler_logger()
Return the currently active CompilerLogger object.
Target.h
Halide::Internal::CompilerLogger::emit_to_stream
virtual std::ostream & emit_to_stream(std::ostream &o)=0
Emit all the gathered data to the given stream.
Halide::Internal::JSONCompilerLogger::emit
void emit()
Halide::Internal::CompilerLogger::Phase::HalideLowering
@ HalideLowering
Halide::Internal::JSONCompilerLogger::function_name
const std::string function_name
Definition: CompilerLogger.h:99
uint64_t
unsigned __INT64_TYPE__ uint64_t
Definition: runtime_internal.h:19
Halide
This file defines the class FunctionDAG, which is our representation of a Halide pipeline,...
Definition: AddAtomicMutex.h:21
Halide::Internal::JSONCompilerLogger::record_failed_to_prove
void record_failed_to_prove(Expr failed_to_prove, Expr original_expr) override
Record when can_prove() fails, but cannot find a counterexample.
Halide::Internal::JSONCompilerLogger::object_code_size
uint64_t object_code_size
Definition: CompilerLogger.h:115
Halide::LinkageType::Internal
@ Internal
Not visible externally, similar to 'static' linkage in C.
Halide::Internal::CompilerLogger::CompilerLogger
CompilerLogger()=default
Halide::Internal::JSONCompilerLogger::failed_to_prove_exprs
std::vector< std::pair< Expr, Expr > > failed_to_prove_exprs
Definition: CompilerLogger.h:112
Halide::Internal::JSONCompilerLogger::compilation_time
std::map< Phase, double > compilation_time
Definition: CompilerLogger.h:118
Halide::Internal::JSONCompilerLogger::obfuscate
void obfuscate()
Halide::Internal::JSONCompilerLogger::non_monotonic_loop_vars
std::map< std::string, std::vector< Expr > > non_monotonic_loop_vars
Definition: CompilerLogger.h:109
Halide::Internal::CompilerLogger::record_non_monotonic_loop_var
virtual void record_non_monotonic_loop_var(const std::string &loop_var, Expr expr)=0
Record when an expression is non-monotonic in a loop variable.
Halide::Internal::JSONCompilerLogger::generator_args
const std::string generator_args
Definition: CompilerLogger.h:102
Expr.h
Halide::Internal::CompilerLogger
Definition: CompilerLogger.h:26
Halide::Internal::JSONCompilerLogger::record_object_code_size
void record_object_code_size(uint64_t bytes) override
Record total size (in bytes) of final generated object code (e.g., file size of .o output).
Halide::Internal::CompilerLogger::record_failed_to_prove
virtual void record_failed_to_prove(Expr failed_to_prove, Expr original_expr)=0
Record when can_prove() fails, but cannot find a counterexample.
Halide::Internal::JSONCompilerLogger::target
const Target target
Definition: CompilerLogger.h:101
Halide::Internal::JSONCompilerLogger
JSONCompilerLogger is a basic implementation of the CompilerLogger interface that saves logged data,...
Definition: CompilerLogger.h:77
Halide::Internal::CompilerLogger::Phase
Phase
The "Phase" of compilation, used for some calls.
Definition: CompilerLogger.h:29
Halide::Internal::JSONCompilerLogger::emit_to_stream
std::ostream & emit_to_stream(std::ostream &o) override
Emit all the gathered data to the given stream.
Halide::Internal::CompilerLogger::record_object_code_size
virtual void record_object_code_size(uint64_t bytes)=0
Record total size (in bytes) of final generated object code (e.g., file size of .o output).
Halide::Internal::CompilerLogger::record_compilation_time
virtual void record_compilation_time(Phase phase, double duration)=0
Record the compilation time (in seconds) for a given phase.
Halide::Internal::JSONCompilerLogger::record_compilation_time
void record_compilation_time(Phase phase, double duration) override
Halide::Internal::JSONCompilerLogger::autoscheduler_name
const std::string autoscheduler_name
Definition: CompilerLogger.h:100
Halide::Internal::JSONCompilerLogger::matched_simplifier_rules
std::map< std::string, std::vector< Expr > > matched_simplifier_rules
Definition: CompilerLogger.h:106
Halide::Expr
A fragment of Halide syntax.
Definition: Expr.h:256
Halide::Internal::JSONCompilerLogger::record_non_monotonic_loop_var
void record_non_monotonic_loop_var(const std::string &loop_var, Expr expr) override
Record when an expression is non-monotonic in a loop variable.
Halide::Internal::CompilerLogger::record_matched_simplifier_rule
virtual void record_matched_simplifier_rule(const std::string &rulename, Expr expr)=0
Record when a particular simplifier rule matches.
Halide::Internal::JSONCompilerLogger::obfuscate_exprs
const bool obfuscate_exprs
Definition: CompilerLogger.h:103
Halide::Internal::JSONCompilerLogger::generator_name
const std::string generator_name
Definition: CompilerLogger.h:98
Halide::Internal::CompilerLogger::~CompilerLogger
virtual ~CompilerLogger()=default
Halide::Target
A struct representing a target machine and os to generate code for.
Definition: Target.h:19