John Stiles | e2aec43 | 2021-03-01 09:27:48 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2021 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/SkSLAnalysis.h" |
| 9 | #include "src/sksl/SkSLConstantFolder.h" |
| 10 | #include "src/sksl/ir/SkSLBinaryExpression.h" |
John Stiles | 9e13fe8 | 2021-03-23 18:45:25 -0400 | [diff] [blame] | 11 | #include "src/sksl/ir/SkSLIndexExpression.h" |
John Stiles | 7591d4b | 2021-09-13 13:32:06 -0400 | [diff] [blame] | 12 | #include "src/sksl/ir/SkSLLiteral.h" |
John Stiles | 9e13fe8 | 2021-03-23 18:45:25 -0400 | [diff] [blame] | 13 | #include "src/sksl/ir/SkSLSetting.h" |
| 14 | #include "src/sksl/ir/SkSLSwizzle.h" |
| 15 | #include "src/sksl/ir/SkSLTernaryExpression.h" |
John Stiles | e2aec43 | 2021-03-01 09:27:48 -0500 | [diff] [blame] | 16 | #include "src/sksl/ir/SkSLType.h" |
| 17 | |
| 18 | namespace SkSL { |
| 19 | |
John Stiles | 70cd598 | 2021-03-25 11:09:53 -0400 | [diff] [blame] | 20 | static bool is_low_precision_matrix_vector_multiply(const Expression& left, |
| 21 | const Operator& op, |
| 22 | const Expression& right, |
| 23 | const Type& resultType) { |
| 24 | return !resultType.highPrecision() && |
| 25 | op.kind() == Token::Kind::TK_STAR && |
| 26 | left.type().isMatrix() && |
| 27 | right.type().isVector() && |
| 28 | left.type().rows() == right.type().columns() && |
| 29 | Analysis::IsTrivialExpression(left) && |
| 30 | Analysis::IsTrivialExpression(right); |
| 31 | } |
| 32 | |
| 33 | static std::unique_ptr<Expression> rewrite_matrix_vector_multiply(const Context& context, |
| 34 | const Expression& left, |
| 35 | const Operator& op, |
| 36 | const Expression& right, |
| 37 | const Type& resultType) { |
| 38 | // Rewrite m33 * v3 as (m[0] * v[0] + m[1] * v[1] + m[2] * v[2]) |
| 39 | std::unique_ptr<Expression> sum; |
| 40 | for (int n = 0; n < left.type().rows(); ++n) { |
| 41 | // Get mat[N] with an index expression. |
| 42 | std::unique_ptr<Expression> matN = IndexExpression::Make( |
Ethan Nicholas | 89cfde1 | 2021-09-27 11:20:34 -0400 | [diff] [blame^] | 43 | context, left.clone(), Literal::MakeInt(context, left.fLine, n)); |
John Stiles | 70cd598 | 2021-03-25 11:09:53 -0400 | [diff] [blame] | 44 | // Get vec[N] with a swizzle expression. |
| 45 | std::unique_ptr<Expression> vecN = Swizzle::Make( |
| 46 | context, right.clone(), ComponentArray{(SkSL::SwizzleComponent::Type)n}); |
| 47 | // Multiply them together. |
| 48 | const Type* matNType = &matN->type(); |
| 49 | std::unique_ptr<Expression> product = |
| 50 | BinaryExpression::Make(context, std::move(matN), op, std::move(vecN), matNType); |
| 51 | // Sum all the components together. |
| 52 | if (!sum) { |
| 53 | sum = std::move(product); |
| 54 | } else { |
| 55 | sum = BinaryExpression::Make(context, |
| 56 | std::move(sum), |
| 57 | Operator(Token::Kind::TK_PLUS), |
| 58 | std::move(product), |
| 59 | matNType); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | return sum; |
| 64 | } |
| 65 | |
John Stiles | 23521a8 | 2021-03-02 17:02:51 -0500 | [diff] [blame] | 66 | std::unique_ptr<Expression> BinaryExpression::Convert(const Context& context, |
| 67 | std::unique_ptr<Expression> left, |
| 68 | Operator op, |
| 69 | std::unique_ptr<Expression> right) { |
John Stiles | e2aec43 | 2021-03-01 09:27:48 -0500 | [diff] [blame] | 70 | if (!left || !right) { |
| 71 | return nullptr; |
| 72 | } |
Ethan Nicholas | 89cfde1 | 2021-09-27 11:20:34 -0400 | [diff] [blame^] | 73 | const int line = left->fLine; |
John Stiles | e2aec43 | 2021-03-01 09:27:48 -0500 | [diff] [blame] | 74 | |
John Stiles | 7591d4b | 2021-09-13 13:32:06 -0400 | [diff] [blame] | 75 | const Type* rawLeftType = (left->isIntLiteral() && right->type().isInteger()) |
| 76 | ? &right->type() |
| 77 | : &left->type(); |
| 78 | const Type* rawRightType = (right->isIntLiteral() && left->type().isInteger()) |
| 79 | ? &left->type() |
| 80 | : &right->type(); |
John Stiles | e2aec43 | 2021-03-01 09:27:48 -0500 | [diff] [blame] | 81 | |
John Stiles | e2aec43 | 2021-03-01 09:27:48 -0500 | [diff] [blame] | 82 | bool isAssignment = op.isAssignment(); |
| 83 | if (isAssignment && |
John Stiles | bb8cf58 | 2021-08-26 23:34:59 -0400 | [diff] [blame] | 84 | !Analysis::UpdateVariableRefKind(left.get(), |
| 85 | op.kind() != Token::Kind::TK_EQ |
| 86 | ? VariableReference::RefKind::kReadWrite |
| 87 | : VariableReference::RefKind::kWrite, |
| 88 | context.fErrors)) { |
John Stiles | e2aec43 | 2021-03-01 09:27:48 -0500 | [diff] [blame] | 89 | return nullptr; |
| 90 | } |
| 91 | |
| 92 | const Type* leftType; |
| 93 | const Type* rightType; |
| 94 | const Type* resultType; |
| 95 | if (!op.determineBinaryType(context, *rawLeftType, *rawRightType, |
| 96 | &leftType, &rightType, &resultType)) { |
Ethan Nicholas | 89cfde1 | 2021-09-27 11:20:34 -0400 | [diff] [blame^] | 97 | context.fErrors->error(line, String("type mismatch: '") + op.operatorName() + |
| 98 | "' cannot operate on '" + left->type().displayName() + |
| 99 | "', '" + right->type().displayName() + "'"); |
John Stiles | e2aec43 | 2021-03-01 09:27:48 -0500 | [diff] [blame] | 100 | return nullptr; |
| 101 | } |
John Stiles | 23521a8 | 2021-03-02 17:02:51 -0500 | [diff] [blame] | 102 | |
John Stiles | e2aec43 | 2021-03-01 09:27:48 -0500 | [diff] [blame] | 103 | if (isAssignment && leftType->componentType().isOpaque()) { |
Ethan Nicholas | 89cfde1 | 2021-09-27 11:20:34 -0400 | [diff] [blame^] | 104 | context.fErrors->error(line, "assignments to opaque type '" + left->type().displayName() + |
| 105 | "' are not permitted"); |
John Stiles | e2aec43 | 2021-03-01 09:27:48 -0500 | [diff] [blame] | 106 | return nullptr; |
| 107 | } |
John Stiles | 23521a8 | 2021-03-02 17:02:51 -0500 | [diff] [blame] | 108 | if (context.fConfig->strictES2Mode()) { |
| 109 | if (!op.isAllowedInStrictES2Mode()) { |
Ethan Nicholas | 89cfde1 | 2021-09-27 11:20:34 -0400 | [diff] [blame^] | 110 | context.fErrors->error(line, String("operator '") + op.operatorName() + |
| 111 | "' is not allowed"); |
John Stiles | 23521a8 | 2021-03-02 17:02:51 -0500 | [diff] [blame] | 112 | return nullptr; |
| 113 | } |
| 114 | if (leftType->isOrContainsArray()) { |
| 115 | // Most operators are already rejected on arrays, but GLSL ES 1.0 is very explicit that |
| 116 | // the *only* operator allowed on arrays is subscripting (and the rules against |
| 117 | // assignment, comparison, and even sequence apply to structs containing arrays as well) |
Ethan Nicholas | 89cfde1 | 2021-09-27 11:20:34 -0400 | [diff] [blame^] | 118 | context.fErrors->error(line, String("operator '") + op.operatorName() + "' can not " |
| 119 | "operate on arrays (or structs containing arrays)"); |
John Stiles | 23521a8 | 2021-03-02 17:02:51 -0500 | [diff] [blame] | 120 | return nullptr; |
| 121 | } |
| 122 | } |
John Stiles | e2aec43 | 2021-03-01 09:27:48 -0500 | [diff] [blame] | 123 | |
| 124 | left = leftType->coerceExpression(std::move(left), context); |
| 125 | right = rightType->coerceExpression(std::move(right), context); |
| 126 | if (!left || !right) { |
| 127 | return nullptr; |
| 128 | } |
| 129 | |
John Stiles | 23521a8 | 2021-03-02 17:02:51 -0500 | [diff] [blame] | 130 | return BinaryExpression::Make(context, std::move(left), op, std::move(right), resultType); |
| 131 | } |
| 132 | |
| 133 | std::unique_ptr<Expression> BinaryExpression::Make(const Context& context, |
| 134 | std::unique_ptr<Expression> left, |
| 135 | Operator op, |
| 136 | std::unique_ptr<Expression> right) { |
| 137 | // Determine the result type of the binary expression. |
| 138 | const Type* leftType; |
| 139 | const Type* rightType; |
| 140 | const Type* resultType; |
| 141 | SkAssertResult(op.determineBinaryType(context, left->type(), right->type(), |
| 142 | &leftType, &rightType, &resultType)); |
| 143 | |
| 144 | return BinaryExpression::Make(context, std::move(left), op, std::move(right), resultType); |
| 145 | } |
| 146 | |
| 147 | std::unique_ptr<Expression> BinaryExpression::Make(const Context& context, |
| 148 | std::unique_ptr<Expression> left, |
| 149 | Operator op, |
| 150 | std::unique_ptr<Expression> right, |
| 151 | const Type* resultType) { |
| 152 | // We should have detected non-ES2 compliant behavior in Convert. |
| 153 | SkASSERT(!context.fConfig->strictES2Mode() || op.isAllowedInStrictES2Mode()); |
| 154 | SkASSERT(!context.fConfig->strictES2Mode() || !left->type().isOrContainsArray()); |
| 155 | |
| 156 | // We should have detected non-assignable assignment expressions in Convert. |
| 157 | SkASSERT(!op.isAssignment() || Analysis::IsAssignable(*left)); |
| 158 | SkASSERT(!op.isAssignment() || !left->type().componentType().isOpaque()); |
| 159 | |
John Stiles | b0d93cc | 2021-06-01 11:07:53 -0400 | [diff] [blame] | 160 | // For simple assignments, detect and report out-of-range literal values. |
| 161 | if (op.kind() == Token::Kind::TK_EQ) { |
| 162 | left->type().checkForOutOfRangeLiteral(context, *right); |
| 163 | } |
| 164 | |
John Stiles | 652e863 | 2021-04-11 09:25:59 -0400 | [diff] [blame] | 165 | // Perform constant-folding on the expression. |
Ethan Nicholas | 89cfde1 | 2021-09-27 11:20:34 -0400 | [diff] [blame^] | 166 | const int line = left->fLine; |
| 167 | if (std::unique_ptr<Expression> result = ConstantFolder::Simplify(context, line, *left, |
John Stiles | 652e863 | 2021-04-11 09:25:59 -0400 | [diff] [blame] | 168 | op, *right, *resultType)) { |
| 169 | return result; |
John Stiles | e2aec43 | 2021-03-01 09:27:48 -0500 | [diff] [blame] | 170 | } |
John Stiles | 23521a8 | 2021-03-02 17:02:51 -0500 | [diff] [blame] | 171 | |
John Stiles | 70cd598 | 2021-03-25 11:09:53 -0400 | [diff] [blame] | 172 | if (context.fConfig->fSettings.fOptimize) { |
| 173 | // When sk_Caps.rewriteMatrixVectorMultiply is set, we rewrite medium-precision |
| 174 | // matrix * vector multiplication as: |
| 175 | // (sk_Caps.rewriteMatrixVectorMultiply ? (mat[0]*vec[0] + ... + mat[N]*vec[N]) |
| 176 | // : mat * vec) |
| 177 | if (is_low_precision_matrix_vector_multiply(*left, op, *right, *resultType)) { |
| 178 | // Look up `sk_Caps.rewriteMatrixVectorMultiply`. |
Ethan Nicholas | 89cfde1 | 2021-09-27 11:20:34 -0400 | [diff] [blame^] | 179 | auto caps = Setting::Convert(context, line, "rewriteMatrixVectorMultiply"); |
John Stiles | 9e13fe8 | 2021-03-23 18:45:25 -0400 | [diff] [blame] | 180 | |
John Stiles | 7591d4b | 2021-09-13 13:32:06 -0400 | [diff] [blame] | 181 | bool capsBitIsTrue = caps->isBoolLiteral() && caps->as<Literal>().boolValue(); |
| 182 | if (capsBitIsTrue || !caps->isBoolLiteral()) { |
John Stiles | 70cd598 | 2021-03-25 11:09:53 -0400 | [diff] [blame] | 183 | // Rewrite the multiplication as a sum of vector-scalar products. |
| 184 | std::unique_ptr<Expression> rewrite = |
| 185 | rewrite_matrix_vector_multiply(context, *left, op, *right, *resultType); |
| 186 | |
| 187 | // If we know the caps bit is true, return the rewritten expression directly. |
| 188 | if (capsBitIsTrue) { |
| 189 | return rewrite; |
John Stiles | 9e13fe8 | 2021-03-23 18:45:25 -0400 | [diff] [blame] | 190 | } |
John Stiles | 9e13fe8 | 2021-03-23 18:45:25 -0400 | [diff] [blame] | 191 | |
John Stiles | 70cd598 | 2021-03-25 11:09:53 -0400 | [diff] [blame] | 192 | // Return a ternary expression: |
| 193 | // sk_Caps.rewriteMatrixVectorMultiply ? (rewrite) : (mat * vec) |
| 194 | return TernaryExpression::Make( |
| 195 | context, |
| 196 | std::move(caps), |
| 197 | std::move(rewrite), |
Ethan Nicholas | 89cfde1 | 2021-09-27 11:20:34 -0400 | [diff] [blame^] | 198 | std::make_unique<BinaryExpression>(line, std::move(left), op, |
John Stiles | 70cd598 | 2021-03-25 11:09:53 -0400 | [diff] [blame] | 199 | std::move(right), resultType)); |
John Stiles | 9e13fe8 | 2021-03-23 18:45:25 -0400 | [diff] [blame] | 200 | } |
John Stiles | 9e13fe8 | 2021-03-23 18:45:25 -0400 | [diff] [blame] | 201 | } |
| 202 | } |
| 203 | |
Ethan Nicholas | 89cfde1 | 2021-09-27 11:20:34 -0400 | [diff] [blame^] | 204 | return std::make_unique<BinaryExpression>(line, std::move(left), op, |
John Stiles | 9e13fe8 | 2021-03-23 18:45:25 -0400 | [diff] [blame] | 205 | std::move(right), resultType); |
John Stiles | e2aec43 | 2021-03-01 09:27:48 -0500 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | bool BinaryExpression::CheckRef(const Expression& expr) { |
| 209 | switch (expr.kind()) { |
| 210 | case Expression::Kind::kFieldAccess: |
| 211 | return CheckRef(*expr.as<FieldAccess>().base()); |
| 212 | |
| 213 | case Expression::Kind::kIndex: |
| 214 | return CheckRef(*expr.as<IndexExpression>().base()); |
| 215 | |
| 216 | case Expression::Kind::kSwizzle: |
| 217 | return CheckRef(*expr.as<Swizzle>().base()); |
| 218 | |
| 219 | case Expression::Kind::kTernary: { |
| 220 | const TernaryExpression& t = expr.as<TernaryExpression>(); |
| 221 | return CheckRef(*t.ifTrue()) && CheckRef(*t.ifFalse()); |
| 222 | } |
| 223 | case Expression::Kind::kVariableReference: { |
| 224 | const VariableReference& ref = expr.as<VariableReference>(); |
| 225 | return ref.refKind() == VariableRefKind::kWrite || |
| 226 | ref.refKind() == VariableRefKind::kReadWrite; |
| 227 | } |
| 228 | default: |
| 229 | return false; |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | std::unique_ptr<Expression> BinaryExpression::clone() const { |
Ethan Nicholas | 89cfde1 | 2021-09-27 11:20:34 -0400 | [diff] [blame^] | 234 | return std::make_unique<BinaryExpression>(fLine, |
John Stiles | e2aec43 | 2021-03-01 09:27:48 -0500 | [diff] [blame] | 235 | this->left()->clone(), |
| 236 | this->getOperator(), |
| 237 | this->right()->clone(), |
| 238 | &this->type()); |
| 239 | } |
| 240 | |
| 241 | String BinaryExpression::description() const { |
| 242 | return "(" + this->left()->description() + |
| 243 | " " + this->getOperator().operatorName() + |
| 244 | " " + this->right()->description() + ")"; |
| 245 | } |
| 246 | |
| 247 | } // namespace SkSL |