Halide
CodeGen_Metal_Dev.h
Go to the documentation of this file.
1 #ifndef HALIDE_CODEGEN_METAL_DEV_H
2 #define HALIDE_CODEGEN_METAL_DEV_H
3 
4 /** \file
5  * Defines the code-generator for producing Apple Metal shading language 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_Metal_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 "metal";
43  }
44 
45 protected:
46  class CodeGen_Metal_C : public CodeGen_C {
47  public:
48  CodeGen_Metal_C(std::ostream &s, Target t)
49  : CodeGen_C(s, t) {
50  }
51  void add_kernel(const 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 space_option = DoNotAppendSpace) override;
58  // Vectors in Metal come in two varieties, regular and packed.
59  // For storage allocations and pointers used in address arithmetic,
60  // packed types must be used. For temporaries, constructors, etc.
61  // regular types must be used.
62  // This concept also potentially applies to half types, which are
63  // often only supported for storage, not arithmetic,
64  // hence the method name.
65  std::string print_storage_type(Type type);
66  std::string print_type_maybe_storage(Type type, bool storage, AppendSpaceIfNeeded space);
67  std::string print_reinterpret(Type type, const Expr &e) override;
68  std::string print_extern_call(const Call *op) override;
69 
70  std::string get_memory_space(const std::string &);
71 
72  std::string shared_name;
73 
74  void visit(const Min *) override;
75  void visit(const Max *) override;
76  void visit(const Div *) override;
77  void visit(const Mod *) override;
78  void visit(const For *) override;
79  void visit(const Ramp *op) override;
80  void visit(const Broadcast *op) override;
81  void visit(const Call *op) override;
82  void visit(const Load *op) override;
83  void visit(const Store *op) override;
84  void visit(const Select *op) override;
85  void visit(const Allocate *op) override;
86  void visit(const Free *op) override;
87  void visit(const Cast *op) override;
88  void visit(const Atomic *op) override;
89  };
90 
91  std::ostringstream src_stream;
92  std::string cur_kernel_name;
94 };
95 
96 } // namespace Internal
97 } // namespace Halide
98 
99 #endif
Halide::Internal::Allocate
Allocate a scratch area called with the given name, type, and size.
Definition: IR.h:352
Halide::Internal::CodeGen_Metal_Dev::src_stream
std::ostringstream src_stream
Definition: CodeGen_Metal_Dev.h:91
Halide::Internal::CodeGen_Metal_Dev::CodeGen_Metal_C::print_storage_type
std::string print_storage_type(Type type)
CodeGen_C.h
Halide::Internal::CodeGen_C::visit
void visit(const Variable *) override
Halide::Internal::For
A for loop.
Definition: IR.h:698
Halide::Internal::Broadcast
A vector with 'lanes' elements, in which every element is 'value'.
Definition: IR.h:241
Halide::Internal::Div
The ratio of two expressions.
Definition: IR.h:65
Target.h
Halide::Internal::CodeGen_Metal_Dev::CodeGen_Metal_C::print_type_maybe_storage
std::string print_type_maybe_storage(Type type, bool storage, AppendSpaceIfNeeded space)
Halide::Internal::CodeGen_Metal_Dev::CodeGen_Metal_C::CodeGen_Metal_C
CodeGen_Metal_C(std::ostream &s, Target t)
Definition: CodeGen_Metal_Dev.h:48
Halide::Internal::CodeGen_Metal_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::Cast
The actual IR nodes begin here.
Definition: IR.h:29
Halide::Internal::CodeGen_Metal_Dev::CodeGen_Metal_C
Definition: CodeGen_Metal_Dev.h:46
Halide::Internal::CodeGen_Metal_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::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::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::CodeGen_Metal_Dev::CodeGen_Metal_Dev
CodeGen_Metal_Dev(Target target)
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_GPU_Dev
A code generator that emits GPU code from a given Halide stmt.
Definition: CodeGen_GPU_Dev.h:17
Halide::Internal::Ramp
A linear ramp vector node.
Definition: IR.h:229
Halide::Internal::CodeGen_Metal_Dev::CodeGen_Metal_C::add_kernel
void add_kernel(const Stmt &stmt, const std::string &name, const std::vector< DeviceArgument > &args)
CodeGen_GPU_Dev.h
Halide::Internal::CodeGen_Metal_Dev::CodeGen_Metal_C::print_type
std::string print_type(Type type, AppendSpaceIfNeeded space_option=DoNotAppendSpace) override
Emit the C name for a halide type.
Halide::Internal::CodeGen_Metal_Dev::CodeGen_Metal_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_Metal_Dev::CodeGen_Metal_C::get_memory_space
std::string get_memory_space(const std::string &)
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_Metal_Dev
Definition: CodeGen_Metal_Dev.h:17
Halide::Internal::Min
The lesser of two values.
Definition: IR.h:85
Halide::Internal::CodeGen_Metal_Dev::init_module
void init_module() override
(Re)initialize the GPU kernel module.
Halide::Internal::Mod
The remainder of a / b.
Definition: IR.h:76
Halide::Internal::CodeGen_Metal_Dev::CodeGen_Metal_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_Metal_Dev::CodeGen_Metal_C::shared_name
std::string shared_name
Definition: CodeGen_Metal_Dev.h:72
Halide::Internal::CodeGen_Metal_Dev::dump
void dump() override
Halide::Internal::CodeGen_Metal_Dev::compile_to_src
std::vector< char > compile_to_src() override
Halide::Internal::Call
A function call.
Definition: IR.h:464
Halide::Internal::CodeGen_Metal_Dev::metal_c
CodeGen_Metal_C metal_c
Definition: CodeGen_Metal_Dev.h:93
Halide::Internal::Select
A ternary operator.
Definition: IR.h:186
Halide::Internal::CodeGen_Metal_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_Metal_Dev.h:41
Halide::Internal::CodeGen_Metal_Dev::get_current_kernel_name
std::string get_current_kernel_name() override
Halide::Output::stmt
@ stmt
Halide::Expr
A fragment of Halide syntax.
Definition: Expr.h:256
Halide::Internal::Atomic
Lock all the Store nodes in the body statement.
Definition: IR.h:809
Halide::Internal::CodeGen_Metal_Dev::cur_kernel_name
std::string cur_kernel_name
Definition: CodeGen_Metal_Dev.h:92
Halide::Internal::CodeGen_Metal_Dev::CodeGen_Metal_C::visit
void visit(const Min *) override
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