Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -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/SkSLAnalysis.h" |
| 9 | |
Ethan Nicholas | daed259 | 2021-03-04 14:30:25 -0500 | [diff] [blame] | 10 | #include "include/private/SkSLModifiers.h" |
Ethan Nicholas | 24c1772 | 2021-03-09 13:10:59 -0500 | [diff] [blame] | 11 | #include "include/private/SkSLProgramElement.h" |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 12 | #include "include/private/SkSLSampleUsage.h" |
Ethan Nicholas | 24c1772 | 2021-03-09 13:10:59 -0500 | [diff] [blame] | 13 | #include "include/private/SkSLStatement.h" |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 14 | #include "src/sksl/SkSLCompiler.h" |
John Stiles | dce4d3e | 2020-09-25 14:35:13 -0400 | [diff] [blame] | 15 | #include "src/sksl/SkSLErrorReporter.h" |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 16 | #include "src/sksl/ir/SkSLExpression.h" |
| 17 | #include "src/sksl/ir/SkSLProgram.h" |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 18 | |
| 19 | // ProgramElements |
| 20 | #include "src/sksl/ir/SkSLEnum.h" |
| 21 | #include "src/sksl/ir/SkSLExtension.h" |
| 22 | #include "src/sksl/ir/SkSLFunctionDefinition.h" |
| 23 | #include "src/sksl/ir/SkSLInterfaceBlock.h" |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 24 | #include "src/sksl/ir/SkSLSection.h" |
| 25 | #include "src/sksl/ir/SkSLVarDeclarations.h" |
| 26 | |
| 27 | // Statements |
| 28 | #include "src/sksl/ir/SkSLBlock.h" |
| 29 | #include "src/sksl/ir/SkSLBreakStatement.h" |
| 30 | #include "src/sksl/ir/SkSLContinueStatement.h" |
| 31 | #include "src/sksl/ir/SkSLDiscardStatement.h" |
| 32 | #include "src/sksl/ir/SkSLDoStatement.h" |
| 33 | #include "src/sksl/ir/SkSLExpressionStatement.h" |
| 34 | #include "src/sksl/ir/SkSLForStatement.h" |
| 35 | #include "src/sksl/ir/SkSLIfStatement.h" |
| 36 | #include "src/sksl/ir/SkSLNop.h" |
| 37 | #include "src/sksl/ir/SkSLReturnStatement.h" |
| 38 | #include "src/sksl/ir/SkSLSwitchStatement.h" |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 39 | |
| 40 | // Expressions |
| 41 | #include "src/sksl/ir/SkSLBinaryExpression.h" |
| 42 | #include "src/sksl/ir/SkSLBoolLiteral.h" |
| 43 | #include "src/sksl/ir/SkSLConstructor.h" |
John Stiles | e118278 | 2021-03-30 22:09:37 -0400 | [diff] [blame] | 44 | #include "src/sksl/ir/SkSLConstructorDiagonalMatrix.h" |
John Stiles | 5abb9e1 | 2021-04-06 13:47:19 -0400 | [diff] [blame] | 45 | #include "src/sksl/ir/SkSLConstructorMatrixResize.h" |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 46 | #include "src/sksl/ir/SkSLExternalFunctionCall.h" |
Brian Osman | be0b3b7 | 2021-01-06 14:27:35 -0500 | [diff] [blame] | 47 | #include "src/sksl/ir/SkSLExternalFunctionReference.h" |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 48 | #include "src/sksl/ir/SkSLFieldAccess.h" |
| 49 | #include "src/sksl/ir/SkSLFloatLiteral.h" |
| 50 | #include "src/sksl/ir/SkSLFunctionCall.h" |
| 51 | #include "src/sksl/ir/SkSLFunctionReference.h" |
| 52 | #include "src/sksl/ir/SkSLIndexExpression.h" |
John Stiles | 98c1f82 | 2020-09-09 14:18:53 -0400 | [diff] [blame] | 53 | #include "src/sksl/ir/SkSLInlineMarker.h" |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 54 | #include "src/sksl/ir/SkSLIntLiteral.h" |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 55 | #include "src/sksl/ir/SkSLPostfixExpression.h" |
| 56 | #include "src/sksl/ir/SkSLPrefixExpression.h" |
| 57 | #include "src/sksl/ir/SkSLSetting.h" |
| 58 | #include "src/sksl/ir/SkSLSwizzle.h" |
| 59 | #include "src/sksl/ir/SkSLTernaryExpression.h" |
| 60 | #include "src/sksl/ir/SkSLTypeReference.h" |
| 61 | #include "src/sksl/ir/SkSLVariableReference.h" |
| 62 | |
| 63 | namespace SkSL { |
| 64 | |
| 65 | namespace { |
| 66 | |
| 67 | static bool is_sample_call_to_fp(const FunctionCall& fc, const Variable& fp) { |
Ethan Nicholas | 0dec992 | 2020-10-05 15:51:52 -0400 | [diff] [blame] | 68 | const FunctionDeclaration& f = fc.function(); |
Ethan Nicholas | ed84b73 | 2020-10-08 11:45:44 -0400 | [diff] [blame] | 69 | return f.isBuiltin() && f.name() == "sample" && fc.arguments().size() >= 1 && |
Ethan Nicholas | 0dec992 | 2020-10-05 15:51:52 -0400 | [diff] [blame] | 70 | fc.arguments()[0]->is<VariableReference>() && |
Ethan Nicholas | 7868692 | 2020-10-08 06:46:27 -0400 | [diff] [blame] | 71 | fc.arguments()[0]->as<VariableReference>().variable() == &fp; |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 72 | } |
| 73 | |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 74 | // Visitor that determines the merged SampleUsage for a given child 'fp' in the program. |
| 75 | class MergeSampleUsageVisitor : public ProgramVisitor { |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 76 | public: |
Brian Osman | cc3d2d2 | 2021-04-16 14:53:24 -0400 | [diff] [blame] | 77 | MergeSampleUsageVisitor(const Context& context, const Variable& fp, bool writesToSampleCoords) |
| 78 | : fContext(context), fFP(fp), fWritesToSampleCoords(writesToSampleCoords) {} |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 79 | |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 80 | SampleUsage visit(const Program& program) { |
| 81 | fUsage = SampleUsage(); // reset to none |
John Stiles | d7ab450 | 2020-09-24 22:41:00 -0400 | [diff] [blame] | 82 | INHERITED::visit(program); |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 83 | return fUsage; |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | protected: |
John Stiles | 933abe3 | 2020-08-28 11:58:40 -0400 | [diff] [blame] | 87 | const Context& fContext; |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 88 | const Variable& fFP; |
Brian Osman | cc3d2d2 | 2021-04-16 14:53:24 -0400 | [diff] [blame] | 89 | const bool fWritesToSampleCoords; |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 90 | SampleUsage fUsage; |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 91 | |
| 92 | bool visitExpression(const Expression& e) override { |
Brian Osman | 0962268 | 2021-04-15 14:43:19 -0400 | [diff] [blame] | 93 | // Looking for sample(fp, ...) |
| 94 | if (e.is<FunctionCall>()) { |
John Stiles | 403a363 | 2020-08-20 12:11:48 -0400 | [diff] [blame] | 95 | const FunctionCall& fc = e.as<FunctionCall>(); |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 96 | if (is_sample_call_to_fp(fc, fFP)) { |
| 97 | // Determine the type of call at this site, and merge it with the accumulated state |
Brian Osman | 0962268 | 2021-04-15 14:43:19 -0400 | [diff] [blame] | 98 | if (fc.arguments().size() >= 2) { |
| 99 | const Expression* coords = fc.arguments()[1].get(); |
| 100 | if (coords->type() == *fContext.fTypes.fFloat2) { |
Brian Osman | cc3d2d2 | 2021-04-16 14:53:24 -0400 | [diff] [blame] | 101 | // If the coords are a direct reference to the program's sample-coords, |
| 102 | // and those coords are never modified, we can conservatively turn this |
| 103 | // into PassThrough sampling. In all other cases, we consider it Explicit. |
| 104 | if (!fWritesToSampleCoords && coords->is<VariableReference>() && |
| 105 | coords->as<VariableReference>() |
| 106 | .variable() |
| 107 | ->modifiers() |
| 108 | .fLayout.fBuiltin == SK_MAIN_COORDS_BUILTIN) { |
| 109 | fUsage.merge(SampleUsage::PassThrough()); |
| 110 | } else { |
| 111 | fUsage.merge(SampleUsage::Explicit()); |
| 112 | } |
Brian Osman | 0962268 | 2021-04-15 14:43:19 -0400 | [diff] [blame] | 113 | } else if (coords->type() == *fContext.fTypes.fFloat3x3) { |
| 114 | // Determine the type of matrix for this call site |
| 115 | if (coords->isConstantOrUniform()) { |
| 116 | if (coords->is<VariableReference>() || coords->isAnyConstructor()) { |
| 117 | // FIXME if this is a constant, we should parse the float3x3 |
| 118 | // constructor and determine if the resulting matrix introduces |
| 119 | // perspective. |
| 120 | fUsage.merge(SampleUsage::UniformMatrix(coords->description())); |
| 121 | } else { |
| 122 | // FIXME this is really to workaround a restriction of the |
| 123 | // downstream code that relies on the SampleUsage's fExpression to |
| 124 | // identify uniform names. Once they are tracked separately, any |
| 125 | // uniform expression can work, but right now this avoids issues |
| 126 | // from '0.5 * matrix' that is both a constant AND a uniform. |
| 127 | fUsage.merge(SampleUsage::VariableMatrix()); |
| 128 | } |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 129 | } else { |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 130 | fUsage.merge(SampleUsage::VariableMatrix()); |
| 131 | } |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 132 | } else { |
Brian Osman | 0962268 | 2021-04-15 14:43:19 -0400 | [diff] [blame] | 133 | // sample(fp, half4 inputColor) -> PassThrough |
| 134 | fUsage.merge(SampleUsage::PassThrough()); |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 135 | } |
| 136 | } else { |
Brian Osman | 0962268 | 2021-04-15 14:43:19 -0400 | [diff] [blame] | 137 | // sample(fp) -> PassThrough |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 138 | fUsage.merge(SampleUsage::PassThrough()); |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 139 | } |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 140 | // NOTE: we don't return true here just because we found a sample call. We need to |
Brian Osman | 0962268 | 2021-04-15 14:43:19 -0400 | [diff] [blame] | 141 | // process the entire program and merge across all encountered calls. |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | |
John Stiles | d7ab450 | 2020-09-24 22:41:00 -0400 | [diff] [blame] | 145 | return INHERITED::visitExpression(e); |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 146 | } |
| 147 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 148 | using INHERITED = ProgramVisitor; |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 149 | }; |
| 150 | |
Brian Osman | 92aac1e | 2020-08-05 16:48:58 -0400 | [diff] [blame] | 151 | // Visitor that searches through the program for references to a particular builtin variable |
| 152 | class BuiltinVariableVisitor : public ProgramVisitor { |
| 153 | public: |
| 154 | BuiltinVariableVisitor(int builtin) : fBuiltin(builtin) {} |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 155 | |
| 156 | bool visitExpression(const Expression& e) override { |
Brian Osman | 79457ef | 2020-09-24 15:01:27 -0400 | [diff] [blame] | 157 | if (e.is<VariableReference>()) { |
John Stiles | 403a363 | 2020-08-20 12:11:48 -0400 | [diff] [blame] | 158 | const VariableReference& var = e.as<VariableReference>(); |
Ethan Nicholas | 7868692 | 2020-10-08 06:46:27 -0400 | [diff] [blame] | 159 | return var.variable()->modifiers().fLayout.fBuiltin == fBuiltin; |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 160 | } |
John Stiles | d7ab450 | 2020-09-24 22:41:00 -0400 | [diff] [blame] | 161 | return INHERITED::visitExpression(e); |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 162 | } |
| 163 | |
Brian Osman | 92aac1e | 2020-08-05 16:48:58 -0400 | [diff] [blame] | 164 | int fBuiltin; |
| 165 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 166 | using INHERITED = ProgramVisitor; |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 167 | }; |
| 168 | |
Ethan Nicholas | 6e0fa40 | 2020-08-20 14:08:23 -0400 | [diff] [blame] | 169 | // Visitor that counts the number of nodes visited |
| 170 | class NodeCountVisitor : public ProgramVisitor { |
| 171 | public: |
John Stiles | 2c1e492 | 2020-10-01 09:14:14 -0400 | [diff] [blame] | 172 | NodeCountVisitor(int limit) : fLimit(limit) {} |
| 173 | |
Ethan Nicholas | 6e0fa40 | 2020-08-20 14:08:23 -0400 | [diff] [blame] | 174 | int visit(const Statement& s) { |
Ethan Nicholas | 6e0fa40 | 2020-08-20 14:08:23 -0400 | [diff] [blame] | 175 | this->visitStatement(s); |
| 176 | return fCount; |
| 177 | } |
| 178 | |
| 179 | bool visitExpression(const Expression& e) override { |
| 180 | ++fCount; |
John Stiles | 9b9415e | 2020-11-23 14:48:06 -0500 | [diff] [blame] | 181 | return (fCount >= fLimit) || INHERITED::visitExpression(e); |
Ethan Nicholas | 6e0fa40 | 2020-08-20 14:08:23 -0400 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | bool visitProgramElement(const ProgramElement& p) override { |
| 185 | ++fCount; |
John Stiles | 9b9415e | 2020-11-23 14:48:06 -0500 | [diff] [blame] | 186 | return (fCount >= fLimit) || INHERITED::visitProgramElement(p); |
Ethan Nicholas | 6e0fa40 | 2020-08-20 14:08:23 -0400 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | bool visitStatement(const Statement& s) override { |
| 190 | ++fCount; |
John Stiles | 9b9415e | 2020-11-23 14:48:06 -0500 | [diff] [blame] | 191 | return (fCount >= fLimit) || INHERITED::visitStatement(s); |
Ethan Nicholas | 6e0fa40 | 2020-08-20 14:08:23 -0400 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | private: |
John Stiles | 2c1e492 | 2020-10-01 09:14:14 -0400 | [diff] [blame] | 195 | int fCount = 0; |
| 196 | int fLimit; |
Ethan Nicholas | 6e0fa40 | 2020-08-20 14:08:23 -0400 | [diff] [blame] | 197 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 198 | using INHERITED = ProgramVisitor; |
Ethan Nicholas | 6e0fa40 | 2020-08-20 14:08:23 -0400 | [diff] [blame] | 199 | }; |
| 200 | |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 201 | class ProgramUsageVisitor : public ProgramVisitor { |
Brian Osman | 2e25ff4 | 2020-10-15 10:32:04 -0400 | [diff] [blame] | 202 | public: |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 203 | ProgramUsageVisitor(ProgramUsage* usage, int delta) : fUsage(usage), fDelta(delta) {} |
| 204 | |
John Stiles | 39465b8 | 2021-03-18 09:19:55 -0400 | [diff] [blame] | 205 | bool visitProgramElement(const ProgramElement& pe) override { |
| 206 | if (pe.is<FunctionDefinition>()) { |
| 207 | for (const Variable* param : pe.as<FunctionDefinition>().declaration().parameters()) { |
| 208 | // Ensure function-parameter variables exist in the variable usage map. They aren't |
| 209 | // otherwise declared, but ProgramUsage::get() should be able to find them, even if |
| 210 | // they are unread and unwritten. |
| 211 | fUsage->fVariableCounts[param]; |
| 212 | } |
John Stiles | 8e2a84b | 2021-04-19 09:35:38 -0400 | [diff] [blame^] | 213 | } else if (pe.is<InterfaceBlock>()) { |
| 214 | // Ensure interface-block variables exist in the variable usage map. |
| 215 | fUsage->fVariableCounts[&pe.as<InterfaceBlock>().variable()]; |
John Stiles | 39465b8 | 2021-03-18 09:19:55 -0400 | [diff] [blame] | 216 | } |
| 217 | return INHERITED::visitProgramElement(pe); |
| 218 | } |
| 219 | |
John Stiles | 04a8a54 | 2021-03-10 19:03:26 -0500 | [diff] [blame] | 220 | bool visitStatement(const Statement& s) override { |
| 221 | if (s.is<VarDeclaration>()) { |
| 222 | // Add all declared variables to the usage map (even if never otherwise accessed). |
| 223 | const VarDeclaration& vd = s.as<VarDeclaration>(); |
| 224 | ProgramUsage::VariableCounts& counts = fUsage->fVariableCounts[&vd.var()]; |
| 225 | counts.fDeclared += fDelta; |
| 226 | SkASSERT(counts.fDeclared >= 0); |
| 227 | if (vd.value()) { |
| 228 | // The initial-value expression, when present, counts as a write. |
| 229 | counts.fWrite += fDelta; |
| 230 | } |
| 231 | } |
| 232 | return INHERITED::visitStatement(s); |
| 233 | } |
| 234 | |
Brian Osman | 2e25ff4 | 2020-10-15 10:32:04 -0400 | [diff] [blame] | 235 | bool visitExpression(const Expression& e) override { |
| 236 | if (e.is<FunctionCall>()) { |
| 237 | const FunctionDeclaration* f = &e.as<FunctionCall>().function(); |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 238 | fUsage->fCallCounts[f] += fDelta; |
| 239 | SkASSERT(fUsage->fCallCounts[f] >= 0); |
| 240 | } else if (e.is<VariableReference>()) { |
| 241 | const VariableReference& ref = e.as<VariableReference>(); |
| 242 | ProgramUsage::VariableCounts& counts = fUsage->fVariableCounts[ref.variable()]; |
| 243 | switch (ref.refKind()) { |
| 244 | case VariableRefKind::kRead: |
| 245 | counts.fRead += fDelta; |
| 246 | break; |
| 247 | case VariableRefKind::kWrite: |
| 248 | counts.fWrite += fDelta; |
| 249 | break; |
| 250 | case VariableRefKind::kReadWrite: |
| 251 | case VariableRefKind::kPointer: |
| 252 | counts.fRead += fDelta; |
| 253 | counts.fWrite += fDelta; |
| 254 | break; |
| 255 | } |
| 256 | SkASSERT(counts.fRead >= 0 && counts.fWrite >= 0); |
Brian Osman | 2e25ff4 | 2020-10-15 10:32:04 -0400 | [diff] [blame] | 257 | } |
| 258 | return INHERITED::visitExpression(e); |
| 259 | } |
| 260 | |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 261 | using ProgramVisitor::visitProgramElement; |
| 262 | using ProgramVisitor::visitStatement; |
| 263 | |
| 264 | ProgramUsage* fUsage; |
| 265 | int fDelta; |
Brian Osman | 2e25ff4 | 2020-10-15 10:32:04 -0400 | [diff] [blame] | 266 | using INHERITED = ProgramVisitor; |
| 267 | }; |
| 268 | |
Ethan Nicholas | 765d2fe | 2020-08-26 08:29:55 -0400 | [diff] [blame] | 269 | class VariableWriteVisitor : public ProgramVisitor { |
| 270 | public: |
| 271 | VariableWriteVisitor(const Variable* var) |
| 272 | : fVar(var) {} |
| 273 | |
| 274 | bool visit(const Statement& s) { |
| 275 | return this->visitStatement(s); |
| 276 | } |
| 277 | |
| 278 | bool visitExpression(const Expression& e) override { |
Brian Osman | 79457ef | 2020-09-24 15:01:27 -0400 | [diff] [blame] | 279 | if (e.is<VariableReference>()) { |
Ethan Nicholas | 765d2fe | 2020-08-26 08:29:55 -0400 | [diff] [blame] | 280 | const VariableReference& ref = e.as<VariableReference>(); |
Ethan Nicholas | 453f67f | 2020-10-09 10:43:45 -0400 | [diff] [blame] | 281 | if (ref.variable() == fVar && |
| 282 | (ref.refKind() == VariableReference::RefKind::kWrite || |
| 283 | ref.refKind() == VariableReference::RefKind::kReadWrite || |
| 284 | ref.refKind() == VariableReference::RefKind::kPointer)) { |
Ethan Nicholas | 765d2fe | 2020-08-26 08:29:55 -0400 | [diff] [blame] | 285 | return true; |
| 286 | } |
| 287 | } |
John Stiles | d7ab450 | 2020-09-24 22:41:00 -0400 | [diff] [blame] | 288 | return INHERITED::visitExpression(e); |
Ethan Nicholas | 765d2fe | 2020-08-26 08:29:55 -0400 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | private: |
| 292 | const Variable* fVar; |
| 293 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 294 | using INHERITED = ProgramVisitor; |
Ethan Nicholas | 765d2fe | 2020-08-26 08:29:55 -0400 | [diff] [blame] | 295 | }; |
| 296 | |
John Stiles | a976da7 | 2020-09-25 23:06:26 -0400 | [diff] [blame] | 297 | // If a caller doesn't care about errors, we can use this trivial reporter that just counts up. |
| 298 | class TrivialErrorReporter : public ErrorReporter { |
| 299 | public: |
| 300 | void error(int offset, String) override { ++fErrorCount; } |
| 301 | int errorCount() override { return fErrorCount; } |
John Stiles | 8d3642e | 2021-01-22 09:50:04 -0500 | [diff] [blame] | 302 | void setErrorCount(int c) override { fErrorCount = c; } |
John Stiles | a976da7 | 2020-09-25 23:06:26 -0400 | [diff] [blame] | 303 | |
| 304 | private: |
| 305 | int fErrorCount = 0; |
| 306 | }; |
| 307 | |
John Stiles | dce4d3e | 2020-09-25 14:35:13 -0400 | [diff] [blame] | 308 | // This isn't actually using ProgramVisitor, because it only considers a subset of the fields for |
| 309 | // any given expression kind. For instance, when indexing an array (e.g. `x[1]`), we only want to |
| 310 | // know if the base (`x`) is assignable; the index expression (`1`) doesn't need to be. |
| 311 | class IsAssignableVisitor { |
| 312 | public: |
John Stiles | b21fac2 | 2020-12-04 15:36:49 -0500 | [diff] [blame] | 313 | IsAssignableVisitor(ErrorReporter* errors) : fErrors(errors) {} |
John Stiles | dce4d3e | 2020-09-25 14:35:13 -0400 | [diff] [blame] | 314 | |
John Stiles | b21fac2 | 2020-12-04 15:36:49 -0500 | [diff] [blame] | 315 | bool visit(Expression& expr, Analysis::AssignmentInfo* info) { |
Ethan Nicholas | 67a0a8a | 2021-01-13 12:36:02 -0500 | [diff] [blame] | 316 | int oldErrorCount = fErrors->errorCount(); |
John Stiles | dce4d3e | 2020-09-25 14:35:13 -0400 | [diff] [blame] | 317 | this->visitExpression(expr); |
John Stiles | b21fac2 | 2020-12-04 15:36:49 -0500 | [diff] [blame] | 318 | if (info) { |
| 319 | info->fAssignedVar = fAssignedVar; |
John Stiles | b21fac2 | 2020-12-04 15:36:49 -0500 | [diff] [blame] | 320 | } |
Ethan Nicholas | 67a0a8a | 2021-01-13 12:36:02 -0500 | [diff] [blame] | 321 | return fErrors->errorCount() == oldErrorCount; |
John Stiles | dce4d3e | 2020-09-25 14:35:13 -0400 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | void visitExpression(Expression& expr) { |
| 325 | switch (expr.kind()) { |
| 326 | case Expression::Kind::kVariableReference: { |
| 327 | VariableReference& varRef = expr.as<VariableReference>(); |
Ethan Nicholas | 7868692 | 2020-10-08 06:46:27 -0400 | [diff] [blame] | 328 | const Variable* var = varRef.variable(); |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 329 | if (var->modifiers().fFlags & (Modifiers::kConst_Flag | Modifiers::kUniform_Flag | |
| 330 | Modifiers::kVarying_Flag)) { |
John Stiles | a976da7 | 2020-09-25 23:06:26 -0400 | [diff] [blame] | 331 | fErrors->error(expr.fOffset, |
Ethan Nicholas | e2c4999 | 2020-10-05 11:49:11 -0400 | [diff] [blame] | 332 | "cannot modify immutable variable '" + var->name() + "'"); |
John Stiles | b21fac2 | 2020-12-04 15:36:49 -0500 | [diff] [blame] | 333 | } else { |
| 334 | SkASSERT(fAssignedVar == nullptr); |
| 335 | fAssignedVar = &varRef; |
John Stiles | dce4d3e | 2020-09-25 14:35:13 -0400 | [diff] [blame] | 336 | } |
| 337 | break; |
| 338 | } |
| 339 | case Expression::Kind::kFieldAccess: |
Ethan Nicholas | 7a95b20 | 2020-10-09 11:55:40 -0400 | [diff] [blame] | 340 | this->visitExpression(*expr.as<FieldAccess>().base()); |
John Stiles | dce4d3e | 2020-09-25 14:35:13 -0400 | [diff] [blame] | 341 | break; |
| 342 | |
| 343 | case Expression::Kind::kSwizzle: { |
| 344 | const Swizzle& swizzle = expr.as<Swizzle>(); |
| 345 | this->checkSwizzleWrite(swizzle); |
Ethan Nicholas | 6b4d581 | 2020-10-12 16:11:51 -0400 | [diff] [blame] | 346 | this->visitExpression(*swizzle.base()); |
John Stiles | dce4d3e | 2020-09-25 14:35:13 -0400 | [diff] [blame] | 347 | break; |
| 348 | } |
John Stiles | 47c0a74 | 2021-02-09 09:30:35 -0500 | [diff] [blame] | 349 | case Expression::Kind::kIndex: |
| 350 | this->visitExpression(*expr.as<IndexExpression>().base()); |
John Stiles | dce4d3e | 2020-09-25 14:35:13 -0400 | [diff] [blame] | 351 | break; |
John Stiles | 47c0a74 | 2021-02-09 09:30:35 -0500 | [diff] [blame] | 352 | |
John Stiles | dce4d3e | 2020-09-25 14:35:13 -0400 | [diff] [blame] | 353 | default: |
John Stiles | a976da7 | 2020-09-25 23:06:26 -0400 | [diff] [blame] | 354 | fErrors->error(expr.fOffset, "cannot assign to this expression"); |
John Stiles | dce4d3e | 2020-09-25 14:35:13 -0400 | [diff] [blame] | 355 | break; |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | private: |
| 360 | void checkSwizzleWrite(const Swizzle& swizzle) { |
| 361 | int bits = 0; |
John Stiles | 6e88e04 | 2021-02-19 14:09:38 -0500 | [diff] [blame] | 362 | for (int8_t idx : swizzle.components()) { |
| 363 | SkASSERT(idx >= SwizzleComponent::X && idx <= SwizzleComponent::W); |
John Stiles | dce4d3e | 2020-09-25 14:35:13 -0400 | [diff] [blame] | 364 | int bit = 1 << idx; |
| 365 | if (bits & bit) { |
John Stiles | a976da7 | 2020-09-25 23:06:26 -0400 | [diff] [blame] | 366 | fErrors->error(swizzle.fOffset, |
| 367 | "cannot write to the same swizzle field more than once"); |
John Stiles | dce4d3e | 2020-09-25 14:35:13 -0400 | [diff] [blame] | 368 | break; |
| 369 | } |
| 370 | bits |= bit; |
| 371 | } |
| 372 | } |
| 373 | |
John Stiles | a976da7 | 2020-09-25 23:06:26 -0400 | [diff] [blame] | 374 | ErrorReporter* fErrors; |
John Stiles | b21fac2 | 2020-12-04 15:36:49 -0500 | [diff] [blame] | 375 | VariableReference* fAssignedVar = nullptr; |
John Stiles | dce4d3e | 2020-09-25 14:35:13 -0400 | [diff] [blame] | 376 | |
| 377 | using INHERITED = ProgramVisitor; |
| 378 | }; |
| 379 | |
John Stiles | 642cde2 | 2021-02-23 14:57:01 -0500 | [diff] [blame] | 380 | class SwitchCaseContainsExit : public ProgramVisitor { |
| 381 | public: |
| 382 | SwitchCaseContainsExit(bool conditionalExits) : fConditionalExits(conditionalExits) {} |
| 383 | |
| 384 | bool visitStatement(const Statement& stmt) override { |
| 385 | switch (stmt.kind()) { |
| 386 | case Statement::Kind::kBlock: |
John Stiles | a0e56e3 | 2021-03-03 13:14:37 -0500 | [diff] [blame] | 387 | case Statement::Kind::kSwitchCase: |
John Stiles | 642cde2 | 2021-02-23 14:57:01 -0500 | [diff] [blame] | 388 | return INHERITED::visitStatement(stmt); |
| 389 | |
| 390 | case Statement::Kind::kReturn: |
| 391 | // Returns are an early exit regardless of the surrounding control structures. |
| 392 | return fConditionalExits ? fInConditional : !fInConditional; |
| 393 | |
| 394 | case Statement::Kind::kContinue: |
| 395 | // Continues are an early exit from switches, but not loops. |
| 396 | return !fInLoop && |
| 397 | (fConditionalExits ? fInConditional : !fInConditional); |
| 398 | |
| 399 | case Statement::Kind::kBreak: |
| 400 | // Breaks cannot escape from switches or loops. |
| 401 | return !fInLoop && !fInSwitch && |
| 402 | (fConditionalExits ? fInConditional : !fInConditional); |
| 403 | |
| 404 | case Statement::Kind::kIf: { |
| 405 | ++fInConditional; |
| 406 | bool result = INHERITED::visitStatement(stmt); |
| 407 | --fInConditional; |
| 408 | return result; |
| 409 | } |
| 410 | |
| 411 | case Statement::Kind::kFor: |
| 412 | case Statement::Kind::kDo: { |
| 413 | // Loops are treated as conditionals because a loop could potentially execute zero |
| 414 | // times. We don't have a straightforward way to determine that a loop definitely |
| 415 | // executes at least once. |
| 416 | ++fInConditional; |
| 417 | ++fInLoop; |
| 418 | bool result = INHERITED::visitStatement(stmt); |
| 419 | --fInLoop; |
| 420 | --fInConditional; |
| 421 | return result; |
| 422 | } |
| 423 | |
| 424 | case Statement::Kind::kSwitch: { |
| 425 | ++fInSwitch; |
| 426 | bool result = INHERITED::visitStatement(stmt); |
| 427 | --fInSwitch; |
| 428 | return result; |
| 429 | } |
| 430 | |
| 431 | default: |
| 432 | return false; |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | bool fConditionalExits = false; |
| 437 | int fInConditional = 0; |
| 438 | int fInLoop = 0; |
| 439 | int fInSwitch = 0; |
| 440 | using INHERITED = ProgramVisitor; |
| 441 | }; |
| 442 | |
John Stiles | b3dcbb1 | 2021-03-04 16:00:20 -0500 | [diff] [blame] | 443 | class ReturnsOnAllPathsVisitor : public ProgramVisitor { |
| 444 | public: |
| 445 | bool visitExpression(const Expression& expr) override { |
| 446 | // We can avoid processing expressions entirely. |
| 447 | return false; |
| 448 | } |
| 449 | |
| 450 | bool visitStatement(const Statement& stmt) override { |
| 451 | switch (stmt.kind()) { |
| 452 | // Returns, breaks, or continues will stop the scan, so only one of these should ever be |
| 453 | // true. |
| 454 | case Statement::Kind::kReturn: |
| 455 | fFoundReturn = true; |
| 456 | return true; |
| 457 | |
| 458 | case Statement::Kind::kBreak: |
| 459 | fFoundBreak = true; |
| 460 | return true; |
| 461 | |
| 462 | case Statement::Kind::kContinue: |
| 463 | fFoundContinue = true; |
| 464 | return true; |
| 465 | |
| 466 | case Statement::Kind::kIf: { |
| 467 | const IfStatement& i = stmt.as<IfStatement>(); |
| 468 | ReturnsOnAllPathsVisitor trueVisitor; |
| 469 | ReturnsOnAllPathsVisitor falseVisitor; |
| 470 | trueVisitor.visitStatement(*i.ifTrue()); |
| 471 | if (i.ifFalse()) { |
| 472 | falseVisitor.visitStatement(*i.ifFalse()); |
| 473 | } |
| 474 | // If either branch leads to a break or continue, we report the entire if as |
| 475 | // containing a break or continue, since we don't know which side will be reached. |
| 476 | fFoundBreak = (trueVisitor.fFoundBreak || falseVisitor.fFoundBreak); |
| 477 | fFoundContinue = (trueVisitor.fFoundContinue || falseVisitor.fFoundContinue); |
| 478 | // On the other hand, we only want to report returns that definitely happen, so we |
| 479 | // require those to be found on both sides. |
| 480 | fFoundReturn = (trueVisitor.fFoundReturn && falseVisitor.fFoundReturn); |
| 481 | return fFoundBreak || fFoundContinue || fFoundReturn; |
| 482 | } |
| 483 | case Statement::Kind::kFor: { |
| 484 | const ForStatement& f = stmt.as<ForStatement>(); |
| 485 | // We assume a for/while loop runs for at least one iteration; this isn't strictly |
| 486 | // guaranteed, but it's better to be slightly over-permissive here than to fail on |
| 487 | // reasonable code. |
| 488 | ReturnsOnAllPathsVisitor forVisitor; |
| 489 | forVisitor.visitStatement(*f.statement()); |
| 490 | // A for loop that contains a break or continue is safe; it won't exit the entire |
| 491 | // function, just the loop. So we disregard those signals. |
| 492 | fFoundReturn = forVisitor.fFoundReturn; |
| 493 | return fFoundReturn; |
| 494 | } |
| 495 | case Statement::Kind::kDo: { |
| 496 | const DoStatement& d = stmt.as<DoStatement>(); |
| 497 | // Do-while blocks are always entered at least once. |
| 498 | ReturnsOnAllPathsVisitor doVisitor; |
| 499 | doVisitor.visitStatement(*d.statement()); |
| 500 | // A do-while loop that contains a break or continue is safe; it won't exit the |
| 501 | // entire function, just the loop. So we disregard those signals. |
| 502 | fFoundReturn = doVisitor.fFoundReturn; |
| 503 | return fFoundReturn; |
| 504 | } |
| 505 | case Statement::Kind::kBlock: |
| 506 | // Blocks are definitely entered and don't imply any additional control flow. |
| 507 | // If the block contains a break, continue or return, we want to keep that. |
| 508 | return INHERITED::visitStatement(stmt); |
| 509 | |
| 510 | case Statement::Kind::kSwitch: { |
| 511 | // Switches are the most complex control flow we need to deal with; fortunately we |
| 512 | // already have good primitives for dissecting them. We need to verify that: |
| 513 | // - a default case exists, so that every possible input value is covered |
| 514 | // - every switch-case either (a) returns unconditionally, or |
| 515 | // (b) falls through to another case that does |
| 516 | const SwitchStatement& s = stmt.as<SwitchStatement>(); |
| 517 | bool foundDefault = false; |
| 518 | bool fellThrough = false; |
John Stiles | b23a64b | 2021-03-11 08:27:59 -0500 | [diff] [blame] | 519 | for (const std::unique_ptr<Statement>& stmt : s.cases()) { |
John Stiles | b3dcbb1 | 2021-03-04 16:00:20 -0500 | [diff] [blame] | 520 | // The default case is indicated by a null value. A switch without a default |
| 521 | // case cannot definitively return, as its value might not be in the cases list. |
John Stiles | b23a64b | 2021-03-11 08:27:59 -0500 | [diff] [blame] | 522 | const SwitchCase& sc = stmt->as<SwitchCase>(); |
| 523 | if (!sc.value()) { |
John Stiles | b3dcbb1 | 2021-03-04 16:00:20 -0500 | [diff] [blame] | 524 | foundDefault = true; |
| 525 | } |
| 526 | // Scan this switch-case for any exit (break, continue or return). |
| 527 | ReturnsOnAllPathsVisitor caseVisitor; |
John Stiles | b23a64b | 2021-03-11 08:27:59 -0500 | [diff] [blame] | 528 | caseVisitor.visitStatement(sc); |
John Stiles | b3dcbb1 | 2021-03-04 16:00:20 -0500 | [diff] [blame] | 529 | |
| 530 | // If we found a break or continue, whether conditional or not, this switch case |
| 531 | // can't be called an unconditional return. Switches absorb breaks but not |
| 532 | // continues. |
| 533 | if (caseVisitor.fFoundContinue) { |
| 534 | fFoundContinue = true; |
| 535 | return false; |
| 536 | } |
| 537 | if (caseVisitor.fFoundBreak) { |
| 538 | return false; |
| 539 | } |
| 540 | // We just confirmed that there weren't any breaks or continues. If we didn't |
| 541 | // find an unconditional return either, the switch is considered fallen-through. |
| 542 | // (There might be a conditional return, but that doesn't count.) |
| 543 | fellThrough = !caseVisitor.fFoundReturn; |
| 544 | } |
| 545 | |
| 546 | // If we didn't find a default case, or the very last case fell through, this switch |
| 547 | // doesn't meet our criteria. |
| 548 | if (fellThrough || !foundDefault) { |
| 549 | return false; |
| 550 | } |
| 551 | |
| 552 | // We scanned the entire switch, found a default case, and every section either fell |
| 553 | // through or contained an unconditional return. |
| 554 | fFoundReturn = true; |
| 555 | return true; |
| 556 | } |
| 557 | |
| 558 | case Statement::Kind::kSwitchCase: |
| 559 | // Recurse into the switch-case. |
| 560 | return INHERITED::visitStatement(stmt); |
| 561 | |
| 562 | case Statement::Kind::kDiscard: |
| 563 | case Statement::Kind::kExpression: |
| 564 | case Statement::Kind::kInlineMarker: |
| 565 | case Statement::Kind::kNop: |
| 566 | case Statement::Kind::kVarDeclaration: |
| 567 | // None of these statements could contain a return. |
| 568 | break; |
| 569 | } |
| 570 | |
| 571 | return false; |
| 572 | } |
| 573 | |
| 574 | bool fFoundReturn = false; |
| 575 | bool fFoundBreak = false; |
| 576 | bool fFoundContinue = false; |
| 577 | |
| 578 | using INHERITED = ProgramVisitor; |
| 579 | }; |
| 580 | |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 581 | } // namespace |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 582 | |
| 583 | //////////////////////////////////////////////////////////////////////////////// |
| 584 | // Analysis |
| 585 | |
Brian Osman | cc3d2d2 | 2021-04-16 14:53:24 -0400 | [diff] [blame] | 586 | SampleUsage Analysis::GetSampleUsage(const Program& program, |
| 587 | const Variable& fp, |
| 588 | bool writesToSampleCoords) { |
| 589 | MergeSampleUsageVisitor visitor(*program.fContext, fp, writesToSampleCoords); |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 590 | return visitor.visit(program); |
| 591 | } |
| 592 | |
Brian Osman | 92aac1e | 2020-08-05 16:48:58 -0400 | [diff] [blame] | 593 | bool Analysis::ReferencesBuiltin(const Program& program, int builtin) { |
| 594 | BuiltinVariableVisitor visitor(builtin); |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 595 | return visitor.visit(program); |
| 596 | } |
| 597 | |
Brian Osman | 92aac1e | 2020-08-05 16:48:58 -0400 | [diff] [blame] | 598 | bool Analysis::ReferencesSampleCoords(const Program& program) { |
| 599 | return Analysis::ReferencesBuiltin(program, SK_MAIN_COORDS_BUILTIN); |
| 600 | } |
| 601 | |
| 602 | bool Analysis::ReferencesFragCoords(const Program& program) { |
| 603 | return Analysis::ReferencesBuiltin(program, SK_FRAGCOORD_BUILTIN); |
| 604 | } |
| 605 | |
John Stiles | 9b9415e | 2020-11-23 14:48:06 -0500 | [diff] [blame] | 606 | int Analysis::NodeCountUpToLimit(const FunctionDefinition& function, int limit) { |
| 607 | return NodeCountVisitor{limit}.visit(*function.body()); |
Ethan Nicholas | 6e0fa40 | 2020-08-20 14:08:23 -0400 | [diff] [blame] | 608 | } |
| 609 | |
John Stiles | 642cde2 | 2021-02-23 14:57:01 -0500 | [diff] [blame] | 610 | bool Analysis::SwitchCaseContainsUnconditionalExit(Statement& stmt) { |
| 611 | return SwitchCaseContainsExit{/*conditionalExits=*/false}.visitStatement(stmt); |
| 612 | } |
| 613 | |
| 614 | bool Analysis::SwitchCaseContainsConditionalExit(Statement& stmt) { |
| 615 | return SwitchCaseContainsExit{/*conditionalExits=*/true}.visitStatement(stmt); |
| 616 | } |
| 617 | |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 618 | std::unique_ptr<ProgramUsage> Analysis::GetUsage(const Program& program) { |
| 619 | auto usage = std::make_unique<ProgramUsage>(); |
| 620 | ProgramUsageVisitor addRefs(usage.get(), /*delta=*/+1); |
| 621 | addRefs.visit(program); |
| 622 | return usage; |
| 623 | } |
| 624 | |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 625 | std::unique_ptr<ProgramUsage> Analysis::GetUsage(const LoadedModule& module) { |
| 626 | auto usage = std::make_unique<ProgramUsage>(); |
| 627 | ProgramUsageVisitor addRefs(usage.get(), /*delta=*/+1); |
| 628 | for (const auto& element : module.fElements) { |
| 629 | addRefs.visitProgramElement(*element); |
| 630 | } |
| 631 | return usage; |
| 632 | } |
| 633 | |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 634 | ProgramUsage::VariableCounts ProgramUsage::get(const Variable& v) const { |
John Stiles | 04a8a54 | 2021-03-10 19:03:26 -0500 | [diff] [blame] | 635 | const VariableCounts* counts = fVariableCounts.find(&v); |
| 636 | SkASSERT(counts); |
| 637 | return *counts; |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 638 | } |
| 639 | |
| 640 | bool ProgramUsage::isDead(const Variable& v) const { |
| 641 | const Modifiers& modifiers = v.modifiers(); |
| 642 | VariableCounts counts = this->get(v); |
| 643 | if ((v.storage() != Variable::Storage::kLocal && counts.fRead) || |
| 644 | (modifiers.fFlags & (Modifiers::kIn_Flag | Modifiers::kOut_Flag | Modifiers::kUniform_Flag | |
| 645 | Modifiers::kVarying_Flag))) { |
| 646 | return false; |
| 647 | } |
John Stiles | f10eff3 | 2021-03-16 10:57:55 -0400 | [diff] [blame] | 648 | // Consider the variable dead if it's never read and never written (besides the initial-value). |
| 649 | return !counts.fRead && (counts.fWrite <= (v.initialValue() ? 1 : 0)); |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | int ProgramUsage::get(const FunctionDeclaration& f) const { |
| 653 | const int* count = fCallCounts.find(&f); |
| 654 | return count ? *count : 0; |
| 655 | } |
| 656 | |
| 657 | void ProgramUsage::replace(const Expression* oldExpr, const Expression* newExpr) { |
| 658 | if (oldExpr) { |
| 659 | ProgramUsageVisitor subRefs(this, /*delta=*/-1); |
| 660 | subRefs.visitExpression(*oldExpr); |
| 661 | } |
| 662 | if (newExpr) { |
| 663 | ProgramUsageVisitor addRefs(this, /*delta=*/+1); |
| 664 | addRefs.visitExpression(*newExpr); |
| 665 | } |
| 666 | } |
| 667 | |
| 668 | void ProgramUsage::add(const Statement* stmt) { |
| 669 | ProgramUsageVisitor addRefs(this, /*delta=*/+1); |
| 670 | addRefs.visitStatement(*stmt); |
| 671 | } |
| 672 | |
| 673 | void ProgramUsage::remove(const Expression* expr) { |
| 674 | ProgramUsageVisitor subRefs(this, /*delta=*/-1); |
| 675 | subRefs.visitExpression(*expr); |
| 676 | } |
| 677 | |
| 678 | void ProgramUsage::remove(const Statement* stmt) { |
| 679 | ProgramUsageVisitor subRefs(this, /*delta=*/-1); |
| 680 | subRefs.visitStatement(*stmt); |
| 681 | } |
| 682 | |
| 683 | void ProgramUsage::remove(const ProgramElement& element) { |
| 684 | ProgramUsageVisitor subRefs(this, /*delta=*/-1); |
| 685 | subRefs.visitProgramElement(element); |
Brian Osman | 2e25ff4 | 2020-10-15 10:32:04 -0400 | [diff] [blame] | 686 | } |
| 687 | |
Ethan Nicholas | 765d2fe | 2020-08-26 08:29:55 -0400 | [diff] [blame] | 688 | bool Analysis::StatementWritesToVariable(const Statement& stmt, const Variable& var) { |
| 689 | return VariableWriteVisitor(&var).visit(stmt); |
| 690 | } |
| 691 | |
John Stiles | b21fac2 | 2020-12-04 15:36:49 -0500 | [diff] [blame] | 692 | bool Analysis::IsAssignable(Expression& expr, AssignmentInfo* info, ErrorReporter* errors) { |
John Stiles | a976da7 | 2020-09-25 23:06:26 -0400 | [diff] [blame] | 693 | TrivialErrorReporter trivialErrors; |
John Stiles | b21fac2 | 2020-12-04 15:36:49 -0500 | [diff] [blame] | 694 | return IsAssignableVisitor{errors ? errors : &trivialErrors}.visit(expr, info); |
John Stiles | dce4d3e | 2020-09-25 14:35:13 -0400 | [diff] [blame] | 695 | } |
| 696 | |
John Stiles | 47c0a74 | 2021-02-09 09:30:35 -0500 | [diff] [blame] | 697 | void Analysis::UpdateRefKind(Expression* expr, VariableRefKind refKind) { |
| 698 | class RefKindWriter : public ProgramWriter { |
| 699 | public: |
| 700 | RefKindWriter(VariableReference::RefKind refKind) : fRefKind(refKind) {} |
| 701 | |
| 702 | bool visitExpression(Expression& expr) override { |
| 703 | if (expr.is<VariableReference>()) { |
| 704 | expr.as<VariableReference>().setRefKind(fRefKind); |
| 705 | } |
| 706 | return INHERITED::visitExpression(expr); |
| 707 | } |
| 708 | |
| 709 | private: |
| 710 | VariableReference::RefKind fRefKind; |
| 711 | |
| 712 | using INHERITED = ProgramWriter; |
| 713 | }; |
| 714 | |
| 715 | RefKindWriter{refKind}.visitExpression(*expr); |
| 716 | } |
| 717 | |
John Stiles | 516704b | 2021-02-26 15:01:57 -0500 | [diff] [blame] | 718 | bool Analysis::MakeAssignmentExpr(Expression* expr, |
| 719 | VariableReference::RefKind kind, |
| 720 | ErrorReporter* errors) { |
| 721 | Analysis::AssignmentInfo info; |
| 722 | if (!Analysis::IsAssignable(*expr, &info, errors)) { |
| 723 | return false; |
| 724 | } |
| 725 | if (!info.fAssignedVar) { |
| 726 | errors->error(expr->fOffset, "can't assign to expression '" + expr->description() + "'"); |
| 727 | return false; |
| 728 | } |
| 729 | info.fAssignedVar->setRefKind(kind); |
| 730 | return true; |
| 731 | } |
| 732 | |
John Stiles | c30fbca | 2020-11-19 16:25:49 -0500 | [diff] [blame] | 733 | bool Analysis::IsTrivialExpression(const Expression& expr) { |
| 734 | return expr.is<IntLiteral>() || |
| 735 | expr.is<FloatLiteral>() || |
| 736 | expr.is<BoolLiteral>() || |
| 737 | expr.is<VariableReference>() || |
| 738 | (expr.is<Swizzle>() && |
| 739 | IsTrivialExpression(*expr.as<Swizzle>().base())) || |
| 740 | (expr.is<FieldAccess>() && |
| 741 | IsTrivialExpression(*expr.as<FieldAccess>().base())) || |
John Stiles | 2938eea | 2021-04-01 18:58:25 -0400 | [diff] [blame] | 742 | (expr.isAnyConstructor() && |
| 743 | expr.asAnyConstructor().argumentSpan().size() == 1 && |
| 744 | IsTrivialExpression(*expr.asAnyConstructor().argumentSpan().front())) || |
| 745 | (expr.isAnyConstructor() && |
John Stiles | c30fbca | 2020-11-19 16:25:49 -0500 | [diff] [blame] | 746 | expr.isConstantOrUniform()) || |
| 747 | (expr.is<IndexExpression>() && |
| 748 | expr.as<IndexExpression>().index()->is<IntLiteral>() && |
| 749 | IsTrivialExpression(*expr.as<IndexExpression>().base())); |
| 750 | } |
| 751 | |
John Stiles | 5676c57 | 2021-03-08 17:10:52 -0500 | [diff] [blame] | 752 | bool Analysis::IsSameExpressionTree(const Expression& left, const Expression& right) { |
John Stiles | 95d0bad | 2021-03-01 17:02:28 -0500 | [diff] [blame] | 753 | if (left.kind() != right.kind() || left.type() != right.type()) { |
| 754 | return false; |
| 755 | } |
| 756 | |
John Stiles | 5676c57 | 2021-03-08 17:10:52 -0500 | [diff] [blame] | 757 | // This isn't a fully exhaustive list of expressions by any stretch of the imagination; for |
| 758 | // instance, `x[y+1] = x[y+1]` isn't detected because we don't look at BinaryExpressions. |
| 759 | // Since this is intended to be used for optimization purposes, handling the common cases is |
| 760 | // sufficient. |
John Stiles | 95d0bad | 2021-03-01 17:02:28 -0500 | [diff] [blame] | 761 | switch (left.kind()) { |
| 762 | case Expression::Kind::kIntLiteral: |
| 763 | return left.as<IntLiteral>().value() == right.as<IntLiteral>().value(); |
| 764 | |
John Stiles | 5676c57 | 2021-03-08 17:10:52 -0500 | [diff] [blame] | 765 | case Expression::Kind::kFloatLiteral: |
| 766 | return left.as<FloatLiteral>().value() == right.as<FloatLiteral>().value(); |
| 767 | |
| 768 | case Expression::Kind::kBoolLiteral: |
| 769 | return left.as<BoolLiteral>().value() == right.as<BoolLiteral>().value(); |
| 770 | |
John Stiles | 7384b37 | 2021-04-01 13:48:15 -0400 | [diff] [blame] | 771 | case Expression::Kind::kConstructorArray: |
John Stiles | 8cad637 | 2021-04-07 12:31:13 -0400 | [diff] [blame] | 772 | case Expression::Kind::kConstructorCompound: |
| 773 | case Expression::Kind::kConstructorCompoundCast: |
John Stiles | 2938eea | 2021-04-01 18:58:25 -0400 | [diff] [blame] | 774 | case Expression::Kind::kConstructorDiagonalMatrix: |
John Stiles | 5abb9e1 | 2021-04-06 13:47:19 -0400 | [diff] [blame] | 775 | case Expression::Kind::kConstructorMatrixResize: |
John Stiles | fd7252f | 2021-04-04 22:24:40 -0400 | [diff] [blame] | 776 | case Expression::Kind::kConstructorScalarCast: |
John Stiles | d47330f | 2021-04-08 23:25:52 -0400 | [diff] [blame] | 777 | case Expression::Kind::kConstructorStruct: |
John Stiles | 268a73f | 2021-04-07 12:30:22 -0400 | [diff] [blame] | 778 | case Expression::Kind::kConstructorSplat: { |
John Stiles | fd7252f | 2021-04-04 22:24:40 -0400 | [diff] [blame] | 779 | if (left.kind() != right.kind()) { |
| 780 | return false; |
| 781 | } |
John Stiles | 7384b37 | 2021-04-01 13:48:15 -0400 | [diff] [blame] | 782 | const AnyConstructor& leftCtor = left.asAnyConstructor(); |
| 783 | const AnyConstructor& rightCtor = right.asAnyConstructor(); |
| 784 | const auto leftSpan = leftCtor.argumentSpan(); |
| 785 | const auto rightSpan = rightCtor.argumentSpan(); |
| 786 | if (leftSpan.size() != rightSpan.size()) { |
John Stiles | 5676c57 | 2021-03-08 17:10:52 -0500 | [diff] [blame] | 787 | return false; |
| 788 | } |
John Stiles | 7384b37 | 2021-04-01 13:48:15 -0400 | [diff] [blame] | 789 | for (size_t index = 0; index < leftSpan.size(); ++index) { |
| 790 | if (!IsSameExpressionTree(*leftSpan[index], *rightSpan[index])) { |
John Stiles | 5676c57 | 2021-03-08 17:10:52 -0500 | [diff] [blame] | 791 | return false; |
| 792 | } |
| 793 | } |
| 794 | return true; |
| 795 | } |
John Stiles | 95d0bad | 2021-03-01 17:02:28 -0500 | [diff] [blame] | 796 | case Expression::Kind::kFieldAccess: |
| 797 | return left.as<FieldAccess>().fieldIndex() == right.as<FieldAccess>().fieldIndex() && |
John Stiles | 5676c57 | 2021-03-08 17:10:52 -0500 | [diff] [blame] | 798 | IsSameExpressionTree(*left.as<FieldAccess>().base(), |
| 799 | *right.as<FieldAccess>().base()); |
John Stiles | 95d0bad | 2021-03-01 17:02:28 -0500 | [diff] [blame] | 800 | |
| 801 | case Expression::Kind::kIndex: |
John Stiles | 5676c57 | 2021-03-08 17:10:52 -0500 | [diff] [blame] | 802 | return IsSameExpressionTree(*left.as<IndexExpression>().index(), |
| 803 | *right.as<IndexExpression>().index()) && |
| 804 | IsSameExpressionTree(*left.as<IndexExpression>().base(), |
| 805 | *right.as<IndexExpression>().base()); |
John Stiles | 95d0bad | 2021-03-01 17:02:28 -0500 | [diff] [blame] | 806 | |
| 807 | case Expression::Kind::kSwizzle: |
| 808 | return left.as<Swizzle>().components() == right.as<Swizzle>().components() && |
John Stiles | 5676c57 | 2021-03-08 17:10:52 -0500 | [diff] [blame] | 809 | IsSameExpressionTree(*left.as<Swizzle>().base(), *right.as<Swizzle>().base()); |
John Stiles | 95d0bad | 2021-03-01 17:02:28 -0500 | [diff] [blame] | 810 | |
| 811 | case Expression::Kind::kVariableReference: |
| 812 | return left.as<VariableReference>().variable() == |
| 813 | right.as<VariableReference>().variable(); |
| 814 | |
| 815 | default: |
| 816 | return false; |
| 817 | } |
| 818 | } |
| 819 | |
John Stiles | d80cef6 | 2021-03-29 11:22:43 -0400 | [diff] [blame] | 820 | static bool get_constant_value(const Expression& expr, double* val) { |
John Stiles | 21a50ec | 2021-04-06 14:49:36 -0400 | [diff] [blame] | 821 | const Expression* valExpr = expr.getConstantSubexpression(0); |
| 822 | if (!valExpr) { |
John Stiles | d80cef6 | 2021-03-29 11:22:43 -0400 | [diff] [blame] | 823 | return false; |
| 824 | } |
John Stiles | 21a50ec | 2021-04-06 14:49:36 -0400 | [diff] [blame] | 825 | if (valExpr->is<IntLiteral>()) { |
| 826 | *val = static_cast<double>(valExpr->as<IntLiteral>().value()); |
| 827 | return true; |
John Stiles | d80cef6 | 2021-03-29 11:22:43 -0400 | [diff] [blame] | 828 | } |
John Stiles | 21a50ec | 2021-04-06 14:49:36 -0400 | [diff] [blame] | 829 | if (valExpr->is<FloatLiteral>()) { |
| 830 | *val = static_cast<double>(valExpr->as<FloatLiteral>().value()); |
| 831 | return true; |
| 832 | } |
| 833 | SkDEBUGFAILF("unexpected constant type (%s)", expr.type().description().c_str()); |
| 834 | return false; |
John Stiles | d80cef6 | 2021-03-29 11:22:43 -0400 | [diff] [blame] | 835 | } |
| 836 | |
John Stiles | 232b4ce | 2021-03-01 22:14:22 -0500 | [diff] [blame] | 837 | static const char* invalid_for_ES2(int offset, |
| 838 | const Statement* loopInitializer, |
| 839 | const Expression* loopTest, |
| 840 | const Expression* loopNext, |
| 841 | const Statement* loopStatement, |
Brian Osman | 77ba810 | 2021-01-12 17:15:30 -0500 | [diff] [blame] | 842 | Analysis::UnrollableLoopInfo& loopInfo) { |
Brian Osman | 77ba810 | 2021-01-12 17:15:30 -0500 | [diff] [blame] | 843 | // |
| 844 | // init_declaration has the form: type_specifier identifier = constant_expression |
| 845 | // |
John Stiles | 232b4ce | 2021-03-01 22:14:22 -0500 | [diff] [blame] | 846 | if (!loopInitializer) { |
Brian Osman | 77ba810 | 2021-01-12 17:15:30 -0500 | [diff] [blame] | 847 | return "missing init declaration"; |
| 848 | } |
John Stiles | 232b4ce | 2021-03-01 22:14:22 -0500 | [diff] [blame] | 849 | if (!loopInitializer->is<VarDeclaration>()) { |
Brian Osman | 77ba810 | 2021-01-12 17:15:30 -0500 | [diff] [blame] | 850 | return "invalid init declaration"; |
| 851 | } |
John Stiles | 232b4ce | 2021-03-01 22:14:22 -0500 | [diff] [blame] | 852 | const VarDeclaration& initDecl = loopInitializer->as<VarDeclaration>(); |
Brian Osman | 6c7910e | 2021-01-14 09:50:52 -0500 | [diff] [blame] | 853 | if (!initDecl.baseType().isNumber()) { |
Brian Osman | 77ba810 | 2021-01-12 17:15:30 -0500 | [diff] [blame] | 854 | return "invalid type for loop index"; |
| 855 | } |
| 856 | if (initDecl.arraySize() != 0) { |
| 857 | return "invalid type for loop index"; |
| 858 | } |
| 859 | if (!initDecl.value()) { |
| 860 | return "missing loop index initializer"; |
| 861 | } |
John Stiles | d80cef6 | 2021-03-29 11:22:43 -0400 | [diff] [blame] | 862 | if (!get_constant_value(*initDecl.value(), &loopInfo.fStart)) { |
Brian Osman | 77ba810 | 2021-01-12 17:15:30 -0500 | [diff] [blame] | 863 | return "loop index initializer must be a constant expression"; |
| 864 | } |
| 865 | |
| 866 | loopInfo.fIndex = &initDecl.var(); |
| 867 | |
| 868 | auto is_loop_index = [&](const std::unique_ptr<Expression>& expr) { |
| 869 | return expr->is<VariableReference>() && |
| 870 | expr->as<VariableReference>().variable() == loopInfo.fIndex; |
| 871 | }; |
| 872 | |
| 873 | // |
| 874 | // condition has the form: loop_index relational_operator constant_expression |
| 875 | // |
John Stiles | 232b4ce | 2021-03-01 22:14:22 -0500 | [diff] [blame] | 876 | if (!loopTest) { |
Brian Osman | 77ba810 | 2021-01-12 17:15:30 -0500 | [diff] [blame] | 877 | return "missing condition"; |
| 878 | } |
John Stiles | 232b4ce | 2021-03-01 22:14:22 -0500 | [diff] [blame] | 879 | if (!loopTest->is<BinaryExpression>()) { |
Brian Osman | 77ba810 | 2021-01-12 17:15:30 -0500 | [diff] [blame] | 880 | return "invalid condition"; |
| 881 | } |
John Stiles | 232b4ce | 2021-03-01 22:14:22 -0500 | [diff] [blame] | 882 | const BinaryExpression& cond = loopTest->as<BinaryExpression>(); |
Brian Osman | 77ba810 | 2021-01-12 17:15:30 -0500 | [diff] [blame] | 883 | if (!is_loop_index(cond.left())) { |
| 884 | return "expected loop index on left hand side of condition"; |
| 885 | } |
| 886 | // relational_operator is one of: > >= < <= == or != |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 887 | switch (cond.getOperator().kind()) { |
Brian Osman | 77ba810 | 2021-01-12 17:15:30 -0500 | [diff] [blame] | 888 | case Token::Kind::TK_GT: |
| 889 | case Token::Kind::TK_GTEQ: |
| 890 | case Token::Kind::TK_LT: |
| 891 | case Token::Kind::TK_LTEQ: |
| 892 | case Token::Kind::TK_EQEQ: |
| 893 | case Token::Kind::TK_NEQ: |
| 894 | break; |
| 895 | default: |
| 896 | return "invalid relational operator"; |
| 897 | } |
| 898 | double loopEnd = 0; |
John Stiles | d80cef6 | 2021-03-29 11:22:43 -0400 | [diff] [blame] | 899 | if (!get_constant_value(*cond.right(), &loopEnd)) { |
Brian Osman | 77ba810 | 2021-01-12 17:15:30 -0500 | [diff] [blame] | 900 | return "loop index must be compared with a constant expression"; |
| 901 | } |
| 902 | |
| 903 | // |
| 904 | // expression has one of the following forms: |
| 905 | // loop_index++ |
| 906 | // loop_index-- |
| 907 | // loop_index += constant_expression |
| 908 | // loop_index -= constant_expression |
| 909 | // The spec doesn't mention prefix increment and decrement, but there is some consensus that |
| 910 | // it's an oversight, so we allow those as well. |
| 911 | // |
John Stiles | 232b4ce | 2021-03-01 22:14:22 -0500 | [diff] [blame] | 912 | if (!loopNext) { |
Brian Osman | 77ba810 | 2021-01-12 17:15:30 -0500 | [diff] [blame] | 913 | return "missing loop expression"; |
| 914 | } |
John Stiles | 232b4ce | 2021-03-01 22:14:22 -0500 | [diff] [blame] | 915 | switch (loopNext->kind()) { |
Brian Osman | 77ba810 | 2021-01-12 17:15:30 -0500 | [diff] [blame] | 916 | case Expression::Kind::kBinary: { |
John Stiles | 232b4ce | 2021-03-01 22:14:22 -0500 | [diff] [blame] | 917 | const BinaryExpression& next = loopNext->as<BinaryExpression>(); |
Brian Osman | 77ba810 | 2021-01-12 17:15:30 -0500 | [diff] [blame] | 918 | if (!is_loop_index(next.left())) { |
| 919 | return "expected loop index in loop expression"; |
| 920 | } |
John Stiles | d80cef6 | 2021-03-29 11:22:43 -0400 | [diff] [blame] | 921 | if (!get_constant_value(*next.right(), &loopInfo.fDelta)) { |
Brian Osman | 77ba810 | 2021-01-12 17:15:30 -0500 | [diff] [blame] | 922 | return "loop index must be modified by a constant expression"; |
| 923 | } |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 924 | switch (next.getOperator().kind()) { |
Brian Osman | 77ba810 | 2021-01-12 17:15:30 -0500 | [diff] [blame] | 925 | case Token::Kind::TK_PLUSEQ: break; |
| 926 | case Token::Kind::TK_MINUSEQ: loopInfo.fDelta = -loopInfo.fDelta; break; |
| 927 | default: |
| 928 | return "invalid operator in loop expression"; |
| 929 | } |
| 930 | } break; |
| 931 | case Expression::Kind::kPrefix: { |
John Stiles | 232b4ce | 2021-03-01 22:14:22 -0500 | [diff] [blame] | 932 | const PrefixExpression& next = loopNext->as<PrefixExpression>(); |
Brian Osman | 77ba810 | 2021-01-12 17:15:30 -0500 | [diff] [blame] | 933 | if (!is_loop_index(next.operand())) { |
| 934 | return "expected loop index in loop expression"; |
| 935 | } |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 936 | switch (next.getOperator().kind()) { |
Brian Osman | 77ba810 | 2021-01-12 17:15:30 -0500 | [diff] [blame] | 937 | case Token::Kind::TK_PLUSPLUS: loopInfo.fDelta = 1; break; |
| 938 | case Token::Kind::TK_MINUSMINUS: loopInfo.fDelta = -1; break; |
| 939 | default: |
| 940 | return "invalid operator in loop expression"; |
| 941 | } |
| 942 | } break; |
| 943 | case Expression::Kind::kPostfix: { |
John Stiles | 232b4ce | 2021-03-01 22:14:22 -0500 | [diff] [blame] | 944 | const PostfixExpression& next = loopNext->as<PostfixExpression>(); |
Brian Osman | 77ba810 | 2021-01-12 17:15:30 -0500 | [diff] [blame] | 945 | if (!is_loop_index(next.operand())) { |
| 946 | return "expected loop index in loop expression"; |
| 947 | } |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 948 | switch (next.getOperator().kind()) { |
Brian Osman | 77ba810 | 2021-01-12 17:15:30 -0500 | [diff] [blame] | 949 | case Token::Kind::TK_PLUSPLUS: loopInfo.fDelta = 1; break; |
| 950 | case Token::Kind::TK_MINUSMINUS: loopInfo.fDelta = -1; break; |
| 951 | default: |
| 952 | return "invalid operator in loop expression"; |
| 953 | } |
| 954 | } break; |
| 955 | default: |
| 956 | return "invalid loop expression"; |
| 957 | } |
| 958 | |
| 959 | // |
| 960 | // Within the body of the loop, the loop index is not statically assigned to, nor is it used as |
| 961 | // argument to a function 'out' or 'inout' parameter. |
| 962 | // |
John Stiles | 232b4ce | 2021-03-01 22:14:22 -0500 | [diff] [blame] | 963 | if (Analysis::StatementWritesToVariable(*loopStatement, initDecl.var())) { |
Brian Osman | 77ba810 | 2021-01-12 17:15:30 -0500 | [diff] [blame] | 964 | return "loop index must not be modified within body of the loop"; |
| 965 | } |
| 966 | |
| 967 | // Finally, compute the iteration count, based on the bounds, and the termination operator. |
| 968 | constexpr int kMaxUnrollableLoopLength = 128; |
| 969 | loopInfo.fCount = 0; |
| 970 | |
| 971 | double val = loopInfo.fStart; |
| 972 | auto evalCond = [&]() { |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 973 | switch (cond.getOperator().kind()) { |
Brian Osman | 77ba810 | 2021-01-12 17:15:30 -0500 | [diff] [blame] | 974 | case Token::Kind::TK_GT: return val > loopEnd; |
| 975 | case Token::Kind::TK_GTEQ: return val >= loopEnd; |
| 976 | case Token::Kind::TK_LT: return val < loopEnd; |
| 977 | case Token::Kind::TK_LTEQ: return val <= loopEnd; |
| 978 | case Token::Kind::TK_EQEQ: return val == loopEnd; |
| 979 | case Token::Kind::TK_NEQ: return val != loopEnd; |
| 980 | default: SkUNREACHABLE; |
| 981 | } |
| 982 | }; |
| 983 | |
| 984 | for (loopInfo.fCount = 0; loopInfo.fCount <= kMaxUnrollableLoopLength; ++loopInfo.fCount) { |
| 985 | if (!evalCond()) { |
| 986 | break; |
| 987 | } |
| 988 | val += loopInfo.fDelta; |
| 989 | } |
| 990 | |
| 991 | if (loopInfo.fCount > kMaxUnrollableLoopLength) { |
| 992 | return "loop must guarantee termination in fewer iterations"; |
| 993 | } |
| 994 | |
| 995 | return nullptr; // All checks pass |
| 996 | } |
| 997 | |
John Stiles | 232b4ce | 2021-03-01 22:14:22 -0500 | [diff] [blame] | 998 | bool Analysis::ForLoopIsValidForES2(int offset, |
| 999 | const Statement* loopInitializer, |
| 1000 | const Expression* loopTest, |
| 1001 | const Expression* loopNext, |
| 1002 | const Statement* loopStatement, |
Brian Osman | 77ba810 | 2021-01-12 17:15:30 -0500 | [diff] [blame] | 1003 | Analysis::UnrollableLoopInfo* outLoopInfo, |
| 1004 | ErrorReporter* errors) { |
| 1005 | UnrollableLoopInfo ignored, |
| 1006 | *loopInfo = outLoopInfo ? outLoopInfo : &ignored; |
John Stiles | 232b4ce | 2021-03-01 22:14:22 -0500 | [diff] [blame] | 1007 | if (const char* msg = invalid_for_ES2( |
| 1008 | offset, loopInitializer, loopTest, loopNext, loopStatement, *loopInfo)) { |
Brian Osman | 77ba810 | 2021-01-12 17:15:30 -0500 | [diff] [blame] | 1009 | if (errors) { |
John Stiles | 232b4ce | 2021-03-01 22:14:22 -0500 | [diff] [blame] | 1010 | errors->error(offset, msg); |
Brian Osman | 77ba810 | 2021-01-12 17:15:30 -0500 | [diff] [blame] | 1011 | } |
| 1012 | return false; |
| 1013 | } |
| 1014 | return true; |
| 1015 | } |
| 1016 | |
Brian Osman | 7b36149 | 2021-02-25 11:25:30 -0500 | [diff] [blame] | 1017 | // Checks for ES2 constant-expression rules, and (optionally) constant-index-expression rules |
| 1018 | // (if loopIndices is non-nullptr) |
| 1019 | class ConstantExpressionVisitor : public ProgramVisitor { |
Brian Osman | ea485e5 | 2021-01-15 13:20:32 -0500 | [diff] [blame] | 1020 | public: |
Brian Osman | 7b36149 | 2021-02-25 11:25:30 -0500 | [diff] [blame] | 1021 | ConstantExpressionVisitor(const std::set<const Variable*>* loopIndices) |
Brian Osman | ea485e5 | 2021-01-15 13:20:32 -0500 | [diff] [blame] | 1022 | : fLoopIndices(loopIndices) {} |
| 1023 | |
| 1024 | bool visitExpression(const Expression& e) override { |
| 1025 | // A constant-(index)-expression is one of... |
| 1026 | switch (e.kind()) { |
| 1027 | // ... a literal value |
| 1028 | case Expression::Kind::kBoolLiteral: |
| 1029 | case Expression::Kind::kIntLiteral: |
| 1030 | case Expression::Kind::kFloatLiteral: |
| 1031 | return false; |
| 1032 | |
John Stiles | 532138c | 2021-03-04 16:29:22 -0500 | [diff] [blame] | 1033 | // ... settings can appear in fragment processors; they will resolve when compiled |
| 1034 | case Expression::Kind::kSetting: |
| 1035 | return false; |
| 1036 | |
Brian Osman | 7b36149 | 2021-02-25 11:25:30 -0500 | [diff] [blame] | 1037 | // ... a global or local variable qualified as 'const', excluding function parameters. |
| 1038 | // ... loop indices as defined in section 4. [constant-index-expression] |
| 1039 | case Expression::Kind::kVariableReference: { |
| 1040 | const Variable* v = e.as<VariableReference>().variable(); |
| 1041 | if ((v->storage() == Variable::Storage::kGlobal || |
| 1042 | v->storage() == Variable::Storage::kLocal) && |
| 1043 | (v->modifiers().fFlags & Modifiers::kConst_Flag)) { |
| 1044 | return false; |
| 1045 | } |
| 1046 | return !fLoopIndices || fLoopIndices->find(v) == fLoopIndices->end(); |
| 1047 | } |
Brian Osman | ea485e5 | 2021-01-15 13:20:32 -0500 | [diff] [blame] | 1048 | |
| 1049 | // ... expressions composed of both of the above |
| 1050 | case Expression::Kind::kBinary: |
John Stiles | 7384b37 | 2021-04-01 13:48:15 -0400 | [diff] [blame] | 1051 | case Expression::Kind::kConstructorArray: |
John Stiles | 8cad637 | 2021-04-07 12:31:13 -0400 | [diff] [blame] | 1052 | case Expression::Kind::kConstructorCompound: |
| 1053 | case Expression::Kind::kConstructorCompoundCast: |
John Stiles | e118278 | 2021-03-30 22:09:37 -0400 | [diff] [blame] | 1054 | case Expression::Kind::kConstructorDiagonalMatrix: |
John Stiles | 5abb9e1 | 2021-04-06 13:47:19 -0400 | [diff] [blame] | 1055 | case Expression::Kind::kConstructorMatrixResize: |
John Stiles | fd7252f | 2021-04-04 22:24:40 -0400 | [diff] [blame] | 1056 | case Expression::Kind::kConstructorScalarCast: |
John Stiles | 2938eea | 2021-04-01 18:58:25 -0400 | [diff] [blame] | 1057 | case Expression::Kind::kConstructorSplat: |
John Stiles | d47330f | 2021-04-08 23:25:52 -0400 | [diff] [blame] | 1058 | case Expression::Kind::kConstructorStruct: |
Brian Osman | ea485e5 | 2021-01-15 13:20:32 -0500 | [diff] [blame] | 1059 | case Expression::Kind::kFieldAccess: |
| 1060 | case Expression::Kind::kIndex: |
| 1061 | case Expression::Kind::kPrefix: |
| 1062 | case Expression::Kind::kPostfix: |
| 1063 | case Expression::Kind::kSwizzle: |
| 1064 | case Expression::Kind::kTernary: |
| 1065 | return INHERITED::visitExpression(e); |
| 1066 | |
Brian Osman | 7b36149 | 2021-02-25 11:25:30 -0500 | [diff] [blame] | 1067 | // These are completely disallowed in SkSL constant-(index)-expressions. GLSL allows |
Brian Osman | ea485e5 | 2021-01-15 13:20:32 -0500 | [diff] [blame] | 1068 | // calls to built-in functions where the arguments are all constant-expressions, but |
| 1069 | // we don't guarantee that behavior. (skbug.com/10835) |
| 1070 | case Expression::Kind::kExternalFunctionCall: |
| 1071 | case Expression::Kind::kFunctionCall: |
| 1072 | return true; |
| 1073 | |
| 1074 | // These should never appear in final IR |
| 1075 | case Expression::Kind::kDefined: |
| 1076 | case Expression::Kind::kExternalFunctionReference: |
| 1077 | case Expression::Kind::kFunctionReference: |
Brian Osman | ea485e5 | 2021-01-15 13:20:32 -0500 | [diff] [blame] | 1078 | case Expression::Kind::kTypeReference: |
| 1079 | default: |
| 1080 | SkDEBUGFAIL("Unexpected expression type"); |
| 1081 | return true; |
| 1082 | } |
| 1083 | } |
| 1084 | |
| 1085 | private: |
| 1086 | const std::set<const Variable*>* fLoopIndices; |
| 1087 | using INHERITED = ProgramVisitor; |
| 1088 | }; |
| 1089 | |
| 1090 | class ES2IndexingVisitor : public ProgramVisitor { |
| 1091 | public: |
| 1092 | ES2IndexingVisitor(ErrorReporter& errors) : fErrors(errors) {} |
| 1093 | |
| 1094 | bool visitStatement(const Statement& s) override { |
| 1095 | if (s.is<ForStatement>()) { |
| 1096 | const ForStatement& f = s.as<ForStatement>(); |
| 1097 | SkASSERT(f.initializer() && f.initializer()->is<VarDeclaration>()); |
| 1098 | const Variable* var = &f.initializer()->as<VarDeclaration>().var(); |
| 1099 | auto [iter, inserted] = fLoopIndices.insert(var); |
| 1100 | SkASSERT(inserted); |
| 1101 | bool result = this->visitStatement(*f.statement()); |
| 1102 | fLoopIndices.erase(iter); |
| 1103 | return result; |
| 1104 | } |
| 1105 | return INHERITED::visitStatement(s); |
| 1106 | } |
| 1107 | |
| 1108 | bool visitExpression(const Expression& e) override { |
| 1109 | if (e.is<IndexExpression>()) { |
| 1110 | const IndexExpression& i = e.as<IndexExpression>(); |
Brian Osman | 7b36149 | 2021-02-25 11:25:30 -0500 | [diff] [blame] | 1111 | ConstantExpressionVisitor indexerInvalid(&fLoopIndices); |
Brian Osman | ea485e5 | 2021-01-15 13:20:32 -0500 | [diff] [blame] | 1112 | if (indexerInvalid.visitExpression(*i.index())) { |
| 1113 | fErrors.error(i.fOffset, "index expression must be constant"); |
| 1114 | return true; |
| 1115 | } |
| 1116 | } |
| 1117 | return INHERITED::visitExpression(e); |
| 1118 | } |
| 1119 | |
| 1120 | using ProgramVisitor::visitProgramElement; |
| 1121 | |
| 1122 | private: |
| 1123 | ErrorReporter& fErrors; |
| 1124 | std::set<const Variable*> fLoopIndices; |
| 1125 | using INHERITED = ProgramVisitor; |
| 1126 | }; |
| 1127 | |
| 1128 | |
| 1129 | void Analysis::ValidateIndexingForES2(const ProgramElement& pe, ErrorReporter& errors) { |
| 1130 | ES2IndexingVisitor visitor(errors); |
| 1131 | visitor.visitProgramElement(pe); |
| 1132 | } |
| 1133 | |
Brian Osman | 7b36149 | 2021-02-25 11:25:30 -0500 | [diff] [blame] | 1134 | bool Analysis::IsConstantExpression(const Expression& expr) { |
| 1135 | ConstantExpressionVisitor visitor(/*loopIndices=*/nullptr); |
| 1136 | return !visitor.visitExpression(expr); |
| 1137 | } |
| 1138 | |
John Stiles | 958f4b5 | 2021-03-17 16:39:49 -0400 | [diff] [blame] | 1139 | bool Analysis::CanExitWithoutReturningValue(const FunctionDeclaration& funcDecl, |
| 1140 | const Statement& body) { |
| 1141 | if (funcDecl.returnType().isVoid()) { |
| 1142 | return false; |
| 1143 | } |
John Stiles | b3dcbb1 | 2021-03-04 16:00:20 -0500 | [diff] [blame] | 1144 | ReturnsOnAllPathsVisitor visitor; |
John Stiles | 958f4b5 | 2021-03-17 16:39:49 -0400 | [diff] [blame] | 1145 | visitor.visitStatement(body); |
John Stiles | b3dcbb1 | 2021-03-04 16:00:20 -0500 | [diff] [blame] | 1146 | return !visitor.fFoundReturn; |
| 1147 | } |
| 1148 | |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 1149 | //////////////////////////////////////////////////////////////////////////////// |
| 1150 | // ProgramVisitor |
| 1151 | |
Brian Osman | 133724c | 2020-10-28 14:14:39 -0400 | [diff] [blame] | 1152 | bool ProgramVisitor::visit(const Program& program) { |
| 1153 | for (const ProgramElement* pe : program.elements()) { |
Brian Osman | 1179fcf | 2020-10-08 16:04:40 -0400 | [diff] [blame] | 1154 | if (this->visitProgramElement(*pe)) { |
John Stiles | 933abe3 | 2020-08-28 11:58:40 -0400 | [diff] [blame] | 1155 | return true; |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 1156 | } |
| 1157 | } |
John Stiles | 933abe3 | 2020-08-28 11:58:40 -0400 | [diff] [blame] | 1158 | return false; |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 1159 | } |
| 1160 | |
John Stiles | 48b2558 | 2021-03-11 14:26:42 -0500 | [diff] [blame] | 1161 | template <typename T> bool TProgramVisitor<T>::visitExpression(typename T::Expression& e) { |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1162 | switch (e.kind()) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1163 | case Expression::Kind::kBoolLiteral: |
| 1164 | case Expression::Kind::kDefined: |
Brian Osman | be0b3b7 | 2021-01-06 14:27:35 -0500 | [diff] [blame] | 1165 | case Expression::Kind::kExternalFunctionReference: |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1166 | case Expression::Kind::kFloatLiteral: |
| 1167 | case Expression::Kind::kFunctionReference: |
| 1168 | case Expression::Kind::kIntLiteral: |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1169 | case Expression::Kind::kSetting: |
| 1170 | case Expression::Kind::kTypeReference: |
| 1171 | case Expression::Kind::kVariableReference: |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 1172 | // Leaf expressions return false |
| 1173 | return false; |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1174 | |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1175 | case Expression::Kind::kBinary: { |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1176 | auto& b = e.template as<BinaryExpression>(); |
John Stiles | 48b2558 | 2021-03-11 14:26:42 -0500 | [diff] [blame] | 1177 | return (b.left() && this->visitExpressionPtr(b.left())) || |
| 1178 | (b.right() && this->visitExpressionPtr(b.right())); |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1179 | } |
John Stiles | 7384b37 | 2021-04-01 13:48:15 -0400 | [diff] [blame] | 1180 | case Expression::Kind::kConstructorArray: |
John Stiles | 8cad637 | 2021-04-07 12:31:13 -0400 | [diff] [blame] | 1181 | case Expression::Kind::kConstructorCompound: |
| 1182 | case Expression::Kind::kConstructorCompoundCast: |
John Stiles | 2938eea | 2021-04-01 18:58:25 -0400 | [diff] [blame] | 1183 | case Expression::Kind::kConstructorDiagonalMatrix: |
John Stiles | 5abb9e1 | 2021-04-06 13:47:19 -0400 | [diff] [blame] | 1184 | case Expression::Kind::kConstructorMatrixResize: |
John Stiles | fd7252f | 2021-04-04 22:24:40 -0400 | [diff] [blame] | 1185 | case Expression::Kind::kConstructorScalarCast: |
John Stiles | d47330f | 2021-04-08 23:25:52 -0400 | [diff] [blame] | 1186 | case Expression::Kind::kConstructorSplat: |
| 1187 | case Expression::Kind::kConstructorStruct: { |
John Stiles | 7384b37 | 2021-04-01 13:48:15 -0400 | [diff] [blame] | 1188 | auto& c = e.asAnyConstructor(); |
| 1189 | for (auto& arg : c.argumentSpan()) { |
John Stiles | 48b2558 | 2021-03-11 14:26:42 -0500 | [diff] [blame] | 1190 | if (this->visitExpressionPtr(arg)) { return true; } |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 1191 | } |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1192 | return false; |
| 1193 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1194 | case Expression::Kind::kExternalFunctionCall: { |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1195 | auto& c = e.template as<ExternalFunctionCall>(); |
Ethan Nicholas | 6e86ec9 | 2020-09-30 14:29:56 -0400 | [diff] [blame] | 1196 | for (auto& arg : c.arguments()) { |
John Stiles | 48b2558 | 2021-03-11 14:26:42 -0500 | [diff] [blame] | 1197 | if (this->visitExpressionPtr(arg)) { return true; } |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 1198 | } |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1199 | return false; |
| 1200 | } |
John Stiles | d7ab450 | 2020-09-24 22:41:00 -0400 | [diff] [blame] | 1201 | case Expression::Kind::kFieldAccess: |
John Stiles | 48b2558 | 2021-03-11 14:26:42 -0500 | [diff] [blame] | 1202 | return this->visitExpressionPtr(e.template as<FieldAccess>().base()); |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1203 | |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1204 | case Expression::Kind::kFunctionCall: { |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1205 | auto& c = e.template as<FunctionCall>(); |
Ethan Nicholas | 0dec992 | 2020-10-05 15:51:52 -0400 | [diff] [blame] | 1206 | for (auto& arg : c.arguments()) { |
John Stiles | 48b2558 | 2021-03-11 14:26:42 -0500 | [diff] [blame] | 1207 | if (arg && this->visitExpressionPtr(arg)) { return true; } |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 1208 | } |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1209 | return false; |
| 1210 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1211 | case Expression::Kind::kIndex: { |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1212 | auto& i = e.template as<IndexExpression>(); |
John Stiles | 48b2558 | 2021-03-11 14:26:42 -0500 | [diff] [blame] | 1213 | return this->visitExpressionPtr(i.base()) || this->visitExpressionPtr(i.index()); |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1214 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1215 | case Expression::Kind::kPostfix: |
John Stiles | 48b2558 | 2021-03-11 14:26:42 -0500 | [diff] [blame] | 1216 | return this->visitExpressionPtr(e.template as<PostfixExpression>().operand()); |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1217 | |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1218 | case Expression::Kind::kPrefix: |
John Stiles | 48b2558 | 2021-03-11 14:26:42 -0500 | [diff] [blame] | 1219 | return this->visitExpressionPtr(e.template as<PrefixExpression>().operand()); |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1220 | |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 1221 | case Expression::Kind::kSwizzle: { |
| 1222 | auto& s = e.template as<Swizzle>(); |
John Stiles | 48b2558 | 2021-03-11 14:26:42 -0500 | [diff] [blame] | 1223 | return s.base() && this->visitExpressionPtr(s.base()); |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 1224 | } |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1225 | |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1226 | case Expression::Kind::kTernary: { |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1227 | auto& t = e.template as<TernaryExpression>(); |
John Stiles | 48b2558 | 2021-03-11 14:26:42 -0500 | [diff] [blame] | 1228 | return this->visitExpressionPtr(t.test()) || |
| 1229 | (t.ifTrue() && this->visitExpressionPtr(t.ifTrue())) || |
| 1230 | (t.ifFalse() && this->visitExpressionPtr(t.ifFalse())); |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1231 | } |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 1232 | default: |
| 1233 | SkUNREACHABLE; |
| 1234 | } |
| 1235 | } |
| 1236 | |
John Stiles | 48b2558 | 2021-03-11 14:26:42 -0500 | [diff] [blame] | 1237 | template <typename T> bool TProgramVisitor<T>::visitStatement(typename T::Statement& s) { |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1238 | switch (s.kind()) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1239 | case Statement::Kind::kBreak: |
| 1240 | case Statement::Kind::kContinue: |
| 1241 | case Statement::Kind::kDiscard: |
John Stiles | 98c1f82 | 2020-09-09 14:18:53 -0400 | [diff] [blame] | 1242 | case Statement::Kind::kInlineMarker: |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1243 | case Statement::Kind::kNop: |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 1244 | // Leaf statements just return false |
| 1245 | return false; |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1246 | |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1247 | case Statement::Kind::kBlock: |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1248 | for (auto& stmt : s.template as<Block>().children()) { |
John Stiles | 48b2558 | 2021-03-11 14:26:42 -0500 | [diff] [blame] | 1249 | if (stmt && this->visitStatementPtr(stmt)) { |
Ethan Nicholas | 7bd6043 | 2020-09-25 14:31:59 -0400 | [diff] [blame] | 1250 | return true; |
| 1251 | } |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 1252 | } |
| 1253 | return false; |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1254 | |
John Stiles | a0e56e3 | 2021-03-03 13:14:37 -0500 | [diff] [blame] | 1255 | case Statement::Kind::kSwitchCase: { |
| 1256 | auto& sc = s.template as<SwitchCase>(); |
John Stiles | 48b2558 | 2021-03-11 14:26:42 -0500 | [diff] [blame] | 1257 | if (sc.value() && this->visitExpressionPtr(sc.value())) { |
John Stiles | a0e56e3 | 2021-03-03 13:14:37 -0500 | [diff] [blame] | 1258 | return true; |
| 1259 | } |
John Stiles | 48b2558 | 2021-03-11 14:26:42 -0500 | [diff] [blame] | 1260 | return this->visitStatementPtr(sc.statement()); |
John Stiles | a0e56e3 | 2021-03-03 13:14:37 -0500 | [diff] [blame] | 1261 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1262 | case Statement::Kind::kDo: { |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1263 | auto& d = s.template as<DoStatement>(); |
John Stiles | 48b2558 | 2021-03-11 14:26:42 -0500 | [diff] [blame] | 1264 | return this->visitExpressionPtr(d.test()) || this->visitStatementPtr(d.statement()); |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1265 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1266 | case Statement::Kind::kExpression: |
John Stiles | 48b2558 | 2021-03-11 14:26:42 -0500 | [diff] [blame] | 1267 | return this->visitExpressionPtr(s.template as<ExpressionStatement>().expression()); |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1268 | |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1269 | case Statement::Kind::kFor: { |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1270 | auto& f = s.template as<ForStatement>(); |
John Stiles | 48b2558 | 2021-03-11 14:26:42 -0500 | [diff] [blame] | 1271 | return (f.initializer() && this->visitStatementPtr(f.initializer())) || |
| 1272 | (f.test() && this->visitExpressionPtr(f.test())) || |
| 1273 | (f.next() && this->visitExpressionPtr(f.next())) || |
| 1274 | this->visitStatementPtr(f.statement()); |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1275 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1276 | case Statement::Kind::kIf: { |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1277 | auto& i = s.template as<IfStatement>(); |
John Stiles | 48b2558 | 2021-03-11 14:26:42 -0500 | [diff] [blame] | 1278 | return (i.test() && this->visitExpressionPtr(i.test())) || |
| 1279 | (i.ifTrue() && this->visitStatementPtr(i.ifTrue())) || |
| 1280 | (i.ifFalse() && this->visitStatementPtr(i.ifFalse())); |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1281 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1282 | case Statement::Kind::kReturn: { |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1283 | auto& r = s.template as<ReturnStatement>(); |
John Stiles | 48b2558 | 2021-03-11 14:26:42 -0500 | [diff] [blame] | 1284 | return r.expression() && this->visitExpressionPtr(r.expression()); |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1285 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1286 | case Statement::Kind::kSwitch: { |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1287 | auto& sw = s.template as<SwitchStatement>(); |
John Stiles | 48b2558 | 2021-03-11 14:26:42 -0500 | [diff] [blame] | 1288 | if (this->visitExpressionPtr(sw.value())) { |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1289 | return true; |
| 1290 | } |
John Stiles | 48b2558 | 2021-03-11 14:26:42 -0500 | [diff] [blame] | 1291 | for (auto& c : sw.cases()) { |
| 1292 | if (this->visitStatementPtr(c)) { |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1293 | return true; |
| 1294 | } |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 1295 | } |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1296 | return false; |
| 1297 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1298 | case Statement::Kind::kVarDeclaration: { |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1299 | auto& v = s.template as<VarDeclaration>(); |
John Stiles | 48b2558 | 2021-03-11 14:26:42 -0500 | [diff] [blame] | 1300 | return v.value() && this->visitExpressionPtr(v.value()); |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1301 | } |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 1302 | default: |
| 1303 | SkUNREACHABLE; |
| 1304 | } |
| 1305 | } |
| 1306 | |
John Stiles | 48b2558 | 2021-03-11 14:26:42 -0500 | [diff] [blame] | 1307 | template <typename T> bool TProgramVisitor<T>::visitProgramElement(typename T::ProgramElement& pe) { |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1308 | switch (pe.kind()) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1309 | case ProgramElement::Kind::kEnum: |
| 1310 | case ProgramElement::Kind::kExtension: |
John Stiles | 569249b | 2020-11-03 12:18:22 -0500 | [diff] [blame] | 1311 | case ProgramElement::Kind::kFunctionPrototype: |
John Stiles | d39aec0 | 2020-12-03 10:42:26 -0500 | [diff] [blame] | 1312 | case ProgramElement::Kind::kInterfaceBlock: |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1313 | case ProgramElement::Kind::kModifiers: |
| 1314 | case ProgramElement::Kind::kSection: |
John Stiles | dc75a97 | 2020-11-25 16:24:55 -0500 | [diff] [blame] | 1315 | case ProgramElement::Kind::kStructDefinition: |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 1316 | // Leaf program elements just return false by default |
| 1317 | return false; |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1318 | |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1319 | case ProgramElement::Kind::kFunction: |
John Stiles | 48b2558 | 2021-03-11 14:26:42 -0500 | [diff] [blame] | 1320 | return this->visitStatementPtr(pe.template as<FunctionDefinition>().body()); |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1321 | |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 1322 | case ProgramElement::Kind::kGlobalVar: |
John Stiles | 48b2558 | 2021-03-11 14:26:42 -0500 | [diff] [blame] | 1323 | return this->visitStatementPtr(pe.template as<GlobalVarDeclaration>().declaration()); |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1324 | |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 1325 | default: |
| 1326 | SkUNREACHABLE; |
| 1327 | } |
| 1328 | } |
| 1329 | |
John Stiles | 48b2558 | 2021-03-11 14:26:42 -0500 | [diff] [blame] | 1330 | template class TProgramVisitor<ProgramVisitorTypes>; |
| 1331 | template class TProgramVisitor<ProgramWriterTypes>; |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1332 | |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 1333 | } // namespace SkSL |