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 #ifndef SKSL_INLINER 9 #define SKSL_INLINER 10 11 #ifndef SK_ENABLE_OPTIMIZE_SIZE 12 13 #include "src/core/SkTHash.h" 14 #include "src/sksl/SkSLContext.h" 15 #include "src/sksl/SkSLMangler.h" 16 #include "src/sksl/SkSLProgramSettings.h" 17 #include "src/sksl/ir/SkSLBlock.h" 18 #include "src/sksl/ir/SkSLExpression.h" 19 20 #include <memory> 21 #include <vector> 22 23 namespace SkSL { 24 25 class FunctionCall; 26 class FunctionDeclaration; 27 class FunctionDefinition; 28 class Position; 29 class ProgramElement; 30 class ProgramUsage; 31 class Statement; 32 class SymbolTable; 33 class Variable; 34 struct InlineCandidate; 35 struct InlineCandidateList; 36 namespace Analysis { enum class ReturnComplexity; } 37 38 /** 39 * Converts a FunctionCall in the IR to a set of statements to be injected ahead of the function 40 * call, and a replacement expression. Can also detect cases where inlining isn't cleanly possible 41 * (e.g. return statements nested inside of a loop construct). The inliner isn't able to guarantee 42 * identical-to-GLSL execution order if the inlined function has visible side effects. 43 */ 44 class Inliner { 45 public: Inliner(const Context * context)46 Inliner(const Context* context) : fContext(context) {} 47 48 /** Inlines any eligible functions that are found. Returns true if any changes are made. */ 49 bool analyze(const std::vector<std::unique_ptr<ProgramElement>>& elements, 50 std::shared_ptr<SymbolTable> symbols, 51 ProgramUsage* usage); 52 53 private: 54 using VariableRewriteMap = SkTHashMap<const Variable*, std::unique_ptr<Expression>>; 55 settings()56 const ProgramSettings& settings() const { return fContext->fConfig->fSettings; } 57 58 void buildCandidateList(const std::vector<std::unique_ptr<ProgramElement>>& elements, 59 std::shared_ptr<SymbolTable> symbols, ProgramUsage* usage, 60 InlineCandidateList* candidateList); 61 62 std::unique_ptr<Expression> inlineExpression(Position pos, 63 VariableRewriteMap* varMap, 64 SymbolTable* symbolTableForExpression, 65 const Expression& expression); 66 std::unique_ptr<Statement> inlineStatement(Position pos, 67 VariableRewriteMap* varMap, 68 SymbolTable* symbolTableForStatement, 69 std::unique_ptr<Expression>* resultExpr, 70 Analysis::ReturnComplexity returnComplexity, 71 const Statement& statement, 72 const ProgramUsage& usage, 73 bool isBuiltinCode); 74 75 /** 76 * Searches the rewrite map for an rewritten Variable* for the passed-in one. Asserts if the 77 * rewrite map doesn't contain the variable, or contains a different type of expression. 78 */ 79 static const Variable* RemapVariable(const Variable* variable, 80 const VariableRewriteMap* varMap); 81 82 using InlinabilityCache = SkTHashMap<const FunctionDeclaration*, bool>; 83 bool candidateCanBeInlined(const InlineCandidate& candidate, 84 const ProgramUsage& usage, 85 InlinabilityCache* cache); 86 87 using FunctionSizeCache = SkTHashMap<const FunctionDeclaration*, int>; 88 int getFunctionSize(const FunctionDeclaration& fnDecl, FunctionSizeCache* cache); 89 90 /** 91 * Processes the passed-in FunctionCall expression. The FunctionCall expression should be 92 * replaced with `fReplacementExpr`. If non-null, `fInlinedBody` should be inserted immediately 93 * above the statement containing the inlined expression. 94 */ 95 struct InlinedCall { 96 std::unique_ptr<Block> fInlinedBody; 97 std::unique_ptr<Expression> fReplacementExpr; 98 }; 99 InlinedCall inlineCall(const FunctionCall&, 100 std::shared_ptr<SymbolTable>, 101 const ProgramUsage&, 102 const FunctionDeclaration* caller); 103 104 /** Adds a scope to inlined bodies returned by `inlineCall`, if one is required. */ 105 void ensureScopedBlocks(Statement* inlinedBody, Statement* parentStmt); 106 107 /** Checks whether inlining is viable for a FunctionCall, modulo recursion and function size. */ 108 bool isSafeToInline(const FunctionDefinition* functionDef, const ProgramUsage& usage); 109 110 const Context* fContext = nullptr; 111 Mangler fMangler; 112 int fInlinedStatementCounter = 0; 113 }; 114 115 } // namespace SkSL 116 117 #endif // SK_ENABLE_OPTIMIZE_SIZE 118 119 #endif // SKSL_INLINER 120