blob: d3b302dcbf6e439d80a4d3c575c13332977ddcd7 [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
Jean-Luc Brouilleta2dd52f2017-02-16 20:57:26 -080017#include "bcc/RSCompilerDriver.h"
18
19#include "Assert.h"
20#include "FileMutex.h"
21#include "InputFile.h"
22#include "Log.h"
23#include "OutputFile.h"
24#include "RSScriptGroupFusion.h"
25
26#include "bcc/BCCContext.h"
27#include "bcc/Compiler.h"
28#include "bcc/CompilerConfig.h"
29#include "bcc/Config.h"
30#include "bcc/Initialization.h"
Jean-Luc Brouillet0a2acce2017-02-17 13:29:47 -080031#include "bcc/Script.h"
Jean-Luc Brouilleta2dd52f2017-02-16 20:57:26 -080032#include "bcc/Source.h"
33#include "bcinfo/BitcodeWrapper.h"
34#include "bcinfo/MetadataExtractor.h"
Zonr Chang0fffa7e2012-04-12 19:43:53 +080035
Pirama Arumuga Nainarf229c402016-03-06 23:05:45 -080036#include "llvm/ADT/STLExtras.h"
Yang Nia4ded132014-11-17 17:44:08 -080037#include "llvm/IR/AssemblyAnnotationWriter.h"
Tobias Grosser7b980e12013-06-20 10:12:13 -070038#include <llvm/IR/Module.h>
Yang Ni0ab50b82015-02-16 12:31:56 -080039#include "llvm/Linker/Linker.h"
Stephen Hinesad694762013-04-29 18:59:47 -070040#include <llvm/Support/CommandLine.h>
Stephen Hinesb10c3a72013-08-07 23:15:22 -070041#include <llvm/Support/Path.h>
Tobias Grosser7b980e12013-06-20 10:12:13 -070042#include <llvm/Support/raw_ostream.h>
Pirama Arumuga Nainard2d5ee32016-04-12 14:04:50 -070043#include <llvm/Target/TargetMachine.h>
Shih-wei Liao7bcec852012-04-25 04:07:09 -070044
Yang Ni0ab50b82015-02-16 12:31:56 -080045#include <sstream>
Yang Nia4ded132014-11-17 17:44:08 -080046#include <string>
47
Elliott Hughesfe0de782015-08-12 15:09:33 -070048#ifdef __ANDROID__
Zonr Chang0fffa7e2012-04-12 19:43:53 +080049#include <cutils/properties.h>
Nick Kralevichb81d6972013-05-21 16:52:35 -070050#endif
Shih-wei Liao7bcec852012-04-25 04:07:09 -070051#include <utils/StopWatch.h>
Zonr Chang0fffa7e2012-04-12 19:43:53 +080052
53using namespace bcc;
54
David Grossefe14342016-02-18 11:46:57 -080055RSCompilerDriver::RSCompilerDriver() :
Chris Wailes900c6c12014-08-13 15:40:00 -070056 mConfig(nullptr), mCompiler(), mDebugContext(false),
Stephen Hines750ee652015-04-16 16:24:18 -070057 mLinkRuntimeCallback(nullptr), mEnableGlobalMerge(true),
58 mEmbedGlobalInfo(false), mEmbedGlobalInfoSkipConstant(false) {
Zonr Chang0fffa7e2012-04-12 19:43:53 +080059 init::Initialize();
Zonr Chang0fffa7e2012-04-12 19:43:53 +080060}
61
62RSCompilerDriver::~RSCompilerDriver() {
63 delete mConfig;
64}
65
Zonr Chang0fffa7e2012-04-12 19:43:53 +080066
Stephen Hinesbde1a252014-05-15 18:02:33 -070067#if defined(PROVIDE_ARM_CODEGEN)
Stephen Hinesad694762013-04-29 18:59:47 -070068extern llvm::cl::opt<bool> EnableGlobalMerge;
Stephen Hinesc06cd062013-07-12 10:51:29 -070069#endif
70
Jean-Luc Brouillet0a2acce2017-02-17 13:29:47 -080071bool RSCompilerDriver::setupConfig(const Script &pScript) {
Zonr Chang0fffa7e2012-04-12 19:43:53 +080072 bool changed = false;
73
Jean-Luc Brouillet0a2acce2017-02-17 13:29:47 -080074 const llvm::CodeGenOpt::Level script_opt_level = pScript.getOptimizationLevel();
Zonr Chang0fffa7e2012-04-12 19:43:53 +080075
Stephen Hinesbde1a252014-05-15 18:02:33 -070076#if defined(PROVIDE_ARM_CODEGEN)
Stephen Hines045558b2014-02-18 14:07:15 -080077 EnableGlobalMerge = mEnableGlobalMerge;
78#endif
79
Chris Wailes900c6c12014-08-13 15:40:00 -070080 if (mConfig != nullptr) {
Zonr Chang0fffa7e2012-04-12 19:43:53 +080081 // Renderscript bitcode may have their optimization flag configuration
82 // different than the previous run of RS compilation.
83 if (mConfig->getOptimizationLevel() != script_opt_level) {
84 mConfig->setOptimizationLevel(script_opt_level);
85 changed = true;
86 }
87 } else {
88 // Haven't run the compiler ever.
Stephen Hinesbde1a252014-05-15 18:02:33 -070089 mConfig = new (std::nothrow) CompilerConfig(DEFAULT_TARGET_TRIPLE_STRING);
Chris Wailes900c6c12014-08-13 15:40:00 -070090 if (mConfig == nullptr) {
Zonr Chang0fffa7e2012-04-12 19:43:53 +080091 // Return false since mConfig remains NULL and out-of-memory.
92 return false;
93 }
94 mConfig->setOptimizationLevel(script_opt_level);
95 changed = true;
96 }
97
Stephen Hinesbde1a252014-05-15 18:02:33 -070098#if defined(PROVIDE_ARM_CODEGEN)
Pirama Arumuga Nainara65fba62015-02-19 11:01:30 -080099 bcinfo::MetadataExtractor me(&pScript.getSource().getModule());
100 if (!me.extract()) {
David Grossc2ca7422015-05-29 14:54:33 -0700101 bccAssert("Could not extract RS pragma metadata for module!");
Pirama Arumuga Nainara65fba62015-02-19 11:01:30 -0800102 }
103
104 bool script_full_prec = (me.getRSFloatPrecision() == bcinfo::RS_FP_Full);
Stephen Hinesbde1a252014-05-15 18:02:33 -0700105 if (mConfig->getFullPrecision() != script_full_prec) {
106 mConfig->setFullPrecision(script_full_prec);
107 changed = true;
Zonr Chang0fffa7e2012-04-12 19:43:53 +0800108 }
109#endif
110
111 return changed;
112}
113
Jean-Luc Brouillet0a2acce2017-02-17 13:29:47 -0800114Compiler::ErrorCode RSCompilerDriver::compileScript(Script& pScript, const char* pScriptName,
Jean-Luc Brouilletf2ac3172014-06-25 18:21:36 -0700115 const char* pOutputPath,
116 const char* pRuntimePath,
Pirama Arumuga Nainar51ee77b2015-02-19 16:33:27 -0800117 const char* pBuildChecksum,
Pirama Arumuga Nainara65fba62015-02-19 11:01:30 -0800118 bool pDumpIR) {
Pirama Arumuga Nainar51ee77b2015-02-19 16:33:27 -0800119 // embed build checksum metadata into the source
120 if (pBuildChecksum != nullptr && strlen(pBuildChecksum) > 0) {
121 pScript.getSource().addBuildChecksumMetadata(pBuildChecksum);
122 }
123
Pirama Arumuga Nainarebff2ea2015-05-21 15:45:05 -0700124 // Verify that the only external functions in pScript are Renderscript
125 // functions. Fail if verification returns an error.
126 if (mCompiler.screenGlobalFunctions(pScript) != Compiler::kSuccess) {
127 return Compiler::kErrInvalidSource;
128 }
129
Pirama Arumuga Nainard2d5ee32016-04-12 14:04:50 -0700130 // For (32-bit) x86, translate GEPs on structs or arrays of structs to GEPs on
131 // int8* with byte offsets. This is to ensure that layout of structs with
132 // 64-bit scalar fields matches frontend-generated code that adheres to ARM
133 // data layout.
134 //
135 // The translation is done before RenderScript runtime library is linked
136 // (during LinkRuntime below) to ensure that RenderScript-driver-provided
137 // structs (like Allocation_t) don't get forced into using the ARM layout
138 // rules.
139 if (mCompiler.getTargetMachine().getTargetTriple().getArch() == llvm::Triple::x86) {
140 mCompiler.translateGEPs(pScript);
141 }
142
Zonr Chang0fffa7e2012-04-12 19:43:53 +0800143 //===--------------------------------------------------------------------===//
Stephen Hinese198abe2012-07-27 18:05:41 -0700144 // Link RS script with Renderscript runtime.
Shih-wei Liaoba420642012-06-30 11:27:37 -0700145 //===--------------------------------------------------------------------===//
Jean-Luc Brouillet0a2acce2017-02-17 13:29:47 -0800146 if (!pScript.LinkRuntime(pRuntimePath)) {
Yang Ni6da4e252015-03-11 10:00:42 -0700147 ALOGE("Failed to link script '%s' with Renderscript runtime %s!",
148 pScriptName, pRuntimePath);
Stephen Hines47f0d5a2013-06-05 00:27:38 -0700149 return Compiler::kErrInvalidSource;
Shih-wei Liaoba420642012-06-30 11:27:37 -0700150 }
151
Stephen Hines01f05d42013-05-31 20:51:27 -0700152 {
Stephen Hines07843652013-08-15 15:41:47 -0700153 // FIXME(srhines): Windows compilation can't use locking like this, but
154 // we also don't need to worry about concurrent writers of the same file.
Elliott Hughes495e8062016-10-05 09:47:59 -0700155#ifndef _WIN32
Stephen Hines07843652013-08-15 15:41:47 -0700156 //===------------------------------------------------------------------===//
Stephen Hines01f05d42013-05-31 20:51:27 -0700157 // Acquire the write lock for writing output object file.
Stephen Hines07843652013-08-15 15:41:47 -0700158 //===------------------------------------------------------------------===//
Stephen Hines01f05d42013-05-31 20:51:27 -0700159 FileMutex<FileBase::kWriteLock> write_output_mutex(pOutputPath);
Zonr Chang0fffa7e2012-04-12 19:43:53 +0800160
Stephen Hines01f05d42013-05-31 20:51:27 -0700161 if (write_output_mutex.hasError() || !write_output_mutex.lock()) {
162 ALOGE("Unable to acquire the lock for writing %s! (%s)",
163 pOutputPath, write_output_mutex.getErrorMessage().c_str());
Stephen Hines47f0d5a2013-06-05 00:27:38 -0700164 return Compiler::kErrInvalidSource;
Zonr Chang0fffa7e2012-04-12 19:43:53 +0800165 }
Stephen Hinesd7a95262013-08-08 16:03:19 -0700166#endif
Zonr Chang0fffa7e2012-04-12 19:43:53 +0800167
Stephen Hines01f05d42013-05-31 20:51:27 -0700168 // Open the output file for write.
Stephen Hinesacf9c9e2013-09-26 16:32:31 -0700169 OutputFile output_file(pOutputPath,
170 FileBase::kTruncate | FileBase::kBinary);
Stephen Hines01f05d42013-05-31 20:51:27 -0700171
172 if (output_file.hasError()) {
173 ALOGE("Unable to open %s for write! (%s)", pOutputPath,
174 output_file.getErrorMessage().c_str());
Stephen Hines47f0d5a2013-06-05 00:27:38 -0700175 return Compiler::kErrInvalidSource;
Stephen Hines01f05d42013-05-31 20:51:27 -0700176 }
177
178 // Setup the config to the compiler.
179 bool compiler_need_reconfigure = setupConfig(pScript);
180
Chris Wailes900c6c12014-08-13 15:40:00 -0700181 if (mConfig == nullptr) {
Stephen Hines01f05d42013-05-31 20:51:27 -0700182 ALOGE("Failed to setup config for RS compiler to compile %s!",
183 pOutputPath);
Stephen Hines47f0d5a2013-06-05 00:27:38 -0700184 return Compiler::kErrInvalidSource;
Stephen Hines01f05d42013-05-31 20:51:27 -0700185 }
186
187 if (compiler_need_reconfigure) {
188 Compiler::ErrorCode err = mCompiler.config(*mConfig);
189 if (err != Compiler::kSuccess) {
190 ALOGE("Failed to config the RS compiler for %s! (%s)",pOutputPath,
191 Compiler::GetErrorString(err));
Stephen Hines47f0d5a2013-06-05 00:27:38 -0700192 return Compiler::kErrInvalidSource;
Stephen Hines01f05d42013-05-31 20:51:27 -0700193 }
194 }
195
Chris Wailes900c6c12014-08-13 15:40:00 -0700196 OutputFile *ir_file = nullptr;
197 llvm::raw_fd_ostream *IRStream = nullptr;
Tobias Grosser27fb7ed2013-06-21 18:34:56 -0700198 if (pDumpIR) {
Chris Wailes35978e72014-08-11 14:01:47 -0700199 std::string path(pOutputPath);
Tobias Grosser27fb7ed2013-06-21 18:34:56 -0700200 path.append(".ll");
Chris Wailes35978e72014-08-11 14:01:47 -0700201 ir_file = new OutputFile(path.c_str(), FileBase::kTruncate);
Tobias Grosser27fb7ed2013-06-21 18:34:56 -0700202 IRStream = ir_file->dup();
203 }
204
Stephen Hines01f05d42013-05-31 20:51:27 -0700205 // Run the compiler.
Chris Wailes35978e72014-08-11 14:01:47 -0700206 Compiler::ErrorCode compile_result =
207 mCompiler.compile(pScript, output_file, IRStream);
Tobias Grosser27fb7ed2013-06-21 18:34:56 -0700208
209 if (ir_file) {
210 ir_file->close();
211 delete ir_file;
212 }
Stephen Hines01f05d42013-05-31 20:51:27 -0700213
214 if (compile_result != Compiler::kSuccess) {
215 ALOGE("Unable to compile the source to file %s! (%s)", pOutputPath,
216 Compiler::GetErrorString(compile_result));
Stephen Hines47f0d5a2013-06-05 00:27:38 -0700217 return Compiler::kErrInvalidSource;
Stephen Hines01f05d42013-05-31 20:51:27 -0700218 }
Zonr Chang0fffa7e2012-04-12 19:43:53 +0800219 }
220
Stephen Hines47f0d5a2013-06-05 00:27:38 -0700221 return Compiler::kSuccess;
Zonr Chang0fffa7e2012-04-12 19:43:53 +0800222}
223
Stephen Hines47f0d5a2013-06-05 00:27:38 -0700224bool RSCompilerDriver::build(BCCContext &pContext,
225 const char *pCacheDir,
226 const char *pResName,
227 const char *pBitcode,
228 size_t pBitcodeSize,
Pirama Arumuga Nainar51ee77b2015-02-19 16:33:27 -0800229 const char *pBuildChecksum,
Stephen Hines47f0d5a2013-06-05 00:27:38 -0700230 const char *pRuntimePath,
Tobias Grosser7b980e12013-06-20 10:12:13 -0700231 RSLinkRuntimeCallback pLinkRuntimeCallback,
232 bool pDumpIR) {
Tim Murrayc89f78b2013-05-09 11:57:12 -0700233 // android::StopWatch build_time("bcc: RSCompilerDriver::build time");
Shih-wei Liao7bcec852012-04-25 04:07:09 -0700234 //===--------------------------------------------------------------------===//
235 // Check parameters.
236 //===--------------------------------------------------------------------===//
Chris Wailes900c6c12014-08-13 15:40:00 -0700237 if ((pCacheDir == nullptr) || (pResName == nullptr)) {
Shih-wei Liao7bcec852012-04-25 04:07:09 -0700238 ALOGE("Invalid parameter passed to RSCompilerDriver::build()! (cache dir: "
239 "%s, resource name: %s)", ((pCacheDir) ? pCacheDir : "(null)"),
240 ((pResName) ? pResName : "(null)"));
Stephen Hines47f0d5a2013-06-05 00:27:38 -0700241 return false;
Shih-wei Liao7bcec852012-04-25 04:07:09 -0700242 }
243
Chris Wailes900c6c12014-08-13 15:40:00 -0700244 if ((pBitcode == nullptr) || (pBitcodeSize <= 0)) {
Shih-wei Liao7bcec852012-04-25 04:07:09 -0700245 ALOGE("No bitcode supplied! (bitcode: %p, size of bitcode: %u)",
246 pBitcode, static_cast<unsigned>(pBitcodeSize));
Stephen Hines47f0d5a2013-06-05 00:27:38 -0700247 return false;
Shih-wei Liao7bcec852012-04-25 04:07:09 -0700248 }
249
250 //===--------------------------------------------------------------------===//
Shih-wei Liao7bcec852012-04-25 04:07:09 -0700251 // Construct output path.
Shih-wei Liao7bcec852012-04-25 04:07:09 -0700252 // {pCacheDir}/{pResName}.o
Stephen Hinesb10c3a72013-08-07 23:15:22 -0700253 //===--------------------------------------------------------------------===//
254 llvm::SmallString<80> output_path(pCacheDir);
255 llvm::sys::path::append(output_path, pResName);
256 llvm::sys::path::replace_extension(output_path, ".o");
Shih-wei Liao7bcec852012-04-25 04:07:09 -0700257
258 //===--------------------------------------------------------------------===//
Shih-wei Liao7bcec852012-04-25 04:07:09 -0700259 // Load the bitcode and create script.
260 //===--------------------------------------------------------------------===//
261 Source *source = Source::CreateFromBuffer(pContext, pResName,
262 pBitcode, pBitcodeSize);
Chris Wailes900c6c12014-08-13 15:40:00 -0700263 if (source == nullptr) {
Stephen Hines47f0d5a2013-06-05 00:27:38 -0700264 return false;
Shih-wei Liao7bcec852012-04-25 04:07:09 -0700265 }
266
Jean-Luc Brouillet0a2acce2017-02-17 13:29:47 -0800267 Script script(source);
268 script.setOptimizationLevel(getConfig()->getOptimizationLevel());
Stephen Hinesc3437f02014-01-30 17:57:21 -0800269 if (pLinkRuntimeCallback) {
270 setLinkRuntimeCallback(pLinkRuntimeCallback);
271 }
Shih-wei Liao7bcec852012-04-25 04:07:09 -0700272
Jean-Luc Brouilletf2ac3172014-06-25 18:21:36 -0700273 script.setLinkRuntimeCallback(getLinkRuntimeCallback());
Stephen Hines06731a62013-02-12 19:29:42 -0800274
Stephen Hines750ee652015-04-16 16:24:18 -0700275 script.setEmbedGlobalInfo(mEmbedGlobalInfo);
276 script.setEmbedGlobalInfoSkipConstant(mEmbedGlobalInfoSkipConstant);
277
Shih-wei Liao7bcec852012-04-25 04:07:09 -0700278 // Read information from bitcode wrapper.
279 bcinfo::BitcodeWrapper wrapper(pBitcode, pBitcodeSize);
Jean-Luc Brouillet0a2acce2017-02-17 13:29:47 -0800280 script.setOptimizationLevel(static_cast<llvm::CodeGenOpt::Level>(
Jean-Luc Brouilletf2ac3172014-06-25 18:21:36 -0700281 wrapper.getOptimizationLevel()));
Shih-wei Liao7bcec852012-04-25 04:07:09 -0700282
Stephen Hinesc31e1012015-08-25 23:10:18 -0700283// Assertion-enabled builds can't compile legacy bitcode (due to the use of
284// getName() with anonymous structure definitions).
Colin Cross1d3ac562016-12-07 14:21:42 -0800285#ifdef _DEBUG
Stephen Hinesc31e1012015-08-25 23:10:18 -0700286 static const uint32_t kSlangMinimumFixedStructureNames = 2310;
287 uint32_t version = wrapper.getCompilerVersion();
288 if (version < kSlangMinimumFixedStructureNames) {
289 ALOGE("Found invalid legacy bitcode compiled with a version %u llvm-rs-cc "
290 "used with an assertion build", version);
291 ALOGE("Please recompile this apk with a more recent llvm-rs-cc "
292 "(at least %u)", kSlangMinimumFixedStructureNames);
293 return false;
294 }
295#endif
296
Shih-wei Liao7bcec852012-04-25 04:07:09 -0700297 //===--------------------------------------------------------------------===//
298 // Compile the script
299 //===--------------------------------------------------------------------===//
Jean-Luc Brouilletf2ac3172014-06-25 18:21:36 -0700300 Compiler::ErrorCode status = compileScript(script, pResName,
Stephen Hines47f0d5a2013-06-05 00:27:38 -0700301 output_path.c_str(),
Pirama Arumuga Nainara65fba62015-02-19 11:01:30 -0800302 pRuntimePath,
303 pBuildChecksum,
304 pDumpIR);
Shih-wei Liao7bcec852012-04-25 04:07:09 -0700305
Jean-Luc Brouilletf2ac3172014-06-25 18:21:36 -0700306 return status == Compiler::kSuccess;
Zonr Chang0fffa7e2012-04-12 19:43:53 +0800307}
Stephen Hines331310e2012-10-26 19:27:55 -0700308
Yang Nia4ded132014-11-17 17:44:08 -0800309bool RSCompilerDriver::buildScriptGroup(
Yang Ni0ab50b82015-02-16 12:31:56 -0800310 BCCContext& Context, const char* pOutputFilepath, const char* pRuntimePath,
Yang Ni186d2f32015-04-07 14:51:47 -0700311 const char* pRuntimeRelaxedPath, bool dumpIR, const char* buildChecksum,
Yang Ni6da4e252015-03-11 10:00:42 -0700312 const std::vector<Source*>& sources,
Yang Ni0ab50b82015-02-16 12:31:56 -0800313 const std::list<std::list<std::pair<int, int>>>& toFuse,
314 const std::list<std::string>& fused,
315 const std::list<std::list<std::pair<int, int>>>& invokes,
316 const std::list<std::string>& invokeBatchNames) {
Pirama Arumuga Nainarf229c402016-03-06 23:05:45 -0800317
318 // Read and store metadata before linking the modules together
319 std::vector<bcinfo::MetadataExtractor*> metadata;
320 for (Source* source : sources) {
321 if (!source->extractMetadata()) {
322 ALOGE("Cannot extract metadata from module");
323 return false;
324 }
325 }
326
Yang Ni0ab50b82015-02-16 12:31:56 -0800327 // ---------------------------------------------------------------------------
328 // Link all input modules into a single module
329 // ---------------------------------------------------------------------------
330
331 llvm::LLVMContext& context = Context.getLLVMContext();
332 llvm::Module module("Merged Script Group", context);
333
Pirama Arumuga Nainarf229c402016-03-06 23:05:45 -0800334 llvm::Linker linker(module);
Yang Ni0ab50b82015-02-16 12:31:56 -0800335 for (Source* source : sources) {
Pirama Arumuga Nainarf229c402016-03-06 23:05:45 -0800336 std::unique_ptr<llvm::Module> sourceModule(&source->getModule());
337 if (linker.linkInModule(std::move(sourceModule))) {
Yang Ni0ab50b82015-02-16 12:31:56 -0800338 ALOGE("Linking for module in source failed.");
339 return false;
340 }
Pirama Arumuga Nainarf229c402016-03-06 23:05:45 -0800341 // source->getModule() is destroyed after linking.
342 source->markModuleDestroyed();
Yang Nia4ded132014-11-17 17:44:08 -0800343 }
344
Yang Ni0ab50b82015-02-16 12:31:56 -0800345 // ---------------------------------------------------------------------------
346 // Create fused kernels
347 // ---------------------------------------------------------------------------
348
349 auto inputIter = toFuse.begin();
350 for (const std::string& nameOfFused : fused) {
351 auto inputKernels = *inputIter++;
352 std::vector<Source*> sourcesToFuse;
353 std::vector<int> slots;
354
355 for (auto p : inputKernels) {
356 sourcesToFuse.push_back(sources[p.first]);
357 slots.push_back(p.second);
358 }
359
360 if (!fuseKernels(Context, sourcesToFuse, slots, nameOfFused, &module)) {
361 return false;
362 }
363 }
364
365 // ---------------------------------------------------------------------------
366 // Rename invokes
367 // ---------------------------------------------------------------------------
368
369 auto invokeIter = invokes.begin();
370 for (const std::string& newName : invokeBatchNames) {
371 auto inputInvoke = *invokeIter++;
372 auto p = inputInvoke.front();
373 Source* source = sources[p.first];
374 int slot = p.second;
375
376 if (!renameInvoke(Context, source, slot, newName, &module)) {
377 return false;
378 }
379 }
380
381 // ---------------------------------------------------------------------------
382 // Compile the new module with fused kernels
383 // ---------------------------------------------------------------------------
384
Yang Nia4ded132014-11-17 17:44:08 -0800385 const std::unique_ptr<Source> source(
Yang Ni0ab50b82015-02-16 12:31:56 -0800386 Source::CreateFromModule(Context, pOutputFilepath, module, true));
Jean-Luc Brouillet0a2acce2017-02-17 13:29:47 -0800387 Script script(source.get());
Yang Nia4ded132014-11-17 17:44:08 -0800388
Yang Ni0ab50b82015-02-16 12:31:56 -0800389 // Embed the info string directly in the ELF
390 script.setEmbedInfo(true);
Jean-Luc Brouillet0a2acce2017-02-17 13:29:47 -0800391 // TODO jeanluc Should we override the config's optimization?
392 // i.e., why not script.setOptimizationLevel(getConfig()->getOptimizationLevel)?
393 script.setOptimizationLevel(llvm::CodeGenOpt::Level::Aggressive);
Stephen Hines750ee652015-04-16 16:24:18 -0700394 script.setEmbedGlobalInfo(mEmbedGlobalInfo);
395 script.setEmbedGlobalInfoSkipConstant(mEmbedGlobalInfoSkipConstant);
Yang Nia4ded132014-11-17 17:44:08 -0800396
397 llvm::SmallString<80> output_path(pOutputFilepath);
398 llvm::sys::path::replace_extension(output_path, ".o");
399
Yang Ni6da4e252015-03-11 10:00:42 -0700400 // Pick the right runtime lib
401 const char* coreLibPath = pRuntimePath;
402 if (strcmp(pRuntimeRelaxedPath, "")) {
403 bcinfo::MetadataExtractor me(&module);
404 me.extract();
405 if (me.getRSFloatPrecision() == bcinfo::RS_FP_Relaxed) {
406 coreLibPath = pRuntimeRelaxedPath;
407 }
408 }
409
410 compileScript(script, pOutputFilepath, output_path.c_str(), coreLibPath,
Pirama Arumuga Nainara65fba62015-02-19 11:01:30 -0800411 buildChecksum, dumpIR);
Yang Nia4ded132014-11-17 17:44:08 -0800412
413 return true;
414}
Stephen Hines331310e2012-10-26 19:27:55 -0700415
Jean-Luc Brouillet0a2acce2017-02-17 13:29:47 -0800416bool RSCompilerDriver::buildForCompatLib(Script &pScript, const char *pOut,
Pirama Arumuga Nainar51ee77b2015-02-19 16:33:27 -0800417 const char *pBuildChecksum,
Tim Murray77c1d552015-01-22 15:15:17 -0800418 const char *pRuntimePath,
419 bool pDumpIR) {
Stephen Hines86a0b792012-11-06 20:04:47 -0800420 // Embed the info string directly in the ELF, since this path is for an
421 // offline (host) compilation.
422 pScript.setEmbedInfo(true);
423
Stephen Hines750ee652015-04-16 16:24:18 -0700424 pScript.setEmbedGlobalInfo(mEmbedGlobalInfo);
425 pScript.setEmbedGlobalInfoSkipConstant(mEmbedGlobalInfoSkipConstant);
Stephen Hinesefb3af32015-07-29 00:34:30 -0700426 pScript.setLinkRuntimeCallback(getLinkRuntimeCallback());
Stephen Hines750ee652015-04-16 16:24:18 -0700427
Yang Ni6da4e252015-03-11 10:00:42 -0700428 Compiler::ErrorCode status = compileScript(pScript, pOut, pOut, pRuntimePath,
Pirama Arumuga Nainara65fba62015-02-19 11:01:30 -0800429 pBuildChecksum, pDumpIR);
Stephen Hines47f0d5a2013-06-05 00:27:38 -0700430 if (status != Compiler::kSuccess) {
431 return false;
432 }
433
434 return true;
Stephen Hines331310e2012-10-26 19:27:55 -0700435}