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