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