blob: 147a81393a7c2045818c5dd7a4780f06f710c393 [file] [log] [blame]
ethannicholasb3058bd2016-07-01 08:22:01 -07001/*
2 * Copyright 2016 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
Mike Klein6ad99092016-10-26 10:35:22 -04007
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/sksl/SkSLCompiler.h"
ethannicholasb3058bd2016-07-01 08:22:01 -07009
John Stilesfbd050b2020-08-03 13:21:46 -040010#include <memory>
John Stilesb8e010c2020-08-11 18:05:39 -040011#include <unordered_set>
John Stilesfbd050b2020-08-03 13:21:46 -040012
John Stilesb92641c2020-08-31 18:09:01 -040013#include "src/sksl/SkSLAnalysis.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "src/sksl/SkSLByteCodeGenerator.h"
15#include "src/sksl/SkSLCFGGenerator.h"
16#include "src/sksl/SkSLCPPCodeGenerator.h"
17#include "src/sksl/SkSLGLSLCodeGenerator.h"
18#include "src/sksl/SkSLHCodeGenerator.h"
19#include "src/sksl/SkSLIRGenerator.h"
20#include "src/sksl/SkSLMetalCodeGenerator.h"
21#include "src/sksl/SkSLPipelineStageCodeGenerator.h"
Ethan Nicholasc18bb512020-07-28 14:46:53 -040022#include "src/sksl/SkSLRehydrator.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "src/sksl/SkSLSPIRVCodeGenerator.h"
Brian Osmanc0243912020-02-19 15:35:26 -050024#include "src/sksl/SkSLSPIRVtoHLSL.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "src/sksl/ir/SkSLEnum.h"
26#include "src/sksl/ir/SkSLExpression.h"
27#include "src/sksl/ir/SkSLExpressionStatement.h"
28#include "src/sksl/ir/SkSLFunctionCall.h"
29#include "src/sksl/ir/SkSLIntLiteral.h"
30#include "src/sksl/ir/SkSLModifiersDeclaration.h"
31#include "src/sksl/ir/SkSLNop.h"
32#include "src/sksl/ir/SkSLSymbolTable.h"
33#include "src/sksl/ir/SkSLTernaryExpression.h"
34#include "src/sksl/ir/SkSLUnresolvedFunction.h"
35#include "src/sksl/ir/SkSLVarDeclarations.h"
ethannicholasb3058bd2016-07-01 08:22:01 -070036
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -040037#include <fstream>
38
Ethan Nicholasa11035b2019-11-26 16:27:47 -050039#if !defined(SKSL_STANDALONE) & SK_SUPPORT_GPU
40#include "include/gpu/GrContextOptions.h"
41#include "src/gpu/GrShaderCaps.h"
42#endif
43
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -040044#ifdef SK_ENABLE_SPIRV_VALIDATION
45#include "spirv-tools/libspirv.hpp"
46#endif
47
Brian Osmandd496172020-08-08 08:17:18 -040048#if !SKSL_STANDALONE
Ethan Nicholasc18bb512020-07-28 14:46:53 -040049
50#include "src/sksl/generated/sksl_fp.dehydrated.sksl"
51#include "src/sksl/generated/sksl_frag.dehydrated.sksl"
52#include "src/sksl/generated/sksl_geom.dehydrated.sksl"
53#include "src/sksl/generated/sksl_gpu.dehydrated.sksl"
54#include "src/sksl/generated/sksl_interp.dehydrated.sksl"
55#include "src/sksl/generated/sksl_pipeline.dehydrated.sksl"
56#include "src/sksl/generated/sksl_vert.dehydrated.sksl"
57
58#else
59
Brian Osmandd496172020-08-08 08:17:18 -040060// GN generates or copies all of these files to the skslc executable directory
61static const char SKSL_GPU_INCLUDE[] = "sksl_gpu.sksl";
62static const char SKSL_INTERP_INCLUDE[] = "sksl_interp.sksl";
63static const char SKSL_VERT_INCLUDE[] = "sksl_vert.sksl";
64static const char SKSL_FRAG_INCLUDE[] = "sksl_frag.sksl";
65static const char SKSL_GEOM_INCLUDE[] = "sksl_geom.sksl";
66static const char SKSL_FP_INCLUDE[] = "sksl_fp.sksl";
67static const char SKSL_PIPELINE_INCLUDE[] = "sksl_pipeline.sksl";
Ethan Nicholasc18bb512020-07-28 14:46:53 -040068
69#endif
Ethan Nicholas0d997662019-04-08 09:46:01 -040070
ethannicholasb3058bd2016-07-01 08:22:01 -070071namespace SkSL {
72
Ethan Nicholasdb80f692019-11-22 14:06:12 -050073static void grab_intrinsics(std::vector<std::unique_ptr<ProgramElement>>* src,
John Stiles810c8cf2020-08-26 19:46:27 -040074 IRIntrinsicMap* target) {
Brian Osman08f986d2020-05-13 17:06:46 -040075 for (auto iter = src->begin(); iter != src->end(); ) {
76 std::unique_ptr<ProgramElement>& element = *iter;
Ethan Nicholase6592142020-09-08 10:22:09 -040077 switch (element->kind()) {
78 case ProgramElement::Kind::kFunction: {
John Stiles3dc0da62020-08-19 17:48:31 -040079 FunctionDefinition& f = element->as<FunctionDefinition>();
Brian Osman08f986d2020-05-13 17:06:46 -040080 SkASSERT(f.fDeclaration.fBuiltin);
Brian Osman2b469eb2020-09-21 11:32:10 -040081 target->insertOrDie(f.fDeclaration.description(), std::move(element));
Brian Osman08f986d2020-05-13 17:06:46 -040082 iter = src->erase(iter);
Ethan Nicholasdb80f692019-11-22 14:06:12 -050083 break;
84 }
Ethan Nicholase6592142020-09-08 10:22:09 -040085 case ProgramElement::Kind::kEnum: {
John Stiles3dc0da62020-08-19 17:48:31 -040086 Enum& e = element->as<Enum>();
Brian Osman2b469eb2020-09-21 11:32:10 -040087 target->insertOrDie(e.fTypeName, std::move(element));
Brian Osman08f986d2020-05-13 17:06:46 -040088 iter = src->erase(iter);
Ethan Nicholasdb80f692019-11-22 14:06:12 -050089 break;
90 }
91 default:
Brian Osman2b469eb2020-09-21 11:32:10 -040092 // Unsupported element, leave it in the list.
93 ++iter;
94 break;
Ethan Nicholasdb80f692019-11-22 14:06:12 -050095 }
96 }
97}
98
John Stilesbc0c29e2020-09-28 13:13:40 -040099static void reset_call_counts(std::vector<std::unique_ptr<ProgramElement>>* src) {
100 for (std::unique_ptr<ProgramElement>& element : *src) {
101 if (element->is<FunctionDefinition>()) {
102 const FunctionDeclaration& fnDecl = element->as<FunctionDefinition>().fDeclaration;
103 fnDecl.fCallCount = 0;
104 }
105 }
106}
107
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -0400108Compiler::Compiler(Flags flags)
Brian Osman2b469eb2020-09-21 11:32:10 -0400109: fGPUIntrinsics(std::make_unique<IRIntrinsicMap>(/*parent=*/nullptr))
110, fInterpreterIntrinsics(std::make_unique<IRIntrinsicMap>(/*parent=*/nullptr))
John Stiles810c8cf2020-08-26 19:46:27 -0400111, fFlags(flags)
112, fContext(std::make_shared<Context>())
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -0400113, fErrorCount(0) {
Brian Osmaneac49832020-09-18 11:49:22 -0400114 fRootSymbolTable = std::make_shared<SymbolTable>(this);
John Stiles941fc712020-09-19 12:47:10 +0000115 fIRGenerator =
116 std::make_unique<IRGenerator>(fContext.get(), &fInliner, fRootSymbolTable, *this);
Brian Osmaneac49832020-09-18 11:49:22 -0400117 #define ADD_TYPE(t) fRootSymbolTable->addWithoutOwnership(fContext->f ## t ## _Type->fName, \
118 fContext->f ## t ## _Type.get())
ethannicholasb3058bd2016-07-01 08:22:01 -0700119 ADD_TYPE(Void);
120 ADD_TYPE(Float);
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400121 ADD_TYPE(Float2);
122 ADD_TYPE(Float3);
123 ADD_TYPE(Float4);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400124 ADD_TYPE(Half);
125 ADD_TYPE(Half2);
126 ADD_TYPE(Half3);
127 ADD_TYPE(Half4);
ethannicholasb3058bd2016-07-01 08:22:01 -0700128 ADD_TYPE(Int);
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400129 ADD_TYPE(Int2);
130 ADD_TYPE(Int3);
131 ADD_TYPE(Int4);
ethannicholasb3058bd2016-07-01 08:22:01 -0700132 ADD_TYPE(UInt);
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400133 ADD_TYPE(UInt2);
134 ADD_TYPE(UInt3);
135 ADD_TYPE(UInt4);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400136 ADD_TYPE(Short);
137 ADD_TYPE(Short2);
138 ADD_TYPE(Short3);
139 ADD_TYPE(Short4);
140 ADD_TYPE(UShort);
141 ADD_TYPE(UShort2);
142 ADD_TYPE(UShort3);
143 ADD_TYPE(UShort4);
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400144 ADD_TYPE(Byte);
145 ADD_TYPE(Byte2);
146 ADD_TYPE(Byte3);
147 ADD_TYPE(Byte4);
148 ADD_TYPE(UByte);
149 ADD_TYPE(UByte2);
150 ADD_TYPE(UByte3);
151 ADD_TYPE(UByte4);
ethannicholasb3058bd2016-07-01 08:22:01 -0700152 ADD_TYPE(Bool);
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400153 ADD_TYPE(Bool2);
154 ADD_TYPE(Bool3);
155 ADD_TYPE(Bool4);
156 ADD_TYPE(Float2x2);
157 ADD_TYPE(Float2x3);
158 ADD_TYPE(Float2x4);
159 ADD_TYPE(Float3x2);
160 ADD_TYPE(Float3x3);
161 ADD_TYPE(Float3x4);
162 ADD_TYPE(Float4x2);
163 ADD_TYPE(Float4x3);
164 ADD_TYPE(Float4x4);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400165 ADD_TYPE(Half2x2);
166 ADD_TYPE(Half2x3);
167 ADD_TYPE(Half2x4);
168 ADD_TYPE(Half3x2);
169 ADD_TYPE(Half3x3);
170 ADD_TYPE(Half3x4);
171 ADD_TYPE(Half4x2);
172 ADD_TYPE(Half4x3);
173 ADD_TYPE(Half4x4);
ethannicholasb3058bd2016-07-01 08:22:01 -0700174 ADD_TYPE(GenType);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400175 ADD_TYPE(GenHType);
ethannicholasb3058bd2016-07-01 08:22:01 -0700176 ADD_TYPE(GenIType);
177 ADD_TYPE(GenUType);
178 ADD_TYPE(GenBType);
179 ADD_TYPE(Mat);
180 ADD_TYPE(Vec);
181 ADD_TYPE(GVec);
182 ADD_TYPE(GVec2);
183 ADD_TYPE(GVec3);
184 ADD_TYPE(GVec4);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400185 ADD_TYPE(HVec);
ethannicholasb3058bd2016-07-01 08:22:01 -0700186 ADD_TYPE(IVec);
187 ADD_TYPE(UVec);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400188 ADD_TYPE(SVec);
189 ADD_TYPE(USVec);
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400190 ADD_TYPE(ByteVec);
191 ADD_TYPE(UByteVec);
ethannicholasb3058bd2016-07-01 08:22:01 -0700192 ADD_TYPE(BVec);
193
194 ADD_TYPE(Sampler1D);
195 ADD_TYPE(Sampler2D);
196 ADD_TYPE(Sampler3D);
ethannicholas5961bc92016-10-12 06:39:56 -0700197 ADD_TYPE(SamplerExternalOES);
ethannicholasb3058bd2016-07-01 08:22:01 -0700198 ADD_TYPE(SamplerCube);
199 ADD_TYPE(Sampler2DRect);
200 ADD_TYPE(Sampler1DArray);
201 ADD_TYPE(Sampler2DArray);
202 ADD_TYPE(SamplerCubeArray);
203 ADD_TYPE(SamplerBuffer);
204 ADD_TYPE(Sampler2DMS);
205 ADD_TYPE(Sampler2DMSArray);
206
Brian Salomonbf7b6202016-11-11 16:08:03 -0500207 ADD_TYPE(ISampler2D);
208
Brian Salomon2a51de82016-11-16 12:06:01 -0500209 ADD_TYPE(Image2D);
210 ADD_TYPE(IImage2D);
211
Greg Daniel64773e62016-11-22 09:44:03 -0500212 ADD_TYPE(SubpassInput);
213 ADD_TYPE(SubpassInputMS);
214
ethannicholasb3058bd2016-07-01 08:22:01 -0700215 ADD_TYPE(GSampler1D);
216 ADD_TYPE(GSampler2D);
217 ADD_TYPE(GSampler3D);
218 ADD_TYPE(GSamplerCube);
219 ADD_TYPE(GSampler2DRect);
220 ADD_TYPE(GSampler1DArray);
221 ADD_TYPE(GSampler2DArray);
222 ADD_TYPE(GSamplerCubeArray);
223 ADD_TYPE(GSamplerBuffer);
224 ADD_TYPE(GSampler2DMS);
225 ADD_TYPE(GSampler2DMSArray);
226
227 ADD_TYPE(Sampler1DShadow);
228 ADD_TYPE(Sampler2DShadow);
229 ADD_TYPE(SamplerCubeShadow);
230 ADD_TYPE(Sampler2DRectShadow);
231 ADD_TYPE(Sampler1DArrayShadow);
232 ADD_TYPE(Sampler2DArrayShadow);
233 ADD_TYPE(SamplerCubeArrayShadow);
234 ADD_TYPE(GSampler2DArrayShadow);
235 ADD_TYPE(GSamplerCubeArrayShadow);
Ethan Nicholasc9472af2017-10-10 16:30:21 -0400236 ADD_TYPE(FragmentProcessor);
Stephen Whiteff5d7a22019-07-26 17:42:06 -0400237 ADD_TYPE(Sampler);
238 ADD_TYPE(Texture2D);
ethannicholasb3058bd2016-07-01 08:22:01 -0700239
Brian Osman28590d52020-03-23 16:59:08 -0400240 StringFragment fpAliasName("shader");
Brian Osmaneac49832020-09-18 11:49:22 -0400241 fRootSymbolTable->addWithoutOwnership(fpAliasName, fContext->fFragmentProcessor_Type.get());
Brian Osman28590d52020-03-23 16:59:08 -0400242
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700243 StringFragment skCapsName("sk_Caps");
Brian Osmaneac49832020-09-18 11:49:22 -0400244 fRootSymbolTable->add(
John Stiles311dd9d2020-08-13 17:09:29 -0400245 skCapsName,
246 std::make_unique<Variable>(/*offset=*/-1, Modifiers(), skCapsName,
Ethan Nicholas30d30222020-09-11 12:27:26 -0400247 fContext->fSkCaps_Type.get(), Variable::kGlobal_Storage));
Ethan Nicholas3605ace2016-11-21 15:59:48 -0500248
John Stiles810c8cf2020-08-26 19:46:27 -0400249 fIRGenerator->fIntrinsics = fGPUIntrinsics.get();
Ethan Nicholasdb80f692019-11-22 14:06:12 -0500250 std::vector<std::unique_ptr<ProgramElement>> gpuIntrinsics;
Brian Osmandd496172020-08-08 08:17:18 -0400251#if SKSL_STANDALONE
Brian Osmaneac49832020-09-18 11:49:22 -0400252 this->processIncludeFile(Program::kFragment_Kind, SKSL_GPU_INCLUDE, fRootSymbolTable,
253 &gpuIntrinsics, &fGpuSymbolTable);
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -0400254 this->processIncludeFile(Program::kVertex_Kind, SKSL_VERT_INCLUDE, fGpuSymbolTable,
255 &fVertexInclude, &fVertexSymbolTable);
256 this->processIncludeFile(Program::kFragment_Kind, SKSL_FRAG_INCLUDE, fGpuSymbolTable,
257 &fFragmentInclude, &fFragmentSymbolTable);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400258#else
259 {
Brian Osmaneac49832020-09-18 11:49:22 -0400260 Rehydrator rehydrator(fContext.get(), fRootSymbolTable, this, SKSL_INCLUDE_sksl_gpu,
261 SKSL_INCLUDE_sksl_gpu_LENGTH);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400262 fGpuSymbolTable = rehydrator.symbolTable();
263 gpuIntrinsics = rehydrator.elements();
264 }
265 {
266 Rehydrator rehydrator(fContext.get(), fGpuSymbolTable, this, SKSL_INCLUDE_sksl_vert,
Brian Osmaneac49832020-09-18 11:49:22 -0400267 SKSL_INCLUDE_sksl_vert_LENGTH);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400268 fVertexSymbolTable = rehydrator.symbolTable();
269 fVertexInclude = rehydrator.elements();
270 }
271 {
272 Rehydrator rehydrator(fContext.get(), fGpuSymbolTable, this, SKSL_INCLUDE_sksl_frag,
Brian Osmaneac49832020-09-18 11:49:22 -0400273 SKSL_INCLUDE_sksl_frag_LENGTH);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400274 fFragmentSymbolTable = rehydrator.symbolTable();
275 fFragmentInclude = rehydrator.elements();
276 }
277#endif
John Stilesbc0c29e2020-09-28 13:13:40 -0400278 // Call counts are used to track dead-stripping and inlinability within the program being
279 // currently compiled, and always should start at zero for a new program. Zero out any call
280 // counts that were registered during the assembly of the intrinsics/include data. (If we
281 // actually use calls from inside the intrinsics, we will clone them into the program and they
282 // will get new call counts.)
283 reset_call_counts(&gpuIntrinsics);
284 reset_call_counts(&fVertexInclude);
285 reset_call_counts(&fFragmentInclude);
286
John Stiles810c8cf2020-08-26 19:46:27 -0400287 grab_intrinsics(&gpuIntrinsics, fGPUIntrinsics.get());
Brian Osman2b469eb2020-09-21 11:32:10 -0400288 SkASSERT(gpuIntrinsics.empty());
ethannicholasb3058bd2016-07-01 08:22:01 -0700289}
290
John Stiles656427a2020-08-27 15:26:26 -0400291Compiler::~Compiler() {}
ethannicholasb3058bd2016-07-01 08:22:01 -0700292
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400293void Compiler::loadGeometryIntrinsics() {
294 if (fGeometrySymbolTable) {
295 return;
296 }
Brian Osmandd496172020-08-08 08:17:18 -0400297 #if !SKSL_STANDALONE
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400298 {
299 Rehydrator rehydrator(fContext.get(), fGpuSymbolTable, this, SKSL_INCLUDE_sksl_geom,
300 SKSL_INCLUDE_sksl_geom_LENGTH);
301 fGeometrySymbolTable = rehydrator.symbolTable();
302 fGeometryInclude = rehydrator.elements();
303 }
304 #else
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -0400305 this->processIncludeFile(Program::kGeometry_Kind, SKSL_GEOM_INCLUDE, fGpuSymbolTable,
306 &fGeometryInclude, &fGeometrySymbolTable);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400307 #endif
308}
309
310void Compiler::loadPipelineIntrinsics() {
311 if (fPipelineSymbolTable) {
312 return;
313 }
Brian Osmandd496172020-08-08 08:17:18 -0400314 #if !SKSL_STANDALONE
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400315 {
316 Rehydrator rehydrator(fContext.get(), fGpuSymbolTable, this,
317 SKSL_INCLUDE_sksl_pipeline,
318 SKSL_INCLUDE_sksl_pipeline_LENGTH);
319 fPipelineSymbolTable = rehydrator.symbolTable();
320 fPipelineInclude = rehydrator.elements();
321 }
322 #else
323 this->processIncludeFile(Program::kPipelineStage_Kind, SKSL_PIPELINE_INCLUDE,
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -0400324 fGpuSymbolTable, &fPipelineInclude, &fPipelineSymbolTable);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400325 #endif
326}
327
328void Compiler::loadInterpreterIntrinsics() {
329 if (fInterpreterSymbolTable) {
330 return;
331 }
Brian Osmaneac49832020-09-18 11:49:22 -0400332 std::vector<std::unique_ptr<ProgramElement>> interpIntrinsics;
Brian Osmandd496172020-08-08 08:17:18 -0400333 #if !SKSL_STANDALONE
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400334 {
Brian Osmaneac49832020-09-18 11:49:22 -0400335 Rehydrator rehydrator(fContext.get(), fRootSymbolTable, this,
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400336 SKSL_INCLUDE_sksl_interp,
337 SKSL_INCLUDE_sksl_interp_LENGTH);
338 fInterpreterSymbolTable = rehydrator.symbolTable();
Brian Osmaneac49832020-09-18 11:49:22 -0400339 interpIntrinsics = rehydrator.elements();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400340 }
341 #else
342 this->processIncludeFile(Program::kGeneric_Kind, SKSL_INTERP_INCLUDE,
Brian Osmaneac49832020-09-18 11:49:22 -0400343 fIRGenerator->fSymbolTable, &interpIntrinsics,
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -0400344 &fInterpreterSymbolTable);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400345 #endif
Brian Osmaneac49832020-09-18 11:49:22 -0400346 grab_intrinsics(&interpIntrinsics, fInterpreterIntrinsics.get());
Brian Osman2b469eb2020-09-21 11:32:10 -0400347 SkASSERT(interpIntrinsics.empty());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400348}
349
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -0400350void Compiler::processIncludeFile(Program::Kind kind, const char* path,
Ethan Nicholas8da1e652019-05-24 11:01:59 -0400351 std::shared_ptr<SymbolTable> base,
352 std::vector<std::unique_ptr<ProgramElement>>* outElements,
353 std::shared_ptr<SymbolTable>* outSymbolTable) {
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -0400354 std::ifstream in(path);
Brian Osmane498b3c2020-09-23 14:42:11 -0400355 std::unique_ptr<String> text = std::make_unique<String>(std::istreambuf_iterator<char>(in),
356 std::istreambuf_iterator<char>());
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -0400357 if (in.rdstate()) {
358 printf("error reading %s\n", path);
359 abort();
360 }
Brian Osmane498b3c2020-09-23 14:42:11 -0400361 const String* source = fRootSymbolTable->takeOwnershipOfString(std::move(text));
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -0400362 fSource = source;
Ethan Nicholas8da1e652019-05-24 11:01:59 -0400363 Program::Settings settings;
Ethan Nicholasa11035b2019-11-26 16:27:47 -0500364#if !defined(SKSL_STANDALONE) & SK_SUPPORT_GPU
365 GrContextOptions opts;
366 GrShaderCaps caps(opts);
367 settings.fCaps = &caps;
368#endif
John Stiles881a10c2020-09-19 10:13:24 -0400369 SkASSERT(fIRGenerator->fCanInline);
370 fIRGenerator->fCanInline = false;
Brian Osmane498b3c2020-09-23 14:42:11 -0400371 fIRGenerator->start(&settings, base ? base : fRootSymbolTable, nullptr, true);
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -0400372 fIRGenerator->convertProgram(kind, source->c_str(), source->length(), outElements);
John Stiles881a10c2020-09-19 10:13:24 -0400373 fIRGenerator->fCanInline = true;
Ethan Nicholas8da1e652019-05-24 11:01:59 -0400374 if (this->fErrorCount) {
375 printf("Unexpected errors: %s\n", this->fErrorText.c_str());
376 }
377 SkASSERT(!fErrorCount);
Ethan Nicholas8da1e652019-05-24 11:01:59 -0400378 *outSymbolTable = fIRGenerator->fSymbolTable;
Ethan Nicholasa11035b2019-11-26 16:27:47 -0500379#ifdef SK_DEBUG
380 fSource = nullptr;
381#endif
Brian Osmane498b3c2020-09-23 14:42:11 -0400382 fIRGenerator->finish();
Ethan Nicholas8da1e652019-05-24 11:01:59 -0400383}
384
ethannicholas22f939e2016-10-13 13:25:34 -0700385// add the definition created by assigning to the lvalue to the definition set
Ethan Nicholas86a43402017-01-19 13:32:00 -0500386void Compiler::addDefinition(const Expression* lvalue, std::unique_ptr<Expression>* expr,
387 DefinitionMap* definitions) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400388 switch (lvalue->kind()) {
389 case Expression::Kind::kVariableReference: {
Brian Osman79457ef2020-09-24 15:01:27 -0400390 const Variable& var = *lvalue->as<VariableReference>().fVariable;
ethannicholas22f939e2016-10-13 13:25:34 -0700391 if (var.fStorage == Variable::kLocal_Storage) {
392 (*definitions)[&var] = expr;
393 }
394 break;
395 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400396 case Expression::Kind::kSwizzle:
ethannicholas22f939e2016-10-13 13:25:34 -0700397 // We consider the variable written to as long as at least some of its components have
398 // been written to. This will lead to some false negatives (we won't catch it if you
399 // write to foo.x and then read foo.y), but being stricter could lead to false positives
Mike Klein6ad99092016-10-26 10:35:22 -0400400 // (we write to foo.x, and then pass foo to a function which happens to only read foo.x,
401 // but since we pass foo as a whole it is flagged as an error) unless we perform a much
ethannicholas22f939e2016-10-13 13:25:34 -0700402 // more complicated whole-program analysis. This is probably good enough.
John Stilesa5a97b42020-08-18 11:19:07 -0400403 this->addDefinition(lvalue->as<Swizzle>().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 Nicholase6592142020-09-08 10:22:09 -0400407 case Expression::Kind::kIndex:
ethannicholas22f939e2016-10-13 13:25:34 -0700408 // see comments in Swizzle
John Stilesa5a97b42020-08-18 11:19:07 -0400409 this->addDefinition(lvalue->as<IndexExpression>().fBase.get(),
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400410 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
ethannicholas22f939e2016-10-13 13:25:34 -0700411 definitions);
412 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400413 case Expression::Kind::kFieldAccess:
ethannicholas22f939e2016-10-13 13:25:34 -0700414 // see comments in Swizzle
John Stilesa5a97b42020-08-18 11:19:07 -0400415 this->addDefinition(lvalue->as<FieldAccess>().fBase.get(),
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400416 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
ethannicholas22f939e2016-10-13 13:25:34 -0700417 definitions);
418 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400419 case Expression::Kind::kTernary:
Ethan Nicholasa583b812018-01-18 13:32:11 -0500420 // To simplify analysis, we just pretend that we write to both sides of the ternary.
421 // This allows for false positives (meaning we fail to detect that a variable might not
422 // have been assigned), but is preferable to false negatives.
John Stilesa5a97b42020-08-18 11:19:07 -0400423 this->addDefinition(lvalue->as<TernaryExpression>().fIfTrue.get(),
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400424 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
Ethan Nicholasa583b812018-01-18 13:32:11 -0500425 definitions);
John Stilesa5a97b42020-08-18 11:19:07 -0400426 this->addDefinition(lvalue->as<TernaryExpression>().fIfFalse.get(),
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400427 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
Ethan Nicholasa583b812018-01-18 13:32:11 -0500428 definitions);
429 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400430 case Expression::Kind::kExternalValue:
Ethan Nicholas91164d12019-05-15 15:29:54 -0400431 break;
ethannicholas22f939e2016-10-13 13:25:34 -0700432 default:
433 // not an lvalue, can't happen
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400434 SkASSERT(false);
ethannicholas22f939e2016-10-13 13:25:34 -0700435 }
436}
437
438// add local variables defined by this node to the set
Mike Klein6ad99092016-10-26 10:35:22 -0400439void Compiler::addDefinitions(const BasicBlock::Node& node,
Ethan Nicholas86a43402017-01-19 13:32:00 -0500440 DefinitionMap* definitions) {
ethannicholas22f939e2016-10-13 13:25:34 -0700441 switch (node.fKind) {
442 case BasicBlock::Node::kExpression_Kind: {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400443 SkASSERT(node.expression());
John Stilesa5a97b42020-08-18 11:19:07 -0400444 Expression* expr = node.expression()->get();
Ethan Nicholase6592142020-09-08 10:22:09 -0400445 switch (expr->kind()) {
446 case Expression::Kind::kBinary: {
John Stilesa5a97b42020-08-18 11:19:07 -0400447 BinaryExpression* b = &expr->as<BinaryExpression>();
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400448 if (b->getOperator() == Token::Kind::TK_EQ) {
449 this->addDefinition(&b->left(), &b->rightPointer(), definitions);
450 } else if (Compiler::IsAssignment(b->getOperator())) {
Ethan Nicholas86a43402017-01-19 13:32:00 -0500451 this->addDefinition(
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400452 &b->left(),
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400453 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
454 definitions);
Ethan Nicholas86a43402017-01-19 13:32:00 -0500455
456 }
457 break;
ethannicholas22f939e2016-10-13 13:25:34 -0700458 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400459 case Expression::Kind::kFunctionCall: {
John Stilesa5a97b42020-08-18 11:19:07 -0400460 const FunctionCall& c = expr->as<FunctionCall>();
Ethan Nicholasc6a19f12018-03-29 16:46:56 -0400461 for (size_t i = 0; i < c.fFunction.fParameters.size(); ++i) {
462 if (c.fFunction.fParameters[i]->fModifiers.fFlags & Modifiers::kOut_Flag) {
463 this->addDefinition(
464 c.fArguments[i].get(),
465 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
466 definitions);
467 }
468 }
469 break;
470 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400471 case Expression::Kind::kPrefix: {
John Stilesa5a97b42020-08-18 11:19:07 -0400472 const PrefixExpression* p = &expr->as<PrefixExpression>();
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400473 if (p->fOperator == Token::Kind::TK_MINUSMINUS ||
474 p->fOperator == Token::Kind::TK_PLUSPLUS) {
Ethan Nicholas86a43402017-01-19 13:32:00 -0500475 this->addDefinition(
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400476 p->fOperand.get(),
477 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
478 definitions);
Ethan Nicholas86a43402017-01-19 13:32:00 -0500479 }
480 break;
481 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400482 case Expression::Kind::kPostfix: {
John Stilesa5a97b42020-08-18 11:19:07 -0400483 const PostfixExpression* p = &expr->as<PostfixExpression>();
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400484 if (p->fOperator == Token::Kind::TK_MINUSMINUS ||
485 p->fOperator == Token::Kind::TK_PLUSPLUS) {
Ethan Nicholas86a43402017-01-19 13:32:00 -0500486 this->addDefinition(
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400487 p->fOperand.get(),
488 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
489 definitions);
Ethan Nicholas86a43402017-01-19 13:32:00 -0500490 }
491 break;
492 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400493 case Expression::Kind::kVariableReference: {
John Stilesa5a97b42020-08-18 11:19:07 -0400494 const VariableReference* v = &expr->as<VariableReference>();
Ethan Nicholascb670962017-04-20 19:31:52 -0400495 if (v->fRefKind != VariableReference::kRead_RefKind) {
496 this->addDefinition(
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400497 v,
498 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
499 definitions);
Ethan Nicholascb670962017-04-20 19:31:52 -0400500 }
John Stiles30212b72020-06-11 17:55:07 -0400501 break;
Ethan Nicholascb670962017-04-20 19:31:52 -0400502 }
Ethan Nicholas86a43402017-01-19 13:32:00 -0500503 default:
504 break;
ethannicholas22f939e2016-10-13 13:25:34 -0700505 }
506 break;
507 }
508 case BasicBlock::Node::kStatement_Kind: {
John Stilesa5a97b42020-08-18 11:19:07 -0400509 Statement* stmt = node.statement()->get();
Ethan Nicholase6592142020-09-08 10:22:09 -0400510 if (stmt->kind() == Statement::Kind::kVarDeclaration) {
John Stilesa5a97b42020-08-18 11:19:07 -0400511 VarDeclaration& vd = stmt->as<VarDeclaration>();
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000512 if (vd.fValue) {
513 (*definitions)[vd.fVar] = &vd.fValue;
ethannicholas22f939e2016-10-13 13:25:34 -0700514 }
515 }
516 break;
517 }
518 }
519}
520
521void Compiler::scanCFG(CFG* cfg, BlockId blockId, std::set<BlockId>* workList) {
522 BasicBlock& block = cfg->fBlocks[blockId];
523
524 // compute definitions after this block
Ethan Nicholas86a43402017-01-19 13:32:00 -0500525 DefinitionMap after = block.fBefore;
ethannicholas22f939e2016-10-13 13:25:34 -0700526 for (const BasicBlock::Node& n : block.fNodes) {
527 this->addDefinitions(n, &after);
528 }
529
530 // propagate definitions to exits
531 for (BlockId exitId : block.fExits) {
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400532 if (exitId == blockId) {
533 continue;
534 }
ethannicholas22f939e2016-10-13 13:25:34 -0700535 BasicBlock& exit = cfg->fBlocks[exitId];
536 for (const auto& pair : after) {
Ethan Nicholas86a43402017-01-19 13:32:00 -0500537 std::unique_ptr<Expression>* e1 = pair.second;
538 auto found = exit.fBefore.find(pair.first);
539 if (found == exit.fBefore.end()) {
540 // exit has no definition for it, just copy it
541 workList->insert(exitId);
ethannicholas22f939e2016-10-13 13:25:34 -0700542 exit.fBefore[pair.first] = e1;
543 } else {
Ethan Nicholas86a43402017-01-19 13:32:00 -0500544 // exit has a (possibly different) value already defined
545 std::unique_ptr<Expression>* e2 = exit.fBefore[pair.first];
ethannicholas22f939e2016-10-13 13:25:34 -0700546 if (e1 != e2) {
547 // definition has changed, merge and add exit block to worklist
548 workList->insert(exitId);
Ethan Nicholasaf197692017-02-27 13:26:45 -0500549 if (e1 && e2) {
550 exit.fBefore[pair.first] =
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400551 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression;
Ethan Nicholasaf197692017-02-27 13:26:45 -0500552 } else {
553 exit.fBefore[pair.first] = nullptr;
554 }
ethannicholas22f939e2016-10-13 13:25:34 -0700555 }
556 }
557 }
558 }
559}
560
561// returns a map which maps all local variables in the function to null, indicating that their value
562// is initially unknown
Ethan Nicholas86a43402017-01-19 13:32:00 -0500563static DefinitionMap compute_start_state(const CFG& cfg) {
564 DefinitionMap result;
Mike Klein6ad99092016-10-26 10:35:22 -0400565 for (const auto& block : cfg.fBlocks) {
566 for (const auto& node : block.fNodes) {
ethannicholas22f939e2016-10-13 13:25:34 -0700567 if (node.fKind == BasicBlock::Node::kStatement_Kind) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400568 SkASSERT(node.statement());
Ethan Nicholascb670962017-04-20 19:31:52 -0400569 const Statement* s = node.statement()->get();
Brian Osman79457ef2020-09-24 15:01:27 -0400570 if (s->is<VarDeclarationsStatement>()) {
John Stilesa5a97b42020-08-18 11:19:07 -0400571 const VarDeclarationsStatement* vd = &s->as<VarDeclarationsStatement>();
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000572 for (const auto& decl : vd->fDeclaration->fVars) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400573 if (decl->kind() == Statement::Kind::kVarDeclaration) {
John Stilesa5a97b42020-08-18 11:19:07 -0400574 result[decl->as<VarDeclaration>().fVar] = nullptr;
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000575 }
Mike Klein6ad99092016-10-26 10:35:22 -0400576 }
ethannicholas22f939e2016-10-13 13:25:34 -0700577 }
578 }
579 }
580 }
581 return result;
582}
583
Ethan Nicholascb670962017-04-20 19:31:52 -0400584/**
585 * Returns true if assigning to this lvalue has no effect.
586 */
587static bool is_dead(const Expression& lvalue) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400588 switch (lvalue.kind()) {
589 case Expression::Kind::kVariableReference:
Brian Osman79457ef2020-09-24 15:01:27 -0400590 return lvalue.as<VariableReference>().fVariable->dead();
Ethan Nicholase6592142020-09-08 10:22:09 -0400591 case Expression::Kind::kSwizzle:
John Stilesa5a97b42020-08-18 11:19:07 -0400592 return is_dead(*lvalue.as<Swizzle>().fBase);
Ethan Nicholase6592142020-09-08 10:22:09 -0400593 case Expression::Kind::kFieldAccess:
John Stilesa5a97b42020-08-18 11:19:07 -0400594 return is_dead(*lvalue.as<FieldAccess>().fBase);
Ethan Nicholase6592142020-09-08 10:22:09 -0400595 case Expression::Kind::kIndex: {
John Stilesa5a97b42020-08-18 11:19:07 -0400596 const IndexExpression& idx = lvalue.as<IndexExpression>();
Ethan Nicholas2a099da2020-01-02 14:40:54 -0500597 return is_dead(*idx.fBase) &&
598 !idx.fIndex->hasProperty(Expression::Property::kSideEffects);
Ethan Nicholascb670962017-04-20 19:31:52 -0400599 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400600 case Expression::Kind::kTernary: {
John Stilesa5a97b42020-08-18 11:19:07 -0400601 const TernaryExpression& t = lvalue.as<TernaryExpression>();
Ethan Nicholasa583b812018-01-18 13:32:11 -0500602 return !t.fTest->hasSideEffects() && is_dead(*t.fIfTrue) && is_dead(*t.fIfFalse);
603 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400604 case Expression::Kind::kExternalValue:
Ethan Nicholas91164d12019-05-15 15:29:54 -0400605 return false;
Ethan Nicholascb670962017-04-20 19:31:52 -0400606 default:
Ethan Nicholas2a099da2020-01-02 14:40:54 -0500607#ifdef SK_DEBUG
Ethan Nicholascb670962017-04-20 19:31:52 -0400608 ABORT("invalid lvalue: %s\n", lvalue.description().c_str());
Ethan Nicholas2a099da2020-01-02 14:40:54 -0500609#endif
610 return false;
Ethan Nicholascb670962017-04-20 19:31:52 -0400611 }
612}
ethannicholas22f939e2016-10-13 13:25:34 -0700613
Ethan Nicholascb670962017-04-20 19:31:52 -0400614/**
615 * Returns true if this is an assignment which can be collapsed down to just the right hand side due
616 * to a dead target and lack of side effects on the left hand side.
617 */
618static bool dead_assignment(const BinaryExpression& b) {
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400619 if (!Compiler::IsAssignment(b.getOperator())) {
Ethan Nicholascb670962017-04-20 19:31:52 -0400620 return false;
621 }
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400622 return is_dead(b.left());
Ethan Nicholascb670962017-04-20 19:31:52 -0400623}
624
625void Compiler::computeDataFlow(CFG* cfg) {
626 cfg->fBlocks[cfg->fStart].fBefore = compute_start_state(*cfg);
ethannicholas22f939e2016-10-13 13:25:34 -0700627 std::set<BlockId> workList;
Ethan Nicholascb670962017-04-20 19:31:52 -0400628 for (BlockId i = 0; i < cfg->fBlocks.size(); i++) {
ethannicholas22f939e2016-10-13 13:25:34 -0700629 workList.insert(i);
630 }
631 while (workList.size()) {
632 BlockId next = *workList.begin();
633 workList.erase(workList.begin());
Ethan Nicholascb670962017-04-20 19:31:52 -0400634 this->scanCFG(cfg, next, &workList);
ethannicholas22f939e2016-10-13 13:25:34 -0700635 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400636}
637
638/**
639 * Attempts to replace the expression pointed to by iter with a new one (in both the CFG and the
640 * IR). If the expression can be cleanly removed, returns true and updates the iterator to point to
641 * the newly-inserted element. Otherwise updates only the IR and returns false (and the CFG will
642 * need to be regenerated).
643 */
John Stilesafbf8992020-08-18 10:08:21 -0400644static bool try_replace_expression(BasicBlock* b,
645 std::vector<BasicBlock::Node>::iterator* iter,
646 std::unique_ptr<Expression>* newExpression) {
Ethan Nicholascb670962017-04-20 19:31:52 -0400647 std::unique_ptr<Expression>* target = (*iter)->expression();
648 if (!b->tryRemoveExpression(iter)) {
649 *target = std::move(*newExpression);
650 return false;
651 }
652 *target = std::move(*newExpression);
653 return b->tryInsertExpression(iter, target);
654}
655
656/**
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400657 * Returns true if the expression is a constant numeric literal with the specified value, or a
658 * constant vector with all elements equal to the specified value.
Ethan Nicholascb670962017-04-20 19:31:52 -0400659 */
John Stiles9d944232020-08-19 09:56:49 -0400660template <typename T = double>
661static bool is_constant(const Expression& expr, T value) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400662 switch (expr.kind()) {
663 case Expression::Kind::kIntLiteral:
John Stiles81365af2020-08-18 09:24:00 -0400664 return expr.as<IntLiteral>().fValue == value;
John Stiles9d944232020-08-19 09:56:49 -0400665
Ethan Nicholase6592142020-09-08 10:22:09 -0400666 case Expression::Kind::kFloatLiteral:
John Stiles81365af2020-08-18 09:24:00 -0400667 return expr.as<FloatLiteral>().fValue == value;
John Stiles9d944232020-08-19 09:56:49 -0400668
Ethan Nicholase6592142020-09-08 10:22:09 -0400669 case Expression::Kind::kConstructor: {
John Stiles9d944232020-08-19 09:56:49 -0400670 const Constructor& constructor = expr.as<Constructor>();
671 if (constructor.isCompileTimeConstant()) {
Ethan Nicholas30d30222020-09-11 12:27:26 -0400672 const Type& constructorType = constructor.type();
673 bool isFloat = constructorType.columns() > 1
674 ? constructorType.componentType().isFloat()
675 : constructorType.isFloat();
676 switch (constructorType.typeKind()) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400677 case Type::TypeKind::kVector:
Ethan Nicholas30d30222020-09-11 12:27:26 -0400678 for (int i = 0; i < constructorType.columns(); ++i) {
John Stiles9d944232020-08-19 09:56:49 -0400679 if (isFloat) {
680 if (constructor.getFVecComponent(i) != value) {
681 return false;
682 }
683 } else {
684 if (constructor.getIVecComponent(i) != value) {
685 return false;
686 }
687 }
Ethan Nicholasd188c182019-06-10 15:55:38 -0400688 }
John Stiles9d944232020-08-19 09:56:49 -0400689 return true;
690
Ethan Nicholase6592142020-09-08 10:22:09 -0400691 case Type::TypeKind::kScalar:
John Stiles9d944232020-08-19 09:56:49 -0400692 SkASSERT(constructor.fArguments.size() == 1);
693 return is_constant<T>(*constructor.fArguments[0], value);
694
695 default:
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400696 return false;
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400697 }
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400698 }
699 return false;
700 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400701 default:
702 return false;
703 }
704}
705
706/**
707 * Collapses the binary expression pointed to by iter down to just the right side (in both the IR
708 * and CFG structures).
709 */
John Stilesafbf8992020-08-18 10:08:21 -0400710static void delete_left(BasicBlock* b,
711 std::vector<BasicBlock::Node>::iterator* iter,
712 bool* outUpdated,
713 bool* outNeedsRescan) {
Ethan Nicholascb670962017-04-20 19:31:52 -0400714 *outUpdated = true;
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400715 std::unique_ptr<Expression>* target = (*iter)->expression();
John Stilesa5a97b42020-08-18 11:19:07 -0400716 BinaryExpression& bin = (*target)->as<BinaryExpression>();
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400717 Expression& left = bin.left();
718 std::unique_ptr<Expression>& rightPointer = bin.rightPointer();
719 SkASSERT(!left.hasSideEffects());
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400720 bool result;
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400721 if (bin.getOperator() == Token::Kind::TK_EQ) {
722 result = b->tryRemoveLValueBefore(iter, &left);
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400723 } else {
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400724 result = b->tryRemoveExpressionBefore(iter, &left);
Ethan Nicholascb670962017-04-20 19:31:52 -0400725 }
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400726 *target = std::move(rightPointer);
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400727 if (!result) {
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400728 *outNeedsRescan = true;
729 return;
730 }
731 if (*iter == b->fNodes.begin()) {
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400732 *outNeedsRescan = true;
733 return;
734 }
735 --(*iter);
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400736 if ((*iter)->fKind != BasicBlock::Node::kExpression_Kind ||
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400737 (*iter)->expression() != &rightPointer) {
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400738 *outNeedsRescan = true;
739 return;
740 }
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400741 *iter = b->fNodes.erase(*iter);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400742 SkASSERT((*iter)->expression() == target);
Ethan Nicholascb670962017-04-20 19:31:52 -0400743}
744
745/**
746 * Collapses the binary expression pointed to by iter down to just the left side (in both the IR and
747 * CFG structures).
748 */
John Stilesafbf8992020-08-18 10:08:21 -0400749static void delete_right(BasicBlock* b,
750 std::vector<BasicBlock::Node>::iterator* iter,
751 bool* outUpdated,
752 bool* outNeedsRescan) {
Ethan Nicholascb670962017-04-20 19:31:52 -0400753 *outUpdated = true;
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400754 std::unique_ptr<Expression>* target = (*iter)->expression();
John Stilesa5a97b42020-08-18 11:19:07 -0400755 BinaryExpression& bin = (*target)->as<BinaryExpression>();
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400756 std::unique_ptr<Expression>& leftPointer = bin.leftPointer();
757 Expression& right = bin.right();
758 SkASSERT(!right.hasSideEffects());
759 if (!b->tryRemoveExpressionBefore(iter, &right)) {
760 *target = std::move(leftPointer);
Ethan Nicholascb670962017-04-20 19:31:52 -0400761 *outNeedsRescan = true;
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400762 return;
Ethan Nicholascb670962017-04-20 19:31:52 -0400763 }
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400764 *target = std::move(leftPointer);
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400765 if (*iter == b->fNodes.begin()) {
766 *outNeedsRescan = true;
767 return;
768 }
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400769 --(*iter);
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400770 if (((*iter)->fKind != BasicBlock::Node::kExpression_Kind ||
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400771 (*iter)->expression() != &leftPointer)) {
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400772 *outNeedsRescan = true;
773 return;
774 }
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400775 *iter = b->fNodes.erase(*iter);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400776 SkASSERT((*iter)->expression() == target);
Ethan Nicholascb670962017-04-20 19:31:52 -0400777}
778
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400779/**
780 * Constructs the specified type using a single argument.
781 */
Ethan Nicholas30d30222020-09-11 12:27:26 -0400782static std::unique_ptr<Expression> construct(const Type* type, std::unique_ptr<Expression> v) {
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400783 std::vector<std::unique_ptr<Expression>> args;
784 args.push_back(std::move(v));
Ethan Nicholase6592142020-09-08 10:22:09 -0400785 std::unique_ptr<Expression> result = std::make_unique<Constructor>(-1, type, std::move(args));
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400786 return result;
787}
788
789/**
790 * Used in the implementations of vectorize_left and vectorize_right. Given a vector type and an
791 * expression x, deletes the expression pointed to by iter and replaces it with <type>(x).
792 */
793static void vectorize(BasicBlock* b,
794 std::vector<BasicBlock::Node>::iterator* iter,
795 const Type& type,
796 std::unique_ptr<Expression>* otherExpression,
797 bool* outUpdated,
798 bool* outNeedsRescan) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400799 SkASSERT((*(*iter)->expression())->kind() == Expression::Kind::kBinary);
800 SkASSERT(type.typeKind() == Type::TypeKind::kVector);
Ethan Nicholas30d30222020-09-11 12:27:26 -0400801 SkASSERT((*otherExpression)->type().typeKind() == Type::TypeKind::kScalar);
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400802 *outUpdated = true;
803 std::unique_ptr<Expression>* target = (*iter)->expression();
804 if (!b->tryRemoveExpression(iter)) {
Ethan Nicholas30d30222020-09-11 12:27:26 -0400805 *target = construct(&type, std::move(*otherExpression));
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400806 *outNeedsRescan = true;
807 } else {
Ethan Nicholas30d30222020-09-11 12:27:26 -0400808 *target = construct(&type, std::move(*otherExpression));
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400809 if (!b->tryInsertExpression(iter, target)) {
810 *outNeedsRescan = true;
811 }
812 }
813}
814
815/**
816 * Given a binary expression of the form x <op> vec<n>(y), deletes the right side and vectorizes the
817 * left to yield vec<n>(x).
818 */
819static void vectorize_left(BasicBlock* b,
820 std::vector<BasicBlock::Node>::iterator* iter,
821 bool* outUpdated,
822 bool* outNeedsRescan) {
John Stilesa5a97b42020-08-18 11:19:07 -0400823 BinaryExpression& bin = (*(*iter)->expression())->as<BinaryExpression>();
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400824 vectorize(b, iter, bin.right().type(), &bin.leftPointer(), outUpdated, outNeedsRescan);
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400825}
826
827/**
828 * Given a binary expression of the form vec<n>(x) <op> y, deletes the left side and vectorizes the
829 * right to yield vec<n>(y).
830 */
831static void vectorize_right(BasicBlock* b,
832 std::vector<BasicBlock::Node>::iterator* iter,
833 bool* outUpdated,
834 bool* outNeedsRescan) {
John Stilesa5a97b42020-08-18 11:19:07 -0400835 BinaryExpression& bin = (*(*iter)->expression())->as<BinaryExpression>();
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400836 vectorize(b, iter, bin.left().type(), &bin.rightPointer(), outUpdated, outNeedsRescan);
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400837}
838
839// Mark that an expression which we were writing to is no longer being written to
John Stilesa5a97b42020-08-18 11:19:07 -0400840static void clear_write(Expression& expr) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400841 switch (expr.kind()) {
842 case Expression::Kind::kVariableReference: {
John Stilesa5a97b42020-08-18 11:19:07 -0400843 expr.as<VariableReference>().setRefKind(VariableReference::kRead_RefKind);
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400844 break;
845 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400846 case Expression::Kind::kFieldAccess:
John Stilesa5a97b42020-08-18 11:19:07 -0400847 clear_write(*expr.as<FieldAccess>().fBase);
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400848 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400849 case Expression::Kind::kSwizzle:
John Stilesa5a97b42020-08-18 11:19:07 -0400850 clear_write(*expr.as<Swizzle>().fBase);
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400851 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400852 case Expression::Kind::kIndex:
John Stilesa5a97b42020-08-18 11:19:07 -0400853 clear_write(*expr.as<IndexExpression>().fBase);
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400854 break;
855 default:
856 ABORT("shouldn't be writing to this kind of expression\n");
857 break;
858 }
859}
860
Ethan Nicholascb670962017-04-20 19:31:52 -0400861void Compiler::simplifyExpression(DefinitionMap& definitions,
862 BasicBlock& b,
863 std::vector<BasicBlock::Node>::iterator* iter,
864 std::unordered_set<const Variable*>* undefinedVariables,
865 bool* outUpdated,
866 bool* outNeedsRescan) {
867 Expression* expr = (*iter)->expression()->get();
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400868 SkASSERT(expr);
Ethan Nicholascb670962017-04-20 19:31:52 -0400869 if ((*iter)->fConstantPropagation) {
870 std::unique_ptr<Expression> optimized = expr->constantPropagate(*fIRGenerator, definitions);
871 if (optimized) {
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400872 *outUpdated = true;
Ethan Nicholas30d30222020-09-11 12:27:26 -0400873 optimized = fIRGenerator->coerce(std::move(optimized), expr->type());
Ethan Nicholas1d881522020-09-11 09:32:54 -0400874 SkASSERT(optimized);
Ethan Nicholascb670962017-04-20 19:31:52 -0400875 if (!try_replace_expression(&b, iter, &optimized)) {
876 *outNeedsRescan = true;
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400877 return;
Ethan Nicholascb670962017-04-20 19:31:52 -0400878 }
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400879 SkASSERT((*iter)->fKind == BasicBlock::Node::kExpression_Kind);
Ethan Nicholascb670962017-04-20 19:31:52 -0400880 expr = (*iter)->expression()->get();
Ethan Nicholascb670962017-04-20 19:31:52 -0400881 }
882 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400883 switch (expr->kind()) {
884 case Expression::Kind::kVariableReference: {
John Stilesa5a97b42020-08-18 11:19:07 -0400885 const VariableReference& ref = expr->as<VariableReference>();
Brian Osman79457ef2020-09-24 15:01:27 -0400886 const Variable* var = ref.fVariable;
Ethan Nicholas8f7e28f2018-03-26 14:24:27 -0400887 if (ref.refKind() != VariableReference::kWrite_RefKind &&
888 ref.refKind() != VariableReference::kPointer_RefKind &&
Brian Osman79457ef2020-09-24 15:01:27 -0400889 var->fStorage == Variable::kLocal_Storage && !definitions[var] &&
890 (*undefinedVariables).find(var) == (*undefinedVariables).end()) {
891 (*undefinedVariables).insert(var);
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000892 this->error(expr->fOffset,
Brian Osman79457ef2020-09-24 15:01:27 -0400893 "'" + var->fName + "' has not been assigned");
Ethan Nicholascb670962017-04-20 19:31:52 -0400894 }
895 break;
896 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400897 case Expression::Kind::kTernary: {
John Stiles403a3632020-08-20 12:11:48 -0400898 TernaryExpression* t = &expr->as<TernaryExpression>();
Ethan Nicholase6592142020-09-08 10:22:09 -0400899 if (t->fTest->kind() == Expression::Kind::kBoolLiteral) {
Ethan Nicholascb670962017-04-20 19:31:52 -0400900 // ternary has a constant test, replace it with either the true or
901 // false branch
Ethan Nicholas59d660c2020-09-28 09:18:15 -0400902 if (t->fTest->as<BoolLiteral>().value()) {
Ethan Nicholascb670962017-04-20 19:31:52 -0400903 (*iter)->setExpression(std::move(t->fIfTrue));
904 } else {
905 (*iter)->setExpression(std::move(t->fIfFalse));
906 }
907 *outUpdated = true;
908 *outNeedsRescan = true;
909 }
910 break;
911 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400912 case Expression::Kind::kBinary: {
John Stiles403a3632020-08-20 12:11:48 -0400913 BinaryExpression* bin = &expr->as<BinaryExpression>();
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400914 if (dead_assignment(*bin)) {
915 delete_left(&b, iter, outUpdated, outNeedsRescan);
916 break;
917 }
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400918 Expression& left = bin->left();
919 Expression& right = bin->right();
920 const Type& leftType = left.type();
921 const Type& rightType = right.type();
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400922 // collapse useless expressions like x * 1 or x + 0
Ethan Nicholas30d30222020-09-11 12:27:26 -0400923 if (((leftType.typeKind() != Type::TypeKind::kScalar) &&
924 (leftType.typeKind() != Type::TypeKind::kVector)) ||
925 ((rightType.typeKind() != Type::TypeKind::kScalar) &&
926 (rightType.typeKind() != Type::TypeKind::kVector))) {
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400927 break;
928 }
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400929 switch (bin->getOperator()) {
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400930 case Token::Kind::TK_STAR:
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400931 if (is_constant(left, 1)) {
Ethan Nicholas30d30222020-09-11 12:27:26 -0400932 if (leftType.typeKind() == Type::TypeKind::kVector &&
933 rightType.typeKind() == Type::TypeKind::kScalar) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400934 // float4(1) * x -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400935 vectorize_right(&b, iter, outUpdated, outNeedsRescan);
936 } else {
937 // 1 * x -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400938 // 1 * float4(x) -> float4(x)
939 // float4(1) * float4(x) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400940 delete_left(&b, iter, outUpdated, outNeedsRescan);
941 }
942 }
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400943 else if (is_constant(left, 0)) {
Ethan Nicholas30d30222020-09-11 12:27:26 -0400944 if (leftType.typeKind() == Type::TypeKind::kScalar &&
945 rightType.typeKind() == Type::TypeKind::kVector &&
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400946 !right.hasSideEffects()) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400947 // 0 * float4(x) -> float4(0)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400948 vectorize_left(&b, iter, outUpdated, outNeedsRescan);
949 } else {
950 // 0 * x -> 0
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400951 // float4(0) * x -> float4(0)
952 // float4(0) * float4(x) -> float4(0)
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400953 if (!right.hasSideEffects()) {
Ethan Nicholas51493ee2017-12-11 12:34:33 -0500954 delete_right(&b, iter, outUpdated, outNeedsRescan);
955 }
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400956 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400957 }
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400958 else if (is_constant(right, 1)) {
Ethan Nicholas30d30222020-09-11 12:27:26 -0400959 if (leftType.typeKind() == Type::TypeKind::kScalar &&
960 rightType.typeKind() == Type::TypeKind::kVector) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400961 // x * float4(1) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400962 vectorize_left(&b, iter, outUpdated, outNeedsRescan);
963 } else {
964 // x * 1 -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400965 // float4(x) * 1 -> float4(x)
966 // float4(x) * float4(1) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400967 delete_right(&b, iter, outUpdated, outNeedsRescan);
968 }
969 }
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400970 else if (is_constant(right, 0)) {
Ethan Nicholas30d30222020-09-11 12:27:26 -0400971 if (leftType.typeKind() == Type::TypeKind::kVector &&
972 rightType.typeKind() == Type::TypeKind::kScalar &&
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400973 !left.hasSideEffects()) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400974 // float4(x) * 0 -> float4(0)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400975 vectorize_right(&b, iter, outUpdated, outNeedsRescan);
976 } else {
977 // x * 0 -> 0
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400978 // x * float4(0) -> float4(0)
979 // float4(x) * float4(0) -> float4(0)
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400980 if (!left.hasSideEffects()) {
Ethan Nicholas51493ee2017-12-11 12:34:33 -0500981 delete_left(&b, iter, outUpdated, outNeedsRescan);
982 }
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400983 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400984 }
985 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400986 case Token::Kind::TK_PLUS:
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400987 if (is_constant(left, 0)) {
Ethan Nicholas30d30222020-09-11 12:27:26 -0400988 if (leftType.typeKind() == Type::TypeKind::kVector &&
989 rightType.typeKind() == Type::TypeKind::kScalar) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400990 // float4(0) + x -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400991 vectorize_right(&b, iter, outUpdated, outNeedsRescan);
992 } else {
993 // 0 + x -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400994 // 0 + float4(x) -> float4(x)
995 // float4(0) + float4(x) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400996 delete_left(&b, iter, outUpdated, outNeedsRescan);
997 }
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400998 } else if (is_constant(right, 0)) {
Ethan Nicholas30d30222020-09-11 12:27:26 -0400999 if (leftType.typeKind() == Type::TypeKind::kScalar &&
1000 rightType.typeKind() == Type::TypeKind::kVector) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001001 // x + float4(0) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001002 vectorize_left(&b, iter, outUpdated, outNeedsRescan);
1003 } else {
1004 // x + 0 -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001005 // float4(x) + 0 -> float4(x)
1006 // float4(x) + float4(0) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001007 delete_right(&b, iter, outUpdated, outNeedsRescan);
1008 }
Ethan Nicholas56e42712017-04-21 10:23:37 -04001009 }
1010 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001011 case Token::Kind::TK_MINUS:
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -04001012 if (is_constant(right, 0)) {
Ethan Nicholas30d30222020-09-11 12:27:26 -04001013 if (leftType.typeKind() == Type::TypeKind::kScalar &&
1014 rightType.typeKind() == Type::TypeKind::kVector) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001015 // x - float4(0) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001016 vectorize_left(&b, iter, outUpdated, outNeedsRescan);
1017 } else {
1018 // x - 0 -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001019 // float4(x) - 0 -> float4(x)
1020 // float4(x) - float4(0) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001021 delete_right(&b, iter, outUpdated, outNeedsRescan);
1022 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001023 }
1024 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001025 case Token::Kind::TK_SLASH:
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -04001026 if (is_constant(right, 1)) {
Ethan Nicholas30d30222020-09-11 12:27:26 -04001027 if (leftType.typeKind() == Type::TypeKind::kScalar &&
1028 rightType.typeKind() == Type::TypeKind::kVector) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001029 // x / float4(1) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001030 vectorize_left(&b, iter, outUpdated, outNeedsRescan);
1031 } else {
1032 // x / 1 -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001033 // float4(x) / 1 -> float4(x)
1034 // float4(x) / float4(1) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001035 delete_right(&b, iter, outUpdated, outNeedsRescan);
1036 }
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -04001037 } else if (is_constant(left, 0)) {
Ethan Nicholas30d30222020-09-11 12:27:26 -04001038 if (leftType.typeKind() == Type::TypeKind::kScalar &&
1039 rightType.typeKind() == Type::TypeKind::kVector &&
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -04001040 !right.hasSideEffects()) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001041 // 0 / float4(x) -> float4(0)
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001042 vectorize_left(&b, iter, outUpdated, outNeedsRescan);
1043 } else {
1044 // 0 / x -> 0
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001045 // float4(0) / x -> float4(0)
1046 // float4(0) / float4(x) -> float4(0)
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -04001047 if (!right.hasSideEffects()) {
Ethan Nicholas51493ee2017-12-11 12:34:33 -05001048 delete_right(&b, iter, outUpdated, outNeedsRescan);
1049 }
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001050 }
1051 }
1052 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001053 case Token::Kind::TK_PLUSEQ:
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -04001054 if (is_constant(right, 0)) {
1055 clear_write(left);
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001056 delete_right(&b, iter, outUpdated, outNeedsRescan);
1057 }
1058 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001059 case Token::Kind::TK_MINUSEQ:
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -04001060 if (is_constant(right, 0)) {
1061 clear_write(left);
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001062 delete_right(&b, iter, outUpdated, outNeedsRescan);
1063 }
1064 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001065 case Token::Kind::TK_STAREQ:
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -04001066 if (is_constant(right, 1)) {
1067 clear_write(left);
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001068 delete_right(&b, iter, outUpdated, outNeedsRescan);
1069 }
1070 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001071 case Token::Kind::TK_SLASHEQ:
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -04001072 if (is_constant(right, 1)) {
1073 clear_write(left);
Ethan Nicholascb670962017-04-20 19:31:52 -04001074 delete_right(&b, iter, outUpdated, outNeedsRescan);
1075 }
1076 break;
1077 default:
1078 break;
1079 }
Ethan Nicholas409f6f02019-09-17 12:34:39 -04001080 break;
1081 }
Ethan Nicholase6592142020-09-08 10:22:09 -04001082 case Expression::Kind::kSwizzle: {
John Stiles403a3632020-08-20 12:11:48 -04001083 Swizzle& s = expr->as<Swizzle>();
Ethan Nicholas409f6f02019-09-17 12:34:39 -04001084 // detect identity swizzles like foo.rgba
Ethan Nicholas30d30222020-09-11 12:27:26 -04001085 if ((int) s.fComponents.size() == s.fBase->type().columns()) {
Ethan Nicholas409f6f02019-09-17 12:34:39 -04001086 bool identity = true;
1087 for (int i = 0; i < (int) s.fComponents.size(); ++i) {
1088 if (s.fComponents[i] != i) {
1089 identity = false;
1090 break;
1091 }
1092 }
1093 if (identity) {
1094 *outUpdated = true;
1095 if (!try_replace_expression(&b, iter, &s.fBase)) {
1096 *outNeedsRescan = true;
1097 return;
1098 }
1099 SkASSERT((*iter)->fKind == BasicBlock::Node::kExpression_Kind);
1100 break;
1101 }
1102 }
1103 // detect swizzles of swizzles, e.g. replace foo.argb.r000 with foo.a000
Ethan Nicholase6592142020-09-08 10:22:09 -04001104 if (s.fBase->kind() == Expression::Kind::kSwizzle) {
John Stilesa5a97b42020-08-18 11:19:07 -04001105 Swizzle& base = s.fBase->as<Swizzle>();
Ethan Nicholas409f6f02019-09-17 12:34:39 -04001106 std::vector<int> final;
1107 for (int c : s.fComponents) {
Brian Osman25647672020-09-15 15:16:56 -04001108 final.push_back(base.fComponents[c]);
Ethan Nicholas409f6f02019-09-17 12:34:39 -04001109 }
1110 *outUpdated = true;
1111 std::unique_ptr<Expression> replacement(new Swizzle(*fContext, base.fBase->clone(),
1112 std::move(final)));
1113 if (!try_replace_expression(&b, iter, &replacement)) {
1114 *outNeedsRescan = true;
1115 return;
1116 }
1117 SkASSERT((*iter)->fKind == BasicBlock::Node::kExpression_Kind);
Ethan Nicholas409f6f02019-09-17 12:34:39 -04001118 }
John Stiles30212b72020-06-11 17:55:07 -04001119 break;
Ethan Nicholascb670962017-04-20 19:31:52 -04001120 }
1121 default:
1122 break;
1123 }
1124}
1125
John Stiles92219b42020-06-15 12:32:24 -04001126// Returns true if this statement could potentially execute a break at the current level. We ignore
1127// nested loops and switches, since any breaks inside of them will merely break the loop / switch.
John Stilesb92641c2020-08-31 18:09:01 -04001128static bool contains_conditional_break(Statement& stmt) {
1129 class ContainsConditionalBreak : public ProgramVisitor {
1130 public:
1131 bool visitStatement(const Statement& stmt) override {
Ethan Nicholase6592142020-09-08 10:22:09 -04001132 switch (stmt.kind()) {
1133 case Statement::Kind::kBlock:
John Stilesb92641c2020-08-31 18:09:01 -04001134 return this->INHERITED::visitStatement(stmt);
1135
Ethan Nicholase6592142020-09-08 10:22:09 -04001136 case Statement::Kind::kBreak:
John Stilesb92641c2020-08-31 18:09:01 -04001137 return fInConditional > 0;
1138
Ethan Nicholase6592142020-09-08 10:22:09 -04001139 case Statement::Kind::kIf: {
John Stilesb92641c2020-08-31 18:09:01 -04001140 ++fInConditional;
1141 bool result = this->INHERITED::visitStatement(stmt);
1142 --fInConditional;
1143 return result;
1144 }
1145
1146 default:
1147 return false;
1148 }
1149 }
1150
1151 int fInConditional = 0;
1152 using INHERITED = ProgramVisitor;
1153 };
1154
1155 return ContainsConditionalBreak{}.visitStatement(stmt);
John Stiles92219b42020-06-15 12:32:24 -04001156}
1157
Ethan Nicholas5005a222018-08-24 13:06:27 -04001158// returns true if this statement definitely executes a break at the current level (we ignore
1159// nested loops and switches, since any breaks inside of them will merely break the loop / switch)
John Stilesb92641c2020-08-31 18:09:01 -04001160static bool contains_unconditional_break(Statement& stmt) {
1161 class ContainsUnconditionalBreak : public ProgramVisitor {
1162 public:
1163 bool visitStatement(const Statement& stmt) override {
Ethan Nicholase6592142020-09-08 10:22:09 -04001164 switch (stmt.kind()) {
1165 case Statement::Kind::kBlock:
John Stilesb92641c2020-08-31 18:09:01 -04001166 return this->INHERITED::visitStatement(stmt);
1167
Ethan Nicholase6592142020-09-08 10:22:09 -04001168 case Statement::Kind::kBreak:
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001169 return true;
John Stilesb92641c2020-08-31 18:09:01 -04001170
1171 default:
1172 return false;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001173 }
John Stilesb92641c2020-08-31 18:09:01 -04001174 }
John Stiles92219b42020-06-15 12:32:24 -04001175
John Stilesb92641c2020-08-31 18:09:01 -04001176 using INHERITED = ProgramVisitor;
1177 };
John Stiles92219b42020-06-15 12:32:24 -04001178
John Stilesb92641c2020-08-31 18:09:01 -04001179 return ContainsUnconditionalBreak{}.visitStatement(stmt);
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001180}
1181
John Stiles92219b42020-06-15 12:32:24 -04001182static void move_all_but_break(std::unique_ptr<Statement>& stmt,
1183 std::vector<std::unique_ptr<Statement>>* target) {
Ethan Nicholase6592142020-09-08 10:22:09 -04001184 switch (stmt->kind()) {
1185 case Statement::Kind::kBlock: {
John Stiles92219b42020-06-15 12:32:24 -04001186 // Recurse into the block.
1187 Block& block = static_cast<Block&>(*stmt);
1188
1189 std::vector<std::unique_ptr<Statement>> blockStmts;
Ethan Nicholas7bd60432020-09-25 14:31:59 -04001190 blockStmts.reserve(block.children().size());
1191 for (std::unique_ptr<Statement>& stmt : block.children()) {
1192 move_all_but_break(stmt, &blockStmts);
Ethan Nicholas739e1ca2020-06-11 12:16:14 -04001193 }
John Stiles92219b42020-06-15 12:32:24 -04001194
1195 target->push_back(std::make_unique<Block>(block.fOffset, std::move(blockStmts),
Ethan Nicholas7bd60432020-09-25 14:31:59 -04001196 block.symbolTable(), block.isScope()));
Ethan Nicholas739e1ca2020-06-11 12:16:14 -04001197 break;
John Stiles92219b42020-06-15 12:32:24 -04001198 }
1199
Ethan Nicholase6592142020-09-08 10:22:09 -04001200 case Statement::Kind::kBreak:
John Stiles92219b42020-06-15 12:32:24 -04001201 // Do not append a break to the target.
1202 break;
1203
Ethan Nicholas739e1ca2020-06-11 12:16:14 -04001204 default:
John Stiles92219b42020-06-15 12:32:24 -04001205 // Append normal statements to the target.
1206 target->push_back(std::move(stmt));
1207 break;
Ethan Nicholas739e1ca2020-06-11 12:16:14 -04001208 }
1209}
1210
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001211// Returns a block containing all of the statements that will be run if the given case matches
1212// (which, owing to the statements being owned by unique_ptrs, means the switch itself will be
1213// broken by this call and must then be discarded).
1214// Returns null (and leaves the switch unmodified) if no such simple reduction is possible, such as
1215// when break statements appear inside conditionals.
John Stiles92219b42020-06-15 12:32:24 -04001216static std::unique_ptr<Statement> block_for_case(SwitchStatement* switchStatement,
1217 SwitchCase* caseToCapture) {
1218 // We have to be careful to not move any of the pointers until after we're sure we're going to
1219 // succeed, so before we make any changes at all, we check the switch-cases to decide on a plan
1220 // of action. First, find the switch-case we are interested in.
1221 auto iter = switchStatement->fCases.begin();
1222 for (; iter != switchStatement->fCases.end(); ++iter) {
1223 if (iter->get() == caseToCapture) {
1224 break;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001225 }
John Stiles92219b42020-06-15 12:32:24 -04001226 }
1227
1228 // Next, walk forward through the rest of the switch. If we find a conditional break, we're
1229 // stuck and can't simplify at all. If we find an unconditional break, we have a range of
1230 // statements that we can use for simplification.
1231 auto startIter = iter;
1232 Statement* unconditionalBreakStmt = nullptr;
1233 for (; iter != switchStatement->fCases.end(); ++iter) {
1234 for (std::unique_ptr<Statement>& stmt : (*iter)->fStatements) {
1235 if (contains_conditional_break(*stmt)) {
1236 // We can't reduce switch-cases to a block when they have conditional breaks.
1237 return nullptr;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001238 }
John Stiles92219b42020-06-15 12:32:24 -04001239
1240 if (contains_unconditional_break(*stmt)) {
1241 // We found an unconditional break. We can use this block, but we need to strip
1242 // out the break statement.
1243 unconditionalBreakStmt = stmt.get();
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001244 break;
1245 }
1246 }
John Stiles92219b42020-06-15 12:32:24 -04001247
1248 if (unconditionalBreakStmt != nullptr) {
1249 break;
1250 }
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001251 }
John Stiles92219b42020-06-15 12:32:24 -04001252
1253 // We fell off the bottom of the switch or encountered a break. We know the range of statements
1254 // that we need to move over, and we know it's safe to do so.
1255 std::vector<std::unique_ptr<Statement>> caseStmts;
1256
1257 // We can move over most of the statements as-is.
1258 while (startIter != iter) {
1259 for (std::unique_ptr<Statement>& stmt : (*startIter)->fStatements) {
1260 caseStmts.push_back(std::move(stmt));
1261 }
1262 ++startIter;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001263 }
John Stiles92219b42020-06-15 12:32:24 -04001264
1265 // If we found an unconditional break at the end, we need to move what we can while avoiding
1266 // that break.
1267 if (unconditionalBreakStmt != nullptr) {
1268 for (std::unique_ptr<Statement>& stmt : (*startIter)->fStatements) {
1269 if (stmt.get() == unconditionalBreakStmt) {
1270 move_all_but_break(stmt, &caseStmts);
1271 unconditionalBreakStmt = nullptr;
1272 break;
1273 }
1274
1275 caseStmts.push_back(std::move(stmt));
1276 }
1277 }
1278
1279 SkASSERT(unconditionalBreakStmt == nullptr); // Verify that we fixed the unconditional break.
1280
1281 // Return our newly-synthesized block.
1282 return std::make_unique<Block>(/*offset=*/-1, std::move(caseStmts), switchStatement->fSymbols);
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001283}
1284
Ethan Nicholascb670962017-04-20 19:31:52 -04001285void Compiler::simplifyStatement(DefinitionMap& definitions,
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001286 BasicBlock& b,
1287 std::vector<BasicBlock::Node>::iterator* iter,
1288 std::unordered_set<const Variable*>* undefinedVariables,
1289 bool* outUpdated,
1290 bool* outNeedsRescan) {
Ethan Nicholascb670962017-04-20 19:31:52 -04001291 Statement* stmt = (*iter)->statement()->get();
Ethan Nicholase6592142020-09-08 10:22:09 -04001292 switch (stmt->kind()) {
1293 case Statement::Kind::kVarDeclaration: {
John Stilesa5a97b42020-08-18 11:19:07 -04001294 const auto& varDecl = stmt->as<VarDeclaration>();
Ethan Nicholas82a62d22017-11-07 14:42:10 +00001295 if (varDecl.fVar->dead() &&
1296 (!varDecl.fValue ||
1297 !varDecl.fValue->hasSideEffects())) {
1298 if (varDecl.fValue) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001299 SkASSERT((*iter)->statement()->get() == stmt);
Ethan Nicholas82a62d22017-11-07 14:42:10 +00001300 if (!b.tryRemoveExpressionBefore(iter, varDecl.fValue.get())) {
1301 *outNeedsRescan = true;
Ethan Nicholascb670962017-04-20 19:31:52 -04001302 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001303 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001304 (*iter)->setStatement(std::unique_ptr<Statement>(new Nop()));
Ethan Nicholas82a62d22017-11-07 14:42:10 +00001305 *outUpdated = true;
Ethan Nicholascb670962017-04-20 19:31:52 -04001306 }
1307 break;
1308 }
Ethan Nicholase6592142020-09-08 10:22:09 -04001309 case Statement::Kind::kIf: {
John Stilesa5a97b42020-08-18 11:19:07 -04001310 IfStatement& i = stmt->as<IfStatement>();
Ethan Nicholase6592142020-09-08 10:22:09 -04001311 if (i.fTest->kind() == Expression::Kind::kBoolLiteral) {
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001312 // constant if, collapse down to a single branch
Ethan Nicholas59d660c2020-09-28 09:18:15 -04001313 if (i.fTest->as<BoolLiteral>().value()) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001314 SkASSERT(i.fIfTrue);
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001315 (*iter)->setStatement(std::move(i.fIfTrue));
1316 } else {
1317 if (i.fIfFalse) {
1318 (*iter)->setStatement(std::move(i.fIfFalse));
1319 } else {
1320 (*iter)->setStatement(std::unique_ptr<Statement>(new Nop()));
1321 }
1322 }
1323 *outUpdated = true;
1324 *outNeedsRescan = true;
1325 break;
1326 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001327 if (i.fIfFalse && i.fIfFalse->isEmpty()) {
1328 // else block doesn't do anything, remove it
1329 i.fIfFalse.reset();
1330 *outUpdated = true;
1331 *outNeedsRescan = true;
1332 }
1333 if (!i.fIfFalse && i.fIfTrue->isEmpty()) {
1334 // if block doesn't do anything, no else block
1335 if (i.fTest->hasSideEffects()) {
1336 // test has side effects, keep it
1337 (*iter)->setStatement(std::unique_ptr<Statement>(
1338 new ExpressionStatement(std::move(i.fTest))));
1339 } else {
1340 // no if, no else, no test side effects, kill the whole if
1341 // statement
1342 (*iter)->setStatement(std::unique_ptr<Statement>(new Nop()));
1343 }
1344 *outUpdated = true;
1345 *outNeedsRescan = true;
1346 }
1347 break;
1348 }
Ethan Nicholase6592142020-09-08 10:22:09 -04001349 case Statement::Kind::kSwitch: {
John Stilesa5a97b42020-08-18 11:19:07 -04001350 SwitchStatement& s = stmt->as<SwitchStatement>();
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001351 int64_t switchValue;
1352 if (fIRGenerator->getConstantInt(*s.fValue, &switchValue)) {
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001353 // switch is constant, replace it with the case that matches
1354 bool found = false;
1355 SwitchCase* defaultCase = nullptr;
John Stiles9d944232020-08-19 09:56:49 -04001356 for (const std::unique_ptr<SwitchCase>& c : s.fCases) {
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001357 if (!c->fValue) {
1358 defaultCase = c.get();
1359 continue;
1360 }
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001361 int64_t caseValue;
1362 SkAssertResult(fIRGenerator->getConstantInt(*c->fValue, &caseValue));
1363 if (caseValue == switchValue) {
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001364 std::unique_ptr<Statement> newBlock = block_for_case(&s, c.get());
1365 if (newBlock) {
1366 (*iter)->setStatement(std::move(newBlock));
John Stiles9d944232020-08-19 09:56:49 -04001367 found = true;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001368 break;
1369 } else {
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -04001370 if (s.fIsStatic && !(fFlags & kPermitInvalidStaticTests_Flag)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001371 this->error(s.fOffset,
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001372 "static switch contains non-static conditional break");
1373 s.fIsStatic = false;
1374 }
1375 return; // can't simplify
1376 }
1377 }
1378 }
1379 if (!found) {
1380 // no matching case. use default if it exists, or kill the whole thing
1381 if (defaultCase) {
1382 std::unique_ptr<Statement> newBlock = block_for_case(&s, defaultCase);
1383 if (newBlock) {
1384 (*iter)->setStatement(std::move(newBlock));
1385 } else {
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -04001386 if (s.fIsStatic && !(fFlags & kPermitInvalidStaticTests_Flag)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001387 this->error(s.fOffset,
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001388 "static switch contains non-static conditional break");
1389 s.fIsStatic = false;
1390 }
1391 return; // can't simplify
1392 }
1393 } else {
1394 (*iter)->setStatement(std::unique_ptr<Statement>(new Nop()));
1395 }
1396 }
1397 *outUpdated = true;
1398 *outNeedsRescan = true;
1399 }
1400 break;
1401 }
Ethan Nicholase6592142020-09-08 10:22:09 -04001402 case Statement::Kind::kExpression: {
John Stilesa5a97b42020-08-18 11:19:07 -04001403 ExpressionStatement& e = stmt->as<ExpressionStatement>();
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001404 SkASSERT((*iter)->statement()->get() == &e);
Ethan Nicholascb670962017-04-20 19:31:52 -04001405 if (!e.fExpression->hasSideEffects()) {
1406 // Expression statement with no side effects, kill it
1407 if (!b.tryRemoveExpressionBefore(iter, e.fExpression.get())) {
1408 *outNeedsRescan = true;
1409 }
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001410 SkASSERT((*iter)->statement()->get() == stmt);
Ethan Nicholascb670962017-04-20 19:31:52 -04001411 (*iter)->setStatement(std::unique_ptr<Statement>(new Nop()));
1412 *outUpdated = true;
1413 }
1414 break;
1415 }
1416 default:
1417 break;
1418 }
1419}
1420
John Stiles0cc193a2020-09-09 09:39:34 -04001421bool Compiler::scanCFG(FunctionDefinition& f) {
1422 bool madeChanges = false;
1423
Ethan Nicholascb670962017-04-20 19:31:52 -04001424 CFG cfg = CFGGenerator().getCFG(f);
1425 this->computeDataFlow(&cfg);
ethannicholas22f939e2016-10-13 13:25:34 -07001426
1427 // check for unreachable code
1428 for (size_t i = 0; i < cfg.fBlocks.size(); i++) {
John Stiles0cc193a2020-09-09 09:39:34 -04001429 const BasicBlock& block = cfg.fBlocks[i];
1430 if (i != cfg.fStart && !block.fEntrances.size() && block.fNodes.size()) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001431 int offset;
John Stiles0cc193a2020-09-09 09:39:34 -04001432 const BasicBlock::Node& node = block.fNodes[0];
1433 switch (node.fKind) {
Ethan Nicholas86a43402017-01-19 13:32:00 -05001434 case BasicBlock::Node::kStatement_Kind:
John Stiles0cc193a2020-09-09 09:39:34 -04001435 offset = (*node.statement())->fOffset;
Ethan Nicholas86a43402017-01-19 13:32:00 -05001436 break;
1437 case BasicBlock::Node::kExpression_Kind:
John Stiles0cc193a2020-09-09 09:39:34 -04001438 offset = (*node.expression())->fOffset;
1439 if ((*node.expression())->is<BoolLiteral>()) {
Ethan Nicholas70728ef2020-05-28 07:09:00 -04001440 // Function inlining can generate do { ... } while(false) loops which always
1441 // break, so the boolean condition is considered unreachable. Since not
1442 // being able to reach a literal is a non-issue in the first place, we
1443 // don't report an error in this case.
1444 continue;
1445 }
Ethan Nicholas86a43402017-01-19 13:32:00 -05001446 break;
1447 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001448 this->error(offset, String("unreachable"));
ethannicholas22f939e2016-10-13 13:25:34 -07001449 }
1450 }
1451 if (fErrorCount) {
John Stiles0cc193a2020-09-09 09:39:34 -04001452 return madeChanges;
ethannicholas22f939e2016-10-13 13:25:34 -07001453 }
1454
Ethan Nicholascb670962017-04-20 19:31:52 -04001455 // check for dead code & undefined variables, perform constant propagation
1456 std::unordered_set<const Variable*> undefinedVariables;
1457 bool updated;
1458 bool needsRescan = false;
1459 do {
1460 if (needsRescan) {
1461 cfg = CFGGenerator().getCFG(f);
1462 this->computeDataFlow(&cfg);
1463 needsRescan = false;
Ethan Nicholas113628d2017-02-02 16:11:39 -05001464 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001465
1466 updated = false;
Ethan Nicholas1de14812020-06-19 15:32:49 -04001467 bool first = true;
Ethan Nicholascb670962017-04-20 19:31:52 -04001468 for (BasicBlock& b : cfg.fBlocks) {
Ethan Nicholas1de14812020-06-19 15:32:49 -04001469 if (!first && b.fEntrances.empty()) {
1470 // Block was reachable before optimization, but has since become unreachable. In
1471 // addition to being dead code, it's broken - since control flow can't reach it, no
1472 // prior variable definitions can reach it, and therefore variables might look to
1473 // have not been properly assigned. Kill it.
Brian Osmandb16c482020-09-09 15:15:06 -04001474
1475 // We need to do this in two steps. For any variable declarations, the node list
1476 // will contain statement nodes for each VarDeclaration, and then a statement for
1477 // the VarDeclarationsStatement. When we replace the VDS with a Nop, we delete the
1478 // storage of the unique_ptr that the VD nodes are pointing to. So we remove those
1479 // from the node list entirely, first.
1480 b.fNodes.erase(
1481 std::remove_if(b.fNodes.begin(), b.fNodes.end(),
1482 [](const BasicBlock::Node& node) {
1483 return node.fKind == BasicBlock::Node::kStatement_Kind &&
1484 (*node.statement())->is<VarDeclaration>();
1485 }),
1486 b.fNodes.end());
1487
1488 // Now replace any remaining statements in the block with Nops.
Ethan Nicholas1de14812020-06-19 15:32:49 -04001489 for (BasicBlock::Node& node : b.fNodes) {
1490 if (node.fKind == BasicBlock::Node::kStatement_Kind &&
John Stiles0cc193a2020-09-09 09:39:34 -04001491 !(*node.statement())->is<Nop>()) {
1492 node.setStatement(std::make_unique<Nop>());
1493 madeChanges = true;
Ethan Nicholas1de14812020-06-19 15:32:49 -04001494 }
1495 }
1496 continue;
1497 }
1498 first = false;
Ethan Nicholascb670962017-04-20 19:31:52 -04001499 DefinitionMap definitions = b.fBefore;
1500
1501 for (auto iter = b.fNodes.begin(); iter != b.fNodes.end() && !needsRescan; ++iter) {
1502 if (iter->fKind == BasicBlock::Node::kExpression_Kind) {
1503 this->simplifyExpression(definitions, b, &iter, &undefinedVariables, &updated,
1504 &needsRescan);
1505 } else {
1506 this->simplifyStatement(definitions, b, &iter, &undefinedVariables, &updated,
John Stiles0cc193a2020-09-09 09:39:34 -04001507 &needsRescan);
Ethan Nicholascb670962017-04-20 19:31:52 -04001508 }
Ethan Nicholas4b330df2017-05-17 10:52:55 -04001509 if (needsRescan) {
1510 break;
1511 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001512 this->addDefinitions(*iter, &definitions);
1513 }
Brian Osman01a3eb42020-09-14 11:32:49 -04001514
1515 if (needsRescan) {
1516 break;
1517 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001518 }
John Stiles0cc193a2020-09-09 09:39:34 -04001519 madeChanges |= updated;
Ethan Nicholascb670962017-04-20 19:31:52 -04001520 } while (updated);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001521 SkASSERT(!needsRescan);
ethannicholas22f939e2016-10-13 13:25:34 -07001522
Ethan Nicholas91a10532017-06-22 11:24:38 -04001523 // verify static ifs & switches, clean up dead variable decls
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001524 for (BasicBlock& b : cfg.fBlocks) {
1525 DefinitionMap definitions = b.fBefore;
1526
Ethan Nicholas91a10532017-06-22 11:24:38 -04001527 for (auto iter = b.fNodes.begin(); iter != b.fNodes.end() && !needsRescan;) {
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001528 if (iter->fKind == BasicBlock::Node::kStatement_Kind) {
1529 const Statement& s = **iter->statement();
Ethan Nicholase6592142020-09-08 10:22:09 -04001530 switch (s.kind()) {
1531 case Statement::Kind::kIf:
John Stilesa5a97b42020-08-18 11:19:07 -04001532 if (s.as<IfStatement>().fIsStatic &&
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -04001533 !(fFlags & kPermitInvalidStaticTests_Flag)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001534 this->error(s.fOffset, "static if has non-static test");
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001535 }
Ethan Nicholas91a10532017-06-22 11:24:38 -04001536 ++iter;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001537 break;
Ethan Nicholase6592142020-09-08 10:22:09 -04001538 case Statement::Kind::kSwitch:
John Stilesa5a97b42020-08-18 11:19:07 -04001539 if (s.as<SwitchStatement>().fIsStatic &&
John Stiles0cc193a2020-09-09 09:39:34 -04001540 !(fFlags & kPermitInvalidStaticTests_Flag)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001541 this->error(s.fOffset, "static switch has non-static test");
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001542 }
Ethan Nicholas91a10532017-06-22 11:24:38 -04001543 ++iter;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001544 break;
Ethan Nicholase6592142020-09-08 10:22:09 -04001545 case Statement::Kind::kVarDeclarations: {
John Stilesa5a97b42020-08-18 11:19:07 -04001546 VarDeclarations& decls = *s.as<VarDeclarationsStatement>().fDeclaration;
John Stiles0cc193a2020-09-09 09:39:34 -04001547 decls.fVars.erase(
1548 std::remove_if(decls.fVars.begin(), decls.fVars.end(),
1549 [&](const std::unique_ptr<Statement>& var) {
1550 bool nop = var->is<Nop>();
1551 madeChanges |= nop;
1552 return nop;
1553 }),
1554 decls.fVars.end());
1555 if (decls.fVars.empty()) {
Ethan Nicholas82a62d22017-11-07 14:42:10 +00001556 iter = b.fNodes.erase(iter);
1557 } else {
1558 ++iter;
1559 }
1560 break;
1561 }
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001562 default:
Ethan Nicholas91a10532017-06-22 11:24:38 -04001563 ++iter;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001564 break;
1565 }
Ethan Nicholas91a10532017-06-22 11:24:38 -04001566 } else {
1567 ++iter;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001568 }
1569 }
1570 }
1571
ethannicholas22f939e2016-10-13 13:25:34 -07001572 // check for missing return
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001573 if (f.fDeclaration.fReturnType != *fContext->fVoid_Type) {
ethannicholas22f939e2016-10-13 13:25:34 -07001574 if (cfg.fBlocks[cfg.fExit].fEntrances.size()) {
Ethan Nicholasdb80f692019-11-22 14:06:12 -05001575 this->error(f.fOffset, String("function '" + String(f.fDeclaration.fName) +
1576 "' can exit without returning a value"));
ethannicholas22f939e2016-10-13 13:25:34 -07001577 }
1578 }
John Stiles0cc193a2020-09-09 09:39:34 -04001579
1580 return madeChanges;
ethannicholas22f939e2016-10-13 13:25:34 -07001581}
1582
Brian Osman32d53552020-09-23 13:55:20 -04001583std::unique_ptr<Program> Compiler::convertProgram(
1584 Program::Kind kind,
1585 String text,
1586 const Program::Settings& settings,
1587 const std::vector<std::unique_ptr<ExternalValue>>* externalValues) {
1588 SkASSERT(!externalValues || (kind == Program::kGeneric_Kind));
Ethan Nicholas91164d12019-05-15 15:29:54 -04001589
ethannicholasb3058bd2016-07-01 08:22:01 -07001590 fErrorText = "";
1591 fErrorCount = 0;
John Stiles7b463002020-08-31 17:29:21 -04001592 fInliner.reset(context(), settings);
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001593 std::vector<std::unique_ptr<ProgramElement>>* inherited;
ethannicholasd598f792016-07-25 10:08:54 -07001594 std::vector<std::unique_ptr<ProgramElement>> elements;
ethannicholasb3058bd2016-07-01 08:22:01 -07001595 switch (kind) {
1596 case Program::kVertex_Kind:
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001597 inherited = &fVertexInclude;
John Stiles810c8cf2020-08-26 19:46:27 -04001598 fIRGenerator->fIntrinsics = fGPUIntrinsics.get();
Brian Osmane498b3c2020-09-23 14:42:11 -04001599 fIRGenerator->start(&settings, fVertexSymbolTable, inherited);
ethannicholasb3058bd2016-07-01 08:22:01 -07001600 break;
1601 case Program::kFragment_Kind:
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001602 inherited = &fFragmentInclude;
John Stiles810c8cf2020-08-26 19:46:27 -04001603 fIRGenerator->fIntrinsics = fGPUIntrinsics.get();
Brian Osmane498b3c2020-09-23 14:42:11 -04001604 fIRGenerator->start(&settings, fFragmentSymbolTable, inherited);
ethannicholasb3058bd2016-07-01 08:22:01 -07001605 break;
Ethan Nicholas52cad152017-02-16 16:37:32 -05001606 case Program::kGeometry_Kind:
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001607 this->loadGeometryIntrinsics();
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001608 inherited = &fGeometryInclude;
John Stiles810c8cf2020-08-26 19:46:27 -04001609 fIRGenerator->fIntrinsics = fGPUIntrinsics.get();
Brian Osmane498b3c2020-09-23 14:42:11 -04001610 fIRGenerator->start(&settings, fGeometrySymbolTable, inherited);
Ethan Nicholas52cad152017-02-16 16:37:32 -05001611 break;
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -04001612 case Program::kFragmentProcessor_Kind: {
Brian Osmandd496172020-08-08 08:17:18 -04001613#if !SKSL_STANDALONE
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001614 {
1615 Rehydrator rehydrator(fContext.get(), fGpuSymbolTable, this,
1616 SKSL_INCLUDE_sksl_fp,
1617 SKSL_INCLUDE_sksl_fp_LENGTH);
1618 fFPSymbolTable = rehydrator.symbolTable();
1619 fFPInclude = rehydrator.elements();
1620 }
Brian Osman2b469eb2020-09-21 11:32:10 -04001621 fFPIntrinsics = std::make_unique<IRIntrinsicMap>(fGPUIntrinsics.get());
1622 grab_intrinsics(&fFPInclude, fFPIntrinsics.get());
1623
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001624 inherited = &fFPInclude;
Brian Osman2b469eb2020-09-21 11:32:10 -04001625 fIRGenerator->fIntrinsics = fFPIntrinsics.get();
Brian Osmane498b3c2020-09-23 14:42:11 -04001626 fIRGenerator->start(&settings, fFPSymbolTable, inherited);
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001627 break;
1628#else
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001629 inherited = nullptr;
Brian Osmane498b3c2020-09-23 14:42:11 -04001630 fIRGenerator->start(&settings, fGpuSymbolTable, /*inherited=*/nullptr,
1631 /*builtin=*/true);
John Stiles810c8cf2020-08-26 19:46:27 -04001632 fIRGenerator->fIntrinsics = fGPUIntrinsics.get();
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -04001633 std::ifstream in(SKSL_FP_INCLUDE);
1634 std::string stdText{std::istreambuf_iterator<char>(in),
1635 std::istreambuf_iterator<char>()};
1636 if (in.rdstate()) {
1637 printf("error reading %s\n", SKSL_FP_INCLUDE);
1638 abort();
1639 }
Brian Osmane498b3c2020-09-23 14:42:11 -04001640 const String* source = fRootSymbolTable->takeOwnershipOfString(
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -04001641 std::make_unique<String>(stdText.c_str()));
1642 fIRGenerator->convertProgram(kind, source->c_str(), source->length(), &elements);
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001643 fIRGenerator->fIsBuiltinCode = false;
Ethan Nicholas762466e2017-06-29 10:03:38 -04001644 break;
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001645#endif
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -04001646 }
Ethan Nicholas8da1e652019-05-24 11:01:59 -04001647 case Program::kPipelineStage_Kind:
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001648 this->loadPipelineIntrinsics();
Ethan Nicholas8da1e652019-05-24 11:01:59 -04001649 inherited = &fPipelineInclude;
John Stiles810c8cf2020-08-26 19:46:27 -04001650 fIRGenerator->fIntrinsics = fGPUIntrinsics.get();
Brian Osmane498b3c2020-09-23 14:42:11 -04001651 fIRGenerator->start(&settings, fPipelineSymbolTable, inherited);
Ethan Nicholas8da1e652019-05-24 11:01:59 -04001652 break;
Ethan Nicholas746035a2019-04-23 13:31:09 -04001653 case Program::kGeneric_Kind:
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001654 this->loadInterpreterIntrinsics();
Brian Osmaneac49832020-09-18 11:49:22 -04001655 inherited = nullptr;
John Stiles810c8cf2020-08-26 19:46:27 -04001656 fIRGenerator->fIntrinsics = fInterpreterIntrinsics.get();
Brian Osmane498b3c2020-09-23 14:42:11 -04001657 fIRGenerator->start(&settings, fInterpreterSymbolTable, /*inherited=*/nullptr);
Ethan Nicholas0d997662019-04-08 09:46:01 -04001658 break;
ethannicholasb3058bd2016-07-01 08:22:01 -07001659 }
Brian Osman32d53552020-09-23 13:55:20 -04001660 if (externalValues) {
1661 // Add any external values to the symbol table. IRGenerator::start() has pushed a table, so
1662 // we're only making these visible to the current Program.
1663 for (const auto& ev : *externalValues) {
1664 fIRGenerator->fSymbolTable->addWithoutOwnership(ev->fName, ev.get());
1665 }
1666 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001667 std::unique_ptr<String> textPtr(new String(std::move(text)));
1668 fSource = textPtr.get();
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001669 fIRGenerator->convertProgram(kind, textPtr->c_str(), textPtr->size(), &elements);
John Stilesfbd050b2020-08-03 13:21:46 -04001670 auto result = std::make_unique<Program>(kind,
1671 std::move(textPtr),
1672 settings,
1673 fContext,
1674 inherited,
1675 std::move(elements),
1676 fIRGenerator->fSymbolTable,
1677 fIRGenerator->fInputs);
Brian Osmane498b3c2020-09-23 14:42:11 -04001678 fIRGenerator->finish();
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001679 if (fErrorCount) {
1680 return nullptr;
1681 }
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001682 if (settings.fOptimize && !this->optimize(*result)) {
1683 return nullptr;
1684 }
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001685 return result;
1686}
1687
Ethan Nicholas00543112018-07-31 09:44:36 -04001688bool Compiler::optimize(Program& program) {
1689 SkASSERT(!fErrorCount);
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001690 fIRGenerator->fKind = program.fKind;
1691 fIRGenerator->fSettings = &program.fSettings;
John Stiles7954d6c2020-09-01 10:53:02 -04001692
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001693 while (fErrorCount == 0) {
1694 bool madeChanges = false;
John Stiles7954d6c2020-09-01 10:53:02 -04001695
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001696 // Scan and optimize based on the control-flow graph for each function.
1697 for (ProgramElement& element : program) {
1698 if (element.is<FunctionDefinition>()) {
1699 madeChanges |= this->scanCFG(element.as<FunctionDefinition>());
1700 }
1701 }
1702
1703 // Perform inline-candidate analysis and inline any functions deemed suitable.
John Stiles881a10c2020-09-19 10:13:24 -04001704 madeChanges |= fInliner.analyze(program);
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001705
1706 // Remove dead functions. We wait until after analysis so that we still report errors,
1707 // even in unused code.
1708 if (program.fSettings.fRemoveDeadFunctions) {
1709 program.fElements.erase(
1710 std::remove_if(program.fElements.begin(),
1711 program.fElements.end(),
1712 [&](const std::unique_ptr<ProgramElement>& element) {
1713 if (!element->is<FunctionDefinition>()) {
1714 return false;
1715 }
1716 const auto& fn = element->as<FunctionDefinition>();
1717 bool dead = fn.fDeclaration.fCallCount == 0 &&
1718 fn.fDeclaration.fName != "main";
1719 madeChanges |= dead;
1720 return dead;
1721 }),
1722 program.fElements.end());
1723 }
1724
1725 if (program.fKind != Program::kFragmentProcessor_Kind) {
1726 // Remove dead variables.
John Stileseadfc3b2020-09-02 14:12:41 -04001727 for (ProgramElement& element : program) {
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001728 if (!element.is<VarDeclarations>()) {
1729 continue;
Ethan Nicholas0dc80872019-02-08 15:46:24 -05001730 }
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001731 VarDeclarations& vars = element.as<VarDeclarations>();
1732 vars.fVars.erase(
1733 std::remove_if(vars.fVars.begin(), vars.fVars.end(),
1734 [&](const std::unique_ptr<Statement>& stmt) {
1735 bool dead = stmt->as<VarDeclaration>().fVar->dead();
John Stiles73a6bff2020-09-09 13:40:37 -04001736 madeChanges |= dead;
1737 return dead;
1738 }),
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001739 vars.fVars.end());
John Stiles73a6bff2020-09-09 13:40:37 -04001740 }
1741
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001742 // Remove empty variable declarations with no variables left inside of them.
1743 program.fElements.erase(
1744 std::remove_if(program.fElements.begin(), program.fElements.end(),
1745 [&](const std::unique_ptr<ProgramElement>& element) {
1746 if (!element->is<VarDeclarations>()) {
1747 return false;
1748 }
1749 bool dead = element->as<VarDeclarations>().fVars.empty();
1750 madeChanges |= dead;
1751 return dead;
1752 }),
1753 program.fElements.end());
1754 }
John Stiles73a6bff2020-09-09 13:40:37 -04001755
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001756 if (!madeChanges) {
1757 break;
Ethan Nicholas0dc80872019-02-08 15:46:24 -05001758 }
Ethan Nicholas00543112018-07-31 09:44:36 -04001759 }
1760 return fErrorCount == 0;
1761}
1762
Brian Osmanfb32ddf2019-06-18 10:14:20 -04001763#if defined(SKSL_STANDALONE) || SK_SUPPORT_GPU
1764
Ethan Nicholas00543112018-07-31 09:44:36 -04001765bool Compiler::toSPIRV(Program& program, OutputStream& out) {
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001766#ifdef SK_ENABLE_SPIRV_VALIDATION
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001767 StringStream buffer;
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001768 fSource = program.fSource.get();
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001769 SPIRVCodeGenerator cg(fContext.get(), &program, this, &buffer);
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001770 bool result = cg.generateCode();
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001771 fSource = nullptr;
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001772 if (result) {
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001773 spvtools::SpirvTools tools(SPV_ENV_VULKAN_1_0);
Ethan Nicholas762466e2017-06-29 10:03:38 -04001774 const String& data = buffer.str();
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001775 SkASSERT(0 == data.size() % 4);
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001776 auto dumpmsg = [](spv_message_level_t, const char*, const spv_position_t&, const char* m) {
1777 SkDebugf("SPIR-V validation error: %s\n", m);
1778 };
1779 tools.SetMessageConsumer(dumpmsg);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001780 // Verify that the SPIR-V we produced is valid. If this SkASSERT fails, check the logs prior
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001781 // to the failure to see the validation errors.
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001782 SkAssertResult(tools.Validate((const uint32_t*) data.c_str(), data.size() / 4));
Ethan Nicholas762466e2017-06-29 10:03:38 -04001783 out.write(data.c_str(), data.size());
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001784 }
1785#else
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001786 fSource = program.fSource.get();
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001787 SPIRVCodeGenerator cg(fContext.get(), &program, this, &out);
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001788 bool result = cg.generateCode();
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001789 fSource = nullptr;
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001790#endif
Ethan Nicholasce33f102016-12-09 17:22:59 -05001791 return result;
1792}
1793
Ethan Nicholas00543112018-07-31 09:44:36 -04001794bool Compiler::toSPIRV(Program& program, String* out) {
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001795 StringStream buffer;
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001796 bool result = this->toSPIRV(program, buffer);
1797 if (result) {
Ethan Nicholas762466e2017-06-29 10:03:38 -04001798 *out = buffer.str();
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001799 }
1800 return result;
1801}
1802
Ethan Nicholas00543112018-07-31 09:44:36 -04001803bool Compiler::toGLSL(Program& program, OutputStream& out) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001804 fSource = program.fSource.get();
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001805 GLSLCodeGenerator cg(fContext.get(), &program, this, &out);
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001806 bool result = cg.generateCode();
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001807 fSource = nullptr;
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001808 return result;
1809}
1810
Ethan Nicholas00543112018-07-31 09:44:36 -04001811bool Compiler::toGLSL(Program& program, String* out) {
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001812 StringStream buffer;
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001813 bool result = this->toGLSL(program, buffer);
1814 if (result) {
Ethan Nicholas762466e2017-06-29 10:03:38 -04001815 *out = buffer.str();
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001816 }
1817 return result;
1818}
1819
Brian Osmanc0243912020-02-19 15:35:26 -05001820bool Compiler::toHLSL(Program& program, String* out) {
1821 String spirv;
1822 if (!this->toSPIRV(program, &spirv)) {
1823 return false;
1824 }
1825
1826 return SPIRVtoHLSL(spirv, out);
1827}
1828
Ethan Nicholas00543112018-07-31 09:44:36 -04001829bool Compiler::toMetal(Program& program, OutputStream& out) {
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001830 MetalCodeGenerator cg(fContext.get(), &program, this, &out);
Ethan Nicholascc305772017-10-13 16:17:45 -04001831 bool result = cg.generateCode();
Ethan Nicholascc305772017-10-13 16:17:45 -04001832 return result;
1833}
1834
Ethan Nicholas00543112018-07-31 09:44:36 -04001835bool Compiler::toMetal(Program& program, String* out) {
Timothy Liangb8eeb802018-07-23 16:46:16 -04001836 StringStream buffer;
1837 bool result = this->toMetal(program, buffer);
1838 if (result) {
1839 *out = buffer.str();
1840 }
1841 return result;
1842}
1843
Greg Daniela28ea672020-09-25 11:12:56 -04001844#if defined(SKSL_STANDALONE) || GR_TEST_UTILS
Ethan Nicholas00543112018-07-31 09:44:36 -04001845bool Compiler::toCPP(Program& program, String name, OutputStream& out) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001846 fSource = program.fSource.get();
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001847 CPPCodeGenerator cg(fContext.get(), &program, this, name, &out);
Ethan Nicholas762466e2017-06-29 10:03:38 -04001848 bool result = cg.generateCode();
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001849 fSource = nullptr;
Ethan Nicholas762466e2017-06-29 10:03:38 -04001850 return result;
1851}
1852
Ethan Nicholas00543112018-07-31 09:44:36 -04001853bool Compiler::toH(Program& program, String name, OutputStream& out) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001854 fSource = program.fSource.get();
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001855 HCodeGenerator cg(fContext.get(), &program, this, name, &out);
Ethan Nicholas762466e2017-06-29 10:03:38 -04001856 bool result = cg.generateCode();
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001857 fSource = nullptr;
Ethan Nicholas00543112018-07-31 09:44:36 -04001858 return result;
1859}
Greg Daniela28ea672020-09-25 11:12:56 -04001860#endif // defined(SKSL_STANDALONE) || GR_TEST_UTILS
Ethan Nicholas00543112018-07-31 09:44:36 -04001861
Ethan Nicholas2a479a52020-08-18 16:29:45 -04001862#endif // defined(SKSL_STANDALONE) || SK_SUPPORT_GPU
Brian Osman2e29ab52019-09-20 12:19:11 -04001863
1864#if !defined(SKSL_STANDALONE) && SK_SUPPORT_GPU
Brian Osmana4b91692020-08-10 14:26:16 -04001865bool Compiler::toPipelineStage(Program& program, PipelineStageArgs* outArgs) {
Ethan Nicholas00543112018-07-31 09:44:36 -04001866 fSource = program.fSource.get();
1867 StringStream buffer;
Brian Osman300fe1d2020-01-23 15:42:43 -05001868 PipelineStageCodeGenerator cg(fContext.get(), &program, this, &buffer, outArgs);
Ethan Nicholas00543112018-07-31 09:44:36 -04001869 bool result = cg.generateCode();
1870 fSource = nullptr;
1871 if (result) {
Brian Osman107c6662019-12-30 15:02:30 -05001872 outArgs->fCode = buffer.str();
Ethan Nicholas00543112018-07-31 09:44:36 -04001873 }
Ethan Nicholas762466e2017-06-29 10:03:38 -04001874 return result;
1875}
Brian Osmanfb32ddf2019-06-18 10:14:20 -04001876#endif
1877
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001878std::unique_ptr<ByteCode> Compiler::toByteCode(Program& program) {
Mike Klein853d4ed2020-08-20 00:41:00 +00001879#if defined(SK_ENABLE_SKSL_INTERPRETER)
Brian Osman808f0212020-01-21 15:36:47 -05001880 fSource = program.fSource.get();
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001881 std::unique_ptr<ByteCode> result(new ByteCode());
Brian Osmanb08cc022020-04-02 11:38:40 -04001882 ByteCodeGenerator cg(fContext.get(), &program, this, result.get());
1883 bool success = cg.generateCode();
1884 fSource = nullptr;
1885 if (success) {
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001886 return result;
1887 }
Mike Klein853d4ed2020-08-20 00:41:00 +00001888#else
1889 ABORT("ByteCode interpreter not enabled");
1890#endif
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001891 return nullptr;
1892}
1893
Brian Osman401a0092020-09-10 14:47:24 -04001894const char* Compiler::OperatorName(Token::Kind op) {
1895 switch (op) {
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001896 case Token::Kind::TK_PLUS: return "+";
1897 case Token::Kind::TK_MINUS: return "-";
1898 case Token::Kind::TK_STAR: return "*";
1899 case Token::Kind::TK_SLASH: return "/";
1900 case Token::Kind::TK_PERCENT: return "%";
1901 case Token::Kind::TK_SHL: return "<<";
1902 case Token::Kind::TK_SHR: return ">>";
1903 case Token::Kind::TK_LOGICALNOT: return "!";
1904 case Token::Kind::TK_LOGICALAND: return "&&";
1905 case Token::Kind::TK_LOGICALOR: return "||";
1906 case Token::Kind::TK_LOGICALXOR: return "^^";
1907 case Token::Kind::TK_BITWISENOT: return "~";
1908 case Token::Kind::TK_BITWISEAND: return "&";
1909 case Token::Kind::TK_BITWISEOR: return "|";
1910 case Token::Kind::TK_BITWISEXOR: return "^";
1911 case Token::Kind::TK_EQ: return "=";
1912 case Token::Kind::TK_EQEQ: return "==";
1913 case Token::Kind::TK_NEQ: return "!=";
1914 case Token::Kind::TK_LT: return "<";
1915 case Token::Kind::TK_GT: return ">";
1916 case Token::Kind::TK_LTEQ: return "<=";
1917 case Token::Kind::TK_GTEQ: return ">=";
1918 case Token::Kind::TK_PLUSEQ: return "+=";
1919 case Token::Kind::TK_MINUSEQ: return "-=";
1920 case Token::Kind::TK_STAREQ: return "*=";
1921 case Token::Kind::TK_SLASHEQ: return "/=";
1922 case Token::Kind::TK_PERCENTEQ: return "%=";
1923 case Token::Kind::TK_SHLEQ: return "<<=";
1924 case Token::Kind::TK_SHREQ: return ">>=";
1925 case Token::Kind::TK_LOGICALANDEQ: return "&&=";
1926 case Token::Kind::TK_LOGICALOREQ: return "||=";
1927 case Token::Kind::TK_LOGICALXOREQ: return "^^=";
1928 case Token::Kind::TK_BITWISEANDEQ: return "&=";
1929 case Token::Kind::TK_BITWISEOREQ: return "|=";
1930 case Token::Kind::TK_BITWISEXOREQ: return "^=";
1931 case Token::Kind::TK_PLUSPLUS: return "++";
1932 case Token::Kind::TK_MINUSMINUS: return "--";
1933 case Token::Kind::TK_COMMA: return ",";
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001934 default:
Brian Osman401a0092020-09-10 14:47:24 -04001935 ABORT("unsupported operator: %d\n", (int) op);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001936 }
1937}
1938
1939
1940bool Compiler::IsAssignment(Token::Kind op) {
1941 switch (op) {
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001942 case Token::Kind::TK_EQ: // fall through
1943 case Token::Kind::TK_PLUSEQ: // fall through
1944 case Token::Kind::TK_MINUSEQ: // fall through
1945 case Token::Kind::TK_STAREQ: // fall through
1946 case Token::Kind::TK_SLASHEQ: // fall through
1947 case Token::Kind::TK_PERCENTEQ: // fall through
1948 case Token::Kind::TK_SHLEQ: // fall through
1949 case Token::Kind::TK_SHREQ: // fall through
1950 case Token::Kind::TK_BITWISEOREQ: // fall through
1951 case Token::Kind::TK_BITWISEXOREQ: // fall through
1952 case Token::Kind::TK_BITWISEANDEQ: // fall through
1953 case Token::Kind::TK_LOGICALOREQ: // fall through
1954 case Token::Kind::TK_LOGICALXOREQ: // fall through
1955 case Token::Kind::TK_LOGICALANDEQ:
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001956 return true;
1957 default:
1958 return false;
1959 }
1960}
1961
Brian Osman401a0092020-09-10 14:47:24 -04001962Token::Kind Compiler::RemoveAssignment(Token::Kind op) {
1963 switch (op) {
1964 case Token::Kind::TK_PLUSEQ: return Token::Kind::TK_PLUS;
1965 case Token::Kind::TK_MINUSEQ: return Token::Kind::TK_MINUS;
1966 case Token::Kind::TK_STAREQ: return Token::Kind::TK_STAR;
1967 case Token::Kind::TK_SLASHEQ: return Token::Kind::TK_SLASH;
1968 case Token::Kind::TK_PERCENTEQ: return Token::Kind::TK_PERCENT;
1969 case Token::Kind::TK_SHLEQ: return Token::Kind::TK_SHL;
1970 case Token::Kind::TK_SHREQ: return Token::Kind::TK_SHR;
1971 case Token::Kind::TK_BITWISEOREQ: return Token::Kind::TK_BITWISEOR;
1972 case Token::Kind::TK_BITWISEXOREQ: return Token::Kind::TK_BITWISEXOR;
1973 case Token::Kind::TK_BITWISEANDEQ: return Token::Kind::TK_BITWISEAND;
1974 case Token::Kind::TK_LOGICALOREQ: return Token::Kind::TK_LOGICALOR;
1975 case Token::Kind::TK_LOGICALXOREQ: return Token::Kind::TK_LOGICALXOR;
1976 case Token::Kind::TK_LOGICALANDEQ: return Token::Kind::TK_LOGICALAND;
1977 default: return op;
1978 }
1979}
1980
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001981Position Compiler::position(int offset) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001982 SkASSERT(fSource);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001983 int line = 1;
1984 int column = 1;
1985 for (int i = 0; i < offset; i++) {
1986 if ((*fSource)[i] == '\n') {
1987 ++line;
1988 column = 1;
1989 }
1990 else {
1991 ++column;
1992 }
1993 }
1994 return Position(line, column);
1995}
1996
1997void Compiler::error(int offset, String msg) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001998 fErrorCount++;
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001999 Position pos = this->position(offset);
2000 fErrorText += "error: " + to_string(pos.fLine) + ": " + msg.c_str() + "\n";
ethannicholasb3058bd2016-07-01 08:22:01 -07002001}
2002
Ethan Nicholas0df1b042017-03-31 13:56:23 -04002003String Compiler::errorText() {
Ethan Nicholas00543112018-07-31 09:44:36 -04002004 this->writeErrorCount();
2005 fErrorCount = 0;
Ethan Nicholas0df1b042017-03-31 13:56:23 -04002006 String result = fErrorText;
ethannicholasb3058bd2016-07-01 08:22:01 -07002007 return result;
2008}
2009
2010void Compiler::writeErrorCount() {
2011 if (fErrorCount) {
2012 fErrorText += to_string(fErrorCount) + " error";
2013 if (fErrorCount > 1) {
2014 fErrorText += "s";
2015 }
2016 fErrorText += "\n";
2017 }
2018}
2019
John Stilesa6841be2020-08-06 14:11:56 -04002020} // namespace SkSL