blob: ffe6811b07f2aed81d924ce28b9711db6f1284c0 [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
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -040099Compiler::Compiler(Flags flags)
Brian Osman2b469eb2020-09-21 11:32:10 -0400100: fGPUIntrinsics(std::make_unique<IRIntrinsicMap>(/*parent=*/nullptr))
101, fInterpreterIntrinsics(std::make_unique<IRIntrinsicMap>(/*parent=*/nullptr))
John Stiles810c8cf2020-08-26 19:46:27 -0400102, fFlags(flags)
103, fContext(std::make_shared<Context>())
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -0400104, fErrorCount(0) {
Brian Osmaneac49832020-09-18 11:49:22 -0400105 fRootSymbolTable = std::make_shared<SymbolTable>(this);
John Stiles941fc712020-09-19 12:47:10 +0000106 fIRGenerator =
107 std::make_unique<IRGenerator>(fContext.get(), &fInliner, fRootSymbolTable, *this);
Brian Osmaneac49832020-09-18 11:49:22 -0400108 #define ADD_TYPE(t) fRootSymbolTable->addWithoutOwnership(fContext->f ## t ## _Type->fName, \
109 fContext->f ## t ## _Type.get())
ethannicholasb3058bd2016-07-01 08:22:01 -0700110 ADD_TYPE(Void);
111 ADD_TYPE(Float);
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400112 ADD_TYPE(Float2);
113 ADD_TYPE(Float3);
114 ADD_TYPE(Float4);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400115 ADD_TYPE(Half);
116 ADD_TYPE(Half2);
117 ADD_TYPE(Half3);
118 ADD_TYPE(Half4);
ethannicholasb3058bd2016-07-01 08:22:01 -0700119 ADD_TYPE(Int);
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400120 ADD_TYPE(Int2);
121 ADD_TYPE(Int3);
122 ADD_TYPE(Int4);
ethannicholasb3058bd2016-07-01 08:22:01 -0700123 ADD_TYPE(UInt);
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400124 ADD_TYPE(UInt2);
125 ADD_TYPE(UInt3);
126 ADD_TYPE(UInt4);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400127 ADD_TYPE(Short);
128 ADD_TYPE(Short2);
129 ADD_TYPE(Short3);
130 ADD_TYPE(Short4);
131 ADD_TYPE(UShort);
132 ADD_TYPE(UShort2);
133 ADD_TYPE(UShort3);
134 ADD_TYPE(UShort4);
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400135 ADD_TYPE(Byte);
136 ADD_TYPE(Byte2);
137 ADD_TYPE(Byte3);
138 ADD_TYPE(Byte4);
139 ADD_TYPE(UByte);
140 ADD_TYPE(UByte2);
141 ADD_TYPE(UByte3);
142 ADD_TYPE(UByte4);
ethannicholasb3058bd2016-07-01 08:22:01 -0700143 ADD_TYPE(Bool);
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400144 ADD_TYPE(Bool2);
145 ADD_TYPE(Bool3);
146 ADD_TYPE(Bool4);
147 ADD_TYPE(Float2x2);
148 ADD_TYPE(Float2x3);
149 ADD_TYPE(Float2x4);
150 ADD_TYPE(Float3x2);
151 ADD_TYPE(Float3x3);
152 ADD_TYPE(Float3x4);
153 ADD_TYPE(Float4x2);
154 ADD_TYPE(Float4x3);
155 ADD_TYPE(Float4x4);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400156 ADD_TYPE(Half2x2);
157 ADD_TYPE(Half2x3);
158 ADD_TYPE(Half2x4);
159 ADD_TYPE(Half3x2);
160 ADD_TYPE(Half3x3);
161 ADD_TYPE(Half3x4);
162 ADD_TYPE(Half4x2);
163 ADD_TYPE(Half4x3);
164 ADD_TYPE(Half4x4);
ethannicholasb3058bd2016-07-01 08:22:01 -0700165 ADD_TYPE(GenType);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400166 ADD_TYPE(GenHType);
ethannicholasb3058bd2016-07-01 08:22:01 -0700167 ADD_TYPE(GenIType);
168 ADD_TYPE(GenUType);
169 ADD_TYPE(GenBType);
170 ADD_TYPE(Mat);
171 ADD_TYPE(Vec);
172 ADD_TYPE(GVec);
173 ADD_TYPE(GVec2);
174 ADD_TYPE(GVec3);
175 ADD_TYPE(GVec4);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400176 ADD_TYPE(HVec);
ethannicholasb3058bd2016-07-01 08:22:01 -0700177 ADD_TYPE(IVec);
178 ADD_TYPE(UVec);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400179 ADD_TYPE(SVec);
180 ADD_TYPE(USVec);
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400181 ADD_TYPE(ByteVec);
182 ADD_TYPE(UByteVec);
ethannicholasb3058bd2016-07-01 08:22:01 -0700183 ADD_TYPE(BVec);
184
185 ADD_TYPE(Sampler1D);
186 ADD_TYPE(Sampler2D);
187 ADD_TYPE(Sampler3D);
ethannicholas5961bc92016-10-12 06:39:56 -0700188 ADD_TYPE(SamplerExternalOES);
ethannicholasb3058bd2016-07-01 08:22:01 -0700189 ADD_TYPE(SamplerCube);
190 ADD_TYPE(Sampler2DRect);
191 ADD_TYPE(Sampler1DArray);
192 ADD_TYPE(Sampler2DArray);
193 ADD_TYPE(SamplerCubeArray);
194 ADD_TYPE(SamplerBuffer);
195 ADD_TYPE(Sampler2DMS);
196 ADD_TYPE(Sampler2DMSArray);
197
Brian Salomonbf7b6202016-11-11 16:08:03 -0500198 ADD_TYPE(ISampler2D);
199
Brian Salomon2a51de82016-11-16 12:06:01 -0500200 ADD_TYPE(Image2D);
201 ADD_TYPE(IImage2D);
202
Greg Daniel64773e62016-11-22 09:44:03 -0500203 ADD_TYPE(SubpassInput);
204 ADD_TYPE(SubpassInputMS);
205
ethannicholasb3058bd2016-07-01 08:22:01 -0700206 ADD_TYPE(GSampler1D);
207 ADD_TYPE(GSampler2D);
208 ADD_TYPE(GSampler3D);
209 ADD_TYPE(GSamplerCube);
210 ADD_TYPE(GSampler2DRect);
211 ADD_TYPE(GSampler1DArray);
212 ADD_TYPE(GSampler2DArray);
213 ADD_TYPE(GSamplerCubeArray);
214 ADD_TYPE(GSamplerBuffer);
215 ADD_TYPE(GSampler2DMS);
216 ADD_TYPE(GSampler2DMSArray);
217
218 ADD_TYPE(Sampler1DShadow);
219 ADD_TYPE(Sampler2DShadow);
220 ADD_TYPE(SamplerCubeShadow);
221 ADD_TYPE(Sampler2DRectShadow);
222 ADD_TYPE(Sampler1DArrayShadow);
223 ADD_TYPE(Sampler2DArrayShadow);
224 ADD_TYPE(SamplerCubeArrayShadow);
225 ADD_TYPE(GSampler2DArrayShadow);
226 ADD_TYPE(GSamplerCubeArrayShadow);
Ethan Nicholasc9472af2017-10-10 16:30:21 -0400227 ADD_TYPE(FragmentProcessor);
Stephen Whiteff5d7a22019-07-26 17:42:06 -0400228 ADD_TYPE(Sampler);
229 ADD_TYPE(Texture2D);
ethannicholasb3058bd2016-07-01 08:22:01 -0700230
Brian Osman28590d52020-03-23 16:59:08 -0400231 StringFragment fpAliasName("shader");
Brian Osmaneac49832020-09-18 11:49:22 -0400232 fRootSymbolTable->addWithoutOwnership(fpAliasName, fContext->fFragmentProcessor_Type.get());
Brian Osman28590d52020-03-23 16:59:08 -0400233
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700234 StringFragment skCapsName("sk_Caps");
Brian Osmaneac49832020-09-18 11:49:22 -0400235 fRootSymbolTable->add(
John Stiles311dd9d2020-08-13 17:09:29 -0400236 skCapsName,
237 std::make_unique<Variable>(/*offset=*/-1, Modifiers(), skCapsName,
Ethan Nicholas30d30222020-09-11 12:27:26 -0400238 fContext->fSkCaps_Type.get(), Variable::kGlobal_Storage));
Ethan Nicholas3605ace2016-11-21 15:59:48 -0500239
John Stiles810c8cf2020-08-26 19:46:27 -0400240 fIRGenerator->fIntrinsics = fGPUIntrinsics.get();
Ethan Nicholasdb80f692019-11-22 14:06:12 -0500241 std::vector<std::unique_ptr<ProgramElement>> gpuIntrinsics;
Brian Osmandd496172020-08-08 08:17:18 -0400242#if SKSL_STANDALONE
Brian Osmaneac49832020-09-18 11:49:22 -0400243 this->processIncludeFile(Program::kFragment_Kind, SKSL_GPU_INCLUDE, fRootSymbolTable,
244 &gpuIntrinsics, &fGpuSymbolTable);
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -0400245 this->processIncludeFile(Program::kVertex_Kind, SKSL_VERT_INCLUDE, fGpuSymbolTable,
246 &fVertexInclude, &fVertexSymbolTable);
247 this->processIncludeFile(Program::kFragment_Kind, SKSL_FRAG_INCLUDE, fGpuSymbolTable,
248 &fFragmentInclude, &fFragmentSymbolTable);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400249#else
250 {
Brian Osmaneac49832020-09-18 11:49:22 -0400251 Rehydrator rehydrator(fContext.get(), fRootSymbolTable, this, SKSL_INCLUDE_sksl_gpu,
252 SKSL_INCLUDE_sksl_gpu_LENGTH);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400253 fGpuSymbolTable = rehydrator.symbolTable();
254 gpuIntrinsics = rehydrator.elements();
255 }
256 {
257 Rehydrator rehydrator(fContext.get(), fGpuSymbolTable, this, SKSL_INCLUDE_sksl_vert,
Brian Osmaneac49832020-09-18 11:49:22 -0400258 SKSL_INCLUDE_sksl_vert_LENGTH);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400259 fVertexSymbolTable = rehydrator.symbolTable();
260 fVertexInclude = rehydrator.elements();
261 }
262 {
263 Rehydrator rehydrator(fContext.get(), fGpuSymbolTable, this, SKSL_INCLUDE_sksl_frag,
Brian Osmaneac49832020-09-18 11:49:22 -0400264 SKSL_INCLUDE_sksl_frag_LENGTH);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400265 fFragmentSymbolTable = rehydrator.symbolTable();
266 fFragmentInclude = rehydrator.elements();
267 }
268#endif
John Stiles810c8cf2020-08-26 19:46:27 -0400269 grab_intrinsics(&gpuIntrinsics, fGPUIntrinsics.get());
Brian Osman2b469eb2020-09-21 11:32:10 -0400270 SkASSERT(gpuIntrinsics.empty());
ethannicholasb3058bd2016-07-01 08:22:01 -0700271}
272
John Stiles656427a2020-08-27 15:26:26 -0400273Compiler::~Compiler() {}
ethannicholasb3058bd2016-07-01 08:22:01 -0700274
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400275void Compiler::loadGeometryIntrinsics() {
276 if (fGeometrySymbolTable) {
277 return;
278 }
Brian Osmandd496172020-08-08 08:17:18 -0400279 #if !SKSL_STANDALONE
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400280 {
281 Rehydrator rehydrator(fContext.get(), fGpuSymbolTable, this, SKSL_INCLUDE_sksl_geom,
282 SKSL_INCLUDE_sksl_geom_LENGTH);
283 fGeometrySymbolTable = rehydrator.symbolTable();
284 fGeometryInclude = rehydrator.elements();
285 }
286 #else
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -0400287 this->processIncludeFile(Program::kGeometry_Kind, SKSL_GEOM_INCLUDE, fGpuSymbolTable,
288 &fGeometryInclude, &fGeometrySymbolTable);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400289 #endif
290}
291
292void Compiler::loadPipelineIntrinsics() {
293 if (fPipelineSymbolTable) {
294 return;
295 }
Brian Osmandd496172020-08-08 08:17:18 -0400296 #if !SKSL_STANDALONE
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400297 {
298 Rehydrator rehydrator(fContext.get(), fGpuSymbolTable, this,
299 SKSL_INCLUDE_sksl_pipeline,
300 SKSL_INCLUDE_sksl_pipeline_LENGTH);
301 fPipelineSymbolTable = rehydrator.symbolTable();
302 fPipelineInclude = rehydrator.elements();
303 }
304 #else
305 this->processIncludeFile(Program::kPipelineStage_Kind, SKSL_PIPELINE_INCLUDE,
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -0400306 fGpuSymbolTable, &fPipelineInclude, &fPipelineSymbolTable);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400307 #endif
308}
309
310void Compiler::loadInterpreterIntrinsics() {
311 if (fInterpreterSymbolTable) {
312 return;
313 }
Brian Osmaneac49832020-09-18 11:49:22 -0400314 std::vector<std::unique_ptr<ProgramElement>> interpIntrinsics;
Brian Osmandd496172020-08-08 08:17:18 -0400315 #if !SKSL_STANDALONE
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400316 {
Brian Osmaneac49832020-09-18 11:49:22 -0400317 Rehydrator rehydrator(fContext.get(), fRootSymbolTable, this,
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400318 SKSL_INCLUDE_sksl_interp,
319 SKSL_INCLUDE_sksl_interp_LENGTH);
320 fInterpreterSymbolTable = rehydrator.symbolTable();
Brian Osmaneac49832020-09-18 11:49:22 -0400321 interpIntrinsics = rehydrator.elements();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400322 }
323 #else
324 this->processIncludeFile(Program::kGeneric_Kind, SKSL_INTERP_INCLUDE,
Brian Osmaneac49832020-09-18 11:49:22 -0400325 fIRGenerator->fSymbolTable, &interpIntrinsics,
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -0400326 &fInterpreterSymbolTable);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400327 #endif
Brian Osmaneac49832020-09-18 11:49:22 -0400328 grab_intrinsics(&interpIntrinsics, fInterpreterIntrinsics.get());
Brian Osman2b469eb2020-09-21 11:32:10 -0400329 SkASSERT(interpIntrinsics.empty());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400330}
331
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -0400332void Compiler::processIncludeFile(Program::Kind kind, const char* path,
Ethan Nicholas8da1e652019-05-24 11:01:59 -0400333 std::shared_ptr<SymbolTable> base,
334 std::vector<std::unique_ptr<ProgramElement>>* outElements,
335 std::shared_ptr<SymbolTable>* outSymbolTable) {
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -0400336 std::ifstream in(path);
Brian Osmane498b3c2020-09-23 14:42:11 -0400337 std::unique_ptr<String> text = std::make_unique<String>(std::istreambuf_iterator<char>(in),
338 std::istreambuf_iterator<char>());
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -0400339 if (in.rdstate()) {
340 printf("error reading %s\n", path);
341 abort();
342 }
Brian Osmane498b3c2020-09-23 14:42:11 -0400343 const String* source = fRootSymbolTable->takeOwnershipOfString(std::move(text));
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -0400344 fSource = source;
Ethan Nicholas8da1e652019-05-24 11:01:59 -0400345 Program::Settings settings;
Ethan Nicholasa11035b2019-11-26 16:27:47 -0500346#if !defined(SKSL_STANDALONE) & SK_SUPPORT_GPU
347 GrContextOptions opts;
348 GrShaderCaps caps(opts);
349 settings.fCaps = &caps;
350#endif
John Stiles881a10c2020-09-19 10:13:24 -0400351 SkASSERT(fIRGenerator->fCanInline);
352 fIRGenerator->fCanInline = false;
Brian Osmane498b3c2020-09-23 14:42:11 -0400353 fIRGenerator->start(&settings, base ? base : fRootSymbolTable, nullptr, true);
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -0400354 fIRGenerator->convertProgram(kind, source->c_str(), source->length(), outElements);
John Stiles881a10c2020-09-19 10:13:24 -0400355 fIRGenerator->fCanInline = true;
Ethan Nicholas8da1e652019-05-24 11:01:59 -0400356 if (this->fErrorCount) {
357 printf("Unexpected errors: %s\n", this->fErrorText.c_str());
358 }
359 SkASSERT(!fErrorCount);
Ethan Nicholas8da1e652019-05-24 11:01:59 -0400360 *outSymbolTable = fIRGenerator->fSymbolTable;
Ethan Nicholasa11035b2019-11-26 16:27:47 -0500361#ifdef SK_DEBUG
362 fSource = nullptr;
363#endif
Brian Osmane498b3c2020-09-23 14:42:11 -0400364 fIRGenerator->finish();
Ethan Nicholas8da1e652019-05-24 11:01:59 -0400365}
366
ethannicholas22f939e2016-10-13 13:25:34 -0700367// add the definition created by assigning to the lvalue to the definition set
Ethan Nicholas86a43402017-01-19 13:32:00 -0500368void Compiler::addDefinition(const Expression* lvalue, std::unique_ptr<Expression>* expr,
369 DefinitionMap* definitions) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400370 switch (lvalue->kind()) {
371 case Expression::Kind::kVariableReference: {
John Stilesa5a97b42020-08-18 11:19:07 -0400372 const Variable& var = lvalue->as<VariableReference>().fVariable;
ethannicholas22f939e2016-10-13 13:25:34 -0700373 if (var.fStorage == Variable::kLocal_Storage) {
374 (*definitions)[&var] = expr;
375 }
376 break;
377 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400378 case Expression::Kind::kSwizzle:
ethannicholas22f939e2016-10-13 13:25:34 -0700379 // We consider the variable written to as long as at least some of its components have
380 // been written to. This will lead to some false negatives (we won't catch it if you
381 // write to foo.x and then read foo.y), but being stricter could lead to false positives
Mike Klein6ad99092016-10-26 10:35:22 -0400382 // (we write to foo.x, and then pass foo to a function which happens to only read foo.x,
383 // 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 -0700384 // more complicated whole-program analysis. This is probably good enough.
John Stilesa5a97b42020-08-18 11:19:07 -0400385 this->addDefinition(lvalue->as<Swizzle>().fBase.get(),
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400386 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
ethannicholas22f939e2016-10-13 13:25:34 -0700387 definitions);
388 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400389 case Expression::Kind::kIndex:
ethannicholas22f939e2016-10-13 13:25:34 -0700390 // see comments in Swizzle
John Stilesa5a97b42020-08-18 11:19:07 -0400391 this->addDefinition(lvalue->as<IndexExpression>().fBase.get(),
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400392 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
ethannicholas22f939e2016-10-13 13:25:34 -0700393 definitions);
394 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400395 case Expression::Kind::kFieldAccess:
ethannicholas22f939e2016-10-13 13:25:34 -0700396 // see comments in Swizzle
John Stilesa5a97b42020-08-18 11:19:07 -0400397 this->addDefinition(lvalue->as<FieldAccess>().fBase.get(),
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400398 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
ethannicholas22f939e2016-10-13 13:25:34 -0700399 definitions);
400 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400401 case Expression::Kind::kTernary:
Ethan Nicholasa583b812018-01-18 13:32:11 -0500402 // To simplify analysis, we just pretend that we write to both sides of the ternary.
403 // This allows for false positives (meaning we fail to detect that a variable might not
404 // have been assigned), but is preferable to false negatives.
John Stilesa5a97b42020-08-18 11:19:07 -0400405 this->addDefinition(lvalue->as<TernaryExpression>().fIfTrue.get(),
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400406 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
Ethan Nicholasa583b812018-01-18 13:32:11 -0500407 definitions);
John Stilesa5a97b42020-08-18 11:19:07 -0400408 this->addDefinition(lvalue->as<TernaryExpression>().fIfFalse.get(),
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400409 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
Ethan Nicholasa583b812018-01-18 13:32:11 -0500410 definitions);
411 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400412 case Expression::Kind::kExternalValue:
Ethan Nicholas91164d12019-05-15 15:29:54 -0400413 break;
ethannicholas22f939e2016-10-13 13:25:34 -0700414 default:
415 // not an lvalue, can't happen
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400416 SkASSERT(false);
ethannicholas22f939e2016-10-13 13:25:34 -0700417 }
418}
419
420// add local variables defined by this node to the set
Mike Klein6ad99092016-10-26 10:35:22 -0400421void Compiler::addDefinitions(const BasicBlock::Node& node,
Ethan Nicholas86a43402017-01-19 13:32:00 -0500422 DefinitionMap* definitions) {
ethannicholas22f939e2016-10-13 13:25:34 -0700423 switch (node.fKind) {
424 case BasicBlock::Node::kExpression_Kind: {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400425 SkASSERT(node.expression());
John Stilesa5a97b42020-08-18 11:19:07 -0400426 Expression* expr = node.expression()->get();
Ethan Nicholase6592142020-09-08 10:22:09 -0400427 switch (expr->kind()) {
428 case Expression::Kind::kBinary: {
John Stilesa5a97b42020-08-18 11:19:07 -0400429 BinaryExpression* b = &expr->as<BinaryExpression>();
Ethan Nicholasbf66ffb2020-09-16 22:05:10 +0000430 if (b->fOperator == Token::Kind::TK_EQ) {
431 this->addDefinition(b->fLeft.get(), &b->fRight, definitions);
432 } else if (Compiler::IsAssignment(b->fOperator)) {
Ethan Nicholas86a43402017-01-19 13:32:00 -0500433 this->addDefinition(
Ethan Nicholasbf66ffb2020-09-16 22:05:10 +0000434 b->fLeft.get(),
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400435 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
436 definitions);
Ethan Nicholas86a43402017-01-19 13:32:00 -0500437
438 }
439 break;
ethannicholas22f939e2016-10-13 13:25:34 -0700440 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400441 case Expression::Kind::kFunctionCall: {
John Stilesa5a97b42020-08-18 11:19:07 -0400442 const FunctionCall& c = expr->as<FunctionCall>();
Ethan Nicholasc6a19f12018-03-29 16:46:56 -0400443 for (size_t i = 0; i < c.fFunction.fParameters.size(); ++i) {
444 if (c.fFunction.fParameters[i]->fModifiers.fFlags & Modifiers::kOut_Flag) {
445 this->addDefinition(
446 c.fArguments[i].get(),
447 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
448 definitions);
449 }
450 }
451 break;
452 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400453 case Expression::Kind::kPrefix: {
John Stilesa5a97b42020-08-18 11:19:07 -0400454 const PrefixExpression* p = &expr->as<PrefixExpression>();
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400455 if (p->fOperator == Token::Kind::TK_MINUSMINUS ||
456 p->fOperator == Token::Kind::TK_PLUSPLUS) {
Ethan Nicholas86a43402017-01-19 13:32:00 -0500457 this->addDefinition(
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400458 p->fOperand.get(),
459 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
460 definitions);
Ethan Nicholas86a43402017-01-19 13:32:00 -0500461 }
462 break;
463 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400464 case Expression::Kind::kPostfix: {
John Stilesa5a97b42020-08-18 11:19:07 -0400465 const PostfixExpression* p = &expr->as<PostfixExpression>();
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400466 if (p->fOperator == Token::Kind::TK_MINUSMINUS ||
467 p->fOperator == Token::Kind::TK_PLUSPLUS) {
Ethan Nicholas86a43402017-01-19 13:32:00 -0500468 this->addDefinition(
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400469 p->fOperand.get(),
470 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
471 definitions);
Ethan Nicholas86a43402017-01-19 13:32:00 -0500472 }
473 break;
474 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400475 case Expression::Kind::kVariableReference: {
John Stilesa5a97b42020-08-18 11:19:07 -0400476 const VariableReference* v = &expr->as<VariableReference>();
Ethan Nicholascb670962017-04-20 19:31:52 -0400477 if (v->fRefKind != VariableReference::kRead_RefKind) {
478 this->addDefinition(
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400479 v,
480 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
481 definitions);
Ethan Nicholascb670962017-04-20 19:31:52 -0400482 }
John Stiles30212b72020-06-11 17:55:07 -0400483 break;
Ethan Nicholascb670962017-04-20 19:31:52 -0400484 }
Ethan Nicholas86a43402017-01-19 13:32:00 -0500485 default:
486 break;
ethannicholas22f939e2016-10-13 13:25:34 -0700487 }
488 break;
489 }
490 case BasicBlock::Node::kStatement_Kind: {
John Stilesa5a97b42020-08-18 11:19:07 -0400491 Statement* stmt = node.statement()->get();
Ethan Nicholase6592142020-09-08 10:22:09 -0400492 if (stmt->kind() == Statement::Kind::kVarDeclaration) {
John Stilesa5a97b42020-08-18 11:19:07 -0400493 VarDeclaration& vd = stmt->as<VarDeclaration>();
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000494 if (vd.fValue) {
495 (*definitions)[vd.fVar] = &vd.fValue;
ethannicholas22f939e2016-10-13 13:25:34 -0700496 }
497 }
498 break;
499 }
500 }
501}
502
503void Compiler::scanCFG(CFG* cfg, BlockId blockId, std::set<BlockId>* workList) {
504 BasicBlock& block = cfg->fBlocks[blockId];
505
506 // compute definitions after this block
Ethan Nicholas86a43402017-01-19 13:32:00 -0500507 DefinitionMap after = block.fBefore;
ethannicholas22f939e2016-10-13 13:25:34 -0700508 for (const BasicBlock::Node& n : block.fNodes) {
509 this->addDefinitions(n, &after);
510 }
511
512 // propagate definitions to exits
513 for (BlockId exitId : block.fExits) {
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400514 if (exitId == blockId) {
515 continue;
516 }
ethannicholas22f939e2016-10-13 13:25:34 -0700517 BasicBlock& exit = cfg->fBlocks[exitId];
518 for (const auto& pair : after) {
Ethan Nicholas86a43402017-01-19 13:32:00 -0500519 std::unique_ptr<Expression>* e1 = pair.second;
520 auto found = exit.fBefore.find(pair.first);
521 if (found == exit.fBefore.end()) {
522 // exit has no definition for it, just copy it
523 workList->insert(exitId);
ethannicholas22f939e2016-10-13 13:25:34 -0700524 exit.fBefore[pair.first] = e1;
525 } else {
Ethan Nicholas86a43402017-01-19 13:32:00 -0500526 // exit has a (possibly different) value already defined
527 std::unique_ptr<Expression>* e2 = exit.fBefore[pair.first];
ethannicholas22f939e2016-10-13 13:25:34 -0700528 if (e1 != e2) {
529 // definition has changed, merge and add exit block to worklist
530 workList->insert(exitId);
Ethan Nicholasaf197692017-02-27 13:26:45 -0500531 if (e1 && e2) {
532 exit.fBefore[pair.first] =
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400533 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression;
Ethan Nicholasaf197692017-02-27 13:26:45 -0500534 } else {
535 exit.fBefore[pair.first] = nullptr;
536 }
ethannicholas22f939e2016-10-13 13:25:34 -0700537 }
538 }
539 }
540 }
541}
542
543// returns a map which maps all local variables in the function to null, indicating that their value
544// is initially unknown
Ethan Nicholas86a43402017-01-19 13:32:00 -0500545static DefinitionMap compute_start_state(const CFG& cfg) {
546 DefinitionMap result;
Mike Klein6ad99092016-10-26 10:35:22 -0400547 for (const auto& block : cfg.fBlocks) {
548 for (const auto& node : block.fNodes) {
ethannicholas22f939e2016-10-13 13:25:34 -0700549 if (node.fKind == BasicBlock::Node::kStatement_Kind) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400550 SkASSERT(node.statement());
Ethan Nicholascb670962017-04-20 19:31:52 -0400551 const Statement* s = node.statement()->get();
Ethan Nicholase6592142020-09-08 10:22:09 -0400552 if (s->kind() == Statement::Kind::kVarDeclarations) {
John Stilesa5a97b42020-08-18 11:19:07 -0400553 const VarDeclarationsStatement* vd = &s->as<VarDeclarationsStatement>();
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000554 for (const auto& decl : vd->fDeclaration->fVars) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400555 if (decl->kind() == Statement::Kind::kVarDeclaration) {
John Stilesa5a97b42020-08-18 11:19:07 -0400556 result[decl->as<VarDeclaration>().fVar] = nullptr;
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000557 }
Mike Klein6ad99092016-10-26 10:35:22 -0400558 }
ethannicholas22f939e2016-10-13 13:25:34 -0700559 }
560 }
561 }
562 }
563 return result;
564}
565
Ethan Nicholascb670962017-04-20 19:31:52 -0400566/**
567 * Returns true if assigning to this lvalue has no effect.
568 */
569static bool is_dead(const Expression& lvalue) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400570 switch (lvalue.kind()) {
571 case Expression::Kind::kVariableReference:
John Stilesa5a97b42020-08-18 11:19:07 -0400572 return lvalue.as<VariableReference>().fVariable.dead();
Ethan Nicholase6592142020-09-08 10:22:09 -0400573 case Expression::Kind::kSwizzle:
John Stilesa5a97b42020-08-18 11:19:07 -0400574 return is_dead(*lvalue.as<Swizzle>().fBase);
Ethan Nicholase6592142020-09-08 10:22:09 -0400575 case Expression::Kind::kFieldAccess:
John Stilesa5a97b42020-08-18 11:19:07 -0400576 return is_dead(*lvalue.as<FieldAccess>().fBase);
Ethan Nicholase6592142020-09-08 10:22:09 -0400577 case Expression::Kind::kIndex: {
John Stilesa5a97b42020-08-18 11:19:07 -0400578 const IndexExpression& idx = lvalue.as<IndexExpression>();
Ethan Nicholas2a099da2020-01-02 14:40:54 -0500579 return is_dead(*idx.fBase) &&
580 !idx.fIndex->hasProperty(Expression::Property::kSideEffects);
Ethan Nicholascb670962017-04-20 19:31:52 -0400581 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400582 case Expression::Kind::kTernary: {
John Stilesa5a97b42020-08-18 11:19:07 -0400583 const TernaryExpression& t = lvalue.as<TernaryExpression>();
Ethan Nicholasa583b812018-01-18 13:32:11 -0500584 return !t.fTest->hasSideEffects() && is_dead(*t.fIfTrue) && is_dead(*t.fIfFalse);
585 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400586 case Expression::Kind::kExternalValue:
Ethan Nicholas91164d12019-05-15 15:29:54 -0400587 return false;
Ethan Nicholascb670962017-04-20 19:31:52 -0400588 default:
Ethan Nicholas2a099da2020-01-02 14:40:54 -0500589#ifdef SK_DEBUG
Ethan Nicholascb670962017-04-20 19:31:52 -0400590 ABORT("invalid lvalue: %s\n", lvalue.description().c_str());
Ethan Nicholas2a099da2020-01-02 14:40:54 -0500591#endif
592 return false;
Ethan Nicholascb670962017-04-20 19:31:52 -0400593 }
594}
ethannicholas22f939e2016-10-13 13:25:34 -0700595
Ethan Nicholascb670962017-04-20 19:31:52 -0400596/**
597 * Returns true if this is an assignment which can be collapsed down to just the right hand side due
598 * to a dead target and lack of side effects on the left hand side.
599 */
600static bool dead_assignment(const BinaryExpression& b) {
Ethan Nicholasbf66ffb2020-09-16 22:05:10 +0000601 if (!Compiler::IsAssignment(b.fOperator)) {
Ethan Nicholascb670962017-04-20 19:31:52 -0400602 return false;
603 }
Ethan Nicholasbf66ffb2020-09-16 22:05:10 +0000604 return is_dead(*b.fLeft);
Ethan Nicholascb670962017-04-20 19:31:52 -0400605}
606
607void Compiler::computeDataFlow(CFG* cfg) {
608 cfg->fBlocks[cfg->fStart].fBefore = compute_start_state(*cfg);
ethannicholas22f939e2016-10-13 13:25:34 -0700609 std::set<BlockId> workList;
Ethan Nicholascb670962017-04-20 19:31:52 -0400610 for (BlockId i = 0; i < cfg->fBlocks.size(); i++) {
ethannicholas22f939e2016-10-13 13:25:34 -0700611 workList.insert(i);
612 }
613 while (workList.size()) {
614 BlockId next = *workList.begin();
615 workList.erase(workList.begin());
Ethan Nicholascb670962017-04-20 19:31:52 -0400616 this->scanCFG(cfg, next, &workList);
ethannicholas22f939e2016-10-13 13:25:34 -0700617 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400618}
619
620/**
621 * Attempts to replace the expression pointed to by iter with a new one (in both the CFG and the
622 * IR). If the expression can be cleanly removed, returns true and updates the iterator to point to
623 * the newly-inserted element. Otherwise updates only the IR and returns false (and the CFG will
624 * need to be regenerated).
625 */
John Stilesafbf8992020-08-18 10:08:21 -0400626static bool try_replace_expression(BasicBlock* b,
627 std::vector<BasicBlock::Node>::iterator* iter,
628 std::unique_ptr<Expression>* newExpression) {
Ethan Nicholascb670962017-04-20 19:31:52 -0400629 std::unique_ptr<Expression>* target = (*iter)->expression();
630 if (!b->tryRemoveExpression(iter)) {
631 *target = std::move(*newExpression);
632 return false;
633 }
634 *target = std::move(*newExpression);
635 return b->tryInsertExpression(iter, target);
636}
637
638/**
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400639 * Returns true if the expression is a constant numeric literal with the specified value, or a
640 * constant vector with all elements equal to the specified value.
Ethan Nicholascb670962017-04-20 19:31:52 -0400641 */
John Stiles9d944232020-08-19 09:56:49 -0400642template <typename T = double>
643static bool is_constant(const Expression& expr, T value) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400644 switch (expr.kind()) {
645 case Expression::Kind::kIntLiteral:
John Stiles81365af2020-08-18 09:24:00 -0400646 return expr.as<IntLiteral>().fValue == value;
John Stiles9d944232020-08-19 09:56:49 -0400647
Ethan Nicholase6592142020-09-08 10:22:09 -0400648 case Expression::Kind::kFloatLiteral:
John Stiles81365af2020-08-18 09:24:00 -0400649 return expr.as<FloatLiteral>().fValue == value;
John Stiles9d944232020-08-19 09:56:49 -0400650
Ethan Nicholase6592142020-09-08 10:22:09 -0400651 case Expression::Kind::kConstructor: {
John Stiles9d944232020-08-19 09:56:49 -0400652 const Constructor& constructor = expr.as<Constructor>();
653 if (constructor.isCompileTimeConstant()) {
Ethan Nicholas30d30222020-09-11 12:27:26 -0400654 const Type& constructorType = constructor.type();
655 bool isFloat = constructorType.columns() > 1
656 ? constructorType.componentType().isFloat()
657 : constructorType.isFloat();
658 switch (constructorType.typeKind()) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400659 case Type::TypeKind::kVector:
Ethan Nicholas30d30222020-09-11 12:27:26 -0400660 for (int i = 0; i < constructorType.columns(); ++i) {
John Stiles9d944232020-08-19 09:56:49 -0400661 if (isFloat) {
662 if (constructor.getFVecComponent(i) != value) {
663 return false;
664 }
665 } else {
666 if (constructor.getIVecComponent(i) != value) {
667 return false;
668 }
669 }
Ethan Nicholasd188c182019-06-10 15:55:38 -0400670 }
John Stiles9d944232020-08-19 09:56:49 -0400671 return true;
672
Ethan Nicholase6592142020-09-08 10:22:09 -0400673 case Type::TypeKind::kScalar:
John Stiles9d944232020-08-19 09:56:49 -0400674 SkASSERT(constructor.fArguments.size() == 1);
675 return is_constant<T>(*constructor.fArguments[0], value);
676
677 default:
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400678 return false;
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400679 }
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400680 }
681 return false;
682 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400683 default:
684 return false;
685 }
686}
687
688/**
689 * Collapses the binary expression pointed to by iter down to just the right side (in both the IR
690 * and CFG structures).
691 */
John Stilesafbf8992020-08-18 10:08:21 -0400692static void delete_left(BasicBlock* b,
693 std::vector<BasicBlock::Node>::iterator* iter,
694 bool* outUpdated,
695 bool* outNeedsRescan) {
Ethan Nicholascb670962017-04-20 19:31:52 -0400696 *outUpdated = true;
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400697 std::unique_ptr<Expression>* target = (*iter)->expression();
John Stilesa5a97b42020-08-18 11:19:07 -0400698 BinaryExpression& bin = (*target)->as<BinaryExpression>();
Ethan Nicholasbf66ffb2020-09-16 22:05:10 +0000699 SkASSERT(!bin.fLeft->hasSideEffects());
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400700 bool result;
Ethan Nicholasbf66ffb2020-09-16 22:05:10 +0000701 if (bin.fOperator == Token::Kind::TK_EQ) {
702 result = b->tryRemoveLValueBefore(iter, bin.fLeft.get());
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400703 } else {
Ethan Nicholasbf66ffb2020-09-16 22:05:10 +0000704 result = b->tryRemoveExpressionBefore(iter, bin.fLeft.get());
Ethan Nicholascb670962017-04-20 19:31:52 -0400705 }
Ethan Nicholasbf66ffb2020-09-16 22:05:10 +0000706 *target = std::move(bin.fRight);
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400707 if (!result) {
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400708 *outNeedsRescan = true;
709 return;
710 }
711 if (*iter == b->fNodes.begin()) {
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400712 *outNeedsRescan = true;
713 return;
714 }
715 --(*iter);
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400716 if ((*iter)->fKind != BasicBlock::Node::kExpression_Kind ||
Ethan Nicholasbf66ffb2020-09-16 22:05:10 +0000717 (*iter)->expression() != &bin.fRight) {
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400718 *outNeedsRescan = true;
719 return;
720 }
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400721 *iter = b->fNodes.erase(*iter);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400722 SkASSERT((*iter)->expression() == target);
Ethan Nicholascb670962017-04-20 19:31:52 -0400723}
724
725/**
726 * Collapses the binary expression pointed to by iter down to just the left side (in both the IR and
727 * CFG structures).
728 */
John Stilesafbf8992020-08-18 10:08:21 -0400729static void delete_right(BasicBlock* b,
730 std::vector<BasicBlock::Node>::iterator* iter,
731 bool* outUpdated,
732 bool* outNeedsRescan) {
Ethan Nicholascb670962017-04-20 19:31:52 -0400733 *outUpdated = true;
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400734 std::unique_ptr<Expression>* target = (*iter)->expression();
John Stilesa5a97b42020-08-18 11:19:07 -0400735 BinaryExpression& bin = (*target)->as<BinaryExpression>();
Ethan Nicholasbf66ffb2020-09-16 22:05:10 +0000736 SkASSERT(!bin.fRight->hasSideEffects());
737 if (!b->tryRemoveExpressionBefore(iter, bin.fRight.get())) {
738 *target = std::move(bin.fLeft);
Ethan Nicholascb670962017-04-20 19:31:52 -0400739 *outNeedsRescan = true;
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400740 return;
Ethan Nicholascb670962017-04-20 19:31:52 -0400741 }
Ethan Nicholasbf66ffb2020-09-16 22:05:10 +0000742 *target = std::move(bin.fLeft);
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400743 if (*iter == b->fNodes.begin()) {
744 *outNeedsRescan = true;
745 return;
746 }
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400747 --(*iter);
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400748 if (((*iter)->fKind != BasicBlock::Node::kExpression_Kind ||
Ethan Nicholasbf66ffb2020-09-16 22:05:10 +0000749 (*iter)->expression() != &bin.fLeft)) {
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400750 *outNeedsRescan = true;
751 return;
752 }
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400753 *iter = b->fNodes.erase(*iter);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400754 SkASSERT((*iter)->expression() == target);
Ethan Nicholascb670962017-04-20 19:31:52 -0400755}
756
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400757/**
758 * Constructs the specified type using a single argument.
759 */
Ethan Nicholas30d30222020-09-11 12:27:26 -0400760static std::unique_ptr<Expression> construct(const Type* type, std::unique_ptr<Expression> v) {
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400761 std::vector<std::unique_ptr<Expression>> args;
762 args.push_back(std::move(v));
Ethan Nicholase6592142020-09-08 10:22:09 -0400763 std::unique_ptr<Expression> result = std::make_unique<Constructor>(-1, type, std::move(args));
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400764 return result;
765}
766
767/**
768 * Used in the implementations of vectorize_left and vectorize_right. Given a vector type and an
769 * expression x, deletes the expression pointed to by iter and replaces it with <type>(x).
770 */
771static void vectorize(BasicBlock* b,
772 std::vector<BasicBlock::Node>::iterator* iter,
773 const Type& type,
774 std::unique_ptr<Expression>* otherExpression,
775 bool* outUpdated,
776 bool* outNeedsRescan) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400777 SkASSERT((*(*iter)->expression())->kind() == Expression::Kind::kBinary);
778 SkASSERT(type.typeKind() == Type::TypeKind::kVector);
Ethan Nicholas30d30222020-09-11 12:27:26 -0400779 SkASSERT((*otherExpression)->type().typeKind() == Type::TypeKind::kScalar);
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400780 *outUpdated = true;
781 std::unique_ptr<Expression>* target = (*iter)->expression();
782 if (!b->tryRemoveExpression(iter)) {
Ethan Nicholas30d30222020-09-11 12:27:26 -0400783 *target = construct(&type, std::move(*otherExpression));
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400784 *outNeedsRescan = true;
785 } else {
Ethan Nicholas30d30222020-09-11 12:27:26 -0400786 *target = construct(&type, std::move(*otherExpression));
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400787 if (!b->tryInsertExpression(iter, target)) {
788 *outNeedsRescan = true;
789 }
790 }
791}
792
793/**
794 * Given a binary expression of the form x <op> vec<n>(y), deletes the right side and vectorizes the
795 * left to yield vec<n>(x).
796 */
797static void vectorize_left(BasicBlock* b,
798 std::vector<BasicBlock::Node>::iterator* iter,
799 bool* outUpdated,
800 bool* outNeedsRescan) {
John Stilesa5a97b42020-08-18 11:19:07 -0400801 BinaryExpression& bin = (*(*iter)->expression())->as<BinaryExpression>();
Ethan Nicholasbf66ffb2020-09-16 22:05:10 +0000802 vectorize(b, iter, bin.fRight->type(), &bin.fLeft, outUpdated, outNeedsRescan);
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400803}
804
805/**
806 * Given a binary expression of the form vec<n>(x) <op> y, deletes the left side and vectorizes the
807 * right to yield vec<n>(y).
808 */
809static void vectorize_right(BasicBlock* b,
810 std::vector<BasicBlock::Node>::iterator* iter,
811 bool* outUpdated,
812 bool* outNeedsRescan) {
John Stilesa5a97b42020-08-18 11:19:07 -0400813 BinaryExpression& bin = (*(*iter)->expression())->as<BinaryExpression>();
Ethan Nicholasbf66ffb2020-09-16 22:05:10 +0000814 vectorize(b, iter, bin.fLeft->type(), &bin.fRight, outUpdated, outNeedsRescan);
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400815}
816
817// Mark that an expression which we were writing to is no longer being written to
John Stilesa5a97b42020-08-18 11:19:07 -0400818static void clear_write(Expression& expr) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400819 switch (expr.kind()) {
820 case Expression::Kind::kVariableReference: {
John Stilesa5a97b42020-08-18 11:19:07 -0400821 expr.as<VariableReference>().setRefKind(VariableReference::kRead_RefKind);
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400822 break;
823 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400824 case Expression::Kind::kFieldAccess:
John Stilesa5a97b42020-08-18 11:19:07 -0400825 clear_write(*expr.as<FieldAccess>().fBase);
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400826 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400827 case Expression::Kind::kSwizzle:
John Stilesa5a97b42020-08-18 11:19:07 -0400828 clear_write(*expr.as<Swizzle>().fBase);
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400829 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400830 case Expression::Kind::kIndex:
John Stilesa5a97b42020-08-18 11:19:07 -0400831 clear_write(*expr.as<IndexExpression>().fBase);
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400832 break;
833 default:
834 ABORT("shouldn't be writing to this kind of expression\n");
835 break;
836 }
837}
838
Ethan Nicholascb670962017-04-20 19:31:52 -0400839void Compiler::simplifyExpression(DefinitionMap& definitions,
840 BasicBlock& b,
841 std::vector<BasicBlock::Node>::iterator* iter,
842 std::unordered_set<const Variable*>* undefinedVariables,
843 bool* outUpdated,
844 bool* outNeedsRescan) {
845 Expression* expr = (*iter)->expression()->get();
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400846 SkASSERT(expr);
Ethan Nicholascb670962017-04-20 19:31:52 -0400847 if ((*iter)->fConstantPropagation) {
848 std::unique_ptr<Expression> optimized = expr->constantPropagate(*fIRGenerator, definitions);
849 if (optimized) {
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400850 *outUpdated = true;
Ethan Nicholas30d30222020-09-11 12:27:26 -0400851 optimized = fIRGenerator->coerce(std::move(optimized), expr->type());
Ethan Nicholas1d881522020-09-11 09:32:54 -0400852 SkASSERT(optimized);
Ethan Nicholascb670962017-04-20 19:31:52 -0400853 if (!try_replace_expression(&b, iter, &optimized)) {
854 *outNeedsRescan = true;
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400855 return;
Ethan Nicholascb670962017-04-20 19:31:52 -0400856 }
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400857 SkASSERT((*iter)->fKind == BasicBlock::Node::kExpression_Kind);
Ethan Nicholascb670962017-04-20 19:31:52 -0400858 expr = (*iter)->expression()->get();
Ethan Nicholascb670962017-04-20 19:31:52 -0400859 }
860 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400861 switch (expr->kind()) {
862 case Expression::Kind::kVariableReference: {
John Stilesa5a97b42020-08-18 11:19:07 -0400863 const VariableReference& ref = expr->as<VariableReference>();
Ethan Nicholas8f7e28f2018-03-26 14:24:27 -0400864 const Variable& var = ref.fVariable;
865 if (ref.refKind() != VariableReference::kWrite_RefKind &&
866 ref.refKind() != VariableReference::kPointer_RefKind &&
867 var.fStorage == Variable::kLocal_Storage && !definitions[&var] &&
Ethan Nicholascb670962017-04-20 19:31:52 -0400868 (*undefinedVariables).find(&var) == (*undefinedVariables).end()) {
869 (*undefinedVariables).insert(&var);
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000870 this->error(expr->fOffset,
871 "'" + var.fName + "' has not been assigned");
Ethan Nicholascb670962017-04-20 19:31:52 -0400872 }
873 break;
874 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400875 case Expression::Kind::kTernary: {
John Stiles403a3632020-08-20 12:11:48 -0400876 TernaryExpression* t = &expr->as<TernaryExpression>();
Ethan Nicholase6592142020-09-08 10:22:09 -0400877 if (t->fTest->kind() == Expression::Kind::kBoolLiteral) {
Ethan Nicholascb670962017-04-20 19:31:52 -0400878 // ternary has a constant test, replace it with either the true or
879 // false branch
John Stiles403a3632020-08-20 12:11:48 -0400880 if (t->fTest->as<BoolLiteral>().fValue) {
Ethan Nicholascb670962017-04-20 19:31:52 -0400881 (*iter)->setExpression(std::move(t->fIfTrue));
882 } else {
883 (*iter)->setExpression(std::move(t->fIfFalse));
884 }
885 *outUpdated = true;
886 *outNeedsRescan = true;
887 }
888 break;
889 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400890 case Expression::Kind::kBinary: {
John Stiles403a3632020-08-20 12:11:48 -0400891 BinaryExpression* bin = &expr->as<BinaryExpression>();
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400892 if (dead_assignment(*bin)) {
893 delete_left(&b, iter, outUpdated, outNeedsRescan);
894 break;
895 }
Ethan Nicholasbf66ffb2020-09-16 22:05:10 +0000896 const Type& leftType = bin->fLeft->type();
897 const Type& rightType = bin->fRight->type();
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400898 // collapse useless expressions like x * 1 or x + 0
Ethan Nicholas30d30222020-09-11 12:27:26 -0400899 if (((leftType.typeKind() != Type::TypeKind::kScalar) &&
900 (leftType.typeKind() != Type::TypeKind::kVector)) ||
901 ((rightType.typeKind() != Type::TypeKind::kScalar) &&
902 (rightType.typeKind() != Type::TypeKind::kVector))) {
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400903 break;
904 }
Ethan Nicholasbf66ffb2020-09-16 22:05:10 +0000905 switch (bin->fOperator) {
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400906 case Token::Kind::TK_STAR:
Ethan Nicholasbf66ffb2020-09-16 22:05:10 +0000907 if (is_constant(*bin->fLeft, 1)) {
Ethan Nicholas30d30222020-09-11 12:27:26 -0400908 if (leftType.typeKind() == Type::TypeKind::kVector &&
909 rightType.typeKind() == Type::TypeKind::kScalar) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400910 // float4(1) * x -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400911 vectorize_right(&b, iter, outUpdated, outNeedsRescan);
912 } else {
913 // 1 * x -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400914 // 1 * float4(x) -> float4(x)
915 // float4(1) * float4(x) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400916 delete_left(&b, iter, outUpdated, outNeedsRescan);
917 }
918 }
Ethan Nicholasbf66ffb2020-09-16 22:05:10 +0000919 else if (is_constant(*bin->fLeft, 0)) {
Ethan Nicholas30d30222020-09-11 12:27:26 -0400920 if (leftType.typeKind() == Type::TypeKind::kScalar &&
921 rightType.typeKind() == Type::TypeKind::kVector &&
Ethan Nicholasbf66ffb2020-09-16 22:05:10 +0000922 !bin->fRight->hasSideEffects()) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400923 // 0 * float4(x) -> float4(0)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400924 vectorize_left(&b, iter, outUpdated, outNeedsRescan);
925 } else {
926 // 0 * x -> 0
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400927 // float4(0) * x -> float4(0)
928 // float4(0) * float4(x) -> float4(0)
Ethan Nicholasbf66ffb2020-09-16 22:05:10 +0000929 if (!bin->fRight->hasSideEffects()) {
Ethan Nicholas51493ee2017-12-11 12:34:33 -0500930 delete_right(&b, iter, outUpdated, outNeedsRescan);
931 }
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400932 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400933 }
Ethan Nicholasbf66ffb2020-09-16 22:05:10 +0000934 else if (is_constant(*bin->fRight, 1)) {
Ethan Nicholas30d30222020-09-11 12:27:26 -0400935 if (leftType.typeKind() == Type::TypeKind::kScalar &&
936 rightType.typeKind() == Type::TypeKind::kVector) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400937 // x * float4(1) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400938 vectorize_left(&b, iter, outUpdated, outNeedsRescan);
939 } else {
940 // x * 1 -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400941 // float4(x) * 1 -> float4(x)
942 // float4(x) * float4(1) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400943 delete_right(&b, iter, outUpdated, outNeedsRescan);
944 }
945 }
Ethan Nicholasbf66ffb2020-09-16 22:05:10 +0000946 else if (is_constant(*bin->fRight, 0)) {
Ethan Nicholas30d30222020-09-11 12:27:26 -0400947 if (leftType.typeKind() == Type::TypeKind::kVector &&
948 rightType.typeKind() == Type::TypeKind::kScalar &&
Ethan Nicholasbf66ffb2020-09-16 22:05:10 +0000949 !bin->fLeft->hasSideEffects()) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400950 // float4(x) * 0 -> float4(0)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400951 vectorize_right(&b, iter, outUpdated, outNeedsRescan);
952 } else {
953 // x * 0 -> 0
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400954 // x * float4(0) -> float4(0)
955 // float4(x) * float4(0) -> float4(0)
Ethan Nicholasbf66ffb2020-09-16 22:05:10 +0000956 if (!bin->fLeft->hasSideEffects()) {
Ethan Nicholas51493ee2017-12-11 12:34:33 -0500957 delete_left(&b, iter, outUpdated, outNeedsRescan);
958 }
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400959 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400960 }
961 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400962 case Token::Kind::TK_PLUS:
Ethan Nicholasbf66ffb2020-09-16 22:05:10 +0000963 if (is_constant(*bin->fLeft, 0)) {
Ethan Nicholas30d30222020-09-11 12:27:26 -0400964 if (leftType.typeKind() == Type::TypeKind::kVector &&
965 rightType.typeKind() == Type::TypeKind::kScalar) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400966 // float4(0) + x -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400967 vectorize_right(&b, iter, outUpdated, outNeedsRescan);
968 } else {
969 // 0 + x -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400970 // 0 + float4(x) -> float4(x)
971 // float4(0) + float4(x) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400972 delete_left(&b, iter, outUpdated, outNeedsRescan);
973 }
Ethan Nicholasbf66ffb2020-09-16 22:05:10 +0000974 } else if (is_constant(*bin->fRight, 0)) {
Ethan Nicholas30d30222020-09-11 12:27:26 -0400975 if (leftType.typeKind() == Type::TypeKind::kScalar &&
976 rightType.typeKind() == Type::TypeKind::kVector) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400977 // x + float4(0) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400978 vectorize_left(&b, iter, outUpdated, outNeedsRescan);
979 } else {
980 // x + 0 -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400981 // float4(x) + 0 -> float4(x)
982 // float4(x) + float4(0) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400983 delete_right(&b, iter, outUpdated, outNeedsRescan);
984 }
Ethan Nicholas56e42712017-04-21 10:23:37 -0400985 }
986 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400987 case Token::Kind::TK_MINUS:
Ethan Nicholasbf66ffb2020-09-16 22:05:10 +0000988 if (is_constant(*bin->fRight, 0)) {
Ethan Nicholas30d30222020-09-11 12:27:26 -0400989 if (leftType.typeKind() == Type::TypeKind::kScalar &&
990 rightType.typeKind() == Type::TypeKind::kVector) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400991 // x - float4(0) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400992 vectorize_left(&b, iter, outUpdated, outNeedsRescan);
993 } else {
994 // x - 0 -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400995 // float4(x) - 0 -> float4(x)
996 // float4(x) - float4(0) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400997 delete_right(&b, iter, outUpdated, outNeedsRescan);
998 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400999 }
1000 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001001 case Token::Kind::TK_SLASH:
Ethan Nicholasbf66ffb2020-09-16 22:05:10 +00001002 if (is_constant(*bin->fRight, 1)) {
Ethan Nicholas30d30222020-09-11 12:27:26 -04001003 if (leftType.typeKind() == Type::TypeKind::kScalar &&
1004 rightType.typeKind() == Type::TypeKind::kVector) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001005 // x / float4(1) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001006 vectorize_left(&b, iter, outUpdated, outNeedsRescan);
1007 } else {
1008 // x / 1 -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001009 // float4(x) / 1 -> float4(x)
1010 // float4(x) / float4(1) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001011 delete_right(&b, iter, outUpdated, outNeedsRescan);
1012 }
Ethan Nicholasbf66ffb2020-09-16 22:05:10 +00001013 } else if (is_constant(*bin->fLeft, 0)) {
Ethan Nicholas30d30222020-09-11 12:27:26 -04001014 if (leftType.typeKind() == Type::TypeKind::kScalar &&
1015 rightType.typeKind() == Type::TypeKind::kVector &&
Ethan Nicholasbf66ffb2020-09-16 22:05:10 +00001016 !bin->fRight->hasSideEffects()) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001017 // 0 / float4(x) -> float4(0)
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001018 vectorize_left(&b, iter, outUpdated, outNeedsRescan);
1019 } else {
1020 // 0 / x -> 0
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001021 // float4(0) / x -> float4(0)
1022 // float4(0) / float4(x) -> float4(0)
Ethan Nicholasbf66ffb2020-09-16 22:05:10 +00001023 if (!bin->fRight->hasSideEffects()) {
Ethan Nicholas51493ee2017-12-11 12:34:33 -05001024 delete_right(&b, iter, outUpdated, outNeedsRescan);
1025 }
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001026 }
1027 }
1028 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001029 case Token::Kind::TK_PLUSEQ:
Ethan Nicholasbf66ffb2020-09-16 22:05:10 +00001030 if (is_constant(*bin->fRight, 0)) {
1031 clear_write(*bin->fLeft);
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001032 delete_right(&b, iter, outUpdated, outNeedsRescan);
1033 }
1034 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001035 case Token::Kind::TK_MINUSEQ:
Ethan Nicholasbf66ffb2020-09-16 22:05:10 +00001036 if (is_constant(*bin->fRight, 0)) {
1037 clear_write(*bin->fLeft);
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001038 delete_right(&b, iter, outUpdated, outNeedsRescan);
1039 }
1040 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001041 case Token::Kind::TK_STAREQ:
Ethan Nicholasbf66ffb2020-09-16 22:05:10 +00001042 if (is_constant(*bin->fRight, 1)) {
1043 clear_write(*bin->fLeft);
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001044 delete_right(&b, iter, outUpdated, outNeedsRescan);
1045 }
1046 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001047 case Token::Kind::TK_SLASHEQ:
Ethan Nicholasbf66ffb2020-09-16 22:05:10 +00001048 if (is_constant(*bin->fRight, 1)) {
1049 clear_write(*bin->fLeft);
Ethan Nicholascb670962017-04-20 19:31:52 -04001050 delete_right(&b, iter, outUpdated, outNeedsRescan);
1051 }
1052 break;
1053 default:
1054 break;
1055 }
Ethan Nicholas409f6f02019-09-17 12:34:39 -04001056 break;
1057 }
Ethan Nicholase6592142020-09-08 10:22:09 -04001058 case Expression::Kind::kSwizzle: {
John Stiles403a3632020-08-20 12:11:48 -04001059 Swizzle& s = expr->as<Swizzle>();
Ethan Nicholas409f6f02019-09-17 12:34:39 -04001060 // detect identity swizzles like foo.rgba
Ethan Nicholas30d30222020-09-11 12:27:26 -04001061 if ((int) s.fComponents.size() == s.fBase->type().columns()) {
Ethan Nicholas409f6f02019-09-17 12:34:39 -04001062 bool identity = true;
1063 for (int i = 0; i < (int) s.fComponents.size(); ++i) {
1064 if (s.fComponents[i] != i) {
1065 identity = false;
1066 break;
1067 }
1068 }
1069 if (identity) {
1070 *outUpdated = true;
1071 if (!try_replace_expression(&b, iter, &s.fBase)) {
1072 *outNeedsRescan = true;
1073 return;
1074 }
1075 SkASSERT((*iter)->fKind == BasicBlock::Node::kExpression_Kind);
1076 break;
1077 }
1078 }
1079 // detect swizzles of swizzles, e.g. replace foo.argb.r000 with foo.a000
Ethan Nicholase6592142020-09-08 10:22:09 -04001080 if (s.fBase->kind() == Expression::Kind::kSwizzle) {
John Stilesa5a97b42020-08-18 11:19:07 -04001081 Swizzle& base = s.fBase->as<Swizzle>();
Ethan Nicholas409f6f02019-09-17 12:34:39 -04001082 std::vector<int> final;
1083 for (int c : s.fComponents) {
Brian Osman25647672020-09-15 15:16:56 -04001084 final.push_back(base.fComponents[c]);
Ethan Nicholas409f6f02019-09-17 12:34:39 -04001085 }
1086 *outUpdated = true;
1087 std::unique_ptr<Expression> replacement(new Swizzle(*fContext, base.fBase->clone(),
1088 std::move(final)));
1089 if (!try_replace_expression(&b, iter, &replacement)) {
1090 *outNeedsRescan = true;
1091 return;
1092 }
1093 SkASSERT((*iter)->fKind == BasicBlock::Node::kExpression_Kind);
Ethan Nicholas409f6f02019-09-17 12:34:39 -04001094 }
John Stiles30212b72020-06-11 17:55:07 -04001095 break;
Ethan Nicholascb670962017-04-20 19:31:52 -04001096 }
1097 default:
1098 break;
1099 }
1100}
1101
John Stiles92219b42020-06-15 12:32:24 -04001102// Returns true if this statement could potentially execute a break at the current level. We ignore
1103// nested loops and switches, since any breaks inside of them will merely break the loop / switch.
John Stilesb92641c2020-08-31 18:09:01 -04001104static bool contains_conditional_break(Statement& stmt) {
1105 class ContainsConditionalBreak : public ProgramVisitor {
1106 public:
1107 bool visitStatement(const Statement& stmt) override {
Ethan Nicholase6592142020-09-08 10:22:09 -04001108 switch (stmt.kind()) {
1109 case Statement::Kind::kBlock:
John Stilesb92641c2020-08-31 18:09:01 -04001110 return this->INHERITED::visitStatement(stmt);
1111
Ethan Nicholase6592142020-09-08 10:22:09 -04001112 case Statement::Kind::kBreak:
John Stilesb92641c2020-08-31 18:09:01 -04001113 return fInConditional > 0;
1114
Ethan Nicholase6592142020-09-08 10:22:09 -04001115 case Statement::Kind::kIf: {
John Stilesb92641c2020-08-31 18:09:01 -04001116 ++fInConditional;
1117 bool result = this->INHERITED::visitStatement(stmt);
1118 --fInConditional;
1119 return result;
1120 }
1121
1122 default:
1123 return false;
1124 }
1125 }
1126
1127 int fInConditional = 0;
1128 using INHERITED = ProgramVisitor;
1129 };
1130
1131 return ContainsConditionalBreak{}.visitStatement(stmt);
John Stiles92219b42020-06-15 12:32:24 -04001132}
1133
Ethan Nicholas5005a222018-08-24 13:06:27 -04001134// returns true if this statement definitely executes a break at the current level (we ignore
1135// nested loops and switches, since any breaks inside of them will merely break the loop / switch)
John Stilesb92641c2020-08-31 18:09:01 -04001136static bool contains_unconditional_break(Statement& stmt) {
1137 class ContainsUnconditionalBreak : public ProgramVisitor {
1138 public:
1139 bool visitStatement(const Statement& stmt) override {
Ethan Nicholase6592142020-09-08 10:22:09 -04001140 switch (stmt.kind()) {
1141 case Statement::Kind::kBlock:
John Stilesb92641c2020-08-31 18:09:01 -04001142 return this->INHERITED::visitStatement(stmt);
1143
Ethan Nicholase6592142020-09-08 10:22:09 -04001144 case Statement::Kind::kBreak:
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001145 return true;
John Stilesb92641c2020-08-31 18:09:01 -04001146
1147 default:
1148 return false;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001149 }
John Stilesb92641c2020-08-31 18:09:01 -04001150 }
John Stiles92219b42020-06-15 12:32:24 -04001151
John Stilesb92641c2020-08-31 18:09:01 -04001152 using INHERITED = ProgramVisitor;
1153 };
John Stiles92219b42020-06-15 12:32:24 -04001154
John Stilesb92641c2020-08-31 18:09:01 -04001155 return ContainsUnconditionalBreak{}.visitStatement(stmt);
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001156}
1157
John Stiles92219b42020-06-15 12:32:24 -04001158static void move_all_but_break(std::unique_ptr<Statement>& stmt,
1159 std::vector<std::unique_ptr<Statement>>* target) {
Ethan Nicholase6592142020-09-08 10:22:09 -04001160 switch (stmt->kind()) {
1161 case Statement::Kind::kBlock: {
John Stiles92219b42020-06-15 12:32:24 -04001162 // Recurse into the block.
1163 Block& block = static_cast<Block&>(*stmt);
1164
1165 std::vector<std::unique_ptr<Statement>> blockStmts;
1166 blockStmts.reserve(block.fStatements.size());
1167 for (std::unique_ptr<Statement>& statementInBlock : block.fStatements) {
1168 move_all_but_break(statementInBlock, &blockStmts);
Ethan Nicholas739e1ca2020-06-11 12:16:14 -04001169 }
John Stiles92219b42020-06-15 12:32:24 -04001170
1171 target->push_back(std::make_unique<Block>(block.fOffset, std::move(blockStmts),
1172 block.fSymbols, block.fIsScope));
Ethan Nicholas739e1ca2020-06-11 12:16:14 -04001173 break;
John Stiles92219b42020-06-15 12:32:24 -04001174 }
1175
Ethan Nicholase6592142020-09-08 10:22:09 -04001176 case Statement::Kind::kBreak:
John Stiles92219b42020-06-15 12:32:24 -04001177 // Do not append a break to the target.
1178 break;
1179
Ethan Nicholas739e1ca2020-06-11 12:16:14 -04001180 default:
John Stiles92219b42020-06-15 12:32:24 -04001181 // Append normal statements to the target.
1182 target->push_back(std::move(stmt));
1183 break;
Ethan Nicholas739e1ca2020-06-11 12:16:14 -04001184 }
1185}
1186
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001187// Returns a block containing all of the statements that will be run if the given case matches
1188// (which, owing to the statements being owned by unique_ptrs, means the switch itself will be
1189// broken by this call and must then be discarded).
1190// Returns null (and leaves the switch unmodified) if no such simple reduction is possible, such as
1191// when break statements appear inside conditionals.
John Stiles92219b42020-06-15 12:32:24 -04001192static std::unique_ptr<Statement> block_for_case(SwitchStatement* switchStatement,
1193 SwitchCase* caseToCapture) {
1194 // We have to be careful to not move any of the pointers until after we're sure we're going to
1195 // succeed, so before we make any changes at all, we check the switch-cases to decide on a plan
1196 // of action. First, find the switch-case we are interested in.
1197 auto iter = switchStatement->fCases.begin();
1198 for (; iter != switchStatement->fCases.end(); ++iter) {
1199 if (iter->get() == caseToCapture) {
1200 break;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001201 }
John Stiles92219b42020-06-15 12:32:24 -04001202 }
1203
1204 // Next, walk forward through the rest of the switch. If we find a conditional break, we're
1205 // stuck and can't simplify at all. If we find an unconditional break, we have a range of
1206 // statements that we can use for simplification.
1207 auto startIter = iter;
1208 Statement* unconditionalBreakStmt = nullptr;
1209 for (; iter != switchStatement->fCases.end(); ++iter) {
1210 for (std::unique_ptr<Statement>& stmt : (*iter)->fStatements) {
1211 if (contains_conditional_break(*stmt)) {
1212 // We can't reduce switch-cases to a block when they have conditional breaks.
1213 return nullptr;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001214 }
John Stiles92219b42020-06-15 12:32:24 -04001215
1216 if (contains_unconditional_break(*stmt)) {
1217 // We found an unconditional break. We can use this block, but we need to strip
1218 // out the break statement.
1219 unconditionalBreakStmt = stmt.get();
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001220 break;
1221 }
1222 }
John Stiles92219b42020-06-15 12:32:24 -04001223
1224 if (unconditionalBreakStmt != nullptr) {
1225 break;
1226 }
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001227 }
John Stiles92219b42020-06-15 12:32:24 -04001228
1229 // We fell off the bottom of the switch or encountered a break. We know the range of statements
1230 // that we need to move over, and we know it's safe to do so.
1231 std::vector<std::unique_ptr<Statement>> caseStmts;
1232
1233 // We can move over most of the statements as-is.
1234 while (startIter != iter) {
1235 for (std::unique_ptr<Statement>& stmt : (*startIter)->fStatements) {
1236 caseStmts.push_back(std::move(stmt));
1237 }
1238 ++startIter;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001239 }
John Stiles92219b42020-06-15 12:32:24 -04001240
1241 // If we found an unconditional break at the end, we need to move what we can while avoiding
1242 // that break.
1243 if (unconditionalBreakStmt != nullptr) {
1244 for (std::unique_ptr<Statement>& stmt : (*startIter)->fStatements) {
1245 if (stmt.get() == unconditionalBreakStmt) {
1246 move_all_but_break(stmt, &caseStmts);
1247 unconditionalBreakStmt = nullptr;
1248 break;
1249 }
1250
1251 caseStmts.push_back(std::move(stmt));
1252 }
1253 }
1254
1255 SkASSERT(unconditionalBreakStmt == nullptr); // Verify that we fixed the unconditional break.
1256
1257 // Return our newly-synthesized block.
1258 return std::make_unique<Block>(/*offset=*/-1, std::move(caseStmts), switchStatement->fSymbols);
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001259}
1260
Ethan Nicholascb670962017-04-20 19:31:52 -04001261void Compiler::simplifyStatement(DefinitionMap& definitions,
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001262 BasicBlock& b,
1263 std::vector<BasicBlock::Node>::iterator* iter,
1264 std::unordered_set<const Variable*>* undefinedVariables,
1265 bool* outUpdated,
1266 bool* outNeedsRescan) {
Ethan Nicholascb670962017-04-20 19:31:52 -04001267 Statement* stmt = (*iter)->statement()->get();
Ethan Nicholase6592142020-09-08 10:22:09 -04001268 switch (stmt->kind()) {
1269 case Statement::Kind::kVarDeclaration: {
John Stilesa5a97b42020-08-18 11:19:07 -04001270 const auto& varDecl = stmt->as<VarDeclaration>();
Ethan Nicholas82a62d22017-11-07 14:42:10 +00001271 if (varDecl.fVar->dead() &&
1272 (!varDecl.fValue ||
1273 !varDecl.fValue->hasSideEffects())) {
1274 if (varDecl.fValue) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001275 SkASSERT((*iter)->statement()->get() == stmt);
Ethan Nicholas82a62d22017-11-07 14:42:10 +00001276 if (!b.tryRemoveExpressionBefore(iter, varDecl.fValue.get())) {
1277 *outNeedsRescan = true;
Ethan Nicholascb670962017-04-20 19:31:52 -04001278 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001279 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001280 (*iter)->setStatement(std::unique_ptr<Statement>(new Nop()));
Ethan Nicholas82a62d22017-11-07 14:42:10 +00001281 *outUpdated = true;
Ethan Nicholascb670962017-04-20 19:31:52 -04001282 }
1283 break;
1284 }
Ethan Nicholase6592142020-09-08 10:22:09 -04001285 case Statement::Kind::kIf: {
John Stilesa5a97b42020-08-18 11:19:07 -04001286 IfStatement& i = stmt->as<IfStatement>();
Ethan Nicholase6592142020-09-08 10:22:09 -04001287 if (i.fTest->kind() == Expression::Kind::kBoolLiteral) {
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001288 // constant if, collapse down to a single branch
John Stilesa5a97b42020-08-18 11:19:07 -04001289 if (i.fTest->as<BoolLiteral>().fValue) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001290 SkASSERT(i.fIfTrue);
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001291 (*iter)->setStatement(std::move(i.fIfTrue));
1292 } else {
1293 if (i.fIfFalse) {
1294 (*iter)->setStatement(std::move(i.fIfFalse));
1295 } else {
1296 (*iter)->setStatement(std::unique_ptr<Statement>(new Nop()));
1297 }
1298 }
1299 *outUpdated = true;
1300 *outNeedsRescan = true;
1301 break;
1302 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001303 if (i.fIfFalse && i.fIfFalse->isEmpty()) {
1304 // else block doesn't do anything, remove it
1305 i.fIfFalse.reset();
1306 *outUpdated = true;
1307 *outNeedsRescan = true;
1308 }
1309 if (!i.fIfFalse && i.fIfTrue->isEmpty()) {
1310 // if block doesn't do anything, no else block
1311 if (i.fTest->hasSideEffects()) {
1312 // test has side effects, keep it
1313 (*iter)->setStatement(std::unique_ptr<Statement>(
1314 new ExpressionStatement(std::move(i.fTest))));
1315 } else {
1316 // no if, no else, no test side effects, kill the whole if
1317 // statement
1318 (*iter)->setStatement(std::unique_ptr<Statement>(new Nop()));
1319 }
1320 *outUpdated = true;
1321 *outNeedsRescan = true;
1322 }
1323 break;
1324 }
Ethan Nicholase6592142020-09-08 10:22:09 -04001325 case Statement::Kind::kSwitch: {
John Stilesa5a97b42020-08-18 11:19:07 -04001326 SwitchStatement& s = stmt->as<SwitchStatement>();
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001327 int64_t switchValue;
1328 if (fIRGenerator->getConstantInt(*s.fValue, &switchValue)) {
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001329 // switch is constant, replace it with the case that matches
1330 bool found = false;
1331 SwitchCase* defaultCase = nullptr;
John Stiles9d944232020-08-19 09:56:49 -04001332 for (const std::unique_ptr<SwitchCase>& c : s.fCases) {
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001333 if (!c->fValue) {
1334 defaultCase = c.get();
1335 continue;
1336 }
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001337 int64_t caseValue;
1338 SkAssertResult(fIRGenerator->getConstantInt(*c->fValue, &caseValue));
1339 if (caseValue == switchValue) {
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001340 std::unique_ptr<Statement> newBlock = block_for_case(&s, c.get());
1341 if (newBlock) {
1342 (*iter)->setStatement(std::move(newBlock));
John Stiles9d944232020-08-19 09:56:49 -04001343 found = true;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001344 break;
1345 } else {
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -04001346 if (s.fIsStatic && !(fFlags & kPermitInvalidStaticTests_Flag)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001347 this->error(s.fOffset,
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001348 "static switch contains non-static conditional break");
1349 s.fIsStatic = false;
1350 }
1351 return; // can't simplify
1352 }
1353 }
1354 }
1355 if (!found) {
1356 // no matching case. use default if it exists, or kill the whole thing
1357 if (defaultCase) {
1358 std::unique_ptr<Statement> newBlock = block_for_case(&s, defaultCase);
1359 if (newBlock) {
1360 (*iter)->setStatement(std::move(newBlock));
1361 } else {
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -04001362 if (s.fIsStatic && !(fFlags & kPermitInvalidStaticTests_Flag)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001363 this->error(s.fOffset,
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001364 "static switch contains non-static conditional break");
1365 s.fIsStatic = false;
1366 }
1367 return; // can't simplify
1368 }
1369 } else {
1370 (*iter)->setStatement(std::unique_ptr<Statement>(new Nop()));
1371 }
1372 }
1373 *outUpdated = true;
1374 *outNeedsRescan = true;
1375 }
1376 break;
1377 }
Ethan Nicholase6592142020-09-08 10:22:09 -04001378 case Statement::Kind::kExpression: {
John Stilesa5a97b42020-08-18 11:19:07 -04001379 ExpressionStatement& e = stmt->as<ExpressionStatement>();
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001380 SkASSERT((*iter)->statement()->get() == &e);
Ethan Nicholascb670962017-04-20 19:31:52 -04001381 if (!e.fExpression->hasSideEffects()) {
1382 // Expression statement with no side effects, kill it
1383 if (!b.tryRemoveExpressionBefore(iter, e.fExpression.get())) {
1384 *outNeedsRescan = true;
1385 }
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001386 SkASSERT((*iter)->statement()->get() == stmt);
Ethan Nicholascb670962017-04-20 19:31:52 -04001387 (*iter)->setStatement(std::unique_ptr<Statement>(new Nop()));
1388 *outUpdated = true;
1389 }
1390 break;
1391 }
1392 default:
1393 break;
1394 }
1395}
1396
John Stiles0cc193a2020-09-09 09:39:34 -04001397bool Compiler::scanCFG(FunctionDefinition& f) {
1398 bool madeChanges = false;
1399
Ethan Nicholascb670962017-04-20 19:31:52 -04001400 CFG cfg = CFGGenerator().getCFG(f);
1401 this->computeDataFlow(&cfg);
ethannicholas22f939e2016-10-13 13:25:34 -07001402
1403 // check for unreachable code
1404 for (size_t i = 0; i < cfg.fBlocks.size(); i++) {
John Stiles0cc193a2020-09-09 09:39:34 -04001405 const BasicBlock& block = cfg.fBlocks[i];
1406 if (i != cfg.fStart && !block.fEntrances.size() && block.fNodes.size()) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001407 int offset;
John Stiles0cc193a2020-09-09 09:39:34 -04001408 const BasicBlock::Node& node = block.fNodes[0];
1409 switch (node.fKind) {
Ethan Nicholas86a43402017-01-19 13:32:00 -05001410 case BasicBlock::Node::kStatement_Kind:
John Stiles0cc193a2020-09-09 09:39:34 -04001411 offset = (*node.statement())->fOffset;
Ethan Nicholas86a43402017-01-19 13:32:00 -05001412 break;
1413 case BasicBlock::Node::kExpression_Kind:
John Stiles0cc193a2020-09-09 09:39:34 -04001414 offset = (*node.expression())->fOffset;
1415 if ((*node.expression())->is<BoolLiteral>()) {
Ethan Nicholas70728ef2020-05-28 07:09:00 -04001416 // Function inlining can generate do { ... } while(false) loops which always
1417 // break, so the boolean condition is considered unreachable. Since not
1418 // being able to reach a literal is a non-issue in the first place, we
1419 // don't report an error in this case.
1420 continue;
1421 }
Ethan Nicholas86a43402017-01-19 13:32:00 -05001422 break;
1423 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001424 this->error(offset, String("unreachable"));
ethannicholas22f939e2016-10-13 13:25:34 -07001425 }
1426 }
1427 if (fErrorCount) {
John Stiles0cc193a2020-09-09 09:39:34 -04001428 return madeChanges;
ethannicholas22f939e2016-10-13 13:25:34 -07001429 }
1430
Ethan Nicholascb670962017-04-20 19:31:52 -04001431 // check for dead code & undefined variables, perform constant propagation
1432 std::unordered_set<const Variable*> undefinedVariables;
1433 bool updated;
1434 bool needsRescan = false;
1435 do {
1436 if (needsRescan) {
1437 cfg = CFGGenerator().getCFG(f);
1438 this->computeDataFlow(&cfg);
1439 needsRescan = false;
Ethan Nicholas113628d2017-02-02 16:11:39 -05001440 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001441
1442 updated = false;
Ethan Nicholas1de14812020-06-19 15:32:49 -04001443 bool first = true;
Ethan Nicholascb670962017-04-20 19:31:52 -04001444 for (BasicBlock& b : cfg.fBlocks) {
Ethan Nicholas1de14812020-06-19 15:32:49 -04001445 if (!first && b.fEntrances.empty()) {
1446 // Block was reachable before optimization, but has since become unreachable. In
1447 // addition to being dead code, it's broken - since control flow can't reach it, no
1448 // prior variable definitions can reach it, and therefore variables might look to
1449 // have not been properly assigned. Kill it.
Brian Osmandb16c482020-09-09 15:15:06 -04001450
1451 // We need to do this in two steps. For any variable declarations, the node list
1452 // will contain statement nodes for each VarDeclaration, and then a statement for
1453 // the VarDeclarationsStatement. When we replace the VDS with a Nop, we delete the
1454 // storage of the unique_ptr that the VD nodes are pointing to. So we remove those
1455 // from the node list entirely, first.
1456 b.fNodes.erase(
1457 std::remove_if(b.fNodes.begin(), b.fNodes.end(),
1458 [](const BasicBlock::Node& node) {
1459 return node.fKind == BasicBlock::Node::kStatement_Kind &&
1460 (*node.statement())->is<VarDeclaration>();
1461 }),
1462 b.fNodes.end());
1463
1464 // Now replace any remaining statements in the block with Nops.
Ethan Nicholas1de14812020-06-19 15:32:49 -04001465 for (BasicBlock::Node& node : b.fNodes) {
1466 if (node.fKind == BasicBlock::Node::kStatement_Kind &&
John Stiles0cc193a2020-09-09 09:39:34 -04001467 !(*node.statement())->is<Nop>()) {
1468 node.setStatement(std::make_unique<Nop>());
1469 madeChanges = true;
Ethan Nicholas1de14812020-06-19 15:32:49 -04001470 }
1471 }
1472 continue;
1473 }
1474 first = false;
Ethan Nicholascb670962017-04-20 19:31:52 -04001475 DefinitionMap definitions = b.fBefore;
1476
1477 for (auto iter = b.fNodes.begin(); iter != b.fNodes.end() && !needsRescan; ++iter) {
1478 if (iter->fKind == BasicBlock::Node::kExpression_Kind) {
1479 this->simplifyExpression(definitions, b, &iter, &undefinedVariables, &updated,
1480 &needsRescan);
1481 } else {
1482 this->simplifyStatement(definitions, b, &iter, &undefinedVariables, &updated,
John Stiles0cc193a2020-09-09 09:39:34 -04001483 &needsRescan);
Ethan Nicholascb670962017-04-20 19:31:52 -04001484 }
Ethan Nicholas4b330df2017-05-17 10:52:55 -04001485 if (needsRescan) {
1486 break;
1487 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001488 this->addDefinitions(*iter, &definitions);
1489 }
Brian Osman01a3eb42020-09-14 11:32:49 -04001490
1491 if (needsRescan) {
1492 break;
1493 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001494 }
John Stiles0cc193a2020-09-09 09:39:34 -04001495 madeChanges |= updated;
Ethan Nicholascb670962017-04-20 19:31:52 -04001496 } while (updated);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001497 SkASSERT(!needsRescan);
ethannicholas22f939e2016-10-13 13:25:34 -07001498
Ethan Nicholas91a10532017-06-22 11:24:38 -04001499 // verify static ifs & switches, clean up dead variable decls
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001500 for (BasicBlock& b : cfg.fBlocks) {
1501 DefinitionMap definitions = b.fBefore;
1502
Ethan Nicholas91a10532017-06-22 11:24:38 -04001503 for (auto iter = b.fNodes.begin(); iter != b.fNodes.end() && !needsRescan;) {
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001504 if (iter->fKind == BasicBlock::Node::kStatement_Kind) {
1505 const Statement& s = **iter->statement();
Ethan Nicholase6592142020-09-08 10:22:09 -04001506 switch (s.kind()) {
1507 case Statement::Kind::kIf:
John Stilesa5a97b42020-08-18 11:19:07 -04001508 if (s.as<IfStatement>().fIsStatic &&
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -04001509 !(fFlags & kPermitInvalidStaticTests_Flag)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001510 this->error(s.fOffset, "static if has non-static test");
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001511 }
Ethan Nicholas91a10532017-06-22 11:24:38 -04001512 ++iter;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001513 break;
Ethan Nicholase6592142020-09-08 10:22:09 -04001514 case Statement::Kind::kSwitch:
John Stilesa5a97b42020-08-18 11:19:07 -04001515 if (s.as<SwitchStatement>().fIsStatic &&
John Stiles0cc193a2020-09-09 09:39:34 -04001516 !(fFlags & kPermitInvalidStaticTests_Flag)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001517 this->error(s.fOffset, "static switch has non-static test");
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001518 }
Ethan Nicholas91a10532017-06-22 11:24:38 -04001519 ++iter;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001520 break;
Ethan Nicholase6592142020-09-08 10:22:09 -04001521 case Statement::Kind::kVarDeclarations: {
John Stilesa5a97b42020-08-18 11:19:07 -04001522 VarDeclarations& decls = *s.as<VarDeclarationsStatement>().fDeclaration;
John Stiles0cc193a2020-09-09 09:39:34 -04001523 decls.fVars.erase(
1524 std::remove_if(decls.fVars.begin(), decls.fVars.end(),
1525 [&](const std::unique_ptr<Statement>& var) {
1526 bool nop = var->is<Nop>();
1527 madeChanges |= nop;
1528 return nop;
1529 }),
1530 decls.fVars.end());
1531 if (decls.fVars.empty()) {
Ethan Nicholas82a62d22017-11-07 14:42:10 +00001532 iter = b.fNodes.erase(iter);
1533 } else {
1534 ++iter;
1535 }
1536 break;
1537 }
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001538 default:
Ethan Nicholas91a10532017-06-22 11:24:38 -04001539 ++iter;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001540 break;
1541 }
Ethan Nicholas91a10532017-06-22 11:24:38 -04001542 } else {
1543 ++iter;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001544 }
1545 }
1546 }
1547
ethannicholas22f939e2016-10-13 13:25:34 -07001548 // check for missing return
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001549 if (f.fDeclaration.fReturnType != *fContext->fVoid_Type) {
ethannicholas22f939e2016-10-13 13:25:34 -07001550 if (cfg.fBlocks[cfg.fExit].fEntrances.size()) {
Ethan Nicholasdb80f692019-11-22 14:06:12 -05001551 this->error(f.fOffset, String("function '" + String(f.fDeclaration.fName) +
1552 "' can exit without returning a value"));
ethannicholas22f939e2016-10-13 13:25:34 -07001553 }
1554 }
John Stiles0cc193a2020-09-09 09:39:34 -04001555
1556 return madeChanges;
ethannicholas22f939e2016-10-13 13:25:34 -07001557}
1558
Brian Osman32d53552020-09-23 13:55:20 -04001559std::unique_ptr<Program> Compiler::convertProgram(
1560 Program::Kind kind,
1561 String text,
1562 const Program::Settings& settings,
1563 const std::vector<std::unique_ptr<ExternalValue>>* externalValues) {
1564 SkASSERT(!externalValues || (kind == Program::kGeneric_Kind));
Ethan Nicholas91164d12019-05-15 15:29:54 -04001565
ethannicholasb3058bd2016-07-01 08:22:01 -07001566 fErrorText = "";
1567 fErrorCount = 0;
John Stiles7b463002020-08-31 17:29:21 -04001568 fInliner.reset(context(), settings);
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001569 std::vector<std::unique_ptr<ProgramElement>>* inherited;
ethannicholasd598f792016-07-25 10:08:54 -07001570 std::vector<std::unique_ptr<ProgramElement>> elements;
ethannicholasb3058bd2016-07-01 08:22:01 -07001571 switch (kind) {
1572 case Program::kVertex_Kind:
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001573 inherited = &fVertexInclude;
John Stiles810c8cf2020-08-26 19:46:27 -04001574 fIRGenerator->fIntrinsics = fGPUIntrinsics.get();
Brian Osmane498b3c2020-09-23 14:42:11 -04001575 fIRGenerator->start(&settings, fVertexSymbolTable, inherited);
ethannicholasb3058bd2016-07-01 08:22:01 -07001576 break;
1577 case Program::kFragment_Kind:
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001578 inherited = &fFragmentInclude;
John Stiles810c8cf2020-08-26 19:46:27 -04001579 fIRGenerator->fIntrinsics = fGPUIntrinsics.get();
Brian Osmane498b3c2020-09-23 14:42:11 -04001580 fIRGenerator->start(&settings, fFragmentSymbolTable, inherited);
ethannicholasb3058bd2016-07-01 08:22:01 -07001581 break;
Ethan Nicholas52cad152017-02-16 16:37:32 -05001582 case Program::kGeometry_Kind:
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001583 this->loadGeometryIntrinsics();
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001584 inherited = &fGeometryInclude;
John Stiles810c8cf2020-08-26 19:46:27 -04001585 fIRGenerator->fIntrinsics = fGPUIntrinsics.get();
Brian Osmane498b3c2020-09-23 14:42:11 -04001586 fIRGenerator->start(&settings, fGeometrySymbolTable, inherited);
Ethan Nicholas52cad152017-02-16 16:37:32 -05001587 break;
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -04001588 case Program::kFragmentProcessor_Kind: {
Brian Osmandd496172020-08-08 08:17:18 -04001589#if !SKSL_STANDALONE
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001590 {
1591 Rehydrator rehydrator(fContext.get(), fGpuSymbolTable, this,
1592 SKSL_INCLUDE_sksl_fp,
1593 SKSL_INCLUDE_sksl_fp_LENGTH);
1594 fFPSymbolTable = rehydrator.symbolTable();
1595 fFPInclude = rehydrator.elements();
1596 }
Brian Osman2b469eb2020-09-21 11:32:10 -04001597 fFPIntrinsics = std::make_unique<IRIntrinsicMap>(fGPUIntrinsics.get());
1598 grab_intrinsics(&fFPInclude, fFPIntrinsics.get());
1599
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001600 inherited = &fFPInclude;
Brian Osman2b469eb2020-09-21 11:32:10 -04001601 fIRGenerator->fIntrinsics = fFPIntrinsics.get();
Brian Osmane498b3c2020-09-23 14:42:11 -04001602 fIRGenerator->start(&settings, fFPSymbolTable, inherited);
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001603 break;
1604#else
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001605 inherited = nullptr;
Brian Osmane498b3c2020-09-23 14:42:11 -04001606 fIRGenerator->start(&settings, fGpuSymbolTable, /*inherited=*/nullptr,
1607 /*builtin=*/true);
John Stiles810c8cf2020-08-26 19:46:27 -04001608 fIRGenerator->fIntrinsics = fGPUIntrinsics.get();
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -04001609 std::ifstream in(SKSL_FP_INCLUDE);
1610 std::string stdText{std::istreambuf_iterator<char>(in),
1611 std::istreambuf_iterator<char>()};
1612 if (in.rdstate()) {
1613 printf("error reading %s\n", SKSL_FP_INCLUDE);
1614 abort();
1615 }
Brian Osmane498b3c2020-09-23 14:42:11 -04001616 const String* source = fRootSymbolTable->takeOwnershipOfString(
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -04001617 std::make_unique<String>(stdText.c_str()));
1618 fIRGenerator->convertProgram(kind, source->c_str(), source->length(), &elements);
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001619 fIRGenerator->fIsBuiltinCode = false;
Ethan Nicholas762466e2017-06-29 10:03:38 -04001620 break;
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001621#endif
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -04001622 }
Ethan Nicholas8da1e652019-05-24 11:01:59 -04001623 case Program::kPipelineStage_Kind:
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001624 this->loadPipelineIntrinsics();
Ethan Nicholas8da1e652019-05-24 11:01:59 -04001625 inherited = &fPipelineInclude;
John Stiles810c8cf2020-08-26 19:46:27 -04001626 fIRGenerator->fIntrinsics = fGPUIntrinsics.get();
Brian Osmane498b3c2020-09-23 14:42:11 -04001627 fIRGenerator->start(&settings, fPipelineSymbolTable, inherited);
Ethan Nicholas8da1e652019-05-24 11:01:59 -04001628 break;
Ethan Nicholas746035a2019-04-23 13:31:09 -04001629 case Program::kGeneric_Kind:
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001630 this->loadInterpreterIntrinsics();
Brian Osmaneac49832020-09-18 11:49:22 -04001631 inherited = nullptr;
John Stiles810c8cf2020-08-26 19:46:27 -04001632 fIRGenerator->fIntrinsics = fInterpreterIntrinsics.get();
Brian Osmane498b3c2020-09-23 14:42:11 -04001633 fIRGenerator->start(&settings, fInterpreterSymbolTable, /*inherited=*/nullptr);
Ethan Nicholas0d997662019-04-08 09:46:01 -04001634 break;
ethannicholasb3058bd2016-07-01 08:22:01 -07001635 }
Brian Osman32d53552020-09-23 13:55:20 -04001636 if (externalValues) {
1637 // Add any external values to the symbol table. IRGenerator::start() has pushed a table, so
1638 // we're only making these visible to the current Program.
1639 for (const auto& ev : *externalValues) {
1640 fIRGenerator->fSymbolTable->addWithoutOwnership(ev->fName, ev.get());
1641 }
1642 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001643 std::unique_ptr<String> textPtr(new String(std::move(text)));
1644 fSource = textPtr.get();
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001645 fIRGenerator->convertProgram(kind, textPtr->c_str(), textPtr->size(), &elements);
John Stilesfbd050b2020-08-03 13:21:46 -04001646 auto result = std::make_unique<Program>(kind,
1647 std::move(textPtr),
1648 settings,
1649 fContext,
1650 inherited,
1651 std::move(elements),
1652 fIRGenerator->fSymbolTable,
1653 fIRGenerator->fInputs);
Brian Osmane498b3c2020-09-23 14:42:11 -04001654 fIRGenerator->finish();
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001655 if (fErrorCount) {
1656 return nullptr;
1657 }
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001658 if (settings.fOptimize && !this->optimize(*result)) {
1659 return nullptr;
1660 }
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001661 return result;
1662}
1663
Ethan Nicholas00543112018-07-31 09:44:36 -04001664bool Compiler::optimize(Program& program) {
1665 SkASSERT(!fErrorCount);
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001666 fIRGenerator->fKind = program.fKind;
1667 fIRGenerator->fSettings = &program.fSettings;
John Stiles7954d6c2020-09-01 10:53:02 -04001668
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001669 while (fErrorCount == 0) {
1670 bool madeChanges = false;
John Stiles7954d6c2020-09-01 10:53:02 -04001671
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001672 // Scan and optimize based on the control-flow graph for each function.
1673 for (ProgramElement& element : program) {
1674 if (element.is<FunctionDefinition>()) {
1675 madeChanges |= this->scanCFG(element.as<FunctionDefinition>());
1676 }
1677 }
1678
1679 // Perform inline-candidate analysis and inline any functions deemed suitable.
John Stiles881a10c2020-09-19 10:13:24 -04001680 madeChanges |= fInliner.analyze(program);
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001681
1682 // Remove dead functions. We wait until after analysis so that we still report errors,
1683 // even in unused code.
1684 if (program.fSettings.fRemoveDeadFunctions) {
1685 program.fElements.erase(
1686 std::remove_if(program.fElements.begin(),
1687 program.fElements.end(),
1688 [&](const std::unique_ptr<ProgramElement>& element) {
1689 if (!element->is<FunctionDefinition>()) {
1690 return false;
1691 }
1692 const auto& fn = element->as<FunctionDefinition>();
1693 bool dead = fn.fDeclaration.fCallCount == 0 &&
1694 fn.fDeclaration.fName != "main";
1695 madeChanges |= dead;
1696 return dead;
1697 }),
1698 program.fElements.end());
1699 }
1700
1701 if (program.fKind != Program::kFragmentProcessor_Kind) {
1702 // Remove dead variables.
John Stileseadfc3b2020-09-02 14:12:41 -04001703 for (ProgramElement& element : program) {
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001704 if (!element.is<VarDeclarations>()) {
1705 continue;
Ethan Nicholas0dc80872019-02-08 15:46:24 -05001706 }
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001707 VarDeclarations& vars = element.as<VarDeclarations>();
1708 vars.fVars.erase(
1709 std::remove_if(vars.fVars.begin(), vars.fVars.end(),
1710 [&](const std::unique_ptr<Statement>& stmt) {
1711 bool dead = stmt->as<VarDeclaration>().fVar->dead();
John Stiles73a6bff2020-09-09 13:40:37 -04001712 madeChanges |= dead;
1713 return dead;
1714 }),
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001715 vars.fVars.end());
John Stiles73a6bff2020-09-09 13:40:37 -04001716 }
1717
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001718 // Remove empty variable declarations with no variables left inside of them.
1719 program.fElements.erase(
1720 std::remove_if(program.fElements.begin(), program.fElements.end(),
1721 [&](const std::unique_ptr<ProgramElement>& element) {
1722 if (!element->is<VarDeclarations>()) {
1723 return false;
1724 }
1725 bool dead = element->as<VarDeclarations>().fVars.empty();
1726 madeChanges |= dead;
1727 return dead;
1728 }),
1729 program.fElements.end());
1730 }
John Stiles73a6bff2020-09-09 13:40:37 -04001731
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001732 if (!madeChanges) {
1733 break;
Ethan Nicholas0dc80872019-02-08 15:46:24 -05001734 }
Ethan Nicholas00543112018-07-31 09:44:36 -04001735 }
1736 return fErrorCount == 0;
1737}
1738
Brian Osmanfb32ddf2019-06-18 10:14:20 -04001739#if defined(SKSL_STANDALONE) || SK_SUPPORT_GPU
1740
Ethan Nicholas00543112018-07-31 09:44:36 -04001741bool Compiler::toSPIRV(Program& program, OutputStream& out) {
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001742#ifdef SK_ENABLE_SPIRV_VALIDATION
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001743 StringStream buffer;
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001744 fSource = program.fSource.get();
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001745 SPIRVCodeGenerator cg(fContext.get(), &program, this, &buffer);
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001746 bool result = cg.generateCode();
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001747 fSource = nullptr;
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001748 if (result) {
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001749 spvtools::SpirvTools tools(SPV_ENV_VULKAN_1_0);
Ethan Nicholas762466e2017-06-29 10:03:38 -04001750 const String& data = buffer.str();
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001751 SkASSERT(0 == data.size() % 4);
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001752 auto dumpmsg = [](spv_message_level_t, const char*, const spv_position_t&, const char* m) {
1753 SkDebugf("SPIR-V validation error: %s\n", m);
1754 };
1755 tools.SetMessageConsumer(dumpmsg);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001756 // Verify that the SPIR-V we produced is valid. If this SkASSERT fails, check the logs prior
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001757 // to the failure to see the validation errors.
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001758 SkAssertResult(tools.Validate((const uint32_t*) data.c_str(), data.size() / 4));
Ethan Nicholas762466e2017-06-29 10:03:38 -04001759 out.write(data.c_str(), data.size());
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001760 }
1761#else
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001762 fSource = program.fSource.get();
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001763 SPIRVCodeGenerator cg(fContext.get(), &program, this, &out);
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001764 bool result = cg.generateCode();
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001765 fSource = nullptr;
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001766#endif
Ethan Nicholasce33f102016-12-09 17:22:59 -05001767 return result;
1768}
1769
Ethan Nicholas00543112018-07-31 09:44:36 -04001770bool Compiler::toSPIRV(Program& program, String* out) {
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001771 StringStream buffer;
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001772 bool result = this->toSPIRV(program, buffer);
1773 if (result) {
Ethan Nicholas762466e2017-06-29 10:03:38 -04001774 *out = buffer.str();
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001775 }
1776 return result;
1777}
1778
Ethan Nicholas00543112018-07-31 09:44:36 -04001779bool Compiler::toGLSL(Program& program, OutputStream& out) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001780 fSource = program.fSource.get();
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001781 GLSLCodeGenerator cg(fContext.get(), &program, this, &out);
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001782 bool result = cg.generateCode();
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001783 fSource = nullptr;
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001784 return result;
1785}
1786
Ethan Nicholas00543112018-07-31 09:44:36 -04001787bool Compiler::toGLSL(Program& program, String* out) {
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001788 StringStream buffer;
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001789 bool result = this->toGLSL(program, buffer);
1790 if (result) {
Ethan Nicholas762466e2017-06-29 10:03:38 -04001791 *out = buffer.str();
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001792 }
1793 return result;
1794}
1795
Brian Osmanc0243912020-02-19 15:35:26 -05001796bool Compiler::toHLSL(Program& program, String* out) {
1797 String spirv;
1798 if (!this->toSPIRV(program, &spirv)) {
1799 return false;
1800 }
1801
1802 return SPIRVtoHLSL(spirv, out);
1803}
1804
Ethan Nicholas00543112018-07-31 09:44:36 -04001805bool Compiler::toMetal(Program& program, OutputStream& out) {
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001806 MetalCodeGenerator cg(fContext.get(), &program, this, &out);
Ethan Nicholascc305772017-10-13 16:17:45 -04001807 bool result = cg.generateCode();
Ethan Nicholascc305772017-10-13 16:17:45 -04001808 return result;
1809}
1810
Ethan Nicholas00543112018-07-31 09:44:36 -04001811bool Compiler::toMetal(Program& program, String* out) {
Timothy Liangb8eeb802018-07-23 16:46:16 -04001812 StringStream buffer;
1813 bool result = this->toMetal(program, buffer);
1814 if (result) {
1815 *out = buffer.str();
1816 }
1817 return result;
1818}
1819
Ethan Nicholas2a479a52020-08-18 16:29:45 -04001820#if defined(SKSL_STANDALONE) || defined(GR_TEST_UTILS)
Ethan Nicholas00543112018-07-31 09:44:36 -04001821bool Compiler::toCPP(Program& program, String name, OutputStream& out) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001822 fSource = program.fSource.get();
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001823 CPPCodeGenerator cg(fContext.get(), &program, this, name, &out);
Ethan Nicholas762466e2017-06-29 10:03:38 -04001824 bool result = cg.generateCode();
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001825 fSource = nullptr;
Ethan Nicholas762466e2017-06-29 10:03:38 -04001826 return result;
1827}
1828
Ethan Nicholas00543112018-07-31 09:44:36 -04001829bool Compiler::toH(Program& program, String name, OutputStream& out) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001830 fSource = program.fSource.get();
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001831 HCodeGenerator cg(fContext.get(), &program, this, name, &out);
Ethan Nicholas762466e2017-06-29 10:03:38 -04001832 bool result = cg.generateCode();
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001833 fSource = nullptr;
Ethan Nicholas00543112018-07-31 09:44:36 -04001834 return result;
1835}
Ethan Nicholas2a479a52020-08-18 16:29:45 -04001836#endif // defined(SKSL_STANDALONE) || defined(GR_TEST_UTILS)
Ethan Nicholas00543112018-07-31 09:44:36 -04001837
Ethan Nicholas2a479a52020-08-18 16:29:45 -04001838#endif // defined(SKSL_STANDALONE) || SK_SUPPORT_GPU
Brian Osman2e29ab52019-09-20 12:19:11 -04001839
1840#if !defined(SKSL_STANDALONE) && SK_SUPPORT_GPU
Brian Osmana4b91692020-08-10 14:26:16 -04001841bool Compiler::toPipelineStage(Program& program, PipelineStageArgs* outArgs) {
Ethan Nicholas00543112018-07-31 09:44:36 -04001842 fSource = program.fSource.get();
1843 StringStream buffer;
Brian Osman300fe1d2020-01-23 15:42:43 -05001844 PipelineStageCodeGenerator cg(fContext.get(), &program, this, &buffer, outArgs);
Ethan Nicholas00543112018-07-31 09:44:36 -04001845 bool result = cg.generateCode();
1846 fSource = nullptr;
1847 if (result) {
Brian Osman107c6662019-12-30 15:02:30 -05001848 outArgs->fCode = buffer.str();
Ethan Nicholas00543112018-07-31 09:44:36 -04001849 }
Ethan Nicholas762466e2017-06-29 10:03:38 -04001850 return result;
1851}
Brian Osmanfb32ddf2019-06-18 10:14:20 -04001852#endif
1853
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001854std::unique_ptr<ByteCode> Compiler::toByteCode(Program& program) {
Mike Klein853d4ed2020-08-20 00:41:00 +00001855#if defined(SK_ENABLE_SKSL_INTERPRETER)
Brian Osman808f0212020-01-21 15:36:47 -05001856 fSource = program.fSource.get();
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001857 std::unique_ptr<ByteCode> result(new ByteCode());
Brian Osmanb08cc022020-04-02 11:38:40 -04001858 ByteCodeGenerator cg(fContext.get(), &program, this, result.get());
1859 bool success = cg.generateCode();
1860 fSource = nullptr;
1861 if (success) {
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001862 return result;
1863 }
Mike Klein853d4ed2020-08-20 00:41:00 +00001864#else
1865 ABORT("ByteCode interpreter not enabled");
1866#endif
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001867 return nullptr;
1868}
1869
Brian Osman401a0092020-09-10 14:47:24 -04001870const char* Compiler::OperatorName(Token::Kind op) {
1871 switch (op) {
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001872 case Token::Kind::TK_PLUS: return "+";
1873 case Token::Kind::TK_MINUS: return "-";
1874 case Token::Kind::TK_STAR: return "*";
1875 case Token::Kind::TK_SLASH: return "/";
1876 case Token::Kind::TK_PERCENT: return "%";
1877 case Token::Kind::TK_SHL: return "<<";
1878 case Token::Kind::TK_SHR: return ">>";
1879 case Token::Kind::TK_LOGICALNOT: return "!";
1880 case Token::Kind::TK_LOGICALAND: return "&&";
1881 case Token::Kind::TK_LOGICALOR: return "||";
1882 case Token::Kind::TK_LOGICALXOR: return "^^";
1883 case Token::Kind::TK_BITWISENOT: return "~";
1884 case Token::Kind::TK_BITWISEAND: return "&";
1885 case Token::Kind::TK_BITWISEOR: return "|";
1886 case Token::Kind::TK_BITWISEXOR: return "^";
1887 case Token::Kind::TK_EQ: return "=";
1888 case Token::Kind::TK_EQEQ: return "==";
1889 case Token::Kind::TK_NEQ: return "!=";
1890 case Token::Kind::TK_LT: return "<";
1891 case Token::Kind::TK_GT: return ">";
1892 case Token::Kind::TK_LTEQ: return "<=";
1893 case Token::Kind::TK_GTEQ: return ">=";
1894 case Token::Kind::TK_PLUSEQ: return "+=";
1895 case Token::Kind::TK_MINUSEQ: return "-=";
1896 case Token::Kind::TK_STAREQ: return "*=";
1897 case Token::Kind::TK_SLASHEQ: return "/=";
1898 case Token::Kind::TK_PERCENTEQ: return "%=";
1899 case Token::Kind::TK_SHLEQ: return "<<=";
1900 case Token::Kind::TK_SHREQ: return ">>=";
1901 case Token::Kind::TK_LOGICALANDEQ: return "&&=";
1902 case Token::Kind::TK_LOGICALOREQ: return "||=";
1903 case Token::Kind::TK_LOGICALXOREQ: return "^^=";
1904 case Token::Kind::TK_BITWISEANDEQ: return "&=";
1905 case Token::Kind::TK_BITWISEOREQ: return "|=";
1906 case Token::Kind::TK_BITWISEXOREQ: return "^=";
1907 case Token::Kind::TK_PLUSPLUS: return "++";
1908 case Token::Kind::TK_MINUSMINUS: return "--";
1909 case Token::Kind::TK_COMMA: return ",";
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001910 default:
Brian Osman401a0092020-09-10 14:47:24 -04001911 ABORT("unsupported operator: %d\n", (int) op);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001912 }
1913}
1914
1915
1916bool Compiler::IsAssignment(Token::Kind op) {
1917 switch (op) {
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001918 case Token::Kind::TK_EQ: // fall through
1919 case Token::Kind::TK_PLUSEQ: // fall through
1920 case Token::Kind::TK_MINUSEQ: // fall through
1921 case Token::Kind::TK_STAREQ: // fall through
1922 case Token::Kind::TK_SLASHEQ: // fall through
1923 case Token::Kind::TK_PERCENTEQ: // fall through
1924 case Token::Kind::TK_SHLEQ: // fall through
1925 case Token::Kind::TK_SHREQ: // fall through
1926 case Token::Kind::TK_BITWISEOREQ: // fall through
1927 case Token::Kind::TK_BITWISEXOREQ: // fall through
1928 case Token::Kind::TK_BITWISEANDEQ: // fall through
1929 case Token::Kind::TK_LOGICALOREQ: // fall through
1930 case Token::Kind::TK_LOGICALXOREQ: // fall through
1931 case Token::Kind::TK_LOGICALANDEQ:
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001932 return true;
1933 default:
1934 return false;
1935 }
1936}
1937
Brian Osman401a0092020-09-10 14:47:24 -04001938Token::Kind Compiler::RemoveAssignment(Token::Kind op) {
1939 switch (op) {
1940 case Token::Kind::TK_PLUSEQ: return Token::Kind::TK_PLUS;
1941 case Token::Kind::TK_MINUSEQ: return Token::Kind::TK_MINUS;
1942 case Token::Kind::TK_STAREQ: return Token::Kind::TK_STAR;
1943 case Token::Kind::TK_SLASHEQ: return Token::Kind::TK_SLASH;
1944 case Token::Kind::TK_PERCENTEQ: return Token::Kind::TK_PERCENT;
1945 case Token::Kind::TK_SHLEQ: return Token::Kind::TK_SHL;
1946 case Token::Kind::TK_SHREQ: return Token::Kind::TK_SHR;
1947 case Token::Kind::TK_BITWISEOREQ: return Token::Kind::TK_BITWISEOR;
1948 case Token::Kind::TK_BITWISEXOREQ: return Token::Kind::TK_BITWISEXOR;
1949 case Token::Kind::TK_BITWISEANDEQ: return Token::Kind::TK_BITWISEAND;
1950 case Token::Kind::TK_LOGICALOREQ: return Token::Kind::TK_LOGICALOR;
1951 case Token::Kind::TK_LOGICALXOREQ: return Token::Kind::TK_LOGICALXOR;
1952 case Token::Kind::TK_LOGICALANDEQ: return Token::Kind::TK_LOGICALAND;
1953 default: return op;
1954 }
1955}
1956
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001957Position Compiler::position(int offset) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001958 SkASSERT(fSource);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001959 int line = 1;
1960 int column = 1;
1961 for (int i = 0; i < offset; i++) {
1962 if ((*fSource)[i] == '\n') {
1963 ++line;
1964 column = 1;
1965 }
1966 else {
1967 ++column;
1968 }
1969 }
1970 return Position(line, column);
1971}
1972
1973void Compiler::error(int offset, String msg) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001974 fErrorCount++;
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001975 Position pos = this->position(offset);
1976 fErrorText += "error: " + to_string(pos.fLine) + ": " + msg.c_str() + "\n";
ethannicholasb3058bd2016-07-01 08:22:01 -07001977}
1978
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001979String Compiler::errorText() {
Ethan Nicholas00543112018-07-31 09:44:36 -04001980 this->writeErrorCount();
1981 fErrorCount = 0;
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001982 String result = fErrorText;
ethannicholasb3058bd2016-07-01 08:22:01 -07001983 return result;
1984}
1985
1986void Compiler::writeErrorCount() {
1987 if (fErrorCount) {
1988 fErrorText += to_string(fErrorCount) + " error";
1989 if (fErrorCount > 1) {
1990 fErrorText += "s";
1991 }
1992 fErrorText += "\n";
1993 }
1994}
1995
John Stilesa6841be2020-08-06 14:11:56 -04001996} // namespace SkSL