ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 7 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 8 | #ifndef SKSL_COMPILER |
| 9 | #define SKSL_COMPILER |
| 10 | |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 11 | #include <set> |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 12 | #include <unordered_set> |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 13 | #include <vector> |
Ethan Nicholas | db80f69 | 2019-11-22 14:06:12 -0500 | [diff] [blame] | 14 | #include "src/sksl/SkSLASTFile.h" |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 15 | #include "src/sksl/SkSLAnalysis.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 16 | #include "src/sksl/SkSLCFGGenerator.h" |
| 17 | #include "src/sksl/SkSLContext.h" |
| 18 | #include "src/sksl/SkSLErrorReporter.h" |
John Stiles | 7b46300 | 2020-08-31 17:29:21 -0400 | [diff] [blame] | 19 | #include "src/sksl/SkSLInliner.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 20 | #include "src/sksl/SkSLLexer.h" |
| 21 | #include "src/sksl/ir/SkSLProgram.h" |
| 22 | #include "src/sksl/ir/SkSLSymbolTable.h" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 23 | |
Brian Osman | 2e29ab5 | 2019-09-20 12:19:11 -0400 | [diff] [blame] | 24 | #if !defined(SKSL_STANDALONE) && SK_SUPPORT_GPU |
| 25 | #include "src/gpu/GrShaderVar.h" |
| 26 | #endif |
| 27 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 28 | #define SK_FRAGCOLOR_BUILTIN 10001 |
| 29 | #define SK_IN_BUILTIN 10002 |
Ethan Nicholas | 16c1196 | 2018-03-16 12:20:54 -0400 | [diff] [blame] | 30 | #define SK_OUT_BUILTIN 10007 |
Ethan Nicholas | eab2baa | 2018-04-13 15:16:27 -0400 | [diff] [blame] | 31 | #define SK_LASTFRAGCOLOR_BUILTIN 10008 |
Brian Osman | 7353dc5 | 2020-02-07 13:37:12 -0500 | [diff] [blame] | 32 | #define SK_MAIN_COORDS_BUILTIN 10009 |
Ethan Nicholas | cd700e9 | 2018-08-24 16:43:57 -0400 | [diff] [blame] | 33 | #define SK_WIDTH_BUILTIN 10011 |
| 34 | #define SK_HEIGHT_BUILTIN 10012 |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 35 | #define SK_FRAGCOORD_BUILTIN 15 |
Chris Dalton | 49d14e9 | 2018-07-27 12:38:35 -0600 | [diff] [blame] | 36 | #define SK_CLOCKWISE_BUILTIN 17 |
Chris Dalton | b0fd4b1 | 2019-10-29 13:41:22 -0600 | [diff] [blame] | 37 | #define SK_SAMPLEMASK_BUILTIN 20 |
Ethan Nicholas | 9eded2c | 2018-03-22 10:10:44 -0400 | [diff] [blame] | 38 | #define SK_VERTEXID_BUILTIN 42 |
| 39 | #define SK_INSTANCEID_BUILTIN 43 |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 40 | #define SK_INVOCATIONID_BUILTIN 8 |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 41 | #define SK_POSITION_BUILTIN 0 |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 42 | |
John Stiles | e615000 | 2020-10-05 12:03:53 -0400 | [diff] [blame] | 43 | class SkBitSet; |
Ethan Nicholas | ba9a04f | 2020-11-06 09:28:04 -0500 | [diff] [blame] | 44 | class SkSLCompileBench; |
John Stiles | e615000 | 2020-10-05 12:03:53 -0400 | [diff] [blame] | 45 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 46 | namespace SkSL { |
| 47 | |
Brian Osman | 9b8b455 | 2019-09-30 13:23:14 -0400 | [diff] [blame] | 48 | class ByteCode; |
Brian Osman | be0b3b7 | 2021-01-06 14:27:35 -0500 | [diff] [blame^] | 49 | class ExternalFunction; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 50 | class IRGenerator; |
Brian Osman | 2b469eb | 2020-09-21 11:32:10 -0400 | [diff] [blame] | 51 | class IRIntrinsicMap; |
Brian Osman | 107c666 | 2019-12-30 15:02:30 -0500 | [diff] [blame] | 52 | struct PipelineStageArgs; |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 53 | class ProgramUsage; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 54 | |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 55 | struct LoadedModule { |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 56 | Program::Kind fKind; |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 57 | std::shared_ptr<SymbolTable> fSymbols; |
| 58 | std::vector<std::unique_ptr<ProgramElement>> fElements; |
| 59 | }; |
| 60 | |
| 61 | struct ParsedModule { |
| 62 | std::shared_ptr<SymbolTable> fSymbols; |
| 63 | std::shared_ptr<IRIntrinsicMap> fIntrinsics; |
| 64 | }; |
| 65 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 66 | /** |
| 67 | * Main compiler entry point. This is a traditional compiler design which first parses the .sksl |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 68 | * file into an abstract syntax tree (a tree of ASTNodes), then performs semantic analysis to |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 69 | * produce a Program (a tree of IRNodes), then feeds the Program into a CodeGenerator to produce |
| 70 | * compiled output. |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 71 | * |
| 72 | * See the README for information about SkSL. |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 73 | */ |
Ethan Nicholas | 4100b7c | 2019-03-12 11:50:48 -0400 | [diff] [blame] | 74 | class SK_API Compiler : public ErrorReporter { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 75 | public: |
Robert Phillips | fe8da17 | 2018-01-24 14:52:02 +0000 | [diff] [blame] | 76 | static constexpr const char* RTADJUST_NAME = "sk_RTAdjust"; |
| 77 | static constexpr const char* PERVERTEX_NAME = "sk_PerVertex"; |
| 78 | |
Ethan Nicholas | 6e1cbc0 | 2017-07-14 10:12:15 -0400 | [diff] [blame] | 79 | enum Flags { |
| 80 | kNone_Flags = 0, |
| 81 | // permits static if/switch statements to be used with non-constant tests. This is used when |
| 82 | // producing H and CPP code; the static tests don't have to have constant values *yet*, but |
| 83 | // the generated code will contain a static test which then does have to be a constant. |
| 84 | kPermitInvalidStaticTests_Flag = 1, |
| 85 | }; |
| 86 | |
Brian Osman | ac9d3f6 | 2020-06-25 11:07:30 -0400 | [diff] [blame] | 87 | // An invalid (otherwise unused) character to mark where FormatArgs are inserted |
| 88 | static constexpr char kFormatArgPlaceholder = '\001'; |
| 89 | static constexpr const char* kFormatArgPlaceholderStr = "\001"; |
| 90 | |
Ethan Nicholas | ce00811 | 2018-08-30 09:19:50 -0400 | [diff] [blame] | 91 | struct FormatArg { |
| 92 | enum class Kind { |
Brian Osman | 7353dc5 | 2020-02-07 13:37:12 -0500 | [diff] [blame] | 93 | kCoords, |
Ethan Nicholas | ce00811 | 2018-08-30 09:19:50 -0400 | [diff] [blame] | 94 | kUniform, |
Brian Osman | 2e29ab5 | 2019-09-20 12:19:11 -0400 | [diff] [blame] | 95 | kChildProcessor, |
Brian Osman | be1b837 | 2020-06-18 13:40:26 -0400 | [diff] [blame] | 96 | kChildProcessorWithMatrix, |
Brian Osman | 2e29ab5 | 2019-09-20 12:19:11 -0400 | [diff] [blame] | 97 | kFunctionName |
Ethan Nicholas | ce00811 | 2018-08-30 09:19:50 -0400 | [diff] [blame] | 98 | }; |
| 99 | |
| 100 | FormatArg(Kind kind) |
| 101 | : fKind(kind) {} |
| 102 | |
| 103 | FormatArg(Kind kind, int index) |
| 104 | : fKind(kind) |
| 105 | , fIndex(index) {} |
| 106 | |
| 107 | Kind fKind; |
Ethan Nicholas | ce00811 | 2018-08-30 09:19:50 -0400 | [diff] [blame] | 108 | int fIndex; |
Brian Osman | 87e3bef | 2020-01-27 16:21:34 -0500 | [diff] [blame] | 109 | String fCoords; |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 110 | }; |
| 111 | |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 112 | struct OptimizationContext { |
| 113 | // nodes we have already reported errors for and should not error on again |
| 114 | std::unordered_set<const IRNode*> fSilences; |
| 115 | // true if we have updated the CFG during this pass |
| 116 | bool fUpdated = false; |
| 117 | // true if we need to completely regenerate the CFG |
| 118 | bool fNeedsRescan = false; |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 119 | // Metadata about function and variable usage within the program |
| 120 | ProgramUsage* fUsage = nullptr; |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 121 | }; |
| 122 | |
Brian Osman | 2e29ab5 | 2019-09-20 12:19:11 -0400 | [diff] [blame] | 123 | #if !defined(SKSL_STANDALONE) && SK_SUPPORT_GPU |
| 124 | /** |
| 125 | * Represents the arguments to GrGLSLShaderBuilder::emitFunction. |
| 126 | */ |
| 127 | struct GLSLFunction { |
| 128 | GrSLType fReturnType; |
| 129 | SkString fName; |
| 130 | std::vector<GrShaderVar> fParameters; |
Brian Osman | ac9d3f6 | 2020-06-25 11:07:30 -0400 | [diff] [blame] | 131 | String fBody; |
Ethan Nicholas | fc671ad | 2019-11-21 11:14:11 -0500 | [diff] [blame] | 132 | std::vector<Compiler::FormatArg> fFormatArgs; |
Brian Osman | 2e29ab5 | 2019-09-20 12:19:11 -0400 | [diff] [blame] | 133 | }; |
| 134 | #endif |
| 135 | |
Brian Osman | d7e7659 | 2020-11-02 12:26:22 -0500 | [diff] [blame] | 136 | Compiler(const ShaderCapsClass* caps, Flags flags = kNone_Flags); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 137 | |
Brian Salomon | d3b6597 | 2017-03-22 12:05:03 -0400 | [diff] [blame] | 138 | ~Compiler() override; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 139 | |
Brian Osman | 1400382 | 2019-04-10 11:47:26 -0400 | [diff] [blame] | 140 | Compiler(const Compiler&) = delete; |
| 141 | Compiler& operator=(const Compiler&) = delete; |
| 142 | |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 143 | /** |
Brian Osman | be0b3b7 | 2021-01-06 14:27:35 -0500 | [diff] [blame^] | 144 | * If externalFunctions is supplied, those values are registered in the symbol table of the |
Brian Osman | 32d5355 | 2020-09-23 13:55:20 -0400 | [diff] [blame] | 145 | * Program, but ownership is *not* transferred. It is up to the caller to keep them alive. |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 146 | */ |
Brian Osman | 32d5355 | 2020-09-23 13:55:20 -0400 | [diff] [blame] | 147 | std::unique_ptr<Program> convertProgram( |
| 148 | Program::Kind kind, |
| 149 | String text, |
| 150 | const Program::Settings& settings, |
Brian Osman | be0b3b7 | 2021-01-06 14:27:35 -0500 | [diff] [blame^] | 151 | const std::vector<std::unique_ptr<ExternalFunction>>* externalFunctions = nullptr); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 152 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 153 | bool toSPIRV(Program& program, OutputStream& out); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 154 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 155 | bool toSPIRV(Program& program, String* out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 156 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 157 | bool toGLSL(Program& program, OutputStream& out); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 158 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 159 | bool toGLSL(Program& program, String* out); |
Timothy Liang | b8eeb80 | 2018-07-23 16:46:16 -0400 | [diff] [blame] | 160 | |
Brian Osman | c024391 | 2020-02-19 15:35:26 -0500 | [diff] [blame] | 161 | bool toHLSL(Program& program, String* out); |
| 162 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 163 | bool toMetal(Program& program, OutputStream& out); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 164 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 165 | bool toMetal(Program& program, String* out); |
| 166 | |
Greg Daniel | a28ea67 | 2020-09-25 11:12:56 -0400 | [diff] [blame] | 167 | #if defined(SKSL_STANDALONE) || GR_TEST_UTILS |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 168 | bool toCPP(Program& program, String name, OutputStream& out); |
| 169 | |
| 170 | bool toH(Program& program, String name, OutputStream& out); |
Ethan Nicholas | 2a479a5 | 2020-08-18 16:29:45 -0400 | [diff] [blame] | 171 | #endif |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 172 | |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 173 | std::unique_ptr<ByteCode> toByteCode(Program& program); |
| 174 | |
Brian Osman | 2e29ab5 | 2019-09-20 12:19:11 -0400 | [diff] [blame] | 175 | #if !defined(SKSL_STANDALONE) && SK_SUPPORT_GPU |
Brian Osman | a4b9169 | 2020-08-10 14:26:16 -0400 | [diff] [blame] | 176 | bool toPipelineStage(Program& program, PipelineStageArgs* outArgs); |
Brian Osman | 2e29ab5 | 2019-09-20 12:19:11 -0400 | [diff] [blame] | 177 | #endif |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 178 | |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 179 | void error(int offset, String msg) override; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 180 | |
Ethan Nicholas | dcd2f86 | 2020-12-17 23:24:25 +0000 | [diff] [blame] | 181 | String errorText(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 182 | |
| 183 | void writeErrorCount(); |
| 184 | |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 185 | int errorCount() override { |
| 186 | return fErrorCount; |
| 187 | } |
| 188 | |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 189 | Context& context() { |
| 190 | return *fContext; |
| 191 | } |
| 192 | |
Brian Osman | 401a009 | 2020-09-10 14:47:24 -0400 | [diff] [blame] | 193 | static const char* OperatorName(Token::Kind op); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 194 | |
Brian Osman | 401a009 | 2020-09-10 14:47:24 -0400 | [diff] [blame] | 195 | // Returns true if op is '=' or any compound assignment operator ('+=', '-=', etc.) |
| 196 | static bool IsAssignment(Token::Kind op); |
| 197 | |
| 198 | // Given a compound assignment operator, returns the non-assignment version of the operator |
| 199 | // (e.g. '+=' becomes '+') |
| 200 | static Token::Kind RemoveAssignment(Token::Kind op); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 201 | |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 202 | // When SKSL_STANDALONE, fPath is used. (fData, fSize) will be (nullptr, 0) |
| 203 | // When !SKSL_STANDALONE, fData and fSize are used. fPath will be nullptr. |
| 204 | struct ModuleData { |
| 205 | const char* fPath; |
| 206 | |
| 207 | const uint8_t* fData; |
| 208 | size_t fSize; |
| 209 | }; |
| 210 | |
| 211 | static ModuleData MakeModulePath(const char* path) { |
| 212 | return ModuleData{path, /*fData=*/nullptr, /*fSize=*/0}; |
| 213 | } |
| 214 | static ModuleData MakeModuleData(const uint8_t* data, size_t size) { |
| 215 | return ModuleData{/*fPath=*/nullptr, data, size}; |
| 216 | } |
| 217 | |
| 218 | LoadedModule loadModule(Program::Kind kind, ModuleData data, std::shared_ptr<SymbolTable> base); |
| 219 | ParsedModule parseModule(Program::Kind kind, ModuleData data, const ParsedModule& base); |
Ethan Nicholas | 8da1e65 | 2019-05-24 11:01:59 -0400 | [diff] [blame] | 220 | |
Ethan Nicholas | ba9a04f | 2020-11-06 09:28:04 -0500 | [diff] [blame] | 221 | IRGenerator& irGenerator() { |
| 222 | return *fIRGenerator; |
| 223 | } |
| 224 | |
| 225 | const ParsedModule& moduleForProgramKind(Program::Kind kind); |
| 226 | |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 227 | private: |
Brian Osman | 5626998 | 2020-11-20 12:38:07 -0500 | [diff] [blame] | 228 | const ParsedModule& loadGPUModule(); |
| 229 | const ParsedModule& loadFragmentModule(); |
| 230 | const ParsedModule& loadVertexModule(); |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 231 | const ParsedModule& loadFPModule(); |
| 232 | const ParsedModule& loadGeometryModule(); |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 233 | const ParsedModule& loadPublicModule(); |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 234 | const ParsedModule& loadInterpreterModule(); |
Brian Osman | 9194675 | 2020-12-21 13:20:40 -0500 | [diff] [blame] | 235 | const ParsedModule& loadRuntimeEffectModule(); |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 236 | |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 237 | void addDefinition(const Expression* lvalue, std::unique_ptr<Expression>* expr, |
| 238 | DefinitionMap* definitions); |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 239 | void addDefinitions(const BasicBlock::Node& node, DefinitionMap* definitions); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 240 | |
John Stiles | e615000 | 2020-10-05 12:03:53 -0400 | [diff] [blame] | 241 | void scanCFG(CFG* cfg, BlockId block, SkBitSet* processedSet); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 242 | void computeDataFlow(CFG* cfg); |
| 243 | |
| 244 | /** |
| 245 | * Simplifies the expression pointed to by iter (in both the IR and CFG structures), if |
| 246 | * possible. |
| 247 | */ |
| 248 | void simplifyExpression(DefinitionMap& definitions, |
| 249 | BasicBlock& b, |
| 250 | std::vector<BasicBlock::Node>::iterator* iter, |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 251 | OptimizationContext* context); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 252 | |
| 253 | /** |
| 254 | * Simplifies the statement pointed to by iter (in both the IR and CFG structures), if |
| 255 | * possible. |
| 256 | */ |
| 257 | void simplifyStatement(DefinitionMap& definitions, |
| 258 | BasicBlock& b, |
| 259 | std::vector<BasicBlock::Node>::iterator* iter, |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 260 | OptimizationContext* context); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 261 | |
John Stiles | 0cc193a | 2020-09-09 09:39:34 -0400 | [diff] [blame] | 262 | /** |
| 263 | * Optimizes a function based on control flow analysis. Returns true if changes were made. |
| 264 | */ |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 265 | bool scanCFG(FunctionDefinition& f, ProgramUsage* usage); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 266 | |
Ethan Nicholas | 34b19c5 | 2020-09-14 11:33:47 -0400 | [diff] [blame] | 267 | /** |
| 268 | * Optimize every function in the program. |
| 269 | */ |
| 270 | bool optimize(Program& program); |
| 271 | |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 272 | bool optimize(LoadedModule& module); |
| 273 | |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 274 | Position position(int offset); |
| 275 | |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 276 | std::shared_ptr<Context> fContext; |
Brian Osman | d7e7659 | 2020-11-02 12:26:22 -0500 | [diff] [blame] | 277 | const ShaderCapsClass* fCaps = nullptr; |
| 278 | |
Brian Osman | eac4983 | 2020-09-18 11:49:22 -0400 | [diff] [blame] | 279 | std::shared_ptr<SymbolTable> fRootSymbolTable; |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 280 | std::shared_ptr<SymbolTable> fPrivateSymbolTable; |
Brian Osman | 2b469eb | 2020-09-21 11:32:10 -0400 | [diff] [blame] | 281 | |
Brian Osman | 9194675 | 2020-12-21 13:20:40 -0500 | [diff] [blame] | 282 | ParsedModule fRootModule; // Core types |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 283 | |
Brian Osman | 9194675 | 2020-12-21 13:20:40 -0500 | [diff] [blame] | 284 | ParsedModule fPrivateModule; // [Root] + Internal types |
| 285 | ParsedModule fGPUModule; // [Private] + GPU intrinsics, helper functions |
| 286 | ParsedModule fVertexModule; // [GPU] + Vertex stage decls |
| 287 | ParsedModule fFragmentModule; // [GPU] + Fragment stage decls |
| 288 | ParsedModule fGeometryModule; // [GPU] + Geometry stage decls |
| 289 | ParsedModule fFPModule; // [GPU] + FP features |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 290 | |
Brian Osman | 9194675 | 2020-12-21 13:20:40 -0500 | [diff] [blame] | 291 | ParsedModule fPublicModule; // [Root] + Public features |
| 292 | ParsedModule fInterpreterModule; // [Public] + Interpreter-only decls |
| 293 | ParsedModule fRuntimeEffectModule; // [Public] + Runtime effect decls |
Ethan Nicholas | 3c6ae62 | 2018-04-24 13:06:09 -0400 | [diff] [blame] | 294 | |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 295 | // holds ModifiersPools belonging to the core includes for lifetime purposes |
| 296 | std::vector<std::unique_ptr<ModifiersPool>> fModifiers; |
| 297 | |
John Stiles | 7b46300 | 2020-08-31 17:29:21 -0400 | [diff] [blame] | 298 | Inliner fInliner; |
John Stiles | 656427a | 2020-08-27 15:26:26 -0400 | [diff] [blame] | 299 | std::unique_ptr<IRGenerator> fIRGenerator; |
Ethan Nicholas | 6e1cbc0 | 2017-07-14 10:12:15 -0400 | [diff] [blame] | 300 | int fFlags; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 301 | |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 302 | const String* fSource; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 303 | int fErrorCount; |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 304 | String fErrorText; |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 305 | |
| 306 | friend class AutoSource; |
Ethan Nicholas | ba9a04f | 2020-11-06 09:28:04 -0500 | [diff] [blame] | 307 | friend class ::SkSLCompileBench; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 308 | }; |
| 309 | |
Brian Osman | 107c666 | 2019-12-30 15:02:30 -0500 | [diff] [blame] | 310 | #if !defined(SKSL_STANDALONE) && SK_SUPPORT_GPU |
| 311 | struct PipelineStageArgs { |
| 312 | String fCode; |
| 313 | std::vector<Compiler::FormatArg> fFormatArgs; |
| 314 | std::vector<Compiler::GLSLFunction> fFunctions; |
| 315 | }; |
| 316 | #endif |
| 317 | |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 318 | } // namespace SkSL |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 319 | |
| 320 | #endif |