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