blob: be913437a6fd0aa03debe87cf22dd6003d69809f [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"
Jean-Luc Brouilleta2dd52f2017-02-16 20:57:26 -080021#include "Log.h"
Jean-Luc Brouilleta2dd52f2017-02-16 20:57:26 -080022#include "RSScriptGroupFusion.h"
David Gross97e50992017-03-29 20:52:30 +000023#include "slang_version.h"
Jean-Luc Brouilleta2dd52f2017-02-16 20:57:26 -080024
25#include "bcc/BCCContext.h"
26#include "bcc/Compiler.h"
27#include "bcc/CompilerConfig.h"
28#include "bcc/Config.h"
29#include "bcc/Initialization.h"
Jean-Luc Brouillet0a2acce2017-02-17 13:29:47 -080030#include "bcc/Script.h"
Jean-Luc Brouilleta2dd52f2017-02-16 20:57:26 -080031#include "bcc/Source.h"
32#include "bcinfo/BitcodeWrapper.h"
33#include "bcinfo/MetadataExtractor.h"
Zonr Chang0fffa7e2012-04-12 19:43:53 +080034
Pirama Arumuga Nainarf229c402016-03-06 23:05:45 -080035#include "llvm/ADT/STLExtras.h"
Yang Nia4ded132014-11-17 17:44:08 -080036#include "llvm/IR/AssemblyAnnotationWriter.h"
Tobias Grosser7b980e12013-06-20 10:12:13 -070037#include <llvm/IR/Module.h>
Yang Ni0ab50b82015-02-16 12:31:56 -080038#include "llvm/Linker/Linker.h"
Stephen Hinesad694762013-04-29 18:59:47 -070039#include <llvm/Support/CommandLine.h>
Jean-Luc Brouillet295e50f2017-02-21 17:19:21 -080040#include <llvm/Support/FileSystem.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
Zonr Chang0fffa7e2012-04-12 19:43:53 +080048using namespace bcc;
49
David Grossefe14342016-02-18 11:46:57 -080050RSCompilerDriver::RSCompilerDriver() :
Chris Wailes900c6c12014-08-13 15:40:00 -070051 mConfig(nullptr), mCompiler(), mDebugContext(false),
Stephen Hines750ee652015-04-16 16:24:18 -070052 mLinkRuntimeCallback(nullptr), mEnableGlobalMerge(true),
53 mEmbedGlobalInfo(false), mEmbedGlobalInfoSkipConstant(false) {
Zonr Chang0fffa7e2012-04-12 19:43:53 +080054 init::Initialize();
Zonr Chang0fffa7e2012-04-12 19:43:53 +080055}
56
57RSCompilerDriver::~RSCompilerDriver() {
58 delete mConfig;
59}
60
Zonr Chang0fffa7e2012-04-12 19:43:53 +080061
Stephen Hinesbde1a252014-05-15 18:02:33 -070062#if defined(PROVIDE_ARM_CODEGEN)
Stephen Hinesad694762013-04-29 18:59:47 -070063extern llvm::cl::opt<bool> EnableGlobalMerge;
Stephen Hinesc06cd062013-07-12 10:51:29 -070064#endif
65
Jean-Luc Brouillet0a2acce2017-02-17 13:29:47 -080066bool RSCompilerDriver::setupConfig(const Script &pScript) {
Zonr Chang0fffa7e2012-04-12 19:43:53 +080067 bool changed = false;
68
Jean-Luc Brouillet0a2acce2017-02-17 13:29:47 -080069 const llvm::CodeGenOpt::Level script_opt_level = pScript.getOptimizationLevel();
Zonr Chang0fffa7e2012-04-12 19:43:53 +080070
Stephen Hinesbde1a252014-05-15 18:02:33 -070071#if defined(PROVIDE_ARM_CODEGEN)
Stephen Hines045558b2014-02-18 14:07:15 -080072 EnableGlobalMerge = mEnableGlobalMerge;
73#endif
74
Chris Wailes900c6c12014-08-13 15:40:00 -070075 if (mConfig != nullptr) {
Zonr Chang0fffa7e2012-04-12 19:43:53 +080076 // Renderscript bitcode may have their optimization flag configuration
77 // different than the previous run of RS compilation.
78 if (mConfig->getOptimizationLevel() != script_opt_level) {
79 mConfig->setOptimizationLevel(script_opt_level);
80 changed = true;
81 }
82 } else {
83 // Haven't run the compiler ever.
Stephen Hinesbde1a252014-05-15 18:02:33 -070084 mConfig = new (std::nothrow) CompilerConfig(DEFAULT_TARGET_TRIPLE_STRING);
Chris Wailes900c6c12014-08-13 15:40:00 -070085 if (mConfig == nullptr) {
Zonr Chang0fffa7e2012-04-12 19:43:53 +080086 // Return false since mConfig remains NULL and out-of-memory.
87 return false;
88 }
89 mConfig->setOptimizationLevel(script_opt_level);
90 changed = true;
91 }
92
Stephen Hinesbde1a252014-05-15 18:02:33 -070093#if defined(PROVIDE_ARM_CODEGEN)
Pirama Arumuga Nainara65fba62015-02-19 11:01:30 -080094 bcinfo::MetadataExtractor me(&pScript.getSource().getModule());
95 if (!me.extract()) {
David Grossc2ca7422015-05-29 14:54:33 -070096 bccAssert("Could not extract RS pragma metadata for module!");
Pirama Arumuga Nainara65fba62015-02-19 11:01:30 -080097 }
98
99 bool script_full_prec = (me.getRSFloatPrecision() == bcinfo::RS_FP_Full);
Stephen Hinesbde1a252014-05-15 18:02:33 -0700100 if (mConfig->getFullPrecision() != script_full_prec) {
101 mConfig->setFullPrecision(script_full_prec);
102 changed = true;
Zonr Chang0fffa7e2012-04-12 19:43:53 +0800103 }
104#endif
105
106 return changed;
107}
108
Jean-Luc Brouillet0a2acce2017-02-17 13:29:47 -0800109Compiler::ErrorCode RSCompilerDriver::compileScript(Script& pScript, const char* pScriptName,
Jean-Luc Brouilletf2ac3172014-06-25 18:21:36 -0700110 const char* pOutputPath,
111 const char* pRuntimePath,
Pirama Arumuga Nainar51ee77b2015-02-19 16:33:27 -0800112 const char* pBuildChecksum,
Pirama Arumuga Nainara65fba62015-02-19 11:01:30 -0800113 bool pDumpIR) {
Pirama Arumuga Nainar51ee77b2015-02-19 16:33:27 -0800114 // embed build checksum metadata into the source
115 if (pBuildChecksum != nullptr && strlen(pBuildChecksum) > 0) {
116 pScript.getSource().addBuildChecksumMetadata(pBuildChecksum);
117 }
118
Pirama Arumuga Nainarebff2ea2015-05-21 15:45:05 -0700119 // Verify that the only external functions in pScript are Renderscript
120 // functions. Fail if verification returns an error.
121 if (mCompiler.screenGlobalFunctions(pScript) != Compiler::kSuccess) {
122 return Compiler::kErrInvalidSource;
123 }
124
Pirama Arumuga Nainard2d5ee32016-04-12 14:04:50 -0700125 // For (32-bit) x86, translate GEPs on structs or arrays of structs to GEPs on
126 // int8* with byte offsets. This is to ensure that layout of structs with
127 // 64-bit scalar fields matches frontend-generated code that adheres to ARM
128 // data layout.
129 //
130 // The translation is done before RenderScript runtime library is linked
131 // (during LinkRuntime below) to ensure that RenderScript-driver-provided
132 // structs (like Allocation_t) don't get forced into using the ARM layout
133 // rules.
David Gross97e50992017-03-29 20:52:30 +0000134 if (!pScript.isStructExplicitlyPaddedBySlang() &&
135 (mCompiler.getTargetMachine().getTargetTriple().getArch() == llvm::Triple::x86)) {
Pirama Arumuga Nainard2d5ee32016-04-12 14:04:50 -0700136 mCompiler.translateGEPs(pScript);
137 }
138
Zonr Chang0fffa7e2012-04-12 19:43:53 +0800139 //===--------------------------------------------------------------------===//
Stephen Hinese198abe2012-07-27 18:05:41 -0700140 // Link RS script with Renderscript runtime.
Shih-wei Liaoba420642012-06-30 11:27:37 -0700141 //===--------------------------------------------------------------------===//
Jean-Luc Brouillet0a2acce2017-02-17 13:29:47 -0800142 if (!pScript.LinkRuntime(pRuntimePath)) {
Yang Ni6da4e252015-03-11 10:00:42 -0700143 ALOGE("Failed to link script '%s' with Renderscript runtime %s!",
144 pScriptName, pRuntimePath);
Stephen Hines47f0d5a2013-06-05 00:27:38 -0700145 return Compiler::kErrInvalidSource;
Shih-wei Liaoba420642012-06-30 11:27:37 -0700146 }
147
Stephen Hines01f05d42013-05-31 20:51:27 -0700148 {
Stephen Hines07843652013-08-15 15:41:47 -0700149 // FIXME(srhines): Windows compilation can't use locking like this, but
150 // we also don't need to worry about concurrent writers of the same file.
Elliott Hughes495e8062016-10-05 09:47:59 -0700151#ifndef _WIN32
Stephen Hines07843652013-08-15 15:41:47 -0700152 //===------------------------------------------------------------------===//
Stephen Hines01f05d42013-05-31 20:51:27 -0700153 // Acquire the write lock for writing output object file.
Stephen Hines07843652013-08-15 15:41:47 -0700154 //===------------------------------------------------------------------===//
Jean-Luc Brouillet84864982017-02-20 14:19:38 -0800155 FileMutex write_output_mutex(pOutputPath);
Zonr Chang0fffa7e2012-04-12 19:43:53 +0800156
Jean-Luc Brouillet84864982017-02-20 14:19:38 -0800157 if (write_output_mutex.hasError() || !write_output_mutex.lockMutex()) {
Stephen Hines01f05d42013-05-31 20:51:27 -0700158 ALOGE("Unable to acquire the lock for writing %s! (%s)",
159 pOutputPath, write_output_mutex.getErrorMessage().c_str());
Jean-Luc Brouillet295e50f2017-02-21 17:19:21 -0800160 return Compiler::kErrInvalidOutputFileState;
Zonr Chang0fffa7e2012-04-12 19:43:53 +0800161 }
Stephen Hinesd7a95262013-08-08 16:03:19 -0700162#endif
Zonr Chang0fffa7e2012-04-12 19:43:53 +0800163
Stephen Hines01f05d42013-05-31 20:51:27 -0700164 // Open the output file for write.
Jean-Luc Brouillet295e50f2017-02-21 17:19:21 -0800165 std::error_code error;
166 llvm::raw_fd_ostream out_stream(pOutputPath, error, llvm::sys::fs::F_RW);
167 if (error) {
168 ALOGE("Unable to open %s for write! (%s)", pOutputPath,
169 error.message().c_str());
170 return Compiler::kErrPrepareOutput;
Stephen Hines01f05d42013-05-31 20:51:27 -0700171 }
172
173 // Setup the config to the compiler.
174 bool compiler_need_reconfigure = setupConfig(pScript);
175
Chris Wailes900c6c12014-08-13 15:40:00 -0700176 if (mConfig == nullptr) {
Stephen Hines01f05d42013-05-31 20:51:27 -0700177 ALOGE("Failed to setup config for RS compiler to compile %s!",
178 pOutputPath);
Stephen Hines47f0d5a2013-06-05 00:27:38 -0700179 return Compiler::kErrInvalidSource;
Stephen Hines01f05d42013-05-31 20:51:27 -0700180 }
181
182 if (compiler_need_reconfigure) {
183 Compiler::ErrorCode err = mCompiler.config(*mConfig);
184 if (err != Compiler::kSuccess) {
185 ALOGE("Failed to config the RS compiler for %s! (%s)",pOutputPath,
186 Compiler::GetErrorString(err));
Stephen Hines47f0d5a2013-06-05 00:27:38 -0700187 return Compiler::kErrInvalidSource;
Stephen Hines01f05d42013-05-31 20:51:27 -0700188 }
189 }
190
Jean-Luc Brouillet295e50f2017-02-21 17:19:21 -0800191 std::unique_ptr<llvm::raw_fd_ostream> IRStream;
Tobias Grosser27fb7ed2013-06-21 18:34:56 -0700192 if (pDumpIR) {
Chris Wailes35978e72014-08-11 14:01:47 -0700193 std::string path(pOutputPath);
Tobias Grosser27fb7ed2013-06-21 18:34:56 -0700194 path.append(".ll");
Jean-Luc Brouillet295e50f2017-02-21 17:19:21 -0800195 IRStream.reset(new llvm::raw_fd_ostream(
196 path.c_str(), error, llvm::sys::fs::F_RW | llvm::sys::fs::F_Text));
197 if (error) {
198 ALOGE("Unable to open %s for write! (%s)", path.c_str(),
199 error.message().c_str());
200 return Compiler::kErrPrepareOutput;
201 }
Tobias Grosser27fb7ed2013-06-21 18:34:56 -0700202 }
203
Stephen Hines01f05d42013-05-31 20:51:27 -0700204 // Run the compiler.
Chris Wailes35978e72014-08-11 14:01:47 -0700205 Compiler::ErrorCode compile_result =
Jean-Luc Brouillet295e50f2017-02-21 17:19:21 -0800206 mCompiler.compile(pScript, out_stream, IRStream.get());
Stephen Hines01f05d42013-05-31 20:51:27 -0700207
208 if (compile_result != Compiler::kSuccess) {
209 ALOGE("Unable to compile the source to file %s! (%s)", pOutputPath,
210 Compiler::GetErrorString(compile_result));
Stephen Hines47f0d5a2013-06-05 00:27:38 -0700211 return Compiler::kErrInvalidSource;
Stephen Hines01f05d42013-05-31 20:51:27 -0700212 }
Zonr Chang0fffa7e2012-04-12 19:43:53 +0800213 }
214
Stephen Hines47f0d5a2013-06-05 00:27:38 -0700215 return Compiler::kSuccess;
Zonr Chang0fffa7e2012-04-12 19:43:53 +0800216}
217
Stephen Hines47f0d5a2013-06-05 00:27:38 -0700218bool RSCompilerDriver::build(BCCContext &pContext,
219 const char *pCacheDir,
220 const char *pResName,
221 const char *pBitcode,
222 size_t pBitcodeSize,
Pirama Arumuga Nainar51ee77b2015-02-19 16:33:27 -0800223 const char *pBuildChecksum,
Stephen Hines47f0d5a2013-06-05 00:27:38 -0700224 const char *pRuntimePath,
Tobias Grosser7b980e12013-06-20 10:12:13 -0700225 RSLinkRuntimeCallback pLinkRuntimeCallback,
226 bool pDumpIR) {
Tim Murrayc89f78b2013-05-09 11:57:12 -0700227 // android::StopWatch build_time("bcc: RSCompilerDriver::build time");
Shih-wei Liao7bcec852012-04-25 04:07:09 -0700228 //===--------------------------------------------------------------------===//
229 // Check parameters.
230 //===--------------------------------------------------------------------===//
Chris Wailes900c6c12014-08-13 15:40:00 -0700231 if ((pCacheDir == nullptr) || (pResName == nullptr)) {
Shih-wei Liao7bcec852012-04-25 04:07:09 -0700232 ALOGE("Invalid parameter passed to RSCompilerDriver::build()! (cache dir: "
233 "%s, resource name: %s)", ((pCacheDir) ? pCacheDir : "(null)"),
234 ((pResName) ? pResName : "(null)"));
Stephen Hines47f0d5a2013-06-05 00:27:38 -0700235 return false;
Shih-wei Liao7bcec852012-04-25 04:07:09 -0700236 }
237
Chris Wailes900c6c12014-08-13 15:40:00 -0700238 if ((pBitcode == nullptr) || (pBitcodeSize <= 0)) {
Shih-wei Liao7bcec852012-04-25 04:07:09 -0700239 ALOGE("No bitcode supplied! (bitcode: %p, size of bitcode: %u)",
240 pBitcode, static_cast<unsigned>(pBitcodeSize));
Stephen Hines47f0d5a2013-06-05 00:27:38 -0700241 return false;
Shih-wei Liao7bcec852012-04-25 04:07:09 -0700242 }
243
244 //===--------------------------------------------------------------------===//
Shih-wei Liao7bcec852012-04-25 04:07:09 -0700245 // Construct output path.
Shih-wei Liao7bcec852012-04-25 04:07:09 -0700246 // {pCacheDir}/{pResName}.o
Stephen Hinesb10c3a72013-08-07 23:15:22 -0700247 //===--------------------------------------------------------------------===//
248 llvm::SmallString<80> output_path(pCacheDir);
249 llvm::sys::path::append(output_path, pResName);
250 llvm::sys::path::replace_extension(output_path, ".o");
Shih-wei Liao7bcec852012-04-25 04:07:09 -0700251
252 //===--------------------------------------------------------------------===//
Shih-wei Liao7bcec852012-04-25 04:07:09 -0700253 // Load the bitcode and create script.
254 //===--------------------------------------------------------------------===//
255 Source *source = Source::CreateFromBuffer(pContext, pResName,
256 pBitcode, pBitcodeSize);
Chris Wailes900c6c12014-08-13 15:40:00 -0700257 if (source == nullptr) {
Stephen Hines47f0d5a2013-06-05 00:27:38 -0700258 return false;
Shih-wei Liao7bcec852012-04-25 04:07:09 -0700259 }
260
Jean-Luc Brouillet0a2acce2017-02-17 13:29:47 -0800261 Script script(source);
262 script.setOptimizationLevel(getConfig()->getOptimizationLevel());
Stephen Hinesc3437f02014-01-30 17:57:21 -0800263 if (pLinkRuntimeCallback) {
264 setLinkRuntimeCallback(pLinkRuntimeCallback);
265 }
Shih-wei Liao7bcec852012-04-25 04:07:09 -0700266
Jean-Luc Brouilletf2ac3172014-06-25 18:21:36 -0700267 script.setLinkRuntimeCallback(getLinkRuntimeCallback());
Stephen Hines06731a62013-02-12 19:29:42 -0800268
Stephen Hines750ee652015-04-16 16:24:18 -0700269 script.setEmbedGlobalInfo(mEmbedGlobalInfo);
270 script.setEmbedGlobalInfoSkipConstant(mEmbedGlobalInfoSkipConstant);
271
David Grosscf5afcb2017-03-20 15:23:26 -0700272 // Read optimization level from bitcode wrapper.
Shih-wei Liao7bcec852012-04-25 04:07:09 -0700273 bcinfo::BitcodeWrapper wrapper(pBitcode, pBitcodeSize);
Jean-Luc Brouillet0a2acce2017-02-17 13:29:47 -0800274 script.setOptimizationLevel(static_cast<llvm::CodeGenOpt::Level>(
Jean-Luc Brouilletf2ac3172014-06-25 18:21:36 -0700275 wrapper.getOptimizationLevel()));
Shih-wei Liao7bcec852012-04-25 04:07:09 -0700276
Stephen Hinesc31e1012015-08-25 23:10:18 -0700277// Assertion-enabled builds can't compile legacy bitcode (due to the use of
278// getName() with anonymous structure definitions).
Colin Cross1d3ac562016-12-07 14:21:42 -0800279#ifdef _DEBUG
David Gross97e50992017-03-29 20:52:30 +0000280 static const uint32_t kSlangMinimumFixedStructureNames = SlangVersion::M_RS_OBJECT;
Stephen Hinesc31e1012015-08-25 23:10:18 -0700281 uint32_t version = wrapper.getCompilerVersion();
282 if (version < kSlangMinimumFixedStructureNames) {
283 ALOGE("Found invalid legacy bitcode compiled with a version %u llvm-rs-cc "
284 "used with an assertion build", version);
285 ALOGE("Please recompile this apk with a more recent llvm-rs-cc "
286 "(at least %u)", kSlangMinimumFixedStructureNames);
287 return false;
288 }
289#endif
290
Shih-wei Liao7bcec852012-04-25 04:07:09 -0700291 //===--------------------------------------------------------------------===//
292 // Compile the script
293 //===--------------------------------------------------------------------===//
Jean-Luc Brouilletf2ac3172014-06-25 18:21:36 -0700294 Compiler::ErrorCode status = compileScript(script, pResName,
Stephen Hines47f0d5a2013-06-05 00:27:38 -0700295 output_path.c_str(),
Pirama Arumuga Nainara65fba62015-02-19 11:01:30 -0800296 pRuntimePath,
297 pBuildChecksum,
298 pDumpIR);
Shih-wei Liao7bcec852012-04-25 04:07:09 -0700299
Jean-Luc Brouilletf2ac3172014-06-25 18:21:36 -0700300 return status == Compiler::kSuccess;
Zonr Chang0fffa7e2012-04-12 19:43:53 +0800301}
Stephen Hines331310e2012-10-26 19:27:55 -0700302
Yang Nia4ded132014-11-17 17:44:08 -0800303bool RSCompilerDriver::buildScriptGroup(
Yang Ni0ab50b82015-02-16 12:31:56 -0800304 BCCContext& Context, const char* pOutputFilepath, const char* pRuntimePath,
Yang Ni186d2f32015-04-07 14:51:47 -0700305 const char* pRuntimeRelaxedPath, bool dumpIR, const char* buildChecksum,
Yang Ni6da4e252015-03-11 10:00:42 -0700306 const std::vector<Source*>& sources,
Yang Ni0ab50b82015-02-16 12:31:56 -0800307 const std::list<std::list<std::pair<int, int>>>& toFuse,
308 const std::list<std::string>& fused,
309 const std::list<std::list<std::pair<int, int>>>& invokes,
310 const std::list<std::string>& invokeBatchNames) {
Pirama Arumuga Nainarf229c402016-03-06 23:05:45 -0800311
312 // Read and store metadata before linking the modules together
313 std::vector<bcinfo::MetadataExtractor*> metadata;
314 for (Source* source : sources) {
315 if (!source->extractMetadata()) {
316 ALOGE("Cannot extract metadata from module");
317 return false;
318 }
319 }
320
Yang Ni0ab50b82015-02-16 12:31:56 -0800321 // ---------------------------------------------------------------------------
322 // Link all input modules into a single module
323 // ---------------------------------------------------------------------------
324
325 llvm::LLVMContext& context = Context.getLLVMContext();
326 llvm::Module module("Merged Script Group", context);
327
David Grosscf5afcb2017-03-20 15:23:26 -0700328 unsigned wrapperCompilerVersion = 0, wrapperOptimizationLevel = 0;
329 bool gotFirstSource = false;
Pirama Arumuga Nainarf229c402016-03-06 23:05:45 -0800330 llvm::Linker linker(module);
Yang Ni0ab50b82015-02-16 12:31:56 -0800331 for (Source* source : sources) {
David Grosscf5afcb2017-03-20 15:23:26 -0700332 unsigned sourceWrapperCompilerVersion, sourceWrapperOptimizationLevel;
333 source->getWrapperInformation(&sourceWrapperCompilerVersion, &sourceWrapperOptimizationLevel);
334 if (gotFirstSource) {
335 if ((wrapperCompilerVersion != sourceWrapperCompilerVersion) ||
336 (wrapperOptimizationLevel != sourceWrapperOptimizationLevel))
337 ALOGE("ScriptGroup source files have inconsistent metadata");
338 return false;
339 } else {
340 wrapperCompilerVersion = sourceWrapperCompilerVersion;
341 wrapperOptimizationLevel = sourceWrapperOptimizationLevel;
342 gotFirstSource = true;
343 }
Pirama Arumuga Nainarf229c402016-03-06 23:05:45 -0800344 std::unique_ptr<llvm::Module> sourceModule(&source->getModule());
345 if (linker.linkInModule(std::move(sourceModule))) {
Yang Ni0ab50b82015-02-16 12:31:56 -0800346 ALOGE("Linking for module in source failed.");
347 return false;
348 }
Pirama Arumuga Nainarf229c402016-03-06 23:05:45 -0800349 // source->getModule() is destroyed after linking.
350 source->markModuleDestroyed();
David Grosscf5afcb2017-03-20 15:23:26 -0700351 // linking copies metadata from source->getModule(), but we don't
352 // want the wrapper metadata (we'll be reconstructing this when we
353 // instantiate a Source instance from the new Module).
354 llvm::NamedMDNode *const wrapperMDNode =
355 module.getNamedMetadata(bcinfo::MetadataExtractor::kWrapperMetadataName);
356 bccAssert(wrapperMDNode != nullptr);
357 module.eraseNamedMetadata(wrapperMDNode);
Yang Nia4ded132014-11-17 17:44:08 -0800358 }
359
Yang Ni0ab50b82015-02-16 12:31:56 -0800360 // ---------------------------------------------------------------------------
361 // Create fused kernels
362 // ---------------------------------------------------------------------------
363
364 auto inputIter = toFuse.begin();
365 for (const std::string& nameOfFused : fused) {
366 auto inputKernels = *inputIter++;
367 std::vector<Source*> sourcesToFuse;
368 std::vector<int> slots;
369
370 for (auto p : inputKernels) {
371 sourcesToFuse.push_back(sources[p.first]);
372 slots.push_back(p.second);
373 }
374
375 if (!fuseKernels(Context, sourcesToFuse, slots, nameOfFused, &module)) {
376 return false;
377 }
378 }
379
380 // ---------------------------------------------------------------------------
381 // Rename invokes
382 // ---------------------------------------------------------------------------
383
384 auto invokeIter = invokes.begin();
385 for (const std::string& newName : invokeBatchNames) {
386 auto inputInvoke = *invokeIter++;
387 auto p = inputInvoke.front();
388 Source* source = sources[p.first];
389 int slot = p.second;
390
391 if (!renameInvoke(Context, source, slot, newName, &module)) {
392 return false;
393 }
394 }
395
396 // ---------------------------------------------------------------------------
397 // Compile the new module with fused kernels
398 // ---------------------------------------------------------------------------
399
Yang Nia4ded132014-11-17 17:44:08 -0800400 const std::unique_ptr<Source> source(
David Grosscf5afcb2017-03-20 15:23:26 -0700401 Source::CreateFromModule(Context, pOutputFilepath, module,
402 wrapperCompilerVersion, wrapperOptimizationLevel,
403 true));
Jean-Luc Brouillet0a2acce2017-02-17 13:29:47 -0800404 Script script(source.get());
Yang Nia4ded132014-11-17 17:44:08 -0800405
Yang Ni0ab50b82015-02-16 12:31:56 -0800406 // Embed the info string directly in the ELF
407 script.setEmbedInfo(true);
Jean-Luc Brouillet0a2acce2017-02-17 13:29:47 -0800408 // TODO jeanluc Should we override the config's optimization?
409 // i.e., why not script.setOptimizationLevel(getConfig()->getOptimizationLevel)?
410 script.setOptimizationLevel(llvm::CodeGenOpt::Level::Aggressive);
Stephen Hines750ee652015-04-16 16:24:18 -0700411 script.setEmbedGlobalInfo(mEmbedGlobalInfo);
412 script.setEmbedGlobalInfoSkipConstant(mEmbedGlobalInfoSkipConstant);
Yang Nia4ded132014-11-17 17:44:08 -0800413
414 llvm::SmallString<80> output_path(pOutputFilepath);
415 llvm::sys::path::replace_extension(output_path, ".o");
416
Yang Ni6da4e252015-03-11 10:00:42 -0700417 // Pick the right runtime lib
418 const char* coreLibPath = pRuntimePath;
419 if (strcmp(pRuntimeRelaxedPath, "")) {
420 bcinfo::MetadataExtractor me(&module);
421 me.extract();
422 if (me.getRSFloatPrecision() == bcinfo::RS_FP_Relaxed) {
423 coreLibPath = pRuntimeRelaxedPath;
424 }
425 }
426
427 compileScript(script, pOutputFilepath, output_path.c_str(), coreLibPath,
Pirama Arumuga Nainara65fba62015-02-19 11:01:30 -0800428 buildChecksum, dumpIR);
Yang Nia4ded132014-11-17 17:44:08 -0800429
430 return true;
431}
Stephen Hines331310e2012-10-26 19:27:55 -0700432
Jean-Luc Brouillet0a2acce2017-02-17 13:29:47 -0800433bool RSCompilerDriver::buildForCompatLib(Script &pScript, const char *pOut,
Pirama Arumuga Nainar51ee77b2015-02-19 16:33:27 -0800434 const char *pBuildChecksum,
Tim Murray77c1d552015-01-22 15:15:17 -0800435 const char *pRuntimePath,
436 bool pDumpIR) {
Stephen Hines86a0b792012-11-06 20:04:47 -0800437 // Embed the info string directly in the ELF, since this path is for an
438 // offline (host) compilation.
439 pScript.setEmbedInfo(true);
440
Stephen Hines750ee652015-04-16 16:24:18 -0700441 pScript.setEmbedGlobalInfo(mEmbedGlobalInfo);
442 pScript.setEmbedGlobalInfoSkipConstant(mEmbedGlobalInfoSkipConstant);
Stephen Hinesefb3af32015-07-29 00:34:30 -0700443 pScript.setLinkRuntimeCallback(getLinkRuntimeCallback());
Stephen Hines750ee652015-04-16 16:24:18 -0700444
Yang Ni6da4e252015-03-11 10:00:42 -0700445 Compiler::ErrorCode status = compileScript(pScript, pOut, pOut, pRuntimePath,
Pirama Arumuga Nainara65fba62015-02-19 11:01:30 -0800446 pBuildChecksum, pDumpIR);
Stephen Hines47f0d5a2013-06-05 00:27:38 -0700447 if (status != Compiler::kSuccess) {
448 return false;
449 }
450
451 return true;
Stephen Hines331310e2012-10-26 19:27:55 -0700452}