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 | |
John Stiles | 106e0cd | 2021-09-07 11:43:51 -0400 | [diff] [blame] | 10 | #include "include/private/SkFloatingPoint.h" |
Ethan Nicholas | daed259 | 2021-03-04 14:30:25 -0500 | [diff] [blame] | 11 | #include "include/private/SkSLModifiers.h" |
Ethan Nicholas | 24c1772 | 2021-03-09 13:10:59 -0500 | [diff] [blame] | 12 | #include "include/private/SkSLProgramElement.h" |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 13 | #include "include/private/SkSLSampleUsage.h" |
Ethan Nicholas | 24c1772 | 2021-03-09 13:10:59 -0500 | [diff] [blame] | 14 | #include "include/private/SkSLStatement.h" |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 15 | #include "include/sksl/SkSLErrorReporter.h" |
John Stiles | 61e5e20 | 2021-09-02 09:56:31 -0400 | [diff] [blame] | 16 | #include "src/core/SkSafeMath.h" |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 17 | #include "src/sksl/SkSLCompiler.h" |
Brian Osman | 448b2d5 | 2021-09-23 11:36:15 -0400 | [diff] [blame] | 18 | #include "src/sksl/SkSLConstantFolder.h" |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 19 | #include "src/sksl/ir/SkSLExpression.h" |
| 20 | #include "src/sksl/ir/SkSLProgram.h" |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 21 | |
| 22 | // ProgramElements |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 23 | #include "src/sksl/ir/SkSLExtension.h" |
| 24 | #include "src/sksl/ir/SkSLFunctionDefinition.h" |
| 25 | #include "src/sksl/ir/SkSLInterfaceBlock.h" |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 26 | #include "src/sksl/ir/SkSLVarDeclarations.h" |
| 27 | |
| 28 | // Statements |
| 29 | #include "src/sksl/ir/SkSLBlock.h" |
| 30 | #include "src/sksl/ir/SkSLBreakStatement.h" |
| 31 | #include "src/sksl/ir/SkSLContinueStatement.h" |
| 32 | #include "src/sksl/ir/SkSLDiscardStatement.h" |
| 33 | #include "src/sksl/ir/SkSLDoStatement.h" |
| 34 | #include "src/sksl/ir/SkSLExpressionStatement.h" |
| 35 | #include "src/sksl/ir/SkSLForStatement.h" |
| 36 | #include "src/sksl/ir/SkSLIfStatement.h" |
| 37 | #include "src/sksl/ir/SkSLNop.h" |
| 38 | #include "src/sksl/ir/SkSLReturnStatement.h" |
| 39 | #include "src/sksl/ir/SkSLSwitchStatement.h" |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 40 | |
| 41 | // Expressions |
| 42 | #include "src/sksl/ir/SkSLBinaryExpression.h" |
Brian Osman | eb0f29d | 2021-08-04 11:34:16 -0400 | [diff] [blame] | 43 | #include "src/sksl/ir/SkSLChildCall.h" |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 44 | #include "src/sksl/ir/SkSLConstructor.h" |
John Stiles | e118278 | 2021-03-30 22:09:37 -0400 | [diff] [blame] | 45 | #include "src/sksl/ir/SkSLConstructorDiagonalMatrix.h" |
John Stiles | 5abb9e1 | 2021-04-06 13:47:19 -0400 | [diff] [blame] | 46 | #include "src/sksl/ir/SkSLConstructorMatrixResize.h" |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 47 | #include "src/sksl/ir/SkSLExternalFunctionCall.h" |
Brian Osman | be0b3b7 | 2021-01-06 14:27:35 -0500 | [diff] [blame] | 48 | #include "src/sksl/ir/SkSLExternalFunctionReference.h" |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 49 | #include "src/sksl/ir/SkSLFieldAccess.h" |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 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" |
John Stiles | 7591d4b | 2021-09-13 13:32:06 -0400 | [diff] [blame] | 54 | #include "src/sksl/ir/SkSLLiteral.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 | |
John Stiles | 49b1a42 | 2021-09-22 09:35:39 -0400 | [diff] [blame] | 63 | #include <stack> |
| 64 | |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 65 | namespace SkSL { |
| 66 | |
| 67 | namespace { |
| 68 | |
Brian Osman | eb0f29d | 2021-08-04 11:34:16 -0400 | [diff] [blame] | 69 | // Visitor that determines the merged SampleUsage for a given child in the program. |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 70 | class MergeSampleUsageVisitor : public ProgramVisitor { |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 71 | public: |
Brian Osman | eb0f29d | 2021-08-04 11:34:16 -0400 | [diff] [blame] | 72 | MergeSampleUsageVisitor(const Context& context, |
| 73 | const Variable& child, |
| 74 | bool writesToSampleCoords) |
| 75 | : fContext(context), fChild(child), fWritesToSampleCoords(writesToSampleCoords) {} |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 76 | |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 77 | SampleUsage visit(const Program& program) { |
| 78 | fUsage = SampleUsage(); // reset to none |
John Stiles | d7ab450 | 2020-09-24 22:41:00 -0400 | [diff] [blame] | 79 | INHERITED::visit(program); |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 80 | return fUsage; |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 81 | } |
| 82 | |
Brian Osman | 8cdf28f | 2021-05-24 09:52:39 -0400 | [diff] [blame] | 83 | int elidedSampleCoordCount() const { return fElidedSampleCoordCount; } |
| 84 | |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 85 | protected: |
John Stiles | 933abe3 | 2020-08-28 11:58:40 -0400 | [diff] [blame] | 86 | const Context& fContext; |
Brian Osman | eb0f29d | 2021-08-04 11:34:16 -0400 | [diff] [blame] | 87 | const Variable& fChild; |
Brian Osman | 4d57111 | 2021-04-27 09:10:10 -0400 | [diff] [blame] | 88 | const bool fWritesToSampleCoords; |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 89 | SampleUsage fUsage; |
Brian Osman | 8cdf28f | 2021-05-24 09:52:39 -0400 | [diff] [blame] | 90 | int fElidedSampleCoordCount = 0; |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 91 | |
| 92 | bool visitExpression(const Expression& e) override { |
Brian Osman | eb0f29d | 2021-08-04 11:34:16 -0400 | [diff] [blame] | 93 | // Looking for child(...) |
| 94 | if (e.is<ChildCall>() && &e.as<ChildCall>().child() == &fChild) { |
| 95 | // Determine the type of call at this site, and merge it with the accumulated state |
| 96 | const ExpressionArray& arguments = e.as<ChildCall>().arguments(); |
| 97 | SkASSERT(arguments.size() >= 1); |
| 98 | |
| 99 | const Expression* maybeCoords = arguments[0].get(); |
| 100 | if (maybeCoords->type() == *fContext.fTypes.fFloat2) { |
| 101 | // If the coords are a direct reference to the program's sample-coords, and those |
| 102 | // coords are never modified, we can conservatively turn this into PassThrough |
| 103 | // sampling. In all other cases, we consider it Explicit. |
| 104 | if (!fWritesToSampleCoords && maybeCoords->is<VariableReference>() && |
| 105 | maybeCoords->as<VariableReference>().variable()->modifiers().fLayout.fBuiltin == |
| 106 | SK_MAIN_COORDS_BUILTIN) { |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 107 | fUsage.merge(SampleUsage::PassThrough()); |
Brian Osman | eb0f29d | 2021-08-04 11:34:16 -0400 | [diff] [blame] | 108 | ++fElidedSampleCoordCount; |
| 109 | } else { |
| 110 | fUsage.merge(SampleUsage::Explicit()); |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 111 | } |
Brian Osman | eb0f29d | 2021-08-04 11:34:16 -0400 | [diff] [blame] | 112 | } else { |
| 113 | // child(inputColor) or child(srcColor, dstColor) -> PassThrough |
| 114 | fUsage.merge(SampleUsage::PassThrough()); |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 115 | } |
| 116 | } |
| 117 | |
John Stiles | d7ab450 | 2020-09-24 22:41:00 -0400 | [diff] [blame] | 118 | return INHERITED::visitExpression(e); |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 119 | } |
| 120 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 121 | using INHERITED = ProgramVisitor; |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 122 | }; |
| 123 | |
Brian Osman | 92aac1e | 2020-08-05 16:48:58 -0400 | [diff] [blame] | 124 | // Visitor that searches through the program for references to a particular builtin variable |
| 125 | class BuiltinVariableVisitor : public ProgramVisitor { |
| 126 | public: |
| 127 | BuiltinVariableVisitor(int builtin) : fBuiltin(builtin) {} |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 128 | |
| 129 | bool visitExpression(const Expression& e) override { |
Brian Osman | 79457ef | 2020-09-24 15:01:27 -0400 | [diff] [blame] | 130 | if (e.is<VariableReference>()) { |
John Stiles | 403a363 | 2020-08-20 12:11:48 -0400 | [diff] [blame] | 131 | const VariableReference& var = e.as<VariableReference>(); |
Ethan Nicholas | 7868692 | 2020-10-08 06:46:27 -0400 | [diff] [blame] | 132 | return var.variable()->modifiers().fLayout.fBuiltin == fBuiltin; |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 133 | } |
John Stiles | d7ab450 | 2020-09-24 22:41:00 -0400 | [diff] [blame] | 134 | return INHERITED::visitExpression(e); |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 135 | } |
| 136 | |
Brian Osman | 92aac1e | 2020-08-05 16:48:58 -0400 | [diff] [blame] | 137 | int fBuiltin; |
| 138 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 139 | using INHERITED = ProgramVisitor; |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 140 | }; |
| 141 | |
Brian Osman | eb0f29d | 2021-08-04 11:34:16 -0400 | [diff] [blame] | 142 | // Visitor that searches for child calls from a function other than main() |
Brian Osman | 04d79fc | 2021-07-02 13:25:35 -0400 | [diff] [blame] | 143 | class SampleOutsideMainVisitor : public ProgramVisitor { |
| 144 | public: |
| 145 | SampleOutsideMainVisitor() {} |
| 146 | |
| 147 | bool visitExpression(const Expression& e) override { |
Brian Osman | eb0f29d | 2021-08-04 11:34:16 -0400 | [diff] [blame] | 148 | if (e.is<ChildCall>()) { |
| 149 | return true; |
Brian Osman | 04d79fc | 2021-07-02 13:25:35 -0400 | [diff] [blame] | 150 | } |
| 151 | return INHERITED::visitExpression(e); |
| 152 | } |
| 153 | |
| 154 | bool visitProgramElement(const ProgramElement& p) override { |
| 155 | return p.is<FunctionDefinition>() && |
| 156 | !p.as<FunctionDefinition>().declaration().isMain() && |
| 157 | INHERITED::visitProgramElement(p); |
| 158 | } |
| 159 | |
| 160 | using INHERITED = ProgramVisitor; |
| 161 | }; |
| 162 | |
Ethan Nicholas | 6e0fa40 | 2020-08-20 14:08:23 -0400 | [diff] [blame] | 163 | // Visitor that counts the number of nodes visited |
| 164 | class NodeCountVisitor : public ProgramVisitor { |
| 165 | public: |
John Stiles | 2c1e492 | 2020-10-01 09:14:14 -0400 | [diff] [blame] | 166 | NodeCountVisitor(int limit) : fLimit(limit) {} |
| 167 | |
Ethan Nicholas | 6e0fa40 | 2020-08-20 14:08:23 -0400 | [diff] [blame] | 168 | int visit(const Statement& s) { |
Ethan Nicholas | 6e0fa40 | 2020-08-20 14:08:23 -0400 | [diff] [blame] | 169 | this->visitStatement(s); |
| 170 | return fCount; |
| 171 | } |
| 172 | |
| 173 | bool visitExpression(const Expression& e) override { |
| 174 | ++fCount; |
John Stiles | 9b9415e | 2020-11-23 14:48:06 -0500 | [diff] [blame] | 175 | return (fCount >= fLimit) || INHERITED::visitExpression(e); |
Ethan Nicholas | 6e0fa40 | 2020-08-20 14:08:23 -0400 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | bool visitProgramElement(const ProgramElement& p) override { |
| 179 | ++fCount; |
John Stiles | 9b9415e | 2020-11-23 14:48:06 -0500 | [diff] [blame] | 180 | return (fCount >= fLimit) || INHERITED::visitProgramElement(p); |
Ethan Nicholas | 6e0fa40 | 2020-08-20 14:08:23 -0400 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | bool visitStatement(const Statement& s) override { |
| 184 | ++fCount; |
John Stiles | 9b9415e | 2020-11-23 14:48:06 -0500 | [diff] [blame] | 185 | return (fCount >= fLimit) || INHERITED::visitStatement(s); |
Ethan Nicholas | 6e0fa40 | 2020-08-20 14:08:23 -0400 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | private: |
John Stiles | 2c1e492 | 2020-10-01 09:14:14 -0400 | [diff] [blame] | 189 | int fCount = 0; |
| 190 | int fLimit; |
Ethan Nicholas | 6e0fa40 | 2020-08-20 14:08:23 -0400 | [diff] [blame] | 191 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 192 | using INHERITED = ProgramVisitor; |
Ethan Nicholas | 6e0fa40 | 2020-08-20 14:08:23 -0400 | [diff] [blame] | 193 | }; |
| 194 | |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 195 | class ProgramUsageVisitor : public ProgramVisitor { |
Brian Osman | 2e25ff4 | 2020-10-15 10:32:04 -0400 | [diff] [blame] | 196 | public: |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 197 | ProgramUsageVisitor(ProgramUsage* usage, int delta) : fUsage(usage), fDelta(delta) {} |
| 198 | |
John Stiles | 39465b8 | 2021-03-18 09:19:55 -0400 | [diff] [blame] | 199 | bool visitProgramElement(const ProgramElement& pe) override { |
| 200 | if (pe.is<FunctionDefinition>()) { |
| 201 | for (const Variable* param : pe.as<FunctionDefinition>().declaration().parameters()) { |
| 202 | // Ensure function-parameter variables exist in the variable usage map. They aren't |
| 203 | // otherwise declared, but ProgramUsage::get() should be able to find them, even if |
| 204 | // they are unread and unwritten. |
| 205 | fUsage->fVariableCounts[param]; |
| 206 | } |
John Stiles | 8e2a84b | 2021-04-19 09:35:38 -0400 | [diff] [blame] | 207 | } else if (pe.is<InterfaceBlock>()) { |
| 208 | // Ensure interface-block variables exist in the variable usage map. |
| 209 | fUsage->fVariableCounts[&pe.as<InterfaceBlock>().variable()]; |
John Stiles | 39465b8 | 2021-03-18 09:19:55 -0400 | [diff] [blame] | 210 | } |
| 211 | return INHERITED::visitProgramElement(pe); |
| 212 | } |
| 213 | |
John Stiles | 04a8a54 | 2021-03-10 19:03:26 -0500 | [diff] [blame] | 214 | bool visitStatement(const Statement& s) override { |
| 215 | if (s.is<VarDeclaration>()) { |
| 216 | // Add all declared variables to the usage map (even if never otherwise accessed). |
| 217 | const VarDeclaration& vd = s.as<VarDeclaration>(); |
| 218 | ProgramUsage::VariableCounts& counts = fUsage->fVariableCounts[&vd.var()]; |
| 219 | counts.fDeclared += fDelta; |
| 220 | SkASSERT(counts.fDeclared >= 0); |
| 221 | if (vd.value()) { |
| 222 | // The initial-value expression, when present, counts as a write. |
| 223 | counts.fWrite += fDelta; |
| 224 | } |
| 225 | } |
| 226 | return INHERITED::visitStatement(s); |
| 227 | } |
| 228 | |
Brian Osman | 2e25ff4 | 2020-10-15 10:32:04 -0400 | [diff] [blame] | 229 | bool visitExpression(const Expression& e) override { |
| 230 | if (e.is<FunctionCall>()) { |
| 231 | const FunctionDeclaration* f = &e.as<FunctionCall>().function(); |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 232 | fUsage->fCallCounts[f] += fDelta; |
| 233 | SkASSERT(fUsage->fCallCounts[f] >= 0); |
| 234 | } else if (e.is<VariableReference>()) { |
| 235 | const VariableReference& ref = e.as<VariableReference>(); |
| 236 | ProgramUsage::VariableCounts& counts = fUsage->fVariableCounts[ref.variable()]; |
| 237 | switch (ref.refKind()) { |
| 238 | case VariableRefKind::kRead: |
| 239 | counts.fRead += fDelta; |
| 240 | break; |
| 241 | case VariableRefKind::kWrite: |
| 242 | counts.fWrite += fDelta; |
| 243 | break; |
| 244 | case VariableRefKind::kReadWrite: |
| 245 | case VariableRefKind::kPointer: |
| 246 | counts.fRead += fDelta; |
| 247 | counts.fWrite += fDelta; |
| 248 | break; |
| 249 | } |
| 250 | SkASSERT(counts.fRead >= 0 && counts.fWrite >= 0); |
Brian Osman | 2e25ff4 | 2020-10-15 10:32:04 -0400 | [diff] [blame] | 251 | } |
| 252 | return INHERITED::visitExpression(e); |
| 253 | } |
| 254 | |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 255 | using ProgramVisitor::visitProgramElement; |
| 256 | using ProgramVisitor::visitStatement; |
| 257 | |
| 258 | ProgramUsage* fUsage; |
| 259 | int fDelta; |
Brian Osman | 2e25ff4 | 2020-10-15 10:32:04 -0400 | [diff] [blame] | 260 | using INHERITED = ProgramVisitor; |
| 261 | }; |
| 262 | |
Ethan Nicholas | 765d2fe | 2020-08-26 08:29:55 -0400 | [diff] [blame] | 263 | class VariableWriteVisitor : public ProgramVisitor { |
| 264 | public: |
| 265 | VariableWriteVisitor(const Variable* var) |
| 266 | : fVar(var) {} |
| 267 | |
| 268 | bool visit(const Statement& s) { |
| 269 | return this->visitStatement(s); |
| 270 | } |
| 271 | |
| 272 | bool visitExpression(const Expression& e) override { |
Brian Osman | 79457ef | 2020-09-24 15:01:27 -0400 | [diff] [blame] | 273 | if (e.is<VariableReference>()) { |
Ethan Nicholas | 765d2fe | 2020-08-26 08:29:55 -0400 | [diff] [blame] | 274 | const VariableReference& ref = e.as<VariableReference>(); |
Ethan Nicholas | 453f67f | 2020-10-09 10:43:45 -0400 | [diff] [blame] | 275 | if (ref.variable() == fVar && |
| 276 | (ref.refKind() == VariableReference::RefKind::kWrite || |
| 277 | ref.refKind() == VariableReference::RefKind::kReadWrite || |
| 278 | ref.refKind() == VariableReference::RefKind::kPointer)) { |
Ethan Nicholas | 765d2fe | 2020-08-26 08:29:55 -0400 | [diff] [blame] | 279 | return true; |
| 280 | } |
| 281 | } |
John Stiles | d7ab450 | 2020-09-24 22:41:00 -0400 | [diff] [blame] | 282 | return INHERITED::visitExpression(e); |
Ethan Nicholas | 765d2fe | 2020-08-26 08:29:55 -0400 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | private: |
| 286 | const Variable* fVar; |
| 287 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 288 | using INHERITED = ProgramVisitor; |
Ethan Nicholas | 765d2fe | 2020-08-26 08:29:55 -0400 | [diff] [blame] | 289 | }; |
| 290 | |
John Stiles | a976da7 | 2020-09-25 23:06:26 -0400 | [diff] [blame] | 291 | // If a caller doesn't care about errors, we can use this trivial reporter that just counts up. |
| 292 | class TrivialErrorReporter : public ErrorReporter { |
| 293 | public: |
John Stiles | bb8cf58 | 2021-08-26 23:34:59 -0400 | [diff] [blame] | 294 | ~TrivialErrorReporter() override { this->reportPendingErrors({}); } |
Ethan Nicholas | 3272412 | 2021-09-07 13:49:07 -0400 | [diff] [blame] | 295 | void handleError(skstd::string_view, PositionInfo) override {} |
John Stiles | a976da7 | 2020-09-25 23:06:26 -0400 | [diff] [blame] | 296 | }; |
| 297 | |
John Stiles | dce4d3e | 2020-09-25 14:35:13 -0400 | [diff] [blame] | 298 | // This isn't actually using ProgramVisitor, because it only considers a subset of the fields for |
| 299 | // any given expression kind. For instance, when indexing an array (e.g. `x[1]`), we only want to |
| 300 | // know if the base (`x`) is assignable; the index expression (`1`) doesn't need to be. |
| 301 | class IsAssignableVisitor { |
| 302 | public: |
John Stiles | b21fac2 | 2020-12-04 15:36:49 -0500 | [diff] [blame] | 303 | IsAssignableVisitor(ErrorReporter* errors) : fErrors(errors) {} |
John Stiles | dce4d3e | 2020-09-25 14:35:13 -0400 | [diff] [blame] | 304 | |
John Stiles | b21fac2 | 2020-12-04 15:36:49 -0500 | [diff] [blame] | 305 | bool visit(Expression& expr, Analysis::AssignmentInfo* info) { |
Ethan Nicholas | 67a0a8a | 2021-01-13 12:36:02 -0500 | [diff] [blame] | 306 | int oldErrorCount = fErrors->errorCount(); |
John Stiles | dce4d3e | 2020-09-25 14:35:13 -0400 | [diff] [blame] | 307 | this->visitExpression(expr); |
John Stiles | b21fac2 | 2020-12-04 15:36:49 -0500 | [diff] [blame] | 308 | if (info) { |
| 309 | info->fAssignedVar = fAssignedVar; |
John Stiles | b21fac2 | 2020-12-04 15:36:49 -0500 | [diff] [blame] | 310 | } |
Ethan Nicholas | 67a0a8a | 2021-01-13 12:36:02 -0500 | [diff] [blame] | 311 | return fErrors->errorCount() == oldErrorCount; |
John Stiles | dce4d3e | 2020-09-25 14:35:13 -0400 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | void visitExpression(Expression& expr) { |
| 315 | switch (expr.kind()) { |
| 316 | case Expression::Kind::kVariableReference: { |
| 317 | VariableReference& varRef = expr.as<VariableReference>(); |
Ethan Nicholas | 7868692 | 2020-10-08 06:46:27 -0400 | [diff] [blame] | 318 | const Variable* var = varRef.variable(); |
Brian Osman | e49703f | 2021-04-19 11:15:24 -0400 | [diff] [blame] | 319 | if (var->modifiers().fFlags & (Modifiers::kConst_Flag | Modifiers::kUniform_Flag)) { |
John Stiles | a976da7 | 2020-09-25 23:06:26 -0400 | [diff] [blame] | 320 | fErrors->error(expr.fOffset, |
Ethan Nicholas | e2c4999 | 2020-10-05 11:49:11 -0400 | [diff] [blame] | 321 | "cannot modify immutable variable '" + var->name() + "'"); |
John Stiles | b21fac2 | 2020-12-04 15:36:49 -0500 | [diff] [blame] | 322 | } else { |
| 323 | SkASSERT(fAssignedVar == nullptr); |
| 324 | fAssignedVar = &varRef; |
John Stiles | dce4d3e | 2020-09-25 14:35:13 -0400 | [diff] [blame] | 325 | } |
| 326 | break; |
| 327 | } |
| 328 | case Expression::Kind::kFieldAccess: |
Ethan Nicholas | 7a95b20 | 2020-10-09 11:55:40 -0400 | [diff] [blame] | 329 | this->visitExpression(*expr.as<FieldAccess>().base()); |
John Stiles | dce4d3e | 2020-09-25 14:35:13 -0400 | [diff] [blame] | 330 | break; |
| 331 | |
| 332 | case Expression::Kind::kSwizzle: { |
| 333 | const Swizzle& swizzle = expr.as<Swizzle>(); |
| 334 | this->checkSwizzleWrite(swizzle); |
Ethan Nicholas | 6b4d581 | 2020-10-12 16:11:51 -0400 | [diff] [blame] | 335 | this->visitExpression(*swizzle.base()); |
John Stiles | dce4d3e | 2020-09-25 14:35:13 -0400 | [diff] [blame] | 336 | break; |
| 337 | } |
John Stiles | 47c0a74 | 2021-02-09 09:30:35 -0500 | [diff] [blame] | 338 | case Expression::Kind::kIndex: |
| 339 | this->visitExpression(*expr.as<IndexExpression>().base()); |
John Stiles | dce4d3e | 2020-09-25 14:35:13 -0400 | [diff] [blame] | 340 | break; |
John Stiles | 47c0a74 | 2021-02-09 09:30:35 -0500 | [diff] [blame] | 341 | |
Ethan Nicholas | c898d04 | 2021-08-28 19:53:34 -0400 | [diff] [blame] | 342 | case Expression::Kind::kPoison: |
| 343 | break; |
| 344 | |
John Stiles | dce4d3e | 2020-09-25 14:35:13 -0400 | [diff] [blame] | 345 | default: |
John Stiles | a976da7 | 2020-09-25 23:06:26 -0400 | [diff] [blame] | 346 | fErrors->error(expr.fOffset, "cannot assign to this expression"); |
John Stiles | dce4d3e | 2020-09-25 14:35:13 -0400 | [diff] [blame] | 347 | break; |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | private: |
| 352 | void checkSwizzleWrite(const Swizzle& swizzle) { |
| 353 | int bits = 0; |
John Stiles | 6e88e04 | 2021-02-19 14:09:38 -0500 | [diff] [blame] | 354 | for (int8_t idx : swizzle.components()) { |
| 355 | SkASSERT(idx >= SwizzleComponent::X && idx <= SwizzleComponent::W); |
John Stiles | dce4d3e | 2020-09-25 14:35:13 -0400 | [diff] [blame] | 356 | int bit = 1 << idx; |
| 357 | if (bits & bit) { |
John Stiles | a976da7 | 2020-09-25 23:06:26 -0400 | [diff] [blame] | 358 | fErrors->error(swizzle.fOffset, |
| 359 | "cannot write to the same swizzle field more than once"); |
John Stiles | dce4d3e | 2020-09-25 14:35:13 -0400 | [diff] [blame] | 360 | break; |
| 361 | } |
| 362 | bits |= bit; |
| 363 | } |
| 364 | } |
| 365 | |
John Stiles | a976da7 | 2020-09-25 23:06:26 -0400 | [diff] [blame] | 366 | ErrorReporter* fErrors; |
John Stiles | b21fac2 | 2020-12-04 15:36:49 -0500 | [diff] [blame] | 367 | VariableReference* fAssignedVar = nullptr; |
John Stiles | dce4d3e | 2020-09-25 14:35:13 -0400 | [diff] [blame] | 368 | |
| 369 | using INHERITED = ProgramVisitor; |
| 370 | }; |
| 371 | |
John Stiles | 642cde2 | 2021-02-23 14:57:01 -0500 | [diff] [blame] | 372 | class SwitchCaseContainsExit : public ProgramVisitor { |
| 373 | public: |
| 374 | SwitchCaseContainsExit(bool conditionalExits) : fConditionalExits(conditionalExits) {} |
| 375 | |
| 376 | bool visitStatement(const Statement& stmt) override { |
| 377 | switch (stmt.kind()) { |
| 378 | case Statement::Kind::kBlock: |
John Stiles | a0e56e3 | 2021-03-03 13:14:37 -0500 | [diff] [blame] | 379 | case Statement::Kind::kSwitchCase: |
John Stiles | 642cde2 | 2021-02-23 14:57:01 -0500 | [diff] [blame] | 380 | return INHERITED::visitStatement(stmt); |
| 381 | |
| 382 | case Statement::Kind::kReturn: |
| 383 | // Returns are an early exit regardless of the surrounding control structures. |
| 384 | return fConditionalExits ? fInConditional : !fInConditional; |
| 385 | |
| 386 | case Statement::Kind::kContinue: |
| 387 | // Continues are an early exit from switches, but not loops. |
| 388 | return !fInLoop && |
| 389 | (fConditionalExits ? fInConditional : !fInConditional); |
| 390 | |
| 391 | case Statement::Kind::kBreak: |
| 392 | // Breaks cannot escape from switches or loops. |
| 393 | return !fInLoop && !fInSwitch && |
| 394 | (fConditionalExits ? fInConditional : !fInConditional); |
| 395 | |
| 396 | case Statement::Kind::kIf: { |
| 397 | ++fInConditional; |
| 398 | bool result = INHERITED::visitStatement(stmt); |
| 399 | --fInConditional; |
| 400 | return result; |
| 401 | } |
| 402 | |
| 403 | case Statement::Kind::kFor: |
| 404 | case Statement::Kind::kDo: { |
| 405 | // Loops are treated as conditionals because a loop could potentially execute zero |
| 406 | // times. We don't have a straightforward way to determine that a loop definitely |
| 407 | // executes at least once. |
| 408 | ++fInConditional; |
| 409 | ++fInLoop; |
| 410 | bool result = INHERITED::visitStatement(stmt); |
| 411 | --fInLoop; |
| 412 | --fInConditional; |
| 413 | return result; |
| 414 | } |
| 415 | |
| 416 | case Statement::Kind::kSwitch: { |
| 417 | ++fInSwitch; |
| 418 | bool result = INHERITED::visitStatement(stmt); |
| 419 | --fInSwitch; |
| 420 | return result; |
| 421 | } |
| 422 | |
| 423 | default: |
| 424 | return false; |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | bool fConditionalExits = false; |
| 429 | int fInConditional = 0; |
| 430 | int fInLoop = 0; |
| 431 | int fInSwitch = 0; |
| 432 | using INHERITED = ProgramVisitor; |
| 433 | }; |
| 434 | |
John Stiles | b3dcbb1 | 2021-03-04 16:00:20 -0500 | [diff] [blame] | 435 | class ReturnsOnAllPathsVisitor : public ProgramVisitor { |
| 436 | public: |
| 437 | bool visitExpression(const Expression& expr) override { |
| 438 | // We can avoid processing expressions entirely. |
| 439 | return false; |
| 440 | } |
| 441 | |
| 442 | bool visitStatement(const Statement& stmt) override { |
| 443 | switch (stmt.kind()) { |
| 444 | // Returns, breaks, or continues will stop the scan, so only one of these should ever be |
| 445 | // true. |
| 446 | case Statement::Kind::kReturn: |
| 447 | fFoundReturn = true; |
| 448 | return true; |
| 449 | |
| 450 | case Statement::Kind::kBreak: |
| 451 | fFoundBreak = true; |
| 452 | return true; |
| 453 | |
| 454 | case Statement::Kind::kContinue: |
| 455 | fFoundContinue = true; |
| 456 | return true; |
| 457 | |
| 458 | case Statement::Kind::kIf: { |
| 459 | const IfStatement& i = stmt.as<IfStatement>(); |
| 460 | ReturnsOnAllPathsVisitor trueVisitor; |
| 461 | ReturnsOnAllPathsVisitor falseVisitor; |
| 462 | trueVisitor.visitStatement(*i.ifTrue()); |
| 463 | if (i.ifFalse()) { |
| 464 | falseVisitor.visitStatement(*i.ifFalse()); |
| 465 | } |
| 466 | // If either branch leads to a break or continue, we report the entire if as |
| 467 | // containing a break or continue, since we don't know which side will be reached. |
| 468 | fFoundBreak = (trueVisitor.fFoundBreak || falseVisitor.fFoundBreak); |
| 469 | fFoundContinue = (trueVisitor.fFoundContinue || falseVisitor.fFoundContinue); |
| 470 | // On the other hand, we only want to report returns that definitely happen, so we |
| 471 | // require those to be found on both sides. |
| 472 | fFoundReturn = (trueVisitor.fFoundReturn && falseVisitor.fFoundReturn); |
| 473 | return fFoundBreak || fFoundContinue || fFoundReturn; |
| 474 | } |
| 475 | case Statement::Kind::kFor: { |
| 476 | const ForStatement& f = stmt.as<ForStatement>(); |
| 477 | // We assume a for/while loop runs for at least one iteration; this isn't strictly |
| 478 | // guaranteed, but it's better to be slightly over-permissive here than to fail on |
| 479 | // reasonable code. |
| 480 | ReturnsOnAllPathsVisitor forVisitor; |
| 481 | forVisitor.visitStatement(*f.statement()); |
| 482 | // A for loop that contains a break or continue is safe; it won't exit the entire |
| 483 | // function, just the loop. So we disregard those signals. |
| 484 | fFoundReturn = forVisitor.fFoundReturn; |
| 485 | return fFoundReturn; |
| 486 | } |
| 487 | case Statement::Kind::kDo: { |
| 488 | const DoStatement& d = stmt.as<DoStatement>(); |
| 489 | // Do-while blocks are always entered at least once. |
| 490 | ReturnsOnAllPathsVisitor doVisitor; |
| 491 | doVisitor.visitStatement(*d.statement()); |
| 492 | // A do-while loop that contains a break or continue is safe; it won't exit the |
| 493 | // entire function, just the loop. So we disregard those signals. |
| 494 | fFoundReturn = doVisitor.fFoundReturn; |
| 495 | return fFoundReturn; |
| 496 | } |
| 497 | case Statement::Kind::kBlock: |
| 498 | // Blocks are definitely entered and don't imply any additional control flow. |
| 499 | // If the block contains a break, continue or return, we want to keep that. |
| 500 | return INHERITED::visitStatement(stmt); |
| 501 | |
| 502 | case Statement::Kind::kSwitch: { |
| 503 | // Switches are the most complex control flow we need to deal with; fortunately we |
| 504 | // already have good primitives for dissecting them. We need to verify that: |
| 505 | // - a default case exists, so that every possible input value is covered |
| 506 | // - every switch-case either (a) returns unconditionally, or |
| 507 | // (b) falls through to another case that does |
| 508 | const SwitchStatement& s = stmt.as<SwitchStatement>(); |
| 509 | bool foundDefault = false; |
| 510 | bool fellThrough = false; |
John Stiles | 628777c | 2021-08-04 22:07:41 -0400 | [diff] [blame] | 511 | for (const std::unique_ptr<Statement>& switchStmt : s.cases()) { |
John Stiles | b3dcbb1 | 2021-03-04 16:00:20 -0500 | [diff] [blame] | 512 | // The default case is indicated by a null value. A switch without a default |
| 513 | // case cannot definitively return, as its value might not be in the cases list. |
John Stiles | 628777c | 2021-08-04 22:07:41 -0400 | [diff] [blame] | 514 | const SwitchCase& sc = switchStmt->as<SwitchCase>(); |
John Stiles | b23a64b | 2021-03-11 08:27:59 -0500 | [diff] [blame] | 515 | if (!sc.value()) { |
John Stiles | b3dcbb1 | 2021-03-04 16:00:20 -0500 | [diff] [blame] | 516 | foundDefault = true; |
| 517 | } |
| 518 | // Scan this switch-case for any exit (break, continue or return). |
| 519 | ReturnsOnAllPathsVisitor caseVisitor; |
John Stiles | b23a64b | 2021-03-11 08:27:59 -0500 | [diff] [blame] | 520 | caseVisitor.visitStatement(sc); |
John Stiles | b3dcbb1 | 2021-03-04 16:00:20 -0500 | [diff] [blame] | 521 | |
| 522 | // If we found a break or continue, whether conditional or not, this switch case |
| 523 | // can't be called an unconditional return. Switches absorb breaks but not |
| 524 | // continues. |
| 525 | if (caseVisitor.fFoundContinue) { |
| 526 | fFoundContinue = true; |
| 527 | return false; |
| 528 | } |
| 529 | if (caseVisitor.fFoundBreak) { |
| 530 | return false; |
| 531 | } |
| 532 | // We just confirmed that there weren't any breaks or continues. If we didn't |
| 533 | // find an unconditional return either, the switch is considered fallen-through. |
| 534 | // (There might be a conditional return, but that doesn't count.) |
| 535 | fellThrough = !caseVisitor.fFoundReturn; |
| 536 | } |
| 537 | |
| 538 | // If we didn't find a default case, or the very last case fell through, this switch |
| 539 | // doesn't meet our criteria. |
| 540 | if (fellThrough || !foundDefault) { |
| 541 | return false; |
| 542 | } |
| 543 | |
| 544 | // We scanned the entire switch, found a default case, and every section either fell |
| 545 | // through or contained an unconditional return. |
| 546 | fFoundReturn = true; |
| 547 | return true; |
| 548 | } |
| 549 | |
| 550 | case Statement::Kind::kSwitchCase: |
| 551 | // Recurse into the switch-case. |
| 552 | return INHERITED::visitStatement(stmt); |
| 553 | |
| 554 | case Statement::Kind::kDiscard: |
| 555 | case Statement::Kind::kExpression: |
| 556 | case Statement::Kind::kInlineMarker: |
| 557 | case Statement::Kind::kNop: |
| 558 | case Statement::Kind::kVarDeclaration: |
| 559 | // None of these statements could contain a return. |
| 560 | break; |
| 561 | } |
| 562 | |
| 563 | return false; |
| 564 | } |
| 565 | |
| 566 | bool fFoundReturn = false; |
| 567 | bool fFoundBreak = false; |
| 568 | bool fFoundContinue = false; |
| 569 | |
| 570 | using INHERITED = ProgramVisitor; |
| 571 | }; |
| 572 | |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 573 | } // namespace |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 574 | |
| 575 | //////////////////////////////////////////////////////////////////////////////// |
| 576 | // Analysis |
| 577 | |
Brian Osman | 4d57111 | 2021-04-27 09:10:10 -0400 | [diff] [blame] | 578 | SampleUsage Analysis::GetSampleUsage(const Program& program, |
Brian Osman | 293497e | 2021-08-24 14:08:50 -0400 | [diff] [blame] | 579 | const Variable& child, |
Brian Osman | 8cdf28f | 2021-05-24 09:52:39 -0400 | [diff] [blame] | 580 | bool writesToSampleCoords, |
| 581 | int* elidedSampleCoordCount) { |
Brian Osman | 293497e | 2021-08-24 14:08:50 -0400 | [diff] [blame] | 582 | MergeSampleUsageVisitor visitor(*program.fContext, child, writesToSampleCoords); |
Brian Osman | 8cdf28f | 2021-05-24 09:52:39 -0400 | [diff] [blame] | 583 | SampleUsage result = visitor.visit(program); |
| 584 | if (elidedSampleCoordCount) { |
| 585 | *elidedSampleCoordCount += visitor.elidedSampleCoordCount(); |
| 586 | } |
| 587 | return result; |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 588 | } |
| 589 | |
Brian Osman | 92aac1e | 2020-08-05 16:48:58 -0400 | [diff] [blame] | 590 | bool Analysis::ReferencesBuiltin(const Program& program, int builtin) { |
| 591 | BuiltinVariableVisitor visitor(builtin); |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 592 | return visitor.visit(program); |
| 593 | } |
| 594 | |
Brian Osman | 92aac1e | 2020-08-05 16:48:58 -0400 | [diff] [blame] | 595 | bool Analysis::ReferencesSampleCoords(const Program& program) { |
| 596 | return Analysis::ReferencesBuiltin(program, SK_MAIN_COORDS_BUILTIN); |
| 597 | } |
| 598 | |
| 599 | bool Analysis::ReferencesFragCoords(const Program& program) { |
| 600 | return Analysis::ReferencesBuiltin(program, SK_FRAGCOORD_BUILTIN); |
| 601 | } |
| 602 | |
Brian Osman | 04d79fc | 2021-07-02 13:25:35 -0400 | [diff] [blame] | 603 | bool Analysis::CallsSampleOutsideMain(const Program& program) { |
| 604 | SampleOutsideMainVisitor visitor; |
| 605 | return visitor.visit(program); |
| 606 | } |
| 607 | |
John Stiles | 61e5e20 | 2021-09-02 09:56:31 -0400 | [diff] [blame] | 608 | bool Analysis::CheckProgramUnrolledSize(const Program& program) { |
| 609 | // We check the size of strict-ES2 programs since SkVM will completely unroll them. |
| 610 | // Note that we *cannot* safely check the program size of non-ES2 code at this time, as it is |
| 611 | // allowed to do things we can't measure (e.g. the program can contain a recursive cycle). We |
| 612 | // could, at best, compute a lower bound. |
| 613 | const Context& context = *program.fContext; |
| 614 | SkASSERT(context.fConfig->strictES2Mode()); |
| 615 | |
| 616 | // If we decide that expressions are cheaper than statements, or that certain statements are |
| 617 | // more expensive than others, etc., we can always tweak these ratios as needed. A very rough |
| 618 | // ballpark estimate is currently good enough for our purposes. |
John Stiles | a047e8b | 2021-09-15 11:41:02 -0400 | [diff] [blame] | 619 | static constexpr size_t kExpressionCost = 1; |
| 620 | static constexpr size_t kStatementCost = 1; |
| 621 | static constexpr size_t kUnknownCost = -1; |
| 622 | static constexpr size_t kProgramSizeLimit = 100000; |
| 623 | static constexpr size_t kProgramStackDepthLimit = 50; |
John Stiles | 61e5e20 | 2021-09-02 09:56:31 -0400 | [diff] [blame] | 624 | |
| 625 | class ProgramSizeVisitor : public ProgramVisitor { |
| 626 | public: |
John Stiles | 6475b10 | 2021-09-02 14:09:23 -0400 | [diff] [blame] | 627 | ProgramSizeVisitor(const Context& c) : fContext(c) {} |
John Stiles | 61e5e20 | 2021-09-02 09:56:31 -0400 | [diff] [blame] | 628 | |
| 629 | using ProgramVisitor::visitProgramElement; |
| 630 | |
John Stiles | a047e8b | 2021-09-15 11:41:02 -0400 | [diff] [blame] | 631 | size_t functionSize() const { |
John Stiles | 6475b10 | 2021-09-02 14:09:23 -0400 | [diff] [blame] | 632 | return fFunctionSize; |
| 633 | } |
| 634 | |
| 635 | bool visitProgramElement(const ProgramElement& pe) override { |
| 636 | if (pe.is<FunctionDefinition>()) { |
| 637 | // Check the function-size cache map first. We don't need to visit this function if |
| 638 | // we already processed it before. |
| 639 | const FunctionDeclaration* decl = &pe.as<FunctionDefinition>().declaration(); |
| 640 | auto [iter, wasInserted] = fFunctionCostMap.insert({decl, kUnknownCost}); |
| 641 | if (!wasInserted) { |
| 642 | // We already have this function in our map. We don't need to check it again. |
| 643 | if (iter->second == kUnknownCost) { |
| 644 | // If the function is present in the map with an unknown cost, we're |
| 645 | // recursively processing it--in other words, we found a cycle in the code. |
John Stiles | 98ddea0 | 2021-09-02 14:33:08 -0400 | [diff] [blame] | 646 | // Unwind our stack into a string. |
| 647 | String msg = "\n\t" + decl->description(); |
| 648 | for (auto unwind = fStack.rbegin(); unwind != fStack.rend(); ++unwind) { |
| 649 | msg = "\n\t" + (*unwind)->description() + msg; |
| 650 | if (*unwind == decl) { |
| 651 | break; |
| 652 | } |
| 653 | } |
| 654 | msg = "potential recursion (function call cycle) not allowed:" + msg; |
| 655 | fContext.fErrors->error(pe.fOffset, std::move(msg)); |
John Stiles | 6475b10 | 2021-09-02 14:09:23 -0400 | [diff] [blame] | 656 | fFunctionSize = iter->second = 0; |
| 657 | return true; |
| 658 | } |
| 659 | // Set the size to its known value. |
| 660 | fFunctionSize = iter->second; |
| 661 | return false; |
| 662 | } |
| 663 | |
John Stiles | 2af4b13 | 2021-09-02 16:17:07 -0400 | [diff] [blame] | 664 | // If the function-call stack has gotten too deep, stop the analysis. |
| 665 | if (fStack.size() >= kProgramStackDepthLimit) { |
| 666 | String msg = "exceeded max function call depth:"; |
| 667 | for (auto unwind = fStack.begin(); unwind != fStack.end(); ++unwind) { |
| 668 | msg += "\n\t" + (*unwind)->description(); |
| 669 | } |
| 670 | msg += "\n\t" + decl->description(); |
| 671 | fContext.fErrors->error(pe.fOffset, std::move(msg)); |
| 672 | fFunctionSize = iter->second = 0; |
| 673 | return true; |
| 674 | } |
| 675 | |
John Stiles | 6475b10 | 2021-09-02 14:09:23 -0400 | [diff] [blame] | 676 | // Calculate the function cost and store it in our cache. |
John Stiles | 98ddea0 | 2021-09-02 14:33:08 -0400 | [diff] [blame] | 677 | fStack.push_back(decl); |
John Stiles | 6475b10 | 2021-09-02 14:09:23 -0400 | [diff] [blame] | 678 | fFunctionSize = 0; |
John Stiles | 6475b10 | 2021-09-02 14:09:23 -0400 | [diff] [blame] | 679 | bool result = INHERITED::visitProgramElement(pe); |
| 680 | iter->second = fFunctionSize; |
John Stiles | 98ddea0 | 2021-09-02 14:33:08 -0400 | [diff] [blame] | 681 | fStack.pop_back(); |
| 682 | |
John Stiles | 6475b10 | 2021-09-02 14:09:23 -0400 | [diff] [blame] | 683 | return result; |
| 684 | } |
| 685 | |
| 686 | return INHERITED::visitProgramElement(pe); |
John Stiles | 61e5e20 | 2021-09-02 09:56:31 -0400 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | bool visitStatement(const Statement& stmt) override { |
| 690 | switch (stmt.kind()) { |
| 691 | case Statement::Kind::kFor: { |
| 692 | // We count a for-loop's unrolled size here. We expect that the init statement |
| 693 | // will be emitted once, and the next-expr and statement will be repeated in the |
| 694 | // output for every iteration of the loop. The test-expr is optimized away |
| 695 | // during the unroll and is not counted at all. |
| 696 | const ForStatement& forStmt = stmt.as<ForStatement>(); |
John Stiles | a047e8b | 2021-09-15 11:41:02 -0400 | [diff] [blame] | 697 | bool result = this->visitStatement(*forStmt.initializer()); |
John Stiles | 61e5e20 | 2021-09-02 09:56:31 -0400 | [diff] [blame] | 698 | |
John Stiles | a047e8b | 2021-09-15 11:41:02 -0400 | [diff] [blame] | 699 | size_t originalFunctionSize = fFunctionSize; |
| 700 | fFunctionSize = 0; |
John Stiles | 61e5e20 | 2021-09-02 09:56:31 -0400 | [diff] [blame] | 701 | |
John Stiles | 78b84cc | 2021-09-15 09:44:54 -0400 | [diff] [blame] | 702 | result = this->visitExpression(*forStmt.next()) || |
| 703 | this->visitStatement(*forStmt.statement()) || result; |
John Stiles | 61e5e20 | 2021-09-02 09:56:31 -0400 | [diff] [blame] | 704 | |
John Stiles | a047e8b | 2021-09-15 11:41:02 -0400 | [diff] [blame] | 705 | if (const LoopUnrollInfo* unrollInfo = forStmt.unrollInfo()) { |
| 706 | fFunctionSize = SkSafeMath::Mul(fFunctionSize, unrollInfo->fCount); |
| 707 | } else { |
| 708 | SkDEBUGFAIL("for-loops should always have unroll info in an ES2 program"); |
| 709 | } |
| 710 | |
| 711 | fFunctionSize = SkSafeMath::Add(fFunctionSize, originalFunctionSize); |
John Stiles | 61e5e20 | 2021-09-02 09:56:31 -0400 | [diff] [blame] | 712 | return result; |
| 713 | } |
| 714 | |
| 715 | case Statement::Kind::kExpression: |
| 716 | // The cost of an expression-statement is counted in visitExpression. It would |
| 717 | // be double-dipping to count it here too. |
| 718 | break; |
| 719 | |
| 720 | case Statement::Kind::kInlineMarker: |
| 721 | case Statement::Kind::kNop: |
| 722 | case Statement::Kind::kVarDeclaration: |
| 723 | // These statements don't directly consume any space in a compiled program. |
| 724 | break; |
| 725 | |
| 726 | case Statement::Kind::kDo: |
John Stiles | 61e5e20 | 2021-09-02 09:56:31 -0400 | [diff] [blame] | 727 | SkDEBUGFAIL("encountered a statement that shouldn't exist in an ES2 program"); |
| 728 | break; |
| 729 | |
| 730 | default: |
John Stiles | a047e8b | 2021-09-15 11:41:02 -0400 | [diff] [blame] | 731 | fFunctionSize = SkSafeMath::Add(fFunctionSize, kStatementCost); |
John Stiles | 61e5e20 | 2021-09-02 09:56:31 -0400 | [diff] [blame] | 732 | break; |
| 733 | } |
| 734 | |
John Stiles | a047e8b | 2021-09-15 11:41:02 -0400 | [diff] [blame] | 735 | bool earlyExit = fFunctionSize > kProgramSizeLimit; |
| 736 | return earlyExit || INHERITED::visitStatement(stmt); |
John Stiles | 61e5e20 | 2021-09-02 09:56:31 -0400 | [diff] [blame] | 737 | } |
| 738 | |
| 739 | bool visitExpression(const Expression& expr) override { |
| 740 | // Other than function calls, all expressions are assumed to have a fixed unit cost. |
John Stiles | 6475b10 | 2021-09-02 14:09:23 -0400 | [diff] [blame] | 741 | bool earlyExit = false; |
John Stiles | a047e8b | 2021-09-15 11:41:02 -0400 | [diff] [blame] | 742 | size_t expressionCost = kExpressionCost; |
John Stiles | 61e5e20 | 2021-09-02 09:56:31 -0400 | [diff] [blame] | 743 | |
| 744 | if (expr.is<FunctionCall>()) { |
John Stiles | 6475b10 | 2021-09-02 14:09:23 -0400 | [diff] [blame] | 745 | // Visit this function call to calculate its size. If we've already sized it, this |
| 746 | // will retrieve the size from our cache. |
John Stiles | 61e5e20 | 2021-09-02 09:56:31 -0400 | [diff] [blame] | 747 | const FunctionCall& call = expr.as<FunctionCall>(); |
| 748 | const FunctionDeclaration* decl = &call.function(); |
John Stiles | 61e5e20 | 2021-09-02 09:56:31 -0400 | [diff] [blame] | 749 | if (decl->definition() && !decl->isIntrinsic()) { |
John Stiles | a047e8b | 2021-09-15 11:41:02 -0400 | [diff] [blame] | 750 | size_t originalFunctionSize = fFunctionSize; |
| 751 | fFunctionSize = 0; |
John Stiles | 61e5e20 | 2021-09-02 09:56:31 -0400 | [diff] [blame] | 752 | |
John Stiles | 6475b10 | 2021-09-02 14:09:23 -0400 | [diff] [blame] | 753 | earlyExit = this->visitProgramElement(*decl->definition()); |
| 754 | expressionCost = fFunctionSize; |
John Stiles | 61e5e20 | 2021-09-02 09:56:31 -0400 | [diff] [blame] | 755 | |
John Stiles | 6475b10 | 2021-09-02 14:09:23 -0400 | [diff] [blame] | 756 | fFunctionSize = originalFunctionSize; |
John Stiles | 61e5e20 | 2021-09-02 09:56:31 -0400 | [diff] [blame] | 757 | } |
| 758 | } |
| 759 | |
John Stiles | a047e8b | 2021-09-15 11:41:02 -0400 | [diff] [blame] | 760 | fFunctionSize = SkSafeMath::Add(fFunctionSize, expressionCost); |
John Stiles | 6475b10 | 2021-09-02 14:09:23 -0400 | [diff] [blame] | 761 | return earlyExit || INHERITED::visitExpression(expr); |
John Stiles | 61e5e20 | 2021-09-02 09:56:31 -0400 | [diff] [blame] | 762 | } |
| 763 | |
| 764 | private: |
| 765 | using INHERITED = ProgramVisitor; |
| 766 | |
John Stiles | 36ddccc | 2021-09-03 10:57:04 -0400 | [diff] [blame] | 767 | const Context& fContext; |
John Stiles | a047e8b | 2021-09-15 11:41:02 -0400 | [diff] [blame] | 768 | size_t fFunctionSize = 0; |
| 769 | std::unordered_map<const FunctionDeclaration*, size_t> fFunctionCostMap; |
John Stiles | 98ddea0 | 2021-09-02 14:33:08 -0400 | [diff] [blame] | 770 | std::vector<const FunctionDeclaration*> fStack; |
John Stiles | 61e5e20 | 2021-09-02 09:56:31 -0400 | [diff] [blame] | 771 | }; |
| 772 | |
John Stiles | 6475b10 | 2021-09-02 14:09:23 -0400 | [diff] [blame] | 773 | // Process every function in our program. |
| 774 | ProgramSizeVisitor visitor{context}; |
John Stiles | 61e5e20 | 2021-09-02 09:56:31 -0400 | [diff] [blame] | 775 | for (const std::unique_ptr<ProgramElement>& element : program.ownedElements()) { |
John Stiles | 6475b10 | 2021-09-02 14:09:23 -0400 | [diff] [blame] | 776 | if (element->is<FunctionDefinition>()) { |
| 777 | // Visit every function--we want to detect static recursion and report it as an error, |
| 778 | // even in unreferenced functions. |
John Stiles | 61e5e20 | 2021-09-02 09:56:31 -0400 | [diff] [blame] | 779 | visitor.visitProgramElement(*element); |
John Stiles | 6475b10 | 2021-09-02 14:09:23 -0400 | [diff] [blame] | 780 | // Report an error when main()'s flattened size is larger than our program limit. |
| 781 | if (visitor.functionSize() > kProgramSizeLimit && |
| 782 | element->as<FunctionDefinition>().declaration().isMain()) { |
| 783 | context.fErrors->error(/*offset=*/-1, "program is too large"); |
| 784 | } |
John Stiles | 61e5e20 | 2021-09-02 09:56:31 -0400 | [diff] [blame] | 785 | } |
| 786 | } |
| 787 | |
John Stiles | 61e5e20 | 2021-09-02 09:56:31 -0400 | [diff] [blame] | 788 | return true; |
| 789 | } |
| 790 | |
Ethan Nicholas | 98eae1e | 2021-09-01 14:37:29 -0400 | [diff] [blame] | 791 | bool Analysis::DetectVarDeclarationWithoutScope(const Statement& stmt, ErrorReporter* errors) { |
| 792 | // A variable declaration can create either a lone VarDeclaration or an unscoped Block |
| 793 | // containing multiple VarDeclaration statements. We need to detect either case. |
| 794 | const Variable* var; |
| 795 | if (stmt.is<VarDeclaration>()) { |
| 796 | // The single-variable case. No blocks at all. |
| 797 | var = &stmt.as<VarDeclaration>().var(); |
| 798 | } else if (stmt.is<Block>()) { |
| 799 | // The multiple-variable case: an unscoped, non-empty block... |
| 800 | const Block& block = stmt.as<Block>(); |
| 801 | if (block.isScope() || block.children().empty()) { |
| 802 | return false; |
| 803 | } |
| 804 | // ... holding a variable declaration. |
| 805 | const Statement& innerStmt = *block.children().front(); |
| 806 | if (!innerStmt.is<VarDeclaration>()) { |
| 807 | return false; |
| 808 | } |
| 809 | var = &innerStmt.as<VarDeclaration>().var(); |
| 810 | } else { |
| 811 | // This statement wasn't a variable declaration. No problem. |
| 812 | return false; |
| 813 | } |
| 814 | |
| 815 | // Report an error. |
| 816 | SkASSERT(var); |
| 817 | if (errors) { |
| 818 | errors->error(stmt.fOffset, "variable '" + var->name() + "' must be created in a scope"); |
| 819 | } |
| 820 | return true; |
| 821 | } |
| 822 | |
John Stiles | 9b9415e | 2020-11-23 14:48:06 -0500 | [diff] [blame] | 823 | int Analysis::NodeCountUpToLimit(const FunctionDefinition& function, int limit) { |
| 824 | return NodeCountVisitor{limit}.visit(*function.body()); |
Ethan Nicholas | 6e0fa40 | 2020-08-20 14:08:23 -0400 | [diff] [blame] | 825 | } |
| 826 | |
John Stiles | 642cde2 | 2021-02-23 14:57:01 -0500 | [diff] [blame] | 827 | bool Analysis::SwitchCaseContainsUnconditionalExit(Statement& stmt) { |
| 828 | return SwitchCaseContainsExit{/*conditionalExits=*/false}.visitStatement(stmt); |
| 829 | } |
| 830 | |
| 831 | bool Analysis::SwitchCaseContainsConditionalExit(Statement& stmt) { |
| 832 | return SwitchCaseContainsExit{/*conditionalExits=*/true}.visitStatement(stmt); |
| 833 | } |
| 834 | |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 835 | std::unique_ptr<ProgramUsage> Analysis::GetUsage(const Program& program) { |
| 836 | auto usage = std::make_unique<ProgramUsage>(); |
| 837 | ProgramUsageVisitor addRefs(usage.get(), /*delta=*/+1); |
| 838 | addRefs.visit(program); |
| 839 | return usage; |
| 840 | } |
| 841 | |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 842 | std::unique_ptr<ProgramUsage> Analysis::GetUsage(const LoadedModule& module) { |
| 843 | auto usage = std::make_unique<ProgramUsage>(); |
| 844 | ProgramUsageVisitor addRefs(usage.get(), /*delta=*/+1); |
| 845 | for (const auto& element : module.fElements) { |
| 846 | addRefs.visitProgramElement(*element); |
| 847 | } |
| 848 | return usage; |
| 849 | } |
| 850 | |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 851 | ProgramUsage::VariableCounts ProgramUsage::get(const Variable& v) const { |
John Stiles | 04a8a54 | 2021-03-10 19:03:26 -0500 | [diff] [blame] | 852 | const VariableCounts* counts = fVariableCounts.find(&v); |
| 853 | SkASSERT(counts); |
| 854 | return *counts; |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 855 | } |
| 856 | |
| 857 | bool ProgramUsage::isDead(const Variable& v) const { |
| 858 | const Modifiers& modifiers = v.modifiers(); |
| 859 | VariableCounts counts = this->get(v); |
| 860 | if ((v.storage() != Variable::Storage::kLocal && counts.fRead) || |
Brian Osman | e49703f | 2021-04-19 11:15:24 -0400 | [diff] [blame] | 861 | (modifiers.fFlags & |
| 862 | (Modifiers::kIn_Flag | Modifiers::kOut_Flag | Modifiers::kUniform_Flag))) { |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 863 | return false; |
| 864 | } |
John Stiles | f10eff3 | 2021-03-16 10:57:55 -0400 | [diff] [blame] | 865 | // Consider the variable dead if it's never read and never written (besides the initial-value). |
| 866 | return !counts.fRead && (counts.fWrite <= (v.initialValue() ? 1 : 0)); |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 867 | } |
| 868 | |
| 869 | int ProgramUsage::get(const FunctionDeclaration& f) const { |
| 870 | const int* count = fCallCounts.find(&f); |
| 871 | return count ? *count : 0; |
| 872 | } |
| 873 | |
John Stiles | 60dbf07 | 2021-08-02 14:22:34 -0400 | [diff] [blame] | 874 | void ProgramUsage::add(const Expression* expr) { |
| 875 | ProgramUsageVisitor addRefs(this, /*delta=*/+1); |
| 876 | addRefs.visitExpression(*expr); |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 877 | } |
| 878 | |
| 879 | void ProgramUsage::add(const Statement* stmt) { |
| 880 | ProgramUsageVisitor addRefs(this, /*delta=*/+1); |
| 881 | addRefs.visitStatement(*stmt); |
| 882 | } |
| 883 | |
John Stiles | 60dbf07 | 2021-08-02 14:22:34 -0400 | [diff] [blame] | 884 | void ProgramUsage::add(const ProgramElement& element) { |
| 885 | ProgramUsageVisitor addRefs(this, /*delta=*/+1); |
| 886 | addRefs.visitProgramElement(element); |
| 887 | } |
| 888 | |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 889 | void ProgramUsage::remove(const Expression* expr) { |
| 890 | ProgramUsageVisitor subRefs(this, /*delta=*/-1); |
| 891 | subRefs.visitExpression(*expr); |
| 892 | } |
| 893 | |
| 894 | void ProgramUsage::remove(const Statement* stmt) { |
| 895 | ProgramUsageVisitor subRefs(this, /*delta=*/-1); |
| 896 | subRefs.visitStatement(*stmt); |
| 897 | } |
| 898 | |
| 899 | void ProgramUsage::remove(const ProgramElement& element) { |
| 900 | ProgramUsageVisitor subRefs(this, /*delta=*/-1); |
| 901 | subRefs.visitProgramElement(element); |
Brian Osman | 2e25ff4 | 2020-10-15 10:32:04 -0400 | [diff] [blame] | 902 | } |
| 903 | |
Ethan Nicholas | 765d2fe | 2020-08-26 08:29:55 -0400 | [diff] [blame] | 904 | bool Analysis::StatementWritesToVariable(const Statement& stmt, const Variable& var) { |
| 905 | return VariableWriteVisitor(&var).visit(stmt); |
| 906 | } |
| 907 | |
John Stiles | b21fac2 | 2020-12-04 15:36:49 -0500 | [diff] [blame] | 908 | bool Analysis::IsAssignable(Expression& expr, AssignmentInfo* info, ErrorReporter* errors) { |
John Stiles | a976da7 | 2020-09-25 23:06:26 -0400 | [diff] [blame] | 909 | TrivialErrorReporter trivialErrors; |
John Stiles | b21fac2 | 2020-12-04 15:36:49 -0500 | [diff] [blame] | 910 | return IsAssignableVisitor{errors ? errors : &trivialErrors}.visit(expr, info); |
John Stiles | dce4d3e | 2020-09-25 14:35:13 -0400 | [diff] [blame] | 911 | } |
| 912 | |
John Stiles | bb8cf58 | 2021-08-26 23:34:59 -0400 | [diff] [blame] | 913 | bool Analysis::UpdateVariableRefKind(Expression* expr, |
| 914 | VariableReference::RefKind kind, |
| 915 | ErrorReporter* errors) { |
John Stiles | 516704b | 2021-02-26 15:01:57 -0500 | [diff] [blame] | 916 | Analysis::AssignmentInfo info; |
| 917 | if (!Analysis::IsAssignable(*expr, &info, errors)) { |
| 918 | return false; |
| 919 | } |
| 920 | if (!info.fAssignedVar) { |
John Stiles | bb8cf58 | 2021-08-26 23:34:59 -0400 | [diff] [blame] | 921 | if (errors) { |
| 922 | errors->error(expr->fOffset, "can't assign to expression '" + |
| 923 | expr->description() + "'"); |
| 924 | } |
John Stiles | 516704b | 2021-02-26 15:01:57 -0500 | [diff] [blame] | 925 | return false; |
| 926 | } |
| 927 | info.fAssignedVar->setRefKind(kind); |
| 928 | return true; |
| 929 | } |
| 930 | |
John Stiles | c30fbca | 2020-11-19 16:25:49 -0500 | [diff] [blame] | 931 | bool Analysis::IsTrivialExpression(const Expression& expr) { |
John Stiles | 7591d4b | 2021-09-13 13:32:06 -0400 | [diff] [blame] | 932 | return expr.is<Literal>() || |
John Stiles | c30fbca | 2020-11-19 16:25:49 -0500 | [diff] [blame] | 933 | expr.is<VariableReference>() || |
| 934 | (expr.is<Swizzle>() && |
| 935 | IsTrivialExpression(*expr.as<Swizzle>().base())) || |
| 936 | (expr.is<FieldAccess>() && |
| 937 | IsTrivialExpression(*expr.as<FieldAccess>().base())) || |
John Stiles | 2938eea | 2021-04-01 18:58:25 -0400 | [diff] [blame] | 938 | (expr.isAnyConstructor() && |
| 939 | expr.asAnyConstructor().argumentSpan().size() == 1 && |
| 940 | IsTrivialExpression(*expr.asAnyConstructor().argumentSpan().front())) || |
| 941 | (expr.isAnyConstructor() && |
John Stiles | c30fbca | 2020-11-19 16:25:49 -0500 | [diff] [blame] | 942 | expr.isConstantOrUniform()) || |
| 943 | (expr.is<IndexExpression>() && |
John Stiles | 7591d4b | 2021-09-13 13:32:06 -0400 | [diff] [blame] | 944 | expr.as<IndexExpression>().index()->isIntLiteral() && |
John Stiles | c30fbca | 2020-11-19 16:25:49 -0500 | [diff] [blame] | 945 | IsTrivialExpression(*expr.as<IndexExpression>().base())); |
| 946 | } |
| 947 | |
John Stiles | 5676c57 | 2021-03-08 17:10:52 -0500 | [diff] [blame] | 948 | bool Analysis::IsSameExpressionTree(const Expression& left, const Expression& right) { |
John Stiles | 95d0bad | 2021-03-01 17:02:28 -0500 | [diff] [blame] | 949 | if (left.kind() != right.kind() || left.type() != right.type()) { |
| 950 | return false; |
| 951 | } |
| 952 | |
John Stiles | 5676c57 | 2021-03-08 17:10:52 -0500 | [diff] [blame] | 953 | // This isn't a fully exhaustive list of expressions by any stretch of the imagination; for |
| 954 | // instance, `x[y+1] = x[y+1]` isn't detected because we don't look at BinaryExpressions. |
| 955 | // Since this is intended to be used for optimization purposes, handling the common cases is |
| 956 | // sufficient. |
John Stiles | 95d0bad | 2021-03-01 17:02:28 -0500 | [diff] [blame] | 957 | switch (left.kind()) { |
John Stiles | 7591d4b | 2021-09-13 13:32:06 -0400 | [diff] [blame] | 958 | case Expression::Kind::kLiteral: |
| 959 | return left.as<Literal>().value() == right.as<Literal>().value(); |
John Stiles | 5676c57 | 2021-03-08 17:10:52 -0500 | [diff] [blame] | 960 | |
John Stiles | 7384b37 | 2021-04-01 13:48:15 -0400 | [diff] [blame] | 961 | case Expression::Kind::kConstructorArray: |
John Stiles | e3ae968 | 2021-08-05 10:35:01 -0400 | [diff] [blame] | 962 | case Expression::Kind::kConstructorArrayCast: |
John Stiles | 8cad637 | 2021-04-07 12:31:13 -0400 | [diff] [blame] | 963 | case Expression::Kind::kConstructorCompound: |
| 964 | case Expression::Kind::kConstructorCompoundCast: |
John Stiles | 2938eea | 2021-04-01 18:58:25 -0400 | [diff] [blame] | 965 | case Expression::Kind::kConstructorDiagonalMatrix: |
John Stiles | 5abb9e1 | 2021-04-06 13:47:19 -0400 | [diff] [blame] | 966 | case Expression::Kind::kConstructorMatrixResize: |
John Stiles | fd7252f | 2021-04-04 22:24:40 -0400 | [diff] [blame] | 967 | case Expression::Kind::kConstructorScalarCast: |
John Stiles | d47330f | 2021-04-08 23:25:52 -0400 | [diff] [blame] | 968 | case Expression::Kind::kConstructorStruct: |
John Stiles | 268a73f | 2021-04-07 12:30:22 -0400 | [diff] [blame] | 969 | case Expression::Kind::kConstructorSplat: { |
John Stiles | fd7252f | 2021-04-04 22:24:40 -0400 | [diff] [blame] | 970 | if (left.kind() != right.kind()) { |
| 971 | return false; |
| 972 | } |
John Stiles | 7384b37 | 2021-04-01 13:48:15 -0400 | [diff] [blame] | 973 | const AnyConstructor& leftCtor = left.asAnyConstructor(); |
| 974 | const AnyConstructor& rightCtor = right.asAnyConstructor(); |
| 975 | const auto leftSpan = leftCtor.argumentSpan(); |
| 976 | const auto rightSpan = rightCtor.argumentSpan(); |
| 977 | if (leftSpan.size() != rightSpan.size()) { |
John Stiles | 5676c57 | 2021-03-08 17:10:52 -0500 | [diff] [blame] | 978 | return false; |
| 979 | } |
John Stiles | 7384b37 | 2021-04-01 13:48:15 -0400 | [diff] [blame] | 980 | for (size_t index = 0; index < leftSpan.size(); ++index) { |
| 981 | if (!IsSameExpressionTree(*leftSpan[index], *rightSpan[index])) { |
John Stiles | 5676c57 | 2021-03-08 17:10:52 -0500 | [diff] [blame] | 982 | return false; |
| 983 | } |
| 984 | } |
| 985 | return true; |
| 986 | } |
John Stiles | 95d0bad | 2021-03-01 17:02:28 -0500 | [diff] [blame] | 987 | case Expression::Kind::kFieldAccess: |
| 988 | return left.as<FieldAccess>().fieldIndex() == right.as<FieldAccess>().fieldIndex() && |
John Stiles | 5676c57 | 2021-03-08 17:10:52 -0500 | [diff] [blame] | 989 | IsSameExpressionTree(*left.as<FieldAccess>().base(), |
| 990 | *right.as<FieldAccess>().base()); |
John Stiles | 95d0bad | 2021-03-01 17:02:28 -0500 | [diff] [blame] | 991 | |
| 992 | case Expression::Kind::kIndex: |
John Stiles | 5676c57 | 2021-03-08 17:10:52 -0500 | [diff] [blame] | 993 | return IsSameExpressionTree(*left.as<IndexExpression>().index(), |
| 994 | *right.as<IndexExpression>().index()) && |
| 995 | IsSameExpressionTree(*left.as<IndexExpression>().base(), |
| 996 | *right.as<IndexExpression>().base()); |
John Stiles | 95d0bad | 2021-03-01 17:02:28 -0500 | [diff] [blame] | 997 | |
| 998 | case Expression::Kind::kSwizzle: |
| 999 | return left.as<Swizzle>().components() == right.as<Swizzle>().components() && |
John Stiles | 5676c57 | 2021-03-08 17:10:52 -0500 | [diff] [blame] | 1000 | IsSameExpressionTree(*left.as<Swizzle>().base(), *right.as<Swizzle>().base()); |
John Stiles | 95d0bad | 2021-03-01 17:02:28 -0500 | [diff] [blame] | 1001 | |
| 1002 | case Expression::Kind::kVariableReference: |
| 1003 | return left.as<VariableReference>().variable() == |
| 1004 | right.as<VariableReference>().variable(); |
| 1005 | |
| 1006 | default: |
| 1007 | return false; |
| 1008 | } |
| 1009 | } |
| 1010 | |
John Stiles | 232b4ce | 2021-03-01 22:14:22 -0500 | [diff] [blame] | 1011 | static const char* invalid_for_ES2(int offset, |
| 1012 | const Statement* loopInitializer, |
| 1013 | const Expression* loopTest, |
| 1014 | const Expression* loopNext, |
| 1015 | const Statement* loopStatement, |
John Stiles | 9c975c5 | 2021-08-31 10:18:57 -0400 | [diff] [blame] | 1016 | LoopUnrollInfo& loopInfo) { |
Brian Osman | 77ba810 | 2021-01-12 17:15:30 -0500 | [diff] [blame] | 1017 | // |
| 1018 | // init_declaration has the form: type_specifier identifier = constant_expression |
| 1019 | // |
John Stiles | 232b4ce | 2021-03-01 22:14:22 -0500 | [diff] [blame] | 1020 | if (!loopInitializer) { |
Brian Osman | 77ba810 | 2021-01-12 17:15:30 -0500 | [diff] [blame] | 1021 | return "missing init declaration"; |
| 1022 | } |
John Stiles | 232b4ce | 2021-03-01 22:14:22 -0500 | [diff] [blame] | 1023 | if (!loopInitializer->is<VarDeclaration>()) { |
Brian Osman | 77ba810 | 2021-01-12 17:15:30 -0500 | [diff] [blame] | 1024 | return "invalid init declaration"; |
| 1025 | } |
John Stiles | 232b4ce | 2021-03-01 22:14:22 -0500 | [diff] [blame] | 1026 | const VarDeclaration& initDecl = loopInitializer->as<VarDeclaration>(); |
Brian Osman | 6c7910e | 2021-01-14 09:50:52 -0500 | [diff] [blame] | 1027 | if (!initDecl.baseType().isNumber()) { |
Brian Osman | 77ba810 | 2021-01-12 17:15:30 -0500 | [diff] [blame] | 1028 | return "invalid type for loop index"; |
| 1029 | } |
| 1030 | if (initDecl.arraySize() != 0) { |
| 1031 | return "invalid type for loop index"; |
| 1032 | } |
| 1033 | if (!initDecl.value()) { |
| 1034 | return "missing loop index initializer"; |
| 1035 | } |
Brian Osman | 448b2d5 | 2021-09-23 11:36:15 -0400 | [diff] [blame] | 1036 | if (!ConstantFolder::GetConstantValue(*initDecl.value(), &loopInfo.fStart)) { |
Brian Osman | 77ba810 | 2021-01-12 17:15:30 -0500 | [diff] [blame] | 1037 | return "loop index initializer must be a constant expression"; |
| 1038 | } |
| 1039 | |
| 1040 | loopInfo.fIndex = &initDecl.var(); |
| 1041 | |
| 1042 | auto is_loop_index = [&](const std::unique_ptr<Expression>& expr) { |
| 1043 | return expr->is<VariableReference>() && |
| 1044 | expr->as<VariableReference>().variable() == loopInfo.fIndex; |
| 1045 | }; |
| 1046 | |
| 1047 | // |
| 1048 | // condition has the form: loop_index relational_operator constant_expression |
| 1049 | // |
John Stiles | 232b4ce | 2021-03-01 22:14:22 -0500 | [diff] [blame] | 1050 | if (!loopTest) { |
Brian Osman | 77ba810 | 2021-01-12 17:15:30 -0500 | [diff] [blame] | 1051 | return "missing condition"; |
| 1052 | } |
John Stiles | 232b4ce | 2021-03-01 22:14:22 -0500 | [diff] [blame] | 1053 | if (!loopTest->is<BinaryExpression>()) { |
Brian Osman | 77ba810 | 2021-01-12 17:15:30 -0500 | [diff] [blame] | 1054 | return "invalid condition"; |
| 1055 | } |
John Stiles | 232b4ce | 2021-03-01 22:14:22 -0500 | [diff] [blame] | 1056 | const BinaryExpression& cond = loopTest->as<BinaryExpression>(); |
Brian Osman | 77ba810 | 2021-01-12 17:15:30 -0500 | [diff] [blame] | 1057 | if (!is_loop_index(cond.left())) { |
| 1058 | return "expected loop index on left hand side of condition"; |
| 1059 | } |
| 1060 | // relational_operator is one of: > >= < <= == or != |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 1061 | switch (cond.getOperator().kind()) { |
Brian Osman | 77ba810 | 2021-01-12 17:15:30 -0500 | [diff] [blame] | 1062 | case Token::Kind::TK_GT: |
| 1063 | case Token::Kind::TK_GTEQ: |
| 1064 | case Token::Kind::TK_LT: |
| 1065 | case Token::Kind::TK_LTEQ: |
| 1066 | case Token::Kind::TK_EQEQ: |
| 1067 | case Token::Kind::TK_NEQ: |
| 1068 | break; |
| 1069 | default: |
| 1070 | return "invalid relational operator"; |
| 1071 | } |
| 1072 | double loopEnd = 0; |
Brian Osman | 448b2d5 | 2021-09-23 11:36:15 -0400 | [diff] [blame] | 1073 | if (!ConstantFolder::GetConstantValue(*cond.right(), &loopEnd)) { |
Brian Osman | 77ba810 | 2021-01-12 17:15:30 -0500 | [diff] [blame] | 1074 | return "loop index must be compared with a constant expression"; |
| 1075 | } |
| 1076 | |
| 1077 | // |
| 1078 | // expression has one of the following forms: |
| 1079 | // loop_index++ |
| 1080 | // loop_index-- |
| 1081 | // loop_index += constant_expression |
| 1082 | // loop_index -= constant_expression |
| 1083 | // The spec doesn't mention prefix increment and decrement, but there is some consensus that |
| 1084 | // it's an oversight, so we allow those as well. |
| 1085 | // |
John Stiles | 232b4ce | 2021-03-01 22:14:22 -0500 | [diff] [blame] | 1086 | if (!loopNext) { |
Brian Osman | 77ba810 | 2021-01-12 17:15:30 -0500 | [diff] [blame] | 1087 | return "missing loop expression"; |
| 1088 | } |
John Stiles | 232b4ce | 2021-03-01 22:14:22 -0500 | [diff] [blame] | 1089 | switch (loopNext->kind()) { |
Brian Osman | 77ba810 | 2021-01-12 17:15:30 -0500 | [diff] [blame] | 1090 | case Expression::Kind::kBinary: { |
John Stiles | 232b4ce | 2021-03-01 22:14:22 -0500 | [diff] [blame] | 1091 | const BinaryExpression& next = loopNext->as<BinaryExpression>(); |
Brian Osman | 77ba810 | 2021-01-12 17:15:30 -0500 | [diff] [blame] | 1092 | if (!is_loop_index(next.left())) { |
| 1093 | return "expected loop index in loop expression"; |
| 1094 | } |
Brian Osman | 448b2d5 | 2021-09-23 11:36:15 -0400 | [diff] [blame] | 1095 | if (!ConstantFolder::GetConstantValue(*next.right(), &loopInfo.fDelta)) { |
Brian Osman | 77ba810 | 2021-01-12 17:15:30 -0500 | [diff] [blame] | 1096 | return "loop index must be modified by a constant expression"; |
| 1097 | } |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 1098 | switch (next.getOperator().kind()) { |
Brian Osman | 77ba810 | 2021-01-12 17:15:30 -0500 | [diff] [blame] | 1099 | case Token::Kind::TK_PLUSEQ: break; |
| 1100 | case Token::Kind::TK_MINUSEQ: loopInfo.fDelta = -loopInfo.fDelta; break; |
| 1101 | default: |
| 1102 | return "invalid operator in loop expression"; |
| 1103 | } |
| 1104 | } break; |
| 1105 | case Expression::Kind::kPrefix: { |
John Stiles | 232b4ce | 2021-03-01 22:14:22 -0500 | [diff] [blame] | 1106 | const PrefixExpression& next = loopNext->as<PrefixExpression>(); |
Brian Osman | 77ba810 | 2021-01-12 17:15:30 -0500 | [diff] [blame] | 1107 | if (!is_loop_index(next.operand())) { |
| 1108 | return "expected loop index in loop expression"; |
| 1109 | } |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 1110 | switch (next.getOperator().kind()) { |
Brian Osman | 77ba810 | 2021-01-12 17:15:30 -0500 | [diff] [blame] | 1111 | case Token::Kind::TK_PLUSPLUS: loopInfo.fDelta = 1; break; |
| 1112 | case Token::Kind::TK_MINUSMINUS: loopInfo.fDelta = -1; break; |
| 1113 | default: |
| 1114 | return "invalid operator in loop expression"; |
| 1115 | } |
| 1116 | } break; |
| 1117 | case Expression::Kind::kPostfix: { |
John Stiles | 232b4ce | 2021-03-01 22:14:22 -0500 | [diff] [blame] | 1118 | const PostfixExpression& next = loopNext->as<PostfixExpression>(); |
Brian Osman | 77ba810 | 2021-01-12 17:15:30 -0500 | [diff] [blame] | 1119 | if (!is_loop_index(next.operand())) { |
| 1120 | return "expected loop index in loop expression"; |
| 1121 | } |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 1122 | switch (next.getOperator().kind()) { |
Brian Osman | 77ba810 | 2021-01-12 17:15:30 -0500 | [diff] [blame] | 1123 | case Token::Kind::TK_PLUSPLUS: loopInfo.fDelta = 1; break; |
| 1124 | case Token::Kind::TK_MINUSMINUS: loopInfo.fDelta = -1; break; |
| 1125 | default: |
| 1126 | return "invalid operator in loop expression"; |
| 1127 | } |
| 1128 | } break; |
| 1129 | default: |
| 1130 | return "invalid loop expression"; |
| 1131 | } |
| 1132 | |
| 1133 | // |
| 1134 | // Within the body of the loop, the loop index is not statically assigned to, nor is it used as |
| 1135 | // argument to a function 'out' or 'inout' parameter. |
| 1136 | // |
John Stiles | 232b4ce | 2021-03-01 22:14:22 -0500 | [diff] [blame] | 1137 | if (Analysis::StatementWritesToVariable(*loopStatement, initDecl.var())) { |
Brian Osman | 77ba810 | 2021-01-12 17:15:30 -0500 | [diff] [blame] | 1138 | return "loop index must not be modified within body of the loop"; |
| 1139 | } |
| 1140 | |
| 1141 | // Finally, compute the iteration count, based on the bounds, and the termination operator. |
John Stiles | 106e0cd | 2021-09-07 11:43:51 -0400 | [diff] [blame] | 1142 | static constexpr int kLoopTerminationLimit = 100000; |
Brian Osman | 77ba810 | 2021-01-12 17:15:30 -0500 | [diff] [blame] | 1143 | loopInfo.fCount = 0; |
| 1144 | |
John Stiles | 106e0cd | 2021-09-07 11:43:51 -0400 | [diff] [blame] | 1145 | auto calculateCount = [](double start, double end, double delta, |
| 1146 | bool forwards, bool inclusive) -> int { |
| 1147 | if (forwards != (start < end)) { |
| 1148 | // The loop starts in a completed state (the start has already advanced past the end). |
| 1149 | return 0; |
Brian Osman | 77ba810 | 2021-01-12 17:15:30 -0500 | [diff] [blame] | 1150 | } |
John Stiles | 106e0cd | 2021-09-07 11:43:51 -0400 | [diff] [blame] | 1151 | if ((delta == 0.0) || forwards != (delta > 0.0)) { |
| 1152 | // The loop does not progress toward a completed state, and will never terminate. |
| 1153 | return kLoopTerminationLimit; |
| 1154 | } |
| 1155 | double iterations = sk_ieee_double_divide(end - start, delta); |
| 1156 | double count = std::ceil(iterations); |
| 1157 | if (inclusive && (count == iterations)) { |
| 1158 | count += 1.0; |
| 1159 | } |
| 1160 | if (count > kLoopTerminationLimit || !std::isfinite(count)) { |
| 1161 | // The loop runs for more iterations than we can safely unroll. |
| 1162 | return kLoopTerminationLimit; |
| 1163 | } |
| 1164 | return (int)count; |
Brian Osman | 77ba810 | 2021-01-12 17:15:30 -0500 | [diff] [blame] | 1165 | }; |
| 1166 | |
John Stiles | 106e0cd | 2021-09-07 11:43:51 -0400 | [diff] [blame] | 1167 | switch (cond.getOperator().kind()) { |
| 1168 | case Token::Kind::TK_LT: |
| 1169 | loopInfo.fCount = calculateCount(loopInfo.fStart, loopEnd, loopInfo.fDelta, |
| 1170 | /*forwards=*/true, /*inclusive=*/false); |
| 1171 | break; |
| 1172 | |
| 1173 | case Token::Kind::TK_GT: |
| 1174 | loopInfo.fCount = calculateCount(loopInfo.fStart, loopEnd, loopInfo.fDelta, |
| 1175 | /*forwards=*/false, /*inclusive=*/false); |
| 1176 | break; |
| 1177 | |
| 1178 | case Token::Kind::TK_LTEQ: |
| 1179 | loopInfo.fCount = calculateCount(loopInfo.fStart, loopEnd, loopInfo.fDelta, |
| 1180 | /*forwards=*/true, /*inclusive=*/true); |
| 1181 | break; |
| 1182 | |
| 1183 | case Token::Kind::TK_GTEQ: |
| 1184 | loopInfo.fCount = calculateCount(loopInfo.fStart, loopEnd, loopInfo.fDelta, |
| 1185 | /*forwards=*/false, /*inclusive=*/true); |
| 1186 | break; |
| 1187 | |
| 1188 | case Token::Kind::TK_NEQ: { |
| 1189 | float iterations = sk_ieee_double_divide(loopEnd - loopInfo.fStart, loopInfo.fDelta); |
| 1190 | loopInfo.fCount = std::ceil(iterations); |
| 1191 | if (loopInfo.fCount < 0 || loopInfo.fCount != iterations || |
| 1192 | !std::isfinite(iterations)) { |
| 1193 | // The loop doesn't reach the exact endpoint and so will never terminate. |
| 1194 | loopInfo.fCount = kLoopTerminationLimit; |
| 1195 | } |
Brian Osman | 77ba810 | 2021-01-12 17:15:30 -0500 | [diff] [blame] | 1196 | break; |
| 1197 | } |
John Stiles | 106e0cd | 2021-09-07 11:43:51 -0400 | [diff] [blame] | 1198 | case Token::Kind::TK_EQEQ: { |
| 1199 | if (loopInfo.fStart == loopEnd) { |
| 1200 | // Start and end begin in the same place, so we can run one iteration... |
| 1201 | if (loopInfo.fDelta) { |
| 1202 | // ... and then they diverge, so the loop terminates. |
| 1203 | loopInfo.fCount = 1; |
| 1204 | } else { |
| 1205 | // ... but they never diverge, so the loop runs forever. |
| 1206 | loopInfo.fCount = kLoopTerminationLimit; |
| 1207 | } |
| 1208 | } else { |
| 1209 | // Start never equals end, so the loop will not run a single iteration. |
| 1210 | loopInfo.fCount = 0; |
| 1211 | } |
| 1212 | break; |
| 1213 | } |
| 1214 | default: SkUNREACHABLE; |
Brian Osman | 77ba810 | 2021-01-12 17:15:30 -0500 | [diff] [blame] | 1215 | } |
| 1216 | |
John Stiles | 106e0cd | 2021-09-07 11:43:51 -0400 | [diff] [blame] | 1217 | SkASSERT(loopInfo.fCount >= 0); |
| 1218 | if (loopInfo.fCount >= kLoopTerminationLimit) { |
Brian Osman | 77ba810 | 2021-01-12 17:15:30 -0500 | [diff] [blame] | 1219 | return "loop must guarantee termination in fewer iterations"; |
| 1220 | } |
| 1221 | |
| 1222 | return nullptr; // All checks pass |
| 1223 | } |
| 1224 | |
John Stiles | 9c975c5 | 2021-08-31 10:18:57 -0400 | [diff] [blame] | 1225 | std::unique_ptr<LoopUnrollInfo> Analysis::GetLoopUnrollInfo(int offset, |
| 1226 | const Statement* loopInitializer, |
| 1227 | const Expression* loopTest, |
| 1228 | const Expression* loopNext, |
| 1229 | const Statement* loopStatement, |
| 1230 | ErrorReporter* errors) { |
| 1231 | auto result = std::make_unique<LoopUnrollInfo>(); |
| 1232 | if (const char* msg = invalid_for_ES2(offset, loopInitializer, loopTest, loopNext, |
| 1233 | loopStatement, *result)) { |
| 1234 | result = nullptr; |
Brian Osman | 77ba810 | 2021-01-12 17:15:30 -0500 | [diff] [blame] | 1235 | if (errors) { |
John Stiles | 232b4ce | 2021-03-01 22:14:22 -0500 | [diff] [blame] | 1236 | errors->error(offset, msg); |
Brian Osman | 77ba810 | 2021-01-12 17:15:30 -0500 | [diff] [blame] | 1237 | } |
Brian Osman | 77ba810 | 2021-01-12 17:15:30 -0500 | [diff] [blame] | 1238 | } |
John Stiles | 9c975c5 | 2021-08-31 10:18:57 -0400 | [diff] [blame] | 1239 | return result; |
Brian Osman | 77ba810 | 2021-01-12 17:15:30 -0500 | [diff] [blame] | 1240 | } |
| 1241 | |
Brian Osman | 7b36149 | 2021-02-25 11:25:30 -0500 | [diff] [blame] | 1242 | // Checks for ES2 constant-expression rules, and (optionally) constant-index-expression rules |
| 1243 | // (if loopIndices is non-nullptr) |
| 1244 | class ConstantExpressionVisitor : public ProgramVisitor { |
Brian Osman | ea485e5 | 2021-01-15 13:20:32 -0500 | [diff] [blame] | 1245 | public: |
Brian Osman | 7b36149 | 2021-02-25 11:25:30 -0500 | [diff] [blame] | 1246 | ConstantExpressionVisitor(const std::set<const Variable*>* loopIndices) |
Brian Osman | ea485e5 | 2021-01-15 13:20:32 -0500 | [diff] [blame] | 1247 | : fLoopIndices(loopIndices) {} |
| 1248 | |
| 1249 | bool visitExpression(const Expression& e) override { |
| 1250 | // A constant-(index)-expression is one of... |
| 1251 | switch (e.kind()) { |
| 1252 | // ... a literal value |
John Stiles | 7591d4b | 2021-09-13 13:32:06 -0400 | [diff] [blame] | 1253 | case Expression::Kind::kLiteral: |
Brian Osman | ea485e5 | 2021-01-15 13:20:32 -0500 | [diff] [blame] | 1254 | return false; |
| 1255 | |
John Stiles | 532138c | 2021-03-04 16:29:22 -0500 | [diff] [blame] | 1256 | // ... settings can appear in fragment processors; they will resolve when compiled |
| 1257 | case Expression::Kind::kSetting: |
| 1258 | return false; |
| 1259 | |
Brian Osman | 7b36149 | 2021-02-25 11:25:30 -0500 | [diff] [blame] | 1260 | // ... a global or local variable qualified as 'const', excluding function parameters. |
| 1261 | // ... loop indices as defined in section 4. [constant-index-expression] |
| 1262 | case Expression::Kind::kVariableReference: { |
| 1263 | const Variable* v = e.as<VariableReference>().variable(); |
| 1264 | if ((v->storage() == Variable::Storage::kGlobal || |
| 1265 | v->storage() == Variable::Storage::kLocal) && |
| 1266 | (v->modifiers().fFlags & Modifiers::kConst_Flag)) { |
| 1267 | return false; |
| 1268 | } |
| 1269 | return !fLoopIndices || fLoopIndices->find(v) == fLoopIndices->end(); |
| 1270 | } |
Brian Osman | ea485e5 | 2021-01-15 13:20:32 -0500 | [diff] [blame] | 1271 | |
| 1272 | // ... expressions composed of both of the above |
| 1273 | case Expression::Kind::kBinary: |
John Stiles | 7384b37 | 2021-04-01 13:48:15 -0400 | [diff] [blame] | 1274 | case Expression::Kind::kConstructorArray: |
John Stiles | e3ae968 | 2021-08-05 10:35:01 -0400 | [diff] [blame] | 1275 | case Expression::Kind::kConstructorArrayCast: |
John Stiles | 8cad637 | 2021-04-07 12:31:13 -0400 | [diff] [blame] | 1276 | case Expression::Kind::kConstructorCompound: |
| 1277 | case Expression::Kind::kConstructorCompoundCast: |
John Stiles | e118278 | 2021-03-30 22:09:37 -0400 | [diff] [blame] | 1278 | case Expression::Kind::kConstructorDiagonalMatrix: |
John Stiles | 5abb9e1 | 2021-04-06 13:47:19 -0400 | [diff] [blame] | 1279 | case Expression::Kind::kConstructorMatrixResize: |
John Stiles | fd7252f | 2021-04-04 22:24:40 -0400 | [diff] [blame] | 1280 | case Expression::Kind::kConstructorScalarCast: |
John Stiles | 2938eea | 2021-04-01 18:58:25 -0400 | [diff] [blame] | 1281 | case Expression::Kind::kConstructorSplat: |
John Stiles | d47330f | 2021-04-08 23:25:52 -0400 | [diff] [blame] | 1282 | case Expression::Kind::kConstructorStruct: |
Brian Osman | ea485e5 | 2021-01-15 13:20:32 -0500 | [diff] [blame] | 1283 | case Expression::Kind::kFieldAccess: |
| 1284 | case Expression::Kind::kIndex: |
| 1285 | case Expression::Kind::kPrefix: |
| 1286 | case Expression::Kind::kPostfix: |
| 1287 | case Expression::Kind::kSwizzle: |
| 1288 | case Expression::Kind::kTernary: |
| 1289 | return INHERITED::visitExpression(e); |
| 1290 | |
John Stiles | 7bd3f1c | 2021-08-27 16:12:10 -0400 | [diff] [blame] | 1291 | // Function calls are completely disallowed in SkSL constant-(index)-expressions. |
| 1292 | // GLSL does mandate that calling a built-in function where the arguments are all |
| 1293 | // constant-expressions should result in a constant-expression. SkSL handles this by |
| 1294 | // optimizing fully-constant function calls into literals in FunctionCall::Make. |
Brian Osman | ea485e5 | 2021-01-15 13:20:32 -0500 | [diff] [blame] | 1295 | case Expression::Kind::kFunctionCall: |
John Stiles | 7bd3f1c | 2021-08-27 16:12:10 -0400 | [diff] [blame] | 1296 | case Expression::Kind::kExternalFunctionCall: |
| 1297 | case Expression::Kind::kChildCall: |
Brian Osman | ea485e5 | 2021-01-15 13:20:32 -0500 | [diff] [blame] | 1298 | |
John Stiles | 7bd3f1c | 2021-08-27 16:12:10 -0400 | [diff] [blame] | 1299 | // These shouldn't appear in a valid program at all, and definitely aren't |
| 1300 | // constant-index-expressions. |
Ethan Nicholas | 549c6b8 | 2021-06-25 12:31:44 -0400 | [diff] [blame] | 1301 | case Expression::Kind::kPoison: |
John Stiles | 7bd3f1c | 2021-08-27 16:12:10 -0400 | [diff] [blame] | 1302 | case Expression::Kind::kFunctionReference: |
| 1303 | case Expression::Kind::kExternalFunctionReference: |
Brian Osman | 3099f79 | 2021-09-01 13:12:16 -0400 | [diff] [blame] | 1304 | case Expression::Kind::kMethodReference: |
John Stiles | 7bd3f1c | 2021-08-27 16:12:10 -0400 | [diff] [blame] | 1305 | case Expression::Kind::kTypeReference: |
| 1306 | case Expression::Kind::kCodeString: |
Ethan Nicholas | 549c6b8 | 2021-06-25 12:31:44 -0400 | [diff] [blame] | 1307 | return true; |
| 1308 | |
Brian Osman | ea485e5 | 2021-01-15 13:20:32 -0500 | [diff] [blame] | 1309 | default: |
| 1310 | SkDEBUGFAIL("Unexpected expression type"); |
| 1311 | return true; |
| 1312 | } |
| 1313 | } |
| 1314 | |
| 1315 | private: |
| 1316 | const std::set<const Variable*>* fLoopIndices; |
| 1317 | using INHERITED = ProgramVisitor; |
| 1318 | }; |
| 1319 | |
| 1320 | class ES2IndexingVisitor : public ProgramVisitor { |
| 1321 | public: |
| 1322 | ES2IndexingVisitor(ErrorReporter& errors) : fErrors(errors) {} |
| 1323 | |
| 1324 | bool visitStatement(const Statement& s) override { |
| 1325 | if (s.is<ForStatement>()) { |
| 1326 | const ForStatement& f = s.as<ForStatement>(); |
| 1327 | SkASSERT(f.initializer() && f.initializer()->is<VarDeclaration>()); |
| 1328 | const Variable* var = &f.initializer()->as<VarDeclaration>().var(); |
| 1329 | auto [iter, inserted] = fLoopIndices.insert(var); |
| 1330 | SkASSERT(inserted); |
| 1331 | bool result = this->visitStatement(*f.statement()); |
| 1332 | fLoopIndices.erase(iter); |
| 1333 | return result; |
| 1334 | } |
| 1335 | return INHERITED::visitStatement(s); |
| 1336 | } |
| 1337 | |
| 1338 | bool visitExpression(const Expression& e) override { |
| 1339 | if (e.is<IndexExpression>()) { |
| 1340 | const IndexExpression& i = e.as<IndexExpression>(); |
Brian Osman | 7b36149 | 2021-02-25 11:25:30 -0500 | [diff] [blame] | 1341 | ConstantExpressionVisitor indexerInvalid(&fLoopIndices); |
Brian Osman | ea485e5 | 2021-01-15 13:20:32 -0500 | [diff] [blame] | 1342 | if (indexerInvalid.visitExpression(*i.index())) { |
| 1343 | fErrors.error(i.fOffset, "index expression must be constant"); |
| 1344 | return true; |
| 1345 | } |
| 1346 | } |
| 1347 | return INHERITED::visitExpression(e); |
| 1348 | } |
| 1349 | |
| 1350 | using ProgramVisitor::visitProgramElement; |
| 1351 | |
| 1352 | private: |
| 1353 | ErrorReporter& fErrors; |
| 1354 | std::set<const Variable*> fLoopIndices; |
| 1355 | using INHERITED = ProgramVisitor; |
| 1356 | }; |
| 1357 | |
| 1358 | |
| 1359 | void Analysis::ValidateIndexingForES2(const ProgramElement& pe, ErrorReporter& errors) { |
| 1360 | ES2IndexingVisitor visitor(errors); |
| 1361 | visitor.visitProgramElement(pe); |
| 1362 | } |
| 1363 | |
Brian Osman | 7b36149 | 2021-02-25 11:25:30 -0500 | [diff] [blame] | 1364 | bool Analysis::IsConstantExpression(const Expression& expr) { |
| 1365 | ConstantExpressionVisitor visitor(/*loopIndices=*/nullptr); |
| 1366 | return !visitor.visitExpression(expr); |
| 1367 | } |
| 1368 | |
John Stiles | 958f4b5 | 2021-03-17 16:39:49 -0400 | [diff] [blame] | 1369 | bool Analysis::CanExitWithoutReturningValue(const FunctionDeclaration& funcDecl, |
| 1370 | const Statement& body) { |
| 1371 | if (funcDecl.returnType().isVoid()) { |
| 1372 | return false; |
| 1373 | } |
John Stiles | b3dcbb1 | 2021-03-04 16:00:20 -0500 | [diff] [blame] | 1374 | ReturnsOnAllPathsVisitor visitor; |
John Stiles | 958f4b5 | 2021-03-17 16:39:49 -0400 | [diff] [blame] | 1375 | visitor.visitStatement(body); |
John Stiles | b3dcbb1 | 2021-03-04 16:00:20 -0500 | [diff] [blame] | 1376 | return !visitor.fFoundReturn; |
| 1377 | } |
| 1378 | |
John Stiles | 2ecc595 | 2021-09-01 14:41:36 -0400 | [diff] [blame] | 1379 | void Analysis::VerifyStaticTestsAndExpressions(const Program& program) { |
| 1380 | class TestsAndExpressions : public ProgramVisitor { |
John Stiles | 0fc6bed | 2021-09-01 11:35:59 -0400 | [diff] [blame] | 1381 | public: |
John Stiles | 2ecc595 | 2021-09-01 14:41:36 -0400 | [diff] [blame] | 1382 | TestsAndExpressions(const Context& ctx) : fContext(ctx) {} |
John Stiles | 0fc6bed | 2021-09-01 11:35:59 -0400 | [diff] [blame] | 1383 | |
| 1384 | using ProgramVisitor::visitProgramElement; |
| 1385 | |
| 1386 | bool visitStatement(const Statement& stmt) override { |
John Stiles | 2ecc595 | 2021-09-01 14:41:36 -0400 | [diff] [blame] | 1387 | if (!fContext.fConfig->fSettings.fPermitInvalidStaticTests) { |
| 1388 | switch (stmt.kind()) { |
| 1389 | case Statement::Kind::kIf: |
| 1390 | if (stmt.as<IfStatement>().isStatic()) { |
| 1391 | fContext.fErrors->error(stmt.fOffset, "static if has non-static test"); |
| 1392 | } |
| 1393 | break; |
John Stiles | 0fc6bed | 2021-09-01 11:35:59 -0400 | [diff] [blame] | 1394 | |
John Stiles | 2ecc595 | 2021-09-01 14:41:36 -0400 | [diff] [blame] | 1395 | case Statement::Kind::kSwitch: |
| 1396 | if (stmt.as<SwitchStatement>().isStatic()) { |
| 1397 | fContext.fErrors->error(stmt.fOffset, |
| 1398 | "static switch has non-static test"); |
| 1399 | } |
| 1400 | break; |
John Stiles | 0fc6bed | 2021-09-01 11:35:59 -0400 | [diff] [blame] | 1401 | |
John Stiles | 2ecc595 | 2021-09-01 14:41:36 -0400 | [diff] [blame] | 1402 | default: |
| 1403 | break; |
| 1404 | } |
John Stiles | 0fc6bed | 2021-09-01 11:35:59 -0400 | [diff] [blame] | 1405 | } |
| 1406 | return INHERITED::visitStatement(stmt); |
| 1407 | } |
| 1408 | |
John Stiles | 2ecc595 | 2021-09-01 14:41:36 -0400 | [diff] [blame] | 1409 | bool visitExpression(const Expression& expr) override { |
| 1410 | switch (expr.kind()) { |
| 1411 | case Expression::Kind::kFunctionCall: { |
| 1412 | const FunctionDeclaration& decl = expr.as<FunctionCall>().function(); |
| 1413 | if (!decl.isBuiltin() && !decl.definition()) { |
| 1414 | fContext.fErrors->error(expr.fOffset, "function '" + decl.description() + |
| 1415 | "' is not defined"); |
| 1416 | } |
| 1417 | break; |
| 1418 | } |
| 1419 | case Expression::Kind::kExternalFunctionReference: |
| 1420 | case Expression::Kind::kFunctionReference: |
Brian Osman | 3099f79 | 2021-09-01 13:12:16 -0400 | [diff] [blame] | 1421 | case Expression::Kind::kMethodReference: |
John Stiles | 2ecc595 | 2021-09-01 14:41:36 -0400 | [diff] [blame] | 1422 | case Expression::Kind::kTypeReference: |
| 1423 | SkDEBUGFAIL("invalid reference-expr, should have been reported by coerce()"); |
| 1424 | fContext.fErrors->error(expr.fOffset, "invalid expression"); |
| 1425 | break; |
| 1426 | default: |
| 1427 | if (expr.type() == *fContext.fTypes.fInvalid) { |
| 1428 | fContext.fErrors->error(expr.fOffset, "invalid expression"); |
| 1429 | } |
| 1430 | break; |
| 1431 | } |
| 1432 | return INHERITED::visitExpression(expr); |
John Stiles | 0fc6bed | 2021-09-01 11:35:59 -0400 | [diff] [blame] | 1433 | } |
| 1434 | |
| 1435 | private: |
| 1436 | using INHERITED = ProgramVisitor; |
John Stiles | 2ecc595 | 2021-09-01 14:41:36 -0400 | [diff] [blame] | 1437 | const Context& fContext; |
John Stiles | 0fc6bed | 2021-09-01 11:35:59 -0400 | [diff] [blame] | 1438 | }; |
| 1439 | |
John Stiles | 0fc6bed | 2021-09-01 11:35:59 -0400 | [diff] [blame] | 1440 | // Check all of the program's owned elements. (Built-in elements are assumed to be valid.) |
John Stiles | 2ecc595 | 2021-09-01 14:41:36 -0400 | [diff] [blame] | 1441 | TestsAndExpressions visitor{*program.fContext}; |
John Stiles | 0fc6bed | 2021-09-01 11:35:59 -0400 | [diff] [blame] | 1442 | for (const std::unique_ptr<ProgramElement>& element : program.ownedElements()) { |
John Stiles | 2ecc595 | 2021-09-01 14:41:36 -0400 | [diff] [blame] | 1443 | visitor.visitProgramElement(*element); |
John Stiles | 0fc6bed | 2021-09-01 11:35:59 -0400 | [diff] [blame] | 1444 | } |
| 1445 | } |
| 1446 | |
John Stiles | 49b1a42 | 2021-09-22 09:35:39 -0400 | [diff] [blame] | 1447 | void Analysis::EliminateUnreachableCode(std::unique_ptr<Statement>& stmt, ProgramUsage* usage) { |
| 1448 | class UnreachableCodeEliminator : public ProgramWriter { |
| 1449 | public: |
| 1450 | UnreachableCodeEliminator(ProgramUsage* usage) |
| 1451 | : fUsage(usage) { |
| 1452 | fFoundFunctionExit.push(false); |
| 1453 | fFoundLoopExit.push(false); |
| 1454 | } |
| 1455 | |
| 1456 | bool visitExpressionPtr(std::unique_ptr<Expression>& expr) override { |
| 1457 | // We don't need to look inside expressions at all. |
| 1458 | return false; |
| 1459 | } |
| 1460 | |
| 1461 | bool visitStatementPtr(std::unique_ptr<Statement>& stmt) override { |
| 1462 | if (fFoundFunctionExit.top() || fFoundLoopExit.top()) { |
| 1463 | // If we already found an exit in this section, anything beyond it is dead code. |
| 1464 | if (!stmt->is<Nop>()) { |
| 1465 | // Eliminate the dead statement by substituting a Nop. |
| 1466 | if (fUsage) { |
| 1467 | fUsage->remove(stmt.get()); |
| 1468 | } |
| 1469 | stmt = std::make_unique<Nop>(); |
| 1470 | } |
| 1471 | return false; |
| 1472 | } |
| 1473 | |
| 1474 | switch (stmt->kind()) { |
| 1475 | case Statement::Kind::kReturn: |
| 1476 | case Statement::Kind::kDiscard: |
| 1477 | // We found a function exit on this path. |
| 1478 | fFoundFunctionExit.top() = true; |
| 1479 | break; |
| 1480 | |
| 1481 | case Statement::Kind::kBreak: |
| 1482 | case Statement::Kind::kContinue: |
| 1483 | // We found a loop exit on this path. Note that we skip over switch statements |
| 1484 | // completely when eliminating code, so any `break` statement would be breaking |
| 1485 | // out of a loop, not out of a switch. |
| 1486 | fFoundLoopExit.top() = true; |
| 1487 | break; |
| 1488 | |
| 1489 | case Statement::Kind::kExpression: |
| 1490 | case Statement::Kind::kInlineMarker: |
| 1491 | case Statement::Kind::kNop: |
| 1492 | case Statement::Kind::kVarDeclaration: |
| 1493 | // These statements don't affect control flow. |
| 1494 | break; |
| 1495 | |
| 1496 | case Statement::Kind::kBlock: |
| 1497 | // Blocks are on the straight-line path and don't affect control flow. |
| 1498 | return INHERITED::visitStatementPtr(stmt); |
| 1499 | |
| 1500 | case Statement::Kind::kDo: { |
| 1501 | // Function-exits are allowed to propagate outside of a do-loop, because it |
| 1502 | // always executes its body at least once. |
| 1503 | fFoundLoopExit.push(false); |
| 1504 | bool result = INHERITED::visitStatementPtr(stmt); |
| 1505 | fFoundLoopExit.pop(); |
| 1506 | return result; |
| 1507 | } |
| 1508 | case Statement::Kind::kFor: { |
| 1509 | // Function-exits are not allowed to propagate out, because a for-loop or while- |
| 1510 | // loop could potentially run zero times. |
| 1511 | fFoundFunctionExit.push(false); |
| 1512 | fFoundLoopExit.push(false); |
| 1513 | bool result = INHERITED::visitStatementPtr(stmt); |
| 1514 | fFoundLoopExit.pop(); |
| 1515 | fFoundFunctionExit.pop(); |
| 1516 | return result; |
| 1517 | } |
| 1518 | case Statement::Kind::kIf: { |
| 1519 | // This statement is conditional and encloses two inner sections of code. |
| 1520 | // If both sides contain a function-exit or loop-exit, that exit is allowed to |
| 1521 | // propagate out. |
| 1522 | IfStatement& ifStmt = stmt->as<IfStatement>(); |
| 1523 | |
| 1524 | fFoundFunctionExit.push(false); |
| 1525 | fFoundLoopExit.push(false); |
| 1526 | bool result = (ifStmt.ifTrue() && this->visitStatementPtr(ifStmt.ifTrue())); |
| 1527 | bool foundFunctionExitOnTrue = fFoundFunctionExit.top(); |
| 1528 | bool foundLoopExitOnTrue = fFoundLoopExit.top(); |
| 1529 | fFoundFunctionExit.pop(); |
| 1530 | fFoundLoopExit.pop(); |
| 1531 | |
| 1532 | fFoundFunctionExit.push(false); |
| 1533 | fFoundLoopExit.push(false); |
| 1534 | result |= (ifStmt.ifFalse() && this->visitStatementPtr(ifStmt.ifFalse())); |
| 1535 | bool foundFunctionExitOnFalse = fFoundFunctionExit.top(); |
| 1536 | bool foundLoopExitOnFalse = fFoundLoopExit.top(); |
| 1537 | fFoundFunctionExit.pop(); |
| 1538 | fFoundLoopExit.pop(); |
| 1539 | |
| 1540 | fFoundFunctionExit.top() |= foundFunctionExitOnTrue && foundFunctionExitOnFalse; |
| 1541 | fFoundLoopExit.top() |= foundLoopExitOnTrue && foundLoopExitOnFalse; |
| 1542 | return result; |
| 1543 | } |
| 1544 | case Statement::Kind::kSwitch: |
| 1545 | case Statement::Kind::kSwitchCase: |
| 1546 | // We skip past switch statements entirely when scanning for dead code. Their |
| 1547 | // control flow is quite complex and we already do a good job of flattening out |
| 1548 | // switches on constant values. |
| 1549 | break; |
| 1550 | } |
| 1551 | |
| 1552 | return false; |
| 1553 | } |
| 1554 | |
| 1555 | ProgramUsage* fUsage; |
| 1556 | std::stack<bool> fFoundFunctionExit; |
| 1557 | std::stack<bool> fFoundLoopExit; |
| 1558 | |
| 1559 | using INHERITED = ProgramWriter; |
| 1560 | }; |
| 1561 | |
| 1562 | UnreachableCodeEliminator visitor{usage}; |
| 1563 | visitor.visitStatementPtr(stmt); |
| 1564 | } |
| 1565 | |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 1566 | //////////////////////////////////////////////////////////////////////////////// |
| 1567 | // ProgramVisitor |
| 1568 | |
Brian Osman | 133724c | 2020-10-28 14:14:39 -0400 | [diff] [blame] | 1569 | bool ProgramVisitor::visit(const Program& program) { |
| 1570 | for (const ProgramElement* pe : program.elements()) { |
Brian Osman | 1179fcf | 2020-10-08 16:04:40 -0400 | [diff] [blame] | 1571 | if (this->visitProgramElement(*pe)) { |
John Stiles | 933abe3 | 2020-08-28 11:58:40 -0400 | [diff] [blame] | 1572 | return true; |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 1573 | } |
| 1574 | } |
John Stiles | 933abe3 | 2020-08-28 11:58:40 -0400 | [diff] [blame] | 1575 | return false; |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 1576 | } |
| 1577 | |
John Stiles | 48b2558 | 2021-03-11 14:26:42 -0500 | [diff] [blame] | 1578 | template <typename T> bool TProgramVisitor<T>::visitExpression(typename T::Expression& e) { |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1579 | switch (e.kind()) { |
Brian Osman | be0b3b7 | 2021-01-06 14:27:35 -0500 | [diff] [blame] | 1580 | case Expression::Kind::kExternalFunctionReference: |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1581 | case Expression::Kind::kFunctionReference: |
John Stiles | 7591d4b | 2021-09-13 13:32:06 -0400 | [diff] [blame] | 1582 | case Expression::Kind::kLiteral: |
Brian Osman | 3099f79 | 2021-09-01 13:12:16 -0400 | [diff] [blame] | 1583 | case Expression::Kind::kMethodReference: |
Ethan Nicholas | 549c6b8 | 2021-06-25 12:31:44 -0400 | [diff] [blame] | 1584 | case Expression::Kind::kPoison: |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1585 | case Expression::Kind::kSetting: |
| 1586 | case Expression::Kind::kTypeReference: |
| 1587 | case Expression::Kind::kVariableReference: |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 1588 | // Leaf expressions return false |
| 1589 | return false; |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1590 | |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1591 | case Expression::Kind::kBinary: { |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1592 | auto& b = e.template as<BinaryExpression>(); |
John Stiles | 48b2558 | 2021-03-11 14:26:42 -0500 | [diff] [blame] | 1593 | return (b.left() && this->visitExpressionPtr(b.left())) || |
| 1594 | (b.right() && this->visitExpressionPtr(b.right())); |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1595 | } |
Brian Osman | eb0f29d | 2021-08-04 11:34:16 -0400 | [diff] [blame] | 1596 | case Expression::Kind::kChildCall: { |
| 1597 | // We don't visit the child variable itself, just the arguments |
| 1598 | auto& c = e.template as<ChildCall>(); |
| 1599 | for (auto& arg : c.arguments()) { |
| 1600 | if (arg && this->visitExpressionPtr(arg)) { return true; } |
| 1601 | } |
| 1602 | return false; |
| 1603 | } |
John Stiles | 7384b37 | 2021-04-01 13:48:15 -0400 | [diff] [blame] | 1604 | case Expression::Kind::kConstructorArray: |
John Stiles | e3ae968 | 2021-08-05 10:35:01 -0400 | [diff] [blame] | 1605 | case Expression::Kind::kConstructorArrayCast: |
John Stiles | 8cad637 | 2021-04-07 12:31:13 -0400 | [diff] [blame] | 1606 | case Expression::Kind::kConstructorCompound: |
| 1607 | case Expression::Kind::kConstructorCompoundCast: |
John Stiles | 2938eea | 2021-04-01 18:58:25 -0400 | [diff] [blame] | 1608 | case Expression::Kind::kConstructorDiagonalMatrix: |
John Stiles | 5abb9e1 | 2021-04-06 13:47:19 -0400 | [diff] [blame] | 1609 | case Expression::Kind::kConstructorMatrixResize: |
John Stiles | fd7252f | 2021-04-04 22:24:40 -0400 | [diff] [blame] | 1610 | case Expression::Kind::kConstructorScalarCast: |
John Stiles | d47330f | 2021-04-08 23:25:52 -0400 | [diff] [blame] | 1611 | case Expression::Kind::kConstructorSplat: |
| 1612 | case Expression::Kind::kConstructorStruct: { |
John Stiles | 7384b37 | 2021-04-01 13:48:15 -0400 | [diff] [blame] | 1613 | auto& c = e.asAnyConstructor(); |
| 1614 | for (auto& arg : c.argumentSpan()) { |
John Stiles | 48b2558 | 2021-03-11 14:26:42 -0500 | [diff] [blame] | 1615 | if (this->visitExpressionPtr(arg)) { return true; } |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 1616 | } |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1617 | return false; |
| 1618 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1619 | case Expression::Kind::kExternalFunctionCall: { |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1620 | auto& c = e.template as<ExternalFunctionCall>(); |
Ethan Nicholas | 6e86ec9 | 2020-09-30 14:29:56 -0400 | [diff] [blame] | 1621 | for (auto& arg : c.arguments()) { |
John Stiles | 48b2558 | 2021-03-11 14:26:42 -0500 | [diff] [blame] | 1622 | if (this->visitExpressionPtr(arg)) { return true; } |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 1623 | } |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1624 | return false; |
| 1625 | } |
John Stiles | d7ab450 | 2020-09-24 22:41:00 -0400 | [diff] [blame] | 1626 | case Expression::Kind::kFieldAccess: |
John Stiles | 48b2558 | 2021-03-11 14:26:42 -0500 | [diff] [blame] | 1627 | return this->visitExpressionPtr(e.template as<FieldAccess>().base()); |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1628 | |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1629 | case Expression::Kind::kFunctionCall: { |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1630 | auto& c = e.template as<FunctionCall>(); |
Ethan Nicholas | 0dec992 | 2020-10-05 15:51:52 -0400 | [diff] [blame] | 1631 | for (auto& arg : c.arguments()) { |
John Stiles | 48b2558 | 2021-03-11 14:26:42 -0500 | [diff] [blame] | 1632 | if (arg && this->visitExpressionPtr(arg)) { return true; } |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 1633 | } |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1634 | return false; |
| 1635 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1636 | case Expression::Kind::kIndex: { |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1637 | auto& i = e.template as<IndexExpression>(); |
John Stiles | 48b2558 | 2021-03-11 14:26:42 -0500 | [diff] [blame] | 1638 | return this->visitExpressionPtr(i.base()) || this->visitExpressionPtr(i.index()); |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1639 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1640 | case Expression::Kind::kPostfix: |
John Stiles | 48b2558 | 2021-03-11 14:26:42 -0500 | [diff] [blame] | 1641 | return this->visitExpressionPtr(e.template as<PostfixExpression>().operand()); |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1642 | |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1643 | case Expression::Kind::kPrefix: |
John Stiles | 48b2558 | 2021-03-11 14:26:42 -0500 | [diff] [blame] | 1644 | return this->visitExpressionPtr(e.template as<PrefixExpression>().operand()); |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1645 | |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 1646 | case Expression::Kind::kSwizzle: { |
| 1647 | auto& s = e.template as<Swizzle>(); |
John Stiles | 48b2558 | 2021-03-11 14:26:42 -0500 | [diff] [blame] | 1648 | return s.base() && this->visitExpressionPtr(s.base()); |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 1649 | } |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1650 | |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1651 | case Expression::Kind::kTernary: { |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1652 | auto& t = e.template as<TernaryExpression>(); |
John Stiles | 48b2558 | 2021-03-11 14:26:42 -0500 | [diff] [blame] | 1653 | return this->visitExpressionPtr(t.test()) || |
| 1654 | (t.ifTrue() && this->visitExpressionPtr(t.ifTrue())) || |
| 1655 | (t.ifFalse() && this->visitExpressionPtr(t.ifFalse())); |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1656 | } |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 1657 | default: |
| 1658 | SkUNREACHABLE; |
| 1659 | } |
| 1660 | } |
| 1661 | |
John Stiles | 48b2558 | 2021-03-11 14:26:42 -0500 | [diff] [blame] | 1662 | template <typename T> bool TProgramVisitor<T>::visitStatement(typename T::Statement& s) { |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1663 | switch (s.kind()) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1664 | case Statement::Kind::kBreak: |
| 1665 | case Statement::Kind::kContinue: |
| 1666 | case Statement::Kind::kDiscard: |
John Stiles | 98c1f82 | 2020-09-09 14:18:53 -0400 | [diff] [blame] | 1667 | case Statement::Kind::kInlineMarker: |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1668 | case Statement::Kind::kNop: |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 1669 | // Leaf statements just return false |
| 1670 | return false; |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1671 | |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1672 | case Statement::Kind::kBlock: |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1673 | for (auto& stmt : s.template as<Block>().children()) { |
John Stiles | 48b2558 | 2021-03-11 14:26:42 -0500 | [diff] [blame] | 1674 | if (stmt && this->visitStatementPtr(stmt)) { |
Ethan Nicholas | 7bd6043 | 2020-09-25 14:31:59 -0400 | [diff] [blame] | 1675 | return true; |
| 1676 | } |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 1677 | } |
| 1678 | return false; |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1679 | |
John Stiles | a0e56e3 | 2021-03-03 13:14:37 -0500 | [diff] [blame] | 1680 | case Statement::Kind::kSwitchCase: { |
| 1681 | auto& sc = s.template as<SwitchCase>(); |
John Stiles | 48b2558 | 2021-03-11 14:26:42 -0500 | [diff] [blame] | 1682 | if (sc.value() && this->visitExpressionPtr(sc.value())) { |
John Stiles | a0e56e3 | 2021-03-03 13:14:37 -0500 | [diff] [blame] | 1683 | return true; |
| 1684 | } |
John Stiles | 48b2558 | 2021-03-11 14:26:42 -0500 | [diff] [blame] | 1685 | return this->visitStatementPtr(sc.statement()); |
John Stiles | a0e56e3 | 2021-03-03 13:14:37 -0500 | [diff] [blame] | 1686 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1687 | case Statement::Kind::kDo: { |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1688 | auto& d = s.template as<DoStatement>(); |
John Stiles | 48b2558 | 2021-03-11 14:26:42 -0500 | [diff] [blame] | 1689 | return this->visitExpressionPtr(d.test()) || this->visitStatementPtr(d.statement()); |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1690 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1691 | case Statement::Kind::kExpression: |
John Stiles | 48b2558 | 2021-03-11 14:26:42 -0500 | [diff] [blame] | 1692 | return this->visitExpressionPtr(s.template as<ExpressionStatement>().expression()); |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1693 | |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1694 | case Statement::Kind::kFor: { |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1695 | auto& f = s.template as<ForStatement>(); |
John Stiles | 48b2558 | 2021-03-11 14:26:42 -0500 | [diff] [blame] | 1696 | return (f.initializer() && this->visitStatementPtr(f.initializer())) || |
| 1697 | (f.test() && this->visitExpressionPtr(f.test())) || |
| 1698 | (f.next() && this->visitExpressionPtr(f.next())) || |
| 1699 | this->visitStatementPtr(f.statement()); |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1700 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1701 | case Statement::Kind::kIf: { |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1702 | auto& i = s.template as<IfStatement>(); |
John Stiles | 48b2558 | 2021-03-11 14:26:42 -0500 | [diff] [blame] | 1703 | return (i.test() && this->visitExpressionPtr(i.test())) || |
| 1704 | (i.ifTrue() && this->visitStatementPtr(i.ifTrue())) || |
| 1705 | (i.ifFalse() && this->visitStatementPtr(i.ifFalse())); |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1706 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1707 | case Statement::Kind::kReturn: { |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1708 | auto& r = s.template as<ReturnStatement>(); |
John Stiles | 48b2558 | 2021-03-11 14:26:42 -0500 | [diff] [blame] | 1709 | return r.expression() && this->visitExpressionPtr(r.expression()); |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1710 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1711 | case Statement::Kind::kSwitch: { |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1712 | auto& sw = s.template as<SwitchStatement>(); |
John Stiles | 48b2558 | 2021-03-11 14:26:42 -0500 | [diff] [blame] | 1713 | if (this->visitExpressionPtr(sw.value())) { |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1714 | return true; |
| 1715 | } |
John Stiles | 48b2558 | 2021-03-11 14:26:42 -0500 | [diff] [blame] | 1716 | for (auto& c : sw.cases()) { |
| 1717 | if (this->visitStatementPtr(c)) { |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1718 | return true; |
| 1719 | } |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 1720 | } |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1721 | return false; |
| 1722 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1723 | case Statement::Kind::kVarDeclaration: { |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1724 | auto& v = s.template as<VarDeclaration>(); |
John Stiles | 48b2558 | 2021-03-11 14:26:42 -0500 | [diff] [blame] | 1725 | return v.value() && this->visitExpressionPtr(v.value()); |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1726 | } |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 1727 | default: |
| 1728 | SkUNREACHABLE; |
| 1729 | } |
| 1730 | } |
| 1731 | |
John Stiles | 48b2558 | 2021-03-11 14:26:42 -0500 | [diff] [blame] | 1732 | template <typename T> bool TProgramVisitor<T>::visitProgramElement(typename T::ProgramElement& pe) { |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1733 | switch (pe.kind()) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1734 | case ProgramElement::Kind::kExtension: |
John Stiles | 569249b | 2020-11-03 12:18:22 -0500 | [diff] [blame] | 1735 | case ProgramElement::Kind::kFunctionPrototype: |
John Stiles | d39aec0 | 2020-12-03 10:42:26 -0500 | [diff] [blame] | 1736 | case ProgramElement::Kind::kInterfaceBlock: |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1737 | case ProgramElement::Kind::kModifiers: |
John Stiles | dc75a97 | 2020-11-25 16:24:55 -0500 | [diff] [blame] | 1738 | case ProgramElement::Kind::kStructDefinition: |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 1739 | // Leaf program elements just return false by default |
| 1740 | return false; |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1741 | |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1742 | case ProgramElement::Kind::kFunction: |
John Stiles | 48b2558 | 2021-03-11 14:26:42 -0500 | [diff] [blame] | 1743 | return this->visitStatementPtr(pe.template as<FunctionDefinition>().body()); |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1744 | |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 1745 | case ProgramElement::Kind::kGlobalVar: |
John Stiles | 48b2558 | 2021-03-11 14:26:42 -0500 | [diff] [blame] | 1746 | return this->visitStatementPtr(pe.template as<GlobalVarDeclaration>().declaration()); |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1747 | |
Michael Ludwig | 8f3a836 | 2020-06-29 17:27:00 -0400 | [diff] [blame] | 1748 | default: |
| 1749 | SkUNREACHABLE; |
| 1750 | } |
| 1751 | } |
| 1752 | |
John Stiles | 48b2558 | 2021-03-11 14:26:42 -0500 | [diff] [blame] | 1753 | template class TProgramVisitor<ProgramVisitorTypes>; |
| 1754 | template class TProgramVisitor<ProgramWriterTypes>; |
John Stiles | 70b8242 | 2020-09-30 10:55:12 -0400 | [diff] [blame] | 1755 | |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 1756 | } // namespace SkSL |