blob: d9593c0223bd8fe57397644201d16908839234c6 [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
Stephen Hinese198abe2012-07-27 18:05:41 -070017#include "bcc/Renderscript/RSCompilerDriver.h"
Zonr Chang0fffa7e2012-04-12 19:43:53 +080018
Tobias Grosser7b980e12013-06-20 10:12:13 -070019#include <llvm/IR/Module.h>
Stephen Hinesad694762013-04-29 18:59:47 -070020#include <llvm/Support/CommandLine.h>
Stephen Hinesb10c3a72013-08-07 23:15:22 -070021#include <llvm/Support/Path.h>
Tobias Grosser7b980e12013-06-20 10:12:13 -070022#include <llvm/Support/raw_ostream.h>
Shih-wei Liao7bcec852012-04-25 04:07:09 -070023
24#include "bcinfo/BitcodeWrapper.h"
25
Stephen Hines47f0d5a2013-06-05 00:27:38 -070026#include "bcc/Compiler.h"
Stephen Hinese198abe2012-07-27 18:05:41 -070027#include "bcc/Renderscript/RSExecutable.h"
28#include "bcc/Renderscript/RSScript.h"
Zonr Changc72c4dd2012-04-12 15:38:53 +080029#include "bcc/Support/CompilerConfig.h"
30#include "bcc/Support/TargetCompilerConfigs.h"
Shih-wei Liao7bcec852012-04-25 04:07:09 -070031#include "bcc/Source.h"
Zonr Changc72c4dd2012-04-12 15:38:53 +080032#include "bcc/Support/FileMutex.h"
Zonr Changef73a242012-04-12 16:44:01 +080033#include "bcc/Support/Log.h"
Zonr Changc72c4dd2012-04-12 15:38:53 +080034#include "bcc/Support/InputFile.h"
35#include "bcc/Support/Initialization.h"
Shih-wei Liao7bcec852012-04-25 04:07:09 -070036#include "bcc/Support/Sha1Util.h"
Zonr Changc72c4dd2012-04-12 15:38:53 +080037#include "bcc/Support/OutputFile.h"
Zonr Chang0fffa7e2012-04-12 19:43:53 +080038
Nick Kralevichb81d6972013-05-21 16:52:35 -070039#ifdef HAVE_ANDROID_OS
Zonr Chang0fffa7e2012-04-12 19:43:53 +080040#include <cutils/properties.h>
Nick Kralevichb81d6972013-05-21 16:52:35 -070041#endif
Zonr Chang0fffa7e2012-04-12 19:43:53 +080042#include <utils/String8.h>
Shih-wei Liao7bcec852012-04-25 04:07:09 -070043#include <utils/StopWatch.h>
Zonr Chang0fffa7e2012-04-12 19:43:53 +080044
45using namespace bcc;
46
Stephen Hines3ab9da12013-02-01 18:39:15 -080047RSCompilerDriver::RSCompilerDriver(bool pUseCompilerRT) :
Stephen Hinesad694762013-04-29 18:59:47 -070048 mConfig(NULL), mCompiler(), mCompilerRuntime(NULL), mDebugContext(false),
49 mEnableGlobalMerge(true) {
Zonr Chang0fffa7e2012-04-12 19:43:53 +080050 init::Initialize();
Stephen Hines3ab9da12013-02-01 18:39:15 -080051 // Chain the symbol resolvers for compiler_rt and RS runtimes.
52 if (pUseCompilerRT) {
53 mCompilerRuntime = new CompilerRTSymbolResolver();
54 mResolver.chainResolver(*mCompilerRuntime);
55 }
Zonr Chang0fffa7e2012-04-12 19:43:53 +080056 mResolver.chainResolver(mRSRuntime);
57}
58
59RSCompilerDriver::~RSCompilerDriver() {
Stephen Hines3ab9da12013-02-01 18:39:15 -080060 delete mCompilerRuntime;
Zonr Chang0fffa7e2012-04-12 19:43:53 +080061 delete mConfig;
62}
63
Shih-wei Liao7bcec852012-04-25 04:07:09 -070064RSExecutable *
Stephen Hines47f0d5a2013-06-05 00:27:38 -070065RSCompilerDriver::loadScript(const char *pCacheDir, const char *pResName,
66 const char *pBitcode, size_t pBitcodeSize) {
67 //android::StopWatch load_time("bcc: RSCompilerDriver::loadScript time");
68 if ((pCacheDir == NULL) || (pResName == NULL)) {
69 ALOGE("Missing pCacheDir and/or pResName");
70 return NULL;
71 }
72
73 if ((pBitcode == NULL) || (pBitcodeSize <= 0)) {
74 ALOGE("No bitcode supplied! (bitcode: %p, size of bitcode: %zu)",
75 pBitcode, pBitcodeSize);
76 return NULL;
77 }
78
79 RSInfo::DependencyTableTy dep_info;
80 uint8_t bitcode_sha1[20];
81 Sha1Util::GetSHA1DigestFromBuffer(bitcode_sha1, pBitcode, pBitcodeSize);
Stephen Hines47f0d5a2013-06-05 00:27:38 -070082
Stephen Hines47f0d5a2013-06-05 00:27:38 -070083 // {pCacheDir}/{pResName}.o
Stephen Hinesb10c3a72013-08-07 23:15:22 -070084 llvm::SmallString<80> output_path(pCacheDir);
85 llvm::sys::path::append(output_path, pResName);
86 llvm::sys::path::replace_extension(output_path, ".o");
Zonr Chang0fffa7e2012-04-12 19:43:53 +080087
Stephen Hines5eea9732013-06-19 19:09:32 -070088 dep_info.push(std::make_pair(output_path.c_str(), bitcode_sha1));
89
Zonr Chang0fffa7e2012-04-12 19:43:53 +080090 //===--------------------------------------------------------------------===//
Stephen Hines01f05d42013-05-31 20:51:27 -070091 // Acquire the read lock for reading the Script object file.
Zonr Chang0fffa7e2012-04-12 19:43:53 +080092 //===--------------------------------------------------------------------===//
Stephen Hines47f0d5a2013-06-05 00:27:38 -070093 FileMutex<FileBase::kReadLock> read_output_mutex(output_path.c_str());
Zonr Chang0fffa7e2012-04-12 19:43:53 +080094
95 if (read_output_mutex.hasError() || !read_output_mutex.lock()) {
Stephen Hines47f0d5a2013-06-05 00:27:38 -070096 ALOGE("Unable to acquire the read lock for %s! (%s)", output_path.c_str(),
Zonr Chang0fffa7e2012-04-12 19:43:53 +080097 read_output_mutex.getErrorMessage().c_str());
98 return NULL;
99 }
100
101 //===--------------------------------------------------------------------===//
102 // Read the output object file.
103 //===--------------------------------------------------------------------===//
Stephen Hines47f0d5a2013-06-05 00:27:38 -0700104 InputFile *object_file = new (std::nothrow) InputFile(output_path.c_str());
Zonr Chang0fffa7e2012-04-12 19:43:53 +0800105
Stephen Hines01f05d42013-05-31 20:51:27 -0700106 if ((object_file == NULL) || object_file->hasError()) {
Stephen Hines47f0d5a2013-06-05 00:27:38 -0700107 // ALOGE("Unable to open the %s for read! (%s)", output_path.c_str(),
Stephen Hines01f05d42013-05-31 20:51:27 -0700108 // object_file->getErrorMessage().c_str());
109 delete object_file;
Zonr Chang0fffa7e2012-04-12 19:43:53 +0800110 return NULL;
111 }
112
113 //===--------------------------------------------------------------------===//
Stephen Hines01f05d42013-05-31 20:51:27 -0700114 // Acquire the read lock on object_file for reading its RS info file.
Zonr Chang0fffa7e2012-04-12 19:43:53 +0800115 //===--------------------------------------------------------------------===//
Stephen Hines47f0d5a2013-06-05 00:27:38 -0700116 android::String8 info_path = RSInfo::GetPath(output_path.c_str());
Zonr Chang0fffa7e2012-04-12 19:43:53 +0800117
Stephen Hines01f05d42013-05-31 20:51:27 -0700118 if (!object_file->lock()) {
Zonr Chang0fffa7e2012-04-12 19:43:53 +0800119 ALOGE("Unable to acquire the read lock on %s for reading %s! (%s)",
Stephen Hines47f0d5a2013-06-05 00:27:38 -0700120 output_path.c_str(), info_path.string(),
Stephen Hines01f05d42013-05-31 20:51:27 -0700121 object_file->getErrorMessage().c_str());
122 delete object_file;
Zonr Chang0fffa7e2012-04-12 19:43:53 +0800123 return NULL;
124 }
125
126 //===---------------------------------------------------------------------===//
127 // Open and load the RS info file.
128 //===--------------------------------------------------------------------===//
129 InputFile info_file(info_path.string());
Stephen Hines47f0d5a2013-06-05 00:27:38 -0700130 RSInfo *info = RSInfo::ReadFromFile(info_file, dep_info);
Zonr Chang0fffa7e2012-04-12 19:43:53 +0800131
Stephen Hines01f05d42013-05-31 20:51:27 -0700132 // Release the lock on object_file.
133 object_file->unlock();
Zonr Chang0fffa7e2012-04-12 19:43:53 +0800134
135 if (info == NULL) {
Stephen Hines01f05d42013-05-31 20:51:27 -0700136 delete object_file;
Zonr Chang0fffa7e2012-04-12 19:43:53 +0800137 return NULL;
138 }
139
140 //===--------------------------------------------------------------------===//
141 // Create the RSExecutable.
142 //===--------------------------------------------------------------------===//
Stephen Hines47f0d5a2013-06-05 00:27:38 -0700143 RSExecutable *result = RSExecutable::Create(*info, *object_file, mResolver);
Zonr Chang0fffa7e2012-04-12 19:43:53 +0800144 if (result == NULL) {
Stephen Hines01f05d42013-05-31 20:51:27 -0700145 delete object_file;
Zonr Chang0fffa7e2012-04-12 19:43:53 +0800146 delete info;
147 return NULL;
148 }
149
Zonr Chang0fffa7e2012-04-12 19:43:53 +0800150 return result;
151}
152
Stephen Hinesc06cd062013-07-12 10:51:29 -0700153#if defined(DEFAULT_ARM_CODEGEN)
Stephen Hinesad694762013-04-29 18:59:47 -0700154extern llvm::cl::opt<bool> EnableGlobalMerge;
Stephen Hinesc06cd062013-07-12 10:51:29 -0700155#endif
156
Zonr Chang0fffa7e2012-04-12 19:43:53 +0800157bool RSCompilerDriver::setupConfig(const RSScript &pScript) {
158 bool changed = false;
159
160 const llvm::CodeGenOpt::Level script_opt_level =
161 static_cast<llvm::CodeGenOpt::Level>(pScript.getOptimizationLevel());
162
163 if (mConfig != NULL) {
164 // Renderscript bitcode may have their optimization flag configuration
165 // different than the previous run of RS compilation.
166 if (mConfig->getOptimizationLevel() != script_opt_level) {
167 mConfig->setOptimizationLevel(script_opt_level);
168 changed = true;
169 }
170 } else {
171 // Haven't run the compiler ever.
172 mConfig = new (std::nothrow) DefaultCompilerConfig();
173 if (mConfig == NULL) {
174 // Return false since mConfig remains NULL and out-of-memory.
175 return false;
176 }
177 mConfig->setOptimizationLevel(script_opt_level);
Stephen Hinesc06cd062013-07-12 10:51:29 -0700178#if defined(DEFAULT_ARM_CODEGEN)
Stephen Hinesad694762013-04-29 18:59:47 -0700179 EnableGlobalMerge = mEnableGlobalMerge;
Stephen Hinesc06cd062013-07-12 10:51:29 -0700180#endif
Zonr Chang0fffa7e2012-04-12 19:43:53 +0800181 changed = true;
182 }
183
184#if defined(DEFAULT_ARM_CODEGEN)
185 // NEON should be disable when full-precision floating point is required.
186 assert((pScript.getInfo() != NULL) && "NULL RS info!");
Shih-wei Liaoed7fffb2012-06-30 11:27:59 -0700187 if (pScript.getInfo()->getFloatPrecisionRequirement() == RSInfo::FP_Full) {
Zonr Chang0fffa7e2012-04-12 19:43:53 +0800188 // Must be ARMCompilerConfig.
189 ARMCompilerConfig *arm_config = static_cast<ARMCompilerConfig *>(mConfig);
190 changed |= arm_config->enableNEON(/* pEnable */false);
191 }
192#endif
193
194 return changed;
195}
196
Stephen Hines47f0d5a2013-06-05 00:27:38 -0700197Compiler::ErrorCode
Shih-wei Liao7bcec852012-04-25 04:07:09 -0700198RSCompilerDriver::compileScript(RSScript &pScript,
Shih-wei Liaoba420642012-06-30 11:27:37 -0700199 const char* pScriptName,
Shih-wei Liao7bcec852012-04-25 04:07:09 -0700200 const char *pOutputPath,
Stephen Hines331310e2012-10-26 19:27:55 -0700201 const char *pRuntimePath,
202 const RSInfo::DependencyTableTy &pDeps,
Tobias Grosser7b980e12013-06-20 10:12:13 -0700203 bool pSkipLoad, bool pDumpIR) {
Tim Murrayc89f78b2013-05-09 11:57:12 -0700204 //android::StopWatch compile_time("bcc: RSCompilerDriver::compileScript time");
Zonr Chang0fffa7e2012-04-12 19:43:53 +0800205 RSInfo *info = NULL;
206
207 //===--------------------------------------------------------------------===//
208 // Extract RS-specific information from source bitcode.
209 //===--------------------------------------------------------------------===//
210 // RS info may contains configuration (such as #optimization_level) to the
211 // compiler therefore it should be extracted before compilation.
Shih-wei Liao7bcec852012-04-25 04:07:09 -0700212 info = RSInfo::ExtractFromSource(pScript.getSource(), pDeps);
Zonr Chang0fffa7e2012-04-12 19:43:53 +0800213 if (info == NULL) {
Stephen Hines47f0d5a2013-06-05 00:27:38 -0700214 return Compiler::kErrInvalidSource;
Zonr Chang0fffa7e2012-04-12 19:43:53 +0800215 }
216
217 //===--------------------------------------------------------------------===//
218 // Associate script with its info
219 //===--------------------------------------------------------------------===//
220 // This is required since RS compiler may need information in the info file
221 // to do some transformation (e.g., expand foreach-able function.)
222 pScript.setInfo(info);
223
224 //===--------------------------------------------------------------------===//
Stephen Hinese198abe2012-07-27 18:05:41 -0700225 // Link RS script with Renderscript runtime.
Shih-wei Liaoba420642012-06-30 11:27:37 -0700226 //===--------------------------------------------------------------------===//
Stephen Hines331310e2012-10-26 19:27:55 -0700227 if (!RSScript::LinkRuntime(pScript, pRuntimePath)) {
Stephen Hinese198abe2012-07-27 18:05:41 -0700228 ALOGE("Failed to link script '%s' with Renderscript runtime!", pScriptName);
Stephen Hines47f0d5a2013-06-05 00:27:38 -0700229 return Compiler::kErrInvalidSource;
Shih-wei Liaoba420642012-06-30 11:27:37 -0700230 }
231
Stephen Hines01f05d42013-05-31 20:51:27 -0700232 {
233 // Acquire the write lock for writing output object file.
234 FileMutex<FileBase::kWriteLock> write_output_mutex(pOutputPath);
Zonr Chang0fffa7e2012-04-12 19:43:53 +0800235
Stephen Hines01f05d42013-05-31 20:51:27 -0700236 if (write_output_mutex.hasError() || !write_output_mutex.lock()) {
237 ALOGE("Unable to acquire the lock for writing %s! (%s)",
238 pOutputPath, write_output_mutex.getErrorMessage().c_str());
Stephen Hines47f0d5a2013-06-05 00:27:38 -0700239 return Compiler::kErrInvalidSource;
Zonr Chang0fffa7e2012-04-12 19:43:53 +0800240 }
Zonr Chang0fffa7e2012-04-12 19:43:53 +0800241
Stephen Hines01f05d42013-05-31 20:51:27 -0700242 // Open the output file for write.
243 OutputFile output_file(pOutputPath, FileBase::kTruncate);
244
245 if (output_file.hasError()) {
246 ALOGE("Unable to open %s for write! (%s)", pOutputPath,
247 output_file.getErrorMessage().c_str());
Stephen Hines47f0d5a2013-06-05 00:27:38 -0700248 return Compiler::kErrInvalidSource;
Stephen Hines01f05d42013-05-31 20:51:27 -0700249 }
250
251 // Setup the config to the compiler.
252 bool compiler_need_reconfigure = setupConfig(pScript);
253
254 if (mConfig == NULL) {
255 ALOGE("Failed to setup config for RS compiler to compile %s!",
256 pOutputPath);
Stephen Hines47f0d5a2013-06-05 00:27:38 -0700257 return Compiler::kErrInvalidSource;
Stephen Hines01f05d42013-05-31 20:51:27 -0700258 }
259
260 if (compiler_need_reconfigure) {
261 Compiler::ErrorCode err = mCompiler.config(*mConfig);
262 if (err != Compiler::kSuccess) {
263 ALOGE("Failed to config the RS compiler for %s! (%s)",pOutputPath,
264 Compiler::GetErrorString(err));
Stephen Hines47f0d5a2013-06-05 00:27:38 -0700265 return Compiler::kErrInvalidSource;
Stephen Hines01f05d42013-05-31 20:51:27 -0700266 }
267 }
268
Tobias Grosser27fb7ed2013-06-21 18:34:56 -0700269 OutputFile *ir_file = NULL;
270 llvm::raw_fd_ostream *IRStream = NULL;
271 if (pDumpIR) {
272 android::String8 path(pOutputPath);
273 path.append(".ll");
274 ir_file = new OutputFile(path.string(), FileBase::kTruncate);
275 IRStream = ir_file->dup();
276 }
277
Stephen Hines01f05d42013-05-31 20:51:27 -0700278 // Run the compiler.
Tobias Grosser27fb7ed2013-06-21 18:34:56 -0700279 Compiler::ErrorCode compile_result = mCompiler.compile(pScript,
280 output_file, IRStream);
281
282 if (ir_file) {
283 ir_file->close();
284 delete ir_file;
285 }
Stephen Hines01f05d42013-05-31 20:51:27 -0700286
287 if (compile_result != Compiler::kSuccess) {
288 ALOGE("Unable to compile the source to file %s! (%s)", pOutputPath,
289 Compiler::GetErrorString(compile_result));
Stephen Hines47f0d5a2013-06-05 00:27:38 -0700290 return Compiler::kErrInvalidSource;
Stephen Hines01f05d42013-05-31 20:51:27 -0700291 }
Zonr Chang0fffa7e2012-04-12 19:43:53 +0800292 }
293
Stephen Hines331310e2012-10-26 19:27:55 -0700294 // No need to produce an RSExecutable in this case.
295 // TODO: Error handling in this case is nonexistent.
296 if (pSkipLoad) {
Stephen Hines47f0d5a2013-06-05 00:27:38 -0700297 return Compiler::kSuccess;
Stephen Hines331310e2012-10-26 19:27:55 -0700298 }
299
Stephen Hines01f05d42013-05-31 20:51:27 -0700300 {
301 android::String8 info_path = RSInfo::GetPath(pOutputPath);
302 OutputFile info_file(info_path.string(), FileBase::kTruncate);
303
304 if (info_file.hasError()) {
305 ALOGE("Failed to open the info file %s for write! (%s)",
306 info_path.string(), info_file.getErrorMessage().c_str());
Stephen Hines47f0d5a2013-06-05 00:27:38 -0700307 return Compiler::kErrInvalidSource;
Stephen Hines01f05d42013-05-31 20:51:27 -0700308 }
309
310 FileMutex<FileBase::kWriteLock> write_info_mutex(info_path.string());
311 if (write_info_mutex.hasError() || !write_info_mutex.lock()) {
312 ALOGE("Unable to acquire the lock for writing %s! (%s)",
313 info_path.string(), write_info_mutex.getErrorMessage().c_str());
Stephen Hines47f0d5a2013-06-05 00:27:38 -0700314 return Compiler::kErrInvalidSource;
Stephen Hines01f05d42013-05-31 20:51:27 -0700315 }
316
317 // Perform the write.
318 if (!info->write(info_file)) {
319 ALOGE("Failed to sync the RS info file %s!", info_path.string());
Stephen Hines47f0d5a2013-06-05 00:27:38 -0700320 return Compiler::kErrInvalidSource;
Stephen Hines01f05d42013-05-31 20:51:27 -0700321 }
Zonr Chang0fffa7e2012-04-12 19:43:53 +0800322 }
323
Stephen Hines47f0d5a2013-06-05 00:27:38 -0700324 return Compiler::kSuccess;
Zonr Chang0fffa7e2012-04-12 19:43:53 +0800325}
326
Stephen Hines47f0d5a2013-06-05 00:27:38 -0700327bool RSCompilerDriver::build(BCCContext &pContext,
328 const char *pCacheDir,
329 const char *pResName,
330 const char *pBitcode,
331 size_t pBitcodeSize,
332 const char *pRuntimePath,
Tobias Grosser7b980e12013-06-20 10:12:13 -0700333 RSLinkRuntimeCallback pLinkRuntimeCallback,
334 bool pDumpIR) {
Tim Murrayc89f78b2013-05-09 11:57:12 -0700335 // android::StopWatch build_time("bcc: RSCompilerDriver::build time");
Shih-wei Liao7bcec852012-04-25 04:07:09 -0700336 //===--------------------------------------------------------------------===//
337 // Check parameters.
338 //===--------------------------------------------------------------------===//
339 if ((pCacheDir == NULL) || (pResName == NULL)) {
340 ALOGE("Invalid parameter passed to RSCompilerDriver::build()! (cache dir: "
341 "%s, resource name: %s)", ((pCacheDir) ? pCacheDir : "(null)"),
342 ((pResName) ? pResName : "(null)"));
Stephen Hines47f0d5a2013-06-05 00:27:38 -0700343 return false;
Shih-wei Liao7bcec852012-04-25 04:07:09 -0700344 }
345
346 if ((pBitcode == NULL) || (pBitcodeSize <= 0)) {
347 ALOGE("No bitcode supplied! (bitcode: %p, size of bitcode: %u)",
348 pBitcode, static_cast<unsigned>(pBitcodeSize));
Stephen Hines47f0d5a2013-06-05 00:27:38 -0700349 return false;
Shih-wei Liao7bcec852012-04-25 04:07:09 -0700350 }
351
352 //===--------------------------------------------------------------------===//
353 // Prepare dependency information.
354 //===--------------------------------------------------------------------===//
355 RSInfo::DependencyTableTy dep_info;
356 uint8_t bitcode_sha1[20];
357 Sha1Util::GetSHA1DigestFromBuffer(bitcode_sha1, pBitcode, pBitcodeSize);
Shih-wei Liao7bcec852012-04-25 04:07:09 -0700358
359 //===--------------------------------------------------------------------===//
360 // Construct output path.
Shih-wei Liao7bcec852012-04-25 04:07:09 -0700361 // {pCacheDir}/{pResName}.o
Stephen Hinesb10c3a72013-08-07 23:15:22 -0700362 //===--------------------------------------------------------------------===//
363 llvm::SmallString<80> output_path(pCacheDir);
364 llvm::sys::path::append(output_path, pResName);
365 llvm::sys::path::replace_extension(output_path, ".o");
Shih-wei Liao7bcec852012-04-25 04:07:09 -0700366
Stephen Hines5eea9732013-06-19 19:09:32 -0700367 dep_info.push(std::make_pair(output_path.c_str(), bitcode_sha1));
368
Shih-wei Liao7bcec852012-04-25 04:07:09 -0700369 //===--------------------------------------------------------------------===//
Shih-wei Liao7bcec852012-04-25 04:07:09 -0700370 // Load the bitcode and create script.
371 //===--------------------------------------------------------------------===//
372 Source *source = Source::CreateFromBuffer(pContext, pResName,
373 pBitcode, pBitcodeSize);
374 if (source == NULL) {
Stephen Hines47f0d5a2013-06-05 00:27:38 -0700375 return false;
Shih-wei Liao7bcec852012-04-25 04:07:09 -0700376 }
377
378 RSScript *script = new (std::nothrow) RSScript(*source);
379 if (script == NULL) {
380 ALOGE("Out of memory when create Script object for '%s'! (output: %s)",
381 pResName, output_path.c_str());
382 delete source;
Stephen Hines47f0d5a2013-06-05 00:27:38 -0700383 return false;
Shih-wei Liao7bcec852012-04-25 04:07:09 -0700384 }
385
Stephen Hines06731a62013-02-12 19:29:42 -0800386 script->setLinkRuntimeCallback(pLinkRuntimeCallback);
387
Shih-wei Liao7bcec852012-04-25 04:07:09 -0700388 // Read information from bitcode wrapper.
389 bcinfo::BitcodeWrapper wrapper(pBitcode, pBitcodeSize);
390 script->setCompilerVersion(wrapper.getCompilerVersion());
391 script->setOptimizationLevel(static_cast<RSScript::OptimizationLevel>(
392 wrapper.getOptimizationLevel()));
393
394 //===--------------------------------------------------------------------===//
395 // Compile the script
396 //===--------------------------------------------------------------------===//
Stephen Hines47f0d5a2013-06-05 00:27:38 -0700397 Compiler::ErrorCode status = compileScript(*script, pResName,
398 output_path.c_str(),
Tobias Grosser7b980e12013-06-20 10:12:13 -0700399 pRuntimePath, dep_info, false,
400 pDumpIR);
Shih-wei Liao7bcec852012-04-25 04:07:09 -0700401
402 // Script is no longer used. Free it to get more memory.
403 delete script;
404
Stephen Hines47f0d5a2013-06-05 00:27:38 -0700405 if (status != Compiler::kSuccess) {
406 return false;
Shih-wei Liao7bcec852012-04-25 04:07:09 -0700407 }
408
Stephen Hines47f0d5a2013-06-05 00:27:38 -0700409 return true;
Zonr Chang0fffa7e2012-04-12 19:43:53 +0800410}
Stephen Hines331310e2012-10-26 19:27:55 -0700411
412
Stephen Hines47f0d5a2013-06-05 00:27:38 -0700413bool RSCompilerDriver::build(RSScript &pScript, const char *pOut,
414 const char *pRuntimePath) {
Stephen Hines331310e2012-10-26 19:27:55 -0700415 RSInfo::DependencyTableTy dep_info;
416 RSInfo *info = RSInfo::ExtractFromSource(pScript.getSource(), dep_info);
417 if (info == NULL) {
Stephen Hines47f0d5a2013-06-05 00:27:38 -0700418 return false;
Stephen Hines331310e2012-10-26 19:27:55 -0700419 }
420 pScript.setInfo(info);
421
Stephen Hines86a0b792012-11-06 20:04:47 -0800422 // Embed the info string directly in the ELF, since this path is for an
423 // offline (host) compilation.
424 pScript.setEmbedInfo(true);
425
Stephen Hines47f0d5a2013-06-05 00:27:38 -0700426 Compiler::ErrorCode status = compileScript(pScript, pOut, pOut, pRuntimePath,
427 dep_info, true);
428 if (status != Compiler::kSuccess) {
429 return false;
430 }
431
432 return true;
Stephen Hines331310e2012-10-26 19:27:55 -0700433}
434