Halide  12.0.1
Halide compiler and libraries
Parameter.h
Go to the documentation of this file.
1 #ifndef HALIDE_PARAMETER_H
2 #define HALIDE_PARAMETER_H
3 
4 /** \file
5  * Defines the internal representation of parameters to halide piplines
6  */
7 #include <string>
8 
9 #include "IntrusivePtr.h"
10 #include "Type.h"
11 #include "Util.h" // for HALIDE_NO_USER_CODE_INLINE
12 #include "runtime/HalideRuntime.h" // for HALIDE_ALWAYS_INLINE
13 
14 namespace Halide {
15 
16 struct ArgumentEstimates;
17 template<typename T>
18 class Buffer;
19 struct Expr;
20 struct Type;
21 enum class MemoryType;
22 
23 namespace Internal {
24 
25 struct ParameterContents;
26 
27 /** A reference-counted handle to a parameter to a halide
28  * pipeline. May be a scalar parameter or a buffer */
29 class Parameter {
30  void check_defined() const;
31  void check_is_buffer() const;
32  void check_is_scalar() const;
33  void check_dim_ok(int dim) const;
34  void check_type(const Type &t) const;
35 
36 protected:
38 
39 public:
40  /** Construct a new undefined handle */
41  Parameter() = default;
42 
43  /** Construct a new parameter of the given type. If the second
44  * argument is true, this is a buffer parameter of the given
45  * dimensionality, otherwise, it is a scalar parameter (and the
46  * dimensionality should be zero). The parameter will be given a
47  * unique auto-generated name. */
48  Parameter(const Type &t, bool is_buffer, int dimensions);
49 
50  /** Construct a new parameter of the given type with name given by
51  * the third argument. If the second argument is true, this is a
52  * buffer parameter, otherwise, it is a scalar parameter. The
53  * third argument gives the dimensionality of the buffer
54  * parameter. It should be zero for scalar parameters. If the
55  * fifth argument is true, the the name being passed in was
56  * explicitly specified (as opposed to autogenerated). */
57  Parameter(const Type &t, bool is_buffer, int dimensions, const std::string &name);
58 
59  Parameter(const Parameter &) = default;
60  Parameter &operator=(const Parameter &) = default;
61  Parameter(Parameter &&) = default;
62  Parameter &operator=(Parameter &&) = default;
63 
64  /** Get the type of this parameter */
65  Type type() const;
66 
67  /** Get the dimensionality of this parameter. Zero for scalars. */
68  int dimensions() const;
69 
70  /** Get the name of this parameter */
71  const std::string &name() const;
72 
73  /** Does this parameter refer to a buffer/image? */
74  bool is_buffer() const;
75 
76  /** If the parameter is a scalar parameter, get its currently
77  * bound value. Only relevant when jitting */
78  template<typename T>
80  check_type(type_of<T>());
81  return *((const T *)(scalar_address()));
82  }
83 
84  /** This returns the current value of scalar<type()>()
85  * as an Expr. */
86  Expr scalar_expr() const;
87 
88  /** If the parameter is a scalar parameter, set its current
89  * value. Only relevant when jitting */
90  template<typename T>
92  check_type(type_of<T>());
93  *((T *)(scalar_address())) = val;
94  }
95 
96  /** If the parameter is a scalar parameter, set its current
97  * value. Only relevant when jitting */
99  check_type(val_type);
100  memcpy(scalar_address(), &val, val_type.bytes());
101  }
102 
103  /** If the parameter is a buffer parameter, get its currently
104  * bound buffer. Only relevant when jitting */
106 
107  /** Get the raw currently-bound buffer. null if unbound */
108  const halide_buffer_t *raw_buffer() const;
109 
110  /** If the parameter is a buffer parameter, set its current
111  * value. Only relevant when jitting */
112  void set_buffer(const Buffer<void> &b);
113 
114  /** Get the pointer to the current value of the scalar
115  * parameter. For a given parameter, this address will never
116  * change. Only relevant when jitting. */
117  void *scalar_address() const;
118 
119  /** Tests if this handle is the same as another handle */
120  bool same_as(const Parameter &other) const;
121 
122  /** Tests if this handle is non-nullptr */
123  bool defined() const;
124 
125  /** Get and set constraints for the min, extent, stride, and estimates on
126  * the min/extent. */
127  //@{
128  void set_min_constraint(int dim, Expr e);
129  void set_extent_constraint(int dim, Expr e);
130  void set_stride_constraint(int dim, Expr e);
132  void set_extent_constraint_estimate(int dim, Expr extent);
133  void set_host_alignment(int bytes);
134  Expr min_constraint(int dim) const;
135  Expr extent_constraint(int dim) const;
136  Expr stride_constraint(int dim) const;
139  int host_alignment() const;
140  //@}
141 
142  /** Get and set constraints for scalar parameters. These are used
143  * directly by Param, so they must be exported. */
144  // @{
145  void set_min_value(const Expr &e);
146  Expr min_value() const;
147  void set_max_value(const Expr &e);
148  Expr max_value() const;
150  Expr estimate() const;
151  // @}
152 
153  /** Get and set the default values for scalar parameters. At present, these
154  * are used only to emit the default values in the metadata. There isn't
155  * yet a public API in Param<> for them (this is used internally by the
156  * Generator code). */
157  // @{
158  void set_default_value(const Expr &e);
160  // @}
161 
162  /** Order Parameters by their IntrusivePtr so they can be used
163  * to index maps. */
164  bool operator<(const Parameter &other) const {
165  return contents < other.contents;
166  }
167 
168  /** Get the ArgumentEstimates appropriate for this Parameter. */
170 
173 };
174 
175 /** Validate arguments to a call to a func, image or imageparam. */
176 void check_call_arg_types(const std::string &name, std::vector<Expr> *args, int dims);
177 
178 } // namespace Internal
179 } // namespace Halide
180 
181 #endif
This file declares the routines used by Halide internally in its runtime.
Support classes for reference-counting via intrusive shared pointers.
Defines halide types.
Various utility functions used internally Halide.
#define HALIDE_NO_USER_CODE_INLINE
Definition: Util.h:45
A reference-counted handle to a parameter to a halide pipeline.
Definition: Parameter.h:29
ArgumentEstimates get_argument_estimates() const
Get the ArgumentEstimates appropriate for this Parameter.
Expr stride_constraint(int dim) const
Expr extent_constraint(int dim) const
Parameter(const Type &t, bool is_buffer, int dimensions)
Construct a new parameter of the given type.
HALIDE_NO_USER_CODE_INLINE void set_scalar(const Type &val_type, halide_scalar_value_t val)
If the parameter is a scalar parameter, set its current value.
Definition: Parameter.h:98
MemoryType memory_type() const
HALIDE_NO_USER_CODE_INLINE T scalar() const
If the parameter is a scalar parameter, get its currently bound value.
Definition: Parameter.h:79
void set_buffer(const Buffer< void > &b)
If the parameter is a buffer parameter, set its current value.
HALIDE_NO_USER_CODE_INLINE void set_scalar(T val)
If the parameter is a scalar parameter, set its current value.
Definition: Parameter.h:91
void set_default_value(const Expr &e)
Get and set the default values for scalar parameters.
const std::string & name() const
Get the name of this parameter.
Type type() const
Get the type of this parameter.
bool is_buffer() const
Does this parameter refer to a buffer/image?
void set_min_value(const Expr &e)
Get and set constraints for scalar parameters.
Parameter(Parameter &&)=default
Expr min_constraint(int dim) const
void store_in(MemoryType memory_type)
Expr scalar_expr() const
This returns the current value of scalar<type()>() as an Expr.
Expr extent_constraint_estimate(int dim) const
const halide_buffer_t * raw_buffer() const
Get the raw currently-bound buffer.
Parameter(const Parameter &)=default
void set_host_alignment(int bytes)
void set_extent_constraint_estimate(int dim, Expr extent)
IntrusivePtr< ParameterContents > contents
Definition: Parameter.h:37
void set_extent_constraint(int dim, Expr e)
Parameter & operator=(Parameter &&)=default
void set_min_constraint(int dim, Expr e)
Get and set constraints for the min, extent, stride, and estimates on the min/extent.
void * scalar_address() const
Get the pointer to the current value of the scalar parameter.
void set_min_constraint_estimate(int dim, Expr min)
int dimensions() const
Get the dimensionality of this parameter.
Parameter & operator=(const Parameter &)=default
void set_stride_constraint(int dim, Expr e)
void set_max_value(const Expr &e)
bool defined() const
Tests if this handle is non-nullptr.
bool operator<(const Parameter &other) const
Order Parameters by their IntrusivePtr so they can be used to index maps.
Definition: Parameter.h:164
bool same_as(const Parameter &other) const
Tests if this handle is the same as another handle.
Parameter(const Type &t, bool is_buffer, int dimensions, const std::string &name)
Construct a new parameter of the given type with name given by the third argument.
Parameter()=default
Construct a new undefined handle.
Buffer< void > buffer() const
If the parameter is a buffer parameter, get its currently bound buffer.
Expr min_constraint_estimate(int dim) const
void check_call_arg_types(const std::string &name, std::vector< Expr > *args, int dims)
Validate arguments to a call to a func, image or imageparam.
This file defines the class FunctionDAG, which is our representation of a Halide pipeline,...
@ Internal
Not visible externally, similar to 'static' linkage in C.
Expr min(const FuncRef &a, const FuncRef &b)
Explicit overloads of min and max for FuncRef.
Definition: Func.h:578
MemoryType
An enum describing different address spaces to be used with Func::store_in.
Definition: Expr.h:346
void * memcpy(void *s1, const void *s2, size_t n)
A fragment of Halide syntax.
Definition: Expr.h:256
Types in the halide type system.
Definition: Type.h:269
int bytes() const
The number of bytes required to store a single scalar value of this type.
Definition: Type.h:285
The raw representation of an image passed around by generated Halide code.
halide_scalar_value_t is a simple union able to represent all the well-known scalar values in a filte...