blob: 55f5c2535906d3a3d7556e7ccdb582f8a3131f7c [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"
John Stilese6150002020-10-05 12:03:53 -040036#include "src/utils/SkBitSet.h"
ethannicholasb3058bd2016-07-01 08:22:01 -070037
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -040038#include <fstream>
39
Ethan Nicholasa11035b2019-11-26 16:27:47 -050040#if !defined(SKSL_STANDALONE) & SK_SUPPORT_GPU
41#include "include/gpu/GrContextOptions.h"
42#include "src/gpu/GrShaderCaps.h"
43#endif
44
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -040045#ifdef SK_ENABLE_SPIRV_VALIDATION
46#include "spirv-tools/libspirv.hpp"
47#endif
48
Brian Osmandd496172020-08-08 08:17:18 -040049#if !SKSL_STANDALONE
Ethan Nicholasc18bb512020-07-28 14:46:53 -040050
51#include "src/sksl/generated/sksl_fp.dehydrated.sksl"
52#include "src/sksl/generated/sksl_frag.dehydrated.sksl"
53#include "src/sksl/generated/sksl_geom.dehydrated.sksl"
54#include "src/sksl/generated/sksl_gpu.dehydrated.sksl"
55#include "src/sksl/generated/sksl_interp.dehydrated.sksl"
56#include "src/sksl/generated/sksl_pipeline.dehydrated.sksl"
57#include "src/sksl/generated/sksl_vert.dehydrated.sksl"
58
59#else
60
Brian Osmandd496172020-08-08 08:17:18 -040061// GN generates or copies all of these files to the skslc executable directory
62static const char SKSL_GPU_INCLUDE[] = "sksl_gpu.sksl";
63static const char SKSL_INTERP_INCLUDE[] = "sksl_interp.sksl";
64static const char SKSL_VERT_INCLUDE[] = "sksl_vert.sksl";
65static const char SKSL_FRAG_INCLUDE[] = "sksl_frag.sksl";
66static const char SKSL_GEOM_INCLUDE[] = "sksl_geom.sksl";
67static const char SKSL_FP_INCLUDE[] = "sksl_fp.sksl";
68static const char SKSL_PIPELINE_INCLUDE[] = "sksl_pipeline.sksl";
Ethan Nicholasc18bb512020-07-28 14:46:53 -040069
70#endif
Ethan Nicholas0d997662019-04-08 09:46:01 -040071
ethannicholasb3058bd2016-07-01 08:22:01 -070072namespace SkSL {
73
Ethan Nicholasdb80f692019-11-22 14:06:12 -050074static void grab_intrinsics(std::vector<std::unique_ptr<ProgramElement>>* src,
John Stiles810c8cf2020-08-26 19:46:27 -040075 IRIntrinsicMap* target) {
Brian Osman08f986d2020-05-13 17:06:46 -040076 for (auto iter = src->begin(); iter != src->end(); ) {
77 std::unique_ptr<ProgramElement>& element = *iter;
Ethan Nicholase6592142020-09-08 10:22:09 -040078 switch (element->kind()) {
79 case ProgramElement::Kind::kFunction: {
John Stiles3dc0da62020-08-19 17:48:31 -040080 FunctionDefinition& f = element->as<FunctionDefinition>();
Brian Osman08f986d2020-05-13 17:06:46 -040081 SkASSERT(f.fDeclaration.fBuiltin);
Brian Osman2b469eb2020-09-21 11:32:10 -040082 target->insertOrDie(f.fDeclaration.description(), std::move(element));
Brian Osman08f986d2020-05-13 17:06:46 -040083 iter = src->erase(iter);
Ethan Nicholasdb80f692019-11-22 14:06:12 -050084 break;
85 }
Ethan Nicholase6592142020-09-08 10:22:09 -040086 case ProgramElement::Kind::kEnum: {
John Stiles3dc0da62020-08-19 17:48:31 -040087 Enum& e = element->as<Enum>();
Ethan Nicholasd83ded82020-09-29 17:05:54 -040088 target->insertOrDie(e.typeName(), std::move(element));
Brian Osman08f986d2020-05-13 17:06:46 -040089 iter = src->erase(iter);
Ethan Nicholasdb80f692019-11-22 14:06:12 -050090 break;
91 }
Brian Osmanc0213602020-10-06 14:43:32 -040092 case ProgramElement::Kind::kGlobalVar: {
93 const GlobalVarDeclaration& vd = element->as<GlobalVarDeclaration>();
94 const Variable* var = vd.fDecl->fVar;
Ethan Nicholase2c49992020-10-05 11:49:11 -040095 target->insertOrDie(var->name(), std::move(element));
Brian Osman8e2ef022020-09-30 13:26:43 -040096 iter = src->erase(iter);
97 break;
98 }
Ethan Nicholasdb80f692019-11-22 14:06:12 -050099 default:
Brian Osman00a8b5b2020-10-02 09:06:04 -0400100 printf("Unsupported element: %s\n", element->description().c_str());
101 SkASSERT(false);
Brian Osman2b469eb2020-09-21 11:32:10 -0400102 break;
Ethan Nicholasdb80f692019-11-22 14:06:12 -0500103 }
104 }
105}
106
John Stilesbc0c29e2020-09-28 13:13:40 -0400107static void reset_call_counts(std::vector<std::unique_ptr<ProgramElement>>* src) {
108 for (std::unique_ptr<ProgramElement>& element : *src) {
109 if (element->is<FunctionDefinition>()) {
110 const FunctionDeclaration& fnDecl = element->as<FunctionDefinition>().fDeclaration;
111 fnDecl.fCallCount = 0;
112 }
113 }
114}
115
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -0400116Compiler::Compiler(Flags flags)
Brian Osman00a8b5b2020-10-02 09:06:04 -0400117: fFlags(flags)
John Stiles810c8cf2020-08-26 19:46:27 -0400118, fContext(std::make_shared<Context>())
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -0400119, fErrorCount(0) {
Brian Osmaneac49832020-09-18 11:49:22 -0400120 fRootSymbolTable = std::make_shared<SymbolTable>(this);
John Stiles941fc712020-09-19 12:47:10 +0000121 fIRGenerator =
122 std::make_unique<IRGenerator>(fContext.get(), &fInliner, fRootSymbolTable, *this);
Ethan Nicholase2c49992020-10-05 11:49:11 -0400123 #define ADD_TYPE(t) fRootSymbolTable->addWithoutOwnership(fContext->f ## t ## _Type->name(), \
Brian Osmaneac49832020-09-18 11:49:22 -0400124 fContext->f ## t ## _Type.get())
ethannicholasb3058bd2016-07-01 08:22:01 -0700125 ADD_TYPE(Void);
126 ADD_TYPE(Float);
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400127 ADD_TYPE(Float2);
128 ADD_TYPE(Float3);
129 ADD_TYPE(Float4);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400130 ADD_TYPE(Half);
131 ADD_TYPE(Half2);
132 ADD_TYPE(Half3);
133 ADD_TYPE(Half4);
ethannicholasb3058bd2016-07-01 08:22:01 -0700134 ADD_TYPE(Int);
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400135 ADD_TYPE(Int2);
136 ADD_TYPE(Int3);
137 ADD_TYPE(Int4);
ethannicholasb3058bd2016-07-01 08:22:01 -0700138 ADD_TYPE(UInt);
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400139 ADD_TYPE(UInt2);
140 ADD_TYPE(UInt3);
141 ADD_TYPE(UInt4);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400142 ADD_TYPE(Short);
143 ADD_TYPE(Short2);
144 ADD_TYPE(Short3);
145 ADD_TYPE(Short4);
146 ADD_TYPE(UShort);
147 ADD_TYPE(UShort2);
148 ADD_TYPE(UShort3);
149 ADD_TYPE(UShort4);
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400150 ADD_TYPE(Byte);
151 ADD_TYPE(Byte2);
152 ADD_TYPE(Byte3);
153 ADD_TYPE(Byte4);
154 ADD_TYPE(UByte);
155 ADD_TYPE(UByte2);
156 ADD_TYPE(UByte3);
157 ADD_TYPE(UByte4);
ethannicholasb3058bd2016-07-01 08:22:01 -0700158 ADD_TYPE(Bool);
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400159 ADD_TYPE(Bool2);
160 ADD_TYPE(Bool3);
161 ADD_TYPE(Bool4);
162 ADD_TYPE(Float2x2);
163 ADD_TYPE(Float2x3);
164 ADD_TYPE(Float2x4);
165 ADD_TYPE(Float3x2);
166 ADD_TYPE(Float3x3);
167 ADD_TYPE(Float3x4);
168 ADD_TYPE(Float4x2);
169 ADD_TYPE(Float4x3);
170 ADD_TYPE(Float4x4);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400171 ADD_TYPE(Half2x2);
172 ADD_TYPE(Half2x3);
173 ADD_TYPE(Half2x4);
174 ADD_TYPE(Half3x2);
175 ADD_TYPE(Half3x3);
176 ADD_TYPE(Half3x4);
177 ADD_TYPE(Half4x2);
178 ADD_TYPE(Half4x3);
179 ADD_TYPE(Half4x4);
ethannicholasb3058bd2016-07-01 08:22:01 -0700180 ADD_TYPE(GenType);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400181 ADD_TYPE(GenHType);
ethannicholasb3058bd2016-07-01 08:22:01 -0700182 ADD_TYPE(GenIType);
183 ADD_TYPE(GenUType);
184 ADD_TYPE(GenBType);
185 ADD_TYPE(Mat);
186 ADD_TYPE(Vec);
187 ADD_TYPE(GVec);
188 ADD_TYPE(GVec2);
189 ADD_TYPE(GVec3);
190 ADD_TYPE(GVec4);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400191 ADD_TYPE(HVec);
ethannicholasb3058bd2016-07-01 08:22:01 -0700192 ADD_TYPE(IVec);
193 ADD_TYPE(UVec);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400194 ADD_TYPE(SVec);
195 ADD_TYPE(USVec);
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400196 ADD_TYPE(ByteVec);
197 ADD_TYPE(UByteVec);
ethannicholasb3058bd2016-07-01 08:22:01 -0700198 ADD_TYPE(BVec);
199
200 ADD_TYPE(Sampler1D);
201 ADD_TYPE(Sampler2D);
202 ADD_TYPE(Sampler3D);
ethannicholas5961bc92016-10-12 06:39:56 -0700203 ADD_TYPE(SamplerExternalOES);
ethannicholasb3058bd2016-07-01 08:22:01 -0700204 ADD_TYPE(SamplerCube);
205 ADD_TYPE(Sampler2DRect);
206 ADD_TYPE(Sampler1DArray);
207 ADD_TYPE(Sampler2DArray);
208 ADD_TYPE(SamplerCubeArray);
209 ADD_TYPE(SamplerBuffer);
210 ADD_TYPE(Sampler2DMS);
211 ADD_TYPE(Sampler2DMSArray);
212
Brian Salomonbf7b6202016-11-11 16:08:03 -0500213 ADD_TYPE(ISampler2D);
214
Brian Salomon2a51de82016-11-16 12:06:01 -0500215 ADD_TYPE(Image2D);
216 ADD_TYPE(IImage2D);
217
Greg Daniel64773e62016-11-22 09:44:03 -0500218 ADD_TYPE(SubpassInput);
219 ADD_TYPE(SubpassInputMS);
220
ethannicholasb3058bd2016-07-01 08:22:01 -0700221 ADD_TYPE(GSampler1D);
222 ADD_TYPE(GSampler2D);
223 ADD_TYPE(GSampler3D);
224 ADD_TYPE(GSamplerCube);
225 ADD_TYPE(GSampler2DRect);
226 ADD_TYPE(GSampler1DArray);
227 ADD_TYPE(GSampler2DArray);
228 ADD_TYPE(GSamplerCubeArray);
229 ADD_TYPE(GSamplerBuffer);
230 ADD_TYPE(GSampler2DMS);
231 ADD_TYPE(GSampler2DMSArray);
232
233 ADD_TYPE(Sampler1DShadow);
234 ADD_TYPE(Sampler2DShadow);
235 ADD_TYPE(SamplerCubeShadow);
236 ADD_TYPE(Sampler2DRectShadow);
237 ADD_TYPE(Sampler1DArrayShadow);
238 ADD_TYPE(Sampler2DArrayShadow);
239 ADD_TYPE(SamplerCubeArrayShadow);
240 ADD_TYPE(GSampler2DArrayShadow);
241 ADD_TYPE(GSamplerCubeArrayShadow);
Ethan Nicholasc9472af2017-10-10 16:30:21 -0400242 ADD_TYPE(FragmentProcessor);
Stephen Whiteff5d7a22019-07-26 17:42:06 -0400243 ADD_TYPE(Sampler);
244 ADD_TYPE(Texture2D);
ethannicholasb3058bd2016-07-01 08:22:01 -0700245
Brian Osman28590d52020-03-23 16:59:08 -0400246 StringFragment fpAliasName("shader");
John Stiles49a547f2020-10-06 16:14:37 -0400247 fRootSymbolTable->addAlias(fpAliasName, fContext->fFragmentProcessor_Type.get());
Brian Osman28590d52020-03-23 16:59:08 -0400248
Brian Osman3887a012020-09-30 13:22:27 -0400249 // sk_Caps is "builtin", but all references to it are resolved to Settings, so we don't need to
250 // treat it as builtin (ie, no need to clone it into the Program).
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700251 StringFragment skCapsName("sk_Caps");
Brian Osman3887a012020-09-30 13:22:27 -0400252 fRootSymbolTable->add(skCapsName,
253 std::make_unique<Variable>(/*offset=*/-1, Modifiers(), skCapsName,
254 fContext->fSkCaps_Type.get(),
255 /*builtin=*/false, Variable::kGlobal_Storage));
Ethan Nicholas3605ace2016-11-21 15:59:48 -0500256
Brian Osman00a8b5b2020-10-02 09:06:04 -0400257 fIRGenerator->fIntrinsics = nullptr;
258 std::vector<std::unique_ptr<ProgramElement>> gpuElements;
259 std::vector<std::unique_ptr<ProgramElement>> fragElements;
Brian Osmandd496172020-08-08 08:17:18 -0400260#if SKSL_STANDALONE
Brian Osmaneac49832020-09-18 11:49:22 -0400261 this->processIncludeFile(Program::kFragment_Kind, SKSL_GPU_INCLUDE, fRootSymbolTable,
Brian Osman00a8b5b2020-10-02 09:06:04 -0400262 &gpuElements, &fGpuSymbolTable);
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -0400263 this->processIncludeFile(Program::kVertex_Kind, SKSL_VERT_INCLUDE, fGpuSymbolTable,
264 &fVertexInclude, &fVertexSymbolTable);
265 this->processIncludeFile(Program::kFragment_Kind, SKSL_FRAG_INCLUDE, fGpuSymbolTable,
Brian Osman00a8b5b2020-10-02 09:06:04 -0400266 &fragElements, &fFragmentSymbolTable);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400267#else
268 {
Brian Osmaneac49832020-09-18 11:49:22 -0400269 Rehydrator rehydrator(fContext.get(), fRootSymbolTable, this, SKSL_INCLUDE_sksl_gpu,
270 SKSL_INCLUDE_sksl_gpu_LENGTH);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400271 fGpuSymbolTable = rehydrator.symbolTable();
Brian Osman00a8b5b2020-10-02 09:06:04 -0400272 gpuElements = rehydrator.elements();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400273 }
274 {
275 Rehydrator rehydrator(fContext.get(), fGpuSymbolTable, this, SKSL_INCLUDE_sksl_vert,
Brian Osmaneac49832020-09-18 11:49:22 -0400276 SKSL_INCLUDE_sksl_vert_LENGTH);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400277 fVertexSymbolTable = rehydrator.symbolTable();
278 fVertexInclude = rehydrator.elements();
279 }
280 {
281 Rehydrator rehydrator(fContext.get(), fGpuSymbolTable, this, SKSL_INCLUDE_sksl_frag,
Brian Osmaneac49832020-09-18 11:49:22 -0400282 SKSL_INCLUDE_sksl_frag_LENGTH);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400283 fFragmentSymbolTable = rehydrator.symbolTable();
Brian Osman00a8b5b2020-10-02 09:06:04 -0400284 fragElements = rehydrator.elements();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400285 }
286#endif
John Stilesbc0c29e2020-09-28 13:13:40 -0400287 // Call counts are used to track dead-stripping and inlinability within the program being
288 // currently compiled, and always should start at zero for a new program. Zero out any call
289 // counts that were registered during the assembly of the intrinsics/include data. (If we
290 // actually use calls from inside the intrinsics, we will clone them into the program and they
291 // will get new call counts.)
Brian Osman00a8b5b2020-10-02 09:06:04 -0400292 reset_call_counts(&gpuElements);
John Stilesbc0c29e2020-09-28 13:13:40 -0400293 reset_call_counts(&fVertexInclude);
Brian Osman00a8b5b2020-10-02 09:06:04 -0400294 reset_call_counts(&fragElements);
John Stilesbc0c29e2020-09-28 13:13:40 -0400295
Brian Osman00a8b5b2020-10-02 09:06:04 -0400296 fGPUIntrinsics = std::make_unique<IRIntrinsicMap>(/*parent=*/nullptr);
297 grab_intrinsics(&gpuElements, fGPUIntrinsics.get());
298
299 fFragmentIntrinsics = std::make_unique<IRIntrinsicMap>(fGPUIntrinsics.get());
300 grab_intrinsics(&fragElements, fFragmentIntrinsics.get());
ethannicholasb3058bd2016-07-01 08:22:01 -0700301}
302
John Stiles656427a2020-08-27 15:26:26 -0400303Compiler::~Compiler() {}
ethannicholasb3058bd2016-07-01 08:22:01 -0700304
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400305void Compiler::loadGeometryIntrinsics() {
306 if (fGeometrySymbolTable) {
307 return;
308 }
Brian Osmandd496172020-08-08 08:17:18 -0400309 #if !SKSL_STANDALONE
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400310 {
311 Rehydrator rehydrator(fContext.get(), fGpuSymbolTable, this, SKSL_INCLUDE_sksl_geom,
312 SKSL_INCLUDE_sksl_geom_LENGTH);
313 fGeometrySymbolTable = rehydrator.symbolTable();
314 fGeometryInclude = rehydrator.elements();
315 }
316 #else
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -0400317 this->processIncludeFile(Program::kGeometry_Kind, SKSL_GEOM_INCLUDE, fGpuSymbolTable,
318 &fGeometryInclude, &fGeometrySymbolTable);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400319 #endif
320}
321
Brian Osman8e2ef022020-09-30 13:26:43 -0400322void Compiler::loadFPIntrinsics() {
323 if (fFPSymbolTable) {
324 return;
325 }
326 fFPIntrinsics = std::make_unique<IRIntrinsicMap>(fGPUIntrinsics.get());
327 std::vector<std::unique_ptr<ProgramElement>> fpElements;
328 #if !SKSL_STANDALONE
329 {
330 Rehydrator rehydrator(fContext.get(), fGpuSymbolTable, this, SKSL_INCLUDE_sksl_fp,
331 SKSL_INCLUDE_sksl_fp_LENGTH);
332 fFPSymbolTable = rehydrator.symbolTable();
333 fpElements = rehydrator.elements();
334 }
335 #else
336 this->processIncludeFile(Program::kFragmentProcessor_Kind, SKSL_FP_INCLUDE, fGpuSymbolTable,
337 &fpElements, &fFPSymbolTable);
338 #endif
339 grab_intrinsics(&fpElements, fFPIntrinsics.get());
Brian Osman8e2ef022020-09-30 13:26:43 -0400340}
341
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400342void Compiler::loadPipelineIntrinsics() {
343 if (fPipelineSymbolTable) {
344 return;
345 }
Brian Osman00a8b5b2020-10-02 09:06:04 -0400346 fPipelineIntrinsics = std::make_unique<IRIntrinsicMap>(fGPUIntrinsics.get());
Brian Osman8e2ef022020-09-30 13:26:43 -0400347 std::vector<std::unique_ptr<ProgramElement>> pipelineIntrinics;
Brian Osmandd496172020-08-08 08:17:18 -0400348 #if !SKSL_STANDALONE
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400349 {
350 Rehydrator rehydrator(fContext.get(), fGpuSymbolTable, this,
351 SKSL_INCLUDE_sksl_pipeline,
352 SKSL_INCLUDE_sksl_pipeline_LENGTH);
353 fPipelineSymbolTable = rehydrator.symbolTable();
Brian Osman8e2ef022020-09-30 13:26:43 -0400354 pipelineIntrinics = rehydrator.elements();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400355 }
356 #else
357 this->processIncludeFile(Program::kPipelineStage_Kind, SKSL_PIPELINE_INCLUDE,
Brian Osman8e2ef022020-09-30 13:26:43 -0400358 fGpuSymbolTable, &pipelineIntrinics, &fPipelineSymbolTable);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400359 #endif
Brian Osman8e2ef022020-09-30 13:26:43 -0400360 grab_intrinsics(&pipelineIntrinics, fPipelineIntrinsics.get());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400361}
362
363void Compiler::loadInterpreterIntrinsics() {
364 if (fInterpreterSymbolTable) {
365 return;
366 }
Brian Osman00a8b5b2020-10-02 09:06:04 -0400367 fInterpreterIntrinsics = std::make_unique<IRIntrinsicMap>(/*parent=*/nullptr);
368 std::vector<std::unique_ptr<ProgramElement>> interpElements;
Brian Osmandd496172020-08-08 08:17:18 -0400369 #if !SKSL_STANDALONE
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400370 {
Brian Osmaneac49832020-09-18 11:49:22 -0400371 Rehydrator rehydrator(fContext.get(), fRootSymbolTable, this,
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400372 SKSL_INCLUDE_sksl_interp,
373 SKSL_INCLUDE_sksl_interp_LENGTH);
374 fInterpreterSymbolTable = rehydrator.symbolTable();
Brian Osman00a8b5b2020-10-02 09:06:04 -0400375 interpElements = rehydrator.elements();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400376 }
377 #else
378 this->processIncludeFile(Program::kGeneric_Kind, SKSL_INTERP_INCLUDE,
Brian Osman00a8b5b2020-10-02 09:06:04 -0400379 fIRGenerator->fSymbolTable, &interpElements,
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -0400380 &fInterpreterSymbolTable);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400381 #endif
Brian Osman00a8b5b2020-10-02 09:06:04 -0400382 grab_intrinsics(&interpElements, fInterpreterIntrinsics.get());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400383}
384
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -0400385void Compiler::processIncludeFile(Program::Kind kind, const char* path,
Ethan Nicholas8da1e652019-05-24 11:01:59 -0400386 std::shared_ptr<SymbolTable> base,
387 std::vector<std::unique_ptr<ProgramElement>>* outElements,
388 std::shared_ptr<SymbolTable>* outSymbolTable) {
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -0400389 std::ifstream in(path);
Brian Osmane498b3c2020-09-23 14:42:11 -0400390 std::unique_ptr<String> text = std::make_unique<String>(std::istreambuf_iterator<char>(in),
391 std::istreambuf_iterator<char>());
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -0400392 if (in.rdstate()) {
393 printf("error reading %s\n", path);
394 abort();
395 }
Brian Osmane498b3c2020-09-23 14:42:11 -0400396 const String* source = fRootSymbolTable->takeOwnershipOfString(std::move(text));
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -0400397 fSource = source;
Ethan Nicholas8da1e652019-05-24 11:01:59 -0400398 Program::Settings settings;
Ethan Nicholasa11035b2019-11-26 16:27:47 -0500399#if !defined(SKSL_STANDALONE) & SK_SUPPORT_GPU
400 GrContextOptions opts;
401 GrShaderCaps caps(opts);
402 settings.fCaps = &caps;
403#endif
John Stiles881a10c2020-09-19 10:13:24 -0400404 SkASSERT(fIRGenerator->fCanInline);
405 fIRGenerator->fCanInline = false;
Brian Osmane498b3c2020-09-23 14:42:11 -0400406 fIRGenerator->start(&settings, base ? base : fRootSymbolTable, nullptr, true);
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -0400407 fIRGenerator->convertProgram(kind, source->c_str(), source->length(), outElements);
John Stiles881a10c2020-09-19 10:13:24 -0400408 fIRGenerator->fCanInline = true;
Ethan Nicholas8da1e652019-05-24 11:01:59 -0400409 if (this->fErrorCount) {
410 printf("Unexpected errors: %s\n", this->fErrorText.c_str());
John Stiles88f3b682020-10-06 13:37:52 -0400411 SkDEBUGFAILF("%s %s\n", path, this->fErrorText.c_str());
Ethan Nicholas8da1e652019-05-24 11:01:59 -0400412 }
Ethan Nicholas8da1e652019-05-24 11:01:59 -0400413 *outSymbolTable = fIRGenerator->fSymbolTable;
Ethan Nicholasa11035b2019-11-26 16:27:47 -0500414#ifdef SK_DEBUG
415 fSource = nullptr;
416#endif
Brian Osmane498b3c2020-09-23 14:42:11 -0400417 fIRGenerator->finish();
Ethan Nicholas8da1e652019-05-24 11:01:59 -0400418}
419
ethannicholas22f939e2016-10-13 13:25:34 -0700420// add the definition created by assigning to the lvalue to the definition set
Ethan Nicholas86a43402017-01-19 13:32:00 -0500421void Compiler::addDefinition(const Expression* lvalue, std::unique_ptr<Expression>* expr,
422 DefinitionMap* definitions) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400423 switch (lvalue->kind()) {
424 case Expression::Kind::kVariableReference: {
Brian Osman79457ef2020-09-24 15:01:27 -0400425 const Variable& var = *lvalue->as<VariableReference>().fVariable;
ethannicholas22f939e2016-10-13 13:25:34 -0700426 if (var.fStorage == Variable::kLocal_Storage) {
427 (*definitions)[&var] = expr;
428 }
429 break;
430 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400431 case Expression::Kind::kSwizzle:
ethannicholas22f939e2016-10-13 13:25:34 -0700432 // We consider the variable written to as long as at least some of its components have
433 // been written to. This will lead to some false negatives (we won't catch it if you
434 // write to foo.x and then read foo.y), but being stricter could lead to false positives
Mike Klein6ad99092016-10-26 10:35:22 -0400435 // (we write to foo.x, and then pass foo to a function which happens to only read foo.x,
436 // 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 -0700437 // more complicated whole-program analysis. This is probably good enough.
John Stilesa5a97b42020-08-18 11:19:07 -0400438 this->addDefinition(lvalue->as<Swizzle>().fBase.get(),
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400439 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
ethannicholas22f939e2016-10-13 13:25:34 -0700440 definitions);
441 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400442 case Expression::Kind::kIndex:
ethannicholas22f939e2016-10-13 13:25:34 -0700443 // see comments in Swizzle
John Stilesa5a97b42020-08-18 11:19:07 -0400444 this->addDefinition(lvalue->as<IndexExpression>().fBase.get(),
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400445 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
ethannicholas22f939e2016-10-13 13:25:34 -0700446 definitions);
447 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400448 case Expression::Kind::kFieldAccess:
ethannicholas22f939e2016-10-13 13:25:34 -0700449 // see comments in Swizzle
John Stilesa5a97b42020-08-18 11:19:07 -0400450 this->addDefinition(lvalue->as<FieldAccess>().fBase.get(),
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400451 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
ethannicholas22f939e2016-10-13 13:25:34 -0700452 definitions);
453 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400454 case Expression::Kind::kTernary:
Ethan Nicholasa583b812018-01-18 13:32:11 -0500455 // To simplify analysis, we just pretend that we write to both sides of the ternary.
456 // This allows for false positives (meaning we fail to detect that a variable might not
457 // have been assigned), but is preferable to false negatives.
John Stilesa5a97b42020-08-18 11:19:07 -0400458 this->addDefinition(lvalue->as<TernaryExpression>().fIfTrue.get(),
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400459 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
Ethan Nicholasa583b812018-01-18 13:32:11 -0500460 definitions);
John Stilesa5a97b42020-08-18 11:19:07 -0400461 this->addDefinition(lvalue->as<TernaryExpression>().fIfFalse.get(),
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400462 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
Ethan Nicholasa583b812018-01-18 13:32:11 -0500463 definitions);
464 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400465 case Expression::Kind::kExternalValue:
Ethan Nicholas91164d12019-05-15 15:29:54 -0400466 break;
ethannicholas22f939e2016-10-13 13:25:34 -0700467 default:
468 // not an lvalue, can't happen
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400469 SkASSERT(false);
ethannicholas22f939e2016-10-13 13:25:34 -0700470 }
471}
472
473// add local variables defined by this node to the set
Mike Klein6ad99092016-10-26 10:35:22 -0400474void Compiler::addDefinitions(const BasicBlock::Node& node,
Ethan Nicholas86a43402017-01-19 13:32:00 -0500475 DefinitionMap* definitions) {
John Stiles70025e52020-09-28 16:08:58 -0400476 if (node.isExpression()) {
477 Expression* expr = node.expression()->get();
478 switch (expr->kind()) {
479 case Expression::Kind::kBinary: {
480 BinaryExpression* b = &expr->as<BinaryExpression>();
481 if (b->getOperator() == Token::Kind::TK_EQ) {
482 this->addDefinition(&b->left(), &b->rightPointer(), definitions);
483 } else if (Compiler::IsAssignment(b->getOperator())) {
484 this->addDefinition(
485 &b->left(),
486 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
487 definitions);
Ethan Nicholas86a43402017-01-19 13:32:00 -0500488
ethannicholas22f939e2016-10-13 13:25:34 -0700489 }
John Stiles70025e52020-09-28 16:08:58 -0400490 break;
ethannicholas22f939e2016-10-13 13:25:34 -0700491 }
John Stiles70025e52020-09-28 16:08:58 -0400492 case Expression::Kind::kFunctionCall: {
493 const FunctionCall& c = expr->as<FunctionCall>();
Ethan Nicholas0dec9922020-10-05 15:51:52 -0400494 for (size_t i = 0; i < c.function().fParameters.size(); ++i) {
495 if (c.function().fParameters[i]->fModifiers.fFlags & Modifiers::kOut_Flag) {
John Stiles70025e52020-09-28 16:08:58 -0400496 this->addDefinition(
Ethan Nicholas0dec9922020-10-05 15:51:52 -0400497 c.arguments()[i].get(),
John Stiles70025e52020-09-28 16:08:58 -0400498 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
499 definitions);
500 }
501 }
502 break;
503 }
504 case Expression::Kind::kPrefix: {
505 const PrefixExpression* p = &expr->as<PrefixExpression>();
506 if (p->fOperator == Token::Kind::TK_MINUSMINUS ||
507 p->fOperator == Token::Kind::TK_PLUSPLUS) {
508 this->addDefinition(
509 p->fOperand.get(),
510 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
511 definitions);
512 }
513 break;
514 }
515 case Expression::Kind::kPostfix: {
516 const PostfixExpression* p = &expr->as<PostfixExpression>();
517 if (p->fOperator == Token::Kind::TK_MINUSMINUS ||
518 p->fOperator == Token::Kind::TK_PLUSPLUS) {
519 this->addDefinition(
520 p->fOperand.get(),
521 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
522 definitions);
523 }
524 break;
525 }
526 case Expression::Kind::kVariableReference: {
527 const VariableReference* v = &expr->as<VariableReference>();
528 if (v->fRefKind != VariableReference::kRead_RefKind) {
529 this->addDefinition(
530 v,
531 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
532 definitions);
533 }
534 break;
535 }
536 default:
537 break;
ethannicholas22f939e2016-10-13 13:25:34 -0700538 }
John Stiles70025e52020-09-28 16:08:58 -0400539 } else if (node.isStatement()) {
540 Statement* stmt = node.statement()->get();
541 if (stmt->is<VarDeclaration>()) {
542 VarDeclaration& vd = stmt->as<VarDeclaration>();
543 if (vd.fValue) {
544 (*definitions)[vd.fVar] = &vd.fValue;
ethannicholas22f939e2016-10-13 13:25:34 -0700545 }
ethannicholas22f939e2016-10-13 13:25:34 -0700546 }
547 }
548}
549
John Stilese6150002020-10-05 12:03:53 -0400550void Compiler::scanCFG(CFG* cfg, BlockId blockId, SkBitSet* processedSet) {
ethannicholas22f939e2016-10-13 13:25:34 -0700551 BasicBlock& block = cfg->fBlocks[blockId];
552
553 // compute definitions after this block
Ethan Nicholas86a43402017-01-19 13:32:00 -0500554 DefinitionMap after = block.fBefore;
ethannicholas22f939e2016-10-13 13:25:34 -0700555 for (const BasicBlock::Node& n : block.fNodes) {
556 this->addDefinitions(n, &after);
557 }
558
559 // propagate definitions to exits
560 for (BlockId exitId : block.fExits) {
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400561 if (exitId == blockId) {
562 continue;
563 }
ethannicholas22f939e2016-10-13 13:25:34 -0700564 BasicBlock& exit = cfg->fBlocks[exitId];
565 for (const auto& pair : after) {
Ethan Nicholas86a43402017-01-19 13:32:00 -0500566 std::unique_ptr<Expression>* e1 = pair.second;
567 auto found = exit.fBefore.find(pair.first);
568 if (found == exit.fBefore.end()) {
John Stilese6150002020-10-05 12:03:53 -0400569 // exit has no definition for it, just copy it and reprocess exit block
570 processedSet->reset(exitId);
ethannicholas22f939e2016-10-13 13:25:34 -0700571 exit.fBefore[pair.first] = e1;
572 } else {
Ethan Nicholas86a43402017-01-19 13:32:00 -0500573 // exit has a (possibly different) value already defined
574 std::unique_ptr<Expression>* e2 = exit.fBefore[pair.first];
ethannicholas22f939e2016-10-13 13:25:34 -0700575 if (e1 != e2) {
John Stilese6150002020-10-05 12:03:53 -0400576 // definition has changed, merge and reprocess the exit block
577 processedSet->reset(exitId);
Ethan Nicholasaf197692017-02-27 13:26:45 -0500578 if (e1 && e2) {
579 exit.fBefore[pair.first] =
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400580 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression;
Ethan Nicholasaf197692017-02-27 13:26:45 -0500581 } else {
582 exit.fBefore[pair.first] = nullptr;
583 }
ethannicholas22f939e2016-10-13 13:25:34 -0700584 }
585 }
586 }
587 }
588}
589
590// returns a map which maps all local variables in the function to null, indicating that their value
591// is initially unknown
Ethan Nicholas86a43402017-01-19 13:32:00 -0500592static DefinitionMap compute_start_state(const CFG& cfg) {
593 DefinitionMap result;
Mike Klein6ad99092016-10-26 10:35:22 -0400594 for (const auto& block : cfg.fBlocks) {
595 for (const auto& node : block.fNodes) {
John Stiles70025e52020-09-28 16:08:58 -0400596 if (node.isStatement()) {
Ethan Nicholascb670962017-04-20 19:31:52 -0400597 const Statement* s = node.statement()->get();
Brian Osmanc0213602020-10-06 14:43:32 -0400598 if (s->is<VarDeclaration>()) {
599 result[s->as<VarDeclaration>().fVar] = nullptr;
ethannicholas22f939e2016-10-13 13:25:34 -0700600 }
601 }
602 }
603 }
604 return result;
605}
606
Ethan Nicholascb670962017-04-20 19:31:52 -0400607/**
608 * Returns true if assigning to this lvalue has no effect.
609 */
610static bool is_dead(const Expression& lvalue) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400611 switch (lvalue.kind()) {
612 case Expression::Kind::kVariableReference:
Brian Osman79457ef2020-09-24 15:01:27 -0400613 return lvalue.as<VariableReference>().fVariable->dead();
Ethan Nicholase6592142020-09-08 10:22:09 -0400614 case Expression::Kind::kSwizzle:
John Stilesa5a97b42020-08-18 11:19:07 -0400615 return is_dead(*lvalue.as<Swizzle>().fBase);
Ethan Nicholase6592142020-09-08 10:22:09 -0400616 case Expression::Kind::kFieldAccess:
John Stilesa5a97b42020-08-18 11:19:07 -0400617 return is_dead(*lvalue.as<FieldAccess>().fBase);
Ethan Nicholase6592142020-09-08 10:22:09 -0400618 case Expression::Kind::kIndex: {
John Stilesa5a97b42020-08-18 11:19:07 -0400619 const IndexExpression& idx = lvalue.as<IndexExpression>();
Ethan Nicholas2a099da2020-01-02 14:40:54 -0500620 return is_dead(*idx.fBase) &&
621 !idx.fIndex->hasProperty(Expression::Property::kSideEffects);
Ethan Nicholascb670962017-04-20 19:31:52 -0400622 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400623 case Expression::Kind::kTernary: {
John Stilesa5a97b42020-08-18 11:19:07 -0400624 const TernaryExpression& t = lvalue.as<TernaryExpression>();
Ethan Nicholasa583b812018-01-18 13:32:11 -0500625 return !t.fTest->hasSideEffects() && is_dead(*t.fIfTrue) && is_dead(*t.fIfFalse);
626 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400627 case Expression::Kind::kExternalValue:
Ethan Nicholas91164d12019-05-15 15:29:54 -0400628 return false;
Ethan Nicholascb670962017-04-20 19:31:52 -0400629 default:
Ethan Nicholas2a099da2020-01-02 14:40:54 -0500630#ifdef SK_DEBUG
Ethan Nicholascb670962017-04-20 19:31:52 -0400631 ABORT("invalid lvalue: %s\n", lvalue.description().c_str());
Ethan Nicholas2a099da2020-01-02 14:40:54 -0500632#endif
633 return false;
Ethan Nicholascb670962017-04-20 19:31:52 -0400634 }
635}
ethannicholas22f939e2016-10-13 13:25:34 -0700636
Ethan Nicholascb670962017-04-20 19:31:52 -0400637/**
638 * Returns true if this is an assignment which can be collapsed down to just the right hand side due
639 * to a dead target and lack of side effects on the left hand side.
640 */
641static bool dead_assignment(const BinaryExpression& b) {
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400642 if (!Compiler::IsAssignment(b.getOperator())) {
Ethan Nicholascb670962017-04-20 19:31:52 -0400643 return false;
644 }
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400645 return is_dead(b.left());
Ethan Nicholascb670962017-04-20 19:31:52 -0400646}
647
648void Compiler::computeDataFlow(CFG* cfg) {
649 cfg->fBlocks[cfg->fStart].fBefore = compute_start_state(*cfg);
John Stilese6150002020-10-05 12:03:53 -0400650
651 // We set bits in the "processed" set after a block has been scanned.
652 SkBitSet processedSet(cfg->fBlocks.size());
653 while (SkBitSet::OptionalIndex blockId = processedSet.findFirstUnset()) {
654 processedSet.set(*blockId);
655 this->scanCFG(cfg, *blockId, &processedSet);
ethannicholas22f939e2016-10-13 13:25:34 -0700656 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400657}
658
659/**
660 * Attempts to replace the expression pointed to by iter with a new one (in both the CFG and the
661 * IR). If the expression can be cleanly removed, returns true and updates the iterator to point to
662 * the newly-inserted element. Otherwise updates only the IR and returns false (and the CFG will
663 * need to be regenerated).
664 */
John Stilesafbf8992020-08-18 10:08:21 -0400665static bool try_replace_expression(BasicBlock* b,
666 std::vector<BasicBlock::Node>::iterator* iter,
667 std::unique_ptr<Expression>* newExpression) {
Ethan Nicholascb670962017-04-20 19:31:52 -0400668 std::unique_ptr<Expression>* target = (*iter)->expression();
669 if (!b->tryRemoveExpression(iter)) {
670 *target = std::move(*newExpression);
671 return false;
672 }
673 *target = std::move(*newExpression);
674 return b->tryInsertExpression(iter, target);
675}
676
677/**
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400678 * Returns true if the expression is a constant numeric literal with the specified value, or a
679 * constant vector with all elements equal to the specified value.
Ethan Nicholascb670962017-04-20 19:31:52 -0400680 */
Ethan Nicholasa3f22f12020-10-01 12:13:17 -0400681template <typename T = SKSL_FLOAT>
John Stiles9d944232020-08-19 09:56:49 -0400682static bool is_constant(const Expression& expr, T value) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400683 switch (expr.kind()) {
684 case Expression::Kind::kIntLiteral:
Ethan Nicholase96cdd12020-09-28 16:27:18 -0400685 return expr.as<IntLiteral>().value() == value;
John Stiles9d944232020-08-19 09:56:49 -0400686
Ethan Nicholase6592142020-09-08 10:22:09 -0400687 case Expression::Kind::kFloatLiteral:
Ethan Nicholasa3f22f12020-10-01 12:13:17 -0400688 return expr.as<FloatLiteral>().value() == value;
John Stiles9d944232020-08-19 09:56:49 -0400689
Ethan Nicholase6592142020-09-08 10:22:09 -0400690 case Expression::Kind::kConstructor: {
John Stiles9d944232020-08-19 09:56:49 -0400691 const Constructor& constructor = expr.as<Constructor>();
692 if (constructor.isCompileTimeConstant()) {
Ethan Nicholas30d30222020-09-11 12:27:26 -0400693 const Type& constructorType = constructor.type();
694 bool isFloat = constructorType.columns() > 1
695 ? constructorType.componentType().isFloat()
696 : constructorType.isFloat();
697 switch (constructorType.typeKind()) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400698 case Type::TypeKind::kVector:
Ethan Nicholas30d30222020-09-11 12:27:26 -0400699 for (int i = 0; i < constructorType.columns(); ++i) {
John Stiles9d944232020-08-19 09:56:49 -0400700 if (isFloat) {
701 if (constructor.getFVecComponent(i) != value) {
702 return false;
703 }
704 } else {
705 if (constructor.getIVecComponent(i) != value) {
706 return false;
707 }
708 }
Ethan Nicholasd188c182019-06-10 15:55:38 -0400709 }
John Stiles9d944232020-08-19 09:56:49 -0400710 return true;
711
Ethan Nicholase6592142020-09-08 10:22:09 -0400712 case Type::TypeKind::kScalar:
Ethan Nicholasf70f0442020-09-29 12:41:35 -0400713 SkASSERT(constructor.arguments().size() == 1);
714 return is_constant<T>(*constructor.arguments()[0], value);
John Stiles9d944232020-08-19 09:56:49 -0400715
716 default:
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400717 return false;
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400718 }
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400719 }
720 return false;
721 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400722 default:
723 return false;
724 }
725}
726
727/**
728 * Collapses the binary expression pointed to by iter down to just the right side (in both the IR
729 * and CFG structures).
730 */
John Stilesafbf8992020-08-18 10:08:21 -0400731static void delete_left(BasicBlock* b,
732 std::vector<BasicBlock::Node>::iterator* iter,
733 bool* outUpdated,
734 bool* outNeedsRescan) {
Ethan Nicholascb670962017-04-20 19:31:52 -0400735 *outUpdated = true;
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400736 std::unique_ptr<Expression>* target = (*iter)->expression();
John Stilesa5a97b42020-08-18 11:19:07 -0400737 BinaryExpression& bin = (*target)->as<BinaryExpression>();
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400738 Expression& left = bin.left();
739 std::unique_ptr<Expression>& rightPointer = bin.rightPointer();
740 SkASSERT(!left.hasSideEffects());
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400741 bool result;
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400742 if (bin.getOperator() == Token::Kind::TK_EQ) {
743 result = b->tryRemoveLValueBefore(iter, &left);
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400744 } else {
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400745 result = b->tryRemoveExpressionBefore(iter, &left);
Ethan Nicholascb670962017-04-20 19:31:52 -0400746 }
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400747 *target = std::move(rightPointer);
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400748 if (!result) {
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400749 *outNeedsRescan = true;
750 return;
751 }
752 if (*iter == b->fNodes.begin()) {
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400753 *outNeedsRescan = true;
754 return;
755 }
756 --(*iter);
John Stiles70025e52020-09-28 16:08:58 -0400757 if (!(*iter)->isExpression() || (*iter)->expression() != &rightPointer) {
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400758 *outNeedsRescan = true;
759 return;
760 }
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400761 *iter = b->fNodes.erase(*iter);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400762 SkASSERT((*iter)->expression() == target);
Ethan Nicholascb670962017-04-20 19:31:52 -0400763}
764
765/**
766 * Collapses the binary expression pointed to by iter down to just the left side (in both the IR and
767 * CFG structures).
768 */
John Stilesafbf8992020-08-18 10:08:21 -0400769static void delete_right(BasicBlock* b,
770 std::vector<BasicBlock::Node>::iterator* iter,
771 bool* outUpdated,
772 bool* outNeedsRescan) {
Ethan Nicholascb670962017-04-20 19:31:52 -0400773 *outUpdated = true;
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400774 std::unique_ptr<Expression>* target = (*iter)->expression();
John Stilesa5a97b42020-08-18 11:19:07 -0400775 BinaryExpression& bin = (*target)->as<BinaryExpression>();
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400776 std::unique_ptr<Expression>& leftPointer = bin.leftPointer();
777 Expression& right = bin.right();
778 SkASSERT(!right.hasSideEffects());
779 if (!b->tryRemoveExpressionBefore(iter, &right)) {
780 *target = std::move(leftPointer);
Ethan Nicholascb670962017-04-20 19:31:52 -0400781 *outNeedsRescan = true;
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400782 return;
Ethan Nicholascb670962017-04-20 19:31:52 -0400783 }
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400784 *target = std::move(leftPointer);
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400785 if (*iter == b->fNodes.begin()) {
786 *outNeedsRescan = true;
787 return;
788 }
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400789 --(*iter);
John Stiles70025e52020-09-28 16:08:58 -0400790 if ((!(*iter)->isExpression() || (*iter)->expression() != &leftPointer)) {
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400791 *outNeedsRescan = true;
792 return;
793 }
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400794 *iter = b->fNodes.erase(*iter);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400795 SkASSERT((*iter)->expression() == target);
Ethan Nicholascb670962017-04-20 19:31:52 -0400796}
797
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400798/**
799 * Constructs the specified type using a single argument.
800 */
Ethan Nicholas30d30222020-09-11 12:27:26 -0400801static std::unique_ptr<Expression> construct(const Type* type, std::unique_ptr<Expression> v) {
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400802 std::vector<std::unique_ptr<Expression>> args;
803 args.push_back(std::move(v));
Ethan Nicholase6592142020-09-08 10:22:09 -0400804 std::unique_ptr<Expression> result = std::make_unique<Constructor>(-1, type, std::move(args));
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400805 return result;
806}
807
808/**
809 * Used in the implementations of vectorize_left and vectorize_right. Given a vector type and an
810 * expression x, deletes the expression pointed to by iter and replaces it with <type>(x).
811 */
812static void vectorize(BasicBlock* b,
813 std::vector<BasicBlock::Node>::iterator* iter,
814 const Type& type,
815 std::unique_ptr<Expression>* otherExpression,
816 bool* outUpdated,
817 bool* outNeedsRescan) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400818 SkASSERT((*(*iter)->expression())->kind() == Expression::Kind::kBinary);
819 SkASSERT(type.typeKind() == Type::TypeKind::kVector);
Ethan Nicholas30d30222020-09-11 12:27:26 -0400820 SkASSERT((*otherExpression)->type().typeKind() == Type::TypeKind::kScalar);
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400821 *outUpdated = true;
822 std::unique_ptr<Expression>* target = (*iter)->expression();
823 if (!b->tryRemoveExpression(iter)) {
Ethan Nicholas30d30222020-09-11 12:27:26 -0400824 *target = construct(&type, std::move(*otherExpression));
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400825 *outNeedsRescan = true;
826 } else {
Ethan Nicholas30d30222020-09-11 12:27:26 -0400827 *target = construct(&type, std::move(*otherExpression));
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400828 if (!b->tryInsertExpression(iter, target)) {
829 *outNeedsRescan = true;
830 }
831 }
832}
833
834/**
835 * Given a binary expression of the form x <op> vec<n>(y), deletes the right side and vectorizes the
836 * left to yield vec<n>(x).
837 */
838static void vectorize_left(BasicBlock* b,
839 std::vector<BasicBlock::Node>::iterator* iter,
840 bool* outUpdated,
841 bool* outNeedsRescan) {
John Stilesa5a97b42020-08-18 11:19:07 -0400842 BinaryExpression& bin = (*(*iter)->expression())->as<BinaryExpression>();
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400843 vectorize(b, iter, bin.right().type(), &bin.leftPointer(), outUpdated, outNeedsRescan);
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400844}
845
846/**
847 * Given a binary expression of the form vec<n>(x) <op> y, deletes the left side and vectorizes the
848 * right to yield vec<n>(y).
849 */
850static void vectorize_right(BasicBlock* b,
851 std::vector<BasicBlock::Node>::iterator* iter,
852 bool* outUpdated,
853 bool* outNeedsRescan) {
John Stilesa5a97b42020-08-18 11:19:07 -0400854 BinaryExpression& bin = (*(*iter)->expression())->as<BinaryExpression>();
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400855 vectorize(b, iter, bin.left().type(), &bin.rightPointer(), outUpdated, outNeedsRescan);
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400856}
857
858// Mark that an expression which we were writing to is no longer being written to
John Stilesa5a97b42020-08-18 11:19:07 -0400859static void clear_write(Expression& expr) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400860 switch (expr.kind()) {
861 case Expression::Kind::kVariableReference: {
John Stilesa5a97b42020-08-18 11:19:07 -0400862 expr.as<VariableReference>().setRefKind(VariableReference::kRead_RefKind);
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400863 break;
864 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400865 case Expression::Kind::kFieldAccess:
John Stilesa5a97b42020-08-18 11:19:07 -0400866 clear_write(*expr.as<FieldAccess>().fBase);
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400867 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400868 case Expression::Kind::kSwizzle:
John Stilesa5a97b42020-08-18 11:19:07 -0400869 clear_write(*expr.as<Swizzle>().fBase);
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400870 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400871 case Expression::Kind::kIndex:
John Stilesa5a97b42020-08-18 11:19:07 -0400872 clear_write(*expr.as<IndexExpression>().fBase);
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400873 break;
874 default:
875 ABORT("shouldn't be writing to this kind of expression\n");
876 break;
877 }
878}
879
Ethan Nicholascb670962017-04-20 19:31:52 -0400880void Compiler::simplifyExpression(DefinitionMap& definitions,
881 BasicBlock& b,
882 std::vector<BasicBlock::Node>::iterator* iter,
883 std::unordered_set<const Variable*>* undefinedVariables,
884 bool* outUpdated,
885 bool* outNeedsRescan) {
886 Expression* expr = (*iter)->expression()->get();
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400887 SkASSERT(expr);
Ethan Nicholascb670962017-04-20 19:31:52 -0400888 if ((*iter)->fConstantPropagation) {
889 std::unique_ptr<Expression> optimized = expr->constantPropagate(*fIRGenerator, definitions);
890 if (optimized) {
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400891 *outUpdated = true;
Ethan Nicholas30d30222020-09-11 12:27:26 -0400892 optimized = fIRGenerator->coerce(std::move(optimized), expr->type());
Ethan Nicholas1d881522020-09-11 09:32:54 -0400893 SkASSERT(optimized);
Ethan Nicholascb670962017-04-20 19:31:52 -0400894 if (!try_replace_expression(&b, iter, &optimized)) {
895 *outNeedsRescan = true;
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400896 return;
Ethan Nicholascb670962017-04-20 19:31:52 -0400897 }
John Stiles70025e52020-09-28 16:08:58 -0400898 SkASSERT((*iter)->isExpression());
Ethan Nicholascb670962017-04-20 19:31:52 -0400899 expr = (*iter)->expression()->get();
Ethan Nicholascb670962017-04-20 19:31:52 -0400900 }
901 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400902 switch (expr->kind()) {
903 case Expression::Kind::kVariableReference: {
John Stilesa5a97b42020-08-18 11:19:07 -0400904 const VariableReference& ref = expr->as<VariableReference>();
Brian Osman79457ef2020-09-24 15:01:27 -0400905 const Variable* var = ref.fVariable;
Ethan Nicholas8f7e28f2018-03-26 14:24:27 -0400906 if (ref.refKind() != VariableReference::kWrite_RefKind &&
907 ref.refKind() != VariableReference::kPointer_RefKind &&
Brian Osman79457ef2020-09-24 15:01:27 -0400908 var->fStorage == Variable::kLocal_Storage && !definitions[var] &&
909 (*undefinedVariables).find(var) == (*undefinedVariables).end()) {
910 (*undefinedVariables).insert(var);
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000911 this->error(expr->fOffset,
Ethan Nicholase2c49992020-10-05 11:49:11 -0400912 "'" + var->name() + "' has not been assigned");
Ethan Nicholascb670962017-04-20 19:31:52 -0400913 }
914 break;
915 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400916 case Expression::Kind::kTernary: {
John Stiles403a3632020-08-20 12:11:48 -0400917 TernaryExpression* t = &expr->as<TernaryExpression>();
Ethan Nicholase6592142020-09-08 10:22:09 -0400918 if (t->fTest->kind() == Expression::Kind::kBoolLiteral) {
Ethan Nicholascb670962017-04-20 19:31:52 -0400919 // ternary has a constant test, replace it with either the true or
920 // false branch
Ethan Nicholas59d660c2020-09-28 09:18:15 -0400921 if (t->fTest->as<BoolLiteral>().value()) {
Ethan Nicholascb670962017-04-20 19:31:52 -0400922 (*iter)->setExpression(std::move(t->fIfTrue));
923 } else {
924 (*iter)->setExpression(std::move(t->fIfFalse));
925 }
926 *outUpdated = true;
927 *outNeedsRescan = true;
928 }
929 break;
930 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400931 case Expression::Kind::kBinary: {
John Stiles403a3632020-08-20 12:11:48 -0400932 BinaryExpression* bin = &expr->as<BinaryExpression>();
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400933 if (dead_assignment(*bin)) {
934 delete_left(&b, iter, outUpdated, outNeedsRescan);
935 break;
936 }
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400937 Expression& left = bin->left();
938 Expression& right = bin->right();
939 const Type& leftType = left.type();
940 const Type& rightType = right.type();
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400941 // collapse useless expressions like x * 1 or x + 0
Ethan Nicholas30d30222020-09-11 12:27:26 -0400942 if (((leftType.typeKind() != Type::TypeKind::kScalar) &&
943 (leftType.typeKind() != Type::TypeKind::kVector)) ||
944 ((rightType.typeKind() != Type::TypeKind::kScalar) &&
945 (rightType.typeKind() != Type::TypeKind::kVector))) {
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400946 break;
947 }
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400948 switch (bin->getOperator()) {
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400949 case Token::Kind::TK_STAR:
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400950 if (is_constant(left, 1)) {
Ethan Nicholas30d30222020-09-11 12:27:26 -0400951 if (leftType.typeKind() == Type::TypeKind::kVector &&
952 rightType.typeKind() == Type::TypeKind::kScalar) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400953 // float4(1) * x -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400954 vectorize_right(&b, iter, outUpdated, outNeedsRescan);
955 } else {
956 // 1 * x -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400957 // 1 * float4(x) -> float4(x)
958 // float4(1) * float4(x) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400959 delete_left(&b, iter, outUpdated, outNeedsRescan);
960 }
961 }
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400962 else if (is_constant(left, 0)) {
Ethan Nicholas30d30222020-09-11 12:27:26 -0400963 if (leftType.typeKind() == Type::TypeKind::kScalar &&
964 rightType.typeKind() == Type::TypeKind::kVector &&
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400965 !right.hasSideEffects()) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400966 // 0 * float4(x) -> float4(0)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400967 vectorize_left(&b, iter, outUpdated, outNeedsRescan);
968 } else {
969 // 0 * x -> 0
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400970 // float4(0) * x -> float4(0)
971 // float4(0) * float4(x) -> float4(0)
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400972 if (!right.hasSideEffects()) {
Ethan Nicholas51493ee2017-12-11 12:34:33 -0500973 delete_right(&b, iter, outUpdated, outNeedsRescan);
974 }
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400975 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400976 }
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400977 else if (is_constant(right, 1)) {
Ethan Nicholas30d30222020-09-11 12:27:26 -0400978 if (leftType.typeKind() == Type::TypeKind::kScalar &&
979 rightType.typeKind() == Type::TypeKind::kVector) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400980 // x * float4(1) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400981 vectorize_left(&b, iter, outUpdated, outNeedsRescan);
982 } else {
983 // x * 1 -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400984 // float4(x) * 1 -> float4(x)
985 // float4(x) * float4(1) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400986 delete_right(&b, iter, outUpdated, outNeedsRescan);
987 }
988 }
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400989 else if (is_constant(right, 0)) {
Ethan Nicholas30d30222020-09-11 12:27:26 -0400990 if (leftType.typeKind() == Type::TypeKind::kVector &&
991 rightType.typeKind() == Type::TypeKind::kScalar &&
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400992 !left.hasSideEffects()) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400993 // float4(x) * 0 -> float4(0)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400994 vectorize_right(&b, iter, outUpdated, outNeedsRescan);
995 } else {
996 // x * 0 -> 0
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400997 // x * float4(0) -> float4(0)
998 // float4(x) * float4(0) -> float4(0)
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400999 if (!left.hasSideEffects()) {
Ethan Nicholas51493ee2017-12-11 12:34:33 -05001000 delete_left(&b, iter, outUpdated, outNeedsRescan);
1001 }
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001002 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001003 }
1004 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001005 case Token::Kind::TK_PLUS:
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -04001006 if (is_constant(left, 0)) {
Ethan Nicholas30d30222020-09-11 12:27:26 -04001007 if (leftType.typeKind() == Type::TypeKind::kVector &&
1008 rightType.typeKind() == Type::TypeKind::kScalar) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001009 // float4(0) + x -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001010 vectorize_right(&b, iter, outUpdated, outNeedsRescan);
1011 } else {
1012 // 0 + x -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001013 // 0 + float4(x) -> float4(x)
1014 // float4(0) + float4(x) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001015 delete_left(&b, iter, outUpdated, outNeedsRescan);
1016 }
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -04001017 } else if (is_constant(right, 0)) {
Ethan Nicholas30d30222020-09-11 12:27:26 -04001018 if (leftType.typeKind() == Type::TypeKind::kScalar &&
1019 rightType.typeKind() == Type::TypeKind::kVector) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001020 // x + float4(0) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001021 vectorize_left(&b, iter, outUpdated, outNeedsRescan);
1022 } else {
1023 // x + 0 -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001024 // float4(x) + 0 -> float4(x)
1025 // float4(x) + float4(0) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001026 delete_right(&b, iter, outUpdated, outNeedsRescan);
1027 }
Ethan Nicholas56e42712017-04-21 10:23:37 -04001028 }
1029 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001030 case Token::Kind::TK_MINUS:
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -04001031 if (is_constant(right, 0)) {
Ethan Nicholas30d30222020-09-11 12:27:26 -04001032 if (leftType.typeKind() == Type::TypeKind::kScalar &&
1033 rightType.typeKind() == Type::TypeKind::kVector) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001034 // x - float4(0) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001035 vectorize_left(&b, iter, outUpdated, outNeedsRescan);
1036 } else {
1037 // x - 0 -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001038 // float4(x) - 0 -> float4(x)
1039 // float4(x) - float4(0) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001040 delete_right(&b, iter, outUpdated, outNeedsRescan);
1041 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001042 }
1043 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001044 case Token::Kind::TK_SLASH:
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -04001045 if (is_constant(right, 1)) {
Ethan Nicholas30d30222020-09-11 12:27:26 -04001046 if (leftType.typeKind() == Type::TypeKind::kScalar &&
1047 rightType.typeKind() == Type::TypeKind::kVector) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001048 // x / float4(1) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001049 vectorize_left(&b, iter, outUpdated, outNeedsRescan);
1050 } else {
1051 // x / 1 -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001052 // float4(x) / 1 -> float4(x)
1053 // float4(x) / float4(1) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001054 delete_right(&b, iter, outUpdated, outNeedsRescan);
1055 }
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -04001056 } else if (is_constant(left, 0)) {
Ethan Nicholas30d30222020-09-11 12:27:26 -04001057 if (leftType.typeKind() == Type::TypeKind::kScalar &&
1058 rightType.typeKind() == Type::TypeKind::kVector &&
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -04001059 !right.hasSideEffects()) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001060 // 0 / float4(x) -> float4(0)
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001061 vectorize_left(&b, iter, outUpdated, outNeedsRescan);
1062 } else {
1063 // 0 / x -> 0
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001064 // float4(0) / x -> float4(0)
1065 // float4(0) / float4(x) -> float4(0)
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -04001066 if (!right.hasSideEffects()) {
Ethan Nicholas51493ee2017-12-11 12:34:33 -05001067 delete_right(&b, iter, outUpdated, outNeedsRescan);
1068 }
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001069 }
1070 }
1071 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001072 case Token::Kind::TK_PLUSEQ:
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -04001073 if (is_constant(right, 0)) {
1074 clear_write(left);
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001075 delete_right(&b, iter, outUpdated, outNeedsRescan);
1076 }
1077 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001078 case Token::Kind::TK_MINUSEQ:
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -04001079 if (is_constant(right, 0)) {
1080 clear_write(left);
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001081 delete_right(&b, iter, outUpdated, outNeedsRescan);
1082 }
1083 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001084 case Token::Kind::TK_STAREQ:
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -04001085 if (is_constant(right, 1)) {
1086 clear_write(left);
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001087 delete_right(&b, iter, outUpdated, outNeedsRescan);
1088 }
1089 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001090 case Token::Kind::TK_SLASHEQ:
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -04001091 if (is_constant(right, 1)) {
1092 clear_write(left);
Ethan Nicholascb670962017-04-20 19:31:52 -04001093 delete_right(&b, iter, outUpdated, outNeedsRescan);
1094 }
1095 break;
1096 default:
1097 break;
1098 }
Ethan Nicholas409f6f02019-09-17 12:34:39 -04001099 break;
1100 }
Ethan Nicholase6592142020-09-08 10:22:09 -04001101 case Expression::Kind::kSwizzle: {
John Stiles403a3632020-08-20 12:11:48 -04001102 Swizzle& s = expr->as<Swizzle>();
Ethan Nicholas409f6f02019-09-17 12:34:39 -04001103 // detect identity swizzles like foo.rgba
Ethan Nicholas30d30222020-09-11 12:27:26 -04001104 if ((int) s.fComponents.size() == s.fBase->type().columns()) {
Ethan Nicholas409f6f02019-09-17 12:34:39 -04001105 bool identity = true;
1106 for (int i = 0; i < (int) s.fComponents.size(); ++i) {
1107 if (s.fComponents[i] != i) {
1108 identity = false;
1109 break;
1110 }
1111 }
1112 if (identity) {
1113 *outUpdated = true;
1114 if (!try_replace_expression(&b, iter, &s.fBase)) {
1115 *outNeedsRescan = true;
1116 return;
1117 }
John Stiles70025e52020-09-28 16:08:58 -04001118 SkASSERT((*iter)->isExpression());
Ethan Nicholas409f6f02019-09-17 12:34:39 -04001119 break;
1120 }
1121 }
1122 // detect swizzles of swizzles, e.g. replace foo.argb.r000 with foo.a000
Ethan Nicholase6592142020-09-08 10:22:09 -04001123 if (s.fBase->kind() == Expression::Kind::kSwizzle) {
John Stilesa5a97b42020-08-18 11:19:07 -04001124 Swizzle& base = s.fBase->as<Swizzle>();
Ethan Nicholas409f6f02019-09-17 12:34:39 -04001125 std::vector<int> final;
1126 for (int c : s.fComponents) {
Brian Osman25647672020-09-15 15:16:56 -04001127 final.push_back(base.fComponents[c]);
Ethan Nicholas409f6f02019-09-17 12:34:39 -04001128 }
1129 *outUpdated = true;
1130 std::unique_ptr<Expression> replacement(new Swizzle(*fContext, base.fBase->clone(),
1131 std::move(final)));
1132 if (!try_replace_expression(&b, iter, &replacement)) {
1133 *outNeedsRescan = true;
1134 return;
1135 }
John Stiles70025e52020-09-28 16:08:58 -04001136 SkASSERT((*iter)->isExpression());
Ethan Nicholas409f6f02019-09-17 12:34:39 -04001137 }
John Stiles30212b72020-06-11 17:55:07 -04001138 break;
Ethan Nicholascb670962017-04-20 19:31:52 -04001139 }
1140 default:
1141 break;
1142 }
1143}
1144
John Stiles92219b42020-06-15 12:32:24 -04001145// Returns true if this statement could potentially execute a break at the current level. We ignore
1146// nested loops and switches, since any breaks inside of them will merely break the loop / switch.
John Stilesb92641c2020-08-31 18:09:01 -04001147static bool contains_conditional_break(Statement& stmt) {
1148 class ContainsConditionalBreak : public ProgramVisitor {
1149 public:
1150 bool visitStatement(const Statement& stmt) override {
Ethan Nicholase6592142020-09-08 10:22:09 -04001151 switch (stmt.kind()) {
1152 case Statement::Kind::kBlock:
John Stilesb92641c2020-08-31 18:09:01 -04001153 return this->INHERITED::visitStatement(stmt);
1154
Ethan Nicholase6592142020-09-08 10:22:09 -04001155 case Statement::Kind::kBreak:
John Stilesb92641c2020-08-31 18:09:01 -04001156 return fInConditional > 0;
1157
Ethan Nicholase6592142020-09-08 10:22:09 -04001158 case Statement::Kind::kIf: {
John Stilesb92641c2020-08-31 18:09:01 -04001159 ++fInConditional;
1160 bool result = this->INHERITED::visitStatement(stmt);
1161 --fInConditional;
1162 return result;
1163 }
1164
1165 default:
1166 return false;
1167 }
1168 }
1169
1170 int fInConditional = 0;
1171 using INHERITED = ProgramVisitor;
1172 };
1173
1174 return ContainsConditionalBreak{}.visitStatement(stmt);
John Stiles92219b42020-06-15 12:32:24 -04001175}
1176
Ethan Nicholas5005a222018-08-24 13:06:27 -04001177// returns true if this statement definitely executes a break at the current level (we ignore
1178// nested loops and switches, since any breaks inside of them will merely break the loop / switch)
John Stilesb92641c2020-08-31 18:09:01 -04001179static bool contains_unconditional_break(Statement& stmt) {
1180 class ContainsUnconditionalBreak : public ProgramVisitor {
1181 public:
1182 bool visitStatement(const Statement& stmt) override {
Ethan Nicholase6592142020-09-08 10:22:09 -04001183 switch (stmt.kind()) {
1184 case Statement::Kind::kBlock:
John Stilesb92641c2020-08-31 18:09:01 -04001185 return this->INHERITED::visitStatement(stmt);
1186
Ethan Nicholase6592142020-09-08 10:22:09 -04001187 case Statement::Kind::kBreak:
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001188 return true;
John Stilesb92641c2020-08-31 18:09:01 -04001189
1190 default:
1191 return false;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001192 }
John Stilesb92641c2020-08-31 18:09:01 -04001193 }
John Stiles92219b42020-06-15 12:32:24 -04001194
John Stilesb92641c2020-08-31 18:09:01 -04001195 using INHERITED = ProgramVisitor;
1196 };
John Stiles92219b42020-06-15 12:32:24 -04001197
John Stilesb92641c2020-08-31 18:09:01 -04001198 return ContainsUnconditionalBreak{}.visitStatement(stmt);
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001199}
1200
John Stiles92219b42020-06-15 12:32:24 -04001201static void move_all_but_break(std::unique_ptr<Statement>& stmt,
1202 std::vector<std::unique_ptr<Statement>>* target) {
Ethan Nicholase6592142020-09-08 10:22:09 -04001203 switch (stmt->kind()) {
1204 case Statement::Kind::kBlock: {
John Stiles92219b42020-06-15 12:32:24 -04001205 // Recurse into the block.
1206 Block& block = static_cast<Block&>(*stmt);
1207
1208 std::vector<std::unique_ptr<Statement>> blockStmts;
Ethan Nicholas7bd60432020-09-25 14:31:59 -04001209 blockStmts.reserve(block.children().size());
1210 for (std::unique_ptr<Statement>& stmt : block.children()) {
1211 move_all_but_break(stmt, &blockStmts);
Ethan Nicholas739e1ca2020-06-11 12:16:14 -04001212 }
John Stiles92219b42020-06-15 12:32:24 -04001213
1214 target->push_back(std::make_unique<Block>(block.fOffset, std::move(blockStmts),
Ethan Nicholas7bd60432020-09-25 14:31:59 -04001215 block.symbolTable(), block.isScope()));
Ethan Nicholas739e1ca2020-06-11 12:16:14 -04001216 break;
John Stiles92219b42020-06-15 12:32:24 -04001217 }
1218
Ethan Nicholase6592142020-09-08 10:22:09 -04001219 case Statement::Kind::kBreak:
John Stiles92219b42020-06-15 12:32:24 -04001220 // Do not append a break to the target.
1221 break;
1222
Ethan Nicholas739e1ca2020-06-11 12:16:14 -04001223 default:
John Stiles92219b42020-06-15 12:32:24 -04001224 // Append normal statements to the target.
1225 target->push_back(std::move(stmt));
1226 break;
Ethan Nicholas739e1ca2020-06-11 12:16:14 -04001227 }
1228}
1229
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001230// Returns a block containing all of the statements that will be run if the given case matches
1231// (which, owing to the statements being owned by unique_ptrs, means the switch itself will be
1232// broken by this call and must then be discarded).
1233// Returns null (and leaves the switch unmodified) if no such simple reduction is possible, such as
1234// when break statements appear inside conditionals.
John Stiles92219b42020-06-15 12:32:24 -04001235static std::unique_ptr<Statement> block_for_case(SwitchStatement* switchStatement,
1236 SwitchCase* caseToCapture) {
1237 // We have to be careful to not move any of the pointers until after we're sure we're going to
1238 // succeed, so before we make any changes at all, we check the switch-cases to decide on a plan
1239 // of action. First, find the switch-case we are interested in.
1240 auto iter = switchStatement->fCases.begin();
1241 for (; iter != switchStatement->fCases.end(); ++iter) {
1242 if (iter->get() == caseToCapture) {
1243 break;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001244 }
John Stiles92219b42020-06-15 12:32:24 -04001245 }
1246
1247 // Next, walk forward through the rest of the switch. If we find a conditional break, we're
1248 // stuck and can't simplify at all. If we find an unconditional break, we have a range of
1249 // statements that we can use for simplification.
1250 auto startIter = iter;
1251 Statement* unconditionalBreakStmt = nullptr;
1252 for (; iter != switchStatement->fCases.end(); ++iter) {
1253 for (std::unique_ptr<Statement>& stmt : (*iter)->fStatements) {
1254 if (contains_conditional_break(*stmt)) {
1255 // We can't reduce switch-cases to a block when they have conditional breaks.
1256 return nullptr;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001257 }
John Stiles92219b42020-06-15 12:32:24 -04001258
1259 if (contains_unconditional_break(*stmt)) {
1260 // We found an unconditional break. We can use this block, but we need to strip
1261 // out the break statement.
1262 unconditionalBreakStmt = stmt.get();
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001263 break;
1264 }
1265 }
John Stiles92219b42020-06-15 12:32:24 -04001266
1267 if (unconditionalBreakStmt != nullptr) {
1268 break;
1269 }
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001270 }
John Stiles92219b42020-06-15 12:32:24 -04001271
1272 // We fell off the bottom of the switch or encountered a break. We know the range of statements
1273 // that we need to move over, and we know it's safe to do so.
1274 std::vector<std::unique_ptr<Statement>> caseStmts;
1275
1276 // We can move over most of the statements as-is.
1277 while (startIter != iter) {
1278 for (std::unique_ptr<Statement>& stmt : (*startIter)->fStatements) {
1279 caseStmts.push_back(std::move(stmt));
1280 }
1281 ++startIter;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001282 }
John Stiles92219b42020-06-15 12:32:24 -04001283
1284 // If we found an unconditional break at the end, we need to move what we can while avoiding
1285 // that break.
1286 if (unconditionalBreakStmt != nullptr) {
1287 for (std::unique_ptr<Statement>& stmt : (*startIter)->fStatements) {
1288 if (stmt.get() == unconditionalBreakStmt) {
1289 move_all_but_break(stmt, &caseStmts);
1290 unconditionalBreakStmt = nullptr;
1291 break;
1292 }
1293
1294 caseStmts.push_back(std::move(stmt));
1295 }
1296 }
1297
1298 SkASSERT(unconditionalBreakStmt == nullptr); // Verify that we fixed the unconditional break.
1299
1300 // Return our newly-synthesized block.
1301 return std::make_unique<Block>(/*offset=*/-1, std::move(caseStmts), switchStatement->fSymbols);
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001302}
1303
Ethan Nicholascb670962017-04-20 19:31:52 -04001304void Compiler::simplifyStatement(DefinitionMap& definitions,
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001305 BasicBlock& b,
1306 std::vector<BasicBlock::Node>::iterator* iter,
1307 std::unordered_set<const Variable*>* undefinedVariables,
1308 bool* outUpdated,
1309 bool* outNeedsRescan) {
Ethan Nicholascb670962017-04-20 19:31:52 -04001310 Statement* stmt = (*iter)->statement()->get();
Ethan Nicholase6592142020-09-08 10:22:09 -04001311 switch (stmt->kind()) {
1312 case Statement::Kind::kVarDeclaration: {
John Stilesa5a97b42020-08-18 11:19:07 -04001313 const auto& varDecl = stmt->as<VarDeclaration>();
Ethan Nicholas82a62d22017-11-07 14:42:10 +00001314 if (varDecl.fVar->dead() &&
1315 (!varDecl.fValue ||
1316 !varDecl.fValue->hasSideEffects())) {
1317 if (varDecl.fValue) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001318 SkASSERT((*iter)->statement()->get() == stmt);
Ethan Nicholas82a62d22017-11-07 14:42:10 +00001319 if (!b.tryRemoveExpressionBefore(iter, varDecl.fValue.get())) {
1320 *outNeedsRescan = true;
Ethan Nicholascb670962017-04-20 19:31:52 -04001321 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001322 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001323 (*iter)->setStatement(std::unique_ptr<Statement>(new Nop()));
Ethan Nicholas82a62d22017-11-07 14:42:10 +00001324 *outUpdated = true;
Ethan Nicholascb670962017-04-20 19:31:52 -04001325 }
1326 break;
1327 }
Ethan Nicholase6592142020-09-08 10:22:09 -04001328 case Statement::Kind::kIf: {
John Stilesa5a97b42020-08-18 11:19:07 -04001329 IfStatement& i = stmt->as<IfStatement>();
Ethan Nicholase6592142020-09-08 10:22:09 -04001330 if (i.fTest->kind() == Expression::Kind::kBoolLiteral) {
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001331 // constant if, collapse down to a single branch
Ethan Nicholas59d660c2020-09-28 09:18:15 -04001332 if (i.fTest->as<BoolLiteral>().value()) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001333 SkASSERT(i.fIfTrue);
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001334 (*iter)->setStatement(std::move(i.fIfTrue));
1335 } else {
1336 if (i.fIfFalse) {
1337 (*iter)->setStatement(std::move(i.fIfFalse));
1338 } else {
1339 (*iter)->setStatement(std::unique_ptr<Statement>(new Nop()));
1340 }
1341 }
1342 *outUpdated = true;
1343 *outNeedsRescan = true;
1344 break;
1345 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001346 if (i.fIfFalse && i.fIfFalse->isEmpty()) {
1347 // else block doesn't do anything, remove it
1348 i.fIfFalse.reset();
1349 *outUpdated = true;
1350 *outNeedsRescan = true;
1351 }
1352 if (!i.fIfFalse && i.fIfTrue->isEmpty()) {
1353 // if block doesn't do anything, no else block
1354 if (i.fTest->hasSideEffects()) {
1355 // test has side effects, keep it
1356 (*iter)->setStatement(std::unique_ptr<Statement>(
1357 new ExpressionStatement(std::move(i.fTest))));
1358 } else {
1359 // no if, no else, no test side effects, kill the whole if
1360 // statement
1361 (*iter)->setStatement(std::unique_ptr<Statement>(new Nop()));
1362 }
1363 *outUpdated = true;
1364 *outNeedsRescan = true;
1365 }
1366 break;
1367 }
Ethan Nicholase6592142020-09-08 10:22:09 -04001368 case Statement::Kind::kSwitch: {
John Stilesa5a97b42020-08-18 11:19:07 -04001369 SwitchStatement& s = stmt->as<SwitchStatement>();
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001370 int64_t switchValue;
1371 if (fIRGenerator->getConstantInt(*s.fValue, &switchValue)) {
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001372 // switch is constant, replace it with the case that matches
1373 bool found = false;
1374 SwitchCase* defaultCase = nullptr;
John Stiles9d944232020-08-19 09:56:49 -04001375 for (const std::unique_ptr<SwitchCase>& c : s.fCases) {
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001376 if (!c->fValue) {
1377 defaultCase = c.get();
1378 continue;
1379 }
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001380 int64_t caseValue;
1381 SkAssertResult(fIRGenerator->getConstantInt(*c->fValue, &caseValue));
1382 if (caseValue == switchValue) {
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001383 std::unique_ptr<Statement> newBlock = block_for_case(&s, c.get());
1384 if (newBlock) {
1385 (*iter)->setStatement(std::move(newBlock));
John Stiles9d944232020-08-19 09:56:49 -04001386 found = true;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001387 break;
1388 } else {
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -04001389 if (s.fIsStatic && !(fFlags & kPermitInvalidStaticTests_Flag)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001390 this->error(s.fOffset,
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001391 "static switch contains non-static conditional break");
1392 s.fIsStatic = false;
1393 }
1394 return; // can't simplify
1395 }
1396 }
1397 }
1398 if (!found) {
1399 // no matching case. use default if it exists, or kill the whole thing
1400 if (defaultCase) {
1401 std::unique_ptr<Statement> newBlock = block_for_case(&s, defaultCase);
1402 if (newBlock) {
1403 (*iter)->setStatement(std::move(newBlock));
1404 } else {
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -04001405 if (s.fIsStatic && !(fFlags & kPermitInvalidStaticTests_Flag)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001406 this->error(s.fOffset,
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001407 "static switch contains non-static conditional break");
1408 s.fIsStatic = false;
1409 }
1410 return; // can't simplify
1411 }
1412 } else {
1413 (*iter)->setStatement(std::unique_ptr<Statement>(new Nop()));
1414 }
1415 }
1416 *outUpdated = true;
1417 *outNeedsRescan = true;
1418 }
1419 break;
1420 }
Ethan Nicholase6592142020-09-08 10:22:09 -04001421 case Statement::Kind::kExpression: {
John Stilesa5a97b42020-08-18 11:19:07 -04001422 ExpressionStatement& e = stmt->as<ExpressionStatement>();
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001423 SkASSERT((*iter)->statement()->get() == &e);
Ethan Nicholasd503a5a2020-09-30 09:29:55 -04001424 if (!e.expression()->hasSideEffects()) {
Ethan Nicholascb670962017-04-20 19:31:52 -04001425 // Expression statement with no side effects, kill it
Ethan Nicholasd503a5a2020-09-30 09:29:55 -04001426 if (!b.tryRemoveExpressionBefore(iter, e.expression().get())) {
Ethan Nicholascb670962017-04-20 19:31:52 -04001427 *outNeedsRescan = true;
1428 }
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001429 SkASSERT((*iter)->statement()->get() == stmt);
Ethan Nicholascb670962017-04-20 19:31:52 -04001430 (*iter)->setStatement(std::unique_ptr<Statement>(new Nop()));
1431 *outUpdated = true;
1432 }
1433 break;
1434 }
1435 default:
1436 break;
1437 }
1438}
1439
John Stiles0cc193a2020-09-09 09:39:34 -04001440bool Compiler::scanCFG(FunctionDefinition& f) {
1441 bool madeChanges = false;
1442
Ethan Nicholascb670962017-04-20 19:31:52 -04001443 CFG cfg = CFGGenerator().getCFG(f);
1444 this->computeDataFlow(&cfg);
ethannicholas22f939e2016-10-13 13:25:34 -07001445
1446 // check for unreachable code
1447 for (size_t i = 0; i < cfg.fBlocks.size(); i++) {
John Stiles0cc193a2020-09-09 09:39:34 -04001448 const BasicBlock& block = cfg.fBlocks[i];
John Stiles61e75e32020-10-01 15:42:37 -04001449 if (i != cfg.fStart && !block.fIsReachable && block.fNodes.size()) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001450 int offset;
John Stiles0cc193a2020-09-09 09:39:34 -04001451 const BasicBlock::Node& node = block.fNodes[0];
John Stiles70025e52020-09-28 16:08:58 -04001452 if (node.isStatement()) {
1453 offset = (*node.statement())->fOffset;
1454 } else {
1455 offset = (*node.expression())->fOffset;
1456 if ((*node.expression())->is<BoolLiteral>()) {
1457 // Function inlining can generate do { ... } while(false) loops which always
1458 // break, so the boolean condition is considered unreachable. Since not being
1459 // able to reach a literal is a non-issue in the first place, we don't report an
1460 // error in this case.
1461 continue;
1462 }
Ethan Nicholas86a43402017-01-19 13:32:00 -05001463 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001464 this->error(offset, String("unreachable"));
ethannicholas22f939e2016-10-13 13:25:34 -07001465 }
1466 }
1467 if (fErrorCount) {
John Stiles0cc193a2020-09-09 09:39:34 -04001468 return madeChanges;
ethannicholas22f939e2016-10-13 13:25:34 -07001469 }
1470
Ethan Nicholascb670962017-04-20 19:31:52 -04001471 // check for dead code & undefined variables, perform constant propagation
1472 std::unordered_set<const Variable*> undefinedVariables;
1473 bool updated;
1474 bool needsRescan = false;
1475 do {
1476 if (needsRescan) {
1477 cfg = CFGGenerator().getCFG(f);
1478 this->computeDataFlow(&cfg);
1479 needsRescan = false;
Ethan Nicholas113628d2017-02-02 16:11:39 -05001480 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001481
1482 updated = false;
Ethan Nicholas1de14812020-06-19 15:32:49 -04001483 bool first = true;
Ethan Nicholascb670962017-04-20 19:31:52 -04001484 for (BasicBlock& b : cfg.fBlocks) {
John Stiles61e75e32020-10-01 15:42:37 -04001485 if (!first && !b.fIsReachable) {
Ethan Nicholas1de14812020-06-19 15:32:49 -04001486 // Block was reachable before optimization, but has since become unreachable. In
1487 // addition to being dead code, it's broken - since control flow can't reach it, no
1488 // prior variable definitions can reach it, and therefore variables might look to
Brian Osmanc0213602020-10-06 14:43:32 -04001489 // have not been properly assigned. Kill it by replacing all statements with Nops.
Ethan Nicholas1de14812020-06-19 15:32:49 -04001490 for (BasicBlock::Node& node : b.fNodes) {
John Stiles70025e52020-09-28 16:08:58 -04001491 if (node.isStatement() && !(*node.statement())->is<Nop>()) {
John Stiles0cc193a2020-09-09 09:39:34 -04001492 node.setStatement(std::make_unique<Nop>());
1493 madeChanges = true;
Ethan Nicholas1de14812020-06-19 15:32:49 -04001494 }
1495 }
1496 continue;
1497 }
1498 first = false;
Ethan Nicholascb670962017-04-20 19:31:52 -04001499 DefinitionMap definitions = b.fBefore;
1500
1501 for (auto iter = b.fNodes.begin(); iter != b.fNodes.end() && !needsRescan; ++iter) {
John Stiles70025e52020-09-28 16:08:58 -04001502 if (iter->isExpression()) {
Ethan Nicholascb670962017-04-20 19:31:52 -04001503 this->simplifyExpression(definitions, b, &iter, &undefinedVariables, &updated,
1504 &needsRescan);
1505 } else {
1506 this->simplifyStatement(definitions, b, &iter, &undefinedVariables, &updated,
John Stiles0cc193a2020-09-09 09:39:34 -04001507 &needsRescan);
Ethan Nicholascb670962017-04-20 19:31:52 -04001508 }
Ethan Nicholas4b330df2017-05-17 10:52:55 -04001509 if (needsRescan) {
1510 break;
1511 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001512 this->addDefinitions(*iter, &definitions);
1513 }
Brian Osman01a3eb42020-09-14 11:32:49 -04001514
1515 if (needsRescan) {
1516 break;
1517 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001518 }
John Stiles0cc193a2020-09-09 09:39:34 -04001519 madeChanges |= updated;
Ethan Nicholascb670962017-04-20 19:31:52 -04001520 } while (updated);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001521 SkASSERT(!needsRescan);
ethannicholas22f939e2016-10-13 13:25:34 -07001522
Ethan Nicholas91a10532017-06-22 11:24:38 -04001523 // verify static ifs & switches, clean up dead variable decls
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001524 for (BasicBlock& b : cfg.fBlocks) {
1525 DefinitionMap definitions = b.fBefore;
1526
Ethan Nicholas91a10532017-06-22 11:24:38 -04001527 for (auto iter = b.fNodes.begin(); iter != b.fNodes.end() && !needsRescan;) {
John Stiles70025e52020-09-28 16:08:58 -04001528 if (iter->isStatement()) {
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001529 const Statement& s = **iter->statement();
Ethan Nicholase6592142020-09-08 10:22:09 -04001530 switch (s.kind()) {
1531 case Statement::Kind::kIf:
John Stilesa5a97b42020-08-18 11:19:07 -04001532 if (s.as<IfStatement>().fIsStatic &&
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -04001533 !(fFlags & kPermitInvalidStaticTests_Flag)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001534 this->error(s.fOffset, "static if has non-static test");
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001535 }
Ethan Nicholas91a10532017-06-22 11:24:38 -04001536 ++iter;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001537 break;
Ethan Nicholase6592142020-09-08 10:22:09 -04001538 case Statement::Kind::kSwitch:
John Stilesa5a97b42020-08-18 11:19:07 -04001539 if (s.as<SwitchStatement>().fIsStatic &&
John Stiles0cc193a2020-09-09 09:39:34 -04001540 !(fFlags & kPermitInvalidStaticTests_Flag)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001541 this->error(s.fOffset, "static switch has non-static test");
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001542 }
Ethan Nicholas91a10532017-06-22 11:24:38 -04001543 ++iter;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001544 break;
1545 default:
Ethan Nicholas91a10532017-06-22 11:24:38 -04001546 ++iter;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001547 break;
1548 }
Ethan Nicholas91a10532017-06-22 11:24:38 -04001549 } else {
1550 ++iter;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001551 }
1552 }
1553 }
1554
ethannicholas22f939e2016-10-13 13:25:34 -07001555 // check for missing return
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001556 if (f.fDeclaration.fReturnType != *fContext->fVoid_Type) {
John Stiles61e75e32020-10-01 15:42:37 -04001557 if (cfg.fBlocks[cfg.fExit].fIsReachable) {
Ethan Nicholase2c49992020-10-05 11:49:11 -04001558 this->error(f.fOffset, String("function '" + String(f.fDeclaration.name()) +
Ethan Nicholasdb80f692019-11-22 14:06:12 -05001559 "' can exit without returning a value"));
ethannicholas22f939e2016-10-13 13:25:34 -07001560 }
1561 }
John Stiles0cc193a2020-09-09 09:39:34 -04001562
1563 return madeChanges;
ethannicholas22f939e2016-10-13 13:25:34 -07001564}
1565
Brian Osman32d53552020-09-23 13:55:20 -04001566std::unique_ptr<Program> Compiler::convertProgram(
1567 Program::Kind kind,
1568 String text,
1569 const Program::Settings& settings,
1570 const std::vector<std::unique_ptr<ExternalValue>>* externalValues) {
1571 SkASSERT(!externalValues || (kind == Program::kGeneric_Kind));
Ethan Nicholas91164d12019-05-15 15:29:54 -04001572
ethannicholasb3058bd2016-07-01 08:22:01 -07001573 fErrorText = "";
1574 fErrorCount = 0;
John Stiles7b463002020-08-31 17:29:21 -04001575 fInliner.reset(context(), settings);
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001576 std::vector<std::unique_ptr<ProgramElement>>* inherited;
ethannicholasd598f792016-07-25 10:08:54 -07001577 std::vector<std::unique_ptr<ProgramElement>> elements;
ethannicholasb3058bd2016-07-01 08:22:01 -07001578 switch (kind) {
1579 case Program::kVertex_Kind:
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001580 inherited = &fVertexInclude;
John Stiles810c8cf2020-08-26 19:46:27 -04001581 fIRGenerator->fIntrinsics = fGPUIntrinsics.get();
Brian Osmane498b3c2020-09-23 14:42:11 -04001582 fIRGenerator->start(&settings, fVertexSymbolTable, inherited);
ethannicholasb3058bd2016-07-01 08:22:01 -07001583 break;
1584 case Program::kFragment_Kind:
Brian Osman00a8b5b2020-10-02 09:06:04 -04001585 inherited = nullptr;
1586 fIRGenerator->fIntrinsics = fFragmentIntrinsics.get();
1587 fIRGenerator->start(&settings, fFragmentSymbolTable, /*inherited=*/nullptr);
ethannicholasb3058bd2016-07-01 08:22:01 -07001588 break;
Ethan Nicholas52cad152017-02-16 16:37:32 -05001589 case Program::kGeometry_Kind:
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001590 this->loadGeometryIntrinsics();
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001591 inherited = &fGeometryInclude;
John Stiles810c8cf2020-08-26 19:46:27 -04001592 fIRGenerator->fIntrinsics = fGPUIntrinsics.get();
Brian Osmane498b3c2020-09-23 14:42:11 -04001593 fIRGenerator->start(&settings, fGeometrySymbolTable, inherited);
Ethan Nicholas52cad152017-02-16 16:37:32 -05001594 break;
Brian Osman8e2ef022020-09-30 13:26:43 -04001595 case Program::kFragmentProcessor_Kind:
1596 this->loadFPIntrinsics();
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001597 inherited = nullptr;
Brian Osman8e2ef022020-09-30 13:26:43 -04001598 fIRGenerator->fIntrinsics = fFPIntrinsics.get();
1599 fIRGenerator->start(&settings, fFPSymbolTable, /*inherited=*/nullptr);
Ethan Nicholas762466e2017-06-29 10:03:38 -04001600 break;
Ethan Nicholas8da1e652019-05-24 11:01:59 -04001601 case Program::kPipelineStage_Kind:
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001602 this->loadPipelineIntrinsics();
Brian Osman8e2ef022020-09-30 13:26:43 -04001603 inherited = nullptr;
1604 fIRGenerator->fIntrinsics = fPipelineIntrinsics.get();
1605 fIRGenerator->start(&settings, fPipelineSymbolTable, /*inherited=*/nullptr);
Ethan Nicholas8da1e652019-05-24 11:01:59 -04001606 break;
Ethan Nicholas746035a2019-04-23 13:31:09 -04001607 case Program::kGeneric_Kind:
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001608 this->loadInterpreterIntrinsics();
Brian Osmaneac49832020-09-18 11:49:22 -04001609 inherited = nullptr;
John Stiles810c8cf2020-08-26 19:46:27 -04001610 fIRGenerator->fIntrinsics = fInterpreterIntrinsics.get();
Brian Osmane498b3c2020-09-23 14:42:11 -04001611 fIRGenerator->start(&settings, fInterpreterSymbolTable, /*inherited=*/nullptr);
Ethan Nicholas0d997662019-04-08 09:46:01 -04001612 break;
ethannicholasb3058bd2016-07-01 08:22:01 -07001613 }
Brian Osman32d53552020-09-23 13:55:20 -04001614 if (externalValues) {
1615 // Add any external values to the symbol table. IRGenerator::start() has pushed a table, so
1616 // we're only making these visible to the current Program.
1617 for (const auto& ev : *externalValues) {
Ethan Nicholase2c49992020-10-05 11:49:11 -04001618 fIRGenerator->fSymbolTable->addWithoutOwnership(ev->name(), ev.get());
Brian Osman32d53552020-09-23 13:55:20 -04001619 }
1620 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001621 std::unique_ptr<String> textPtr(new String(std::move(text)));
1622 fSource = textPtr.get();
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001623 fIRGenerator->convertProgram(kind, textPtr->c_str(), textPtr->size(), &elements);
John Stilesfbd050b2020-08-03 13:21:46 -04001624 auto result = std::make_unique<Program>(kind,
1625 std::move(textPtr),
1626 settings,
1627 fContext,
1628 inherited,
1629 std::move(elements),
1630 fIRGenerator->fSymbolTable,
1631 fIRGenerator->fInputs);
Brian Osmane498b3c2020-09-23 14:42:11 -04001632 fIRGenerator->finish();
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001633 if (fErrorCount) {
1634 return nullptr;
1635 }
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001636 if (settings.fOptimize && !this->optimize(*result)) {
1637 return nullptr;
1638 }
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001639 return result;
1640}
1641
Ethan Nicholas00543112018-07-31 09:44:36 -04001642bool Compiler::optimize(Program& program) {
1643 SkASSERT(!fErrorCount);
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001644 fIRGenerator->fKind = program.fKind;
1645 fIRGenerator->fSettings = &program.fSettings;
John Stiles7954d6c2020-09-01 10:53:02 -04001646
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001647 while (fErrorCount == 0) {
1648 bool madeChanges = false;
John Stiles7954d6c2020-09-01 10:53:02 -04001649
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001650 // Scan and optimize based on the control-flow graph for each function.
1651 for (ProgramElement& element : program) {
1652 if (element.is<FunctionDefinition>()) {
1653 madeChanges |= this->scanCFG(element.as<FunctionDefinition>());
1654 }
1655 }
1656
1657 // Perform inline-candidate analysis and inline any functions deemed suitable.
John Stiles881a10c2020-09-19 10:13:24 -04001658 madeChanges |= fInliner.analyze(program);
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001659
1660 // Remove dead functions. We wait until after analysis so that we still report errors,
1661 // even in unused code.
1662 if (program.fSettings.fRemoveDeadFunctions) {
1663 program.fElements.erase(
1664 std::remove_if(program.fElements.begin(),
1665 program.fElements.end(),
1666 [&](const std::unique_ptr<ProgramElement>& element) {
1667 if (!element->is<FunctionDefinition>()) {
1668 return false;
1669 }
1670 const auto& fn = element->as<FunctionDefinition>();
1671 bool dead = fn.fDeclaration.fCallCount == 0 &&
Ethan Nicholase2c49992020-10-05 11:49:11 -04001672 fn.fDeclaration.name() != "main";
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001673 madeChanges |= dead;
1674 return dead;
1675 }),
1676 program.fElements.end());
1677 }
1678
1679 if (program.fKind != Program::kFragmentProcessor_Kind) {
Brian Osmanc0213602020-10-06 14:43:32 -04001680 // Remove declarations of dead global variables
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001681 program.fElements.erase(
1682 std::remove_if(program.fElements.begin(), program.fElements.end(),
1683 [&](const std::unique_ptr<ProgramElement>& element) {
Brian Osmanc0213602020-10-06 14:43:32 -04001684 if (!element->is<GlobalVarDeclaration>()) {
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001685 return false;
1686 }
Brian Osmanc0213602020-10-06 14:43:32 -04001687 const auto& varDecl = element->as<GlobalVarDeclaration>();
1688 bool dead = varDecl.fDecl->fVar->dead();
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001689 madeChanges |= dead;
1690 return dead;
1691 }),
1692 program.fElements.end());
1693 }
John Stiles73a6bff2020-09-09 13:40:37 -04001694
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001695 if (!madeChanges) {
1696 break;
Ethan Nicholas0dc80872019-02-08 15:46:24 -05001697 }
Ethan Nicholas00543112018-07-31 09:44:36 -04001698 }
1699 return fErrorCount == 0;
1700}
1701
Brian Osmanfb32ddf2019-06-18 10:14:20 -04001702#if defined(SKSL_STANDALONE) || SK_SUPPORT_GPU
1703
Ethan Nicholas00543112018-07-31 09:44:36 -04001704bool Compiler::toSPIRV(Program& program, OutputStream& out) {
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001705#ifdef SK_ENABLE_SPIRV_VALIDATION
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001706 StringStream buffer;
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001707 fSource = program.fSource.get();
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001708 SPIRVCodeGenerator cg(fContext.get(), &program, this, &buffer);
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001709 bool result = cg.generateCode();
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001710 fSource = nullptr;
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001711 if (result) {
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001712 spvtools::SpirvTools tools(SPV_ENV_VULKAN_1_0);
Ethan Nicholas762466e2017-06-29 10:03:38 -04001713 const String& data = buffer.str();
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001714 SkASSERT(0 == data.size() % 4);
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001715 auto dumpmsg = [](spv_message_level_t, const char*, const spv_position_t&, const char* m) {
1716 SkDebugf("SPIR-V validation error: %s\n", m);
1717 };
1718 tools.SetMessageConsumer(dumpmsg);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001719 // Verify that the SPIR-V we produced is valid. If this SkASSERT fails, check the logs prior
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001720 // to the failure to see the validation errors.
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001721 SkAssertResult(tools.Validate((const uint32_t*) data.c_str(), data.size() / 4));
Ethan Nicholas762466e2017-06-29 10:03:38 -04001722 out.write(data.c_str(), data.size());
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001723 }
1724#else
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001725 fSource = program.fSource.get();
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001726 SPIRVCodeGenerator cg(fContext.get(), &program, this, &out);
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001727 bool result = cg.generateCode();
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001728 fSource = nullptr;
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001729#endif
Ethan Nicholasce33f102016-12-09 17:22:59 -05001730 return result;
1731}
1732
Ethan Nicholas00543112018-07-31 09:44:36 -04001733bool Compiler::toSPIRV(Program& program, String* out) {
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001734 StringStream buffer;
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001735 bool result = this->toSPIRV(program, buffer);
1736 if (result) {
Ethan Nicholas762466e2017-06-29 10:03:38 -04001737 *out = buffer.str();
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001738 }
1739 return result;
1740}
1741
Ethan Nicholas00543112018-07-31 09:44:36 -04001742bool Compiler::toGLSL(Program& program, OutputStream& out) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001743 fSource = program.fSource.get();
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001744 GLSLCodeGenerator cg(fContext.get(), &program, this, &out);
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001745 bool result = cg.generateCode();
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001746 fSource = nullptr;
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001747 return result;
1748}
1749
Ethan Nicholas00543112018-07-31 09:44:36 -04001750bool Compiler::toGLSL(Program& program, String* out) {
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001751 StringStream buffer;
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001752 bool result = this->toGLSL(program, buffer);
1753 if (result) {
Ethan Nicholas762466e2017-06-29 10:03:38 -04001754 *out = buffer.str();
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001755 }
1756 return result;
1757}
1758
Brian Osmanc0243912020-02-19 15:35:26 -05001759bool Compiler::toHLSL(Program& program, String* out) {
1760 String spirv;
1761 if (!this->toSPIRV(program, &spirv)) {
1762 return false;
1763 }
1764
1765 return SPIRVtoHLSL(spirv, out);
1766}
1767
Ethan Nicholas00543112018-07-31 09:44:36 -04001768bool Compiler::toMetal(Program& program, OutputStream& out) {
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001769 MetalCodeGenerator cg(fContext.get(), &program, this, &out);
Ethan Nicholascc305772017-10-13 16:17:45 -04001770 bool result = cg.generateCode();
Ethan Nicholascc305772017-10-13 16:17:45 -04001771 return result;
1772}
1773
Ethan Nicholas00543112018-07-31 09:44:36 -04001774bool Compiler::toMetal(Program& program, String* out) {
Timothy Liangb8eeb802018-07-23 16:46:16 -04001775 StringStream buffer;
1776 bool result = this->toMetal(program, buffer);
1777 if (result) {
1778 *out = buffer.str();
1779 }
1780 return result;
1781}
1782
Greg Daniela28ea672020-09-25 11:12:56 -04001783#if defined(SKSL_STANDALONE) || GR_TEST_UTILS
Ethan Nicholas00543112018-07-31 09:44:36 -04001784bool Compiler::toCPP(Program& program, String name, OutputStream& out) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001785 fSource = program.fSource.get();
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001786 CPPCodeGenerator cg(fContext.get(), &program, this, name, &out);
Ethan Nicholas762466e2017-06-29 10:03:38 -04001787 bool result = cg.generateCode();
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001788 fSource = nullptr;
Ethan Nicholas762466e2017-06-29 10:03:38 -04001789 return result;
1790}
1791
Ethan Nicholas00543112018-07-31 09:44:36 -04001792bool Compiler::toH(Program& program, String name, OutputStream& out) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001793 fSource = program.fSource.get();
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001794 HCodeGenerator cg(fContext.get(), &program, this, name, &out);
Ethan Nicholas762466e2017-06-29 10:03:38 -04001795 bool result = cg.generateCode();
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001796 fSource = nullptr;
Ethan Nicholas00543112018-07-31 09:44:36 -04001797 return result;
1798}
Greg Daniela28ea672020-09-25 11:12:56 -04001799#endif // defined(SKSL_STANDALONE) || GR_TEST_UTILS
Ethan Nicholas00543112018-07-31 09:44:36 -04001800
Ethan Nicholas2a479a52020-08-18 16:29:45 -04001801#endif // defined(SKSL_STANDALONE) || SK_SUPPORT_GPU
Brian Osman2e29ab52019-09-20 12:19:11 -04001802
1803#if !defined(SKSL_STANDALONE) && SK_SUPPORT_GPU
Brian Osmana4b91692020-08-10 14:26:16 -04001804bool Compiler::toPipelineStage(Program& program, PipelineStageArgs* outArgs) {
Ethan Nicholas00543112018-07-31 09:44:36 -04001805 fSource = program.fSource.get();
1806 StringStream buffer;
Brian Osman300fe1d2020-01-23 15:42:43 -05001807 PipelineStageCodeGenerator cg(fContext.get(), &program, this, &buffer, outArgs);
Ethan Nicholas00543112018-07-31 09:44:36 -04001808 bool result = cg.generateCode();
1809 fSource = nullptr;
1810 if (result) {
Brian Osman107c6662019-12-30 15:02:30 -05001811 outArgs->fCode = buffer.str();
Ethan Nicholas00543112018-07-31 09:44:36 -04001812 }
Ethan Nicholas762466e2017-06-29 10:03:38 -04001813 return result;
1814}
Brian Osmanfb32ddf2019-06-18 10:14:20 -04001815#endif
1816
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001817std::unique_ptr<ByteCode> Compiler::toByteCode(Program& program) {
Mike Klein853d4ed2020-08-20 00:41:00 +00001818#if defined(SK_ENABLE_SKSL_INTERPRETER)
Brian Osman808f0212020-01-21 15:36:47 -05001819 fSource = program.fSource.get();
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001820 std::unique_ptr<ByteCode> result(new ByteCode());
Brian Osmanb08cc022020-04-02 11:38:40 -04001821 ByteCodeGenerator cg(fContext.get(), &program, this, result.get());
1822 bool success = cg.generateCode();
1823 fSource = nullptr;
1824 if (success) {
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001825 return result;
1826 }
Mike Klein853d4ed2020-08-20 00:41:00 +00001827#else
1828 ABORT("ByteCode interpreter not enabled");
1829#endif
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001830 return nullptr;
1831}
1832
Brian Osman401a0092020-09-10 14:47:24 -04001833const char* Compiler::OperatorName(Token::Kind op) {
1834 switch (op) {
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001835 case Token::Kind::TK_PLUS: return "+";
1836 case Token::Kind::TK_MINUS: return "-";
1837 case Token::Kind::TK_STAR: return "*";
1838 case Token::Kind::TK_SLASH: return "/";
1839 case Token::Kind::TK_PERCENT: return "%";
1840 case Token::Kind::TK_SHL: return "<<";
1841 case Token::Kind::TK_SHR: return ">>";
1842 case Token::Kind::TK_LOGICALNOT: return "!";
1843 case Token::Kind::TK_LOGICALAND: return "&&";
1844 case Token::Kind::TK_LOGICALOR: return "||";
1845 case Token::Kind::TK_LOGICALXOR: return "^^";
1846 case Token::Kind::TK_BITWISENOT: return "~";
1847 case Token::Kind::TK_BITWISEAND: return "&";
1848 case Token::Kind::TK_BITWISEOR: return "|";
1849 case Token::Kind::TK_BITWISEXOR: return "^";
1850 case Token::Kind::TK_EQ: return "=";
1851 case Token::Kind::TK_EQEQ: return "==";
1852 case Token::Kind::TK_NEQ: return "!=";
1853 case Token::Kind::TK_LT: return "<";
1854 case Token::Kind::TK_GT: return ">";
1855 case Token::Kind::TK_LTEQ: return "<=";
1856 case Token::Kind::TK_GTEQ: return ">=";
1857 case Token::Kind::TK_PLUSEQ: return "+=";
1858 case Token::Kind::TK_MINUSEQ: return "-=";
1859 case Token::Kind::TK_STAREQ: return "*=";
1860 case Token::Kind::TK_SLASHEQ: return "/=";
1861 case Token::Kind::TK_PERCENTEQ: return "%=";
1862 case Token::Kind::TK_SHLEQ: return "<<=";
1863 case Token::Kind::TK_SHREQ: return ">>=";
1864 case Token::Kind::TK_LOGICALANDEQ: return "&&=";
1865 case Token::Kind::TK_LOGICALOREQ: return "||=";
1866 case Token::Kind::TK_LOGICALXOREQ: return "^^=";
1867 case Token::Kind::TK_BITWISEANDEQ: return "&=";
1868 case Token::Kind::TK_BITWISEOREQ: return "|=";
1869 case Token::Kind::TK_BITWISEXOREQ: return "^=";
1870 case Token::Kind::TK_PLUSPLUS: return "++";
1871 case Token::Kind::TK_MINUSMINUS: return "--";
1872 case Token::Kind::TK_COMMA: return ",";
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001873 default:
Brian Osman401a0092020-09-10 14:47:24 -04001874 ABORT("unsupported operator: %d\n", (int) op);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001875 }
1876}
1877
1878
1879bool Compiler::IsAssignment(Token::Kind op) {
1880 switch (op) {
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001881 case Token::Kind::TK_EQ: // fall through
1882 case Token::Kind::TK_PLUSEQ: // fall through
1883 case Token::Kind::TK_MINUSEQ: // fall through
1884 case Token::Kind::TK_STAREQ: // fall through
1885 case Token::Kind::TK_SLASHEQ: // fall through
1886 case Token::Kind::TK_PERCENTEQ: // fall through
1887 case Token::Kind::TK_SHLEQ: // fall through
1888 case Token::Kind::TK_SHREQ: // fall through
1889 case Token::Kind::TK_BITWISEOREQ: // fall through
1890 case Token::Kind::TK_BITWISEXOREQ: // fall through
1891 case Token::Kind::TK_BITWISEANDEQ: // fall through
1892 case Token::Kind::TK_LOGICALOREQ: // fall through
1893 case Token::Kind::TK_LOGICALXOREQ: // fall through
1894 case Token::Kind::TK_LOGICALANDEQ:
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001895 return true;
1896 default:
1897 return false;
1898 }
1899}
1900
Brian Osman401a0092020-09-10 14:47:24 -04001901Token::Kind Compiler::RemoveAssignment(Token::Kind op) {
1902 switch (op) {
1903 case Token::Kind::TK_PLUSEQ: return Token::Kind::TK_PLUS;
1904 case Token::Kind::TK_MINUSEQ: return Token::Kind::TK_MINUS;
1905 case Token::Kind::TK_STAREQ: return Token::Kind::TK_STAR;
1906 case Token::Kind::TK_SLASHEQ: return Token::Kind::TK_SLASH;
1907 case Token::Kind::TK_PERCENTEQ: return Token::Kind::TK_PERCENT;
1908 case Token::Kind::TK_SHLEQ: return Token::Kind::TK_SHL;
1909 case Token::Kind::TK_SHREQ: return Token::Kind::TK_SHR;
1910 case Token::Kind::TK_BITWISEOREQ: return Token::Kind::TK_BITWISEOR;
1911 case Token::Kind::TK_BITWISEXOREQ: return Token::Kind::TK_BITWISEXOR;
1912 case Token::Kind::TK_BITWISEANDEQ: return Token::Kind::TK_BITWISEAND;
1913 case Token::Kind::TK_LOGICALOREQ: return Token::Kind::TK_LOGICALOR;
1914 case Token::Kind::TK_LOGICALXOREQ: return Token::Kind::TK_LOGICALXOR;
1915 case Token::Kind::TK_LOGICALANDEQ: return Token::Kind::TK_LOGICALAND;
1916 default: return op;
1917 }
1918}
1919
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001920Position Compiler::position(int offset) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001921 SkASSERT(fSource);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001922 int line = 1;
1923 int column = 1;
1924 for (int i = 0; i < offset; i++) {
1925 if ((*fSource)[i] == '\n') {
1926 ++line;
1927 column = 1;
1928 }
1929 else {
1930 ++column;
1931 }
1932 }
1933 return Position(line, column);
1934}
1935
1936void Compiler::error(int offset, String msg) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001937 fErrorCount++;
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001938 Position pos = this->position(offset);
1939 fErrorText += "error: " + to_string(pos.fLine) + ": " + msg.c_str() + "\n";
ethannicholasb3058bd2016-07-01 08:22:01 -07001940}
1941
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001942String Compiler::errorText() {
Ethan Nicholas00543112018-07-31 09:44:36 -04001943 this->writeErrorCount();
1944 fErrorCount = 0;
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001945 String result = fErrorText;
ethannicholasb3058bd2016-07-01 08:22:01 -07001946 return result;
1947}
1948
1949void Compiler::writeErrorCount() {
1950 if (fErrorCount) {
1951 fErrorText += to_string(fErrorCount) + " error";
1952 if (fErrorCount > 1) {
1953 fErrorText += "s";
1954 }
1955 fErrorText += "\n";
1956 }
1957}
1958
John Stilesa6841be2020-08-06 14:11:56 -04001959} // namespace SkSL