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