blob: e7ce1b518ceaef6b6bb40c547a835058b1f8d53f [file] [log] [blame]
Stephen Hinesfb81ec12015-05-18 20:04:23 -07001/*
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>
David Gross23c2bfe2015-06-22 14:19:00 -070023#include <llvm/IR/DerivedTypes.h>
David Gross57fd9f82016-04-08 12:35:41 -070024#include <llvm/ADT/StringRef.h>
25
26#include <string>
Stephen Hinesfb81ec12015-05-18 20:04:23 -070027
28namespace {
29
David Gross23c2bfe2015-06-22 14:19:00 -070030static inline llvm::StringRef getUnsuffixedStructName(const llvm::StructType *T) {
Colin Cross1d3ac562016-12-07 14:21:42 -080031#ifdef _DEBUG
Stephen Hinesc31e1012015-08-25 23:10:18 -070032 // 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
David Gross23c2bfe2015-06-22 14:19:00 -070059 // 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
Stephen Hinesfb81ec12015-05-18 20:04:23 -070071const 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()) {
David Gross23c2bfe2015-06-22 14:19:00 -070084 const llvm::StringRef StructName = getUnsuffixedStructName(llvm::dyn_cast<const llvm::StructType>(T));
Stephen Hinesfb81ec12015-05-18 20:04:23 -070085 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
Stephen Hinesabfa7852015-05-22 19:43:10 -0700100// 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
Stephen Hinesfb81ec12015-05-18 20:04:23 -0700106} // end namespace
107
David Gross57fd9f82016-04-08 12:35:41 -0700108// 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?
David Grossa48ea362016-06-02 14:46:55 -0700112static inline std::string nameReduceCombinerFromAccumulator(llvm::StringRef accumName) {
David Gross57fd9f82016-04-08 12:35:41 -0700113 return std::string(accumName) + ".combiner";
114}
115
Stephen Hinesfb81ec12015-05-18 20:04:23 -0700116#endif // BCC_RS_UTILS_H