1#ifndef HALIDE_CODEGEN_C_H
2#define HALIDE_CODEGEN_C_H
44 const std::string &include_guard =
"");
96 std::map<std::string, std::string>
cache;
262 static std::string
with_sep(
const std::vector<T> &v,
const std::string &sep) {
263 std::ostringstream o;
264 for (
size_t i = 0; i < v.size(); ++i) {
296 const std::vector<LoweredArgument> &args);
298 const std::vector<LoweredArgument> &args,
301 const std::vector<LoweredArgument> &args,
This header file defines operators that let you dump a Halide expression, statement,...
Defines the Scope class, which is used for keeping track of names in a scope while traversing IR.
Defines the structure that describes a Halide target.
This class emits C++ code equivalent to a halide Stmt.
@ CPlusPlusFunctionInfoHeader
@ CPlusPlusImplementation
void visit(const GE *) override
bool is_extern_decl()
Return true if only generating an interface, which may be extern "C" or C++.
void visit(const Provide *) override
void visit(const Evaluate *) override
virtual std::string print_type(Type, AppendSpaceIfNeeded space_option=DoNotAppendSpace)
Emit the C name for a halide type.
void print_heap_free(const std::string &alloc_name)
Emit free for the heap allocation.
void visit(const NE *) override
void visit(const Realize *) override
CodeGen_C(std::ostream &dest, const Target &target, OutputKind output_kind=CImplementation, const std::string &include_guard="")
Initialize a C code generator pointing at a particular output stream (e.g.
void visit(const Call *) override
void visit(const UIntImm *) override
void close_scope(const std::string &comment)
Close a C scope (i.e.
void visit(const Or *) override
void visit(const Sub *) override
void create_assertion(const std::string &id_cond, const Expr &message)
void open_scope()
Open a new C scope (i.e.
virtual bool is_stack_private_to_thread() const
Some architectures have private memory for the call stack; this means a thread cannot hand pointers t...
OutputKind output_kind
Controls whether this instance is generating declarations or definitions and whether the interface us...
bool is_header()
Return true if only generating an interface, which may be extern "C" or C++.
bool using_vector_typedefs
true if add_vector_typedefs() has been called.
void visit(const Max *) override
void visit(const Select *) override
void visit(const Load *) override
void visit(const FloatImm *) override
void forward_declare_type_if_needed(const Type &t)
If the Type is a handle type, emit a forward-declaration for it if we haven't already.
void visit(const LE *) override
void visit(const Div *) override
Expr scalarize_vector_reduce(const VectorReduce *op)
void visit(const Let *) override
void visit(const And *) override
void visit(const Atomic *) override
std::set< const halide_handle_cplusplus_type * > forward_declared
Track which handle types have been forward-declared already.
bool is_header_or_extern_decl()
Return true if only generating an interface, which may be extern "C" or C++.
virtual void add_vector_typedefs(const std::set< Type > &vector_types)
Add typedefs for vector types.
void visit(const For *) override
void print_stmt(const Stmt &)
Emit a statement.
void compile(const LoweredFunc &func, const MetadataNameMap &metadata_name_map)
Emit a declaration.
virtual Stmt preprocess_function_body(const Stmt &stmt)
This is a hook that subclasses can use to transform a function body just before it is emitted – e....
Scope< Allocation > allocations
Track the types of allocations to avoid unnecessary casts.
virtual std::string print_name(const std::string &)
Emit a version of a string that is a valid identifier in C (.
void visit(const GT *) override
void visit(const Free *) override
void compile(const Buffer<> &buffer)
void visit(const Store *) override
bool emit_atomic_stores
Emit atomic store instructions?
void emit_halide_free_helper(const std::string &alloc_name, const std::string &free_function)
void visit(const Prefetch *) override
void visit(const Add *) override
Scope heap_allocations
Track which allocations actually went on the heap.
Target target
The target being generated for.
virtual std::string print_extern_call(const Call *op)
Bottleneck to allow customization of calls to generic Extern/PureExtern calls.
virtual std::string print_reinterpret(Type, const Expr &)
Emit a statement to reinterpret an expression as another type.
void visit(const Reinterpret *) override
void visit(const AssertStmt *) override
void visit(const EQ *) override
const Target & get_target() const
The target we're generating code for.
void create_assertion(const Expr &cond, const Expr &message)
void visit(const VectorReduce *) override
void visit(const Ramp *) override
void emit_argv_wrapper(const std::string &function_name, const std::vector< LoweredArgument > &args)
bool is_c_plus_plus_interface()
Return true if generating C++ linkage.
void visit(const Not *) override
void emit_constexpr_function_info(const std::string &function_name, const std::vector< LoweredArgument > &args, const MetadataNameMap &metadata_name_map)
std::string id
An ID for the most recently generated ssa variable.
void visit_relop(Type t, const Expr &a, const Expr &b, const char *scalar_op, const char *vector_op)
static std::string with_commas(const std::vector< T > &v)
void visit(const Min *) override
void visit(const Variable *) override
void emit_metadata_getter(const std::string &function_name, const std::vector< LoweredArgument > &args, const MetadataNameMap &metadata_name_map)
void set_name_mangling_mode(NameMangling mode)
void visit(const Mul *) override
void visit(const Mod *) override
void visit(const Allocate *) override
bool have_user_context
True if there is a void * __user_context parameter in the arguments.
static std::string with_sep(const std::vector< T > &v, const std::string &sep)
void visit(const Cast *) override
virtual std::string print_assignment(Type t, const std::string &rhs)
Emit an SSA-style assignment, and set id to the freshly generated name.
bool extern_c_open
Track current calling convention scope.
virtual void add_platform_prologue()
Add platform specific prologue.
bool inside_atomic_mutex_node
Are we inside an atomic node that uses mutex locks? This is used for detecting deadlocks from nested ...
void visit(const IfThenElse *) override
void visit(const Broadcast *) override
void visit(const StringImm *) override
void compile(const Module &module)
Emit the declarations contained in the module as C code.
void visit(const Shuffle *) override
std::string print_expr(const Expr &)
Emit an expression as an assignment, then return the id of the resulting var.
void visit(const LT *) override
std::string print_scalarized_expr(const Expr &e)
Convert a vector Expr into a series of scalar Exprs, then reassemble into vector of original type.
void visit(const LetStmt *) override
bool uses_gpu_for_loops
True if at least one gpu-based for loop is used.
void visit(const Fork *) override
void visit(const ProducerConsumer *) override
void visit(const Acquire *) override
std::map< std::string, std::string > cache
A cache of generated values in scope.
IntegerSuffixStyle integer_suffix_style
How to emit 64-bit integer constants.
void visit(const IntImm *) override
void visit_binop(Type t, const Expr &a, const Expr &b, const char *op)
std::string print_cast_expr(const Type &, const Expr &)
Like print_expr, but cast the Expr to the given Type.
An IRVisitor that emits IR to the given output stream in a human readable form.
void visit(const IntImm *) override
A common pattern when traversing Halide IR is that you need to keep track of stuff when you find a Le...
This file defines the class FunctionDAG, which is our representation of a Halide pipeline,...
@ Internal
Not visible externally, similar to 'static' linkage in C.
NameMangling
An enum to specify calling convention for extern stages.
std::map< std::string, std::string > MetadataNameMap
A fragment of Halide syntax.
The sum of two expressions.
Allocate a scratch area called with the given name, type, and size.
Logical and - are both expressions true.
If the 'condition' is false, then evaluate and return the message, which should be a call to an error...
Lock all the Store nodes in the body statement.
A vector with 'lanes' elements, in which every element is 'value'.
The actual IR nodes begin here.
The ratio of two expressions.
Is the first expression equal to the second.
Evaluate and discard an expression, presumably because it has some side-effect.
Floating point constants.
A pair of statements executed concurrently.
Free the resources associated with the given buffer.
Is the first expression greater than or equal to the second.
Is the first expression greater than the second.
Is the first expression less than or equal to the second.
Is the first expression less than the second.
A let expression, like you might find in a functional language.
The statement form of a let node.
Load a value from a named symbol if predicate is true.
Definition of a lowered function.
The greater of two values.
The lesser of two values.
The product of two expressions.
Is the first expression not equal to the second.
Logical not - true if the expression false.
Logical or - is at least one of the expression true.
Represent a multi-dimensional region of a Func or an ImageParam that needs to be prefetched.
This node is a helpful annotation to do with permissions.
This defines the value of a function at a multi-dimensional location.
A linear ramp vector node.
Allocate a multi-dimensional buffer of the given type and size.
Reinterpret value as another type, without affecting any of the bits (on little-endian systems).
Construct a new vector by taking elements from another sequence of vectors.
A reference-counted handle to a statement node.
Store a 'value' to the buffer called 'name' at a given 'index' if 'predicate' is true.
The difference of two expressions.
Unsigned integer constants.
Horizontally reduce a vector to a scalar or narrower vector using the given commutative and associati...
A struct representing a target machine and os to generate code for.
Types in the halide type system.