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