2020-08-31 17:16:04 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2020 Google LLC
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef SKSL_INLINER
|
|
|
|
#define SKSL_INLINER
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
#include <unordered_map>
|
|
|
|
|
|
|
|
#include "src/sksl/ir/SkSLProgram.h"
|
|
|
|
|
|
|
|
namespace SkSL {
|
|
|
|
|
2020-09-08 14:16:10 +00:00
|
|
|
struct Block;
|
2020-08-31 17:16:04 +00:00
|
|
|
class Context;
|
|
|
|
struct Expression;
|
|
|
|
struct FunctionCall;
|
|
|
|
struct Statement;
|
|
|
|
class SymbolTable;
|
|
|
|
struct Variable;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts a FunctionCall in the IR to a set of statements to be injected ahead of the function
|
|
|
|
* call, and a replacement expression. Can also detect cases where inlining isn't cleanly possible
|
|
|
|
* (e.g. return statements nested inside of a loop construct). The inliner isn't able to guarantee
|
|
|
|
* identical-to-GLSL execution order if the inlined function has visible side effects.
|
|
|
|
*/
|
|
|
|
class Inliner {
|
|
|
|
public:
|
|
|
|
Inliner() {}
|
|
|
|
|
|
|
|
void reset(const Context&, const Program::Settings&);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Processes the passed-in FunctionCall expression. The FunctionCall expression should be
|
2020-08-31 18:16:06 +00:00
|
|
|
* replaced with `fReplacementExpr`. If non-null, `fInlinedBody` should be inserted immediately
|
|
|
|
* above the statement containing the inlined expression.
|
2020-08-31 17:16:04 +00:00
|
|
|
*/
|
|
|
|
struct InlinedCall {
|
2020-09-08 14:16:10 +00:00
|
|
|
std::unique_ptr<Block> fInlinedBody;
|
2020-08-31 17:16:04 +00:00
|
|
|
std::unique_ptr<Expression> fReplacementExpr;
|
|
|
|
};
|
2020-09-08 14:16:10 +00:00
|
|
|
InlinedCall inlineCall(FunctionCall*, SymbolTable*);
|
2020-08-31 17:16:04 +00:00
|
|
|
|
2020-09-21 16:26:59 +00:00
|
|
|
/** Adds a scope to inlined bodies returned by `inlineCall`, if one is required. */
|
|
|
|
void ensureScopedBlocks(Statement* inlinedBody, Statement* parentStmt);
|
|
|
|
|
2020-08-31 17:16:04 +00:00
|
|
|
/** Checks whether inlining is viable for a FunctionCall. */
|
|
|
|
bool isSafeToInline(const FunctionCall&, int inlineThreshold);
|
|
|
|
|
2020-09-14 13:38:13 +00:00
|
|
|
/** Inlines any eligible functions that are found. Returns true if any changes are made. */
|
2020-09-11 16:11:27 +00:00
|
|
|
bool analyze(Program& program);
|
|
|
|
|
2020-08-31 17:16:04 +00:00
|
|
|
private:
|
|
|
|
using VariableRewriteMap = std::unordered_map<const Variable*, const Variable*>;
|
|
|
|
|
2020-09-14 22:24:12 +00:00
|
|
|
String uniqueNameForInlineVar(const String& baseName, SymbolTable* symbolTable);
|
|
|
|
|
2020-08-31 17:16:04 +00:00
|
|
|
std::unique_ptr<Expression> inlineExpression(int offset,
|
|
|
|
VariableRewriteMap* varMap,
|
|
|
|
const Expression& expression);
|
|
|
|
std::unique_ptr<Statement> inlineStatement(int offset,
|
|
|
|
VariableRewriteMap* varMap,
|
|
|
|
SymbolTable* symbolTableForStatement,
|
|
|
|
const Variable* returnVar,
|
|
|
|
bool haveEarlyReturns,
|
|
|
|
const Statement& statement);
|
|
|
|
|
|
|
|
const Context* fContext = nullptr;
|
|
|
|
const Program::Settings* fSettings = nullptr;
|
|
|
|
int fInlineVarCounter = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace SkSL
|
|
|
|
|
|
|
|
#endif // SKSL_INLINER
|