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