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