RSCompilerDriver.h revision a4ded1373d7ad3e503f186e65bccf94126a0f020
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#ifndef BCC_RS_COMPILER_DRIVER_H
18#define BCC_RS_COMPILER_DRIVER_H
19
20#include "bcc/Compiler.h"
21#include "bcc/Renderscript/RSInfo.h"
22#include "bcc/Renderscript/RSScript.h"
23
24#include "bcinfo/MetadataExtractor.h"
25
26#include <vector>
27
28namespace bcc {
29
30class BCCContext;
31class CompilerConfig;
32class RSCompilerDriver;
33class Source;
34
35// Type signature for dynamically loaded initialization of an RSCompilerDriver.
36typedef void (*RSCompilerDriverInit_t) (bcc::RSCompilerDriver *);
37// Name of the function that we attempt to dynamically load/execute.
38#define RS_COMPILER_DRIVER_INIT_FN rsCompilerDriverInit
39
40class RSCompilerDriver {
41private:
42  CompilerConfig *mConfig;
43  Compiler mCompiler;
44
45  // Are we compiling under an RS debug context with additional checks?
46  bool mDebugContext;
47
48  // Callback before linking with the runtime library.
49  RSLinkRuntimeCallback mLinkRuntimeCallback;
50
51  // Do we merge global variables on ARM using LLVM's optimization pass?
52  // Disabling LLVM's global merge pass allows static globals to be correctly
53  // emitted to ELF. This can result in decreased performance due to increased
54  // register pressure, but it does make the resulting code easier to debug
55  // and work with.
56  bool mEnableGlobalMerge;
57
58  // Setup the compiler config for the given script. Return true if mConfig has
59  // been changed and false if it remains unchanged.
60  bool setupConfig(const RSScript &pScript);
61
62  // Compiles the provided bitcode, placing the binary at pOutputPath.
63  // - If saveInfoFile is true, it also stores the RSInfo data in a file with a path derived from
64  //   pOutputPath.
65  // - pSourceHash and commandLineToEmbed are values to embed in the RSInfo for future cache
66  //   invalidation decision.
67  // - If pDumpIR is true, a ".ll" file will also be created.
68  Compiler::ErrorCode compileScript(RSScript& pScript, const char* pScriptName,
69                                    const char* pOutputPath, const char* pRuntimePath,
70                                    const RSInfo::DependencyHashTy& pSourceHash,
71                                    const char* commandLineToEmbed, bool saveInfoFile, bool pDumpIR);
72
73public:
74  RSCompilerDriver(bool pUseCompilerRT = true);
75  ~RSCompilerDriver();
76
77  Compiler *getCompiler() {
78    return &mCompiler;
79  }
80
81  void setConfig(CompilerConfig *config) {
82    mConfig = config;
83  }
84
85  void setDebugContext(bool v) {
86    mDebugContext = v;
87  }
88
89  void setLinkRuntimeCallback(RSLinkRuntimeCallback c) {
90    mLinkRuntimeCallback = c;
91  }
92
93  RSLinkRuntimeCallback getLinkRuntimeCallback() const {
94    return mLinkRuntimeCallback;
95  }
96
97  // This function enables/disables merging of global static variables.
98  // Note that it only takes effect on ARM architectures (other architectures
99  // do not offer this option).
100  void setEnableGlobalMerge(bool v) {
101    mEnableGlobalMerge = v;
102  }
103
104  bool getEnableGlobalMerge() const {
105    return mEnableGlobalMerge;
106  }
107
108  // FIXME: This method accompany with loadScript and compileScript should
109  //        all be const-methods. They're not now because the getAddress() in
110  //        SymbolResolverInterface is not a const-method.
111  // Returns true if script is successfully compiled.
112  bool build(BCCContext& pContext, const char* pCacheDir, const char* pResName,
113             const char* pBitcode, size_t pBitcodeSize, const char* commandLine,
114             const char* pRuntimePath,
115             RSLinkRuntimeCallback pLinkRuntimeCallback = nullptr,
116             bool pDumpIR = false);
117
118  bool buildScriptGroup(
119      BCCContext& Context, const char* pOutputFilepath, const char* pRuntimePath,
120      const std::vector<const Source*>& sources, const std::vector<int>& slots,
121      bool dumpIR);
122
123  // Returns true if script is successfully compiled.
124  bool buildForCompatLib(RSScript &pScript, const char *pOut, const char *pRuntimePath);
125};
126
127} // end namespace bcc
128
129#endif // BCC_RS_COMPILER_DRIVER_H
130