blob: ba1221ce929d263606f2a3132c5916edb8ba19c5 [file] [log] [blame]
ethannicholasb3058bd2016-07-01 08:22:01 -07001/*
2 * Copyright 2016 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
Mike Klein6ad99092016-10-26 10:35:22 -04007
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/sksl/SkSLCompiler.h"
ethannicholasb3058bd2016-07-01 08:22:01 -07009
John Stilesfbd050b2020-08-03 13:21:46 -040010#include <memory>
11
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "src/sksl/SkSLByteCodeGenerator.h"
13#include "src/sksl/SkSLCFGGenerator.h"
14#include "src/sksl/SkSLCPPCodeGenerator.h"
15#include "src/sksl/SkSLGLSLCodeGenerator.h"
16#include "src/sksl/SkSLHCodeGenerator.h"
17#include "src/sksl/SkSLIRGenerator.h"
18#include "src/sksl/SkSLMetalCodeGenerator.h"
19#include "src/sksl/SkSLPipelineStageCodeGenerator.h"
Ethan Nicholasc18bb512020-07-28 14:46:53 -040020#include "src/sksl/SkSLRehydrator.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "src/sksl/SkSLSPIRVCodeGenerator.h"
Brian Osmanc0243912020-02-19 15:35:26 -050022#include "src/sksl/SkSLSPIRVtoHLSL.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "src/sksl/ir/SkSLEnum.h"
24#include "src/sksl/ir/SkSLExpression.h"
25#include "src/sksl/ir/SkSLExpressionStatement.h"
26#include "src/sksl/ir/SkSLFunctionCall.h"
27#include "src/sksl/ir/SkSLIntLiteral.h"
28#include "src/sksl/ir/SkSLModifiersDeclaration.h"
29#include "src/sksl/ir/SkSLNop.h"
30#include "src/sksl/ir/SkSLSymbolTable.h"
31#include "src/sksl/ir/SkSLTernaryExpression.h"
32#include "src/sksl/ir/SkSLUnresolvedFunction.h"
33#include "src/sksl/ir/SkSLVarDeclarations.h"
ethannicholasb3058bd2016-07-01 08:22:01 -070034
Ethan Nicholasa11035b2019-11-26 16:27:47 -050035#if !defined(SKSL_STANDALONE) & SK_SUPPORT_GPU
36#include "include/gpu/GrContextOptions.h"
37#include "src/gpu/GrShaderCaps.h"
38#endif
39
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -040040#ifdef SK_ENABLE_SPIRV_VALIDATION
41#include "spirv-tools/libspirv.hpp"
42#endif
43
Ethan Nicholasc18bb512020-07-28 14:46:53 -040044// If true, we use a compact binary IR representation of the core include files; otherwise we parse
45// the actual source code for the include files at runtime. The main reason you would need to change
46// this is to make format changes easier: set it to 0, change the encoder and decoder as needed,
47// build Skia to regenerate the encoded files, then set this back to 1 to actually use the
48// newly-generated files.
49#define REHYDRATE 1
ethannicholasb3058bd2016-07-01 08:22:01 -070050
Ethan Nicholasc18bb512020-07-28 14:46:53 -040051#if REHYDRATE
52
53#include "src/sksl/generated/sksl_fp.dehydrated.sksl"
54#include "src/sksl/generated/sksl_frag.dehydrated.sksl"
55#include "src/sksl/generated/sksl_geom.dehydrated.sksl"
56#include "src/sksl/generated/sksl_gpu.dehydrated.sksl"
57#include "src/sksl/generated/sksl_interp.dehydrated.sksl"
58#include "src/sksl/generated/sksl_pipeline.dehydrated.sksl"
59#include "src/sksl/generated/sksl_vert.dehydrated.sksl"
60
61#else
62
63#warning SkSL rehydrator is disabled
Ethan Nicholas79707652017-11-16 11:20:11 -050064
Ethan Nicholas8da1e652019-05-24 11:01:59 -040065static const char* SKSL_GPU_INCLUDE =
Ethan Nicholasc18bb512020-07-28 14:46:53 -040066#include "src/sksl/generated/sksl_gpu.c.inc"
Ethan Nicholas8da1e652019-05-24 11:01:59 -040067static const char* SKSL_INTERP_INCLUDE =
Ethan Nicholasc18bb512020-07-28 14:46:53 -040068#include "src/sksl/generated/sksl_interp.c.inc"
ethannicholas5961bc92016-10-12 06:39:56 -070069static const char* SKSL_VERT_INCLUDE =
Ethan Nicholasc18bb512020-07-28 14:46:53 -040070#include "src/sksl/generated/sksl_vert.c.inc"
ethannicholas5961bc92016-10-12 06:39:56 -070071static const char* SKSL_FRAG_INCLUDE =
Ethan Nicholasc18bb512020-07-28 14:46:53 -040072#include "src/sksl/generated/sksl_frag.c.inc"
Ethan Nicholas52cad152017-02-16 16:37:32 -050073static const char* SKSL_GEOM_INCLUDE =
Ethan Nicholasc18bb512020-07-28 14:46:53 -040074#include "src/sksl/generated/sksl_geom.c.inc"
Ethan Nicholas762466e2017-06-29 10:03:38 -040075static const char* SKSL_FP_INCLUDE =
Ethan Nicholasc18bb512020-07-28 14:46:53 -040076#include "src/sksl/generated/sksl_fp.c.inc"
Ethan Nicholas8da1e652019-05-24 11:01:59 -040077static const char* SKSL_PIPELINE_INCLUDE =
Ethan Nicholasc18bb512020-07-28 14:46:53 -040078#include "src/sksl/generated/sksl_pipeline.c.inc"
79
80#endif
Ethan Nicholas0d997662019-04-08 09:46:01 -040081
ethannicholasb3058bd2016-07-01 08:22:01 -070082namespace SkSL {
83
Ethan Nicholasdb80f692019-11-22 14:06:12 -050084static void grab_intrinsics(std::vector<std::unique_ptr<ProgramElement>>* src,
Brian Osman08f986d2020-05-13 17:06:46 -040085 std::map<String, std::pair<std::unique_ptr<ProgramElement>, bool>>* target) {
86 for (auto iter = src->begin(); iter != src->end(); ) {
87 std::unique_ptr<ProgramElement>& element = *iter;
Ethan Nicholasdb80f692019-11-22 14:06:12 -050088 switch (element->fKind) {
89 case ProgramElement::kFunction_Kind: {
90 FunctionDefinition& f = (FunctionDefinition&) *element;
Brian Osman08f986d2020-05-13 17:06:46 -040091 SkASSERT(f.fDeclaration.fBuiltin);
Ethan Nicholasc18bb512020-07-28 14:46:53 -040092 String key = f.fDeclaration.description();
Brian Osman08f986d2020-05-13 17:06:46 -040093 SkASSERT(target->find(key) == target->end());
94 (*target)[key] = std::make_pair(std::move(element), false);
95 iter = src->erase(iter);
Ethan Nicholasdb80f692019-11-22 14:06:12 -050096 break;
97 }
98 case ProgramElement::kEnum_Kind: {
99 Enum& e = (Enum&) *element;
100 StringFragment name = e.fTypeName;
101 SkASSERT(target->find(name) == target->end());
102 (*target)[name] = std::make_pair(std::move(element), false);
Brian Osman08f986d2020-05-13 17:06:46 -0400103 iter = src->erase(iter);
Ethan Nicholasdb80f692019-11-22 14:06:12 -0500104 break;
105 }
106 default:
107 printf("unsupported include file element\n");
108 SkASSERT(false);
109 }
110 }
111}
112
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -0400113Compiler::Compiler(Flags flags)
114: fFlags(flags)
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400115, fContext(new Context())
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -0400116, fErrorCount(0) {
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400117 auto symbols = std::shared_ptr<SymbolTable>(new SymbolTable(this));
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400118 fIRGenerator = new IRGenerator(fContext.get(), symbols, *this);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400119 #define ADD_TYPE(t) symbols->addWithoutOwnership(fContext->f ## t ## _Type->fName, \
120 fContext->f ## t ## _Type.get())
ethannicholasb3058bd2016-07-01 08:22:01 -0700121 ADD_TYPE(Void);
122 ADD_TYPE(Float);
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400123 ADD_TYPE(Float2);
124 ADD_TYPE(Float3);
125 ADD_TYPE(Float4);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400126 ADD_TYPE(Half);
127 ADD_TYPE(Half2);
128 ADD_TYPE(Half3);
129 ADD_TYPE(Half4);
ethannicholasb3058bd2016-07-01 08:22:01 -0700130 ADD_TYPE(Int);
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400131 ADD_TYPE(Int2);
132 ADD_TYPE(Int3);
133 ADD_TYPE(Int4);
ethannicholasb3058bd2016-07-01 08:22:01 -0700134 ADD_TYPE(UInt);
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400135 ADD_TYPE(UInt2);
136 ADD_TYPE(UInt3);
137 ADD_TYPE(UInt4);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400138 ADD_TYPE(Short);
139 ADD_TYPE(Short2);
140 ADD_TYPE(Short3);
141 ADD_TYPE(Short4);
142 ADD_TYPE(UShort);
143 ADD_TYPE(UShort2);
144 ADD_TYPE(UShort3);
145 ADD_TYPE(UShort4);
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400146 ADD_TYPE(Byte);
147 ADD_TYPE(Byte2);
148 ADD_TYPE(Byte3);
149 ADD_TYPE(Byte4);
150 ADD_TYPE(UByte);
151 ADD_TYPE(UByte2);
152 ADD_TYPE(UByte3);
153 ADD_TYPE(UByte4);
ethannicholasb3058bd2016-07-01 08:22:01 -0700154 ADD_TYPE(Bool);
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400155 ADD_TYPE(Bool2);
156 ADD_TYPE(Bool3);
157 ADD_TYPE(Bool4);
158 ADD_TYPE(Float2x2);
159 ADD_TYPE(Float2x3);
160 ADD_TYPE(Float2x4);
161 ADD_TYPE(Float3x2);
162 ADD_TYPE(Float3x3);
163 ADD_TYPE(Float3x4);
164 ADD_TYPE(Float4x2);
165 ADD_TYPE(Float4x3);
166 ADD_TYPE(Float4x4);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400167 ADD_TYPE(Half2x2);
168 ADD_TYPE(Half2x3);
169 ADD_TYPE(Half2x4);
170 ADD_TYPE(Half3x2);
171 ADD_TYPE(Half3x3);
172 ADD_TYPE(Half3x4);
173 ADD_TYPE(Half4x2);
174 ADD_TYPE(Half4x3);
175 ADD_TYPE(Half4x4);
ethannicholasb3058bd2016-07-01 08:22:01 -0700176 ADD_TYPE(GenType);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400177 ADD_TYPE(GenHType);
ethannicholasb3058bd2016-07-01 08:22:01 -0700178 ADD_TYPE(GenIType);
179 ADD_TYPE(GenUType);
180 ADD_TYPE(GenBType);
181 ADD_TYPE(Mat);
182 ADD_TYPE(Vec);
183 ADD_TYPE(GVec);
184 ADD_TYPE(GVec2);
185 ADD_TYPE(GVec3);
186 ADD_TYPE(GVec4);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400187 ADD_TYPE(HVec);
ethannicholasb3058bd2016-07-01 08:22:01 -0700188 ADD_TYPE(IVec);
189 ADD_TYPE(UVec);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400190 ADD_TYPE(SVec);
191 ADD_TYPE(USVec);
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400192 ADD_TYPE(ByteVec);
193 ADD_TYPE(UByteVec);
ethannicholasb3058bd2016-07-01 08:22:01 -0700194 ADD_TYPE(BVec);
195
196 ADD_TYPE(Sampler1D);
197 ADD_TYPE(Sampler2D);
198 ADD_TYPE(Sampler3D);
ethannicholas5961bc92016-10-12 06:39:56 -0700199 ADD_TYPE(SamplerExternalOES);
ethannicholasb3058bd2016-07-01 08:22:01 -0700200 ADD_TYPE(SamplerCube);
201 ADD_TYPE(Sampler2DRect);
202 ADD_TYPE(Sampler1DArray);
203 ADD_TYPE(Sampler2DArray);
204 ADD_TYPE(SamplerCubeArray);
205 ADD_TYPE(SamplerBuffer);
206 ADD_TYPE(Sampler2DMS);
207 ADD_TYPE(Sampler2DMSArray);
208
Brian Salomonbf7b6202016-11-11 16:08:03 -0500209 ADD_TYPE(ISampler2D);
210
Brian Salomon2a51de82016-11-16 12:06:01 -0500211 ADD_TYPE(Image2D);
212 ADD_TYPE(IImage2D);
213
Greg Daniel64773e62016-11-22 09:44:03 -0500214 ADD_TYPE(SubpassInput);
215 ADD_TYPE(SubpassInputMS);
216
ethannicholasb3058bd2016-07-01 08:22:01 -0700217 ADD_TYPE(GSampler1D);
218 ADD_TYPE(GSampler2D);
219 ADD_TYPE(GSampler3D);
220 ADD_TYPE(GSamplerCube);
221 ADD_TYPE(GSampler2DRect);
222 ADD_TYPE(GSampler1DArray);
223 ADD_TYPE(GSampler2DArray);
224 ADD_TYPE(GSamplerCubeArray);
225 ADD_TYPE(GSamplerBuffer);
226 ADD_TYPE(GSampler2DMS);
227 ADD_TYPE(GSampler2DMSArray);
228
229 ADD_TYPE(Sampler1DShadow);
230 ADD_TYPE(Sampler2DShadow);
231 ADD_TYPE(SamplerCubeShadow);
232 ADD_TYPE(Sampler2DRectShadow);
233 ADD_TYPE(Sampler1DArrayShadow);
234 ADD_TYPE(Sampler2DArrayShadow);
235 ADD_TYPE(SamplerCubeArrayShadow);
236 ADD_TYPE(GSampler2DArrayShadow);
237 ADD_TYPE(GSamplerCubeArrayShadow);
Ethan Nicholasc9472af2017-10-10 16:30:21 -0400238 ADD_TYPE(FragmentProcessor);
Stephen Whiteff5d7a22019-07-26 17:42:06 -0400239 ADD_TYPE(Sampler);
240 ADD_TYPE(Texture2D);
ethannicholasb3058bd2016-07-01 08:22:01 -0700241
Brian Osman28590d52020-03-23 16:59:08 -0400242 StringFragment fpAliasName("shader");
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400243 symbols->addWithoutOwnership(fpAliasName, fContext->fFragmentProcessor_Type.get());
Brian Osman28590d52020-03-23 16:59:08 -0400244
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700245 StringFragment skCapsName("sk_Caps");
246 Variable* skCaps = new Variable(-1, Modifiers(), skCapsName,
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400247 *fContext->fSkCaps_Type, Variable::kGlobal_Storage);
Ethan Nicholas3605ace2016-11-21 15:59:48 -0500248 fIRGenerator->fSymbolTable->add(skCapsName, std::unique_ptr<Symbol>(skCaps));
249
Ethan Nicholasdb80f692019-11-22 14:06:12 -0500250 fIRGenerator->fIntrinsics = &fGPUIntrinsics;
251 std::vector<std::unique_ptr<ProgramElement>> gpuIntrinsics;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400252 std::vector<std::unique_ptr<ProgramElement>> interpIntrinsics;
253#if !REHYDRATE
Ethan Nicholas8da1e652019-05-24 11:01:59 -0400254 this->processIncludeFile(Program::kFragment_Kind, SKSL_GPU_INCLUDE, strlen(SKSL_GPU_INCLUDE),
Ethan Nicholasdb80f692019-11-22 14:06:12 -0500255 symbols, &gpuIntrinsics, &fGpuSymbolTable);
Ethan Nicholasdb80f692019-11-22 14:06:12 -0500256 // need to hang on to the source so that FunctionDefinition.fSource pointers in this file
257 // remain valid
258 fGpuIncludeSource = std::move(fIRGenerator->fFile);
Ethan Nicholas8da1e652019-05-24 11:01:59 -0400259 this->processIncludeFile(Program::kVertex_Kind, SKSL_VERT_INCLUDE, strlen(SKSL_VERT_INCLUDE),
260 fGpuSymbolTable, &fVertexInclude, &fVertexSymbolTable);
261 this->processIncludeFile(Program::kFragment_Kind, SKSL_FRAG_INCLUDE, strlen(SKSL_FRAG_INCLUDE),
262 fGpuSymbolTable, &fFragmentInclude, &fFragmentSymbolTable);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400263#else
264 {
265 Rehydrator rehydrator(fContext.get(), symbols, this, SKSL_INCLUDE_sksl_gpu,
266 SKSL_INCLUDE_sksl_gpu_LENGTH);
267 fGpuSymbolTable = rehydrator.symbolTable();
268 gpuIntrinsics = rehydrator.elements();
269 }
270 {
271 Rehydrator rehydrator(fContext.get(), fGpuSymbolTable, this, SKSL_INCLUDE_sksl_vert,
272 SKSL_INCLUDE_sksl_vert_LENGTH);
273 fVertexSymbolTable = rehydrator.symbolTable();
274 fVertexInclude = rehydrator.elements();
275 }
276 {
277 Rehydrator rehydrator(fContext.get(), fGpuSymbolTable, this, SKSL_INCLUDE_sksl_frag,
278 SKSL_INCLUDE_sksl_frag_LENGTH);
279 fFragmentSymbolTable = rehydrator.symbolTable();
280 fFragmentInclude = rehydrator.elements();
281 }
282#endif
283 grab_intrinsics(&gpuIntrinsics, &fGPUIntrinsics);
Brian Osmanb08cc022020-04-02 11:38:40 -0400284 grab_intrinsics(&interpIntrinsics, &fInterpreterIntrinsics);
ethannicholasb3058bd2016-07-01 08:22:01 -0700285}
286
287Compiler::~Compiler() {
288 delete fIRGenerator;
289}
290
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400291void Compiler::loadGeometryIntrinsics() {
292 if (fGeometrySymbolTable) {
293 return;
294 }
295 #if REHYDRATE
296 {
297 Rehydrator rehydrator(fContext.get(), fGpuSymbolTable, this, SKSL_INCLUDE_sksl_geom,
298 SKSL_INCLUDE_sksl_geom_LENGTH);
299 fGeometrySymbolTable = rehydrator.symbolTable();
300 fGeometryInclude = rehydrator.elements();
301 }
302 #else
303 this->processIncludeFile(Program::kGeometry_Kind, SKSL_GEOM_INCLUDE,
304 strlen(SKSL_GEOM_INCLUDE), fGpuSymbolTable, &fGeometryInclude,
305 &fGeometrySymbolTable);
306 #endif
307}
308
309void Compiler::loadPipelineIntrinsics() {
310 if (fPipelineSymbolTable) {
311 return;
312 }
313 #if REHYDRATE
314 {
315 Rehydrator rehydrator(fContext.get(), fGpuSymbolTable, this,
316 SKSL_INCLUDE_sksl_pipeline,
317 SKSL_INCLUDE_sksl_pipeline_LENGTH);
318 fPipelineSymbolTable = rehydrator.symbolTable();
319 fPipelineInclude = rehydrator.elements();
320 }
321 #else
322 this->processIncludeFile(Program::kPipelineStage_Kind, SKSL_PIPELINE_INCLUDE,
323 strlen(SKSL_PIPELINE_INCLUDE), fGpuSymbolTable, &fPipelineInclude,
324 &fPipelineSymbolTable);
325 #endif
326}
327
328void Compiler::loadInterpreterIntrinsics() {
329 if (fInterpreterSymbolTable) {
330 return;
331 }
332 this->loadPipelineIntrinsics();
333 #if REHYDRATE
334 {
335 Rehydrator rehydrator(fContext.get(), fPipelineSymbolTable, this,
336 SKSL_INCLUDE_sksl_interp,
337 SKSL_INCLUDE_sksl_interp_LENGTH);
338 fInterpreterSymbolTable = rehydrator.symbolTable();
339 fInterpreterInclude = rehydrator.elements();
340 }
341 #else
342 this->processIncludeFile(Program::kGeneric_Kind, SKSL_INTERP_INCLUDE,
343 strlen(SKSL_INTERP_INCLUDE), fIRGenerator->fSymbolTable,
344 &fInterpreterInclude, &fInterpreterSymbolTable);
345 #endif
346}
347
Ethan Nicholas8da1e652019-05-24 11:01:59 -0400348void Compiler::processIncludeFile(Program::Kind kind, const char* src, size_t length,
349 std::shared_ptr<SymbolTable> base,
350 std::vector<std::unique_ptr<ProgramElement>>* outElements,
351 std::shared_ptr<SymbolTable>* outSymbolTable) {
Ethan Nicholasa11035b2019-11-26 16:27:47 -0500352#ifdef SK_DEBUG
353 String source(src, length);
354 fSource = &source;
355#endif
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400356 std::shared_ptr<SymbolTable> old = fIRGenerator->fSymbolTable;
357 if (base) {
358 fIRGenerator->fSymbolTable = std::move(base);
359 }
Ethan Nicholas8da1e652019-05-24 11:01:59 -0400360 Program::Settings settings;
Ethan Nicholasa11035b2019-11-26 16:27:47 -0500361#if !defined(SKSL_STANDALONE) & SK_SUPPORT_GPU
362 GrContextOptions opts;
363 GrShaderCaps caps(opts);
364 settings.fCaps = &caps;
365#endif
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400366 SkASSERT(fIRGenerator->fCanInline);
367 fIRGenerator->fCanInline = false;
368 fIRGenerator->start(&settings, nullptr, true);
369 fIRGenerator->convertProgram(kind, src, length, outElements);
370 fIRGenerator->fCanInline = true;
Ethan Nicholas8da1e652019-05-24 11:01:59 -0400371 if (this->fErrorCount) {
372 printf("Unexpected errors: %s\n", this->fErrorText.c_str());
373 }
374 SkASSERT(!fErrorCount);
Ethan Nicholas8da1e652019-05-24 11:01:59 -0400375 *outSymbolTable = fIRGenerator->fSymbolTable;
Ethan Nicholasa11035b2019-11-26 16:27:47 -0500376#ifdef SK_DEBUG
377 fSource = nullptr;
378#endif
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400379 fIRGenerator->fSymbolTable = std::move(old);
Ethan Nicholas8da1e652019-05-24 11:01:59 -0400380}
381
ethannicholas22f939e2016-10-13 13:25:34 -0700382// add the definition created by assigning to the lvalue to the definition set
Ethan Nicholas86a43402017-01-19 13:32:00 -0500383void Compiler::addDefinition(const Expression* lvalue, std::unique_ptr<Expression>* expr,
384 DefinitionMap* definitions) {
ethannicholas22f939e2016-10-13 13:25:34 -0700385 switch (lvalue->fKind) {
386 case Expression::kVariableReference_Kind: {
387 const Variable& var = ((VariableReference*) lvalue)->fVariable;
388 if (var.fStorage == Variable::kLocal_Storage) {
389 (*definitions)[&var] = expr;
390 }
391 break;
392 }
393 case Expression::kSwizzle_Kind:
394 // We consider the variable written to as long as at least some of its components have
395 // been written to. This will lead to some false negatives (we won't catch it if you
396 // write to foo.x and then read foo.y), but being stricter could lead to false positives
Mike Klein6ad99092016-10-26 10:35:22 -0400397 // (we write to foo.x, and then pass foo to a function which happens to only read foo.x,
398 // 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 -0700399 // more complicated whole-program analysis. This is probably good enough.
Mike Klein6ad99092016-10-26 10:35:22 -0400400 this->addDefinition(((Swizzle*) lvalue)->fBase.get(),
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400401 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
ethannicholas22f939e2016-10-13 13:25:34 -0700402 definitions);
403 break;
404 case Expression::kIndex_Kind:
405 // see comments in Swizzle
Mike Klein6ad99092016-10-26 10:35:22 -0400406 this->addDefinition(((IndexExpression*) lvalue)->fBase.get(),
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400407 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
ethannicholas22f939e2016-10-13 13:25:34 -0700408 definitions);
409 break;
410 case Expression::kFieldAccess_Kind:
411 // see comments in Swizzle
Mike Klein6ad99092016-10-26 10:35:22 -0400412 this->addDefinition(((FieldAccess*) lvalue)->fBase.get(),
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400413 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
ethannicholas22f939e2016-10-13 13:25:34 -0700414 definitions);
415 break;
Ethan Nicholasa583b812018-01-18 13:32:11 -0500416 case Expression::kTernary_Kind:
417 // To simplify analysis, we just pretend that we write to both sides of the ternary.
418 // This allows for false positives (meaning we fail to detect that a variable might not
419 // have been assigned), but is preferable to false negatives.
420 this->addDefinition(((TernaryExpression*) lvalue)->fIfTrue.get(),
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400421 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
Ethan Nicholasa583b812018-01-18 13:32:11 -0500422 definitions);
423 this->addDefinition(((TernaryExpression*) lvalue)->fIfFalse.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 break;
Ethan Nicholas91164d12019-05-15 15:29:54 -0400427 case Expression::kExternalValue_Kind:
428 break;
ethannicholas22f939e2016-10-13 13:25:34 -0700429 default:
430 // not an lvalue, can't happen
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400431 SkASSERT(false);
ethannicholas22f939e2016-10-13 13:25:34 -0700432 }
433}
434
435// add local variables defined by this node to the set
Mike Klein6ad99092016-10-26 10:35:22 -0400436void Compiler::addDefinitions(const BasicBlock::Node& node,
Ethan Nicholas86a43402017-01-19 13:32:00 -0500437 DefinitionMap* definitions) {
ethannicholas22f939e2016-10-13 13:25:34 -0700438 switch (node.fKind) {
439 case BasicBlock::Node::kExpression_Kind: {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400440 SkASSERT(node.expression());
Ethan Nicholascb670962017-04-20 19:31:52 -0400441 const Expression* expr = (Expression*) node.expression()->get();
Ethan Nicholas86a43402017-01-19 13:32:00 -0500442 switch (expr->fKind) {
443 case Expression::kBinary_Kind: {
444 BinaryExpression* b = (BinaryExpression*) expr;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400445 if (b->fOperator == Token::Kind::TK_EQ) {
Ethan Nicholas86a43402017-01-19 13:32:00 -0500446 this->addDefinition(b->fLeft.get(), &b->fRight, definitions);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700447 } else if (Compiler::IsAssignment(b->fOperator)) {
Ethan Nicholas86a43402017-01-19 13:32:00 -0500448 this->addDefinition(
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400449 b->fLeft.get(),
450 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
451 definitions);
Ethan Nicholas86a43402017-01-19 13:32:00 -0500452
453 }
454 break;
ethannicholas22f939e2016-10-13 13:25:34 -0700455 }
Ethan Nicholasc6a19f12018-03-29 16:46:56 -0400456 case Expression::kFunctionCall_Kind: {
457 const FunctionCall& c = (const FunctionCall&) *expr;
458 for (size_t i = 0; i < c.fFunction.fParameters.size(); ++i) {
459 if (c.fFunction.fParameters[i]->fModifiers.fFlags & Modifiers::kOut_Flag) {
460 this->addDefinition(
461 c.fArguments[i].get(),
462 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
463 definitions);
464 }
465 }
466 break;
467 }
Ethan Nicholas86a43402017-01-19 13:32:00 -0500468 case Expression::kPrefix_Kind: {
469 const PrefixExpression* p = (PrefixExpression*) expr;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400470 if (p->fOperator == Token::Kind::TK_MINUSMINUS ||
471 p->fOperator == Token::Kind::TK_PLUSPLUS) {
Ethan Nicholas86a43402017-01-19 13:32:00 -0500472 this->addDefinition(
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400473 p->fOperand.get(),
474 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
475 definitions);
Ethan Nicholas86a43402017-01-19 13:32:00 -0500476 }
477 break;
478 }
479 case Expression::kPostfix_Kind: {
480 const PostfixExpression* p = (PostfixExpression*) expr;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400481 if (p->fOperator == Token::Kind::TK_MINUSMINUS ||
482 p->fOperator == Token::Kind::TK_PLUSPLUS) {
Ethan Nicholas86a43402017-01-19 13:32:00 -0500483 this->addDefinition(
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400484 p->fOperand.get(),
485 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
486 definitions);
Ethan Nicholas86a43402017-01-19 13:32:00 -0500487 }
488 break;
489 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400490 case Expression::kVariableReference_Kind: {
491 const VariableReference* v = (VariableReference*) expr;
492 if (v->fRefKind != VariableReference::kRead_RefKind) {
493 this->addDefinition(
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400494 v,
495 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
496 definitions);
Ethan Nicholascb670962017-04-20 19:31:52 -0400497 }
John Stiles30212b72020-06-11 17:55:07 -0400498 break;
Ethan Nicholascb670962017-04-20 19:31:52 -0400499 }
Ethan Nicholas86a43402017-01-19 13:32:00 -0500500 default:
501 break;
ethannicholas22f939e2016-10-13 13:25:34 -0700502 }
503 break;
504 }
505 case BasicBlock::Node::kStatement_Kind: {
Ethan Nicholascb670962017-04-20 19:31:52 -0400506 const Statement* stmt = (Statement*) node.statement()->get();
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000507 if (stmt->fKind == Statement::kVarDeclaration_Kind) {
508 VarDeclaration& vd = (VarDeclaration&) *stmt;
509 if (vd.fValue) {
510 (*definitions)[vd.fVar] = &vd.fValue;
ethannicholas22f939e2016-10-13 13:25:34 -0700511 }
512 }
513 break;
514 }
515 }
516}
517
518void Compiler::scanCFG(CFG* cfg, BlockId blockId, std::set<BlockId>* workList) {
519 BasicBlock& block = cfg->fBlocks[blockId];
520
521 // compute definitions after this block
Ethan Nicholas86a43402017-01-19 13:32:00 -0500522 DefinitionMap after = block.fBefore;
ethannicholas22f939e2016-10-13 13:25:34 -0700523 for (const BasicBlock::Node& n : block.fNodes) {
524 this->addDefinitions(n, &after);
525 }
526
527 // propagate definitions to exits
528 for (BlockId exitId : block.fExits) {
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400529 if (exitId == blockId) {
530 continue;
531 }
ethannicholas22f939e2016-10-13 13:25:34 -0700532 BasicBlock& exit = cfg->fBlocks[exitId];
533 for (const auto& pair : after) {
Ethan Nicholas86a43402017-01-19 13:32:00 -0500534 std::unique_ptr<Expression>* e1 = pair.second;
535 auto found = exit.fBefore.find(pair.first);
536 if (found == exit.fBefore.end()) {
537 // exit has no definition for it, just copy it
538 workList->insert(exitId);
ethannicholas22f939e2016-10-13 13:25:34 -0700539 exit.fBefore[pair.first] = e1;
540 } else {
Ethan Nicholas86a43402017-01-19 13:32:00 -0500541 // exit has a (possibly different) value already defined
542 std::unique_ptr<Expression>* e2 = exit.fBefore[pair.first];
ethannicholas22f939e2016-10-13 13:25:34 -0700543 if (e1 != e2) {
544 // definition has changed, merge and add exit block to worklist
545 workList->insert(exitId);
Ethan Nicholasaf197692017-02-27 13:26:45 -0500546 if (e1 && e2) {
547 exit.fBefore[pair.first] =
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400548 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression;
Ethan Nicholasaf197692017-02-27 13:26:45 -0500549 } else {
550 exit.fBefore[pair.first] = nullptr;
551 }
ethannicholas22f939e2016-10-13 13:25:34 -0700552 }
553 }
554 }
555 }
556}
557
558// returns a map which maps all local variables in the function to null, indicating that their value
559// is initially unknown
Ethan Nicholas86a43402017-01-19 13:32:00 -0500560static DefinitionMap compute_start_state(const CFG& cfg) {
561 DefinitionMap result;
Mike Klein6ad99092016-10-26 10:35:22 -0400562 for (const auto& block : cfg.fBlocks) {
563 for (const auto& node : block.fNodes) {
ethannicholas22f939e2016-10-13 13:25:34 -0700564 if (node.fKind == BasicBlock::Node::kStatement_Kind) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400565 SkASSERT(node.statement());
Ethan Nicholascb670962017-04-20 19:31:52 -0400566 const Statement* s = node.statement()->get();
ethannicholas22f939e2016-10-13 13:25:34 -0700567 if (s->fKind == Statement::kVarDeclarations_Kind) {
568 const VarDeclarationsStatement* vd = (const VarDeclarationsStatement*) s;
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000569 for (const auto& decl : vd->fDeclaration->fVars) {
570 if (decl->fKind == Statement::kVarDeclaration_Kind) {
571 result[((VarDeclaration&) *decl).fVar] = nullptr;
572 }
Mike Klein6ad99092016-10-26 10:35:22 -0400573 }
ethannicholas22f939e2016-10-13 13:25:34 -0700574 }
575 }
576 }
577 }
578 return result;
579}
580
Ethan Nicholascb670962017-04-20 19:31:52 -0400581/**
582 * Returns true if assigning to this lvalue has no effect.
583 */
584static bool is_dead(const Expression& lvalue) {
585 switch (lvalue.fKind) {
586 case Expression::kVariableReference_Kind:
587 return ((VariableReference&) lvalue).fVariable.dead();
588 case Expression::kSwizzle_Kind:
589 return is_dead(*((Swizzle&) lvalue).fBase);
590 case Expression::kFieldAccess_Kind:
591 return is_dead(*((FieldAccess&) lvalue).fBase);
592 case Expression::kIndex_Kind: {
593 const IndexExpression& idx = (IndexExpression&) lvalue;
Ethan Nicholas2a099da2020-01-02 14:40:54 -0500594 return is_dead(*idx.fBase) &&
595 !idx.fIndex->hasProperty(Expression::Property::kSideEffects);
Ethan Nicholascb670962017-04-20 19:31:52 -0400596 }
Ethan Nicholasa583b812018-01-18 13:32:11 -0500597 case Expression::kTernary_Kind: {
598 const TernaryExpression& t = (TernaryExpression&) lvalue;
599 return !t.fTest->hasSideEffects() && is_dead(*t.fIfTrue) && is_dead(*t.fIfFalse);
600 }
Ethan Nicholas91164d12019-05-15 15:29:54 -0400601 case Expression::kExternalValue_Kind:
602 return false;
Ethan Nicholascb670962017-04-20 19:31:52 -0400603 default:
Ethan Nicholas2a099da2020-01-02 14:40:54 -0500604#ifdef SK_DEBUG
Ethan Nicholascb670962017-04-20 19:31:52 -0400605 ABORT("invalid lvalue: %s\n", lvalue.description().c_str());
Ethan Nicholas2a099da2020-01-02 14:40:54 -0500606#endif
607 return false;
Ethan Nicholascb670962017-04-20 19:31:52 -0400608 }
609}
ethannicholas22f939e2016-10-13 13:25:34 -0700610
Ethan Nicholascb670962017-04-20 19:31:52 -0400611/**
612 * Returns true if this is an assignment which can be collapsed down to just the right hand side due
613 * to a dead target and lack of side effects on the left hand side.
614 */
615static bool dead_assignment(const BinaryExpression& b) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700616 if (!Compiler::IsAssignment(b.fOperator)) {
Ethan Nicholascb670962017-04-20 19:31:52 -0400617 return false;
618 }
619 return is_dead(*b.fLeft);
620}
621
622void Compiler::computeDataFlow(CFG* cfg) {
623 cfg->fBlocks[cfg->fStart].fBefore = compute_start_state(*cfg);
ethannicholas22f939e2016-10-13 13:25:34 -0700624 std::set<BlockId> workList;
Ethan Nicholascb670962017-04-20 19:31:52 -0400625 for (BlockId i = 0; i < cfg->fBlocks.size(); i++) {
ethannicholas22f939e2016-10-13 13:25:34 -0700626 workList.insert(i);
627 }
628 while (workList.size()) {
629 BlockId next = *workList.begin();
630 workList.erase(workList.begin());
Ethan Nicholascb670962017-04-20 19:31:52 -0400631 this->scanCFG(cfg, next, &workList);
ethannicholas22f939e2016-10-13 13:25:34 -0700632 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400633}
634
635/**
636 * Attempts to replace the expression pointed to by iter with a new one (in both the CFG and the
637 * IR). If the expression can be cleanly removed, returns true and updates the iterator to point to
638 * the newly-inserted element. Otherwise updates only the IR and returns false (and the CFG will
639 * need to be regenerated).
640 */
641bool try_replace_expression(BasicBlock* b,
642 std::vector<BasicBlock::Node>::iterator* iter,
643 std::unique_ptr<Expression>* newExpression) {
644 std::unique_ptr<Expression>* target = (*iter)->expression();
645 if (!b->tryRemoveExpression(iter)) {
646 *target = std::move(*newExpression);
647 return false;
648 }
649 *target = std::move(*newExpression);
650 return b->tryInsertExpression(iter, target);
651}
652
653/**
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400654 * Returns true if the expression is a constant numeric literal with the specified value, or a
655 * constant vector with all elements equal to the specified value.
Ethan Nicholascb670962017-04-20 19:31:52 -0400656 */
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400657bool is_constant(const Expression& expr, double value) {
Ethan Nicholascb670962017-04-20 19:31:52 -0400658 switch (expr.fKind) {
659 case Expression::kIntLiteral_Kind:
660 return ((IntLiteral&) expr).fValue == value;
661 case Expression::kFloatLiteral_Kind:
662 return ((FloatLiteral&) expr).fValue == value;
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400663 case Expression::kConstructor_Kind: {
664 Constructor& c = (Constructor&) expr;
Ethan Nicholasd188c182019-06-10 15:55:38 -0400665 bool isFloat = c.fType.columns() > 1 ? c.fType.componentType().isFloat()
666 : c.fType.isFloat();
Brian Osmanb6b95732020-06-30 11:44:27 -0400667 if (c.fType.kind() == Type::kVector_Kind && c.isCompileTimeConstant()) {
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400668 for (int i = 0; i < c.fType.columns(); ++i) {
Ethan Nicholasd188c182019-06-10 15:55:38 -0400669 if (isFloat) {
670 if (c.getFVecComponent(i) != value) {
671 return false;
672 }
673 } else if (c.getIVecComponent(i) != value) {
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400674 return false;
675 }
676 }
677 return true;
678 }
679 return false;
680 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400681 default:
682 return false;
683 }
684}
685
686/**
687 * Collapses the binary expression pointed to by iter down to just the right side (in both the IR
688 * and CFG structures).
689 */
690void delete_left(BasicBlock* b,
691 std::vector<BasicBlock::Node>::iterator* iter,
692 bool* outUpdated,
693 bool* outNeedsRescan) {
694 *outUpdated = true;
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400695 std::unique_ptr<Expression>* target = (*iter)->expression();
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400696 SkASSERT((*target)->fKind == Expression::kBinary_Kind);
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400697 BinaryExpression& bin = (BinaryExpression&) **target;
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400698 SkASSERT(!bin.fLeft->hasSideEffects());
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400699 bool result;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400700 if (bin.fOperator == Token::Kind::TK_EQ) {
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400701 result = b->tryRemoveLValueBefore(iter, bin.fLeft.get());
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400702 } else {
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400703 result = b->tryRemoveExpressionBefore(iter, bin.fLeft.get());
Ethan Nicholascb670962017-04-20 19:31:52 -0400704 }
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400705 *target = std::move(bin.fRight);
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400706 if (!result) {
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400707 *outNeedsRescan = true;
708 return;
709 }
710 if (*iter == b->fNodes.begin()) {
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400711 *outNeedsRescan = true;
712 return;
713 }
714 --(*iter);
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400715 if ((*iter)->fKind != BasicBlock::Node::kExpression_Kind ||
716 (*iter)->expression() != &bin.fRight) {
717 *outNeedsRescan = true;
718 return;
719 }
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400720 *iter = b->fNodes.erase(*iter);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400721 SkASSERT((*iter)->expression() == target);
Ethan Nicholascb670962017-04-20 19:31:52 -0400722}
723
724/**
725 * Collapses the binary expression pointed to by iter down to just the left side (in both the IR and
726 * CFG structures).
727 */
728void delete_right(BasicBlock* b,
729 std::vector<BasicBlock::Node>::iterator* iter,
730 bool* outUpdated,
731 bool* outNeedsRescan) {
732 *outUpdated = true;
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400733 std::unique_ptr<Expression>* target = (*iter)->expression();
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400734 SkASSERT((*target)->fKind == Expression::kBinary_Kind);
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400735 BinaryExpression& bin = (BinaryExpression&) **target;
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400736 SkASSERT(!bin.fRight->hasSideEffects());
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400737 if (!b->tryRemoveExpressionBefore(iter, bin.fRight.get())) {
738 *target = std::move(bin.fLeft);
Ethan Nicholascb670962017-04-20 19:31:52 -0400739 *outNeedsRescan = true;
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400740 return;
Ethan Nicholascb670962017-04-20 19:31:52 -0400741 }
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400742 *target = std::move(bin.fLeft);
743 if (*iter == b->fNodes.begin()) {
744 *outNeedsRescan = true;
745 return;
746 }
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400747 --(*iter);
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400748 if (((*iter)->fKind != BasicBlock::Node::kExpression_Kind ||
749 (*iter)->expression() != &bin.fLeft)) {
750 *outNeedsRescan = true;
751 return;
752 }
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400753 *iter = b->fNodes.erase(*iter);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400754 SkASSERT((*iter)->expression() == target);
Ethan Nicholascb670962017-04-20 19:31:52 -0400755}
756
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400757/**
758 * Constructs the specified type using a single argument.
759 */
760static std::unique_ptr<Expression> construct(const Type& type, std::unique_ptr<Expression> v) {
761 std::vector<std::unique_ptr<Expression>> args;
762 args.push_back(std::move(v));
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700763 auto result = std::unique_ptr<Expression>(new Constructor(-1, type, std::move(args)));
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400764 return result;
765}
766
767/**
768 * Used in the implementations of vectorize_left and vectorize_right. Given a vector type and an
769 * expression x, deletes the expression pointed to by iter and replaces it with <type>(x).
770 */
771static void vectorize(BasicBlock* b,
772 std::vector<BasicBlock::Node>::iterator* iter,
773 const Type& type,
774 std::unique_ptr<Expression>* otherExpression,
775 bool* outUpdated,
776 bool* outNeedsRescan) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400777 SkASSERT((*(*iter)->expression())->fKind == Expression::kBinary_Kind);
778 SkASSERT(type.kind() == Type::kVector_Kind);
779 SkASSERT((*otherExpression)->fType.kind() == Type::kScalar_Kind);
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400780 *outUpdated = true;
781 std::unique_ptr<Expression>* target = (*iter)->expression();
782 if (!b->tryRemoveExpression(iter)) {
783 *target = construct(type, std::move(*otherExpression));
784 *outNeedsRescan = true;
785 } else {
786 *target = construct(type, std::move(*otherExpression));
787 if (!b->tryInsertExpression(iter, target)) {
788 *outNeedsRescan = true;
789 }
790 }
791}
792
793/**
794 * Given a binary expression of the form x <op> vec<n>(y), deletes the right side and vectorizes the
795 * left to yield vec<n>(x).
796 */
797static void vectorize_left(BasicBlock* b,
798 std::vector<BasicBlock::Node>::iterator* iter,
799 bool* outUpdated,
800 bool* outNeedsRescan) {
801 BinaryExpression& bin = (BinaryExpression&) **(*iter)->expression();
802 vectorize(b, iter, bin.fRight->fType, &bin.fLeft, outUpdated, outNeedsRescan);
803}
804
805/**
806 * Given a binary expression of the form vec<n>(x) <op> y, deletes the left side and vectorizes the
807 * right to yield vec<n>(y).
808 */
809static void vectorize_right(BasicBlock* b,
810 std::vector<BasicBlock::Node>::iterator* iter,
811 bool* outUpdated,
812 bool* outNeedsRescan) {
813 BinaryExpression& bin = (BinaryExpression&) **(*iter)->expression();
814 vectorize(b, iter, bin.fLeft->fType, &bin.fRight, outUpdated, outNeedsRescan);
815}
816
817// Mark that an expression which we were writing to is no longer being written to
818void clear_write(const Expression& expr) {
819 switch (expr.fKind) {
820 case Expression::kVariableReference_Kind: {
821 ((VariableReference&) expr).setRefKind(VariableReference::kRead_RefKind);
822 break;
823 }
824 case Expression::kFieldAccess_Kind:
825 clear_write(*((FieldAccess&) expr).fBase);
826 break;
827 case Expression::kSwizzle_Kind:
828 clear_write(*((Swizzle&) expr).fBase);
829 break;
830 case Expression::kIndex_Kind:
831 clear_write(*((IndexExpression&) expr).fBase);
832 break;
833 default:
834 ABORT("shouldn't be writing to this kind of expression\n");
835 break;
836 }
837}
838
Ethan Nicholascb670962017-04-20 19:31:52 -0400839void Compiler::simplifyExpression(DefinitionMap& definitions,
840 BasicBlock& b,
841 std::vector<BasicBlock::Node>::iterator* iter,
842 std::unordered_set<const Variable*>* undefinedVariables,
843 bool* outUpdated,
844 bool* outNeedsRescan) {
845 Expression* expr = (*iter)->expression()->get();
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400846 SkASSERT(expr);
Ethan Nicholascb670962017-04-20 19:31:52 -0400847 if ((*iter)->fConstantPropagation) {
848 std::unique_ptr<Expression> optimized = expr->constantPropagate(*fIRGenerator, definitions);
849 if (optimized) {
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400850 *outUpdated = true;
Ethan Nicholascb670962017-04-20 19:31:52 -0400851 if (!try_replace_expression(&b, iter, &optimized)) {
852 *outNeedsRescan = true;
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400853 return;
Ethan Nicholascb670962017-04-20 19:31:52 -0400854 }
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400855 SkASSERT((*iter)->fKind == BasicBlock::Node::kExpression_Kind);
Ethan Nicholascb670962017-04-20 19:31:52 -0400856 expr = (*iter)->expression()->get();
Ethan Nicholascb670962017-04-20 19:31:52 -0400857 }
858 }
859 switch (expr->fKind) {
860 case Expression::kVariableReference_Kind: {
Ethan Nicholas8f7e28f2018-03-26 14:24:27 -0400861 const VariableReference& ref = (VariableReference&) *expr;
862 const Variable& var = ref.fVariable;
863 if (ref.refKind() != VariableReference::kWrite_RefKind &&
864 ref.refKind() != VariableReference::kPointer_RefKind &&
865 var.fStorage == Variable::kLocal_Storage && !definitions[&var] &&
Ethan Nicholascb670962017-04-20 19:31:52 -0400866 (*undefinedVariables).find(&var) == (*undefinedVariables).end()) {
867 (*undefinedVariables).insert(&var);
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000868 this->error(expr->fOffset,
869 "'" + var.fName + "' has not been assigned");
Ethan Nicholascb670962017-04-20 19:31:52 -0400870 }
871 break;
872 }
873 case Expression::kTernary_Kind: {
874 TernaryExpression* t = (TernaryExpression*) expr;
875 if (t->fTest->fKind == Expression::kBoolLiteral_Kind) {
876 // ternary has a constant test, replace it with either the true or
877 // false branch
878 if (((BoolLiteral&) *t->fTest).fValue) {
879 (*iter)->setExpression(std::move(t->fIfTrue));
880 } else {
881 (*iter)->setExpression(std::move(t->fIfFalse));
882 }
883 *outUpdated = true;
884 *outNeedsRescan = true;
885 }
886 break;
887 }
888 case Expression::kBinary_Kind: {
Ethan Nicholascb670962017-04-20 19:31:52 -0400889 BinaryExpression* bin = (BinaryExpression*) expr;
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400890 if (dead_assignment(*bin)) {
891 delete_left(&b, iter, outUpdated, outNeedsRescan);
892 break;
893 }
894 // collapse useless expressions like x * 1 or x + 0
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400895 if (((bin->fLeft->fType.kind() != Type::kScalar_Kind) &&
896 (bin->fLeft->fType.kind() != Type::kVector_Kind)) ||
897 ((bin->fRight->fType.kind() != Type::kScalar_Kind) &&
898 (bin->fRight->fType.kind() != Type::kVector_Kind))) {
899 break;
900 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400901 switch (bin->fOperator) {
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400902 case Token::Kind::TK_STAR:
Ethan Nicholascb670962017-04-20 19:31:52 -0400903 if (is_constant(*bin->fLeft, 1)) {
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400904 if (bin->fLeft->fType.kind() == Type::kVector_Kind &&
905 bin->fRight->fType.kind() == Type::kScalar_Kind) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400906 // float4(1) * x -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400907 vectorize_right(&b, iter, outUpdated, outNeedsRescan);
908 } else {
909 // 1 * x -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400910 // 1 * float4(x) -> float4(x)
911 // float4(1) * float4(x) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400912 delete_left(&b, iter, outUpdated, outNeedsRescan);
913 }
914 }
915 else if (is_constant(*bin->fLeft, 0)) {
916 if (bin->fLeft->fType.kind() == Type::kScalar_Kind &&
Ethan Nicholas08dae922018-01-23 10:31:56 -0500917 bin->fRight->fType.kind() == Type::kVector_Kind &&
918 !bin->fRight->hasSideEffects()) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400919 // 0 * float4(x) -> float4(0)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400920 vectorize_left(&b, iter, outUpdated, outNeedsRescan);
921 } else {
922 // 0 * x -> 0
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400923 // float4(0) * x -> float4(0)
924 // float4(0) * float4(x) -> float4(0)
Ethan Nicholas51493ee2017-12-11 12:34:33 -0500925 if (!bin->fRight->hasSideEffects()) {
926 delete_right(&b, iter, outUpdated, outNeedsRescan);
927 }
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400928 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400929 }
930 else if (is_constant(*bin->fRight, 1)) {
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400931 if (bin->fLeft->fType.kind() == Type::kScalar_Kind &&
932 bin->fRight->fType.kind() == Type::kVector_Kind) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400933 // x * float4(1) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400934 vectorize_left(&b, iter, outUpdated, outNeedsRescan);
935 } else {
936 // x * 1 -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400937 // float4(x) * 1 -> float4(x)
938 // float4(x) * float4(1) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400939 delete_right(&b, iter, outUpdated, outNeedsRescan);
940 }
941 }
942 else if (is_constant(*bin->fRight, 0)) {
943 if (bin->fLeft->fType.kind() == Type::kVector_Kind &&
Ethan Nicholas08dae922018-01-23 10:31:56 -0500944 bin->fRight->fType.kind() == Type::kScalar_Kind &&
945 !bin->fLeft->hasSideEffects()) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400946 // float4(x) * 0 -> float4(0)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400947 vectorize_right(&b, iter, outUpdated, outNeedsRescan);
948 } else {
949 // x * 0 -> 0
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400950 // x * float4(0) -> float4(0)
951 // float4(x) * float4(0) -> float4(0)
Ethan Nicholas51493ee2017-12-11 12:34:33 -0500952 if (!bin->fLeft->hasSideEffects()) {
953 delete_left(&b, iter, outUpdated, outNeedsRescan);
954 }
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400955 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400956 }
957 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400958 case Token::Kind::TK_PLUS:
Ethan Nicholascb670962017-04-20 19:31:52 -0400959 if (is_constant(*bin->fLeft, 0)) {
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400960 if (bin->fLeft->fType.kind() == Type::kVector_Kind &&
961 bin->fRight->fType.kind() == Type::kScalar_Kind) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400962 // float4(0) + x -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400963 vectorize_right(&b, iter, outUpdated, outNeedsRescan);
964 } else {
965 // 0 + x -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400966 // 0 + float4(x) -> float4(x)
967 // float4(0) + float4(x) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400968 delete_left(&b, iter, outUpdated, outNeedsRescan);
969 }
970 } else if (is_constant(*bin->fRight, 0)) {
971 if (bin->fLeft->fType.kind() == Type::kScalar_Kind &&
972 bin->fRight->fType.kind() == Type::kVector_Kind) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400973 // x + float4(0) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400974 vectorize_left(&b, iter, outUpdated, outNeedsRescan);
975 } else {
976 // x + 0 -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400977 // float4(x) + 0 -> float4(x)
978 // float4(x) + float4(0) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400979 delete_right(&b, iter, outUpdated, outNeedsRescan);
980 }
Ethan Nicholas56e42712017-04-21 10:23:37 -0400981 }
982 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400983 case Token::Kind::TK_MINUS:
Ethan Nicholas56e42712017-04-21 10:23:37 -0400984 if (is_constant(*bin->fRight, 0)) {
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400985 if (bin->fLeft->fType.kind() == Type::kScalar_Kind &&
986 bin->fRight->fType.kind() == Type::kVector_Kind) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400987 // x - float4(0) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400988 vectorize_left(&b, iter, outUpdated, outNeedsRescan);
989 } else {
990 // x - 0 -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400991 // float4(x) - 0 -> float4(x)
992 // float4(x) - float4(0) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400993 delete_right(&b, iter, outUpdated, outNeedsRescan);
994 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400995 }
996 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400997 case Token::Kind::TK_SLASH:
Ethan Nicholascb670962017-04-20 19:31:52 -0400998 if (is_constant(*bin->fRight, 1)) {
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400999 if (bin->fLeft->fType.kind() == Type::kScalar_Kind &&
1000 bin->fRight->fType.kind() == Type::kVector_Kind) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001001 // x / float4(1) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001002 vectorize_left(&b, iter, outUpdated, outNeedsRescan);
1003 } else {
1004 // x / 1 -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001005 // float4(x) / 1 -> float4(x)
1006 // float4(x) / float4(1) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001007 delete_right(&b, iter, outUpdated, outNeedsRescan);
1008 }
1009 } else if (is_constant(*bin->fLeft, 0)) {
1010 if (bin->fLeft->fType.kind() == Type::kScalar_Kind &&
Ethan Nicholas08dae922018-01-23 10:31:56 -05001011 bin->fRight->fType.kind() == Type::kVector_Kind &&
1012 !bin->fRight->hasSideEffects()) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001013 // 0 / float4(x) -> float4(0)
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001014 vectorize_left(&b, iter, outUpdated, outNeedsRescan);
1015 } else {
1016 // 0 / x -> 0
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001017 // float4(0) / x -> float4(0)
1018 // float4(0) / float4(x) -> float4(0)
Ethan Nicholas51493ee2017-12-11 12:34:33 -05001019 if (!bin->fRight->hasSideEffects()) {
1020 delete_right(&b, iter, outUpdated, outNeedsRescan);
1021 }
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001022 }
1023 }
1024 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001025 case Token::Kind::TK_PLUSEQ:
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001026 if (is_constant(*bin->fRight, 0)) {
1027 clear_write(*bin->fLeft);
1028 delete_right(&b, iter, outUpdated, outNeedsRescan);
1029 }
1030 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001031 case Token::Kind::TK_MINUSEQ:
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001032 if (is_constant(*bin->fRight, 0)) {
1033 clear_write(*bin->fLeft);
1034 delete_right(&b, iter, outUpdated, outNeedsRescan);
1035 }
1036 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001037 case Token::Kind::TK_STAREQ:
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001038 if (is_constant(*bin->fRight, 1)) {
1039 clear_write(*bin->fLeft);
1040 delete_right(&b, iter, outUpdated, outNeedsRescan);
1041 }
1042 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001043 case Token::Kind::TK_SLASHEQ:
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001044 if (is_constant(*bin->fRight, 1)) {
1045 clear_write(*bin->fLeft);
Ethan Nicholascb670962017-04-20 19:31:52 -04001046 delete_right(&b, iter, outUpdated, outNeedsRescan);
1047 }
1048 break;
1049 default:
1050 break;
1051 }
Ethan Nicholas409f6f02019-09-17 12:34:39 -04001052 break;
1053 }
1054 case Expression::kSwizzle_Kind: {
1055 Swizzle& s = (Swizzle&) *expr;
1056 // detect identity swizzles like foo.rgba
1057 if ((int) s.fComponents.size() == s.fBase->fType.columns()) {
1058 bool identity = true;
1059 for (int i = 0; i < (int) s.fComponents.size(); ++i) {
1060 if (s.fComponents[i] != i) {
1061 identity = false;
1062 break;
1063 }
1064 }
1065 if (identity) {
1066 *outUpdated = true;
1067 if (!try_replace_expression(&b, iter, &s.fBase)) {
1068 *outNeedsRescan = true;
1069 return;
1070 }
1071 SkASSERT((*iter)->fKind == BasicBlock::Node::kExpression_Kind);
1072 break;
1073 }
1074 }
1075 // detect swizzles of swizzles, e.g. replace foo.argb.r000 with foo.a000
1076 if (s.fBase->fKind == Expression::kSwizzle_Kind) {
1077 Swizzle& base = (Swizzle&) *s.fBase;
1078 std::vector<int> final;
1079 for (int c : s.fComponents) {
1080 if (c == SKSL_SWIZZLE_0 || c == SKSL_SWIZZLE_1) {
1081 final.push_back(c);
1082 } else {
1083 final.push_back(base.fComponents[c]);
1084 }
1085 }
1086 *outUpdated = true;
1087 std::unique_ptr<Expression> replacement(new Swizzle(*fContext, base.fBase->clone(),
1088 std::move(final)));
1089 if (!try_replace_expression(&b, iter, &replacement)) {
1090 *outNeedsRescan = true;
1091 return;
1092 }
1093 SkASSERT((*iter)->fKind == BasicBlock::Node::kExpression_Kind);
Ethan Nicholas409f6f02019-09-17 12:34:39 -04001094 }
John Stiles30212b72020-06-11 17:55:07 -04001095 break;
Ethan Nicholascb670962017-04-20 19:31:52 -04001096 }
1097 default:
1098 break;
1099 }
1100}
1101
John Stiles92219b42020-06-15 12:32:24 -04001102// Implementation-detail recursive helper function for `contains_conditional_break`.
1103static bool contains_conditional_break_impl(Statement& s, bool inConditional) {
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001104 switch (s.fKind) {
1105 case Statement::kBlock_Kind:
John Stiles92219b42020-06-15 12:32:24 -04001106 for (const std::unique_ptr<Statement>& sub : static_cast<Block&>(s).fStatements) {
1107 if (contains_conditional_break_impl(*sub, inConditional)) {
Ethan Nicholas5005a222018-08-24 13:06:27 -04001108 return true;
1109 }
1110 }
1111 return false;
John Stiles92219b42020-06-15 12:32:24 -04001112
Ethan Nicholas5005a222018-08-24 13:06:27 -04001113 case Statement::kBreak_Kind:
1114 return inConditional;
John Stiles92219b42020-06-15 12:32:24 -04001115
Ethan Nicholas5005a222018-08-24 13:06:27 -04001116 case Statement::kIf_Kind: {
John Stiles92219b42020-06-15 12:32:24 -04001117 const IfStatement& i = static_cast<IfStatement&>(s);
1118 return contains_conditional_break_impl(*i.fIfTrue, /*inConditional=*/true) ||
1119 (i.fIfFalse &&
1120 contains_conditional_break_impl(*i.fIfFalse, /*inConditional=*/true));
Ethan Nicholas5005a222018-08-24 13:06:27 -04001121 }
John Stiles92219b42020-06-15 12:32:24 -04001122
Ethan Nicholas5005a222018-08-24 13:06:27 -04001123 default:
1124 return false;
1125 }
1126}
1127
John Stiles92219b42020-06-15 12:32:24 -04001128// Returns true if this statement could potentially execute a break at the current level. We ignore
1129// nested loops and switches, since any breaks inside of them will merely break the loop / switch.
1130static bool contains_conditional_break(Statement& s) {
1131 return contains_conditional_break_impl(s, /*inConditional=*/false);
1132}
1133
Ethan Nicholas5005a222018-08-24 13:06:27 -04001134// returns true if this statement definitely executes a break at the current level (we ignore
1135// nested loops and switches, since any breaks inside of them will merely break the loop / switch)
1136static bool contains_unconditional_break(Statement& s) {
1137 switch (s.fKind) {
1138 case Statement::kBlock_Kind:
John Stiles92219b42020-06-15 12:32:24 -04001139 for (const std::unique_ptr<Statement>& sub : static_cast<Block&>(s).fStatements) {
Ethan Nicholas5005a222018-08-24 13:06:27 -04001140 if (contains_unconditional_break(*sub)) {
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001141 return true;
1142 }
1143 }
1144 return false;
John Stiles92219b42020-06-15 12:32:24 -04001145
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001146 case Statement::kBreak_Kind:
1147 return true;
John Stiles92219b42020-06-15 12:32:24 -04001148
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001149 default:
1150 return false;
1151 }
1152}
1153
John Stiles92219b42020-06-15 12:32:24 -04001154static void move_all_but_break(std::unique_ptr<Statement>& stmt,
1155 std::vector<std::unique_ptr<Statement>>* target) {
Ethan Nicholas739e1ca2020-06-11 12:16:14 -04001156 switch (stmt->fKind) {
John Stiles92219b42020-06-15 12:32:24 -04001157 case Statement::kBlock_Kind: {
1158 // Recurse into the block.
1159 Block& block = static_cast<Block&>(*stmt);
1160
1161 std::vector<std::unique_ptr<Statement>> blockStmts;
1162 blockStmts.reserve(block.fStatements.size());
1163 for (std::unique_ptr<Statement>& statementInBlock : block.fStatements) {
1164 move_all_but_break(statementInBlock, &blockStmts);
Ethan Nicholas739e1ca2020-06-11 12:16:14 -04001165 }
John Stiles92219b42020-06-15 12:32:24 -04001166
1167 target->push_back(std::make_unique<Block>(block.fOffset, std::move(blockStmts),
1168 block.fSymbols, block.fIsScope));
Ethan Nicholas739e1ca2020-06-11 12:16:14 -04001169 break;
John Stiles92219b42020-06-15 12:32:24 -04001170 }
1171
Ethan Nicholas739e1ca2020-06-11 12:16:14 -04001172 case Statement::kBreak_Kind:
John Stiles92219b42020-06-15 12:32:24 -04001173 // Do not append a break to the target.
1174 break;
1175
Ethan Nicholas739e1ca2020-06-11 12:16:14 -04001176 default:
John Stiles92219b42020-06-15 12:32:24 -04001177 // Append normal statements to the target.
1178 target->push_back(std::move(stmt));
1179 break;
Ethan Nicholas739e1ca2020-06-11 12:16:14 -04001180 }
1181}
1182
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001183// Returns a block containing all of the statements that will be run if the given case matches
1184// (which, owing to the statements being owned by unique_ptrs, means the switch itself will be
1185// broken by this call and must then be discarded).
1186// Returns null (and leaves the switch unmodified) if no such simple reduction is possible, such as
1187// when break statements appear inside conditionals.
John Stiles92219b42020-06-15 12:32:24 -04001188static std::unique_ptr<Statement> block_for_case(SwitchStatement* switchStatement,
1189 SwitchCase* caseToCapture) {
1190 // We have to be careful to not move any of the pointers until after we're sure we're going to
1191 // succeed, so before we make any changes at all, we check the switch-cases to decide on a plan
1192 // of action. First, find the switch-case we are interested in.
1193 auto iter = switchStatement->fCases.begin();
1194 for (; iter != switchStatement->fCases.end(); ++iter) {
1195 if (iter->get() == caseToCapture) {
1196 break;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001197 }
John Stiles92219b42020-06-15 12:32:24 -04001198 }
1199
1200 // Next, walk forward through the rest of the switch. If we find a conditional break, we're
1201 // stuck and can't simplify at all. If we find an unconditional break, we have a range of
1202 // statements that we can use for simplification.
1203 auto startIter = iter;
1204 Statement* unconditionalBreakStmt = nullptr;
1205 for (; iter != switchStatement->fCases.end(); ++iter) {
1206 for (std::unique_ptr<Statement>& stmt : (*iter)->fStatements) {
1207 if (contains_conditional_break(*stmt)) {
1208 // We can't reduce switch-cases to a block when they have conditional breaks.
1209 return nullptr;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001210 }
John Stiles92219b42020-06-15 12:32:24 -04001211
1212 if (contains_unconditional_break(*stmt)) {
1213 // We found an unconditional break. We can use this block, but we need to strip
1214 // out the break statement.
1215 unconditionalBreakStmt = stmt.get();
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001216 break;
1217 }
1218 }
John Stiles92219b42020-06-15 12:32:24 -04001219
1220 if (unconditionalBreakStmt != nullptr) {
1221 break;
1222 }
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001223 }
John Stiles92219b42020-06-15 12:32:24 -04001224
1225 // We fell off the bottom of the switch or encountered a break. We know the range of statements
1226 // that we need to move over, and we know it's safe to do so.
1227 std::vector<std::unique_ptr<Statement>> caseStmts;
1228
1229 // We can move over most of the statements as-is.
1230 while (startIter != iter) {
1231 for (std::unique_ptr<Statement>& stmt : (*startIter)->fStatements) {
1232 caseStmts.push_back(std::move(stmt));
1233 }
1234 ++startIter;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001235 }
John Stiles92219b42020-06-15 12:32:24 -04001236
1237 // If we found an unconditional break at the end, we need to move what we can while avoiding
1238 // that break.
1239 if (unconditionalBreakStmt != nullptr) {
1240 for (std::unique_ptr<Statement>& stmt : (*startIter)->fStatements) {
1241 if (stmt.get() == unconditionalBreakStmt) {
1242 move_all_but_break(stmt, &caseStmts);
1243 unconditionalBreakStmt = nullptr;
1244 break;
1245 }
1246
1247 caseStmts.push_back(std::move(stmt));
1248 }
1249 }
1250
1251 SkASSERT(unconditionalBreakStmt == nullptr); // Verify that we fixed the unconditional break.
1252
1253 // Return our newly-synthesized block.
1254 return std::make_unique<Block>(/*offset=*/-1, std::move(caseStmts), switchStatement->fSymbols);
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001255}
1256
Ethan Nicholascb670962017-04-20 19:31:52 -04001257void Compiler::simplifyStatement(DefinitionMap& definitions,
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001258 BasicBlock& b,
1259 std::vector<BasicBlock::Node>::iterator* iter,
1260 std::unordered_set<const Variable*>* undefinedVariables,
1261 bool* outUpdated,
1262 bool* outNeedsRescan) {
Ethan Nicholascb670962017-04-20 19:31:52 -04001263 Statement* stmt = (*iter)->statement()->get();
1264 switch (stmt->fKind) {
Ethan Nicholas82a62d22017-11-07 14:42:10 +00001265 case Statement::kVarDeclaration_Kind: {
1266 const auto& varDecl = (VarDeclaration&) *stmt;
1267 if (varDecl.fVar->dead() &&
1268 (!varDecl.fValue ||
1269 !varDecl.fValue->hasSideEffects())) {
1270 if (varDecl.fValue) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001271 SkASSERT((*iter)->statement()->get() == stmt);
Ethan Nicholas82a62d22017-11-07 14:42:10 +00001272 if (!b.tryRemoveExpressionBefore(iter, varDecl.fValue.get())) {
1273 *outNeedsRescan = true;
Ethan Nicholascb670962017-04-20 19:31:52 -04001274 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001275 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001276 (*iter)->setStatement(std::unique_ptr<Statement>(new Nop()));
Ethan Nicholas82a62d22017-11-07 14:42:10 +00001277 *outUpdated = true;
Ethan Nicholascb670962017-04-20 19:31:52 -04001278 }
1279 break;
1280 }
1281 case Statement::kIf_Kind: {
1282 IfStatement& i = (IfStatement&) *stmt;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001283 if (i.fTest->fKind == Expression::kBoolLiteral_Kind) {
1284 // constant if, collapse down to a single branch
1285 if (((BoolLiteral&) *i.fTest).fValue) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001286 SkASSERT(i.fIfTrue);
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001287 (*iter)->setStatement(std::move(i.fIfTrue));
1288 } else {
1289 if (i.fIfFalse) {
1290 (*iter)->setStatement(std::move(i.fIfFalse));
1291 } else {
1292 (*iter)->setStatement(std::unique_ptr<Statement>(new Nop()));
1293 }
1294 }
1295 *outUpdated = true;
1296 *outNeedsRescan = true;
1297 break;
1298 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001299 if (i.fIfFalse && i.fIfFalse->isEmpty()) {
1300 // else block doesn't do anything, remove it
1301 i.fIfFalse.reset();
1302 *outUpdated = true;
1303 *outNeedsRescan = true;
1304 }
1305 if (!i.fIfFalse && i.fIfTrue->isEmpty()) {
1306 // if block doesn't do anything, no else block
1307 if (i.fTest->hasSideEffects()) {
1308 // test has side effects, keep it
1309 (*iter)->setStatement(std::unique_ptr<Statement>(
1310 new ExpressionStatement(std::move(i.fTest))));
1311 } else {
1312 // no if, no else, no test side effects, kill the whole if
1313 // statement
1314 (*iter)->setStatement(std::unique_ptr<Statement>(new Nop()));
1315 }
1316 *outUpdated = true;
1317 *outNeedsRescan = true;
1318 }
1319 break;
1320 }
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001321 case Statement::kSwitch_Kind: {
1322 SwitchStatement& s = (SwitchStatement&) *stmt;
Brian Osmanb6b95732020-06-30 11:44:27 -04001323 if (s.fValue->isCompileTimeConstant()) {
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001324 // switch is constant, replace it with the case that matches
1325 bool found = false;
1326 SwitchCase* defaultCase = nullptr;
1327 for (const auto& c : s.fCases) {
1328 if (!c->fValue) {
1329 defaultCase = c.get();
1330 continue;
1331 }
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001332 SkASSERT(c->fValue->fKind == s.fValue->fKind);
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001333 found = c->fValue->compareConstant(*fContext, *s.fValue);
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001334 if (found) {
1335 std::unique_ptr<Statement> newBlock = block_for_case(&s, c.get());
1336 if (newBlock) {
1337 (*iter)->setStatement(std::move(newBlock));
1338 break;
1339 } else {
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -04001340 if (s.fIsStatic && !(fFlags & kPermitInvalidStaticTests_Flag)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001341 this->error(s.fOffset,
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001342 "static switch contains non-static conditional break");
1343 s.fIsStatic = false;
1344 }
1345 return; // can't simplify
1346 }
1347 }
1348 }
1349 if (!found) {
1350 // no matching case. use default if it exists, or kill the whole thing
1351 if (defaultCase) {
1352 std::unique_ptr<Statement> newBlock = block_for_case(&s, defaultCase);
1353 if (newBlock) {
1354 (*iter)->setStatement(std::move(newBlock));
1355 } else {
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -04001356 if (s.fIsStatic && !(fFlags & kPermitInvalidStaticTests_Flag)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001357 this->error(s.fOffset,
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001358 "static switch contains non-static conditional break");
1359 s.fIsStatic = false;
1360 }
1361 return; // can't simplify
1362 }
1363 } else {
1364 (*iter)->setStatement(std::unique_ptr<Statement>(new Nop()));
1365 }
1366 }
1367 *outUpdated = true;
1368 *outNeedsRescan = true;
1369 }
1370 break;
1371 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001372 case Statement::kExpression_Kind: {
1373 ExpressionStatement& e = (ExpressionStatement&) *stmt;
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001374 SkASSERT((*iter)->statement()->get() == &e);
Ethan Nicholascb670962017-04-20 19:31:52 -04001375 if (!e.fExpression->hasSideEffects()) {
1376 // Expression statement with no side effects, kill it
1377 if (!b.tryRemoveExpressionBefore(iter, e.fExpression.get())) {
1378 *outNeedsRescan = true;
1379 }
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001380 SkASSERT((*iter)->statement()->get() == stmt);
Ethan Nicholascb670962017-04-20 19:31:52 -04001381 (*iter)->setStatement(std::unique_ptr<Statement>(new Nop()));
1382 *outUpdated = true;
1383 }
1384 break;
1385 }
1386 default:
1387 break;
1388 }
1389}
1390
1391void Compiler::scanCFG(FunctionDefinition& f) {
1392 CFG cfg = CFGGenerator().getCFG(f);
1393 this->computeDataFlow(&cfg);
ethannicholas22f939e2016-10-13 13:25:34 -07001394
1395 // check for unreachable code
1396 for (size_t i = 0; i < cfg.fBlocks.size(); i++) {
Mike Klein6ad99092016-10-26 10:35:22 -04001397 if (i != cfg.fStart && !cfg.fBlocks[i].fEntrances.size() &&
ethannicholas22f939e2016-10-13 13:25:34 -07001398 cfg.fBlocks[i].fNodes.size()) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001399 int offset;
Ethan Nicholas86a43402017-01-19 13:32:00 -05001400 switch (cfg.fBlocks[i].fNodes[0].fKind) {
1401 case BasicBlock::Node::kStatement_Kind:
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001402 offset = (*cfg.fBlocks[i].fNodes[0].statement())->fOffset;
Ethan Nicholas86a43402017-01-19 13:32:00 -05001403 break;
1404 case BasicBlock::Node::kExpression_Kind:
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001405 offset = (*cfg.fBlocks[i].fNodes[0].expression())->fOffset;
Ethan Nicholas70728ef2020-05-28 07:09:00 -04001406 if ((*cfg.fBlocks[i].fNodes[0].expression())->fKind ==
1407 Expression::kBoolLiteral_Kind) {
1408 // Function inlining can generate do { ... } while(false) loops which always
1409 // break, so the boolean condition is considered unreachable. Since not
1410 // being able to reach a literal is a non-issue in the first place, we
1411 // don't report an error in this case.
1412 continue;
1413 }
Ethan Nicholas86a43402017-01-19 13:32:00 -05001414 break;
1415 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001416 this->error(offset, String("unreachable"));
ethannicholas22f939e2016-10-13 13:25:34 -07001417 }
1418 }
1419 if (fErrorCount) {
1420 return;
1421 }
1422
Ethan Nicholascb670962017-04-20 19:31:52 -04001423 // check for dead code & undefined variables, perform constant propagation
1424 std::unordered_set<const Variable*> undefinedVariables;
1425 bool updated;
1426 bool needsRescan = false;
1427 do {
1428 if (needsRescan) {
1429 cfg = CFGGenerator().getCFG(f);
1430 this->computeDataFlow(&cfg);
1431 needsRescan = false;
Ethan Nicholas113628d2017-02-02 16:11:39 -05001432 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001433
1434 updated = false;
Ethan Nicholas1de14812020-06-19 15:32:49 -04001435 bool first = true;
Ethan Nicholascb670962017-04-20 19:31:52 -04001436 for (BasicBlock& b : cfg.fBlocks) {
Ethan Nicholas1de14812020-06-19 15:32:49 -04001437 if (!first && b.fEntrances.empty()) {
1438 // Block was reachable before optimization, but has since become unreachable. In
1439 // addition to being dead code, it's broken - since control flow can't reach it, no
1440 // prior variable definitions can reach it, and therefore variables might look to
1441 // have not been properly assigned. Kill it.
1442 for (BasicBlock::Node& node : b.fNodes) {
1443 if (node.fKind == BasicBlock::Node::kStatement_Kind &&
1444 (*node.statement())->fKind != Statement::kNop_Kind) {
1445 node.setStatement(std::unique_ptr<Statement>(new Nop()));
1446 }
1447 }
1448 continue;
1449 }
1450 first = false;
Ethan Nicholascb670962017-04-20 19:31:52 -04001451 DefinitionMap definitions = b.fBefore;
1452
1453 for (auto iter = b.fNodes.begin(); iter != b.fNodes.end() && !needsRescan; ++iter) {
1454 if (iter->fKind == BasicBlock::Node::kExpression_Kind) {
1455 this->simplifyExpression(definitions, b, &iter, &undefinedVariables, &updated,
1456 &needsRescan);
1457 } else {
1458 this->simplifyStatement(definitions, b, &iter, &undefinedVariables, &updated,
1459 &needsRescan);
1460 }
Ethan Nicholas4b330df2017-05-17 10:52:55 -04001461 if (needsRescan) {
1462 break;
1463 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001464 this->addDefinitions(*iter, &definitions);
1465 }
1466 }
1467 } while (updated);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001468 SkASSERT(!needsRescan);
ethannicholas22f939e2016-10-13 13:25:34 -07001469
Ethan Nicholas91a10532017-06-22 11:24:38 -04001470 // verify static ifs & switches, clean up dead variable decls
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001471 for (BasicBlock& b : cfg.fBlocks) {
1472 DefinitionMap definitions = b.fBefore;
1473
Ethan Nicholas91a10532017-06-22 11:24:38 -04001474 for (auto iter = b.fNodes.begin(); iter != b.fNodes.end() && !needsRescan;) {
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001475 if (iter->fKind == BasicBlock::Node::kStatement_Kind) {
1476 const Statement& s = **iter->statement();
1477 switch (s.fKind) {
1478 case Statement::kIf_Kind:
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -04001479 if (((const IfStatement&) s).fIsStatic &&
1480 !(fFlags & kPermitInvalidStaticTests_Flag)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001481 this->error(s.fOffset, "static if has non-static test");
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001482 }
Ethan Nicholas91a10532017-06-22 11:24:38 -04001483 ++iter;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001484 break;
1485 case Statement::kSwitch_Kind:
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -04001486 if (((const SwitchStatement&) s).fIsStatic &&
1487 !(fFlags & kPermitInvalidStaticTests_Flag)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001488 this->error(s.fOffset, "static switch has non-static test");
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001489 }
Ethan Nicholas91a10532017-06-22 11:24:38 -04001490 ++iter;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001491 break;
Ethan Nicholas82a62d22017-11-07 14:42:10 +00001492 case Statement::kVarDeclarations_Kind: {
1493 VarDeclarations& decls = *((VarDeclarationsStatement&) s).fDeclaration;
1494 for (auto varIter = decls.fVars.begin(); varIter != decls.fVars.end();) {
1495 if ((*varIter)->fKind == Statement::kNop_Kind) {
1496 varIter = decls.fVars.erase(varIter);
1497 } else {
1498 ++varIter;
1499 }
1500 }
1501 if (!decls.fVars.size()) {
1502 iter = b.fNodes.erase(iter);
1503 } else {
1504 ++iter;
1505 }
1506 break;
1507 }
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001508 default:
Ethan Nicholas91a10532017-06-22 11:24:38 -04001509 ++iter;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001510 break;
1511 }
Ethan Nicholas91a10532017-06-22 11:24:38 -04001512 } else {
1513 ++iter;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001514 }
1515 }
1516 }
1517
ethannicholas22f939e2016-10-13 13:25:34 -07001518 // check for missing return
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001519 if (f.fDeclaration.fReturnType != *fContext->fVoid_Type) {
ethannicholas22f939e2016-10-13 13:25:34 -07001520 if (cfg.fBlocks[cfg.fExit].fEntrances.size()) {
Ethan Nicholasdb80f692019-11-22 14:06:12 -05001521 this->error(f.fOffset, String("function '" + String(f.fDeclaration.fName) +
1522 "' can exit without returning a value"));
ethannicholas22f939e2016-10-13 13:25:34 -07001523 }
1524 }
1525}
1526
Ethan Nicholas91164d12019-05-15 15:29:54 -04001527void Compiler::registerExternalValue(ExternalValue* value) {
1528 fIRGenerator->fRootSymbolTable->addWithoutOwnership(value->fName, value);
1529}
1530
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001531const Symbol* Compiler::takeOwnership(std::unique_ptr<const Symbol> symbol) {
John Stiles3ae071e2020-08-05 15:29:29 -04001532 return fIRGenerator->fRootSymbolTable->takeOwnershipOfSymbol(std::move(symbol));
Ethan Nicholas91164d12019-05-15 15:29:54 -04001533}
1534
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001535std::unique_ptr<Program> Compiler::convertProgram(Program::Kind kind, String text,
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001536 const Program::Settings& settings) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001537 fErrorText = "";
1538 fErrorCount = 0;
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001539 std::vector<std::unique_ptr<ProgramElement>>* inherited;
ethannicholasd598f792016-07-25 10:08:54 -07001540 std::vector<std::unique_ptr<ProgramElement>> elements;
ethannicholasb3058bd2016-07-01 08:22:01 -07001541 switch (kind) {
1542 case Program::kVertex_Kind:
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001543 inherited = &fVertexInclude;
1544 fIRGenerator->fSymbolTable = fVertexSymbolTable;
Ethan Nicholasdb80f692019-11-22 14:06:12 -05001545 fIRGenerator->fIntrinsics = &fGPUIntrinsics;
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001546 fIRGenerator->start(&settings, inherited);
ethannicholasb3058bd2016-07-01 08:22:01 -07001547 break;
1548 case Program::kFragment_Kind:
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001549 inherited = &fFragmentInclude;
1550 fIRGenerator->fSymbolTable = fFragmentSymbolTable;
Ethan Nicholasdb80f692019-11-22 14:06:12 -05001551 fIRGenerator->fIntrinsics = &fGPUIntrinsics;
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001552 fIRGenerator->start(&settings, inherited);
ethannicholasb3058bd2016-07-01 08:22:01 -07001553 break;
Ethan Nicholas52cad152017-02-16 16:37:32 -05001554 case Program::kGeometry_Kind:
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001555 this->loadGeometryIntrinsics();
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001556 inherited = &fGeometryInclude;
1557 fIRGenerator->fSymbolTable = fGeometrySymbolTable;
Ethan Nicholasdb80f692019-11-22 14:06:12 -05001558 fIRGenerator->fIntrinsics = &fGPUIntrinsics;
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001559 fIRGenerator->start(&settings, inherited);
Ethan Nicholas52cad152017-02-16 16:37:32 -05001560 break;
Ethan Nicholas762466e2017-06-29 10:03:38 -04001561 case Program::kFragmentProcessor_Kind:
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001562#if REHYDRATE
1563 {
1564 Rehydrator rehydrator(fContext.get(), fGpuSymbolTable, this,
1565 SKSL_INCLUDE_sksl_fp,
1566 SKSL_INCLUDE_sksl_fp_LENGTH);
1567 fFPSymbolTable = rehydrator.symbolTable();
1568 fFPInclude = rehydrator.elements();
1569 }
1570 inherited = &fFPInclude;
1571 fIRGenerator->fSymbolTable = fFPSymbolTable;
1572 fIRGenerator->fIntrinsics = &fGPUIntrinsics;
1573 fIRGenerator->start(&settings, inherited);
1574 break;
1575#else
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001576 inherited = nullptr;
Ethan Nicholas8da1e652019-05-24 11:01:59 -04001577 fIRGenerator->fSymbolTable = fGpuSymbolTable;
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001578 fIRGenerator->start(&settings, nullptr, true);
Ethan Nicholasdb80f692019-11-22 14:06:12 -05001579 fIRGenerator->fIntrinsics = &fGPUIntrinsics;
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001580 fIRGenerator->convertProgram(kind, SKSL_FP_INCLUDE, strlen(SKSL_FP_INCLUDE), &elements);
1581 fIRGenerator->fIsBuiltinCode = false;
Ethan Nicholas762466e2017-06-29 10:03:38 -04001582 break;
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001583#endif
Ethan Nicholas8da1e652019-05-24 11:01:59 -04001584 case Program::kPipelineStage_Kind:
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001585 this->loadPipelineIntrinsics();
Ethan Nicholas8da1e652019-05-24 11:01:59 -04001586 inherited = &fPipelineInclude;
1587 fIRGenerator->fSymbolTable = fPipelineSymbolTable;
Ethan Nicholasdb80f692019-11-22 14:06:12 -05001588 fIRGenerator->fIntrinsics = &fGPUIntrinsics;
Ethan Nicholas8da1e652019-05-24 11:01:59 -04001589 fIRGenerator->start(&settings, inherited);
1590 break;
Ethan Nicholas746035a2019-04-23 13:31:09 -04001591 case Program::kGeneric_Kind:
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001592 this->loadInterpreterIntrinsics();
Ethan Nicholas8da1e652019-05-24 11:01:59 -04001593 inherited = &fInterpreterInclude;
1594 fIRGenerator->fSymbolTable = fInterpreterSymbolTable;
Ethan Nicholasdb80f692019-11-22 14:06:12 -05001595 fIRGenerator->fIntrinsics = &fInterpreterIntrinsics;
Ethan Nicholas8da1e652019-05-24 11:01:59 -04001596 fIRGenerator->start(&settings, inherited);
Ethan Nicholas0d997662019-04-08 09:46:01 -04001597 break;
ethannicholasb3058bd2016-07-01 08:22:01 -07001598 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001599 std::unique_ptr<String> textPtr(new String(std::move(text)));
1600 fSource = textPtr.get();
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001601 fIRGenerator->convertProgram(kind, textPtr->c_str(), textPtr->size(), &elements);
John Stilesfbd050b2020-08-03 13:21:46 -04001602 auto result = std::make_unique<Program>(kind,
1603 std::move(textPtr),
1604 settings,
1605 fContext,
1606 inherited,
1607 std::move(elements),
1608 fIRGenerator->fSymbolTable,
1609 fIRGenerator->fInputs);
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001610 if (fErrorCount) {
1611 return nullptr;
1612 }
1613 return result;
1614}
1615
Ethan Nicholas00543112018-07-31 09:44:36 -04001616bool Compiler::optimize(Program& program) {
1617 SkASSERT(!fErrorCount);
1618 if (!program.fIsOptimized) {
1619 program.fIsOptimized = true;
1620 fIRGenerator->fKind = program.fKind;
1621 fIRGenerator->fSettings = &program.fSettings;
1622 for (auto& element : program) {
1623 if (element.fKind == ProgramElement::kFunction_Kind) {
1624 this->scanCFG((FunctionDefinition&) element);
1625 }
1626 }
Ethan Nicholase8ad02c2020-06-03 16:58:20 -04001627 // we wait until after analysis to remove dead functions so that we still report errors
1628 // even in unused code
1629 if (program.fSettings.fRemoveDeadFunctions) {
1630 for (auto iter = program.fElements.begin(); iter != program.fElements.end(); ) {
1631 if ((*iter)->fKind == ProgramElement::kFunction_Kind) {
1632 const FunctionDefinition& f = (const FunctionDefinition&) **iter;
1633 if (!f.fDeclaration.fCallCount && f.fDeclaration.fName != "main") {
1634 iter = program.fElements.erase(iter);
1635 continue;
1636 }
1637 }
1638 ++iter;
1639 }
1640 }
Ethan Nicholas0dc80872019-02-08 15:46:24 -05001641 if (program.fKind != Program::kFragmentProcessor_Kind) {
1642 for (auto iter = program.fElements.begin(); iter != program.fElements.end();) {
1643 if ((*iter)->fKind == ProgramElement::kVar_Kind) {
1644 VarDeclarations& vars = (VarDeclarations&) **iter;
1645 for (auto varIter = vars.fVars.begin(); varIter != vars.fVars.end();) {
1646 const Variable& var = *((VarDeclaration&) **varIter).fVar;
1647 if (var.dead()) {
1648 varIter = vars.fVars.erase(varIter);
1649 } else {
1650 ++varIter;
1651 }
1652 }
1653 if (vars.fVars.size() == 0) {
1654 iter = program.fElements.erase(iter);
1655 continue;
1656 }
1657 }
1658 ++iter;
1659 }
1660 }
Ethan Nicholas00543112018-07-31 09:44:36 -04001661 }
1662 return fErrorCount == 0;
1663}
1664
1665std::unique_ptr<Program> Compiler::specialize(
1666 Program& program,
1667 const std::unordered_map<SkSL::String, SkSL::Program::Settings::Value>& inputs) {
1668 std::vector<std::unique_ptr<ProgramElement>> elements;
1669 for (const auto& e : program) {
1670 elements.push_back(e.clone());
1671 }
1672 Program::Settings settings;
1673 settings.fCaps = program.fSettings.fCaps;
1674 for (auto iter = inputs.begin(); iter != inputs.end(); ++iter) {
1675 settings.fArgs.insert(*iter);
1676 }
Brian Osman808f0212020-01-21 15:36:47 -05001677 std::unique_ptr<String> sourceCopy(new String(*program.fSource));
Ethan Nicholas00543112018-07-31 09:44:36 -04001678 std::unique_ptr<Program> result(new Program(program.fKind,
Brian Osman808f0212020-01-21 15:36:47 -05001679 std::move(sourceCopy),
Ethan Nicholas00543112018-07-31 09:44:36 -04001680 settings,
1681 program.fContext,
1682 program.fInheritedElements,
1683 std::move(elements),
1684 program.fSymbols,
1685 program.fInputs));
1686 return result;
1687}
1688
Brian Osmanfb32ddf2019-06-18 10:14:20 -04001689#if defined(SKSL_STANDALONE) || SK_SUPPORT_GPU
1690
Ethan Nicholas00543112018-07-31 09:44:36 -04001691bool Compiler::toSPIRV(Program& program, OutputStream& out) {
1692 if (!this->optimize(program)) {
1693 return false;
1694 }
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001695#ifdef SK_ENABLE_SPIRV_VALIDATION
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001696 StringStream buffer;
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001697 fSource = program.fSource.get();
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001698 SPIRVCodeGenerator cg(fContext.get(), &program, this, &buffer);
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001699 bool result = cg.generateCode();
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001700 fSource = nullptr;
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001701 if (result) {
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001702 spvtools::SpirvTools tools(SPV_ENV_VULKAN_1_0);
Ethan Nicholas762466e2017-06-29 10:03:38 -04001703 const String& data = buffer.str();
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001704 SkASSERT(0 == data.size() % 4);
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001705 auto dumpmsg = [](spv_message_level_t, const char*, const spv_position_t&, const char* m) {
1706 SkDebugf("SPIR-V validation error: %s\n", m);
1707 };
1708 tools.SetMessageConsumer(dumpmsg);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001709 // Verify that the SPIR-V we produced is valid. If this SkASSERT fails, check the logs prior
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001710 // to the failure to see the validation errors.
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001711 SkAssertResult(tools.Validate((const uint32_t*) data.c_str(), data.size() / 4));
Ethan Nicholas762466e2017-06-29 10:03:38 -04001712 out.write(data.c_str(), data.size());
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001713 }
1714#else
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001715 fSource = program.fSource.get();
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001716 SPIRVCodeGenerator cg(fContext.get(), &program, this, &out);
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001717 bool result = cg.generateCode();
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001718 fSource = nullptr;
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001719#endif
Ethan Nicholasce33f102016-12-09 17:22:59 -05001720 return result;
1721}
1722
Ethan Nicholas00543112018-07-31 09:44:36 -04001723bool Compiler::toSPIRV(Program& program, String* out) {
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001724 StringStream buffer;
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001725 bool result = this->toSPIRV(program, buffer);
1726 if (result) {
Ethan Nicholas762466e2017-06-29 10:03:38 -04001727 *out = buffer.str();
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001728 }
1729 return result;
1730}
1731
Ethan Nicholas00543112018-07-31 09:44:36 -04001732bool Compiler::toGLSL(Program& program, OutputStream& out) {
1733 if (!this->optimize(program)) {
1734 return false;
1735 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001736 fSource = program.fSource.get();
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001737 GLSLCodeGenerator cg(fContext.get(), &program, this, &out);
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001738 bool result = cg.generateCode();
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001739 fSource = nullptr;
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001740 return result;
1741}
1742
Ethan Nicholas00543112018-07-31 09:44:36 -04001743bool Compiler::toGLSL(Program& program, String* out) {
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001744 StringStream buffer;
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001745 bool result = this->toGLSL(program, buffer);
1746 if (result) {
Ethan Nicholas762466e2017-06-29 10:03:38 -04001747 *out = buffer.str();
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001748 }
1749 return result;
1750}
1751
Brian Osmanc0243912020-02-19 15:35:26 -05001752bool Compiler::toHLSL(Program& program, String* out) {
1753 String spirv;
1754 if (!this->toSPIRV(program, &spirv)) {
1755 return false;
1756 }
1757
1758 return SPIRVtoHLSL(spirv, out);
1759}
1760
Ethan Nicholas00543112018-07-31 09:44:36 -04001761bool Compiler::toMetal(Program& program, OutputStream& out) {
1762 if (!this->optimize(program)) {
1763 return false;
1764 }
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001765 MetalCodeGenerator cg(fContext.get(), &program, this, &out);
Ethan Nicholascc305772017-10-13 16:17:45 -04001766 bool result = cg.generateCode();
Ethan Nicholascc305772017-10-13 16:17:45 -04001767 return result;
1768}
1769
Ethan Nicholas00543112018-07-31 09:44:36 -04001770bool Compiler::toMetal(Program& program, String* out) {
1771 if (!this->optimize(program)) {
1772 return false;
1773 }
Timothy Liangb8eeb802018-07-23 16:46:16 -04001774 StringStream buffer;
1775 bool result = this->toMetal(program, buffer);
1776 if (result) {
1777 *out = buffer.str();
1778 }
1779 return result;
1780}
1781
Ethan Nicholas00543112018-07-31 09:44:36 -04001782bool Compiler::toCPP(Program& program, String name, OutputStream& out) {
1783 if (!this->optimize(program)) {
1784 return false;
1785 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001786 fSource = program.fSource.get();
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001787 CPPCodeGenerator cg(fContext.get(), &program, this, name, &out);
Ethan Nicholas762466e2017-06-29 10:03:38 -04001788 bool result = cg.generateCode();
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001789 fSource = nullptr;
Ethan Nicholas762466e2017-06-29 10:03:38 -04001790 return result;
1791}
1792
Ethan Nicholas00543112018-07-31 09:44:36 -04001793bool Compiler::toH(Program& program, String name, OutputStream& out) {
1794 if (!this->optimize(program)) {
1795 return false;
1796 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001797 fSource = program.fSource.get();
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001798 HCodeGenerator cg(fContext.get(), &program, this, name, &out);
Ethan Nicholas762466e2017-06-29 10:03:38 -04001799 bool result = cg.generateCode();
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001800 fSource = nullptr;
Ethan Nicholas00543112018-07-31 09:44:36 -04001801 return result;
1802}
1803
Brian Osman2e29ab52019-09-20 12:19:11 -04001804#endif
1805
1806#if !defined(SKSL_STANDALONE) && SK_SUPPORT_GPU
Brian Osman107c6662019-12-30 15:02:30 -05001807bool Compiler::toPipelineStage(const Program& program, PipelineStageArgs* outArgs) {
Ethan Nicholas00543112018-07-31 09:44:36 -04001808 SkASSERT(program.fIsOptimized);
1809 fSource = program.fSource.get();
1810 StringStream buffer;
Brian Osman300fe1d2020-01-23 15:42:43 -05001811 PipelineStageCodeGenerator cg(fContext.get(), &program, this, &buffer, outArgs);
Ethan Nicholas00543112018-07-31 09:44:36 -04001812 bool result = cg.generateCode();
1813 fSource = nullptr;
1814 if (result) {
Brian Osman107c6662019-12-30 15:02:30 -05001815 outArgs->fCode = buffer.str();
Ethan Nicholas00543112018-07-31 09:44:36 -04001816 }
Ethan Nicholas762466e2017-06-29 10:03:38 -04001817 return result;
1818}
Brian Osmanfb32ddf2019-06-18 10:14:20 -04001819#endif
1820
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001821std::unique_ptr<ByteCode> Compiler::toByteCode(Program& program) {
Brian Osman489cf882019-07-09 10:48:28 -04001822#if defined(SK_ENABLE_SKSL_INTERPRETER)
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001823 if (!this->optimize(program)) {
1824 return nullptr;
1825 }
Brian Osman808f0212020-01-21 15:36:47 -05001826 fSource = program.fSource.get();
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001827 std::unique_ptr<ByteCode> result(new ByteCode());
Brian Osmanb08cc022020-04-02 11:38:40 -04001828 ByteCodeGenerator cg(fContext.get(), &program, this, result.get());
1829 bool success = cg.generateCode();
1830 fSource = nullptr;
1831 if (success) {
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001832 return result;
1833 }
Brian Osman489cf882019-07-09 10:48:28 -04001834#else
1835 ABORT("ByteCode interpreter not enabled");
1836#endif
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001837 return nullptr;
1838}
1839
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001840const char* Compiler::OperatorName(Token::Kind kind) {
1841 switch (kind) {
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001842 case Token::Kind::TK_PLUS: return "+";
1843 case Token::Kind::TK_MINUS: return "-";
1844 case Token::Kind::TK_STAR: return "*";
1845 case Token::Kind::TK_SLASH: return "/";
1846 case Token::Kind::TK_PERCENT: return "%";
1847 case Token::Kind::TK_SHL: return "<<";
1848 case Token::Kind::TK_SHR: return ">>";
1849 case Token::Kind::TK_LOGICALNOT: return "!";
1850 case Token::Kind::TK_LOGICALAND: return "&&";
1851 case Token::Kind::TK_LOGICALOR: return "||";
1852 case Token::Kind::TK_LOGICALXOR: return "^^";
1853 case Token::Kind::TK_BITWISENOT: return "~";
1854 case Token::Kind::TK_BITWISEAND: return "&";
1855 case Token::Kind::TK_BITWISEOR: return "|";
1856 case Token::Kind::TK_BITWISEXOR: return "^";
1857 case Token::Kind::TK_EQ: return "=";
1858 case Token::Kind::TK_EQEQ: return "==";
1859 case Token::Kind::TK_NEQ: return "!=";
1860 case Token::Kind::TK_LT: return "<";
1861 case Token::Kind::TK_GT: return ">";
1862 case Token::Kind::TK_LTEQ: return "<=";
1863 case Token::Kind::TK_GTEQ: return ">=";
1864 case Token::Kind::TK_PLUSEQ: return "+=";
1865 case Token::Kind::TK_MINUSEQ: return "-=";
1866 case Token::Kind::TK_STAREQ: return "*=";
1867 case Token::Kind::TK_SLASHEQ: return "/=";
1868 case Token::Kind::TK_PERCENTEQ: return "%=";
1869 case Token::Kind::TK_SHLEQ: return "<<=";
1870 case Token::Kind::TK_SHREQ: return ">>=";
1871 case Token::Kind::TK_LOGICALANDEQ: return "&&=";
1872 case Token::Kind::TK_LOGICALOREQ: return "||=";
1873 case Token::Kind::TK_LOGICALXOREQ: return "^^=";
1874 case Token::Kind::TK_BITWISEANDEQ: return "&=";
1875 case Token::Kind::TK_BITWISEOREQ: return "|=";
1876 case Token::Kind::TK_BITWISEXOREQ: return "^=";
1877 case Token::Kind::TK_PLUSPLUS: return "++";
1878 case Token::Kind::TK_MINUSMINUS: return "--";
1879 case Token::Kind::TK_COMMA: return ",";
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001880 default:
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001881 ABORT("unsupported operator: %d\n", (int) kind);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001882 }
1883}
1884
1885
1886bool Compiler::IsAssignment(Token::Kind op) {
1887 switch (op) {
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001888 case Token::Kind::TK_EQ: // fall through
1889 case Token::Kind::TK_PLUSEQ: // fall through
1890 case Token::Kind::TK_MINUSEQ: // fall through
1891 case Token::Kind::TK_STAREQ: // fall through
1892 case Token::Kind::TK_SLASHEQ: // fall through
1893 case Token::Kind::TK_PERCENTEQ: // fall through
1894 case Token::Kind::TK_SHLEQ: // fall through
1895 case Token::Kind::TK_SHREQ: // fall through
1896 case Token::Kind::TK_BITWISEOREQ: // fall through
1897 case Token::Kind::TK_BITWISEXOREQ: // fall through
1898 case Token::Kind::TK_BITWISEANDEQ: // fall through
1899 case Token::Kind::TK_LOGICALOREQ: // fall through
1900 case Token::Kind::TK_LOGICALXOREQ: // fall through
1901 case Token::Kind::TK_LOGICALANDEQ:
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001902 return true;
1903 default:
1904 return false;
1905 }
1906}
1907
1908Position Compiler::position(int offset) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001909 SkASSERT(fSource);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001910 int line = 1;
1911 int column = 1;
1912 for (int i = 0; i < offset; i++) {
1913 if ((*fSource)[i] == '\n') {
1914 ++line;
1915 column = 1;
1916 }
1917 else {
1918 ++column;
1919 }
1920 }
1921 return Position(line, column);
1922}
1923
1924void Compiler::error(int offset, String msg) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001925 fErrorCount++;
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001926 Position pos = this->position(offset);
1927 fErrorText += "error: " + to_string(pos.fLine) + ": " + msg.c_str() + "\n";
ethannicholasb3058bd2016-07-01 08:22:01 -07001928}
1929
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001930String Compiler::errorText() {
Ethan Nicholas00543112018-07-31 09:44:36 -04001931 this->writeErrorCount();
1932 fErrorCount = 0;
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001933 String result = fErrorText;
ethannicholasb3058bd2016-07-01 08:22:01 -07001934 return result;
1935}
1936
1937void Compiler::writeErrorCount() {
1938 if (fErrorCount) {
1939 fErrorText += to_string(fErrorCount) + " error";
1940 if (fErrorCount > 1) {
1941 fErrorText += "s";
1942 }
1943 fErrorText += "\n";
1944 }
1945}
1946
John Stilesa6841be2020-08-06 14:11:56 -04001947} // namespace SkSL