Halide  12.0.1
Halide compiler and libraries
Module.h
Go to the documentation of this file.
1 #ifndef HALIDE_MODULE_H
2 #define HALIDE_MODULE_H
3 
4 /** \file
5  *
6  * Defines Module, an IR container that fully describes a Halide program.
7  */
8 
9 #include <functional>
10 #include <map>
11 #include <memory>
12 #include <string>
13 
14 #include "Argument.h"
15 #include "Expr.h"
16 #include "ExternalCode.h"
17 #include "Function.h" // for NameMangling
18 #include "ModulusRemainder.h"
19 
20 namespace Halide {
21 
22 template<typename T>
23 class Buffer;
24 struct Target;
25 
26 /** Enums specifying various kinds of outputs that can be produced from a Halide Pipeline. */
27 enum class Output {
28  assembly,
29  bitcode,
30  c_header,
31  c_source,
33  cpp_stub,
36  object,
40  schedule,
42  stmt,
43  stmt_html,
44 };
45 
46 /** Type of linkage a function in a lowered Halide module can have.
47  Also controls whether auxiliary functions and metadata are generated. */
48 enum class LinkageType {
49  External, ///< Visible externally.
50  ExternalPlusMetadata, ///< Visible externally. Argument metadata and an argv wrapper are also generated.
51  Internal, ///< Not visible externally, similar to 'static' linkage in C.
52 };
53 
54 namespace Internal {
55 
56 struct OutputInfo {
57  std::string name, extension;
58 
59  // `is_multi` indicates how these outputs are generated
60  // when using the compile_to_multitarget_xxx() APIs (or via the
61  // Generator command-line mode):
62  //
63  // - If `is_multi` is true, then a separate file of this Output type is
64  // generated for each target in the multitarget (e.g. object files,
65  // assembly files, etc). Each of the files will have a suffix appended
66  // that is based on the specific subtarget.
67  //
68  // - If `is_multi` is false, then only one file of this Output type
69  // regardless of how many targets are in the multitarget. No additional
70  // suffix will be appended to the filename.
71  //
72  bool is_multi{false};
73 };
74 std::map<Output, const OutputInfo> get_output_info(const Target &target);
75 
76 /** Definition of an argument to a LoweredFunc. This is similar to
77  * Argument, except it enables passing extra information useful to
78  * some targets to LoweredFunc. */
79 struct LoweredArgument : public Argument {
80  /** For scalar arguments, the modulus and remainder of this
81  * argument. */
83 
84  LoweredArgument() = default;
85  explicit LoweredArgument(const Argument &arg)
86  : Argument(arg) {
87  }
88  LoweredArgument(const std::string &_name, Kind _kind, const Type &_type, uint8_t _dimensions, const ArgumentEstimates &argument_estimates)
89  : Argument(_name, _kind, _type, _dimensions, argument_estimates) {
90  }
91 };
92 
93 /** Definition of a lowered function. This object provides a concrete
94  * mapping between parameters used in the function body and their
95  * declarations in the argument list. */
96 struct LoweredFunc {
97  std::string name;
98 
99  /** Arguments referred to in the body of this function. */
100  std::vector<LoweredArgument> args;
101 
102  /** Body of this function. */
104 
105  /** The linkage of this function. */
107 
108  /** The name-mangling choice for the function. Defaults to using
109  * the Target. */
111 
112  LoweredFunc(const std::string &name,
113  const std::vector<LoweredArgument> &args,
114  Stmt body,
117  LoweredFunc(const std::string &name,
118  const std::vector<Argument> &args,
119  Stmt body,
122 };
123 
124 } // namespace Internal
125 
126 namespace Internal {
127 struct ModuleContents;
128 class CompilerLogger;
129 } // namespace Internal
130 
131 struct AutoSchedulerResults;
132 
133 /** A halide module. This represents IR containing lowered function
134  * definitions and buffers. */
135 class Module {
137 
138 public:
139  Module(const std::string &name, const Target &target);
140 
141  /** Get the target this module has been lowered for. */
142  const Target &target() const;
143 
144  /** The name of this module. This is used as the default filename
145  * for output operations. */
146  const std::string &name() const;
147 
148  /** If this Module had an auto-generated schedule, return a read-only pointer
149  * to the AutoSchedulerResults. If not, return nullptr. */
151 
152  /** Return whether this module uses strict floating-point anywhere. */
153  bool any_strict_float() const;
154 
155  /** The declarations contained in this module. */
156  // @{
157  const std::vector<Buffer<void>> &buffers() const;
158  const std::vector<Internal::LoweredFunc> &functions() const;
159  std::vector<Internal::LoweredFunc> &functions();
160  const std::vector<Module> &submodules() const;
161  const std::vector<ExternalCode> &external_code() const;
162  // @}
163 
164  /** Return the function with the given name. If no such function
165  * exists in this module, assert. */
167 
168  /** Add a declaration to this module. */
169  // @{
170  void append(const Buffer<void> &buffer);
171  void append(const Internal::LoweredFunc &function);
172  void append(const Module &module);
174  // @}
175 
176  /** Compile a halide Module to variety of outputs, depending on
177  * the fields set in output_files. */
178  void compile(const std::map<Output, std::string> &output_files) const;
179 
180  /** Compile a halide Module to in-memory object code. Currently
181  * only supports LLVM based compilation, but should be extended to
182  * handle source code backends. */
184 
185  /** Return a new module with all submodules compiled to buffers on
186  * on the result Module. */
188 
189  /** When generating metadata from this module, remap any occurrences
190  * of 'from' into 'to'. */
191  void remap_metadata_name(const std::string &from, const std::string &to) const;
192 
193  /** Retrieve the metadata name map. */
194  std::map<std::string, std::string> get_metadata_name_map() const;
195 
196  /** Set the AutoSchedulerResults for the Module. It is an error to call this
197  * multiple times for a given Module. */
199 
200  /** Set whether this module uses strict floating-point directives anywhere. */
202 };
203 
204 /** Link a set of modules together into one module. */
205 Module link_modules(const std::string &name, const std::vector<Module> &modules);
206 
207 /** Create an object file containing the Halide runtime for a given target. For
208  * use with Target::NoRuntime. Standalone runtimes are only compatible with
209  * pipelines compiled by the same build of Halide used to call this function. */
210 void compile_standalone_runtime(const std::string &object_filename, const Target &t);
211 
212 /** Create an object and/or static library file containing the Halide runtime
213  * for a given target. For use with Target::NoRuntime. Standalone runtimes are
214  * only compatible with pipelines compiled by the same build of Halide used to
215  * call this function. Return a map with just the actual outputs filled in
216  * (typically, Output::object and/or Output::static_library).
217  */
218 std::map<Output, std::string> compile_standalone_runtime(const std::map<Output, std::string> &output_files, const Target &t);
219 
220 using ModuleFactory = std::function<Module(const std::string &fn_name, const Target &target)>;
221 using CompilerLoggerFactory = std::function<std::unique_ptr<Internal::CompilerLogger>(const std::string &fn_name, const Target &target)>;
222 
223 void compile_multitarget(const std::string &fn_name,
224  const std::map<Output, std::string> &output_files,
225  const std::vector<Target> &targets,
226  const std::vector<std::string> &suffixes,
227  const ModuleFactory &module_factory,
228  const CompilerLoggerFactory &compiler_logger_factory = nullptr);
229 
230 } // namespace Halide
231 
232 #endif
Defines a type used for expressing the type signature of a generated halide pipeline.
Base classes for Halide expressions (Halide::Expr) and statements (Halide::Internal::Stmt)
Defines the internal representation of a halide function and related classes.
Routines for statically determining what expressions are divisible by.
A halide module.
Definition: Module.h:135
std::map< std::string, std::string > get_metadata_name_map() const
Retrieve the metadata name map.
void append(const Buffer< void > &buffer)
Add a declaration to this module.
const AutoSchedulerResults * get_auto_scheduler_results() const
If this Module had an auto-generated schedule, return a read-only pointer to the AutoSchedulerResults...
const std::vector< Internal::LoweredFunc > & functions() const
Module(const std::string &name, const Target &target)
const std::vector< ExternalCode > & external_code() const
const std::vector< Buffer< void > > & buffers() const
The declarations contained in this module.
std::vector< Internal::LoweredFunc > & functions()
Buffer< uint8_t > compile_to_buffer() const
Compile a halide Module to in-memory object code.
void set_auto_scheduler_results(const AutoSchedulerResults &results)
Set the AutoSchedulerResults for the Module.
Module resolve_submodules() const
Return a new module with all submodules compiled to buffers on on the result Module.
void remap_metadata_name(const std::string &from, const std::string &to) const
When generating metadata from this module, remap any occurrences of 'from' into 'to'.
const std::string & name() const
The name of this module.
const std::vector< Module > & submodules() const
const Target & target() const
Get the target this module has been lowered for.
void compile(const std::map< Output, std::string > &output_files) const
Compile a halide Module to variety of outputs, depending on the fields set in output_files.
Internal::LoweredFunc get_function_by_name(const std::string &name) const
Return the function with the given name.
void append(const ExternalCode &external_code)
void append(const Module &module)
void set_any_strict_float(bool any_strict_float)
Set whether this module uses strict floating-point directives anywhere.
bool any_strict_float() const
Return whether this module uses strict floating-point anywhere.
void append(const Internal::LoweredFunc &function)
std::map< Output, const OutputInfo > get_output_info(const Target &target)
This file defines the class FunctionDAG, which is our representation of a Halide pipeline,...
LinkageType
Type of linkage a function in a lowered Halide module can have.
Definition: Module.h:48
@ ExternalPlusMetadata
Visible externally. Argument metadata and an argv wrapper are also generated.
@ Internal
Not visible externally, similar to 'static' linkage in C.
@ External
Visible externally.
std::function< std::unique_ptr< Internal::CompilerLogger >(const std::string &fn_name, const Target &target)> CompilerLoggerFactory
Definition: Module.h:221
void compile_multitarget(const std::string &fn_name, const std::map< Output, std::string > &output_files, const std::vector< Target > &targets, const std::vector< std::string > &suffixes, const ModuleFactory &module_factory, const CompilerLoggerFactory &compiler_logger_factory=nullptr)
NameMangling
An enum to specify calling convention for extern stages.
Definition: Function.h:24
@ Default
Match whatever is specified in the Target.
void compile_standalone_runtime(const std::string &object_filename, const Target &t)
Create an object file containing the Halide runtime for a given target.
Module link_modules(const std::string &name, const std::vector< Module > &modules)
Link a set of modules together into one module.
Output
Enums specifying various kinds of outputs that can be produced from a Halide Pipeline.
Definition: Module.h:27
std::function< Module(const std::string &fn_name, const Target &target)> ModuleFactory
Definition: Module.h:220
unsigned __INT8_TYPE__ uint8_t
A struct representing an argument to a halide-generated function.
Definition: Argument.h:37
Kind
An argument is either a primitive type (for parameters), or a buffer pointer.
Definition: Argument.h:52
ArgumentEstimates argument_estimates
Definition: Argument.h:72
Definition of an argument to a LoweredFunc.
Definition: Module.h:79
ModulusRemainder alignment
For scalar arguments, the modulus and remainder of this argument.
Definition: Module.h:82
LoweredArgument(const Argument &arg)
Definition: Module.h:85
LoweredArgument(const std::string &_name, Kind _kind, const Type &_type, uint8_t _dimensions, const ArgumentEstimates &argument_estimates)
Definition: Module.h:88
Definition of a lowered function.
Definition: Module.h:96
LoweredFunc(const std::string &name, const std::vector< LoweredArgument > &args, Stmt body, LinkageType linkage, NameMangling mangling=NameMangling::Default)
std::vector< LoweredArgument > args
Arguments referred to in the body of this function.
Definition: Module.h:100
LoweredFunc(const std::string &name, const std::vector< Argument > &args, Stmt body, LinkageType linkage, NameMangling mangling=NameMangling::Default)
NameMangling name_mangling
The name-mangling choice for the function.
Definition: Module.h:110
LinkageType linkage
The linkage of this function.
Definition: Module.h:106
Stmt body
Body of this function.
Definition: Module.h:103
The result of modulus_remainder analysis.
A reference-counted handle to a statement node.
Definition: Expr.h:413
A struct representing a target machine and os to generate code for.
Definition: Target.h:19
Types in the halide type system.
Definition: Type.h:269