Zonr Chang | f74ee19 | 2012-04-12 15:34:58 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012, 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 | |
Jean-Luc Brouillet | a2dd52f | 2017-02-16 20:57:26 -0800 | [diff] [blame] | 17 | #include "bcc/CompilerConfig.h" |
| 18 | |
| 19 | #include "Log.h" |
| 20 | #include "Properties.h" |
| 21 | |
| 22 | #include "bcc/Config.h" |
Zonr Chang | f74ee19 | 2012-04-12 15:34:58 +0800 | [diff] [blame] | 23 | |
| 24 | #include <llvm/CodeGen/SchedulerRegistry.h> |
| 25 | #include <llvm/MC/SubtargetFeature.h> |
Stephen Hines | bde1a25 | 2014-05-15 18:02:33 -0700 | [diff] [blame] | 26 | #include <llvm/Support/Host.h> |
Zonr Chang | f74ee19 | 2012-04-12 15:34:58 +0800 | [diff] [blame] | 27 | #include <llvm/Support/TargetRegistry.h> |
| 28 | |
Zonr Chang | f74ee19 | 2012-04-12 15:34:58 +0800 | [diff] [blame] | 29 | using namespace bcc; |
| 30 | |
Pirama Arumuga Nainar | 70e3dcf | 2015-04-07 10:50:39 -0700 | [diff] [blame] | 31 | #if defined (PROVIDE_X86_CODEGEN) && !defined(__HOST__) |
| 32 | |
| 33 | namespace { |
| 34 | |
Jean-Luc Brouillet | 5379131 | 2017-02-21 23:03:29 -0800 | [diff] [blame] | 35 | // Utility function to add feature flags supported by the running CPU. |
| 36 | // This function is only needed for on-device bcc for x86. |
| 37 | void AddX86NativeCPUFeatures(std::vector<std::string>* attributes) { |
Pirama Arumuga Nainar | 70e3dcf | 2015-04-07 10:50:39 -0700 | [diff] [blame] | 38 | llvm::StringMap<bool> features; |
Jean-Luc Brouillet | 5379131 | 2017-02-21 23:03:29 -0800 | [diff] [blame] | 39 | if (llvm::sys::getHostCPUFeatures(features)) { |
| 40 | for (const auto& f : features) |
| 41 | attributes->push_back((f.second ? '+' : '-') + f.first().str()); |
| 42 | } |
Pirama Arumuga Nainar | 70e3dcf | 2015-04-07 10:50:39 -0700 | [diff] [blame] | 43 | |
Jean-Luc Brouillet | 5379131 | 2017-02-21 23:03:29 -0800 | [diff] [blame] | 44 | // LLVM generates AVX code that treats a long3 as 256 bits, while |
| 45 | // RenderScript considers a long3 192 bits (http://b/28879581) |
| 46 | attributes->push_back("-avx"); |
Pirama Arumuga Nainar | 70e3dcf | 2015-04-07 10:50:39 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | } |
| 50 | #endif // (PROVIDE_X86_CODEGEN) && !defined(__HOST__) |
| 51 | |
Zonr Chang | f74ee19 | 2012-04-12 15:34:58 +0800 | [diff] [blame] | 52 | CompilerConfig::CompilerConfig(const std::string &pTriple) |
Chris Wailes | 900c6c1 | 2014-08-13 15:40:00 -0700 | [diff] [blame] | 53 | : mTriple(pTriple), mFullPrecision(true), mTarget(nullptr) { |
Zonr Chang | f74ee19 | 2012-04-12 15:34:58 +0800 | [diff] [blame] | 54 | //===--------------------------------------------------------------------===// |
Zonr Chang | f74ee19 | 2012-04-12 15:34:58 +0800 | [diff] [blame] | 55 | // Default setting of target options |
| 56 | //===--------------------------------------------------------------------===// |
Pirama Arumuga Nainar | 86e5c86 | 2015-06-30 19:48:46 -0700 | [diff] [blame] | 57 | |
| 58 | // Use soft-float ABI. This only selects the ABI (and is applicable only to |
| 59 | // ARM targets). Codegen still uses hardware FPU by default. To use software |
| 60 | // floating point, add 'soft-float' feature to mFeatureString below. |
Zonr Chang | f74ee19 | 2012-04-12 15:34:58 +0800 | [diff] [blame] | 61 | mTargetOpts.FloatABIType = llvm::FloatABI::Soft; |
Zonr Chang | f74ee19 | 2012-04-12 15:34:58 +0800 | [diff] [blame] | 62 | |
Zonr Chang | f74ee19 | 2012-04-12 15:34:58 +0800 | [diff] [blame] | 63 | //===--------------------------------------------------------------------===// |
| 64 | // Default setting for code model |
| 65 | //===--------------------------------------------------------------------===// |
| 66 | mCodeModel = llvm::CodeModel::Small; |
| 67 | |
| 68 | //===--------------------------------------------------------------------===// |
Zonr Chang | f74ee19 | 2012-04-12 15:34:58 +0800 | [diff] [blame] | 69 | // Default setting for optimization level (-O2) |
| 70 | //===--------------------------------------------------------------------===// |
| 71 | mOptLevel = llvm::CodeGenOpt::Default; |
| 72 | |
| 73 | //===--------------------------------------------------------------------===// |
| 74 | // Default setting for architecture type |
| 75 | //===--------------------------------------------------------------------===// |
| 76 | mArchType = llvm::Triple::UnknownArch; |
| 77 | |
| 78 | initializeTarget(); |
| 79 | initializeArch(); |
| 80 | |
| 81 | return; |
| 82 | } |
| 83 | |
| 84 | bool CompilerConfig::initializeTarget() { |
| 85 | std::string error; |
| 86 | mTarget = llvm::TargetRegistry::lookupTarget(mTriple, error); |
Chris Wailes | 900c6c1 | 2014-08-13 15:40:00 -0700 | [diff] [blame] | 87 | if (mTarget != nullptr) { |
Zonr Chang | f74ee19 | 2012-04-12 15:34:58 +0800 | [diff] [blame] | 88 | return true; |
| 89 | } else { |
| 90 | ALOGE("Cannot initialize llvm::Target for given triple '%s'! (%s)", |
| 91 | mTriple.c_str(), error.c_str()); |
| 92 | return false; |
| 93 | } |
| 94 | } |
| 95 | |
Stephen Hines | bde1a25 | 2014-05-15 18:02:33 -0700 | [diff] [blame] | 96 | bool CompilerConfig::initializeArch() { |
Chris Wailes | 900c6c1 | 2014-08-13 15:40:00 -0700 | [diff] [blame] | 97 | if (mTarget != nullptr) { |
Zonr Chang | f74ee19 | 2012-04-12 15:34:58 +0800 | [diff] [blame] | 98 | mArchType = llvm::Triple::getArchTypeForLLVMName(mTarget->getName()); |
| 99 | } else { |
| 100 | mArchType = llvm::Triple::UnknownArch; |
Stephen Hines | bde1a25 | 2014-05-15 18:02:33 -0700 | [diff] [blame] | 101 | return false; |
Zonr Chang | f74ee19 | 2012-04-12 15:34:58 +0800 | [diff] [blame] | 102 | } |
Stephen Hines | bde1a25 | 2014-05-15 18:02:33 -0700 | [diff] [blame] | 103 | |
| 104 | // Configure each architecture for any necessary additional flags. |
Luke Drummond | 3c05a9c | 2015-11-11 18:54:41 +0000 | [diff] [blame] | 105 | std::vector<std::string> attributes; |
Stephen Hines | bde1a25 | 2014-05-15 18:02:33 -0700 | [diff] [blame] | 106 | switch (mArchType) { |
| 107 | #if defined(PROVIDE_ARM_CODEGEN) |
| 108 | case llvm::Triple::arm: { |
| 109 | llvm::StringMap<bool> features; |
| 110 | llvm::sys::getHostCPUFeatures(features); |
Stephen Hines | bde1a25 | 2014-05-15 18:02:33 -0700 | [diff] [blame] | 111 | |
| 112 | #if defined(__HOST__) || defined(ARCH_ARM_HAVE_VFP) |
| 113 | attributes.push_back("+vfp3"); |
| 114 | #if !defined(__HOST__) && !defined(ARCH_ARM_HAVE_VFP_D32) |
| 115 | attributes.push_back("+d16"); |
| 116 | #endif // !__HOST__ && !ARCH_ARM_HAVE_VFP_D32 |
| 117 | #endif // __HOST__ || ARCH_ARM_HAVE_VFP |
| 118 | |
| 119 | #if defined(__HOST__) || defined(ARCH_ARM_HAVE_NEON) |
| 120 | // Only enable NEON on ARM if we have relaxed precision floats. |
| 121 | if (!mFullPrecision) { |
| 122 | attributes.push_back("+neon"); |
| 123 | } else { |
| 124 | #endif // __HOST__ || ARCH_ARM_HAVE_NEON |
| 125 | attributes.push_back("-neon"); |
| 126 | attributes.push_back("-neonfp"); |
| 127 | #if defined(__HOST__) || defined(ARCH_ARM_HAVE_NEON) |
| 128 | } |
| 129 | #endif // __HOST__ || ARCH_ARM_HAVE_NEON |
| 130 | |
| 131 | if (!getProperty("debug.rs.arm-no-hwdiv")) { |
| 132 | if (features.count("hwdiv-arm") && features["hwdiv-arm"]) |
| 133 | attributes.push_back("+hwdiv-arm"); |
| 134 | |
| 135 | if (features.count("hwdiv") && features["hwdiv"]) |
| 136 | attributes.push_back("+hwdiv"); |
| 137 | } |
| 138 | |
Pirama Arumuga Nainar | 70e3dcf | 2015-04-07 10:50:39 -0700 | [diff] [blame] | 139 | // Enable fp16 attribute if available in the feature list. This feature |
| 140 | // will not be added in the host version of bcc or bcc_compat since |
| 141 | // 'features' would correspond to features in an x86 host. |
| 142 | if (features.count("fp16") && features["fp16"]) |
| 143 | attributes.push_back("+fp16"); |
| 144 | |
Pirama Arumuga Nainar | ae3184b | 2016-03-23 20:40:27 -0700 | [diff] [blame] | 145 | #if defined(PROVIDE_ARM64_CODEGEN) |
| 146 | // On AArch64, asimd in /proc/cpuinfo signals the presence of hardware |
| 147 | // half-precision conversion instructions. getHostCPUFeatures translates |
| 148 | // this to "neon". If PROVIDE_ARM64_CODEGEN is set, enable "+fp16" for ARM |
| 149 | // codegen if "neon" is present in features. |
| 150 | if (features.count("neon") && features["neon"]) |
| 151 | attributes.push_back("+fp16"); |
| 152 | #endif // PROVIDE_ARM64_CODEGEN |
Stephen Hines | bde1a25 | 2014-05-15 18:02:33 -0700 | [diff] [blame] | 153 | |
| 154 | #if defined(TARGET_BUILD) |
| 155 | if (!getProperty("debug.rs.arm-no-tune-for-cpu")) { |
Yong Chen | d856316 | 2015-07-20 17:28:19 +0800 | [diff] [blame] | 156 | #ifdef DEFAULT_ARM_CODEGEN |
Stephen Hines | bde1a25 | 2014-05-15 18:02:33 -0700 | [diff] [blame] | 157 | setCPU(llvm::sys::getHostCPUName()); |
Yong Chen | d856316 | 2015-07-20 17:28:19 +0800 | [diff] [blame] | 158 | #endif |
Stephen Hines | bde1a25 | 2014-05-15 18:02:33 -0700 | [diff] [blame] | 159 | } |
| 160 | #endif // TARGET_BUILD |
| 161 | |
| 162 | break; |
| 163 | } |
| 164 | #endif // PROVIDE_ARM_CODEGEN |
| 165 | |
| 166 | #if defined(PROVIDE_ARM64_CODEGEN) |
| 167 | case llvm::Triple::aarch64: |
| 168 | #if defined(TARGET_BUILD) |
| 169 | if (!getProperty("debug.rs.arm-no-tune-for-cpu")) { |
Yong Chen | d856316 | 2015-07-20 17:28:19 +0800 | [diff] [blame] | 170 | #ifdef DEFAULT_ARM64_CODEGEN |
Stephen Hines | bde1a25 | 2014-05-15 18:02:33 -0700 | [diff] [blame] | 171 | setCPU(llvm::sys::getHostCPUName()); |
Yong Chen | d856316 | 2015-07-20 17:28:19 +0800 | [diff] [blame] | 172 | #endif |
Stephen Hines | bde1a25 | 2014-05-15 18:02:33 -0700 | [diff] [blame] | 173 | } |
| 174 | #endif // TARGET_BUILD |
| 175 | break; |
| 176 | #endif // PROVIDE_ARM64_CODEGEN |
| 177 | |
| 178 | #if defined (PROVIDE_MIPS_CODEGEN) |
| 179 | case llvm::Triple::mips: |
| 180 | case llvm::Triple::mipsel: |
Pirama Arumuga Nainar | f5b49a0 | 2016-09-15 23:04:25 -0700 | [diff] [blame] | 181 | if (!mRelocModel.hasValue()) { |
| 182 | mRelocModel = llvm::Reloc::Static; |
Stephen Hines | a16a916 | 2014-05-30 13:19:07 -0700 | [diff] [blame] | 183 | } |
Stephen Hines | bde1a25 | 2014-05-15 18:02:33 -0700 | [diff] [blame] | 184 | break; |
| 185 | #endif // PROVIDE_MIPS_CODEGEN |
| 186 | |
Petar Jovanovic | 55e1178 | 2014-07-10 19:08:03 +0200 | [diff] [blame] | 187 | #if defined (PROVIDE_MIPS64_CODEGEN) |
| 188 | case llvm::Triple::mips64: |
| 189 | case llvm::Triple::mips64el: |
Petar Jovanovic | 4171e69 | 2014-10-17 20:24:02 +0200 | [diff] [blame] | 190 | // Default revision for MIPS64 Android is R6. |
| 191 | setCPU("mips64r6"); |
Petar Jovanovic | 55e1178 | 2014-07-10 19:08:03 +0200 | [diff] [blame] | 192 | break; |
| 193 | #endif // PROVIDE_MIPS64_CODEGEN |
| 194 | |
Stephen Hines | bde1a25 | 2014-05-15 18:02:33 -0700 | [diff] [blame] | 195 | #if defined (PROVIDE_X86_CODEGEN) |
| 196 | case llvm::Triple::x86: |
Stephen Hines | bde1a25 | 2014-05-15 18:02:33 -0700 | [diff] [blame] | 197 | getTargetOptions().UseInitArray = true; |
Jean-Luc Brouillet | 5379131 | 2017-02-21 23:03:29 -0800 | [diff] [blame] | 198 | #if defined (DEFAULT_X86_CODEGEN) && !defined (__HOST__) |
Luke Drummond | 3c05a9c | 2015-11-11 18:54:41 +0000 | [diff] [blame] | 199 | setCPU(llvm::sys::getHostCPUName()); |
Jean-Luc Brouillet | 5379131 | 2017-02-21 23:03:29 -0800 | [diff] [blame] | 200 | AddX86NativeCPUFeatures(&attributes); |
Luke Drummond | 3c05a9c | 2015-11-11 18:54:41 +0000 | [diff] [blame] | 201 | #else |
| 202 | // generic fallback for 32bit x86 targets |
| 203 | setCPU("atom"); |
Jean-Luc Brouillet | 5379131 | 2017-02-21 23:03:29 -0800 | [diff] [blame] | 204 | #endif // DEFAULT_X86_CODEGEN && !__HOST__ |
Stephen Hines | bde1a25 | 2014-05-15 18:02:33 -0700 | [diff] [blame] | 205 | break; |
| 206 | #endif // PROVIDE_X86_CODEGEN |
| 207 | |
| 208 | #if defined (PROVIDE_X86_CODEGEN) |
Luke Drummond | 3c05a9c | 2015-11-11 18:54:41 +0000 | [diff] [blame] | 209 | // PROVIDE_X86_CODEGEN is defined for both x86 and x86_64 |
Stephen Hines | bde1a25 | 2014-05-15 18:02:33 -0700 | [diff] [blame] | 210 | case llvm::Triple::x86_64: |
Miao Wang | 4e39604 | 2016-03-31 18:34:19 -0700 | [diff] [blame] | 211 | #if defined(DEFAULT_X86_64_CODEGEN) && !defined(__HOST__) |
Luke Drummond | 3c05a9c | 2015-11-11 18:54:41 +0000 | [diff] [blame] | 212 | setCPU(llvm::sys::getHostCPUName()); |
Jean-Luc Brouillet | 5379131 | 2017-02-21 23:03:29 -0800 | [diff] [blame] | 213 | AddX86NativeCPUFeatures(&attributes); |
Luke Drummond | 3c05a9c | 2015-11-11 18:54:41 +0000 | [diff] [blame] | 214 | #else |
| 215 | // generic fallback for 64bit x86 targets |
| 216 | setCPU("core2"); |
| 217 | #endif |
Yong Chen | 166b8d0 | 2015-05-08 19:19:45 +0800 | [diff] [blame] | 218 | // x86_64 needs small CodeModel if use PIC_ reloc, or else dlopen failed with TEXTREL. |
Pirama Arumuga Nainar | f5b49a0 | 2016-09-15 23:04:25 -0700 | [diff] [blame] | 219 | if (mRelocModel.hasValue() && mRelocModel.getValue() == llvm::Reloc::PIC_) { |
Yong Chen | 166b8d0 | 2015-05-08 19:19:45 +0800 | [diff] [blame] | 220 | setCodeModel(llvm::CodeModel::Small); |
| 221 | } else { |
| 222 | setCodeModel(llvm::CodeModel::Medium); |
| 223 | } |
Stephen Hines | bde1a25 | 2014-05-15 18:02:33 -0700 | [diff] [blame] | 224 | getTargetOptions().UseInitArray = true; |
| 225 | break; |
| 226 | #endif // PROVIDE_X86_CODEGEN |
| 227 | |
| 228 | default: |
| 229 | ALOGE("Unsupported architecture type: %s", mTarget->getName()); |
| 230 | return false; |
| 231 | } |
| 232 | |
Luke Drummond | 3c05a9c | 2015-11-11 18:54:41 +0000 | [diff] [blame] | 233 | setFeatureString(attributes); |
Stephen Hines | bde1a25 | 2014-05-15 18:02:33 -0700 | [diff] [blame] | 234 | return true; |
Zonr Chang | f74ee19 | 2012-04-12 15:34:58 +0800 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | void CompilerConfig::setFeatureString(const std::vector<std::string> &pAttrs) { |
| 238 | llvm::SubtargetFeatures f; |
| 239 | |
| 240 | for (std::vector<std::string>::const_iterator attr_iter = pAttrs.begin(), |
| 241 | attr_end = pAttrs.end(); |
| 242 | attr_iter != attr_end; attr_iter++) { |
| 243 | f.AddFeature(*attr_iter); |
| 244 | } |
| 245 | |
| 246 | mFeatureString = f.getString(); |
| 247 | return; |
| 248 | } |