blob: 25292297ebbb2387c59867bba81d8e23270a6700 [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 Osman3d87e9f2020-10-08 11:50:22 -040049#if defined(SKSL_STANDALONE)
Ethan Nicholasc18bb512020-07-28 14:46:53 -040050
Brian Osman3d87e9f2020-10-08 11:50:22 -040051// In standalone mode, we load the textual sksl source files. GN generates or copies these files
52// to the skslc executable directory. The "data" in this mode is just the filename.
53#define MODULE_DATA(name) MakeModulePath("sksl_" #name ".sksl")
54
55#else
56
57// At runtime, we load the dehydrated sksl data files. The data is a (pointer, size) pair.
Ethan Nicholasc18bb512020-07-28 14:46:53 -040058#include "src/sksl/generated/sksl_fp.dehydrated.sksl"
59#include "src/sksl/generated/sksl_frag.dehydrated.sksl"
60#include "src/sksl/generated/sksl_geom.dehydrated.sksl"
61#include "src/sksl/generated/sksl_gpu.dehydrated.sksl"
62#include "src/sksl/generated/sksl_interp.dehydrated.sksl"
63#include "src/sksl/generated/sksl_pipeline.dehydrated.sksl"
64#include "src/sksl/generated/sksl_vert.dehydrated.sksl"
65
Brian Osman3d87e9f2020-10-08 11:50:22 -040066#define MODULE_DATA(name) MakeModuleData(SKSL_INCLUDE_sksl_##name,\
67 SKSL_INCLUDE_sksl_##name##_LENGTH)
Ethan Nicholasc18bb512020-07-28 14:46:53 -040068
69#endif
Ethan Nicholas0d997662019-04-08 09:46:01 -040070
ethannicholasb3058bd2016-07-01 08:22:01 -070071namespace SkSL {
72
Brian Osman88cda172020-10-09 12:05:16 -040073class AutoSource {
74public:
75 AutoSource(Compiler* compiler, const String* source)
76 : fCompiler(compiler), fOldSource(fCompiler->fSource) {
77 fCompiler->fSource = source;
78 }
79
80 ~AutoSource() { fCompiler->fSource = fOldSource; }
81
82 Compiler* fCompiler;
83 const String* fOldSource;
84};
85
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -040086Compiler::Compiler(Flags flags)
Brian Osman00a8b5b2020-10-02 09:06:04 -040087: fFlags(flags)
John Stiles810c8cf2020-08-26 19:46:27 -040088, fContext(std::make_shared<Context>())
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -040089, fErrorCount(0) {
John Stiles7c3515b2020-10-16 18:38:39 -040090 fRootSymbolTable = std::make_shared<SymbolTable>(this, /*builtin=*/true);
John Stiles4c412bc2020-10-13 11:19:41 -040091 fIRGenerator = std::make_unique<IRGenerator>(fContext.get(), &fInliner, *this);
Brian Osman88cda172020-10-09 12:05:16 -040092#define ADD_TYPE(t) fRootSymbolTable->addWithoutOwnership(fContext->f##t##_Type.get())
ethannicholasb3058bd2016-07-01 08:22:01 -070093 ADD_TYPE(Void);
94 ADD_TYPE(Float);
Ethan Nicholas5af9ea32017-07-28 15:19:46 -040095 ADD_TYPE(Float2);
96 ADD_TYPE(Float3);
97 ADD_TYPE(Float4);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -040098 ADD_TYPE(Half);
99 ADD_TYPE(Half2);
100 ADD_TYPE(Half3);
101 ADD_TYPE(Half4);
ethannicholasb3058bd2016-07-01 08:22:01 -0700102 ADD_TYPE(Int);
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400103 ADD_TYPE(Int2);
104 ADD_TYPE(Int3);
105 ADD_TYPE(Int4);
ethannicholasb3058bd2016-07-01 08:22:01 -0700106 ADD_TYPE(UInt);
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400107 ADD_TYPE(UInt2);
108 ADD_TYPE(UInt3);
109 ADD_TYPE(UInt4);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400110 ADD_TYPE(Short);
111 ADD_TYPE(Short2);
112 ADD_TYPE(Short3);
113 ADD_TYPE(Short4);
114 ADD_TYPE(UShort);
115 ADD_TYPE(UShort2);
116 ADD_TYPE(UShort3);
117 ADD_TYPE(UShort4);
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400118 ADD_TYPE(Byte);
119 ADD_TYPE(Byte2);
120 ADD_TYPE(Byte3);
121 ADD_TYPE(Byte4);
122 ADD_TYPE(UByte);
123 ADD_TYPE(UByte2);
124 ADD_TYPE(UByte3);
125 ADD_TYPE(UByte4);
ethannicholasb3058bd2016-07-01 08:22:01 -0700126 ADD_TYPE(Bool);
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400127 ADD_TYPE(Bool2);
128 ADD_TYPE(Bool3);
129 ADD_TYPE(Bool4);
130 ADD_TYPE(Float2x2);
131 ADD_TYPE(Float2x3);
132 ADD_TYPE(Float2x4);
133 ADD_TYPE(Float3x2);
134 ADD_TYPE(Float3x3);
135 ADD_TYPE(Float3x4);
136 ADD_TYPE(Float4x2);
137 ADD_TYPE(Float4x3);
138 ADD_TYPE(Float4x4);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400139 ADD_TYPE(Half2x2);
140 ADD_TYPE(Half2x3);
141 ADD_TYPE(Half2x4);
142 ADD_TYPE(Half3x2);
143 ADD_TYPE(Half3x3);
144 ADD_TYPE(Half3x4);
145 ADD_TYPE(Half4x2);
146 ADD_TYPE(Half4x3);
147 ADD_TYPE(Half4x4);
ethannicholasb3058bd2016-07-01 08:22:01 -0700148 ADD_TYPE(GenType);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400149 ADD_TYPE(GenHType);
ethannicholasb3058bd2016-07-01 08:22:01 -0700150 ADD_TYPE(GenIType);
151 ADD_TYPE(GenUType);
152 ADD_TYPE(GenBType);
153 ADD_TYPE(Mat);
154 ADD_TYPE(Vec);
155 ADD_TYPE(GVec);
156 ADD_TYPE(GVec2);
157 ADD_TYPE(GVec3);
158 ADD_TYPE(GVec4);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400159 ADD_TYPE(HVec);
ethannicholasb3058bd2016-07-01 08:22:01 -0700160 ADD_TYPE(IVec);
161 ADD_TYPE(UVec);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400162 ADD_TYPE(SVec);
163 ADD_TYPE(USVec);
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400164 ADD_TYPE(ByteVec);
165 ADD_TYPE(UByteVec);
ethannicholasb3058bd2016-07-01 08:22:01 -0700166 ADD_TYPE(BVec);
167
168 ADD_TYPE(Sampler1D);
169 ADD_TYPE(Sampler2D);
170 ADD_TYPE(Sampler3D);
ethannicholas5961bc92016-10-12 06:39:56 -0700171 ADD_TYPE(SamplerExternalOES);
ethannicholasb3058bd2016-07-01 08:22:01 -0700172 ADD_TYPE(SamplerCube);
173 ADD_TYPE(Sampler2DRect);
174 ADD_TYPE(Sampler1DArray);
175 ADD_TYPE(Sampler2DArray);
176 ADD_TYPE(SamplerCubeArray);
177 ADD_TYPE(SamplerBuffer);
178 ADD_TYPE(Sampler2DMS);
179 ADD_TYPE(Sampler2DMSArray);
180
Brian Salomonbf7b6202016-11-11 16:08:03 -0500181 ADD_TYPE(ISampler2D);
182
Brian Salomon2a51de82016-11-16 12:06:01 -0500183 ADD_TYPE(Image2D);
184 ADD_TYPE(IImage2D);
185
Greg Daniel64773e62016-11-22 09:44:03 -0500186 ADD_TYPE(SubpassInput);
187 ADD_TYPE(SubpassInputMS);
188
ethannicholasb3058bd2016-07-01 08:22:01 -0700189 ADD_TYPE(GSampler1D);
190 ADD_TYPE(GSampler2D);
191 ADD_TYPE(GSampler3D);
192 ADD_TYPE(GSamplerCube);
193 ADD_TYPE(GSampler2DRect);
194 ADD_TYPE(GSampler1DArray);
195 ADD_TYPE(GSampler2DArray);
196 ADD_TYPE(GSamplerCubeArray);
197 ADD_TYPE(GSamplerBuffer);
198 ADD_TYPE(GSampler2DMS);
199 ADD_TYPE(GSampler2DMSArray);
200
201 ADD_TYPE(Sampler1DShadow);
202 ADD_TYPE(Sampler2DShadow);
203 ADD_TYPE(SamplerCubeShadow);
204 ADD_TYPE(Sampler2DRectShadow);
205 ADD_TYPE(Sampler1DArrayShadow);
206 ADD_TYPE(Sampler2DArrayShadow);
207 ADD_TYPE(SamplerCubeArrayShadow);
208 ADD_TYPE(GSampler2DArrayShadow);
209 ADD_TYPE(GSamplerCubeArrayShadow);
Ethan Nicholasc9472af2017-10-10 16:30:21 -0400210 ADD_TYPE(FragmentProcessor);
Stephen Whiteff5d7a22019-07-26 17:42:06 -0400211 ADD_TYPE(Sampler);
212 ADD_TYPE(Texture2D);
ethannicholasb3058bd2016-07-01 08:22:01 -0700213
Brian Osman3887a012020-09-30 13:22:27 -0400214 // sk_Caps is "builtin", but all references to it are resolved to Settings, so we don't need to
215 // treat it as builtin (ie, no need to clone it into the Program).
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700216 StringFragment skCapsName("sk_Caps");
John Stilesb8cc6652020-10-08 09:12:07 -0400217 fRootSymbolTable->add(std::make_unique<Variable>(/*offset=*/-1,
Ethan Nicholas041fd0a2020-10-07 16:42:04 -0400218 fIRGenerator->fModifiers->handle(Modifiers()),
Ethan Nicholas453f67f2020-10-09 10:43:45 -0400219 skCapsName,
220 fContext->fSkCaps_Type.get(),
221 /*builtin=*/false,
222 Variable::Storage::kGlobal));
Ethan Nicholas3605ace2016-11-21 15:59:48 -0500223
Brian Osman3d87e9f2020-10-08 11:50:22 -0400224 fRootModule = {fRootSymbolTable, /*fIntrinsics=*/nullptr};
John Stilesbc0c29e2020-09-28 13:13:40 -0400225
Brian Osman3d87e9f2020-10-08 11:50:22 -0400226 fGPUModule = this->parseModule(Program::kFragment_Kind, MODULE_DATA(gpu), fRootModule);
227 fVertexModule = this->parseModule(Program::kVertex_Kind, MODULE_DATA(vert), fGPUModule);
228 fFragmentModule = this->parseModule(Program::kFragment_Kind, MODULE_DATA(frag), fGPUModule);
ethannicholasb3058bd2016-07-01 08:22:01 -0700229}
230
John Stilesdd13dba2020-10-29 10:45:34 -0400231Compiler::~Compiler() {}
ethannicholasb3058bd2016-07-01 08:22:01 -0700232
Brian Osman88cda172020-10-09 12:05:16 -0400233const ParsedModule& Compiler::loadGeometryModule() {
Brian Osman3d87e9f2020-10-08 11:50:22 -0400234 if (!fGeometryModule.fSymbols) {
235 fGeometryModule = this->parseModule(Program::kGeometry_Kind, MODULE_DATA(geom), fGPUModule);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400236 }
Brian Osman88cda172020-10-09 12:05:16 -0400237 return fGeometryModule;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400238}
239
Brian Osman88cda172020-10-09 12:05:16 -0400240const ParsedModule& Compiler::loadFPModule() {
Brian Osman3d87e9f2020-10-08 11:50:22 -0400241 if (!fFPModule.fSymbols) {
242 fFPModule =
243 this->parseModule(Program::kFragmentProcessor_Kind, MODULE_DATA(fp), fGPUModule);
Brian Osman8e2ef022020-09-30 13:26:43 -0400244 }
Brian Osman88cda172020-10-09 12:05:16 -0400245 return fFPModule;
Brian Osman8e2ef022020-09-30 13:26:43 -0400246}
247
Brian Osman88cda172020-10-09 12:05:16 -0400248const ParsedModule& Compiler::loadPipelineModule() {
Brian Osman3d87e9f2020-10-08 11:50:22 -0400249 if (!fPipelineModule.fSymbols) {
250 fPipelineModule =
251 this->parseModule(Program::kPipelineStage_Kind, MODULE_DATA(pipeline), fGPUModule);
Brian Osmanf1319c32020-10-13 09:34:23 -0400252
253 // Add some aliases to the pipeline module so that it's friendlier, and more like GLSL
254 fPipelineModule.fSymbols->addAlias("shader", fContext->fFragmentProcessor_Type.get());
255
256 fPipelineModule.fSymbols->addAlias("vec2", fContext->fFloat2_Type.get());
257 fPipelineModule.fSymbols->addAlias("vec3", fContext->fFloat3_Type.get());
258 fPipelineModule.fSymbols->addAlias("vec4", fContext->fFloat4_Type.get());
259
260 fPipelineModule.fSymbols->addAlias("bvec2", fContext->fBool2_Type.get());
261 fPipelineModule.fSymbols->addAlias("bvec3", fContext->fBool3_Type.get());
262 fPipelineModule.fSymbols->addAlias("bvec4", fContext->fBool4_Type.get());
263
264 fPipelineModule.fSymbols->addAlias("mat2", fContext->fFloat2x2_Type.get());
265 fPipelineModule.fSymbols->addAlias("mat3", fContext->fFloat3x3_Type.get());
266 fPipelineModule.fSymbols->addAlias("mat4", fContext->fFloat4x4_Type.get());
267
268 fPipelineModule.fSymbols->addAlias("mat2x2", fContext->fFloat2x2_Type.get());
269 fPipelineModule.fSymbols->addAlias("mat2x3", fContext->fFloat2x3_Type.get());
270 fPipelineModule.fSymbols->addAlias("mat2x4", fContext->fFloat2x4_Type.get());
271
272 fPipelineModule.fSymbols->addAlias("mat3x2", fContext->fFloat3x2_Type.get());
273 fPipelineModule.fSymbols->addAlias("mat3x3", fContext->fFloat3x3_Type.get());
274 fPipelineModule.fSymbols->addAlias("mat3x4", fContext->fFloat3x4_Type.get());
275
276 fPipelineModule.fSymbols->addAlias("mat4x2", fContext->fFloat4x2_Type.get());
277 fPipelineModule.fSymbols->addAlias("mat4x3", fContext->fFloat4x3_Type.get());
278 fPipelineModule.fSymbols->addAlias("mat4x4", fContext->fFloat4x4_Type.get());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400279 }
Brian Osman88cda172020-10-09 12:05:16 -0400280 return fPipelineModule;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400281}
282
Brian Osman88cda172020-10-09 12:05:16 -0400283const ParsedModule& Compiler::loadInterpreterModule() {
Brian Osman3d87e9f2020-10-08 11:50:22 -0400284 if (!fInterpreterModule.fSymbols) {
285 fInterpreterModule =
286 this->parseModule(Program::kGeneric_Kind, MODULE_DATA(interp), fRootModule);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400287 }
Brian Osman88cda172020-10-09 12:05:16 -0400288 return fInterpreterModule;
289}
290
291const ParsedModule& Compiler::moduleForProgramKind(Program::Kind kind) {
292 switch (kind) {
293 case Program::kVertex_Kind: return fVertexModule; break;
294 case Program::kFragment_Kind: return fFragmentModule; break;
295 case Program::kGeometry_Kind: return this->loadGeometryModule(); break;
296 case Program::kFragmentProcessor_Kind: return this->loadFPModule(); break;
297 case Program::kPipelineStage_Kind: return this->loadPipelineModule(); break;
298 case Program::kGeneric_Kind: return this->loadInterpreterModule(); break;
299 }
300 SkUNREACHABLE;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400301}
302
Brian Osman3d87e9f2020-10-08 11:50:22 -0400303LoadedModule Compiler::loadModule(Program::Kind kind,
304 ModuleData data,
305 std::shared_ptr<SymbolTable> base) {
Brian Osman3d87e9f2020-10-08 11:50:22 -0400306 if (!base) {
307 base = fRootSymbolTable;
308 }
309
310#if defined(SKSL_STANDALONE)
311 SkASSERT(data.fPath);
312 std::ifstream in(data.fPath);
Brian Osmane498b3c2020-09-23 14:42:11 -0400313 std::unique_ptr<String> text = std::make_unique<String>(std::istreambuf_iterator<char>(in),
314 std::istreambuf_iterator<char>());
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -0400315 if (in.rdstate()) {
Brian Osman3d87e9f2020-10-08 11:50:22 -0400316 printf("error reading %s\n", data.fPath);
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -0400317 abort();
318 }
Brian Osmane498b3c2020-09-23 14:42:11 -0400319 const String* source = fRootSymbolTable->takeOwnershipOfString(std::move(text));
Brian Osman88cda172020-10-09 12:05:16 -0400320 AutoSource as(this, source);
Ethan Nicholas8da1e652019-05-24 11:01:59 -0400321 Program::Settings settings;
John Stiles881a10c2020-09-19 10:13:24 -0400322 SkASSERT(fIRGenerator->fCanInline);
323 fIRGenerator->fCanInline = false;
Brian Osman88cda172020-10-09 12:05:16 -0400324 ParsedModule baseModule = {base, /*fIntrinsics=*/nullptr};
325 IRGenerator::IRBundle ir = fIRGenerator->convertProgram(
326 kind, &settings, baseModule, /*isBuiltinCode=*/true, source->c_str(), source->length(),
327 /*externalValues=*/nullptr);
328 LoadedModule module = { std::move(ir.fSymbolTable), std::move(ir.fElements) };
John Stiles881a10c2020-09-19 10:13:24 -0400329 fIRGenerator->fCanInline = true;
Ethan Nicholas8da1e652019-05-24 11:01:59 -0400330 if (this->fErrorCount) {
331 printf("Unexpected errors: %s\n", this->fErrorText.c_str());
Brian Osman3d87e9f2020-10-08 11:50:22 -0400332 SkDEBUGFAILF("%s %s\n", data.fPath, this->fErrorText.c_str());
Ethan Nicholas8da1e652019-05-24 11:01:59 -0400333 }
Brian Osman88cda172020-10-09 12:05:16 -0400334 fModifiers.push_back(std::move(ir.fModifiers));
Brian Osman3d87e9f2020-10-08 11:50:22 -0400335#else
336 SkASSERT(data.fData && (data.fSize != 0));
337 Rehydrator rehydrator(fContext.get(), fIRGenerator->fModifiers.get(), base, this,
338 data.fData, data.fSize);
Brian Osman88cda172020-10-09 12:05:16 -0400339 LoadedModule module = { rehydrator.symbolTable(), rehydrator.elements() };
Brian Osman3d87e9f2020-10-08 11:50:22 -0400340 fModifiers.push_back(fIRGenerator->releaseModifiers());
341#endif
342
343 return module;
344}
345
346ParsedModule Compiler::parseModule(Program::Kind kind, ModuleData data, const ParsedModule& base) {
347 auto [symbols, elements] = this->loadModule(kind, data, base.fSymbols);
348
349 // For modules that just declare (but don't define) intrinsic functions, there will be no new
350 // program elements. In that case, we can share our parent's intrinsic map:
351 if (elements.empty()) {
352 return {symbols, base.fIntrinsics};
353 }
354
355 auto intrinsics = std::make_shared<IRIntrinsicMap>(base.fIntrinsics.get());
356
357 // Now, transfer all of the program elements to an intrinsic map. This maps certain types of
358 // global objects to the declaring ProgramElement.
359 for (std::unique_ptr<ProgramElement>& element : elements) {
360 switch (element->kind()) {
361 case ProgramElement::Kind::kFunction: {
362 const FunctionDefinition& f = element->as<FunctionDefinition>();
Ethan Nicholas0a5d0962020-10-14 13:33:18 -0400363 SkASSERT(f.declaration().isBuiltin());
364 intrinsics->insertOrDie(f.declaration().description(), std::move(element));
Brian Osman3d87e9f2020-10-08 11:50:22 -0400365 break;
366 }
John Stiles569249b2020-11-03 12:18:22 -0500367 case ProgramElement::Kind::kFunctionPrototype: {
368 // These are already in the symbol table.
369 break;
370 }
Brian Osman3d87e9f2020-10-08 11:50:22 -0400371 case ProgramElement::Kind::kEnum: {
372 const Enum& e = element->as<Enum>();
373 SkASSERT(e.isBuiltin());
374 intrinsics->insertOrDie(e.typeName(), std::move(element));
375 break;
376 }
377 case ProgramElement::Kind::kGlobalVar: {
Ethan Nicholasc51f33e2020-10-13 13:49:44 -0400378 const GlobalVarDeclaration& global = element->as<GlobalVarDeclaration>();
379 const Variable& var = global.declaration()->as<VarDeclaration>().var();
380 SkASSERT(var.isBuiltin());
381 intrinsics->insertOrDie(var.name(), std::move(element));
Brian Osman3d87e9f2020-10-08 11:50:22 -0400382 break;
383 }
384 case ProgramElement::Kind::kInterfaceBlock: {
Ethan Nicholaseaf47882020-10-15 10:10:08 -0400385 const Variable& var = element->as<InterfaceBlock>().variable();
386 SkASSERT(var.isBuiltin());
387 intrinsics->insertOrDie(var.name(), std::move(element));
Brian Osman3d87e9f2020-10-08 11:50:22 -0400388 break;
389 }
390 default:
391 printf("Unsupported element: %s\n", element->description().c_str());
392 SkASSERT(false);
393 break;
394 }
395 }
396
397 return {symbols, std::move(intrinsics)};
Ethan Nicholas8da1e652019-05-24 11:01:59 -0400398}
399
ethannicholas22f939e2016-10-13 13:25:34 -0700400// add the definition created by assigning to the lvalue to the definition set
Ethan Nicholas86a43402017-01-19 13:32:00 -0500401void Compiler::addDefinition(const Expression* lvalue, std::unique_ptr<Expression>* expr,
402 DefinitionMap* definitions) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400403 switch (lvalue->kind()) {
404 case Expression::Kind::kVariableReference: {
Ethan Nicholas78686922020-10-08 06:46:27 -0400405 const Variable& var = *lvalue->as<VariableReference>().variable();
Ethan Nicholas453f67f2020-10-09 10:43:45 -0400406 if (var.storage() == Variable::Storage::kLocal) {
John Stiles796cdb72020-10-08 12:06:53 -0400407 definitions->set(&var, expr);
ethannicholas22f939e2016-10-13 13:25:34 -0700408 }
409 break;
410 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400411 case Expression::Kind::kSwizzle:
ethannicholas22f939e2016-10-13 13:25:34 -0700412 // We consider the variable written to as long as at least some of its components have
413 // been written to. This will lead to some false negatives (we won't catch it if you
414 // write to foo.x and then read foo.y), but being stricter could lead to false positives
Mike Klein6ad99092016-10-26 10:35:22 -0400415 // (we write to foo.x, and then pass foo to a function which happens to only read foo.x,
416 // 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 -0700417 // more complicated whole-program analysis. This is probably good enough.
Ethan Nicholas6b4d5812020-10-12 16:11:51 -0400418 this->addDefinition(lvalue->as<Swizzle>().base().get(),
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400419 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
ethannicholas22f939e2016-10-13 13:25:34 -0700420 definitions);
421 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400422 case Expression::Kind::kIndex:
ethannicholas22f939e2016-10-13 13:25:34 -0700423 // see comments in Swizzle
Ethan Nicholas2a4952d2020-10-08 15:35:56 -0400424 this->addDefinition(lvalue->as<IndexExpression>().base().get(),
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400425 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
ethannicholas22f939e2016-10-13 13:25:34 -0700426 definitions);
427 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400428 case Expression::Kind::kFieldAccess:
ethannicholas22f939e2016-10-13 13:25:34 -0700429 // see comments in Swizzle
Ethan Nicholas7a95b202020-10-09 11:55:40 -0400430 this->addDefinition(lvalue->as<FieldAccess>().base().get(),
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400431 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
ethannicholas22f939e2016-10-13 13:25:34 -0700432 definitions);
433 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400434 case Expression::Kind::kTernary:
Ethan Nicholasa583b812018-01-18 13:32:11 -0500435 // To simplify analysis, we just pretend that we write to both sides of the ternary.
436 // This allows for false positives (meaning we fail to detect that a variable might not
437 // have been assigned), but is preferable to false negatives.
Ethan Nicholasdd218162020-10-08 05:48:01 -0400438 this->addDefinition(lvalue->as<TernaryExpression>().ifTrue().get(),
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400439 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
Ethan Nicholasa583b812018-01-18 13:32:11 -0500440 definitions);
Ethan Nicholasdd218162020-10-08 05:48:01 -0400441 this->addDefinition(lvalue->as<TernaryExpression>().ifFalse().get(),
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400442 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
Ethan Nicholasa583b812018-01-18 13:32:11 -0500443 definitions);
444 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400445 case Expression::Kind::kExternalValue:
Ethan Nicholas91164d12019-05-15 15:29:54 -0400446 break;
ethannicholas22f939e2016-10-13 13:25:34 -0700447 default:
448 // not an lvalue, can't happen
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400449 SkASSERT(false);
ethannicholas22f939e2016-10-13 13:25:34 -0700450 }
451}
452
453// add local variables defined by this node to the set
John Stiles796cdb72020-10-08 12:06:53 -0400454void Compiler::addDefinitions(const BasicBlock::Node& node, DefinitionMap* definitions) {
John Stiles70025e52020-09-28 16:08:58 -0400455 if (node.isExpression()) {
456 Expression* expr = node.expression()->get();
457 switch (expr->kind()) {
458 case Expression::Kind::kBinary: {
459 BinaryExpression* b = &expr->as<BinaryExpression>();
460 if (b->getOperator() == Token::Kind::TK_EQ) {
John Stiles2d4f9592020-10-30 10:29:12 -0400461 this->addDefinition(b->left().get(), &b->right(), definitions);
John Stiles70025e52020-09-28 16:08:58 -0400462 } else if (Compiler::IsAssignment(b->getOperator())) {
463 this->addDefinition(
John Stiles2d4f9592020-10-30 10:29:12 -0400464 b->left().get(),
John Stiles70025e52020-09-28 16:08:58 -0400465 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
466 definitions);
Ethan Nicholas86a43402017-01-19 13:32:00 -0500467
ethannicholas22f939e2016-10-13 13:25:34 -0700468 }
John Stiles70025e52020-09-28 16:08:58 -0400469 break;
ethannicholas22f939e2016-10-13 13:25:34 -0700470 }
John Stiles70025e52020-09-28 16:08:58 -0400471 case Expression::Kind::kFunctionCall: {
472 const FunctionCall& c = expr->as<FunctionCall>();
Brian Osman5bf3e202020-10-13 10:34:18 -0400473 const std::vector<const Variable*>& parameters = c.function().parameters();
Ethan Nicholased84b732020-10-08 11:45:44 -0400474 for (size_t i = 0; i < parameters.size(); ++i) {
475 if (parameters[i]->modifiers().fFlags & Modifiers::kOut_Flag) {
John Stiles70025e52020-09-28 16:08:58 -0400476 this->addDefinition(
Ethan Nicholas0dec9922020-10-05 15:51:52 -0400477 c.arguments()[i].get(),
John Stiles70025e52020-09-28 16:08:58 -0400478 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
479 definitions);
480 }
481 }
482 break;
483 }
484 case Expression::Kind::kPrefix: {
485 const PrefixExpression* p = &expr->as<PrefixExpression>();
Ethan Nicholas444ccc62020-10-09 10:16:22 -0400486 if (p->getOperator() == Token::Kind::TK_MINUSMINUS ||
487 p->getOperator() == Token::Kind::TK_PLUSPLUS) {
John Stiles70025e52020-09-28 16:08:58 -0400488 this->addDefinition(
Ethan Nicholas444ccc62020-10-09 10:16:22 -0400489 p->operand().get(),
John Stiles70025e52020-09-28 16:08:58 -0400490 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
491 definitions);
492 }
493 break;
494 }
495 case Expression::Kind::kPostfix: {
496 const PostfixExpression* p = &expr->as<PostfixExpression>();
Ethan Nicholas444ccc62020-10-09 10:16:22 -0400497 if (p->getOperator() == Token::Kind::TK_MINUSMINUS ||
498 p->getOperator() == Token::Kind::TK_PLUSPLUS) {
John Stiles70025e52020-09-28 16:08:58 -0400499 this->addDefinition(
Ethan Nicholas444ccc62020-10-09 10:16:22 -0400500 p->operand().get(),
John Stiles70025e52020-09-28 16:08:58 -0400501 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
502 definitions);
503 }
504 break;
505 }
506 case Expression::Kind::kVariableReference: {
507 const VariableReference* v = &expr->as<VariableReference>();
Ethan Nicholas453f67f2020-10-09 10:43:45 -0400508 if (v->refKind() != VariableReference::RefKind::kRead) {
John Stiles70025e52020-09-28 16:08:58 -0400509 this->addDefinition(
510 v,
511 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
512 definitions);
513 }
514 break;
515 }
516 default:
517 break;
ethannicholas22f939e2016-10-13 13:25:34 -0700518 }
John Stiles70025e52020-09-28 16:08:58 -0400519 } else if (node.isStatement()) {
520 Statement* stmt = node.statement()->get();
521 if (stmt->is<VarDeclaration>()) {
522 VarDeclaration& vd = stmt->as<VarDeclaration>();
Ethan Nicholasc51f33e2020-10-13 13:49:44 -0400523 if (vd.value()) {
524 definitions->set(&vd.var(), &vd.value());
ethannicholas22f939e2016-10-13 13:25:34 -0700525 }
ethannicholas22f939e2016-10-13 13:25:34 -0700526 }
527 }
528}
529
John Stilese6150002020-10-05 12:03:53 -0400530void Compiler::scanCFG(CFG* cfg, BlockId blockId, SkBitSet* processedSet) {
ethannicholas22f939e2016-10-13 13:25:34 -0700531 BasicBlock& block = cfg->fBlocks[blockId];
532
533 // compute definitions after this block
Ethan Nicholas86a43402017-01-19 13:32:00 -0500534 DefinitionMap after = block.fBefore;
ethannicholas22f939e2016-10-13 13:25:34 -0700535 for (const BasicBlock::Node& n : block.fNodes) {
536 this->addDefinitions(n, &after);
537 }
538
539 // propagate definitions to exits
540 for (BlockId exitId : block.fExits) {
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400541 if (exitId == blockId) {
542 continue;
543 }
ethannicholas22f939e2016-10-13 13:25:34 -0700544 BasicBlock& exit = cfg->fBlocks[exitId];
John Stiles796cdb72020-10-08 12:06:53 -0400545 after.foreach([&](const Variable* var, std::unique_ptr<Expression>** e1Ptr) {
546 std::unique_ptr<Expression>* e1 = *e1Ptr;
547 std::unique_ptr<Expression>** exitDef = exit.fBefore.find(var);
548 if (!exitDef) {
John Stilese6150002020-10-05 12:03:53 -0400549 // exit has no definition for it, just copy it and reprocess exit block
550 processedSet->reset(exitId);
John Stiles796cdb72020-10-08 12:06:53 -0400551 exit.fBefore[var] = e1;
ethannicholas22f939e2016-10-13 13:25:34 -0700552 } else {
Ethan Nicholas86a43402017-01-19 13:32:00 -0500553 // exit has a (possibly different) value already defined
John Stiles796cdb72020-10-08 12:06:53 -0400554 std::unique_ptr<Expression>* e2 = *exitDef;
ethannicholas22f939e2016-10-13 13:25:34 -0700555 if (e1 != e2) {
John Stilese6150002020-10-05 12:03:53 -0400556 // definition has changed, merge and reprocess the exit block
557 processedSet->reset(exitId);
Ethan Nicholasaf197692017-02-27 13:26:45 -0500558 if (e1 && e2) {
John Stiles796cdb72020-10-08 12:06:53 -0400559 *exitDef = (std::unique_ptr<Expression>*)&fContext->fDefined_Expression;
Ethan Nicholasaf197692017-02-27 13:26:45 -0500560 } else {
John Stiles796cdb72020-10-08 12:06:53 -0400561 *exitDef = nullptr;
Ethan Nicholasaf197692017-02-27 13:26:45 -0500562 }
ethannicholas22f939e2016-10-13 13:25:34 -0700563 }
564 }
John Stiles796cdb72020-10-08 12:06:53 -0400565 });
ethannicholas22f939e2016-10-13 13:25:34 -0700566 }
567}
568
569// returns a map which maps all local variables in the function to null, indicating that their value
570// is initially unknown
Ethan Nicholas86a43402017-01-19 13:32:00 -0500571static DefinitionMap compute_start_state(const CFG& cfg) {
572 DefinitionMap result;
Mike Klein6ad99092016-10-26 10:35:22 -0400573 for (const auto& block : cfg.fBlocks) {
574 for (const auto& node : block.fNodes) {
John Stiles70025e52020-09-28 16:08:58 -0400575 if (node.isStatement()) {
Ethan Nicholascb670962017-04-20 19:31:52 -0400576 const Statement* s = node.statement()->get();
Brian Osmanc0213602020-10-06 14:43:32 -0400577 if (s->is<VarDeclaration>()) {
Ethan Nicholasc51f33e2020-10-13 13:49:44 -0400578 result[&s->as<VarDeclaration>().var()] = nullptr;
ethannicholas22f939e2016-10-13 13:25:34 -0700579 }
580 }
581 }
582 }
583 return result;
584}
585
Ethan Nicholascb670962017-04-20 19:31:52 -0400586/**
587 * Returns true if assigning to this lvalue has no effect.
588 */
Brian Osman010ce6a2020-10-19 16:34:10 -0400589static bool is_dead(const Expression& lvalue, ProgramUsage* usage) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400590 switch (lvalue.kind()) {
591 case Expression::Kind::kVariableReference:
Brian Osman010ce6a2020-10-19 16:34:10 -0400592 return usage->isDead(*lvalue.as<VariableReference>().variable());
Ethan Nicholase6592142020-09-08 10:22:09 -0400593 case Expression::Kind::kSwizzle:
Brian Osman010ce6a2020-10-19 16:34:10 -0400594 return is_dead(*lvalue.as<Swizzle>().base(), usage);
Ethan Nicholase6592142020-09-08 10:22:09 -0400595 case Expression::Kind::kFieldAccess:
Brian Osman010ce6a2020-10-19 16:34:10 -0400596 return is_dead(*lvalue.as<FieldAccess>().base(), usage);
Ethan Nicholase6592142020-09-08 10:22:09 -0400597 case Expression::Kind::kIndex: {
John Stilesa5a97b42020-08-18 11:19:07 -0400598 const IndexExpression& idx = lvalue.as<IndexExpression>();
Brian Osman010ce6a2020-10-19 16:34:10 -0400599 return is_dead(*idx.base(), usage) &&
Ethan Nicholas2a4952d2020-10-08 15:35:56 -0400600 !idx.index()->hasProperty(Expression::Property::kSideEffects);
Ethan Nicholascb670962017-04-20 19:31:52 -0400601 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400602 case Expression::Kind::kTernary: {
John Stilesa5a97b42020-08-18 11:19:07 -0400603 const TernaryExpression& t = lvalue.as<TernaryExpression>();
Brian Osman010ce6a2020-10-19 16:34:10 -0400604 return !t.test()->hasSideEffects() &&
605 is_dead(*t.ifTrue(), usage) &&
606 is_dead(*t.ifFalse(), usage);
Ethan Nicholasa583b812018-01-18 13:32:11 -0500607 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400608 case Expression::Kind::kExternalValue:
Ethan Nicholas91164d12019-05-15 15:29:54 -0400609 return false;
Ethan Nicholascb670962017-04-20 19:31:52 -0400610 default:
Ethan Nicholas2a099da2020-01-02 14:40:54 -0500611#ifdef SK_DEBUG
Ethan Nicholascb670962017-04-20 19:31:52 -0400612 ABORT("invalid lvalue: %s\n", lvalue.description().c_str());
Ethan Nicholas2a099da2020-01-02 14:40:54 -0500613#endif
614 return false;
Ethan Nicholascb670962017-04-20 19:31:52 -0400615 }
616}
ethannicholas22f939e2016-10-13 13:25:34 -0700617
Ethan Nicholascb670962017-04-20 19:31:52 -0400618/**
619 * Returns true if this is an assignment which can be collapsed down to just the right hand side due
620 * to a dead target and lack of side effects on the left hand side.
621 */
Brian Osman010ce6a2020-10-19 16:34:10 -0400622static bool dead_assignment(const BinaryExpression& b, ProgramUsage* usage) {
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400623 if (!Compiler::IsAssignment(b.getOperator())) {
Ethan Nicholascb670962017-04-20 19:31:52 -0400624 return false;
625 }
John Stiles2d4f9592020-10-30 10:29:12 -0400626 return is_dead(*b.left(), usage);
Ethan Nicholascb670962017-04-20 19:31:52 -0400627}
628
629void Compiler::computeDataFlow(CFG* cfg) {
630 cfg->fBlocks[cfg->fStart].fBefore = compute_start_state(*cfg);
John Stilese6150002020-10-05 12:03:53 -0400631
632 // We set bits in the "processed" set after a block has been scanned.
633 SkBitSet processedSet(cfg->fBlocks.size());
634 while (SkBitSet::OptionalIndex blockId = processedSet.findFirstUnset()) {
635 processedSet.set(*blockId);
636 this->scanCFG(cfg, *blockId, &processedSet);
ethannicholas22f939e2016-10-13 13:25:34 -0700637 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400638}
639
640/**
641 * Attempts to replace the expression pointed to by iter with a new one (in both the CFG and the
642 * IR). If the expression can be cleanly removed, returns true and updates the iterator to point to
643 * the newly-inserted element. Otherwise updates only the IR and returns false (and the CFG will
644 * need to be regenerated).
645 */
John Stilesafbf8992020-08-18 10:08:21 -0400646static bool try_replace_expression(BasicBlock* b,
647 std::vector<BasicBlock::Node>::iterator* iter,
648 std::unique_ptr<Expression>* newExpression) {
Ethan Nicholascb670962017-04-20 19:31:52 -0400649 std::unique_ptr<Expression>* target = (*iter)->expression();
650 if (!b->tryRemoveExpression(iter)) {
651 *target = std::move(*newExpression);
652 return false;
653 }
654 *target = std::move(*newExpression);
655 return b->tryInsertExpression(iter, target);
656}
657
658/**
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400659 * Returns true if the expression is a constant numeric literal with the specified value, or a
660 * constant vector with all elements equal to the specified value.
Ethan Nicholascb670962017-04-20 19:31:52 -0400661 */
Ethan Nicholasa3f22f12020-10-01 12:13:17 -0400662template <typename T = SKSL_FLOAT>
John Stiles9d944232020-08-19 09:56:49 -0400663static bool is_constant(const Expression& expr, T value) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400664 switch (expr.kind()) {
665 case Expression::Kind::kIntLiteral:
Ethan Nicholase96cdd12020-09-28 16:27:18 -0400666 return expr.as<IntLiteral>().value() == value;
John Stiles9d944232020-08-19 09:56:49 -0400667
Ethan Nicholase6592142020-09-08 10:22:09 -0400668 case Expression::Kind::kFloatLiteral:
Ethan Nicholasa3f22f12020-10-01 12:13:17 -0400669 return expr.as<FloatLiteral>().value() == value;
John Stiles9d944232020-08-19 09:56:49 -0400670
Ethan Nicholase6592142020-09-08 10:22:09 -0400671 case Expression::Kind::kConstructor: {
John Stiles9d944232020-08-19 09:56:49 -0400672 const Constructor& constructor = expr.as<Constructor>();
673 if (constructor.isCompileTimeConstant()) {
Ethan Nicholas30d30222020-09-11 12:27:26 -0400674 const Type& constructorType = constructor.type();
675 bool isFloat = constructorType.columns() > 1
676 ? constructorType.componentType().isFloat()
677 : constructorType.isFloat();
678 switch (constructorType.typeKind()) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400679 case Type::TypeKind::kVector:
Ethan Nicholas30d30222020-09-11 12:27:26 -0400680 for (int i = 0; i < constructorType.columns(); ++i) {
John Stiles9d944232020-08-19 09:56:49 -0400681 if (isFloat) {
682 if (constructor.getFVecComponent(i) != value) {
683 return false;
684 }
685 } else {
686 if (constructor.getIVecComponent(i) != value) {
687 return false;
688 }
689 }
Ethan Nicholasd188c182019-06-10 15:55:38 -0400690 }
John Stiles9d944232020-08-19 09:56:49 -0400691 return true;
692
Ethan Nicholase6592142020-09-08 10:22:09 -0400693 case Type::TypeKind::kScalar:
Ethan Nicholasf70f0442020-09-29 12:41:35 -0400694 SkASSERT(constructor.arguments().size() == 1);
695 return is_constant<T>(*constructor.arguments()[0], value);
John Stiles9d944232020-08-19 09:56:49 -0400696
697 default:
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400698 return false;
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400699 }
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400700 }
701 return false;
702 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400703 default:
704 return false;
705 }
706}
707
708/**
709 * Collapses the binary expression pointed to by iter down to just the right side (in both the IR
710 * and CFG structures).
711 */
John Stilesafbf8992020-08-18 10:08:21 -0400712static void delete_left(BasicBlock* b,
713 std::vector<BasicBlock::Node>::iterator* iter,
Ethan Nicholascdeae8c2020-10-22 14:39:46 -0400714 Compiler::OptimizationContext* optimizationContext) {
715 optimizationContext->fUpdated = true;
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400716 std::unique_ptr<Expression>* target = (*iter)->expression();
John Stilesa5a97b42020-08-18 11:19:07 -0400717 BinaryExpression& bin = (*target)->as<BinaryExpression>();
John Stiles2d4f9592020-10-30 10:29:12 -0400718 Expression& left = *bin.left();
719 std::unique_ptr<Expression>& rightPointer = bin.right();
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400720 SkASSERT(!left.hasSideEffects());
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400721 bool result;
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400722 if (bin.getOperator() == Token::Kind::TK_EQ) {
723 result = b->tryRemoveLValueBefore(iter, &left);
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400724 } else {
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400725 result = b->tryRemoveExpressionBefore(iter, &left);
Ethan Nicholascb670962017-04-20 19:31:52 -0400726 }
Brian Osman010ce6a2020-10-19 16:34:10 -0400727 // Remove references within LHS.
728 optimizationContext->fUsage->remove(&left);
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400729 *target = std::move(rightPointer);
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400730 if (!result) {
Ethan Nicholascdeae8c2020-10-22 14:39:46 -0400731 optimizationContext->fNeedsRescan = true;
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400732 return;
733 }
734 if (*iter == b->fNodes.begin()) {
Ethan Nicholascdeae8c2020-10-22 14:39:46 -0400735 optimizationContext->fNeedsRescan = true;
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400736 return;
737 }
738 --(*iter);
John Stiles70025e52020-09-28 16:08:58 -0400739 if (!(*iter)->isExpression() || (*iter)->expression() != &rightPointer) {
Ethan Nicholascdeae8c2020-10-22 14:39:46 -0400740 optimizationContext->fNeedsRescan = true;
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400741 return;
742 }
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400743 *iter = b->fNodes.erase(*iter);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400744 SkASSERT((*iter)->expression() == target);
Ethan Nicholascb670962017-04-20 19:31:52 -0400745}
746
747/**
748 * Collapses the binary expression pointed to by iter down to just the left side (in both the IR and
749 * CFG structures).
750 */
John Stilesafbf8992020-08-18 10:08:21 -0400751static void delete_right(BasicBlock* b,
752 std::vector<BasicBlock::Node>::iterator* iter,
Ethan Nicholascdeae8c2020-10-22 14:39:46 -0400753 Compiler::OptimizationContext* optimizationContext) {
754 optimizationContext->fUpdated = true;
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400755 std::unique_ptr<Expression>* target = (*iter)->expression();
John Stilesa5a97b42020-08-18 11:19:07 -0400756 BinaryExpression& bin = (*target)->as<BinaryExpression>();
John Stiles2d4f9592020-10-30 10:29:12 -0400757 std::unique_ptr<Expression>& leftPointer = bin.left();
758 Expression& right = *bin.right();
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400759 SkASSERT(!right.hasSideEffects());
Brian Osman010ce6a2020-10-19 16:34:10 -0400760 // Remove references within RHS.
761 optimizationContext->fUsage->remove(&right);
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400762 if (!b->tryRemoveExpressionBefore(iter, &right)) {
763 *target = std::move(leftPointer);
Ethan Nicholascdeae8c2020-10-22 14:39:46 -0400764 optimizationContext->fNeedsRescan = true;
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400765 return;
Ethan Nicholascb670962017-04-20 19:31:52 -0400766 }
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400767 *target = std::move(leftPointer);
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400768 if (*iter == b->fNodes.begin()) {
Ethan Nicholascdeae8c2020-10-22 14:39:46 -0400769 optimizationContext->fNeedsRescan = true;
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400770 return;
771 }
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400772 --(*iter);
John Stiles70025e52020-09-28 16:08:58 -0400773 if ((!(*iter)->isExpression() || (*iter)->expression() != &leftPointer)) {
Ethan Nicholascdeae8c2020-10-22 14:39:46 -0400774 optimizationContext->fNeedsRescan = true;
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400775 return;
776 }
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400777 *iter = b->fNodes.erase(*iter);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400778 SkASSERT((*iter)->expression() == target);
Ethan Nicholascb670962017-04-20 19:31:52 -0400779}
780
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400781/**
782 * Constructs the specified type using a single argument.
783 */
Ethan Nicholas30d30222020-09-11 12:27:26 -0400784static std::unique_ptr<Expression> construct(const Type* type, std::unique_ptr<Expression> v) {
John Stiles8e3b6be2020-10-13 11:14:08 -0400785 ExpressionArray args;
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400786 args.push_back(std::move(v));
Ethan Nicholase6592142020-09-08 10:22:09 -0400787 std::unique_ptr<Expression> result = std::make_unique<Constructor>(-1, type, std::move(args));
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400788 return result;
789}
790
791/**
792 * Used in the implementations of vectorize_left and vectorize_right. Given a vector type and an
793 * expression x, deletes the expression pointed to by iter and replaces it with <type>(x).
794 */
795static void vectorize(BasicBlock* b,
796 std::vector<BasicBlock::Node>::iterator* iter,
797 const Type& type,
798 std::unique_ptr<Expression>* otherExpression,
Ethan Nicholascdeae8c2020-10-22 14:39:46 -0400799 Compiler::OptimizationContext* optimizationContext) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400800 SkASSERT((*(*iter)->expression())->kind() == Expression::Kind::kBinary);
801 SkASSERT(type.typeKind() == Type::TypeKind::kVector);
Ethan Nicholas30d30222020-09-11 12:27:26 -0400802 SkASSERT((*otherExpression)->type().typeKind() == Type::TypeKind::kScalar);
Ethan Nicholascdeae8c2020-10-22 14:39:46 -0400803 optimizationContext->fUpdated = true;
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400804 std::unique_ptr<Expression>* target = (*iter)->expression();
805 if (!b->tryRemoveExpression(iter)) {
Ethan Nicholas30d30222020-09-11 12:27:26 -0400806 *target = construct(&type, std::move(*otherExpression));
Ethan Nicholascdeae8c2020-10-22 14:39:46 -0400807 optimizationContext->fNeedsRescan = true;
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400808 } else {
Ethan Nicholas30d30222020-09-11 12:27:26 -0400809 *target = construct(&type, std::move(*otherExpression));
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400810 if (!b->tryInsertExpression(iter, target)) {
Ethan Nicholascdeae8c2020-10-22 14:39:46 -0400811 optimizationContext->fNeedsRescan = true;
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400812 }
813 }
814}
815
816/**
817 * Given a binary expression of the form x <op> vec<n>(y), deletes the right side and vectorizes the
818 * left to yield vec<n>(x).
819 */
820static void vectorize_left(BasicBlock* b,
821 std::vector<BasicBlock::Node>::iterator* iter,
Ethan Nicholascdeae8c2020-10-22 14:39:46 -0400822 Compiler::OptimizationContext* optimizationContext) {
John Stilesa5a97b42020-08-18 11:19:07 -0400823 BinaryExpression& bin = (*(*iter)->expression())->as<BinaryExpression>();
Brian Osman010ce6a2020-10-19 16:34:10 -0400824 // Remove references within RHS. Vectorization of LHS doesn't change reference counts.
John Stiles2d4f9592020-10-30 10:29:12 -0400825 optimizationContext->fUsage->remove(bin.right().get());
826 vectorize(b, iter, bin.right()->type(), &bin.left(), optimizationContext);
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400827}
828
829/**
830 * Given a binary expression of the form vec<n>(x) <op> y, deletes the left side and vectorizes the
831 * right to yield vec<n>(y).
832 */
833static void vectorize_right(BasicBlock* b,
834 std::vector<BasicBlock::Node>::iterator* iter,
Ethan Nicholascdeae8c2020-10-22 14:39:46 -0400835 Compiler::OptimizationContext* optimizationContext) {
John Stilesa5a97b42020-08-18 11:19:07 -0400836 BinaryExpression& bin = (*(*iter)->expression())->as<BinaryExpression>();
Brian Osman010ce6a2020-10-19 16:34:10 -0400837 // Remove references within LHS. Vectorization of RHS doesn't change reference counts.
John Stiles2d4f9592020-10-30 10:29:12 -0400838 optimizationContext->fUsage->remove(bin.left().get());
839 vectorize(b, iter, bin.left()->type(), &bin.right(), optimizationContext);
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400840}
841
842// Mark that an expression which we were writing to is no longer being written to
John Stilesa5a97b42020-08-18 11:19:07 -0400843static void clear_write(Expression& expr) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400844 switch (expr.kind()) {
845 case Expression::Kind::kVariableReference: {
Ethan Nicholas453f67f2020-10-09 10:43:45 -0400846 expr.as<VariableReference>().setRefKind(VariableReference::RefKind::kRead);
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400847 break;
848 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400849 case Expression::Kind::kFieldAccess:
Ethan Nicholas7a95b202020-10-09 11:55:40 -0400850 clear_write(*expr.as<FieldAccess>().base());
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400851 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400852 case Expression::Kind::kSwizzle:
Ethan Nicholas6b4d5812020-10-12 16:11:51 -0400853 clear_write(*expr.as<Swizzle>().base());
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400854 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400855 case Expression::Kind::kIndex:
Ethan Nicholas2a4952d2020-10-08 15:35:56 -0400856 clear_write(*expr.as<IndexExpression>().base());
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400857 break;
858 default:
859 ABORT("shouldn't be writing to this kind of expression\n");
860 break;
861 }
862}
863
Ethan Nicholascb670962017-04-20 19:31:52 -0400864void Compiler::simplifyExpression(DefinitionMap& definitions,
865 BasicBlock& b,
866 std::vector<BasicBlock::Node>::iterator* iter,
Ethan Nicholascdeae8c2020-10-22 14:39:46 -0400867 OptimizationContext* optimizationContext) {
Ethan Nicholascb670962017-04-20 19:31:52 -0400868 Expression* expr = (*iter)->expression()->get();
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400869 SkASSERT(expr);
Ethan Nicholascb670962017-04-20 19:31:52 -0400870 if ((*iter)->fConstantPropagation) {
Ethan Nicholascdeae8c2020-10-22 14:39:46 -0400871 std::unique_ptr<Expression> optimized = expr->constantPropagate(*fIRGenerator,
872 definitions);
Ethan Nicholascb670962017-04-20 19:31:52 -0400873 if (optimized) {
Ethan Nicholascdeae8c2020-10-22 14:39:46 -0400874 optimizationContext->fUpdated = true;
Ethan Nicholas30d30222020-09-11 12:27:26 -0400875 optimized = fIRGenerator->coerce(std::move(optimized), expr->type());
Ethan Nicholas1d881522020-09-11 09:32:54 -0400876 SkASSERT(optimized);
Brian Osman010ce6a2020-10-19 16:34:10 -0400877 // Remove references within 'expr', add references within 'optimized'
878 optimizationContext->fUsage->replace(expr, optimized.get());
Ethan Nicholascb670962017-04-20 19:31:52 -0400879 if (!try_replace_expression(&b, iter, &optimized)) {
Ethan Nicholascdeae8c2020-10-22 14:39:46 -0400880 optimizationContext->fNeedsRescan = true;
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400881 return;
Ethan Nicholascb670962017-04-20 19:31:52 -0400882 }
John Stiles70025e52020-09-28 16:08:58 -0400883 SkASSERT((*iter)->isExpression());
Ethan Nicholascb670962017-04-20 19:31:52 -0400884 expr = (*iter)->expression()->get();
Ethan Nicholascb670962017-04-20 19:31:52 -0400885 }
886 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400887 switch (expr->kind()) {
888 case Expression::Kind::kVariableReference: {
John Stilesa5a97b42020-08-18 11:19:07 -0400889 const VariableReference& ref = expr->as<VariableReference>();
Ethan Nicholas78686922020-10-08 06:46:27 -0400890 const Variable* var = ref.variable();
Ethan Nicholas453f67f2020-10-09 10:43:45 -0400891 if (ref.refKind() != VariableReference::RefKind::kWrite &&
892 ref.refKind() != VariableReference::RefKind::kPointer &&
893 var->storage() == Variable::Storage::kLocal && !definitions[var] &&
Ethan Nicholascdeae8c2020-10-22 14:39:46 -0400894 optimizationContext->fSilences.find(var) == optimizationContext->fSilences.end()) {
895 optimizationContext->fSilences.insert(var);
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000896 this->error(expr->fOffset,
Ethan Nicholase2c49992020-10-05 11:49:11 -0400897 "'" + var->name() + "' has not been assigned");
Ethan Nicholascb670962017-04-20 19:31:52 -0400898 }
899 break;
900 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400901 case Expression::Kind::kTernary: {
John Stiles403a3632020-08-20 12:11:48 -0400902 TernaryExpression* t = &expr->as<TernaryExpression>();
Ethan Nicholasdd218162020-10-08 05:48:01 -0400903 if (t->test()->is<BoolLiteral>()) {
Ethan Nicholascb670962017-04-20 19:31:52 -0400904 // ternary has a constant test, replace it with either the true or
905 // false branch
Ethan Nicholasdd218162020-10-08 05:48:01 -0400906 if (t->test()->as<BoolLiteral>().value()) {
Brian Osman010ce6a2020-10-19 16:34:10 -0400907 (*iter)->setExpression(std::move(t->ifTrue()), optimizationContext->fUsage);
Ethan Nicholascb670962017-04-20 19:31:52 -0400908 } else {
Brian Osman010ce6a2020-10-19 16:34:10 -0400909 (*iter)->setExpression(std::move(t->ifFalse()), optimizationContext->fUsage);
Ethan Nicholascb670962017-04-20 19:31:52 -0400910 }
Ethan Nicholascdeae8c2020-10-22 14:39:46 -0400911 optimizationContext->fUpdated = true;
912 optimizationContext->fNeedsRescan = true;
Ethan Nicholascb670962017-04-20 19:31:52 -0400913 }
914 break;
915 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400916 case Expression::Kind::kBinary: {
John Stiles403a3632020-08-20 12:11:48 -0400917 BinaryExpression* bin = &expr->as<BinaryExpression>();
Brian Osman010ce6a2020-10-19 16:34:10 -0400918 if (dead_assignment(*bin, optimizationContext->fUsage)) {
Ethan Nicholascdeae8c2020-10-22 14:39:46 -0400919 delete_left(&b, iter, optimizationContext);
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400920 break;
921 }
John Stiles2d4f9592020-10-30 10:29:12 -0400922 Expression& left = *bin->left();
923 Expression& right = *bin->right();
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400924 const Type& leftType = left.type();
925 const Type& rightType = right.type();
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400926 // collapse useless expressions like x * 1 or x + 0
Ethan Nicholas30d30222020-09-11 12:27:26 -0400927 if (((leftType.typeKind() != Type::TypeKind::kScalar) &&
928 (leftType.typeKind() != Type::TypeKind::kVector)) ||
929 ((rightType.typeKind() != Type::TypeKind::kScalar) &&
930 (rightType.typeKind() != Type::TypeKind::kVector))) {
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400931 break;
932 }
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400933 switch (bin->getOperator()) {
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400934 case Token::Kind::TK_STAR:
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400935 if (is_constant(left, 1)) {
Ethan Nicholas30d30222020-09-11 12:27:26 -0400936 if (leftType.typeKind() == Type::TypeKind::kVector &&
937 rightType.typeKind() == Type::TypeKind::kScalar) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400938 // float4(1) * x -> float4(x)
Ethan Nicholascdeae8c2020-10-22 14:39:46 -0400939 vectorize_right(&b, iter, optimizationContext);
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400940 } else {
941 // 1 * x -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400942 // 1 * float4(x) -> float4(x)
943 // float4(1) * float4(x) -> float4(x)
Ethan Nicholascdeae8c2020-10-22 14:39:46 -0400944 delete_left(&b, iter, optimizationContext);
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400945 }
946 }
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400947 else if (is_constant(left, 0)) {
Ethan Nicholas30d30222020-09-11 12:27:26 -0400948 if (leftType.typeKind() == Type::TypeKind::kScalar &&
949 rightType.typeKind() == Type::TypeKind::kVector &&
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400950 !right.hasSideEffects()) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400951 // 0 * float4(x) -> float4(0)
Ethan Nicholascdeae8c2020-10-22 14:39:46 -0400952 vectorize_left(&b, iter, optimizationContext);
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400953 } else {
954 // 0 * x -> 0
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400955 // float4(0) * x -> float4(0)
956 // float4(0) * float4(x) -> float4(0)
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400957 if (!right.hasSideEffects()) {
Ethan Nicholascdeae8c2020-10-22 14:39:46 -0400958 delete_right(&b, iter, optimizationContext);
Ethan Nicholas51493ee2017-12-11 12:34:33 -0500959 }
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400960 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400961 }
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400962 else if (is_constant(right, 1)) {
Ethan Nicholas30d30222020-09-11 12:27:26 -0400963 if (leftType.typeKind() == Type::TypeKind::kScalar &&
964 rightType.typeKind() == Type::TypeKind::kVector) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400965 // x * float4(1) -> float4(x)
Ethan Nicholascdeae8c2020-10-22 14:39:46 -0400966 vectorize_left(&b, iter, optimizationContext);
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400967 } else {
968 // x * 1 -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400969 // float4(x) * 1 -> float4(x)
970 // float4(x) * float4(1) -> float4(x)
Ethan Nicholascdeae8c2020-10-22 14:39:46 -0400971 delete_right(&b, iter, optimizationContext);
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400972 }
973 }
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400974 else if (is_constant(right, 0)) {
Ethan Nicholas30d30222020-09-11 12:27:26 -0400975 if (leftType.typeKind() == Type::TypeKind::kVector &&
976 rightType.typeKind() == Type::TypeKind::kScalar &&
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400977 !left.hasSideEffects()) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400978 // float4(x) * 0 -> float4(0)
Ethan Nicholascdeae8c2020-10-22 14:39:46 -0400979 vectorize_right(&b, iter, optimizationContext);
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400980 } else {
981 // x * 0 -> 0
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400982 // x * float4(0) -> float4(0)
983 // float4(x) * float4(0) -> float4(0)
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400984 if (!left.hasSideEffects()) {
Ethan Nicholascdeae8c2020-10-22 14:39:46 -0400985 delete_left(&b, iter, optimizationContext);
Ethan Nicholas51493ee2017-12-11 12:34:33 -0500986 }
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400987 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400988 }
989 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400990 case Token::Kind::TK_PLUS:
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400991 if (is_constant(left, 0)) {
Ethan Nicholas30d30222020-09-11 12:27:26 -0400992 if (leftType.typeKind() == Type::TypeKind::kVector &&
993 rightType.typeKind() == Type::TypeKind::kScalar) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400994 // float4(0) + x -> float4(x)
Ethan Nicholascdeae8c2020-10-22 14:39:46 -0400995 vectorize_right(&b, iter, optimizationContext);
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400996 } else {
997 // 0 + x -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400998 // 0 + float4(x) -> float4(x)
999 // float4(0) + float4(x) -> float4(x)
Ethan Nicholascdeae8c2020-10-22 14:39:46 -04001000 delete_left(&b, iter, optimizationContext);
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001001 }
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -04001002 } else if (is_constant(right, 0)) {
Ethan Nicholas30d30222020-09-11 12:27:26 -04001003 if (leftType.typeKind() == Type::TypeKind::kScalar &&
1004 rightType.typeKind() == Type::TypeKind::kVector) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001005 // x + float4(0) -> float4(x)
Ethan Nicholascdeae8c2020-10-22 14:39:46 -04001006 vectorize_left(&b, iter, optimizationContext);
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001007 } else {
1008 // x + 0 -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001009 // float4(x) + 0 -> float4(x)
1010 // float4(x) + float4(0) -> float4(x)
Ethan Nicholascdeae8c2020-10-22 14:39:46 -04001011 delete_right(&b, iter, optimizationContext);
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001012 }
Ethan Nicholas56e42712017-04-21 10:23:37 -04001013 }
1014 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001015 case Token::Kind::TK_MINUS:
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -04001016 if (is_constant(right, 0)) {
Ethan Nicholas30d30222020-09-11 12:27:26 -04001017 if (leftType.typeKind() == Type::TypeKind::kScalar &&
1018 rightType.typeKind() == Type::TypeKind::kVector) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001019 // x - float4(0) -> float4(x)
Ethan Nicholascdeae8c2020-10-22 14:39:46 -04001020 vectorize_left(&b, iter, optimizationContext);
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001021 } else {
1022 // x - 0 -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001023 // float4(x) - 0 -> float4(x)
1024 // float4(x) - float4(0) -> float4(x)
Ethan Nicholascdeae8c2020-10-22 14:39:46 -04001025 delete_right(&b, iter, optimizationContext);
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001026 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001027 }
1028 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001029 case Token::Kind::TK_SLASH:
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -04001030 if (is_constant(right, 1)) {
Ethan Nicholas30d30222020-09-11 12:27:26 -04001031 if (leftType.typeKind() == Type::TypeKind::kScalar &&
1032 rightType.typeKind() == Type::TypeKind::kVector) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001033 // x / float4(1) -> float4(x)
Ethan Nicholascdeae8c2020-10-22 14:39:46 -04001034 vectorize_left(&b, iter, optimizationContext);
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001035 } else {
1036 // x / 1 -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001037 // float4(x) / 1 -> float4(x)
1038 // float4(x) / float4(1) -> float4(x)
Ethan Nicholascdeae8c2020-10-22 14:39:46 -04001039 delete_right(&b, iter, optimizationContext);
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001040 }
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -04001041 } else if (is_constant(left, 0)) {
Ethan Nicholas30d30222020-09-11 12:27:26 -04001042 if (leftType.typeKind() == Type::TypeKind::kScalar &&
1043 rightType.typeKind() == Type::TypeKind::kVector &&
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -04001044 !right.hasSideEffects()) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001045 // 0 / float4(x) -> float4(0)
Ethan Nicholascdeae8c2020-10-22 14:39:46 -04001046 vectorize_left(&b, iter, optimizationContext);
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001047 } else {
1048 // 0 / x -> 0
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001049 // float4(0) / x -> float4(0)
1050 // float4(0) / float4(x) -> float4(0)
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -04001051 if (!right.hasSideEffects()) {
Ethan Nicholascdeae8c2020-10-22 14:39:46 -04001052 delete_right(&b, iter, optimizationContext);
Ethan Nicholas51493ee2017-12-11 12:34:33 -05001053 }
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001054 }
1055 }
1056 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001057 case Token::Kind::TK_PLUSEQ:
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -04001058 if (is_constant(right, 0)) {
1059 clear_write(left);
Ethan Nicholascdeae8c2020-10-22 14:39:46 -04001060 delete_right(&b, iter, optimizationContext);
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001061 }
1062 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001063 case Token::Kind::TK_MINUSEQ:
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -04001064 if (is_constant(right, 0)) {
1065 clear_write(left);
Ethan Nicholascdeae8c2020-10-22 14:39:46 -04001066 delete_right(&b, iter, optimizationContext);
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001067 }
1068 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001069 case Token::Kind::TK_STAREQ:
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -04001070 if (is_constant(right, 1)) {
1071 clear_write(left);
Ethan Nicholascdeae8c2020-10-22 14:39:46 -04001072 delete_right(&b, iter, optimizationContext);
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001073 }
1074 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001075 case Token::Kind::TK_SLASHEQ:
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -04001076 if (is_constant(right, 1)) {
1077 clear_write(left);
Ethan Nicholascdeae8c2020-10-22 14:39:46 -04001078 delete_right(&b, iter, optimizationContext);
Ethan Nicholascb670962017-04-20 19:31:52 -04001079 }
1080 break;
1081 default:
1082 break;
1083 }
Ethan Nicholas409f6f02019-09-17 12:34:39 -04001084 break;
1085 }
Ethan Nicholase6592142020-09-08 10:22:09 -04001086 case Expression::Kind::kSwizzle: {
John Stiles403a3632020-08-20 12:11:48 -04001087 Swizzle& s = expr->as<Swizzle>();
Ethan Nicholas409f6f02019-09-17 12:34:39 -04001088 // detect identity swizzles like foo.rgba
Ethan Nicholas6b4d5812020-10-12 16:11:51 -04001089 if ((int) s.components().size() == s.base()->type().columns()) {
Ethan Nicholas409f6f02019-09-17 12:34:39 -04001090 bool identity = true;
Ethan Nicholas6b4d5812020-10-12 16:11:51 -04001091 for (int i = 0; i < (int) s.components().size(); ++i) {
1092 if (s.components()[i] != i) {
Ethan Nicholas409f6f02019-09-17 12:34:39 -04001093 identity = false;
1094 break;
1095 }
1096 }
1097 if (identity) {
Ethan Nicholascdeae8c2020-10-22 14:39:46 -04001098 optimizationContext->fUpdated = true;
Brian Osman010ce6a2020-10-19 16:34:10 -04001099 // No fUsage change: foo.rgba and foo have equivalent reference counts
Ethan Nicholas6b4d5812020-10-12 16:11:51 -04001100 if (!try_replace_expression(&b, iter, &s.base())) {
Ethan Nicholascdeae8c2020-10-22 14:39:46 -04001101 optimizationContext->fNeedsRescan = true;
Ethan Nicholas409f6f02019-09-17 12:34:39 -04001102 return;
1103 }
John Stiles70025e52020-09-28 16:08:58 -04001104 SkASSERT((*iter)->isExpression());
Ethan Nicholas409f6f02019-09-17 12:34:39 -04001105 break;
1106 }
1107 }
1108 // detect swizzles of swizzles, e.g. replace foo.argb.r000 with foo.a000
Ethan Nicholas6b4d5812020-10-12 16:11:51 -04001109 if (s.base()->kind() == Expression::Kind::kSwizzle) {
1110 Swizzle& base = s.base()->as<Swizzle>();
John Stiles750109b2020-10-30 13:45:46 -04001111 ComponentArray final;
Ethan Nicholas6b4d5812020-10-12 16:11:51 -04001112 for (int c : s.components()) {
1113 final.push_back(base.components()[c]);
Ethan Nicholas409f6f02019-09-17 12:34:39 -04001114 }
Ethan Nicholascdeae8c2020-10-22 14:39:46 -04001115 optimizationContext->fUpdated = true;
Ethan Nicholas6b4d5812020-10-12 16:11:51 -04001116 std::unique_ptr<Expression> replacement(new Swizzle(*fContext, base.base()->clone(),
John Stiles750109b2020-10-30 13:45:46 -04001117 final));
Brian Osman010ce6a2020-10-19 16:34:10 -04001118 // No fUsage change: foo.gbr.gbr and foo.brg have equivalent reference counts
Ethan Nicholas409f6f02019-09-17 12:34:39 -04001119 if (!try_replace_expression(&b, iter, &replacement)) {
Ethan Nicholascdeae8c2020-10-22 14:39:46 -04001120 optimizationContext->fNeedsRescan = true;
Ethan Nicholas409f6f02019-09-17 12:34:39 -04001121 return;
1122 }
John Stiles70025e52020-09-28 16:08:58 -04001123 SkASSERT((*iter)->isExpression());
Ethan Nicholas409f6f02019-09-17 12:34:39 -04001124 }
John Stiles30212b72020-06-11 17:55:07 -04001125 break;
Ethan Nicholascb670962017-04-20 19:31:52 -04001126 }
1127 default:
1128 break;
1129 }
1130}
1131
John Stiles92219b42020-06-15 12:32:24 -04001132// Returns true if this statement could potentially execute a break at the current level. We ignore
1133// nested loops and switches, since any breaks inside of them will merely break the loop / switch.
John Stilesb92641c2020-08-31 18:09:01 -04001134static bool contains_conditional_break(Statement& stmt) {
1135 class ContainsConditionalBreak : public ProgramVisitor {
1136 public:
1137 bool visitStatement(const Statement& stmt) override {
Ethan Nicholase6592142020-09-08 10:22:09 -04001138 switch (stmt.kind()) {
1139 case Statement::Kind::kBlock:
John Stilesb92641c2020-08-31 18:09:01 -04001140 return this->INHERITED::visitStatement(stmt);
1141
Ethan Nicholase6592142020-09-08 10:22:09 -04001142 case Statement::Kind::kBreak:
John Stilesb92641c2020-08-31 18:09:01 -04001143 return fInConditional > 0;
1144
Ethan Nicholase6592142020-09-08 10:22:09 -04001145 case Statement::Kind::kIf: {
John Stilesb92641c2020-08-31 18:09:01 -04001146 ++fInConditional;
1147 bool result = this->INHERITED::visitStatement(stmt);
1148 --fInConditional;
1149 return result;
1150 }
1151
1152 default:
1153 return false;
1154 }
1155 }
1156
1157 int fInConditional = 0;
1158 using INHERITED = ProgramVisitor;
1159 };
1160
1161 return ContainsConditionalBreak{}.visitStatement(stmt);
John Stiles92219b42020-06-15 12:32:24 -04001162}
1163
Ethan Nicholas5005a222018-08-24 13:06:27 -04001164// returns true if this statement definitely executes a break at the current level (we ignore
1165// nested loops and switches, since any breaks inside of them will merely break the loop / switch)
John Stilesb92641c2020-08-31 18:09:01 -04001166static bool contains_unconditional_break(Statement& stmt) {
1167 class ContainsUnconditionalBreak : public ProgramVisitor {
1168 public:
1169 bool visitStatement(const Statement& stmt) override {
Ethan Nicholase6592142020-09-08 10:22:09 -04001170 switch (stmt.kind()) {
1171 case Statement::Kind::kBlock:
John Stilesb92641c2020-08-31 18:09:01 -04001172 return this->INHERITED::visitStatement(stmt);
1173
Ethan Nicholase6592142020-09-08 10:22:09 -04001174 case Statement::Kind::kBreak:
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001175 return true;
John Stilesb92641c2020-08-31 18:09:01 -04001176
1177 default:
1178 return false;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001179 }
John Stilesb92641c2020-08-31 18:09:01 -04001180 }
John Stiles92219b42020-06-15 12:32:24 -04001181
John Stilesb92641c2020-08-31 18:09:01 -04001182 using INHERITED = ProgramVisitor;
1183 };
John Stiles92219b42020-06-15 12:32:24 -04001184
John Stilesb92641c2020-08-31 18:09:01 -04001185 return ContainsUnconditionalBreak{}.visitStatement(stmt);
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001186}
1187
John Stiles8f2a0cf2020-10-13 12:48:21 -04001188static void move_all_but_break(std::unique_ptr<Statement>& stmt, StatementArray* target) {
Ethan Nicholase6592142020-09-08 10:22:09 -04001189 switch (stmt->kind()) {
1190 case Statement::Kind::kBlock: {
John Stiles92219b42020-06-15 12:32:24 -04001191 // Recurse into the block.
1192 Block& block = static_cast<Block&>(*stmt);
1193
John Stiles8f2a0cf2020-10-13 12:48:21 -04001194 StatementArray blockStmts;
John Stilesf4bda742020-10-14 16:57:41 -04001195 blockStmts.reserve_back(block.children().size());
Ethan Nicholas7bd60432020-09-25 14:31:59 -04001196 for (std::unique_ptr<Statement>& stmt : block.children()) {
1197 move_all_but_break(stmt, &blockStmts);
Ethan Nicholas739e1ca2020-06-11 12:16:14 -04001198 }
John Stiles92219b42020-06-15 12:32:24 -04001199
1200 target->push_back(std::make_unique<Block>(block.fOffset, std::move(blockStmts),
Ethan Nicholas7bd60432020-09-25 14:31:59 -04001201 block.symbolTable(), block.isScope()));
Ethan Nicholas739e1ca2020-06-11 12:16:14 -04001202 break;
John Stiles92219b42020-06-15 12:32:24 -04001203 }
1204
Ethan Nicholase6592142020-09-08 10:22:09 -04001205 case Statement::Kind::kBreak:
John Stiles92219b42020-06-15 12:32:24 -04001206 // Do not append a break to the target.
1207 break;
1208
Ethan Nicholas739e1ca2020-06-11 12:16:14 -04001209 default:
John Stiles92219b42020-06-15 12:32:24 -04001210 // Append normal statements to the target.
1211 target->push_back(std::move(stmt));
1212 break;
Ethan Nicholas739e1ca2020-06-11 12:16:14 -04001213 }
1214}
1215
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001216// Returns a block containing all of the statements that will be run if the given case matches
1217// (which, owing to the statements being owned by unique_ptrs, means the switch itself will be
1218// broken by this call and must then be discarded).
1219// Returns null (and leaves the switch unmodified) if no such simple reduction is possible, such as
1220// when break statements appear inside conditionals.
John Stiles92219b42020-06-15 12:32:24 -04001221static std::unique_ptr<Statement> block_for_case(SwitchStatement* switchStatement,
1222 SwitchCase* caseToCapture) {
1223 // We have to be careful to not move any of the pointers until after we're sure we're going to
1224 // succeed, so before we make any changes at all, we check the switch-cases to decide on a plan
1225 // of action. First, find the switch-case we are interested in.
Ethan Nicholas01b05e52020-10-22 15:53:41 -04001226 auto iter = switchStatement->cases().begin();
1227 for (; iter != switchStatement->cases().end(); ++iter) {
John Stiles2d4f9592020-10-30 10:29:12 -04001228 if (iter->get() == caseToCapture) {
John Stiles92219b42020-06-15 12:32:24 -04001229 break;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001230 }
John Stiles92219b42020-06-15 12:32:24 -04001231 }
1232
1233 // Next, walk forward through the rest of the switch. If we find a conditional break, we're
1234 // stuck and can't simplify at all. If we find an unconditional break, we have a range of
1235 // statements that we can use for simplification.
1236 auto startIter = iter;
1237 Statement* unconditionalBreakStmt = nullptr;
Ethan Nicholas01b05e52020-10-22 15:53:41 -04001238 for (; iter != switchStatement->cases().end(); ++iter) {
John Stiles2d4f9592020-10-30 10:29:12 -04001239 for (std::unique_ptr<Statement>& stmt : (*iter)->statements()) {
John Stiles92219b42020-06-15 12:32:24 -04001240 if (contains_conditional_break(*stmt)) {
1241 // We can't reduce switch-cases to a block when they have conditional breaks.
1242 return nullptr;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001243 }
John Stiles92219b42020-06-15 12:32:24 -04001244
1245 if (contains_unconditional_break(*stmt)) {
1246 // We found an unconditional break. We can use this block, but we need to strip
1247 // out the break statement.
1248 unconditionalBreakStmt = stmt.get();
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001249 break;
1250 }
1251 }
John Stiles92219b42020-06-15 12:32:24 -04001252
1253 if (unconditionalBreakStmt != nullptr) {
1254 break;
1255 }
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001256 }
John Stiles92219b42020-06-15 12:32:24 -04001257
1258 // We fell off the bottom of the switch or encountered a break. We know the range of statements
1259 // that we need to move over, and we know it's safe to do so.
John Stiles8f2a0cf2020-10-13 12:48:21 -04001260 StatementArray caseStmts;
John Stiles92219b42020-06-15 12:32:24 -04001261
1262 // We can move over most of the statements as-is.
1263 while (startIter != iter) {
John Stiles2d4f9592020-10-30 10:29:12 -04001264 for (std::unique_ptr<Statement>& stmt : (*startIter)->statements()) {
John Stiles92219b42020-06-15 12:32:24 -04001265 caseStmts.push_back(std::move(stmt));
1266 }
1267 ++startIter;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001268 }
John Stiles92219b42020-06-15 12:32:24 -04001269
1270 // If we found an unconditional break at the end, we need to move what we can while avoiding
1271 // that break.
1272 if (unconditionalBreakStmt != nullptr) {
John Stiles2d4f9592020-10-30 10:29:12 -04001273 for (std::unique_ptr<Statement>& stmt : (*startIter)->statements()) {
John Stiles92219b42020-06-15 12:32:24 -04001274 if (stmt.get() == unconditionalBreakStmt) {
1275 move_all_but_break(stmt, &caseStmts);
1276 unconditionalBreakStmt = nullptr;
1277 break;
1278 }
1279
1280 caseStmts.push_back(std::move(stmt));
1281 }
1282 }
1283
1284 SkASSERT(unconditionalBreakStmt == nullptr); // Verify that we fixed the unconditional break.
1285
1286 // Return our newly-synthesized block.
Ethan Nicholas01b05e52020-10-22 15:53:41 -04001287 return std::make_unique<Block>(/*offset=*/-1, std::move(caseStmts), switchStatement->symbols());
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001288}
1289
Ethan Nicholascb670962017-04-20 19:31:52 -04001290void Compiler::simplifyStatement(DefinitionMap& definitions,
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001291 BasicBlock& b,
1292 std::vector<BasicBlock::Node>::iterator* iter,
Ethan Nicholascdeae8c2020-10-22 14:39:46 -04001293 OptimizationContext* optimizationContext) {
Brian Osman010ce6a2020-10-19 16:34:10 -04001294 ProgramUsage* usage = optimizationContext->fUsage;
Ethan Nicholascb670962017-04-20 19:31:52 -04001295 Statement* stmt = (*iter)->statement()->get();
Ethan Nicholase6592142020-09-08 10:22:09 -04001296 switch (stmt->kind()) {
1297 case Statement::Kind::kVarDeclaration: {
John Stilesa5a97b42020-08-18 11:19:07 -04001298 const auto& varDecl = stmt->as<VarDeclaration>();
Brian Osman010ce6a2020-10-19 16:34:10 -04001299 if (usage->isDead(varDecl.var()) &&
Ethan Nicholasc51f33e2020-10-13 13:49:44 -04001300 (!varDecl.value() ||
1301 !varDecl.value()->hasSideEffects())) {
1302 if (varDecl.value()) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001303 SkASSERT((*iter)->statement()->get() == stmt);
Ethan Nicholasc51f33e2020-10-13 13:49:44 -04001304 if (!b.tryRemoveExpressionBefore(iter, varDecl.value().get())) {
Ethan Nicholascdeae8c2020-10-22 14:39:46 -04001305 optimizationContext->fNeedsRescan = true;
Ethan Nicholascb670962017-04-20 19:31:52 -04001306 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001307 }
Brian Osman010ce6a2020-10-19 16:34:10 -04001308 (*iter)->setStatement(std::make_unique<Nop>(), usage);
Ethan Nicholascdeae8c2020-10-22 14:39:46 -04001309 optimizationContext->fUpdated = true;
Ethan Nicholascb670962017-04-20 19:31:52 -04001310 }
1311 break;
1312 }
Ethan Nicholase6592142020-09-08 10:22:09 -04001313 case Statement::Kind::kIf: {
John Stilesa5a97b42020-08-18 11:19:07 -04001314 IfStatement& i = stmt->as<IfStatement>();
Ethan Nicholas8c44eca2020-10-07 16:47:09 -04001315 if (i.test()->kind() == Expression::Kind::kBoolLiteral) {
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001316 // constant if, collapse down to a single branch
Ethan Nicholas8c44eca2020-10-07 16:47:09 -04001317 if (i.test()->as<BoolLiteral>().value()) {
1318 SkASSERT(i.ifTrue());
Brian Osman010ce6a2020-10-19 16:34:10 -04001319 (*iter)->setStatement(std::move(i.ifTrue()), usage);
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001320 } else {
Ethan Nicholas8c44eca2020-10-07 16:47:09 -04001321 if (i.ifFalse()) {
Brian Osman010ce6a2020-10-19 16:34:10 -04001322 (*iter)->setStatement(std::move(i.ifFalse()), usage);
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001323 } else {
Brian Osman010ce6a2020-10-19 16:34:10 -04001324 (*iter)->setStatement(std::make_unique<Nop>(), usage);
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001325 }
1326 }
Ethan Nicholascdeae8c2020-10-22 14:39:46 -04001327 optimizationContext->fUpdated = true;
1328 optimizationContext->fNeedsRescan = true;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001329 break;
1330 }
Ethan Nicholas8c44eca2020-10-07 16:47:09 -04001331 if (i.ifFalse() && i.ifFalse()->isEmpty()) {
Ethan Nicholascb670962017-04-20 19:31:52 -04001332 // else block doesn't do anything, remove it
Ethan Nicholas8c44eca2020-10-07 16:47:09 -04001333 i.ifFalse().reset();
Ethan Nicholascdeae8c2020-10-22 14:39:46 -04001334 optimizationContext->fUpdated = true;
1335 optimizationContext->fNeedsRescan = true;
Ethan Nicholascb670962017-04-20 19:31:52 -04001336 }
Ethan Nicholas8c44eca2020-10-07 16:47:09 -04001337 if (!i.ifFalse() && i.ifTrue()->isEmpty()) {
Ethan Nicholascb670962017-04-20 19:31:52 -04001338 // if block doesn't do anything, no else block
Ethan Nicholas8c44eca2020-10-07 16:47:09 -04001339 if (i.test()->hasSideEffects()) {
Ethan Nicholascb670962017-04-20 19:31:52 -04001340 // test has side effects, keep it
Brian Osman010ce6a2020-10-19 16:34:10 -04001341 (*iter)->setStatement(
1342 std::make_unique<ExpressionStatement>(std::move(i.test())), usage);
Ethan Nicholascb670962017-04-20 19:31:52 -04001343 } else {
1344 // no if, no else, no test side effects, kill the whole if
1345 // statement
Brian Osman010ce6a2020-10-19 16:34:10 -04001346 (*iter)->setStatement(std::make_unique<Nop>(), usage);
Ethan Nicholascb670962017-04-20 19:31:52 -04001347 }
Ethan Nicholascdeae8c2020-10-22 14:39:46 -04001348 optimizationContext->fUpdated = true;
1349 optimizationContext->fNeedsRescan = true;
Ethan Nicholascb670962017-04-20 19:31:52 -04001350 }
1351 break;
1352 }
Ethan Nicholase6592142020-09-08 10:22:09 -04001353 case Statement::Kind::kSwitch: {
John Stilesa5a97b42020-08-18 11:19:07 -04001354 SwitchStatement& s = stmt->as<SwitchStatement>();
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001355 int64_t switchValue;
Ethan Nicholas01b05e52020-10-22 15:53:41 -04001356 if (fIRGenerator->getConstantInt(*s.value(), &switchValue)) {
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001357 // switch is constant, replace it with the case that matches
1358 bool found = false;
1359 SwitchCase* defaultCase = nullptr;
John Stiles2d4f9592020-10-30 10:29:12 -04001360 for (const std::unique_ptr<SwitchCase>& c : s.cases()) {
1361 if (!c->value()) {
1362 defaultCase = c.get();
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001363 continue;
1364 }
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001365 int64_t caseValue;
John Stiles2d4f9592020-10-30 10:29:12 -04001366 SkAssertResult(fIRGenerator->getConstantInt(*c->value(), &caseValue));
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001367 if (caseValue == switchValue) {
John Stiles2d4f9592020-10-30 10:29:12 -04001368 std::unique_ptr<Statement> newBlock = block_for_case(&s, c.get());
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001369 if (newBlock) {
Brian Osman010ce6a2020-10-19 16:34:10 -04001370 (*iter)->setStatement(std::move(newBlock), usage);
John Stiles9d944232020-08-19 09:56:49 -04001371 found = true;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001372 break;
1373 } else {
Ethan Nicholas01b05e52020-10-22 15:53:41 -04001374 if (s.isStatic() && !(fFlags & kPermitInvalidStaticTests_Flag) &&
1375 optimizationContext->fSilences.find(&s) ==
1376 optimizationContext->fSilences.end()) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001377 this->error(s.fOffset,
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001378 "static switch contains non-static conditional break");
Ethan Nicholas01b05e52020-10-22 15:53:41 -04001379 optimizationContext->fSilences.insert(&s);
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001380 }
1381 return; // can't simplify
1382 }
1383 }
1384 }
1385 if (!found) {
1386 // no matching case. use default if it exists, or kill the whole thing
1387 if (defaultCase) {
1388 std::unique_ptr<Statement> newBlock = block_for_case(&s, defaultCase);
1389 if (newBlock) {
Brian Osman010ce6a2020-10-19 16:34:10 -04001390 (*iter)->setStatement(std::move(newBlock), usage);
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001391 } else {
Ethan Nicholas01b05e52020-10-22 15:53:41 -04001392 if (s.isStatic() && !(fFlags & kPermitInvalidStaticTests_Flag) &&
1393 optimizationContext->fSilences.find(&s) ==
1394 optimizationContext->fSilences.end()) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001395 this->error(s.fOffset,
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001396 "static switch contains non-static conditional break");
Ethan Nicholas01b05e52020-10-22 15:53:41 -04001397 optimizationContext->fSilences.insert(&s);
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001398 }
1399 return; // can't simplify
1400 }
1401 } else {
Brian Osman010ce6a2020-10-19 16:34:10 -04001402 (*iter)->setStatement(std::make_unique<Nop>(), usage);
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001403 }
1404 }
Ethan Nicholascdeae8c2020-10-22 14:39:46 -04001405 optimizationContext->fUpdated = true;
1406 optimizationContext->fNeedsRescan = true;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001407 }
1408 break;
1409 }
Ethan Nicholase6592142020-09-08 10:22:09 -04001410 case Statement::Kind::kExpression: {
John Stilesa5a97b42020-08-18 11:19:07 -04001411 ExpressionStatement& e = stmt->as<ExpressionStatement>();
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001412 SkASSERT((*iter)->statement()->get() == &e);
Ethan Nicholasd503a5a2020-09-30 09:29:55 -04001413 if (!e.expression()->hasSideEffects()) {
Ethan Nicholascb670962017-04-20 19:31:52 -04001414 // Expression statement with no side effects, kill it
Ethan Nicholasd503a5a2020-09-30 09:29:55 -04001415 if (!b.tryRemoveExpressionBefore(iter, e.expression().get())) {
Ethan Nicholascdeae8c2020-10-22 14:39:46 -04001416 optimizationContext->fNeedsRescan = true;
Ethan Nicholascb670962017-04-20 19:31:52 -04001417 }
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001418 SkASSERT((*iter)->statement()->get() == stmt);
Brian Osman010ce6a2020-10-19 16:34:10 -04001419 (*iter)->setStatement(std::make_unique<Nop>(), usage);
Ethan Nicholascdeae8c2020-10-22 14:39:46 -04001420 optimizationContext->fUpdated = true;
Ethan Nicholascb670962017-04-20 19:31:52 -04001421 }
1422 break;
1423 }
1424 default:
1425 break;
1426 }
1427}
1428
Brian Osman010ce6a2020-10-19 16:34:10 -04001429bool Compiler::scanCFG(FunctionDefinition& f, ProgramUsage* usage) {
John Stiles0cc193a2020-09-09 09:39:34 -04001430 bool madeChanges = false;
1431
Ethan Nicholascb670962017-04-20 19:31:52 -04001432 CFG cfg = CFGGenerator().getCFG(f);
1433 this->computeDataFlow(&cfg);
ethannicholas22f939e2016-10-13 13:25:34 -07001434
1435 // check for unreachable code
1436 for (size_t i = 0; i < cfg.fBlocks.size(); i++) {
John Stiles0cc193a2020-09-09 09:39:34 -04001437 const BasicBlock& block = cfg.fBlocks[i];
John Stiles61e75e32020-10-01 15:42:37 -04001438 if (i != cfg.fStart && !block.fIsReachable && block.fNodes.size()) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001439 int offset;
John Stiles0cc193a2020-09-09 09:39:34 -04001440 const BasicBlock::Node& node = block.fNodes[0];
John Stiles70025e52020-09-28 16:08:58 -04001441 if (node.isStatement()) {
1442 offset = (*node.statement())->fOffset;
1443 } else {
1444 offset = (*node.expression())->fOffset;
1445 if ((*node.expression())->is<BoolLiteral>()) {
1446 // Function inlining can generate do { ... } while(false) loops which always
1447 // break, so the boolean condition is considered unreachable. Since not being
1448 // able to reach a literal is a non-issue in the first place, we don't report an
1449 // error in this case.
1450 continue;
1451 }
Ethan Nicholas86a43402017-01-19 13:32:00 -05001452 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001453 this->error(offset, String("unreachable"));
ethannicholas22f939e2016-10-13 13:25:34 -07001454 }
1455 }
1456 if (fErrorCount) {
John Stiles0cc193a2020-09-09 09:39:34 -04001457 return madeChanges;
ethannicholas22f939e2016-10-13 13:25:34 -07001458 }
1459
Ethan Nicholascb670962017-04-20 19:31:52 -04001460 // check for dead code & undefined variables, perform constant propagation
Ethan Nicholascdeae8c2020-10-22 14:39:46 -04001461 OptimizationContext optimizationContext;
Brian Osman010ce6a2020-10-19 16:34:10 -04001462 optimizationContext.fUsage = usage;
John Stiles7d3f0892020-11-03 11:35:01 -05001463 SkBitSet eliminatedBlockIds(cfg.fBlocks.size());
Ethan Nicholascb670962017-04-20 19:31:52 -04001464 do {
Ethan Nicholascdeae8c2020-10-22 14:39:46 -04001465 if (optimizationContext.fNeedsRescan) {
Ethan Nicholascb670962017-04-20 19:31:52 -04001466 cfg = CFGGenerator().getCFG(f);
1467 this->computeDataFlow(&cfg);
Ethan Nicholascdeae8c2020-10-22 14:39:46 -04001468 optimizationContext.fNeedsRescan = false;
Ethan Nicholas113628d2017-02-02 16:11:39 -05001469 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001470
John Stiles7d3f0892020-11-03 11:35:01 -05001471 eliminatedBlockIds.reset();
Ethan Nicholascdeae8c2020-10-22 14:39:46 -04001472 optimizationContext.fUpdated = false;
John Stiles7d3f0892020-11-03 11:35:01 -05001473
1474 for (BlockId blockId = 0; blockId < cfg.fBlocks.size(); ++blockId) {
1475 if (eliminatedBlockIds.test(blockId)) {
1476 // We reached a block ID that might have been eliminated. Be cautious and rescan.
1477 optimizationContext.fUpdated = true;
1478 optimizationContext.fNeedsRescan = true;
1479 break;
1480 }
1481
1482 BasicBlock& b = cfg.fBlocks[blockId];
1483 if (blockId > 0 && !b.fIsReachable) {
Ethan Nicholas1de14812020-06-19 15:32:49 -04001484 // Block was reachable before optimization, but has since become unreachable. In
1485 // addition to being dead code, it's broken - since control flow can't reach it, no
1486 // prior variable definitions can reach it, and therefore variables might look to
Brian Osmanc0213602020-10-06 14:43:32 -04001487 // have not been properly assigned. Kill it by replacing all statements with Nops.
Ethan Nicholas1de14812020-06-19 15:32:49 -04001488 for (BasicBlock::Node& node : b.fNodes) {
John Stiles70025e52020-09-28 16:08:58 -04001489 if (node.isStatement() && !(*node.statement())->is<Nop>()) {
John Stiles7d3f0892020-11-03 11:35:01 -05001490 // Eliminating a node runs the risk of eliminating that node's exits as
1491 // well. Keep track of this and do a rescan if we are about to access one
1492 // of these.
1493 for (BlockId id : b.fExits) {
1494 eliminatedBlockIds.set(id);
1495 }
Brian Osman010ce6a2020-10-19 16:34:10 -04001496 node.setStatement(std::make_unique<Nop>(), usage);
John Stiles0cc193a2020-09-09 09:39:34 -04001497 madeChanges = true;
Ethan Nicholas1de14812020-06-19 15:32:49 -04001498 }
1499 }
1500 continue;
1501 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001502 DefinitionMap definitions = b.fBefore;
1503
Ethan Nicholascdeae8c2020-10-22 14:39:46 -04001504 for (auto iter = b.fNodes.begin(); iter != b.fNodes.end() &&
1505 !optimizationContext.fNeedsRescan; ++iter) {
John Stiles70025e52020-09-28 16:08:58 -04001506 if (iter->isExpression()) {
Ethan Nicholascdeae8c2020-10-22 14:39:46 -04001507 this->simplifyExpression(definitions, b, &iter, &optimizationContext);
Ethan Nicholascb670962017-04-20 19:31:52 -04001508 } else {
Ethan Nicholascdeae8c2020-10-22 14:39:46 -04001509 this->simplifyStatement(definitions, b, &iter, &optimizationContext);
Ethan Nicholascb670962017-04-20 19:31:52 -04001510 }
Ethan Nicholascdeae8c2020-10-22 14:39:46 -04001511 if (optimizationContext.fNeedsRescan) {
Ethan Nicholas4b330df2017-05-17 10:52:55 -04001512 break;
1513 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001514 this->addDefinitions(*iter, &definitions);
1515 }
Brian Osman01a3eb42020-09-14 11:32:49 -04001516
Ethan Nicholascdeae8c2020-10-22 14:39:46 -04001517 if (optimizationContext.fNeedsRescan) {
Brian Osman01a3eb42020-09-14 11:32:49 -04001518 break;
1519 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001520 }
Ethan Nicholascdeae8c2020-10-22 14:39:46 -04001521 madeChanges |= optimizationContext.fUpdated;
1522 } while (optimizationContext.fUpdated);
1523 SkASSERT(!optimizationContext.fNeedsRescan);
ethannicholas22f939e2016-10-13 13:25:34 -07001524
Ethan Nicholas91a10532017-06-22 11:24:38 -04001525 // verify static ifs & switches, clean up dead variable decls
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001526 for (BasicBlock& b : cfg.fBlocks) {
Ethan Nicholascdeae8c2020-10-22 14:39:46 -04001527 for (auto iter = b.fNodes.begin(); iter != b.fNodes.end() &&
1528 !optimizationContext.fNeedsRescan;) {
John Stiles70025e52020-09-28 16:08:58 -04001529 if (iter->isStatement()) {
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001530 const Statement& s = **iter->statement();
Ethan Nicholase6592142020-09-08 10:22:09 -04001531 switch (s.kind()) {
1532 case Statement::Kind::kIf:
Ethan Nicholas8c44eca2020-10-07 16:47:09 -04001533 if (s.as<IfStatement>().isStatic() &&
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -04001534 !(fFlags & kPermitInvalidStaticTests_Flag)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001535 this->error(s.fOffset, "static if has non-static test");
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001536 }
Ethan Nicholas91a10532017-06-22 11:24:38 -04001537 ++iter;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001538 break;
Ethan Nicholase6592142020-09-08 10:22:09 -04001539 case Statement::Kind::kSwitch:
Ethan Nicholas01b05e52020-10-22 15:53:41 -04001540 if (s.as<SwitchStatement>().isStatic() &&
1541 !(fFlags & kPermitInvalidStaticTests_Flag) &&
1542 optimizationContext.fSilences.find(&s) ==
1543 optimizationContext.fSilences.end()) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001544 this->error(s.fOffset, "static switch has non-static test");
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001545 }
Ethan Nicholas91a10532017-06-22 11:24:38 -04001546 ++iter;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001547 break;
1548 default:
Ethan Nicholas91a10532017-06-22 11:24:38 -04001549 ++iter;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001550 break;
1551 }
Ethan Nicholas91a10532017-06-22 11:24:38 -04001552 } else {
1553 ++iter;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001554 }
1555 }
1556 }
1557
ethannicholas22f939e2016-10-13 13:25:34 -07001558 // check for missing return
Ethan Nicholas0a5d0962020-10-14 13:33:18 -04001559 if (f.declaration().returnType() != *fContext->fVoid_Type) {
John Stiles61e75e32020-10-01 15:42:37 -04001560 if (cfg.fBlocks[cfg.fExit].fIsReachable) {
Ethan Nicholas0a5d0962020-10-14 13:33:18 -04001561 this->error(f.fOffset, String("function '" + String(f.declaration().name()) +
Ethan Nicholasdb80f692019-11-22 14:06:12 -05001562 "' can exit without returning a value"));
ethannicholas22f939e2016-10-13 13:25:34 -07001563 }
1564 }
John Stiles0cc193a2020-09-09 09:39:34 -04001565
1566 return madeChanges;
ethannicholas22f939e2016-10-13 13:25:34 -07001567}
1568
Brian Osman32d53552020-09-23 13:55:20 -04001569std::unique_ptr<Program> Compiler::convertProgram(
1570 Program::Kind kind,
1571 String text,
1572 const Program::Settings& settings,
1573 const std::vector<std::unique_ptr<ExternalValue>>* externalValues) {
1574 SkASSERT(!externalValues || (kind == Program::kGeneric_Kind));
Ethan Nicholas91164d12019-05-15 15:29:54 -04001575
ethannicholasb3058bd2016-07-01 08:22:01 -07001576 fErrorText = "";
1577 fErrorCount = 0;
Brian Osman3d87e9f2020-10-08 11:50:22 -04001578 fInliner.reset(fContext.get(), fIRGenerator->fModifiers.get(), &settings);
Brian Osman88cda172020-10-09 12:05:16 -04001579
1580 // Not using AutoSource, because caller is likely to call errorText() if we fail to compile
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001581 std::unique_ptr<String> textPtr(new String(std::move(text)));
1582 fSource = textPtr.get();
Brian Osman88cda172020-10-09 12:05:16 -04001583
1584 const ParsedModule& baseModule = this->moduleForProgramKind(kind);
1585
John Stiles5c7bb322020-10-22 11:09:15 -04001586 // Enable node pooling while converting and optimizing the program for a performance boost.
1587 // The Program will take ownership of the pool.
John Stiles2d68ea32020-10-22 15:42:27 -04001588 std::unique_ptr<Pool> pool = Pool::Create();
1589 pool->attachToThread();
Brian Osman88cda172020-10-09 12:05:16 -04001590 IRGenerator::IRBundle ir =
1591 fIRGenerator->convertProgram(kind, &settings, baseModule, /*isBuiltinCode=*/false,
1592 textPtr->c_str(), textPtr->size(), externalValues);
John Stiles5c7bb322020-10-22 11:09:15 -04001593 auto program = std::make_unique<Program>(kind,
1594 std::move(textPtr),
1595 settings,
1596 fContext,
1597 std::move(ir.fElements),
1598 std::move(ir.fModifiers),
1599 std::move(ir.fSymbolTable),
1600 std::move(pool),
1601 ir.fInputs);
1602 bool success = false;
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001603 if (fErrorCount) {
John Stiles5c7bb322020-10-22 11:09:15 -04001604 // Do not return programs that failed to compile.
1605 } else if (settings.fOptimize && !this->optimize(*program)) {
1606 // Do not return programs that failed to optimize.
1607 } else {
1608 // We have a successful program!
1609 success = true;
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001610 }
John Stiles5c7bb322020-10-22 11:09:15 -04001611
1612 program->fPool->detachFromThread();
1613 return success ? std::move(program) : nullptr;
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001614}
1615
Ethan Nicholas00543112018-07-31 09:44:36 -04001616bool Compiler::optimize(Program& program) {
1617 SkASSERT(!fErrorCount);
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001618 fIRGenerator->fKind = program.fKind;
1619 fIRGenerator->fSettings = &program.fSettings;
Brian Osman010ce6a2020-10-19 16:34:10 -04001620 ProgramUsage* usage = program.fUsage.get();
John Stiles7954d6c2020-09-01 10:53:02 -04001621
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001622 while (fErrorCount == 0) {
1623 bool madeChanges = false;
John Stiles7954d6c2020-09-01 10:53:02 -04001624
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001625 // Scan and optimize based on the control-flow graph for each function.
Brian Osman1179fcf2020-10-08 16:04:40 -04001626 for (const auto& element : program.elements()) {
1627 if (element->is<FunctionDefinition>()) {
Brian Osman010ce6a2020-10-19 16:34:10 -04001628 madeChanges |= this->scanCFG(element->as<FunctionDefinition>(), usage);
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001629 }
1630 }
1631
1632 // Perform inline-candidate analysis and inline any functions deemed suitable.
John Stiles881a10c2020-09-19 10:13:24 -04001633 madeChanges |= fInliner.analyze(program);
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001634
1635 // Remove dead functions. We wait until after analysis so that we still report errors,
1636 // even in unused code.
1637 if (program.fSettings.fRemoveDeadFunctions) {
1638 program.fElements.erase(
1639 std::remove_if(program.fElements.begin(),
1640 program.fElements.end(),
1641 [&](const std::unique_ptr<ProgramElement>& element) {
1642 if (!element->is<FunctionDefinition>()) {
1643 return false;
1644 }
1645 const auto& fn = element->as<FunctionDefinition>();
Brian Osman2e25ff42020-10-15 10:32:04 -04001646 bool dead = fn.declaration().name() != "main" &&
Brian Osman010ce6a2020-10-19 16:34:10 -04001647 usage->get(fn.declaration()) == 0;
1648 if (dead) {
1649 madeChanges = true;
1650 usage->remove(*element);
1651 }
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001652 return dead;
1653 }),
1654 program.fElements.end());
1655 }
1656
1657 if (program.fKind != Program::kFragmentProcessor_Kind) {
Brian Osmanc0213602020-10-06 14:43:32 -04001658 // Remove declarations of dead global variables
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001659 program.fElements.erase(
1660 std::remove_if(program.fElements.begin(), program.fElements.end(),
1661 [&](const std::unique_ptr<ProgramElement>& element) {
Brian Osmanc0213602020-10-06 14:43:32 -04001662 if (!element->is<GlobalVarDeclaration>()) {
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001663 return false;
1664 }
Ethan Nicholasc51f33e2020-10-13 13:49:44 -04001665 const auto& global = element->as<GlobalVarDeclaration>();
1666 const auto& varDecl =
1667 global.declaration()->as<VarDeclaration>();
Brian Osman010ce6a2020-10-19 16:34:10 -04001668 bool dead = usage->isDead(varDecl.var());
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001669 madeChanges |= dead;
1670 return dead;
1671 }),
1672 program.fElements.end());
1673 }
John Stiles73a6bff2020-09-09 13:40:37 -04001674
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001675 if (!madeChanges) {
1676 break;
Ethan Nicholas0dc80872019-02-08 15:46:24 -05001677 }
Ethan Nicholas00543112018-07-31 09:44:36 -04001678 }
1679 return fErrorCount == 0;
1680}
1681
Brian Osmanfb32ddf2019-06-18 10:14:20 -04001682#if defined(SKSL_STANDALONE) || SK_SUPPORT_GPU
1683
Ethan Nicholas00543112018-07-31 09:44:36 -04001684bool Compiler::toSPIRV(Program& program, OutputStream& out) {
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001685#ifdef SK_ENABLE_SPIRV_VALIDATION
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001686 StringStream buffer;
Brian Osman88cda172020-10-09 12:05:16 -04001687 AutoSource as(this, program.fSource.get());
Brian Osman8b43dad2020-10-09 13:31:42 -04001688 SPIRVCodeGenerator cg(fContext.get(), &program, this, &buffer);
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001689 bool result = cg.generateCode();
1690 if (result) {
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001691 spvtools::SpirvTools tools(SPV_ENV_VULKAN_1_0);
Ethan Nicholas762466e2017-06-29 10:03:38 -04001692 const String& data = buffer.str();
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001693 SkASSERT(0 == data.size() % 4);
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001694 auto dumpmsg = [](spv_message_level_t, const char*, const spv_position_t&, const char* m) {
1695 SkDebugf("SPIR-V validation error: %s\n", m);
1696 };
1697 tools.SetMessageConsumer(dumpmsg);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001698 // Verify that the SPIR-V we produced is valid. If this SkASSERT fails, check the logs prior
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001699 // to the failure to see the validation errors.
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001700 SkAssertResult(tools.Validate((const uint32_t*) data.c_str(), data.size() / 4));
Ethan Nicholas762466e2017-06-29 10:03:38 -04001701 out.write(data.c_str(), data.size());
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001702 }
1703#else
Brian Osman88cda172020-10-09 12:05:16 -04001704 AutoSource as(this, program.fSource.get());
Brian Osman8b43dad2020-10-09 13:31:42 -04001705 SPIRVCodeGenerator cg(fContext.get(), &program, this, &out);
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001706 bool result = cg.generateCode();
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001707#endif
Ethan Nicholasce33f102016-12-09 17:22:59 -05001708 return result;
1709}
1710
Ethan Nicholas00543112018-07-31 09:44:36 -04001711bool Compiler::toSPIRV(Program& program, String* out) {
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001712 StringStream buffer;
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001713 bool result = this->toSPIRV(program, buffer);
1714 if (result) {
Ethan Nicholas762466e2017-06-29 10:03:38 -04001715 *out = buffer.str();
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001716 }
1717 return result;
1718}
1719
Ethan Nicholas00543112018-07-31 09:44:36 -04001720bool Compiler::toGLSL(Program& program, OutputStream& out) {
Brian Osman88cda172020-10-09 12:05:16 -04001721 AutoSource as(this, program.fSource.get());
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001722 GLSLCodeGenerator cg(fContext.get(), &program, this, &out);
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001723 bool result = cg.generateCode();
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001724 return result;
1725}
1726
Ethan Nicholas00543112018-07-31 09:44:36 -04001727bool Compiler::toGLSL(Program& program, String* out) {
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001728 StringStream buffer;
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001729 bool result = this->toGLSL(program, buffer);
1730 if (result) {
Ethan Nicholas762466e2017-06-29 10:03:38 -04001731 *out = buffer.str();
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001732 }
1733 return result;
1734}
1735
Brian Osmanc0243912020-02-19 15:35:26 -05001736bool Compiler::toHLSL(Program& program, String* out) {
1737 String spirv;
1738 if (!this->toSPIRV(program, &spirv)) {
1739 return false;
1740 }
1741
1742 return SPIRVtoHLSL(spirv, out);
1743}
1744
Ethan Nicholas00543112018-07-31 09:44:36 -04001745bool Compiler::toMetal(Program& program, OutputStream& out) {
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001746 MetalCodeGenerator cg(fContext.get(), &program, this, &out);
Ethan Nicholascc305772017-10-13 16:17:45 -04001747 bool result = cg.generateCode();
Ethan Nicholascc305772017-10-13 16:17:45 -04001748 return result;
1749}
1750
Ethan Nicholas00543112018-07-31 09:44:36 -04001751bool Compiler::toMetal(Program& program, String* out) {
Timothy Liangb8eeb802018-07-23 16:46:16 -04001752 StringStream buffer;
1753 bool result = this->toMetal(program, buffer);
1754 if (result) {
1755 *out = buffer.str();
1756 }
1757 return result;
1758}
1759
Greg Daniela28ea672020-09-25 11:12:56 -04001760#if defined(SKSL_STANDALONE) || GR_TEST_UTILS
Ethan Nicholas00543112018-07-31 09:44:36 -04001761bool Compiler::toCPP(Program& program, String name, OutputStream& out) {
Brian Osman88cda172020-10-09 12:05:16 -04001762 AutoSource as(this, program.fSource.get());
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001763 CPPCodeGenerator cg(fContext.get(), &program, this, name, &out);
Ethan Nicholas762466e2017-06-29 10:03:38 -04001764 bool result = cg.generateCode();
Ethan Nicholas762466e2017-06-29 10:03:38 -04001765 return result;
1766}
1767
Ethan Nicholas00543112018-07-31 09:44:36 -04001768bool Compiler::toH(Program& program, String name, OutputStream& out) {
Brian Osman88cda172020-10-09 12:05:16 -04001769 AutoSource as(this, program.fSource.get());
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001770 HCodeGenerator cg(fContext.get(), &program, this, name, &out);
Ethan Nicholas762466e2017-06-29 10:03:38 -04001771 bool result = cg.generateCode();
Ethan Nicholas00543112018-07-31 09:44:36 -04001772 return result;
1773}
Greg Daniela28ea672020-09-25 11:12:56 -04001774#endif // defined(SKSL_STANDALONE) || GR_TEST_UTILS
Ethan Nicholas00543112018-07-31 09:44:36 -04001775
Ethan Nicholas2a479a52020-08-18 16:29:45 -04001776#endif // defined(SKSL_STANDALONE) || SK_SUPPORT_GPU
Brian Osman2e29ab52019-09-20 12:19:11 -04001777
1778#if !defined(SKSL_STANDALONE) && SK_SUPPORT_GPU
Brian Osmana4b91692020-08-10 14:26:16 -04001779bool Compiler::toPipelineStage(Program& program, PipelineStageArgs* outArgs) {
Brian Osman88cda172020-10-09 12:05:16 -04001780 AutoSource as(this, program.fSource.get());
Ethan Nicholas00543112018-07-31 09:44:36 -04001781 StringStream buffer;
Brian Osman300fe1d2020-01-23 15:42:43 -05001782 PipelineStageCodeGenerator cg(fContext.get(), &program, this, &buffer, outArgs);
Ethan Nicholas00543112018-07-31 09:44:36 -04001783 bool result = cg.generateCode();
Ethan Nicholas00543112018-07-31 09:44:36 -04001784 if (result) {
Brian Osman107c6662019-12-30 15:02:30 -05001785 outArgs->fCode = buffer.str();
Ethan Nicholas00543112018-07-31 09:44:36 -04001786 }
Ethan Nicholas762466e2017-06-29 10:03:38 -04001787 return result;
1788}
Brian Osmanfb32ddf2019-06-18 10:14:20 -04001789#endif
1790
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001791std::unique_ptr<ByteCode> Compiler::toByteCode(Program& program) {
Mike Klein853d4ed2020-08-20 00:41:00 +00001792#if defined(SK_ENABLE_SKSL_INTERPRETER)
Brian Osman88cda172020-10-09 12:05:16 -04001793 AutoSource as(this, program.fSource.get());
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001794 std::unique_ptr<ByteCode> result(new ByteCode());
Brian Osmanb08cc022020-04-02 11:38:40 -04001795 ByteCodeGenerator cg(fContext.get(), &program, this, result.get());
1796 bool success = cg.generateCode();
Brian Osmanb08cc022020-04-02 11:38:40 -04001797 if (success) {
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001798 return result;
1799 }
Mike Klein853d4ed2020-08-20 00:41:00 +00001800#else
1801 ABORT("ByteCode interpreter not enabled");
1802#endif
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001803 return nullptr;
1804}
1805
Brian Osman401a0092020-09-10 14:47:24 -04001806const char* Compiler::OperatorName(Token::Kind op) {
1807 switch (op) {
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001808 case Token::Kind::TK_PLUS: return "+";
1809 case Token::Kind::TK_MINUS: return "-";
1810 case Token::Kind::TK_STAR: return "*";
1811 case Token::Kind::TK_SLASH: return "/";
1812 case Token::Kind::TK_PERCENT: return "%";
1813 case Token::Kind::TK_SHL: return "<<";
1814 case Token::Kind::TK_SHR: return ">>";
1815 case Token::Kind::TK_LOGICALNOT: return "!";
1816 case Token::Kind::TK_LOGICALAND: return "&&";
1817 case Token::Kind::TK_LOGICALOR: return "||";
1818 case Token::Kind::TK_LOGICALXOR: return "^^";
1819 case Token::Kind::TK_BITWISENOT: return "~";
1820 case Token::Kind::TK_BITWISEAND: return "&";
1821 case Token::Kind::TK_BITWISEOR: return "|";
1822 case Token::Kind::TK_BITWISEXOR: return "^";
1823 case Token::Kind::TK_EQ: return "=";
1824 case Token::Kind::TK_EQEQ: return "==";
1825 case Token::Kind::TK_NEQ: return "!=";
1826 case Token::Kind::TK_LT: return "<";
1827 case Token::Kind::TK_GT: return ">";
1828 case Token::Kind::TK_LTEQ: return "<=";
1829 case Token::Kind::TK_GTEQ: return ">=";
1830 case Token::Kind::TK_PLUSEQ: return "+=";
1831 case Token::Kind::TK_MINUSEQ: return "-=";
1832 case Token::Kind::TK_STAREQ: return "*=";
1833 case Token::Kind::TK_SLASHEQ: return "/=";
1834 case Token::Kind::TK_PERCENTEQ: return "%=";
1835 case Token::Kind::TK_SHLEQ: return "<<=";
1836 case Token::Kind::TK_SHREQ: return ">>=";
1837 case Token::Kind::TK_LOGICALANDEQ: return "&&=";
1838 case Token::Kind::TK_LOGICALOREQ: return "||=";
1839 case Token::Kind::TK_LOGICALXOREQ: return "^^=";
1840 case Token::Kind::TK_BITWISEANDEQ: return "&=";
1841 case Token::Kind::TK_BITWISEOREQ: return "|=";
1842 case Token::Kind::TK_BITWISEXOREQ: return "^=";
1843 case Token::Kind::TK_PLUSPLUS: return "++";
1844 case Token::Kind::TK_MINUSMINUS: return "--";
1845 case Token::Kind::TK_COMMA: return ",";
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001846 default:
Brian Osman401a0092020-09-10 14:47:24 -04001847 ABORT("unsupported operator: %d\n", (int) op);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001848 }
1849}
1850
1851
1852bool Compiler::IsAssignment(Token::Kind op) {
1853 switch (op) {
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001854 case Token::Kind::TK_EQ: // fall through
1855 case Token::Kind::TK_PLUSEQ: // fall through
1856 case Token::Kind::TK_MINUSEQ: // fall through
1857 case Token::Kind::TK_STAREQ: // fall through
1858 case Token::Kind::TK_SLASHEQ: // fall through
1859 case Token::Kind::TK_PERCENTEQ: // fall through
1860 case Token::Kind::TK_SHLEQ: // fall through
1861 case Token::Kind::TK_SHREQ: // fall through
1862 case Token::Kind::TK_BITWISEOREQ: // fall through
1863 case Token::Kind::TK_BITWISEXOREQ: // fall through
1864 case Token::Kind::TK_BITWISEANDEQ: // fall through
1865 case Token::Kind::TK_LOGICALOREQ: // fall through
1866 case Token::Kind::TK_LOGICALXOREQ: // fall through
1867 case Token::Kind::TK_LOGICALANDEQ:
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001868 return true;
1869 default:
1870 return false;
1871 }
1872}
1873
Brian Osman401a0092020-09-10 14:47:24 -04001874Token::Kind Compiler::RemoveAssignment(Token::Kind op) {
1875 switch (op) {
1876 case Token::Kind::TK_PLUSEQ: return Token::Kind::TK_PLUS;
1877 case Token::Kind::TK_MINUSEQ: return Token::Kind::TK_MINUS;
1878 case Token::Kind::TK_STAREQ: return Token::Kind::TK_STAR;
1879 case Token::Kind::TK_SLASHEQ: return Token::Kind::TK_SLASH;
1880 case Token::Kind::TK_PERCENTEQ: return Token::Kind::TK_PERCENT;
1881 case Token::Kind::TK_SHLEQ: return Token::Kind::TK_SHL;
1882 case Token::Kind::TK_SHREQ: return Token::Kind::TK_SHR;
1883 case Token::Kind::TK_BITWISEOREQ: return Token::Kind::TK_BITWISEOR;
1884 case Token::Kind::TK_BITWISEXOREQ: return Token::Kind::TK_BITWISEXOR;
1885 case Token::Kind::TK_BITWISEANDEQ: return Token::Kind::TK_BITWISEAND;
1886 case Token::Kind::TK_LOGICALOREQ: return Token::Kind::TK_LOGICALOR;
1887 case Token::Kind::TK_LOGICALXOREQ: return Token::Kind::TK_LOGICALXOR;
1888 case Token::Kind::TK_LOGICALANDEQ: return Token::Kind::TK_LOGICALAND;
1889 default: return op;
1890 }
1891}
1892
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001893Position Compiler::position(int offset) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001894 SkASSERT(fSource);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001895 int line = 1;
1896 int column = 1;
1897 for (int i = 0; i < offset; i++) {
1898 if ((*fSource)[i] == '\n') {
1899 ++line;
1900 column = 1;
1901 }
1902 else {
1903 ++column;
1904 }
1905 }
1906 return Position(line, column);
1907}
1908
1909void Compiler::error(int offset, String msg) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001910 fErrorCount++;
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001911 Position pos = this->position(offset);
1912 fErrorText += "error: " + to_string(pos.fLine) + ": " + msg.c_str() + "\n";
ethannicholasb3058bd2016-07-01 08:22:01 -07001913}
1914
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001915String Compiler::errorText() {
Ethan Nicholas00543112018-07-31 09:44:36 -04001916 this->writeErrorCount();
1917 fErrorCount = 0;
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001918 String result = fErrorText;
ethannicholasb3058bd2016-07-01 08:22:01 -07001919 return result;
1920}
1921
1922void Compiler::writeErrorCount() {
1923 if (fErrorCount) {
1924 fErrorText += to_string(fErrorCount) + " error";
1925 if (fErrorCount > 1) {
1926 fErrorText += "s";
1927 }
1928 fErrorText += "\n";
1929 }
1930}
1931
John Stilesa6841be2020-08-06 14:11:56 -04001932} // namespace SkSL