Halide
CodeGen_OpenCL_Dev.h
Go to the documentation of this file.
1 #ifndef HALIDE_CODEGEN_OPENCL_DEV_H
2 #define HALIDE_CODEGEN_OPENCL_DEV_H
3 
4 /** \file
5  * Defines the code-generator for producing OpenCL C kernel code
6  */
7 
8 #include <sstream>
9 
10 #include "CodeGen_C.h"
11 #include "CodeGen_GPU_Dev.h"
12 #include "Target.h"
13 
14 namespace Halide {
15 namespace Internal {
16 
18 public:
19  CodeGen_OpenCL_Dev(Target target);
20 
21  /** Compile a GPU kernel into the module. This may be called many times
22  * with different kernels, which will all be accumulated into a single
23  * source module shared by a given Halide pipeline. */
24  void add_kernel(Stmt stmt,
25  const std::string &name,
26  const std::vector<DeviceArgument> &args) override;
27 
28  /** (Re)initialize the GPU kernel module. This is separate from compile,
29  * since a GPU device module will often have many kernels compiled into it
30  * for a single pipeline. */
31  void init_module() override;
32 
33  std::vector<char> compile_to_src() override;
34 
35  std::string get_current_kernel_name() override;
36 
37  void dump() override;
38 
39  std::string print_gpu_name(const std::string &name) override;
40 
41  std::string api_unique_name() override {
42  return "opencl";
43  }
44 
45 protected:
46  class CodeGen_OpenCL_C : public CodeGen_C {
47  public:
48  CodeGen_OpenCL_C(std::ostream &s, Target t)
49  : CodeGen_C(s, t) {
50  }
51  void add_kernel(Stmt stmt,
52  const std::string &name,
53  const std::vector<DeviceArgument> &args);
54 
55  protected:
56  using CodeGen_C::visit;
57  std::string print_type(Type type, AppendSpaceIfNeeded append_space = DoNotAppendSpace) override;
58  std::string print_reinterpret(Type type, const Expr &e) override;
59  std::string print_extern_call(const Call *op) override;
60  std::string print_array_access(const std::string &name,
61  const Type &type,
62  const std::string &id_index);
63  void add_vector_typedefs(const std::set<Type> &vector_types) override;
64 
65  std::string get_memory_space(const std::string &);
66 
67  std::string shared_name;
68 
69  void visit(const For *) override;
70  void visit(const Ramp *op) override;
71  void visit(const Broadcast *op) override;
72  void visit(const Call *op) override;
73  void visit(const Load *op) override;
74  void visit(const Store *op) override;
75  void visit(const Cast *op) override;
76  void visit(const Select *op) override;
77  void visit(const EQ *) override;
78  void visit(const NE *) override;
79  void visit(const LT *) override;
80  void visit(const LE *) override;
81  void visit(const GT *) override;
82  void visit(const GE *) override;
83  void visit(const Allocate *op) override;
84  void visit(const Free *op) override;
85  void visit(const AssertStmt *op) override;
86  void visit(const Shuffle *op) override;
87  void visit(const Min *op) override;
88  void visit(const Max *op) override;
89  void visit(const Atomic *op) override;
90  };
91 
92  std::ostringstream src_stream;
93  std::string cur_kernel_name;
95 };
96 
97 } // namespace Internal
98 } // namespace Halide
99 
100 #endif
Halide::Internal::Allocate
Allocate a scratch area called with the given name, type, and size.
Definition: IR.h:352
Halide::Internal::CodeGen_OpenCL_Dev::CodeGen_OpenCL_C::CodeGen_OpenCL_C
CodeGen_OpenCL_C(std::ostream &s, Target t)
Definition: CodeGen_OpenCL_Dev.h:48
Halide::Internal::CodeGen_OpenCL_Dev::CodeGen_OpenCL_C
Definition: CodeGen_OpenCL_Dev.h:46
Halide::Internal::CodeGen_OpenCL_Dev::cur_kernel_name
std::string cur_kernel_name
Definition: CodeGen_OpenCL_Dev.h:93
Halide::Internal::CodeGen_OpenCL_Dev::CodeGen_OpenCL_C::add_kernel
void add_kernel(Stmt stmt, const std::string &name, const std::vector< DeviceArgument > &args)
Halide::Internal::CodeGen_OpenCL_Dev::CodeGen_OpenCL_Dev
CodeGen_OpenCL_Dev(Target target)
CodeGen_C.h
Halide::Internal::CodeGen_C::visit
void visit(const Variable *) override
Halide::Internal::GE
Is the first expression greater than or equal to the second.
Definition: IR.h:148
Halide::Internal::For
A for loop.
Definition: IR.h:698
Halide::Internal::CodeGen_OpenCL_Dev::CodeGen_OpenCL_C::add_vector_typedefs
void add_vector_typedefs(const std::set< Type > &vector_types) override
Add typedefs for vector types.
Halide::Internal::Broadcast
A vector with 'lanes' elements, in which every element is 'value'.
Definition: IR.h:241
Target.h
Halide::Internal::CodeGen_OpenCL_Dev::CodeGen_OpenCL_C::shared_name
std::string shared_name
Definition: CodeGen_OpenCL_Dev.h:67
Halide::Internal::Cast
The actual IR nodes begin here.
Definition: IR.h:29
Halide::Internal::LE
Is the first expression less than or equal to the second.
Definition: IR.h:130
Halide::Internal::NE
Is the first expression not equal to the second.
Definition: IR.h:112
Halide::Internal::CodeGen_OpenCL_Dev::CodeGen_OpenCL_C::print_reinterpret
std::string print_reinterpret(Type type, const Expr &e) override
Emit a statement to reinterpret an expression as another type.
Halide::Internal::CodeGen_OpenCL_Dev
Definition: CodeGen_OpenCL_Dev.h:17
Halide::Internal::CodeGen_OpenCL_Dev::add_kernel
void add_kernel(Stmt stmt, const std::string &name, const std::vector< DeviceArgument > &args) override
Compile a GPU kernel into the module.
Halide::Internal::Stmt
A reference-counted handle to a statement node.
Definition: Expr.h:409
Halide::Internal::CodeGen_C
This class emits C++ code equivalent to a halide Stmt.
Definition: CodeGen_C.h:24
Halide::Type
Types in the halide type system.
Definition: Type.h:269
Halide::Internal::CodeGen_OpenCL_Dev::CodeGen_OpenCL_C::print_extern_call
std::string print_extern_call(const Call *op) override
Bottleneck to allow customization of calls to generic Extern/PureExtern calls.
Halide::Internal::Load
Load a value from a named symbol if predicate is true.
Definition: IR.h:199
Halide::Internal::Free
Free the resources associated with the given buffer.
Definition: IR.h:388
Halide
This file defines the class FunctionDAG, which is our representation of a Halide pipeline,...
Definition: AddAtomicMutex.h:21
Halide::Internal::EQ
Is the first expression equal to the second.
Definition: IR.h:103
Halide::Internal::CodeGen_OpenCL_Dev::src_stream
std::ostringstream src_stream
Definition: CodeGen_OpenCL_Dev.h:92
Halide::Internal::CodeGen_C::AppendSpaceIfNeeded
AppendSpaceIfNeeded
Definition: CodeGen_C.h:89
Halide::LinkageType::Internal
@ Internal
Not visible externally, similar to 'static' linkage in C.
Halide::Internal::Max
The greater of two values.
Definition: IR.h:94
Halide::Internal::CodeGen_OpenCL_Dev::get_current_kernel_name
std::string get_current_kernel_name() override
Halide::Internal::CodeGen_GPU_Dev
A code generator that emits GPU code from a given Halide stmt.
Definition: CodeGen_GPU_Dev.h:17
Halide::Internal::CodeGen_OpenCL_Dev::CodeGen_OpenCL_C::get_memory_space
std::string get_memory_space(const std::string &)
Halide::Internal::CodeGen_OpenCL_Dev::CodeGen_OpenCL_C::print_type
std::string print_type(Type type, AppendSpaceIfNeeded append_space=DoNotAppendSpace) override
Emit the C name for a halide type.
Halide::Internal::CodeGen_OpenCL_Dev::clc
CodeGen_OpenCL_C clc
Definition: CodeGen_OpenCL_Dev.h:94
Halide::Internal::CodeGen_OpenCL_Dev::compile_to_src
std::vector< char > compile_to_src() override
Halide::Internal::Ramp
A linear ramp vector node.
Definition: IR.h:229
Halide::Internal::CodeGen_OpenCL_Dev::dump
void dump() override
CodeGen_GPU_Dev.h
Halide::Internal::Store
Store a 'value' to the buffer called 'name' at a given 'index' if 'predicate' is true.
Definition: IR.h:315
Halide::Internal::CodeGen_OpenCL_Dev::CodeGen_OpenCL_C::print_array_access
std::string print_array_access(const std::string &name, const Type &type, const std::string &id_index)
Halide::Internal::Min
The lesser of two values.
Definition: IR.h:85
Halide::Internal::AssertStmt
If the 'condition' is false, then evaluate and return the message, which should be a call to an error...
Definition: IR.h:276
Halide::Internal::Call
A function call.
Definition: IR.h:464
Halide::Internal::CodeGen_OpenCL_Dev::print_gpu_name
std::string print_gpu_name(const std::string &name) override
Returns the specified name transformed by the variable naming rules for the GPU language backend.
Halide::Internal::CodeGen_OpenCL_Dev::CodeGen_OpenCL_C::visit
void visit(const For *) override
Halide::Internal::CodeGen_OpenCL_Dev::init_module
void init_module() override
(Re)initialize the GPU kernel module.
Halide::Internal::Select
A ternary operator.
Definition: IR.h:186
Halide::Output::stmt
@ stmt
Halide::Internal::CodeGen_OpenCL_Dev::api_unique_name
std::string api_unique_name() override
This routine returns the GPU API name that is combined into runtime routine names to ensure each GPU ...
Definition: CodeGen_OpenCL_Dev.h:41
Halide::Expr
A fragment of Halide syntax.
Definition: Expr.h:256
Halide::Internal::GT
Is the first expression greater than the second.
Definition: IR.h:139
Halide::Internal::Atomic
Lock all the Store nodes in the body statement.
Definition: IR.h:809
Halide::Internal::Shuffle
Construct a new vector by taking elements from another sequence of vectors.
Definition: IR.h:729
Halide::Internal::CodeGen_C::DoNotAppendSpace
@ DoNotAppendSpace
Definition: CodeGen_C.h:90
Halide::Target
A struct representing a target machine and os to generate code for.
Definition: Target.h:19
Halide::Internal::LT
Is the first expression less than the second.
Definition: IR.h:121