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/ADT/STLExtras.h"
20#include "llvm/IR/AssemblyAnnotationWriter.h"
21#include <llvm/IR/Module.h>
22#include "llvm/Linker/Linker.h"
23#include <llvm/Support/CommandLine.h>
24#include <llvm/Support/Path.h>
25#include <llvm/Support/raw_ostream.h>
26#include <llvm/Target/TargetMachine.h>
27
28#include "bcinfo/BitcodeWrapper.h"
29#include "bcc/Assert.h"
30#include "bcinfo/MetadataExtractor.h"
31#include "bcc/BCCContext.h"
32#include "bcc/Compiler.h"
33#include "bcc/Config/Config.h"
34#include "bcc/Renderscript/RSScript.h"
35#include "bcc/Renderscript/RSScriptGroupFusion.h"
36#include "bcc/Support/CompilerConfig.h"
37#include "bcc/Source.h"
38#include "bcc/Support/FileMutex.h"
39#include "bcc/Support/Log.h"
40#include "bcc/Support/InputFile.h"
41#include "bcc/Support/Initialization.h"
42#include "bcc/Support/OutputFile.h"
43
44#include <sstream>
45#include <string>
46
47#ifdef __ANDROID__
48#include <cutils/properties.h>
49#endif
50#include <utils/StopWatch.h>
51
52using namespace bcc;
53
54RSCompilerDriver::RSCompilerDriver(bool pUseCompilerRT) :
55    mConfig(nullptr), mCompiler(), mDebugContext(false),
56    mLinkRuntimeCallback(nullptr), mEnableGlobalMerge(true),
57    mEmbedGlobalInfo(false), mEmbedGlobalInfoSkipConstant(false) {
58  init::Initialize();
59}
60
61RSCompilerDriver::~RSCompilerDriver() {
62  delete mConfig;
63}
64
65
66#if defined(PROVIDE_ARM_CODEGEN)
67extern llvm::cl::opt<bool> EnableGlobalMerge;
68#endif
69
70bool RSCompilerDriver::setupConfig(const RSScript &pScript) {
71  bool changed = false;
72
73  const llvm::CodeGenOpt::Level script_opt_level =
74      static_cast<llvm::CodeGenOpt::Level>(pScript.getOptimizationLevel());
75
76#if defined(PROVIDE_ARM_CODEGEN)
77  EnableGlobalMerge = mEnableGlobalMerge;
78#endif
79
80  if (mConfig != nullptr) {
81    // 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.
89    mConfig = new (std::nothrow) CompilerConfig(DEFAULT_TARGET_TRIPLE_STRING);
90    if (mConfig == nullptr) {
91      // 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
98#if defined(PROVIDE_ARM_CODEGEN)
99  bcinfo::MetadataExtractor me(&pScript.getSource().getModule());
100  if (!me.extract()) {
101    bccAssert("Could not extract RS pragma metadata for module!");
102  }
103
104  bool script_full_prec = (me.getRSFloatPrecision() == bcinfo::RS_FP_Full);
105  if (mConfig->getFullPrecision() != script_full_prec) {
106    mConfig->setFullPrecision(script_full_prec);
107    changed = true;
108  }
109#endif
110
111  return changed;
112}
113
114Compiler::ErrorCode RSCompilerDriver::compileScript(RSScript& pScript, const char* pScriptName,
115                                                    const char* pOutputPath,
116                                                    const char* pRuntimePath,
117                                                    const char* pBuildChecksum,
118                                                    bool pDumpIR) {
119  // embed build checksum metadata into the source
120  if (pBuildChecksum != nullptr && strlen(pBuildChecksum) > 0) {
121    pScript.getSource().addBuildChecksumMetadata(pBuildChecksum);
122  }
123
124  // 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
130  // 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
143  //===--------------------------------------------------------------------===//
144  // Link RS script with Renderscript runtime.
145  //===--------------------------------------------------------------------===//
146  if (!RSScript::LinkRuntime(pScript, pRuntimePath)) {
147    ALOGE("Failed to link script '%s' with Renderscript runtime %s!",
148          pScriptName, pRuntimePath);
149    return Compiler::kErrInvalidSource;
150  }
151
152  {
153    // 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.
155#ifndef USE_MINGW
156    //===------------------------------------------------------------------===//
157    // Acquire the write lock for writing output object file.
158    //===------------------------------------------------------------------===//
159    FileMutex<FileBase::kWriteLock> write_output_mutex(pOutputPath);
160
161    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());
164      return Compiler::kErrInvalidSource;
165    }
166#endif
167
168    // Open the output file for write.
169    OutputFile output_file(pOutputPath,
170                           FileBase::kTruncate | FileBase::kBinary);
171
172    if (output_file.hasError()) {
173        ALOGE("Unable to open %s for write! (%s)", pOutputPath,
174              output_file.getErrorMessage().c_str());
175      return Compiler::kErrInvalidSource;
176    }
177
178    // Setup the config to the compiler.
179    bool compiler_need_reconfigure = setupConfig(pScript);
180
181    if (mConfig == nullptr) {
182      ALOGE("Failed to setup config for RS compiler to compile %s!",
183            pOutputPath);
184      return Compiler::kErrInvalidSource;
185    }
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));
192        return Compiler::kErrInvalidSource;
193      }
194    }
195
196    OutputFile *ir_file = nullptr;
197    llvm::raw_fd_ostream *IRStream = nullptr;
198    if (pDumpIR) {
199      std::string path(pOutputPath);
200      path.append(".ll");
201      ir_file = new OutputFile(path.c_str(), FileBase::kTruncate);
202      IRStream = ir_file->dup();
203    }
204
205    // Run the compiler.
206    Compiler::ErrorCode compile_result =
207        mCompiler.compile(pScript, output_file, IRStream);
208
209    if (ir_file) {
210      ir_file->close();
211      delete ir_file;
212    }
213
214    if (compile_result != Compiler::kSuccess) {
215      ALOGE("Unable to compile the source to file %s! (%s)", pOutputPath,
216            Compiler::GetErrorString(compile_result));
217      return Compiler::kErrInvalidSource;
218    }
219  }
220
221  return Compiler::kSuccess;
222}
223
224bool RSCompilerDriver::build(BCCContext &pContext,
225                             const char *pCacheDir,
226                             const char *pResName,
227                             const char *pBitcode,
228                             size_t pBitcodeSize,
229                             const char *pBuildChecksum,
230                             const char *pRuntimePath,
231                             RSLinkRuntimeCallback pLinkRuntimeCallback,
232                             bool pDumpIR) {
233    //  android::StopWatch build_time("bcc: RSCompilerDriver::build time");
234  //===--------------------------------------------------------------------===//
235  // Check parameters.
236  //===--------------------------------------------------------------------===//
237  if ((pCacheDir == nullptr) || (pResName == nullptr)) {
238    ALOGE("Invalid parameter passed to RSCompilerDriver::build()! (cache dir: "
239          "%s, resource name: %s)", ((pCacheDir) ? pCacheDir : "(null)"),
240                                    ((pResName) ? pResName : "(null)"));
241    return false;
242  }
243
244  if ((pBitcode == nullptr) || (pBitcodeSize <= 0)) {
245    ALOGE("No bitcode supplied! (bitcode: %p, size of bitcode: %u)",
246          pBitcode, static_cast<unsigned>(pBitcodeSize));
247    return false;
248  }
249
250  //===--------------------------------------------------------------------===//
251  // Construct output path.
252  // {pCacheDir}/{pResName}.o
253  //===--------------------------------------------------------------------===//
254  llvm::SmallString<80> output_path(pCacheDir);
255  llvm::sys::path::append(output_path, pResName);
256  llvm::sys::path::replace_extension(output_path, ".o");
257
258  //===--------------------------------------------------------------------===//
259  // Load the bitcode and create script.
260  //===--------------------------------------------------------------------===//
261  Source *source = Source::CreateFromBuffer(pContext, pResName,
262                                            pBitcode, pBitcodeSize);
263  if (source == nullptr) {
264    return false;
265  }
266
267  RSScript script(*source, getConfig());
268  if (pLinkRuntimeCallback) {
269    setLinkRuntimeCallback(pLinkRuntimeCallback);
270  }
271
272  script.setLinkRuntimeCallback(getLinkRuntimeCallback());
273
274  script.setEmbedGlobalInfo(mEmbedGlobalInfo);
275  script.setEmbedGlobalInfoSkipConstant(mEmbedGlobalInfoSkipConstant);
276
277  // Read information from bitcode wrapper.
278  bcinfo::BitcodeWrapper wrapper(pBitcode, pBitcodeSize);
279  script.setCompilerVersion(wrapper.getCompilerVersion());
280  script.setOptimizationLevel(static_cast<RSScript::OptimizationLevel>(
281                              wrapper.getOptimizationLevel()));
282
283// Assertion-enabled builds can't compile legacy bitcode (due to the use of
284// getName() with anonymous structure definitions).
285#ifdef FORCE_BUILD_LLVM_DISABLE_NDEBUG
286  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
297  //===--------------------------------------------------------------------===//
298  // Compile the script
299  //===--------------------------------------------------------------------===//
300  Compiler::ErrorCode status = compileScript(script, pResName,
301                                             output_path.c_str(),
302                                             pRuntimePath,
303                                             pBuildChecksum,
304                                             pDumpIR);
305
306  return status == Compiler::kSuccess;
307}
308
309bool RSCompilerDriver::buildScriptGroup(
310    BCCContext& Context, const char* pOutputFilepath, const char* pRuntimePath,
311    const char* pRuntimeRelaxedPath, bool dumpIR, const char* buildChecksum,
312    const std::vector<Source*>& sources,
313    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) {
317
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
327  // ---------------------------------------------------------------------------
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
334  llvm::Linker linker(module);
335  for (Source* source : sources) {
336    std::unique_ptr<llvm::Module> sourceModule(&source->getModule());
337    if (linker.linkInModule(std::move(sourceModule))) {
338      ALOGE("Linking for module in source failed.");
339      return false;
340    }
341    // source->getModule() is destroyed after linking.
342    source->markModuleDestroyed();
343  }
344
345  // ---------------------------------------------------------------------------
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
385  const std::unique_ptr<Source> source(
386      Source::CreateFromModule(Context, pOutputFilepath, module, true));
387  RSScript script(*source);
388
389  // Embed the info string directly in the ELF
390  script.setEmbedInfo(true);
391  script.setOptimizationLevel(RSScript::kOptLvl3);
392  script.setEmbedGlobalInfo(mEmbedGlobalInfo);
393  script.setEmbedGlobalInfoSkipConstant(mEmbedGlobalInfoSkipConstant);
394
395  llvm::SmallString<80> output_path(pOutputFilepath);
396  llvm::sys::path::replace_extension(output_path, ".o");
397
398  // Pick the right runtime lib
399  const char* coreLibPath = pRuntimePath;
400  if (strcmp(pRuntimeRelaxedPath, "")) {
401      bcinfo::MetadataExtractor me(&module);
402      me.extract();
403      if (me.getRSFloatPrecision() == bcinfo::RS_FP_Relaxed) {
404          coreLibPath = pRuntimeRelaxedPath;
405      }
406  }
407
408  compileScript(script, pOutputFilepath, output_path.c_str(), coreLibPath,
409                buildChecksum, dumpIR);
410
411  return true;
412}
413
414bool RSCompilerDriver::buildForCompatLib(RSScript &pScript, const char *pOut,
415                                         const char *pBuildChecksum,
416                                         const char *pRuntimePath,
417                                         bool pDumpIR) {
418  // Embed the info string directly in the ELF, since this path is for an
419  // offline (host) compilation.
420  pScript.setEmbedInfo(true);
421
422  pScript.setEmbedGlobalInfo(mEmbedGlobalInfo);
423  pScript.setEmbedGlobalInfoSkipConstant(mEmbedGlobalInfoSkipConstant);
424  pScript.setLinkRuntimeCallback(getLinkRuntimeCallback());
425
426  Compiler::ErrorCode status = compileScript(pScript, pOut, pOut, pRuntimePath,
427                                             pBuildChecksum, pDumpIR);
428  if (status != Compiler::kSuccess) {
429    return false;
430  }
431
432  return true;
433}
434