blob: 5342d69272a4c0b5a5d2136a4549367ea1969e69 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "src/sksl/SkSLByteCodeGenerator.h"
11#include "src/sksl/SkSLCFGGenerator.h"
12#include "src/sksl/SkSLCPPCodeGenerator.h"
13#include "src/sksl/SkSLGLSLCodeGenerator.h"
14#include "src/sksl/SkSLHCodeGenerator.h"
15#include "src/sksl/SkSLIRGenerator.h"
16#include "src/sksl/SkSLMetalCodeGenerator.h"
17#include "src/sksl/SkSLPipelineStageCodeGenerator.h"
Ethan Nicholasc18bb512020-07-28 14:46:53 -040018#include "src/sksl/SkSLRehydrator.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "src/sksl/SkSLSPIRVCodeGenerator.h"
Brian Osmanc0243912020-02-19 15:35:26 -050020#include "src/sksl/SkSLSPIRVtoHLSL.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "src/sksl/ir/SkSLEnum.h"
22#include "src/sksl/ir/SkSLExpression.h"
23#include "src/sksl/ir/SkSLExpressionStatement.h"
24#include "src/sksl/ir/SkSLFunctionCall.h"
25#include "src/sksl/ir/SkSLIntLiteral.h"
26#include "src/sksl/ir/SkSLModifiersDeclaration.h"
27#include "src/sksl/ir/SkSLNop.h"
28#include "src/sksl/ir/SkSLSymbolTable.h"
29#include "src/sksl/ir/SkSLTernaryExpression.h"
30#include "src/sksl/ir/SkSLUnresolvedFunction.h"
31#include "src/sksl/ir/SkSLVarDeclarations.h"
ethannicholasb3058bd2016-07-01 08:22:01 -070032
Ethan Nicholasa11035b2019-11-26 16:27:47 -050033#if !defined(SKSL_STANDALONE) & SK_SUPPORT_GPU
34#include "include/gpu/GrContextOptions.h"
35#include "src/gpu/GrShaderCaps.h"
36#endif
37
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -040038#ifdef SK_ENABLE_SPIRV_VALIDATION
39#include "spirv-tools/libspirv.hpp"
40#endif
41
Ethan Nicholasc18bb512020-07-28 14:46:53 -040042// If true, we use a compact binary IR representation of the core include files; otherwise we parse
43// the actual source code for the include files at runtime. The main reason you would need to change
44// this is to make format changes easier: set it to 0, change the encoder and decoder as needed,
45// build Skia to regenerate the encoded files, then set this back to 1 to actually use the
46// newly-generated files.
47#define REHYDRATE 1
ethannicholasb3058bd2016-07-01 08:22:01 -070048
Ethan Nicholasc18bb512020-07-28 14:46:53 -040049#if REHYDRATE
50
51#include "src/sksl/generated/sksl_fp.dehydrated.sksl"
52#include "src/sksl/generated/sksl_frag.dehydrated.sksl"
53#include "src/sksl/generated/sksl_geom.dehydrated.sksl"
54#include "src/sksl/generated/sksl_gpu.dehydrated.sksl"
55#include "src/sksl/generated/sksl_interp.dehydrated.sksl"
56#include "src/sksl/generated/sksl_pipeline.dehydrated.sksl"
57#include "src/sksl/generated/sksl_vert.dehydrated.sksl"
58
59#else
60
61#warning SkSL rehydrator is disabled
Ethan Nicholas79707652017-11-16 11:20:11 -050062
Ethan Nicholas8da1e652019-05-24 11:01:59 -040063static const char* SKSL_GPU_INCLUDE =
Ethan Nicholasc18bb512020-07-28 14:46:53 -040064#include "src/sksl/generated/sksl_gpu.c.inc"
Ethan Nicholas8da1e652019-05-24 11:01:59 -040065static const char* SKSL_INTERP_INCLUDE =
Ethan Nicholasc18bb512020-07-28 14:46:53 -040066#include "src/sksl/generated/sksl_interp.c.inc"
ethannicholas5961bc92016-10-12 06:39:56 -070067static const char* SKSL_VERT_INCLUDE =
Ethan Nicholasc18bb512020-07-28 14:46:53 -040068#include "src/sksl/generated/sksl_vert.c.inc"
ethannicholas5961bc92016-10-12 06:39:56 -070069static const char* SKSL_FRAG_INCLUDE =
Ethan Nicholasc18bb512020-07-28 14:46:53 -040070#include "src/sksl/generated/sksl_frag.c.inc"
Ethan Nicholas52cad152017-02-16 16:37:32 -050071static const char* SKSL_GEOM_INCLUDE =
Ethan Nicholasc18bb512020-07-28 14:46:53 -040072#include "src/sksl/generated/sksl_geom.c.inc"
Ethan Nicholas762466e2017-06-29 10:03:38 -040073static const char* SKSL_FP_INCLUDE =
Ethan Nicholasc18bb512020-07-28 14:46:53 -040074#include "src/sksl/generated/sksl_fp.c.inc"
Ethan Nicholas8da1e652019-05-24 11:01:59 -040075static const char* SKSL_PIPELINE_INCLUDE =
Ethan Nicholasc18bb512020-07-28 14:46:53 -040076#include "src/sksl/generated/sksl_pipeline.c.inc"
77
78#endif
Ethan Nicholas0d997662019-04-08 09:46:01 -040079
ethannicholasb3058bd2016-07-01 08:22:01 -070080namespace SkSL {
81
Ethan Nicholasdb80f692019-11-22 14:06:12 -050082static void grab_intrinsics(std::vector<std::unique_ptr<ProgramElement>>* src,
Brian Osman08f986d2020-05-13 17:06:46 -040083 std::map<String, std::pair<std::unique_ptr<ProgramElement>, bool>>* target) {
84 for (auto iter = src->begin(); iter != src->end(); ) {
85 std::unique_ptr<ProgramElement>& element = *iter;
Ethan Nicholasdb80f692019-11-22 14:06:12 -050086 switch (element->fKind) {
87 case ProgramElement::kFunction_Kind: {
88 FunctionDefinition& f = (FunctionDefinition&) *element;
Brian Osman08f986d2020-05-13 17:06:46 -040089 SkASSERT(f.fDeclaration.fBuiltin);
Ethan Nicholasc18bb512020-07-28 14:46:53 -040090 String key = f.fDeclaration.description();
Brian Osman08f986d2020-05-13 17:06:46 -040091 SkASSERT(target->find(key) == target->end());
92 (*target)[key] = std::make_pair(std::move(element), false);
93 iter = src->erase(iter);
Ethan Nicholasdb80f692019-11-22 14:06:12 -050094 break;
95 }
96 case ProgramElement::kEnum_Kind: {
97 Enum& e = (Enum&) *element;
98 StringFragment name = e.fTypeName;
99 SkASSERT(target->find(name) == target->end());
100 (*target)[name] = std::make_pair(std::move(element), false);
Brian Osman08f986d2020-05-13 17:06:46 -0400101 iter = src->erase(iter);
Ethan Nicholasdb80f692019-11-22 14:06:12 -0500102 break;
103 }
104 default:
105 printf("unsupported include file element\n");
106 SkASSERT(false);
107 }
108 }
109}
110
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -0400111Compiler::Compiler(Flags flags)
112: fFlags(flags)
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400113, fContext(new Context())
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -0400114, fErrorCount(0) {
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400115 auto symbols = std::shared_ptr<SymbolTable>(new SymbolTable(this));
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400116 fIRGenerator = new IRGenerator(fContext.get(), symbols, *this);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400117 #define ADD_TYPE(t) symbols->addWithoutOwnership(fContext->f ## t ## _Type->fName, \
118 fContext->f ## t ## _Type.get())
ethannicholasb3058bd2016-07-01 08:22:01 -0700119 ADD_TYPE(Void);
120 ADD_TYPE(Float);
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400121 ADD_TYPE(Float2);
122 ADD_TYPE(Float3);
123 ADD_TYPE(Float4);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400124 ADD_TYPE(Half);
125 ADD_TYPE(Half2);
126 ADD_TYPE(Half3);
127 ADD_TYPE(Half4);
ethannicholasb3058bd2016-07-01 08:22:01 -0700128 ADD_TYPE(Int);
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400129 ADD_TYPE(Int2);
130 ADD_TYPE(Int3);
131 ADD_TYPE(Int4);
ethannicholasb3058bd2016-07-01 08:22:01 -0700132 ADD_TYPE(UInt);
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400133 ADD_TYPE(UInt2);
134 ADD_TYPE(UInt3);
135 ADD_TYPE(UInt4);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400136 ADD_TYPE(Short);
137 ADD_TYPE(Short2);
138 ADD_TYPE(Short3);
139 ADD_TYPE(Short4);
140 ADD_TYPE(UShort);
141 ADD_TYPE(UShort2);
142 ADD_TYPE(UShort3);
143 ADD_TYPE(UShort4);
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400144 ADD_TYPE(Byte);
145 ADD_TYPE(Byte2);
146 ADD_TYPE(Byte3);
147 ADD_TYPE(Byte4);
148 ADD_TYPE(UByte);
149 ADD_TYPE(UByte2);
150 ADD_TYPE(UByte3);
151 ADD_TYPE(UByte4);
ethannicholasb3058bd2016-07-01 08:22:01 -0700152 ADD_TYPE(Bool);
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400153 ADD_TYPE(Bool2);
154 ADD_TYPE(Bool3);
155 ADD_TYPE(Bool4);
156 ADD_TYPE(Float2x2);
157 ADD_TYPE(Float2x3);
158 ADD_TYPE(Float2x4);
159 ADD_TYPE(Float3x2);
160 ADD_TYPE(Float3x3);
161 ADD_TYPE(Float3x4);
162 ADD_TYPE(Float4x2);
163 ADD_TYPE(Float4x3);
164 ADD_TYPE(Float4x4);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400165 ADD_TYPE(Half2x2);
166 ADD_TYPE(Half2x3);
167 ADD_TYPE(Half2x4);
168 ADD_TYPE(Half3x2);
169 ADD_TYPE(Half3x3);
170 ADD_TYPE(Half3x4);
171 ADD_TYPE(Half4x2);
172 ADD_TYPE(Half4x3);
173 ADD_TYPE(Half4x4);
ethannicholasb3058bd2016-07-01 08:22:01 -0700174 ADD_TYPE(GenType);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400175 ADD_TYPE(GenHType);
ethannicholasb3058bd2016-07-01 08:22:01 -0700176 ADD_TYPE(GenIType);
177 ADD_TYPE(GenUType);
178 ADD_TYPE(GenBType);
179 ADD_TYPE(Mat);
180 ADD_TYPE(Vec);
181 ADD_TYPE(GVec);
182 ADD_TYPE(GVec2);
183 ADD_TYPE(GVec3);
184 ADD_TYPE(GVec4);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400185 ADD_TYPE(HVec);
ethannicholasb3058bd2016-07-01 08:22:01 -0700186 ADD_TYPE(IVec);
187 ADD_TYPE(UVec);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400188 ADD_TYPE(SVec);
189 ADD_TYPE(USVec);
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400190 ADD_TYPE(ByteVec);
191 ADD_TYPE(UByteVec);
ethannicholasb3058bd2016-07-01 08:22:01 -0700192 ADD_TYPE(BVec);
193
194 ADD_TYPE(Sampler1D);
195 ADD_TYPE(Sampler2D);
196 ADD_TYPE(Sampler3D);
ethannicholas5961bc92016-10-12 06:39:56 -0700197 ADD_TYPE(SamplerExternalOES);
ethannicholasb3058bd2016-07-01 08:22:01 -0700198 ADD_TYPE(SamplerCube);
199 ADD_TYPE(Sampler2DRect);
200 ADD_TYPE(Sampler1DArray);
201 ADD_TYPE(Sampler2DArray);
202 ADD_TYPE(SamplerCubeArray);
203 ADD_TYPE(SamplerBuffer);
204 ADD_TYPE(Sampler2DMS);
205 ADD_TYPE(Sampler2DMSArray);
206
Brian Salomonbf7b6202016-11-11 16:08:03 -0500207 ADD_TYPE(ISampler2D);
208
Brian Salomon2a51de82016-11-16 12:06:01 -0500209 ADD_TYPE(Image2D);
210 ADD_TYPE(IImage2D);
211
Greg Daniel64773e62016-11-22 09:44:03 -0500212 ADD_TYPE(SubpassInput);
213 ADD_TYPE(SubpassInputMS);
214
ethannicholasb3058bd2016-07-01 08:22:01 -0700215 ADD_TYPE(GSampler1D);
216 ADD_TYPE(GSampler2D);
217 ADD_TYPE(GSampler3D);
218 ADD_TYPE(GSamplerCube);
219 ADD_TYPE(GSampler2DRect);
220 ADD_TYPE(GSampler1DArray);
221 ADD_TYPE(GSampler2DArray);
222 ADD_TYPE(GSamplerCubeArray);
223 ADD_TYPE(GSamplerBuffer);
224 ADD_TYPE(GSampler2DMS);
225 ADD_TYPE(GSampler2DMSArray);
226
227 ADD_TYPE(Sampler1DShadow);
228 ADD_TYPE(Sampler2DShadow);
229 ADD_TYPE(SamplerCubeShadow);
230 ADD_TYPE(Sampler2DRectShadow);
231 ADD_TYPE(Sampler1DArrayShadow);
232 ADD_TYPE(Sampler2DArrayShadow);
233 ADD_TYPE(SamplerCubeArrayShadow);
234 ADD_TYPE(GSampler2DArrayShadow);
235 ADD_TYPE(GSamplerCubeArrayShadow);
Ethan Nicholasc9472af2017-10-10 16:30:21 -0400236 ADD_TYPE(FragmentProcessor);
Stephen Whiteff5d7a22019-07-26 17:42:06 -0400237 ADD_TYPE(Sampler);
238 ADD_TYPE(Texture2D);
ethannicholasb3058bd2016-07-01 08:22:01 -0700239
Brian Osman28590d52020-03-23 16:59:08 -0400240 StringFragment fpAliasName("shader");
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400241 symbols->addWithoutOwnership(fpAliasName, fContext->fFragmentProcessor_Type.get());
Brian Osman28590d52020-03-23 16:59:08 -0400242
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700243 StringFragment skCapsName("sk_Caps");
244 Variable* skCaps = new Variable(-1, Modifiers(), skCapsName,
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400245 *fContext->fSkCaps_Type, Variable::kGlobal_Storage);
Ethan Nicholas3605ace2016-11-21 15:59:48 -0500246 fIRGenerator->fSymbolTable->add(skCapsName, std::unique_ptr<Symbol>(skCaps));
247
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700248 StringFragment skArgsName("sk_Args");
249 Variable* skArgs = new Variable(-1, Modifiers(), skArgsName,
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400250 *fContext->fSkArgs_Type, Variable::kGlobal_Storage);
Ethan Nicholas762466e2017-06-29 10:03:38 -0400251 fIRGenerator->fSymbolTable->add(skArgsName, std::unique_ptr<Symbol>(skArgs));
252
Ethan Nicholasdb80f692019-11-22 14:06:12 -0500253 fIRGenerator->fIntrinsics = &fGPUIntrinsics;
254 std::vector<std::unique_ptr<ProgramElement>> gpuIntrinsics;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400255 std::vector<std::unique_ptr<ProgramElement>> interpIntrinsics;
256#if !REHYDRATE
Ethan Nicholas8da1e652019-05-24 11:01:59 -0400257 this->processIncludeFile(Program::kFragment_Kind, SKSL_GPU_INCLUDE, strlen(SKSL_GPU_INCLUDE),
Ethan Nicholasdb80f692019-11-22 14:06:12 -0500258 symbols, &gpuIntrinsics, &fGpuSymbolTable);
Ethan Nicholasdb80f692019-11-22 14:06:12 -0500259 // need to hang on to the source so that FunctionDefinition.fSource pointers in this file
260 // remain valid
261 fGpuIncludeSource = std::move(fIRGenerator->fFile);
Ethan Nicholas8da1e652019-05-24 11:01:59 -0400262 this->processIncludeFile(Program::kVertex_Kind, SKSL_VERT_INCLUDE, strlen(SKSL_VERT_INCLUDE),
263 fGpuSymbolTable, &fVertexInclude, &fVertexSymbolTable);
264 this->processIncludeFile(Program::kFragment_Kind, SKSL_FRAG_INCLUDE, strlen(SKSL_FRAG_INCLUDE),
265 fGpuSymbolTable, &fFragmentInclude, &fFragmentSymbolTable);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400266#else
267 {
268 Rehydrator rehydrator(fContext.get(), symbols, this, SKSL_INCLUDE_sksl_gpu,
269 SKSL_INCLUDE_sksl_gpu_LENGTH);
270 fGpuSymbolTable = rehydrator.symbolTable();
271 gpuIntrinsics = rehydrator.elements();
272 }
273 {
274 Rehydrator rehydrator(fContext.get(), fGpuSymbolTable, this, SKSL_INCLUDE_sksl_vert,
275 SKSL_INCLUDE_sksl_vert_LENGTH);
276 fVertexSymbolTable = rehydrator.symbolTable();
277 fVertexInclude = rehydrator.elements();
278 }
279 {
280 Rehydrator rehydrator(fContext.get(), fGpuSymbolTable, this, SKSL_INCLUDE_sksl_frag,
281 SKSL_INCLUDE_sksl_frag_LENGTH);
282 fFragmentSymbolTable = rehydrator.symbolTable();
283 fFragmentInclude = rehydrator.elements();
284 }
285#endif
286 grab_intrinsics(&gpuIntrinsics, &fGPUIntrinsics);
Brian Osmanb08cc022020-04-02 11:38:40 -0400287 grab_intrinsics(&interpIntrinsics, &fInterpreterIntrinsics);
ethannicholasb3058bd2016-07-01 08:22:01 -0700288}
289
290Compiler::~Compiler() {
291 delete fIRGenerator;
292}
293
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400294void Compiler::loadGeometryIntrinsics() {
295 if (fGeometrySymbolTable) {
296 return;
297 }
298 #if REHYDRATE
299 {
300 Rehydrator rehydrator(fContext.get(), fGpuSymbolTable, this, SKSL_INCLUDE_sksl_geom,
301 SKSL_INCLUDE_sksl_geom_LENGTH);
302 fGeometrySymbolTable = rehydrator.symbolTable();
303 fGeometryInclude = rehydrator.elements();
304 }
305 #else
306 this->processIncludeFile(Program::kGeometry_Kind, SKSL_GEOM_INCLUDE,
307 strlen(SKSL_GEOM_INCLUDE), fGpuSymbolTable, &fGeometryInclude,
308 &fGeometrySymbolTable);
309 #endif
310}
311
312void Compiler::loadPipelineIntrinsics() {
313 if (fPipelineSymbolTable) {
314 return;
315 }
316 #if REHYDRATE
317 {
318 Rehydrator rehydrator(fContext.get(), fGpuSymbolTable, this,
319 SKSL_INCLUDE_sksl_pipeline,
320 SKSL_INCLUDE_sksl_pipeline_LENGTH);
321 fPipelineSymbolTable = rehydrator.symbolTable();
322 fPipelineInclude = rehydrator.elements();
323 }
324 #else
325 this->processIncludeFile(Program::kPipelineStage_Kind, SKSL_PIPELINE_INCLUDE,
326 strlen(SKSL_PIPELINE_INCLUDE), fGpuSymbolTable, &fPipelineInclude,
327 &fPipelineSymbolTable);
328 #endif
329}
330
331void Compiler::loadInterpreterIntrinsics() {
332 if (fInterpreterSymbolTable) {
333 return;
334 }
335 this->loadPipelineIntrinsics();
336 #if REHYDRATE
337 {
338 Rehydrator rehydrator(fContext.get(), fPipelineSymbolTable, this,
339 SKSL_INCLUDE_sksl_interp,
340 SKSL_INCLUDE_sksl_interp_LENGTH);
341 fInterpreterSymbolTable = rehydrator.symbolTable();
342 fInterpreterInclude = rehydrator.elements();
343 }
344 #else
345 this->processIncludeFile(Program::kGeneric_Kind, SKSL_INTERP_INCLUDE,
346 strlen(SKSL_INTERP_INCLUDE), fIRGenerator->fSymbolTable,
347 &fInterpreterInclude, &fInterpreterSymbolTable);
348 #endif
349}
350
Ethan Nicholas8da1e652019-05-24 11:01:59 -0400351void Compiler::processIncludeFile(Program::Kind kind, const char* src, size_t length,
352 std::shared_ptr<SymbolTable> base,
353 std::vector<std::unique_ptr<ProgramElement>>* outElements,
354 std::shared_ptr<SymbolTable>* outSymbolTable) {
Ethan Nicholasa11035b2019-11-26 16:27:47 -0500355#ifdef SK_DEBUG
356 String source(src, length);
357 fSource = &source;
358#endif
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400359 std::shared_ptr<SymbolTable> old = fIRGenerator->fSymbolTable;
360 if (base) {
361 fIRGenerator->fSymbolTable = std::move(base);
362 }
Ethan Nicholas8da1e652019-05-24 11:01:59 -0400363 Program::Settings settings;
Ethan Nicholasa11035b2019-11-26 16:27:47 -0500364#if !defined(SKSL_STANDALONE) & SK_SUPPORT_GPU
365 GrContextOptions opts;
366 GrShaderCaps caps(opts);
367 settings.fCaps = &caps;
368#endif
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400369 SkASSERT(fIRGenerator->fCanInline);
370 fIRGenerator->fCanInline = false;
371 fIRGenerator->start(&settings, nullptr, true);
372 fIRGenerator->convertProgram(kind, src, length, outElements);
373 fIRGenerator->fCanInline = true;
Ethan Nicholas8da1e652019-05-24 11:01:59 -0400374 if (this->fErrorCount) {
375 printf("Unexpected errors: %s\n", this->fErrorText.c_str());
376 }
377 SkASSERT(!fErrorCount);
Ethan Nicholas8da1e652019-05-24 11:01:59 -0400378 *outSymbolTable = fIRGenerator->fSymbolTable;
Ethan Nicholasa11035b2019-11-26 16:27:47 -0500379#ifdef SK_DEBUG
380 fSource = nullptr;
381#endif
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400382 fIRGenerator->fSymbolTable = std::move(old);
Ethan Nicholas8da1e652019-05-24 11:01:59 -0400383}
384
ethannicholas22f939e2016-10-13 13:25:34 -0700385// add the definition created by assigning to the lvalue to the definition set
Ethan Nicholas86a43402017-01-19 13:32:00 -0500386void Compiler::addDefinition(const Expression* lvalue, std::unique_ptr<Expression>* expr,
387 DefinitionMap* definitions) {
ethannicholas22f939e2016-10-13 13:25:34 -0700388 switch (lvalue->fKind) {
389 case Expression::kVariableReference_Kind: {
390 const Variable& var = ((VariableReference*) lvalue)->fVariable;
391 if (var.fStorage == Variable::kLocal_Storage) {
392 (*definitions)[&var] = expr;
393 }
394 break;
395 }
396 case Expression::kSwizzle_Kind:
397 // We consider the variable written to as long as at least some of its components have
398 // been written to. This will lead to some false negatives (we won't catch it if you
399 // write to foo.x and then read foo.y), but being stricter could lead to false positives
Mike Klein6ad99092016-10-26 10:35:22 -0400400 // (we write to foo.x, and then pass foo to a function which happens to only read foo.x,
401 // 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 -0700402 // more complicated whole-program analysis. This is probably good enough.
Mike Klein6ad99092016-10-26 10:35:22 -0400403 this->addDefinition(((Swizzle*) lvalue)->fBase.get(),
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400404 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
ethannicholas22f939e2016-10-13 13:25:34 -0700405 definitions);
406 break;
407 case Expression::kIndex_Kind:
408 // see comments in Swizzle
Mike Klein6ad99092016-10-26 10:35:22 -0400409 this->addDefinition(((IndexExpression*) lvalue)->fBase.get(),
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400410 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
ethannicholas22f939e2016-10-13 13:25:34 -0700411 definitions);
412 break;
413 case Expression::kFieldAccess_Kind:
414 // see comments in Swizzle
Mike Klein6ad99092016-10-26 10:35:22 -0400415 this->addDefinition(((FieldAccess*) lvalue)->fBase.get(),
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400416 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
ethannicholas22f939e2016-10-13 13:25:34 -0700417 definitions);
418 break;
Ethan Nicholasa583b812018-01-18 13:32:11 -0500419 case Expression::kTernary_Kind:
420 // To simplify analysis, we just pretend that we write to both sides of the ternary.
421 // This allows for false positives (meaning we fail to detect that a variable might not
422 // have been assigned), but is preferable to false negatives.
423 this->addDefinition(((TernaryExpression*) lvalue)->fIfTrue.get(),
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400424 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
Ethan Nicholasa583b812018-01-18 13:32:11 -0500425 definitions);
426 this->addDefinition(((TernaryExpression*) lvalue)->fIfFalse.get(),
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400427 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
Ethan Nicholasa583b812018-01-18 13:32:11 -0500428 definitions);
429 break;
Ethan Nicholas91164d12019-05-15 15:29:54 -0400430 case Expression::kExternalValue_Kind:
431 break;
ethannicholas22f939e2016-10-13 13:25:34 -0700432 default:
433 // not an lvalue, can't happen
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400434 SkASSERT(false);
ethannicholas22f939e2016-10-13 13:25:34 -0700435 }
436}
437
438// add local variables defined by this node to the set
Mike Klein6ad99092016-10-26 10:35:22 -0400439void Compiler::addDefinitions(const BasicBlock::Node& node,
Ethan Nicholas86a43402017-01-19 13:32:00 -0500440 DefinitionMap* definitions) {
ethannicholas22f939e2016-10-13 13:25:34 -0700441 switch (node.fKind) {
442 case BasicBlock::Node::kExpression_Kind: {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400443 SkASSERT(node.expression());
Ethan Nicholascb670962017-04-20 19:31:52 -0400444 const Expression* expr = (Expression*) node.expression()->get();
Ethan Nicholas86a43402017-01-19 13:32:00 -0500445 switch (expr->fKind) {
446 case Expression::kBinary_Kind: {
447 BinaryExpression* b = (BinaryExpression*) expr;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400448 if (b->fOperator == Token::Kind::TK_EQ) {
Ethan Nicholas86a43402017-01-19 13:32:00 -0500449 this->addDefinition(b->fLeft.get(), &b->fRight, definitions);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700450 } else if (Compiler::IsAssignment(b->fOperator)) {
Ethan Nicholas86a43402017-01-19 13:32:00 -0500451 this->addDefinition(
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400452 b->fLeft.get(),
453 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
454 definitions);
Ethan Nicholas86a43402017-01-19 13:32:00 -0500455
456 }
457 break;
ethannicholas22f939e2016-10-13 13:25:34 -0700458 }
Ethan Nicholasc6a19f12018-03-29 16:46:56 -0400459 case Expression::kFunctionCall_Kind: {
460 const FunctionCall& c = (const FunctionCall&) *expr;
461 for (size_t i = 0; i < c.fFunction.fParameters.size(); ++i) {
462 if (c.fFunction.fParameters[i]->fModifiers.fFlags & Modifiers::kOut_Flag) {
463 this->addDefinition(
464 c.fArguments[i].get(),
465 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
466 definitions);
467 }
468 }
469 break;
470 }
Ethan Nicholas86a43402017-01-19 13:32:00 -0500471 case Expression::kPrefix_Kind: {
472 const PrefixExpression* p = (PrefixExpression*) expr;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400473 if (p->fOperator == Token::Kind::TK_MINUSMINUS ||
474 p->fOperator == Token::Kind::TK_PLUSPLUS) {
Ethan Nicholas86a43402017-01-19 13:32:00 -0500475 this->addDefinition(
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400476 p->fOperand.get(),
477 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
478 definitions);
Ethan Nicholas86a43402017-01-19 13:32:00 -0500479 }
480 break;
481 }
482 case Expression::kPostfix_Kind: {
483 const PostfixExpression* p = (PostfixExpression*) expr;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400484 if (p->fOperator == Token::Kind::TK_MINUSMINUS ||
485 p->fOperator == Token::Kind::TK_PLUSPLUS) {
Ethan Nicholas86a43402017-01-19 13:32:00 -0500486 this->addDefinition(
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400487 p->fOperand.get(),
488 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
489 definitions);
Ethan Nicholas86a43402017-01-19 13:32:00 -0500490 }
491 break;
492 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400493 case Expression::kVariableReference_Kind: {
494 const VariableReference* v = (VariableReference*) expr;
495 if (v->fRefKind != VariableReference::kRead_RefKind) {
496 this->addDefinition(
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400497 v,
498 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
499 definitions);
Ethan Nicholascb670962017-04-20 19:31:52 -0400500 }
John Stiles30212b72020-06-11 17:55:07 -0400501 break;
Ethan Nicholascb670962017-04-20 19:31:52 -0400502 }
Ethan Nicholas86a43402017-01-19 13:32:00 -0500503 default:
504 break;
ethannicholas22f939e2016-10-13 13:25:34 -0700505 }
506 break;
507 }
508 case BasicBlock::Node::kStatement_Kind: {
Ethan Nicholascb670962017-04-20 19:31:52 -0400509 const Statement* stmt = (Statement*) node.statement()->get();
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000510 if (stmt->fKind == Statement::kVarDeclaration_Kind) {
511 VarDeclaration& vd = (VarDeclaration&) *stmt;
512 if (vd.fValue) {
513 (*definitions)[vd.fVar] = &vd.fValue;
ethannicholas22f939e2016-10-13 13:25:34 -0700514 }
515 }
516 break;
517 }
518 }
519}
520
521void Compiler::scanCFG(CFG* cfg, BlockId blockId, std::set<BlockId>* workList) {
522 BasicBlock& block = cfg->fBlocks[blockId];
523
524 // compute definitions after this block
Ethan Nicholas86a43402017-01-19 13:32:00 -0500525 DefinitionMap after = block.fBefore;
ethannicholas22f939e2016-10-13 13:25:34 -0700526 for (const BasicBlock::Node& n : block.fNodes) {
527 this->addDefinitions(n, &after);
528 }
529
530 // propagate definitions to exits
531 for (BlockId exitId : block.fExits) {
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400532 if (exitId == blockId) {
533 continue;
534 }
ethannicholas22f939e2016-10-13 13:25:34 -0700535 BasicBlock& exit = cfg->fBlocks[exitId];
536 for (const auto& pair : after) {
Ethan Nicholas86a43402017-01-19 13:32:00 -0500537 std::unique_ptr<Expression>* e1 = pair.second;
538 auto found = exit.fBefore.find(pair.first);
539 if (found == exit.fBefore.end()) {
540 // exit has no definition for it, just copy it
541 workList->insert(exitId);
ethannicholas22f939e2016-10-13 13:25:34 -0700542 exit.fBefore[pair.first] = e1;
543 } else {
Ethan Nicholas86a43402017-01-19 13:32:00 -0500544 // exit has a (possibly different) value already defined
545 std::unique_ptr<Expression>* e2 = exit.fBefore[pair.first];
ethannicholas22f939e2016-10-13 13:25:34 -0700546 if (e1 != e2) {
547 // definition has changed, merge and add exit block to worklist
548 workList->insert(exitId);
Ethan Nicholasaf197692017-02-27 13:26:45 -0500549 if (e1 && e2) {
550 exit.fBefore[pair.first] =
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400551 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression;
Ethan Nicholasaf197692017-02-27 13:26:45 -0500552 } else {
553 exit.fBefore[pair.first] = nullptr;
554 }
ethannicholas22f939e2016-10-13 13:25:34 -0700555 }
556 }
557 }
558 }
559}
560
561// returns a map which maps all local variables in the function to null, indicating that their value
562// is initially unknown
Ethan Nicholas86a43402017-01-19 13:32:00 -0500563static DefinitionMap compute_start_state(const CFG& cfg) {
564 DefinitionMap result;
Mike Klein6ad99092016-10-26 10:35:22 -0400565 for (const auto& block : cfg.fBlocks) {
566 for (const auto& node : block.fNodes) {
ethannicholas22f939e2016-10-13 13:25:34 -0700567 if (node.fKind == BasicBlock::Node::kStatement_Kind) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400568 SkASSERT(node.statement());
Ethan Nicholascb670962017-04-20 19:31:52 -0400569 const Statement* s = node.statement()->get();
ethannicholas22f939e2016-10-13 13:25:34 -0700570 if (s->fKind == Statement::kVarDeclarations_Kind) {
571 const VarDeclarationsStatement* vd = (const VarDeclarationsStatement*) s;
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000572 for (const auto& decl : vd->fDeclaration->fVars) {
573 if (decl->fKind == Statement::kVarDeclaration_Kind) {
574 result[((VarDeclaration&) *decl).fVar] = nullptr;
575 }
Mike Klein6ad99092016-10-26 10:35:22 -0400576 }
ethannicholas22f939e2016-10-13 13:25:34 -0700577 }
578 }
579 }
580 }
581 return result;
582}
583
Ethan Nicholascb670962017-04-20 19:31:52 -0400584/**
585 * Returns true if assigning to this lvalue has no effect.
586 */
587static bool is_dead(const Expression& lvalue) {
588 switch (lvalue.fKind) {
589 case Expression::kVariableReference_Kind:
590 return ((VariableReference&) lvalue).fVariable.dead();
591 case Expression::kSwizzle_Kind:
592 return is_dead(*((Swizzle&) lvalue).fBase);
593 case Expression::kFieldAccess_Kind:
594 return is_dead(*((FieldAccess&) lvalue).fBase);
595 case Expression::kIndex_Kind: {
596 const IndexExpression& idx = (IndexExpression&) lvalue;
Ethan Nicholas2a099da2020-01-02 14:40:54 -0500597 return is_dead(*idx.fBase) &&
598 !idx.fIndex->hasProperty(Expression::Property::kSideEffects);
Ethan Nicholascb670962017-04-20 19:31:52 -0400599 }
Ethan Nicholasa583b812018-01-18 13:32:11 -0500600 case Expression::kTernary_Kind: {
601 const TernaryExpression& t = (TernaryExpression&) lvalue;
602 return !t.fTest->hasSideEffects() && is_dead(*t.fIfTrue) && is_dead(*t.fIfFalse);
603 }
Ethan Nicholas91164d12019-05-15 15:29:54 -0400604 case Expression::kExternalValue_Kind:
605 return false;
Ethan Nicholascb670962017-04-20 19:31:52 -0400606 default:
Ethan Nicholas2a099da2020-01-02 14:40:54 -0500607#ifdef SK_DEBUG
Ethan Nicholascb670962017-04-20 19:31:52 -0400608 ABORT("invalid lvalue: %s\n", lvalue.description().c_str());
Ethan Nicholas2a099da2020-01-02 14:40:54 -0500609#endif
610 return false;
Ethan Nicholascb670962017-04-20 19:31:52 -0400611 }
612}
ethannicholas22f939e2016-10-13 13:25:34 -0700613
Ethan Nicholascb670962017-04-20 19:31:52 -0400614/**
615 * Returns true if this is an assignment which can be collapsed down to just the right hand side due
616 * to a dead target and lack of side effects on the left hand side.
617 */
618static bool dead_assignment(const BinaryExpression& b) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700619 if (!Compiler::IsAssignment(b.fOperator)) {
Ethan Nicholascb670962017-04-20 19:31:52 -0400620 return false;
621 }
622 return is_dead(*b.fLeft);
623}
624
625void Compiler::computeDataFlow(CFG* cfg) {
626 cfg->fBlocks[cfg->fStart].fBefore = compute_start_state(*cfg);
ethannicholas22f939e2016-10-13 13:25:34 -0700627 std::set<BlockId> workList;
Ethan Nicholascb670962017-04-20 19:31:52 -0400628 for (BlockId i = 0; i < cfg->fBlocks.size(); i++) {
ethannicholas22f939e2016-10-13 13:25:34 -0700629 workList.insert(i);
630 }
631 while (workList.size()) {
632 BlockId next = *workList.begin();
633 workList.erase(workList.begin());
Ethan Nicholascb670962017-04-20 19:31:52 -0400634 this->scanCFG(cfg, next, &workList);
ethannicholas22f939e2016-10-13 13:25:34 -0700635 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400636}
637
638/**
639 * Attempts to replace the expression pointed to by iter with a new one (in both the CFG and the
640 * IR). If the expression can be cleanly removed, returns true and updates the iterator to point to
641 * the newly-inserted element. Otherwise updates only the IR and returns false (and the CFG will
642 * need to be regenerated).
643 */
644bool try_replace_expression(BasicBlock* b,
645 std::vector<BasicBlock::Node>::iterator* iter,
646 std::unique_ptr<Expression>* newExpression) {
647 std::unique_ptr<Expression>* target = (*iter)->expression();
648 if (!b->tryRemoveExpression(iter)) {
649 *target = std::move(*newExpression);
650 return false;
651 }
652 *target = std::move(*newExpression);
653 return b->tryInsertExpression(iter, target);
654}
655
656/**
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400657 * Returns true if the expression is a constant numeric literal with the specified value, or a
658 * constant vector with all elements equal to the specified value.
Ethan Nicholascb670962017-04-20 19:31:52 -0400659 */
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400660bool is_constant(const Expression& expr, double value) {
Ethan Nicholascb670962017-04-20 19:31:52 -0400661 switch (expr.fKind) {
662 case Expression::kIntLiteral_Kind:
663 return ((IntLiteral&) expr).fValue == value;
664 case Expression::kFloatLiteral_Kind:
665 return ((FloatLiteral&) expr).fValue == value;
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400666 case Expression::kConstructor_Kind: {
667 Constructor& c = (Constructor&) expr;
Ethan Nicholasd188c182019-06-10 15:55:38 -0400668 bool isFloat = c.fType.columns() > 1 ? c.fType.componentType().isFloat()
669 : c.fType.isFloat();
Brian Osmanb6b95732020-06-30 11:44:27 -0400670 if (c.fType.kind() == Type::kVector_Kind && c.isCompileTimeConstant()) {
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400671 for (int i = 0; i < c.fType.columns(); ++i) {
Ethan Nicholasd188c182019-06-10 15:55:38 -0400672 if (isFloat) {
673 if (c.getFVecComponent(i) != value) {
674 return false;
675 }
676 } else if (c.getIVecComponent(i) != value) {
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400677 return false;
678 }
679 }
680 return true;
681 }
682 return false;
683 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400684 default:
685 return false;
686 }
687}
688
689/**
690 * Collapses the binary expression pointed to by iter down to just the right side (in both the IR
691 * and CFG structures).
692 */
693void delete_left(BasicBlock* b,
694 std::vector<BasicBlock::Node>::iterator* iter,
695 bool* outUpdated,
696 bool* outNeedsRescan) {
697 *outUpdated = true;
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400698 std::unique_ptr<Expression>* target = (*iter)->expression();
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400699 SkASSERT((*target)->fKind == Expression::kBinary_Kind);
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400700 BinaryExpression& bin = (BinaryExpression&) **target;
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400701 SkASSERT(!bin.fLeft->hasSideEffects());
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400702 bool result;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400703 if (bin.fOperator == Token::Kind::TK_EQ) {
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400704 result = b->tryRemoveLValueBefore(iter, bin.fLeft.get());
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400705 } else {
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400706 result = b->tryRemoveExpressionBefore(iter, bin.fLeft.get());
Ethan Nicholascb670962017-04-20 19:31:52 -0400707 }
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400708 *target = std::move(bin.fRight);
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400709 if (!result) {
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400710 *outNeedsRescan = true;
711 return;
712 }
713 if (*iter == b->fNodes.begin()) {
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400714 *outNeedsRescan = true;
715 return;
716 }
717 --(*iter);
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400718 if ((*iter)->fKind != BasicBlock::Node::kExpression_Kind ||
719 (*iter)->expression() != &bin.fRight) {
720 *outNeedsRescan = true;
721 return;
722 }
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400723 *iter = b->fNodes.erase(*iter);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400724 SkASSERT((*iter)->expression() == target);
Ethan Nicholascb670962017-04-20 19:31:52 -0400725}
726
727/**
728 * Collapses the binary expression pointed to by iter down to just the left side (in both the IR and
729 * CFG structures).
730 */
731void delete_right(BasicBlock* b,
732 std::vector<BasicBlock::Node>::iterator* iter,
733 bool* outUpdated,
734 bool* outNeedsRescan) {
735 *outUpdated = true;
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400736 std::unique_ptr<Expression>* target = (*iter)->expression();
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400737 SkASSERT((*target)->fKind == Expression::kBinary_Kind);
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400738 BinaryExpression& bin = (BinaryExpression&) **target;
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400739 SkASSERT(!bin.fRight->hasSideEffects());
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400740 if (!b->tryRemoveExpressionBefore(iter, bin.fRight.get())) {
741 *target = std::move(bin.fLeft);
Ethan Nicholascb670962017-04-20 19:31:52 -0400742 *outNeedsRescan = true;
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400743 return;
Ethan Nicholascb670962017-04-20 19:31:52 -0400744 }
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400745 *target = std::move(bin.fLeft);
746 if (*iter == b->fNodes.begin()) {
747 *outNeedsRescan = true;
748 return;
749 }
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400750 --(*iter);
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400751 if (((*iter)->fKind != BasicBlock::Node::kExpression_Kind ||
752 (*iter)->expression() != &bin.fLeft)) {
753 *outNeedsRescan = true;
754 return;
755 }
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400756 *iter = b->fNodes.erase(*iter);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400757 SkASSERT((*iter)->expression() == target);
Ethan Nicholascb670962017-04-20 19:31:52 -0400758}
759
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400760/**
761 * Constructs the specified type using a single argument.
762 */
763static std::unique_ptr<Expression> construct(const Type& type, std::unique_ptr<Expression> v) {
764 std::vector<std::unique_ptr<Expression>> args;
765 args.push_back(std::move(v));
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700766 auto result = std::unique_ptr<Expression>(new Constructor(-1, type, std::move(args)));
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400767 return result;
768}
769
770/**
771 * Used in the implementations of vectorize_left and vectorize_right. Given a vector type and an
772 * expression x, deletes the expression pointed to by iter and replaces it with <type>(x).
773 */
774static void vectorize(BasicBlock* b,
775 std::vector<BasicBlock::Node>::iterator* iter,
776 const Type& type,
777 std::unique_ptr<Expression>* otherExpression,
778 bool* outUpdated,
779 bool* outNeedsRescan) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400780 SkASSERT((*(*iter)->expression())->fKind == Expression::kBinary_Kind);
781 SkASSERT(type.kind() == Type::kVector_Kind);
782 SkASSERT((*otherExpression)->fType.kind() == Type::kScalar_Kind);
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400783 *outUpdated = true;
784 std::unique_ptr<Expression>* target = (*iter)->expression();
785 if (!b->tryRemoveExpression(iter)) {
786 *target = construct(type, std::move(*otherExpression));
787 *outNeedsRescan = true;
788 } else {
789 *target = construct(type, std::move(*otherExpression));
790 if (!b->tryInsertExpression(iter, target)) {
791 *outNeedsRescan = true;
792 }
793 }
794}
795
796/**
797 * Given a binary expression of the form x <op> vec<n>(y), deletes the right side and vectorizes the
798 * left to yield vec<n>(x).
799 */
800static void vectorize_left(BasicBlock* b,
801 std::vector<BasicBlock::Node>::iterator* iter,
802 bool* outUpdated,
803 bool* outNeedsRescan) {
804 BinaryExpression& bin = (BinaryExpression&) **(*iter)->expression();
805 vectorize(b, iter, bin.fRight->fType, &bin.fLeft, outUpdated, outNeedsRescan);
806}
807
808/**
809 * Given a binary expression of the form vec<n>(x) <op> y, deletes the left side and vectorizes the
810 * right to yield vec<n>(y).
811 */
812static void vectorize_right(BasicBlock* b,
813 std::vector<BasicBlock::Node>::iterator* iter,
814 bool* outUpdated,
815 bool* outNeedsRescan) {
816 BinaryExpression& bin = (BinaryExpression&) **(*iter)->expression();
817 vectorize(b, iter, bin.fLeft->fType, &bin.fRight, outUpdated, outNeedsRescan);
818}
819
820// Mark that an expression which we were writing to is no longer being written to
821void clear_write(const Expression& expr) {
822 switch (expr.fKind) {
823 case Expression::kVariableReference_Kind: {
824 ((VariableReference&) expr).setRefKind(VariableReference::kRead_RefKind);
825 break;
826 }
827 case Expression::kFieldAccess_Kind:
828 clear_write(*((FieldAccess&) expr).fBase);
829 break;
830 case Expression::kSwizzle_Kind:
831 clear_write(*((Swizzle&) expr).fBase);
832 break;
833 case Expression::kIndex_Kind:
834 clear_write(*((IndexExpression&) expr).fBase);
835 break;
836 default:
837 ABORT("shouldn't be writing to this kind of expression\n");
838 break;
839 }
840}
841
Ethan Nicholascb670962017-04-20 19:31:52 -0400842void Compiler::simplifyExpression(DefinitionMap& definitions,
843 BasicBlock& b,
844 std::vector<BasicBlock::Node>::iterator* iter,
845 std::unordered_set<const Variable*>* undefinedVariables,
846 bool* outUpdated,
847 bool* outNeedsRescan) {
848 Expression* expr = (*iter)->expression()->get();
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400849 SkASSERT(expr);
Ethan Nicholascb670962017-04-20 19:31:52 -0400850 if ((*iter)->fConstantPropagation) {
851 std::unique_ptr<Expression> optimized = expr->constantPropagate(*fIRGenerator, definitions);
852 if (optimized) {
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400853 *outUpdated = true;
Ethan Nicholascb670962017-04-20 19:31:52 -0400854 if (!try_replace_expression(&b, iter, &optimized)) {
855 *outNeedsRescan = true;
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400856 return;
Ethan Nicholascb670962017-04-20 19:31:52 -0400857 }
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400858 SkASSERT((*iter)->fKind == BasicBlock::Node::kExpression_Kind);
Ethan Nicholascb670962017-04-20 19:31:52 -0400859 expr = (*iter)->expression()->get();
Ethan Nicholascb670962017-04-20 19:31:52 -0400860 }
861 }
862 switch (expr->fKind) {
863 case Expression::kVariableReference_Kind: {
Ethan Nicholas8f7e28f2018-03-26 14:24:27 -0400864 const VariableReference& ref = (VariableReference&) *expr;
865 const Variable& var = ref.fVariable;
866 if (ref.refKind() != VariableReference::kWrite_RefKind &&
867 ref.refKind() != VariableReference::kPointer_RefKind &&
868 var.fStorage == Variable::kLocal_Storage && !definitions[&var] &&
Ethan Nicholascb670962017-04-20 19:31:52 -0400869 (*undefinedVariables).find(&var) == (*undefinedVariables).end()) {
870 (*undefinedVariables).insert(&var);
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000871 this->error(expr->fOffset,
872 "'" + var.fName + "' has not been assigned");
Ethan Nicholascb670962017-04-20 19:31:52 -0400873 }
874 break;
875 }
876 case Expression::kTernary_Kind: {
877 TernaryExpression* t = (TernaryExpression*) expr;
878 if (t->fTest->fKind == Expression::kBoolLiteral_Kind) {
879 // ternary has a constant test, replace it with either the true or
880 // false branch
881 if (((BoolLiteral&) *t->fTest).fValue) {
882 (*iter)->setExpression(std::move(t->fIfTrue));
883 } else {
884 (*iter)->setExpression(std::move(t->fIfFalse));
885 }
886 *outUpdated = true;
887 *outNeedsRescan = true;
888 }
889 break;
890 }
891 case Expression::kBinary_Kind: {
Ethan Nicholascb670962017-04-20 19:31:52 -0400892 BinaryExpression* bin = (BinaryExpression*) expr;
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400893 if (dead_assignment(*bin)) {
894 delete_left(&b, iter, outUpdated, outNeedsRescan);
895 break;
896 }
897 // collapse useless expressions like x * 1 or x + 0
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400898 if (((bin->fLeft->fType.kind() != Type::kScalar_Kind) &&
899 (bin->fLeft->fType.kind() != Type::kVector_Kind)) ||
900 ((bin->fRight->fType.kind() != Type::kScalar_Kind) &&
901 (bin->fRight->fType.kind() != Type::kVector_Kind))) {
902 break;
903 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400904 switch (bin->fOperator) {
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400905 case Token::Kind::TK_STAR:
Ethan Nicholascb670962017-04-20 19:31:52 -0400906 if (is_constant(*bin->fLeft, 1)) {
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400907 if (bin->fLeft->fType.kind() == Type::kVector_Kind &&
908 bin->fRight->fType.kind() == Type::kScalar_Kind) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400909 // float4(1) * x -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400910 vectorize_right(&b, iter, outUpdated, outNeedsRescan);
911 } else {
912 // 1 * x -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400913 // 1 * float4(x) -> float4(x)
914 // float4(1) * float4(x) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400915 delete_left(&b, iter, outUpdated, outNeedsRescan);
916 }
917 }
918 else if (is_constant(*bin->fLeft, 0)) {
919 if (bin->fLeft->fType.kind() == Type::kScalar_Kind &&
Ethan Nicholas08dae922018-01-23 10:31:56 -0500920 bin->fRight->fType.kind() == Type::kVector_Kind &&
921 !bin->fRight->hasSideEffects()) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400922 // 0 * float4(x) -> float4(0)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400923 vectorize_left(&b, iter, outUpdated, outNeedsRescan);
924 } else {
925 // 0 * x -> 0
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400926 // float4(0) * x -> float4(0)
927 // float4(0) * float4(x) -> float4(0)
Ethan Nicholas51493ee2017-12-11 12:34:33 -0500928 if (!bin->fRight->hasSideEffects()) {
929 delete_right(&b, iter, outUpdated, outNeedsRescan);
930 }
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400931 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400932 }
933 else if (is_constant(*bin->fRight, 1)) {
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400934 if (bin->fLeft->fType.kind() == Type::kScalar_Kind &&
935 bin->fRight->fType.kind() == Type::kVector_Kind) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400936 // x * float4(1) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400937 vectorize_left(&b, iter, outUpdated, outNeedsRescan);
938 } else {
939 // x * 1 -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400940 // float4(x) * 1 -> float4(x)
941 // float4(x) * float4(1) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400942 delete_right(&b, iter, outUpdated, outNeedsRescan);
943 }
944 }
945 else if (is_constant(*bin->fRight, 0)) {
946 if (bin->fLeft->fType.kind() == Type::kVector_Kind &&
Ethan Nicholas08dae922018-01-23 10:31:56 -0500947 bin->fRight->fType.kind() == Type::kScalar_Kind &&
948 !bin->fLeft->hasSideEffects()) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400949 // float4(x) * 0 -> float4(0)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400950 vectorize_right(&b, iter, outUpdated, outNeedsRescan);
951 } else {
952 // x * 0 -> 0
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400953 // x * float4(0) -> float4(0)
954 // float4(x) * float4(0) -> float4(0)
Ethan Nicholas51493ee2017-12-11 12:34:33 -0500955 if (!bin->fLeft->hasSideEffects()) {
956 delete_left(&b, iter, outUpdated, outNeedsRescan);
957 }
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400958 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400959 }
960 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400961 case Token::Kind::TK_PLUS:
Ethan Nicholascb670962017-04-20 19:31:52 -0400962 if (is_constant(*bin->fLeft, 0)) {
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400963 if (bin->fLeft->fType.kind() == Type::kVector_Kind &&
964 bin->fRight->fType.kind() == Type::kScalar_Kind) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400965 // float4(0) + x -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400966 vectorize_right(&b, iter, outUpdated, outNeedsRescan);
967 } else {
968 // 0 + x -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400969 // 0 + float4(x) -> float4(x)
970 // float4(0) + float4(x) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400971 delete_left(&b, iter, outUpdated, outNeedsRescan);
972 }
973 } else if (is_constant(*bin->fRight, 0)) {
974 if (bin->fLeft->fType.kind() == Type::kScalar_Kind &&
975 bin->fRight->fType.kind() == Type::kVector_Kind) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400976 // x + float4(0) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400977 vectorize_left(&b, iter, outUpdated, outNeedsRescan);
978 } else {
979 // x + 0 -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400980 // float4(x) + 0 -> float4(x)
981 // float4(x) + float4(0) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400982 delete_right(&b, iter, outUpdated, outNeedsRescan);
983 }
Ethan Nicholas56e42712017-04-21 10:23:37 -0400984 }
985 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400986 case Token::Kind::TK_MINUS:
Ethan Nicholas56e42712017-04-21 10:23:37 -0400987 if (is_constant(*bin->fRight, 0)) {
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400988 if (bin->fLeft->fType.kind() == Type::kScalar_Kind &&
989 bin->fRight->fType.kind() == Type::kVector_Kind) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400990 // x - float4(0) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400991 vectorize_left(&b, iter, outUpdated, outNeedsRescan);
992 } else {
993 // x - 0 -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400994 // float4(x) - 0 -> float4(x)
995 // float4(x) - float4(0) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400996 delete_right(&b, iter, outUpdated, outNeedsRescan);
997 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400998 }
999 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001000 case Token::Kind::TK_SLASH:
Ethan Nicholascb670962017-04-20 19:31:52 -04001001 if (is_constant(*bin->fRight, 1)) {
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001002 if (bin->fLeft->fType.kind() == Type::kScalar_Kind &&
1003 bin->fRight->fType.kind() == Type::kVector_Kind) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001004 // x / float4(1) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001005 vectorize_left(&b, iter, outUpdated, outNeedsRescan);
1006 } else {
1007 // x / 1 -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001008 // float4(x) / 1 -> float4(x)
1009 // float4(x) / float4(1) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001010 delete_right(&b, iter, outUpdated, outNeedsRescan);
1011 }
1012 } else if (is_constant(*bin->fLeft, 0)) {
1013 if (bin->fLeft->fType.kind() == Type::kScalar_Kind &&
Ethan Nicholas08dae922018-01-23 10:31:56 -05001014 bin->fRight->fType.kind() == Type::kVector_Kind &&
1015 !bin->fRight->hasSideEffects()) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001016 // 0 / float4(x) -> float4(0)
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001017 vectorize_left(&b, iter, outUpdated, outNeedsRescan);
1018 } else {
1019 // 0 / x -> 0
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001020 // float4(0) / x -> float4(0)
1021 // float4(0) / float4(x) -> float4(0)
Ethan Nicholas51493ee2017-12-11 12:34:33 -05001022 if (!bin->fRight->hasSideEffects()) {
1023 delete_right(&b, iter, outUpdated, outNeedsRescan);
1024 }
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001025 }
1026 }
1027 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001028 case Token::Kind::TK_PLUSEQ:
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001029 if (is_constant(*bin->fRight, 0)) {
1030 clear_write(*bin->fLeft);
1031 delete_right(&b, iter, outUpdated, outNeedsRescan);
1032 }
1033 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001034 case Token::Kind::TK_MINUSEQ:
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001035 if (is_constant(*bin->fRight, 0)) {
1036 clear_write(*bin->fLeft);
1037 delete_right(&b, iter, outUpdated, outNeedsRescan);
1038 }
1039 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001040 case Token::Kind::TK_STAREQ:
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001041 if (is_constant(*bin->fRight, 1)) {
1042 clear_write(*bin->fLeft);
1043 delete_right(&b, iter, outUpdated, outNeedsRescan);
1044 }
1045 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001046 case Token::Kind::TK_SLASHEQ:
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001047 if (is_constant(*bin->fRight, 1)) {
1048 clear_write(*bin->fLeft);
Ethan Nicholascb670962017-04-20 19:31:52 -04001049 delete_right(&b, iter, outUpdated, outNeedsRescan);
1050 }
1051 break;
1052 default:
1053 break;
1054 }
Ethan Nicholas409f6f02019-09-17 12:34:39 -04001055 break;
1056 }
1057 case Expression::kSwizzle_Kind: {
1058 Swizzle& s = (Swizzle&) *expr;
1059 // detect identity swizzles like foo.rgba
1060 if ((int) s.fComponents.size() == s.fBase->fType.columns()) {
1061 bool identity = true;
1062 for (int i = 0; i < (int) s.fComponents.size(); ++i) {
1063 if (s.fComponents[i] != i) {
1064 identity = false;
1065 break;
1066 }
1067 }
1068 if (identity) {
1069 *outUpdated = true;
1070 if (!try_replace_expression(&b, iter, &s.fBase)) {
1071 *outNeedsRescan = true;
1072 return;
1073 }
1074 SkASSERT((*iter)->fKind == BasicBlock::Node::kExpression_Kind);
1075 break;
1076 }
1077 }
1078 // detect swizzles of swizzles, e.g. replace foo.argb.r000 with foo.a000
1079 if (s.fBase->fKind == Expression::kSwizzle_Kind) {
1080 Swizzle& base = (Swizzle&) *s.fBase;
1081 std::vector<int> final;
1082 for (int c : s.fComponents) {
1083 if (c == SKSL_SWIZZLE_0 || c == SKSL_SWIZZLE_1) {
1084 final.push_back(c);
1085 } else {
1086 final.push_back(base.fComponents[c]);
1087 }
1088 }
1089 *outUpdated = true;
1090 std::unique_ptr<Expression> replacement(new Swizzle(*fContext, base.fBase->clone(),
1091 std::move(final)));
1092 if (!try_replace_expression(&b, iter, &replacement)) {
1093 *outNeedsRescan = true;
1094 return;
1095 }
1096 SkASSERT((*iter)->fKind == BasicBlock::Node::kExpression_Kind);
Ethan Nicholas409f6f02019-09-17 12:34:39 -04001097 }
John Stiles30212b72020-06-11 17:55:07 -04001098 break;
Ethan Nicholascb670962017-04-20 19:31:52 -04001099 }
1100 default:
1101 break;
1102 }
1103}
1104
John Stiles92219b42020-06-15 12:32:24 -04001105// Implementation-detail recursive helper function for `contains_conditional_break`.
1106static bool contains_conditional_break_impl(Statement& s, bool inConditional) {
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001107 switch (s.fKind) {
1108 case Statement::kBlock_Kind:
John Stiles92219b42020-06-15 12:32:24 -04001109 for (const std::unique_ptr<Statement>& sub : static_cast<Block&>(s).fStatements) {
1110 if (contains_conditional_break_impl(*sub, inConditional)) {
Ethan Nicholas5005a222018-08-24 13:06:27 -04001111 return true;
1112 }
1113 }
1114 return false;
John Stiles92219b42020-06-15 12:32:24 -04001115
Ethan Nicholas5005a222018-08-24 13:06:27 -04001116 case Statement::kBreak_Kind:
1117 return inConditional;
John Stiles92219b42020-06-15 12:32:24 -04001118
Ethan Nicholas5005a222018-08-24 13:06:27 -04001119 case Statement::kIf_Kind: {
John Stiles92219b42020-06-15 12:32:24 -04001120 const IfStatement& i = static_cast<IfStatement&>(s);
1121 return contains_conditional_break_impl(*i.fIfTrue, /*inConditional=*/true) ||
1122 (i.fIfFalse &&
1123 contains_conditional_break_impl(*i.fIfFalse, /*inConditional=*/true));
Ethan Nicholas5005a222018-08-24 13:06:27 -04001124 }
John Stiles92219b42020-06-15 12:32:24 -04001125
Ethan Nicholas5005a222018-08-24 13:06:27 -04001126 default:
1127 return false;
1128 }
1129}
1130
John Stiles92219b42020-06-15 12:32:24 -04001131// Returns true if this statement could potentially execute a break at the current level. We ignore
1132// nested loops and switches, since any breaks inside of them will merely break the loop / switch.
1133static bool contains_conditional_break(Statement& s) {
1134 return contains_conditional_break_impl(s, /*inConditional=*/false);
1135}
1136
Ethan Nicholas5005a222018-08-24 13:06:27 -04001137// returns true if this statement definitely executes a break at the current level (we ignore
1138// nested loops and switches, since any breaks inside of them will merely break the loop / switch)
1139static bool contains_unconditional_break(Statement& s) {
1140 switch (s.fKind) {
1141 case Statement::kBlock_Kind:
John Stiles92219b42020-06-15 12:32:24 -04001142 for (const std::unique_ptr<Statement>& sub : static_cast<Block&>(s).fStatements) {
Ethan Nicholas5005a222018-08-24 13:06:27 -04001143 if (contains_unconditional_break(*sub)) {
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001144 return true;
1145 }
1146 }
1147 return false;
John Stiles92219b42020-06-15 12:32:24 -04001148
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001149 case Statement::kBreak_Kind:
1150 return true;
John Stiles92219b42020-06-15 12:32:24 -04001151
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001152 default:
1153 return false;
1154 }
1155}
1156
John Stiles92219b42020-06-15 12:32:24 -04001157static void move_all_but_break(std::unique_ptr<Statement>& stmt,
1158 std::vector<std::unique_ptr<Statement>>* target) {
Ethan Nicholas739e1ca2020-06-11 12:16:14 -04001159 switch (stmt->fKind) {
John Stiles92219b42020-06-15 12:32:24 -04001160 case Statement::kBlock_Kind: {
1161 // Recurse into the block.
1162 Block& block = static_cast<Block&>(*stmt);
1163
1164 std::vector<std::unique_ptr<Statement>> blockStmts;
1165 blockStmts.reserve(block.fStatements.size());
1166 for (std::unique_ptr<Statement>& statementInBlock : block.fStatements) {
1167 move_all_but_break(statementInBlock, &blockStmts);
Ethan Nicholas739e1ca2020-06-11 12:16:14 -04001168 }
John Stiles92219b42020-06-15 12:32:24 -04001169
1170 target->push_back(std::make_unique<Block>(block.fOffset, std::move(blockStmts),
1171 block.fSymbols, block.fIsScope));
Ethan Nicholas739e1ca2020-06-11 12:16:14 -04001172 break;
John Stiles92219b42020-06-15 12:32:24 -04001173 }
1174
Ethan Nicholas739e1ca2020-06-11 12:16:14 -04001175 case Statement::kBreak_Kind:
John Stiles92219b42020-06-15 12:32:24 -04001176 // Do not append a break to the target.
1177 break;
1178
Ethan Nicholas739e1ca2020-06-11 12:16:14 -04001179 default:
John Stiles92219b42020-06-15 12:32:24 -04001180 // Append normal statements to the target.
1181 target->push_back(std::move(stmt));
1182 break;
Ethan Nicholas739e1ca2020-06-11 12:16:14 -04001183 }
1184}
1185
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001186// Returns a block containing all of the statements that will be run if the given case matches
1187// (which, owing to the statements being owned by unique_ptrs, means the switch itself will be
1188// broken by this call and must then be discarded).
1189// Returns null (and leaves the switch unmodified) if no such simple reduction is possible, such as
1190// when break statements appear inside conditionals.
John Stiles92219b42020-06-15 12:32:24 -04001191static std::unique_ptr<Statement> block_for_case(SwitchStatement* switchStatement,
1192 SwitchCase* caseToCapture) {
1193 // We have to be careful to not move any of the pointers until after we're sure we're going to
1194 // succeed, so before we make any changes at all, we check the switch-cases to decide on a plan
1195 // of action. First, find the switch-case we are interested in.
1196 auto iter = switchStatement->fCases.begin();
1197 for (; iter != switchStatement->fCases.end(); ++iter) {
1198 if (iter->get() == caseToCapture) {
1199 break;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001200 }
John Stiles92219b42020-06-15 12:32:24 -04001201 }
1202
1203 // Next, walk forward through the rest of the switch. If we find a conditional break, we're
1204 // stuck and can't simplify at all. If we find an unconditional break, we have a range of
1205 // statements that we can use for simplification.
1206 auto startIter = iter;
1207 Statement* unconditionalBreakStmt = nullptr;
1208 for (; iter != switchStatement->fCases.end(); ++iter) {
1209 for (std::unique_ptr<Statement>& stmt : (*iter)->fStatements) {
1210 if (contains_conditional_break(*stmt)) {
1211 // We can't reduce switch-cases to a block when they have conditional breaks.
1212 return nullptr;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001213 }
John Stiles92219b42020-06-15 12:32:24 -04001214
1215 if (contains_unconditional_break(*stmt)) {
1216 // We found an unconditional break. We can use this block, but we need to strip
1217 // out the break statement.
1218 unconditionalBreakStmt = stmt.get();
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001219 break;
1220 }
1221 }
John Stiles92219b42020-06-15 12:32:24 -04001222
1223 if (unconditionalBreakStmt != nullptr) {
1224 break;
1225 }
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001226 }
John Stiles92219b42020-06-15 12:32:24 -04001227
1228 // We fell off the bottom of the switch or encountered a break. We know the range of statements
1229 // that we need to move over, and we know it's safe to do so.
1230 std::vector<std::unique_ptr<Statement>> caseStmts;
1231
1232 // We can move over most of the statements as-is.
1233 while (startIter != iter) {
1234 for (std::unique_ptr<Statement>& stmt : (*startIter)->fStatements) {
1235 caseStmts.push_back(std::move(stmt));
1236 }
1237 ++startIter;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001238 }
John Stiles92219b42020-06-15 12:32:24 -04001239
1240 // If we found an unconditional break at the end, we need to move what we can while avoiding
1241 // that break.
1242 if (unconditionalBreakStmt != nullptr) {
1243 for (std::unique_ptr<Statement>& stmt : (*startIter)->fStatements) {
1244 if (stmt.get() == unconditionalBreakStmt) {
1245 move_all_but_break(stmt, &caseStmts);
1246 unconditionalBreakStmt = nullptr;
1247 break;
1248 }
1249
1250 caseStmts.push_back(std::move(stmt));
1251 }
1252 }
1253
1254 SkASSERT(unconditionalBreakStmt == nullptr); // Verify that we fixed the unconditional break.
1255
1256 // Return our newly-synthesized block.
1257 return std::make_unique<Block>(/*offset=*/-1, std::move(caseStmts), switchStatement->fSymbols);
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001258}
1259
Ethan Nicholascb670962017-04-20 19:31:52 -04001260void Compiler::simplifyStatement(DefinitionMap& definitions,
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001261 BasicBlock& b,
1262 std::vector<BasicBlock::Node>::iterator* iter,
1263 std::unordered_set<const Variable*>* undefinedVariables,
1264 bool* outUpdated,
1265 bool* outNeedsRescan) {
Ethan Nicholascb670962017-04-20 19:31:52 -04001266 Statement* stmt = (*iter)->statement()->get();
1267 switch (stmt->fKind) {
Ethan Nicholas82a62d22017-11-07 14:42:10 +00001268 case Statement::kVarDeclaration_Kind: {
1269 const auto& varDecl = (VarDeclaration&) *stmt;
1270 if (varDecl.fVar->dead() &&
1271 (!varDecl.fValue ||
1272 !varDecl.fValue->hasSideEffects())) {
1273 if (varDecl.fValue) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001274 SkASSERT((*iter)->statement()->get() == stmt);
Ethan Nicholas82a62d22017-11-07 14:42:10 +00001275 if (!b.tryRemoveExpressionBefore(iter, varDecl.fValue.get())) {
1276 *outNeedsRescan = true;
Ethan Nicholascb670962017-04-20 19:31:52 -04001277 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001278 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001279 (*iter)->setStatement(std::unique_ptr<Statement>(new Nop()));
Ethan Nicholas82a62d22017-11-07 14:42:10 +00001280 *outUpdated = true;
Ethan Nicholascb670962017-04-20 19:31:52 -04001281 }
1282 break;
1283 }
1284 case Statement::kIf_Kind: {
1285 IfStatement& i = (IfStatement&) *stmt;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001286 if (i.fTest->fKind == Expression::kBoolLiteral_Kind) {
1287 // constant if, collapse down to a single branch
1288 if (((BoolLiteral&) *i.fTest).fValue) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001289 SkASSERT(i.fIfTrue);
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001290 (*iter)->setStatement(std::move(i.fIfTrue));
1291 } else {
1292 if (i.fIfFalse) {
1293 (*iter)->setStatement(std::move(i.fIfFalse));
1294 } else {
1295 (*iter)->setStatement(std::unique_ptr<Statement>(new Nop()));
1296 }
1297 }
1298 *outUpdated = true;
1299 *outNeedsRescan = true;
1300 break;
1301 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001302 if (i.fIfFalse && i.fIfFalse->isEmpty()) {
1303 // else block doesn't do anything, remove it
1304 i.fIfFalse.reset();
1305 *outUpdated = true;
1306 *outNeedsRescan = true;
1307 }
1308 if (!i.fIfFalse && i.fIfTrue->isEmpty()) {
1309 // if block doesn't do anything, no else block
1310 if (i.fTest->hasSideEffects()) {
1311 // test has side effects, keep it
1312 (*iter)->setStatement(std::unique_ptr<Statement>(
1313 new ExpressionStatement(std::move(i.fTest))));
1314 } else {
1315 // no if, no else, no test side effects, kill the whole if
1316 // statement
1317 (*iter)->setStatement(std::unique_ptr<Statement>(new Nop()));
1318 }
1319 *outUpdated = true;
1320 *outNeedsRescan = true;
1321 }
1322 break;
1323 }
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001324 case Statement::kSwitch_Kind: {
1325 SwitchStatement& s = (SwitchStatement&) *stmt;
Brian Osmanb6b95732020-06-30 11:44:27 -04001326 if (s.fValue->isCompileTimeConstant()) {
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001327 // switch is constant, replace it with the case that matches
1328 bool found = false;
1329 SwitchCase* defaultCase = nullptr;
1330 for (const auto& c : s.fCases) {
1331 if (!c->fValue) {
1332 defaultCase = c.get();
1333 continue;
1334 }
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001335 SkASSERT(c->fValue->fKind == s.fValue->fKind);
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001336 found = c->fValue->compareConstant(*fContext, *s.fValue);
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001337 if (found) {
1338 std::unique_ptr<Statement> newBlock = block_for_case(&s, c.get());
1339 if (newBlock) {
1340 (*iter)->setStatement(std::move(newBlock));
1341 break;
1342 } else {
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -04001343 if (s.fIsStatic && !(fFlags & kPermitInvalidStaticTests_Flag)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001344 this->error(s.fOffset,
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001345 "static switch contains non-static conditional break");
1346 s.fIsStatic = false;
1347 }
1348 return; // can't simplify
1349 }
1350 }
1351 }
1352 if (!found) {
1353 // no matching case. use default if it exists, or kill the whole thing
1354 if (defaultCase) {
1355 std::unique_ptr<Statement> newBlock = block_for_case(&s, defaultCase);
1356 if (newBlock) {
1357 (*iter)->setStatement(std::move(newBlock));
1358 } else {
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -04001359 if (s.fIsStatic && !(fFlags & kPermitInvalidStaticTests_Flag)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001360 this->error(s.fOffset,
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001361 "static switch contains non-static conditional break");
1362 s.fIsStatic = false;
1363 }
1364 return; // can't simplify
1365 }
1366 } else {
1367 (*iter)->setStatement(std::unique_ptr<Statement>(new Nop()));
1368 }
1369 }
1370 *outUpdated = true;
1371 *outNeedsRescan = true;
1372 }
1373 break;
1374 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001375 case Statement::kExpression_Kind: {
1376 ExpressionStatement& e = (ExpressionStatement&) *stmt;
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001377 SkASSERT((*iter)->statement()->get() == &e);
Ethan Nicholascb670962017-04-20 19:31:52 -04001378 if (!e.fExpression->hasSideEffects()) {
1379 // Expression statement with no side effects, kill it
1380 if (!b.tryRemoveExpressionBefore(iter, e.fExpression.get())) {
1381 *outNeedsRescan = true;
1382 }
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001383 SkASSERT((*iter)->statement()->get() == stmt);
Ethan Nicholascb670962017-04-20 19:31:52 -04001384 (*iter)->setStatement(std::unique_ptr<Statement>(new Nop()));
1385 *outUpdated = true;
1386 }
1387 break;
1388 }
1389 default:
1390 break;
1391 }
1392}
1393
1394void Compiler::scanCFG(FunctionDefinition& f) {
1395 CFG cfg = CFGGenerator().getCFG(f);
1396 this->computeDataFlow(&cfg);
ethannicholas22f939e2016-10-13 13:25:34 -07001397
1398 // check for unreachable code
1399 for (size_t i = 0; i < cfg.fBlocks.size(); i++) {
Mike Klein6ad99092016-10-26 10:35:22 -04001400 if (i != cfg.fStart && !cfg.fBlocks[i].fEntrances.size() &&
ethannicholas22f939e2016-10-13 13:25:34 -07001401 cfg.fBlocks[i].fNodes.size()) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001402 int offset;
Ethan Nicholas86a43402017-01-19 13:32:00 -05001403 switch (cfg.fBlocks[i].fNodes[0].fKind) {
1404 case BasicBlock::Node::kStatement_Kind:
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001405 offset = (*cfg.fBlocks[i].fNodes[0].statement())->fOffset;
Ethan Nicholas86a43402017-01-19 13:32:00 -05001406 break;
1407 case BasicBlock::Node::kExpression_Kind:
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001408 offset = (*cfg.fBlocks[i].fNodes[0].expression())->fOffset;
Ethan Nicholas70728ef2020-05-28 07:09:00 -04001409 if ((*cfg.fBlocks[i].fNodes[0].expression())->fKind ==
1410 Expression::kBoolLiteral_Kind) {
1411 // Function inlining can generate do { ... } while(false) loops which always
1412 // break, so the boolean condition is considered unreachable. Since not
1413 // being able to reach a literal is a non-issue in the first place, we
1414 // don't report an error in this case.
1415 continue;
1416 }
Ethan Nicholas86a43402017-01-19 13:32:00 -05001417 break;
1418 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001419 this->error(offset, String("unreachable"));
ethannicholas22f939e2016-10-13 13:25:34 -07001420 }
1421 }
1422 if (fErrorCount) {
1423 return;
1424 }
1425
Ethan Nicholascb670962017-04-20 19:31:52 -04001426 // check for dead code & undefined variables, perform constant propagation
1427 std::unordered_set<const Variable*> undefinedVariables;
1428 bool updated;
1429 bool needsRescan = false;
1430 do {
1431 if (needsRescan) {
1432 cfg = CFGGenerator().getCFG(f);
1433 this->computeDataFlow(&cfg);
1434 needsRescan = false;
Ethan Nicholas113628d2017-02-02 16:11:39 -05001435 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001436
1437 updated = false;
Ethan Nicholas1de14812020-06-19 15:32:49 -04001438 bool first = true;
Ethan Nicholascb670962017-04-20 19:31:52 -04001439 for (BasicBlock& b : cfg.fBlocks) {
Ethan Nicholas1de14812020-06-19 15:32:49 -04001440 if (!first && b.fEntrances.empty()) {
1441 // Block was reachable before optimization, but has since become unreachable. In
1442 // addition to being dead code, it's broken - since control flow can't reach it, no
1443 // prior variable definitions can reach it, and therefore variables might look to
1444 // have not been properly assigned. Kill it.
1445 for (BasicBlock::Node& node : b.fNodes) {
1446 if (node.fKind == BasicBlock::Node::kStatement_Kind &&
1447 (*node.statement())->fKind != Statement::kNop_Kind) {
1448 node.setStatement(std::unique_ptr<Statement>(new Nop()));
1449 }
1450 }
1451 continue;
1452 }
1453 first = false;
Ethan Nicholascb670962017-04-20 19:31:52 -04001454 DefinitionMap definitions = b.fBefore;
1455
1456 for (auto iter = b.fNodes.begin(); iter != b.fNodes.end() && !needsRescan; ++iter) {
1457 if (iter->fKind == BasicBlock::Node::kExpression_Kind) {
1458 this->simplifyExpression(definitions, b, &iter, &undefinedVariables, &updated,
1459 &needsRescan);
1460 } else {
1461 this->simplifyStatement(definitions, b, &iter, &undefinedVariables, &updated,
1462 &needsRescan);
1463 }
Ethan Nicholas4b330df2017-05-17 10:52:55 -04001464 if (needsRescan) {
1465 break;
1466 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001467 this->addDefinitions(*iter, &definitions);
1468 }
1469 }
1470 } while (updated);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001471 SkASSERT(!needsRescan);
ethannicholas22f939e2016-10-13 13:25:34 -07001472
Ethan Nicholas91a10532017-06-22 11:24:38 -04001473 // verify static ifs & switches, clean up dead variable decls
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001474 for (BasicBlock& b : cfg.fBlocks) {
1475 DefinitionMap definitions = b.fBefore;
1476
Ethan Nicholas91a10532017-06-22 11:24:38 -04001477 for (auto iter = b.fNodes.begin(); iter != b.fNodes.end() && !needsRescan;) {
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001478 if (iter->fKind == BasicBlock::Node::kStatement_Kind) {
1479 const Statement& s = **iter->statement();
1480 switch (s.fKind) {
1481 case Statement::kIf_Kind:
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -04001482 if (((const IfStatement&) s).fIsStatic &&
1483 !(fFlags & kPermitInvalidStaticTests_Flag)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001484 this->error(s.fOffset, "static if has non-static test");
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001485 }
Ethan Nicholas91a10532017-06-22 11:24:38 -04001486 ++iter;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001487 break;
1488 case Statement::kSwitch_Kind:
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -04001489 if (((const SwitchStatement&) s).fIsStatic &&
1490 !(fFlags & kPermitInvalidStaticTests_Flag)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001491 this->error(s.fOffset, "static switch has non-static test");
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001492 }
Ethan Nicholas91a10532017-06-22 11:24:38 -04001493 ++iter;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001494 break;
Ethan Nicholas82a62d22017-11-07 14:42:10 +00001495 case Statement::kVarDeclarations_Kind: {
1496 VarDeclarations& decls = *((VarDeclarationsStatement&) s).fDeclaration;
1497 for (auto varIter = decls.fVars.begin(); varIter != decls.fVars.end();) {
1498 if ((*varIter)->fKind == Statement::kNop_Kind) {
1499 varIter = decls.fVars.erase(varIter);
1500 } else {
1501 ++varIter;
1502 }
1503 }
1504 if (!decls.fVars.size()) {
1505 iter = b.fNodes.erase(iter);
1506 } else {
1507 ++iter;
1508 }
1509 break;
1510 }
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001511 default:
Ethan Nicholas91a10532017-06-22 11:24:38 -04001512 ++iter;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001513 break;
1514 }
Ethan Nicholas91a10532017-06-22 11:24:38 -04001515 } else {
1516 ++iter;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001517 }
1518 }
1519 }
1520
ethannicholas22f939e2016-10-13 13:25:34 -07001521 // check for missing return
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001522 if (f.fDeclaration.fReturnType != *fContext->fVoid_Type) {
ethannicholas22f939e2016-10-13 13:25:34 -07001523 if (cfg.fBlocks[cfg.fExit].fEntrances.size()) {
Ethan Nicholasdb80f692019-11-22 14:06:12 -05001524 this->error(f.fOffset, String("function '" + String(f.fDeclaration.fName) +
1525 "' can exit without returning a value"));
ethannicholas22f939e2016-10-13 13:25:34 -07001526 }
1527 }
1528}
1529
Ethan Nicholas91164d12019-05-15 15:29:54 -04001530void Compiler::registerExternalValue(ExternalValue* value) {
1531 fIRGenerator->fRootSymbolTable->addWithoutOwnership(value->fName, value);
1532}
1533
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001534const Symbol* Compiler::takeOwnership(std::unique_ptr<const Symbol> symbol) {
Ethan Nicholas91164d12019-05-15 15:29:54 -04001535 return fIRGenerator->fRootSymbolTable->takeOwnership(std::move(symbol));
1536}
1537
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001538std::unique_ptr<Program> Compiler::convertProgram(Program::Kind kind, String text,
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001539 const Program::Settings& settings) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001540 fErrorText = "";
1541 fErrorCount = 0;
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001542 std::vector<std::unique_ptr<ProgramElement>>* inherited;
ethannicholasd598f792016-07-25 10:08:54 -07001543 std::vector<std::unique_ptr<ProgramElement>> elements;
ethannicholasb3058bd2016-07-01 08:22:01 -07001544 switch (kind) {
1545 case Program::kVertex_Kind:
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001546 inherited = &fVertexInclude;
1547 fIRGenerator->fSymbolTable = fVertexSymbolTable;
Ethan Nicholasdb80f692019-11-22 14:06:12 -05001548 fIRGenerator->fIntrinsics = &fGPUIntrinsics;
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001549 fIRGenerator->start(&settings, inherited);
ethannicholasb3058bd2016-07-01 08:22:01 -07001550 break;
1551 case Program::kFragment_Kind:
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001552 inherited = &fFragmentInclude;
1553 fIRGenerator->fSymbolTable = fFragmentSymbolTable;
Ethan Nicholasdb80f692019-11-22 14:06:12 -05001554 fIRGenerator->fIntrinsics = &fGPUIntrinsics;
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001555 fIRGenerator->start(&settings, inherited);
ethannicholasb3058bd2016-07-01 08:22:01 -07001556 break;
Ethan Nicholas52cad152017-02-16 16:37:32 -05001557 case Program::kGeometry_Kind:
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001558 this->loadGeometryIntrinsics();
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001559 inherited = &fGeometryInclude;
1560 fIRGenerator->fSymbolTable = fGeometrySymbolTable;
Ethan Nicholasdb80f692019-11-22 14:06:12 -05001561 fIRGenerator->fIntrinsics = &fGPUIntrinsics;
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001562 fIRGenerator->start(&settings, inherited);
Ethan Nicholas52cad152017-02-16 16:37:32 -05001563 break;
Ethan Nicholas762466e2017-06-29 10:03:38 -04001564 case Program::kFragmentProcessor_Kind:
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001565#if REHYDRATE
1566 {
1567 Rehydrator rehydrator(fContext.get(), fGpuSymbolTable, this,
1568 SKSL_INCLUDE_sksl_fp,
1569 SKSL_INCLUDE_sksl_fp_LENGTH);
1570 fFPSymbolTable = rehydrator.symbolTable();
1571 fFPInclude = rehydrator.elements();
1572 }
1573 inherited = &fFPInclude;
1574 fIRGenerator->fSymbolTable = fFPSymbolTable;
1575 fIRGenerator->fIntrinsics = &fGPUIntrinsics;
1576 fIRGenerator->start(&settings, inherited);
1577 break;
1578#else
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001579 inherited = nullptr;
Ethan Nicholas8da1e652019-05-24 11:01:59 -04001580 fIRGenerator->fSymbolTable = fGpuSymbolTable;
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001581 fIRGenerator->start(&settings, nullptr, true);
Ethan Nicholasdb80f692019-11-22 14:06:12 -05001582 fIRGenerator->fIntrinsics = &fGPUIntrinsics;
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001583 fIRGenerator->convertProgram(kind, SKSL_FP_INCLUDE, strlen(SKSL_FP_INCLUDE), &elements);
1584 fIRGenerator->fIsBuiltinCode = false;
Ethan Nicholas762466e2017-06-29 10:03:38 -04001585 break;
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001586#endif
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);
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001605 auto result = std::unique_ptr<Program>(new Program(kind,
1606 std::move(textPtr),
1607 settings,
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001608 fContext,
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001609 inherited,
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001610 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
1668std::unique_ptr<Program> Compiler::specialize(
1669 Program& program,
1670 const std::unordered_map<SkSL::String, SkSL::Program::Settings::Value>& inputs) {
1671 std::vector<std::unique_ptr<ProgramElement>> elements;
1672 for (const auto& e : program) {
1673 elements.push_back(e.clone());
1674 }
1675 Program::Settings settings;
1676 settings.fCaps = program.fSettings.fCaps;
1677 for (auto iter = inputs.begin(); iter != inputs.end(); ++iter) {
1678 settings.fArgs.insert(*iter);
1679 }
Brian Osman808f0212020-01-21 15:36:47 -05001680 std::unique_ptr<String> sourceCopy(new String(*program.fSource));
Ethan Nicholas00543112018-07-31 09:44:36 -04001681 std::unique_ptr<Program> result(new Program(program.fKind,
Brian Osman808f0212020-01-21 15:36:47 -05001682 std::move(sourceCopy),
Ethan Nicholas00543112018-07-31 09:44:36 -04001683 settings,
1684 program.fContext,
1685 program.fInheritedElements,
1686 std::move(elements),
1687 program.fSymbols,
1688 program.fInputs));
1689 return result;
1690}
1691
Brian Osmanfb32ddf2019-06-18 10:14:20 -04001692#if defined(SKSL_STANDALONE) || SK_SUPPORT_GPU
1693
Ethan Nicholas00543112018-07-31 09:44:36 -04001694bool Compiler::toSPIRV(Program& program, OutputStream& out) {
1695 if (!this->optimize(program)) {
1696 return false;
1697 }
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001698#ifdef SK_ENABLE_SPIRV_VALIDATION
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001699 StringStream buffer;
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001700 fSource = program.fSource.get();
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001701 SPIRVCodeGenerator cg(fContext.get(), &program, this, &buffer);
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001702 bool result = cg.generateCode();
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001703 fSource = nullptr;
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001704 if (result) {
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001705 spvtools::SpirvTools tools(SPV_ENV_VULKAN_1_0);
Ethan Nicholas762466e2017-06-29 10:03:38 -04001706 const String& data = buffer.str();
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001707 SkASSERT(0 == data.size() % 4);
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001708 auto dumpmsg = [](spv_message_level_t, const char*, const spv_position_t&, const char* m) {
1709 SkDebugf("SPIR-V validation error: %s\n", m);
1710 };
1711 tools.SetMessageConsumer(dumpmsg);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001712 // Verify that the SPIR-V we produced is valid. If this SkASSERT fails, check the logs prior
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001713 // to the failure to see the validation errors.
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001714 SkAssertResult(tools.Validate((const uint32_t*) data.c_str(), data.size() / 4));
Ethan Nicholas762466e2017-06-29 10:03:38 -04001715 out.write(data.c_str(), data.size());
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001716 }
1717#else
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001718 fSource = program.fSource.get();
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001719 SPIRVCodeGenerator cg(fContext.get(), &program, this, &out);
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001720 bool result = cg.generateCode();
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001721 fSource = nullptr;
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001722#endif
Ethan Nicholasce33f102016-12-09 17:22:59 -05001723 return result;
1724}
1725
Ethan Nicholas00543112018-07-31 09:44:36 -04001726bool Compiler::toSPIRV(Program& program, String* out) {
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001727 StringStream buffer;
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001728 bool result = this->toSPIRV(program, buffer);
1729 if (result) {
Ethan Nicholas762466e2017-06-29 10:03:38 -04001730 *out = buffer.str();
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001731 }
1732 return result;
1733}
1734
Ethan Nicholas00543112018-07-31 09:44:36 -04001735bool Compiler::toGLSL(Program& program, OutputStream& out) {
1736 if (!this->optimize(program)) {
1737 return false;
1738 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001739 fSource = program.fSource.get();
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001740 GLSLCodeGenerator cg(fContext.get(), &program, this, &out);
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001741 bool result = cg.generateCode();
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001742 fSource = nullptr;
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001743 return result;
1744}
1745
Ethan Nicholas00543112018-07-31 09:44:36 -04001746bool Compiler::toGLSL(Program& program, String* out) {
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001747 StringStream buffer;
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001748 bool result = this->toGLSL(program, buffer);
1749 if (result) {
Ethan Nicholas762466e2017-06-29 10:03:38 -04001750 *out = buffer.str();
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001751 }
1752 return result;
1753}
1754
Brian Osmanc0243912020-02-19 15:35:26 -05001755bool Compiler::toHLSL(Program& program, String* out) {
1756 String spirv;
1757 if (!this->toSPIRV(program, &spirv)) {
1758 return false;
1759 }
1760
1761 return SPIRVtoHLSL(spirv, out);
1762}
1763
Ethan Nicholas00543112018-07-31 09:44:36 -04001764bool Compiler::toMetal(Program& program, OutputStream& out) {
1765 if (!this->optimize(program)) {
1766 return false;
1767 }
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001768 MetalCodeGenerator cg(fContext.get(), &program, this, &out);
Ethan Nicholascc305772017-10-13 16:17:45 -04001769 bool result = cg.generateCode();
Ethan Nicholascc305772017-10-13 16:17:45 -04001770 return result;
1771}
1772
Ethan Nicholas00543112018-07-31 09:44:36 -04001773bool Compiler::toMetal(Program& program, String* out) {
1774 if (!this->optimize(program)) {
1775 return false;
1776 }
Timothy Liangb8eeb802018-07-23 16:46:16 -04001777 StringStream buffer;
1778 bool result = this->toMetal(program, buffer);
1779 if (result) {
1780 *out = buffer.str();
1781 }
1782 return result;
1783}
1784
Ethan Nicholas00543112018-07-31 09:44:36 -04001785bool Compiler::toCPP(Program& program, String name, OutputStream& out) {
1786 if (!this->optimize(program)) {
1787 return false;
1788 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001789 fSource = program.fSource.get();
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001790 CPPCodeGenerator cg(fContext.get(), &program, this, name, &out);
Ethan Nicholas762466e2017-06-29 10:03:38 -04001791 bool result = cg.generateCode();
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001792 fSource = nullptr;
Ethan Nicholas762466e2017-06-29 10:03:38 -04001793 return result;
1794}
1795
Ethan Nicholas00543112018-07-31 09:44:36 -04001796bool Compiler::toH(Program& program, String name, OutputStream& out) {
1797 if (!this->optimize(program)) {
1798 return false;
1799 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001800 fSource = program.fSource.get();
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001801 HCodeGenerator cg(fContext.get(), &program, this, name, &out);
Ethan Nicholas762466e2017-06-29 10:03:38 -04001802 bool result = cg.generateCode();
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001803 fSource = nullptr;
Ethan Nicholas00543112018-07-31 09:44:36 -04001804 return result;
1805}
1806
Brian Osman2e29ab52019-09-20 12:19:11 -04001807#endif
1808
1809#if !defined(SKSL_STANDALONE) && SK_SUPPORT_GPU
Brian Osman107c6662019-12-30 15:02:30 -05001810bool Compiler::toPipelineStage(const Program& program, PipelineStageArgs* outArgs) {
Ethan Nicholas00543112018-07-31 09:44:36 -04001811 SkASSERT(program.fIsOptimized);
1812 fSource = program.fSource.get();
1813 StringStream buffer;
Brian Osman300fe1d2020-01-23 15:42:43 -05001814 PipelineStageCodeGenerator cg(fContext.get(), &program, this, &buffer, outArgs);
Ethan Nicholas00543112018-07-31 09:44:36 -04001815 bool result = cg.generateCode();
1816 fSource = nullptr;
1817 if (result) {
Brian Osman107c6662019-12-30 15:02:30 -05001818 outArgs->fCode = buffer.str();
Ethan Nicholas00543112018-07-31 09:44:36 -04001819 }
Ethan Nicholas762466e2017-06-29 10:03:38 -04001820 return result;
1821}
Brian Osmanfb32ddf2019-06-18 10:14:20 -04001822#endif
1823
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001824std::unique_ptr<ByteCode> Compiler::toByteCode(Program& program) {
Brian Osman489cf882019-07-09 10:48:28 -04001825#if defined(SK_ENABLE_SKSL_INTERPRETER)
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001826 if (!this->optimize(program)) {
1827 return nullptr;
1828 }
Brian Osman808f0212020-01-21 15:36:47 -05001829 fSource = program.fSource.get();
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001830 std::unique_ptr<ByteCode> result(new ByteCode());
Brian Osmanb08cc022020-04-02 11:38:40 -04001831 ByteCodeGenerator cg(fContext.get(), &program, this, result.get());
1832 bool success = cg.generateCode();
1833 fSource = nullptr;
1834 if (success) {
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001835 return result;
1836 }
Brian Osman489cf882019-07-09 10:48:28 -04001837#else
1838 ABORT("ByteCode interpreter not enabled");
1839#endif
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001840 return nullptr;
1841}
1842
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001843const char* Compiler::OperatorName(Token::Kind kind) {
1844 switch (kind) {
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001845 case Token::Kind::TK_PLUS: return "+";
1846 case Token::Kind::TK_MINUS: return "-";
1847 case Token::Kind::TK_STAR: return "*";
1848 case Token::Kind::TK_SLASH: return "/";
1849 case Token::Kind::TK_PERCENT: return "%";
1850 case Token::Kind::TK_SHL: return "<<";
1851 case Token::Kind::TK_SHR: return ">>";
1852 case Token::Kind::TK_LOGICALNOT: return "!";
1853 case Token::Kind::TK_LOGICALAND: return "&&";
1854 case Token::Kind::TK_LOGICALOR: return "||";
1855 case Token::Kind::TK_LOGICALXOR: return "^^";
1856 case Token::Kind::TK_BITWISENOT: return "~";
1857 case Token::Kind::TK_BITWISEAND: return "&";
1858 case Token::Kind::TK_BITWISEOR: return "|";
1859 case Token::Kind::TK_BITWISEXOR: return "^";
1860 case Token::Kind::TK_EQ: return "=";
1861 case Token::Kind::TK_EQEQ: return "==";
1862 case Token::Kind::TK_NEQ: return "!=";
1863 case Token::Kind::TK_LT: return "<";
1864 case Token::Kind::TK_GT: return ">";
1865 case Token::Kind::TK_LTEQ: return "<=";
1866 case Token::Kind::TK_GTEQ: return ">=";
1867 case Token::Kind::TK_PLUSEQ: return "+=";
1868 case Token::Kind::TK_MINUSEQ: return "-=";
1869 case Token::Kind::TK_STAREQ: return "*=";
1870 case Token::Kind::TK_SLASHEQ: return "/=";
1871 case Token::Kind::TK_PERCENTEQ: return "%=";
1872 case Token::Kind::TK_SHLEQ: return "<<=";
1873 case Token::Kind::TK_SHREQ: return ">>=";
1874 case Token::Kind::TK_LOGICALANDEQ: return "&&=";
1875 case Token::Kind::TK_LOGICALOREQ: return "||=";
1876 case Token::Kind::TK_LOGICALXOREQ: return "^^=";
1877 case Token::Kind::TK_BITWISEANDEQ: return "&=";
1878 case Token::Kind::TK_BITWISEOREQ: return "|=";
1879 case Token::Kind::TK_BITWISEXOREQ: return "^=";
1880 case Token::Kind::TK_PLUSPLUS: return "++";
1881 case Token::Kind::TK_MINUSMINUS: return "--";
1882 case Token::Kind::TK_COMMA: return ",";
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001883 default:
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001884 ABORT("unsupported operator: %d\n", (int) kind);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001885 }
1886}
1887
1888
1889bool Compiler::IsAssignment(Token::Kind op) {
1890 switch (op) {
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001891 case Token::Kind::TK_EQ: // fall through
1892 case Token::Kind::TK_PLUSEQ: // fall through
1893 case Token::Kind::TK_MINUSEQ: // fall through
1894 case Token::Kind::TK_STAREQ: // fall through
1895 case Token::Kind::TK_SLASHEQ: // fall through
1896 case Token::Kind::TK_PERCENTEQ: // fall through
1897 case Token::Kind::TK_SHLEQ: // fall through
1898 case Token::Kind::TK_SHREQ: // fall through
1899 case Token::Kind::TK_BITWISEOREQ: // fall through
1900 case Token::Kind::TK_BITWISEXOREQ: // fall through
1901 case Token::Kind::TK_BITWISEANDEQ: // fall through
1902 case Token::Kind::TK_LOGICALOREQ: // fall through
1903 case Token::Kind::TK_LOGICALXOREQ: // fall through
1904 case Token::Kind::TK_LOGICALANDEQ:
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001905 return true;
1906 default:
1907 return false;
1908 }
1909}
1910
1911Position Compiler::position(int offset) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001912 SkASSERT(fSource);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001913 int line = 1;
1914 int column = 1;
1915 for (int i = 0; i < offset; i++) {
1916 if ((*fSource)[i] == '\n') {
1917 ++line;
1918 column = 1;
1919 }
1920 else {
1921 ++column;
1922 }
1923 }
1924 return Position(line, column);
1925}
1926
1927void Compiler::error(int offset, String msg) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001928 fErrorCount++;
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001929 Position pos = this->position(offset);
1930 fErrorText += "error: " + to_string(pos.fLine) + ": " + msg.c_str() + "\n";
ethannicholasb3058bd2016-07-01 08:22:01 -07001931}
1932
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001933String Compiler::errorText() {
Ethan Nicholas00543112018-07-31 09:44:36 -04001934 this->writeErrorCount();
1935 fErrorCount = 0;
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001936 String result = fErrorText;
ethannicholasb3058bd2016-07-01 08:22:01 -07001937 return result;
1938}
1939
1940void Compiler::writeErrorCount() {
1941 if (fErrorCount) {
1942 fErrorText += to_string(fErrorCount) + " error";
1943 if (fErrorCount > 1) {
1944 fErrorText += "s";
1945 }
1946 fErrorText += "\n";
1947 }
1948}
1949
ethannicholasb3058bd2016-07-01 08:22:01 -07001950} // namespace