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