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