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