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