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