blob: cfd652067bbfd3770107566c43b63ced7b9c9a3d [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 Nicholasb33fa3f2020-08-06 13:00:19 -040035#include <fstream>
36
Ethan Nicholasa11035b2019-11-26 16:27:47 -050037#if !defined(SKSL_STANDALONE) & SK_SUPPORT_GPU
38#include "include/gpu/GrContextOptions.h"
39#include "src/gpu/GrShaderCaps.h"
40#endif
41
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -040042#ifdef SK_ENABLE_SPIRV_VALIDATION
43#include "spirv-tools/libspirv.hpp"
44#endif
45
Brian Osmandd496172020-08-08 08:17:18 -040046#if !SKSL_STANDALONE
Ethan Nicholasc18bb512020-07-28 14:46:53 -040047
48#include "src/sksl/generated/sksl_fp.dehydrated.sksl"
49#include "src/sksl/generated/sksl_frag.dehydrated.sksl"
50#include "src/sksl/generated/sksl_geom.dehydrated.sksl"
51#include "src/sksl/generated/sksl_gpu.dehydrated.sksl"
52#include "src/sksl/generated/sksl_interp.dehydrated.sksl"
53#include "src/sksl/generated/sksl_pipeline.dehydrated.sksl"
54#include "src/sksl/generated/sksl_vert.dehydrated.sksl"
55
56#else
57
Brian Osmandd496172020-08-08 08:17:18 -040058// GN generates or copies all of these files to the skslc executable directory
59static const char SKSL_GPU_INCLUDE[] = "sksl_gpu.sksl";
60static const char SKSL_INTERP_INCLUDE[] = "sksl_interp.sksl";
61static const char SKSL_VERT_INCLUDE[] = "sksl_vert.sksl";
62static const char SKSL_FRAG_INCLUDE[] = "sksl_frag.sksl";
63static const char SKSL_GEOM_INCLUDE[] = "sksl_geom.sksl";
64static const char SKSL_FP_INCLUDE[] = "sksl_fp.sksl";
65static const char SKSL_PIPELINE_INCLUDE[] = "sksl_pipeline.sksl";
Ethan Nicholasc18bb512020-07-28 14:46:53 -040066
67#endif
Ethan Nicholas0d997662019-04-08 09:46:01 -040068
ethannicholasb3058bd2016-07-01 08:22:01 -070069namespace SkSL {
70
Ethan Nicholasdb80f692019-11-22 14:06:12 -050071static void grab_intrinsics(std::vector<std::unique_ptr<ProgramElement>>* src,
Brian Osman08f986d2020-05-13 17:06:46 -040072 std::map<String, std::pair<std::unique_ptr<ProgramElement>, bool>>* target) {
73 for (auto iter = src->begin(); iter != src->end(); ) {
74 std::unique_ptr<ProgramElement>& element = *iter;
Ethan Nicholasdb80f692019-11-22 14:06:12 -050075 switch (element->fKind) {
76 case ProgramElement::kFunction_Kind: {
77 FunctionDefinition& f = (FunctionDefinition&) *element;
Brian Osman08f986d2020-05-13 17:06:46 -040078 SkASSERT(f.fDeclaration.fBuiltin);
Ethan Nicholasc18bb512020-07-28 14:46:53 -040079 String key = f.fDeclaration.description();
Brian Osman08f986d2020-05-13 17:06:46 -040080 SkASSERT(target->find(key) == target->end());
81 (*target)[key] = std::make_pair(std::move(element), false);
82 iter = src->erase(iter);
Ethan Nicholasdb80f692019-11-22 14:06:12 -050083 break;
84 }
85 case ProgramElement::kEnum_Kind: {
86 Enum& e = (Enum&) *element;
87 StringFragment name = e.fTypeName;
88 SkASSERT(target->find(name) == target->end());
89 (*target)[name] = std::make_pair(std::move(element), false);
Brian Osman08f986d2020-05-13 17:06:46 -040090 iter = src->erase(iter);
Ethan Nicholasdb80f692019-11-22 14:06:12 -050091 break;
92 }
93 default:
94 printf("unsupported include file element\n");
95 SkASSERT(false);
96 }
97 }
98}
99
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -0400100Compiler::Compiler(Flags flags)
101: fFlags(flags)
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400102, fContext(new Context())
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -0400103, fErrorCount(0) {
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400104 auto symbols = std::shared_ptr<SymbolTable>(new SymbolTable(this));
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400105 fIRGenerator = new IRGenerator(fContext.get(), symbols, *this);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400106 #define ADD_TYPE(t) symbols->addWithoutOwnership(fContext->f ## t ## _Type->fName, \
107 fContext->f ## t ## _Type.get())
ethannicholasb3058bd2016-07-01 08:22:01 -0700108 ADD_TYPE(Void);
109 ADD_TYPE(Float);
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400110 ADD_TYPE(Float2);
111 ADD_TYPE(Float3);
112 ADD_TYPE(Float4);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400113 ADD_TYPE(Half);
114 ADD_TYPE(Half2);
115 ADD_TYPE(Half3);
116 ADD_TYPE(Half4);
ethannicholasb3058bd2016-07-01 08:22:01 -0700117 ADD_TYPE(Int);
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400118 ADD_TYPE(Int2);
119 ADD_TYPE(Int3);
120 ADD_TYPE(Int4);
ethannicholasb3058bd2016-07-01 08:22:01 -0700121 ADD_TYPE(UInt);
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400122 ADD_TYPE(UInt2);
123 ADD_TYPE(UInt3);
124 ADD_TYPE(UInt4);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400125 ADD_TYPE(Short);
126 ADD_TYPE(Short2);
127 ADD_TYPE(Short3);
128 ADD_TYPE(Short4);
129 ADD_TYPE(UShort);
130 ADD_TYPE(UShort2);
131 ADD_TYPE(UShort3);
132 ADD_TYPE(UShort4);
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400133 ADD_TYPE(Byte);
134 ADD_TYPE(Byte2);
135 ADD_TYPE(Byte3);
136 ADD_TYPE(Byte4);
137 ADD_TYPE(UByte);
138 ADD_TYPE(UByte2);
139 ADD_TYPE(UByte3);
140 ADD_TYPE(UByte4);
ethannicholasb3058bd2016-07-01 08:22:01 -0700141 ADD_TYPE(Bool);
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400142 ADD_TYPE(Bool2);
143 ADD_TYPE(Bool3);
144 ADD_TYPE(Bool4);
145 ADD_TYPE(Float2x2);
146 ADD_TYPE(Float2x3);
147 ADD_TYPE(Float2x4);
148 ADD_TYPE(Float3x2);
149 ADD_TYPE(Float3x3);
150 ADD_TYPE(Float3x4);
151 ADD_TYPE(Float4x2);
152 ADD_TYPE(Float4x3);
153 ADD_TYPE(Float4x4);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400154 ADD_TYPE(Half2x2);
155 ADD_TYPE(Half2x3);
156 ADD_TYPE(Half2x4);
157 ADD_TYPE(Half3x2);
158 ADD_TYPE(Half3x3);
159 ADD_TYPE(Half3x4);
160 ADD_TYPE(Half4x2);
161 ADD_TYPE(Half4x3);
162 ADD_TYPE(Half4x4);
ethannicholasb3058bd2016-07-01 08:22:01 -0700163 ADD_TYPE(GenType);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400164 ADD_TYPE(GenHType);
ethannicholasb3058bd2016-07-01 08:22:01 -0700165 ADD_TYPE(GenIType);
166 ADD_TYPE(GenUType);
167 ADD_TYPE(GenBType);
168 ADD_TYPE(Mat);
169 ADD_TYPE(Vec);
170 ADD_TYPE(GVec);
171 ADD_TYPE(GVec2);
172 ADD_TYPE(GVec3);
173 ADD_TYPE(GVec4);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400174 ADD_TYPE(HVec);
ethannicholasb3058bd2016-07-01 08:22:01 -0700175 ADD_TYPE(IVec);
176 ADD_TYPE(UVec);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400177 ADD_TYPE(SVec);
178 ADD_TYPE(USVec);
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400179 ADD_TYPE(ByteVec);
180 ADD_TYPE(UByteVec);
ethannicholasb3058bd2016-07-01 08:22:01 -0700181 ADD_TYPE(BVec);
182
183 ADD_TYPE(Sampler1D);
184 ADD_TYPE(Sampler2D);
185 ADD_TYPE(Sampler3D);
ethannicholas5961bc92016-10-12 06:39:56 -0700186 ADD_TYPE(SamplerExternalOES);
ethannicholasb3058bd2016-07-01 08:22:01 -0700187 ADD_TYPE(SamplerCube);
188 ADD_TYPE(Sampler2DRect);
189 ADD_TYPE(Sampler1DArray);
190 ADD_TYPE(Sampler2DArray);
191 ADD_TYPE(SamplerCubeArray);
192 ADD_TYPE(SamplerBuffer);
193 ADD_TYPE(Sampler2DMS);
194 ADD_TYPE(Sampler2DMSArray);
195
Brian Salomonbf7b6202016-11-11 16:08:03 -0500196 ADD_TYPE(ISampler2D);
197
Brian Salomon2a51de82016-11-16 12:06:01 -0500198 ADD_TYPE(Image2D);
199 ADD_TYPE(IImage2D);
200
Greg Daniel64773e62016-11-22 09:44:03 -0500201 ADD_TYPE(SubpassInput);
202 ADD_TYPE(SubpassInputMS);
203
ethannicholasb3058bd2016-07-01 08:22:01 -0700204 ADD_TYPE(GSampler1D);
205 ADD_TYPE(GSampler2D);
206 ADD_TYPE(GSampler3D);
207 ADD_TYPE(GSamplerCube);
208 ADD_TYPE(GSampler2DRect);
209 ADD_TYPE(GSampler1DArray);
210 ADD_TYPE(GSampler2DArray);
211 ADD_TYPE(GSamplerCubeArray);
212 ADD_TYPE(GSamplerBuffer);
213 ADD_TYPE(GSampler2DMS);
214 ADD_TYPE(GSampler2DMSArray);
215
216 ADD_TYPE(Sampler1DShadow);
217 ADD_TYPE(Sampler2DShadow);
218 ADD_TYPE(SamplerCubeShadow);
219 ADD_TYPE(Sampler2DRectShadow);
220 ADD_TYPE(Sampler1DArrayShadow);
221 ADD_TYPE(Sampler2DArrayShadow);
222 ADD_TYPE(SamplerCubeArrayShadow);
223 ADD_TYPE(GSampler2DArrayShadow);
224 ADD_TYPE(GSamplerCubeArrayShadow);
Ethan Nicholasc9472af2017-10-10 16:30:21 -0400225 ADD_TYPE(FragmentProcessor);
Stephen Whiteff5d7a22019-07-26 17:42:06 -0400226 ADD_TYPE(Sampler);
227 ADD_TYPE(Texture2D);
ethannicholasb3058bd2016-07-01 08:22:01 -0700228
Brian Osman28590d52020-03-23 16:59:08 -0400229 StringFragment fpAliasName("shader");
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400230 symbols->addWithoutOwnership(fpAliasName, fContext->fFragmentProcessor_Type.get());
Brian Osman28590d52020-03-23 16:59:08 -0400231
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700232 StringFragment skCapsName("sk_Caps");
233 Variable* skCaps = new Variable(-1, Modifiers(), skCapsName,
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400234 *fContext->fSkCaps_Type, Variable::kGlobal_Storage);
Ethan Nicholas3605ace2016-11-21 15:59:48 -0500235 fIRGenerator->fSymbolTable->add(skCapsName, std::unique_ptr<Symbol>(skCaps));
236
Ethan Nicholasdb80f692019-11-22 14:06:12 -0500237 fIRGenerator->fIntrinsics = &fGPUIntrinsics;
238 std::vector<std::unique_ptr<ProgramElement>> gpuIntrinsics;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400239 std::vector<std::unique_ptr<ProgramElement>> interpIntrinsics;
Brian Osmandd496172020-08-08 08:17:18 -0400240#if SKSL_STANDALONE
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -0400241 this->processIncludeFile(Program::kFragment_Kind, SKSL_GPU_INCLUDE, symbols, &gpuIntrinsics,
242 &fGpuSymbolTable);
243 this->processIncludeFile(Program::kVertex_Kind, SKSL_VERT_INCLUDE, fGpuSymbolTable,
244 &fVertexInclude, &fVertexSymbolTable);
245 this->processIncludeFile(Program::kFragment_Kind, SKSL_FRAG_INCLUDE, fGpuSymbolTable,
246 &fFragmentInclude, &fFragmentSymbolTable);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400247#else
248 {
249 Rehydrator rehydrator(fContext.get(), symbols, this, SKSL_INCLUDE_sksl_gpu,
250 SKSL_INCLUDE_sksl_gpu_LENGTH);
251 fGpuSymbolTable = rehydrator.symbolTable();
252 gpuIntrinsics = rehydrator.elements();
253 }
254 {
255 Rehydrator rehydrator(fContext.get(), fGpuSymbolTable, this, SKSL_INCLUDE_sksl_vert,
256 SKSL_INCLUDE_sksl_vert_LENGTH);
257 fVertexSymbolTable = rehydrator.symbolTable();
258 fVertexInclude = rehydrator.elements();
259 }
260 {
261 Rehydrator rehydrator(fContext.get(), fGpuSymbolTable, this, SKSL_INCLUDE_sksl_frag,
262 SKSL_INCLUDE_sksl_frag_LENGTH);
263 fFragmentSymbolTable = rehydrator.symbolTable();
264 fFragmentInclude = rehydrator.elements();
265 }
266#endif
267 grab_intrinsics(&gpuIntrinsics, &fGPUIntrinsics);
Brian Osmanb08cc022020-04-02 11:38:40 -0400268 grab_intrinsics(&interpIntrinsics, &fInterpreterIntrinsics);
ethannicholasb3058bd2016-07-01 08:22:01 -0700269}
270
271Compiler::~Compiler() {
272 delete fIRGenerator;
273}
274
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400275void Compiler::loadGeometryIntrinsics() {
276 if (fGeometrySymbolTable) {
277 return;
278 }
Brian Osmandd496172020-08-08 08:17:18 -0400279 #if !SKSL_STANDALONE
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400280 {
281 Rehydrator rehydrator(fContext.get(), fGpuSymbolTable, this, SKSL_INCLUDE_sksl_geom,
282 SKSL_INCLUDE_sksl_geom_LENGTH);
283 fGeometrySymbolTable = rehydrator.symbolTable();
284 fGeometryInclude = rehydrator.elements();
285 }
286 #else
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -0400287 this->processIncludeFile(Program::kGeometry_Kind, SKSL_GEOM_INCLUDE, fGpuSymbolTable,
288 &fGeometryInclude, &fGeometrySymbolTable);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400289 #endif
290}
291
292void Compiler::loadPipelineIntrinsics() {
293 if (fPipelineSymbolTable) {
294 return;
295 }
Brian Osmandd496172020-08-08 08:17:18 -0400296 #if !SKSL_STANDALONE
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400297 {
298 Rehydrator rehydrator(fContext.get(), fGpuSymbolTable, this,
299 SKSL_INCLUDE_sksl_pipeline,
300 SKSL_INCLUDE_sksl_pipeline_LENGTH);
301 fPipelineSymbolTable = rehydrator.symbolTable();
302 fPipelineInclude = rehydrator.elements();
303 }
304 #else
305 this->processIncludeFile(Program::kPipelineStage_Kind, SKSL_PIPELINE_INCLUDE,
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -0400306 fGpuSymbolTable, &fPipelineInclude, &fPipelineSymbolTable);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400307 #endif
308}
309
310void Compiler::loadInterpreterIntrinsics() {
311 if (fInterpreterSymbolTable) {
312 return;
313 }
314 this->loadPipelineIntrinsics();
Brian Osmandd496172020-08-08 08:17:18 -0400315 #if !SKSL_STANDALONE
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400316 {
317 Rehydrator rehydrator(fContext.get(), fPipelineSymbolTable, this,
318 SKSL_INCLUDE_sksl_interp,
319 SKSL_INCLUDE_sksl_interp_LENGTH);
320 fInterpreterSymbolTable = rehydrator.symbolTable();
321 fInterpreterInclude = rehydrator.elements();
322 }
323 #else
324 this->processIncludeFile(Program::kGeneric_Kind, SKSL_INTERP_INCLUDE,
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -0400325 fIRGenerator->fSymbolTable, &fInterpreterInclude,
326 &fInterpreterSymbolTable);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400327 #endif
328}
329
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -0400330void Compiler::processIncludeFile(Program::Kind kind, const char* path,
Ethan Nicholas8da1e652019-05-24 11:01:59 -0400331 std::shared_ptr<SymbolTable> base,
332 std::vector<std::unique_ptr<ProgramElement>>* outElements,
333 std::shared_ptr<SymbolTable>* outSymbolTable) {
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -0400334 std::ifstream in(path);
335 std::string stdText{std::istreambuf_iterator<char>(in),
336 std::istreambuf_iterator<char>()};
337 if (in.rdstate()) {
338 printf("error reading %s\n", path);
339 abort();
340 }
341 if (!base) {
342 base = fIRGenerator->fSymbolTable;
343 }
344 SkASSERT(base);
345 const String* source = base->takeOwnershipOfString(std::make_unique<String>(stdText.c_str()));
346 fSource = source;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400347 std::shared_ptr<SymbolTable> old = fIRGenerator->fSymbolTable;
348 if (base) {
349 fIRGenerator->fSymbolTable = std::move(base);
350 }
Ethan Nicholas8da1e652019-05-24 11:01:59 -0400351 Program::Settings settings;
Ethan Nicholasa11035b2019-11-26 16:27:47 -0500352#if !defined(SKSL_STANDALONE) & SK_SUPPORT_GPU
353 GrContextOptions opts;
354 GrShaderCaps caps(opts);
355 settings.fCaps = &caps;
356#endif
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400357 SkASSERT(fIRGenerator->fCanInline);
358 fIRGenerator->fCanInline = false;
359 fIRGenerator->start(&settings, nullptr, true);
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -0400360 fIRGenerator->convertProgram(kind, source->c_str(), source->length(), outElements);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400361 fIRGenerator->fCanInline = true;
Ethan Nicholas8da1e652019-05-24 11:01:59 -0400362 if (this->fErrorCount) {
363 printf("Unexpected errors: %s\n", this->fErrorText.c_str());
364 }
365 SkASSERT(!fErrorCount);
Ethan Nicholas8da1e652019-05-24 11:01:59 -0400366 *outSymbolTable = fIRGenerator->fSymbolTable;
Ethan Nicholasa11035b2019-11-26 16:27:47 -0500367#ifdef SK_DEBUG
368 fSource = nullptr;
369#endif
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400370 fIRGenerator->fSymbolTable = std::move(old);
Ethan Nicholas8da1e652019-05-24 11:01:59 -0400371}
372
ethannicholas22f939e2016-10-13 13:25:34 -0700373// add the definition created by assigning to the lvalue to the definition set
Ethan Nicholas86a43402017-01-19 13:32:00 -0500374void Compiler::addDefinition(const Expression* lvalue, std::unique_ptr<Expression>* expr,
375 DefinitionMap* definitions) {
ethannicholas22f939e2016-10-13 13:25:34 -0700376 switch (lvalue->fKind) {
377 case Expression::kVariableReference_Kind: {
378 const Variable& var = ((VariableReference*) lvalue)->fVariable;
379 if (var.fStorage == Variable::kLocal_Storage) {
380 (*definitions)[&var] = expr;
381 }
382 break;
383 }
384 case Expression::kSwizzle_Kind:
385 // We consider the variable written to as long as at least some of its components have
386 // been written to. This will lead to some false negatives (we won't catch it if you
387 // write to foo.x and then read foo.y), but being stricter could lead to false positives
Mike Klein6ad99092016-10-26 10:35:22 -0400388 // (we write to foo.x, and then pass foo to a function which happens to only read foo.x,
389 // 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 -0700390 // more complicated whole-program analysis. This is probably good enough.
Mike Klein6ad99092016-10-26 10:35:22 -0400391 this->addDefinition(((Swizzle*) lvalue)->fBase.get(),
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400392 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
ethannicholas22f939e2016-10-13 13:25:34 -0700393 definitions);
394 break;
395 case Expression::kIndex_Kind:
396 // see comments in Swizzle
Mike Klein6ad99092016-10-26 10:35:22 -0400397 this->addDefinition(((IndexExpression*) lvalue)->fBase.get(),
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400398 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
ethannicholas22f939e2016-10-13 13:25:34 -0700399 definitions);
400 break;
401 case Expression::kFieldAccess_Kind:
402 // see comments in Swizzle
Mike Klein6ad99092016-10-26 10:35:22 -0400403 this->addDefinition(((FieldAccess*) lvalue)->fBase.get(),
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400404 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
ethannicholas22f939e2016-10-13 13:25:34 -0700405 definitions);
406 break;
Ethan Nicholasa583b812018-01-18 13:32:11 -0500407 case Expression::kTernary_Kind:
408 // To simplify analysis, we just pretend that we write to both sides of the ternary.
409 // This allows for false positives (meaning we fail to detect that a variable might not
410 // have been assigned), but is preferable to false negatives.
411 this->addDefinition(((TernaryExpression*) lvalue)->fIfTrue.get(),
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400412 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
Ethan Nicholasa583b812018-01-18 13:32:11 -0500413 definitions);
414 this->addDefinition(((TernaryExpression*) lvalue)->fIfFalse.get(),
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400415 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
Ethan Nicholasa583b812018-01-18 13:32:11 -0500416 definitions);
417 break;
Ethan Nicholas91164d12019-05-15 15:29:54 -0400418 case Expression::kExternalValue_Kind:
419 break;
ethannicholas22f939e2016-10-13 13:25:34 -0700420 default:
421 // not an lvalue, can't happen
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400422 SkASSERT(false);
ethannicholas22f939e2016-10-13 13:25:34 -0700423 }
424}
425
426// add local variables defined by this node to the set
Mike Klein6ad99092016-10-26 10:35:22 -0400427void Compiler::addDefinitions(const BasicBlock::Node& node,
Ethan Nicholas86a43402017-01-19 13:32:00 -0500428 DefinitionMap* definitions) {
ethannicholas22f939e2016-10-13 13:25:34 -0700429 switch (node.fKind) {
430 case BasicBlock::Node::kExpression_Kind: {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400431 SkASSERT(node.expression());
Ethan Nicholascb670962017-04-20 19:31:52 -0400432 const Expression* expr = (Expression*) node.expression()->get();
Ethan Nicholas86a43402017-01-19 13:32:00 -0500433 switch (expr->fKind) {
434 case Expression::kBinary_Kind: {
435 BinaryExpression* b = (BinaryExpression*) expr;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400436 if (b->fOperator == Token::Kind::TK_EQ) {
Ethan Nicholas86a43402017-01-19 13:32:00 -0500437 this->addDefinition(b->fLeft.get(), &b->fRight, definitions);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700438 } else if (Compiler::IsAssignment(b->fOperator)) {
Ethan Nicholas86a43402017-01-19 13:32:00 -0500439 this->addDefinition(
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400440 b->fLeft.get(),
441 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
442 definitions);
Ethan Nicholas86a43402017-01-19 13:32:00 -0500443
444 }
445 break;
ethannicholas22f939e2016-10-13 13:25:34 -0700446 }
Ethan Nicholasc6a19f12018-03-29 16:46:56 -0400447 case Expression::kFunctionCall_Kind: {
448 const FunctionCall& c = (const FunctionCall&) *expr;
449 for (size_t i = 0; i < c.fFunction.fParameters.size(); ++i) {
450 if (c.fFunction.fParameters[i]->fModifiers.fFlags & Modifiers::kOut_Flag) {
451 this->addDefinition(
452 c.fArguments[i].get(),
453 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
454 definitions);
455 }
456 }
457 break;
458 }
Ethan Nicholas86a43402017-01-19 13:32:00 -0500459 case Expression::kPrefix_Kind: {
460 const PrefixExpression* p = (PrefixExpression*) expr;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400461 if (p->fOperator == Token::Kind::TK_MINUSMINUS ||
462 p->fOperator == Token::Kind::TK_PLUSPLUS) {
Ethan Nicholas86a43402017-01-19 13:32:00 -0500463 this->addDefinition(
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400464 p->fOperand.get(),
465 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
466 definitions);
Ethan Nicholas86a43402017-01-19 13:32:00 -0500467 }
468 break;
469 }
470 case Expression::kPostfix_Kind: {
471 const PostfixExpression* p = (PostfixExpression*) expr;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400472 if (p->fOperator == Token::Kind::TK_MINUSMINUS ||
473 p->fOperator == Token::Kind::TK_PLUSPLUS) {
Ethan Nicholas86a43402017-01-19 13:32:00 -0500474 this->addDefinition(
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400475 p->fOperand.get(),
476 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
477 definitions);
Ethan Nicholas86a43402017-01-19 13:32:00 -0500478 }
479 break;
480 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400481 case Expression::kVariableReference_Kind: {
482 const VariableReference* v = (VariableReference*) expr;
483 if (v->fRefKind != VariableReference::kRead_RefKind) {
484 this->addDefinition(
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400485 v,
486 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
487 definitions);
Ethan Nicholascb670962017-04-20 19:31:52 -0400488 }
John Stiles30212b72020-06-11 17:55:07 -0400489 break;
Ethan Nicholascb670962017-04-20 19:31:52 -0400490 }
Ethan Nicholas86a43402017-01-19 13:32:00 -0500491 default:
492 break;
ethannicholas22f939e2016-10-13 13:25:34 -0700493 }
494 break;
495 }
496 case BasicBlock::Node::kStatement_Kind: {
Ethan Nicholascb670962017-04-20 19:31:52 -0400497 const Statement* stmt = (Statement*) node.statement()->get();
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000498 if (stmt->fKind == Statement::kVarDeclaration_Kind) {
499 VarDeclaration& vd = (VarDeclaration&) *stmt;
500 if (vd.fValue) {
501 (*definitions)[vd.fVar] = &vd.fValue;
ethannicholas22f939e2016-10-13 13:25:34 -0700502 }
503 }
504 break;
505 }
506 }
507}
508
509void Compiler::scanCFG(CFG* cfg, BlockId blockId, std::set<BlockId>* workList) {
510 BasicBlock& block = cfg->fBlocks[blockId];
511
512 // compute definitions after this block
Ethan Nicholas86a43402017-01-19 13:32:00 -0500513 DefinitionMap after = block.fBefore;
ethannicholas22f939e2016-10-13 13:25:34 -0700514 for (const BasicBlock::Node& n : block.fNodes) {
515 this->addDefinitions(n, &after);
516 }
517
518 // propagate definitions to exits
519 for (BlockId exitId : block.fExits) {
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400520 if (exitId == blockId) {
521 continue;
522 }
ethannicholas22f939e2016-10-13 13:25:34 -0700523 BasicBlock& exit = cfg->fBlocks[exitId];
524 for (const auto& pair : after) {
Ethan Nicholas86a43402017-01-19 13:32:00 -0500525 std::unique_ptr<Expression>* e1 = pair.second;
526 auto found = exit.fBefore.find(pair.first);
527 if (found == exit.fBefore.end()) {
528 // exit has no definition for it, just copy it
529 workList->insert(exitId);
ethannicholas22f939e2016-10-13 13:25:34 -0700530 exit.fBefore[pair.first] = e1;
531 } else {
Ethan Nicholas86a43402017-01-19 13:32:00 -0500532 // exit has a (possibly different) value already defined
533 std::unique_ptr<Expression>* e2 = exit.fBefore[pair.first];
ethannicholas22f939e2016-10-13 13:25:34 -0700534 if (e1 != e2) {
535 // definition has changed, merge and add exit block to worklist
536 workList->insert(exitId);
Ethan Nicholasaf197692017-02-27 13:26:45 -0500537 if (e1 && e2) {
538 exit.fBefore[pair.first] =
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400539 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression;
Ethan Nicholasaf197692017-02-27 13:26:45 -0500540 } else {
541 exit.fBefore[pair.first] = nullptr;
542 }
ethannicholas22f939e2016-10-13 13:25:34 -0700543 }
544 }
545 }
546 }
547}
548
549// returns a map which maps all local variables in the function to null, indicating that their value
550// is initially unknown
Ethan Nicholas86a43402017-01-19 13:32:00 -0500551static DefinitionMap compute_start_state(const CFG& cfg) {
552 DefinitionMap result;
Mike Klein6ad99092016-10-26 10:35:22 -0400553 for (const auto& block : cfg.fBlocks) {
554 for (const auto& node : block.fNodes) {
ethannicholas22f939e2016-10-13 13:25:34 -0700555 if (node.fKind == BasicBlock::Node::kStatement_Kind) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400556 SkASSERT(node.statement());
Ethan Nicholascb670962017-04-20 19:31:52 -0400557 const Statement* s = node.statement()->get();
ethannicholas22f939e2016-10-13 13:25:34 -0700558 if (s->fKind == Statement::kVarDeclarations_Kind) {
559 const VarDeclarationsStatement* vd = (const VarDeclarationsStatement*) s;
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000560 for (const auto& decl : vd->fDeclaration->fVars) {
561 if (decl->fKind == Statement::kVarDeclaration_Kind) {
562 result[((VarDeclaration&) *decl).fVar] = nullptr;
563 }
Mike Klein6ad99092016-10-26 10:35:22 -0400564 }
ethannicholas22f939e2016-10-13 13:25:34 -0700565 }
566 }
567 }
568 }
569 return result;
570}
571
Ethan Nicholascb670962017-04-20 19:31:52 -0400572/**
573 * Returns true if assigning to this lvalue has no effect.
574 */
575static bool is_dead(const Expression& lvalue) {
576 switch (lvalue.fKind) {
577 case Expression::kVariableReference_Kind:
578 return ((VariableReference&) lvalue).fVariable.dead();
579 case Expression::kSwizzle_Kind:
580 return is_dead(*((Swizzle&) lvalue).fBase);
581 case Expression::kFieldAccess_Kind:
582 return is_dead(*((FieldAccess&) lvalue).fBase);
583 case Expression::kIndex_Kind: {
584 const IndexExpression& idx = (IndexExpression&) lvalue;
Ethan Nicholas2a099da2020-01-02 14:40:54 -0500585 return is_dead(*idx.fBase) &&
586 !idx.fIndex->hasProperty(Expression::Property::kSideEffects);
Ethan Nicholascb670962017-04-20 19:31:52 -0400587 }
Ethan Nicholasa583b812018-01-18 13:32:11 -0500588 case Expression::kTernary_Kind: {
589 const TernaryExpression& t = (TernaryExpression&) lvalue;
590 return !t.fTest->hasSideEffects() && is_dead(*t.fIfTrue) && is_dead(*t.fIfFalse);
591 }
Ethan Nicholas91164d12019-05-15 15:29:54 -0400592 case Expression::kExternalValue_Kind:
593 return false;
Ethan Nicholascb670962017-04-20 19:31:52 -0400594 default:
Ethan Nicholas2a099da2020-01-02 14:40:54 -0500595#ifdef SK_DEBUG
Ethan Nicholascb670962017-04-20 19:31:52 -0400596 ABORT("invalid lvalue: %s\n", lvalue.description().c_str());
Ethan Nicholas2a099da2020-01-02 14:40:54 -0500597#endif
598 return false;
Ethan Nicholascb670962017-04-20 19:31:52 -0400599 }
600}
ethannicholas22f939e2016-10-13 13:25:34 -0700601
Ethan Nicholascb670962017-04-20 19:31:52 -0400602/**
603 * Returns true if this is an assignment which can be collapsed down to just the right hand side due
604 * to a dead target and lack of side effects on the left hand side.
605 */
606static bool dead_assignment(const BinaryExpression& b) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700607 if (!Compiler::IsAssignment(b.fOperator)) {
Ethan Nicholascb670962017-04-20 19:31:52 -0400608 return false;
609 }
610 return is_dead(*b.fLeft);
611}
612
613void Compiler::computeDataFlow(CFG* cfg) {
614 cfg->fBlocks[cfg->fStart].fBefore = compute_start_state(*cfg);
ethannicholas22f939e2016-10-13 13:25:34 -0700615 std::set<BlockId> workList;
Ethan Nicholascb670962017-04-20 19:31:52 -0400616 for (BlockId i = 0; i < cfg->fBlocks.size(); i++) {
ethannicholas22f939e2016-10-13 13:25:34 -0700617 workList.insert(i);
618 }
619 while (workList.size()) {
620 BlockId next = *workList.begin();
621 workList.erase(workList.begin());
Ethan Nicholascb670962017-04-20 19:31:52 -0400622 this->scanCFG(cfg, next, &workList);
ethannicholas22f939e2016-10-13 13:25:34 -0700623 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400624}
625
626/**
627 * Attempts to replace the expression pointed to by iter with a new one (in both the CFG and the
628 * IR). If the expression can be cleanly removed, returns true and updates the iterator to point to
629 * the newly-inserted element. Otherwise updates only the IR and returns false (and the CFG will
630 * need to be regenerated).
631 */
632bool try_replace_expression(BasicBlock* b,
633 std::vector<BasicBlock::Node>::iterator* iter,
634 std::unique_ptr<Expression>* newExpression) {
635 std::unique_ptr<Expression>* target = (*iter)->expression();
636 if (!b->tryRemoveExpression(iter)) {
637 *target = std::move(*newExpression);
638 return false;
639 }
640 *target = std::move(*newExpression);
641 return b->tryInsertExpression(iter, target);
642}
643
644/**
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400645 * Returns true if the expression is a constant numeric literal with the specified value, or a
646 * constant vector with all elements equal to the specified value.
Ethan Nicholascb670962017-04-20 19:31:52 -0400647 */
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400648bool is_constant(const Expression& expr, double value) {
Ethan Nicholascb670962017-04-20 19:31:52 -0400649 switch (expr.fKind) {
650 case Expression::kIntLiteral_Kind:
651 return ((IntLiteral&) expr).fValue == value;
652 case Expression::kFloatLiteral_Kind:
653 return ((FloatLiteral&) expr).fValue == value;
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400654 case Expression::kConstructor_Kind: {
655 Constructor& c = (Constructor&) expr;
Ethan Nicholasd188c182019-06-10 15:55:38 -0400656 bool isFloat = c.fType.columns() > 1 ? c.fType.componentType().isFloat()
657 : c.fType.isFloat();
Brian Osmanb6b95732020-06-30 11:44:27 -0400658 if (c.fType.kind() == Type::kVector_Kind && c.isCompileTimeConstant()) {
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400659 for (int i = 0; i < c.fType.columns(); ++i) {
Ethan Nicholasd188c182019-06-10 15:55:38 -0400660 if (isFloat) {
661 if (c.getFVecComponent(i) != value) {
662 return false;
663 }
664 } else if (c.getIVecComponent(i) != value) {
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400665 return false;
666 }
667 }
668 return true;
669 }
670 return false;
671 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400672 default:
673 return false;
674 }
675}
676
677/**
678 * Collapses the binary expression pointed to by iter down to just the right side (in both the IR
679 * and CFG structures).
680 */
681void delete_left(BasicBlock* b,
682 std::vector<BasicBlock::Node>::iterator* iter,
683 bool* outUpdated,
684 bool* outNeedsRescan) {
685 *outUpdated = true;
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400686 std::unique_ptr<Expression>* target = (*iter)->expression();
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400687 SkASSERT((*target)->fKind == Expression::kBinary_Kind);
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400688 BinaryExpression& bin = (BinaryExpression&) **target;
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400689 SkASSERT(!bin.fLeft->hasSideEffects());
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400690 bool result;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400691 if (bin.fOperator == Token::Kind::TK_EQ) {
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400692 result = b->tryRemoveLValueBefore(iter, bin.fLeft.get());
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400693 } else {
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400694 result = b->tryRemoveExpressionBefore(iter, bin.fLeft.get());
Ethan Nicholascb670962017-04-20 19:31:52 -0400695 }
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400696 *target = std::move(bin.fRight);
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400697 if (!result) {
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400698 *outNeedsRescan = true;
699 return;
700 }
701 if (*iter == b->fNodes.begin()) {
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400702 *outNeedsRescan = true;
703 return;
704 }
705 --(*iter);
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400706 if ((*iter)->fKind != BasicBlock::Node::kExpression_Kind ||
707 (*iter)->expression() != &bin.fRight) {
708 *outNeedsRescan = true;
709 return;
710 }
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400711 *iter = b->fNodes.erase(*iter);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400712 SkASSERT((*iter)->expression() == target);
Ethan Nicholascb670962017-04-20 19:31:52 -0400713}
714
715/**
716 * Collapses the binary expression pointed to by iter down to just the left side (in both the IR and
717 * CFG structures).
718 */
719void delete_right(BasicBlock* b,
720 std::vector<BasicBlock::Node>::iterator* iter,
721 bool* outUpdated,
722 bool* outNeedsRescan) {
723 *outUpdated = true;
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400724 std::unique_ptr<Expression>* target = (*iter)->expression();
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400725 SkASSERT((*target)->fKind == Expression::kBinary_Kind);
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400726 BinaryExpression& bin = (BinaryExpression&) **target;
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400727 SkASSERT(!bin.fRight->hasSideEffects());
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400728 if (!b->tryRemoveExpressionBefore(iter, bin.fRight.get())) {
729 *target = std::move(bin.fLeft);
Ethan Nicholascb670962017-04-20 19:31:52 -0400730 *outNeedsRescan = true;
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400731 return;
Ethan Nicholascb670962017-04-20 19:31:52 -0400732 }
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400733 *target = std::move(bin.fLeft);
734 if (*iter == b->fNodes.begin()) {
735 *outNeedsRescan = true;
736 return;
737 }
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400738 --(*iter);
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400739 if (((*iter)->fKind != BasicBlock::Node::kExpression_Kind ||
740 (*iter)->expression() != &bin.fLeft)) {
741 *outNeedsRescan = true;
742 return;
743 }
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400744 *iter = b->fNodes.erase(*iter);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400745 SkASSERT((*iter)->expression() == target);
Ethan Nicholascb670962017-04-20 19:31:52 -0400746}
747
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400748/**
749 * Constructs the specified type using a single argument.
750 */
751static std::unique_ptr<Expression> construct(const Type& type, std::unique_ptr<Expression> v) {
752 std::vector<std::unique_ptr<Expression>> args;
753 args.push_back(std::move(v));
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700754 auto result = std::unique_ptr<Expression>(new Constructor(-1, type, std::move(args)));
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400755 return result;
756}
757
758/**
759 * Used in the implementations of vectorize_left and vectorize_right. Given a vector type and an
760 * expression x, deletes the expression pointed to by iter and replaces it with <type>(x).
761 */
762static void vectorize(BasicBlock* b,
763 std::vector<BasicBlock::Node>::iterator* iter,
764 const Type& type,
765 std::unique_ptr<Expression>* otherExpression,
766 bool* outUpdated,
767 bool* outNeedsRescan) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400768 SkASSERT((*(*iter)->expression())->fKind == Expression::kBinary_Kind);
769 SkASSERT(type.kind() == Type::kVector_Kind);
770 SkASSERT((*otherExpression)->fType.kind() == Type::kScalar_Kind);
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400771 *outUpdated = true;
772 std::unique_ptr<Expression>* target = (*iter)->expression();
773 if (!b->tryRemoveExpression(iter)) {
774 *target = construct(type, std::move(*otherExpression));
775 *outNeedsRescan = true;
776 } else {
777 *target = construct(type, std::move(*otherExpression));
778 if (!b->tryInsertExpression(iter, target)) {
779 *outNeedsRescan = true;
780 }
781 }
782}
783
784/**
785 * Given a binary expression of the form x <op> vec<n>(y), deletes the right side and vectorizes the
786 * left to yield vec<n>(x).
787 */
788static void vectorize_left(BasicBlock* b,
789 std::vector<BasicBlock::Node>::iterator* iter,
790 bool* outUpdated,
791 bool* outNeedsRescan) {
792 BinaryExpression& bin = (BinaryExpression&) **(*iter)->expression();
793 vectorize(b, iter, bin.fRight->fType, &bin.fLeft, outUpdated, outNeedsRescan);
794}
795
796/**
797 * Given a binary expression of the form vec<n>(x) <op> y, deletes the left side and vectorizes the
798 * right to yield vec<n>(y).
799 */
800static void vectorize_right(BasicBlock* b,
801 std::vector<BasicBlock::Node>::iterator* iter,
802 bool* outUpdated,
803 bool* outNeedsRescan) {
804 BinaryExpression& bin = (BinaryExpression&) **(*iter)->expression();
805 vectorize(b, iter, bin.fLeft->fType, &bin.fRight, outUpdated, outNeedsRescan);
806}
807
808// Mark that an expression which we were writing to is no longer being written to
809void clear_write(const Expression& expr) {
810 switch (expr.fKind) {
811 case Expression::kVariableReference_Kind: {
812 ((VariableReference&) expr).setRefKind(VariableReference::kRead_RefKind);
813 break;
814 }
815 case Expression::kFieldAccess_Kind:
816 clear_write(*((FieldAccess&) expr).fBase);
817 break;
818 case Expression::kSwizzle_Kind:
819 clear_write(*((Swizzle&) expr).fBase);
820 break;
821 case Expression::kIndex_Kind:
822 clear_write(*((IndexExpression&) expr).fBase);
823 break;
824 default:
825 ABORT("shouldn't be writing to this kind of expression\n");
826 break;
827 }
828}
829
Ethan Nicholascb670962017-04-20 19:31:52 -0400830void Compiler::simplifyExpression(DefinitionMap& definitions,
831 BasicBlock& b,
832 std::vector<BasicBlock::Node>::iterator* iter,
833 std::unordered_set<const Variable*>* undefinedVariables,
834 bool* outUpdated,
835 bool* outNeedsRescan) {
836 Expression* expr = (*iter)->expression()->get();
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400837 SkASSERT(expr);
Ethan Nicholascb670962017-04-20 19:31:52 -0400838 if ((*iter)->fConstantPropagation) {
839 std::unique_ptr<Expression> optimized = expr->constantPropagate(*fIRGenerator, definitions);
840 if (optimized) {
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400841 *outUpdated = true;
Ethan Nicholascb670962017-04-20 19:31:52 -0400842 if (!try_replace_expression(&b, iter, &optimized)) {
843 *outNeedsRescan = true;
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400844 return;
Ethan Nicholascb670962017-04-20 19:31:52 -0400845 }
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400846 SkASSERT((*iter)->fKind == BasicBlock::Node::kExpression_Kind);
Ethan Nicholascb670962017-04-20 19:31:52 -0400847 expr = (*iter)->expression()->get();
Ethan Nicholascb670962017-04-20 19:31:52 -0400848 }
849 }
850 switch (expr->fKind) {
851 case Expression::kVariableReference_Kind: {
Ethan Nicholas8f7e28f2018-03-26 14:24:27 -0400852 const VariableReference& ref = (VariableReference&) *expr;
853 const Variable& var = ref.fVariable;
854 if (ref.refKind() != VariableReference::kWrite_RefKind &&
855 ref.refKind() != VariableReference::kPointer_RefKind &&
856 var.fStorage == Variable::kLocal_Storage && !definitions[&var] &&
Ethan Nicholascb670962017-04-20 19:31:52 -0400857 (*undefinedVariables).find(&var) == (*undefinedVariables).end()) {
858 (*undefinedVariables).insert(&var);
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000859 this->error(expr->fOffset,
860 "'" + var.fName + "' has not been assigned");
Ethan Nicholascb670962017-04-20 19:31:52 -0400861 }
862 break;
863 }
864 case Expression::kTernary_Kind: {
865 TernaryExpression* t = (TernaryExpression*) expr;
866 if (t->fTest->fKind == Expression::kBoolLiteral_Kind) {
867 // ternary has a constant test, replace it with either the true or
868 // false branch
869 if (((BoolLiteral&) *t->fTest).fValue) {
870 (*iter)->setExpression(std::move(t->fIfTrue));
871 } else {
872 (*iter)->setExpression(std::move(t->fIfFalse));
873 }
874 *outUpdated = true;
875 *outNeedsRescan = true;
876 }
877 break;
878 }
879 case Expression::kBinary_Kind: {
Ethan Nicholascb670962017-04-20 19:31:52 -0400880 BinaryExpression* bin = (BinaryExpression*) expr;
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400881 if (dead_assignment(*bin)) {
882 delete_left(&b, iter, outUpdated, outNeedsRescan);
883 break;
884 }
885 // collapse useless expressions like x * 1 or x + 0
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400886 if (((bin->fLeft->fType.kind() != Type::kScalar_Kind) &&
887 (bin->fLeft->fType.kind() != Type::kVector_Kind)) ||
888 ((bin->fRight->fType.kind() != Type::kScalar_Kind) &&
889 (bin->fRight->fType.kind() != Type::kVector_Kind))) {
890 break;
891 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400892 switch (bin->fOperator) {
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400893 case Token::Kind::TK_STAR:
Ethan Nicholascb670962017-04-20 19:31:52 -0400894 if (is_constant(*bin->fLeft, 1)) {
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400895 if (bin->fLeft->fType.kind() == Type::kVector_Kind &&
896 bin->fRight->fType.kind() == Type::kScalar_Kind) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400897 // float4(1) * x -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400898 vectorize_right(&b, iter, outUpdated, outNeedsRescan);
899 } else {
900 // 1 * x -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400901 // 1 * float4(x) -> float4(x)
902 // float4(1) * float4(x) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400903 delete_left(&b, iter, outUpdated, outNeedsRescan);
904 }
905 }
906 else if (is_constant(*bin->fLeft, 0)) {
907 if (bin->fLeft->fType.kind() == Type::kScalar_Kind &&
Ethan Nicholas08dae922018-01-23 10:31:56 -0500908 bin->fRight->fType.kind() == Type::kVector_Kind &&
909 !bin->fRight->hasSideEffects()) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400910 // 0 * float4(x) -> float4(0)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400911 vectorize_left(&b, iter, outUpdated, outNeedsRescan);
912 } else {
913 // 0 * x -> 0
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400914 // float4(0) * x -> float4(0)
915 // float4(0) * float4(x) -> float4(0)
Ethan Nicholas51493ee2017-12-11 12:34:33 -0500916 if (!bin->fRight->hasSideEffects()) {
917 delete_right(&b, iter, outUpdated, outNeedsRescan);
918 }
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400919 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400920 }
921 else if (is_constant(*bin->fRight, 1)) {
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400922 if (bin->fLeft->fType.kind() == Type::kScalar_Kind &&
923 bin->fRight->fType.kind() == Type::kVector_Kind) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400924 // x * float4(1) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400925 vectorize_left(&b, iter, outUpdated, outNeedsRescan);
926 } else {
927 // x * 1 -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400928 // float4(x) * 1 -> float4(x)
929 // float4(x) * float4(1) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400930 delete_right(&b, iter, outUpdated, outNeedsRescan);
931 }
932 }
933 else if (is_constant(*bin->fRight, 0)) {
934 if (bin->fLeft->fType.kind() == Type::kVector_Kind &&
Ethan Nicholas08dae922018-01-23 10:31:56 -0500935 bin->fRight->fType.kind() == Type::kScalar_Kind &&
936 !bin->fLeft->hasSideEffects()) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400937 // float4(x) * 0 -> float4(0)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400938 vectorize_right(&b, iter, outUpdated, outNeedsRescan);
939 } else {
940 // x * 0 -> 0
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400941 // x * float4(0) -> float4(0)
942 // float4(x) * float4(0) -> float4(0)
Ethan Nicholas51493ee2017-12-11 12:34:33 -0500943 if (!bin->fLeft->hasSideEffects()) {
944 delete_left(&b, iter, outUpdated, outNeedsRescan);
945 }
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400946 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400947 }
948 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400949 case Token::Kind::TK_PLUS:
Ethan Nicholascb670962017-04-20 19:31:52 -0400950 if (is_constant(*bin->fLeft, 0)) {
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400951 if (bin->fLeft->fType.kind() == Type::kVector_Kind &&
952 bin->fRight->fType.kind() == Type::kScalar_Kind) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400953 // float4(0) + x -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400954 vectorize_right(&b, iter, outUpdated, outNeedsRescan);
955 } else {
956 // 0 + x -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400957 // 0 + float4(x) -> float4(x)
958 // float4(0) + float4(x) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400959 delete_left(&b, iter, outUpdated, outNeedsRescan);
960 }
961 } else if (is_constant(*bin->fRight, 0)) {
962 if (bin->fLeft->fType.kind() == Type::kScalar_Kind &&
963 bin->fRight->fType.kind() == Type::kVector_Kind) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400964 // x + float4(0) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400965 vectorize_left(&b, iter, outUpdated, outNeedsRescan);
966 } else {
967 // x + 0 -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400968 // float4(x) + 0 -> float4(x)
969 // float4(x) + float4(0) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400970 delete_right(&b, iter, outUpdated, outNeedsRescan);
971 }
Ethan Nicholas56e42712017-04-21 10:23:37 -0400972 }
973 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400974 case Token::Kind::TK_MINUS:
Ethan Nicholas56e42712017-04-21 10:23:37 -0400975 if (is_constant(*bin->fRight, 0)) {
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400976 if (bin->fLeft->fType.kind() == Type::kScalar_Kind &&
977 bin->fRight->fType.kind() == Type::kVector_Kind) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400978 // x - float4(0) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400979 vectorize_left(&b, iter, outUpdated, outNeedsRescan);
980 } else {
981 // x - 0 -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400982 // float4(x) - 0 -> float4(x)
983 // float4(x) - float4(0) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400984 delete_right(&b, iter, outUpdated, outNeedsRescan);
985 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400986 }
987 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400988 case Token::Kind::TK_SLASH:
Ethan Nicholascb670962017-04-20 19:31:52 -0400989 if (is_constant(*bin->fRight, 1)) {
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400990 if (bin->fLeft->fType.kind() == Type::kScalar_Kind &&
991 bin->fRight->fType.kind() == Type::kVector_Kind) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400992 // x / float4(1) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400993 vectorize_left(&b, iter, outUpdated, outNeedsRescan);
994 } else {
995 // x / 1 -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400996 // float4(x) / 1 -> float4(x)
997 // float4(x) / float4(1) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400998 delete_right(&b, iter, outUpdated, outNeedsRescan);
999 }
1000 } else if (is_constant(*bin->fLeft, 0)) {
1001 if (bin->fLeft->fType.kind() == Type::kScalar_Kind &&
Ethan Nicholas08dae922018-01-23 10:31:56 -05001002 bin->fRight->fType.kind() == Type::kVector_Kind &&
1003 !bin->fRight->hasSideEffects()) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001004 // 0 / float4(x) -> float4(0)
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001005 vectorize_left(&b, iter, outUpdated, outNeedsRescan);
1006 } else {
1007 // 0 / x -> 0
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001008 // float4(0) / x -> float4(0)
1009 // float4(0) / float4(x) -> float4(0)
Ethan Nicholas51493ee2017-12-11 12:34:33 -05001010 if (!bin->fRight->hasSideEffects()) {
1011 delete_right(&b, iter, outUpdated, outNeedsRescan);
1012 }
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001013 }
1014 }
1015 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001016 case Token::Kind::TK_PLUSEQ:
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001017 if (is_constant(*bin->fRight, 0)) {
1018 clear_write(*bin->fLeft);
1019 delete_right(&b, iter, outUpdated, outNeedsRescan);
1020 }
1021 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001022 case Token::Kind::TK_MINUSEQ:
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001023 if (is_constant(*bin->fRight, 0)) {
1024 clear_write(*bin->fLeft);
1025 delete_right(&b, iter, outUpdated, outNeedsRescan);
1026 }
1027 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001028 case Token::Kind::TK_STAREQ:
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001029 if (is_constant(*bin->fRight, 1)) {
1030 clear_write(*bin->fLeft);
1031 delete_right(&b, iter, outUpdated, outNeedsRescan);
1032 }
1033 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001034 case Token::Kind::TK_SLASHEQ:
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001035 if (is_constant(*bin->fRight, 1)) {
1036 clear_write(*bin->fLeft);
Ethan Nicholascb670962017-04-20 19:31:52 -04001037 delete_right(&b, iter, outUpdated, outNeedsRescan);
1038 }
1039 break;
1040 default:
1041 break;
1042 }
Ethan Nicholas409f6f02019-09-17 12:34:39 -04001043 break;
1044 }
1045 case Expression::kSwizzle_Kind: {
1046 Swizzle& s = (Swizzle&) *expr;
1047 // detect identity swizzles like foo.rgba
1048 if ((int) s.fComponents.size() == s.fBase->fType.columns()) {
1049 bool identity = true;
1050 for (int i = 0; i < (int) s.fComponents.size(); ++i) {
1051 if (s.fComponents[i] != i) {
1052 identity = false;
1053 break;
1054 }
1055 }
1056 if (identity) {
1057 *outUpdated = true;
1058 if (!try_replace_expression(&b, iter, &s.fBase)) {
1059 *outNeedsRescan = true;
1060 return;
1061 }
1062 SkASSERT((*iter)->fKind == BasicBlock::Node::kExpression_Kind);
1063 break;
1064 }
1065 }
1066 // detect swizzles of swizzles, e.g. replace foo.argb.r000 with foo.a000
1067 if (s.fBase->fKind == Expression::kSwizzle_Kind) {
1068 Swizzle& base = (Swizzle&) *s.fBase;
1069 std::vector<int> final;
1070 for (int c : s.fComponents) {
1071 if (c == SKSL_SWIZZLE_0 || c == SKSL_SWIZZLE_1) {
1072 final.push_back(c);
1073 } else {
1074 final.push_back(base.fComponents[c]);
1075 }
1076 }
1077 *outUpdated = true;
1078 std::unique_ptr<Expression> replacement(new Swizzle(*fContext, base.fBase->clone(),
1079 std::move(final)));
1080 if (!try_replace_expression(&b, iter, &replacement)) {
1081 *outNeedsRescan = true;
1082 return;
1083 }
1084 SkASSERT((*iter)->fKind == BasicBlock::Node::kExpression_Kind);
Ethan Nicholas409f6f02019-09-17 12:34:39 -04001085 }
John Stiles30212b72020-06-11 17:55:07 -04001086 break;
Ethan Nicholascb670962017-04-20 19:31:52 -04001087 }
1088 default:
1089 break;
1090 }
1091}
1092
John Stiles92219b42020-06-15 12:32:24 -04001093// Implementation-detail recursive helper function for `contains_conditional_break`.
1094static bool contains_conditional_break_impl(Statement& s, bool inConditional) {
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001095 switch (s.fKind) {
1096 case Statement::kBlock_Kind:
John Stiles92219b42020-06-15 12:32:24 -04001097 for (const std::unique_ptr<Statement>& sub : static_cast<Block&>(s).fStatements) {
1098 if (contains_conditional_break_impl(*sub, inConditional)) {
Ethan Nicholas5005a222018-08-24 13:06:27 -04001099 return true;
1100 }
1101 }
1102 return false;
John Stiles92219b42020-06-15 12:32:24 -04001103
Ethan Nicholas5005a222018-08-24 13:06:27 -04001104 case Statement::kBreak_Kind:
1105 return inConditional;
John Stiles92219b42020-06-15 12:32:24 -04001106
Ethan Nicholas5005a222018-08-24 13:06:27 -04001107 case Statement::kIf_Kind: {
John Stiles92219b42020-06-15 12:32:24 -04001108 const IfStatement& i = static_cast<IfStatement&>(s);
1109 return contains_conditional_break_impl(*i.fIfTrue, /*inConditional=*/true) ||
1110 (i.fIfFalse &&
1111 contains_conditional_break_impl(*i.fIfFalse, /*inConditional=*/true));
Ethan Nicholas5005a222018-08-24 13:06:27 -04001112 }
John Stiles92219b42020-06-15 12:32:24 -04001113
Ethan Nicholas5005a222018-08-24 13:06:27 -04001114 default:
1115 return false;
1116 }
1117}
1118
John Stiles92219b42020-06-15 12:32:24 -04001119// Returns true if this statement could potentially execute a break at the current level. We ignore
1120// nested loops and switches, since any breaks inside of them will merely break the loop / switch.
1121static bool contains_conditional_break(Statement& s) {
1122 return contains_conditional_break_impl(s, /*inConditional=*/false);
1123}
1124
Ethan Nicholas5005a222018-08-24 13:06:27 -04001125// returns true if this statement definitely executes a break at the current level (we ignore
1126// nested loops and switches, since any breaks inside of them will merely break the loop / switch)
1127static bool contains_unconditional_break(Statement& s) {
1128 switch (s.fKind) {
1129 case Statement::kBlock_Kind:
John Stiles92219b42020-06-15 12:32:24 -04001130 for (const std::unique_ptr<Statement>& sub : static_cast<Block&>(s).fStatements) {
Ethan Nicholas5005a222018-08-24 13:06:27 -04001131 if (contains_unconditional_break(*sub)) {
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001132 return true;
1133 }
1134 }
1135 return false;
John Stiles92219b42020-06-15 12:32:24 -04001136
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001137 case Statement::kBreak_Kind:
1138 return true;
John Stiles92219b42020-06-15 12:32:24 -04001139
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001140 default:
1141 return false;
1142 }
1143}
1144
John Stiles92219b42020-06-15 12:32:24 -04001145static void move_all_but_break(std::unique_ptr<Statement>& stmt,
1146 std::vector<std::unique_ptr<Statement>>* target) {
Ethan Nicholas739e1ca2020-06-11 12:16:14 -04001147 switch (stmt->fKind) {
John Stiles92219b42020-06-15 12:32:24 -04001148 case Statement::kBlock_Kind: {
1149 // Recurse into the block.
1150 Block& block = static_cast<Block&>(*stmt);
1151
1152 std::vector<std::unique_ptr<Statement>> blockStmts;
1153 blockStmts.reserve(block.fStatements.size());
1154 for (std::unique_ptr<Statement>& statementInBlock : block.fStatements) {
1155 move_all_but_break(statementInBlock, &blockStmts);
Ethan Nicholas739e1ca2020-06-11 12:16:14 -04001156 }
John Stiles92219b42020-06-15 12:32:24 -04001157
1158 target->push_back(std::make_unique<Block>(block.fOffset, std::move(blockStmts),
1159 block.fSymbols, block.fIsScope));
Ethan Nicholas739e1ca2020-06-11 12:16:14 -04001160 break;
John Stiles92219b42020-06-15 12:32:24 -04001161 }
1162
Ethan Nicholas739e1ca2020-06-11 12:16:14 -04001163 case Statement::kBreak_Kind:
John Stiles92219b42020-06-15 12:32:24 -04001164 // Do not append a break to the target.
1165 break;
1166
Ethan Nicholas739e1ca2020-06-11 12:16:14 -04001167 default:
John Stiles92219b42020-06-15 12:32:24 -04001168 // Append normal statements to the target.
1169 target->push_back(std::move(stmt));
1170 break;
Ethan Nicholas739e1ca2020-06-11 12:16:14 -04001171 }
1172}
1173
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001174// Returns a block containing all of the statements that will be run if the given case matches
1175// (which, owing to the statements being owned by unique_ptrs, means the switch itself will be
1176// broken by this call and must then be discarded).
1177// Returns null (and leaves the switch unmodified) if no such simple reduction is possible, such as
1178// when break statements appear inside conditionals.
John Stiles92219b42020-06-15 12:32:24 -04001179static std::unique_ptr<Statement> block_for_case(SwitchStatement* switchStatement,
1180 SwitchCase* caseToCapture) {
1181 // We have to be careful to not move any of the pointers until after we're sure we're going to
1182 // succeed, so before we make any changes at all, we check the switch-cases to decide on a plan
1183 // of action. First, find the switch-case we are interested in.
1184 auto iter = switchStatement->fCases.begin();
1185 for (; iter != switchStatement->fCases.end(); ++iter) {
1186 if (iter->get() == caseToCapture) {
1187 break;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001188 }
John Stiles92219b42020-06-15 12:32:24 -04001189 }
1190
1191 // Next, walk forward through the rest of the switch. If we find a conditional break, we're
1192 // stuck and can't simplify at all. If we find an unconditional break, we have a range of
1193 // statements that we can use for simplification.
1194 auto startIter = iter;
1195 Statement* unconditionalBreakStmt = nullptr;
1196 for (; iter != switchStatement->fCases.end(); ++iter) {
1197 for (std::unique_ptr<Statement>& stmt : (*iter)->fStatements) {
1198 if (contains_conditional_break(*stmt)) {
1199 // We can't reduce switch-cases to a block when they have conditional breaks.
1200 return nullptr;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001201 }
John Stiles92219b42020-06-15 12:32:24 -04001202
1203 if (contains_unconditional_break(*stmt)) {
1204 // We found an unconditional break. We can use this block, but we need to strip
1205 // out the break statement.
1206 unconditionalBreakStmt = stmt.get();
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001207 break;
1208 }
1209 }
John Stiles92219b42020-06-15 12:32:24 -04001210
1211 if (unconditionalBreakStmt != nullptr) {
1212 break;
1213 }
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001214 }
John Stiles92219b42020-06-15 12:32:24 -04001215
1216 // We fell off the bottom of the switch or encountered a break. We know the range of statements
1217 // that we need to move over, and we know it's safe to do so.
1218 std::vector<std::unique_ptr<Statement>> caseStmts;
1219
1220 // We can move over most of the statements as-is.
1221 while (startIter != iter) {
1222 for (std::unique_ptr<Statement>& stmt : (*startIter)->fStatements) {
1223 caseStmts.push_back(std::move(stmt));
1224 }
1225 ++startIter;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001226 }
John Stiles92219b42020-06-15 12:32:24 -04001227
1228 // If we found an unconditional break at the end, we need to move what we can while avoiding
1229 // that break.
1230 if (unconditionalBreakStmt != nullptr) {
1231 for (std::unique_ptr<Statement>& stmt : (*startIter)->fStatements) {
1232 if (stmt.get() == unconditionalBreakStmt) {
1233 move_all_but_break(stmt, &caseStmts);
1234 unconditionalBreakStmt = nullptr;
1235 break;
1236 }
1237
1238 caseStmts.push_back(std::move(stmt));
1239 }
1240 }
1241
1242 SkASSERT(unconditionalBreakStmt == nullptr); // Verify that we fixed the unconditional break.
1243
1244 // Return our newly-synthesized block.
1245 return std::make_unique<Block>(/*offset=*/-1, std::move(caseStmts), switchStatement->fSymbols);
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001246}
1247
Ethan Nicholascb670962017-04-20 19:31:52 -04001248void Compiler::simplifyStatement(DefinitionMap& definitions,
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001249 BasicBlock& b,
1250 std::vector<BasicBlock::Node>::iterator* iter,
1251 std::unordered_set<const Variable*>* undefinedVariables,
1252 bool* outUpdated,
1253 bool* outNeedsRescan) {
Ethan Nicholascb670962017-04-20 19:31:52 -04001254 Statement* stmt = (*iter)->statement()->get();
1255 switch (stmt->fKind) {
Ethan Nicholas82a62d22017-11-07 14:42:10 +00001256 case Statement::kVarDeclaration_Kind: {
1257 const auto& varDecl = (VarDeclaration&) *stmt;
1258 if (varDecl.fVar->dead() &&
1259 (!varDecl.fValue ||
1260 !varDecl.fValue->hasSideEffects())) {
1261 if (varDecl.fValue) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001262 SkASSERT((*iter)->statement()->get() == stmt);
Ethan Nicholas82a62d22017-11-07 14:42:10 +00001263 if (!b.tryRemoveExpressionBefore(iter, varDecl.fValue.get())) {
1264 *outNeedsRescan = true;
Ethan Nicholascb670962017-04-20 19:31:52 -04001265 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001266 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001267 (*iter)->setStatement(std::unique_ptr<Statement>(new Nop()));
Ethan Nicholas82a62d22017-11-07 14:42:10 +00001268 *outUpdated = true;
Ethan Nicholascb670962017-04-20 19:31:52 -04001269 }
1270 break;
1271 }
1272 case Statement::kIf_Kind: {
1273 IfStatement& i = (IfStatement&) *stmt;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001274 if (i.fTest->fKind == Expression::kBoolLiteral_Kind) {
1275 // constant if, collapse down to a single branch
1276 if (((BoolLiteral&) *i.fTest).fValue) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001277 SkASSERT(i.fIfTrue);
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001278 (*iter)->setStatement(std::move(i.fIfTrue));
1279 } else {
1280 if (i.fIfFalse) {
1281 (*iter)->setStatement(std::move(i.fIfFalse));
1282 } else {
1283 (*iter)->setStatement(std::unique_ptr<Statement>(new Nop()));
1284 }
1285 }
1286 *outUpdated = true;
1287 *outNeedsRescan = true;
1288 break;
1289 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001290 if (i.fIfFalse && i.fIfFalse->isEmpty()) {
1291 // else block doesn't do anything, remove it
1292 i.fIfFalse.reset();
1293 *outUpdated = true;
1294 *outNeedsRescan = true;
1295 }
1296 if (!i.fIfFalse && i.fIfTrue->isEmpty()) {
1297 // if block doesn't do anything, no else block
1298 if (i.fTest->hasSideEffects()) {
1299 // test has side effects, keep it
1300 (*iter)->setStatement(std::unique_ptr<Statement>(
1301 new ExpressionStatement(std::move(i.fTest))));
1302 } else {
1303 // no if, no else, no test side effects, kill the whole if
1304 // statement
1305 (*iter)->setStatement(std::unique_ptr<Statement>(new Nop()));
1306 }
1307 *outUpdated = true;
1308 *outNeedsRescan = true;
1309 }
1310 break;
1311 }
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001312 case Statement::kSwitch_Kind: {
1313 SwitchStatement& s = (SwitchStatement&) *stmt;
Brian Osmanb6b95732020-06-30 11:44:27 -04001314 if (s.fValue->isCompileTimeConstant()) {
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001315 // switch is constant, replace it with the case that matches
1316 bool found = false;
1317 SwitchCase* defaultCase = nullptr;
1318 for (const auto& c : s.fCases) {
1319 if (!c->fValue) {
1320 defaultCase = c.get();
1321 continue;
1322 }
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001323 SkASSERT(c->fValue->fKind == s.fValue->fKind);
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001324 found = c->fValue->compareConstant(*fContext, *s.fValue);
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001325 if (found) {
1326 std::unique_ptr<Statement> newBlock = block_for_case(&s, c.get());
1327 if (newBlock) {
1328 (*iter)->setStatement(std::move(newBlock));
1329 break;
1330 } else {
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -04001331 if (s.fIsStatic && !(fFlags & kPermitInvalidStaticTests_Flag)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001332 this->error(s.fOffset,
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001333 "static switch contains non-static conditional break");
1334 s.fIsStatic = false;
1335 }
1336 return; // can't simplify
1337 }
1338 }
1339 }
1340 if (!found) {
1341 // no matching case. use default if it exists, or kill the whole thing
1342 if (defaultCase) {
1343 std::unique_ptr<Statement> newBlock = block_for_case(&s, defaultCase);
1344 if (newBlock) {
1345 (*iter)->setStatement(std::move(newBlock));
1346 } else {
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -04001347 if (s.fIsStatic && !(fFlags & kPermitInvalidStaticTests_Flag)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001348 this->error(s.fOffset,
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001349 "static switch contains non-static conditional break");
1350 s.fIsStatic = false;
1351 }
1352 return; // can't simplify
1353 }
1354 } else {
1355 (*iter)->setStatement(std::unique_ptr<Statement>(new Nop()));
1356 }
1357 }
1358 *outUpdated = true;
1359 *outNeedsRescan = true;
1360 }
1361 break;
1362 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001363 case Statement::kExpression_Kind: {
1364 ExpressionStatement& e = (ExpressionStatement&) *stmt;
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001365 SkASSERT((*iter)->statement()->get() == &e);
Ethan Nicholascb670962017-04-20 19:31:52 -04001366 if (!e.fExpression->hasSideEffects()) {
1367 // Expression statement with no side effects, kill it
1368 if (!b.tryRemoveExpressionBefore(iter, e.fExpression.get())) {
1369 *outNeedsRescan = true;
1370 }
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001371 SkASSERT((*iter)->statement()->get() == stmt);
Ethan Nicholascb670962017-04-20 19:31:52 -04001372 (*iter)->setStatement(std::unique_ptr<Statement>(new Nop()));
1373 *outUpdated = true;
1374 }
1375 break;
1376 }
1377 default:
1378 break;
1379 }
1380}
1381
1382void Compiler::scanCFG(FunctionDefinition& f) {
1383 CFG cfg = CFGGenerator().getCFG(f);
1384 this->computeDataFlow(&cfg);
ethannicholas22f939e2016-10-13 13:25:34 -07001385
1386 // check for unreachable code
1387 for (size_t i = 0; i < cfg.fBlocks.size(); i++) {
Mike Klein6ad99092016-10-26 10:35:22 -04001388 if (i != cfg.fStart && !cfg.fBlocks[i].fEntrances.size() &&
ethannicholas22f939e2016-10-13 13:25:34 -07001389 cfg.fBlocks[i].fNodes.size()) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001390 int offset;
Ethan Nicholas86a43402017-01-19 13:32:00 -05001391 switch (cfg.fBlocks[i].fNodes[0].fKind) {
1392 case BasicBlock::Node::kStatement_Kind:
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001393 offset = (*cfg.fBlocks[i].fNodes[0].statement())->fOffset;
Ethan Nicholas86a43402017-01-19 13:32:00 -05001394 break;
1395 case BasicBlock::Node::kExpression_Kind:
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001396 offset = (*cfg.fBlocks[i].fNodes[0].expression())->fOffset;
Ethan Nicholas70728ef2020-05-28 07:09:00 -04001397 if ((*cfg.fBlocks[i].fNodes[0].expression())->fKind ==
1398 Expression::kBoolLiteral_Kind) {
1399 // Function inlining can generate do { ... } while(false) loops which always
1400 // break, so the boolean condition is considered unreachable. Since not
1401 // being able to reach a literal is a non-issue in the first place, we
1402 // don't report an error in this case.
1403 continue;
1404 }
Ethan Nicholas86a43402017-01-19 13:32:00 -05001405 break;
1406 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001407 this->error(offset, String("unreachable"));
ethannicholas22f939e2016-10-13 13:25:34 -07001408 }
1409 }
1410 if (fErrorCount) {
1411 return;
1412 }
1413
Ethan Nicholascb670962017-04-20 19:31:52 -04001414 // check for dead code & undefined variables, perform constant propagation
1415 std::unordered_set<const Variable*> undefinedVariables;
1416 bool updated;
1417 bool needsRescan = false;
1418 do {
1419 if (needsRescan) {
1420 cfg = CFGGenerator().getCFG(f);
1421 this->computeDataFlow(&cfg);
1422 needsRescan = false;
Ethan Nicholas113628d2017-02-02 16:11:39 -05001423 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001424
1425 updated = false;
Ethan Nicholas1de14812020-06-19 15:32:49 -04001426 bool first = true;
Ethan Nicholascb670962017-04-20 19:31:52 -04001427 for (BasicBlock& b : cfg.fBlocks) {
Ethan Nicholas1de14812020-06-19 15:32:49 -04001428 if (!first && b.fEntrances.empty()) {
1429 // Block was reachable before optimization, but has since become unreachable. In
1430 // addition to being dead code, it's broken - since control flow can't reach it, no
1431 // prior variable definitions can reach it, and therefore variables might look to
1432 // have not been properly assigned. Kill it.
1433 for (BasicBlock::Node& node : b.fNodes) {
1434 if (node.fKind == BasicBlock::Node::kStatement_Kind &&
1435 (*node.statement())->fKind != Statement::kNop_Kind) {
1436 node.setStatement(std::unique_ptr<Statement>(new Nop()));
1437 }
1438 }
1439 continue;
1440 }
1441 first = false;
Ethan Nicholascb670962017-04-20 19:31:52 -04001442 DefinitionMap definitions = b.fBefore;
1443
1444 for (auto iter = b.fNodes.begin(); iter != b.fNodes.end() && !needsRescan; ++iter) {
1445 if (iter->fKind == BasicBlock::Node::kExpression_Kind) {
1446 this->simplifyExpression(definitions, b, &iter, &undefinedVariables, &updated,
1447 &needsRescan);
1448 } else {
1449 this->simplifyStatement(definitions, b, &iter, &undefinedVariables, &updated,
1450 &needsRescan);
1451 }
Ethan Nicholas4b330df2017-05-17 10:52:55 -04001452 if (needsRescan) {
1453 break;
1454 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001455 this->addDefinitions(*iter, &definitions);
1456 }
1457 }
1458 } while (updated);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001459 SkASSERT(!needsRescan);
ethannicholas22f939e2016-10-13 13:25:34 -07001460
Ethan Nicholas91a10532017-06-22 11:24:38 -04001461 // verify static ifs & switches, clean up dead variable decls
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001462 for (BasicBlock& b : cfg.fBlocks) {
1463 DefinitionMap definitions = b.fBefore;
1464
Ethan Nicholas91a10532017-06-22 11:24:38 -04001465 for (auto iter = b.fNodes.begin(); iter != b.fNodes.end() && !needsRescan;) {
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001466 if (iter->fKind == BasicBlock::Node::kStatement_Kind) {
1467 const Statement& s = **iter->statement();
1468 switch (s.fKind) {
1469 case Statement::kIf_Kind:
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -04001470 if (((const IfStatement&) s).fIsStatic &&
1471 !(fFlags & kPermitInvalidStaticTests_Flag)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001472 this->error(s.fOffset, "static if has non-static test");
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001473 }
Ethan Nicholas91a10532017-06-22 11:24:38 -04001474 ++iter;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001475 break;
1476 case Statement::kSwitch_Kind:
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -04001477 if (((const SwitchStatement&) s).fIsStatic &&
1478 !(fFlags & kPermitInvalidStaticTests_Flag)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001479 this->error(s.fOffset, "static switch has non-static test");
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001480 }
Ethan Nicholas91a10532017-06-22 11:24:38 -04001481 ++iter;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001482 break;
Ethan Nicholas82a62d22017-11-07 14:42:10 +00001483 case Statement::kVarDeclarations_Kind: {
1484 VarDeclarations& decls = *((VarDeclarationsStatement&) s).fDeclaration;
1485 for (auto varIter = decls.fVars.begin(); varIter != decls.fVars.end();) {
1486 if ((*varIter)->fKind == Statement::kNop_Kind) {
1487 varIter = decls.fVars.erase(varIter);
1488 } else {
1489 ++varIter;
1490 }
1491 }
1492 if (!decls.fVars.size()) {
1493 iter = b.fNodes.erase(iter);
1494 } else {
1495 ++iter;
1496 }
1497 break;
1498 }
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001499 default:
Ethan Nicholas91a10532017-06-22 11:24:38 -04001500 ++iter;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001501 break;
1502 }
Ethan Nicholas91a10532017-06-22 11:24:38 -04001503 } else {
1504 ++iter;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001505 }
1506 }
1507 }
1508
ethannicholas22f939e2016-10-13 13:25:34 -07001509 // check for missing return
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001510 if (f.fDeclaration.fReturnType != *fContext->fVoid_Type) {
ethannicholas22f939e2016-10-13 13:25:34 -07001511 if (cfg.fBlocks[cfg.fExit].fEntrances.size()) {
Ethan Nicholasdb80f692019-11-22 14:06:12 -05001512 this->error(f.fOffset, String("function '" + String(f.fDeclaration.fName) +
1513 "' can exit without returning a value"));
ethannicholas22f939e2016-10-13 13:25:34 -07001514 }
1515 }
1516}
1517
Ethan Nicholas91164d12019-05-15 15:29:54 -04001518void Compiler::registerExternalValue(ExternalValue* value) {
1519 fIRGenerator->fRootSymbolTable->addWithoutOwnership(value->fName, value);
1520}
1521
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001522const Symbol* Compiler::takeOwnership(std::unique_ptr<const Symbol> symbol) {
John Stiles3ae071e2020-08-05 15:29:29 -04001523 return fIRGenerator->fRootSymbolTable->takeOwnershipOfSymbol(std::move(symbol));
Ethan Nicholas91164d12019-05-15 15:29:54 -04001524}
1525
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001526std::unique_ptr<Program> Compiler::convertProgram(Program::Kind kind, String text,
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001527 const Program::Settings& settings) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001528 fErrorText = "";
1529 fErrorCount = 0;
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001530 std::vector<std::unique_ptr<ProgramElement>>* inherited;
ethannicholasd598f792016-07-25 10:08:54 -07001531 std::vector<std::unique_ptr<ProgramElement>> elements;
ethannicholasb3058bd2016-07-01 08:22:01 -07001532 switch (kind) {
1533 case Program::kVertex_Kind:
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001534 inherited = &fVertexInclude;
1535 fIRGenerator->fSymbolTable = fVertexSymbolTable;
Ethan Nicholasdb80f692019-11-22 14:06:12 -05001536 fIRGenerator->fIntrinsics = &fGPUIntrinsics;
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001537 fIRGenerator->start(&settings, inherited);
ethannicholasb3058bd2016-07-01 08:22:01 -07001538 break;
1539 case Program::kFragment_Kind:
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001540 inherited = &fFragmentInclude;
1541 fIRGenerator->fSymbolTable = fFragmentSymbolTable;
Ethan Nicholasdb80f692019-11-22 14:06:12 -05001542 fIRGenerator->fIntrinsics = &fGPUIntrinsics;
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001543 fIRGenerator->start(&settings, inherited);
ethannicholasb3058bd2016-07-01 08:22:01 -07001544 break;
Ethan Nicholas52cad152017-02-16 16:37:32 -05001545 case Program::kGeometry_Kind:
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001546 this->loadGeometryIntrinsics();
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001547 inherited = &fGeometryInclude;
1548 fIRGenerator->fSymbolTable = fGeometrySymbolTable;
Ethan Nicholasdb80f692019-11-22 14:06:12 -05001549 fIRGenerator->fIntrinsics = &fGPUIntrinsics;
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001550 fIRGenerator->start(&settings, inherited);
Ethan Nicholas52cad152017-02-16 16:37:32 -05001551 break;
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -04001552 case Program::kFragmentProcessor_Kind: {
Brian Osmandd496172020-08-08 08:17:18 -04001553#if !SKSL_STANDALONE
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001554 {
1555 Rehydrator rehydrator(fContext.get(), fGpuSymbolTable, this,
1556 SKSL_INCLUDE_sksl_fp,
1557 SKSL_INCLUDE_sksl_fp_LENGTH);
1558 fFPSymbolTable = rehydrator.symbolTable();
1559 fFPInclude = rehydrator.elements();
1560 }
1561 inherited = &fFPInclude;
1562 fIRGenerator->fSymbolTable = fFPSymbolTable;
1563 fIRGenerator->fIntrinsics = &fGPUIntrinsics;
1564 fIRGenerator->start(&settings, inherited);
1565 break;
1566#else
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001567 inherited = nullptr;
Ethan Nicholas8da1e652019-05-24 11:01:59 -04001568 fIRGenerator->fSymbolTable = fGpuSymbolTable;
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -04001569 fIRGenerator->start(&settings, /*inherited=*/nullptr, /*builtin=*/true);
Ethan Nicholasdb80f692019-11-22 14:06:12 -05001570 fIRGenerator->fIntrinsics = &fGPUIntrinsics;
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -04001571 std::ifstream in(SKSL_FP_INCLUDE);
1572 std::string stdText{std::istreambuf_iterator<char>(in),
1573 std::istreambuf_iterator<char>()};
1574 if (in.rdstate()) {
1575 printf("error reading %s\n", SKSL_FP_INCLUDE);
1576 abort();
1577 }
1578 const String* source = fGpuSymbolTable->takeOwnershipOfString(
1579 std::make_unique<String>(stdText.c_str()));
1580 fIRGenerator->convertProgram(kind, source->c_str(), source->length(), &elements);
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001581 fIRGenerator->fIsBuiltinCode = false;
Ethan Nicholas762466e2017-06-29 10:03:38 -04001582 break;
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001583#endif
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -04001584 }
Ethan Nicholas8da1e652019-05-24 11:01:59 -04001585 case Program::kPipelineStage_Kind:
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001586 this->loadPipelineIntrinsics();
Ethan Nicholas8da1e652019-05-24 11:01:59 -04001587 inherited = &fPipelineInclude;
1588 fIRGenerator->fSymbolTable = fPipelineSymbolTable;
Ethan Nicholasdb80f692019-11-22 14:06:12 -05001589 fIRGenerator->fIntrinsics = &fGPUIntrinsics;
Ethan Nicholas8da1e652019-05-24 11:01:59 -04001590 fIRGenerator->start(&settings, inherited);
1591 break;
Ethan Nicholas746035a2019-04-23 13:31:09 -04001592 case Program::kGeneric_Kind:
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001593 this->loadInterpreterIntrinsics();
Ethan Nicholas8da1e652019-05-24 11:01:59 -04001594 inherited = &fInterpreterInclude;
1595 fIRGenerator->fSymbolTable = fInterpreterSymbolTable;
Ethan Nicholasdb80f692019-11-22 14:06:12 -05001596 fIRGenerator->fIntrinsics = &fInterpreterIntrinsics;
Ethan Nicholas8da1e652019-05-24 11:01:59 -04001597 fIRGenerator->start(&settings, inherited);
Ethan Nicholas0d997662019-04-08 09:46:01 -04001598 break;
ethannicholasb3058bd2016-07-01 08:22:01 -07001599 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001600 std::unique_ptr<String> textPtr(new String(std::move(text)));
1601 fSource = textPtr.get();
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001602 fIRGenerator->convertProgram(kind, textPtr->c_str(), textPtr->size(), &elements);
John Stilesfbd050b2020-08-03 13:21:46 -04001603 auto result = std::make_unique<Program>(kind,
1604 std::move(textPtr),
1605 settings,
1606 fContext,
1607 inherited,
1608 std::move(elements),
1609 fIRGenerator->fSymbolTable,
1610 fIRGenerator->fInputs);
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001611 if (fErrorCount) {
1612 return nullptr;
1613 }
1614 return result;
1615}
1616
Ethan Nicholas00543112018-07-31 09:44:36 -04001617bool Compiler::optimize(Program& program) {
1618 SkASSERT(!fErrorCount);
1619 if (!program.fIsOptimized) {
1620 program.fIsOptimized = true;
1621 fIRGenerator->fKind = program.fKind;
1622 fIRGenerator->fSettings = &program.fSettings;
1623 for (auto& element : program) {
1624 if (element.fKind == ProgramElement::kFunction_Kind) {
1625 this->scanCFG((FunctionDefinition&) element);
1626 }
1627 }
Ethan Nicholase8ad02c2020-06-03 16:58:20 -04001628 // we wait until after analysis to remove dead functions so that we still report errors
1629 // even in unused code
1630 if (program.fSettings.fRemoveDeadFunctions) {
1631 for (auto iter = program.fElements.begin(); iter != program.fElements.end(); ) {
1632 if ((*iter)->fKind == ProgramElement::kFunction_Kind) {
1633 const FunctionDefinition& f = (const FunctionDefinition&) **iter;
1634 if (!f.fDeclaration.fCallCount && f.fDeclaration.fName != "main") {
1635 iter = program.fElements.erase(iter);
1636 continue;
1637 }
1638 }
1639 ++iter;
1640 }
1641 }
Ethan Nicholas0dc80872019-02-08 15:46:24 -05001642 if (program.fKind != Program::kFragmentProcessor_Kind) {
1643 for (auto iter = program.fElements.begin(); iter != program.fElements.end();) {
1644 if ((*iter)->fKind == ProgramElement::kVar_Kind) {
1645 VarDeclarations& vars = (VarDeclarations&) **iter;
1646 for (auto varIter = vars.fVars.begin(); varIter != vars.fVars.end();) {
1647 const Variable& var = *((VarDeclaration&) **varIter).fVar;
1648 if (var.dead()) {
1649 varIter = vars.fVars.erase(varIter);
1650 } else {
1651 ++varIter;
1652 }
1653 }
1654 if (vars.fVars.size() == 0) {
1655 iter = program.fElements.erase(iter);
1656 continue;
1657 }
1658 }
1659 ++iter;
1660 }
1661 }
Ethan Nicholas00543112018-07-31 09:44:36 -04001662 }
1663 return fErrorCount == 0;
1664}
1665
Brian Osmanfb32ddf2019-06-18 10:14:20 -04001666#if defined(SKSL_STANDALONE) || SK_SUPPORT_GPU
1667
Ethan Nicholas00543112018-07-31 09:44:36 -04001668bool Compiler::toSPIRV(Program& program, OutputStream& out) {
1669 if (!this->optimize(program)) {
1670 return false;
1671 }
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001672#ifdef SK_ENABLE_SPIRV_VALIDATION
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001673 StringStream buffer;
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001674 fSource = program.fSource.get();
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001675 SPIRVCodeGenerator cg(fContext.get(), &program, this, &buffer);
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001676 bool result = cg.generateCode();
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001677 fSource = nullptr;
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001678 if (result) {
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001679 spvtools::SpirvTools tools(SPV_ENV_VULKAN_1_0);
Ethan Nicholas762466e2017-06-29 10:03:38 -04001680 const String& data = buffer.str();
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001681 SkASSERT(0 == data.size() % 4);
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001682 auto dumpmsg = [](spv_message_level_t, const char*, const spv_position_t&, const char* m) {
1683 SkDebugf("SPIR-V validation error: %s\n", m);
1684 };
1685 tools.SetMessageConsumer(dumpmsg);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001686 // Verify that the SPIR-V we produced is valid. If this SkASSERT fails, check the logs prior
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001687 // to the failure to see the validation errors.
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001688 SkAssertResult(tools.Validate((const uint32_t*) data.c_str(), data.size() / 4));
Ethan Nicholas762466e2017-06-29 10:03:38 -04001689 out.write(data.c_str(), data.size());
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001690 }
1691#else
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001692 fSource = program.fSource.get();
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001693 SPIRVCodeGenerator cg(fContext.get(), &program, this, &out);
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001694 bool result = cg.generateCode();
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001695 fSource = nullptr;
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001696#endif
Ethan Nicholasce33f102016-12-09 17:22:59 -05001697 return result;
1698}
1699
Ethan Nicholas00543112018-07-31 09:44:36 -04001700bool Compiler::toSPIRV(Program& program, String* out) {
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001701 StringStream buffer;
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001702 bool result = this->toSPIRV(program, buffer);
1703 if (result) {
Ethan Nicholas762466e2017-06-29 10:03:38 -04001704 *out = buffer.str();
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001705 }
1706 return result;
1707}
1708
Ethan Nicholas00543112018-07-31 09:44:36 -04001709bool Compiler::toGLSL(Program& program, OutputStream& out) {
1710 if (!this->optimize(program)) {
1711 return false;
1712 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001713 fSource = program.fSource.get();
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001714 GLSLCodeGenerator cg(fContext.get(), &program, this, &out);
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001715 bool result = cg.generateCode();
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001716 fSource = nullptr;
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001717 return result;
1718}
1719
Ethan Nicholas00543112018-07-31 09:44:36 -04001720bool Compiler::toGLSL(Program& program, String* out) {
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001721 StringStream buffer;
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001722 bool result = this->toGLSL(program, buffer);
1723 if (result) {
Ethan Nicholas762466e2017-06-29 10:03:38 -04001724 *out = buffer.str();
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001725 }
1726 return result;
1727}
1728
Brian Osmanc0243912020-02-19 15:35:26 -05001729bool Compiler::toHLSL(Program& program, String* out) {
1730 String spirv;
1731 if (!this->toSPIRV(program, &spirv)) {
1732 return false;
1733 }
1734
1735 return SPIRVtoHLSL(spirv, out);
1736}
1737
Ethan Nicholas00543112018-07-31 09:44:36 -04001738bool Compiler::toMetal(Program& program, OutputStream& out) {
1739 if (!this->optimize(program)) {
1740 return false;
1741 }
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001742 MetalCodeGenerator cg(fContext.get(), &program, this, &out);
Ethan Nicholascc305772017-10-13 16:17:45 -04001743 bool result = cg.generateCode();
Ethan Nicholascc305772017-10-13 16:17:45 -04001744 return result;
1745}
1746
Ethan Nicholas00543112018-07-31 09:44:36 -04001747bool Compiler::toMetal(Program& program, String* out) {
1748 if (!this->optimize(program)) {
1749 return false;
1750 }
Timothy Liangb8eeb802018-07-23 16:46:16 -04001751 StringStream buffer;
1752 bool result = this->toMetal(program, buffer);
1753 if (result) {
1754 *out = buffer.str();
1755 }
1756 return result;
1757}
1758
Ethan Nicholas00543112018-07-31 09:44:36 -04001759bool Compiler::toCPP(Program& program, String name, OutputStream& out) {
1760 if (!this->optimize(program)) {
1761 return false;
1762 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001763 fSource = program.fSource.get();
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001764 CPPCodeGenerator cg(fContext.get(), &program, this, name, &out);
Ethan Nicholas762466e2017-06-29 10:03:38 -04001765 bool result = cg.generateCode();
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001766 fSource = nullptr;
Ethan Nicholas762466e2017-06-29 10:03:38 -04001767 return result;
1768}
1769
Ethan Nicholas00543112018-07-31 09:44:36 -04001770bool Compiler::toH(Program& program, String name, OutputStream& out) {
1771 if (!this->optimize(program)) {
1772 return false;
1773 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001774 fSource = program.fSource.get();
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001775 HCodeGenerator cg(fContext.get(), &program, this, name, &out);
Ethan Nicholas762466e2017-06-29 10:03:38 -04001776 bool result = cg.generateCode();
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001777 fSource = nullptr;
Ethan Nicholas00543112018-07-31 09:44:36 -04001778 return result;
1779}
1780
Brian Osman2e29ab52019-09-20 12:19:11 -04001781#endif
1782
1783#if !defined(SKSL_STANDALONE) && SK_SUPPORT_GPU
Brian Osmana4b91692020-08-10 14:26:16 -04001784bool Compiler::toPipelineStage(Program& program, PipelineStageArgs* outArgs) {
1785 if (!this->optimize(program)) {
1786 return false;
1787 }
Ethan Nicholas00543112018-07-31 09:44:36 -04001788 fSource = program.fSource.get();
1789 StringStream buffer;
Brian Osman300fe1d2020-01-23 15:42:43 -05001790 PipelineStageCodeGenerator cg(fContext.get(), &program, this, &buffer, outArgs);
Ethan Nicholas00543112018-07-31 09:44:36 -04001791 bool result = cg.generateCode();
1792 fSource = nullptr;
1793 if (result) {
Brian Osman107c6662019-12-30 15:02:30 -05001794 outArgs->fCode = buffer.str();
Ethan Nicholas00543112018-07-31 09:44:36 -04001795 }
Ethan Nicholas762466e2017-06-29 10:03:38 -04001796 return result;
1797}
Brian Osmanfb32ddf2019-06-18 10:14:20 -04001798#endif
1799
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001800std::unique_ptr<ByteCode> Compiler::toByteCode(Program& program) {
Brian Osman489cf882019-07-09 10:48:28 -04001801#if defined(SK_ENABLE_SKSL_INTERPRETER)
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001802 if (!this->optimize(program)) {
1803 return nullptr;
1804 }
Brian Osman808f0212020-01-21 15:36:47 -05001805 fSource = program.fSource.get();
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001806 std::unique_ptr<ByteCode> result(new ByteCode());
Brian Osmanb08cc022020-04-02 11:38:40 -04001807 ByteCodeGenerator cg(fContext.get(), &program, this, result.get());
1808 bool success = cg.generateCode();
1809 fSource = nullptr;
1810 if (success) {
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001811 return result;
1812 }
Brian Osman489cf882019-07-09 10:48:28 -04001813#else
1814 ABORT("ByteCode interpreter not enabled");
1815#endif
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001816 return nullptr;
1817}
1818
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001819const char* Compiler::OperatorName(Token::Kind kind) {
1820 switch (kind) {
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001821 case Token::Kind::TK_PLUS: return "+";
1822 case Token::Kind::TK_MINUS: return "-";
1823 case Token::Kind::TK_STAR: return "*";
1824 case Token::Kind::TK_SLASH: return "/";
1825 case Token::Kind::TK_PERCENT: return "%";
1826 case Token::Kind::TK_SHL: return "<<";
1827 case Token::Kind::TK_SHR: return ">>";
1828 case Token::Kind::TK_LOGICALNOT: return "!";
1829 case Token::Kind::TK_LOGICALAND: return "&&";
1830 case Token::Kind::TK_LOGICALOR: return "||";
1831 case Token::Kind::TK_LOGICALXOR: return "^^";
1832 case Token::Kind::TK_BITWISENOT: return "~";
1833 case Token::Kind::TK_BITWISEAND: return "&";
1834 case Token::Kind::TK_BITWISEOR: return "|";
1835 case Token::Kind::TK_BITWISEXOR: return "^";
1836 case Token::Kind::TK_EQ: return "=";
1837 case Token::Kind::TK_EQEQ: return "==";
1838 case Token::Kind::TK_NEQ: return "!=";
1839 case Token::Kind::TK_LT: return "<";
1840 case Token::Kind::TK_GT: return ">";
1841 case Token::Kind::TK_LTEQ: return "<=";
1842 case Token::Kind::TK_GTEQ: return ">=";
1843 case Token::Kind::TK_PLUSEQ: return "+=";
1844 case Token::Kind::TK_MINUSEQ: return "-=";
1845 case Token::Kind::TK_STAREQ: return "*=";
1846 case Token::Kind::TK_SLASHEQ: return "/=";
1847 case Token::Kind::TK_PERCENTEQ: return "%=";
1848 case Token::Kind::TK_SHLEQ: return "<<=";
1849 case Token::Kind::TK_SHREQ: return ">>=";
1850 case Token::Kind::TK_LOGICALANDEQ: return "&&=";
1851 case Token::Kind::TK_LOGICALOREQ: return "||=";
1852 case Token::Kind::TK_LOGICALXOREQ: return "^^=";
1853 case Token::Kind::TK_BITWISEANDEQ: return "&=";
1854 case Token::Kind::TK_BITWISEOREQ: return "|=";
1855 case Token::Kind::TK_BITWISEXOREQ: return "^=";
1856 case Token::Kind::TK_PLUSPLUS: return "++";
1857 case Token::Kind::TK_MINUSMINUS: return "--";
1858 case Token::Kind::TK_COMMA: return ",";
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001859 default:
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001860 ABORT("unsupported operator: %d\n", (int) kind);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001861 }
1862}
1863
1864
1865bool Compiler::IsAssignment(Token::Kind op) {
1866 switch (op) {
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001867 case Token::Kind::TK_EQ: // fall through
1868 case Token::Kind::TK_PLUSEQ: // fall through
1869 case Token::Kind::TK_MINUSEQ: // fall through
1870 case Token::Kind::TK_STAREQ: // fall through
1871 case Token::Kind::TK_SLASHEQ: // fall through
1872 case Token::Kind::TK_PERCENTEQ: // fall through
1873 case Token::Kind::TK_SHLEQ: // fall through
1874 case Token::Kind::TK_SHREQ: // fall through
1875 case Token::Kind::TK_BITWISEOREQ: // fall through
1876 case Token::Kind::TK_BITWISEXOREQ: // fall through
1877 case Token::Kind::TK_BITWISEANDEQ: // fall through
1878 case Token::Kind::TK_LOGICALOREQ: // fall through
1879 case Token::Kind::TK_LOGICALXOREQ: // fall through
1880 case Token::Kind::TK_LOGICALANDEQ:
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001881 return true;
1882 default:
1883 return false;
1884 }
1885}
1886
1887Position Compiler::position(int offset) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001888 SkASSERT(fSource);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001889 int line = 1;
1890 int column = 1;
1891 for (int i = 0; i < offset; i++) {
1892 if ((*fSource)[i] == '\n') {
1893 ++line;
1894 column = 1;
1895 }
1896 else {
1897 ++column;
1898 }
1899 }
1900 return Position(line, column);
1901}
1902
1903void Compiler::error(int offset, String msg) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001904 fErrorCount++;
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001905 Position pos = this->position(offset);
1906 fErrorText += "error: " + to_string(pos.fLine) + ": " + msg.c_str() + "\n";
ethannicholasb3058bd2016-07-01 08:22:01 -07001907}
1908
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001909String Compiler::errorText() {
Ethan Nicholas00543112018-07-31 09:44:36 -04001910 this->writeErrorCount();
1911 fErrorCount = 0;
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001912 String result = fErrorText;
ethannicholasb3058bd2016-07-01 08:22:01 -07001913 return result;
1914}
1915
1916void Compiler::writeErrorCount() {
1917 if (fErrorCount) {
1918 fErrorText += to_string(fErrorCount) + " error";
1919 if (fErrorCount > 1) {
1920 fErrorText += "s";
1921 }
1922 fErrorText += "\n";
1923 }
1924}
1925
John Stilesa6841be2020-08-06 14:11:56 -04001926} // namespace SkSL