codegen_webgpu.h 3.56 KB
Newer Older
root's avatar
init  
root committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

/*!
 * \file codegen_webgpu.h
 * \brief Generate WebGPU shaders in WGSL.
 *
 * This module generates WGSL shading language.
 * See https://www.w3.org/TR/WGSL/ for the language reference.
 */
#ifndef TVM_TARGET_SOURCE_CODEGEN_WEBGPU_H_
#define TVM_TARGET_SOURCE_CODEGEN_WEBGPU_H_

#include <tvm/target/codegen.h>

#include <string>

#include "target/source/codegen_c.h"

namespace tvm {
namespace codegen {

/*!
 * \brief WebGPU code generator.
 *
 * Note WGSL have a different syntax from normal C.
 * We only leverage the C for expression generation and
 * write most of the language generations.
 */
class CodeGenTileLangWebGPU final : public CodeGenC {
public:
  explicit CodeGenTileLangWebGPU(Target target);
  // overrides
  std::string Finish() final;
  using CodeGenC::AddFunction;
  runtime::FunctionInfo AddFunction(const PrimFunc &f,
                                    bool skip_readonly_decl); // NOLINT(*)
  void InitFuncState(const PrimFunc &f) final;
  void PrintStorageSync(const CallNode *op) final;    // NOLINT(*)
  void PrintType(DataType t, std::ostream &os) final; // NOLINT(*)
  void BindThreadIndex(const IterVar &iv) final;      // NOLINT(*)

  // assignment printing
  void PrintSSAAssign(const std::string &target, const std::string &src,
                      DataType type) final;

  // overload visitor
  void VisitExpr_(const BroadcastNode *op, std::ostream &os) final; // NOLINT(*)
  void VisitExpr_(const CallNode *op, std::ostream &os) final;      // NOLINT(*)
  void VisitExpr_(const BufferLoadNode *op,
                  std::ostream &os) final;                          // NOLINT(*)
  void VisitExpr_(const CastNode *op, std::ostream &os) final;      // NOLINT(*)
  void VisitExpr_(const SelectNode *op, std::ostream &os) override; // NOLINT(*)
  void VisitExpr_(const FloatImmNode *op, std::ostream &os) final;  // NOLINT(*)
  void VisitExpr_(const IntImmNode *op, std::ostream &os) final;    // NOLINT(*)

  // stmt printing
  void VisitStmt_(const LetStmtNode *op) final;
  void VisitStmt_(const BufferStoreNode *op) final;
  void VisitStmt_(const ForNode *op) final;
  void VisitStmt_(const AllocateNode *op) final;
  void VisitStmt_(const AssertStmtNode *op) final;
  void VisitStmt_(const AllocateConstNode *op) final;
  void VisitStmt_(const WhileNode *op) final;

private:
  /*!
   * \brief Enforce value to be U32.
   */
  static PrimExpr EnforceU32(PrimExpr value);
  /*!
   * \brief Storage type of bool values.
   */
  DataType boolean_storage_type_{DataType::Int(8)};

  // whether enable fp16
  bool enable_fp16_{false};

  /*! \brief the header stream for function label and enable directive if any,
   * goes before any other declaration */
  std::ostringstream header_stream;

  Target target_;
};
} // namespace codegen
} // namespace tvm

#endif // TVM_TARGET_SOURCE_CODEGEN_WEBGPU_H_