codegen_c.h 3.67 KB
Newer Older
“yuguo”'s avatar
2.5  
“yuguo” committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// Copyright (c) 2021 CINN Authors. All Rights Reserved.
//
// Licensed 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.

#pragma once

#include <string>
#include <vector>

#include "paddle/cinn/common/common.h"
#include "paddle/cinn/ir/intrinsic_ops.h"
#include "paddle/cinn/ir/ir.h"
yuguo-Jack's avatar
yuguo-Jack committed
23
#include "paddle/cinn/ir/ir_printer.h"
“yuguo”'s avatar
2.5  
“yuguo” committed
24
25
26
27
#include "paddle/cinn/ir/lowered_func.h"
#include "paddle/cinn/ir/module.h"
#include "paddle/cinn/lang/packed_func.h"
#include "paddle/cinn/runtime/cinn_runtime.h"
yuguo-Jack's avatar
yuguo-Jack committed
28
#include "paddle/utils/flags.h"
“yuguo”'s avatar
2.5  
“yuguo” committed
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130

namespace cinn {

namespace ir {
class Module;
}  // namespace ir

namespace backends {

//! keyword of __restrict__.
extern const char* kCKeywordRestrict;

class CodeGenC : public ir::IrPrinter {
 public:
  enum class OutputKind {
    CHeader,  //! output the C header file.
    CImpl,    //! output the C implementation file.
  };

  explicit CodeGenC(Target target);

  void Compile(const ir::Module& module, const Outputs& outputs);

  virtual std::string Compile(const ir::Module& module, OutputKind output_kind);

  //! Disable inline the builtin codes(too large) for simpler string comparison.
  void SetInlineBuiltinCodes(bool x = true) { inline_builtin_codes_ = x; }

 protected:
  void Compile(const ir::LoweredFunc& function);

  void GenerateHeaderFile(const ir::Module& module);

  std::string GetTypeName(Type type);

  std::string GetTypeRepr(Type type);
  //! type cast, print like "int(x)"
  // @{
  void PrintCastExpr(const Type& type, Expr e);
  void PrintCastExpr(const std::string& type, Expr e);
  // @}

  void PrintFunctionDeclaration(const ir::_LoweredFunc_* op) {
    str_ += "void ";
    str_ += op->name;
    str_ += "(";
    str_ += "void* _args, int32_t num_args";
    str_ += ")";
  }

  void PrintShape(const std::vector<Expr>& shape,
                  char leftb = '{',
                  char rightb = '}');

  virtual void PrintIncludes();
  void PrintBuiltinCodes();
  void PrintFileGuardOpen(const std::string& module_name);
  void PrintFileGuardClose(const std::string& module_name);

  //! Create the buffers in global scope(just creation without allocating them).
  void PrintBufferCreation(const std::vector<ir::Buffer>& buffers);
  void PrintBufferDestroy(const std::vector<ir::Buffer>& buffers);
  void PrintRuntimeType(const cinn_type_t& type);

  //! Print different kinds of Calls.
  // @{
  void PrintCallArgs(const ir::Call* call);
  void PrintCall_buffer_malloc(const ir::Call* op);
  void PrintCall_cinn_pod_value_to_(const ir::Call* op);
  void PrintCall_get_address(const ir::Call* op);
  void PrintCall_pod_values_to_array(const ir::Call* op);
  // @}

#define __DEFINE_VISIT(op__) void Visit(const ir::op__* op) override;
  NODETY_FORALL(__DEFINE_VISIT)
#undef __DEFINE_VISIT

#define __DEFINE_VISIT(op__) \
  void Visit(const ir::intrinsics::op__* op) override;
  INTRINSIC_KIND_FOR_EACH(__DEFINE_VISIT)
#undef __DEFINE_VISIT

  void PrintFuncArg(const ir::Argument& arg);

  void PrintStackVecType(Type type, int lanes);

  friend class ExternFunctionEmitter;

 protected:
  Target target_;
  std::stringstream ss_;
  bool inline_builtin_codes_{true};
};

namespace detail {

Expr StridedRampBase(Expr e, int stride);

}  // namespace detail

}  // namespace backends
}  // namespace cinn