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 <string>
17 #include <utility>
18 
19 #include "Expr.h"
20 #include "Target.h"
21 
22 namespace Halide {
23 namespace Internal {
24 
26 public:
27  /** The "Phase" of compilation, used for some calls */
28  enum class Phase {
30  LLVM,
31  };
32 
33  CompilerLogger() = default;
34  virtual ~CompilerLogger() = default;
35 
36  /** Record when a particular simplifier rule matches.
37  */
38  virtual void record_matched_simplifier_rule(const std::string &rulename, Expr expr) = 0;
39 
40  /** Record when an expression is non-monotonic in a loop variable.
41  */
42  virtual void record_non_monotonic_loop_var(const std::string &loop_var, Expr expr) = 0;
43 
44  /** Record when can_prove() fails, but cannot find a counterexample.
45  */
46  virtual void record_failed_to_prove(Expr failed_to_prove, Expr original_expr) = 0;
47 
48  /** Record total size (in bytes) of final generated object code (e.g., file size of .o output).
49  */
50  virtual void record_object_code_size(uint64_t bytes) = 0;
51 
52  /** Record the compilation time (in seconds) for a given phase.
53  */
54  virtual void record_compilation_time(Phase phase, double duration) = 0;
55 
56  /**
57  * Emit all the gathered data to the given stream. This may be called multiple times.
58  */
59  virtual std::ostream &emit_to_stream(std::ostream &o) = 0;
60 };
61 
62 /** Set the active CompilerLogger object, replacing any existing one.
63  * It is legal to pass in a nullptr (which means "don't do any compiler logging").
64  * Returns the previous CompilerLogger (if any). */
65 std::unique_ptr<CompilerLogger> set_compiler_logger(std::unique_ptr<CompilerLogger> compiler_logger);
66 
67 /** Return the currently active CompilerLogger object. If set_compiler_logger()
68  * has never been called, a nullptr implementation will be returned.
69  * Do not save the pointer returned! It is intended to be used for immediate
70  * calls only. */
71 CompilerLogger *get_compiler_logger();
72 
73 /** JSONCompilerLogger is a basic implementation of the CompilerLogger interface
74  * that saves logged data, then logs it all in JSON format in emit_to_stream().
75  */
77 public:
78  JSONCompilerLogger() = default;
79 
81  const std::string &generator_name,
82  const std::string &function_name,
83  const std::string &autoscheduler_name,
84  const Target &target,
85  const std::string &generator_args,
86  bool obfuscate_exprs);
87 
88  void record_matched_simplifier_rule(const std::string &rulename, Expr expr) override;
89  void record_non_monotonic_loop_var(const std::string &loop_var, Expr expr) override;
90  void record_failed_to_prove(Expr failed_to_prove, Expr original_expr) override;
91  void record_object_code_size(uint64_t bytes) override;
92  void record_compilation_time(Phase phase, double duration) override;
93 
94  std::ostream &emit_to_stream(std::ostream &o) override;
95 
96 protected:
97  const std::string generator_name;
98  const std::string function_name;
99  const std::string autoscheduler_name;
100  const Target target = Target();
101  const std::string generator_args;
102  const bool obfuscate_exprs{false};
103 
104  // Maps from string representing rewrite rule -> list of Exprs that matched that rule
105  std::map<std::string, std::vector<Expr>> matched_simplifier_rules;
106 
107  // Maps loop_var -> list of Exprs that were nonmonotonic for that loop_var
108  std::map<std::string, std::vector<Expr>> non_monotonic_loop_vars;
109 
110  // List of (unprovable simplified Expr, original version of that Expr passed to can_prove()).
111  std::vector<std::pair<Expr, Expr>> failed_to_prove_exprs;
112 
113  // Total code size generated, in bytes.
115 
116  // Map of the time take for each phase of compilation.
117  std::map<Phase, double> compilation_time;
118 
119  void obfuscate();
120  void emit();
121 };
122 
123 } // namespace Internal
124 } // namespace Halide
125 
126 #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:98
uint64_t
unsigned __INT64_TYPE__ uint64_t
Definition: runtime_internal.h:23
Halide
This file defines the class FunctionDAG, which is our representation of a Halide pipeline,...
Definition: AbstractGenerator.h:19
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:114
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:111
Halide::Internal::JSONCompilerLogger::compilation_time
std::map< Phase, double > compilation_time
Definition: CompilerLogger.h:117
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:108
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:101
Expr.h
Halide::Internal::CompilerLogger
Definition: CompilerLogger.h:25
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:100
Halide::Internal::JSONCompilerLogger
JSONCompilerLogger is a basic implementation of the CompilerLogger interface that saves logged data,...
Definition: CompilerLogger.h:76
Halide::Internal::CompilerLogger::Phase
Phase
The "Phase" of compilation, used for some calls.
Definition: CompilerLogger.h:28
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:99
Halide::Internal::JSONCompilerLogger::matched_simplifier_rules
std::map< std::string, std::vector< Expr > > matched_simplifier_rules
Definition: CompilerLogger.h:105
Halide::Expr
A fragment of Halide syntax.
Definition: Expr.h:257
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:102
Halide::Internal::JSONCompilerLogger::generator_name
const std::string generator_name
Definition: CompilerLogger.h:97
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