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