Halide  12.0.1
Halide compiler and libraries
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. */
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_
Base classes for Halide expressions (Halide::Expr) and statements (Halide::Internal::Stmt)
Defines the structure that describes a Halide target.
Phase
The "Phase" of compilation, used for some calls.
virtual void record_matched_simplifier_rule(const std::string &rulename, Expr expr)=0
Record when a particular simplifier rule matches.
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.
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).
virtual void record_failed_to_prove(Expr failed_to_prove, Expr original_expr)=0
Record when can_prove() fails, but cannot find a counterexample.
virtual ~CompilerLogger()=default
virtual std::ostream & emit_to_stream(std::ostream &o)=0
Emit all the gathered data to the given stream.
virtual void record_compilation_time(Phase phase, double duration)=0
Record the compilation time (in seconds) for a given phase.
JSONCompilerLogger is a basic implementation of the CompilerLogger interface that saves logged data,...
void record_matched_simplifier_rule(const std::string &rulename, Expr expr) override
Record when a particular simplifier rule matches.
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).
std::map< std::string, std::vector< Expr > > matched_simplifier_rules
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.
std::map< std::string, std::vector< Expr > > non_monotonic_loop_vars
std::map< Phase, double > compilation_time
JSONCompilerLogger(const std::string &generator_name, const std::string &function_name, const std::string &autoscheduler_name, const Target &target, const std::string &generator_args, bool obfuscate_exprs)
std::vector< std::pair< Expr, Expr > > failed_to_prove_exprs
void record_failed_to_prove(Expr failed_to_prove, Expr original_expr) override
Record when can_prove() fails, but cannot find a counterexample.
std::ostream & emit_to_stream(std::ostream &o) override
Emit all the gathered data to the given stream.
void record_compilation_time(Phase phase, double duration) override
Record the compilation time (in seconds) for a given phase.
CompilerLogger * get_compiler_logger()
Return the currently active CompilerLogger object.
std::unique_ptr< CompilerLogger > set_compiler_logger(std::unique_ptr< CompilerLogger > compiler_logger)
Set the active CompilerLogger object, replacing any existing one.
This file defines the class FunctionDAG, which is our representation of a Halide pipeline,...
@ Internal
Not visible externally, similar to 'static' linkage in C.
unsigned __INT64_TYPE__ uint64_t
A fragment of Halide syntax.
Definition: Expr.h:256
A struct representing a target machine and os to generate code for.
Definition: Target.h:19