blob: a59a7f91b0b24347c51373afda26d3106d98e6b6 [file] [log] [blame]
Zonr Chang0fffa7e2012-04-12 19:43:53 +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
Zonr Changc72c4dd2012-04-12 15:38:53 +080017#include "bcc/RenderScript/RSCompilerDriver.h"
Zonr Chang0fffa7e2012-04-12 19:43:53 +080018
Shih-wei Liao7bcec852012-04-25 04:07:09 -070019#include <llvm/Support/Path.h>
20
21#include "bcinfo/BitcodeWrapper.h"
22
Zonr Changc72c4dd2012-04-12 15:38:53 +080023#include "bcc/RenderScript/RSExecutable.h"
Shih-wei Liao7bcec852012-04-25 04:07:09 -070024#include "bcc/RenderScript/RSScript.h"
Zonr Changc72c4dd2012-04-12 15:38:53 +080025#include "bcc/Support/CompilerConfig.h"
26#include "bcc/Support/TargetCompilerConfigs.h"
Shih-wei Liao7bcec852012-04-25 04:07:09 -070027#include "bcc/Source.h"
Zonr Changc72c4dd2012-04-12 15:38:53 +080028#include "bcc/Support/FileMutex.h"
Zonr Changef73a242012-04-12 16:44:01 +080029#include "bcc/Support/Log.h"
Zonr Changc72c4dd2012-04-12 15:38:53 +080030#include "bcc/Support/InputFile.h"
31#include "bcc/Support/Initialization.h"
Shih-wei Liao7bcec852012-04-25 04:07:09 -070032#include "bcc/Support/Sha1Util.h"
Zonr Changc72c4dd2012-04-12 15:38:53 +080033#include "bcc/Support/OutputFile.h"
Zonr Chang0fffa7e2012-04-12 19:43:53 +080034
35#include <cutils/properties.h>
36#include <utils/String8.h>
Shih-wei Liao7bcec852012-04-25 04:07:09 -070037#include <utils/StopWatch.h>
Zonr Chang0fffa7e2012-04-12 19:43:53 +080038
39using namespace bcc;
40
41namespace {
42
43bool is_force_recompile() {
44 char buf[PROPERTY_VALUE_MAX];
45
Shih-wei Liaoed7fffb2012-06-30 11:27:59 -070046 // Re-compile if floating point precision has been overridden.
47 property_get("debug.rs.precision", buf, "");
48 if (buf[0] != '\0') {
49 return true;
50 }
51
52 // Re-compile if debug.rs.forcerecompile is set.
Zonr Chang0fffa7e2012-04-12 19:43:53 +080053 property_get("debug.rs.forcerecompile", buf, "0");
54 if ((::strcmp(buf, "1") == 0) || (::strcmp(buf, "true") == 0)) {
55 return true;
56 } else {
57 return false;
58 }
59}
60
61} // end anonymous namespace
62
63RSCompilerDriver::RSCompilerDriver() : mConfig(NULL), mCompiler() {
64 init::Initialize();
65 // Chain the symbol resolvers for BCC runtimes and RS runtimes.
66 mResolver.chainResolver(mBCCRuntime);
67 mResolver.chainResolver(mRSRuntime);
68}
69
70RSCompilerDriver::~RSCompilerDriver() {
71 delete mConfig;
72}
73
Shih-wei Liao7bcec852012-04-25 04:07:09 -070074RSExecutable *
75RSCompilerDriver::loadScriptCache(const char *pOutputPath,
76 const RSInfo::DependencyTableTy &pDeps) {
77 android::StopWatch load_time("bcc: RSCompilerDriver::loadScriptCache time");
Zonr Chang0fffa7e2012-04-12 19:43:53 +080078 RSExecutable *result = NULL;
79
80 if (is_force_recompile())
81 return NULL;
82
83 //===--------------------------------------------------------------------===//
84 // Acquire the read lock for reading output object file.
85 //===--------------------------------------------------------------------===//
86 FileMutex<FileBase::kReadLock> read_output_mutex(pOutputPath);
87
88 if (read_output_mutex.hasError() || !read_output_mutex.lock()) {
Shih-wei Liao7bcec852012-04-25 04:07:09 -070089 ALOGE("Unable to acquire the read lock for %s! (%s)", pOutputPath,
Zonr Chang0fffa7e2012-04-12 19:43:53 +080090 read_output_mutex.getErrorMessage().c_str());
91 return NULL;
92 }
93
94 //===--------------------------------------------------------------------===//
95 // Read the output object file.
96 //===--------------------------------------------------------------------===//
97 InputFile *output_file = new (std::nothrow) InputFile(pOutputPath);
98
99 if ((output_file == NULL) || output_file->hasError()) {
Shih-wei Liao7bcec852012-04-25 04:07:09 -0700100 ALOGE("Unable to open the %s for read! (%s)", pOutputPath,
Zonr Chang0fffa7e2012-04-12 19:43:53 +0800101 output_file->getErrorMessage().c_str());
102 delete output_file;
103 return NULL;
104 }
105
106 //===--------------------------------------------------------------------===//
107 // Acquire the read lock on output_file for reading its RS info file.
108 //===--------------------------------------------------------------------===//
109 android::String8 info_path = RSInfo::GetPath(*output_file);
110
111 if (!output_file->lock()) {
112 ALOGE("Unable to acquire the read lock on %s for reading %s! (%s)",
Shih-wei Liao7bcec852012-04-25 04:07:09 -0700113 pOutputPath, info_path.string(),
Zonr Chang0fffa7e2012-04-12 19:43:53 +0800114 output_file->getErrorMessage().c_str());
115 delete output_file;
116 return NULL;
117 }
118
119 //===---------------------------------------------------------------------===//
120 // Open and load the RS info file.
121 //===--------------------------------------------------------------------===//
122 InputFile info_file(info_path.string());
Shih-wei Liao7bcec852012-04-25 04:07:09 -0700123 RSInfo *info = RSInfo::ReadFromFile(info_file, pDeps);
Zonr Chang0fffa7e2012-04-12 19:43:53 +0800124
125 // Release the lock on output_file.
126 output_file->unlock();
127
128 if (info == NULL) {
129 delete output_file;
130 return NULL;
131 }
132
133 //===--------------------------------------------------------------------===//
134 // Create the RSExecutable.
135 //===--------------------------------------------------------------------===//
136 result = RSExecutable::Create(*info, *output_file, mResolver);
137 if (result == NULL) {
138 delete output_file;
139 delete info;
140 return NULL;
141 }
142
Zonr Chang0fffa7e2012-04-12 19:43:53 +0800143 return result;
144}
145
146bool RSCompilerDriver::setupConfig(const RSScript &pScript) {
147 bool changed = false;
148
149 const llvm::CodeGenOpt::Level script_opt_level =
150 static_cast<llvm::CodeGenOpt::Level>(pScript.getOptimizationLevel());
151
152 if (mConfig != NULL) {
153 // Renderscript bitcode may have their optimization flag configuration
154 // different than the previous run of RS compilation.
155 if (mConfig->getOptimizationLevel() != script_opt_level) {
156 mConfig->setOptimizationLevel(script_opt_level);
157 changed = true;
158 }
159 } else {
160 // Haven't run the compiler ever.
161 mConfig = new (std::nothrow) DefaultCompilerConfig();
162 if (mConfig == NULL) {
163 // Return false since mConfig remains NULL and out-of-memory.
164 return false;
165 }
166 mConfig->setOptimizationLevel(script_opt_level);
167 changed = true;
168 }
169
170#if defined(DEFAULT_ARM_CODEGEN)
171 // NEON should be disable when full-precision floating point is required.
172 assert((pScript.getInfo() != NULL) && "NULL RS info!");
Shih-wei Liaoed7fffb2012-06-30 11:27:59 -0700173 if (pScript.getInfo()->getFloatPrecisionRequirement() == RSInfo::FP_Full) {
Zonr Chang0fffa7e2012-04-12 19:43:53 +0800174 // Must be ARMCompilerConfig.
175 ARMCompilerConfig *arm_config = static_cast<ARMCompilerConfig *>(mConfig);
176 changed |= arm_config->enableNEON(/* pEnable */false);
177 }
178#endif
179
180 return changed;
181}
182
Shih-wei Liao7bcec852012-04-25 04:07:09 -0700183RSExecutable *
184RSCompilerDriver::compileScript(RSScript &pScript,
Shih-wei Liaoba420642012-06-30 11:27:37 -0700185 const char* pScriptName,
Shih-wei Liao7bcec852012-04-25 04:07:09 -0700186 const char *pOutputPath,
187 const RSInfo::DependencyTableTy &pDeps) {
188 android::StopWatch compile_time("bcc: RSCompilerDriver::compileScript time");
Zonr Chang0fffa7e2012-04-12 19:43:53 +0800189 RSExecutable *result = NULL;
190 RSInfo *info = NULL;
191
192 //===--------------------------------------------------------------------===//
193 // Extract RS-specific information from source bitcode.
194 //===--------------------------------------------------------------------===//
195 // RS info may contains configuration (such as #optimization_level) to the
196 // compiler therefore it should be extracted before compilation.
Shih-wei Liao7bcec852012-04-25 04:07:09 -0700197 info = RSInfo::ExtractFromSource(pScript.getSource(), pDeps);
Zonr Chang0fffa7e2012-04-12 19:43:53 +0800198 if (info == NULL) {
199 return NULL;
200 }
201
202 //===--------------------------------------------------------------------===//
203 // Associate script with its info
204 //===--------------------------------------------------------------------===//
205 // This is required since RS compiler may need information in the info file
206 // to do some transformation (e.g., expand foreach-able function.)
207 pScript.setInfo(info);
208
209 //===--------------------------------------------------------------------===//
Shih-wei Liaoba420642012-06-30 11:27:37 -0700210 // Link RS script with RenderScript runtime.
211 //===--------------------------------------------------------------------===//
212 if (!RSScript::LinkRuntime(pScript)) {
213 ALOGE("Failed to link script '%s' with RenderScript runtime!", pScriptName);
214 return NULL;
215 }
216
217 //===--------------------------------------------------------------------===//
Zonr Chang0fffa7e2012-04-12 19:43:53 +0800218 // Acquire the write lock for writing output object file.
219 //===--------------------------------------------------------------------===//
220 FileMutex<FileBase::kWriteLock> write_output_mutex(pOutputPath);
221
222 if (write_output_mutex.hasError() || !write_output_mutex.lock()) {
223 ALOGE("Unable to acquire the lock for writing %s! (%s)",
Shih-wei Liao7bcec852012-04-25 04:07:09 -0700224 pOutputPath, write_output_mutex.getErrorMessage().c_str());
Zonr Chang0fffa7e2012-04-12 19:43:53 +0800225 return NULL;
226 }
227
228 //===--------------------------------------------------------------------===//
229 // Open the output file for write.
230 //===--------------------------------------------------------------------===//
231 OutputFile *output_file = new (std::nothrow) OutputFile(pOutputPath);
232
233 if ((output_file == NULL) || output_file->hasError()) {
Shih-wei Liao7bcec852012-04-25 04:07:09 -0700234 ALOGE("Unable to open the %s for write! (%s)", pOutputPath,
Zonr Chang0fffa7e2012-04-12 19:43:53 +0800235 output_file->getErrorMessage().c_str());
236 delete info;
237 delete output_file;
238 return NULL;
239 }
240
241 //===--------------------------------------------------------------------===//
242 // Setup the config to the compiler.
243 //===--------------------------------------------------------------------===//
244 bool compiler_need_reconfigure = setupConfig(pScript);
245
246 if (mConfig == NULL) {
Shih-wei Liao7bcec852012-04-25 04:07:09 -0700247 ALOGE("Failed to setup config for RS compiler to compile %s!", pOutputPath);
Zonr Chang0fffa7e2012-04-12 19:43:53 +0800248 delete info;
249 delete output_file;
250 return NULL;
251 }
252
253 // Compiler need to re-config if it's haven't run the config() yet or the
254 // configuration it referenced is changed.
255 if (compiler_need_reconfigure) {
256 Compiler::ErrorCode err = mCompiler.config(*mConfig);
257 if (err != Compiler::kSuccess) {
Shih-wei Liao7bcec852012-04-25 04:07:09 -0700258 ALOGE("Failed to config the RS compiler for %s! (%s)",pOutputPath,
Zonr Chang0fffa7e2012-04-12 19:43:53 +0800259 Compiler::GetErrorString(err));
260 delete info;
261 delete output_file;
262 return NULL;
263 }
264 }
265
266 //===--------------------------------------------------------------------===//
267 // Run the compiler.
268 //===--------------------------------------------------------------------===//
269 Compiler::ErrorCode compile_result = mCompiler.compile(pScript, *output_file);
270 if (compile_result != Compiler::kSuccess) {
Shih-wei Liao7bcec852012-04-25 04:07:09 -0700271 ALOGE("Unable to compile the source to file %s! (%s)", pOutputPath,
Zonr Chang0fffa7e2012-04-12 19:43:53 +0800272 Compiler::GetErrorString(compile_result));
273 delete info;
274 delete output_file;
275 return NULL;
276 }
277
278 //===--------------------------------------------------------------------===//
279 // Create the RSExecutable.
280 //===--------------------------------------------------------------------===//
281 result = RSExecutable::Create(*info, *output_file, mResolver);
282 if (result == NULL) {
283 delete info;
284 delete output_file;
285 return NULL;
286 }
287
Zonr Chang0fffa7e2012-04-12 19:43:53 +0800288 //===--------------------------------------------------------------------===//
289 // Write out the RS info file.
290 //===--------------------------------------------------------------------===//
291 // Note that write failure only results in a warning since the source is
292 // successfully compiled and loaded.
293 if (!result->syncInfo(/* pForce */true)) {
294 ALOGW("%s was successfully compiled and loaded but its RS info file failed "
Shih-wei Liao7bcec852012-04-25 04:07:09 -0700295 "to write out!", pOutputPath);
Zonr Chang0fffa7e2012-04-12 19:43:53 +0800296 }
297
298 return result;
299}
300
Shih-wei Liao7bcec852012-04-25 04:07:09 -0700301RSExecutable *RSCompilerDriver::build(BCCContext &pContext,
302 const char *pCacheDir,
303 const char *pResName,
304 const char *pBitcode,
305 size_t pBitcodeSize) {
306 android::StopWatch build_time("bcc: RSCompilerDriver::build time");
307 //===--------------------------------------------------------------------===//
308 // Check parameters.
309 //===--------------------------------------------------------------------===//
310 if ((pCacheDir == NULL) || (pResName == NULL)) {
311 ALOGE("Invalid parameter passed to RSCompilerDriver::build()! (cache dir: "
312 "%s, resource name: %s)", ((pCacheDir) ? pCacheDir : "(null)"),
313 ((pResName) ? pResName : "(null)"));
314 return NULL;
315 }
316
317 if ((pBitcode == NULL) || (pBitcodeSize <= 0)) {
318 ALOGE("No bitcode supplied! (bitcode: %p, size of bitcode: %u)",
319 pBitcode, static_cast<unsigned>(pBitcodeSize));
320 return NULL;
321 }
322
323 //===--------------------------------------------------------------------===//
324 // Prepare dependency information.
325 //===--------------------------------------------------------------------===//
326 RSInfo::DependencyTableTy dep_info;
327 uint8_t bitcode_sha1[20];
328 Sha1Util::GetSHA1DigestFromBuffer(bitcode_sha1, pBitcode, pBitcodeSize);
329 dep_info.push(std::make_pair(pResName, bitcode_sha1));
330
331 //===--------------------------------------------------------------------===//
332 // Construct output path.
333 //===--------------------------------------------------------------------===//
334 llvm::sys::Path output_path(pCacheDir);
335
336 // {pCacheDir}/{pResName}
337 if (!output_path.appendComponent(pResName)) {
338 ALOGE("Failed to construct output path %s/%s!", pCacheDir, pResName);
339 return NULL;
340 }
341
342 // {pCacheDir}/{pResName}.o
343 output_path.appendSuffix("o");
344
345 //===--------------------------------------------------------------------===//
346 // Load cache.
347 //===--------------------------------------------------------------------===//
348 RSExecutable *result = loadScriptCache(output_path.c_str(), dep_info);
Zonr Chang0fffa7e2012-04-12 19:43:53 +0800349
350 if (result != NULL) {
351 // Cache hit
352 return result;
353 }
354
Shih-wei Liao7bcec852012-04-25 04:07:09 -0700355 //===--------------------------------------------------------------------===//
356 // Load the bitcode and create script.
357 //===--------------------------------------------------------------------===//
358 Source *source = Source::CreateFromBuffer(pContext, pResName,
359 pBitcode, pBitcodeSize);
360 if (source == NULL) {
361 return NULL;
362 }
363
364 RSScript *script = new (std::nothrow) RSScript(*source);
365 if (script == NULL) {
366 ALOGE("Out of memory when create Script object for '%s'! (output: %s)",
367 pResName, output_path.c_str());
368 delete source;
369 return NULL;
370 }
371
Shih-wei Liao7bcec852012-04-25 04:07:09 -0700372 // Read information from bitcode wrapper.
373 bcinfo::BitcodeWrapper wrapper(pBitcode, pBitcodeSize);
374 script->setCompilerVersion(wrapper.getCompilerVersion());
375 script->setOptimizationLevel(static_cast<RSScript::OptimizationLevel>(
376 wrapper.getOptimizationLevel()));
377
378 //===--------------------------------------------------------------------===//
379 // Compile the script
380 //===--------------------------------------------------------------------===//
Shih-wei Liaoba420642012-06-30 11:27:37 -0700381 result = compileScript(*script, pResName, output_path.c_str(), dep_info);
Shih-wei Liao7bcec852012-04-25 04:07:09 -0700382
383 // Script is no longer used. Free it to get more memory.
384 delete script;
385
386 if (result == NULL) {
387 return NULL;
388 }
389
390 return result;
Zonr Chang0fffa7e2012-04-12 19:43:53 +0800391}