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 | |
| 10 | #include "limits.h" |
| 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" |
| 52 | #include "src/sksl/ir/SkSLVarDeclarationsStatement.h" |
| 53 | #include "src/sksl/ir/SkSLVariable.h" |
| 54 | #include "src/sksl/ir/SkSLVariableReference.h" |
| 55 | #include "src/sksl/ir/SkSLWhileStatement.h" |
| 56 | |
| 57 | namespace SkSL { |
| 58 | namespace { |
| 59 | |
| 60 | static int count_all_returns(const FunctionDefinition& funcDef) { |
| 61 | class CountAllReturns : public ProgramVisitor { |
| 62 | public: |
| 63 | CountAllReturns(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::kReturn: |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 70 | ++fNumReturns; |
| 71 | [[fallthrough]]; |
| 72 | |
| 73 | default: |
| 74 | return this->INHERITED::visitStatement(stmt); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | int fNumReturns = 0; |
| 79 | using INHERITED = ProgramVisitor; |
| 80 | }; |
| 81 | |
| 82 | return CountAllReturns{funcDef}.fNumReturns; |
| 83 | } |
| 84 | |
| 85 | static int count_returns_at_end_of_control_flow(const FunctionDefinition& funcDef) { |
| 86 | class CountReturnsAtEndOfControlFlow : public ProgramVisitor { |
| 87 | public: |
| 88 | CountReturnsAtEndOfControlFlow(const FunctionDefinition& funcDef) { |
| 89 | this->visitProgramElement(funcDef); |
| 90 | } |
| 91 | |
| 92 | bool visitStatement(const Statement& stmt) override { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 93 | switch (stmt.kind()) { |
| 94 | case Statement::Kind::kBlock: { |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 95 | // Check only the last statement of a block. |
| 96 | const auto& blockStmts = stmt.as<Block>().fStatements; |
| 97 | return (blockStmts.size() > 0) ? this->visitStatement(*blockStmts.back()) |
| 98 | : false; |
| 99 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 100 | case Statement::Kind::kSwitch: |
| 101 | case Statement::Kind::kWhile: |
| 102 | case Statement::Kind::kDo: |
| 103 | case Statement::Kind::kFor: |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 104 | // Don't introspect switches or loop structures at all. |
| 105 | return false; |
| 106 | |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 107 | case Statement::Kind::kReturn: |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 108 | ++fNumReturns; |
| 109 | [[fallthrough]]; |
| 110 | |
| 111 | default: |
| 112 | return this->INHERITED::visitStatement(stmt); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | int fNumReturns = 0; |
| 117 | using INHERITED = ProgramVisitor; |
| 118 | }; |
| 119 | |
| 120 | return CountReturnsAtEndOfControlFlow{funcDef}.fNumReturns; |
| 121 | } |
| 122 | |
| 123 | static int count_returns_in_breakable_constructs(const FunctionDefinition& funcDef) { |
| 124 | class CountReturnsInBreakableConstructs : public ProgramVisitor { |
| 125 | public: |
| 126 | CountReturnsInBreakableConstructs(const FunctionDefinition& funcDef) { |
| 127 | this->visitProgramElement(funcDef); |
| 128 | } |
| 129 | |
| 130 | bool visitStatement(const Statement& stmt) override { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 131 | switch (stmt.kind()) { |
| 132 | case Statement::Kind::kSwitch: |
| 133 | case Statement::Kind::kWhile: |
| 134 | case Statement::Kind::kDo: |
| 135 | case Statement::Kind::kFor: { |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 136 | ++fInsideBreakableConstruct; |
| 137 | bool result = this->INHERITED::visitStatement(stmt); |
| 138 | --fInsideBreakableConstruct; |
| 139 | return result; |
| 140 | } |
| 141 | |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 142 | case Statement::Kind::kReturn: |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 143 | fNumReturns += (fInsideBreakableConstruct > 0) ? 1 : 0; |
| 144 | [[fallthrough]]; |
| 145 | |
| 146 | default: |
| 147 | return this->INHERITED::visitStatement(stmt); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | int fNumReturns = 0; |
| 152 | int fInsideBreakableConstruct = 0; |
| 153 | using INHERITED = ProgramVisitor; |
| 154 | }; |
| 155 | |
| 156 | return CountReturnsInBreakableConstructs{funcDef}.fNumReturns; |
| 157 | } |
| 158 | |
| 159 | static bool has_early_return(const FunctionDefinition& funcDef) { |
| 160 | int returnCount = count_all_returns(funcDef); |
| 161 | if (returnCount == 0) { |
| 162 | return false; |
| 163 | } |
| 164 | |
| 165 | int returnsAtEndOfControlFlow = count_returns_at_end_of_control_flow(funcDef); |
| 166 | return returnCount > returnsAtEndOfControlFlow; |
| 167 | } |
| 168 | |
John Stiles | 991b09d | 2020-09-10 13:33:40 -0400 | [diff] [blame] | 169 | static bool contains_recursive_call(const FunctionDeclaration& funcDecl) { |
| 170 | class ContainsRecursiveCall : public ProgramVisitor { |
| 171 | public: |
| 172 | bool visit(const FunctionDeclaration& funcDecl) { |
| 173 | fFuncDecl = &funcDecl; |
| 174 | return funcDecl.fDefinition ? this->visitProgramElement(*funcDecl.fDefinition) |
| 175 | : false; |
| 176 | } |
| 177 | |
| 178 | bool visitExpression(const Expression& expr) override { |
| 179 | if (expr.is<FunctionCall>() && expr.as<FunctionCall>().fFunction.matches(*fFuncDecl)) { |
| 180 | return true; |
| 181 | } |
| 182 | return INHERITED::visitExpression(expr); |
| 183 | } |
| 184 | |
| 185 | bool visitStatement(const Statement& stmt) override { |
| 186 | if (stmt.is<InlineMarker>() && stmt.as<InlineMarker>().fFuncDecl->matches(*fFuncDecl)) { |
| 187 | return true; |
| 188 | } |
| 189 | return INHERITED::visitStatement(stmt); |
| 190 | } |
| 191 | |
| 192 | const FunctionDeclaration* fFuncDecl; |
| 193 | using INHERITED = ProgramVisitor; |
| 194 | }; |
| 195 | |
| 196 | return ContainsRecursiveCall{}.visit(funcDecl); |
| 197 | } |
| 198 | |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 199 | static const Type* copy_if_needed(const Type* src, SymbolTable& symbolTable) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 200 | if (src->typeKind() == Type::TypeKind::kArray) { |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 201 | return symbolTable.takeOwnershipOfSymbol(std::make_unique<Type>(*src)); |
| 202 | } |
| 203 | return src; |
| 204 | } |
| 205 | |
| 206 | } // namespace |
| 207 | |
| 208 | void Inliner::reset(const Context& context, const Program::Settings& settings) { |
| 209 | fContext = &context; |
| 210 | fSettings = &settings; |
| 211 | fInlineVarCounter = 0; |
| 212 | } |
| 213 | |
| 214 | std::unique_ptr<Expression> Inliner::inlineExpression(int offset, |
| 215 | VariableRewriteMap* varMap, |
| 216 | const Expression& expression) { |
| 217 | auto expr = [&](const std::unique_ptr<Expression>& e) -> std::unique_ptr<Expression> { |
| 218 | if (e) { |
| 219 | return this->inlineExpression(offset, varMap, *e); |
| 220 | } |
| 221 | return nullptr; |
| 222 | }; |
| 223 | auto argList = [&](const std::vector<std::unique_ptr<Expression>>& originalArgs) |
| 224 | -> std::vector<std::unique_ptr<Expression>> { |
| 225 | std::vector<std::unique_ptr<Expression>> args; |
| 226 | args.reserve(originalArgs.size()); |
| 227 | for (const std::unique_ptr<Expression>& arg : originalArgs) { |
| 228 | args.push_back(expr(arg)); |
| 229 | } |
| 230 | return args; |
| 231 | }; |
| 232 | |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 233 | switch (expression.kind()) { |
| 234 | case Expression::Kind::kBinary: { |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 235 | const BinaryExpression& b = expression.as<BinaryExpression>(); |
| 236 | return std::make_unique<BinaryExpression>(offset, |
| 237 | expr(b.fLeft), |
| 238 | b.fOperator, |
| 239 | expr(b.fRight), |
| 240 | b.fType); |
| 241 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 242 | case Expression::Kind::kBoolLiteral: |
| 243 | case Expression::Kind::kIntLiteral: |
| 244 | case Expression::Kind::kFloatLiteral: |
| 245 | case Expression::Kind::kNullLiteral: |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 246 | return expression.clone(); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 247 | case Expression::Kind::kConstructor: { |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 248 | const Constructor& constructor = expression.as<Constructor>(); |
| 249 | return std::make_unique<Constructor>(offset, constructor.fType, |
| 250 | argList(constructor.fArguments)); |
| 251 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 252 | case Expression::Kind::kExternalFunctionCall: { |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 253 | const ExternalFunctionCall& externalCall = expression.as<ExternalFunctionCall>(); |
| 254 | return std::make_unique<ExternalFunctionCall>(offset, externalCall.fType, |
| 255 | externalCall.fFunction, |
| 256 | argList(externalCall.fArguments)); |
| 257 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 258 | case Expression::Kind::kExternalValue: |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 259 | return expression.clone(); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 260 | case Expression::Kind::kFieldAccess: { |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 261 | const FieldAccess& f = expression.as<FieldAccess>(); |
| 262 | return std::make_unique<FieldAccess>(expr(f.fBase), f.fFieldIndex, f.fOwnerKind); |
| 263 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 264 | case Expression::Kind::kFunctionCall: { |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 265 | const FunctionCall& funcCall = expression.as<FunctionCall>(); |
| 266 | return std::make_unique<FunctionCall>(offset, funcCall.fType, funcCall.fFunction, |
| 267 | argList(funcCall.fArguments)); |
| 268 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 269 | case Expression::Kind::kFunctionReference: |
Brian Osman | 2b3b35f | 2020-09-08 09:17:36 -0400 | [diff] [blame] | 270 | return expression.clone(); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 271 | case Expression::Kind::kIndex: { |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 272 | const IndexExpression& idx = expression.as<IndexExpression>(); |
| 273 | return std::make_unique<IndexExpression>(*fContext, expr(idx.fBase), expr(idx.fIndex)); |
| 274 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 275 | case Expression::Kind::kPrefix: { |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 276 | const PrefixExpression& p = expression.as<PrefixExpression>(); |
| 277 | return std::make_unique<PrefixExpression>(p.fOperator, expr(p.fOperand)); |
| 278 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 279 | case Expression::Kind::kPostfix: { |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 280 | const PostfixExpression& p = expression.as<PostfixExpression>(); |
| 281 | return std::make_unique<PostfixExpression>(expr(p.fOperand), p.fOperator); |
| 282 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 283 | case Expression::Kind::kSetting: |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 284 | return expression.clone(); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 285 | case Expression::Kind::kSwizzle: { |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 286 | const Swizzle& s = expression.as<Swizzle>(); |
| 287 | return std::make_unique<Swizzle>(*fContext, expr(s.fBase), s.fComponents); |
| 288 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 289 | case Expression::Kind::kTernary: { |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 290 | const TernaryExpression& t = expression.as<TernaryExpression>(); |
| 291 | return std::make_unique<TernaryExpression>(offset, expr(t.fTest), |
| 292 | expr(t.fIfTrue), expr(t.fIfFalse)); |
| 293 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 294 | case Expression::Kind::kVariableReference: { |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 295 | const VariableReference& v = expression.as<VariableReference>(); |
| 296 | auto found = varMap->find(&v.fVariable); |
| 297 | if (found != varMap->end()) { |
| 298 | return std::make_unique<VariableReference>(offset, *found->second, v.fRefKind); |
| 299 | } |
| 300 | return v.clone(); |
| 301 | } |
| 302 | default: |
| 303 | SkASSERT(false); |
| 304 | return nullptr; |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | std::unique_ptr<Statement> Inliner::inlineStatement(int offset, |
| 309 | VariableRewriteMap* varMap, |
| 310 | SymbolTable* symbolTableForStatement, |
| 311 | const Variable* returnVar, |
| 312 | bool haveEarlyReturns, |
| 313 | const Statement& statement) { |
| 314 | auto stmt = [&](const std::unique_ptr<Statement>& s) -> std::unique_ptr<Statement> { |
| 315 | if (s) { |
| 316 | return this->inlineStatement(offset, varMap, symbolTableForStatement, returnVar, |
| 317 | haveEarlyReturns, *s); |
| 318 | } |
| 319 | return nullptr; |
| 320 | }; |
| 321 | auto stmts = [&](const std::vector<std::unique_ptr<Statement>>& ss) { |
| 322 | std::vector<std::unique_ptr<Statement>> result; |
| 323 | for (const auto& s : ss) { |
| 324 | result.push_back(stmt(s)); |
| 325 | } |
| 326 | return result; |
| 327 | }; |
| 328 | auto expr = [&](const std::unique_ptr<Expression>& e) -> std::unique_ptr<Expression> { |
| 329 | if (e) { |
| 330 | return this->inlineExpression(offset, varMap, *e); |
| 331 | } |
| 332 | return nullptr; |
| 333 | }; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 334 | switch (statement.kind()) { |
| 335 | case Statement::Kind::kBlock: { |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 336 | const Block& b = statement.as<Block>(); |
| 337 | return std::make_unique<Block>(offset, stmts(b.fStatements), b.fSymbols, b.fIsScope); |
| 338 | } |
| 339 | |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 340 | case Statement::Kind::kBreak: |
| 341 | case Statement::Kind::kContinue: |
| 342 | case Statement::Kind::kDiscard: |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 343 | return statement.clone(); |
| 344 | |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 345 | case Statement::Kind::kDo: { |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 346 | const DoStatement& d = statement.as<DoStatement>(); |
| 347 | return std::make_unique<DoStatement>(offset, stmt(d.fStatement), expr(d.fTest)); |
| 348 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 349 | case Statement::Kind::kExpression: { |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 350 | const ExpressionStatement& e = statement.as<ExpressionStatement>(); |
| 351 | return std::make_unique<ExpressionStatement>(expr(e.fExpression)); |
| 352 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 353 | case Statement::Kind::kFor: { |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 354 | const ForStatement& f = statement.as<ForStatement>(); |
| 355 | // need to ensure initializer is evaluated first so that we've already remapped its |
| 356 | // declarations by the time we evaluate test & next |
| 357 | std::unique_ptr<Statement> initializer = stmt(f.fInitializer); |
| 358 | return std::make_unique<ForStatement>(offset, std::move(initializer), expr(f.fTest), |
| 359 | expr(f.fNext), stmt(f.fStatement), f.fSymbols); |
| 360 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 361 | case Statement::Kind::kIf: { |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 362 | const IfStatement& i = statement.as<IfStatement>(); |
| 363 | return std::make_unique<IfStatement>(offset, i.fIsStatic, expr(i.fTest), |
| 364 | stmt(i.fIfTrue), stmt(i.fIfFalse)); |
| 365 | } |
John Stiles | 98c1f82 | 2020-09-09 14:18:53 -0400 | [diff] [blame] | 366 | case Statement::Kind::kInlineMarker: |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 367 | case Statement::Kind::kNop: |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 368 | return statement.clone(); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 369 | case Statement::Kind::kReturn: { |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 370 | const ReturnStatement& r = statement.as<ReturnStatement>(); |
| 371 | if (r.fExpression) { |
| 372 | auto assignment = std::make_unique<ExpressionStatement>( |
| 373 | std::make_unique<BinaryExpression>( |
| 374 | offset, |
| 375 | std::make_unique<VariableReference>(offset, *returnVar, |
| 376 | VariableReference::kWrite_RefKind), |
| 377 | Token::Kind::TK_EQ, |
| 378 | expr(r.fExpression), |
| 379 | returnVar->fType)); |
| 380 | if (haveEarlyReturns) { |
| 381 | std::vector<std::unique_ptr<Statement>> block; |
| 382 | block.push_back(std::move(assignment)); |
| 383 | block.emplace_back(new BreakStatement(offset)); |
| 384 | return std::make_unique<Block>(offset, std::move(block), /*symbols=*/nullptr, |
| 385 | /*isScope=*/true); |
| 386 | } else { |
| 387 | return std::move(assignment); |
| 388 | } |
| 389 | } else { |
| 390 | if (haveEarlyReturns) { |
| 391 | return std::make_unique<BreakStatement>(offset); |
| 392 | } else { |
| 393 | return std::make_unique<Nop>(); |
| 394 | } |
| 395 | } |
| 396 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 397 | case Statement::Kind::kSwitch: { |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 398 | const SwitchStatement& ss = statement.as<SwitchStatement>(); |
| 399 | std::vector<std::unique_ptr<SwitchCase>> cases; |
| 400 | for (const auto& sc : ss.fCases) { |
| 401 | cases.emplace_back(new SwitchCase(offset, expr(sc->fValue), |
| 402 | stmts(sc->fStatements))); |
| 403 | } |
| 404 | return std::make_unique<SwitchStatement>(offset, ss.fIsStatic, expr(ss.fValue), |
| 405 | std::move(cases), ss.fSymbols); |
| 406 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 407 | case Statement::Kind::kVarDeclaration: { |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 408 | const VarDeclaration& decl = statement.as<VarDeclaration>(); |
| 409 | std::vector<std::unique_ptr<Expression>> sizes; |
| 410 | for (const auto& size : decl.fSizes) { |
| 411 | sizes.push_back(expr(size)); |
| 412 | } |
| 413 | std::unique_ptr<Expression> initialValue = expr(decl.fValue); |
| 414 | const Variable* old = decl.fVar; |
| 415 | // need to copy the var name in case the originating function is discarded and we lose |
| 416 | // its symbols |
| 417 | std::unique_ptr<String> name(new String(old->fName)); |
| 418 | const String* namePtr = symbolTableForStatement->takeOwnershipOfString(std::move(name)); |
| 419 | const Type* typePtr = copy_if_needed(&old->fType, *symbolTableForStatement); |
| 420 | const Variable* clone = symbolTableForStatement->takeOwnershipOfSymbol( |
| 421 | std::make_unique<Variable>(offset, |
| 422 | old->fModifiers, |
| 423 | namePtr->c_str(), |
| 424 | *typePtr, |
| 425 | old->fStorage, |
| 426 | initialValue.get())); |
| 427 | (*varMap)[old] = clone; |
| 428 | return std::make_unique<VarDeclaration>(clone, std::move(sizes), |
| 429 | std::move(initialValue)); |
| 430 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 431 | case Statement::Kind::kVarDeclarations: { |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 432 | const VarDeclarations& decls = *statement.as<VarDeclarationsStatement>().fDeclaration; |
| 433 | std::vector<std::unique_ptr<VarDeclaration>> vars; |
| 434 | for (const auto& var : decls.fVars) { |
| 435 | vars.emplace_back(&stmt(var).release()->as<VarDeclaration>()); |
| 436 | } |
| 437 | const Type* typePtr = copy_if_needed(&decls.fBaseType, *symbolTableForStatement); |
| 438 | return std::unique_ptr<Statement>(new VarDeclarationsStatement( |
| 439 | std::make_unique<VarDeclarations>(offset, typePtr, std::move(vars)))); |
| 440 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 441 | case Statement::Kind::kWhile: { |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 442 | const WhileStatement& w = statement.as<WhileStatement>(); |
| 443 | return std::make_unique<WhileStatement>(offset, expr(w.fTest), stmt(w.fStatement)); |
| 444 | } |
| 445 | default: |
| 446 | SkASSERT(false); |
| 447 | return nullptr; |
| 448 | } |
| 449 | } |
| 450 | |
John Stiles | 6eadf13 | 2020-09-08 10:16:10 -0400 | [diff] [blame] | 451 | Inliner::InlinedCall Inliner::inlineCall(FunctionCall* call, |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 452 | SymbolTable* symbolTableForCall) { |
| 453 | // Inlining is more complicated here than in a typical compiler, because we have to have a |
| 454 | // high-level IR and can't just drop statements into the middle of an expression or even use |
| 455 | // gotos. |
| 456 | // |
| 457 | // Since we can't insert statements into an expression, we run the inline function as extra |
| 458 | // statements before the statement we're currently processing, relying on a lack of execution |
| 459 | // order guarantees. Since we can't use gotos (which are normally used to replace return |
| 460 | // statements), we wrap the whole function in a loop and use break statements to jump to the |
| 461 | // end. |
| 462 | SkASSERT(fSettings); |
| 463 | SkASSERT(fContext); |
| 464 | SkASSERT(call); |
| 465 | SkASSERT(this->isSafeToInline(*call, /*inlineThreshold=*/INT_MAX)); |
| 466 | |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 467 | std::vector<std::unique_ptr<Expression>>& arguments = call->fArguments; |
John Stiles | 6eadf13 | 2020-09-08 10:16:10 -0400 | [diff] [blame] | 468 | const int offset = call->fOffset; |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 469 | const FunctionDefinition& function = *call->fFunction.fDefinition; |
John Stiles | 6eadf13 | 2020-09-08 10:16:10 -0400 | [diff] [blame] | 470 | const bool hasEarlyReturn = has_early_return(function); |
| 471 | |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 472 | InlinedCall inlinedCall; |
John Stiles | 6eadf13 | 2020-09-08 10:16:10 -0400 | [diff] [blame] | 473 | inlinedCall.fInlinedBody = std::make_unique<Block>(offset, |
| 474 | std::vector<std::unique_ptr<Statement>>{}, |
| 475 | /*symbols=*/nullptr, |
| 476 | /*isScope=*/false); |
John Stiles | 98c1f82 | 2020-09-09 14:18:53 -0400 | [diff] [blame] | 477 | |
John Stiles | 6eadf13 | 2020-09-08 10:16:10 -0400 | [diff] [blame] | 478 | std::vector<std::unique_ptr<Statement>>& inlinedBody = inlinedCall.fInlinedBody->fStatements; |
John Stiles | 98c1f82 | 2020-09-09 14:18:53 -0400 | [diff] [blame] | 479 | inlinedBody.reserve(1 + // Inline marker |
| 480 | 1 + // Result variable |
| 481 | arguments.size() + // Function arguments (passing in) |
| 482 | arguments.size() + // Function arguments (copy out-parameters back) |
| 483 | 1); // Inlined code (either as a Block or do-while loop) |
| 484 | |
| 485 | inlinedBody.push_back(std::make_unique<InlineMarker>(call->fFunction)); |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 486 | |
John Stiles | cf936f9 | 2020-08-31 17:18:45 -0400 | [diff] [blame] | 487 | auto makeInlineVar = [&](const String& baseName, const Type& type, Modifiers modifiers, |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 488 | std::unique_ptr<Expression>* initialValue) -> const Variable* { |
John Stiles | cf936f9 | 2020-08-31 17:18:45 -0400 | [diff] [blame] | 489 | // If the base name starts with an underscore, like "_coords", we can't append another |
| 490 | // underscore, because some OpenGL platforms error out when they see two consecutive |
| 491 | // underscores (anywhere in the string!). But in the general case, using the underscore as |
| 492 | // a splitter reads nicely enough that it's worth putting in this special case. |
| 493 | const char* splitter = baseName.startsWith("_") ? "_X" : "_"; |
| 494 | |
| 495 | // Append a unique numeric prefix to avoid name overlap. Check the symbol table to make sure |
| 496 | // we're not reusing an existing name. (Note that within a single compilation pass, this |
| 497 | // check isn't fully comprehensive, as code isn't always generated in top-to-bottom order.) |
| 498 | String uniqueName; |
| 499 | for (;;) { |
| 500 | uniqueName = String::printf("_%d%s%s", fInlineVarCounter++, splitter, baseName.c_str()); |
| 501 | StringFragment frag{uniqueName.data(), uniqueName.length()}; |
| 502 | if ((*symbolTableForCall)[frag] == nullptr) { |
| 503 | break; |
| 504 | } |
| 505 | } |
| 506 | |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 507 | // Add our new variable's name to the symbol table. |
John Stiles | cf936f9 | 2020-08-31 17:18:45 -0400 | [diff] [blame] | 508 | const String* namePtr = symbolTableForCall->takeOwnershipOfString( |
| 509 | std::make_unique<String>(std::move(uniqueName))); |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 510 | StringFragment nameFrag{namePtr->c_str(), namePtr->length()}; |
| 511 | |
| 512 | // Add our new variable to the symbol table. |
| 513 | auto newVar = std::make_unique<Variable>(/*offset=*/-1, Modifiers(), nameFrag, type, |
| 514 | Variable::kLocal_Storage, initialValue->get()); |
| 515 | const Variable* variableSymbol = symbolTableForCall->add(nameFrag, std::move(newVar)); |
| 516 | |
| 517 | // Prepare the variable declaration (taking extra care with `out` params to not clobber any |
| 518 | // initial value). |
| 519 | std::vector<std::unique_ptr<VarDeclaration>> variables; |
| 520 | if (initialValue && (modifiers.fFlags & Modifiers::kOut_Flag)) { |
| 521 | variables.push_back(std::make_unique<VarDeclaration>( |
| 522 | variableSymbol, /*sizes=*/std::vector<std::unique_ptr<Expression>>{}, |
| 523 | (*initialValue)->clone())); |
| 524 | } else { |
| 525 | variables.push_back(std::make_unique<VarDeclaration>( |
| 526 | variableSymbol, /*sizes=*/std::vector<std::unique_ptr<Expression>>{}, |
| 527 | std::move(*initialValue))); |
| 528 | } |
| 529 | |
| 530 | // Add the new variable-declaration statement to our block of extra statements. |
John Stiles | 39616ec | 2020-08-31 14:16:06 -0400 | [diff] [blame] | 531 | inlinedBody.push_back(std::make_unique<VarDeclarationsStatement>( |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 532 | std::make_unique<VarDeclarations>(offset, &type, std::move(variables)))); |
| 533 | |
| 534 | return variableSymbol; |
| 535 | }; |
| 536 | |
| 537 | // Create a variable to hold the result in the extra statements (excepting void). |
| 538 | const Variable* resultVar = nullptr; |
| 539 | if (function.fDeclaration.fReturnType != *fContext->fVoid_Type) { |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 540 | std::unique_ptr<Expression> noInitialValue; |
John Stiles | cf936f9 | 2020-08-31 17:18:45 -0400 | [diff] [blame] | 541 | resultVar = makeInlineVar(String(function.fDeclaration.fName), |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 542 | function.fDeclaration.fReturnType, Modifiers{}, &noInitialValue); |
| 543 | } |
| 544 | |
| 545 | // Create variables in the extra statements to hold the arguments, and assign the arguments to |
| 546 | // them. |
| 547 | VariableRewriteMap varMap; |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 548 | for (int i = 0; i < (int) arguments.size(); ++i) { |
| 549 | const Variable* param = function.fDeclaration.fParameters[i]; |
| 550 | |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 551 | if (arguments[i]->kind() == Expression::Kind::kVariableReference) { |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 552 | // The argument is just a variable, so we only need to copy it if it's an out parameter |
| 553 | // or it's written to within the function. |
| 554 | if ((param->fModifiers.fFlags & Modifiers::kOut_Flag) || |
| 555 | !Analysis::StatementWritesToVariable(*function.fBody, *param)) { |
| 556 | varMap[param] = &arguments[i]->as<VariableReference>().fVariable; |
| 557 | continue; |
| 558 | } |
| 559 | } |
| 560 | |
John Stiles | cf936f9 | 2020-08-31 17:18:45 -0400 | [diff] [blame] | 561 | varMap[param] = makeInlineVar(String(param->fName), arguments[i]->fType, param->fModifiers, |
| 562 | &arguments[i]); |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | const Block& body = function.fBody->as<Block>(); |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 566 | auto inlineBlock = std::make_unique<Block>(offset, std::vector<std::unique_ptr<Statement>>{}); |
| 567 | inlineBlock->fStatements.reserve(body.fStatements.size()); |
| 568 | for (const std::unique_ptr<Statement>& stmt : body.fStatements) { |
| 569 | inlineBlock->fStatements.push_back(this->inlineStatement( |
| 570 | offset, &varMap, symbolTableForCall, resultVar, hasEarlyReturn, *stmt)); |
| 571 | } |
| 572 | if (hasEarlyReturn) { |
| 573 | // Since we output to backends that don't have a goto statement (which would normally be |
| 574 | // used to perform an early return), we fake it by wrapping the function in a |
| 575 | // do { } while (false); and then use break statements to jump to the end in order to |
| 576 | // emulate a goto. |
John Stiles | 39616ec | 2020-08-31 14:16:06 -0400 | [diff] [blame] | 577 | inlinedBody.push_back(std::make_unique<DoStatement>( |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 578 | /*offset=*/-1, |
| 579 | std::move(inlineBlock), |
| 580 | std::make_unique<BoolLiteral>(*fContext, offset, /*value=*/false))); |
| 581 | } else { |
John Stiles | 6eadf13 | 2020-09-08 10:16:10 -0400 | [diff] [blame] | 582 | // No early returns, so we can just dump the code in. We still need to keep the block so we |
| 583 | // don't get name conflicts with locals. |
John Stiles | 39616ec | 2020-08-31 14:16:06 -0400 | [diff] [blame] | 584 | inlinedBody.push_back(std::move(inlineBlock)); |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | // Copy the values of `out` parameters into their destinations. |
| 588 | for (size_t i = 0; i < arguments.size(); ++i) { |
| 589 | const Variable* p = function.fDeclaration.fParameters[i]; |
| 590 | if (p->fModifiers.fFlags & Modifiers::kOut_Flag) { |
| 591 | SkASSERT(varMap.find(p) != varMap.end()); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 592 | if (arguments[i]->kind() == Expression::Kind::kVariableReference && |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 593 | &arguments[i]->as<VariableReference>().fVariable == varMap[p]) { |
John Stiles | 6eadf13 | 2020-09-08 10:16:10 -0400 | [diff] [blame] | 594 | // We didn't create a temporary for this parameter, so there's nothing to copy back |
| 595 | // out. |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 596 | continue; |
| 597 | } |
| 598 | auto varRef = std::make_unique<VariableReference>(offset, *varMap[p]); |
John Stiles | 39616ec | 2020-08-31 14:16:06 -0400 | [diff] [blame] | 599 | inlinedBody.push_back(std::make_unique<ExpressionStatement>( |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 600 | std::make_unique<BinaryExpression>(offset, |
| 601 | arguments[i]->clone(), |
| 602 | Token::Kind::TK_EQ, |
| 603 | std::move(varRef), |
| 604 | arguments[i]->fType))); |
| 605 | } |
| 606 | } |
| 607 | |
| 608 | if (function.fDeclaration.fReturnType != *fContext->fVoid_Type) { |
| 609 | // Return a reference to the result variable as our replacement expression. |
| 610 | inlinedCall.fReplacementExpr = std::make_unique<VariableReference>(offset, *resultVar); |
| 611 | } else { |
| 612 | // It's a void function, so it doesn't actually result in anything, but we have to return |
| 613 | // something non-null as a standin. |
| 614 | inlinedCall.fReplacementExpr = std::make_unique<BoolLiteral>(*fContext, offset, |
| 615 | /*value=*/false); |
| 616 | } |
| 617 | |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 618 | return inlinedCall; |
| 619 | } |
| 620 | |
| 621 | bool Inliner::isSafeToInline(const FunctionCall& functionCall, |
| 622 | int inlineThreshold) { |
| 623 | SkASSERT(fSettings); |
| 624 | |
| 625 | if (functionCall.fFunction.fDefinition == nullptr) { |
| 626 | // Can't inline something if we don't actually have its definition. |
| 627 | return false; |
| 628 | } |
| 629 | const FunctionDefinition& functionDef = *functionCall.fFunction.fDefinition; |
| 630 | if (inlineThreshold < INT_MAX) { |
| 631 | if (!(functionDef.fDeclaration.fModifiers.fFlags & Modifiers::kInline_Flag) && |
| 632 | Analysis::NodeCount(functionDef) >= inlineThreshold) { |
| 633 | // The function exceeds our maximum inline size and is not flagged 'inline'. |
| 634 | return false; |
| 635 | } |
| 636 | } |
John Stiles | 991b09d | 2020-09-10 13:33:40 -0400 | [diff] [blame] | 637 | if (contains_recursive_call(functionCall.fFunction)) { |
| 638 | // We do not perform inlining on recursive calls to avoid an infinite death spiral of |
| 639 | // inlining. |
| 640 | return false; |
| 641 | } |
John Stiles | 44e96be | 2020-08-31 13:16:04 -0400 | [diff] [blame] | 642 | if (!fSettings->fCaps || !fSettings->fCaps->canUseDoLoops()) { |
| 643 | // We don't have do-while loops. We use do-while loops to simulate early returns, so we |
| 644 | // can't inline functions that have an early return. |
| 645 | bool hasEarlyReturn = has_early_return(functionDef); |
| 646 | |
| 647 | // If we didn't detect an early return, there shouldn't be any returns in breakable |
| 648 | // constructs either. |
| 649 | SkASSERT(hasEarlyReturn || count_returns_in_breakable_constructs(functionDef) == 0); |
| 650 | return !hasEarlyReturn; |
| 651 | } |
| 652 | // We have do-while loops, but we don't have any mechanism to simulate early returns within a |
| 653 | // breakable construct (switch/for/do/while), so we can't inline if there's a return inside one. |
| 654 | bool hasReturnInBreakableConstruct = (count_returns_in_breakable_constructs(functionDef) > 0); |
| 655 | |
| 656 | // If we detected returns in breakable constructs, we should also detect an early return. |
| 657 | SkASSERT(!hasReturnInBreakableConstruct || has_early_return(functionDef)); |
| 658 | return !hasReturnInBreakableConstruct; |
| 659 | } |
| 660 | |
| 661 | } // namespace SkSL |