summaryrefslogtreecommitdiff
path: root/rsov/compiler/KernelSignature.cpp
blob: 6367ed4c4019e3f382f54aee0d32f2f0d1886f67 (plain)
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
/*
 * Copyright 2017, The Android Open Source Project
 *
 * 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.
 */

#include "KernelSignature.h"

#include "llvm/Support/Debug.h"
#include "llvm/Support/raw_ostream.h"

using namespace llvm;

namespace {
std::string TypeToString(const Type *Ty) {
  assert(Ty);
  if (Ty->isVoidTy())
    return "void";

  if (auto *IT = dyn_cast<IntegerType>(Ty)) {
    if (IT->getBitWidth() == 32)
      return "int";
    else if (IT->getBitWidth() == 8)
      return "uchar";
  }

  if (Ty->isFloatTy())
    return "float";

  if (auto *VT = dyn_cast<VectorType>(Ty)) {
    auto *ET = VT->getElementType();
    if (auto *IT = dyn_cast<IntegerType>(ET)) {
      if (IT->getBitWidth() == 32)
        return "int4";
      else if (IT->getBitWidth() == 8)
        return "uchar4";
    }
    if (ET->isFloatTy())
      return "float4";
  }

  std::string badNameString;
  raw_string_ostream badNameStream(badNameString);
  badNameStream << '[';
  Ty->print(badNameStream);
  badNameStream << ']';
  return badNameStream.str();
}
} // namespace

namespace rs2spirv {

const std::string KernelSignature::wrapperPrefix = "%__rsov_";

KernelSignature::KernelSignature(const FunctionType *FT,
                                 const std::string Fname, Coords CK)
    : returnType(TypeToString(FT->getReturnType())), name(Fname),
      coordsKind(CK) {
  for (const auto *ArgT : FT->params()) {
    argumentTypes.push_back(TypeToString(ArgT));
  }
  // Drop special arguments
  // TODO: handle all special argument cases.
  argumentTypes.resize(argumentTypes.size()-size_t(CK));
}

void KernelSignature::dump() const {
  dbgs() << returnType << ' ' << name << '(' << argumentTypes[0];
  const auto CoordsNum = size_t(coordsKind);
  for (size_t i = 0; i != CoordsNum; ++i)
    dbgs() << ", " << CoordsNames[i];

  dbgs() << ")\n";
}

} // namespace rs2spirv