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