Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 1 | /* |
Stephen Hines | db16918 | 2012-01-05 18:46:36 -0800 | [diff] [blame] | 2 | * Copyright 2010-2012, The Android Open Source Project |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 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 | |
Zonr Chang | c72c4dd | 2012-04-12 15:38:53 +0800 | [diff] [blame] | 17 | #ifndef BCC_COMPILER_H |
| 18 | #define BCC_COMPILER_H |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 19 | |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 20 | namespace llvm { |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 21 | |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 22 | class raw_ostream; |
Pirama Arumuga Nainar | 98137cc | 2015-05-06 11:18:56 -0700 | [diff] [blame] | 23 | class raw_pwrite_stream; |
Stephen Hines | b730e23 | 2013-01-09 15:31:36 -0800 | [diff] [blame] | 24 | class DataLayout; |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 25 | class TargetMachine; |
| 26 | |
Stephen Hines | dee928b | 2014-02-10 17:19:46 -0800 | [diff] [blame] | 27 | namespace legacy { |
| 28 | class PassManager; |
| 29 | } // end namespace legacy |
| 30 | |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 31 | } // end namespace llvm |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 32 | |
| 33 | namespace bcc { |
| 34 | |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 35 | class CompilerConfig; |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 36 | class Script; |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 37 | |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 38 | //===----------------------------------------------------------------------===// |
| 39 | // Design of Compiler |
| 40 | //===----------------------------------------------------------------------===// |
| 41 | // 1. A compiler instance can be constructed provided an "initial config." |
| 42 | // 2. A compiler can later be re-configured using config(). |
| 43 | // 3. Once config() is invoked, it'll re-create TargetMachine instance (i.e., |
| 44 | // mTarget) according to the configuration supplied. TargetMachine instance |
| 45 | // is *shared* across the different calls to compile() before the next call |
| 46 | // to config(). |
| 47 | // 4. Once a compiler instance is created, you can use the compile() service |
| 48 | // to compile the file over and over again. Each call uses TargetMachine |
| 49 | // instance to construct the compilation passes. |
| 50 | class Compiler { |
| 51 | public: |
| 52 | enum ErrorCode { |
| 53 | kSuccess, |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 54 | |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 55 | kInvalidConfigNoTarget, |
| 56 | kErrCreateTargetMachine, |
| 57 | kErrSwitchTargetMachine, |
| 58 | kErrNoTargetMachine, |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 59 | kErrMaterialization, |
| 60 | kErrInvalidOutputFileState, |
| 61 | kErrPrepareOutput, |
| 62 | kPrepareCodeGenPass, |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 63 | |
Chris Wailes | b4447cd | 2014-08-19 16:22:20 -0700 | [diff] [blame] | 64 | kErrCustomPasses, |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 65 | |
Pirama Arumuga Nainar | 1e0557a | 2014-12-02 15:02:18 -0800 | [diff] [blame] | 66 | kErrInvalidSource, |
| 67 | |
Pirama Arumuga Nainar | d2d5ee3 | 2016-04-12 14:04:50 -0700 | [diff] [blame] | 68 | kIllegalGlobalFunction, |
| 69 | |
David Gross | 97e5099 | 2017-03-29 20:52:30 +0000 | [diff] [blame] | 70 | kErrInvalidTargetMachine, |
| 71 | |
| 72 | kErrInvalidLayout |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 73 | }; |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 74 | |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 75 | static const char *GetErrorString(enum ErrorCode pErrCode); |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 76 | |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 77 | private: |
| 78 | llvm::TargetMachine *mTarget; |
Chris Wailes | b4447cd | 2014-08-19 16:22:20 -0700 | [diff] [blame] | 79 | // Optimization is enabled by default. |
| 80 | bool mEnableOpt; |
Logan | 2a6dc82 | 2011-01-06 04:05:20 +0800 | [diff] [blame] | 81 | |
Pirama Arumuga Nainar | 98137cc | 2015-05-06 11:18:56 -0700 | [diff] [blame] | 82 | enum ErrorCode runPasses(Script &pScript, llvm::raw_pwrite_stream &pResult); |
Chris Wailes | b4447cd | 2014-08-19 16:22:20 -0700 | [diff] [blame] | 83 | |
Stephen Hines | 1bd9f62 | 2015-03-18 14:53:10 -0700 | [diff] [blame] | 84 | bool addInternalizeSymbolsPass(Script &pScript, llvm::legacy::PassManager &pPM); |
Matt Wala | 4e7a506 | 2015-07-30 16:27:51 -0700 | [diff] [blame] | 85 | void addExpandKernelPass(llvm::legacy::PassManager &pPM); |
Dean De Leo | 09c7a41 | 2015-11-25 12:45:45 +0000 | [diff] [blame] | 86 | void addDebugInfoPass(Script &pScript, llvm::legacy::PassManager &pPM); |
David Gross | 5aefc98 | 2015-08-04 10:41:33 -0700 | [diff] [blame] | 87 | void addGlobalInfoPass(Script &pScript, llvm::legacy::PassManager &pPM); |
| 88 | void addInvariantPass(llvm::legacy::PassManager &pPM); |
| 89 | void addInvokeHelperPass(llvm::legacy::PassManager &pPM); |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 90 | |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 91 | public: |
| 92 | Compiler(); |
Chih-Hung Hsieh | 8a019dd | 2016-08-12 15:49:55 -0700 | [diff] [blame] | 93 | explicit Compiler(const CompilerConfig &pConfig); |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 94 | |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 95 | enum ErrorCode config(const CompilerConfig &pConfig); |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 96 | |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 97 | // Compile a script and output the result to a LLVM stream. |
Tobias Grosser | 27fb7ed | 2013-06-21 18:34:56 -0700 | [diff] [blame] | 98 | // |
| 99 | // @param IRStream If not NULL, the LLVM-IR that is fed to code generation |
| 100 | // will be written to IRStream. |
Pirama Arumuga Nainar | 98137cc | 2015-05-06 11:18:56 -0700 | [diff] [blame] | 101 | enum ErrorCode compile(Script &pScript, llvm::raw_pwrite_stream &pResult, |
Tobias Grosser | 27fb7ed | 2013-06-21 18:34:56 -0700 | [diff] [blame] | 102 | llvm::raw_ostream *IRStream); |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 103 | |
Shih-wei Liao | 0dbd4fb | 2012-07-27 03:09:47 -0700 | [diff] [blame] | 104 | const llvm::TargetMachine& getTargetMachine() const |
| 105 | { return *mTarget; } |
| 106 | |
Chris Wailes | b4447cd | 2014-08-19 16:22:20 -0700 | [diff] [blame] | 107 | void enableOpt(bool pEnable = true) |
| 108 | { mEnableOpt = pEnable; } |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 109 | |
Chris Wailes | b4447cd | 2014-08-19 16:22:20 -0700 | [diff] [blame] | 110 | ~Compiler(); |
Pirama Arumuga Nainar | ebff2ea | 2015-05-21 15:45:05 -0700 | [diff] [blame] | 111 | |
Stephen Hines | 4dc1d8e | 2020-07-24 16:18:16 -0700 | [diff] [blame] | 112 | // Compare undefined external functions in pScript against a list of allowed |
| 113 | // RenderScript functions. Returns error if any external function that is |
| 114 | // not in this list is callable from the script. |
Pirama Arumuga Nainar | ebff2ea | 2015-05-21 15:45:05 -0700 | [diff] [blame] | 115 | enum ErrorCode screenGlobalFunctions(Script &pScript); |
Pirama Arumuga Nainar | d2d5ee3 | 2016-04-12 14:04:50 -0700 | [diff] [blame] | 116 | |
| 117 | void translateGEPs(Script &pScript); |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 118 | }; |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 119 | |
Zonr Chang | ade9277 | 2012-04-13 15:58:24 +0800 | [diff] [blame] | 120 | } // end namespace bcc |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 121 | |
Zonr Chang | c72c4dd | 2012-04-12 15:38:53 +0800 | [diff] [blame] | 122 | #endif // BCC_COMPILER_H |