1/*
2 * Copyright 2015, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef BCC_RS_UTILS_H
18#define BCC_RS_UTILS_H
19
20#include "rsDefines.h"
21
22#include <llvm/IR/Type.h>
23#include <llvm/IR/DerivedTypes.h>
24#include <llvm/ADT/StringRef.h>
25
26#include <string>
27
28namespace {
29
30static inline llvm::StringRef getUnsuffixedStructName(const llvm::StructType *T) {
31#ifdef _DEBUG
32  // Bug: 22926131
33  // When building with assertions enabled, LLVM cannot read the name of a
34  // literal (anonymous) structure, because they shouldn't actually ever have
35  // a name. Unfortunately, due to past definitions of RenderScript object
36  // types as anonymous structures typedef-ed to their proper typename,
37  // we had been relying on accessing this information. LLVM bitcode retains
38  // the typedef-ed name for such anonymous structures. There is a
39  // corresponding (safe) fix to the RenderScript headers to actually name
40  // these types the same as their typedef name to simplify things. That
41  // fixes this issue going forward, but it won't allow us to compile legacy
42  // code properly. In that case, we just have non-assert builds ignore the
43  // fact that anonymous structures shouldn't have their name read, and do it
44  // anyway. Note that RSCompilerDriver.cpp checks the compiler version
45  // number (from llvm-rs-cc) to ensure that we are only ever building modern
46  // code when we have assertions enabled. Legacy code can only be compiled
47  // correctly with a non-asserting compiler.
48  //
49  // Note that the whole reason for looking at the "unsuffixed" name of the
50  // type is because LLVM suffixes duplicate typedefs of the same anonymous
51  // structure. In the new case, where all of the RS object types have a
52  // proper name, they won't have a dotted suffix at all. We still need
53  // to look at the old unsuffixed version to handle legacy code properly.
54  if (T->isLiteral()) {
55    return "";
56  }
57#endif
58
59  // Get just the object type name with no suffix.
60  size_t LastDot = T->getName().rfind('.');
61  if (LastDot == strlen("struct")) {
62    // If we get back to just the "struct" part, we know that we had a
63    // raw typename (i.e. struct.rs_element with no ".[0-9]+" suffix on it.
64    // In that case, we will want to create our slice such that it contains
65    // the entire name.
66    LastDot = T->getName().size();
67  }
68  return T->getStructName().slice(0, LastDot);
69}
70
71const char kAllocationTypeName[] = "struct.rs_allocation";
72const char kElementTypeName[]    = "struct.rs_element";
73const char kSamplerTypeName[]    = "struct.rs_sampler";
74const char kScriptTypeName[]     = "struct.rs_script";
75const char kTypeTypeName[]       = "struct.rs_type";
76
77// Returns the RsDataType for a given input LLVM type.
78// This is only used to distinguish the associated RS object types (i.e.
79// rs_allocation, rs_element, rs_sampler, rs_script, and rs_type).
80// All other types are reported back as RS_TYPE_NONE, since no special
81// handling would be necessary.
82static inline enum RsDataType getRsDataTypeForType(const llvm::Type *T) {
83  if (T->isStructTy()) {
84    const llvm::StringRef StructName = getUnsuffixedStructName(llvm::dyn_cast<const llvm::StructType>(T));
85    if (StructName.equals(kAllocationTypeName)) {
86      return RS_TYPE_ALLOCATION;
87    } else if (StructName.equals(kElementTypeName)) {
88      return RS_TYPE_ELEMENT;
89    } else if (StructName.equals(kSamplerTypeName)) {
90      return RS_TYPE_SAMPLER;
91    } else if (StructName.equals(kScriptTypeName)) {
92      return RS_TYPE_SCRIPT;
93    } else if (StructName.equals(kTypeTypeName)) {
94      return RS_TYPE_TYPE;
95    }
96  }
97  return RS_TYPE_NONE;
98}
99
100// Returns true if the input type is one of our RenderScript object types
101// (allocation, element, sampler, script, type) and false if it is not.
102static inline bool isRsObjectType(const llvm::Type *T) {
103  return getRsDataTypeForType(T) != RS_TYPE_NONE;
104}
105
106}  // end namespace
107
108// When we have a general reduction kernel with no combiner function,
109// we will synthesize a combiner function from the accumulator
110// function.  Given the accumulator function name, what should be the
111// name of the combiner function?
112static inline std::string nameReduceCombinerFromAccumulator(llvm::StringRef accumName) {
113  return std::string(accumName) + ".combiner";
114}
115
116#endif // BCC_RS_UTILS_H
117