Halide 14.0.0
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 "Buffer.h"
10#include "IntrusivePtr.h"
11#include "Type.h"
12#include "Util.h" // for HALIDE_NO_USER_CODE_INLINE
13#include "runtime/HalideRuntime.h" // for HALIDE_ALWAYS_INLINE
14
15namespace Halide {
16
17struct ArgumentEstimates;
18struct Expr;
19struct Type;
20enum class MemoryType;
21
22namespace Internal {
23
24struct ParameterContents;
25
26/** A reference-counted handle to a parameter to a halide
27 * pipeline. May be a scalar parameter or a buffer */
28class Parameter {
29 void check_defined() const;
30 void check_is_buffer() const;
31 void check_is_scalar() const;
32 void check_dim_ok(int dim) const;
33 void check_type(const Type &t) const;
34
35protected:
37
38public:
39 /** Construct a new undefined handle */
40 Parameter() = default;
41
42 /** Construct a new parameter of the given type. If the second
43 * argument is true, this is a buffer parameter of the given
44 * dimensionality, otherwise, it is a scalar parameter (and the
45 * dimensionality should be zero). The parameter will be given a
46 * unique auto-generated name. */
47 Parameter(const Type &t, bool is_buffer, int dimensions);
48
49 /** Construct a new parameter of the given type with name given by
50 * the third argument. If the second argument is true, this is a
51 * buffer parameter, otherwise, it is a scalar parameter. The
52 * third argument gives the dimensionality of the buffer
53 * parameter. It should be zero for scalar parameters. If the
54 * fifth argument is true, the the name being passed in was
55 * explicitly specified (as opposed to autogenerated). */
56 Parameter(const Type &t, bool is_buffer, int dimensions, const std::string &name);
57
58 Parameter(const Parameter &) = default;
59 Parameter &operator=(const Parameter &) = default;
60 Parameter(Parameter &&) = default;
62
63 /** Get the type of this parameter */
64 Type type() const;
65
66 /** Get the dimensionality of this parameter. Zero for scalars. */
67 int dimensions() const;
68
69 /** Get the name of this parameter */
70 const std::string &name() const;
71
72 /** Does this parameter refer to a buffer/image? */
73 bool is_buffer() const;
74
75 /** If the parameter is a scalar parameter, get its currently
76 * bound value. Only relevant when jitting */
77 template<typename T>
79 check_type(type_of<T>());
80 return *((const T *)(scalar_address()));
81 }
82
83 /** This returns the current value of scalar<type()>()
84 * as an Expr. */
86
87 /** If the parameter is a scalar parameter, set its current
88 * value. Only relevant when jitting */
89 template<typename T>
91 check_type(type_of<T>());
92 *((T *)(scalar_address())) = val;
93 }
94
95 /** If the parameter is a scalar parameter, set its current
96 * value. Only relevant when jitting */
98 check_type(val_type);
99 memcpy(scalar_address(), &val, val_type.bytes());
100 }
101
102 /** If the parameter is a buffer parameter, get its currently
103 * bound buffer. Only relevant when jitting */
105
106 /** Get the raw currently-bound buffer. null if unbound */
108
109 /** If the parameter is a buffer parameter, set its current
110 * value. Only relevant when jitting */
111 void set_buffer(const Buffer<void> &b);
112
113 /** Get the pointer to the current value of the scalar
114 * parameter. For a given parameter, this address will never
115 * change. Only relevant when jitting. */
116 void *scalar_address() const;
117
118 /** Tests if this handle is the same as another handle */
119 bool same_as(const Parameter &other) const;
120
121 /** Tests if this handle is non-nullptr */
122 bool defined() const;
123
124 /** Get and set constraints for the min, extent, stride, and estimates on
125 * the min/extent. */
126 //@{
127 void set_min_constraint(int dim, Expr e);
128 void set_extent_constraint(int dim, Expr e);
129 void set_stride_constraint(int dim, Expr e);
132 void set_host_alignment(int bytes);
133 Expr min_constraint(int dim) const;
134 Expr extent_constraint(int dim) const;
135 Expr stride_constraint(int dim) const;
138 int host_alignment() const;
139 //@}
140
141 /** Get and set constraints for scalar parameters. These are used
142 * directly by Param, so they must be exported. */
143 // @{
144 void set_min_value(const Expr &e);
146 void set_max_value(const Expr &e);
149 Expr estimate() const;
150 // @}
151
152 /** Get and set the default values for scalar parameters. At present, these
153 * are used only to emit the default values in the metadata. There isn't
154 * yet a public API in Param<> for them (this is used internally by the
155 * Generator code). */
156 // @{
157 void set_default_value(const Expr &e);
159 // @}
160
161 /** Order Parameters by their IntrusivePtr so they can be used
162 * to index maps. */
163 bool operator<(const Parameter &other) const {
164 return contents < other.contents;
165 }
166
167 /** Get the ArgumentEstimates appropriate for this Parameter. */
169
172};
173
174/** Validate arguments to a call to a func, image or imageparam. */
175void check_call_arg_types(const std::string &name, std::vector<Expr> *args, int dims);
176
177} // namespace Internal
178} // namespace Halide
179
180#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:28
Parameter & operator=(Parameter &&)=default
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:97
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:78
void * scalar_address() const
Get the pointer to the current value of the scalar parameter.
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:90
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
const halide_buffer_t * raw_buffer() const
Get the raw currently-bound buffer.
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
Buffer< void > buffer() const
If the parameter is a buffer parameter, get its 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:36
Parameter & operator=(const Parameter &)=default
void set_extent_constraint(int dim, Expr e)
void set_min_constraint(int dim, Expr e)
Get and set constraints for the min, extent, stride, and estimates on the min/extent.
void set_min_constraint_estimate(int dim, Expr min)
int dimensions() const
Get the dimensionality of this parameter.
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:163
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.
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:600
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:266
int bytes() const
The number of bytes required to store a single scalar value of this type.
Definition: Type.h:282
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...