Halide 22.0.0 Halide compiler and libraries

Loading...
Searching...
No Matches
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. */
18 struct CodeGen_GPU_Dev {
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 /** Checks if expr is block uniform, i.e. does not depend on a thread
59 * var. */
60 static bool is_block_uniform(const Expr &expr);
61 /** Checks if the buffer is a candidate for constant storage. Most
62 * GPUs (APIs) support a constant memory storage class that cannot be
63 * written to and performs well for block uniform accesses. A buffer is a
64 * candidate for constant storage if it is never written to, and loads are
65 * uniform within the workgroup. */
66 static bool is_buffer_constant(const Stmt &kernel, const std::string &buffer);
67
68 /** Modifies predicated loads and stores to be non-predicated, since most
69 * GPU backends do not support predication. */
70 static Stmt scalarize_predicated_loads_stores(Stmt &s);
71
72 /** An mask describing which type of memory fence to use for the gpu_thread_barrier()
73 * intrinsic. Not all GPUs APIs support all types.
74 */
75 enum MemoryFenceType {
76 None = 0, // No fence required (just a sync)
77 Device = 1, // Device/global memory fence
78 Shared = 2 // Threadgroup/shared memory fence
79 };
80};
81
82/** A base class for GPU backends that require C-like shader output.
83 * GPU backends derive from and specialize this class. */
84 class CodeGen_GPU_C : public CodeGen_C {
85 public:
86 /** OpenCL and WGSL use different syntax than C for immediate vectors. This
87 enum defines which style should be used by the backend. */
88 enum class VectorDeclarationStyle {
89 CLikeSyntax = 0,
90 OpenCLSyntax = 1,
91 WGSLSyntax = 2,
92 };
93
94 CodeGen_GPU_C(std::ostream &s, Target t)
95 : CodeGen_C(s, t) {
96 }
97
98 protected:
99 using CodeGen_C::visit;
100 void visit(const Shuffle *op) override;
101 void visit(const Call *op) override;
102
103 std::string print_extern_call(const Call *op) override;
104
105 VectorDeclarationStyle vector_declaration_style = VectorDeclarationStyle::CLikeSyntax;
106 bool abs_returns_unsigned_type{false};
107};
108
109} // namespace Internal
110} // namespace Halide
111
112#endif
CodeGen_C.h
Defines an IRPrinter that emits C++ code equivalent to a halide stmt.
DeviceArgument.h
Defines helpers for passing arguments to separate devices, such as GPUs.
Expr.h
Base classes for Halide expressions (Halide::Expr) and statements (Halide::Internal::Stmt)
Halide::Internal::CodeGen_C
This class emits C++ code equivalent to a halide Stmt.
Definition CodeGen_C.h:29
Halide::Internal::CodeGen_C::visit
void visit(const Variable *) override
Halide::Internal::CodeGen_GPU_C
A base class for GPU backends that require C-like shader output.
Definition CodeGen_GPU_Dev.h:84
Halide::Internal::CodeGen_GPU_C::abs_returns_unsigned_type
bool abs_returns_unsigned_type
Definition CodeGen_GPU_Dev.h:106
Halide::Internal::CodeGen_GPU_C::VectorDeclarationStyle
VectorDeclarationStyle
OpenCL and WGSL use different syntax than C for immediate vectors.
Definition CodeGen_GPU_Dev.h:88
Halide::Internal::CodeGen_GPU_C::VectorDeclarationStyle::WGSLSyntax
@ WGSLSyntax
Halide::Internal::CodeGen_GPU_C::VectorDeclarationStyle::CLikeSyntax
@ CLikeSyntax
Halide::Internal::CodeGen_GPU_C::VectorDeclarationStyle::OpenCLSyntax
@ OpenCLSyntax
Halide::Internal::CodeGen_GPU_C::visit
void visit(const Call *op) override
Halide::Internal::CodeGen_GPU_C::CodeGen_GPU_C
CodeGen_GPU_C(std::ostream &s, Target t)
Definition CodeGen_GPU_Dev.h:94
Halide::Internal::CodeGen_GPU_C::visit
void visit(const Shuffle *op) override
Halide::Internal::CodeGen_GPU_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::CodeGen_GPU_C::vector_declaration_style
VectorDeclarationStyle vector_declaration_style
Definition CodeGen_GPU_Dev.h:105
Halide
This file defines the class FunctionDAG, which is our representation of a Halide pipeline,...
Definition AbstractGenerator.h:19
Halide::LinkageType::Internal
@ Internal
Not visible externally, similar to 'static' linkage in C.
Halide::OutputFileType::stmt
@ stmt
Halide::cast
Internal::ConstantInterval cast(Type t, const Internal::ConstantInterval &a)
Cast operators for ConstantIntervals.
Halide::Expr
A fragment of Halide syntax.
Definition Expr.h:258
Halide::Internal::Call
A function call.
Definition IR.h:490
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::init_module
virtual void init_module()=0
(Re)initialize the GPU kernel module.
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_Dev::MemoryFenceType
MemoryFenceType
An mask describing which type of memory fence to use for the gpu_thread_barrier() intrinsic.
Definition CodeGen_GPU_Dev.h:75
Halide::Internal::CodeGen_GPU_Dev::Device
@ Device
Definition CodeGen_GPU_Dev.h:77
Halide::Internal::CodeGen_GPU_Dev::None
@ None
Definition CodeGen_GPU_Dev.h:76
Halide::Internal::CodeGen_GPU_Dev::Shared
@ Shared
Definition CodeGen_GPU_Dev.h:78
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::~CodeGen_GPU_Dev
virtual ~CodeGen_GPU_Dev()
Halide::Internal::CodeGen_GPU_Dev::dump
virtual void dump()=0
Halide::Internal::CodeGen_GPU_Dev::get_current_kernel_name
virtual std::string get_current_kernel_name()=0
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::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::Internal::CodeGen_GPU_Dev::compile_to_src
virtual std::vector< char > compile_to_src()=0
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::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::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::Shuffle
Construct a new vector by taking elements from another sequence of vectors.
Definition IR.h:884
Halide::Internal::Stmt
A reference-counted handle to a statement node.
Definition Expr.h:427
Halide::Target
A struct representing a target machine and os to generate code for.
Definition Target.h:19