RSCompilerDriver.cpp revision c31e101bed9d58be0388fead9c7344624978f580
1/*
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
17#include "bcc/Renderscript/RSCompilerDriver.h"
18
19#include "llvm/IR/AssemblyAnnotationWriter.h"
20#include <llvm/IR/Module.h>
21#include "llvm/Linker/Linker.h"
22#include <llvm/Support/CommandLine.h>
23#include <llvm/Support/Path.h>
24#include <llvm/Support/raw_ostream.h>
25
26#include "bcinfo/BitcodeWrapper.h"
27#include "bcc/Assert.h"
28#include "bcinfo/MetadataExtractor.h"
29#include "bcc/BCCContext.h"
30#include "bcc/Compiler.h"
31#include "bcc/Config/Config.h"
32#include "bcc/Renderscript/RSScript.h"
33#include "bcc/Renderscript/RSScriptGroupFusion.h"
34#include "bcc/Support/CompilerConfig.h"
35#include "bcc/Source.h"
36#include "bcc/Support/FileMutex.h"
37#include "bcc/Support/Log.h"
38#include "bcc/Support/InputFile.h"
39#include "bcc/Support/Initialization.h"
40#include "bcc/Support/OutputFile.h"
41
42#include <sstream>
43#include <string>
44
45#ifdef __ANDROID__
46#include <cutils/properties.h>
47#endif
48#include <utils/StopWatch.h>
49
50using namespace bcc;
51
52RSCompilerDriver::RSCompilerDriver(bool pUseCompilerRT) :
53    mConfig(nullptr), mCompiler(), mDebugContext(false),
54    mLinkRuntimeCallback(nullptr), mEnableGlobalMerge(true),
55    mEmbedGlobalInfo(false), mEmbedGlobalInfoSkipConstant(false) {
56  init::Initialize();
57}
58
59RSCompilerDriver::~RSCompilerDriver() {
60  delete mConfig;
61}
62
63
64#if defined(PROVIDE_ARM_CODEGEN)
65extern llvm::cl::opt<bool> EnableGlobalMerge;
66#endif
67
68bool RSCompilerDriver::setupConfig(const RSScript &pScript) {
69  bool changed = false;
70
71  const llvm::CodeGenOpt::Level script_opt_level =
72      static_cast<llvm::CodeGenOpt::Level>(pScript.getOptimizationLevel());
73
74#if defined(PROVIDE_ARM_CODEGEN)
75  EnableGlobalMerge = mEnableGlobalMerge;
76#endif
77
78  if (mConfig != nullptr) {
79    // Renderscript bitcode may have their optimization flag configuration
80    // different than the previous run of RS compilation.
81    if (mConfig->getOptimizationLevel() != script_opt_level) {
82      mConfig->setOptimizationLevel(script_opt_level);
83      changed = true;
84    }
85  } else {
86    // Haven't run the compiler ever.
87    mConfig = new (std::nothrow) CompilerConfig(DEFAULT_TARGET_TRIPLE_STRING);
88    if (mConfig == nullptr) {
89      // Return false since mConfig remains NULL and out-of-memory.
90      return false;
91    }
92    mConfig->setOptimizationLevel(script_opt_level);
93    changed = true;
94  }
95
96#if defined(PROVIDE_ARM_CODEGEN)
97  bcinfo::MetadataExtractor me(&pScript.getSource().getModule());
98  if (!me.extract()) {
99    bccAssert("Could not extract RS pragma metadata for module!");
100  }
101
102  bool script_full_prec = (me.getRSFloatPrecision() == bcinfo::RS_FP_Full);
103  if (mConfig->getFullPrecision() != script_full_prec) {
104    mConfig->setFullPrecision(script_full_prec);
105    changed = true;
106  }
107#endif
108
109  return changed;
110}
111
112Compiler::ErrorCode RSCompilerDriver::compileScript(RSScript& pScript, const char* pScriptName,
113                                                    const char* pOutputPath,
114                                                    const char* pRuntimePath,
115                                                    const char* pBuildChecksum,
116                                                    bool pDumpIR) {
117  // embed build checksum metadata into the source
118  if (pBuildChecksum != nullptr && strlen(pBuildChecksum) > 0) {
119    pScript.getSource().addBuildChecksumMetadata(pBuildChecksum);
120  }
121
122  // Verify that the only external functions in pScript are Renderscript
123  // functions.  Fail if verification returns an error.
124  if (mCompiler.screenGlobalFunctions(pScript) != Compiler::kSuccess) {
125    return Compiler::kErrInvalidSource;
126  }
127
128  //===--------------------------------------------------------------------===//
129  // Link RS script with Renderscript runtime.
130  //===--------------------------------------------------------------------===//
131  if (!RSScript::LinkRuntime(pScript, pRuntimePath)) {
132    ALOGE("Failed to link script '%s' with Renderscript runtime %s!",
133          pScriptName, pRuntimePath);
134    return Compiler::kErrInvalidSource;
135  }
136
137  {
138    // FIXME(srhines): Windows compilation can't use locking like this, but
139    // we also don't need to worry about concurrent writers of the same file.
140#ifndef USE_MINGW
141    //===------------------------------------------------------------------===//
142    // Acquire the write lock for writing output object file.
143    //===------------------------------------------------------------------===//
144    FileMutex<FileBase::kWriteLock> write_output_mutex(pOutputPath);
145
146    if (write_output_mutex.hasError() || !write_output_mutex.lock()) {
147      ALOGE("Unable to acquire the lock for writing %s! (%s)",
148            pOutputPath, write_output_mutex.getErrorMessage().c_str());
149      return Compiler::kErrInvalidSource;
150    }
151#endif
152
153    // Open the output file for write.
154    OutputFile output_file(pOutputPath,
155                           FileBase::kTruncate | FileBase::kBinary);
156
157    if (output_file.hasError()) {
158        ALOGE("Unable to open %s for write! (%s)", pOutputPath,
159              output_file.getErrorMessage().c_str());
160      return Compiler::kErrInvalidSource;
161    }
162
163    // Setup the config to the compiler.
164    bool compiler_need_reconfigure = setupConfig(pScript);
165
166    if (mConfig == nullptr) {
167      ALOGE("Failed to setup config for RS compiler to compile %s!",
168            pOutputPath);
169      return Compiler::kErrInvalidSource;
170    }
171
172    if (compiler_need_reconfigure) {
173      Compiler::ErrorCode err = mCompiler.config(*mConfig);
174      if (err != Compiler::kSuccess) {
175        ALOGE("Failed to config the RS compiler for %s! (%s)",pOutputPath,
176              Compiler::GetErrorString(err));
177        return Compiler::kErrInvalidSource;
178      }
179    }
180
181    OutputFile *ir_file = nullptr;
182    llvm::raw_fd_ostream *IRStream = nullptr;
183    if (pDumpIR) {
184      std::string path(pOutputPath);
185      path.append(".ll");
186      ir_file = new OutputFile(path.c_str(), FileBase::kTruncate);
187      IRStream = ir_file->dup();
188    }
189
190    // Run the compiler.
191    Compiler::ErrorCode compile_result =
192        mCompiler.compile(pScript, output_file, IRStream);
193
194    if (ir_file) {
195      ir_file->close();
196      delete ir_file;
197    }
198
199    if (compile_result != Compiler::kSuccess) {
200      ALOGE("Unable to compile the source to file %s! (%s)", pOutputPath,
201            Compiler::GetErrorString(compile_result));
202      return Compiler::kErrInvalidSource;
203    }
204  }
205
206  return Compiler::kSuccess;
207}
208
209bool RSCompilerDriver::build(BCCContext &pContext,
210                             const char *pCacheDir,
211                             const char *pResName,
212                             const char *pBitcode,
213                             size_t pBitcodeSize,
214                             const char *pBuildChecksum,
215                             const char *pRuntimePath,
216                             RSLinkRuntimeCallback pLinkRuntimeCallback,
217                             bool pDumpIR) {
218    //  android::StopWatch build_time("bcc: RSCompilerDriver::build time");
219  //===--------------------------------------------------------------------===//
220  // Check parameters.
221  //===--------------------------------------------------------------------===//
222  if ((pCacheDir == nullptr) || (pResName == nullptr)) {
223    ALOGE("Invalid parameter passed to RSCompilerDriver::build()! (cache dir: "
224          "%s, resource name: %s)", ((pCacheDir) ? pCacheDir : "(null)"),
225                                    ((pResName) ? pResName : "(null)"));
226    return false;
227  }
228
229  if ((pBitcode == nullptr) || (pBitcodeSize <= 0)) {
230    ALOGE("No bitcode supplied! (bitcode: %p, size of bitcode: %u)",
231          pBitcode, static_cast<unsigned>(pBitcodeSize));
232    return false;
233  }
234
235  //===--------------------------------------------------------------------===//
236  // Construct output path.
237  // {pCacheDir}/{pResName}.o
238  //===--------------------------------------------------------------------===//
239  llvm::SmallString<80> output_path(pCacheDir);
240  llvm::sys::path::append(output_path, pResName);
241  llvm::sys::path::replace_extension(output_path, ".o");
242
243  //===--------------------------------------------------------------------===//
244  // Load the bitcode and create script.
245  //===--------------------------------------------------------------------===//
246  Source *source = Source::CreateFromBuffer(pContext, pResName,
247                                            pBitcode, pBitcodeSize);
248  if (source == nullptr) {
249    return false;
250  }
251
252  RSScript script(*source, getConfig());
253  if (pLinkRuntimeCallback) {
254    setLinkRuntimeCallback(pLinkRuntimeCallback);
255  }
256
257  script.setLinkRuntimeCallback(getLinkRuntimeCallback());
258
259  script.setEmbedGlobalInfo(mEmbedGlobalInfo);
260  script.setEmbedGlobalInfoSkipConstant(mEmbedGlobalInfoSkipConstant);
261
262  // Read information from bitcode wrapper.
263  bcinfo::BitcodeWrapper wrapper(pBitcode, pBitcodeSize);
264  script.setCompilerVersion(wrapper.getCompilerVersion());
265  script.setOptimizationLevel(static_cast<RSScript::OptimizationLevel>(
266                              wrapper.getOptimizationLevel()));
267
268// Assertion-enabled builds can't compile legacy bitcode (due to the use of
269// getName() with anonymous structure definitions).
270#ifdef FORCE_BUILD_LLVM_DISABLE_NDEBUG
271  static const uint32_t kSlangMinimumFixedStructureNames = 2310;
272  uint32_t version = wrapper.getCompilerVersion();
273  if (version < kSlangMinimumFixedStructureNames) {
274    ALOGE("Found invalid legacy bitcode compiled with a version %u llvm-rs-cc "
275          "used with an assertion build", version);
276    ALOGE("Please recompile this apk with a more recent llvm-rs-cc "
277          "(at least %u)", kSlangMinimumFixedStructureNames);
278    return false;
279  }
280#endif
281
282  //===--------------------------------------------------------------------===//
283  // Compile the script
284  //===--------------------------------------------------------------------===//
285  Compiler::ErrorCode status = compileScript(script, pResName,
286                                             output_path.c_str(),
287                                             pRuntimePath,
288                                             pBuildChecksum,
289                                             pDumpIR);
290
291  return status == Compiler::kSuccess;
292}
293
294bool RSCompilerDriver::buildScriptGroup(
295    BCCContext& Context, const char* pOutputFilepath, const char* pRuntimePath,
296    const char* pRuntimeRelaxedPath, bool dumpIR, const char* buildChecksum,
297    const std::vector<Source*>& sources,
298    const std::list<std::list<std::pair<int, int>>>& toFuse,
299    const std::list<std::string>& fused,
300    const std::list<std::list<std::pair<int, int>>>& invokes,
301    const std::list<std::string>& invokeBatchNames) {
302  // ---------------------------------------------------------------------------
303  // Link all input modules into a single module
304  // ---------------------------------------------------------------------------
305
306  llvm::LLVMContext& context = Context.getLLVMContext();
307  llvm::Module module("Merged Script Group", context);
308
309  llvm::Linker linker(&module);
310  for (Source* source : sources) {
311    if (linker.linkInModule(&source->getModule())) {
312      ALOGE("Linking for module in source failed.");
313      return false;
314    }
315  }
316
317  // ---------------------------------------------------------------------------
318  // Create fused kernels
319  // ---------------------------------------------------------------------------
320
321  auto inputIter = toFuse.begin();
322  for (const std::string& nameOfFused : fused) {
323    auto inputKernels = *inputIter++;
324    std::vector<Source*> sourcesToFuse;
325    std::vector<int> slots;
326
327    for (auto p : inputKernels) {
328      sourcesToFuse.push_back(sources[p.first]);
329      slots.push_back(p.second);
330    }
331
332    if (!fuseKernels(Context, sourcesToFuse, slots, nameOfFused, &module)) {
333      return false;
334    }
335  }
336
337  // ---------------------------------------------------------------------------
338  // Rename invokes
339  // ---------------------------------------------------------------------------
340
341  auto invokeIter = invokes.begin();
342  for (const std::string& newName : invokeBatchNames) {
343    auto inputInvoke = *invokeIter++;
344    auto p = inputInvoke.front();
345    Source* source = sources[p.first];
346    int slot = p.second;
347
348    if (!renameInvoke(Context, source, slot, newName, &module)) {
349      return false;
350    }
351  }
352
353  // ---------------------------------------------------------------------------
354  // Compile the new module with fused kernels
355  // ---------------------------------------------------------------------------
356
357  const std::unique_ptr<Source> source(
358      Source::CreateFromModule(Context, pOutputFilepath, module, true));
359  RSScript script(*source);
360
361  // Embed the info string directly in the ELF
362  script.setEmbedInfo(true);
363  script.setOptimizationLevel(RSScript::kOptLvl3);
364  script.setEmbedGlobalInfo(mEmbedGlobalInfo);
365  script.setEmbedGlobalInfoSkipConstant(mEmbedGlobalInfoSkipConstant);
366
367  llvm::SmallString<80> output_path(pOutputFilepath);
368  llvm::sys::path::replace_extension(output_path, ".o");
369
370  // Pick the right runtime lib
371  const char* coreLibPath = pRuntimePath;
372  if (strcmp(pRuntimeRelaxedPath, "")) {
373      bcinfo::MetadataExtractor me(&module);
374      me.extract();
375      if (me.getRSFloatPrecision() == bcinfo::RS_FP_Relaxed) {
376          coreLibPath = pRuntimeRelaxedPath;
377      }
378  }
379
380  compileScript(script, pOutputFilepath, output_path.c_str(), coreLibPath,
381                buildChecksum, dumpIR);
382
383  return true;
384}
385
386bool RSCompilerDriver::buildForCompatLib(RSScript &pScript, const char *pOut,
387                                         const char *pBuildChecksum,
388                                         const char *pRuntimePath,
389                                         bool pDumpIR) {
390  // Embed the info string directly in the ELF, since this path is for an
391  // offline (host) compilation.
392  pScript.setEmbedInfo(true);
393
394  pScript.setEmbedGlobalInfo(mEmbedGlobalInfo);
395  pScript.setEmbedGlobalInfoSkipConstant(mEmbedGlobalInfoSkipConstant);
396  pScript.setLinkRuntimeCallback(getLinkRuntimeCallback());
397
398  Compiler::ErrorCode status = compileScript(pScript, pOut, pOut, pRuntimePath,
399                                             pBuildChecksum, pDumpIR);
400  if (status != Compiler::kSuccess) {
401    return false;
402  }
403
404  return true;
405}
406