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