RSCompilerDriver.h revision 6da4e253a513feef3405759fef6d0760828808ca
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 <list>
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 saveInfoFile is true, it also stores the RSInfo data in a file with a path derived from
65  //   pOutputPath.
66  // - pSourceHash and commandLineToEmbed are values to embed in the RSInfo for future cache
67  //   invalidation decision.
68  // - If pDumpIR is true, a ".ll" file will also be created.
69  Compiler::ErrorCode compileScript(RSScript& pScript, const char* pScriptName,
70                                    const char* pOutputPath,
71                                    const char *pRuntimePath,
72                                    const RSInfo::DependencyHashTy& pSourceHash,
73                                    const char* commandLineToEmbed,
74                                    const char* pBuildChecksum,
75                                    bool saveInfoFile, bool pDumpIR);
76
77public:
78  RSCompilerDriver(bool pUseCompilerRT = true);
79  ~RSCompilerDriver();
80
81  Compiler *getCompiler() {
82    return &mCompiler;
83  }
84
85  void setConfig(CompilerConfig *config) {
86    mConfig = config;
87  }
88
89  void setDebugContext(bool v) {
90    mDebugContext = v;
91  }
92
93  void setLinkRuntimeCallback(RSLinkRuntimeCallback c) {
94    mLinkRuntimeCallback = c;
95  }
96
97  RSLinkRuntimeCallback getLinkRuntimeCallback() const {
98    return mLinkRuntimeCallback;
99  }
100
101  // This function enables/disables merging of global static variables.
102  // Note that it only takes effect on ARM architectures (other architectures
103  // do not offer this option).
104  void setEnableGlobalMerge(bool v) {
105    mEnableGlobalMerge = v;
106  }
107
108  bool getEnableGlobalMerge() const {
109    return mEnableGlobalMerge;
110  }
111
112  // FIXME: This method accompany with loadScript and compileScript should
113  //        all be const-methods. They're not now because the getAddress() in
114  //        SymbolResolverInterface is not a const-method.
115  // Returns true if script is successfully compiled.
116  bool build(BCCContext& pContext, const char* pCacheDir, const char* pResName,
117             const char* pBitcode, size_t pBitcodeSize, const char* commandLine,
118             const char *pBuildChecksum, const char* pRuntimePath,
119             RSLinkRuntimeCallback pLinkRuntimeCallback = nullptr,
120             bool pDumpIR = false);
121
122  bool buildScriptGroup(
123      BCCContext& Context, const char* pOutputFilepath, const char* pRuntimePath,
124      const char* pRuntimeRelaxedPath, bool dumpIR,
125      const std::vector<Source*>& sources,
126      const std::list<std::list<std::pair<int, int>>>& toFuse,
127      const std::list<std::string>& fused,
128      const std::list<std::list<std::pair<int, int>>>& invokes,
129      const std::list<std::string>& invokeBatchNames);
130
131  // Returns true if script is successfully compiled.
132  bool buildForCompatLib(RSScript &pScript, const char *pOut,
133                         const char *pBuildChecksum, const char *pRuntimePath,
134                         bool pDumpIR);
135};
136
137} // end namespace bcc
138
139#endif // BCC_RS_COMPILER_DRIVER_H
140