blob: 83f1a31f3bac0c5e439ccc8cd29bd10defcee07a [file] [log] [blame]
ethannicholasb3058bd2016-07-01 08:22:01 -07001/*
2 * Copyright 2016 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
Mike Klein6ad99092016-10-26 10:35:22 -04007
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/sksl/SkSLCompiler.h"
ethannicholasb3058bd2016-07-01 08:22:01 -07009
John Stilesfbd050b2020-08-03 13:21:46 -040010#include <memory>
John Stilesb8e010c2020-08-11 18:05:39 -040011#include <unordered_set>
John Stilesfbd050b2020-08-03 13:21:46 -040012
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "src/sksl/SkSLByteCodeGenerator.h"
14#include "src/sksl/SkSLCFGGenerator.h"
15#include "src/sksl/SkSLCPPCodeGenerator.h"
16#include "src/sksl/SkSLGLSLCodeGenerator.h"
17#include "src/sksl/SkSLHCodeGenerator.h"
18#include "src/sksl/SkSLIRGenerator.h"
19#include "src/sksl/SkSLMetalCodeGenerator.h"
20#include "src/sksl/SkSLPipelineStageCodeGenerator.h"
Ethan Nicholasc18bb512020-07-28 14:46:53 -040021#include "src/sksl/SkSLRehydrator.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "src/sksl/SkSLSPIRVCodeGenerator.h"
Brian Osmanc0243912020-02-19 15:35:26 -050023#include "src/sksl/SkSLSPIRVtoHLSL.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "src/sksl/ir/SkSLEnum.h"
25#include "src/sksl/ir/SkSLExpression.h"
26#include "src/sksl/ir/SkSLExpressionStatement.h"
27#include "src/sksl/ir/SkSLFunctionCall.h"
28#include "src/sksl/ir/SkSLIntLiteral.h"
29#include "src/sksl/ir/SkSLModifiersDeclaration.h"
30#include "src/sksl/ir/SkSLNop.h"
31#include "src/sksl/ir/SkSLSymbolTable.h"
32#include "src/sksl/ir/SkSLTernaryExpression.h"
33#include "src/sksl/ir/SkSLUnresolvedFunction.h"
34#include "src/sksl/ir/SkSLVarDeclarations.h"
ethannicholasb3058bd2016-07-01 08:22:01 -070035
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -040036#include <fstream>
37
Ethan Nicholasa11035b2019-11-26 16:27:47 -050038#if !defined(SKSL_STANDALONE) & SK_SUPPORT_GPU
39#include "include/gpu/GrContextOptions.h"
40#include "src/gpu/GrShaderCaps.h"
41#endif
42
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -040043#ifdef SK_ENABLE_SPIRV_VALIDATION
44#include "spirv-tools/libspirv.hpp"
45#endif
46
Brian Osmandd496172020-08-08 08:17:18 -040047#if !SKSL_STANDALONE
Ethan Nicholasc18bb512020-07-28 14:46:53 -040048
49#include "src/sksl/generated/sksl_fp.dehydrated.sksl"
50#include "src/sksl/generated/sksl_frag.dehydrated.sksl"
51#include "src/sksl/generated/sksl_geom.dehydrated.sksl"
52#include "src/sksl/generated/sksl_gpu.dehydrated.sksl"
53#include "src/sksl/generated/sksl_interp.dehydrated.sksl"
54#include "src/sksl/generated/sksl_pipeline.dehydrated.sksl"
55#include "src/sksl/generated/sksl_vert.dehydrated.sksl"
56
57#else
58
Brian Osmandd496172020-08-08 08:17:18 -040059// GN generates or copies all of these files to the skslc executable directory
60static const char SKSL_GPU_INCLUDE[] = "sksl_gpu.sksl";
61static const char SKSL_INTERP_INCLUDE[] = "sksl_interp.sksl";
62static const char SKSL_VERT_INCLUDE[] = "sksl_vert.sksl";
63static const char SKSL_FRAG_INCLUDE[] = "sksl_frag.sksl";
64static const char SKSL_GEOM_INCLUDE[] = "sksl_geom.sksl";
65static const char SKSL_FP_INCLUDE[] = "sksl_fp.sksl";
66static const char SKSL_PIPELINE_INCLUDE[] = "sksl_pipeline.sksl";
Ethan Nicholasc18bb512020-07-28 14:46:53 -040067
68#endif
Ethan Nicholas0d997662019-04-08 09:46:01 -040069
ethannicholasb3058bd2016-07-01 08:22:01 -070070namespace SkSL {
71
Ethan Nicholasdb80f692019-11-22 14:06:12 -050072static void grab_intrinsics(std::vector<std::unique_ptr<ProgramElement>>* src,
Brian Osman08f986d2020-05-13 17:06:46 -040073 std::map<String, std::pair<std::unique_ptr<ProgramElement>, bool>>* target) {
74 for (auto iter = src->begin(); iter != src->end(); ) {
75 std::unique_ptr<ProgramElement>& element = *iter;
Ethan Nicholasdb80f692019-11-22 14:06:12 -050076 switch (element->fKind) {
77 case ProgramElement::kFunction_Kind: {
78 FunctionDefinition& f = (FunctionDefinition&) *element;
Brian Osman08f986d2020-05-13 17:06:46 -040079 SkASSERT(f.fDeclaration.fBuiltin);
Ethan Nicholasc18bb512020-07-28 14:46:53 -040080 String key = f.fDeclaration.description();
Brian Osman08f986d2020-05-13 17:06:46 -040081 SkASSERT(target->find(key) == target->end());
82 (*target)[key] = std::make_pair(std::move(element), false);
83 iter = src->erase(iter);
Ethan Nicholasdb80f692019-11-22 14:06:12 -050084 break;
85 }
86 case ProgramElement::kEnum_Kind: {
87 Enum& e = (Enum&) *element;
88 StringFragment name = e.fTypeName;
89 SkASSERT(target->find(name) == target->end());
90 (*target)[name] = std::make_pair(std::move(element), false);
Brian Osman08f986d2020-05-13 17:06:46 -040091 iter = src->erase(iter);
Ethan Nicholasdb80f692019-11-22 14:06:12 -050092 break;
93 }
94 default:
95 printf("unsupported include file element\n");
96 SkASSERT(false);
97 }
98 }
99}
100
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -0400101Compiler::Compiler(Flags flags)
102: fFlags(flags)
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400103, fContext(new Context())
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -0400104, fErrorCount(0) {
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400105 auto symbols = std::shared_ptr<SymbolTable>(new SymbolTable(this));
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400106 fIRGenerator = new IRGenerator(fContext.get(), symbols, *this);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400107 #define ADD_TYPE(t) symbols->addWithoutOwnership(fContext->f ## t ## _Type->fName, \
108 fContext->f ## t ## _Type.get())
ethannicholasb3058bd2016-07-01 08:22:01 -0700109 ADD_TYPE(Void);
110 ADD_TYPE(Float);
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400111 ADD_TYPE(Float2);
112 ADD_TYPE(Float3);
113 ADD_TYPE(Float4);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400114 ADD_TYPE(Half);
115 ADD_TYPE(Half2);
116 ADD_TYPE(Half3);
117 ADD_TYPE(Half4);
ethannicholasb3058bd2016-07-01 08:22:01 -0700118 ADD_TYPE(Int);
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400119 ADD_TYPE(Int2);
120 ADD_TYPE(Int3);
121 ADD_TYPE(Int4);
ethannicholasb3058bd2016-07-01 08:22:01 -0700122 ADD_TYPE(UInt);
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400123 ADD_TYPE(UInt2);
124 ADD_TYPE(UInt3);
125 ADD_TYPE(UInt4);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400126 ADD_TYPE(Short);
127 ADD_TYPE(Short2);
128 ADD_TYPE(Short3);
129 ADD_TYPE(Short4);
130 ADD_TYPE(UShort);
131 ADD_TYPE(UShort2);
132 ADD_TYPE(UShort3);
133 ADD_TYPE(UShort4);
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400134 ADD_TYPE(Byte);
135 ADD_TYPE(Byte2);
136 ADD_TYPE(Byte3);
137 ADD_TYPE(Byte4);
138 ADD_TYPE(UByte);
139 ADD_TYPE(UByte2);
140 ADD_TYPE(UByte3);
141 ADD_TYPE(UByte4);
ethannicholasb3058bd2016-07-01 08:22:01 -0700142 ADD_TYPE(Bool);
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400143 ADD_TYPE(Bool2);
144 ADD_TYPE(Bool3);
145 ADD_TYPE(Bool4);
146 ADD_TYPE(Float2x2);
147 ADD_TYPE(Float2x3);
148 ADD_TYPE(Float2x4);
149 ADD_TYPE(Float3x2);
150 ADD_TYPE(Float3x3);
151 ADD_TYPE(Float3x4);
152 ADD_TYPE(Float4x2);
153 ADD_TYPE(Float4x3);
154 ADD_TYPE(Float4x4);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400155 ADD_TYPE(Half2x2);
156 ADD_TYPE(Half2x3);
157 ADD_TYPE(Half2x4);
158 ADD_TYPE(Half3x2);
159 ADD_TYPE(Half3x3);
160 ADD_TYPE(Half3x4);
161 ADD_TYPE(Half4x2);
162 ADD_TYPE(Half4x3);
163 ADD_TYPE(Half4x4);
ethannicholasb3058bd2016-07-01 08:22:01 -0700164 ADD_TYPE(GenType);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400165 ADD_TYPE(GenHType);
ethannicholasb3058bd2016-07-01 08:22:01 -0700166 ADD_TYPE(GenIType);
167 ADD_TYPE(GenUType);
168 ADD_TYPE(GenBType);
169 ADD_TYPE(Mat);
170 ADD_TYPE(Vec);
171 ADD_TYPE(GVec);
172 ADD_TYPE(GVec2);
173 ADD_TYPE(GVec3);
174 ADD_TYPE(GVec4);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400175 ADD_TYPE(HVec);
ethannicholasb3058bd2016-07-01 08:22:01 -0700176 ADD_TYPE(IVec);
177 ADD_TYPE(UVec);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400178 ADD_TYPE(SVec);
179 ADD_TYPE(USVec);
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400180 ADD_TYPE(ByteVec);
181 ADD_TYPE(UByteVec);
ethannicholasb3058bd2016-07-01 08:22:01 -0700182 ADD_TYPE(BVec);
183
184 ADD_TYPE(Sampler1D);
185 ADD_TYPE(Sampler2D);
186 ADD_TYPE(Sampler3D);
ethannicholas5961bc92016-10-12 06:39:56 -0700187 ADD_TYPE(SamplerExternalOES);
ethannicholasb3058bd2016-07-01 08:22:01 -0700188 ADD_TYPE(SamplerCube);
189 ADD_TYPE(Sampler2DRect);
190 ADD_TYPE(Sampler1DArray);
191 ADD_TYPE(Sampler2DArray);
192 ADD_TYPE(SamplerCubeArray);
193 ADD_TYPE(SamplerBuffer);
194 ADD_TYPE(Sampler2DMS);
195 ADD_TYPE(Sampler2DMSArray);
196
Brian Salomonbf7b6202016-11-11 16:08:03 -0500197 ADD_TYPE(ISampler2D);
198
Brian Salomon2a51de82016-11-16 12:06:01 -0500199 ADD_TYPE(Image2D);
200 ADD_TYPE(IImage2D);
201
Greg Daniel64773e62016-11-22 09:44:03 -0500202 ADD_TYPE(SubpassInput);
203 ADD_TYPE(SubpassInputMS);
204
ethannicholasb3058bd2016-07-01 08:22:01 -0700205 ADD_TYPE(GSampler1D);
206 ADD_TYPE(GSampler2D);
207 ADD_TYPE(GSampler3D);
208 ADD_TYPE(GSamplerCube);
209 ADD_TYPE(GSampler2DRect);
210 ADD_TYPE(GSampler1DArray);
211 ADD_TYPE(GSampler2DArray);
212 ADD_TYPE(GSamplerCubeArray);
213 ADD_TYPE(GSamplerBuffer);
214 ADD_TYPE(GSampler2DMS);
215 ADD_TYPE(GSampler2DMSArray);
216
217 ADD_TYPE(Sampler1DShadow);
218 ADD_TYPE(Sampler2DShadow);
219 ADD_TYPE(SamplerCubeShadow);
220 ADD_TYPE(Sampler2DRectShadow);
221 ADD_TYPE(Sampler1DArrayShadow);
222 ADD_TYPE(Sampler2DArrayShadow);
223 ADD_TYPE(SamplerCubeArrayShadow);
224 ADD_TYPE(GSampler2DArrayShadow);
225 ADD_TYPE(GSamplerCubeArrayShadow);
Ethan Nicholasc9472af2017-10-10 16:30:21 -0400226 ADD_TYPE(FragmentProcessor);
Stephen Whiteff5d7a22019-07-26 17:42:06 -0400227 ADD_TYPE(Sampler);
228 ADD_TYPE(Texture2D);
ethannicholasb3058bd2016-07-01 08:22:01 -0700229
Brian Osman28590d52020-03-23 16:59:08 -0400230 StringFragment fpAliasName("shader");
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400231 symbols->addWithoutOwnership(fpAliasName, fContext->fFragmentProcessor_Type.get());
Brian Osman28590d52020-03-23 16:59:08 -0400232
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700233 StringFragment skCapsName("sk_Caps");
John Stiles311dd9d2020-08-13 17:09:29 -0400234 fIRGenerator->fSymbolTable->add(
235 skCapsName,
236 std::make_unique<Variable>(/*offset=*/-1, Modifiers(), skCapsName,
237 *fContext->fSkCaps_Type, Variable::kGlobal_Storage));
Ethan Nicholas3605ace2016-11-21 15:59:48 -0500238
Ethan Nicholasdb80f692019-11-22 14:06:12 -0500239 fIRGenerator->fIntrinsics = &fGPUIntrinsics;
240 std::vector<std::unique_ptr<ProgramElement>> gpuIntrinsics;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400241 std::vector<std::unique_ptr<ProgramElement>> interpIntrinsics;
Brian Osmandd496172020-08-08 08:17:18 -0400242#if SKSL_STANDALONE
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -0400243 this->processIncludeFile(Program::kFragment_Kind, SKSL_GPU_INCLUDE, symbols, &gpuIntrinsics,
244 &fGpuSymbolTable);
245 this->processIncludeFile(Program::kVertex_Kind, SKSL_VERT_INCLUDE, fGpuSymbolTable,
246 &fVertexInclude, &fVertexSymbolTable);
247 this->processIncludeFile(Program::kFragment_Kind, SKSL_FRAG_INCLUDE, fGpuSymbolTable,
248 &fFragmentInclude, &fFragmentSymbolTable);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400249#else
250 {
251 Rehydrator rehydrator(fContext.get(), symbols, this, SKSL_INCLUDE_sksl_gpu,
252 SKSL_INCLUDE_sksl_gpu_LENGTH);
253 fGpuSymbolTable = rehydrator.symbolTable();
254 gpuIntrinsics = rehydrator.elements();
255 }
256 {
257 Rehydrator rehydrator(fContext.get(), fGpuSymbolTable, this, SKSL_INCLUDE_sksl_vert,
258 SKSL_INCLUDE_sksl_vert_LENGTH);
259 fVertexSymbolTable = rehydrator.symbolTable();
260 fVertexInclude = rehydrator.elements();
261 }
262 {
263 Rehydrator rehydrator(fContext.get(), fGpuSymbolTable, this, SKSL_INCLUDE_sksl_frag,
264 SKSL_INCLUDE_sksl_frag_LENGTH);
265 fFragmentSymbolTable = rehydrator.symbolTable();
266 fFragmentInclude = rehydrator.elements();
267 }
268#endif
269 grab_intrinsics(&gpuIntrinsics, &fGPUIntrinsics);
Brian Osmanb08cc022020-04-02 11:38:40 -0400270 grab_intrinsics(&interpIntrinsics, &fInterpreterIntrinsics);
ethannicholasb3058bd2016-07-01 08:22:01 -0700271}
272
273Compiler::~Compiler() {
274 delete fIRGenerator;
275}
276
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400277void Compiler::loadGeometryIntrinsics() {
278 if (fGeometrySymbolTable) {
279 return;
280 }
Brian Osmandd496172020-08-08 08:17:18 -0400281 #if !SKSL_STANDALONE
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400282 {
283 Rehydrator rehydrator(fContext.get(), fGpuSymbolTable, this, SKSL_INCLUDE_sksl_geom,
284 SKSL_INCLUDE_sksl_geom_LENGTH);
285 fGeometrySymbolTable = rehydrator.symbolTable();
286 fGeometryInclude = rehydrator.elements();
287 }
288 #else
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -0400289 this->processIncludeFile(Program::kGeometry_Kind, SKSL_GEOM_INCLUDE, fGpuSymbolTable,
290 &fGeometryInclude, &fGeometrySymbolTable);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400291 #endif
292}
293
294void Compiler::loadPipelineIntrinsics() {
295 if (fPipelineSymbolTable) {
296 return;
297 }
Brian Osmandd496172020-08-08 08:17:18 -0400298 #if !SKSL_STANDALONE
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400299 {
300 Rehydrator rehydrator(fContext.get(), fGpuSymbolTable, this,
301 SKSL_INCLUDE_sksl_pipeline,
302 SKSL_INCLUDE_sksl_pipeline_LENGTH);
303 fPipelineSymbolTable = rehydrator.symbolTable();
304 fPipelineInclude = rehydrator.elements();
305 }
306 #else
307 this->processIncludeFile(Program::kPipelineStage_Kind, SKSL_PIPELINE_INCLUDE,
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -0400308 fGpuSymbolTable, &fPipelineInclude, &fPipelineSymbolTable);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400309 #endif
310}
311
312void Compiler::loadInterpreterIntrinsics() {
313 if (fInterpreterSymbolTable) {
314 return;
315 }
316 this->loadPipelineIntrinsics();
Brian Osmandd496172020-08-08 08:17:18 -0400317 #if !SKSL_STANDALONE
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400318 {
319 Rehydrator rehydrator(fContext.get(), fPipelineSymbolTable, this,
320 SKSL_INCLUDE_sksl_interp,
321 SKSL_INCLUDE_sksl_interp_LENGTH);
322 fInterpreterSymbolTable = rehydrator.symbolTable();
323 fInterpreterInclude = rehydrator.elements();
324 }
325 #else
326 this->processIncludeFile(Program::kGeneric_Kind, SKSL_INTERP_INCLUDE,
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -0400327 fIRGenerator->fSymbolTable, &fInterpreterInclude,
328 &fInterpreterSymbolTable);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400329 #endif
330}
331
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -0400332void Compiler::processIncludeFile(Program::Kind kind, const char* path,
Ethan Nicholas8da1e652019-05-24 11:01:59 -0400333 std::shared_ptr<SymbolTable> base,
334 std::vector<std::unique_ptr<ProgramElement>>* outElements,
335 std::shared_ptr<SymbolTable>* outSymbolTable) {
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -0400336 std::ifstream in(path);
337 std::string stdText{std::istreambuf_iterator<char>(in),
338 std::istreambuf_iterator<char>()};
339 if (in.rdstate()) {
340 printf("error reading %s\n", path);
341 abort();
342 }
343 if (!base) {
344 base = fIRGenerator->fSymbolTable;
345 }
346 SkASSERT(base);
347 const String* source = base->takeOwnershipOfString(std::make_unique<String>(stdText.c_str()));
348 fSource = source;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400349 std::shared_ptr<SymbolTable> old = fIRGenerator->fSymbolTable;
350 if (base) {
351 fIRGenerator->fSymbolTable = std::move(base);
352 }
Ethan Nicholas8da1e652019-05-24 11:01:59 -0400353 Program::Settings settings;
Ethan Nicholasa11035b2019-11-26 16:27:47 -0500354#if !defined(SKSL_STANDALONE) & SK_SUPPORT_GPU
355 GrContextOptions opts;
356 GrShaderCaps caps(opts);
357 settings.fCaps = &caps;
358#endif
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400359 SkASSERT(fIRGenerator->fCanInline);
360 fIRGenerator->fCanInline = false;
361 fIRGenerator->start(&settings, nullptr, true);
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -0400362 fIRGenerator->convertProgram(kind, source->c_str(), source->length(), outElements);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400363 fIRGenerator->fCanInline = true;
Ethan Nicholas8da1e652019-05-24 11:01:59 -0400364 if (this->fErrorCount) {
365 printf("Unexpected errors: %s\n", this->fErrorText.c_str());
366 }
367 SkASSERT(!fErrorCount);
Ethan Nicholas8da1e652019-05-24 11:01:59 -0400368 *outSymbolTable = fIRGenerator->fSymbolTable;
Ethan Nicholasa11035b2019-11-26 16:27:47 -0500369#ifdef SK_DEBUG
370 fSource = nullptr;
371#endif
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400372 fIRGenerator->fSymbolTable = std::move(old);
Ethan Nicholas8da1e652019-05-24 11:01:59 -0400373}
374
ethannicholas22f939e2016-10-13 13:25:34 -0700375// add the definition created by assigning to the lvalue to the definition set
Ethan Nicholas86a43402017-01-19 13:32:00 -0500376void Compiler::addDefinition(const Expression* lvalue, std::unique_ptr<Expression>* expr,
377 DefinitionMap* definitions) {
ethannicholas22f939e2016-10-13 13:25:34 -0700378 switch (lvalue->fKind) {
379 case Expression::kVariableReference_Kind: {
380 const Variable& var = ((VariableReference*) lvalue)->fVariable;
381 if (var.fStorage == Variable::kLocal_Storage) {
382 (*definitions)[&var] = expr;
383 }
384 break;
385 }
386 case Expression::kSwizzle_Kind:
387 // We consider the variable written to as long as at least some of its components have
388 // been written to. This will lead to some false negatives (we won't catch it if you
389 // write to foo.x and then read foo.y), but being stricter could lead to false positives
Mike Klein6ad99092016-10-26 10:35:22 -0400390 // (we write to foo.x, and then pass foo to a function which happens to only read foo.x,
391 // but since we pass foo as a whole it is flagged as an error) unless we perform a much
ethannicholas22f939e2016-10-13 13:25:34 -0700392 // more complicated whole-program analysis. This is probably good enough.
Mike Klein6ad99092016-10-26 10:35:22 -0400393 this->addDefinition(((Swizzle*) lvalue)->fBase.get(),
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400394 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
ethannicholas22f939e2016-10-13 13:25:34 -0700395 definitions);
396 break;
397 case Expression::kIndex_Kind:
398 // see comments in Swizzle
Mike Klein6ad99092016-10-26 10:35:22 -0400399 this->addDefinition(((IndexExpression*) lvalue)->fBase.get(),
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400400 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
ethannicholas22f939e2016-10-13 13:25:34 -0700401 definitions);
402 break;
403 case Expression::kFieldAccess_Kind:
404 // see comments in Swizzle
Mike Klein6ad99092016-10-26 10:35:22 -0400405 this->addDefinition(((FieldAccess*) lvalue)->fBase.get(),
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400406 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
ethannicholas22f939e2016-10-13 13:25:34 -0700407 definitions);
408 break;
Ethan Nicholasa583b812018-01-18 13:32:11 -0500409 case Expression::kTernary_Kind:
410 // To simplify analysis, we just pretend that we write to both sides of the ternary.
411 // This allows for false positives (meaning we fail to detect that a variable might not
412 // have been assigned), but is preferable to false negatives.
413 this->addDefinition(((TernaryExpression*) lvalue)->fIfTrue.get(),
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400414 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
Ethan Nicholasa583b812018-01-18 13:32:11 -0500415 definitions);
416 this->addDefinition(((TernaryExpression*) lvalue)->fIfFalse.get(),
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400417 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
Ethan Nicholasa583b812018-01-18 13:32:11 -0500418 definitions);
419 break;
Ethan Nicholas91164d12019-05-15 15:29:54 -0400420 case Expression::kExternalValue_Kind:
421 break;
ethannicholas22f939e2016-10-13 13:25:34 -0700422 default:
423 // not an lvalue, can't happen
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400424 SkASSERT(false);
ethannicholas22f939e2016-10-13 13:25:34 -0700425 }
426}
427
428// add local variables defined by this node to the set
Mike Klein6ad99092016-10-26 10:35:22 -0400429void Compiler::addDefinitions(const BasicBlock::Node& node,
Ethan Nicholas86a43402017-01-19 13:32:00 -0500430 DefinitionMap* definitions) {
ethannicholas22f939e2016-10-13 13:25:34 -0700431 switch (node.fKind) {
432 case BasicBlock::Node::kExpression_Kind: {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400433 SkASSERT(node.expression());
Ethan Nicholascb670962017-04-20 19:31:52 -0400434 const Expression* expr = (Expression*) node.expression()->get();
Ethan Nicholas86a43402017-01-19 13:32:00 -0500435 switch (expr->fKind) {
436 case Expression::kBinary_Kind: {
437 BinaryExpression* b = (BinaryExpression*) expr;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400438 if (b->fOperator == Token::Kind::TK_EQ) {
Ethan Nicholas86a43402017-01-19 13:32:00 -0500439 this->addDefinition(b->fLeft.get(), &b->fRight, definitions);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700440 } else if (Compiler::IsAssignment(b->fOperator)) {
Ethan Nicholas86a43402017-01-19 13:32:00 -0500441 this->addDefinition(
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400442 b->fLeft.get(),
443 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
444 definitions);
Ethan Nicholas86a43402017-01-19 13:32:00 -0500445
446 }
447 break;
ethannicholas22f939e2016-10-13 13:25:34 -0700448 }
Ethan Nicholasc6a19f12018-03-29 16:46:56 -0400449 case Expression::kFunctionCall_Kind: {
450 const FunctionCall& c = (const FunctionCall&) *expr;
451 for (size_t i = 0; i < c.fFunction.fParameters.size(); ++i) {
452 if (c.fFunction.fParameters[i]->fModifiers.fFlags & Modifiers::kOut_Flag) {
453 this->addDefinition(
454 c.fArguments[i].get(),
455 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
456 definitions);
457 }
458 }
459 break;
460 }
Ethan Nicholas86a43402017-01-19 13:32:00 -0500461 case Expression::kPrefix_Kind: {
462 const PrefixExpression* p = (PrefixExpression*) expr;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400463 if (p->fOperator == Token::Kind::TK_MINUSMINUS ||
464 p->fOperator == Token::Kind::TK_PLUSPLUS) {
Ethan Nicholas86a43402017-01-19 13:32:00 -0500465 this->addDefinition(
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400466 p->fOperand.get(),
467 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
468 definitions);
Ethan Nicholas86a43402017-01-19 13:32:00 -0500469 }
470 break;
471 }
472 case Expression::kPostfix_Kind: {
473 const PostfixExpression* p = (PostfixExpression*) expr;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400474 if (p->fOperator == Token::Kind::TK_MINUSMINUS ||
475 p->fOperator == Token::Kind::TK_PLUSPLUS) {
Ethan Nicholas86a43402017-01-19 13:32:00 -0500476 this->addDefinition(
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400477 p->fOperand.get(),
478 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
479 definitions);
Ethan Nicholas86a43402017-01-19 13:32:00 -0500480 }
481 break;
482 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400483 case Expression::kVariableReference_Kind: {
484 const VariableReference* v = (VariableReference*) expr;
485 if (v->fRefKind != VariableReference::kRead_RefKind) {
486 this->addDefinition(
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400487 v,
488 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
489 definitions);
Ethan Nicholascb670962017-04-20 19:31:52 -0400490 }
John Stiles30212b72020-06-11 17:55:07 -0400491 break;
Ethan Nicholascb670962017-04-20 19:31:52 -0400492 }
Ethan Nicholas86a43402017-01-19 13:32:00 -0500493 default:
494 break;
ethannicholas22f939e2016-10-13 13:25:34 -0700495 }
496 break;
497 }
498 case BasicBlock::Node::kStatement_Kind: {
Ethan Nicholascb670962017-04-20 19:31:52 -0400499 const Statement* stmt = (Statement*) node.statement()->get();
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000500 if (stmt->fKind == Statement::kVarDeclaration_Kind) {
501 VarDeclaration& vd = (VarDeclaration&) *stmt;
502 if (vd.fValue) {
503 (*definitions)[vd.fVar] = &vd.fValue;
ethannicholas22f939e2016-10-13 13:25:34 -0700504 }
505 }
506 break;
507 }
508 }
509}
510
511void Compiler::scanCFG(CFG* cfg, BlockId blockId, std::set<BlockId>* workList) {
512 BasicBlock& block = cfg->fBlocks[blockId];
513
514 // compute definitions after this block
Ethan Nicholas86a43402017-01-19 13:32:00 -0500515 DefinitionMap after = block.fBefore;
ethannicholas22f939e2016-10-13 13:25:34 -0700516 for (const BasicBlock::Node& n : block.fNodes) {
517 this->addDefinitions(n, &after);
518 }
519
520 // propagate definitions to exits
521 for (BlockId exitId : block.fExits) {
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400522 if (exitId == blockId) {
523 continue;
524 }
ethannicholas22f939e2016-10-13 13:25:34 -0700525 BasicBlock& exit = cfg->fBlocks[exitId];
526 for (const auto& pair : after) {
Ethan Nicholas86a43402017-01-19 13:32:00 -0500527 std::unique_ptr<Expression>* e1 = pair.second;
528 auto found = exit.fBefore.find(pair.first);
529 if (found == exit.fBefore.end()) {
530 // exit has no definition for it, just copy it
531 workList->insert(exitId);
ethannicholas22f939e2016-10-13 13:25:34 -0700532 exit.fBefore[pair.first] = e1;
533 } else {
Ethan Nicholas86a43402017-01-19 13:32:00 -0500534 // exit has a (possibly different) value already defined
535 std::unique_ptr<Expression>* e2 = exit.fBefore[pair.first];
ethannicholas22f939e2016-10-13 13:25:34 -0700536 if (e1 != e2) {
537 // definition has changed, merge and add exit block to worklist
538 workList->insert(exitId);
Ethan Nicholasaf197692017-02-27 13:26:45 -0500539 if (e1 && e2) {
540 exit.fBefore[pair.first] =
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400541 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression;
Ethan Nicholasaf197692017-02-27 13:26:45 -0500542 } else {
543 exit.fBefore[pair.first] = nullptr;
544 }
ethannicholas22f939e2016-10-13 13:25:34 -0700545 }
546 }
547 }
548 }
549}
550
551// returns a map which maps all local variables in the function to null, indicating that their value
552// is initially unknown
Ethan Nicholas86a43402017-01-19 13:32:00 -0500553static DefinitionMap compute_start_state(const CFG& cfg) {
554 DefinitionMap result;
Mike Klein6ad99092016-10-26 10:35:22 -0400555 for (const auto& block : cfg.fBlocks) {
556 for (const auto& node : block.fNodes) {
ethannicholas22f939e2016-10-13 13:25:34 -0700557 if (node.fKind == BasicBlock::Node::kStatement_Kind) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400558 SkASSERT(node.statement());
Ethan Nicholascb670962017-04-20 19:31:52 -0400559 const Statement* s = node.statement()->get();
ethannicholas22f939e2016-10-13 13:25:34 -0700560 if (s->fKind == Statement::kVarDeclarations_Kind) {
561 const VarDeclarationsStatement* vd = (const VarDeclarationsStatement*) s;
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000562 for (const auto& decl : vd->fDeclaration->fVars) {
563 if (decl->fKind == Statement::kVarDeclaration_Kind) {
564 result[((VarDeclaration&) *decl).fVar] = nullptr;
565 }
Mike Klein6ad99092016-10-26 10:35:22 -0400566 }
ethannicholas22f939e2016-10-13 13:25:34 -0700567 }
568 }
569 }
570 }
571 return result;
572}
573
Ethan Nicholascb670962017-04-20 19:31:52 -0400574/**
575 * Returns true if assigning to this lvalue has no effect.
576 */
577static bool is_dead(const Expression& lvalue) {
578 switch (lvalue.fKind) {
579 case Expression::kVariableReference_Kind:
580 return ((VariableReference&) lvalue).fVariable.dead();
581 case Expression::kSwizzle_Kind:
582 return is_dead(*((Swizzle&) lvalue).fBase);
583 case Expression::kFieldAccess_Kind:
584 return is_dead(*((FieldAccess&) lvalue).fBase);
585 case Expression::kIndex_Kind: {
586 const IndexExpression& idx = (IndexExpression&) lvalue;
Ethan Nicholas2a099da2020-01-02 14:40:54 -0500587 return is_dead(*idx.fBase) &&
588 !idx.fIndex->hasProperty(Expression::Property::kSideEffects);
Ethan Nicholascb670962017-04-20 19:31:52 -0400589 }
Ethan Nicholasa583b812018-01-18 13:32:11 -0500590 case Expression::kTernary_Kind: {
591 const TernaryExpression& t = (TernaryExpression&) lvalue;
592 return !t.fTest->hasSideEffects() && is_dead(*t.fIfTrue) && is_dead(*t.fIfFalse);
593 }
Ethan Nicholas91164d12019-05-15 15:29:54 -0400594 case Expression::kExternalValue_Kind:
595 return false;
Ethan Nicholascb670962017-04-20 19:31:52 -0400596 default:
Ethan Nicholas2a099da2020-01-02 14:40:54 -0500597#ifdef SK_DEBUG
Ethan Nicholascb670962017-04-20 19:31:52 -0400598 ABORT("invalid lvalue: %s\n", lvalue.description().c_str());
Ethan Nicholas2a099da2020-01-02 14:40:54 -0500599#endif
600 return false;
Ethan Nicholascb670962017-04-20 19:31:52 -0400601 }
602}
ethannicholas22f939e2016-10-13 13:25:34 -0700603
Ethan Nicholascb670962017-04-20 19:31:52 -0400604/**
605 * Returns true if this is an assignment which can be collapsed down to just the right hand side due
606 * to a dead target and lack of side effects on the left hand side.
607 */
608static bool dead_assignment(const BinaryExpression& b) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700609 if (!Compiler::IsAssignment(b.fOperator)) {
Ethan Nicholascb670962017-04-20 19:31:52 -0400610 return false;
611 }
612 return is_dead(*b.fLeft);
613}
614
615void Compiler::computeDataFlow(CFG* cfg) {
616 cfg->fBlocks[cfg->fStart].fBefore = compute_start_state(*cfg);
ethannicholas22f939e2016-10-13 13:25:34 -0700617 std::set<BlockId> workList;
Ethan Nicholascb670962017-04-20 19:31:52 -0400618 for (BlockId i = 0; i < cfg->fBlocks.size(); i++) {
ethannicholas22f939e2016-10-13 13:25:34 -0700619 workList.insert(i);
620 }
621 while (workList.size()) {
622 BlockId next = *workList.begin();
623 workList.erase(workList.begin());
Ethan Nicholascb670962017-04-20 19:31:52 -0400624 this->scanCFG(cfg, next, &workList);
ethannicholas22f939e2016-10-13 13:25:34 -0700625 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400626}
627
628/**
629 * Attempts to replace the expression pointed to by iter with a new one (in both the CFG and the
630 * IR). If the expression can be cleanly removed, returns true and updates the iterator to point to
631 * the newly-inserted element. Otherwise updates only the IR and returns false (and the CFG will
632 * need to be regenerated).
633 */
634bool try_replace_expression(BasicBlock* b,
635 std::vector<BasicBlock::Node>::iterator* iter,
636 std::unique_ptr<Expression>* newExpression) {
637 std::unique_ptr<Expression>* target = (*iter)->expression();
638 if (!b->tryRemoveExpression(iter)) {
639 *target = std::move(*newExpression);
640 return false;
641 }
642 *target = std::move(*newExpression);
643 return b->tryInsertExpression(iter, target);
644}
645
646/**
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400647 * Returns true if the expression is a constant numeric literal with the specified value, or a
648 * constant vector with all elements equal to the specified value.
Ethan Nicholascb670962017-04-20 19:31:52 -0400649 */
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400650bool is_constant(const Expression& expr, double value) {
Ethan Nicholascb670962017-04-20 19:31:52 -0400651 switch (expr.fKind) {
652 case Expression::kIntLiteral_Kind:
653 return ((IntLiteral&) expr).fValue == value;
654 case Expression::kFloatLiteral_Kind:
655 return ((FloatLiteral&) expr).fValue == value;
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400656 case Expression::kConstructor_Kind: {
657 Constructor& c = (Constructor&) expr;
Ethan Nicholasd188c182019-06-10 15:55:38 -0400658 bool isFloat = c.fType.columns() > 1 ? c.fType.componentType().isFloat()
659 : c.fType.isFloat();
Brian Osmanb6b95732020-06-30 11:44:27 -0400660 if (c.fType.kind() == Type::kVector_Kind && c.isCompileTimeConstant()) {
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400661 for (int i = 0; i < c.fType.columns(); ++i) {
Ethan Nicholasd188c182019-06-10 15:55:38 -0400662 if (isFloat) {
663 if (c.getFVecComponent(i) != value) {
664 return false;
665 }
666 } else if (c.getIVecComponent(i) != value) {
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400667 return false;
668 }
669 }
670 return true;
671 }
672 return false;
673 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400674 default:
675 return false;
676 }
677}
678
679/**
680 * Collapses the binary expression pointed to by iter down to just the right side (in both the IR
681 * and CFG structures).
682 */
683void delete_left(BasicBlock* b,
684 std::vector<BasicBlock::Node>::iterator* iter,
685 bool* outUpdated,
686 bool* outNeedsRescan) {
687 *outUpdated = true;
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400688 std::unique_ptr<Expression>* target = (*iter)->expression();
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400689 SkASSERT((*target)->fKind == Expression::kBinary_Kind);
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400690 BinaryExpression& bin = (BinaryExpression&) **target;
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400691 SkASSERT(!bin.fLeft->hasSideEffects());
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400692 bool result;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400693 if (bin.fOperator == Token::Kind::TK_EQ) {
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400694 result = b->tryRemoveLValueBefore(iter, bin.fLeft.get());
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400695 } else {
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400696 result = b->tryRemoveExpressionBefore(iter, bin.fLeft.get());
Ethan Nicholascb670962017-04-20 19:31:52 -0400697 }
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400698 *target = std::move(bin.fRight);
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400699 if (!result) {
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400700 *outNeedsRescan = true;
701 return;
702 }
703 if (*iter == b->fNodes.begin()) {
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400704 *outNeedsRescan = true;
705 return;
706 }
707 --(*iter);
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400708 if ((*iter)->fKind != BasicBlock::Node::kExpression_Kind ||
709 (*iter)->expression() != &bin.fRight) {
710 *outNeedsRescan = true;
711 return;
712 }
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400713 *iter = b->fNodes.erase(*iter);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400714 SkASSERT((*iter)->expression() == target);
Ethan Nicholascb670962017-04-20 19:31:52 -0400715}
716
717/**
718 * Collapses the binary expression pointed to by iter down to just the left side (in both the IR and
719 * CFG structures).
720 */
721void delete_right(BasicBlock* b,
722 std::vector<BasicBlock::Node>::iterator* iter,
723 bool* outUpdated,
724 bool* outNeedsRescan) {
725 *outUpdated = true;
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400726 std::unique_ptr<Expression>* target = (*iter)->expression();
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400727 SkASSERT((*target)->fKind == Expression::kBinary_Kind);
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400728 BinaryExpression& bin = (BinaryExpression&) **target;
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400729 SkASSERT(!bin.fRight->hasSideEffects());
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400730 if (!b->tryRemoveExpressionBefore(iter, bin.fRight.get())) {
731 *target = std::move(bin.fLeft);
Ethan Nicholascb670962017-04-20 19:31:52 -0400732 *outNeedsRescan = true;
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400733 return;
Ethan Nicholascb670962017-04-20 19:31:52 -0400734 }
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400735 *target = std::move(bin.fLeft);
736 if (*iter == b->fNodes.begin()) {
737 *outNeedsRescan = true;
738 return;
739 }
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400740 --(*iter);
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400741 if (((*iter)->fKind != BasicBlock::Node::kExpression_Kind ||
742 (*iter)->expression() != &bin.fLeft)) {
743 *outNeedsRescan = true;
744 return;
745 }
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400746 *iter = b->fNodes.erase(*iter);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400747 SkASSERT((*iter)->expression() == target);
Ethan Nicholascb670962017-04-20 19:31:52 -0400748}
749
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400750/**
751 * Constructs the specified type using a single argument.
752 */
753static std::unique_ptr<Expression> construct(const Type& type, std::unique_ptr<Expression> v) {
754 std::vector<std::unique_ptr<Expression>> args;
755 args.push_back(std::move(v));
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700756 auto result = std::unique_ptr<Expression>(new Constructor(-1, type, std::move(args)));
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400757 return result;
758}
759
760/**
761 * Used in the implementations of vectorize_left and vectorize_right. Given a vector type and an
762 * expression x, deletes the expression pointed to by iter and replaces it with <type>(x).
763 */
764static void vectorize(BasicBlock* b,
765 std::vector<BasicBlock::Node>::iterator* iter,
766 const Type& type,
767 std::unique_ptr<Expression>* otherExpression,
768 bool* outUpdated,
769 bool* outNeedsRescan) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400770 SkASSERT((*(*iter)->expression())->fKind == Expression::kBinary_Kind);
771 SkASSERT(type.kind() == Type::kVector_Kind);
772 SkASSERT((*otherExpression)->fType.kind() == Type::kScalar_Kind);
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400773 *outUpdated = true;
774 std::unique_ptr<Expression>* target = (*iter)->expression();
775 if (!b->tryRemoveExpression(iter)) {
776 *target = construct(type, std::move(*otherExpression));
777 *outNeedsRescan = true;
778 } else {
779 *target = construct(type, std::move(*otherExpression));
780 if (!b->tryInsertExpression(iter, target)) {
781 *outNeedsRescan = true;
782 }
783 }
784}
785
786/**
787 * Given a binary expression of the form x <op> vec<n>(y), deletes the right side and vectorizes the
788 * left to yield vec<n>(x).
789 */
790static void vectorize_left(BasicBlock* b,
791 std::vector<BasicBlock::Node>::iterator* iter,
792 bool* outUpdated,
793 bool* outNeedsRescan) {
794 BinaryExpression& bin = (BinaryExpression&) **(*iter)->expression();
795 vectorize(b, iter, bin.fRight->fType, &bin.fLeft, outUpdated, outNeedsRescan);
796}
797
798/**
799 * Given a binary expression of the form vec<n>(x) <op> y, deletes the left side and vectorizes the
800 * right to yield vec<n>(y).
801 */
802static void vectorize_right(BasicBlock* b,
803 std::vector<BasicBlock::Node>::iterator* iter,
804 bool* outUpdated,
805 bool* outNeedsRescan) {
806 BinaryExpression& bin = (BinaryExpression&) **(*iter)->expression();
807 vectorize(b, iter, bin.fLeft->fType, &bin.fRight, outUpdated, outNeedsRescan);
808}
809
810// Mark that an expression which we were writing to is no longer being written to
811void clear_write(const Expression& expr) {
812 switch (expr.fKind) {
813 case Expression::kVariableReference_Kind: {
814 ((VariableReference&) expr).setRefKind(VariableReference::kRead_RefKind);
815 break;
816 }
817 case Expression::kFieldAccess_Kind:
818 clear_write(*((FieldAccess&) expr).fBase);
819 break;
820 case Expression::kSwizzle_Kind:
821 clear_write(*((Swizzle&) expr).fBase);
822 break;
823 case Expression::kIndex_Kind:
824 clear_write(*((IndexExpression&) expr).fBase);
825 break;
826 default:
827 ABORT("shouldn't be writing to this kind of expression\n");
828 break;
829 }
830}
831
Ethan Nicholascb670962017-04-20 19:31:52 -0400832void Compiler::simplifyExpression(DefinitionMap& definitions,
833 BasicBlock& b,
834 std::vector<BasicBlock::Node>::iterator* iter,
835 std::unordered_set<const Variable*>* undefinedVariables,
836 bool* outUpdated,
837 bool* outNeedsRescan) {
838 Expression* expr = (*iter)->expression()->get();
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400839 SkASSERT(expr);
Ethan Nicholascb670962017-04-20 19:31:52 -0400840 if ((*iter)->fConstantPropagation) {
841 std::unique_ptr<Expression> optimized = expr->constantPropagate(*fIRGenerator, definitions);
842 if (optimized) {
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400843 *outUpdated = true;
Ethan Nicholascb670962017-04-20 19:31:52 -0400844 if (!try_replace_expression(&b, iter, &optimized)) {
845 *outNeedsRescan = true;
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400846 return;
Ethan Nicholascb670962017-04-20 19:31:52 -0400847 }
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400848 SkASSERT((*iter)->fKind == BasicBlock::Node::kExpression_Kind);
Ethan Nicholascb670962017-04-20 19:31:52 -0400849 expr = (*iter)->expression()->get();
Ethan Nicholascb670962017-04-20 19:31:52 -0400850 }
851 }
852 switch (expr->fKind) {
853 case Expression::kVariableReference_Kind: {
Ethan Nicholas8f7e28f2018-03-26 14:24:27 -0400854 const VariableReference& ref = (VariableReference&) *expr;
855 const Variable& var = ref.fVariable;
856 if (ref.refKind() != VariableReference::kWrite_RefKind &&
857 ref.refKind() != VariableReference::kPointer_RefKind &&
858 var.fStorage == Variable::kLocal_Storage && !definitions[&var] &&
Ethan Nicholascb670962017-04-20 19:31:52 -0400859 (*undefinedVariables).find(&var) == (*undefinedVariables).end()) {
860 (*undefinedVariables).insert(&var);
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000861 this->error(expr->fOffset,
862 "'" + var.fName + "' has not been assigned");
Ethan Nicholascb670962017-04-20 19:31:52 -0400863 }
864 break;
865 }
866 case Expression::kTernary_Kind: {
867 TernaryExpression* t = (TernaryExpression*) expr;
868 if (t->fTest->fKind == Expression::kBoolLiteral_Kind) {
869 // ternary has a constant test, replace it with either the true or
870 // false branch
871 if (((BoolLiteral&) *t->fTest).fValue) {
872 (*iter)->setExpression(std::move(t->fIfTrue));
873 } else {
874 (*iter)->setExpression(std::move(t->fIfFalse));
875 }
876 *outUpdated = true;
877 *outNeedsRescan = true;
878 }
879 break;
880 }
881 case Expression::kBinary_Kind: {
Ethan Nicholascb670962017-04-20 19:31:52 -0400882 BinaryExpression* bin = (BinaryExpression*) expr;
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400883 if (dead_assignment(*bin)) {
884 delete_left(&b, iter, outUpdated, outNeedsRescan);
885 break;
886 }
887 // collapse useless expressions like x * 1 or x + 0
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400888 if (((bin->fLeft->fType.kind() != Type::kScalar_Kind) &&
889 (bin->fLeft->fType.kind() != Type::kVector_Kind)) ||
890 ((bin->fRight->fType.kind() != Type::kScalar_Kind) &&
891 (bin->fRight->fType.kind() != Type::kVector_Kind))) {
892 break;
893 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400894 switch (bin->fOperator) {
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400895 case Token::Kind::TK_STAR:
Ethan Nicholascb670962017-04-20 19:31:52 -0400896 if (is_constant(*bin->fLeft, 1)) {
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400897 if (bin->fLeft->fType.kind() == Type::kVector_Kind &&
898 bin->fRight->fType.kind() == Type::kScalar_Kind) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400899 // float4(1) * x -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400900 vectorize_right(&b, iter, outUpdated, outNeedsRescan);
901 } else {
902 // 1 * x -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400903 // 1 * float4(x) -> float4(x)
904 // float4(1) * float4(x) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400905 delete_left(&b, iter, outUpdated, outNeedsRescan);
906 }
907 }
908 else if (is_constant(*bin->fLeft, 0)) {
909 if (bin->fLeft->fType.kind() == Type::kScalar_Kind &&
Ethan Nicholas08dae922018-01-23 10:31:56 -0500910 bin->fRight->fType.kind() == Type::kVector_Kind &&
911 !bin->fRight->hasSideEffects()) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400912 // 0 * float4(x) -> float4(0)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400913 vectorize_left(&b, iter, outUpdated, outNeedsRescan);
914 } else {
915 // 0 * x -> 0
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400916 // float4(0) * x -> float4(0)
917 // float4(0) * float4(x) -> float4(0)
Ethan Nicholas51493ee2017-12-11 12:34:33 -0500918 if (!bin->fRight->hasSideEffects()) {
919 delete_right(&b, iter, outUpdated, outNeedsRescan);
920 }
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400921 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400922 }
923 else if (is_constant(*bin->fRight, 1)) {
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400924 if (bin->fLeft->fType.kind() == Type::kScalar_Kind &&
925 bin->fRight->fType.kind() == Type::kVector_Kind) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400926 // x * float4(1) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400927 vectorize_left(&b, iter, outUpdated, outNeedsRescan);
928 } else {
929 // x * 1 -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400930 // float4(x) * 1 -> float4(x)
931 // float4(x) * float4(1) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400932 delete_right(&b, iter, outUpdated, outNeedsRescan);
933 }
934 }
935 else if (is_constant(*bin->fRight, 0)) {
936 if (bin->fLeft->fType.kind() == Type::kVector_Kind &&
Ethan Nicholas08dae922018-01-23 10:31:56 -0500937 bin->fRight->fType.kind() == Type::kScalar_Kind &&
938 !bin->fLeft->hasSideEffects()) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400939 // float4(x) * 0 -> float4(0)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400940 vectorize_right(&b, iter, outUpdated, outNeedsRescan);
941 } else {
942 // x * 0 -> 0
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400943 // x * float4(0) -> float4(0)
944 // float4(x) * float4(0) -> float4(0)
Ethan Nicholas51493ee2017-12-11 12:34:33 -0500945 if (!bin->fLeft->hasSideEffects()) {
946 delete_left(&b, iter, outUpdated, outNeedsRescan);
947 }
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400948 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400949 }
950 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400951 case Token::Kind::TK_PLUS:
Ethan Nicholascb670962017-04-20 19:31:52 -0400952 if (is_constant(*bin->fLeft, 0)) {
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400953 if (bin->fLeft->fType.kind() == Type::kVector_Kind &&
954 bin->fRight->fType.kind() == Type::kScalar_Kind) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400955 // float4(0) + x -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400956 vectorize_right(&b, iter, outUpdated, outNeedsRescan);
957 } else {
958 // 0 + x -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400959 // 0 + float4(x) -> float4(x)
960 // float4(0) + float4(x) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400961 delete_left(&b, iter, outUpdated, outNeedsRescan);
962 }
963 } else if (is_constant(*bin->fRight, 0)) {
964 if (bin->fLeft->fType.kind() == Type::kScalar_Kind &&
965 bin->fRight->fType.kind() == Type::kVector_Kind) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400966 // x + float4(0) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400967 vectorize_left(&b, iter, outUpdated, outNeedsRescan);
968 } else {
969 // x + 0 -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400970 // float4(x) + 0 -> float4(x)
971 // float4(x) + float4(0) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400972 delete_right(&b, iter, outUpdated, outNeedsRescan);
973 }
Ethan Nicholas56e42712017-04-21 10:23:37 -0400974 }
975 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400976 case Token::Kind::TK_MINUS:
Ethan Nicholas56e42712017-04-21 10:23:37 -0400977 if (is_constant(*bin->fRight, 0)) {
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400978 if (bin->fLeft->fType.kind() == Type::kScalar_Kind &&
979 bin->fRight->fType.kind() == Type::kVector_Kind) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400980 // x - float4(0) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400981 vectorize_left(&b, iter, outUpdated, outNeedsRescan);
982 } else {
983 // x - 0 -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400984 // float4(x) - 0 -> float4(x)
985 // float4(x) - float4(0) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400986 delete_right(&b, iter, outUpdated, outNeedsRescan);
987 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400988 }
989 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400990 case Token::Kind::TK_SLASH:
Ethan Nicholascb670962017-04-20 19:31:52 -0400991 if (is_constant(*bin->fRight, 1)) {
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400992 if (bin->fLeft->fType.kind() == Type::kScalar_Kind &&
993 bin->fRight->fType.kind() == Type::kVector_Kind) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400994 // x / float4(1) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400995 vectorize_left(&b, iter, outUpdated, outNeedsRescan);
996 } else {
997 // x / 1 -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400998 // float4(x) / 1 -> float4(x)
999 // float4(x) / float4(1) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001000 delete_right(&b, iter, outUpdated, outNeedsRescan);
1001 }
1002 } else if (is_constant(*bin->fLeft, 0)) {
1003 if (bin->fLeft->fType.kind() == Type::kScalar_Kind &&
Ethan Nicholas08dae922018-01-23 10:31:56 -05001004 bin->fRight->fType.kind() == Type::kVector_Kind &&
1005 !bin->fRight->hasSideEffects()) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001006 // 0 / float4(x) -> float4(0)
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001007 vectorize_left(&b, iter, outUpdated, outNeedsRescan);
1008 } else {
1009 // 0 / x -> 0
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001010 // float4(0) / x -> float4(0)
1011 // float4(0) / float4(x) -> float4(0)
Ethan Nicholas51493ee2017-12-11 12:34:33 -05001012 if (!bin->fRight->hasSideEffects()) {
1013 delete_right(&b, iter, outUpdated, outNeedsRescan);
1014 }
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001015 }
1016 }
1017 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001018 case Token::Kind::TK_PLUSEQ:
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001019 if (is_constant(*bin->fRight, 0)) {
1020 clear_write(*bin->fLeft);
1021 delete_right(&b, iter, outUpdated, outNeedsRescan);
1022 }
1023 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001024 case Token::Kind::TK_MINUSEQ:
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001025 if (is_constant(*bin->fRight, 0)) {
1026 clear_write(*bin->fLeft);
1027 delete_right(&b, iter, outUpdated, outNeedsRescan);
1028 }
1029 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001030 case Token::Kind::TK_STAREQ:
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001031 if (is_constant(*bin->fRight, 1)) {
1032 clear_write(*bin->fLeft);
1033 delete_right(&b, iter, outUpdated, outNeedsRescan);
1034 }
1035 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001036 case Token::Kind::TK_SLASHEQ:
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001037 if (is_constant(*bin->fRight, 1)) {
1038 clear_write(*bin->fLeft);
Ethan Nicholascb670962017-04-20 19:31:52 -04001039 delete_right(&b, iter, outUpdated, outNeedsRescan);
1040 }
1041 break;
1042 default:
1043 break;
1044 }
Ethan Nicholas409f6f02019-09-17 12:34:39 -04001045 break;
1046 }
1047 case Expression::kSwizzle_Kind: {
1048 Swizzle& s = (Swizzle&) *expr;
1049 // detect identity swizzles like foo.rgba
1050 if ((int) s.fComponents.size() == s.fBase->fType.columns()) {
1051 bool identity = true;
1052 for (int i = 0; i < (int) s.fComponents.size(); ++i) {
1053 if (s.fComponents[i] != i) {
1054 identity = false;
1055 break;
1056 }
1057 }
1058 if (identity) {
1059 *outUpdated = true;
1060 if (!try_replace_expression(&b, iter, &s.fBase)) {
1061 *outNeedsRescan = true;
1062 return;
1063 }
1064 SkASSERT((*iter)->fKind == BasicBlock::Node::kExpression_Kind);
1065 break;
1066 }
1067 }
1068 // detect swizzles of swizzles, e.g. replace foo.argb.r000 with foo.a000
1069 if (s.fBase->fKind == Expression::kSwizzle_Kind) {
1070 Swizzle& base = (Swizzle&) *s.fBase;
1071 std::vector<int> final;
1072 for (int c : s.fComponents) {
1073 if (c == SKSL_SWIZZLE_0 || c == SKSL_SWIZZLE_1) {
1074 final.push_back(c);
1075 } else {
1076 final.push_back(base.fComponents[c]);
1077 }
1078 }
1079 *outUpdated = true;
1080 std::unique_ptr<Expression> replacement(new Swizzle(*fContext, base.fBase->clone(),
1081 std::move(final)));
1082 if (!try_replace_expression(&b, iter, &replacement)) {
1083 *outNeedsRescan = true;
1084 return;
1085 }
1086 SkASSERT((*iter)->fKind == BasicBlock::Node::kExpression_Kind);
Ethan Nicholas409f6f02019-09-17 12:34:39 -04001087 }
John Stiles30212b72020-06-11 17:55:07 -04001088 break;
Ethan Nicholascb670962017-04-20 19:31:52 -04001089 }
1090 default:
1091 break;
1092 }
1093}
1094
John Stiles92219b42020-06-15 12:32:24 -04001095// Implementation-detail recursive helper function for `contains_conditional_break`.
1096static bool contains_conditional_break_impl(Statement& s, bool inConditional) {
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001097 switch (s.fKind) {
1098 case Statement::kBlock_Kind:
John Stiles92219b42020-06-15 12:32:24 -04001099 for (const std::unique_ptr<Statement>& sub : static_cast<Block&>(s).fStatements) {
1100 if (contains_conditional_break_impl(*sub, inConditional)) {
Ethan Nicholas5005a222018-08-24 13:06:27 -04001101 return true;
1102 }
1103 }
1104 return false;
John Stiles92219b42020-06-15 12:32:24 -04001105
Ethan Nicholas5005a222018-08-24 13:06:27 -04001106 case Statement::kBreak_Kind:
1107 return inConditional;
John Stiles92219b42020-06-15 12:32:24 -04001108
Ethan Nicholas5005a222018-08-24 13:06:27 -04001109 case Statement::kIf_Kind: {
John Stiles92219b42020-06-15 12:32:24 -04001110 const IfStatement& i = static_cast<IfStatement&>(s);
1111 return contains_conditional_break_impl(*i.fIfTrue, /*inConditional=*/true) ||
1112 (i.fIfFalse &&
1113 contains_conditional_break_impl(*i.fIfFalse, /*inConditional=*/true));
Ethan Nicholas5005a222018-08-24 13:06:27 -04001114 }
John Stiles92219b42020-06-15 12:32:24 -04001115
Ethan Nicholas5005a222018-08-24 13:06:27 -04001116 default:
1117 return false;
1118 }
1119}
1120
John Stiles92219b42020-06-15 12:32:24 -04001121// Returns true if this statement could potentially execute a break at the current level. We ignore
1122// nested loops and switches, since any breaks inside of them will merely break the loop / switch.
1123static bool contains_conditional_break(Statement& s) {
1124 return contains_conditional_break_impl(s, /*inConditional=*/false);
1125}
1126
Ethan Nicholas5005a222018-08-24 13:06:27 -04001127// returns true if this statement definitely executes a break at the current level (we ignore
1128// nested loops and switches, since any breaks inside of them will merely break the loop / switch)
1129static bool contains_unconditional_break(Statement& s) {
1130 switch (s.fKind) {
1131 case Statement::kBlock_Kind:
John Stiles92219b42020-06-15 12:32:24 -04001132 for (const std::unique_ptr<Statement>& sub : static_cast<Block&>(s).fStatements) {
Ethan Nicholas5005a222018-08-24 13:06:27 -04001133 if (contains_unconditional_break(*sub)) {
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001134 return true;
1135 }
1136 }
1137 return false;
John Stiles92219b42020-06-15 12:32:24 -04001138
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001139 case Statement::kBreak_Kind:
1140 return true;
John Stiles92219b42020-06-15 12:32:24 -04001141
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001142 default:
1143 return false;
1144 }
1145}
1146
John Stiles92219b42020-06-15 12:32:24 -04001147static void move_all_but_break(std::unique_ptr<Statement>& stmt,
1148 std::vector<std::unique_ptr<Statement>>* target) {
Ethan Nicholas739e1ca2020-06-11 12:16:14 -04001149 switch (stmt->fKind) {
John Stiles92219b42020-06-15 12:32:24 -04001150 case Statement::kBlock_Kind: {
1151 // Recurse into the block.
1152 Block& block = static_cast<Block&>(*stmt);
1153
1154 std::vector<std::unique_ptr<Statement>> blockStmts;
1155 blockStmts.reserve(block.fStatements.size());
1156 for (std::unique_ptr<Statement>& statementInBlock : block.fStatements) {
1157 move_all_but_break(statementInBlock, &blockStmts);
Ethan Nicholas739e1ca2020-06-11 12:16:14 -04001158 }
John Stiles92219b42020-06-15 12:32:24 -04001159
1160 target->push_back(std::make_unique<Block>(block.fOffset, std::move(blockStmts),
1161 block.fSymbols, block.fIsScope));
Ethan Nicholas739e1ca2020-06-11 12:16:14 -04001162 break;
John Stiles92219b42020-06-15 12:32:24 -04001163 }
1164
Ethan Nicholas739e1ca2020-06-11 12:16:14 -04001165 case Statement::kBreak_Kind:
John Stiles92219b42020-06-15 12:32:24 -04001166 // Do not append a break to the target.
1167 break;
1168
Ethan Nicholas739e1ca2020-06-11 12:16:14 -04001169 default:
John Stiles92219b42020-06-15 12:32:24 -04001170 // Append normal statements to the target.
1171 target->push_back(std::move(stmt));
1172 break;
Ethan Nicholas739e1ca2020-06-11 12:16:14 -04001173 }
1174}
1175
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001176// Returns a block containing all of the statements that will be run if the given case matches
1177// (which, owing to the statements being owned by unique_ptrs, means the switch itself will be
1178// broken by this call and must then be discarded).
1179// Returns null (and leaves the switch unmodified) if no such simple reduction is possible, such as
1180// when break statements appear inside conditionals.
John Stiles92219b42020-06-15 12:32:24 -04001181static std::unique_ptr<Statement> block_for_case(SwitchStatement* switchStatement,
1182 SwitchCase* caseToCapture) {
1183 // We have to be careful to not move any of the pointers until after we're sure we're going to
1184 // succeed, so before we make any changes at all, we check the switch-cases to decide on a plan
1185 // of action. First, find the switch-case we are interested in.
1186 auto iter = switchStatement->fCases.begin();
1187 for (; iter != switchStatement->fCases.end(); ++iter) {
1188 if (iter->get() == caseToCapture) {
1189 break;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001190 }
John Stiles92219b42020-06-15 12:32:24 -04001191 }
1192
1193 // Next, walk forward through the rest of the switch. If we find a conditional break, we're
1194 // stuck and can't simplify at all. If we find an unconditional break, we have a range of
1195 // statements that we can use for simplification.
1196 auto startIter = iter;
1197 Statement* unconditionalBreakStmt = nullptr;
1198 for (; iter != switchStatement->fCases.end(); ++iter) {
1199 for (std::unique_ptr<Statement>& stmt : (*iter)->fStatements) {
1200 if (contains_conditional_break(*stmt)) {
1201 // We can't reduce switch-cases to a block when they have conditional breaks.
1202 return nullptr;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001203 }
John Stiles92219b42020-06-15 12:32:24 -04001204
1205 if (contains_unconditional_break(*stmt)) {
1206 // We found an unconditional break. We can use this block, but we need to strip
1207 // out the break statement.
1208 unconditionalBreakStmt = stmt.get();
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001209 break;
1210 }
1211 }
John Stiles92219b42020-06-15 12:32:24 -04001212
1213 if (unconditionalBreakStmt != nullptr) {
1214 break;
1215 }
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001216 }
John Stiles92219b42020-06-15 12:32:24 -04001217
1218 // We fell off the bottom of the switch or encountered a break. We know the range of statements
1219 // that we need to move over, and we know it's safe to do so.
1220 std::vector<std::unique_ptr<Statement>> caseStmts;
1221
1222 // We can move over most of the statements as-is.
1223 while (startIter != iter) {
1224 for (std::unique_ptr<Statement>& stmt : (*startIter)->fStatements) {
1225 caseStmts.push_back(std::move(stmt));
1226 }
1227 ++startIter;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001228 }
John Stiles92219b42020-06-15 12:32:24 -04001229
1230 // If we found an unconditional break at the end, we need to move what we can while avoiding
1231 // that break.
1232 if (unconditionalBreakStmt != nullptr) {
1233 for (std::unique_ptr<Statement>& stmt : (*startIter)->fStatements) {
1234 if (stmt.get() == unconditionalBreakStmt) {
1235 move_all_but_break(stmt, &caseStmts);
1236 unconditionalBreakStmt = nullptr;
1237 break;
1238 }
1239
1240 caseStmts.push_back(std::move(stmt));
1241 }
1242 }
1243
1244 SkASSERT(unconditionalBreakStmt == nullptr); // Verify that we fixed the unconditional break.
1245
1246 // Return our newly-synthesized block.
1247 return std::make_unique<Block>(/*offset=*/-1, std::move(caseStmts), switchStatement->fSymbols);
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001248}
1249
Ethan Nicholascb670962017-04-20 19:31:52 -04001250void Compiler::simplifyStatement(DefinitionMap& definitions,
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001251 BasicBlock& b,
1252 std::vector<BasicBlock::Node>::iterator* iter,
1253 std::unordered_set<const Variable*>* undefinedVariables,
1254 bool* outUpdated,
1255 bool* outNeedsRescan) {
Ethan Nicholascb670962017-04-20 19:31:52 -04001256 Statement* stmt = (*iter)->statement()->get();
1257 switch (stmt->fKind) {
Ethan Nicholas82a62d22017-11-07 14:42:10 +00001258 case Statement::kVarDeclaration_Kind: {
1259 const auto& varDecl = (VarDeclaration&) *stmt;
1260 if (varDecl.fVar->dead() &&
1261 (!varDecl.fValue ||
1262 !varDecl.fValue->hasSideEffects())) {
1263 if (varDecl.fValue) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001264 SkASSERT((*iter)->statement()->get() == stmt);
Ethan Nicholas82a62d22017-11-07 14:42:10 +00001265 if (!b.tryRemoveExpressionBefore(iter, varDecl.fValue.get())) {
1266 *outNeedsRescan = true;
Ethan Nicholascb670962017-04-20 19:31:52 -04001267 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001268 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001269 (*iter)->setStatement(std::unique_ptr<Statement>(new Nop()));
Ethan Nicholas82a62d22017-11-07 14:42:10 +00001270 *outUpdated = true;
Ethan Nicholascb670962017-04-20 19:31:52 -04001271 }
1272 break;
1273 }
1274 case Statement::kIf_Kind: {
1275 IfStatement& i = (IfStatement&) *stmt;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001276 if (i.fTest->fKind == Expression::kBoolLiteral_Kind) {
1277 // constant if, collapse down to a single branch
1278 if (((BoolLiteral&) *i.fTest).fValue) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001279 SkASSERT(i.fIfTrue);
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001280 (*iter)->setStatement(std::move(i.fIfTrue));
1281 } else {
1282 if (i.fIfFalse) {
1283 (*iter)->setStatement(std::move(i.fIfFalse));
1284 } else {
1285 (*iter)->setStatement(std::unique_ptr<Statement>(new Nop()));
1286 }
1287 }
1288 *outUpdated = true;
1289 *outNeedsRescan = true;
1290 break;
1291 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001292 if (i.fIfFalse && i.fIfFalse->isEmpty()) {
1293 // else block doesn't do anything, remove it
1294 i.fIfFalse.reset();
1295 *outUpdated = true;
1296 *outNeedsRescan = true;
1297 }
1298 if (!i.fIfFalse && i.fIfTrue->isEmpty()) {
1299 // if block doesn't do anything, no else block
1300 if (i.fTest->hasSideEffects()) {
1301 // test has side effects, keep it
1302 (*iter)->setStatement(std::unique_ptr<Statement>(
1303 new ExpressionStatement(std::move(i.fTest))));
1304 } else {
1305 // no if, no else, no test side effects, kill the whole if
1306 // statement
1307 (*iter)->setStatement(std::unique_ptr<Statement>(new Nop()));
1308 }
1309 *outUpdated = true;
1310 *outNeedsRescan = true;
1311 }
1312 break;
1313 }
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001314 case Statement::kSwitch_Kind: {
1315 SwitchStatement& s = (SwitchStatement&) *stmt;
Brian Osmanb6b95732020-06-30 11:44:27 -04001316 if (s.fValue->isCompileTimeConstant()) {
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001317 // switch is constant, replace it with the case that matches
1318 bool found = false;
1319 SwitchCase* defaultCase = nullptr;
1320 for (const auto& c : s.fCases) {
1321 if (!c->fValue) {
1322 defaultCase = c.get();
1323 continue;
1324 }
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001325 SkASSERT(c->fValue->fKind == s.fValue->fKind);
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001326 found = c->fValue->compareConstant(*fContext, *s.fValue);
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001327 if (found) {
1328 std::unique_ptr<Statement> newBlock = block_for_case(&s, c.get());
1329 if (newBlock) {
1330 (*iter)->setStatement(std::move(newBlock));
1331 break;
1332 } else {
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -04001333 if (s.fIsStatic && !(fFlags & kPermitInvalidStaticTests_Flag)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001334 this->error(s.fOffset,
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001335 "static switch contains non-static conditional break");
1336 s.fIsStatic = false;
1337 }
1338 return; // can't simplify
1339 }
1340 }
1341 }
1342 if (!found) {
1343 // no matching case. use default if it exists, or kill the whole thing
1344 if (defaultCase) {
1345 std::unique_ptr<Statement> newBlock = block_for_case(&s, defaultCase);
1346 if (newBlock) {
1347 (*iter)->setStatement(std::move(newBlock));
1348 } else {
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -04001349 if (s.fIsStatic && !(fFlags & kPermitInvalidStaticTests_Flag)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001350 this->error(s.fOffset,
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001351 "static switch contains non-static conditional break");
1352 s.fIsStatic = false;
1353 }
1354 return; // can't simplify
1355 }
1356 } else {
1357 (*iter)->setStatement(std::unique_ptr<Statement>(new Nop()));
1358 }
1359 }
1360 *outUpdated = true;
1361 *outNeedsRescan = true;
1362 }
1363 break;
1364 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001365 case Statement::kExpression_Kind: {
1366 ExpressionStatement& e = (ExpressionStatement&) *stmt;
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001367 SkASSERT((*iter)->statement()->get() == &e);
Ethan Nicholascb670962017-04-20 19:31:52 -04001368 if (!e.fExpression->hasSideEffects()) {
1369 // Expression statement with no side effects, kill it
1370 if (!b.tryRemoveExpressionBefore(iter, e.fExpression.get())) {
1371 *outNeedsRescan = true;
1372 }
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001373 SkASSERT((*iter)->statement()->get() == stmt);
Ethan Nicholascb670962017-04-20 19:31:52 -04001374 (*iter)->setStatement(std::unique_ptr<Statement>(new Nop()));
1375 *outUpdated = true;
1376 }
1377 break;
1378 }
1379 default:
1380 break;
1381 }
1382}
1383
1384void Compiler::scanCFG(FunctionDefinition& f) {
1385 CFG cfg = CFGGenerator().getCFG(f);
1386 this->computeDataFlow(&cfg);
ethannicholas22f939e2016-10-13 13:25:34 -07001387
1388 // check for unreachable code
1389 for (size_t i = 0; i < cfg.fBlocks.size(); i++) {
Mike Klein6ad99092016-10-26 10:35:22 -04001390 if (i != cfg.fStart && !cfg.fBlocks[i].fEntrances.size() &&
ethannicholas22f939e2016-10-13 13:25:34 -07001391 cfg.fBlocks[i].fNodes.size()) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001392 int offset;
Ethan Nicholas86a43402017-01-19 13:32:00 -05001393 switch (cfg.fBlocks[i].fNodes[0].fKind) {
1394 case BasicBlock::Node::kStatement_Kind:
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001395 offset = (*cfg.fBlocks[i].fNodes[0].statement())->fOffset;
Ethan Nicholas86a43402017-01-19 13:32:00 -05001396 break;
1397 case BasicBlock::Node::kExpression_Kind:
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001398 offset = (*cfg.fBlocks[i].fNodes[0].expression())->fOffset;
Ethan Nicholas70728ef2020-05-28 07:09:00 -04001399 if ((*cfg.fBlocks[i].fNodes[0].expression())->fKind ==
1400 Expression::kBoolLiteral_Kind) {
1401 // Function inlining can generate do { ... } while(false) loops which always
1402 // break, so the boolean condition is considered unreachable. Since not
1403 // being able to reach a literal is a non-issue in the first place, we
1404 // don't report an error in this case.
1405 continue;
1406 }
Ethan Nicholas86a43402017-01-19 13:32:00 -05001407 break;
1408 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001409 this->error(offset, String("unreachable"));
ethannicholas22f939e2016-10-13 13:25:34 -07001410 }
1411 }
1412 if (fErrorCount) {
1413 return;
1414 }
1415
Ethan Nicholascb670962017-04-20 19:31:52 -04001416 // check for dead code & undefined variables, perform constant propagation
1417 std::unordered_set<const Variable*> undefinedVariables;
1418 bool updated;
1419 bool needsRescan = false;
1420 do {
1421 if (needsRescan) {
1422 cfg = CFGGenerator().getCFG(f);
1423 this->computeDataFlow(&cfg);
1424 needsRescan = false;
Ethan Nicholas113628d2017-02-02 16:11:39 -05001425 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001426
1427 updated = false;
Ethan Nicholas1de14812020-06-19 15:32:49 -04001428 bool first = true;
Ethan Nicholascb670962017-04-20 19:31:52 -04001429 for (BasicBlock& b : cfg.fBlocks) {
Ethan Nicholas1de14812020-06-19 15:32:49 -04001430 if (!first && b.fEntrances.empty()) {
1431 // Block was reachable before optimization, but has since become unreachable. In
1432 // addition to being dead code, it's broken - since control flow can't reach it, no
1433 // prior variable definitions can reach it, and therefore variables might look to
1434 // have not been properly assigned. Kill it.
1435 for (BasicBlock::Node& node : b.fNodes) {
1436 if (node.fKind == BasicBlock::Node::kStatement_Kind &&
1437 (*node.statement())->fKind != Statement::kNop_Kind) {
1438 node.setStatement(std::unique_ptr<Statement>(new Nop()));
1439 }
1440 }
1441 continue;
1442 }
1443 first = false;
Ethan Nicholascb670962017-04-20 19:31:52 -04001444 DefinitionMap definitions = b.fBefore;
1445
1446 for (auto iter = b.fNodes.begin(); iter != b.fNodes.end() && !needsRescan; ++iter) {
1447 if (iter->fKind == BasicBlock::Node::kExpression_Kind) {
1448 this->simplifyExpression(definitions, b, &iter, &undefinedVariables, &updated,
1449 &needsRescan);
1450 } else {
1451 this->simplifyStatement(definitions, b, &iter, &undefinedVariables, &updated,
1452 &needsRescan);
1453 }
Ethan Nicholas4b330df2017-05-17 10:52:55 -04001454 if (needsRescan) {
1455 break;
1456 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001457 this->addDefinitions(*iter, &definitions);
1458 }
1459 }
1460 } while (updated);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001461 SkASSERT(!needsRescan);
ethannicholas22f939e2016-10-13 13:25:34 -07001462
Ethan Nicholas91a10532017-06-22 11:24:38 -04001463 // verify static ifs & switches, clean up dead variable decls
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001464 for (BasicBlock& b : cfg.fBlocks) {
1465 DefinitionMap definitions = b.fBefore;
1466
Ethan Nicholas91a10532017-06-22 11:24:38 -04001467 for (auto iter = b.fNodes.begin(); iter != b.fNodes.end() && !needsRescan;) {
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001468 if (iter->fKind == BasicBlock::Node::kStatement_Kind) {
1469 const Statement& s = **iter->statement();
1470 switch (s.fKind) {
1471 case Statement::kIf_Kind:
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -04001472 if (((const IfStatement&) s).fIsStatic &&
1473 !(fFlags & kPermitInvalidStaticTests_Flag)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001474 this->error(s.fOffset, "static if has non-static test");
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001475 }
Ethan Nicholas91a10532017-06-22 11:24:38 -04001476 ++iter;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001477 break;
1478 case Statement::kSwitch_Kind:
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -04001479 if (((const SwitchStatement&) s).fIsStatic &&
1480 !(fFlags & kPermitInvalidStaticTests_Flag)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001481 this->error(s.fOffset, "static switch has non-static test");
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001482 }
Ethan Nicholas91a10532017-06-22 11:24:38 -04001483 ++iter;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001484 break;
Ethan Nicholas82a62d22017-11-07 14:42:10 +00001485 case Statement::kVarDeclarations_Kind: {
1486 VarDeclarations& decls = *((VarDeclarationsStatement&) s).fDeclaration;
1487 for (auto varIter = decls.fVars.begin(); varIter != decls.fVars.end();) {
1488 if ((*varIter)->fKind == Statement::kNop_Kind) {
1489 varIter = decls.fVars.erase(varIter);
1490 } else {
1491 ++varIter;
1492 }
1493 }
1494 if (!decls.fVars.size()) {
1495 iter = b.fNodes.erase(iter);
1496 } else {
1497 ++iter;
1498 }
1499 break;
1500 }
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001501 default:
Ethan Nicholas91a10532017-06-22 11:24:38 -04001502 ++iter;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001503 break;
1504 }
Ethan Nicholas91a10532017-06-22 11:24:38 -04001505 } else {
1506 ++iter;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001507 }
1508 }
1509 }
1510
ethannicholas22f939e2016-10-13 13:25:34 -07001511 // check for missing return
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001512 if (f.fDeclaration.fReturnType != *fContext->fVoid_Type) {
ethannicholas22f939e2016-10-13 13:25:34 -07001513 if (cfg.fBlocks[cfg.fExit].fEntrances.size()) {
Ethan Nicholasdb80f692019-11-22 14:06:12 -05001514 this->error(f.fOffset, String("function '" + String(f.fDeclaration.fName) +
1515 "' can exit without returning a value"));
ethannicholas22f939e2016-10-13 13:25:34 -07001516 }
1517 }
1518}
1519
Ethan Nicholas91164d12019-05-15 15:29:54 -04001520void Compiler::registerExternalValue(ExternalValue* value) {
1521 fIRGenerator->fRootSymbolTable->addWithoutOwnership(value->fName, value);
1522}
1523
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001524const Symbol* Compiler::takeOwnership(std::unique_ptr<const Symbol> symbol) {
John Stiles3ae071e2020-08-05 15:29:29 -04001525 return fIRGenerator->fRootSymbolTable->takeOwnershipOfSymbol(std::move(symbol));
Ethan Nicholas91164d12019-05-15 15:29:54 -04001526}
1527
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001528std::unique_ptr<Program> Compiler::convertProgram(Program::Kind kind, String text,
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001529 const Program::Settings& settings) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001530 fErrorText = "";
1531 fErrorCount = 0;
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001532 std::vector<std::unique_ptr<ProgramElement>>* inherited;
ethannicholasd598f792016-07-25 10:08:54 -07001533 std::vector<std::unique_ptr<ProgramElement>> elements;
ethannicholasb3058bd2016-07-01 08:22:01 -07001534 switch (kind) {
1535 case Program::kVertex_Kind:
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001536 inherited = &fVertexInclude;
1537 fIRGenerator->fSymbolTable = fVertexSymbolTable;
Ethan Nicholasdb80f692019-11-22 14:06:12 -05001538 fIRGenerator->fIntrinsics = &fGPUIntrinsics;
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001539 fIRGenerator->start(&settings, inherited);
ethannicholasb3058bd2016-07-01 08:22:01 -07001540 break;
1541 case Program::kFragment_Kind:
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001542 inherited = &fFragmentInclude;
1543 fIRGenerator->fSymbolTable = fFragmentSymbolTable;
Ethan Nicholasdb80f692019-11-22 14:06:12 -05001544 fIRGenerator->fIntrinsics = &fGPUIntrinsics;
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001545 fIRGenerator->start(&settings, inherited);
ethannicholasb3058bd2016-07-01 08:22:01 -07001546 break;
Ethan Nicholas52cad152017-02-16 16:37:32 -05001547 case Program::kGeometry_Kind:
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001548 this->loadGeometryIntrinsics();
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001549 inherited = &fGeometryInclude;
1550 fIRGenerator->fSymbolTable = fGeometrySymbolTable;
Ethan Nicholasdb80f692019-11-22 14:06:12 -05001551 fIRGenerator->fIntrinsics = &fGPUIntrinsics;
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001552 fIRGenerator->start(&settings, inherited);
Ethan Nicholas52cad152017-02-16 16:37:32 -05001553 break;
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -04001554 case Program::kFragmentProcessor_Kind: {
Brian Osmandd496172020-08-08 08:17:18 -04001555#if !SKSL_STANDALONE
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001556 {
1557 Rehydrator rehydrator(fContext.get(), fGpuSymbolTable, this,
1558 SKSL_INCLUDE_sksl_fp,
1559 SKSL_INCLUDE_sksl_fp_LENGTH);
1560 fFPSymbolTable = rehydrator.symbolTable();
1561 fFPInclude = rehydrator.elements();
1562 }
1563 inherited = &fFPInclude;
1564 fIRGenerator->fSymbolTable = fFPSymbolTable;
1565 fIRGenerator->fIntrinsics = &fGPUIntrinsics;
1566 fIRGenerator->start(&settings, inherited);
1567 break;
1568#else
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001569 inherited = nullptr;
Ethan Nicholas8da1e652019-05-24 11:01:59 -04001570 fIRGenerator->fSymbolTable = fGpuSymbolTable;
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -04001571 fIRGenerator->start(&settings, /*inherited=*/nullptr, /*builtin=*/true);
Ethan Nicholasdb80f692019-11-22 14:06:12 -05001572 fIRGenerator->fIntrinsics = &fGPUIntrinsics;
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -04001573 std::ifstream in(SKSL_FP_INCLUDE);
1574 std::string stdText{std::istreambuf_iterator<char>(in),
1575 std::istreambuf_iterator<char>()};
1576 if (in.rdstate()) {
1577 printf("error reading %s\n", SKSL_FP_INCLUDE);
1578 abort();
1579 }
1580 const String* source = fGpuSymbolTable->takeOwnershipOfString(
1581 std::make_unique<String>(stdText.c_str()));
1582 fIRGenerator->convertProgram(kind, source->c_str(), source->length(), &elements);
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001583 fIRGenerator->fIsBuiltinCode = false;
Ethan Nicholas762466e2017-06-29 10:03:38 -04001584 break;
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001585#endif
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -04001586 }
Ethan Nicholas8da1e652019-05-24 11:01:59 -04001587 case Program::kPipelineStage_Kind:
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001588 this->loadPipelineIntrinsics();
Ethan Nicholas8da1e652019-05-24 11:01:59 -04001589 inherited = &fPipelineInclude;
1590 fIRGenerator->fSymbolTable = fPipelineSymbolTable;
Ethan Nicholasdb80f692019-11-22 14:06:12 -05001591 fIRGenerator->fIntrinsics = &fGPUIntrinsics;
Ethan Nicholas8da1e652019-05-24 11:01:59 -04001592 fIRGenerator->start(&settings, inherited);
1593 break;
Ethan Nicholas746035a2019-04-23 13:31:09 -04001594 case Program::kGeneric_Kind:
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001595 this->loadInterpreterIntrinsics();
Ethan Nicholas8da1e652019-05-24 11:01:59 -04001596 inherited = &fInterpreterInclude;
1597 fIRGenerator->fSymbolTable = fInterpreterSymbolTable;
Ethan Nicholasdb80f692019-11-22 14:06:12 -05001598 fIRGenerator->fIntrinsics = &fInterpreterIntrinsics;
Ethan Nicholas8da1e652019-05-24 11:01:59 -04001599 fIRGenerator->start(&settings, inherited);
Ethan Nicholas0d997662019-04-08 09:46:01 -04001600 break;
ethannicholasb3058bd2016-07-01 08:22:01 -07001601 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001602 std::unique_ptr<String> textPtr(new String(std::move(text)));
1603 fSource = textPtr.get();
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001604 fIRGenerator->convertProgram(kind, textPtr->c_str(), textPtr->size(), &elements);
John Stilesfbd050b2020-08-03 13:21:46 -04001605 auto result = std::make_unique<Program>(kind,
1606 std::move(textPtr),
1607 settings,
1608 fContext,
1609 inherited,
1610 std::move(elements),
1611 fIRGenerator->fSymbolTable,
1612 fIRGenerator->fInputs);
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001613 if (fErrorCount) {
1614 return nullptr;
1615 }
1616 return result;
1617}
1618
Ethan Nicholas00543112018-07-31 09:44:36 -04001619bool Compiler::optimize(Program& program) {
1620 SkASSERT(!fErrorCount);
1621 if (!program.fIsOptimized) {
1622 program.fIsOptimized = true;
1623 fIRGenerator->fKind = program.fKind;
1624 fIRGenerator->fSettings = &program.fSettings;
1625 for (auto& element : program) {
1626 if (element.fKind == ProgramElement::kFunction_Kind) {
1627 this->scanCFG((FunctionDefinition&) element);
1628 }
1629 }
Ethan Nicholase8ad02c2020-06-03 16:58:20 -04001630 // we wait until after analysis to remove dead functions so that we still report errors
1631 // even in unused code
1632 if (program.fSettings.fRemoveDeadFunctions) {
1633 for (auto iter = program.fElements.begin(); iter != program.fElements.end(); ) {
1634 if ((*iter)->fKind == ProgramElement::kFunction_Kind) {
1635 const FunctionDefinition& f = (const FunctionDefinition&) **iter;
1636 if (!f.fDeclaration.fCallCount && f.fDeclaration.fName != "main") {
1637 iter = program.fElements.erase(iter);
1638 continue;
1639 }
1640 }
1641 ++iter;
1642 }
1643 }
Ethan Nicholas0dc80872019-02-08 15:46:24 -05001644 if (program.fKind != Program::kFragmentProcessor_Kind) {
1645 for (auto iter = program.fElements.begin(); iter != program.fElements.end();) {
1646 if ((*iter)->fKind == ProgramElement::kVar_Kind) {
1647 VarDeclarations& vars = (VarDeclarations&) **iter;
1648 for (auto varIter = vars.fVars.begin(); varIter != vars.fVars.end();) {
1649 const Variable& var = *((VarDeclaration&) **varIter).fVar;
1650 if (var.dead()) {
1651 varIter = vars.fVars.erase(varIter);
1652 } else {
1653 ++varIter;
1654 }
1655 }
1656 if (vars.fVars.size() == 0) {
1657 iter = program.fElements.erase(iter);
1658 continue;
1659 }
1660 }
1661 ++iter;
1662 }
1663 }
Ethan Nicholas00543112018-07-31 09:44:36 -04001664 }
1665 return fErrorCount == 0;
1666}
1667
Brian Osmanfb32ddf2019-06-18 10:14:20 -04001668#if defined(SKSL_STANDALONE) || SK_SUPPORT_GPU
1669
Ethan Nicholas00543112018-07-31 09:44:36 -04001670bool Compiler::toSPIRV(Program& program, OutputStream& out) {
1671 if (!this->optimize(program)) {
1672 return false;
1673 }
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001674#ifdef SK_ENABLE_SPIRV_VALIDATION
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001675 StringStream buffer;
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001676 fSource = program.fSource.get();
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001677 SPIRVCodeGenerator cg(fContext.get(), &program, this, &buffer);
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001678 bool result = cg.generateCode();
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001679 fSource = nullptr;
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001680 if (result) {
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001681 spvtools::SpirvTools tools(SPV_ENV_VULKAN_1_0);
Ethan Nicholas762466e2017-06-29 10:03:38 -04001682 const String& data = buffer.str();
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001683 SkASSERT(0 == data.size() % 4);
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001684 auto dumpmsg = [](spv_message_level_t, const char*, const spv_position_t&, const char* m) {
1685 SkDebugf("SPIR-V validation error: %s\n", m);
1686 };
1687 tools.SetMessageConsumer(dumpmsg);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001688 // Verify that the SPIR-V we produced is valid. If this SkASSERT fails, check the logs prior
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001689 // to the failure to see the validation errors.
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001690 SkAssertResult(tools.Validate((const uint32_t*) data.c_str(), data.size() / 4));
Ethan Nicholas762466e2017-06-29 10:03:38 -04001691 out.write(data.c_str(), data.size());
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001692 }
1693#else
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001694 fSource = program.fSource.get();
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001695 SPIRVCodeGenerator cg(fContext.get(), &program, this, &out);
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001696 bool result = cg.generateCode();
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001697 fSource = nullptr;
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001698#endif
Ethan Nicholasce33f102016-12-09 17:22:59 -05001699 return result;
1700}
1701
Ethan Nicholas00543112018-07-31 09:44:36 -04001702bool Compiler::toSPIRV(Program& program, String* out) {
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001703 StringStream buffer;
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001704 bool result = this->toSPIRV(program, buffer);
1705 if (result) {
Ethan Nicholas762466e2017-06-29 10:03:38 -04001706 *out = buffer.str();
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001707 }
1708 return result;
1709}
1710
Ethan Nicholas00543112018-07-31 09:44:36 -04001711bool Compiler::toGLSL(Program& program, OutputStream& out) {
1712 if (!this->optimize(program)) {
1713 return false;
1714 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001715 fSource = program.fSource.get();
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001716 GLSLCodeGenerator cg(fContext.get(), &program, this, &out);
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001717 bool result = cg.generateCode();
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001718 fSource = nullptr;
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001719 return result;
1720}
1721
Ethan Nicholas00543112018-07-31 09:44:36 -04001722bool Compiler::toGLSL(Program& program, String* out) {
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001723 StringStream buffer;
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001724 bool result = this->toGLSL(program, buffer);
1725 if (result) {
Ethan Nicholas762466e2017-06-29 10:03:38 -04001726 *out = buffer.str();
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001727 }
1728 return result;
1729}
1730
Brian Osmanc0243912020-02-19 15:35:26 -05001731bool Compiler::toHLSL(Program& program, String* out) {
1732 String spirv;
1733 if (!this->toSPIRV(program, &spirv)) {
1734 return false;
1735 }
1736
1737 return SPIRVtoHLSL(spirv, out);
1738}
1739
Ethan Nicholas00543112018-07-31 09:44:36 -04001740bool Compiler::toMetal(Program& program, OutputStream& out) {
1741 if (!this->optimize(program)) {
1742 return false;
1743 }
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001744 MetalCodeGenerator cg(fContext.get(), &program, this, &out);
Ethan Nicholascc305772017-10-13 16:17:45 -04001745 bool result = cg.generateCode();
Ethan Nicholascc305772017-10-13 16:17:45 -04001746 return result;
1747}
1748
Ethan Nicholas00543112018-07-31 09:44:36 -04001749bool Compiler::toMetal(Program& program, String* out) {
1750 if (!this->optimize(program)) {
1751 return false;
1752 }
Timothy Liangb8eeb802018-07-23 16:46:16 -04001753 StringStream buffer;
1754 bool result = this->toMetal(program, buffer);
1755 if (result) {
1756 *out = buffer.str();
1757 }
1758 return result;
1759}
1760
Ethan Nicholas00543112018-07-31 09:44:36 -04001761bool Compiler::toCPP(Program& program, String name, OutputStream& out) {
1762 if (!this->optimize(program)) {
1763 return false;
1764 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001765 fSource = program.fSource.get();
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001766 CPPCodeGenerator cg(fContext.get(), &program, this, name, &out);
Ethan Nicholas762466e2017-06-29 10:03:38 -04001767 bool result = cg.generateCode();
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001768 fSource = nullptr;
Ethan Nicholas762466e2017-06-29 10:03:38 -04001769 return result;
1770}
1771
Ethan Nicholas00543112018-07-31 09:44:36 -04001772bool Compiler::toH(Program& program, String name, OutputStream& out) {
1773 if (!this->optimize(program)) {
1774 return false;
1775 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001776 fSource = program.fSource.get();
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001777 HCodeGenerator cg(fContext.get(), &program, this, name, &out);
Ethan Nicholas762466e2017-06-29 10:03:38 -04001778 bool result = cg.generateCode();
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001779 fSource = nullptr;
Ethan Nicholas00543112018-07-31 09:44:36 -04001780 return result;
1781}
1782
Brian Osman2e29ab52019-09-20 12:19:11 -04001783#endif
1784
1785#if !defined(SKSL_STANDALONE) && SK_SUPPORT_GPU
Brian Osmana4b91692020-08-10 14:26:16 -04001786bool Compiler::toPipelineStage(Program& program, PipelineStageArgs* outArgs) {
1787 if (!this->optimize(program)) {
1788 return false;
1789 }
Ethan Nicholas00543112018-07-31 09:44:36 -04001790 fSource = program.fSource.get();
1791 StringStream buffer;
Brian Osman300fe1d2020-01-23 15:42:43 -05001792 PipelineStageCodeGenerator cg(fContext.get(), &program, this, &buffer, outArgs);
Ethan Nicholas00543112018-07-31 09:44:36 -04001793 bool result = cg.generateCode();
1794 fSource = nullptr;
1795 if (result) {
Brian Osman107c6662019-12-30 15:02:30 -05001796 outArgs->fCode = buffer.str();
Ethan Nicholas00543112018-07-31 09:44:36 -04001797 }
Ethan Nicholas762466e2017-06-29 10:03:38 -04001798 return result;
1799}
Brian Osmanfb32ddf2019-06-18 10:14:20 -04001800#endif
1801
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001802std::unique_ptr<ByteCode> Compiler::toByteCode(Program& program) {
Brian Osman489cf882019-07-09 10:48:28 -04001803#if defined(SK_ENABLE_SKSL_INTERPRETER)
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001804 if (!this->optimize(program)) {
1805 return nullptr;
1806 }
Brian Osman808f0212020-01-21 15:36:47 -05001807 fSource = program.fSource.get();
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001808 std::unique_ptr<ByteCode> result(new ByteCode());
Brian Osmanb08cc022020-04-02 11:38:40 -04001809 ByteCodeGenerator cg(fContext.get(), &program, this, result.get());
1810 bool success = cg.generateCode();
1811 fSource = nullptr;
1812 if (success) {
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001813 return result;
1814 }
Brian Osman489cf882019-07-09 10:48:28 -04001815#else
1816 ABORT("ByteCode interpreter not enabled");
1817#endif
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001818 return nullptr;
1819}
1820
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001821const char* Compiler::OperatorName(Token::Kind kind) {
1822 switch (kind) {
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001823 case Token::Kind::TK_PLUS: return "+";
1824 case Token::Kind::TK_MINUS: return "-";
1825 case Token::Kind::TK_STAR: return "*";
1826 case Token::Kind::TK_SLASH: return "/";
1827 case Token::Kind::TK_PERCENT: return "%";
1828 case Token::Kind::TK_SHL: return "<<";
1829 case Token::Kind::TK_SHR: return ">>";
1830 case Token::Kind::TK_LOGICALNOT: return "!";
1831 case Token::Kind::TK_LOGICALAND: return "&&";
1832 case Token::Kind::TK_LOGICALOR: return "||";
1833 case Token::Kind::TK_LOGICALXOR: return "^^";
1834 case Token::Kind::TK_BITWISENOT: return "~";
1835 case Token::Kind::TK_BITWISEAND: return "&";
1836 case Token::Kind::TK_BITWISEOR: return "|";
1837 case Token::Kind::TK_BITWISEXOR: return "^";
1838 case Token::Kind::TK_EQ: return "=";
1839 case Token::Kind::TK_EQEQ: return "==";
1840 case Token::Kind::TK_NEQ: return "!=";
1841 case Token::Kind::TK_LT: return "<";
1842 case Token::Kind::TK_GT: return ">";
1843 case Token::Kind::TK_LTEQ: return "<=";
1844 case Token::Kind::TK_GTEQ: return ">=";
1845 case Token::Kind::TK_PLUSEQ: return "+=";
1846 case Token::Kind::TK_MINUSEQ: return "-=";
1847 case Token::Kind::TK_STAREQ: return "*=";
1848 case Token::Kind::TK_SLASHEQ: return "/=";
1849 case Token::Kind::TK_PERCENTEQ: return "%=";
1850 case Token::Kind::TK_SHLEQ: return "<<=";
1851 case Token::Kind::TK_SHREQ: return ">>=";
1852 case Token::Kind::TK_LOGICALANDEQ: return "&&=";
1853 case Token::Kind::TK_LOGICALOREQ: return "||=";
1854 case Token::Kind::TK_LOGICALXOREQ: return "^^=";
1855 case Token::Kind::TK_BITWISEANDEQ: return "&=";
1856 case Token::Kind::TK_BITWISEOREQ: return "|=";
1857 case Token::Kind::TK_BITWISEXOREQ: return "^=";
1858 case Token::Kind::TK_PLUSPLUS: return "++";
1859 case Token::Kind::TK_MINUSMINUS: return "--";
1860 case Token::Kind::TK_COMMA: return ",";
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001861 default:
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001862 ABORT("unsupported operator: %d\n", (int) kind);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001863 }
1864}
1865
1866
1867bool Compiler::IsAssignment(Token::Kind op) {
1868 switch (op) {
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001869 case Token::Kind::TK_EQ: // fall through
1870 case Token::Kind::TK_PLUSEQ: // fall through
1871 case Token::Kind::TK_MINUSEQ: // fall through
1872 case Token::Kind::TK_STAREQ: // fall through
1873 case Token::Kind::TK_SLASHEQ: // fall through
1874 case Token::Kind::TK_PERCENTEQ: // fall through
1875 case Token::Kind::TK_SHLEQ: // fall through
1876 case Token::Kind::TK_SHREQ: // fall through
1877 case Token::Kind::TK_BITWISEOREQ: // fall through
1878 case Token::Kind::TK_BITWISEXOREQ: // fall through
1879 case Token::Kind::TK_BITWISEANDEQ: // fall through
1880 case Token::Kind::TK_LOGICALOREQ: // fall through
1881 case Token::Kind::TK_LOGICALXOREQ: // fall through
1882 case Token::Kind::TK_LOGICALANDEQ:
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001883 return true;
1884 default:
1885 return false;
1886 }
1887}
1888
1889Position Compiler::position(int offset) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001890 SkASSERT(fSource);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001891 int line = 1;
1892 int column = 1;
1893 for (int i = 0; i < offset; i++) {
1894 if ((*fSource)[i] == '\n') {
1895 ++line;
1896 column = 1;
1897 }
1898 else {
1899 ++column;
1900 }
1901 }
1902 return Position(line, column);
1903}
1904
1905void Compiler::error(int offset, String msg) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001906 fErrorCount++;
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001907 Position pos = this->position(offset);
1908 fErrorText += "error: " + to_string(pos.fLine) + ": " + msg.c_str() + "\n";
ethannicholasb3058bd2016-07-01 08:22:01 -07001909}
1910
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001911String Compiler::errorText() {
Ethan Nicholas00543112018-07-31 09:44:36 -04001912 this->writeErrorCount();
1913 fErrorCount = 0;
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001914 String result = fErrorText;
ethannicholasb3058bd2016-07-01 08:22:01 -07001915 return result;
1916}
1917
1918void Compiler::writeErrorCount() {
1919 if (fErrorCount) {
1920 fErrorText += to_string(fErrorCount) + " error";
1921 if (fErrorCount > 1) {
1922 fErrorText += "s";
1923 }
1924 fErrorText += "\n";
1925 }
1926}
1927
John Stilesa6841be2020-08-06 14:11:56 -04001928} // namespace SkSL