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