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