codegen_opengl.cc 9.88 KB
Newer Older
1 2 3 4 5 6 7 8
/*
 * 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
9
 *
10
 *   http://www.apache.org/licenses/LICENSE-2.0
11
 *
12 13 14 15 16 17 18 19
 * 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.
 */

20 21 22 23 24 25 26 27
/*!
 * \file codegen_opengl.cc
 *
 * We are targeting OpenGL 3.3. The reason of not targeting a recent version
 * of OpenGL is to have better compatibility of WebGL 2.
 */
#include <vector>
#include <string>
28 29
#include <utility>
#include <unordered_map>
30
#include "codegen_opengl.h"
31 32
#include "../build_common.h"
#include "../../runtime/thread_storage_scope.h"
33 34 35 36 37 38 39

namespace tvm {
namespace codegen {

CodeGenOpenGL::CodeGenOpenGL()
    : output_(nullptr), output_iter_var_(nullptr) {}

40
void CodeGenOpenGL::InitFuncState(const PrimFunc& f) {
41 42 43 44 45
  CodeGenC::InitFuncState(f);
  output_ = nullptr;
  inputs_.clear();
  output_iter_var_ = nullptr;
  thread_extent_var_ = "";
46 47
  this->decl_stream.str("");
  this->stream.str("");
48 49
}

50
void CodeGenOpenGL::AddFunction(const PrimFunc& f) {
51 52 53 54 55 56 57 58 59 60
  // clear previous generated state.
  this->InitFuncState(f);

  this->decl_stream << "#version 300 es\n";
  this->decl_stream << "precision highp float;\n";

  // skip the first underscore, so SSA variable starts from _1
  GetUniqueName("_");

  // Allocate argument names. Store in `var_idmap_`.
61
  for (auto arg : f->params) {
62 63
    auto arg_name = GetUniqueName(arg.get()->name_hint);
    var_idmap_[arg.get()] = arg_name;
64 65 66 67 68 69

    if (auto* ptr = arg->type_annotation.as<PointerTypeNode>()) {
      if (auto* prim = ptr->element_type.as<PrimTypeNode>()) {
        RegisterHandleType(arg.get(), prim->dtype);
      }
    }
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
  }

  thread_extent_var_ = GetUniqueName("thread_extent");
  this->decl_stream << "uniform int " << thread_extent_var_ << ";\n";

  this->stream << "void main() {\n";

  int func_scope = this->BeginScope();
  this->PrintStmt(f->body);
  this->EndScope(func_scope);

  this->PrintIndent();
  this->stream << "}\n\n";

  // Declare arguments.
85
  for (auto arg : f->params) {
86 87 88 89 90 91 92 93 94 95 96
    if (this->inputs_.find(arg.get()) != this->inputs_.cend()) {
      // Declare input texture.
      // Format:
      // - Float: "uniform sampler2D {name};"
      // - Int: "uniform isampler2D {name};"
      // - UInt: "uniform usampler2D {name};"

      auto arg_name = GetVarID(arg.get());

      auto type_it = this->handle_data_type_.find(arg.get());
      CHECK(type_it != this->handle_data_type_.cend()) << "Cannot find type.";
97
      DLDataType type = type_it->second;
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 131 132
      CHECK_EQ(type.lanes, 1) << "Vector type not supported.";

      switch (type.code) {
        case kDLInt:
          this->decl_stream << "uniform isampler2D " << arg_name << ";\n";
          break;
        case kDLUInt:
          this->decl_stream << "uniform usampler2D " << arg_name << ";\n";
          break;
        case kDLFloat:
          this->decl_stream << "uniform sampler2D " << arg_name << ";\n";
          break;
        default:
          LOG(FATAL) << "Unsupported type code.";
      }

    } else if (this->output_ == arg.get()) {
      // Declare output texture.
      // Format: "out {type} {name};"

      auto arg_name = GetVarID(arg.get());

      auto type_it = this->handle_data_type_.find(arg.get());
      CHECK(type_it != this->handle_data_type_.cend()) << "Cannot find type.";
      auto type = type_it->second;

      this->decl_stream << "out ";
      PrintType(type, this->decl_stream);
      this->decl_stream << " " << arg_name << ";\n";

    } else {
      // Declare uniform value.
      // Format: "uniform {type} {name};"

      auto arg_name = GetVarID(arg.get());
133
      auto type = arg.get()->dtype;
134 135 136 137 138 139 140 141 142

      this->decl_stream << "uniform ";
      PrintType(type, this->decl_stream);
      this->decl_stream << " " << arg_name << ";\n";
    }
  }

  std::vector<std::string> arg_names;
  std::vector<runtime::OpenGLArgKind> arg_kinds;
143
  for (auto arg : f->params) {
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
    std::string name = GetVarID(arg.get());

    runtime::OpenGLArgKind kind;
    if (inputs_.find(arg.get()) != inputs_.cend()) {
      kind = runtime::OpenGLArgKind::kInputTexture;
    } else if (output_ == arg.get()) {
      kind = runtime::OpenGLArgKind::kOutputTexture;
    } else {
      kind = runtime::OpenGLArgKind::kUniform;
    }

    arg_names.push_back(name);
    arg_kinds.push_back(kind);
  }

159
  auto global_symbol = f->GetAttr<String>(tvm::attr::kGlobalSymbol);
160 161 162 163
  CHECK(global_symbol.defined())
      << "CodeGenOpenGL: Expect PrimFunc to have the global_symbol attribute";

  shaders_[static_cast<std::string>(global_symbol)] = runtime::OpenGLShader(
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
      this->decl_stream.str() + this->stream.str(),
      std::move(arg_names), std::move(arg_kinds),
      this->thread_extent_var_);
}

std::unordered_map<std::string, runtime::OpenGLShader> CodeGenOpenGL::Finish() {
  return shaders_;
}

void CodeGenOpenGL::BindThreadIndex(const IterVar& iv) {
  CHECK_EQ(iv->thread_tag, "threadIdx.x") << "Must be threadIdx.x";
  CHECK(var_idmap_.find(iv->var.get()) == var_idmap_.end())
    << "Only support one thread iter var";
  CHECK(output_iter_var_ == nullptr) << "Only support one thread iter var";

  var_idmap_[iv->var.get()] = iv->thread_tag;
  output_iter_var_ = iv->var.get();

  // Declare threadIdx local variable.
  this->PrintIndent();
184 185
  this->stream << "ivec2 threadIdx = ivec2(" << runtime::kTextureRowSize
               << " * int(gl_FragCoord.y) + int(gl_FragCoord.x), 0);\n";
186 187 188 189 190 191 192 193 194 195

  // Return directly if threadIdx.x >= thread_extent.
  this->PrintIndent();
  this->stream << "if (threadIdx.x >= " << thread_extent_var_ << ") {\n";
  this->PrintIndent();
  this->stream << "  return;\n";
  this->PrintIndent();
  this->stream << "}\n";
}

196
void CodeGenOpenGL::VisitStmt_(const StoreNode* op) {
197 198
  LOG(FATAL) << "Store statement not supported in OpenGL."
             << " Texture store should be a Call statement.";
199 200
}

201
// texelFetch(tex, ivec2(idx & kTextureRowMask, idx >> kTextureRowBits), 0).r
202
std::string CodeGenOpenGL::TexelFetch(const VarNode* buffer, PrimExpr index) {
203
  std::ostringstream os;
204
  os << "texelFetch(" << GetVarID(buffer) << ", ivec2(int(";
205
  PrintExpr(index, os);
206 207 208
  os << ") & " << runtime::kTextureRowMask << ", int(";
  PrintExpr(index, os);
  os << ") >> " << runtime::kTextureRowBits << "), 0).r";
209 210 211 212 213 214
  return os.str();
}

// Print a reference expression to a buffer.
// Format: texelFetch(buffer, index, 0).r
std::string CodeGenOpenGL::GetBufferRef(
215
    DataType t, const VarNode* buffer, PrimExpr index) {
216 217 218 219 220 221 222 223 224 225 226 227 228
  CHECK_EQ(t.lanes(), 1) << "Vector type not supported.";
  CHECK(HandleTypeMatch(buffer, t)) << "Type mismatch not supported.";

  if (buffer == this->output_) {
    // This is the output texture.
    return GetVarID(buffer);
  } else {
    // This is an input texture.
    this->inputs_.insert(buffer);
    return TexelFetch(buffer, index);
  }
}

229
void CodeGenOpenGL::PrintType(DataType t, std::ostream& os) {
230
  switch (t.code()) {
231
    case kDLInt:
232 233 234
      CHECK_EQ(t.bits(), 32) << "Only support 32-bit int.";
      os << "int";
      break;
235
    case kDLUInt:
236 237 238
      CHECK_EQ(t.bits(), 32) << "Only support 32-bit uint.";
      os << "uint";
      break;
239
    case kDLFloat:
240 241 242 243 244 245 246 247 248 249
      CHECK_EQ(t.bits(), 32) << "Only support 32-bit float.";
      os << "float";
      break;
    default:
      LOG(FATAL) << "Unsupported type code.";
  }
}

// Codegen for immediate values

250
void CodeGenOpenGL::VisitExpr_(const IntImmNode* op, std::ostream& os) {
251
  CHECK_EQ(op->dtype, DataType::Int(32)) << "GLSL 3.0 only supports 32-bit ints.";
252 253 254
  CodeGenC::VisitExpr_(op, os);
}

255
void CodeGenOpenGL::VisitExpr_(const FloatImmNode* op, std::ostream& os) {
256
  CHECK_EQ(op->dtype, DataType::Float(32)) << "GLSL 3.0 only supports 32-bit floats.";
257 258 259
  CodeGenC::VisitExpr_(op, os);
}

260
void CodeGenOpenGL::VisitExpr_(const StringImmNode*, std::ostream& os) {
261 262 263
  LOG(FATAL) << "GLSL 3.0 doesn't support strings.";
}

264 265 266
void CodeGenOpenGL::VisitStmt_(const EvaluateNode* op) {
  auto call = op->value.as<CallNode>();
  if (call == nullptr || call->name != CallNode::glsl_texture_store) {
267 268 269 270 271
    // Fallback to normal logic.
    CodeGenC::VisitStmt_(op);
  }

  CHECK_EQ(call->args.size(), 2);
272
  auto buffer = call->args[0].as<VarNode>();
273 274 275
  auto value = call->args[1];

  // Doesn't support store to vector.
276
  auto type = value.dtype();
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
  CHECK_EQ(type.lanes(), 1)
    << "Vectorized store not implemented, type = " << type;

  CHECK(inputs_.find(buffer) == inputs_.cend())
    << "Texture has been read from before. Must not store to it.";
  if (output_ == nullptr) {
    output_ = buffer;  // Record that this texture is the output.
  } else {
    CHECK(output_ == buffer) << "GLSL can only write to 1 texture.";
  }

  this->PrintIndent();
  this->stream << GetVarID(buffer) << " = " << PrintExpr(value) << ";\n";
}

292
runtime::Module BuildOpenGL(IRModule mod) {
293 294 295
  bool output_ssa = false;
  CodeGenOpenGL cg;
  cg.Init(output_ssa);
296 297 298 299 300 301 302 303 304

  for (auto kv :  mod->functions) {
    CHECK(kv.second->IsInstance<PrimFuncNode>())
        << "CodeGenOpenGL: Can only take PrimFunc";
    auto f = Downcast<PrimFunc>(kv.second);
    auto calling_conv = f->GetAttr<Integer>(tvm::attr::kCallingConv);
    CHECK(calling_conv.defined() &&
          calling_conv->value == static_cast<int>(CallingConv::kDeviceKernelLaunch))
        << "CodeGenOpenGL: expect calling_conv equals CallingConv::kDeviceKernelLaunch";
305 306
    cg.AddFunction(f);
  }
307

308
  auto shaders = cg.Finish();
309
  return OpenGLModuleCreate(shaders, "gl", ExtractFuncInfo(mod));
310 311
}

312
TVM_REGISTER_GLOBAL("target.build.opengl")
313
.set_body_typed(BuildOpenGL);
314

315 316
}  // namespace codegen
}  // namespace tvm