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