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 
17 class CodeGen_OpenCL_Dev : public CodeGen_GPU_Dev {
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 
api_unique_name()41     std::string api_unique_name() override {
42         return "opencl";
43     }
44 
45 protected:
46     class CodeGen_OpenCL_C : public CodeGen_C {
47     public:
CodeGen_OpenCL_C(std::ostream & s,Target t)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;
94     CodeGen_OpenCL_C clc;
95 };
96 
97 }  // namespace Internal
98 }  // namespace Halide
99 
100 #endif
101