blob: 3ba849870b90c0addc8b87b096ca807615c32185 [file] [log] [blame]
Zonr Changf74ee192012-04-12 15:34:58 +08001/*
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 Brouilleta2dd52f2017-02-16 20:57:26 -080017#include "bcc/CompilerConfig.h"
18
19#include "Log.h"
20#include "Properties.h"
21
22#include "bcc/Config.h"
Zonr Changf74ee192012-04-12 15:34:58 +080023
24#include <llvm/CodeGen/SchedulerRegistry.h>
25#include <llvm/MC/SubtargetFeature.h>
Stephen Hinesbde1a252014-05-15 18:02:33 -070026#include <llvm/Support/Host.h>
Zonr Changf74ee192012-04-12 15:34:58 +080027#include <llvm/Support/TargetRegistry.h>
28
Zonr Changf74ee192012-04-12 15:34:58 +080029using namespace bcc;
30
Pirama Arumuga Nainar70e3dcf2015-04-07 10:50:39 -070031#if defined (PROVIDE_X86_CODEGEN) && !defined(__HOST__)
32
33namespace {
34
Jean-Luc Brouillet53791312017-02-21 23:03:29 -080035// Utility function to add feature flags supported by the running CPU.
36// This function is only needed for on-device bcc for x86.
37void AddX86NativeCPUFeatures(std::vector<std::string>* attributes) {
Pirama Arumuga Nainar70e3dcf2015-04-07 10:50:39 -070038 llvm::StringMap<bool> features;
Jean-Luc Brouillet53791312017-02-21 23:03:29 -080039 if (llvm::sys::getHostCPUFeatures(features)) {
40 for (const auto& f : features)
41 attributes->push_back((f.second ? '+' : '-') + f.first().str());
42 }
Pirama Arumuga Nainar70e3dcf2015-04-07 10:50:39 -070043
Jean-Luc Brouillet53791312017-02-21 23:03:29 -080044 // 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 Nainar70e3dcf2015-04-07 10:50:39 -070047}
48
49}
50#endif // (PROVIDE_X86_CODEGEN) && !defined(__HOST__)
51
Zonr Changf74ee192012-04-12 15:34:58 +080052CompilerConfig::CompilerConfig(const std::string &pTriple)
Chris Wailes900c6c12014-08-13 15:40:00 -070053 : mTriple(pTriple), mFullPrecision(true), mTarget(nullptr) {
Zonr Changf74ee192012-04-12 15:34:58 +080054 //===--------------------------------------------------------------------===//
Zonr Changf74ee192012-04-12 15:34:58 +080055 // Default setting of target options
56 //===--------------------------------------------------------------------===//
Pirama Arumuga Nainar86e5c862015-06-30 19:48:46 -070057
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 Changf74ee192012-04-12 15:34:58 +080061 mTargetOpts.FloatABIType = llvm::FloatABI::Soft;
Zonr Changf74ee192012-04-12 15:34:58 +080062
Zonr Changf74ee192012-04-12 15:34:58 +080063 //===--------------------------------------------------------------------===//
64 // Default setting for code model
65 //===--------------------------------------------------------------------===//
66 mCodeModel = llvm::CodeModel::Small;
67
68 //===--------------------------------------------------------------------===//
Zonr Changf74ee192012-04-12 15:34:58 +080069 // 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
84bool CompilerConfig::initializeTarget() {
85 std::string error;
86 mTarget = llvm::TargetRegistry::lookupTarget(mTriple, error);
Chris Wailes900c6c12014-08-13 15:40:00 -070087 if (mTarget != nullptr) {
Zonr Changf74ee192012-04-12 15:34:58 +080088 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 Hinesbde1a252014-05-15 18:02:33 -070096bool CompilerConfig::initializeArch() {
Chris Wailes900c6c12014-08-13 15:40:00 -070097 if (mTarget != nullptr) {
Zonr Changf74ee192012-04-12 15:34:58 +080098 mArchType = llvm::Triple::getArchTypeForLLVMName(mTarget->getName());
99 } else {
100 mArchType = llvm::Triple::UnknownArch;
Stephen Hinesbde1a252014-05-15 18:02:33 -0700101 return false;
Zonr Changf74ee192012-04-12 15:34:58 +0800102 }
Stephen Hinesbde1a252014-05-15 18:02:33 -0700103
104 // Configure each architecture for any necessary additional flags.
Luke Drummond3c05a9c2015-11-11 18:54:41 +0000105 std::vector<std::string> attributes;
Stephen Hinesbde1a252014-05-15 18:02:33 -0700106 switch (mArchType) {
107#if defined(PROVIDE_ARM_CODEGEN)
108 case llvm::Triple::arm: {
109 llvm::StringMap<bool> features;
110 llvm::sys::getHostCPUFeatures(features);
Stephen Hinesbde1a252014-05-15 18:02:33 -0700111
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 Nainar70e3dcf2015-04-07 10:50:39 -0700139 // 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 Nainarae3184b2016-03-23 20:40:27 -0700145#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 Hinesbde1a252014-05-15 18:02:33 -0700153
154#if defined(TARGET_BUILD)
155 if (!getProperty("debug.rs.arm-no-tune-for-cpu")) {
Yong Chend8563162015-07-20 17:28:19 +0800156#ifdef DEFAULT_ARM_CODEGEN
Stephen Hinesbde1a252014-05-15 18:02:33 -0700157 setCPU(llvm::sys::getHostCPUName());
Yong Chend8563162015-07-20 17:28:19 +0800158#endif
Stephen Hinesbde1a252014-05-15 18:02:33 -0700159 }
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 Chend8563162015-07-20 17:28:19 +0800170#ifdef DEFAULT_ARM64_CODEGEN
Stephen Hinesbde1a252014-05-15 18:02:33 -0700171 setCPU(llvm::sys::getHostCPUName());
Yong Chend8563162015-07-20 17:28:19 +0800172#endif
Stephen Hinesbde1a252014-05-15 18:02:33 -0700173 }
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 Nainarf5b49a02016-09-15 23:04:25 -0700181 if (!mRelocModel.hasValue()) {
182 mRelocModel = llvm::Reloc::Static;
Stephen Hinesa16a9162014-05-30 13:19:07 -0700183 }
Stephen Hinesbde1a252014-05-15 18:02:33 -0700184 break;
185#endif // PROVIDE_MIPS_CODEGEN
186
Petar Jovanovic55e11782014-07-10 19:08:03 +0200187#if defined (PROVIDE_MIPS64_CODEGEN)
188 case llvm::Triple::mips64:
189 case llvm::Triple::mips64el:
Petar Jovanovic4171e692014-10-17 20:24:02 +0200190 // Default revision for MIPS64 Android is R6.
191 setCPU("mips64r6");
Petar Jovanovic55e11782014-07-10 19:08:03 +0200192 break;
193#endif // PROVIDE_MIPS64_CODEGEN
194
Stephen Hinesbde1a252014-05-15 18:02:33 -0700195#if defined (PROVIDE_X86_CODEGEN)
196 case llvm::Triple::x86:
Stephen Hinesbde1a252014-05-15 18:02:33 -0700197 getTargetOptions().UseInitArray = true;
Jean-Luc Brouillet53791312017-02-21 23:03:29 -0800198#if defined (DEFAULT_X86_CODEGEN) && !defined (__HOST__)
Luke Drummond3c05a9c2015-11-11 18:54:41 +0000199 setCPU(llvm::sys::getHostCPUName());
Jean-Luc Brouillet53791312017-02-21 23:03:29 -0800200 AddX86NativeCPUFeatures(&attributes);
Luke Drummond3c05a9c2015-11-11 18:54:41 +0000201#else
202 // generic fallback for 32bit x86 targets
203 setCPU("atom");
Jean-Luc Brouillet53791312017-02-21 23:03:29 -0800204#endif // DEFAULT_X86_CODEGEN && !__HOST__
Stephen Hinesbde1a252014-05-15 18:02:33 -0700205 break;
206#endif // PROVIDE_X86_CODEGEN
207
208#if defined (PROVIDE_X86_CODEGEN)
Luke Drummond3c05a9c2015-11-11 18:54:41 +0000209// PROVIDE_X86_CODEGEN is defined for both x86 and x86_64
Stephen Hinesbde1a252014-05-15 18:02:33 -0700210 case llvm::Triple::x86_64:
Miao Wang4e396042016-03-31 18:34:19 -0700211#if defined(DEFAULT_X86_64_CODEGEN) && !defined(__HOST__)
Luke Drummond3c05a9c2015-11-11 18:54:41 +0000212 setCPU(llvm::sys::getHostCPUName());
Jean-Luc Brouillet53791312017-02-21 23:03:29 -0800213 AddX86NativeCPUFeatures(&attributes);
Luke Drummond3c05a9c2015-11-11 18:54:41 +0000214#else
215 // generic fallback for 64bit x86 targets
216 setCPU("core2");
217#endif
Yong Chen166b8d02015-05-08 19:19:45 +0800218 // x86_64 needs small CodeModel if use PIC_ reloc, or else dlopen failed with TEXTREL.
Pirama Arumuga Nainarf5b49a02016-09-15 23:04:25 -0700219 if (mRelocModel.hasValue() && mRelocModel.getValue() == llvm::Reloc::PIC_) {
Yong Chen166b8d02015-05-08 19:19:45 +0800220 setCodeModel(llvm::CodeModel::Small);
221 } else {
222 setCodeModel(llvm::CodeModel::Medium);
223 }
Stephen Hinesbde1a252014-05-15 18:02:33 -0700224 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 Drummond3c05a9c2015-11-11 18:54:41 +0000233 setFeatureString(attributes);
Stephen Hinesbde1a252014-05-15 18:02:33 -0700234 return true;
Zonr Changf74ee192012-04-12 15:34:58 +0800235}
236
237void 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}