Halide  19.0.0
Halide compiler and libraries
Pipeline.h
Go to the documentation of this file.
1 #ifndef HALIDE_PIPELINE_H
2 #define HALIDE_PIPELINE_H
3 
4 /** \file
5  *
6  * Defines the front-end class representing an entire Halide imaging
7  * pipeline.
8  */
9 
10 #include <initializer_list>
11 #include <map>
12 #include <memory>
13 #include <vector>
14 
15 #include "IROperator.h"
16 #include "IntrusivePtr.h"
17 #include "JITModule.h"
18 #include "Module.h"
19 #include "Realization.h"
20 #include "Target.h"
21 #include "Tuple.h"
22 
23 namespace Halide {
24 
25 struct Argument;
26 class Callable;
27 class Func;
28 struct PipelineContents;
29 
30 /** Special the Autoscheduler to be used (if any), along with arbitrary
31  * additional arguments specific to the given Autoscheduler.
32  *
33  * The 'name' field specifies the type of Autoscheduler
34  * to be used (e.g. Adams2019, Mullapudi2016). If this is an empty string,
35  * no autoscheduling will be done; if not, it mustbe the name of a known Autoscheduler.
36  *
37  * At this time, well-known autoschedulers include:
38  * "Mullapudi2016" -- heuristics-based; the first working autoscheduler; currently built in to libHalide
39  * see http://graphics.cs.cmu.edu/projects/halidesched/
40  * "Adams2019" -- aka "the ML autoscheduler"; currently located in apps/autoscheduler
41  * see https://halide-lang.org/papers/autoscheduler2019.html
42  * "Li2018" -- aka "the gradient autoscheduler"; currently located in apps/gradient_autoscheduler.
43  * see https://people.csail.mit.edu/tzumao/gradient_halide
44  *
45  * The key/value pairs in 'extra' are defined on a per-autoscheduler basis.
46  * An autoscheduler can have any number of required or optional keys.
47  */
49  std::string name;
50  std::map<std::string, std::string> extra;
51 
52  AutoschedulerParams() = default;
53  /*not-explicit*/ AutoschedulerParams(const std::string &name)
54  : name(name) {
55  }
56  AutoschedulerParams(const std::string &name, const std::map<std::string, std::string> &extra)
57  : name(name), extra(extra) {
58  }
59 
60  std::string to_string() const;
61 };
62 
63 namespace Internal {
64 class IRMutator;
65 struct JITCache;
66 struct JITCallArgs;
67 } // namespace Internal
68 
69 /**
70  * Used to determine if the output printed to file should be as a normal string
71  * or as an HTML file which can be opened in a browerser and manipulated via JS and CSS.*/
74  HTML
75 };
76 
77 namespace {
78 // Helper for deleting custom lowering passes. In the header so that
79 // it goes in user code on windows, where you can have multiple heaps.
80 template<typename T>
81 void delete_lowering_pass(T *pass) {
82  delete pass;
83 }
84 } // namespace
85 
86 /** A custom lowering pass. See Pipeline::add_custom_lowering_pass. */
89  std::function<void()> deleter;
90 };
91 
92 struct JITExtern;
93 
95  Target target; // Target specified to the autoscheduler
96  AutoschedulerParams autoscheduler_params; // The autoscheduler used, along with its params
97  std::string schedule_source; // The C++ source code of the generated schedule
98  std::vector<uint8_t> featurization; // The featurization of the pipeline (if any)
99 };
100 
101 class Pipeline;
102 
103 using AutoSchedulerFn = std::function<void(const Pipeline &, const Target &, const AutoschedulerParams &, AutoSchedulerResults *outputs)>;
104 
105 /** A class representing a Halide pipeline. Constructed from the Func
106  * or Funcs that it outputs. */
107 class Pipeline {
108 public:
109  struct RealizationArg {
110  // Only one of the following may be non-null
111  Realization *r{nullptr};
112  halide_buffer_t *buf{nullptr};
113  std::unique_ptr<std::vector<Buffer<>>> buffer_list;
114 
116  : r(&r) {
117  }
119  : r(&r) {
120  }
122  : buf(buf) {
123  }
124  template<typename T, int Dims>
126  : buf(dst.raw_buffer()) {
127  }
128  template<typename T, int Dims>
130  : buf(dst.raw_buffer()) {
131  }
132  template<typename T, int Dims, typename... Args,
133  typename = typename std::enable_if<Internal::all_are_convertible<Buffer<>, Args...>::value>::type>
134  RealizationArg(Buffer<T, Dims> &a, Args &&...args)
135  : buffer_list(std::make_unique<std::vector<Buffer<>>>(std::initializer_list<Buffer<>>{a, std::forward<Args>(args)...})) {
136  }
137  RealizationArg(RealizationArg &&from) = default;
138 
139  size_t size() const {
140  if (r != nullptr) {
141  return r->size();
142  } else if (buffer_list) {
143  return buffer_list->size();
144  }
145  return 1;
146  }
147  };
148 
149 private:
151 
152  void prepare_jit_call_arguments(RealizationArg &output, const Target &target,
153  JITUserContext **user_context, bool is_bounds_inference, Internal::JITCallArgs &args_result);
154 
155  static std::vector<Internal::JITModule> make_externs_jit_module(const Target &target,
156  std::map<std::string, JITExtern> &externs_in_out);
157 
158  static std::map<std::string, AutoSchedulerFn> &get_autoscheduler_map();
159 
160  static AutoSchedulerFn find_autoscheduler(const std::string &autoscheduler_name);
161 
162  int call_jit_code(const Internal::JITCallArgs &args);
163 
164  // Get the value of contents->jit_target, but reality-check that the contents
165  // sensibly match the value. Return Target() if not jitted.
166  Target get_compiled_jit_target() const;
167 
168  static Internal::JITCache compile_jit_cache(const Module &module,
169  std::vector<Argument> args,
170  const std::vector<Internal::Function> &outputs,
171  const std::map<std::string, JITExtern> &jit_externs,
172  const Target &target_arg);
173 
174 public:
175  /** Make an undefined Pipeline object. */
177 
178  /** Make a pipeline that computes the given Func. Schedules the
179  * Func compute_root(). */
180  Pipeline(const Func &output);
181 
182  /** Make a pipeline that computes the givens Funcs as
183  * outputs. Schedules the Funcs compute_root(). */
184  Pipeline(const std::vector<Func> &outputs);
185 
186  /** Make a pipeline from deserialization. */
187  Pipeline(const std::vector<Func> &outputs, const std::vector<Internal::Stmt> &requirements);
188 
189  std::vector<Argument> infer_arguments(const Internal::Stmt &body);
190 
191  /** Get the Funcs this pipeline outputs. */
192  std::vector<Func> outputs() const;
193 
194  /** Get the requirements of this pipeline. */
195  std::vector<Internal::Stmt> requirements() const;
196 
197  /** Generate a schedule for the pipeline using the specified autoscheduler. */
199  const AutoschedulerParams &autoscheduler_params) const;
200 
201  /** Add a new the autoscheduler method with the given name. Does not affect the current default autoscheduler.
202  * It is an error to call this with the same name multiple times. */
203  static void add_autoscheduler(const std::string &autoscheduler_name, const AutoSchedulerFn &autoscheduler);
204 
205  /** Return handle to the index-th Func within the pipeline based on the
206  * topological order. */
207  Func get_func(size_t index);
208 
209  /** Compile and generate multiple target files with single call.
210  * Deduces target files based on filenames specified in
211  * output_files map.
212  */
213  void compile_to(const std::map<OutputFileType, std::string> &output_files,
214  const std::vector<Argument> &args,
215  const std::string &fn_name,
216  const Target &target);
217 
218  /** Statically compile a pipeline to llvm bitcode, with the given
219  * filename (which should probably end in .bc), type signature,
220  * and C function name. If you're compiling a pipeline with a
221  * single output Func, see also Func::compile_to_bitcode. */
222  void compile_to_bitcode(const std::string &filename,
223  const std::vector<Argument> &args,
224  const std::string &fn_name,
225  const Target &target = get_target_from_environment());
226 
227  /** Statically compile a pipeline to llvm assembly, with the given
228  * filename (which should probably end in .ll), type signature,
229  * and C function name. If you're compiling a pipeline with a
230  * single output Func, see also Func::compile_to_llvm_assembly. */
231  void compile_to_llvm_assembly(const std::string &filename,
232  const std::vector<Argument> &args,
233  const std::string &fn_name,
234  const Target &target = get_target_from_environment());
235 
236  /** Statically compile a pipeline with multiple output functions to an
237  * object file, with the given filename (which should probably end in
238  * .o or .obj), type signature, and C function name (which defaults to
239  * the same name as this halide function. You probably don't want to
240  * use this directly; call compile_to_static_library or compile_to_file instead. */
241  void compile_to_object(const std::string &filename,
242  const std::vector<Argument> &,
243  const std::string &fn_name,
244  const Target &target = get_target_from_environment());
245 
246  /** Emit a header file with the given filename for a pipeline. The
247  * header will define a function with the type signature given by
248  * the second argument, and a name given by the third. You don't
249  * actually have to have defined any of these functions yet to
250  * call this. You probably don't want to use this directly; call
251  * compile_to_static_library or compile_to_file instead. */
252  void compile_to_header(const std::string &filename,
253  const std::vector<Argument> &,
254  const std::string &fn_name,
255  const Target &target = get_target_from_environment());
256 
257  /** Statically compile a pipeline to text assembly equivalent to
258  * the object file generated by compile_to_object. This is useful
259  * for checking what Halide is producing without having to
260  * disassemble anything, or if you need to feed the assembly into
261  * some custom toolchain to produce an object file. */
262  void compile_to_assembly(const std::string &filename,
263  const std::vector<Argument> &args,
264  const std::string &fn_name,
265  const Target &target = get_target_from_environment());
266 
267  /** Statically compile a pipeline to C source code. This is useful
268  * for providing fallback code paths that will compile on many
269  * platforms. Vectorization will fail, and parallelization will
270  * produce serial code. */
271  void compile_to_c(const std::string &filename,
272  const std::vector<Argument> &,
273  const std::string &fn_name,
274  const Target &target = get_target_from_environment());
275 
276  /** Write out an internal representation of lowered code. Useful
277  * for analyzing and debugging scheduling. Can emit html or plain
278  * text. */
279  void compile_to_lowered_stmt(const std::string &filename,
280  const std::vector<Argument> &args,
281  StmtOutputFormat fmt = Text,
282  const Target &target = get_target_from_environment());
283 
284  /** Write out a conceptual representation of lowered code, before any parallel loop
285  * get factored out into separate functions, or GPU loops are offloaded to kernel code.r
286  * Useful for analyzing and debugging scheduling. Can emit html or plain text. */
287  void compile_to_conceptual_stmt(const std::string &filename,
288  const std::vector<Argument> &args,
289  StmtOutputFormat fmt = Text,
290  const Target &target = get_target_from_environment());
291 
292  /** Write out the loop nests specified by the schedule for this
293  * Pipeline's Funcs. Helpful for understanding what a schedule is
294  * doing. */
296 
297  /** Compile to object file and header pair, with the given
298  * arguments. */
299  void compile_to_file(const std::string &filename_prefix,
300  const std::vector<Argument> &args,
301  const std::string &fn_name,
302  const Target &target = get_target_from_environment());
303 
304  /** Compile to static-library file and header pair, with the given
305  * arguments. */
306  void compile_to_static_library(const std::string &filename_prefix,
307  const std::vector<Argument> &args,
308  const std::string &fn_name,
309  const Target &target = get_target_from_environment());
310 
311  /** Compile to static-library file and header pair once for each target;
312  * each resulting function will be considered (in order) via halide_can_use_target_features()
313  * at runtime, with the first appropriate match being selected for subsequent use.
314  * This is typically useful for specializations that may vary unpredictably by machine
315  * (e.g., SSE4.1/AVX/AVX2 on x86 desktop machines).
316  * All targets must have identical arch-os-bits.
317  */
318  void compile_to_multitarget_static_library(const std::string &filename_prefix,
319  const std::vector<Argument> &args,
320  const std::vector<Target> &targets);
321 
322  /** Like compile_to_multitarget_static_library(), except that the object files
323  * are all output as object files (rather than bundled into a static library).
324  *
325  * `suffixes` is an optional list of strings to use for as the suffix for each object
326  * file. If nonempty, it must be the same length as `targets`. (If empty, Target::to_string()
327  * will be used for each suffix.)
328  *
329  * Note that if `targets.size()` > 1, the wrapper code (to select the subtarget)
330  * will be generated with the filename `${filename_prefix}_wrapper.o`
331  *
332  * Note that if `targets.size()` > 1 and `no_runtime` is not specified, the runtime
333  * will be generated with the filename `${filename_prefix}_runtime.o`
334  */
335  void compile_to_multitarget_object_files(const std::string &filename_prefix,
336  const std::vector<Argument> &args,
337  const std::vector<Target> &targets,
338  const std::vector<std::string> &suffixes);
339 
340  /** Create an internal representation of lowered code as a self
341  * contained Module suitable for further compilation. */
342  Module compile_to_module(const std::vector<Argument> &args,
343  const std::string &fn_name,
344  const Target &target = get_target_from_environment(),
346 
347  /** Eagerly jit compile the function to machine code. This
348  * normally happens on the first call to realize. If you're
349  * running your halide pipeline inside time-sensitive code and
350  * wish to avoid including the time taken to compile a pipeline,
351  * then you can call this ahead of time. Default is to use the Target
352  * returned from Halide::get_jit_target_from_environment()
353  */
355 
356  /** Eagerly jit compile the function to machine code and return a callable
357  * struct that behaves like a function pointer. The calling convention
358  * will exactly match that of an AOT-compiled version of this Func
359  * with the same Argument list.
360  */
361  Callable compile_to_callable(const std::vector<Argument> &args,
362  const Target &target = get_jit_target_from_environment());
363 
364  /** Install a set of external C functions or Funcs to satisfy
365  * dependencies introduced by HalideExtern and define_extern
366  * mechanisms. These will be used by calls to realize,
367  * infer_bounds, and compile_jit. */
368  void set_jit_externs(const std::map<std::string, JITExtern> &externs);
369 
370  /** Return the map of previously installed externs. Is an empty
371  * map unless set otherwise. */
372  const std::map<std::string, JITExtern> &get_jit_externs();
373 
374  /** Get a struct containing the currently set custom functions
375  * used by JIT. This can be mutated. Changes will take effect the
376  * next time this Pipeline is realized. */
378 
379  /** Add a custom pass to be used during lowering. It is run after
380  * all other lowering passes. Can be used to verify properties of
381  * the lowered Stmt, instrument it with extra code, or otherwise
382  * modify it. The Func takes ownership of the pass, and will call
383  * delete on it when the Func goes out of scope. So don't pass a
384  * stack object, or share pass instances between multiple
385  * Funcs. */
386  template<typename T>
387  void add_custom_lowering_pass(T *pass) {
388  // Template instantiate a custom deleter for this type, then
389  // wrap in a lambda. The custom deleter lives in user code, so
390  // that deletion is on the same heap as construction (I hate Windows).
391  add_custom_lowering_pass(pass, [pass]() { delete_lowering_pass<T>(pass); });
392  }
393 
394  /** Add a custom pass to be used during lowering, with the
395  * function that will be called to delete it also passed in. Set
396  * it to nullptr if you wish to retain ownership of the object. */
397  void add_custom_lowering_pass(Internal::IRMutator *pass, std::function<void()> deleter);
398 
399  /** Remove all previously-set custom lowering passes */
401 
402  /** Get the custom lowering passes. */
403  const std::vector<CustomLoweringPass> &custom_lowering_passes();
404 
405  /** See Func::realize */
406  Realization realize(std::vector<int32_t> sizes = {}, const Target &target = Target());
407 
408  /** Same as above, but takes a custom user-provided context to be
409  * passed to runtime functions. A nullptr context is legal, and is
410  * equivalent to calling the variant of realize that does not take
411  * a context. */
413  std::vector<int32_t> sizes = {},
414  const Target &target = Target());
415 
416  /** Evaluate this Pipeline into an existing allocated buffer or
417  * buffers. If the buffer is also one of the arguments to the
418  * function, strange things may happen, as the pipeline isn't
419  * necessarily safe to run in-place. The realization should
420  * contain one Buffer per tuple component per output Func. For
421  * each individual output Func, all Buffers must have the same
422  * shape, but the shape can vary across the different output
423  * Funcs. This form of realize does *not* automatically copy data
424  * back from the GPU. */
425  void realize(RealizationArg output, const Target &target = Target());
426 
427  /** Same as above, but takes a custom user-provided context to be
428  * passed to runtime functions. A nullptr context is legal, and
429  * is equivalent to calling the variant of realize that does not
430  * take a context. */
431  void realize(JITUserContext *context,
432  RealizationArg output,
433  const Target &target = Target());
434 
435  /** For a given size of output, or a given set of output buffers,
436  * determine the bounds required of all unbound ImageParams
437  * referenced. Communicates the result by allocating new buffers
438  * of the appropriate size and binding them to the unbound
439  * ImageParams. */
440  // @{
441  void infer_input_bounds(const std::vector<int32_t> &sizes,
442  const Target &target = get_jit_target_from_environment());
444  const Target &target = get_jit_target_from_environment());
445  // @}
446 
447  /** Variants of infer_inputs_bounds that take a custom user context */
448  // @{
450  const std::vector<int32_t> &sizes,
451  const Target &target = get_jit_target_from_environment());
453  RealizationArg output,
454  const Target &target = get_jit_target_from_environment());
455  // @}
456 
457  /** Infer the arguments to the Pipeline, sorted into a canonical order:
458  * all buffers (sorted alphabetically by name), followed by all non-buffers
459  * (sorted alphabetically by name).
460  This lets you write things like:
461  \code
462  pipeline.compile_to_assembly("/dev/stdout", pipeline.infer_arguments());
463  \endcode
464  */
465  std::vector<Argument> infer_arguments();
466 
467  /** Check if this pipeline object is defined. That is, does it
468  * have any outputs? */
469  bool defined() const;
470 
471  /** Invalidate any internal cached state, e.g. because Funcs have
472  * been rescheduled. */
474 
475  /** Add a top-level precondition to the generated pipeline,
476  * expressed as a boolean Expr. The Expr may depend on parameters
477  * only, and may not call any Func or use a Var. If the condition
478  * is not true at runtime, the pipeline will call halide_error
479  * with the remaining arguments, and return
480  * halide_error_code_requirement_failed. Requirements are checked
481  * in the order added. */
482  // @{
483  void add_requirement(const Expr &condition, const std::vector<Expr> &error_args);
484 
485  template<typename... Args,
486  typename = typename std::enable_if<Internal::all_are_printable_args<Args...>::value>::type>
487  inline HALIDE_NO_USER_CODE_INLINE void add_requirement(const Expr &condition, Args &&...error_args) {
488  std::vector<Expr> collected_args;
489  Internal::collect_print_args(collected_args, std::forward<Args>(error_args)...);
490  add_requirement(condition, collected_args);
491  }
492  // @}
493 
494  /** Generate begin_pipeline and end_pipeline tracing calls for this pipeline. */
496 
497 private:
498  std::string generate_function_name() const;
499 };
500 
502 private:
503  Type ret_type_; // Only meaningful if is_void_return is false; must be default value otherwise
504  bool is_void_return_{false};
505  std::vector<Type> arg_types_;
506 
507 public:
508  ExternSignature() = default;
509 
510  ExternSignature(const Type &ret_type, bool is_void_return, const std::vector<Type> &arg_types)
511  : ret_type_(ret_type),
512  is_void_return_(is_void_return),
513  arg_types_(arg_types) {
514  internal_assert(!(is_void_return && ret_type != Type()));
515  }
516 
517  template<typename RT, typename... Args>
518  explicit ExternSignature(RT (*f)(Args... args))
519  : ret_type_(type_of<RT>()),
520  is_void_return_(std::is_void<RT>::value),
521  arg_types_({type_of<Args>()...}) {
522  }
523 
524  const Type &ret_type() const {
525  internal_assert(!is_void_return());
526  return ret_type_;
527  }
528 
529  bool is_void_return() const {
530  return is_void_return_;
531  }
532 
533  const std::vector<Type> &arg_types() const {
534  return arg_types_;
535  }
536 
537  friend std::ostream &operator<<(std::ostream &stream, const ExternSignature &sig) {
538  if (sig.is_void_return_) {
539  stream << "void";
540  } else {
541  stream << sig.ret_type_;
542  }
543  stream << " (*)(";
544  bool comma = false;
545  for (const auto &t : sig.arg_types_) {
546  if (comma) {
547  stream << ", ";
548  }
549  stream << t;
550  comma = true;
551  }
552  stream << ")";
553  return stream;
554  }
555 };
556 
558 private:
559  void *address_{nullptr};
560  ExternSignature signature_;
561 
562 public:
563  ExternCFunction() = default;
564 
566  : address_(address), signature_(signature) {
567  }
568 
569  template<typename RT, typename... Args>
570  ExternCFunction(RT (*f)(Args... args))
571  : ExternCFunction((void *)f, ExternSignature(f)) {
572  }
573 
574  void *address() const {
575  return address_;
576  }
577  const ExternSignature &signature() const {
578  return signature_;
579  }
580 };
581 
582 struct JITExtern {
583 private:
584  // Note that exactly one of pipeline_ and extern_c_function_
585  // can be set in a given JITExtern instance.
586  Pipeline pipeline_;
587  ExternCFunction extern_c_function_;
588 
589 public:
591  explicit JITExtern(const Func &func);
593 
594  template<typename RT, typename... Args>
595  explicit JITExtern(RT (*f)(Args... args))
596  : JITExtern(ExternCFunction(f)) {
597  }
598 
599  const Pipeline &pipeline() const {
600  return pipeline_;
601  }
603  return extern_c_function_;
604  }
605 };
606 
607 } // namespace Halide
608 
609 #endif
#define internal_assert(c)
Definition: Errors.h:19
Defines various operator overloads and utility functions that make it more pleasant to work with Hali...
Support classes for reference-counting via intrusive shared pointers.
Defines the struct representing lifetime and dependencies of a JIT compiled halide pipeline.
Defines Module, an IR container that fully describes a Halide program.
Defines Realization - a vector of Buffer for use in pipelines with multiple outputs.
Defines the structure that describes a Halide target.
Defines Tuple - the front-end handle on small arrays of expressions.
#define HALIDE_NO_USER_CODE_INLINE
Definition: Util.h:47
A Halide::Buffer is a named shared reference to a Halide::Runtime::Buffer.
Definition: Buffer.h:122
A halide function.
Definition: Func.h:700
A base class for passes over the IR which modify it (e.g.
Definition: IRMutator.h:26
A halide module.
Definition: Module.h:142
A class representing a Halide pipeline.
Definition: Pipeline.h:107
void compile_to_bitcode(const std::string &filename, const std::vector< Argument > &args, const std::string &fn_name, const Target &target=get_target_from_environment())
Statically compile a pipeline to llvm bitcode, with the given filename (which should probably end in ...
std::vector< Argument > infer_arguments()
Infer the arguments to the Pipeline, sorted into a canonical order: all buffers (sorted alphabeticall...
void compile_to_c(const std::string &filename, const std::vector< Argument > &, const std::string &fn_name, const Target &target=get_target_from_environment())
Statically compile a pipeline to C source code.
void compile_jit(const Target &target=get_jit_target_from_environment())
Eagerly jit compile the function to machine code.
void trace_pipeline()
Generate begin_pipeline and end_pipeline tracing calls for this pipeline.
const std::map< std::string, JITExtern > & get_jit_externs()
Return the map of previously installed externs.
void compile_to_conceptual_stmt(const std::string &filename, const std::vector< Argument > &args, StmtOutputFormat fmt=Text, const Target &target=get_target_from_environment())
Write out a conceptual representation of lowered code, before any parallel loop get factored out into...
Realization realize(std::vector< int32_t > sizes={}, const Target &target=Target())
See Func::realize.
void compile_to_file(const std::string &filename_prefix, const std::vector< Argument > &args, const std::string &fn_name, const Target &target=get_target_from_environment())
Compile to object file and header pair, with the given arguments.
Realization realize(JITUserContext *context, std::vector< int32_t > sizes={}, const Target &target=Target())
Same as above, but takes a custom user-provided context to be passed to runtime functions.
void realize(JITUserContext *context, RealizationArg output, const Target &target=Target())
Same as above, but takes a custom user-provided context to be passed to runtime functions.
Func get_func(size_t index)
Return handle to the index-th Func within the pipeline based on the topological order.
void compile_to_header(const std::string &filename, const std::vector< Argument > &, const std::string &fn_name, const Target &target=get_target_from_environment())
Emit a header file with the given filename for a pipeline.
void infer_input_bounds(JITUserContext *context, const std::vector< int32_t > &sizes, const Target &target=get_jit_target_from_environment())
Variants of infer_inputs_bounds that take a custom user context.
std::vector< Internal::Stmt > requirements() const
Get the requirements of this pipeline.
void compile_to_lowered_stmt(const std::string &filename, const std::vector< Argument > &args, StmtOutputFormat fmt=Text, const Target &target=get_target_from_environment())
Write out an internal representation of lowered code.
static void add_autoscheduler(const std::string &autoscheduler_name, const AutoSchedulerFn &autoscheduler)
Add a new the autoscheduler method with the given name.
void set_jit_externs(const std::map< std::string, JITExtern > &externs)
Install a set of external C functions or Funcs to satisfy dependencies introduced by HalideExtern and...
void add_custom_lowering_pass(Internal::IRMutator *pass, std::function< void()> deleter)
Add a custom pass to be used during lowering, with the function that will be called to delete it also...
Pipeline()
Make an undefined Pipeline object.
void compile_to_llvm_assembly(const std::string &filename, const std::vector< Argument > &args, const std::string &fn_name, const Target &target=get_target_from_environment())
Statically compile a pipeline to llvm assembly, with the given filename (which should probably end in...
Pipeline(const std::vector< Func > &outputs)
Make a pipeline that computes the givens Funcs as outputs.
Callable compile_to_callable(const std::vector< Argument > &args, const Target &target=get_jit_target_from_environment())
Eagerly jit compile the function to machine code and return a callable struct that behaves like a fun...
std::vector< Argument > infer_arguments(const Internal::Stmt &body)
void infer_input_bounds(const std::vector< int32_t > &sizes, const Target &target=get_jit_target_from_environment())
For a given size of output, or a given set of output buffers, determine the bounds required of all un...
Pipeline(const std::vector< Func > &outputs, const std::vector< Internal::Stmt > &requirements)
Make a pipeline from deserialization.
void compile_to_multitarget_object_files(const std::string &filename_prefix, const std::vector< Argument > &args, const std::vector< Target > &targets, const std::vector< std::string > &suffixes)
Like compile_to_multitarget_static_library(), except that the object files are all output as object f...
void compile_to_multitarget_static_library(const std::string &filename_prefix, const std::vector< Argument > &args, const std::vector< Target > &targets)
Compile to static-library file and header pair once for each target; each resulting function will be ...
AutoSchedulerResults apply_autoscheduler(const Target &target, const AutoschedulerParams &autoscheduler_params) const
Generate a schedule for the pipeline using the specified autoscheduler.
JITHandlers & jit_handlers()
Get a struct containing the currently set custom functions used by JIT.
void compile_to_object(const std::string &filename, const std::vector< Argument > &, const std::string &fn_name, const Target &target=get_target_from_environment())
Statically compile a pipeline with multiple output functions to an object file, with the given filena...
void invalidate_cache()
Invalidate any internal cached state, e.g.
const std::vector< CustomLoweringPass > & custom_lowering_passes()
Get the custom lowering passes.
void add_requirement(const Expr &condition, const std::vector< Expr > &error_args)
Add a top-level precondition to the generated pipeline, expressed as a boolean Expr.
void infer_input_bounds(RealizationArg output, const Target &target=get_jit_target_from_environment())
void print_loop_nest()
Write out the loop nests specified by the schedule for this Pipeline's Funcs.
Module compile_to_module(const std::vector< Argument > &args, const std::string &fn_name, const Target &target=get_target_from_environment(), LinkageType linkage_type=LinkageType::ExternalPlusMetadata)
Create an internal representation of lowered code as a self contained Module suitable for further com...
HALIDE_NO_USER_CODE_INLINE void add_requirement(const Expr &condition, Args &&...error_args)
Definition: Pipeline.h:487
void infer_input_bounds(JITUserContext *context, RealizationArg output, const Target &target=get_jit_target_from_environment())
std::vector< Func > outputs() const
Get the Funcs this pipeline outputs.
void compile_to_assembly(const std::string &filename, const std::vector< Argument > &args, const std::string &fn_name, const Target &target=get_target_from_environment())
Statically compile a pipeline to text assembly equivalent to the object file generated by compile_to_...
void realize(RealizationArg output, const Target &target=Target())
Evaluate this Pipeline into an existing allocated buffer or buffers.
void clear_custom_lowering_passes()
Remove all previously-set custom lowering passes.
bool defined() const
Check if this pipeline object is defined.
void compile_to_static_library(const std::string &filename_prefix, const std::vector< Argument > &args, const std::string &fn_name, const Target &target=get_target_from_environment())
Compile to static-library file and header pair, with the given arguments.
Pipeline(const Func &output)
Make a pipeline that computes the given Func.
void add_custom_lowering_pass(T *pass)
Add a custom pass to be used during lowering.
Definition: Pipeline.h:387
void compile_to(const std::map< OutputFileType, std::string > &output_files, const std::vector< Argument > &args, const std::string &fn_name, const Target &target)
Compile and generate multiple target files with single call.
A Realization is a vector of references to existing Buffer objects.
Definition: Realization.h:19
size_t size() const
The number of images in the Realization.
A templated Buffer class that wraps halide_buffer_t and adds functionality.
Definition: HalideBuffer.h:221
HALIDE_NO_USER_CODE_INLINE void collect_print_args(std::vector< Expr > &args)
Definition: IROperator.h:342
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:52
@ ExternalPlusMetadata
Visible externally. Argument metadata and an argv wrapper are also generated.
@ Internal
Not visible externally, similar to 'static' linkage in C.
Type type_of()
Construct the halide equivalent of a C type.
Definition: Type.h:572
std::function< void(const Pipeline &, const Target &, const AutoschedulerParams &, AutoSchedulerResults *outputs)> AutoSchedulerFn
Definition: Pipeline.h:103
Target get_jit_target_from_environment()
Return the target that Halide will use for jit-compilation.
Target get_target_from_environment()
Return the target that Halide will use.
StmtOutputFormat
Used to determine if the output printed to file should be as a normal string or as an HTML file which...
Definition: Pipeline.h:72
@ HTML
Definition: Pipeline.h:74
@ Text
Definition: Pipeline.h:73
std::string schedule_source
Definition: Pipeline.h:97
std::vector< uint8_t > featurization
Definition: Pipeline.h:98
AutoschedulerParams autoscheduler_params
Definition: Pipeline.h:96
Special the Autoscheduler to be used (if any), along with arbitrary additional arguments specific to ...
Definition: Pipeline.h:48
AutoschedulerParams(const std::string &name, const std::map< std::string, std::string > &extra)
Definition: Pipeline.h:56
AutoschedulerParams(const std::string &name)
Definition: Pipeline.h:53
std::string to_string() const
std::map< std::string, std::string > extra
Definition: Pipeline.h:50
A custom lowering pass.
Definition: Pipeline.h:87
Internal::IRMutator * pass
Definition: Pipeline.h:88
std::function< void()> deleter
Definition: Pipeline.h:89
A fragment of Halide syntax.
Definition: Expr.h:258
ExternCFunction(void *address, const ExternSignature &signature)
Definition: Pipeline.h:565
const ExternSignature & signature() const
Definition: Pipeline.h:577
void * address() const
Definition: Pipeline.h:574
ExternCFunction(RT(*f)(Args... args))
Definition: Pipeline.h:570
const std::vector< Type > & arg_types() const
Definition: Pipeline.h:533
friend std::ostream & operator<<(std::ostream &stream, const ExternSignature &sig)
Definition: Pipeline.h:537
ExternSignature(const Type &ret_type, bool is_void_return, const std::vector< Type > &arg_types)
Definition: Pipeline.h:510
const Type & ret_type() const
Definition: Pipeline.h:524
ExternSignature(RT(*f)(Args... args))
Definition: Pipeline.h:518
bool is_void_return() const
Definition: Pipeline.h:529
A reference-counted handle to a statement node.
Definition: Expr.h:427
JITExtern(const Func &func)
JITExtern(Pipeline pipeline)
const Pipeline & pipeline() const
Definition: Pipeline.h:599
JITExtern(RT(*f)(Args... args))
Definition: Pipeline.h:595
const ExternCFunction & extern_c_function() const
Definition: Pipeline.h:602
JITExtern(const ExternCFunction &extern_c_function)
A set of custom overrides of runtime functions.
Definition: JITModule.h:35
A context to be passed to Pipeline::realize.
Definition: JITModule.h:136
RealizationArg(Buffer< T, Dims > &a, Args &&...args)
Definition: Pipeline.h:134
RealizationArg(halide_buffer_t *buf)
Definition: Pipeline.h:121
HALIDE_NO_USER_CODE_INLINE RealizationArg(Buffer< T, Dims > &dst)
Definition: Pipeline.h:129
RealizationArg(RealizationArg &&from)=default
RealizationArg(Runtime::Buffer< T, Dims > &dst)
Definition: Pipeline.h:125
RealizationArg(Realization &&r)
Definition: Pipeline.h:118
RealizationArg(Realization &r)
Definition: Pipeline.h:115
std::unique_ptr< std::vector< Buffer<> > > buffer_list
Definition: Pipeline.h:113
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:283
The raw representation of an image passed around by generated Halide code.