Move constant folding to a separate file.
This doesn't change any logic, just makes the IR generator a few hundred lines shorter. Change-Id: I92010191ee9283c33499c819d65fc85913f25824 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/352121 Commit-Queue: John Stiles <johnstiles@google.com> Auto-Submit: John Stiles <johnstiles@google.com> Reviewed-by: Brian Osman <brianosman@google.com>
This commit is contained in:
parent
eb54bb51b1
commit
dc8ec31ce5
@ -22,6 +22,8 @@ skia_sksl_sources = [
|
||||
"$_src/sksl/SkSLCFGGenerator.h",
|
||||
"$_src/sksl/SkSLCompiler.cpp",
|
||||
"$_src/sksl/SkSLCompiler.h",
|
||||
"$_src/sksl/SkSLConstantFolder.cpp",
|
||||
"$_src/sksl/SkSLConstantFolder.h",
|
||||
"$_src/sksl/SkSLContext.h",
|
||||
"$_src/sksl/SkSLDefines.h",
|
||||
"$_src/sksl/SkSLDehydrator.cpp",
|
||||
|
282
src/sksl/SkSLConstantFolder.cpp
Normal file
282
src/sksl/SkSLConstantFolder.cpp
Normal file
@ -0,0 +1,282 @@
|
||||
/*
|
||||
* Copyright 2020 Google LLC
|
||||
*
|
||||
* Use of this source code is governed by a BSD-style license that can be
|
||||
* found in the LICENSE file.
|
||||
*/
|
||||
|
||||
#include "src/sksl/SkSLConstantFolder.h"
|
||||
|
||||
#include <limits>
|
||||
|
||||
#include "src/sksl/SkSLContext.h"
|
||||
#include "src/sksl/SkSLErrorReporter.h"
|
||||
#include "src/sksl/ir/SkSLBinaryExpression.h"
|
||||
#include "src/sksl/ir/SkSLBoolLiteral.h"
|
||||
#include "src/sksl/ir/SkSLConstructor.h"
|
||||
#include "src/sksl/ir/SkSLExpression.h"
|
||||
#include "src/sksl/ir/SkSLFloatLiteral.h"
|
||||
#include "src/sksl/ir/SkSLIntLiteral.h"
|
||||
#include "src/sksl/ir/SkSLType.h"
|
||||
#include "src/sksl/ir/SkSLVariable.h"
|
||||
#include "src/sksl/ir/SkSLVariableReference.h"
|
||||
|
||||
namespace SkSL {
|
||||
|
||||
static std::unique_ptr<Expression> short_circuit_boolean(const Expression& left,
|
||||
Token::Kind op,
|
||||
const Expression& right) {
|
||||
SkASSERT(left.is<BoolLiteral>());
|
||||
bool leftVal = left.as<BoolLiteral>().value();
|
||||
|
||||
if (op == Token::Kind::TK_LOGICALAND) {
|
||||
// (true && expr) -> (expr) and (false && expr) -> (false)
|
||||
return leftVal ? right.clone()
|
||||
: std::make_unique<BoolLiteral>(left.fOffset, /*value=*/false, &left.type());
|
||||
}
|
||||
if (op == Token::Kind::TK_LOGICALOR) {
|
||||
// (true || expr) -> (true) and (false || expr) -> (expr)
|
||||
return leftVal ? std::make_unique<BoolLiteral>(left.fOffset, /*value=*/true, &left.type())
|
||||
: right.clone();
|
||||
}
|
||||
if (op == Token::Kind::TK_LOGICALXOR && !leftVal) {
|
||||
// (false ^^ expr) -> (expr)
|
||||
return right.clone();
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static std::unique_ptr<Expression> simplify_vector(const Context& context,
|
||||
ErrorReporter& errors,
|
||||
const Expression& left,
|
||||
Token::Kind op,
|
||||
const Expression& right) {
|
||||
SkASSERT(left.type() == right.type());
|
||||
const Type& type = left.type();
|
||||
|
||||
// Handle boolean operations: == !=
|
||||
if (op == Token::Kind::TK_EQEQ || op == Token::Kind::TK_NEQ) {
|
||||
bool equality = (op == Token::Kind::TK_EQEQ);
|
||||
|
||||
switch (left.compareConstant(right)) {
|
||||
case Expression::ComparisonResult::kNotEqual:
|
||||
equality = !equality;
|
||||
[[fallthrough]];
|
||||
|
||||
case Expression::ComparisonResult::kEqual:
|
||||
return std::make_unique<BoolLiteral>(context, left.fOffset, equality);
|
||||
|
||||
case Expression::ComparisonResult::kUnknown:
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
// Handle floating-point arithmetic: + - * /
|
||||
const auto vectorComponentwiseFold = [&](auto foldFn) -> std::unique_ptr<Constructor> {
|
||||
const Type& componentType = type.componentType();
|
||||
ExpressionArray args;
|
||||
args.reserve_back(type.columns());
|
||||
for (int i = 0; i < type.columns(); i++) {
|
||||
T value = foldFn(left.getVecComponent<T>(i), right.getVecComponent<T>(i));
|
||||
args.push_back(std::make_unique<Literal<T>>(left.fOffset, value, &componentType));
|
||||
}
|
||||
return std::make_unique<Constructor>(left.fOffset, &type, std::move(args));
|
||||
};
|
||||
|
||||
const auto isVectorDivisionByZero = [&]() -> bool {
|
||||
for (int i = 0; i < type.columns(); i++) {
|
||||
if (right.getVecComponent<T>(i) == 0) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
switch (op) {
|
||||
case Token::Kind::TK_PLUS: return vectorComponentwiseFold([](T a, T b) { return a + b; });
|
||||
case Token::Kind::TK_MINUS: return vectorComponentwiseFold([](T a, T b) { return a - b; });
|
||||
case Token::Kind::TK_STAR: return vectorComponentwiseFold([](T a, T b) { return a * b; });
|
||||
case Token::Kind::TK_SLASH: {
|
||||
if (isVectorDivisionByZero()) {
|
||||
errors.error(right.fOffset, "division by zero");
|
||||
return nullptr;
|
||||
}
|
||||
return vectorComponentwiseFold([](T a, T b) { return a / b; });
|
||||
}
|
||||
default:
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
std::unique_ptr<Expression> ConstantFolder::Simplify(const Context& context,
|
||||
ErrorReporter& errors,
|
||||
const Expression& left,
|
||||
Token::Kind op,
|
||||
const Expression& right) {
|
||||
// If the left side is a constant boolean literal, the right side does not need to be constant
|
||||
// for short-circuit optimizations to allow the constant to be folded.
|
||||
if (left.is<BoolLiteral>() && !right.isCompileTimeConstant()) {
|
||||
return short_circuit_boolean(left, op, right);
|
||||
}
|
||||
|
||||
if (right.is<BoolLiteral>() && !left.isCompileTimeConstant()) {
|
||||
// There aren't side effects in SkSL within expressions, so (left OP right) is equivalent to
|
||||
// (right OP left) for short-circuit optimizations
|
||||
// TODO: (true || (a=b)) seems to disqualify the above statement. Test this.
|
||||
return short_circuit_boolean(right, op, left);
|
||||
}
|
||||
|
||||
// Other than the short-circuit cases above, constant folding requires both sides to be constant
|
||||
if (!left.isCompileTimeConstant() || !right.isCompileTimeConstant()) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// Perform constant folding on pairs of Booleans.
|
||||
if (left.is<BoolLiteral>() && right.is<BoolLiteral>()) {
|
||||
bool leftVal = left.as<BoolLiteral>().value();
|
||||
bool rightVal = right.as<BoolLiteral>().value();
|
||||
bool result;
|
||||
switch (op) {
|
||||
case Token::Kind::TK_LOGICALAND: result = leftVal && rightVal; break;
|
||||
case Token::Kind::TK_LOGICALOR: result = leftVal || rightVal; break;
|
||||
case Token::Kind::TK_LOGICALXOR: result = leftVal ^ rightVal; break;
|
||||
default: return nullptr;
|
||||
}
|
||||
return std::make_unique<BoolLiteral>(context, left.fOffset, result);
|
||||
}
|
||||
|
||||
// Note that we expressly do not worry about precision and overflow here -- we use the maximum
|
||||
// precision to calculate the results and hope the result makes sense.
|
||||
// TODO: detect and handle integer overflow properly.
|
||||
#define RESULT(t, op) std::make_unique<t ## Literal>(context, left.fOffset, \
|
||||
leftVal op rightVal)
|
||||
#define URESULT(t, op) std::make_unique<t ## Literal>(context, left.fOffset, \
|
||||
(uint64_t) leftVal op \
|
||||
(uint64_t) rightVal)
|
||||
if (left.is<IntLiteral>() && right.is<IntLiteral>()) {
|
||||
SKSL_INT leftVal = left.as<IntLiteral>().value();
|
||||
SKSL_INT rightVal = right.as<IntLiteral>().value();
|
||||
switch (op) {
|
||||
case Token::Kind::TK_PLUS: return URESULT(Int, +);
|
||||
case Token::Kind::TK_MINUS: return URESULT(Int, -);
|
||||
case Token::Kind::TK_STAR: return URESULT(Int, *);
|
||||
case Token::Kind::TK_SLASH:
|
||||
if (leftVal == std::numeric_limits<SKSL_INT>::min() && rightVal == -1) {
|
||||
errors.error(right.fOffset, "arithmetic overflow");
|
||||
return nullptr;
|
||||
}
|
||||
if (!rightVal) {
|
||||
errors.error(right.fOffset, "division by zero");
|
||||
return nullptr;
|
||||
}
|
||||
return RESULT(Int, /);
|
||||
case Token::Kind::TK_PERCENT:
|
||||
if (leftVal == std::numeric_limits<SKSL_INT>::min() && rightVal == -1) {
|
||||
errors.error(right.fOffset, "arithmetic overflow");
|
||||
return nullptr;
|
||||
}
|
||||
if (!rightVal) {
|
||||
errors.error(right.fOffset, "division by zero");
|
||||
return nullptr;
|
||||
}
|
||||
return RESULT(Int, %);
|
||||
case Token::Kind::TK_BITWISEAND: return RESULT(Int, &);
|
||||
case Token::Kind::TK_BITWISEOR: return RESULT(Int, |);
|
||||
case Token::Kind::TK_BITWISEXOR: return RESULT(Int, ^);
|
||||
case Token::Kind::TK_EQEQ: return RESULT(Bool, ==);
|
||||
case Token::Kind::TK_NEQ: return RESULT(Bool, !=);
|
||||
case Token::Kind::TK_GT: return RESULT(Bool, >);
|
||||
case Token::Kind::TK_GTEQ: return RESULT(Bool, >=);
|
||||
case Token::Kind::TK_LT: return RESULT(Bool, <);
|
||||
case Token::Kind::TK_LTEQ: return RESULT(Bool, <=);
|
||||
case Token::Kind::TK_SHL:
|
||||
if (rightVal >= 0 && rightVal <= 31) {
|
||||
return RESULT(Int, <<);
|
||||
}
|
||||
errors.error(right.fOffset, "shift value out of range");
|
||||
return nullptr;
|
||||
case Token::Kind::TK_SHR:
|
||||
if (rightVal >= 0 && rightVal <= 31) {
|
||||
return RESULT(Int, >>);
|
||||
}
|
||||
errors.error(right.fOffset, "shift value out of range");
|
||||
return nullptr;
|
||||
|
||||
default:
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
// Perform constant folding on pairs of floating-point literals.
|
||||
if (left.is<FloatLiteral>() && right.is<FloatLiteral>()) {
|
||||
SKSL_FLOAT leftVal = left.as<FloatLiteral>().value();
|
||||
SKSL_FLOAT rightVal = right.as<FloatLiteral>().value();
|
||||
switch (op) {
|
||||
case Token::Kind::TK_PLUS: return RESULT(Float, +);
|
||||
case Token::Kind::TK_MINUS: return RESULT(Float, -);
|
||||
case Token::Kind::TK_STAR: return RESULT(Float, *);
|
||||
case Token::Kind::TK_SLASH:
|
||||
if (rightVal) {
|
||||
return RESULT(Float, /);
|
||||
}
|
||||
errors.error(right.fOffset, "division by zero");
|
||||
return nullptr;
|
||||
case Token::Kind::TK_EQEQ: return RESULT(Bool, ==);
|
||||
case Token::Kind::TK_NEQ: return RESULT(Bool, !=);
|
||||
case Token::Kind::TK_GT: return RESULT(Bool, >);
|
||||
case Token::Kind::TK_GTEQ: return RESULT(Bool, >=);
|
||||
case Token::Kind::TK_LT: return RESULT(Bool, <);
|
||||
case Token::Kind::TK_LTEQ: return RESULT(Bool, <=);
|
||||
default: return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
// Perform constant folding on pairs of vectors.
|
||||
const Type& leftType = left.type();
|
||||
const Type& rightType = right.type();
|
||||
if (leftType.isVector() && leftType == rightType) {
|
||||
if (leftType.componentType().isFloat()) {
|
||||
return simplify_vector<SKSL_FLOAT>(context, errors, left, op, right);
|
||||
}
|
||||
if (leftType.componentType().isInteger()) {
|
||||
return simplify_vector<SKSL_INT>(context, errors, left, op, right);
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// Perform constant folding on pairs of matrices.
|
||||
if (leftType.isMatrix() && rightType.isMatrix()) {
|
||||
bool equality;
|
||||
switch (op) {
|
||||
case Token::Kind::TK_EQEQ:
|
||||
equality = true;
|
||||
break;
|
||||
case Token::Kind::TK_NEQ:
|
||||
equality = false;
|
||||
break;
|
||||
default:
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
switch (left.compareConstant(right)) {
|
||||
case Expression::ComparisonResult::kNotEqual:
|
||||
equality = !equality;
|
||||
[[fallthrough]];
|
||||
|
||||
case Expression::ComparisonResult::kEqual:
|
||||
return std::make_unique<BoolLiteral>(context, left.fOffset, equality);
|
||||
|
||||
case Expression::ComparisonResult::kUnknown:
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
// We aren't able to constant-fold.
|
||||
#undef RESULT
|
||||
#undef URESULT
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
} // namespace SkSL
|
37
src/sksl/SkSLConstantFolder.h
Normal file
37
src/sksl/SkSLConstantFolder.h
Normal file
@ -0,0 +1,37 @@
|
||||
/*
|
||||
* 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_CONSTANT_FOLDER
|
||||
#define SKSL_CONSTANT_FOLDER
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "src/sksl/SkSLLexer.h"
|
||||
|
||||
namespace SkSL {
|
||||
|
||||
class Context;
|
||||
class ErrorReporter;
|
||||
class Expression;
|
||||
|
||||
/**
|
||||
* Performs constant folding on IR expressions. This simplifies expressions containing
|
||||
* compile-time constants, such as replacing `IntLiteral(2) + IntLiteral(2)` with `IntLiteral(4)`.
|
||||
*/
|
||||
class ConstantFolder {
|
||||
public:
|
||||
/** Simplifies the binary expression `left OP right`. Returns null if it can't be simplified. */
|
||||
static std::unique_ptr<Expression> Simplify(const Context& context,
|
||||
ErrorReporter& errors,
|
||||
const Expression& left,
|
||||
Token::Kind op,
|
||||
const Expression& right);
|
||||
};
|
||||
|
||||
} // namespace SkSL
|
||||
|
||||
#endif // SKSL_CONSTANT_FOLDER
|
@ -15,6 +15,7 @@
|
||||
#include "include/private/SkTArray.h"
|
||||
#include "src/sksl/SkSLAnalysis.h"
|
||||
#include "src/sksl/SkSLCompiler.h"
|
||||
#include "src/sksl/SkSLConstantFolder.h"
|
||||
#include "src/sksl/SkSLParser.h"
|
||||
#include "src/sksl/SkSLUtil.h"
|
||||
#include "src/sksl/ir/SkSLBinaryExpression.h"
|
||||
@ -1771,239 +1772,6 @@ static bool determine_binary_type(const Context& context,
|
||||
return false;
|
||||
}
|
||||
|
||||
static std::unique_ptr<Expression> short_circuit_boolean(const Expression& left,
|
||||
Token::Kind op,
|
||||
const Expression& right) {
|
||||
SkASSERT(left.is<BoolLiteral>());
|
||||
bool leftVal = left.as<BoolLiteral>().value();
|
||||
|
||||
if (op == Token::Kind::TK_LOGICALAND) {
|
||||
// (true && expr) -> (expr) and (false && expr) -> (false)
|
||||
return leftVal ? right.clone()
|
||||
: std::make_unique<BoolLiteral>(left.fOffset, /*value=*/false, &left.type());
|
||||
}
|
||||
if (op == Token::Kind::TK_LOGICALOR) {
|
||||
// (true || expr) -> (true) and (false || expr) -> (expr)
|
||||
return leftVal ? std::make_unique<BoolLiteral>(left.fOffset, /*value=*/true, &left.type())
|
||||
: right.clone();
|
||||
}
|
||||
if (op == Token::Kind::TK_LOGICALXOR && !leftVal) {
|
||||
// (false ^^ expr) -> (expr)
|
||||
return right.clone();
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
std::unique_ptr<Expression> IRGenerator::constantFoldVector(const Expression& left,
|
||||
Token::Kind op,
|
||||
const Expression& right) const {
|
||||
SkASSERT(left.type() == right.type());
|
||||
const Type& type = left.type();
|
||||
|
||||
// Handle boolean operations: == !=
|
||||
if (op == Token::Kind::TK_EQEQ || op == Token::Kind::TK_NEQ) {
|
||||
bool equality = (op == Token::Kind::TK_EQEQ);
|
||||
|
||||
switch (left.compareConstant(right)) {
|
||||
case Expression::ComparisonResult::kNotEqual:
|
||||
equality = !equality;
|
||||
[[fallthrough]];
|
||||
|
||||
case Expression::ComparisonResult::kEqual:
|
||||
return std::make_unique<BoolLiteral>(fContext, left.fOffset, equality);
|
||||
|
||||
case Expression::ComparisonResult::kUnknown:
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
// Handle floating-point arithmetic: + - * /
|
||||
const auto vectorComponentwiseFold = [&](auto foldFn) -> std::unique_ptr<Constructor> {
|
||||
ExpressionArray args;
|
||||
for (int i = 0; i < type.columns(); i++) {
|
||||
T value = foldFn(left.getVecComponent<T>(i), right.getVecComponent<T>(i));
|
||||
args.push_back(std::make_unique<Literal<T>>(fContext, left.fOffset, value));
|
||||
}
|
||||
return std::make_unique<Constructor>(left.fOffset, &type, std::move(args));
|
||||
};
|
||||
|
||||
const auto isVectorDivisionByZero = [&]() -> bool {
|
||||
for (int i = 0; i < type.columns(); i++) {
|
||||
if (right.getVecComponent<T>(i) == 0) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
switch (op) {
|
||||
case Token::Kind::TK_PLUS: return vectorComponentwiseFold([](T a, T b) { return a + b; });
|
||||
case Token::Kind::TK_MINUS: return vectorComponentwiseFold([](T a, T b) { return a - b; });
|
||||
case Token::Kind::TK_STAR: return vectorComponentwiseFold([](T a, T b) { return a * b; });
|
||||
case Token::Kind::TK_SLASH: {
|
||||
if (isVectorDivisionByZero()) {
|
||||
fErrors.error(right.fOffset, "division by zero");
|
||||
return nullptr;
|
||||
}
|
||||
return vectorComponentwiseFold([](T a, T b) { return a / b; });
|
||||
}
|
||||
default:
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
std::unique_ptr<Expression> IRGenerator::constantFold(const Expression& left,
|
||||
Token::Kind op,
|
||||
const Expression& right) const {
|
||||
// If the left side is a constant boolean literal, the right side does not need to be constant
|
||||
// for short circuit optimizations to allow the constant to be folded.
|
||||
if (left.is<BoolLiteral>() && !right.isCompileTimeConstant()) {
|
||||
return short_circuit_boolean(left, op, right);
|
||||
} else if (right.is<BoolLiteral>() && !left.isCompileTimeConstant()) {
|
||||
// There aren't side effects in SkSL within expressions, so (left OP right) is equivalent to
|
||||
// (right OP left) for short-circuit optimizations
|
||||
return short_circuit_boolean(right, op, left);
|
||||
}
|
||||
|
||||
// Other than the short-circuit cases above, constant folding requires both sides to be constant
|
||||
if (!left.isCompileTimeConstant() || !right.isCompileTimeConstant()) {
|
||||
return nullptr;
|
||||
}
|
||||
// Note that we expressly do not worry about precision and overflow here -- we use the maximum
|
||||
// precision to calculate the results and hope the result makes sense. The plan is to move the
|
||||
// Skia caps into SkSL, so we have access to all of them including the precisions of the various
|
||||
// types, which will let us be more intelligent about this.
|
||||
if (left.is<BoolLiteral>() && right.is<BoolLiteral>()) {
|
||||
bool leftVal = left.as<BoolLiteral>().value();
|
||||
bool rightVal = right.as<BoolLiteral>().value();
|
||||
bool result;
|
||||
switch (op) {
|
||||
case Token::Kind::TK_LOGICALAND: result = leftVal && rightVal; break;
|
||||
case Token::Kind::TK_LOGICALOR: result = leftVal || rightVal; break;
|
||||
case Token::Kind::TK_LOGICALXOR: result = leftVal ^ rightVal; break;
|
||||
default: return nullptr;
|
||||
}
|
||||
return std::make_unique<BoolLiteral>(fContext, left.fOffset, result);
|
||||
}
|
||||
#define RESULT(t, op) std::make_unique<t ## Literal>(fContext, left.fOffset, \
|
||||
leftVal op rightVal)
|
||||
#define URESULT(t, op) std::make_unique<t ## Literal>(fContext, left.fOffset, \
|
||||
(uint64_t) leftVal op \
|
||||
(uint64_t) rightVal)
|
||||
if (left.is<IntLiteral>() && right.is<IntLiteral>()) {
|
||||
SKSL_INT leftVal = left.as<IntLiteral>().value();
|
||||
SKSL_INT rightVal = right.as<IntLiteral>().value();
|
||||
switch (op) {
|
||||
case Token::Kind::TK_PLUS: return URESULT(Int, +);
|
||||
case Token::Kind::TK_MINUS: return URESULT(Int, -);
|
||||
case Token::Kind::TK_STAR: return URESULT(Int, *);
|
||||
case Token::Kind::TK_SLASH:
|
||||
if (leftVal == std::numeric_limits<SKSL_INT>::min() && rightVal == -1) {
|
||||
fErrors.error(right.fOffset, "arithmetic overflow");
|
||||
return nullptr;
|
||||
}
|
||||
if (!rightVal) {
|
||||
fErrors.error(right.fOffset, "division by zero");
|
||||
return nullptr;
|
||||
}
|
||||
return RESULT(Int, /);
|
||||
case Token::Kind::TK_PERCENT:
|
||||
if (leftVal == std::numeric_limits<SKSL_INT>::min() && rightVal == -1) {
|
||||
fErrors.error(right.fOffset, "arithmetic overflow");
|
||||
return nullptr;
|
||||
}
|
||||
if (!rightVal) {
|
||||
fErrors.error(right.fOffset, "division by zero");
|
||||
return nullptr;
|
||||
}
|
||||
return RESULT(Int, %);
|
||||
case Token::Kind::TK_BITWISEAND: return RESULT(Int, &);
|
||||
case Token::Kind::TK_BITWISEOR: return RESULT(Int, |);
|
||||
case Token::Kind::TK_BITWISEXOR: return RESULT(Int, ^);
|
||||
case Token::Kind::TK_EQEQ: return RESULT(Bool, ==);
|
||||
case Token::Kind::TK_NEQ: return RESULT(Bool, !=);
|
||||
case Token::Kind::TK_GT: return RESULT(Bool, >);
|
||||
case Token::Kind::TK_GTEQ: return RESULT(Bool, >=);
|
||||
case Token::Kind::TK_LT: return RESULT(Bool, <);
|
||||
case Token::Kind::TK_LTEQ: return RESULT(Bool, <=);
|
||||
case Token::Kind::TK_SHL:
|
||||
if (rightVal >= 0 && rightVal <= 31) {
|
||||
return RESULT(Int, <<);
|
||||
}
|
||||
fErrors.error(right.fOffset, "shift value out of range");
|
||||
return nullptr;
|
||||
case Token::Kind::TK_SHR:
|
||||
if (rightVal >= 0 && rightVal <= 31) {
|
||||
return RESULT(Int, >>);
|
||||
}
|
||||
fErrors.error(right.fOffset, "shift value out of range");
|
||||
return nullptr;
|
||||
|
||||
default:
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
if (left.is<FloatLiteral>() && right.is<FloatLiteral>()) {
|
||||
SKSL_FLOAT leftVal = left.as<FloatLiteral>().value();
|
||||
SKSL_FLOAT rightVal = right.as<FloatLiteral>().value();
|
||||
switch (op) {
|
||||
case Token::Kind::TK_PLUS: return RESULT(Float, +);
|
||||
case Token::Kind::TK_MINUS: return RESULT(Float, -);
|
||||
case Token::Kind::TK_STAR: return RESULT(Float, *);
|
||||
case Token::Kind::TK_SLASH:
|
||||
if (rightVal) {
|
||||
return RESULT(Float, /);
|
||||
}
|
||||
fErrors.error(right.fOffset, "division by zero");
|
||||
return nullptr;
|
||||
case Token::Kind::TK_EQEQ: return RESULT(Bool, ==);
|
||||
case Token::Kind::TK_NEQ: return RESULT(Bool, !=);
|
||||
case Token::Kind::TK_GT: return RESULT(Bool, >);
|
||||
case Token::Kind::TK_GTEQ: return RESULT(Bool, >=);
|
||||
case Token::Kind::TK_LT: return RESULT(Bool, <);
|
||||
case Token::Kind::TK_LTEQ: return RESULT(Bool, <=);
|
||||
default: return nullptr;
|
||||
}
|
||||
}
|
||||
const Type& leftType = left.type();
|
||||
const Type& rightType = right.type();
|
||||
if (leftType.isVector() && leftType == rightType) {
|
||||
if (leftType.componentType().isFloat()) {
|
||||
return constantFoldVector<SKSL_FLOAT>(left, op, right);
|
||||
} else if (leftType.componentType().isInteger()) {
|
||||
return constantFoldVector<SKSL_INT>(left, op, right);
|
||||
}
|
||||
}
|
||||
if (leftType.isMatrix() && rightType.isMatrix()) {
|
||||
bool equality;
|
||||
switch (op) {
|
||||
case Token::Kind::TK_EQEQ:
|
||||
equality = true;
|
||||
break;
|
||||
case Token::Kind::TK_NEQ:
|
||||
equality = false;
|
||||
break;
|
||||
default:
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
switch (left.compareConstant(right)) {
|
||||
case Expression::ComparisonResult::kNotEqual:
|
||||
equality = !equality;
|
||||
[[fallthrough]];
|
||||
|
||||
case Expression::ComparisonResult::kEqual:
|
||||
return std::make_unique<BoolLiteral>(fContext, left.fOffset, equality);
|
||||
|
||||
case Expression::ComparisonResult::kUnknown:
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
#undef RESULT
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
std::unique_ptr<Expression> IRGenerator::convertBinaryExpression(const ASTNode& expression) {
|
||||
SkASSERT(expression.fKind == ASTNode::Kind::kBinary);
|
||||
auto iter = expression.begin();
|
||||
@ -2071,7 +1839,8 @@ std::unique_ptr<Expression> IRGenerator::convertBinaryExpression(
|
||||
if (!left || !right) {
|
||||
return nullptr;
|
||||
}
|
||||
std::unique_ptr<Expression> result = this->constantFold(*left, op, *right);
|
||||
std::unique_ptr<Expression> result = ConstantFolder::Simplify(fContext, fErrors,
|
||||
*left, op, *right);
|
||||
if (!result) {
|
||||
result = std::make_unique<BinaryExpression>(offset, std::move(left), op, std::move(right),
|
||||
resultType);
|
||||
|
@ -126,21 +126,14 @@ public:
|
||||
size_t length,
|
||||
const std::vector<std::unique_ptr<ExternalFunction>>* externalFunctions);
|
||||
|
||||
/**
|
||||
* If both operands are compile-time constants and can be folded, returns an expression
|
||||
* representing the folded value. Otherwise, returns null. Note that unlike most other functions
|
||||
* here, null does not represent a compilation error.
|
||||
*/
|
||||
std::unique_ptr<Expression> constantFold(const Expression& left,
|
||||
Token::Kind op,
|
||||
const Expression& right) const;
|
||||
|
||||
// both of these functions return null and report an error if the setting does not exist
|
||||
const Type* typeForSetting(int offset, String name) const;
|
||||
std::unique_ptr<Expression> valueForSetting(int offset, String name) const;
|
||||
|
||||
const Program::Settings* settings() const { return fSettings; }
|
||||
|
||||
ErrorReporter& errorReporter() const { return fErrors; }
|
||||
|
||||
std::shared_ptr<SymbolTable>& symbolTable() {
|
||||
return fSymbolTable;
|
||||
}
|
||||
@ -179,10 +172,6 @@ private:
|
||||
const ExpressionArray& arguments);
|
||||
std::unique_ptr<Expression> coerce(std::unique_ptr<Expression> expr, const Type& type);
|
||||
CoercionCost coercionCost(const Expression& expr, const Type& type);
|
||||
template <typename T>
|
||||
std::unique_ptr<Expression> constantFoldVector(const Expression& left,
|
||||
Token::Kind op,
|
||||
const Expression& right) const;
|
||||
std::unique_ptr<Expression> convertBinaryExpression(std::unique_ptr<Expression> left,
|
||||
Token::Kind op,
|
||||
std::unique_ptr<Expression> right);
|
||||
|
@ -9,6 +9,7 @@
|
||||
#define SKSL_BINARYEXPRESSION
|
||||
|
||||
#include "src/sksl/SkSLCompiler.h"
|
||||
#include "src/sksl/SkSLConstantFolder.h"
|
||||
#include "src/sksl/SkSLIRGenerator.h"
|
||||
#include "src/sksl/SkSLLexer.h"
|
||||
#include "src/sksl/ir/SkSLExpression.h"
|
||||
@ -84,9 +85,8 @@ public:
|
||||
|
||||
std::unique_ptr<Expression> constantPropagate(const IRGenerator& irGenerator,
|
||||
const DefinitionMap& definitions) override {
|
||||
return irGenerator.constantFold(*this->left(),
|
||||
this->getOperator(),
|
||||
*this->right());
|
||||
return ConstantFolder::Simplify(irGenerator.fContext, irGenerator.errorReporter(),
|
||||
*this->left(), this->getOperator(), *this->right());
|
||||
}
|
||||
|
||||
bool hasProperty(Property property) const override {
|
||||
|
Loading…
Reference in New Issue
Block a user