Halide
CodeGen_GPU_Dev.h
Go to the documentation of this file.
1 #ifndef HALIDE_CODEGEN_GPU_DEV_H
2 #define HALIDE_CODEGEN_GPU_DEV_H
3 
4 /** \file
5  * Defines the code-generator interface for producing GPU device code
6  */
7 #include <string>
8 #include <vector>
9 
10 #include "CodeGen_C.h"
11 #include "DeviceArgument.h"
12 #include "Expr.h"
13 
14 namespace Halide {
15 namespace Internal {
16 
17 /** A code generator that emits GPU code from a given Halide stmt. */
19  virtual ~CodeGen_GPU_Dev();
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  virtual void add_kernel(Stmt stmt,
25  const std::string &name,
26  const std::vector<DeviceArgument> &args) = 0;
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  virtual void init_module() = 0;
32 
33  virtual std::vector<char> compile_to_src() = 0;
34 
35  virtual std::string get_current_kernel_name() = 0;
36 
37  virtual void dump() = 0;
38 
39  /** This routine returns the GPU API name that is combined into
40  * runtime routine names to ensure each GPU API has a unique
41  * name.
42  */
43  virtual std::string api_unique_name() = 0;
44 
45  /** Returns the specified name transformed by the variable naming rules
46  * for the GPU language backend. Used to determine the name of a parameter
47  * during host codegen. */
48  virtual std::string print_gpu_name(const std::string &name) = 0;
49 
50  /** Allows the GPU device specific code to request halide_type_t
51  * values to be passed to the kernel_run routine rather than just
52  * argument type sizes.
53  */
54  virtual bool kernel_run_takes_types() const {
55  return false;
56  }
57 
58  static bool is_gpu_var(const std::string &name);
59  static bool is_gpu_block_var(const std::string &name);
60  static bool is_gpu_thread_var(const std::string &name);
61 
62  /** Checks if expr is block uniform, i.e. does not depend on a thread
63  * var. */
64  static bool is_block_uniform(const Expr &expr);
65  /** Checks if the buffer is a candidate for constant storage. Most
66  * GPUs (APIs) support a constant memory storage class that cannot be
67  * written to and performs well for block uniform accesses. A buffer is a
68  * candidate for constant storage if it is never written to, and loads are
69  * uniform within the workgroup. */
70  static bool is_buffer_constant(const Stmt &kernel, const std::string &buffer);
71 
72  /** Modifies predicated loads and stores to be non-predicated, since most
73  * GPU backends do not support predication. */
75 
76  /** An mask describing which type of memory fence to use for the gpu_thread_barrier()
77  * intrinsic. Not all GPUs APIs support all types.
78  */
80  None = 0, // No fence required (just a sync)
81  Device = 1, // Device/global memory fence
82  Shared = 2 // Threadgroup/shared memory fence
83  };
84 };
85 
86 /** A base class for GPU backends that require C-like shader output.
87  * GPU backends derive from and specialize this class. */
88 class CodeGen_GPU_C : public CodeGen_C {
89 public:
90  /** OpenCL and WGSL use different syntax than C for immediate vectors. This
91  enum defines which style should be used by the backend. */
93  CLikeSyntax = 0,
94  OpenCLSyntax = 1,
95  WGSLSyntax = 2,
96  };
97 
98  CodeGen_GPU_C(std::ostream &s, Target t)
99  : CodeGen_C(s, t) {
100  }
101 
102 protected:
103  using CodeGen_C::visit;
104  void visit(const Shuffle *op) override;
105  void visit(const Call *op) override;
106 
108 };
109 
110 } // namespace Internal
111 } // namespace Halide
112 
113 #endif
Halide::Internal::CodeGen_GPU_Dev::Device
@ Device
Definition: CodeGen_GPU_Dev.h:81
Halide::Internal::CodeGen_GPU_Dev::~CodeGen_GPU_Dev
virtual ~CodeGen_GPU_Dev()
CodeGen_C.h
Halide::Internal::CodeGen_C::visit
void visit(const Variable *) override
Halide::Internal::CodeGen_GPU_Dev::is_gpu_var
static bool is_gpu_var(const std::string &name)
Halide::Internal::CodeGen_GPU_Dev::dump
virtual void dump()=0
Halide::Internal::CodeGen_GPU_Dev::is_gpu_block_var
static bool is_gpu_block_var(const std::string &name)
Halide::Internal::CodeGen_GPU_Dev::print_gpu_name
virtual std::string print_gpu_name(const std::string &name)=0
Returns the specified name transformed by the variable naming rules for the GPU language backend.
Halide::Internal::CodeGen_GPU_Dev::Shared
@ Shared
Definition: CodeGen_GPU_Dev.h:82
Halide::Internal::Stmt
A reference-counted handle to a statement node.
Definition: Expr.h:418
Halide::Internal::CodeGen_C
This class emits C++ code equivalent to a halide Stmt.
Definition: CodeGen_C.h:27
Halide
This file defines the class FunctionDAG, which is our representation of a Halide pipeline,...
Definition: AbstractGenerator.h:19
Halide::Internal::CodeGen_GPU_Dev::api_unique_name
virtual std::string api_unique_name()=0
This routine returns the GPU API name that is combined into runtime routine names to ensure each GPU ...
Halide::Internal::CodeGen_GPU_Dev::MemoryFenceType
MemoryFenceType
An mask describing which type of memory fence to use for the gpu_thread_barrier() intrinsic.
Definition: CodeGen_GPU_Dev.h:79
Halide::LinkageType::Internal
@ Internal
Not visible externally, similar to 'static' linkage in C.
Halide::Internal::CodeGen_GPU_C::VectorDeclarationStyle::WGSLSyntax
@ WGSLSyntax
Halide::Internal::CodeGen_GPU_Dev
A code generator that emits GPU code from a given Halide stmt.
Definition: CodeGen_GPU_Dev.h:18
Halide::Internal::CodeGen_GPU_Dev::add_kernel
virtual void add_kernel(Stmt stmt, const std::string &name, const std::vector< DeviceArgument > &args)=0
Compile a GPU kernel into the module.
Halide::Internal::CodeGen_GPU_Dev::is_gpu_thread_var
static bool is_gpu_thread_var(const std::string &name)
Halide::OutputFileType::stmt
@ stmt
Halide::Internal::CodeGen_GPU_Dev::is_block_uniform
static bool is_block_uniform(const Expr &expr)
Checks if expr is block uniform, i.e.
Halide::Internal::CodeGen_GPU_C
A base class for GPU backends that require C-like shader output.
Definition: CodeGen_GPU_Dev.h:88
Halide::Internal::CodeGen_GPU_C::CodeGen_GPU_C
CodeGen_GPU_C(std::ostream &s, Target t)
Definition: CodeGen_GPU_Dev.h:98
Halide::Internal::CodeGen_GPU_C::VectorDeclarationStyle
VectorDeclarationStyle
OpenCL and WGSL use different syntax than C for immediate vectors.
Definition: CodeGen_GPU_Dev.h:92
Halide::Internal::CodeGen_GPU_C::VectorDeclarationStyle::OpenCLSyntax
@ OpenCLSyntax
Expr.h
Halide::Internal::CodeGen_GPU_Dev::scalarize_predicated_loads_stores
static Stmt scalarize_predicated_loads_stores(Stmt &s)
Modifies predicated loads and stores to be non-predicated, since most GPU backends do not support pre...
Halide::Internal::CodeGen_GPU_Dev::compile_to_src
virtual std::vector< char > compile_to_src()=0
DeviceArgument.h
Halide::Internal::Call
A function call.
Definition: IR.h:482
Halide::Internal::CodeGen_GPU_C::VectorDeclarationStyle::CLikeSyntax
@ CLikeSyntax
Halide::Internal::CodeGen_GPU_Dev::is_buffer_constant
static bool is_buffer_constant(const Stmt &kernel, const std::string &buffer)
Checks if the buffer is a candidate for constant storage.
Halide::Internal::CodeGen_GPU_Dev::kernel_run_takes_types
virtual bool kernel_run_takes_types() const
Allows the GPU device specific code to request halide_type_t values to be passed to the kernel_run ro...
Definition: CodeGen_GPU_Dev.h:54
Halide::Expr
A fragment of Halide syntax.
Definition: Expr.h:257
Halide::Internal::Shuffle
Construct a new vector by taking elements from another sequence of vectors.
Definition: IR.h:819
Halide::Internal::CodeGen_GPU_Dev::get_current_kernel_name
virtual std::string get_current_kernel_name()=0
Halide::Internal::CodeGen_GPU_Dev::init_module
virtual void init_module()=0
(Re)initialize the GPU kernel module.
Halide::Internal::CodeGen_GPU_C::visit
void visit(const Shuffle *op) override
Halide::Target
A struct representing a target machine and os to generate code for.
Definition: Target.h:19
Halide::Internal::CodeGen_GPU_Dev::None
@ None
Definition: CodeGen_GPU_Dev.h:80
Halide::Internal::CodeGen_GPU_C::vector_declaration_style
VectorDeclarationStyle vector_declaration_style
Definition: CodeGen_GPU_Dev.h:107