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
28 namespace {
29
getUnsuffixedStructName(const llvm::StructType * T)30 static inline llvm::StringRef getUnsuffixedStructName(const llvm::StructType *T) {
31 if (!T) {
32 abort(); // exit?
33 }
34 #ifdef _DEBUG
35 // Bug: 22926131
36 // When building with assertions enabled, LLVM cannot read the name of a
37 // literal (anonymous) structure, because they shouldn't actually ever have
38 // a name. Unfortunately, due to past definitions of RenderScript object
39 // types as anonymous structures typedef-ed to their proper typename,
40 // we had been relying on accessing this information. LLVM bitcode retains
41 // the typedef-ed name for such anonymous structures. There is a
42 // corresponding (safe) fix to the RenderScript headers to actually name
43 // these types the same as their typedef name to simplify things. That
44 // fixes this issue going forward, but it won't allow us to compile legacy
45 // code properly. In that case, we just have non-assert builds ignore the
46 // fact that anonymous structures shouldn't have their name read, and do it
47 // anyway. Note that RSCompilerDriver.cpp checks the compiler version
48 // number (from llvm-rs-cc) to ensure that we are only ever building modern
49 // code when we have assertions enabled. Legacy code can only be compiled
50 // correctly with a non-asserting compiler.
51 //
52 // Note that the whole reason for looking at the "unsuffixed" name of the
53 // type is because LLVM suffixes duplicate typedefs of the same anonymous
54 // structure. In the new case, where all of the RS object types have a
55 // proper name, they won't have a dotted suffix at all. We still need
56 // to look at the old unsuffixed version to handle legacy code properly.
57 if (T->isLiteral()) {
58 return "";
59 }
60 #endif
61
62 // Get just the object type name with no suffix.
63 size_t LastDot = T->getName().rfind('.');
64 if (LastDot == strlen("struct")) {
65 // If we get back to just the "struct" part, we know that we had a
66 // raw typename (i.e. struct.rs_element with no ".[0-9]+" suffix on it.
67 // In that case, we will want to create our slice such that it contains
68 // the entire name.
69 LastDot = T->getName().size();
70 }
71 return T->getStructName().slice(0, LastDot);
72 }
73
74 const char kAllocationTypeName[] = "struct.rs_allocation";
75 const char kElementTypeName[] = "struct.rs_element";
76 const char kSamplerTypeName[] = "struct.rs_sampler";
77 const char kScriptTypeName[] = "struct.rs_script";
78 const char kTypeTypeName[] = "struct.rs_type";
79
80 // Returns the RsDataType for a given input LLVM type.
81 // This is only used to distinguish the associated RS object types (i.e.
82 // rs_allocation, rs_element, rs_sampler, rs_script, and rs_type).
83 // All other types are reported back as RS_TYPE_NONE, since no special
84 // handling would be necessary.
getRsDataTypeForType(const llvm::Type * T)85 static inline enum RsDataType getRsDataTypeForType(const llvm::Type *T) {
86 if (T->isStructTy()) {
87 const llvm::StringRef StructName = getUnsuffixedStructName(llvm::dyn_cast<const llvm::StructType>(T));
88 if (StructName.equals(kAllocationTypeName)) {
89 return RS_TYPE_ALLOCATION;
90 } else if (StructName.equals(kElementTypeName)) {
91 return RS_TYPE_ELEMENT;
92 } else if (StructName.equals(kSamplerTypeName)) {
93 return RS_TYPE_SAMPLER;
94 } else if (StructName.equals(kScriptTypeName)) {
95 return RS_TYPE_SCRIPT;
96 } else if (StructName.equals(kTypeTypeName)) {
97 return RS_TYPE_TYPE;
98 }
99 }
100 return RS_TYPE_NONE;
101 }
102
103 // Returns true if the input type is one of our RenderScript object types
104 // (allocation, element, sampler, script, type) and false if it is not.
isRsObjectType(const llvm::Type * T)105 static inline bool isRsObjectType(const llvm::Type *T) {
106 return getRsDataTypeForType(T) != RS_TYPE_NONE;
107 }
108
109 } // end namespace
110
111 // When we have a general reduction kernel with no combiner function,
112 // we will synthesize a combiner function from the accumulator
113 // function. Given the accumulator function name, what should be the
114 // name of the combiner function?
nameReduceCombinerFromAccumulator(llvm::StringRef accumName)115 static inline std::string nameReduceCombinerFromAccumulator(llvm::StringRef accumName) {
116 return std::string(accumName) + ".combiner";
117 }
118
119 #endif // BCC_RS_UTILS_H
120