John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020 Google LLC |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "src/sksl/SkSLInliner.h" |
| 9 | |
John Stiles | 2d7973a | 2020-10-02 15:01:03 -0400 | [diff] [blame] | 10 | #include <limits.h> |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 11 | #include <memory> |
| 12 | #include <unordered_set> |
| 13 | |
Ethan Nicholas | daed259 | 2021-03-04 14:30:25 -0500 | [diff] [blame] | 14 | #include "include/private/SkSLLayout.h" |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 15 | #include "src/sksl/SkSLAnalysis.h" |
| 16 | #include "src/sksl/ir/SkSLBinaryExpression.h" |
| 17 | #include "src/sksl/ir/SkSLBoolLiteral.h" |
| 18 | #include "src/sksl/ir/SkSLBreakStatement.h" |
| 19 | #include "src/sksl/ir/SkSLConstructor.h" |
John Stiles | 7384b37 | 2021-04-01 13:48:15 -0400 | [diff] [blame] | 20 | #include "src/sksl/ir/SkSLConstructorArray.h" |
John Stiles | e118278 | 2021-03-30 22:09:37 -0400 | [diff] [blame] | 21 | #include "src/sksl/ir/SkSLConstructorDiagonalMatrix.h" |
John Stiles | 5abb9e1 | 2021-04-06 13:47:19 -0400 | [diff] [blame^] | 22 | #include "src/sksl/ir/SkSLConstructorMatrixResize.h" |
John Stiles | fd7252f | 2021-04-04 22:24:40 -0400 | [diff] [blame] | 23 | #include "src/sksl/ir/SkSLConstructorScalarCast.h" |
John Stiles | 2938eea | 2021-04-01 18:58:25 -0400 | [diff] [blame] | 24 | #include "src/sksl/ir/SkSLConstructorSplat.h" |
John Stiles | b14a819 | 2021-04-05 11:40:46 -0400 | [diff] [blame] | 25 | #include "src/sksl/ir/SkSLConstructorVectorCast.h" |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 26 | #include "src/sksl/ir/SkSLContinueStatement.h" |
| 27 | #include "src/sksl/ir/SkSLDiscardStatement.h" |
| 28 | #include "src/sksl/ir/SkSLDoStatement.h" |
| 29 | #include "src/sksl/ir/SkSLEnum.h" |
| 30 | #include "src/sksl/ir/SkSLExpressionStatement.h" |
| 31 | #include "src/sksl/ir/SkSLExternalFunctionCall.h" |
Brian Osman | be0b3b7 | 2021-01-06 14:27:35 -0500 | [diff] [blame] | 32 | #include "src/sksl/ir/SkSLExternalFunctionReference.h" |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 33 | #include "src/sksl/ir/SkSLField.h" |
| 34 | #include "src/sksl/ir/SkSLFieldAccess.h" |
| 35 | #include "src/sksl/ir/SkSLFloatLiteral.h" |
| 36 | #include "src/sksl/ir/SkSLForStatement.h" |
| 37 | #include "src/sksl/ir/SkSLFunctionCall.h" |
| 38 | #include "src/sksl/ir/SkSLFunctionDeclaration.h" |
| 39 | #include "src/sksl/ir/SkSLFunctionDefinition.h" |
| 40 | #include "src/sksl/ir/SkSLFunctionReference.h" |
| 41 | #include "src/sksl/ir/SkSLIfStatement.h" |
| 42 | #include "src/sksl/ir/SkSLIndexExpression.h" |
John Stiles | 98c1f82 | 2020-09-09 14:18:53 -0400 | [diff] [blame] | 43 | #include "src/sksl/ir/SkSLInlineMarker.h" |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 44 | #include "src/sksl/ir/SkSLIntLiteral.h" |
| 45 | #include "src/sksl/ir/SkSLInterfaceBlock.h" |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 46 | #include "src/sksl/ir/SkSLNop.h" |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 47 | #include "src/sksl/ir/SkSLPostfixExpression.h" |
| 48 | #include "src/sksl/ir/SkSLPrefixExpression.h" |
| 49 | #include "src/sksl/ir/SkSLReturnStatement.h" |
| 50 | #include "src/sksl/ir/SkSLSetting.h" |
| 51 | #include "src/sksl/ir/SkSLSwitchCase.h" |
| 52 | #include "src/sksl/ir/SkSLSwitchStatement.h" |
| 53 | #include "src/sksl/ir/SkSLSwizzle.h" |
| 54 | #include "src/sksl/ir/SkSLTernaryExpression.h" |
| 55 | #include "src/sksl/ir/SkSLUnresolvedFunction.h" |
| 56 | #include "src/sksl/ir/SkSLVarDeclarations.h" |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 57 | #include "src/sksl/ir/SkSLVariable.h" |
| 58 | #include "src/sksl/ir/SkSLVariableReference.h" |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 59 | |
| 60 | namespace SkSL { |
| 61 | namespace { |
| 62 | |
John Stiles | 031a767 | 2020-11-13 16:13:18 -0500 | [diff] [blame] | 63 | static constexpr int kInlinedStatementLimit = 2500; |
| 64 | |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 65 | static int count_returns_at_end_of_control_flow(const FunctionDefinition& funcDef) { |
| 66 | class CountReturnsAtEndOfControlFlow : public ProgramVisitor { |
| 67 | public: |
| 68 | CountReturnsAtEndOfControlFlow(const FunctionDefinition& funcDef) { |
| 69 | this->visitProgramElement(funcDef); |
| 70 | } |
| 71 | |
John Stiles | 5b408a3 | 2021-03-17 09:53:32 -0400 | [diff] [blame] | 72 | bool visitExpression(const Expression& expr) override { |
| 73 | // Do not recurse into expressions. |
| 74 | return false; |
| 75 | } |
| 76 | |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 77 | bool visitStatement(const Statement& stmt) override { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 78 | switch (stmt.kind()) { |
| 79 | case Statement::Kind::kBlock: { |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 80 | // Check only the last statement of a block. |
Ethan Nicholas | 7bd6043 | 2020-09-25 14:31:59 -0400 | [diff] [blame] | 81 | const auto& block = stmt.as<Block>(); |
| 82 | return block.children().size() && |
| 83 | this->visitStatement(*block.children().back()); |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 84 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 85 | case Statement::Kind::kSwitch: |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 86 | case Statement::Kind::kDo: |
| 87 | case Statement::Kind::kFor: |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 88 | // Don't introspect switches or loop structures at all. |
| 89 | return false; |
| 90 | |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 91 | case Statement::Kind::kReturn: |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 92 | ++fNumReturns; |
| 93 | [[fallthrough]]; |
| 94 | |
| 95 | default: |
John Stiles | 9344262 | 2020-09-11 12:11:27 -0400 | [diff] [blame] | 96 | return INHERITED::visitStatement(stmt); |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 97 | } |
| 98 | } |
| 99 | |
| 100 | int fNumReturns = 0; |
| 101 | using INHERITED = ProgramVisitor; |
| 102 | }; |
| 103 | |
| 104 | return CountReturnsAtEndOfControlFlow{funcDef}.fNumReturns; |
| 105 | } |
| 106 | |
John Stiles | 991b09d | 2020-09-10 13:33:40 -0400 | [diff] [blame] | 107 | static bool contains_recursive_call(const FunctionDeclaration& funcDecl) { |
| 108 | class ContainsRecursiveCall : public ProgramVisitor { |
| 109 | public: |
| 110 | bool visit(const FunctionDeclaration& funcDecl) { |
| 111 | fFuncDecl = &funcDecl; |
Ethan Nicholas | ed84b73 | 2020-10-08 11:45:44 -0400 | [diff] [blame] | 112 | return funcDecl.definition() ? this->visitProgramElement(*funcDecl.definition()) |
| 113 | : false; |
John Stiles | 991b09d | 2020-09-10 13:33:40 -0400 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | bool visitExpression(const Expression& expr) override { |
Ethan Nicholas | 0dec992 | 2020-10-05 15:51:52 -0400 | [diff] [blame] | 117 | if (expr.is<FunctionCall>() && expr.as<FunctionCall>().function().matches(*fFuncDecl)) { |
John Stiles | 991b09d | 2020-09-10 13:33:40 -0400 | [diff] [blame] | 118 | return true; |
| 119 | } |
| 120 | return INHERITED::visitExpression(expr); |
| 121 | } |
| 122 | |
| 123 | bool visitStatement(const Statement& stmt) override { |
Ethan Nicholas | ceb6214 | 2020-10-09 16:51:18 -0400 | [diff] [blame] | 124 | if (stmt.is<InlineMarker>() && |
| 125 | stmt.as<InlineMarker>().function().matches(*fFuncDecl)) { |
John Stiles | 991b09d | 2020-09-10 13:33:40 -0400 | [diff] [blame] | 126 | return true; |
| 127 | } |
| 128 | return INHERITED::visitStatement(stmt); |
| 129 | } |
| 130 | |
| 131 | const FunctionDeclaration* fFuncDecl; |
| 132 | using INHERITED = ProgramVisitor; |
| 133 | }; |
| 134 | |
| 135 | return ContainsRecursiveCall{}.visit(funcDecl); |
| 136 | } |
| 137 | |
John Stiles | 6d69608 | 2020-10-01 10:18:54 -0400 | [diff] [blame] | 138 | static std::unique_ptr<Statement>* find_parent_statement( |
| 139 | const std::vector<std::unique_ptr<Statement>*>& stmtStack) { |
John Stiles | 915a38c | 2020-09-14 09:38:13 -0400 | [diff] [blame] | 140 | SkASSERT(!stmtStack.empty()); |
| 141 | |
| 142 | // Walk the statement stack from back to front, ignoring the last element (which is the |
| 143 | // enclosing statement). |
| 144 | auto iter = stmtStack.rbegin(); |
| 145 | ++iter; |
| 146 | |
| 147 | // Anything counts as a parent statement other than a scopeless Block. |
| 148 | for (; iter != stmtStack.rend(); ++iter) { |
John Stiles | 6d69608 | 2020-10-01 10:18:54 -0400 | [diff] [blame] | 149 | std::unique_ptr<Statement>* stmt = *iter; |
| 150 | if (!(*stmt)->is<Block>() || (*stmt)->as<Block>().isScope()) { |
John Stiles | 915a38c | 2020-09-14 09:38:13 -0400 | [diff] [blame] | 151 | return stmt; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | // There wasn't any parent statement to be found. |
| 156 | return nullptr; |
| 157 | } |
| 158 | |
John Stiles | e41b4ee | 2020-09-28 12:28:16 -0400 | [diff] [blame] | 159 | std::unique_ptr<Expression> clone_with_ref_kind(const Expression& expr, |
| 160 | VariableReference::RefKind refKind) { |
| 161 | std::unique_ptr<Expression> clone = expr.clone(); |
John Stiles | 47c0a74 | 2021-02-09 09:30:35 -0500 | [diff] [blame] | 162 | Analysis::UpdateRefKind(clone.get(), refKind); |
John Stiles | e41b4ee | 2020-09-28 12:28:16 -0400 | [diff] [blame] | 163 | return clone; |
| 164 | } |
| 165 | |
John Stiles | 77702f1 | 2020-12-17 14:38:56 -0500 | [diff] [blame] | 166 | class CountReturnsWithLimit : public ProgramVisitor { |
| 167 | public: |
| 168 | CountReturnsWithLimit(const FunctionDefinition& funcDef, int limit) : fLimit(limit) { |
| 169 | this->visitProgramElement(funcDef); |
| 170 | } |
| 171 | |
John Stiles | 5b408a3 | 2021-03-17 09:53:32 -0400 | [diff] [blame] | 172 | bool visitExpression(const Expression& expr) override { |
| 173 | // Do not recurse into expressions. |
| 174 | return false; |
| 175 | } |
| 176 | |
John Stiles | 77702f1 | 2020-12-17 14:38:56 -0500 | [diff] [blame] | 177 | bool visitStatement(const Statement& stmt) override { |
| 178 | switch (stmt.kind()) { |
| 179 | case Statement::Kind::kReturn: { |
| 180 | ++fNumReturns; |
| 181 | fDeepestReturn = std::max(fDeepestReturn, fScopedBlockDepth); |
| 182 | return (fNumReturns >= fLimit) || INHERITED::visitStatement(stmt); |
| 183 | } |
John Stiles | c5ff486 | 2020-12-22 13:47:05 -0500 | [diff] [blame] | 184 | case Statement::Kind::kVarDeclaration: { |
| 185 | if (fScopedBlockDepth > 1) { |
| 186 | fVariablesInBlocks = true; |
| 187 | } |
| 188 | return INHERITED::visitStatement(stmt); |
| 189 | } |
John Stiles | 77702f1 | 2020-12-17 14:38:56 -0500 | [diff] [blame] | 190 | case Statement::Kind::kBlock: { |
| 191 | int depthIncrement = stmt.as<Block>().isScope() ? 1 : 0; |
| 192 | fScopedBlockDepth += depthIncrement; |
| 193 | bool result = INHERITED::visitStatement(stmt); |
| 194 | fScopedBlockDepth -= depthIncrement; |
John Stiles | c5ff486 | 2020-12-22 13:47:05 -0500 | [diff] [blame] | 195 | if (fNumReturns == 0 && fScopedBlockDepth <= 1) { |
| 196 | // If closing this block puts us back at the top level, and we haven't |
| 197 | // encountered any return statements yet, any vardecls we may have encountered |
| 198 | // up until this point can be ignored. They are out of scope now, and they were |
| 199 | // never used in a return statement. |
| 200 | fVariablesInBlocks = false; |
| 201 | } |
John Stiles | 77702f1 | 2020-12-17 14:38:56 -0500 | [diff] [blame] | 202 | return result; |
| 203 | } |
| 204 | default: |
| 205 | return INHERITED::visitStatement(stmt); |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | int fNumReturns = 0; |
| 210 | int fDeepestReturn = 0; |
| 211 | int fLimit = 0; |
| 212 | int fScopedBlockDepth = 0; |
John Stiles | c5ff486 | 2020-12-22 13:47:05 -0500 | [diff] [blame] | 213 | bool fVariablesInBlocks = false; |
John Stiles | 77702f1 | 2020-12-17 14:38:56 -0500 | [diff] [blame] | 214 | using INHERITED = ProgramVisitor; |
| 215 | }; |
| 216 | |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 217 | } // namespace |
| 218 | |
John Stiles | 77702f1 | 2020-12-17 14:38:56 -0500 | [diff] [blame] | 219 | Inliner::ReturnComplexity Inliner::GetReturnComplexity(const FunctionDefinition& funcDef) { |
| 220 | int returnsAtEndOfControlFlow = count_returns_at_end_of_control_flow(funcDef); |
| 221 | CountReturnsWithLimit counter{funcDef, returnsAtEndOfControlFlow + 1}; |
John Stiles | 77702f1 | 2020-12-17 14:38:56 -0500 | [diff] [blame] | 222 | if (counter.fNumReturns > returnsAtEndOfControlFlow) { |
| 223 | return ReturnComplexity::kEarlyReturns; |
| 224 | } |
John Stiles | c5ff486 | 2020-12-22 13:47:05 -0500 | [diff] [blame] | 225 | if (counter.fNumReturns > 1) { |
John Stiles | 77702f1 | 2020-12-17 14:38:56 -0500 | [diff] [blame] | 226 | return ReturnComplexity::kScopedReturns; |
| 227 | } |
John Stiles | c5ff486 | 2020-12-22 13:47:05 -0500 | [diff] [blame] | 228 | if (counter.fVariablesInBlocks && counter.fDeepestReturn > 1) { |
| 229 | return ReturnComplexity::kScopedReturns; |
| 230 | } |
John Stiles | 8937cd4 | 2021-03-17 19:32:59 +0000 | [diff] [blame] | 231 | return ReturnComplexity::kSingleSafeReturn; |
John Stiles | 77702f1 | 2020-12-17 14:38:56 -0500 | [diff] [blame] | 232 | } |
| 233 | |
John Stiles | b61ee90 | 2020-09-21 12:26:59 -0400 | [diff] [blame] | 234 | void Inliner::ensureScopedBlocks(Statement* inlinedBody, Statement* parentStmt) { |
| 235 | // No changes necessary if this statement isn't actually a block. |
| 236 | if (!inlinedBody || !inlinedBody->is<Block>()) { |
| 237 | return; |
| 238 | } |
| 239 | |
| 240 | // No changes necessary if the parent statement doesn't require a scope. |
| 241 | if (!parentStmt || !(parentStmt->is<IfStatement>() || parentStmt->is<ForStatement>() || |
Brian Osman | d6f2338 | 2020-12-15 17:08:59 -0500 | [diff] [blame] | 242 | parentStmt->is<DoStatement>())) { |
John Stiles | b61ee90 | 2020-09-21 12:26:59 -0400 | [diff] [blame] | 243 | return; |
| 244 | } |
| 245 | |
| 246 | Block& block = inlinedBody->as<Block>(); |
| 247 | |
| 248 | // The inliner will create inlined function bodies as a Block containing multiple statements, |
| 249 | // but no scope. Normally, this is fine, but if this block is used as the statement for a |
| 250 | // do/for/if/while, this isn't actually possible to represent textually; a scope must be added |
| 251 | // for the generated code to match the intent. In the case of Blocks nested inside other Blocks, |
| 252 | // we add the scope to the outermost block if needed. Zero-statement blocks have similar |
| 253 | // issues--if we don't represent the Block textually somehow, we run the risk of accidentally |
| 254 | // absorbing the following statement into our loop--so we also add a scope to these. |
| 255 | for (Block* nestedBlock = █; ) { |
Ethan Nicholas | 7bd6043 | 2020-09-25 14:31:59 -0400 | [diff] [blame] | 256 | if (nestedBlock->isScope()) { |
John Stiles | b61ee90 | 2020-09-21 12:26:59 -0400 | [diff] [blame] | 257 | // We found an explicit scope; all is well. |
| 258 | return; |
| 259 | } |
Ethan Nicholas | 7bd6043 | 2020-09-25 14:31:59 -0400 | [diff] [blame] | 260 | if (nestedBlock->children().size() != 1) { |
John Stiles | b61ee90 | 2020-09-21 12:26:59 -0400 | [diff] [blame] | 261 | // We found a block with multiple (or zero) statements, but no scope? Let's add a scope |
| 262 | // to the outermost block. |
Ethan Nicholas | 7bd6043 | 2020-09-25 14:31:59 -0400 | [diff] [blame] | 263 | block.setIsScope(true); |
John Stiles | b61ee90 | 2020-09-21 12:26:59 -0400 | [diff] [blame] | 264 | return; |
| 265 | } |
Ethan Nicholas | 7bd6043 | 2020-09-25 14:31:59 -0400 | [diff] [blame] | 266 | if (!nestedBlock->children()[0]->is<Block>()) { |
John Stiles | b61ee90 | 2020-09-21 12:26:59 -0400 | [diff] [blame] | 267 | // This block has exactly one thing inside, and it's not another block. No need to scope |
| 268 | // it. |
| 269 | return; |
| 270 | } |
| 271 | // We have to go deeper. |
Ethan Nicholas | 7bd6043 | 2020-09-25 14:31:59 -0400 | [diff] [blame] | 272 | nestedBlock = &nestedBlock->children()[0]->as<Block>(); |
John Stiles | b61ee90 | 2020-09-21 12:26:59 -0400 | [diff] [blame] | 273 | } |
| 274 | } |
| 275 | |
John Stiles | d120464 | 2021-02-17 16:30:02 -0500 | [diff] [blame] | 276 | void Inliner::reset(ModifiersPool* modifiers) { |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 277 | fModifiers = modifiers; |
Ethan Nicholas | 6f4eee2 | 2021-01-11 12:37:42 -0500 | [diff] [blame] | 278 | fMangler.reset(); |
John Stiles | 031a767 | 2020-11-13 16:13:18 -0500 | [diff] [blame] | 279 | fInlinedStatementCounter = 0; |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | std::unique_ptr<Expression> Inliner::inlineExpression(int offset, |
| 283 | VariableRewriteMap* varMap, |
John Stiles | d7cc093 | 2020-11-30 12:24:27 -0500 | [diff] [blame] | 284 | SymbolTable* symbolTableForExpression, |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 285 | const Expression& expression) { |
| 286 | auto expr = [&](const std::unique_ptr<Expression>& e) -> std::unique_ptr<Expression> { |
| 287 | if (e) { |
John Stiles | d7cc093 | 2020-11-30 12:24:27 -0500 | [diff] [blame] | 288 | return this->inlineExpression(offset, varMap, symbolTableForExpression, *e); |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 289 | } |
| 290 | return nullptr; |
| 291 | }; |
John Stiles | 8e3b6be | 2020-10-13 11:14:08 -0400 | [diff] [blame] | 292 | auto argList = [&](const ExpressionArray& originalArgs) -> ExpressionArray { |
| 293 | ExpressionArray args; |
John Stiles | f4bda74 | 2020-10-14 16:57:41 -0400 | [diff] [blame] | 294 | args.reserve_back(originalArgs.size()); |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 295 | for (const std::unique_ptr<Expression>& arg : originalArgs) { |
| 296 | args.push_back(expr(arg)); |
| 297 | } |
| 298 | return args; |
| 299 | }; |
| 300 | |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 301 | switch (expression.kind()) { |
| 302 | case Expression::Kind::kBinary: { |
John Stiles | 6a1a98c | 2021-01-14 18:35:34 -0500 | [diff] [blame] | 303 | const BinaryExpression& binaryExpr = expression.as<BinaryExpression>(); |
John Stiles | e2aec43 | 2021-03-01 09:27:48 -0500 | [diff] [blame] | 304 | return BinaryExpression::Make(*fContext, |
| 305 | expr(binaryExpr.left()), |
| 306 | binaryExpr.getOperator(), |
| 307 | expr(binaryExpr.right())); |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 308 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 309 | case Expression::Kind::kBoolLiteral: |
| 310 | case Expression::Kind::kIntLiteral: |
| 311 | case Expression::Kind::kFloatLiteral: |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 312 | return expression.clone(); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 313 | case Expression::Kind::kConstructor: { |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 314 | const Constructor& constructor = expression.as<Constructor>(); |
John Stiles | 23521a8 | 2021-03-02 17:02:51 -0500 | [diff] [blame] | 315 | auto inlinedCtor = Constructor::Convert( |
| 316 | *fContext, offset, *constructor.type().clone(symbolTableForExpression), |
| 317 | argList(constructor.arguments())); |
| 318 | SkASSERT(inlinedCtor); |
| 319 | return inlinedCtor; |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 320 | } |
John Stiles | 7384b37 | 2021-04-01 13:48:15 -0400 | [diff] [blame] | 321 | case Expression::Kind::kConstructorArray: { |
| 322 | const ConstructorArray& ctor = expression.as<ConstructorArray>(); |
| 323 | return ConstructorArray::Make(*fContext, offset, |
| 324 | *ctor.type().clone(symbolTableForExpression), |
| 325 | argList(ctor.arguments())); |
| 326 | } |
John Stiles | e118278 | 2021-03-30 22:09:37 -0400 | [diff] [blame] | 327 | case Expression::Kind::kConstructorDiagonalMatrix: { |
| 328 | const ConstructorDiagonalMatrix& ctor = expression.as<ConstructorDiagonalMatrix>(); |
| 329 | return ConstructorDiagonalMatrix::Make(*fContext, offset, |
| 330 | *ctor.type().clone(symbolTableForExpression), |
| 331 | expr(ctor.argument())); |
| 332 | } |
John Stiles | 5abb9e1 | 2021-04-06 13:47:19 -0400 | [diff] [blame^] | 333 | case Expression::Kind::kConstructorMatrixResize: { |
| 334 | const ConstructorMatrixResize& ctor = expression.as<ConstructorMatrixResize>(); |
| 335 | return ConstructorMatrixResize::Make(*fContext, offset, |
| 336 | *ctor.type().clone(symbolTableForExpression), |
| 337 | expr(ctor.argument())); |
| 338 | } |
John Stiles | fd7252f | 2021-04-04 22:24:40 -0400 | [diff] [blame] | 339 | case Expression::Kind::kConstructorScalarCast: { |
| 340 | const ConstructorScalarCast& ctor = expression.as<ConstructorScalarCast>(); |
| 341 | return ConstructorScalarCast::Make(*fContext, offset, |
| 342 | *ctor.type().clone(symbolTableForExpression), |
| 343 | expr(ctor.argument())); |
| 344 | } |
John Stiles | 2938eea | 2021-04-01 18:58:25 -0400 | [diff] [blame] | 345 | case Expression::Kind::kConstructorSplat: { |
| 346 | const ConstructorSplat& ctor = expression.as<ConstructorSplat>(); |
| 347 | return ConstructorSplat::Make(*fContext, offset, |
| 348 | *ctor.type().clone(symbolTableForExpression), |
| 349 | expr(ctor.argument())); |
| 350 | } |
John Stiles | b14a819 | 2021-04-05 11:40:46 -0400 | [diff] [blame] | 351 | case Expression::Kind::kConstructorVectorCast: { |
| 352 | const ConstructorVectorCast& ctor = expression.as<ConstructorVectorCast>(); |
| 353 | return ConstructorVectorCast::Make(*fContext, offset, |
| 354 | *ctor.type().clone(symbolTableForExpression), |
| 355 | expr(ctor.argument())); |
| 356 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 357 | case Expression::Kind::kExternalFunctionCall: { |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 358 | const ExternalFunctionCall& externalCall = expression.as<ExternalFunctionCall>(); |
Ethan Nicholas | 444ccc6 | 2020-10-09 10:16:22 -0400 | [diff] [blame] | 359 | return std::make_unique<ExternalFunctionCall>(offset, &externalCall.function(), |
Ethan Nicholas | 6e86ec9 | 2020-09-30 14:29:56 -0400 | [diff] [blame] | 360 | argList(externalCall.arguments())); |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 361 | } |
Brian Osman | be0b3b7 | 2021-01-06 14:27:35 -0500 | [diff] [blame] | 362 | case Expression::Kind::kExternalFunctionReference: |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 363 | return expression.clone(); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 364 | case Expression::Kind::kFieldAccess: { |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 365 | const FieldAccess& f = expression.as<FieldAccess>(); |
John Stiles | 06d600f | 2021-03-08 09:18:21 -0500 | [diff] [blame] | 366 | return FieldAccess::Make(*fContext, expr(f.base()), f.fieldIndex(), f.ownerKind()); |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 367 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 368 | case Expression::Kind::kFunctionCall: { |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 369 | const FunctionCall& funcCall = expression.as<FunctionCall>(); |
John Stiles | cd7ba50 | 2021-03-19 10:54:59 -0400 | [diff] [blame] | 370 | return FunctionCall::Make(*fContext, |
| 371 | offset, |
| 372 | funcCall.type().clone(symbolTableForExpression), |
| 373 | funcCall.function(), |
| 374 | argList(funcCall.arguments())); |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 375 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 376 | case Expression::Kind::kFunctionReference: |
Brian Osman | 2b3b35f | 2020-09-08 09:17:36 -0400 | [diff] [blame] | 377 | return expression.clone(); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 378 | case Expression::Kind::kIndex: { |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 379 | const IndexExpression& idx = expression.as<IndexExpression>(); |
John Stiles | 51d3398 | 2021-03-08 09:18:07 -0500 | [diff] [blame] | 380 | return IndexExpression::Make(*fContext, expr(idx.base()), expr(idx.index())); |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 381 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 382 | case Expression::Kind::kPrefix: { |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 383 | const PrefixExpression& p = expression.as<PrefixExpression>(); |
John Stiles | b0eb20f | 2021-02-26 15:29:33 -0500 | [diff] [blame] | 384 | return PrefixExpression::Make(*fContext, p.getOperator(), expr(p.operand())); |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 385 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 386 | case Expression::Kind::kPostfix: { |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 387 | const PostfixExpression& p = expression.as<PostfixExpression>(); |
John Stiles | 52d3b01 | 2021-02-26 15:56:48 -0500 | [diff] [blame] | 388 | return PostfixExpression::Make(*fContext, expr(p.operand()), p.getOperator()); |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 389 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 390 | case Expression::Kind::kSetting: |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 391 | return expression.clone(); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 392 | case Expression::Kind::kSwizzle: { |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 393 | const Swizzle& s = expression.as<Swizzle>(); |
John Stiles | 6e88e04 | 2021-02-19 14:09:38 -0500 | [diff] [blame] | 394 | return Swizzle::Make(*fContext, expr(s.base()), s.components()); |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 395 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 396 | case Expression::Kind::kTernary: { |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 397 | const TernaryExpression& t = expression.as<TernaryExpression>(); |
John Stiles | 90518f7 | 2021-02-26 20:44:54 -0500 | [diff] [blame] | 398 | return TernaryExpression::Make(*fContext, expr(t.test()), |
| 399 | expr(t.ifTrue()), expr(t.ifFalse())); |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 400 | } |
Brian Osman | 83ba930 | 2020-09-11 13:33:46 -0400 | [diff] [blame] | 401 | case Expression::Kind::kTypeReference: |
| 402 | return expression.clone(); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 403 | case Expression::Kind::kVariableReference: { |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 404 | const VariableReference& v = expression.as<VariableReference>(); |
Ethan Nicholas | 7868692 | 2020-10-08 06:46:27 -0400 | [diff] [blame] | 405 | auto varMapIter = varMap->find(v.variable()); |
John Stiles | e41b4ee | 2020-09-28 12:28:16 -0400 | [diff] [blame] | 406 | if (varMapIter != varMap->end()) { |
Ethan Nicholas | 7868692 | 2020-10-08 06:46:27 -0400 | [diff] [blame] | 407 | return clone_with_ref_kind(*varMapIter->second, v.refKind()); |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 408 | } |
| 409 | return v.clone(); |
| 410 | } |
| 411 | default: |
| 412 | SkASSERT(false); |
| 413 | return nullptr; |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | std::unique_ptr<Statement> Inliner::inlineStatement(int offset, |
| 418 | VariableRewriteMap* varMap, |
| 419 | SymbolTable* symbolTableForStatement, |
John Stiles | 77702f1 | 2020-12-17 14:38:56 -0500 | [diff] [blame] | 420 | std::unique_ptr<Expression>* resultExpr, |
| 421 | ReturnComplexity returnComplexity, |
Brian Osman | 3887a01 | 2020-09-30 13:22:27 -0400 | [diff] [blame] | 422 | const Statement& statement, |
| 423 | bool isBuiltinCode) { |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 424 | auto stmt = [&](const std::unique_ptr<Statement>& s) -> std::unique_ptr<Statement> { |
| 425 | if (s) { |
John Stiles | a5f3c31 | 2020-09-22 12:05:16 -0400 | [diff] [blame] | 426 | return this->inlineStatement(offset, varMap, symbolTableForStatement, resultExpr, |
John Stiles | 77702f1 | 2020-12-17 14:38:56 -0500 | [diff] [blame] | 427 | returnComplexity, *s, isBuiltinCode); |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 428 | } |
| 429 | return nullptr; |
| 430 | }; |
Ethan Nicholas | 7bd6043 | 2020-09-25 14:31:59 -0400 | [diff] [blame] | 431 | auto blockStmts = [&](const Block& block) { |
John Stiles | 8f2a0cf | 2020-10-13 12:48:21 -0400 | [diff] [blame] | 432 | StatementArray result; |
John Stiles | f4bda74 | 2020-10-14 16:57:41 -0400 | [diff] [blame] | 433 | result.reserve_back(block.children().size()); |
Ethan Nicholas | 7bd6043 | 2020-09-25 14:31:59 -0400 | [diff] [blame] | 434 | for (const std::unique_ptr<Statement>& child : block.children()) { |
| 435 | result.push_back(stmt(child)); |
| 436 | } |
| 437 | return result; |
| 438 | }; |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 439 | auto expr = [&](const std::unique_ptr<Expression>& e) -> std::unique_ptr<Expression> { |
| 440 | if (e) { |
John Stiles | d7cc093 | 2020-11-30 12:24:27 -0500 | [diff] [blame] | 441 | return this->inlineExpression(offset, varMap, symbolTableForStatement, *e); |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 442 | } |
| 443 | return nullptr; |
| 444 | }; |
John Stiles | 031a767 | 2020-11-13 16:13:18 -0500 | [diff] [blame] | 445 | |
| 446 | ++fInlinedStatementCounter; |
| 447 | |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 448 | switch (statement.kind()) { |
| 449 | case Statement::Kind::kBlock: { |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 450 | const Block& b = statement.as<Block>(); |
John Stiles | bf16b6c | 2021-03-12 19:24:31 -0500 | [diff] [blame] | 451 | return Block::Make(offset, blockStmts(b), |
| 452 | SymbolTable::WrapIfBuiltin(b.symbolTable()), |
| 453 | b.isScope()); |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 454 | } |
| 455 | |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 456 | case Statement::Kind::kBreak: |
| 457 | case Statement::Kind::kContinue: |
| 458 | case Statement::Kind::kDiscard: |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 459 | return statement.clone(); |
| 460 | |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 461 | case Statement::Kind::kDo: { |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 462 | const DoStatement& d = statement.as<DoStatement>(); |
John Stiles | ea5822e | 2021-02-26 11:18:20 -0500 | [diff] [blame] | 463 | return DoStatement::Make(*fContext, stmt(d.statement()), expr(d.test())); |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 464 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 465 | case Statement::Kind::kExpression: { |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 466 | const ExpressionStatement& e = statement.as<ExpressionStatement>(); |
John Stiles | 3e5871c | 2021-02-25 20:52:03 -0500 | [diff] [blame] | 467 | return ExpressionStatement::Make(*fContext, expr(e.expression())); |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 468 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 469 | case Statement::Kind::kFor: { |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 470 | const ForStatement& f = statement.as<ForStatement>(); |
| 471 | // need to ensure initializer is evaluated first so that we've already remapped its |
| 472 | // declarations by the time we evaluate test & next |
Ethan Nicholas | 0d31ed5 | 2020-10-05 14:47:09 -0400 | [diff] [blame] | 473 | std::unique_ptr<Statement> initializer = stmt(f.initializer()); |
John Stiles | b321a07 | 2021-02-25 16:24:19 -0500 | [diff] [blame] | 474 | return ForStatement::Make(*fContext, offset, std::move(initializer), expr(f.test()), |
| 475 | expr(f.next()), stmt(f.statement()), |
| 476 | SymbolTable::WrapIfBuiltin(f.symbols())); |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 477 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 478 | case Statement::Kind::kIf: { |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 479 | const IfStatement& i = statement.as<IfStatement>(); |
John Stiles | cf3059e | 2021-02-25 14:27:02 -0500 | [diff] [blame] | 480 | return IfStatement::Make(*fContext, offset, i.isStatic(), expr(i.test()), |
| 481 | stmt(i.ifTrue()), stmt(i.ifFalse())); |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 482 | } |
John Stiles | 98c1f82 | 2020-09-09 14:18:53 -0400 | [diff] [blame] | 483 | case Statement::Kind::kInlineMarker: |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 484 | case Statement::Kind::kNop: |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 485 | return statement.clone(); |
John Stiles | ea5822e | 2021-02-26 11:18:20 -0500 | [diff] [blame] | 486 | |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 487 | case Statement::Kind::kReturn: { |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 488 | const ReturnStatement& r = statement.as<ReturnStatement>(); |
John Stiles | 77702f1 | 2020-12-17 14:38:56 -0500 | [diff] [blame] | 489 | if (!r.expression()) { |
John Stiles | dc20847 | 2021-03-17 10:58:16 -0400 | [diff] [blame] | 490 | // This function doesn't return a value. We won't inline functions with early |
| 491 | // returns, so a return statement is a no-op and can be treated as such. |
| 492 | return Nop::Make(); |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 493 | } |
John Stiles | 77702f1 | 2020-12-17 14:38:56 -0500 | [diff] [blame] | 494 | |
John Stiles | c5ff486 | 2020-12-22 13:47:05 -0500 | [diff] [blame] | 495 | // If a function only contains a single return, and it doesn't reference variables from |
| 496 | // inside an Block's scope, we don't need to store the result in a variable at all. Just |
| 497 | // replace the function-call expression with the function's return expression. |
John Stiles | 77702f1 | 2020-12-17 14:38:56 -0500 | [diff] [blame] | 498 | SkASSERT(resultExpr); |
John Stiles | c5ff486 | 2020-12-22 13:47:05 -0500 | [diff] [blame] | 499 | if (returnComplexity <= ReturnComplexity::kSingleSafeReturn) { |
John Stiles | 77702f1 | 2020-12-17 14:38:56 -0500 | [diff] [blame] | 500 | *resultExpr = expr(r.expression()); |
John Stiles | a0c04d6 | 2021-03-11 23:07:24 -0500 | [diff] [blame] | 501 | return Nop::Make(); |
John Stiles | 77702f1 | 2020-12-17 14:38:56 -0500 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | // For more complex functions, assign their result into a variable. |
John Stiles | 511c500 | 2021-02-25 11:17:02 -0500 | [diff] [blame] | 505 | SkASSERT(*resultExpr); |
John Stiles | 3e5871c | 2021-02-25 20:52:03 -0500 | [diff] [blame] | 506 | auto assignment = ExpressionStatement::Make( |
| 507 | *fContext, |
John Stiles | e2aec43 | 2021-03-01 09:27:48 -0500 | [diff] [blame] | 508 | BinaryExpression::Make( |
| 509 | *fContext, |
| 510 | clone_with_ref_kind(**resultExpr, VariableRefKind::kWrite), |
John Stiles | 77702f1 | 2020-12-17 14:38:56 -0500 | [diff] [blame] | 511 | Token::Kind::TK_EQ, |
John Stiles | e2aec43 | 2021-03-01 09:27:48 -0500 | [diff] [blame] | 512 | expr(r.expression()))); |
John Stiles | 77702f1 | 2020-12-17 14:38:56 -0500 | [diff] [blame] | 513 | |
John Stiles | 77702f1 | 2020-12-17 14:38:56 -0500 | [diff] [blame] | 514 | // Functions without early returns aren't wrapped in a for loop and don't need to worry |
| 515 | // about breaking out of the control flow. |
John Stiles | 3e5871c | 2021-02-25 20:52:03 -0500 | [diff] [blame] | 516 | return assignment; |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 517 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 518 | case Statement::Kind::kSwitch: { |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 519 | const SwitchStatement& ss = statement.as<SwitchStatement>(); |
John Stiles | b23a64b | 2021-03-11 08:27:59 -0500 | [diff] [blame] | 520 | StatementArray cases; |
| 521 | cases.reserve_back(ss.cases().size()); |
| 522 | for (const std::unique_ptr<Statement>& statement : ss.cases()) { |
| 523 | const SwitchCase& sc = statement->as<SwitchCase>(); |
| 524 | cases.push_back(std::make_unique<SwitchCase>(offset, expr(sc.value()), |
| 525 | stmt(sc.statement()))); |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 526 | } |
John Stiles | e1d1b08 | 2021-02-23 13:44:36 -0500 | [diff] [blame] | 527 | return SwitchStatement::Make(*fContext, offset, ss.isStatic(), expr(ss.value()), |
| 528 | std::move(cases), SymbolTable::WrapIfBuiltin(ss.symbols())); |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 529 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 530 | case Statement::Kind::kVarDeclaration: { |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 531 | const VarDeclaration& decl = statement.as<VarDeclaration>(); |
John Stiles | 35fee4c | 2020-12-16 18:25:14 +0000 | [diff] [blame] | 532 | std::unique_ptr<Expression> initialValue = expr(decl.value()); |
John Stiles | ddcc843 | 2021-01-15 15:32:32 -0500 | [diff] [blame] | 533 | const Variable& variable = decl.var(); |
| 534 | |
John Stiles | 35fee4c | 2020-12-16 18:25:14 +0000 | [diff] [blame] | 535 | // We assign unique names to inlined variables--scopes hide most of the problems in this |
| 536 | // regard, but see `InlinerAvoidsVariableNameOverlap` for a counterexample where unique |
| 537 | // names are important. |
John Stiles | d51c979 | 2021-03-18 11:40:14 -0400 | [diff] [blame] | 538 | const String* name = symbolTableForStatement->takeOwnershipOfString( |
| 539 | fMangler.uniqueName(variable.name(), symbolTableForStatement)); |
Ethan Nicholas | 5b9b0db | 2021-01-21 13:12:01 -0500 | [diff] [blame] | 540 | auto clonedVar = std::make_unique<Variable>( |
| 541 | offset, |
| 542 | &variable.modifiers(), |
John Stiles | d51c979 | 2021-03-18 11:40:14 -0400 | [diff] [blame] | 543 | name->c_str(), |
Ethan Nicholas | 5b9b0db | 2021-01-21 13:12:01 -0500 | [diff] [blame] | 544 | variable.type().clone(symbolTableForStatement), |
| 545 | isBuiltinCode, |
| 546 | variable.storage()); |
| 547 | (*varMap)[&variable] = std::make_unique<VariableReference>(offset, clonedVar.get()); |
John Stiles | e67bd13 | 2021-03-19 18:39:25 -0400 | [diff] [blame] | 548 | auto result = VarDeclaration::Make(*fContext, |
| 549 | clonedVar.get(), |
| 550 | decl.baseType().clone(symbolTableForStatement), |
| 551 | decl.arraySize(), |
| 552 | std::move(initialValue)); |
Ethan Nicholas | 5b9b0db | 2021-01-21 13:12:01 -0500 | [diff] [blame] | 553 | symbolTableForStatement->takeOwnershipOfSymbol(std::move(clonedVar)); |
John Stiles | e67bd13 | 2021-03-19 18:39:25 -0400 | [diff] [blame] | 554 | return result; |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 555 | } |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 556 | default: |
| 557 | SkASSERT(false); |
| 558 | return nullptr; |
| 559 | } |
| 560 | } |
| 561 | |
John Stiles | 7b92044 | 2020-12-17 10:43:41 -0500 | [diff] [blame] | 562 | Inliner::InlineVariable Inliner::makeInlineVariable(const String& baseName, |
| 563 | const Type* type, |
| 564 | SymbolTable* symbolTable, |
| 565 | Modifiers modifiers, |
| 566 | bool isBuiltinCode, |
| 567 | std::unique_ptr<Expression>* initialValue) { |
| 568 | // $floatLiteral or $intLiteral aren't real types that we can use for scratch variables, so |
| 569 | // replace them if they ever appear here. If this happens, we likely forgot to coerce a type |
| 570 | // somewhere during compilation. |
John Stiles | 1497527 | 2021-01-12 11:41:14 -0500 | [diff] [blame] | 571 | if (type->isLiteral()) { |
| 572 | SkDEBUGFAIL("found a $literal type while inlining"); |
| 573 | type = &type->scalarTypeForLiteral(); |
John Stiles | 7b92044 | 2020-12-17 10:43:41 -0500 | [diff] [blame] | 574 | } |
| 575 | |
John Stiles | bff24ab | 2021-03-17 13:20:10 -0400 | [diff] [blame] | 576 | // Out parameters aren't supported. |
| 577 | SkASSERT(!(modifiers.fFlags & Modifiers::kOut_Flag)); |
| 578 | |
John Stiles | 7b92044 | 2020-12-17 10:43:41 -0500 | [diff] [blame] | 579 | // Provide our new variable with a unique name, and add it to our symbol table. |
John Stiles | d51c979 | 2021-03-18 11:40:14 -0400 | [diff] [blame] | 580 | const String* name = |
| 581 | symbolTable->takeOwnershipOfString(fMangler.uniqueName(baseName, symbolTable)); |
John Stiles | 7b92044 | 2020-12-17 10:43:41 -0500 | [diff] [blame] | 582 | |
| 583 | // Create our new variable and add it to the symbol table. |
| 584 | InlineVariable result; |
Ethan Nicholas | 5b9b0db | 2021-01-21 13:12:01 -0500 | [diff] [blame] | 585 | auto var = std::make_unique<Variable>(/*offset=*/-1, |
| 586 | fModifiers->addToPool(Modifiers()), |
John Stiles | d51c979 | 2021-03-18 11:40:14 -0400 | [diff] [blame] | 587 | name->c_str(), |
Ethan Nicholas | 5b9b0db | 2021-01-21 13:12:01 -0500 | [diff] [blame] | 588 | type, |
| 589 | isBuiltinCode, |
| 590 | Variable::Storage::kLocal); |
John Stiles | 7b92044 | 2020-12-17 10:43:41 -0500 | [diff] [blame] | 591 | |
John Stiles | bff24ab | 2021-03-17 13:20:10 -0400 | [diff] [blame] | 592 | // Create our variable declaration. |
John Stiles | e67bd13 | 2021-03-19 18:39:25 -0400 | [diff] [blame] | 593 | result.fVarDecl = VarDeclaration::Make(*fContext, var.get(), type, /*arraySize=*/0, |
| 594 | std::move(*initialValue)); |
Ethan Nicholas | 5b9b0db | 2021-01-21 13:12:01 -0500 | [diff] [blame] | 595 | result.fVarSymbol = symbolTable->add(std::move(var)); |
John Stiles | 7b92044 | 2020-12-17 10:43:41 -0500 | [diff] [blame] | 596 | return result; |
| 597 | } |
| 598 | |
John Stiles | 6eadf13 | 2020-09-08 10:16:10 -0400 | [diff] [blame] | 599 | Inliner::InlinedCall Inliner::inlineCall(FunctionCall* call, |
John Stiles | 7804758 | 2020-12-16 16:17:41 -0500 | [diff] [blame] | 600 | std::shared_ptr<SymbolTable> symbolTable, |
John Stiles | 30fce9c | 2021-03-18 09:24:06 -0400 | [diff] [blame] | 601 | const ProgramUsage& usage, |
Brian Osman | 3887a01 | 2020-09-30 13:22:27 -0400 | [diff] [blame] | 602 | const FunctionDeclaration* caller) { |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 603 | // Inlining is more complicated here than in a typical compiler, because we have to have a |
| 604 | // high-level IR and can't just drop statements into the middle of an expression or even use |
| 605 | // gotos. |
| 606 | // |
| 607 | // Since we can't insert statements into an expression, we run the inline function as extra |
| 608 | // statements before the statement we're currently processing, relying on a lack of execution |
| 609 | // order guarantees. Since we can't use gotos (which are normally used to replace return |
| 610 | // statements), we wrap the whole function in a loop and use break statements to jump to the |
| 611 | // end. |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 612 | SkASSERT(fContext); |
| 613 | SkASSERT(call); |
Ethan Nicholas | ed84b73 | 2020-10-08 11:45:44 -0400 | [diff] [blame] | 614 | SkASSERT(this->isSafeToInline(call->function().definition())); |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 615 | |
John Stiles | 8e3b6be | 2020-10-13 11:14:08 -0400 | [diff] [blame] | 616 | ExpressionArray& arguments = call->arguments(); |
John Stiles | 6eadf13 | 2020-09-08 10:16:10 -0400 | [diff] [blame] | 617 | const int offset = call->fOffset; |
Ethan Nicholas | ed84b73 | 2020-10-08 11:45:44 -0400 | [diff] [blame] | 618 | const FunctionDefinition& function = *call->function().definition(); |
John Stiles | 28257db | 2021-03-17 15:18:09 -0400 | [diff] [blame] | 619 | const Block& body = function.body()->as<Block>(); |
John Stiles | 77702f1 | 2020-12-17 14:38:56 -0500 | [diff] [blame] | 620 | const ReturnComplexity returnComplexity = GetReturnComplexity(function); |
John Stiles | 6eadf13 | 2020-09-08 10:16:10 -0400 | [diff] [blame] | 621 | |
John Stiles | 28257db | 2021-03-17 15:18:09 -0400 | [diff] [blame] | 622 | StatementArray inlineStatements; |
| 623 | int expectedStmtCount = 1 + // Inline marker |
| 624 | 1 + // Result variable |
| 625 | arguments.size() + // Function argument temp-vars |
| 626 | body.children().size(); // Inlined code |
John Stiles | 98c1f82 | 2020-09-09 14:18:53 -0400 | [diff] [blame] | 627 | |
John Stiles | 28257db | 2021-03-17 15:18:09 -0400 | [diff] [blame] | 628 | inlineStatements.reserve_back(expectedStmtCount); |
| 629 | inlineStatements.push_back(InlineMarker::Make(&call->function())); |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 630 | |
John Stiles | e41b4ee | 2020-09-28 12:28:16 -0400 | [diff] [blame] | 631 | std::unique_ptr<Expression> resultExpr; |
John Stiles | 511c500 | 2021-02-25 11:17:02 -0500 | [diff] [blame] | 632 | if (returnComplexity > ReturnComplexity::kSingleSafeReturn && |
John Stiles | 2558c46 | 2021-03-16 17:49:20 -0400 | [diff] [blame] | 633 | !function.declaration().returnType().isVoid()) { |
John Stiles | 511c500 | 2021-02-25 11:17:02 -0500 | [diff] [blame] | 634 | // Create a variable to hold the result in the extra statements. We don't need to do this |
| 635 | // for void-return functions, or in cases that are simple enough that we can just replace |
| 636 | // the function-call node with the result expression. |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 637 | std::unique_ptr<Expression> noInitialValue; |
John Stiles | 7b92044 | 2020-12-17 10:43:41 -0500 | [diff] [blame] | 638 | InlineVariable var = this->makeInlineVariable(function.declaration().name(), |
| 639 | &function.declaration().returnType(), |
| 640 | symbolTable.get(), Modifiers{}, |
| 641 | caller->isBuiltin(), &noInitialValue); |
John Stiles | 28257db | 2021-03-17 15:18:09 -0400 | [diff] [blame] | 642 | inlineStatements.push_back(std::move(var.fVarDecl)); |
John Stiles | 7b92044 | 2020-12-17 10:43:41 -0500 | [diff] [blame] | 643 | resultExpr = std::make_unique<VariableReference>(/*offset=*/-1, var.fVarSymbol); |
John Stiles | 511c500 | 2021-02-25 11:17:02 -0500 | [diff] [blame] | 644 | } |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 645 | |
| 646 | // Create variables in the extra statements to hold the arguments, and assign the arguments to |
| 647 | // them. |
| 648 | VariableRewriteMap varMap; |
John Stiles | bff24ab | 2021-03-17 13:20:10 -0400 | [diff] [blame] | 649 | for (int i = 0; i < arguments.count(); ++i) { |
John Stiles | 049f0df | 2021-03-19 09:39:44 -0400 | [diff] [blame] | 650 | // If the parameter isn't written to within the inline function ... |
John Stiles | bff24ab | 2021-03-17 13:20:10 -0400 | [diff] [blame] | 651 | const Variable* param = function.declaration().parameters()[i]; |
John Stiles | 049f0df | 2021-03-19 09:39:44 -0400 | [diff] [blame] | 652 | const ProgramUsage::VariableCounts& paramUsage = usage.get(*param); |
| 653 | if (!paramUsage.fWrite) { |
| 654 | // ... and can be inlined trivially (e.g. a swizzle, or a constant array index), |
| 655 | // or any expression without side effects that is only accessed at most once... |
| 656 | if ((paramUsage.fRead > 1) ? Analysis::IsTrivialExpression(*arguments[i]) |
| 657 | : !arguments[i]->hasSideEffects()) { |
John Stiles | f201af8 | 2020-09-29 16:57:55 -0400 | [diff] [blame] | 658 | // ... we don't need to copy it at all! We can just use the existing expression. |
| 659 | varMap[param] = arguments[i]->clone(); |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 660 | continue; |
| 661 | } |
| 662 | } |
John Stiles | 7b92044 | 2020-12-17 10:43:41 -0500 | [diff] [blame] | 663 | InlineVariable var = this->makeInlineVariable(param->name(), &arguments[i]->type(), |
| 664 | symbolTable.get(), param->modifiers(), |
| 665 | caller->isBuiltin(), &arguments[i]); |
John Stiles | 28257db | 2021-03-17 15:18:09 -0400 | [diff] [blame] | 666 | inlineStatements.push_back(std::move(var.fVarDecl)); |
John Stiles | 7b92044 | 2020-12-17 10:43:41 -0500 | [diff] [blame] | 667 | varMap[param] = std::make_unique<VariableReference>(/*offset=*/-1, var.fVarSymbol); |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 668 | } |
| 669 | |
John Stiles | 7b92044 | 2020-12-17 10:43:41 -0500 | [diff] [blame] | 670 | for (const std::unique_ptr<Statement>& stmt : body.children()) { |
John Stiles | 28257db | 2021-03-17 15:18:09 -0400 | [diff] [blame] | 671 | inlineStatements.push_back(this->inlineStatement(offset, &varMap, symbolTable.get(), |
| 672 | &resultExpr, returnComplexity, *stmt, |
| 673 | caller->isBuiltin())); |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 674 | } |
| 675 | |
John Stiles | 28257db | 2021-03-17 15:18:09 -0400 | [diff] [blame] | 676 | SkASSERT(inlineStatements.count() <= expectedStmtCount); |
| 677 | |
John Stiles | bf16b6c | 2021-03-12 19:24:31 -0500 | [diff] [blame] | 678 | // Wrap all of the generated statements in a block. We need a real Block here, so we can't use |
| 679 | // MakeUnscoped. This is because we need to add another child statement to the Block later. |
John Stiles | 28257db | 2021-03-17 15:18:09 -0400 | [diff] [blame] | 680 | InlinedCall inlinedCall; |
| 681 | inlinedCall.fInlinedBody = Block::Make(offset, std::move(inlineStatements), |
John Stiles | bf16b6c | 2021-03-12 19:24:31 -0500 | [diff] [blame] | 682 | /*symbols=*/nullptr, /*isScope=*/false); |
| 683 | |
John Stiles | 0c2d14a | 2021-03-01 10:08:08 -0500 | [diff] [blame] | 684 | if (resultExpr) { |
| 685 | // Return our result expression as-is. |
John Stiles | e41b4ee | 2020-09-28 12:28:16 -0400 | [diff] [blame] | 686 | inlinedCall.fReplacementExpr = std::move(resultExpr); |
John Stiles | 2558c46 | 2021-03-16 17:49:20 -0400 | [diff] [blame] | 687 | } else if (function.declaration().returnType().isVoid()) { |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 688 | // It's a void function, so it doesn't actually result in anything, but we have to return |
| 689 | // something non-null as a standin. |
John Stiles | 9ce80f7 | 2021-03-11 22:35:19 -0500 | [diff] [blame] | 690 | inlinedCall.fReplacementExpr = BoolLiteral::Make(*fContext, offset, /*value=*/false); |
John Stiles | 0c2d14a | 2021-03-01 10:08:08 -0500 | [diff] [blame] | 691 | } else { |
| 692 | // It's a non-void function, but it never created a result expression--that is, it never |
John Stiles | 2dda50d | 2021-03-03 10:46:44 -0500 | [diff] [blame] | 693 | // returned anything on any path! This should have been detected in the function finalizer. |
| 694 | // Still, discard our output and generate an error. |
| 695 | SkDEBUGFAIL("inliner found non-void function that fails to return a value on any path"); |
| 696 | fContext->fErrors.error(function.fOffset, "inliner found non-void function '" + |
John Stiles | 0c2d14a | 2021-03-01 10:08:08 -0500 | [diff] [blame] | 697 | function.declaration().name() + |
John Stiles | 2dda50d | 2021-03-03 10:46:44 -0500 | [diff] [blame] | 698 | "' that fails to return a value on any path"); |
John Stiles | 0c2d14a | 2021-03-01 10:08:08 -0500 | [diff] [blame] | 699 | inlinedCall = {}; |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 700 | } |
| 701 | |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 702 | return inlinedCall; |
| 703 | } |
| 704 | |
John Stiles | 2d7973a | 2020-10-02 15:01:03 -0400 | [diff] [blame] | 705 | bool Inliner::isSafeToInline(const FunctionDefinition* functionDef) { |
John Stiles | 1c03d33 | 2020-10-13 10:30:23 -0400 | [diff] [blame] | 706 | // A threshold of zero indicates that the inliner is completely disabled, so we can just return. |
John Stiles | d120464 | 2021-02-17 16:30:02 -0500 | [diff] [blame] | 707 | if (this->settings().fInlineThreshold <= 0) { |
John Stiles | 1c03d33 | 2020-10-13 10:30:23 -0400 | [diff] [blame] | 708 | return false; |
| 709 | } |
| 710 | |
John Stiles | 031a767 | 2020-11-13 16:13:18 -0500 | [diff] [blame] | 711 | // Enforce a limit on inlining to avoid pathological cases. (inliner/ExponentialGrowth.sksl) |
| 712 | if (fInlinedStatementCounter >= kInlinedStatementLimit) { |
| 713 | return false; |
| 714 | } |
| 715 | |
John Stiles | 2d7973a | 2020-10-02 15:01:03 -0400 | [diff] [blame] | 716 | if (functionDef == nullptr) { |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 717 | // Can't inline something if we don't actually have its definition. |
| 718 | return false; |
| 719 | } |
John Stiles | 2d7973a | 2020-10-02 15:01:03 -0400 | [diff] [blame] | 720 | |
John Stiles | 0dd1a77 | 2021-03-09 22:14:27 -0500 | [diff] [blame] | 721 | if (functionDef->declaration().modifiers().fFlags & Modifiers::kNoInline_Flag) { |
| 722 | // Refuse to inline functions decorated with `noinline`. |
| 723 | return false; |
| 724 | } |
| 725 | |
John Stiles | bff24ab | 2021-03-17 13:20:10 -0400 | [diff] [blame] | 726 | // We don't allow inlining a function with out parameters. (See skia:11326 for rationale.) |
| 727 | for (const Variable* param : functionDef->declaration().parameters()) { |
| 728 | if (param->modifiers().fFlags & Modifiers::Flag::kOut_Flag) { |
| 729 | return false; |
| 730 | } |
| 731 | } |
| 732 | |
John Stiles | dc20847 | 2021-03-17 10:58:16 -0400 | [diff] [blame] | 733 | // We don't have a mechanism to simulate early returns, so we can't inline if there is one. |
| 734 | return GetReturnComplexity(*functionDef) < ReturnComplexity::kEarlyReturns; |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 735 | } |
| 736 | |
John Stiles | 2d7973a | 2020-10-02 15:01:03 -0400 | [diff] [blame] | 737 | // A candidate function for inlining, containing everything that `inlineCall` needs. |
| 738 | struct InlineCandidate { |
John Stiles | 7804758 | 2020-12-16 16:17:41 -0500 | [diff] [blame] | 739 | std::shared_ptr<SymbolTable> fSymbols; // the SymbolTable of the candidate |
John Stiles | 2d7973a | 2020-10-02 15:01:03 -0400 | [diff] [blame] | 740 | std::unique_ptr<Statement>* fParentStmt; // the parent Statement of the enclosing stmt |
| 741 | std::unique_ptr<Statement>* fEnclosingStmt; // the Statement containing the candidate |
| 742 | std::unique_ptr<Expression>* fCandidateExpr; // the candidate FunctionCall to be inlined |
| 743 | FunctionDefinition* fEnclosingFunction; // the Function containing the candidate |
John Stiles | 2d7973a | 2020-10-02 15:01:03 -0400 | [diff] [blame] | 744 | }; |
John Stiles | 9344262 | 2020-09-11 12:11:27 -0400 | [diff] [blame] | 745 | |
John Stiles | 2d7973a | 2020-10-02 15:01:03 -0400 | [diff] [blame] | 746 | struct InlineCandidateList { |
| 747 | std::vector<InlineCandidate> fCandidates; |
| 748 | }; |
| 749 | |
| 750 | class InlineCandidateAnalyzer { |
John Stiles | 70957c8 | 2020-10-02 16:42:10 -0400 | [diff] [blame] | 751 | public: |
| 752 | // A list of all the inlining candidates we found during analysis. |
| 753 | InlineCandidateList* fCandidateList; |
John Stiles | 2d7973a | 2020-10-02 15:01:03 -0400 | [diff] [blame] | 754 | |
John Stiles | 70957c8 | 2020-10-02 16:42:10 -0400 | [diff] [blame] | 755 | // A stack of the symbol tables; since most nodes don't have one, expected to be shallower than |
| 756 | // the enclosing-statement stack. |
John Stiles | 7804758 | 2020-12-16 16:17:41 -0500 | [diff] [blame] | 757 | std::vector<std::shared_ptr<SymbolTable>> fSymbolTableStack; |
John Stiles | 70957c8 | 2020-10-02 16:42:10 -0400 | [diff] [blame] | 758 | // A stack of "enclosing" statements--these would be suitable for the inliner to use for adding |
| 759 | // new instructions. Not all statements are suitable (e.g. a for-loop's initializer). The |
| 760 | // inliner might replace a statement with a block containing the statement. |
| 761 | std::vector<std::unique_ptr<Statement>*> fEnclosingStmtStack; |
| 762 | // The function that we're currently processing (i.e. inlining into). |
| 763 | FunctionDefinition* fEnclosingFunction = nullptr; |
John Stiles | 9344262 | 2020-09-11 12:11:27 -0400 | [diff] [blame] | 764 | |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 765 | void visit(const std::vector<std::unique_ptr<ProgramElement>>& elements, |
John Stiles | 7804758 | 2020-12-16 16:17:41 -0500 | [diff] [blame] | 766 | std::shared_ptr<SymbolTable> symbols, |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 767 | InlineCandidateList* candidateList) { |
John Stiles | 70957c8 | 2020-10-02 16:42:10 -0400 | [diff] [blame] | 768 | fCandidateList = candidateList; |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 769 | fSymbolTableStack.push_back(symbols); |
John Stiles | 9344262 | 2020-09-11 12:11:27 -0400 | [diff] [blame] | 770 | |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 771 | for (const std::unique_ptr<ProgramElement>& pe : elements) { |
Brian Osman | 1179fcf | 2020-10-08 16:04:40 -0400 | [diff] [blame] | 772 | this->visitProgramElement(pe.get()); |
John Stiles | 9344262 | 2020-09-11 12:11:27 -0400 | [diff] [blame] | 773 | } |
| 774 | |
John Stiles | 70957c8 | 2020-10-02 16:42:10 -0400 | [diff] [blame] | 775 | fSymbolTableStack.pop_back(); |
| 776 | fCandidateList = nullptr; |
| 777 | } |
| 778 | |
| 779 | void visitProgramElement(ProgramElement* pe) { |
| 780 | switch (pe->kind()) { |
| 781 | case ProgramElement::Kind::kFunction: { |
| 782 | FunctionDefinition& funcDef = pe->as<FunctionDefinition>(); |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 783 | fEnclosingFunction = &funcDef; |
| 784 | this->visitStatement(&funcDef.body()); |
John Stiles | 70957c8 | 2020-10-02 16:42:10 -0400 | [diff] [blame] | 785 | break; |
John Stiles | 9344262 | 2020-09-11 12:11:27 -0400 | [diff] [blame] | 786 | } |
John Stiles | 70957c8 | 2020-10-02 16:42:10 -0400 | [diff] [blame] | 787 | default: |
| 788 | // The inliner can't operate outside of a function's scope. |
| 789 | break; |
| 790 | } |
| 791 | } |
| 792 | |
| 793 | void visitStatement(std::unique_ptr<Statement>* stmt, |
| 794 | bool isViableAsEnclosingStatement = true) { |
| 795 | if (!*stmt) { |
| 796 | return; |
John Stiles | 9344262 | 2020-09-11 12:11:27 -0400 | [diff] [blame] | 797 | } |
| 798 | |
John Stiles | 70957c8 | 2020-10-02 16:42:10 -0400 | [diff] [blame] | 799 | size_t oldEnclosingStmtStackSize = fEnclosingStmtStack.size(); |
| 800 | size_t oldSymbolStackSize = fSymbolTableStack.size(); |
John Stiles | 9344262 | 2020-09-11 12:11:27 -0400 | [diff] [blame] | 801 | |
John Stiles | 70957c8 | 2020-10-02 16:42:10 -0400 | [diff] [blame] | 802 | if (isViableAsEnclosingStatement) { |
| 803 | fEnclosingStmtStack.push_back(stmt); |
John Stiles | 9344262 | 2020-09-11 12:11:27 -0400 | [diff] [blame] | 804 | } |
| 805 | |
John Stiles | 70957c8 | 2020-10-02 16:42:10 -0400 | [diff] [blame] | 806 | switch ((*stmt)->kind()) { |
| 807 | case Statement::Kind::kBreak: |
| 808 | case Statement::Kind::kContinue: |
| 809 | case Statement::Kind::kDiscard: |
| 810 | case Statement::Kind::kInlineMarker: |
| 811 | case Statement::Kind::kNop: |
| 812 | break; |
| 813 | |
| 814 | case Statement::Kind::kBlock: { |
| 815 | Block& block = (*stmt)->as<Block>(); |
| 816 | if (block.symbolTable()) { |
John Stiles | 7804758 | 2020-12-16 16:17:41 -0500 | [diff] [blame] | 817 | fSymbolTableStack.push_back(block.symbolTable()); |
John Stiles | 70957c8 | 2020-10-02 16:42:10 -0400 | [diff] [blame] | 818 | } |
| 819 | |
| 820 | for (std::unique_ptr<Statement>& stmt : block.children()) { |
| 821 | this->visitStatement(&stmt); |
| 822 | } |
| 823 | break; |
John Stiles | 9344262 | 2020-09-11 12:11:27 -0400 | [diff] [blame] | 824 | } |
John Stiles | 70957c8 | 2020-10-02 16:42:10 -0400 | [diff] [blame] | 825 | case Statement::Kind::kDo: { |
| 826 | DoStatement& doStmt = (*stmt)->as<DoStatement>(); |
| 827 | // The loop body is a candidate for inlining. |
| 828 | this->visitStatement(&doStmt.statement()); |
| 829 | // The inliner isn't smart enough to inline the test-expression for a do-while |
| 830 | // loop at this time. There are two limitations: |
| 831 | // - We would need to insert the inlined-body block at the very end of the do- |
| 832 | // statement's inner fStatement. We don't support that today, but it's doable. |
| 833 | // - We cannot inline the test expression if the loop uses `continue` anywhere; that |
| 834 | // would skip over the inlined block that evaluates the test expression. There |
| 835 | // isn't a good fix for this--any workaround would be more complex than the cost |
| 836 | // of a function call. However, loops that don't use `continue` would still be |
| 837 | // viable candidates for inlining. |
| 838 | break; |
John Stiles | 9344262 | 2020-09-11 12:11:27 -0400 | [diff] [blame] | 839 | } |
John Stiles | 70957c8 | 2020-10-02 16:42:10 -0400 | [diff] [blame] | 840 | case Statement::Kind::kExpression: { |
| 841 | ExpressionStatement& expr = (*stmt)->as<ExpressionStatement>(); |
| 842 | this->visitExpression(&expr.expression()); |
| 843 | break; |
| 844 | } |
| 845 | case Statement::Kind::kFor: { |
| 846 | ForStatement& forStmt = (*stmt)->as<ForStatement>(); |
Ethan Nicholas | 0d31ed5 | 2020-10-05 14:47:09 -0400 | [diff] [blame] | 847 | if (forStmt.symbols()) { |
John Stiles | 7804758 | 2020-12-16 16:17:41 -0500 | [diff] [blame] | 848 | fSymbolTableStack.push_back(forStmt.symbols()); |
John Stiles | 70957c8 | 2020-10-02 16:42:10 -0400 | [diff] [blame] | 849 | } |
| 850 | |
| 851 | // The initializer and loop body are candidates for inlining. |
Ethan Nicholas | 0d31ed5 | 2020-10-05 14:47:09 -0400 | [diff] [blame] | 852 | this->visitStatement(&forStmt.initializer(), |
John Stiles | 70957c8 | 2020-10-02 16:42:10 -0400 | [diff] [blame] | 853 | /*isViableAsEnclosingStatement=*/false); |
Ethan Nicholas | 0d31ed5 | 2020-10-05 14:47:09 -0400 | [diff] [blame] | 854 | this->visitStatement(&forStmt.statement()); |
John Stiles | 70957c8 | 2020-10-02 16:42:10 -0400 | [diff] [blame] | 855 | |
| 856 | // The inliner isn't smart enough to inline the test- or increment-expressions |
| 857 | // of a for loop loop at this time. There are a handful of limitations: |
| 858 | // - We would need to insert the test-expression block at the very beginning of the |
| 859 | // for-loop's inner fStatement, and the increment-expression block at the very |
| 860 | // end. We don't support that today, but it's doable. |
| 861 | // - The for-loop's built-in test-expression would need to be dropped entirely, |
| 862 | // and the loop would be halted via a break statement at the end of the inlined |
| 863 | // test-expression. This is again something we don't support today, but it could |
| 864 | // be implemented. |
| 865 | // - We cannot inline the increment-expression if the loop uses `continue` anywhere; |
| 866 | // that would skip over the inlined block that evaluates the increment expression. |
| 867 | // There isn't a good fix for this--any workaround would be more complex than the |
| 868 | // cost of a function call. However, loops that don't use `continue` would still |
| 869 | // be viable candidates for increment-expression inlining. |
| 870 | break; |
| 871 | } |
| 872 | case Statement::Kind::kIf: { |
| 873 | IfStatement& ifStmt = (*stmt)->as<IfStatement>(); |
Ethan Nicholas | 8c44eca | 2020-10-07 16:47:09 -0400 | [diff] [blame] | 874 | this->visitExpression(&ifStmt.test()); |
| 875 | this->visitStatement(&ifStmt.ifTrue()); |
| 876 | this->visitStatement(&ifStmt.ifFalse()); |
John Stiles | 70957c8 | 2020-10-02 16:42:10 -0400 | [diff] [blame] | 877 | break; |
| 878 | } |
| 879 | case Statement::Kind::kReturn: { |
| 880 | ReturnStatement& returnStmt = (*stmt)->as<ReturnStatement>(); |
Ethan Nicholas | 2a4952d | 2020-10-08 15:35:56 -0400 | [diff] [blame] | 881 | this->visitExpression(&returnStmt.expression()); |
John Stiles | 70957c8 | 2020-10-02 16:42:10 -0400 | [diff] [blame] | 882 | break; |
| 883 | } |
| 884 | case Statement::Kind::kSwitch: { |
| 885 | SwitchStatement& switchStmt = (*stmt)->as<SwitchStatement>(); |
Ethan Nicholas | 01b05e5 | 2020-10-22 15:53:41 -0400 | [diff] [blame] | 886 | if (switchStmt.symbols()) { |
John Stiles | 7804758 | 2020-12-16 16:17:41 -0500 | [diff] [blame] | 887 | fSymbolTableStack.push_back(switchStmt.symbols()); |
John Stiles | 70957c8 | 2020-10-02 16:42:10 -0400 | [diff] [blame] | 888 | } |
| 889 | |
Ethan Nicholas | 01b05e5 | 2020-10-22 15:53:41 -0400 | [diff] [blame] | 890 | this->visitExpression(&switchStmt.value()); |
John Stiles | b23a64b | 2021-03-11 08:27:59 -0500 | [diff] [blame] | 891 | for (const std::unique_ptr<Statement>& switchCase : switchStmt.cases()) { |
John Stiles | 70957c8 | 2020-10-02 16:42:10 -0400 | [diff] [blame] | 892 | // The switch-case's fValue cannot be a FunctionCall; skip it. |
John Stiles | b23a64b | 2021-03-11 08:27:59 -0500 | [diff] [blame] | 893 | this->visitStatement(&switchCase->as<SwitchCase>().statement()); |
John Stiles | 70957c8 | 2020-10-02 16:42:10 -0400 | [diff] [blame] | 894 | } |
| 895 | break; |
| 896 | } |
| 897 | case Statement::Kind::kVarDeclaration: { |
| 898 | VarDeclaration& varDeclStmt = (*stmt)->as<VarDeclaration>(); |
| 899 | // Don't need to scan the declaration's sizes; those are always IntLiterals. |
Ethan Nicholas | c51f33e | 2020-10-13 13:49:44 -0400 | [diff] [blame] | 900 | this->visitExpression(&varDeclStmt.value()); |
John Stiles | 70957c8 | 2020-10-02 16:42:10 -0400 | [diff] [blame] | 901 | break; |
| 902 | } |
John Stiles | 70957c8 | 2020-10-02 16:42:10 -0400 | [diff] [blame] | 903 | default: |
| 904 | SkUNREACHABLE; |
John Stiles | 9344262 | 2020-09-11 12:11:27 -0400 | [diff] [blame] | 905 | } |
| 906 | |
John Stiles | 70957c8 | 2020-10-02 16:42:10 -0400 | [diff] [blame] | 907 | // Pop our symbol and enclosing-statement stacks. |
| 908 | fSymbolTableStack.resize(oldSymbolStackSize); |
| 909 | fEnclosingStmtStack.resize(oldEnclosingStmtStackSize); |
| 910 | } |
| 911 | |
| 912 | void visitExpression(std::unique_ptr<Expression>* expr) { |
| 913 | if (!*expr) { |
| 914 | return; |
John Stiles | 9344262 | 2020-09-11 12:11:27 -0400 | [diff] [blame] | 915 | } |
John Stiles | 70957c8 | 2020-10-02 16:42:10 -0400 | [diff] [blame] | 916 | |
| 917 | switch ((*expr)->kind()) { |
| 918 | case Expression::Kind::kBoolLiteral: |
| 919 | case Expression::Kind::kDefined: |
Brian Osman | be0b3b7 | 2021-01-06 14:27:35 -0500 | [diff] [blame] | 920 | case Expression::Kind::kExternalFunctionReference: |
John Stiles | 70957c8 | 2020-10-02 16:42:10 -0400 | [diff] [blame] | 921 | case Expression::Kind::kFieldAccess: |
| 922 | case Expression::Kind::kFloatLiteral: |
| 923 | case Expression::Kind::kFunctionReference: |
| 924 | case Expression::Kind::kIntLiteral: |
John Stiles | 70957c8 | 2020-10-02 16:42:10 -0400 | [diff] [blame] | 925 | case Expression::Kind::kSetting: |
| 926 | case Expression::Kind::kTypeReference: |
| 927 | case Expression::Kind::kVariableReference: |
| 928 | // Nothing to scan here. |
| 929 | break; |
| 930 | |
| 931 | case Expression::Kind::kBinary: { |
| 932 | BinaryExpression& binaryExpr = (*expr)->as<BinaryExpression>(); |
John Stiles | 2d4f959 | 2020-10-30 10:29:12 -0400 | [diff] [blame] | 933 | this->visitExpression(&binaryExpr.left()); |
John Stiles | 70957c8 | 2020-10-02 16:42:10 -0400 | [diff] [blame] | 934 | |
| 935 | // Logical-and and logical-or binary expressions do not inline the right side, |
| 936 | // because that would invalidate short-circuiting. That is, when evaluating |
| 937 | // expressions like these: |
| 938 | // (false && x()) // always false |
| 939 | // (true || y()) // always true |
| 940 | // It is illegal for side-effects from x() or y() to occur. The simplest way to |
| 941 | // enforce that rule is to avoid inlining the right side entirely. However, it is |
| 942 | // safe for other types of binary expression to inline both sides. |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 943 | Operator op = binaryExpr.getOperator(); |
| 944 | bool shortCircuitable = (op.kind() == Token::Kind::TK_LOGICALAND || |
| 945 | op.kind() == Token::Kind::TK_LOGICALOR); |
John Stiles | 70957c8 | 2020-10-02 16:42:10 -0400 | [diff] [blame] | 946 | if (!shortCircuitable) { |
John Stiles | 2d4f959 | 2020-10-30 10:29:12 -0400 | [diff] [blame] | 947 | this->visitExpression(&binaryExpr.right()); |
John Stiles | 70957c8 | 2020-10-02 16:42:10 -0400 | [diff] [blame] | 948 | } |
| 949 | break; |
| 950 | } |
John Stiles | 7384b37 | 2021-04-01 13:48:15 -0400 | [diff] [blame] | 951 | case Expression::Kind::kConstructor: |
| 952 | case Expression::Kind::kConstructorArray: |
John Stiles | 2938eea | 2021-04-01 18:58:25 -0400 | [diff] [blame] | 953 | case Expression::Kind::kConstructorDiagonalMatrix: |
John Stiles | 5abb9e1 | 2021-04-06 13:47:19 -0400 | [diff] [blame^] | 954 | case Expression::Kind::kConstructorMatrixResize: |
John Stiles | fd7252f | 2021-04-04 22:24:40 -0400 | [diff] [blame] | 955 | case Expression::Kind::kConstructorScalarCast: |
John Stiles | b14a819 | 2021-04-05 11:40:46 -0400 | [diff] [blame] | 956 | case Expression::Kind::kConstructorSplat: |
| 957 | case Expression::Kind::kConstructorVectorCast: { |
John Stiles | 7384b37 | 2021-04-01 13:48:15 -0400 | [diff] [blame] | 958 | AnyConstructor& constructorExpr = (*expr)->asAnyConstructor(); |
| 959 | for (std::unique_ptr<Expression>& arg : constructorExpr.argumentSpan()) { |
John Stiles | 70957c8 | 2020-10-02 16:42:10 -0400 | [diff] [blame] | 960 | this->visitExpression(&arg); |
| 961 | } |
| 962 | break; |
| 963 | } |
| 964 | case Expression::Kind::kExternalFunctionCall: { |
| 965 | ExternalFunctionCall& funcCallExpr = (*expr)->as<ExternalFunctionCall>(); |
| 966 | for (std::unique_ptr<Expression>& arg : funcCallExpr.arguments()) { |
| 967 | this->visitExpression(&arg); |
| 968 | } |
| 969 | break; |
| 970 | } |
| 971 | case Expression::Kind::kFunctionCall: { |
| 972 | FunctionCall& funcCallExpr = (*expr)->as<FunctionCall>(); |
Ethan Nicholas | 0dec992 | 2020-10-05 15:51:52 -0400 | [diff] [blame] | 973 | for (std::unique_ptr<Expression>& arg : funcCallExpr.arguments()) { |
John Stiles | 70957c8 | 2020-10-02 16:42:10 -0400 | [diff] [blame] | 974 | this->visitExpression(&arg); |
| 975 | } |
| 976 | this->addInlineCandidate(expr); |
| 977 | break; |
| 978 | } |
John Stiles | 708faba | 2021-03-19 09:43:23 -0400 | [diff] [blame] | 979 | case Expression::Kind::kIndex: { |
John Stiles | 70957c8 | 2020-10-02 16:42:10 -0400 | [diff] [blame] | 980 | IndexExpression& indexExpr = (*expr)->as<IndexExpression>(); |
Ethan Nicholas | 2a4952d | 2020-10-08 15:35:56 -0400 | [diff] [blame] | 981 | this->visitExpression(&indexExpr.base()); |
| 982 | this->visitExpression(&indexExpr.index()); |
John Stiles | 70957c8 | 2020-10-02 16:42:10 -0400 | [diff] [blame] | 983 | break; |
| 984 | } |
| 985 | case Expression::Kind::kPostfix: { |
| 986 | PostfixExpression& postfixExpr = (*expr)->as<PostfixExpression>(); |
Ethan Nicholas | 444ccc6 | 2020-10-09 10:16:22 -0400 | [diff] [blame] | 987 | this->visitExpression(&postfixExpr.operand()); |
John Stiles | 70957c8 | 2020-10-02 16:42:10 -0400 | [diff] [blame] | 988 | break; |
| 989 | } |
| 990 | case Expression::Kind::kPrefix: { |
| 991 | PrefixExpression& prefixExpr = (*expr)->as<PrefixExpression>(); |
Ethan Nicholas | 444ccc6 | 2020-10-09 10:16:22 -0400 | [diff] [blame] | 992 | this->visitExpression(&prefixExpr.operand()); |
John Stiles | 70957c8 | 2020-10-02 16:42:10 -0400 | [diff] [blame] | 993 | break; |
| 994 | } |
| 995 | case Expression::Kind::kSwizzle: { |
| 996 | Swizzle& swizzleExpr = (*expr)->as<Swizzle>(); |
Ethan Nicholas | 6b4d581 | 2020-10-12 16:11:51 -0400 | [diff] [blame] | 997 | this->visitExpression(&swizzleExpr.base()); |
John Stiles | 70957c8 | 2020-10-02 16:42:10 -0400 | [diff] [blame] | 998 | break; |
| 999 | } |
| 1000 | case Expression::Kind::kTernary: { |
| 1001 | TernaryExpression& ternaryExpr = (*expr)->as<TernaryExpression>(); |
| 1002 | // The test expression is a candidate for inlining. |
Ethan Nicholas | dd21816 | 2020-10-08 05:48:01 -0400 | [diff] [blame] | 1003 | this->visitExpression(&ternaryExpr.test()); |
John Stiles | 70957c8 | 2020-10-02 16:42:10 -0400 | [diff] [blame] | 1004 | // The true- and false-expressions cannot be inlined, because we are only allowed to |
| 1005 | // evaluate one side. |
| 1006 | break; |
| 1007 | } |
| 1008 | default: |
| 1009 | SkUNREACHABLE; |
| 1010 | } |
| 1011 | } |
| 1012 | |
| 1013 | void addInlineCandidate(std::unique_ptr<Expression>* candidate) { |
| 1014 | fCandidateList->fCandidates.push_back( |
| 1015 | InlineCandidate{fSymbolTableStack.back(), |
| 1016 | find_parent_statement(fEnclosingStmtStack), |
| 1017 | fEnclosingStmtStack.back(), |
| 1018 | candidate, |
John Stiles | 9b9415e | 2020-11-23 14:48:06 -0500 | [diff] [blame] | 1019 | fEnclosingFunction}); |
John Stiles | 70957c8 | 2020-10-02 16:42:10 -0400 | [diff] [blame] | 1020 | } |
John Stiles | 2d7973a | 2020-10-02 15:01:03 -0400 | [diff] [blame] | 1021 | }; |
John Stiles | 9344262 | 2020-09-11 12:11:27 -0400 | [diff] [blame] | 1022 | |
John Stiles | 9b9415e | 2020-11-23 14:48:06 -0500 | [diff] [blame] | 1023 | static const FunctionDeclaration& candidate_func(const InlineCandidate& candidate) { |
| 1024 | return (*candidate.fCandidateExpr)->as<FunctionCall>().function(); |
| 1025 | } |
John Stiles | 915a38c | 2020-09-14 09:38:13 -0400 | [diff] [blame] | 1026 | |
John Stiles | 9b9415e | 2020-11-23 14:48:06 -0500 | [diff] [blame] | 1027 | bool Inliner::candidateCanBeInlined(const InlineCandidate& candidate, InlinabilityCache* cache) { |
| 1028 | const FunctionDeclaration& funcDecl = candidate_func(candidate); |
John Stiles | 1c03d33 | 2020-10-13 10:30:23 -0400 | [diff] [blame] | 1029 | auto [iter, wasInserted] = cache->insert({&funcDecl, false}); |
John Stiles | 2d7973a | 2020-10-02 15:01:03 -0400 | [diff] [blame] | 1030 | if (wasInserted) { |
| 1031 | // Recursion is forbidden here to avoid an infinite death spiral of inlining. |
John Stiles | 132cfdd | 2021-03-15 22:08:38 +0000 | [diff] [blame] | 1032 | iter->second = this->isSafeToInline(funcDecl.definition()) && |
| 1033 | !contains_recursive_call(funcDecl); |
John Stiles | 9344262 | 2020-09-11 12:11:27 -0400 | [diff] [blame] | 1034 | } |
| 1035 | |
John Stiles | 2d7973a | 2020-10-02 15:01:03 -0400 | [diff] [blame] | 1036 | return iter->second; |
| 1037 | } |
| 1038 | |
John Stiles | 9b9415e | 2020-11-23 14:48:06 -0500 | [diff] [blame] | 1039 | int Inliner::getFunctionSize(const FunctionDeclaration& funcDecl, FunctionSizeCache* cache) { |
| 1040 | auto [iter, wasInserted] = cache->insert({&funcDecl, 0}); |
John Stiles | 2d7973a | 2020-10-02 15:01:03 -0400 | [diff] [blame] | 1041 | if (wasInserted) { |
John Stiles | 9b9415e | 2020-11-23 14:48:06 -0500 | [diff] [blame] | 1042 | iter->second = Analysis::NodeCountUpToLimit(*funcDecl.definition(), |
John Stiles | d120464 | 2021-02-17 16:30:02 -0500 | [diff] [blame] | 1043 | this->settings().fInlineThreshold); |
John Stiles | 2d7973a | 2020-10-02 15:01:03 -0400 | [diff] [blame] | 1044 | } |
John Stiles | 2d7973a | 2020-10-02 15:01:03 -0400 | [diff] [blame] | 1045 | return iter->second; |
| 1046 | } |
| 1047 | |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 1048 | void Inliner::buildCandidateList(const std::vector<std::unique_ptr<ProgramElement>>& elements, |
John Stiles | 7804758 | 2020-12-16 16:17:41 -0500 | [diff] [blame] | 1049 | std::shared_ptr<SymbolTable> symbols, ProgramUsage* usage, |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 1050 | InlineCandidateList* candidateList) { |
John Stiles | 2d7973a | 2020-10-02 15:01:03 -0400 | [diff] [blame] | 1051 | // This is structured much like a ProgramVisitor, but does not actually use ProgramVisitor. |
| 1052 | // The analyzer needs to keep track of the `unique_ptr<T>*` of statements and expressions so |
| 1053 | // that they can later be replaced, and ProgramVisitor does not provide this; it only provides a |
| 1054 | // `const T&`. |
| 1055 | InlineCandidateAnalyzer analyzer; |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 1056 | analyzer.visit(elements, symbols, candidateList); |
John Stiles | 2d7973a | 2020-10-02 15:01:03 -0400 | [diff] [blame] | 1057 | |
John Stiles | 0ad233f | 2020-11-25 11:02:05 -0500 | [diff] [blame] | 1058 | // Early out if there are no inlining candidates. |
John Stiles | 2d7973a | 2020-10-02 15:01:03 -0400 | [diff] [blame] | 1059 | std::vector<InlineCandidate>& candidates = candidateList->fCandidates; |
John Stiles | 0ad233f | 2020-11-25 11:02:05 -0500 | [diff] [blame] | 1060 | if (candidates.empty()) { |
| 1061 | return; |
| 1062 | } |
| 1063 | |
| 1064 | // Remove candidates that are not safe to inline. |
John Stiles | 2d7973a | 2020-10-02 15:01:03 -0400 | [diff] [blame] | 1065 | InlinabilityCache cache; |
| 1066 | candidates.erase(std::remove_if(candidates.begin(), |
| 1067 | candidates.end(), |
| 1068 | [&](const InlineCandidate& candidate) { |
| 1069 | return !this->candidateCanBeInlined(candidate, &cache); |
| 1070 | }), |
| 1071 | candidates.end()); |
| 1072 | |
John Stiles | 0ad233f | 2020-11-25 11:02:05 -0500 | [diff] [blame] | 1073 | // If the inline threshold is unlimited, or if we have no candidates left, our candidate list is |
| 1074 | // complete. |
John Stiles | d120464 | 2021-02-17 16:30:02 -0500 | [diff] [blame] | 1075 | if (this->settings().fInlineThreshold == INT_MAX || candidates.empty()) { |
John Stiles | 0ad233f | 2020-11-25 11:02:05 -0500 | [diff] [blame] | 1076 | return; |
John Stiles | 2d7973a | 2020-10-02 15:01:03 -0400 | [diff] [blame] | 1077 | } |
John Stiles | 0ad233f | 2020-11-25 11:02:05 -0500 | [diff] [blame] | 1078 | |
| 1079 | // Remove candidates on a per-function basis if the effect of inlining would be to make more |
| 1080 | // than `inlineThreshold` nodes. (i.e. if Func() would be inlined six times and its size is |
| 1081 | // 10 nodes, it should be inlined if the inlineThreshold is 60 or higher.) |
| 1082 | FunctionSizeCache functionSizeCache; |
| 1083 | FunctionSizeCache candidateTotalCost; |
| 1084 | for (InlineCandidate& candidate : candidates) { |
| 1085 | const FunctionDeclaration& fnDecl = candidate_func(candidate); |
| 1086 | candidateTotalCost[&fnDecl] += this->getFunctionSize(fnDecl, &functionSizeCache); |
| 1087 | } |
| 1088 | |
John Stiles | d120464 | 2021-02-17 16:30:02 -0500 | [diff] [blame] | 1089 | candidates.erase(std::remove_if(candidates.begin(), candidates.end(), |
| 1090 | [&](const InlineCandidate& candidate) { |
| 1091 | const FunctionDeclaration& fnDecl = candidate_func(candidate); |
| 1092 | if (fnDecl.modifiers().fFlags & Modifiers::kInline_Flag) { |
| 1093 | // Functions marked `inline` ignore size limitations. |
| 1094 | return false; |
| 1095 | } |
| 1096 | if (usage->get(fnDecl) == 1) { |
| 1097 | // If a function is only used once, it's cost-free to inline. |
| 1098 | return false; |
| 1099 | } |
| 1100 | if (candidateTotalCost[&fnDecl] <= this->settings().fInlineThreshold) { |
| 1101 | // We won't exceed the inline threshold by inlining this. |
| 1102 | return false; |
| 1103 | } |
| 1104 | // Inlining this function will add too many IRNodes. |
| 1105 | return true; |
| 1106 | }), |
| 1107 | candidates.end()); |
John Stiles | 2d7973a | 2020-10-02 15:01:03 -0400 | [diff] [blame] | 1108 | } |
| 1109 | |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 1110 | bool Inliner::analyze(const std::vector<std::unique_ptr<ProgramElement>>& elements, |
John Stiles | 7804758 | 2020-12-16 16:17:41 -0500 | [diff] [blame] | 1111 | std::shared_ptr<SymbolTable> symbols, |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 1112 | ProgramUsage* usage) { |
John Stiles | d34d56e | 2020-10-12 12:04:47 -0400 | [diff] [blame] | 1113 | // A threshold of zero indicates that the inliner is completely disabled, so we can just return. |
John Stiles | d120464 | 2021-02-17 16:30:02 -0500 | [diff] [blame] | 1114 | if (this->settings().fInlineThreshold <= 0) { |
John Stiles | d34d56e | 2020-10-12 12:04:47 -0400 | [diff] [blame] | 1115 | return false; |
| 1116 | } |
| 1117 | |
John Stiles | 031a767 | 2020-11-13 16:13:18 -0500 | [diff] [blame] | 1118 | // Enforce a limit on inlining to avoid pathological cases. (inliner/ExponentialGrowth.sksl) |
| 1119 | if (fInlinedStatementCounter >= kInlinedStatementLimit) { |
| 1120 | return false; |
| 1121 | } |
| 1122 | |
John Stiles | 2d7973a | 2020-10-02 15:01:03 -0400 | [diff] [blame] | 1123 | InlineCandidateList candidateList; |
John Stiles | 9b9415e | 2020-11-23 14:48:06 -0500 | [diff] [blame] | 1124 | this->buildCandidateList(elements, symbols, usage, &candidateList); |
John Stiles | 2d7973a | 2020-10-02 15:01:03 -0400 | [diff] [blame] | 1125 | |
John Stiles | 915a38c | 2020-09-14 09:38:13 -0400 | [diff] [blame] | 1126 | // Inline the candidates where we've determined that it's safe to do so. |
John Stiles | 708faba | 2021-03-19 09:43:23 -0400 | [diff] [blame] | 1127 | using StatementRemappingTable = std::unordered_map<std::unique_ptr<Statement>*, |
| 1128 | std::unique_ptr<Statement>*>; |
| 1129 | StatementRemappingTable statementRemappingTable; |
| 1130 | |
John Stiles | 915a38c | 2020-09-14 09:38:13 -0400 | [diff] [blame] | 1131 | bool madeChanges = false; |
John Stiles | 2d7973a | 2020-10-02 15:01:03 -0400 | [diff] [blame] | 1132 | for (const InlineCandidate& candidate : candidateList.fCandidates) { |
John Stiles | 915a38c | 2020-09-14 09:38:13 -0400 | [diff] [blame] | 1133 | FunctionCall& funcCall = (*candidate.fCandidateExpr)->as<FunctionCall>(); |
John Stiles | 915a38c | 2020-09-14 09:38:13 -0400 | [diff] [blame] | 1134 | |
John Stiles | 915a38c | 2020-09-14 09:38:13 -0400 | [diff] [blame] | 1135 | // Convert the function call to its inlined equivalent. |
John Stiles | 30fce9c | 2021-03-18 09:24:06 -0400 | [diff] [blame] | 1136 | InlinedCall inlinedCall = this->inlineCall(&funcCall, candidate.fSymbols, *usage, |
Ethan Nicholas | 0a5d096 | 2020-10-14 13:33:18 -0400 | [diff] [blame] | 1137 | &candidate.fEnclosingFunction->declaration()); |
John Stiles | 915a38c | 2020-09-14 09:38:13 -0400 | [diff] [blame] | 1138 | |
John Stiles | 0c2d14a | 2021-03-01 10:08:08 -0500 | [diff] [blame] | 1139 | // Stop if an error was detected during the inlining process. |
| 1140 | if (!inlinedCall.fInlinedBody && !inlinedCall.fReplacementExpr) { |
| 1141 | break; |
John Stiles | 915a38c | 2020-09-14 09:38:13 -0400 | [diff] [blame] | 1142 | } |
| 1143 | |
John Stiles | 0c2d14a | 2021-03-01 10:08:08 -0500 | [diff] [blame] | 1144 | // Ensure that the inlined body has a scope if it needs one. |
| 1145 | this->ensureScopedBlocks(inlinedCall.fInlinedBody.get(), candidate.fParentStmt->get()); |
| 1146 | |
| 1147 | // Add references within the inlined body |
| 1148 | usage->add(inlinedCall.fInlinedBody.get()); |
| 1149 | |
John Stiles | 708faba | 2021-03-19 09:43:23 -0400 | [diff] [blame] | 1150 | // Look up the enclosing statement; remap it if necessary. |
| 1151 | std::unique_ptr<Statement>* enclosingStmt = candidate.fEnclosingStmt; |
| 1152 | for (;;) { |
| 1153 | auto iter = statementRemappingTable.find(enclosingStmt); |
| 1154 | if (iter == statementRemappingTable.end()) { |
| 1155 | break; |
| 1156 | } |
| 1157 | enclosingStmt = iter->second; |
| 1158 | } |
| 1159 | |
John Stiles | 0c2d14a | 2021-03-01 10:08:08 -0500 | [diff] [blame] | 1160 | // Move the enclosing statement to the end of the unscoped Block containing the inlined |
| 1161 | // function, then replace the enclosing statement with that Block. |
| 1162 | // Before: |
| 1163 | // fInlinedBody = Block{ stmt1, stmt2, stmt3 } |
| 1164 | // fEnclosingStmt = stmt4 |
| 1165 | // After: |
| 1166 | // fInlinedBody = null |
| 1167 | // fEnclosingStmt = Block{ stmt1, stmt2, stmt3, stmt4 } |
John Stiles | 708faba | 2021-03-19 09:43:23 -0400 | [diff] [blame] | 1168 | inlinedCall.fInlinedBody->children().push_back(std::move(*enclosingStmt)); |
| 1169 | *enclosingStmt = std::move(inlinedCall.fInlinedBody); |
John Stiles | 0c2d14a | 2021-03-01 10:08:08 -0500 | [diff] [blame] | 1170 | |
John Stiles | 915a38c | 2020-09-14 09:38:13 -0400 | [diff] [blame] | 1171 | // Replace the candidate function call with our replacement expression. |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 1172 | usage->replace(candidate.fCandidateExpr->get(), inlinedCall.fReplacementExpr.get()); |
John Stiles | 915a38c | 2020-09-14 09:38:13 -0400 | [diff] [blame] | 1173 | *candidate.fCandidateExpr = std::move(inlinedCall.fReplacementExpr); |
| 1174 | madeChanges = true; |
| 1175 | |
John Stiles | 708faba | 2021-03-19 09:43:23 -0400 | [diff] [blame] | 1176 | // If anything else pointed at our enclosing statement, it's now pointing at a Block |
| 1177 | // containing many other statements as well. Maintain a fix-up table to account for this. |
| 1178 | statementRemappingTable[enclosingStmt] = &(*enclosingStmt)->as<Block>().children().back(); |
| 1179 | |
John Stiles | 031a767 | 2020-11-13 16:13:18 -0500 | [diff] [blame] | 1180 | // Stop inlining if we've reached our hard cap on new statements. |
| 1181 | if (fInlinedStatementCounter >= kInlinedStatementLimit) { |
| 1182 | break; |
| 1183 | } |
| 1184 | |
John Stiles | 915a38c | 2020-09-14 09:38:13 -0400 | [diff] [blame] | 1185 | // Note that nothing was destroyed except for the FunctionCall. All other nodes should |
| 1186 | // remain valid. |
| 1187 | } |
| 1188 | |
| 1189 | return madeChanges; |
John Stiles | 9344262 | 2020-09-11 12:11:27 -0400 | [diff] [blame] | 1190 | } |
| 1191 | |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 1192 | } // namespace SkSL |