blob: 6ac8f33199944821624c6f12425282e43eea2688 [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"
Brian Osmanb06301e2020-11-06 11:45:36 -050063#include "src/sksl/generated/sksl_public.dehydrated.sksl"
Brian Osman91946752020-12-21 13:20:40 -050064#include "src/sksl/generated/sksl_runtime.dehydrated.sksl"
Ethan Nicholasc18bb512020-07-28 14:46:53 -040065#include "src/sksl/generated/sksl_vert.dehydrated.sksl"
66
Brian Osman3d87e9f2020-10-08 11:50:22 -040067#define MODULE_DATA(name) MakeModuleData(SKSL_INCLUDE_sksl_##name,\
68 SKSL_INCLUDE_sksl_##name##_LENGTH)
Ethan Nicholasc18bb512020-07-28 14:46:53 -040069
70#endif
Ethan Nicholas0d997662019-04-08 09:46:01 -040071
ethannicholasb3058bd2016-07-01 08:22:01 -070072namespace SkSL {
73
Brian Osman88cda172020-10-09 12:05:16 -040074class AutoSource {
75public:
76 AutoSource(Compiler* compiler, const String* source)
77 : fCompiler(compiler), fOldSource(fCompiler->fSource) {
78 fCompiler->fSource = source;
79 }
80
81 ~AutoSource() { fCompiler->fSource = fOldSource; }
82
83 Compiler* fCompiler;
84 const String* fOldSource;
85};
86
Brian Osmand7e76592020-11-02 12:26:22 -050087Compiler::Compiler(const ShaderCapsClass* caps, Flags flags)
Brian Osman0006ad02020-11-18 15:38:39 -050088 : fContext(std::make_shared<Context>())
89 , fCaps(caps)
John Stiles7b920442020-12-17 10:43:41 -050090 , fInliner(fContext.get())
Brian Osman0006ad02020-11-18 15:38:39 -050091 , fFlags(flags)
92 , fErrorCount(0) {
93 SkASSERT(fCaps);
John Stiles7c3515b2020-10-16 18:38:39 -040094 fRootSymbolTable = std::make_shared<SymbolTable>(this, /*builtin=*/true);
Brian Osmanb06301e2020-11-06 11:45:36 -050095 fPrivateSymbolTable = std::make_shared<SymbolTable>(fRootSymbolTable, /*builtin=*/true);
John Stiles0f464502020-11-20 12:52:22 -050096 fIRGenerator = std::make_unique<IRGenerator>(fContext.get(), fCaps, *this);
ethannicholasb3058bd2016-07-01 08:22:01 -070097
Brian Osmanb06301e2020-11-06 11:45:36 -050098#define TYPE(t) fContext->f##t##_Type.get()
ethannicholasb3058bd2016-07-01 08:22:01 -070099
Brian Osmanb06301e2020-11-06 11:45:36 -0500100 const SkSL::Symbol* rootTypes[] = {
101 TYPE(Void),
Brian Salomonbf7b6202016-11-11 16:08:03 -0500102
Brian Osmanb06301e2020-11-06 11:45:36 -0500103 TYPE( Float), TYPE( Float2), TYPE( Float3), TYPE( Float4),
104 TYPE( Half), TYPE( Half2), TYPE( Half3), TYPE( Half4),
105 TYPE( Int), TYPE( Int2), TYPE( Int3), TYPE( Int4),
Brian Osmanb06301e2020-11-06 11:45:36 -0500106 TYPE( Bool), TYPE( Bool2), TYPE( Bool3), TYPE( Bool4),
Brian Salomon2a51de82016-11-16 12:06:01 -0500107
Brian Osmanc0f2b642020-12-22 13:35:55 -0500108 TYPE(Float2x2), TYPE(Float3x3), TYPE(Float4x4),
Brian Osmanc63f4312020-12-23 11:44:14 -0500109 TYPE( Half2x2), TYPE( Half3x3), TYPE(Half4x4),
Greg Daniel64773e62016-11-22 09:44:03 -0500110
Brian Osmanc63f4312020-12-23 11:44:14 -0500111 TYPE(SquareMat), TYPE(SquareHMat),
ethannicholasb3058bd2016-07-01 08:22:01 -0700112
Brian Osman20fad322020-12-23 12:42:33 -0500113 TYPE(GenType), TYPE(GenHType), TYPE(GenIType), TYPE(GenBType),
114 TYPE(Vec), TYPE(HVec), TYPE(IVec), TYPE(BVec),
Brian Osmanb06301e2020-11-06 11:45:36 -0500115
116 TYPE(FragmentProcessor),
117 };
118
119 const SkSL::Symbol* privateTypes[] = {
Brian Osman20fad322020-12-23 12:42:33 -0500120 TYPE( UInt), TYPE( UInt2), TYPE( UInt3), TYPE( UInt4),
121 TYPE( Short), TYPE( Short2), TYPE( Short3), TYPE( Short4),
122 TYPE(UShort), TYPE(UShort2), TYPE(UShort3), TYPE(UShort4),
123 TYPE( Byte), TYPE( Byte2), TYPE( Byte3), TYPE( Byte4),
124 TYPE( UByte), TYPE( UByte2), TYPE( UByte3), TYPE( UByte4),
125
126 TYPE(GenUType), TYPE(UVec),
127 TYPE(SVec), TYPE(USVec), TYPE(ByteVec), TYPE(UByteVec),
128
Brian Osmanc0f2b642020-12-22 13:35:55 -0500129 TYPE(Float2x3), TYPE(Float2x4),
130 TYPE(Float3x2), TYPE(Float3x4),
131 TYPE(Float4x2), TYPE(Float4x3),
132
Brian Osmanc63f4312020-12-23 11:44:14 -0500133 TYPE(Half2x3), TYPE(Half2x4),
134 TYPE(Half3x2), TYPE(Half3x4),
135 TYPE(Half4x2), TYPE(Half4x3),
136
Brian Osmanc0f2b642020-12-22 13:35:55 -0500137 TYPE(Mat), TYPE(HMat),
138
Brian Osmanb06301e2020-11-06 11:45:36 -0500139 TYPE(Sampler1D), TYPE(Sampler2D), TYPE(Sampler3D),
140 TYPE(SamplerExternalOES),
Brian Osmanb06301e2020-11-06 11:45:36 -0500141 TYPE(Sampler2DRect),
Brian Osmanb06301e2020-11-06 11:45:36 -0500142
143 TYPE(ISampler2D),
144 TYPE(Image2D), TYPE(IImage2D),
145 TYPE(SubpassInput), TYPE(SubpassInputMS),
146
Brian Osmanb06301e2020-11-06 11:45:36 -0500147 TYPE(Sampler),
148 TYPE(Texture2D),
149 };
150
151 for (const SkSL::Symbol* type : rootTypes) {
152 fRootSymbolTable->addWithoutOwnership(type);
153 }
154 for (const SkSL::Symbol* type : privateTypes) {
155 fPrivateSymbolTable->addWithoutOwnership(type);
156 }
157
158#undef TYPE
ethannicholasb3058bd2016-07-01 08:22:01 -0700159
Brian Osman3887a012020-09-30 13:22:27 -0400160 // sk_Caps is "builtin", but all references to it are resolved to Settings, so we don't need to
161 // treat it as builtin (ie, no need to clone it into the Program).
Brian Osmanb06301e2020-11-06 11:45:36 -0500162 fPrivateSymbolTable->add(
163 std::make_unique<Variable>(/*offset=*/-1,
John Stiles586df952020-11-12 18:27:13 -0500164 fIRGenerator->fModifiers->addToPool(Modifiers()),
Brian Osmanb06301e2020-11-06 11:45:36 -0500165 "sk_Caps",
166 fContext->fSkCaps_Type.get(),
167 /*builtin=*/false,
168 Variable::Storage::kGlobal));
Ethan Nicholas3605ace2016-11-21 15:59:48 -0500169
Brian Osman3d87e9f2020-10-08 11:50:22 -0400170 fRootModule = {fRootSymbolTable, /*fIntrinsics=*/nullptr};
Brian Osmanb06301e2020-11-06 11:45:36 -0500171 fPrivateModule = {fPrivateSymbolTable, /*fIntrinsics=*/nullptr};
ethannicholasb3058bd2016-07-01 08:22:01 -0700172}
173
John Stilesdd13dba2020-10-29 10:45:34 -0400174Compiler::~Compiler() {}
ethannicholasb3058bd2016-07-01 08:22:01 -0700175
Brian Osman56269982020-11-20 12:38:07 -0500176const ParsedModule& Compiler::loadGPUModule() {
177 if (!fGPUModule.fSymbols) {
178 fGPUModule = this->parseModule(Program::kFragment_Kind, MODULE_DATA(gpu), fPrivateModule);
179 }
180 return fGPUModule;
181}
182
183const ParsedModule& Compiler::loadFragmentModule() {
184 if (!fFragmentModule.fSymbols) {
185 fFragmentModule = this->parseModule(Program::kFragment_Kind, MODULE_DATA(frag),
186 this->loadGPUModule());
187 }
188 return fFragmentModule;
189}
190
191const ParsedModule& Compiler::loadVertexModule() {
192 if (!fVertexModule.fSymbols) {
193 fVertexModule = this->parseModule(Program::kVertex_Kind, MODULE_DATA(vert),
194 this->loadGPUModule());
195 }
196 return fVertexModule;
197}
198
Brian Osman88cda172020-10-09 12:05:16 -0400199const ParsedModule& Compiler::loadGeometryModule() {
Brian Osman3d87e9f2020-10-08 11:50:22 -0400200 if (!fGeometryModule.fSymbols) {
Brian Osman56269982020-11-20 12:38:07 -0500201 fGeometryModule = this->parseModule(Program::kGeometry_Kind, MODULE_DATA(geom),
202 this->loadGPUModule());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400203 }
Brian Osman88cda172020-10-09 12:05:16 -0400204 return fGeometryModule;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400205}
206
Brian Osman88cda172020-10-09 12:05:16 -0400207const ParsedModule& Compiler::loadFPModule() {
Brian Osman3d87e9f2020-10-08 11:50:22 -0400208 if (!fFPModule.fSymbols) {
Brian Osman56269982020-11-20 12:38:07 -0500209 fFPModule = this->parseModule(Program::kFragmentProcessor_Kind, MODULE_DATA(fp),
210 this->loadGPUModule());
Brian Osman8e2ef022020-09-30 13:26:43 -0400211 }
Brian Osman88cda172020-10-09 12:05:16 -0400212 return fFPModule;
Brian Osman8e2ef022020-09-30 13:26:43 -0400213}
214
Brian Osmanb06301e2020-11-06 11:45:36 -0500215const ParsedModule& Compiler::loadPublicModule() {
216 if (!fPublicModule.fSymbols) {
217 fPublicModule = this->parseModule(Program::kGeneric_Kind, MODULE_DATA(public), fRootModule);
218 }
219 return fPublicModule;
220}
221
Brian Osman91946752020-12-21 13:20:40 -0500222const ParsedModule& Compiler::loadRuntimeEffectModule() {
223 if (!fRuntimeEffectModule.fSymbols) {
224 fRuntimeEffectModule = this->parseModule(Program::kRuntimeEffect_Kind, MODULE_DATA(runtime),
225 this->loadPublicModule());
Brian Osmanf1319c32020-10-13 09:34:23 -0400226
Brian Osman91946752020-12-21 13:20:40 -0500227 // Add some aliases to the runtime effect module so that it's friendlier, and more like GLSL
228 fRuntimeEffectModule.fSymbols->addAlias("shader", fContext->fFragmentProcessor_Type.get());
Brian Osmanf1319c32020-10-13 09:34:23 -0400229
Brian Osman91946752020-12-21 13:20:40 -0500230 fRuntimeEffectModule.fSymbols->addAlias("vec2", fContext->fFloat2_Type.get());
231 fRuntimeEffectModule.fSymbols->addAlias("vec3", fContext->fFloat3_Type.get());
232 fRuntimeEffectModule.fSymbols->addAlias("vec4", fContext->fFloat4_Type.get());
Brian Osmanf1319c32020-10-13 09:34:23 -0400233
Brian Osman91946752020-12-21 13:20:40 -0500234 fRuntimeEffectModule.fSymbols->addAlias("bvec2", fContext->fBool2_Type.get());
235 fRuntimeEffectModule.fSymbols->addAlias("bvec3", fContext->fBool3_Type.get());
236 fRuntimeEffectModule.fSymbols->addAlias("bvec4", fContext->fBool4_Type.get());
Brian Osmanf1319c32020-10-13 09:34:23 -0400237
Brian Osman91946752020-12-21 13:20:40 -0500238 fRuntimeEffectModule.fSymbols->addAlias("mat2", fContext->fFloat2x2_Type.get());
239 fRuntimeEffectModule.fSymbols->addAlias("mat3", fContext->fFloat3x3_Type.get());
240 fRuntimeEffectModule.fSymbols->addAlias("mat4", fContext->fFloat4x4_Type.get());
Brian Osmanf1319c32020-10-13 09:34:23 -0400241
Brian Osman91946752020-12-21 13:20:40 -0500242 fRuntimeEffectModule.fSymbols->addAlias("mat2x2", fContext->fFloat2x2_Type.get());
243 fRuntimeEffectModule.fSymbols->addAlias("mat2x3", fContext->fFloat2x3_Type.get());
244 fRuntimeEffectModule.fSymbols->addAlias("mat2x4", fContext->fFloat2x4_Type.get());
Brian Osmanf1319c32020-10-13 09:34:23 -0400245
Brian Osman91946752020-12-21 13:20:40 -0500246 fRuntimeEffectModule.fSymbols->addAlias("mat3x2", fContext->fFloat3x2_Type.get());
247 fRuntimeEffectModule.fSymbols->addAlias("mat3x3", fContext->fFloat3x3_Type.get());
248 fRuntimeEffectModule.fSymbols->addAlias("mat3x4", fContext->fFloat3x4_Type.get());
Brian Osmanf1319c32020-10-13 09:34:23 -0400249
Brian Osman91946752020-12-21 13:20:40 -0500250 fRuntimeEffectModule.fSymbols->addAlias("mat4x2", fContext->fFloat4x2_Type.get());
251 fRuntimeEffectModule.fSymbols->addAlias("mat4x3", fContext->fFloat4x3_Type.get());
252 fRuntimeEffectModule.fSymbols->addAlias("mat4x4", fContext->fFloat4x4_Type.get());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400253 }
Brian Osman91946752020-12-21 13:20:40 -0500254 return fRuntimeEffectModule;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400255}
256
Brian Osman88cda172020-10-09 12:05:16 -0400257const ParsedModule& Compiler::loadInterpreterModule() {
Brian Osman3d87e9f2020-10-08 11:50:22 -0400258 if (!fInterpreterModule.fSymbols) {
Brian Osmanb06301e2020-11-06 11:45:36 -0500259 fInterpreterModule = this->parseModule(Program::kGeneric_Kind, MODULE_DATA(interp),
260 this->loadPublicModule());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400261 }
Brian Osman88cda172020-10-09 12:05:16 -0400262 return fInterpreterModule;
263}
264
265const ParsedModule& Compiler::moduleForProgramKind(Program::Kind kind) {
266 switch (kind) {
Brian Osman91946752020-12-21 13:20:40 -0500267 case Program::kVertex_Kind: return this->loadVertexModule(); break;
268 case Program::kFragment_Kind: return this->loadFragmentModule(); break;
269 case Program::kGeometry_Kind: return this->loadGeometryModule(); break;
270 case Program::kFragmentProcessor_Kind: return this->loadFPModule(); break;
271 case Program::kRuntimeEffect_Kind: return this->loadRuntimeEffectModule(); break;
272 case Program::kGeneric_Kind: return this->loadInterpreterModule(); break;
Brian Osman88cda172020-10-09 12:05:16 -0400273 }
274 SkUNREACHABLE;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400275}
276
Brian Osman3d87e9f2020-10-08 11:50:22 -0400277LoadedModule Compiler::loadModule(Program::Kind kind,
278 ModuleData data,
279 std::shared_ptr<SymbolTable> base) {
Brian Osman3d87e9f2020-10-08 11:50:22 -0400280 if (!base) {
Brian Osmanb06301e2020-11-06 11:45:36 -0500281 // NOTE: This is a workaround. The only time 'base' is null is when dehydrating includes.
282 // In that case, skslc doesn't know which module it's preparing, nor what the correct base
283 // module is. We can't use 'Root', because many GPU intrinsics reference private types,
284 // like samplers or textures. Today, 'Private' does contain the union of all known types,
285 // so this is safe. If we ever have types that only exist in 'Public' (for example), this
286 // logic needs to be smarter (by choosing the correct base for the module we're compiling).
287 base = fPrivateSymbolTable;
Brian Osman3d87e9f2020-10-08 11:50:22 -0400288 }
289
290#if defined(SKSL_STANDALONE)
291 SkASSERT(data.fPath);
292 std::ifstream in(data.fPath);
Brian Osmane498b3c2020-09-23 14:42:11 -0400293 std::unique_ptr<String> text = std::make_unique<String>(std::istreambuf_iterator<char>(in),
294 std::istreambuf_iterator<char>());
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -0400295 if (in.rdstate()) {
Brian Osman3d87e9f2020-10-08 11:50:22 -0400296 printf("error reading %s\n", data.fPath);
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -0400297 abort();
298 }
Brian Osmane498b3c2020-09-23 14:42:11 -0400299 const String* source = fRootSymbolTable->takeOwnershipOfString(std::move(text));
Brian Osman88cda172020-10-09 12:05:16 -0400300 AutoSource as(this, source);
Ethan Nicholas8da1e652019-05-24 11:01:59 -0400301 Program::Settings settings;
John Stiles881a10c2020-09-19 10:13:24 -0400302 SkASSERT(fIRGenerator->fCanInline);
303 fIRGenerator->fCanInline = false;
Brian Osman88cda172020-10-09 12:05:16 -0400304 ParsedModule baseModule = {base, /*fIntrinsics=*/nullptr};
Brian Osmand7e76592020-11-02 12:26:22 -0500305 IRGenerator::IRBundle ir =
Brian Osman0006ad02020-11-18 15:38:39 -0500306 fIRGenerator->convertProgram(kind, &settings, baseModule,
Brian Osmand7e76592020-11-02 12:26:22 -0500307 /*isBuiltinCode=*/true, source->c_str(), source->length(),
Brian Osmanbe0b3b72021-01-06 14:27:35 -0500308 /*externalFunctions=*/nullptr);
Brian Osman133724c2020-10-28 14:14:39 -0400309 SkASSERT(ir.fSharedElements.empty());
Brian Osman0006ad02020-11-18 15:38:39 -0500310 LoadedModule module = { kind, std::move(ir.fSymbolTable), std::move(ir.fElements) };
John Stiles881a10c2020-09-19 10:13:24 -0400311 fIRGenerator->fCanInline = true;
Ethan Nicholas8da1e652019-05-24 11:01:59 -0400312 if (this->fErrorCount) {
313 printf("Unexpected errors: %s\n", this->fErrorText.c_str());
Brian Osman3d87e9f2020-10-08 11:50:22 -0400314 SkDEBUGFAILF("%s %s\n", data.fPath, this->fErrorText.c_str());
Ethan Nicholas8da1e652019-05-24 11:01:59 -0400315 }
Brian Osman88cda172020-10-09 12:05:16 -0400316 fModifiers.push_back(std::move(ir.fModifiers));
Brian Osman3d87e9f2020-10-08 11:50:22 -0400317#else
318 SkASSERT(data.fData && (data.fSize != 0));
319 Rehydrator rehydrator(fContext.get(), fIRGenerator->fModifiers.get(), base, this,
320 data.fData, data.fSize);
Brian Osman0006ad02020-11-18 15:38:39 -0500321 LoadedModule module = { kind, rehydrator.symbolTable(), rehydrator.elements() };
Brian Osman3d87e9f2020-10-08 11:50:22 -0400322 fModifiers.push_back(fIRGenerator->releaseModifiers());
323#endif
324
325 return module;
326}
327
328ParsedModule Compiler::parseModule(Program::Kind kind, ModuleData data, const ParsedModule& base) {
Brian Osman0006ad02020-11-18 15:38:39 -0500329 LoadedModule module = this->loadModule(kind, data, base.fSymbols);
330 this->optimize(module);
Brian Osman3d87e9f2020-10-08 11:50:22 -0400331
332 // For modules that just declare (but don't define) intrinsic functions, there will be no new
333 // program elements. In that case, we can share our parent's intrinsic map:
Brian Osman0006ad02020-11-18 15:38:39 -0500334 if (module.fElements.empty()) {
335 return {module.fSymbols, base.fIntrinsics};
Brian Osman3d87e9f2020-10-08 11:50:22 -0400336 }
337
338 auto intrinsics = std::make_shared<IRIntrinsicMap>(base.fIntrinsics.get());
339
340 // Now, transfer all of the program elements to an intrinsic map. This maps certain types of
341 // global objects to the declaring ProgramElement.
Brian Osman0006ad02020-11-18 15:38:39 -0500342 for (std::unique_ptr<ProgramElement>& element : module.fElements) {
Brian Osman3d87e9f2020-10-08 11:50:22 -0400343 switch (element->kind()) {
344 case ProgramElement::Kind::kFunction: {
345 const FunctionDefinition& f = element->as<FunctionDefinition>();
Ethan Nicholas0a5d0962020-10-14 13:33:18 -0400346 SkASSERT(f.declaration().isBuiltin());
347 intrinsics->insertOrDie(f.declaration().description(), std::move(element));
Brian Osman3d87e9f2020-10-08 11:50:22 -0400348 break;
349 }
John Stiles569249b2020-11-03 12:18:22 -0500350 case ProgramElement::Kind::kFunctionPrototype: {
351 // These are already in the symbol table.
352 break;
353 }
Brian Osman3d87e9f2020-10-08 11:50:22 -0400354 case ProgramElement::Kind::kEnum: {
355 const Enum& e = element->as<Enum>();
356 SkASSERT(e.isBuiltin());
357 intrinsics->insertOrDie(e.typeName(), std::move(element));
358 break;
359 }
360 case ProgramElement::Kind::kGlobalVar: {
Ethan Nicholasc51f33e2020-10-13 13:49:44 -0400361 const GlobalVarDeclaration& global = element->as<GlobalVarDeclaration>();
362 const Variable& var = global.declaration()->as<VarDeclaration>().var();
363 SkASSERT(var.isBuiltin());
364 intrinsics->insertOrDie(var.name(), std::move(element));
Brian Osman3d87e9f2020-10-08 11:50:22 -0400365 break;
366 }
367 case ProgramElement::Kind::kInterfaceBlock: {
Ethan Nicholaseaf47882020-10-15 10:10:08 -0400368 const Variable& var = element->as<InterfaceBlock>().variable();
369 SkASSERT(var.isBuiltin());
370 intrinsics->insertOrDie(var.name(), std::move(element));
Brian Osman3d87e9f2020-10-08 11:50:22 -0400371 break;
372 }
373 default:
374 printf("Unsupported element: %s\n", element->description().c_str());
375 SkASSERT(false);
376 break;
377 }
378 }
379
Brian Osman0006ad02020-11-18 15:38:39 -0500380 return {module.fSymbols, std::move(intrinsics)};
Ethan Nicholas8da1e652019-05-24 11:01:59 -0400381}
382
ethannicholas22f939e2016-10-13 13:25:34 -0700383// add the definition created by assigning to the lvalue to the definition set
Ethan Nicholas86a43402017-01-19 13:32:00 -0500384void Compiler::addDefinition(const Expression* lvalue, std::unique_ptr<Expression>* expr,
385 DefinitionMap* definitions) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400386 switch (lvalue->kind()) {
387 case Expression::Kind::kVariableReference: {
Ethan Nicholas78686922020-10-08 06:46:27 -0400388 const Variable& var = *lvalue->as<VariableReference>().variable();
Ethan Nicholas453f67f2020-10-09 10:43:45 -0400389 if (var.storage() == Variable::Storage::kLocal) {
John Stiles796cdb72020-10-08 12:06:53 -0400390 definitions->set(&var, expr);
ethannicholas22f939e2016-10-13 13:25:34 -0700391 }
392 break;
393 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400394 case Expression::Kind::kSwizzle:
ethannicholas22f939e2016-10-13 13:25:34 -0700395 // We consider the variable written to as long as at least some of its components have
396 // been written to. This will lead to some false negatives (we won't catch it if you
397 // write to foo.x and then read foo.y), but being stricter could lead to false positives
Mike Klein6ad99092016-10-26 10:35:22 -0400398 // (we write to foo.x, and then pass foo to a function which happens to only read foo.x,
399 // 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 -0700400 // more complicated whole-program analysis. This is probably good enough.
Ethan Nicholas6b4d5812020-10-12 16:11:51 -0400401 this->addDefinition(lvalue->as<Swizzle>().base().get(),
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400402 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
ethannicholas22f939e2016-10-13 13:25:34 -0700403 definitions);
404 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400405 case Expression::Kind::kIndex:
ethannicholas22f939e2016-10-13 13:25:34 -0700406 // see comments in Swizzle
Ethan Nicholas2a4952d2020-10-08 15:35:56 -0400407 this->addDefinition(lvalue->as<IndexExpression>().base().get(),
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400408 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
ethannicholas22f939e2016-10-13 13:25:34 -0700409 definitions);
410 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400411 case Expression::Kind::kFieldAccess:
ethannicholas22f939e2016-10-13 13:25:34 -0700412 // see comments in Swizzle
Ethan Nicholas7a95b202020-10-09 11:55:40 -0400413 this->addDefinition(lvalue->as<FieldAccess>().base().get(),
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400414 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
ethannicholas22f939e2016-10-13 13:25:34 -0700415 definitions);
416 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400417 case Expression::Kind::kTernary:
Ethan Nicholasa583b812018-01-18 13:32:11 -0500418 // To simplify analysis, we just pretend that we write to both sides of the ternary.
419 // This allows for false positives (meaning we fail to detect that a variable might not
420 // have been assigned), but is preferable to false negatives.
Ethan Nicholasdd218162020-10-08 05:48:01 -0400421 this->addDefinition(lvalue->as<TernaryExpression>().ifTrue().get(),
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400422 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
Ethan Nicholasa583b812018-01-18 13:32:11 -0500423 definitions);
Ethan Nicholasdd218162020-10-08 05:48:01 -0400424 this->addDefinition(lvalue->as<TernaryExpression>().ifFalse().get(),
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400425 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
Ethan Nicholasa583b812018-01-18 13:32:11 -0500426 definitions);
427 break;
ethannicholas22f939e2016-10-13 13:25:34 -0700428 default:
429 // not an lvalue, can't happen
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400430 SkASSERT(false);
ethannicholas22f939e2016-10-13 13:25:34 -0700431 }
432}
433
434// add local variables defined by this node to the set
John Stiles796cdb72020-10-08 12:06:53 -0400435void Compiler::addDefinitions(const BasicBlock::Node& node, DefinitionMap* definitions) {
John Stiles70025e52020-09-28 16:08:58 -0400436 if (node.isExpression()) {
437 Expression* expr = node.expression()->get();
438 switch (expr->kind()) {
439 case Expression::Kind::kBinary: {
440 BinaryExpression* b = &expr->as<BinaryExpression>();
441 if (b->getOperator() == Token::Kind::TK_EQ) {
John Stiles2d4f9592020-10-30 10:29:12 -0400442 this->addDefinition(b->left().get(), &b->right(), definitions);
John Stiles70025e52020-09-28 16:08:58 -0400443 } else if (Compiler::IsAssignment(b->getOperator())) {
444 this->addDefinition(
John Stiles2d4f9592020-10-30 10:29:12 -0400445 b->left().get(),
John Stiles70025e52020-09-28 16:08:58 -0400446 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
447 definitions);
Ethan Nicholas86a43402017-01-19 13:32:00 -0500448
ethannicholas22f939e2016-10-13 13:25:34 -0700449 }
John Stiles70025e52020-09-28 16:08:58 -0400450 break;
ethannicholas22f939e2016-10-13 13:25:34 -0700451 }
John Stiles70025e52020-09-28 16:08:58 -0400452 case Expression::Kind::kFunctionCall: {
453 const FunctionCall& c = expr->as<FunctionCall>();
Brian Osman5bf3e202020-10-13 10:34:18 -0400454 const std::vector<const Variable*>& parameters = c.function().parameters();
Ethan Nicholased84b732020-10-08 11:45:44 -0400455 for (size_t i = 0; i < parameters.size(); ++i) {
456 if (parameters[i]->modifiers().fFlags & Modifiers::kOut_Flag) {
John Stiles70025e52020-09-28 16:08:58 -0400457 this->addDefinition(
Ethan Nicholas0dec9922020-10-05 15:51:52 -0400458 c.arguments()[i].get(),
John Stiles70025e52020-09-28 16:08:58 -0400459 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
460 definitions);
461 }
462 }
463 break;
464 }
465 case Expression::Kind::kPrefix: {
466 const PrefixExpression* p = &expr->as<PrefixExpression>();
Ethan Nicholas444ccc62020-10-09 10:16:22 -0400467 if (p->getOperator() == Token::Kind::TK_MINUSMINUS ||
468 p->getOperator() == Token::Kind::TK_PLUSPLUS) {
John Stiles70025e52020-09-28 16:08:58 -0400469 this->addDefinition(
Ethan Nicholas444ccc62020-10-09 10:16:22 -0400470 p->operand().get(),
John Stiles70025e52020-09-28 16:08:58 -0400471 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
472 definitions);
473 }
474 break;
475 }
476 case Expression::Kind::kPostfix: {
477 const PostfixExpression* p = &expr->as<PostfixExpression>();
Ethan Nicholas444ccc62020-10-09 10:16:22 -0400478 if (p->getOperator() == Token::Kind::TK_MINUSMINUS ||
479 p->getOperator() == Token::Kind::TK_PLUSPLUS) {
John Stiles70025e52020-09-28 16:08:58 -0400480 this->addDefinition(
Ethan Nicholas444ccc62020-10-09 10:16:22 -0400481 p->operand().get(),
John Stiles70025e52020-09-28 16:08:58 -0400482 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
483 definitions);
484 }
485 break;
486 }
487 case Expression::Kind::kVariableReference: {
488 const VariableReference* v = &expr->as<VariableReference>();
Ethan Nicholas453f67f2020-10-09 10:43:45 -0400489 if (v->refKind() != VariableReference::RefKind::kRead) {
John Stiles70025e52020-09-28 16:08:58 -0400490 this->addDefinition(
491 v,
492 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
493 definitions);
494 }
495 break;
496 }
497 default:
498 break;
ethannicholas22f939e2016-10-13 13:25:34 -0700499 }
John Stiles70025e52020-09-28 16:08:58 -0400500 } else if (node.isStatement()) {
501 Statement* stmt = node.statement()->get();
502 if (stmt->is<VarDeclaration>()) {
503 VarDeclaration& vd = stmt->as<VarDeclaration>();
Ethan Nicholasc51f33e2020-10-13 13:49:44 -0400504 if (vd.value()) {
505 definitions->set(&vd.var(), &vd.value());
ethannicholas22f939e2016-10-13 13:25:34 -0700506 }
ethannicholas22f939e2016-10-13 13:25:34 -0700507 }
508 }
509}
510
John Stilese6150002020-10-05 12:03:53 -0400511void Compiler::scanCFG(CFG* cfg, BlockId blockId, SkBitSet* processedSet) {
ethannicholas22f939e2016-10-13 13:25:34 -0700512 BasicBlock& block = cfg->fBlocks[blockId];
513
514 // compute definitions after this block
Ethan Nicholas86a43402017-01-19 13:32:00 -0500515 DefinitionMap after = block.fBefore;
ethannicholas22f939e2016-10-13 13:25:34 -0700516 for (const BasicBlock::Node& n : block.fNodes) {
517 this->addDefinitions(n, &after);
518 }
519
520 // propagate definitions to exits
521 for (BlockId exitId : block.fExits) {
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400522 if (exitId == blockId) {
523 continue;
524 }
ethannicholas22f939e2016-10-13 13:25:34 -0700525 BasicBlock& exit = cfg->fBlocks[exitId];
John Stiles65b48272020-12-22 17:18:34 -0500526 for (const auto& [var, e1] : after) {
John Stiles796cdb72020-10-08 12:06:53 -0400527 std::unique_ptr<Expression>** exitDef = exit.fBefore.find(var);
528 if (!exitDef) {
John Stilese6150002020-10-05 12:03:53 -0400529 // exit has no definition for it, just copy it and reprocess exit block
530 processedSet->reset(exitId);
John Stiles796cdb72020-10-08 12:06:53 -0400531 exit.fBefore[var] = e1;
ethannicholas22f939e2016-10-13 13:25:34 -0700532 } else {
Ethan Nicholas86a43402017-01-19 13:32:00 -0500533 // exit has a (possibly different) value already defined
John Stiles796cdb72020-10-08 12:06:53 -0400534 std::unique_ptr<Expression>* e2 = *exitDef;
ethannicholas22f939e2016-10-13 13:25:34 -0700535 if (e1 != e2) {
John Stilese6150002020-10-05 12:03:53 -0400536 // definition has changed, merge and reprocess the exit block
537 processedSet->reset(exitId);
Ethan Nicholasaf197692017-02-27 13:26:45 -0500538 if (e1 && e2) {
John Stiles796cdb72020-10-08 12:06:53 -0400539 *exitDef = (std::unique_ptr<Expression>*)&fContext->fDefined_Expression;
Ethan Nicholasaf197692017-02-27 13:26:45 -0500540 } else {
John Stiles796cdb72020-10-08 12:06:53 -0400541 *exitDef = nullptr;
Ethan Nicholasaf197692017-02-27 13:26:45 -0500542 }
ethannicholas22f939e2016-10-13 13:25:34 -0700543 }
544 }
John Stiles65b48272020-12-22 17:18:34 -0500545 }
ethannicholas22f939e2016-10-13 13:25:34 -0700546 }
547}
548
549// returns a map which maps all local variables in the function to null, indicating that their value
550// is initially unknown
Ethan Nicholas86a43402017-01-19 13:32:00 -0500551static DefinitionMap compute_start_state(const CFG& cfg) {
552 DefinitionMap result;
Mike Klein6ad99092016-10-26 10:35:22 -0400553 for (const auto& block : cfg.fBlocks) {
554 for (const auto& node : block.fNodes) {
John Stiles70025e52020-09-28 16:08:58 -0400555 if (node.isStatement()) {
Ethan Nicholascb670962017-04-20 19:31:52 -0400556 const Statement* s = node.statement()->get();
Brian Osmanc0213602020-10-06 14:43:32 -0400557 if (s->is<VarDeclaration>()) {
Ethan Nicholasc51f33e2020-10-13 13:49:44 -0400558 result[&s->as<VarDeclaration>().var()] = nullptr;
ethannicholas22f939e2016-10-13 13:25:34 -0700559 }
560 }
561 }
562 }
563 return result;
564}
565
Ethan Nicholascb670962017-04-20 19:31:52 -0400566/**
567 * Returns true if assigning to this lvalue has no effect.
568 */
Brian Osman010ce6a2020-10-19 16:34:10 -0400569static bool is_dead(const Expression& lvalue, ProgramUsage* usage) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400570 switch (lvalue.kind()) {
571 case Expression::Kind::kVariableReference:
Brian Osman010ce6a2020-10-19 16:34:10 -0400572 return usage->isDead(*lvalue.as<VariableReference>().variable());
Ethan Nicholase6592142020-09-08 10:22:09 -0400573 case Expression::Kind::kSwizzle:
Brian Osman010ce6a2020-10-19 16:34:10 -0400574 return is_dead(*lvalue.as<Swizzle>().base(), usage);
Ethan Nicholase6592142020-09-08 10:22:09 -0400575 case Expression::Kind::kFieldAccess:
Brian Osman010ce6a2020-10-19 16:34:10 -0400576 return is_dead(*lvalue.as<FieldAccess>().base(), usage);
Ethan Nicholase6592142020-09-08 10:22:09 -0400577 case Expression::Kind::kIndex: {
John Stilesa5a97b42020-08-18 11:19:07 -0400578 const IndexExpression& idx = lvalue.as<IndexExpression>();
Brian Osman010ce6a2020-10-19 16:34:10 -0400579 return is_dead(*idx.base(), usage) &&
Ethan Nicholas2a4952d2020-10-08 15:35:56 -0400580 !idx.index()->hasProperty(Expression::Property::kSideEffects);
Ethan Nicholascb670962017-04-20 19:31:52 -0400581 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400582 case Expression::Kind::kTernary: {
John Stilesa5a97b42020-08-18 11:19:07 -0400583 const TernaryExpression& t = lvalue.as<TernaryExpression>();
Brian Osman010ce6a2020-10-19 16:34:10 -0400584 return !t.test()->hasSideEffects() &&
585 is_dead(*t.ifTrue(), usage) &&
586 is_dead(*t.ifFalse(), usage);
Ethan Nicholasa583b812018-01-18 13:32:11 -0500587 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400588 default:
Ethan Nicholas2a099da2020-01-02 14:40:54 -0500589#ifdef SK_DEBUG
Ethan Nicholascb670962017-04-20 19:31:52 -0400590 ABORT("invalid lvalue: %s\n", lvalue.description().c_str());
Ethan Nicholas2a099da2020-01-02 14:40:54 -0500591#endif
592 return false;
Ethan Nicholascb670962017-04-20 19:31:52 -0400593 }
594}
ethannicholas22f939e2016-10-13 13:25:34 -0700595
Ethan Nicholascb670962017-04-20 19:31:52 -0400596/**
597 * Returns true if this is an assignment which can be collapsed down to just the right hand side due
598 * to a dead target and lack of side effects on the left hand side.
599 */
Brian Osman010ce6a2020-10-19 16:34:10 -0400600static bool dead_assignment(const BinaryExpression& b, ProgramUsage* usage) {
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400601 if (!Compiler::IsAssignment(b.getOperator())) {
Ethan Nicholascb670962017-04-20 19:31:52 -0400602 return false;
603 }
John Stiles2d4f9592020-10-30 10:29:12 -0400604 return is_dead(*b.left(), usage);
Ethan Nicholascb670962017-04-20 19:31:52 -0400605}
606
607void Compiler::computeDataFlow(CFG* cfg) {
608 cfg->fBlocks[cfg->fStart].fBefore = compute_start_state(*cfg);
John Stilese6150002020-10-05 12:03:53 -0400609
610 // We set bits in the "processed" set after a block has been scanned.
611 SkBitSet processedSet(cfg->fBlocks.size());
612 while (SkBitSet::OptionalIndex blockId = processedSet.findFirstUnset()) {
613 processedSet.set(*blockId);
614 this->scanCFG(cfg, *blockId, &processedSet);
ethannicholas22f939e2016-10-13 13:25:34 -0700615 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400616}
617
618/**
619 * Attempts to replace the expression pointed to by iter with a new one (in both the CFG and the
620 * IR). If the expression can be cleanly removed, returns true and updates the iterator to point to
621 * the newly-inserted element. Otherwise updates only the IR and returns false (and the CFG will
622 * need to be regenerated).
623 */
John Stilesafbf8992020-08-18 10:08:21 -0400624static bool try_replace_expression(BasicBlock* b,
625 std::vector<BasicBlock::Node>::iterator* iter,
626 std::unique_ptr<Expression>* newExpression) {
Ethan Nicholascb670962017-04-20 19:31:52 -0400627 std::unique_ptr<Expression>* target = (*iter)->expression();
628 if (!b->tryRemoveExpression(iter)) {
629 *target = std::move(*newExpression);
630 return false;
631 }
632 *target = std::move(*newExpression);
633 return b->tryInsertExpression(iter, target);
634}
635
636/**
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400637 * Returns true if the expression is a constant numeric literal with the specified value, or a
638 * constant vector with all elements equal to the specified value.
Ethan Nicholascb670962017-04-20 19:31:52 -0400639 */
Ethan Nicholasa3f22f12020-10-01 12:13:17 -0400640template <typename T = SKSL_FLOAT>
John Stiles9d944232020-08-19 09:56:49 -0400641static bool is_constant(const Expression& expr, T value) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400642 switch (expr.kind()) {
643 case Expression::Kind::kIntLiteral:
Ethan Nicholase96cdd12020-09-28 16:27:18 -0400644 return expr.as<IntLiteral>().value() == value;
John Stiles9d944232020-08-19 09:56:49 -0400645
Ethan Nicholase6592142020-09-08 10:22:09 -0400646 case Expression::Kind::kFloatLiteral:
Ethan Nicholasa3f22f12020-10-01 12:13:17 -0400647 return expr.as<FloatLiteral>().value() == value;
John Stiles9d944232020-08-19 09:56:49 -0400648
Ethan Nicholase6592142020-09-08 10:22:09 -0400649 case Expression::Kind::kConstructor: {
John Stiles9d944232020-08-19 09:56:49 -0400650 const Constructor& constructor = expr.as<Constructor>();
651 if (constructor.isCompileTimeConstant()) {
Ethan Nicholas30d30222020-09-11 12:27:26 -0400652 const Type& constructorType = constructor.type();
Ethan Nicholas30d30222020-09-11 12:27:26 -0400653 switch (constructorType.typeKind()) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400654 case Type::TypeKind::kVector:
John Stilesbc75ebb2020-11-24 12:04:47 -0500655 if (constructor.componentType().isFloat()) {
656 for (int i = 0; i < constructorType.columns(); ++i) {
John Stiles9d944232020-08-19 09:56:49 -0400657 if (constructor.getFVecComponent(i) != value) {
658 return false;
659 }
John Stilesbc75ebb2020-11-24 12:04:47 -0500660 }
661 return true;
662 } else if (constructor.componentType().isInteger()) {
663 for (int i = 0; i < constructorType.columns(); ++i) {
John Stiles9d944232020-08-19 09:56:49 -0400664 if (constructor.getIVecComponent(i) != value) {
665 return false;
666 }
667 }
John Stilesbc75ebb2020-11-24 12:04:47 -0500668 return true;
Ethan Nicholasd188c182019-06-10 15:55:38 -0400669 }
John Stilesbc75ebb2020-11-24 12:04:47 -0500670 // Other types (e.g. boolean) might occur, but aren't supported here.
671 return false;
John Stiles9d944232020-08-19 09:56:49 -0400672
Ethan Nicholase6592142020-09-08 10:22:09 -0400673 case Type::TypeKind::kScalar:
Ethan Nicholasf70f0442020-09-29 12:41:35 -0400674 SkASSERT(constructor.arguments().size() == 1);
675 return is_constant<T>(*constructor.arguments()[0], value);
John Stiles9d944232020-08-19 09:56:49 -0400676
677 default:
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400678 return false;
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400679 }
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400680 }
681 return false;
682 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400683 default:
684 return false;
685 }
686}
687
688/**
689 * Collapses the binary expression pointed to by iter down to just the right side (in both the IR
690 * and CFG structures).
691 */
John Stilesafbf8992020-08-18 10:08:21 -0400692static void delete_left(BasicBlock* b,
693 std::vector<BasicBlock::Node>::iterator* iter,
Ethan Nicholascdeae8c2020-10-22 14:39:46 -0400694 Compiler::OptimizationContext* optimizationContext) {
695 optimizationContext->fUpdated = true;
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400696 std::unique_ptr<Expression>* target = (*iter)->expression();
John Stilesa5a97b42020-08-18 11:19:07 -0400697 BinaryExpression& bin = (*target)->as<BinaryExpression>();
John Stiles2d4f9592020-10-30 10:29:12 -0400698 Expression& left = *bin.left();
699 std::unique_ptr<Expression>& rightPointer = bin.right();
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400700 SkASSERT(!left.hasSideEffects());
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400701 bool result;
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400702 if (bin.getOperator() == Token::Kind::TK_EQ) {
703 result = b->tryRemoveLValueBefore(iter, &left);
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400704 } else {
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400705 result = b->tryRemoveExpressionBefore(iter, &left);
Ethan Nicholascb670962017-04-20 19:31:52 -0400706 }
Brian Osman010ce6a2020-10-19 16:34:10 -0400707 // Remove references within LHS.
708 optimizationContext->fUsage->remove(&left);
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400709 *target = std::move(rightPointer);
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400710 if (!result) {
Ethan Nicholascdeae8c2020-10-22 14:39:46 -0400711 optimizationContext->fNeedsRescan = true;
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400712 return;
713 }
714 if (*iter == b->fNodes.begin()) {
Ethan Nicholascdeae8c2020-10-22 14:39:46 -0400715 optimizationContext->fNeedsRescan = true;
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400716 return;
717 }
718 --(*iter);
John Stiles70025e52020-09-28 16:08:58 -0400719 if (!(*iter)->isExpression() || (*iter)->expression() != &rightPointer) {
Ethan Nicholascdeae8c2020-10-22 14:39:46 -0400720 optimizationContext->fNeedsRescan = true;
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400721 return;
722 }
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400723 *iter = b->fNodes.erase(*iter);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400724 SkASSERT((*iter)->expression() == target);
Ethan Nicholascb670962017-04-20 19:31:52 -0400725}
726
727/**
728 * Collapses the binary expression pointed to by iter down to just the left side (in both the IR and
729 * CFG structures).
730 */
John Stilesafbf8992020-08-18 10:08:21 -0400731static void delete_right(BasicBlock* b,
732 std::vector<BasicBlock::Node>::iterator* iter,
Ethan Nicholascdeae8c2020-10-22 14:39:46 -0400733 Compiler::OptimizationContext* optimizationContext) {
734 optimizationContext->fUpdated = true;
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400735 std::unique_ptr<Expression>* target = (*iter)->expression();
John Stilesa5a97b42020-08-18 11:19:07 -0400736 BinaryExpression& bin = (*target)->as<BinaryExpression>();
John Stiles2d4f9592020-10-30 10:29:12 -0400737 std::unique_ptr<Expression>& leftPointer = bin.left();
738 Expression& right = *bin.right();
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400739 SkASSERT(!right.hasSideEffects());
Brian Osman010ce6a2020-10-19 16:34:10 -0400740 // Remove references within RHS.
741 optimizationContext->fUsage->remove(&right);
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400742 if (!b->tryRemoveExpressionBefore(iter, &right)) {
743 *target = std::move(leftPointer);
Ethan Nicholascdeae8c2020-10-22 14:39:46 -0400744 optimizationContext->fNeedsRescan = true;
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400745 return;
Ethan Nicholascb670962017-04-20 19:31:52 -0400746 }
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400747 *target = std::move(leftPointer);
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400748 if (*iter == b->fNodes.begin()) {
Ethan Nicholascdeae8c2020-10-22 14:39:46 -0400749 optimizationContext->fNeedsRescan = true;
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400750 return;
751 }
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400752 --(*iter);
John Stiles70025e52020-09-28 16:08:58 -0400753 if ((!(*iter)->isExpression() || (*iter)->expression() != &leftPointer)) {
Ethan Nicholascdeae8c2020-10-22 14:39:46 -0400754 optimizationContext->fNeedsRescan = true;
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400755 return;
756 }
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400757 *iter = b->fNodes.erase(*iter);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400758 SkASSERT((*iter)->expression() == target);
Ethan Nicholascb670962017-04-20 19:31:52 -0400759}
760
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400761/**
762 * Constructs the specified type using a single argument.
763 */
Ethan Nicholas30d30222020-09-11 12:27:26 -0400764static std::unique_ptr<Expression> construct(const Type* type, std::unique_ptr<Expression> v) {
John Stiles8e3b6be2020-10-13 11:14:08 -0400765 ExpressionArray args;
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400766 args.push_back(std::move(v));
Ethan Nicholase6592142020-09-08 10:22:09 -0400767 std::unique_ptr<Expression> result = std::make_unique<Constructor>(-1, type, std::move(args));
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400768 return result;
769}
770
771/**
772 * Used in the implementations of vectorize_left and vectorize_right. Given a vector type and an
773 * expression x, deletes the expression pointed to by iter and replaces it with <type>(x).
774 */
775static void vectorize(BasicBlock* b,
776 std::vector<BasicBlock::Node>::iterator* iter,
777 const Type& type,
778 std::unique_ptr<Expression>* otherExpression,
Ethan Nicholascdeae8c2020-10-22 14:39:46 -0400779 Compiler::OptimizationContext* optimizationContext) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400780 SkASSERT((*(*iter)->expression())->kind() == Expression::Kind::kBinary);
John Stiles9aeed132020-11-24 17:36:06 -0500781 SkASSERT(type.isVector());
782 SkASSERT((*otherExpression)->type().isScalar());
Ethan Nicholascdeae8c2020-10-22 14:39:46 -0400783 optimizationContext->fUpdated = true;
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400784 std::unique_ptr<Expression>* target = (*iter)->expression();
785 if (!b->tryRemoveExpression(iter)) {
Ethan Nicholas30d30222020-09-11 12:27:26 -0400786 *target = construct(&type, std::move(*otherExpression));
Ethan Nicholascdeae8c2020-10-22 14:39:46 -0400787 optimizationContext->fNeedsRescan = true;
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400788 } else {
Ethan Nicholas30d30222020-09-11 12:27:26 -0400789 *target = construct(&type, std::move(*otherExpression));
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400790 if (!b->tryInsertExpression(iter, target)) {
Ethan Nicholascdeae8c2020-10-22 14:39:46 -0400791 optimizationContext->fNeedsRescan = true;
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400792 }
793 }
794}
795
796/**
797 * Given a binary expression of the form x <op> vec<n>(y), deletes the right side and vectorizes the
798 * left to yield vec<n>(x).
799 */
800static void vectorize_left(BasicBlock* b,
801 std::vector<BasicBlock::Node>::iterator* iter,
Ethan Nicholascdeae8c2020-10-22 14:39:46 -0400802 Compiler::OptimizationContext* optimizationContext) {
John Stilesa5a97b42020-08-18 11:19:07 -0400803 BinaryExpression& bin = (*(*iter)->expression())->as<BinaryExpression>();
Brian Osman010ce6a2020-10-19 16:34:10 -0400804 // Remove references within RHS. Vectorization of LHS doesn't change reference counts.
John Stiles2d4f9592020-10-30 10:29:12 -0400805 optimizationContext->fUsage->remove(bin.right().get());
806 vectorize(b, iter, bin.right()->type(), &bin.left(), optimizationContext);
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400807}
808
809/**
810 * Given a binary expression of the form vec<n>(x) <op> y, deletes the left side and vectorizes the
811 * right to yield vec<n>(y).
812 */
813static void vectorize_right(BasicBlock* b,
814 std::vector<BasicBlock::Node>::iterator* iter,
Ethan Nicholascdeae8c2020-10-22 14:39:46 -0400815 Compiler::OptimizationContext* optimizationContext) {
John Stilesa5a97b42020-08-18 11:19:07 -0400816 BinaryExpression& bin = (*(*iter)->expression())->as<BinaryExpression>();
Brian Osman010ce6a2020-10-19 16:34:10 -0400817 // Remove references within LHS. Vectorization of RHS doesn't change reference counts.
John Stiles2d4f9592020-10-30 10:29:12 -0400818 optimizationContext->fUsage->remove(bin.left().get());
819 vectorize(b, iter, bin.left()->type(), &bin.right(), optimizationContext);
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400820}
821
822// Mark that an expression which we were writing to is no longer being written to
John Stilesa5a97b42020-08-18 11:19:07 -0400823static void clear_write(Expression& expr) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400824 switch (expr.kind()) {
825 case Expression::Kind::kVariableReference: {
Ethan Nicholas453f67f2020-10-09 10:43:45 -0400826 expr.as<VariableReference>().setRefKind(VariableReference::RefKind::kRead);
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400827 break;
828 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400829 case Expression::Kind::kFieldAccess:
Ethan Nicholas7a95b202020-10-09 11:55:40 -0400830 clear_write(*expr.as<FieldAccess>().base());
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400831 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400832 case Expression::Kind::kSwizzle:
Ethan Nicholas6b4d5812020-10-12 16:11:51 -0400833 clear_write(*expr.as<Swizzle>().base());
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400834 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400835 case Expression::Kind::kIndex:
Ethan Nicholas2a4952d2020-10-08 15:35:56 -0400836 clear_write(*expr.as<IndexExpression>().base());
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400837 break;
838 default:
839 ABORT("shouldn't be writing to this kind of expression\n");
840 break;
841 }
842}
843
Ethan Nicholascb670962017-04-20 19:31:52 -0400844void Compiler::simplifyExpression(DefinitionMap& definitions,
845 BasicBlock& b,
846 std::vector<BasicBlock::Node>::iterator* iter,
Ethan Nicholascdeae8c2020-10-22 14:39:46 -0400847 OptimizationContext* optimizationContext) {
Ethan Nicholascb670962017-04-20 19:31:52 -0400848 Expression* expr = (*iter)->expression()->get();
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400849 SkASSERT(expr);
John Stiles108bbe22020-11-18 11:10:38 -0500850
Ethan Nicholascb670962017-04-20 19:31:52 -0400851 if ((*iter)->fConstantPropagation) {
Ethan Nicholascdeae8c2020-10-22 14:39:46 -0400852 std::unique_ptr<Expression> optimized = expr->constantPropagate(*fIRGenerator,
853 definitions);
Ethan Nicholascb670962017-04-20 19:31:52 -0400854 if (optimized) {
Ethan Nicholascdeae8c2020-10-22 14:39:46 -0400855 optimizationContext->fUpdated = true;
Ethan Nicholas30d30222020-09-11 12:27:26 -0400856 optimized = fIRGenerator->coerce(std::move(optimized), expr->type());
Ethan Nicholas1d881522020-09-11 09:32:54 -0400857 SkASSERT(optimized);
Brian Osman010ce6a2020-10-19 16:34:10 -0400858 // Remove references within 'expr', add references within 'optimized'
859 optimizationContext->fUsage->replace(expr, optimized.get());
Ethan Nicholascb670962017-04-20 19:31:52 -0400860 if (!try_replace_expression(&b, iter, &optimized)) {
Ethan Nicholascdeae8c2020-10-22 14:39:46 -0400861 optimizationContext->fNeedsRescan = true;
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400862 return;
Ethan Nicholascb670962017-04-20 19:31:52 -0400863 }
John Stiles70025e52020-09-28 16:08:58 -0400864 SkASSERT((*iter)->isExpression());
Ethan Nicholascb670962017-04-20 19:31:52 -0400865 expr = (*iter)->expression()->get();
Ethan Nicholascb670962017-04-20 19:31:52 -0400866 }
867 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400868 switch (expr->kind()) {
869 case Expression::Kind::kVariableReference: {
John Stilesa5a97b42020-08-18 11:19:07 -0400870 const VariableReference& ref = expr->as<VariableReference>();
Ethan Nicholas78686922020-10-08 06:46:27 -0400871 const Variable* var = ref.variable();
Ethan Nicholas453f67f2020-10-09 10:43:45 -0400872 if (ref.refKind() != VariableReference::RefKind::kWrite &&
873 ref.refKind() != VariableReference::RefKind::kPointer &&
874 var->storage() == Variable::Storage::kLocal && !definitions[var] &&
Ethan Nicholascdeae8c2020-10-22 14:39:46 -0400875 optimizationContext->fSilences.find(var) == optimizationContext->fSilences.end()) {
876 optimizationContext->fSilences.insert(var);
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000877 this->error(expr->fOffset,
Ethan Nicholase2c49992020-10-05 11:49:11 -0400878 "'" + var->name() + "' has not been assigned");
Ethan Nicholascb670962017-04-20 19:31:52 -0400879 }
880 break;
881 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400882 case Expression::Kind::kTernary: {
John Stiles403a3632020-08-20 12:11:48 -0400883 TernaryExpression* t = &expr->as<TernaryExpression>();
Ethan Nicholasdd218162020-10-08 05:48:01 -0400884 if (t->test()->is<BoolLiteral>()) {
Ethan Nicholascb670962017-04-20 19:31:52 -0400885 // ternary has a constant test, replace it with either the true or
886 // false branch
Ethan Nicholasdd218162020-10-08 05:48:01 -0400887 if (t->test()->as<BoolLiteral>().value()) {
Brian Osman010ce6a2020-10-19 16:34:10 -0400888 (*iter)->setExpression(std::move(t->ifTrue()), optimizationContext->fUsage);
Ethan Nicholascb670962017-04-20 19:31:52 -0400889 } else {
Brian Osman010ce6a2020-10-19 16:34:10 -0400890 (*iter)->setExpression(std::move(t->ifFalse()), optimizationContext->fUsage);
Ethan Nicholascb670962017-04-20 19:31:52 -0400891 }
Ethan Nicholascdeae8c2020-10-22 14:39:46 -0400892 optimizationContext->fUpdated = true;
893 optimizationContext->fNeedsRescan = true;
Ethan Nicholascb670962017-04-20 19:31:52 -0400894 }
895 break;
896 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400897 case Expression::Kind::kBinary: {
John Stiles403a3632020-08-20 12:11:48 -0400898 BinaryExpression* bin = &expr->as<BinaryExpression>();
Brian Osman010ce6a2020-10-19 16:34:10 -0400899 if (dead_assignment(*bin, optimizationContext->fUsage)) {
Ethan Nicholascdeae8c2020-10-22 14:39:46 -0400900 delete_left(&b, iter, optimizationContext);
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400901 break;
902 }
John Stiles2d4f9592020-10-30 10:29:12 -0400903 Expression& left = *bin->left();
904 Expression& right = *bin->right();
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400905 const Type& leftType = left.type();
906 const Type& rightType = right.type();
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400907 // collapse useless expressions like x * 1 or x + 0
John Stiles9aeed132020-11-24 17:36:06 -0500908 if ((!leftType.isScalar() && !leftType.isVector()) ||
909 (!rightType.isScalar() && !rightType.isVector())) {
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400910 break;
911 }
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400912 switch (bin->getOperator()) {
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400913 case Token::Kind::TK_STAR:
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400914 if (is_constant(left, 1)) {
John Stiles9aeed132020-11-24 17:36:06 -0500915 if (leftType.isVector() && rightType.isScalar()) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400916 // float4(1) * x -> float4(x)
Ethan Nicholascdeae8c2020-10-22 14:39:46 -0400917 vectorize_right(&b, iter, optimizationContext);
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400918 } else {
919 // 1 * x -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400920 // 1 * float4(x) -> float4(x)
921 // float4(1) * float4(x) -> float4(x)
Ethan Nicholascdeae8c2020-10-22 14:39:46 -0400922 delete_left(&b, iter, optimizationContext);
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400923 }
924 }
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400925 else if (is_constant(left, 0)) {
John Stiles9aeed132020-11-24 17:36:06 -0500926 if (leftType.isScalar() && rightType.isVector() &&
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400927 !right.hasSideEffects()) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400928 // 0 * float4(x) -> float4(0)
Ethan Nicholascdeae8c2020-10-22 14:39:46 -0400929 vectorize_left(&b, iter, optimizationContext);
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400930 } else {
931 // 0 * x -> 0
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400932 // float4(0) * x -> float4(0)
933 // float4(0) * float4(x) -> float4(0)
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400934 if (!right.hasSideEffects()) {
Ethan Nicholascdeae8c2020-10-22 14:39:46 -0400935 delete_right(&b, iter, optimizationContext);
Ethan Nicholas51493ee2017-12-11 12:34:33 -0500936 }
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400937 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400938 }
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400939 else if (is_constant(right, 1)) {
John Stiles9aeed132020-11-24 17:36:06 -0500940 if (leftType.isScalar() && rightType.isVector()) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400941 // x * float4(1) -> float4(x)
Ethan Nicholascdeae8c2020-10-22 14:39:46 -0400942 vectorize_left(&b, iter, optimizationContext);
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400943 } else {
944 // x * 1 -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400945 // float4(x) * 1 -> float4(x)
946 // float4(x) * float4(1) -> float4(x)
Ethan Nicholascdeae8c2020-10-22 14:39:46 -0400947 delete_right(&b, iter, optimizationContext);
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400948 }
949 }
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400950 else if (is_constant(right, 0)) {
John Stiles9aeed132020-11-24 17:36:06 -0500951 if (leftType.isVector() && rightType.isScalar() && !left.hasSideEffects()) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400952 // float4(x) * 0 -> float4(0)
Ethan Nicholascdeae8c2020-10-22 14:39:46 -0400953 vectorize_right(&b, iter, optimizationContext);
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400954 } else {
955 // x * 0 -> 0
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400956 // x * float4(0) -> float4(0)
957 // float4(x) * float4(0) -> float4(0)
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400958 if (!left.hasSideEffects()) {
Ethan Nicholascdeae8c2020-10-22 14:39:46 -0400959 delete_left(&b, iter, optimizationContext);
Ethan Nicholas51493ee2017-12-11 12:34:33 -0500960 }
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400961 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400962 }
963 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400964 case Token::Kind::TK_PLUS:
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400965 if (is_constant(left, 0)) {
John Stiles9aeed132020-11-24 17:36:06 -0500966 if (leftType.isVector() && rightType.isScalar()) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400967 // float4(0) + x -> float4(x)
Ethan Nicholascdeae8c2020-10-22 14:39:46 -0400968 vectorize_right(&b, iter, optimizationContext);
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400969 } else {
970 // 0 + x -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400971 // 0 + float4(x) -> float4(x)
972 // float4(0) + float4(x) -> float4(x)
Ethan Nicholascdeae8c2020-10-22 14:39:46 -0400973 delete_left(&b, iter, optimizationContext);
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400974 }
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400975 } else if (is_constant(right, 0)) {
John Stiles9aeed132020-11-24 17:36:06 -0500976 if (leftType.isScalar() && rightType.isVector()) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400977 // x + float4(0) -> float4(x)
Ethan Nicholascdeae8c2020-10-22 14:39:46 -0400978 vectorize_left(&b, iter, optimizationContext);
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400979 } else {
980 // x + 0 -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400981 // float4(x) + 0 -> float4(x)
982 // float4(x) + float4(0) -> float4(x)
Ethan Nicholascdeae8c2020-10-22 14:39:46 -0400983 delete_right(&b, iter, optimizationContext);
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400984 }
Ethan Nicholas56e42712017-04-21 10:23:37 -0400985 }
986 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400987 case Token::Kind::TK_MINUS:
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400988 if (is_constant(right, 0)) {
John Stiles9aeed132020-11-24 17:36:06 -0500989 if (leftType.isScalar() && rightType.isVector()) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400990 // x - float4(0) -> float4(x)
Ethan Nicholascdeae8c2020-10-22 14:39:46 -0400991 vectorize_left(&b, iter, optimizationContext);
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400992 } else {
993 // x - 0 -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400994 // float4(x) - 0 -> float4(x)
995 // float4(x) - float4(0) -> float4(x)
Ethan Nicholascdeae8c2020-10-22 14:39:46 -0400996 delete_right(&b, iter, optimizationContext);
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400997 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400998 }
999 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001000 case Token::Kind::TK_SLASH:
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -04001001 if (is_constant(right, 1)) {
John Stiles9aeed132020-11-24 17:36:06 -05001002 if (leftType.isScalar() && rightType.isVector()) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001003 // x / float4(1) -> float4(x)
Ethan Nicholascdeae8c2020-10-22 14:39:46 -04001004 vectorize_left(&b, iter, optimizationContext);
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001005 } else {
1006 // x / 1 -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001007 // float4(x) / 1 -> float4(x)
1008 // float4(x) / float4(1) -> float4(x)
Ethan Nicholascdeae8c2020-10-22 14:39:46 -04001009 delete_right(&b, iter, optimizationContext);
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001010 }
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -04001011 } else if (is_constant(left, 0)) {
John Stiles9aeed132020-11-24 17:36:06 -05001012 if (leftType.isScalar() && rightType.isVector() &&
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -04001013 !right.hasSideEffects()) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001014 // 0 / float4(x) -> float4(0)
Ethan Nicholascdeae8c2020-10-22 14:39:46 -04001015 vectorize_left(&b, iter, optimizationContext);
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001016 } else {
1017 // 0 / x -> 0
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001018 // float4(0) / x -> float4(0)
1019 // float4(0) / float4(x) -> float4(0)
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -04001020 if (!right.hasSideEffects()) {
Ethan Nicholascdeae8c2020-10-22 14:39:46 -04001021 delete_right(&b, iter, optimizationContext);
Ethan Nicholas51493ee2017-12-11 12:34:33 -05001022 }
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001023 }
1024 }
1025 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001026 case Token::Kind::TK_PLUSEQ:
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -04001027 if (is_constant(right, 0)) {
1028 clear_write(left);
Ethan Nicholascdeae8c2020-10-22 14:39:46 -04001029 delete_right(&b, iter, optimizationContext);
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001030 }
1031 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001032 case Token::Kind::TK_MINUSEQ:
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -04001033 if (is_constant(right, 0)) {
1034 clear_write(left);
Ethan Nicholascdeae8c2020-10-22 14:39:46 -04001035 delete_right(&b, iter, optimizationContext);
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001036 }
1037 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001038 case Token::Kind::TK_STAREQ:
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -04001039 if (is_constant(right, 1)) {
1040 clear_write(left);
Ethan Nicholascdeae8c2020-10-22 14:39:46 -04001041 delete_right(&b, iter, optimizationContext);
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001042 }
1043 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001044 case Token::Kind::TK_SLASHEQ:
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -04001045 if (is_constant(right, 1)) {
1046 clear_write(left);
Ethan Nicholascdeae8c2020-10-22 14:39:46 -04001047 delete_right(&b, iter, optimizationContext);
Ethan Nicholascb670962017-04-20 19:31:52 -04001048 }
1049 break;
1050 default:
1051 break;
1052 }
Ethan Nicholas409f6f02019-09-17 12:34:39 -04001053 break;
1054 }
John Stilesf5c1d042020-11-21 23:26:07 -05001055 case Expression::Kind::kConstructor: {
1056 // Find constructors embedded inside constructors and flatten them out where possible.
1057 // - float4(float2(1, 2), 3, 4) --> float4(1, 2, 3, 4)
1058 // - float4(w, float3(sin(x), cos(y), tan(z))) --> float4(w, sin(x), cos(y), tan(z))
1059 // Leave single-argument constructors alone, though. These might be casts or splats.
1060 Constructor& c = expr->as<Constructor>();
1061 if (c.type().columns() > 1) {
1062 // Inspect each constructor argument to see if it's a candidate for flattening.
1063 // Remember matched arguments in a bitfield, "argsToOptimize".
1064 int argsToOptimize = 0;
1065 int currBit = 1;
1066 for (const std::unique_ptr<Expression>& arg : c.arguments()) {
1067 if (arg->is<Constructor>()) {
1068 Constructor& inner = arg->as<Constructor>();
1069 if (inner.arguments().size() > 1 &&
1070 inner.type().componentType() == c.type().componentType()) {
1071 argsToOptimize |= currBit;
1072 }
1073 }
1074 currBit <<= 1;
1075 }
1076 if (argsToOptimize) {
1077 // We found at least one argument that could be flattened out. Re-walk the
1078 // constructor args and flatten the candidates we found during our initial pass.
1079 ExpressionArray flattened;
1080 flattened.reserve_back(c.type().columns());
1081 currBit = 1;
1082 for (const std::unique_ptr<Expression>& arg : c.arguments()) {
1083 if (argsToOptimize & currBit) {
1084 Constructor& inner = arg->as<Constructor>();
1085 for (const std::unique_ptr<Expression>& innerArg : inner.arguments()) {
1086 flattened.push_back(innerArg->clone());
1087 }
1088 } else {
1089 flattened.push_back(arg->clone());
1090 }
1091 currBit <<= 1;
1092 }
1093 auto optimized = std::unique_ptr<Expression>(
1094 new Constructor(c.fOffset, &c.type(), std::move(flattened)));
1095 // No fUsage change; no references have been added or removed anywhere.
1096 optimizationContext->fUpdated = true;
1097 if (!try_replace_expression(&b, iter, &optimized)) {
1098 optimizationContext->fNeedsRescan = true;
1099 return;
1100 }
1101 SkASSERT((*iter)->isExpression());
1102 break;
1103 }
1104 }
1105 break;
1106 }
Ethan Nicholase6592142020-09-08 10:22:09 -04001107 case Expression::Kind::kSwizzle: {
John Stiles403a3632020-08-20 12:11:48 -04001108 Swizzle& s = expr->as<Swizzle>();
John Stiles108bbe22020-11-18 11:10:38 -05001109 // Detect identity swizzles like `foo.rgba`.
Ethan Nicholas6b4d5812020-10-12 16:11:51 -04001110 if ((int) s.components().size() == s.base()->type().columns()) {
Ethan Nicholas409f6f02019-09-17 12:34:39 -04001111 bool identity = true;
Ethan Nicholas6b4d5812020-10-12 16:11:51 -04001112 for (int i = 0; i < (int) s.components().size(); ++i) {
1113 if (s.components()[i] != i) {
Ethan Nicholas409f6f02019-09-17 12:34:39 -04001114 identity = false;
1115 break;
1116 }
1117 }
1118 if (identity) {
Ethan Nicholascdeae8c2020-10-22 14:39:46 -04001119 optimizationContext->fUpdated = true;
Brian Osman010ce6a2020-10-19 16:34:10 -04001120 // No fUsage change: foo.rgba and foo have equivalent reference counts
Ethan Nicholas6b4d5812020-10-12 16:11:51 -04001121 if (!try_replace_expression(&b, iter, &s.base())) {
Ethan Nicholascdeae8c2020-10-22 14:39:46 -04001122 optimizationContext->fNeedsRescan = true;
Ethan Nicholas409f6f02019-09-17 12:34:39 -04001123 return;
1124 }
John Stiles70025e52020-09-28 16:08:58 -04001125 SkASSERT((*iter)->isExpression());
Ethan Nicholas409f6f02019-09-17 12:34:39 -04001126 break;
1127 }
1128 }
John Stiles108bbe22020-11-18 11:10:38 -05001129 // Detect swizzles of swizzles, e.g. replace `foo.argb.r000` with `foo.a000`.
1130 if (s.base()->is<Swizzle>()) {
Ethan Nicholas6b4d5812020-10-12 16:11:51 -04001131 Swizzle& base = s.base()->as<Swizzle>();
John Stiles750109b2020-10-30 13:45:46 -04001132 ComponentArray final;
Ethan Nicholas6b4d5812020-10-12 16:11:51 -04001133 for (int c : s.components()) {
1134 final.push_back(base.components()[c]);
Ethan Nicholas409f6f02019-09-17 12:34:39 -04001135 }
Ethan Nicholas6b4d5812020-10-12 16:11:51 -04001136 std::unique_ptr<Expression> replacement(new Swizzle(*fContext, base.base()->clone(),
John Stiles750109b2020-10-30 13:45:46 -04001137 final));
John Stilesd2f51b12021-01-07 18:12:31 -05001138 // We're replacing an expression with a cloned version; we'll need a rescan.
John Stiles108bbe22020-11-18 11:10:38 -05001139 // No fUsage change: `foo.gbr.gbr` and `foo.brg` have equivalent reference counts
John Stilesd2f51b12021-01-07 18:12:31 -05001140 try_replace_expression(&b, iter, &replacement);
1141 optimizationContext->fUpdated = true;
1142 optimizationContext->fNeedsRescan = true;
John Stiles108bbe22020-11-18 11:10:38 -05001143 break;
1144 }
1145 // Optimize swizzles of constructors.
1146 if (s.base()->is<Constructor>()) {
1147 Constructor& base = s.base()->as<Constructor>();
1148 std::unique_ptr<Expression> replacement;
1149 const Type& componentType = base.type().componentType();
1150 int swizzleSize = s.components().size();
1151
1152 // The IR generator has already converted any zero/one swizzle components into
1153 // constructors containing zero/one args. Confirm that this is true by checking that
1154 // our swizzle components are all `xyzw` (values 0 through 3).
1155 SkASSERT(std::all_of(s.components().begin(), s.components().end(),
1156 [](int8_t c) { return c >= 0 && c <= 3; }));
1157
John Stiles9aeed132020-11-24 17:36:06 -05001158 if (base.arguments().size() == 1 && base.arguments().front()->type().isScalar()) {
John Stiles108bbe22020-11-18 11:10:38 -05001159 // `half4(scalar).zyy` can be optimized to `half3(scalar)`. The swizzle
1160 // components don't actually matter since all fields are the same.
1161 ExpressionArray newArgs;
1162 newArgs.push_back(base.arguments().front()->clone());
1163 replacement = std::make_unique<Constructor>(
1164 base.fOffset,
1165 &componentType.toCompound(*fContext, swizzleSize, /*rows=*/1),
1166 std::move(newArgs));
1167
John Stilesa60ac0c2020-12-22 08:59:51 -05001168 // We're replacing an expression with a cloned version; we'll need a rescan.
1169 // There's no fUsage change: `half4(foo).xy` and `half2(foo)` have equivalent
1170 // reference counts.
1171 try_replace_expression(&b, iter, &replacement);
John Stiles0777ac42020-11-19 11:06:47 -05001172 optimizationContext->fUpdated = true;
John Stilesa60ac0c2020-12-22 08:59:51 -05001173 optimizationContext->fNeedsRescan = true;
John Stiles108bbe22020-11-18 11:10:38 -05001174 break;
1175 }
1176
John Stiles0777ac42020-11-19 11:06:47 -05001177 // Swizzles can duplicate some elements and discard others, e.g.
1178 // `half4(1, 2, 3, 4).xxz` --> `half3(1, 1, 3)`. However, there are constraints:
1179 // - Expressions with side effects need to occur exactly once, even if they
1180 // would otherwise be swizzle-eliminated
1181 // - Non-trivial expressions should not be repeated, but elimination is OK.
1182 //
1183 // Look up the argument for the constructor at each index. This is typically simple
1184 // but for weird cases like `half4(bar.yz, half2(foo))`, it can be harder than it
1185 // seems. This example would result in:
1186 // argMap[0] = {.fArgIndex = 0, .fComponent = 0} (bar.yz .x)
1187 // argMap[1] = {.fArgIndex = 0, .fComponent = 1} (bar.yz .y)
1188 // argMap[2] = {.fArgIndex = 1, .fComponent = 0} (half2(foo) .x)
1189 // argMap[3] = {.fArgIndex = 1, .fComponent = 1} (half2(foo) .y)
1190 struct ConstructorArgMap {
1191 int8_t fArgIndex;
1192 int8_t fComponent;
1193 };
1194
1195 int numConstructorArgs = base.type().columns();
1196 ConstructorArgMap argMap[4] = {};
1197 int writeIdx = 0;
1198 for (int argIdx = 0; argIdx < (int) base.arguments().size(); ++argIdx) {
1199 const Expression& expr = *base.arguments()[argIdx];
1200 int argWidth = expr.type().columns();
1201 for (int componentIdx = 0; componentIdx < argWidth; ++componentIdx) {
1202 argMap[writeIdx].fArgIndex = argIdx;
1203 argMap[writeIdx].fComponent = componentIdx;
1204 ++writeIdx;
1205 }
1206 }
1207 SkASSERT(writeIdx == numConstructorArgs);
1208
1209 // Count up the number of times each constructor argument is used by the
1210 // swizzle.
1211 // `half4(bar.yz, half2(foo)).xwxy` -> { 3, 1 }
1212 // - bar.yz is referenced 3 times, by `.x_xy`
1213 // - half(foo) is referenced 1 time, by `._w__`
1214 int8_t exprUsed[4] = {};
1215 for (int c : s.components()) {
1216 exprUsed[argMap[c].fArgIndex]++;
1217 }
1218
1219 bool safeToOptimize = true;
1220 for (int index = 0; index < numConstructorArgs; ++index) {
1221 int8_t constructorArgIndex = argMap[index].fArgIndex;
1222 const Expression& baseArg = *base.arguments()[constructorArgIndex];
1223
1224 // Check that non-trivial expressions are not swizzled in more than once.
John Stilesc30fbca2020-11-19 16:25:49 -05001225 if (exprUsed[constructorArgIndex] > 1 &&
1226 !Analysis::IsTrivialExpression(baseArg)) {
John Stiles0777ac42020-11-19 11:06:47 -05001227 safeToOptimize = false;
1228 break;
1229 }
1230 // Check that side-effect-bearing expressions are swizzled in exactly once.
1231 if (exprUsed[constructorArgIndex] != 1 && baseArg.hasSideEffects()) {
1232 safeToOptimize = false;
1233 break;
1234 }
1235 }
1236
1237 if (safeToOptimize) {
John Stilesd9076cb2020-11-19 12:18:36 -05001238 struct ReorderedArgument {
1239 int8_t fArgIndex;
1240 ComponentArray fComponents;
1241 };
1242 SkSTArray<4, ReorderedArgument> reorderedArgs;
John Stiles0777ac42020-11-19 11:06:47 -05001243 for (int c : s.components()) {
1244 const ConstructorArgMap& argument = argMap[c];
1245 const Expression& baseArg = *base.arguments()[argument.fArgIndex];
1246
John Stiles9aeed132020-11-24 17:36:06 -05001247 if (baseArg.type().isScalar()) {
John Stilesd9076cb2020-11-19 12:18:36 -05001248 // This argument is a scalar; add it to the list as-is.
John Stiles0777ac42020-11-19 11:06:47 -05001249 SkASSERT(argument.fComponent == 0);
John Stilesd9076cb2020-11-19 12:18:36 -05001250 reorderedArgs.push_back({argument.fArgIndex,
1251 ComponentArray{}});
John Stiles0777ac42020-11-19 11:06:47 -05001252 } else {
John Stilesd9076cb2020-11-19 12:18:36 -05001253 // This argument is a component from a vector.
John Stiles0777ac42020-11-19 11:06:47 -05001254 SkASSERT(argument.fComponent < baseArg.type().columns());
John Stilesd9076cb2020-11-19 12:18:36 -05001255 if (reorderedArgs.empty() ||
1256 reorderedArgs.back().fArgIndex != argument.fArgIndex) {
1257 // This can't be combined with the previous argument. Add a new one.
1258 reorderedArgs.push_back({argument.fArgIndex,
1259 ComponentArray{argument.fComponent}});
1260 } else {
1261 // Since we know this argument uses components, it should already
1262 // have at least one component set.
1263 SkASSERT(!reorderedArgs.back().fComponents.empty());
1264 // Build up the current argument with one more component.
1265 reorderedArgs.back().fComponents.push_back(argument.fComponent);
1266 }
John Stiles0777ac42020-11-19 11:06:47 -05001267 }
1268 }
John Stilesd9076cb2020-11-19 12:18:36 -05001269
1270 // Convert our reordered argument list to an actual array of expressions, with
1271 // the new order and any new inner swizzles that need to be applied. Note that
1272 // we expect followup passes to clean up the inner swizzles.
1273 ExpressionArray newArgs;
1274 newArgs.reserve_back(swizzleSize);
1275 for (const ReorderedArgument& reorderedArg : reorderedArgs) {
1276 const Expression& baseArg = *base.arguments()[reorderedArg.fArgIndex];
1277 if (reorderedArg.fComponents.empty()) {
1278 newArgs.push_back(baseArg.clone());
1279 } else {
1280 newArgs.push_back(std::make_unique<Swizzle>(*fContext, baseArg.clone(),
1281 reorderedArg.fComponents));
1282 }
1283 }
1284
1285 // Create a new constructor.
John Stiles0777ac42020-11-19 11:06:47 -05001286 replacement = std::make_unique<Constructor>(
1287 base.fOffset,
1288 &componentType.toCompound(*fContext, swizzleSize, /*rows=*/1),
1289 std::move(newArgs));
1290
John Stilesa60ac0c2020-12-22 08:59:51 -05001291 // Remove references within 'expr', add references within 'replacement.'
John Stiles0777ac42020-11-19 11:06:47 -05001292 optimizationContext->fUsage->replace(expr, replacement.get());
John Stilesa60ac0c2020-12-22 08:59:51 -05001293
1294 // We're replacing an expression with a cloned version; we'll need a rescan.
1295 try_replace_expression(&b, iter, &replacement);
1296 optimizationContext->fUpdated = true;
1297 optimizationContext->fNeedsRescan = true;
John Stiles0777ac42020-11-19 11:06:47 -05001298 }
John Stiles108bbe22020-11-18 11:10:38 -05001299 break;
Ethan Nicholas409f6f02019-09-17 12:34:39 -04001300 }
John Stiles30212b72020-06-11 17:55:07 -04001301 break;
Ethan Nicholascb670962017-04-20 19:31:52 -04001302 }
1303 default:
1304 break;
1305 }
1306}
1307
John Stiles92219b42020-06-15 12:32:24 -04001308// Returns true if this statement could potentially execute a break at the current level. We ignore
1309// nested loops and switches, since any breaks inside of them will merely break the loop / switch.
John Stilesb92641c2020-08-31 18:09:01 -04001310static bool contains_conditional_break(Statement& stmt) {
1311 class ContainsConditionalBreak : public ProgramVisitor {
1312 public:
1313 bool visitStatement(const Statement& stmt) override {
Ethan Nicholase6592142020-09-08 10:22:09 -04001314 switch (stmt.kind()) {
1315 case Statement::Kind::kBlock:
John Stilesb92641c2020-08-31 18:09:01 -04001316 return this->INHERITED::visitStatement(stmt);
1317
Ethan Nicholase6592142020-09-08 10:22:09 -04001318 case Statement::Kind::kBreak:
John Stilesb92641c2020-08-31 18:09:01 -04001319 return fInConditional > 0;
1320
Ethan Nicholase6592142020-09-08 10:22:09 -04001321 case Statement::Kind::kIf: {
John Stilesb92641c2020-08-31 18:09:01 -04001322 ++fInConditional;
1323 bool result = this->INHERITED::visitStatement(stmt);
1324 --fInConditional;
1325 return result;
1326 }
1327
1328 default:
1329 return false;
1330 }
1331 }
1332
1333 int fInConditional = 0;
1334 using INHERITED = ProgramVisitor;
1335 };
1336
1337 return ContainsConditionalBreak{}.visitStatement(stmt);
John Stiles92219b42020-06-15 12:32:24 -04001338}
1339
Ethan Nicholas5005a222018-08-24 13:06:27 -04001340// returns true if this statement definitely executes a break at the current level (we ignore
1341// nested loops and switches, since any breaks inside of them will merely break the loop / switch)
John Stilesb92641c2020-08-31 18:09:01 -04001342static bool contains_unconditional_break(Statement& stmt) {
1343 class ContainsUnconditionalBreak : public ProgramVisitor {
1344 public:
1345 bool visitStatement(const Statement& stmt) override {
Ethan Nicholase6592142020-09-08 10:22:09 -04001346 switch (stmt.kind()) {
1347 case Statement::Kind::kBlock:
John Stilesb92641c2020-08-31 18:09:01 -04001348 return this->INHERITED::visitStatement(stmt);
1349
Ethan Nicholase6592142020-09-08 10:22:09 -04001350 case Statement::Kind::kBreak:
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001351 return true;
John Stilesb92641c2020-08-31 18:09:01 -04001352
1353 default:
1354 return false;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001355 }
John Stilesb92641c2020-08-31 18:09:01 -04001356 }
John Stiles92219b42020-06-15 12:32:24 -04001357
John Stilesb92641c2020-08-31 18:09:01 -04001358 using INHERITED = ProgramVisitor;
1359 };
John Stiles92219b42020-06-15 12:32:24 -04001360
John Stilesb92641c2020-08-31 18:09:01 -04001361 return ContainsUnconditionalBreak{}.visitStatement(stmt);
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001362}
1363
John Stiles8f2a0cf2020-10-13 12:48:21 -04001364static void move_all_but_break(std::unique_ptr<Statement>& stmt, StatementArray* target) {
Ethan Nicholase6592142020-09-08 10:22:09 -04001365 switch (stmt->kind()) {
1366 case Statement::Kind::kBlock: {
John Stiles92219b42020-06-15 12:32:24 -04001367 // Recurse into the block.
1368 Block& block = static_cast<Block&>(*stmt);
1369
John Stiles8f2a0cf2020-10-13 12:48:21 -04001370 StatementArray blockStmts;
John Stilesf4bda742020-10-14 16:57:41 -04001371 blockStmts.reserve_back(block.children().size());
Ethan Nicholas7bd60432020-09-25 14:31:59 -04001372 for (std::unique_ptr<Statement>& stmt : block.children()) {
1373 move_all_but_break(stmt, &blockStmts);
Ethan Nicholas739e1ca2020-06-11 12:16:14 -04001374 }
John Stiles92219b42020-06-15 12:32:24 -04001375
1376 target->push_back(std::make_unique<Block>(block.fOffset, std::move(blockStmts),
Ethan Nicholas7bd60432020-09-25 14:31:59 -04001377 block.symbolTable(), block.isScope()));
Ethan Nicholas739e1ca2020-06-11 12:16:14 -04001378 break;
John Stiles92219b42020-06-15 12:32:24 -04001379 }
1380
Ethan Nicholase6592142020-09-08 10:22:09 -04001381 case Statement::Kind::kBreak:
John Stiles92219b42020-06-15 12:32:24 -04001382 // Do not append a break to the target.
1383 break;
1384
Ethan Nicholas739e1ca2020-06-11 12:16:14 -04001385 default:
John Stiles92219b42020-06-15 12:32:24 -04001386 // Append normal statements to the target.
1387 target->push_back(std::move(stmt));
1388 break;
Ethan Nicholas739e1ca2020-06-11 12:16:14 -04001389 }
1390}
1391
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001392// Returns a block containing all of the statements that will be run if the given case matches
1393// (which, owing to the statements being owned by unique_ptrs, means the switch itself will be
1394// broken by this call and must then be discarded).
1395// Returns null (and leaves the switch unmodified) if no such simple reduction is possible, such as
1396// when break statements appear inside conditionals.
John Stiles92219b42020-06-15 12:32:24 -04001397static std::unique_ptr<Statement> block_for_case(SwitchStatement* switchStatement,
1398 SwitchCase* caseToCapture) {
1399 // We have to be careful to not move any of the pointers until after we're sure we're going to
1400 // succeed, so before we make any changes at all, we check the switch-cases to decide on a plan
1401 // of action. First, find the switch-case we are interested in.
Ethan Nicholas01b05e52020-10-22 15:53:41 -04001402 auto iter = switchStatement->cases().begin();
1403 for (; iter != switchStatement->cases().end(); ++iter) {
John Stiles2d4f9592020-10-30 10:29:12 -04001404 if (iter->get() == caseToCapture) {
John Stiles92219b42020-06-15 12:32:24 -04001405 break;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001406 }
John Stiles92219b42020-06-15 12:32:24 -04001407 }
1408
1409 // Next, walk forward through the rest of the switch. If we find a conditional break, we're
1410 // stuck and can't simplify at all. If we find an unconditional break, we have a range of
1411 // statements that we can use for simplification.
1412 auto startIter = iter;
1413 Statement* unconditionalBreakStmt = nullptr;
Ethan Nicholas01b05e52020-10-22 15:53:41 -04001414 for (; iter != switchStatement->cases().end(); ++iter) {
John Stiles2d4f9592020-10-30 10:29:12 -04001415 for (std::unique_ptr<Statement>& stmt : (*iter)->statements()) {
John Stiles92219b42020-06-15 12:32:24 -04001416 if (contains_conditional_break(*stmt)) {
1417 // We can't reduce switch-cases to a block when they have conditional breaks.
1418 return nullptr;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001419 }
John Stiles92219b42020-06-15 12:32:24 -04001420
1421 if (contains_unconditional_break(*stmt)) {
1422 // We found an unconditional break. We can use this block, but we need to strip
1423 // out the break statement.
1424 unconditionalBreakStmt = stmt.get();
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001425 break;
1426 }
1427 }
John Stiles92219b42020-06-15 12:32:24 -04001428
1429 if (unconditionalBreakStmt != nullptr) {
1430 break;
1431 }
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001432 }
John Stiles92219b42020-06-15 12:32:24 -04001433
1434 // We fell off the bottom of the switch or encountered a break. We know the range of statements
1435 // that we need to move over, and we know it's safe to do so.
John Stiles8f2a0cf2020-10-13 12:48:21 -04001436 StatementArray caseStmts;
John Stiles92219b42020-06-15 12:32:24 -04001437
1438 // We can move over most of the statements as-is.
1439 while (startIter != iter) {
John Stiles2d4f9592020-10-30 10:29:12 -04001440 for (std::unique_ptr<Statement>& stmt : (*startIter)->statements()) {
John Stiles92219b42020-06-15 12:32:24 -04001441 caseStmts.push_back(std::move(stmt));
1442 }
1443 ++startIter;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001444 }
John Stiles92219b42020-06-15 12:32:24 -04001445
1446 // If we found an unconditional break at the end, we need to move what we can while avoiding
1447 // that break.
1448 if (unconditionalBreakStmt != nullptr) {
John Stiles2d4f9592020-10-30 10:29:12 -04001449 for (std::unique_ptr<Statement>& stmt : (*startIter)->statements()) {
John Stiles92219b42020-06-15 12:32:24 -04001450 if (stmt.get() == unconditionalBreakStmt) {
1451 move_all_but_break(stmt, &caseStmts);
1452 unconditionalBreakStmt = nullptr;
1453 break;
1454 }
1455
1456 caseStmts.push_back(std::move(stmt));
1457 }
1458 }
1459
1460 SkASSERT(unconditionalBreakStmt == nullptr); // Verify that we fixed the unconditional break.
1461
1462 // Return our newly-synthesized block.
Ethan Nicholas01b05e52020-10-22 15:53:41 -04001463 return std::make_unique<Block>(/*offset=*/-1, std::move(caseStmts), switchStatement->symbols());
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001464}
1465
Ethan Nicholascb670962017-04-20 19:31:52 -04001466void Compiler::simplifyStatement(DefinitionMap& definitions,
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001467 BasicBlock& b,
1468 std::vector<BasicBlock::Node>::iterator* iter,
Ethan Nicholascdeae8c2020-10-22 14:39:46 -04001469 OptimizationContext* optimizationContext) {
Brian Osman010ce6a2020-10-19 16:34:10 -04001470 ProgramUsage* usage = optimizationContext->fUsage;
Ethan Nicholascb670962017-04-20 19:31:52 -04001471 Statement* stmt = (*iter)->statement()->get();
Ethan Nicholase6592142020-09-08 10:22:09 -04001472 switch (stmt->kind()) {
1473 case Statement::Kind::kVarDeclaration: {
John Stilesa5a97b42020-08-18 11:19:07 -04001474 const auto& varDecl = stmt->as<VarDeclaration>();
Brian Osman010ce6a2020-10-19 16:34:10 -04001475 if (usage->isDead(varDecl.var()) &&
Ethan Nicholasc51f33e2020-10-13 13:49:44 -04001476 (!varDecl.value() ||
1477 !varDecl.value()->hasSideEffects())) {
1478 if (varDecl.value()) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001479 SkASSERT((*iter)->statement()->get() == stmt);
Ethan Nicholasc51f33e2020-10-13 13:49:44 -04001480 if (!b.tryRemoveExpressionBefore(iter, varDecl.value().get())) {
Ethan Nicholascdeae8c2020-10-22 14:39:46 -04001481 optimizationContext->fNeedsRescan = true;
Ethan Nicholascb670962017-04-20 19:31:52 -04001482 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001483 }
Brian Osman010ce6a2020-10-19 16:34:10 -04001484 (*iter)->setStatement(std::make_unique<Nop>(), usage);
Ethan Nicholascdeae8c2020-10-22 14:39:46 -04001485 optimizationContext->fUpdated = true;
Ethan Nicholascb670962017-04-20 19:31:52 -04001486 }
1487 break;
1488 }
Ethan Nicholase6592142020-09-08 10:22:09 -04001489 case Statement::Kind::kIf: {
John Stilesa5a97b42020-08-18 11:19:07 -04001490 IfStatement& i = stmt->as<IfStatement>();
Ethan Nicholas8c44eca2020-10-07 16:47:09 -04001491 if (i.test()->kind() == Expression::Kind::kBoolLiteral) {
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001492 // constant if, collapse down to a single branch
Ethan Nicholas8c44eca2020-10-07 16:47:09 -04001493 if (i.test()->as<BoolLiteral>().value()) {
1494 SkASSERT(i.ifTrue());
Brian Osman010ce6a2020-10-19 16:34:10 -04001495 (*iter)->setStatement(std::move(i.ifTrue()), usage);
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001496 } else {
Ethan Nicholas8c44eca2020-10-07 16:47:09 -04001497 if (i.ifFalse()) {
Brian Osman010ce6a2020-10-19 16:34:10 -04001498 (*iter)->setStatement(std::move(i.ifFalse()), usage);
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001499 } else {
Brian Osman010ce6a2020-10-19 16:34:10 -04001500 (*iter)->setStatement(std::make_unique<Nop>(), usage);
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001501 }
1502 }
Ethan Nicholascdeae8c2020-10-22 14:39:46 -04001503 optimizationContext->fUpdated = true;
1504 optimizationContext->fNeedsRescan = true;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001505 break;
1506 }
Ethan Nicholas8c44eca2020-10-07 16:47:09 -04001507 if (i.ifFalse() && i.ifFalse()->isEmpty()) {
Ethan Nicholascb670962017-04-20 19:31:52 -04001508 // else block doesn't do anything, remove it
Ethan Nicholas8c44eca2020-10-07 16:47:09 -04001509 i.ifFalse().reset();
Ethan Nicholascdeae8c2020-10-22 14:39:46 -04001510 optimizationContext->fUpdated = true;
1511 optimizationContext->fNeedsRescan = true;
Ethan Nicholascb670962017-04-20 19:31:52 -04001512 }
Ethan Nicholas8c44eca2020-10-07 16:47:09 -04001513 if (!i.ifFalse() && i.ifTrue()->isEmpty()) {
Ethan Nicholascb670962017-04-20 19:31:52 -04001514 // if block doesn't do anything, no else block
Ethan Nicholas8c44eca2020-10-07 16:47:09 -04001515 if (i.test()->hasSideEffects()) {
Ethan Nicholascb670962017-04-20 19:31:52 -04001516 // test has side effects, keep it
Brian Osman010ce6a2020-10-19 16:34:10 -04001517 (*iter)->setStatement(
1518 std::make_unique<ExpressionStatement>(std::move(i.test())), usage);
Ethan Nicholascb670962017-04-20 19:31:52 -04001519 } else {
1520 // no if, no else, no test side effects, kill the whole if
1521 // statement
Brian Osman010ce6a2020-10-19 16:34:10 -04001522 (*iter)->setStatement(std::make_unique<Nop>(), usage);
Ethan Nicholascb670962017-04-20 19:31:52 -04001523 }
Ethan Nicholascdeae8c2020-10-22 14:39:46 -04001524 optimizationContext->fUpdated = true;
1525 optimizationContext->fNeedsRescan = true;
Ethan Nicholascb670962017-04-20 19:31:52 -04001526 }
1527 break;
1528 }
Ethan Nicholase6592142020-09-08 10:22:09 -04001529 case Statement::Kind::kSwitch: {
John Stilesa5a97b42020-08-18 11:19:07 -04001530 SwitchStatement& s = stmt->as<SwitchStatement>();
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001531 int64_t switchValue;
Ethan Nicholas01b05e52020-10-22 15:53:41 -04001532 if (fIRGenerator->getConstantInt(*s.value(), &switchValue)) {
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001533 // switch is constant, replace it with the case that matches
1534 bool found = false;
1535 SwitchCase* defaultCase = nullptr;
John Stiles2d4f9592020-10-30 10:29:12 -04001536 for (const std::unique_ptr<SwitchCase>& c : s.cases()) {
1537 if (!c->value()) {
1538 defaultCase = c.get();
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001539 continue;
1540 }
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001541 int64_t caseValue;
John Stiles2d4f9592020-10-30 10:29:12 -04001542 SkAssertResult(fIRGenerator->getConstantInt(*c->value(), &caseValue));
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001543 if (caseValue == switchValue) {
John Stiles2d4f9592020-10-30 10:29:12 -04001544 std::unique_ptr<Statement> newBlock = block_for_case(&s, c.get());
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001545 if (newBlock) {
Brian Osman010ce6a2020-10-19 16:34:10 -04001546 (*iter)->setStatement(std::move(newBlock), usage);
John Stiles9d944232020-08-19 09:56:49 -04001547 found = true;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001548 break;
1549 } else {
Ethan Nicholas01b05e52020-10-22 15:53:41 -04001550 if (s.isStatic() && !(fFlags & kPermitInvalidStaticTests_Flag) &&
1551 optimizationContext->fSilences.find(&s) ==
1552 optimizationContext->fSilences.end()) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001553 this->error(s.fOffset,
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001554 "static switch contains non-static conditional break");
Ethan Nicholas01b05e52020-10-22 15:53:41 -04001555 optimizationContext->fSilences.insert(&s);
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001556 }
1557 return; // can't simplify
1558 }
1559 }
1560 }
1561 if (!found) {
1562 // no matching case. use default if it exists, or kill the whole thing
1563 if (defaultCase) {
1564 std::unique_ptr<Statement> newBlock = block_for_case(&s, defaultCase);
1565 if (newBlock) {
Brian Osman010ce6a2020-10-19 16:34:10 -04001566 (*iter)->setStatement(std::move(newBlock), usage);
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001567 } else {
Ethan Nicholas01b05e52020-10-22 15:53:41 -04001568 if (s.isStatic() && !(fFlags & kPermitInvalidStaticTests_Flag) &&
1569 optimizationContext->fSilences.find(&s) ==
1570 optimizationContext->fSilences.end()) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001571 this->error(s.fOffset,
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001572 "static switch contains non-static conditional break");
Ethan Nicholas01b05e52020-10-22 15:53:41 -04001573 optimizationContext->fSilences.insert(&s);
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001574 }
1575 return; // can't simplify
1576 }
1577 } else {
Brian Osman010ce6a2020-10-19 16:34:10 -04001578 (*iter)->setStatement(std::make_unique<Nop>(), usage);
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001579 }
1580 }
Ethan Nicholascdeae8c2020-10-22 14:39:46 -04001581 optimizationContext->fUpdated = true;
1582 optimizationContext->fNeedsRescan = true;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001583 }
1584 break;
1585 }
Ethan Nicholase6592142020-09-08 10:22:09 -04001586 case Statement::Kind::kExpression: {
John Stilesa5a97b42020-08-18 11:19:07 -04001587 ExpressionStatement& e = stmt->as<ExpressionStatement>();
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001588 SkASSERT((*iter)->statement()->get() == &e);
Ethan Nicholasd503a5a2020-09-30 09:29:55 -04001589 if (!e.expression()->hasSideEffects()) {
Ethan Nicholascb670962017-04-20 19:31:52 -04001590 // Expression statement with no side effects, kill it
Ethan Nicholasd503a5a2020-09-30 09:29:55 -04001591 if (!b.tryRemoveExpressionBefore(iter, e.expression().get())) {
Ethan Nicholascdeae8c2020-10-22 14:39:46 -04001592 optimizationContext->fNeedsRescan = true;
Ethan Nicholascb670962017-04-20 19:31:52 -04001593 }
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001594 SkASSERT((*iter)->statement()->get() == stmt);
Brian Osman010ce6a2020-10-19 16:34:10 -04001595 (*iter)->setStatement(std::make_unique<Nop>(), usage);
Ethan Nicholascdeae8c2020-10-22 14:39:46 -04001596 optimizationContext->fUpdated = true;
Ethan Nicholascb670962017-04-20 19:31:52 -04001597 }
1598 break;
1599 }
1600 default:
1601 break;
1602 }
1603}
1604
Brian Osman010ce6a2020-10-19 16:34:10 -04001605bool Compiler::scanCFG(FunctionDefinition& f, ProgramUsage* usage) {
John Stiles0cc193a2020-09-09 09:39:34 -04001606 bool madeChanges = false;
1607
Ethan Nicholascb670962017-04-20 19:31:52 -04001608 CFG cfg = CFGGenerator().getCFG(f);
1609 this->computeDataFlow(&cfg);
ethannicholas22f939e2016-10-13 13:25:34 -07001610
1611 // check for unreachable code
1612 for (size_t i = 0; i < cfg.fBlocks.size(); i++) {
John Stiles0cc193a2020-09-09 09:39:34 -04001613 const BasicBlock& block = cfg.fBlocks[i];
John Stiles40525102020-12-16 18:10:44 -05001614 if (!block.fIsReachable && !block.fAllowUnreachable && block.fNodes.size()) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001615 int offset;
John Stiles0cc193a2020-09-09 09:39:34 -04001616 const BasicBlock::Node& node = block.fNodes[0];
John Stiles70025e52020-09-28 16:08:58 -04001617 if (node.isStatement()) {
1618 offset = (*node.statement())->fOffset;
1619 } else {
1620 offset = (*node.expression())->fOffset;
1621 if ((*node.expression())->is<BoolLiteral>()) {
1622 // Function inlining can generate do { ... } while(false) loops which always
1623 // break, so the boolean condition is considered unreachable. Since not being
1624 // able to reach a literal is a non-issue in the first place, we don't report an
1625 // error in this case.
1626 continue;
1627 }
Ethan Nicholas86a43402017-01-19 13:32:00 -05001628 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001629 this->error(offset, String("unreachable"));
ethannicholas22f939e2016-10-13 13:25:34 -07001630 }
1631 }
1632 if (fErrorCount) {
John Stiles0cc193a2020-09-09 09:39:34 -04001633 return madeChanges;
ethannicholas22f939e2016-10-13 13:25:34 -07001634 }
1635
Ethan Nicholascb670962017-04-20 19:31:52 -04001636 // check for dead code & undefined variables, perform constant propagation
Ethan Nicholascdeae8c2020-10-22 14:39:46 -04001637 OptimizationContext optimizationContext;
Brian Osman010ce6a2020-10-19 16:34:10 -04001638 optimizationContext.fUsage = usage;
John Stiles7d3f0892020-11-03 11:35:01 -05001639 SkBitSet eliminatedBlockIds(cfg.fBlocks.size());
Ethan Nicholascb670962017-04-20 19:31:52 -04001640 do {
Ethan Nicholascdeae8c2020-10-22 14:39:46 -04001641 if (optimizationContext.fNeedsRescan) {
Ethan Nicholascb670962017-04-20 19:31:52 -04001642 cfg = CFGGenerator().getCFG(f);
1643 this->computeDataFlow(&cfg);
Ethan Nicholascdeae8c2020-10-22 14:39:46 -04001644 optimizationContext.fNeedsRescan = false;
Ethan Nicholas113628d2017-02-02 16:11:39 -05001645 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001646
John Stiles7d3f0892020-11-03 11:35:01 -05001647 eliminatedBlockIds.reset();
Ethan Nicholascdeae8c2020-10-22 14:39:46 -04001648 optimizationContext.fUpdated = false;
John Stiles7d3f0892020-11-03 11:35:01 -05001649
1650 for (BlockId blockId = 0; blockId < cfg.fBlocks.size(); ++blockId) {
1651 if (eliminatedBlockIds.test(blockId)) {
1652 // We reached a block ID that might have been eliminated. Be cautious and rescan.
1653 optimizationContext.fUpdated = true;
1654 optimizationContext.fNeedsRescan = true;
1655 break;
1656 }
1657
1658 BasicBlock& b = cfg.fBlocks[blockId];
1659 if (blockId > 0 && !b.fIsReachable) {
Ethan Nicholas1de14812020-06-19 15:32:49 -04001660 // Block was reachable before optimization, but has since become unreachable. In
1661 // addition to being dead code, it's broken - since control flow can't reach it, no
1662 // prior variable definitions can reach it, and therefore variables might look to
Brian Osmanc0213602020-10-06 14:43:32 -04001663 // have not been properly assigned. Kill it by replacing all statements with Nops.
Ethan Nicholas1de14812020-06-19 15:32:49 -04001664 for (BasicBlock::Node& node : b.fNodes) {
John Stiles70025e52020-09-28 16:08:58 -04001665 if (node.isStatement() && !(*node.statement())->is<Nop>()) {
John Stiles7d3f0892020-11-03 11:35:01 -05001666 // Eliminating a node runs the risk of eliminating that node's exits as
1667 // well. Keep track of this and do a rescan if we are about to access one
1668 // of these.
1669 for (BlockId id : b.fExits) {
1670 eliminatedBlockIds.set(id);
1671 }
Brian Osman010ce6a2020-10-19 16:34:10 -04001672 node.setStatement(std::make_unique<Nop>(), usage);
John Stiles0cc193a2020-09-09 09:39:34 -04001673 madeChanges = true;
Ethan Nicholas1de14812020-06-19 15:32:49 -04001674 }
1675 }
1676 continue;
1677 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001678 DefinitionMap definitions = b.fBefore;
1679
Ethan Nicholascdeae8c2020-10-22 14:39:46 -04001680 for (auto iter = b.fNodes.begin(); iter != b.fNodes.end() &&
1681 !optimizationContext.fNeedsRescan; ++iter) {
John Stiles70025e52020-09-28 16:08:58 -04001682 if (iter->isExpression()) {
Ethan Nicholascdeae8c2020-10-22 14:39:46 -04001683 this->simplifyExpression(definitions, b, &iter, &optimizationContext);
Ethan Nicholascb670962017-04-20 19:31:52 -04001684 } else {
Ethan Nicholascdeae8c2020-10-22 14:39:46 -04001685 this->simplifyStatement(definitions, b, &iter, &optimizationContext);
Ethan Nicholascb670962017-04-20 19:31:52 -04001686 }
Ethan Nicholascdeae8c2020-10-22 14:39:46 -04001687 if (optimizationContext.fNeedsRescan) {
Ethan Nicholas4b330df2017-05-17 10:52:55 -04001688 break;
1689 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001690 this->addDefinitions(*iter, &definitions);
1691 }
Brian Osman01a3eb42020-09-14 11:32:49 -04001692
Ethan Nicholascdeae8c2020-10-22 14:39:46 -04001693 if (optimizationContext.fNeedsRescan) {
Brian Osman01a3eb42020-09-14 11:32:49 -04001694 break;
1695 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001696 }
Ethan Nicholascdeae8c2020-10-22 14:39:46 -04001697 madeChanges |= optimizationContext.fUpdated;
1698 } while (optimizationContext.fUpdated);
1699 SkASSERT(!optimizationContext.fNeedsRescan);
ethannicholas22f939e2016-10-13 13:25:34 -07001700
Ethan Nicholas91a10532017-06-22 11:24:38 -04001701 // verify static ifs & switches, clean up dead variable decls
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001702 for (BasicBlock& b : cfg.fBlocks) {
Ethan Nicholascdeae8c2020-10-22 14:39:46 -04001703 for (auto iter = b.fNodes.begin(); iter != b.fNodes.end() &&
1704 !optimizationContext.fNeedsRescan;) {
John Stiles70025e52020-09-28 16:08:58 -04001705 if (iter->isStatement()) {
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001706 const Statement& s = **iter->statement();
Ethan Nicholase6592142020-09-08 10:22:09 -04001707 switch (s.kind()) {
1708 case Statement::Kind::kIf:
Ethan Nicholas8c44eca2020-10-07 16:47:09 -04001709 if (s.as<IfStatement>().isStatic() &&
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -04001710 !(fFlags & kPermitInvalidStaticTests_Flag)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001711 this->error(s.fOffset, "static if has non-static test");
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001712 }
Ethan Nicholas91a10532017-06-22 11:24:38 -04001713 ++iter;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001714 break;
Ethan Nicholase6592142020-09-08 10:22:09 -04001715 case Statement::Kind::kSwitch:
Ethan Nicholas01b05e52020-10-22 15:53:41 -04001716 if (s.as<SwitchStatement>().isStatic() &&
1717 !(fFlags & kPermitInvalidStaticTests_Flag) &&
1718 optimizationContext.fSilences.find(&s) ==
1719 optimizationContext.fSilences.end()) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001720 this->error(s.fOffset, "static switch has non-static test");
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001721 }
Ethan Nicholas91a10532017-06-22 11:24:38 -04001722 ++iter;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001723 break;
1724 default:
Ethan Nicholas91a10532017-06-22 11:24:38 -04001725 ++iter;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001726 break;
1727 }
Ethan Nicholas91a10532017-06-22 11:24:38 -04001728 } else {
1729 ++iter;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001730 }
1731 }
1732 }
1733
ethannicholas22f939e2016-10-13 13:25:34 -07001734 // check for missing return
Ethan Nicholas0a5d0962020-10-14 13:33:18 -04001735 if (f.declaration().returnType() != *fContext->fVoid_Type) {
John Stiles61e75e32020-10-01 15:42:37 -04001736 if (cfg.fBlocks[cfg.fExit].fIsReachable) {
Ethan Nicholas0a5d0962020-10-14 13:33:18 -04001737 this->error(f.fOffset, String("function '" + String(f.declaration().name()) +
Ethan Nicholasdb80f692019-11-22 14:06:12 -05001738 "' can exit without returning a value"));
ethannicholas22f939e2016-10-13 13:25:34 -07001739 }
1740 }
John Stiles0cc193a2020-09-09 09:39:34 -04001741
1742 return madeChanges;
ethannicholas22f939e2016-10-13 13:25:34 -07001743}
1744
Brian Osman32d53552020-09-23 13:55:20 -04001745std::unique_ptr<Program> Compiler::convertProgram(
1746 Program::Kind kind,
1747 String text,
1748 const Program::Settings& settings,
Brian Osmanbe0b3b72021-01-06 14:27:35 -05001749 const std::vector<std::unique_ptr<ExternalFunction>>* externalFunctions) {
1750 SkASSERT(!externalFunctions || (kind == Program::kGeneric_Kind));
Ethan Nicholas91164d12019-05-15 15:29:54 -04001751
Brian Osman0006ad02020-11-18 15:38:39 -05001752 // Loading and optimizing our base module might reset the inliner, so do that first,
1753 // *then* configure the inliner with the settings for this program.
1754 const ParsedModule& baseModule = this->moduleForProgramKind(kind);
1755
ethannicholasb3058bd2016-07-01 08:22:01 -07001756 fErrorText = "";
1757 fErrorCount = 0;
Brian Osman0006ad02020-11-18 15:38:39 -05001758 fInliner.reset(fIRGenerator->fModifiers.get(), &settings);
Brian Osman88cda172020-10-09 12:05:16 -04001759
1760 // Not using AutoSource, because caller is likely to call errorText() if we fail to compile
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001761 std::unique_ptr<String> textPtr(new String(std::move(text)));
1762 fSource = textPtr.get();
Brian Osman88cda172020-10-09 12:05:16 -04001763
John Stiles5c7bb322020-10-22 11:09:15 -04001764 // Enable node pooling while converting and optimizing the program for a performance boost.
1765 // The Program will take ownership of the pool.
John Stiles2d68ea32020-10-22 15:42:27 -04001766 std::unique_ptr<Pool> pool = Pool::Create();
1767 pool->attachToThread();
Brian Osman0006ad02020-11-18 15:38:39 -05001768 IRGenerator::IRBundle ir =
1769 fIRGenerator->convertProgram(kind, &settings, baseModule, /*isBuiltinCode=*/false,
Brian Osmanbe0b3b72021-01-06 14:27:35 -05001770 textPtr->c_str(), textPtr->size(), externalFunctions);
John Stiles5c7bb322020-10-22 11:09:15 -04001771 auto program = std::make_unique<Program>(kind,
1772 std::move(textPtr),
1773 settings,
Brian Osmand7e76592020-11-02 12:26:22 -05001774 fCaps,
John Stiles5c7bb322020-10-22 11:09:15 -04001775 fContext,
1776 std::move(ir.fElements),
Brian Osman133724c2020-10-28 14:14:39 -04001777 std::move(ir.fSharedElements),
John Stiles5c7bb322020-10-22 11:09:15 -04001778 std::move(ir.fModifiers),
1779 std::move(ir.fSymbolTable),
1780 std::move(pool),
1781 ir.fInputs);
1782 bool success = false;
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001783 if (fErrorCount) {
John Stiles5c7bb322020-10-22 11:09:15 -04001784 // Do not return programs that failed to compile.
1785 } else if (settings.fOptimize && !this->optimize(*program)) {
1786 // Do not return programs that failed to optimize.
1787 } else {
1788 // We have a successful program!
1789 success = true;
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001790 }
John Stiles5c7bb322020-10-22 11:09:15 -04001791
1792 program->fPool->detachFromThread();
1793 return success ? std::move(program) : nullptr;
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001794}
1795
Brian Osman0006ad02020-11-18 15:38:39 -05001796bool Compiler::optimize(LoadedModule& module) {
1797 SkASSERT(!fErrorCount);
1798 Program::Settings settings;
1799 fIRGenerator->fKind = module.fKind;
1800 fIRGenerator->fSettings = &settings;
1801 std::unique_ptr<ProgramUsage> usage = Analysis::GetUsage(module);
1802
1803 fInliner.reset(fModifiers.back().get(), &settings);
1804
1805 while (fErrorCount == 0) {
1806 bool madeChanges = false;
1807
1808 // Scan and optimize based on the control-flow graph for each function.
1809 for (const auto& element : module.fElements) {
1810 if (element->is<FunctionDefinition>()) {
1811 madeChanges |= this->scanCFG(element->as<FunctionDefinition>(), usage.get());
1812 }
1813 }
1814
1815 // Perform inline-candidate analysis and inline any functions deemed suitable.
John Stiles78047582020-12-16 16:17:41 -05001816 madeChanges |= fInliner.analyze(module.fElements, module.fSymbols, usage.get());
Brian Osman0006ad02020-11-18 15:38:39 -05001817
1818 if (!madeChanges) {
1819 break;
1820 }
1821 }
1822 return fErrorCount == 0;
1823}
1824
Ethan Nicholas00543112018-07-31 09:44:36 -04001825bool Compiler::optimize(Program& program) {
1826 SkASSERT(!fErrorCount);
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001827 fIRGenerator->fKind = program.fKind;
1828 fIRGenerator->fSettings = &program.fSettings;
Brian Osman010ce6a2020-10-19 16:34:10 -04001829 ProgramUsage* usage = program.fUsage.get();
John Stiles7954d6c2020-09-01 10:53:02 -04001830
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001831 while (fErrorCount == 0) {
1832 bool madeChanges = false;
John Stiles7954d6c2020-09-01 10:53:02 -04001833
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001834 // Scan and optimize based on the control-flow graph for each function.
Brian Osman133724c2020-10-28 14:14:39 -04001835 for (const auto& element : program.ownedElements()) {
Brian Osman1179fcf2020-10-08 16:04:40 -04001836 if (element->is<FunctionDefinition>()) {
Brian Osman010ce6a2020-10-19 16:34:10 -04001837 madeChanges |= this->scanCFG(element->as<FunctionDefinition>(), usage);
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001838 }
1839 }
1840
1841 // Perform inline-candidate analysis and inline any functions deemed suitable.
John Stiles78047582020-12-16 16:17:41 -05001842 madeChanges |= fInliner.analyze(program.ownedElements(), program.fSymbols, usage);
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001843
1844 // Remove dead functions. We wait until after analysis so that we still report errors,
1845 // even in unused code.
1846 if (program.fSettings.fRemoveDeadFunctions) {
Brian Osman133724c2020-10-28 14:14:39 -04001847 auto isDeadFunction = [&](const ProgramElement* element) {
1848 if (!element->is<FunctionDefinition>()) {
1849 return false;
1850 }
1851 const FunctionDefinition& fn = element->as<FunctionDefinition>();
1852 if (fn.declaration().name() != "main" && usage->get(fn.declaration()) == 0) {
1853 usage->remove(*element);
1854 madeChanges = true;
1855 return true;
1856 }
1857 return false;
1858 };
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001859 program.fElements.erase(
Brian Osman133724c2020-10-28 14:14:39 -04001860 std::remove_if(program.fElements.begin(), program.fElements.end(),
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001861 [&](const std::unique_ptr<ProgramElement>& element) {
Brian Osman133724c2020-10-28 14:14:39 -04001862 return isDeadFunction(element.get());
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001863 }),
1864 program.fElements.end());
Brian Osman133724c2020-10-28 14:14:39 -04001865 program.fSharedElements.erase(
1866 std::remove_if(program.fSharedElements.begin(), program.fSharedElements.end(),
1867 isDeadFunction),
1868 program.fSharedElements.end());
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001869 }
1870
1871 if (program.fKind != Program::kFragmentProcessor_Kind) {
Brian Osmanc0213602020-10-06 14:43:32 -04001872 // Remove declarations of dead global variables
Brian Osman133724c2020-10-28 14:14:39 -04001873 auto isDeadVariable = [&](const ProgramElement* element) {
1874 if (!element->is<GlobalVarDeclaration>()) {
1875 return false;
1876 }
1877 const GlobalVarDeclaration& global = element->as<GlobalVarDeclaration>();
1878 const VarDeclaration& varDecl = global.declaration()->as<VarDeclaration>();
1879 if (usage->isDead(varDecl.var())) {
1880 madeChanges = true;
1881 return true;
1882 }
1883 return false;
1884 };
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001885 program.fElements.erase(
1886 std::remove_if(program.fElements.begin(), program.fElements.end(),
1887 [&](const std::unique_ptr<ProgramElement>& element) {
Brian Osman133724c2020-10-28 14:14:39 -04001888 return isDeadVariable(element.get());
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001889 }),
1890 program.fElements.end());
Brian Osman133724c2020-10-28 14:14:39 -04001891 program.fSharedElements.erase(
1892 std::remove_if(program.fSharedElements.begin(), program.fSharedElements.end(),
1893 isDeadVariable),
1894 program.fSharedElements.end());
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001895 }
John Stiles73a6bff2020-09-09 13:40:37 -04001896
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001897 if (!madeChanges) {
1898 break;
Ethan Nicholas0dc80872019-02-08 15:46:24 -05001899 }
Ethan Nicholas00543112018-07-31 09:44:36 -04001900 }
1901 return fErrorCount == 0;
1902}
1903
Brian Osmanfb32ddf2019-06-18 10:14:20 -04001904#if defined(SKSL_STANDALONE) || SK_SUPPORT_GPU
1905
Ethan Nicholas00543112018-07-31 09:44:36 -04001906bool Compiler::toSPIRV(Program& program, OutputStream& out) {
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001907#ifdef SK_ENABLE_SPIRV_VALIDATION
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001908 StringStream buffer;
Brian Osman88cda172020-10-09 12:05:16 -04001909 AutoSource as(this, program.fSource.get());
Brian Osman8b43dad2020-10-09 13:31:42 -04001910 SPIRVCodeGenerator cg(fContext.get(), &program, this, &buffer);
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001911 bool result = cg.generateCode();
Ethan Nicholasba9a04f2020-11-06 09:28:04 -05001912 if (result && program.fSettings.fValidateSPIRV) {
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001913 spvtools::SpirvTools tools(SPV_ENV_VULKAN_1_0);
Ethan Nicholas762466e2017-06-29 10:03:38 -04001914 const String& data = buffer.str();
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001915 SkASSERT(0 == data.size() % 4);
Brian Osman8d09d4a2020-11-24 15:51:06 -05001916 String errors;
1917 auto dumpmsg = [&errors](spv_message_level_t, const char*, const spv_position_t&,
1918 const char* m) {
1919 errors.appendf("SPIR-V validation error: %s\n", m);
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001920 };
1921 tools.SetMessageConsumer(dumpmsg);
Brian Osman8d09d4a2020-11-24 15:51:06 -05001922
1923 // Verify that the SPIR-V we produced is valid. At runtime, we will abort() with a message
1924 // explaining the error. In standalone mode (skslc), we will send the message, plus the
1925 // entire disassembled SPIR-V (for easier context & debugging) as *our* error message.
1926 result = tools.Validate((const uint32_t*) data.c_str(), data.size() / 4);
1927
1928 if (!result) {
1929#if defined(SKSL_STANDALONE)
1930 // Convert the string-stream to a SPIR-V disassembly.
1931 std::string disassembly;
1932 if (tools.Disassemble((const uint32_t*)data.data(), data.size() / 4, &disassembly)) {
1933 errors.append(disassembly);
1934 }
1935 this->error(-1, errors);
1936#else
1937 SkDEBUGFAILF("%s", errors.c_str());
1938#endif
1939 }
Ethan Nicholas762466e2017-06-29 10:03:38 -04001940 out.write(data.c_str(), data.size());
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001941 }
1942#else
Brian Osman88cda172020-10-09 12:05:16 -04001943 AutoSource as(this, program.fSource.get());
Brian Osman8b43dad2020-10-09 13:31:42 -04001944 SPIRVCodeGenerator cg(fContext.get(), &program, this, &out);
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001945 bool result = cg.generateCode();
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001946#endif
Ethan Nicholasce33f102016-12-09 17:22:59 -05001947 return result;
1948}
1949
Ethan Nicholas00543112018-07-31 09:44:36 -04001950bool Compiler::toSPIRV(Program& program, String* out) {
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001951 StringStream buffer;
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001952 bool result = this->toSPIRV(program, buffer);
1953 if (result) {
Ethan Nicholas762466e2017-06-29 10:03:38 -04001954 *out = buffer.str();
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001955 }
1956 return result;
1957}
1958
Ethan Nicholas00543112018-07-31 09:44:36 -04001959bool Compiler::toGLSL(Program& program, OutputStream& out) {
Brian Osman88cda172020-10-09 12:05:16 -04001960 AutoSource as(this, program.fSource.get());
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001961 GLSLCodeGenerator cg(fContext.get(), &program, this, &out);
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001962 bool result = cg.generateCode();
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001963 return result;
1964}
1965
Ethan Nicholas00543112018-07-31 09:44:36 -04001966bool Compiler::toGLSL(Program& program, String* out) {
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001967 StringStream buffer;
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001968 bool result = this->toGLSL(program, buffer);
1969 if (result) {
Ethan Nicholas762466e2017-06-29 10:03:38 -04001970 *out = buffer.str();
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001971 }
1972 return result;
1973}
1974
Brian Osmanc0243912020-02-19 15:35:26 -05001975bool Compiler::toHLSL(Program& program, String* out) {
1976 String spirv;
1977 if (!this->toSPIRV(program, &spirv)) {
1978 return false;
1979 }
1980
1981 return SPIRVtoHLSL(spirv, out);
1982}
1983
Ethan Nicholas00543112018-07-31 09:44:36 -04001984bool Compiler::toMetal(Program& program, OutputStream& out) {
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001985 MetalCodeGenerator cg(fContext.get(), &program, this, &out);
Ethan Nicholascc305772017-10-13 16:17:45 -04001986 bool result = cg.generateCode();
Ethan Nicholascc305772017-10-13 16:17:45 -04001987 return result;
1988}
1989
Ethan Nicholas00543112018-07-31 09:44:36 -04001990bool Compiler::toMetal(Program& program, String* out) {
Timothy Liangb8eeb802018-07-23 16:46:16 -04001991 StringStream buffer;
1992 bool result = this->toMetal(program, buffer);
1993 if (result) {
1994 *out = buffer.str();
1995 }
1996 return result;
1997}
1998
Greg Daniela28ea672020-09-25 11:12:56 -04001999#if defined(SKSL_STANDALONE) || GR_TEST_UTILS
Ethan Nicholas00543112018-07-31 09:44:36 -04002000bool Compiler::toCPP(Program& program, String name, OutputStream& out) {
Brian Osman88cda172020-10-09 12:05:16 -04002001 AutoSource as(this, program.fSource.get());
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04002002 CPPCodeGenerator cg(fContext.get(), &program, this, name, &out);
Ethan Nicholas762466e2017-06-29 10:03:38 -04002003 bool result = cg.generateCode();
Ethan Nicholas762466e2017-06-29 10:03:38 -04002004 return result;
2005}
2006
Ethan Nicholas00543112018-07-31 09:44:36 -04002007bool Compiler::toH(Program& program, String name, OutputStream& out) {
Brian Osman88cda172020-10-09 12:05:16 -04002008 AutoSource as(this, program.fSource.get());
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04002009 HCodeGenerator cg(fContext.get(), &program, this, name, &out);
Ethan Nicholas762466e2017-06-29 10:03:38 -04002010 bool result = cg.generateCode();
Ethan Nicholas00543112018-07-31 09:44:36 -04002011 return result;
2012}
Greg Daniela28ea672020-09-25 11:12:56 -04002013#endif // defined(SKSL_STANDALONE) || GR_TEST_UTILS
Ethan Nicholas00543112018-07-31 09:44:36 -04002014
Ethan Nicholas2a479a52020-08-18 16:29:45 -04002015#endif // defined(SKSL_STANDALONE) || SK_SUPPORT_GPU
Brian Osman2e29ab52019-09-20 12:19:11 -04002016
2017#if !defined(SKSL_STANDALONE) && SK_SUPPORT_GPU
Brian Osmana4b91692020-08-10 14:26:16 -04002018bool Compiler::toPipelineStage(Program& program, PipelineStageArgs* outArgs) {
Brian Osman88cda172020-10-09 12:05:16 -04002019 AutoSource as(this, program.fSource.get());
Ethan Nicholas00543112018-07-31 09:44:36 -04002020 StringStream buffer;
Brian Osman300fe1d2020-01-23 15:42:43 -05002021 PipelineStageCodeGenerator cg(fContext.get(), &program, this, &buffer, outArgs);
Ethan Nicholas00543112018-07-31 09:44:36 -04002022 bool result = cg.generateCode();
Ethan Nicholas00543112018-07-31 09:44:36 -04002023 if (result) {
Brian Osman107c6662019-12-30 15:02:30 -05002024 outArgs->fCode = buffer.str();
Ethan Nicholas00543112018-07-31 09:44:36 -04002025 }
Ethan Nicholas762466e2017-06-29 10:03:38 -04002026 return result;
2027}
Brian Osmanfb32ddf2019-06-18 10:14:20 -04002028#endif
2029
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04002030std::unique_ptr<ByteCode> Compiler::toByteCode(Program& program) {
Brian Osman88cda172020-10-09 12:05:16 -04002031 AutoSource as(this, program.fSource.get());
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04002032 std::unique_ptr<ByteCode> result(new ByteCode());
Brian Osmanb08cc022020-04-02 11:38:40 -04002033 ByteCodeGenerator cg(fContext.get(), &program, this, result.get());
2034 bool success = cg.generateCode();
Brian Osmanb08cc022020-04-02 11:38:40 -04002035 if (success) {
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04002036 return result;
2037 }
2038 return nullptr;
2039}
2040
Brian Osman401a0092020-09-10 14:47:24 -04002041const char* Compiler::OperatorName(Token::Kind op) {
2042 switch (op) {
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04002043 case Token::Kind::TK_PLUS: return "+";
2044 case Token::Kind::TK_MINUS: return "-";
2045 case Token::Kind::TK_STAR: return "*";
2046 case Token::Kind::TK_SLASH: return "/";
2047 case Token::Kind::TK_PERCENT: return "%";
2048 case Token::Kind::TK_SHL: return "<<";
2049 case Token::Kind::TK_SHR: return ">>";
2050 case Token::Kind::TK_LOGICALNOT: return "!";
2051 case Token::Kind::TK_LOGICALAND: return "&&";
2052 case Token::Kind::TK_LOGICALOR: return "||";
2053 case Token::Kind::TK_LOGICALXOR: return "^^";
2054 case Token::Kind::TK_BITWISENOT: return "~";
2055 case Token::Kind::TK_BITWISEAND: return "&";
2056 case Token::Kind::TK_BITWISEOR: return "|";
2057 case Token::Kind::TK_BITWISEXOR: return "^";
2058 case Token::Kind::TK_EQ: return "=";
2059 case Token::Kind::TK_EQEQ: return "==";
2060 case Token::Kind::TK_NEQ: return "!=";
2061 case Token::Kind::TK_LT: return "<";
2062 case Token::Kind::TK_GT: return ">";
2063 case Token::Kind::TK_LTEQ: return "<=";
2064 case Token::Kind::TK_GTEQ: return ">=";
2065 case Token::Kind::TK_PLUSEQ: return "+=";
2066 case Token::Kind::TK_MINUSEQ: return "-=";
2067 case Token::Kind::TK_STAREQ: return "*=";
2068 case Token::Kind::TK_SLASHEQ: return "/=";
2069 case Token::Kind::TK_PERCENTEQ: return "%=";
2070 case Token::Kind::TK_SHLEQ: return "<<=";
2071 case Token::Kind::TK_SHREQ: return ">>=";
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04002072 case Token::Kind::TK_BITWISEANDEQ: return "&=";
2073 case Token::Kind::TK_BITWISEOREQ: return "|=";
2074 case Token::Kind::TK_BITWISEXOREQ: return "^=";
2075 case Token::Kind::TK_PLUSPLUS: return "++";
2076 case Token::Kind::TK_MINUSMINUS: return "--";
2077 case Token::Kind::TK_COMMA: return ",";
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002078 default:
Brian Osman401a0092020-09-10 14:47:24 -04002079 ABORT("unsupported operator: %d\n", (int) op);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002080 }
2081}
2082
2083
2084bool Compiler::IsAssignment(Token::Kind op) {
2085 switch (op) {
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04002086 case Token::Kind::TK_EQ: // fall through
2087 case Token::Kind::TK_PLUSEQ: // fall through
2088 case Token::Kind::TK_MINUSEQ: // fall through
2089 case Token::Kind::TK_STAREQ: // fall through
2090 case Token::Kind::TK_SLASHEQ: // fall through
2091 case Token::Kind::TK_PERCENTEQ: // fall through
2092 case Token::Kind::TK_SHLEQ: // fall through
2093 case Token::Kind::TK_SHREQ: // fall through
2094 case Token::Kind::TK_BITWISEOREQ: // fall through
2095 case Token::Kind::TK_BITWISEXOREQ: // fall through
John Stiles8b3b1592020-11-23 11:06:44 -05002096 case Token::Kind::TK_BITWISEANDEQ:
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002097 return true;
2098 default:
2099 return false;
2100 }
2101}
2102
Brian Osman401a0092020-09-10 14:47:24 -04002103Token::Kind Compiler::RemoveAssignment(Token::Kind op) {
2104 switch (op) {
2105 case Token::Kind::TK_PLUSEQ: return Token::Kind::TK_PLUS;
2106 case Token::Kind::TK_MINUSEQ: return Token::Kind::TK_MINUS;
2107 case Token::Kind::TK_STAREQ: return Token::Kind::TK_STAR;
2108 case Token::Kind::TK_SLASHEQ: return Token::Kind::TK_SLASH;
2109 case Token::Kind::TK_PERCENTEQ: return Token::Kind::TK_PERCENT;
2110 case Token::Kind::TK_SHLEQ: return Token::Kind::TK_SHL;
2111 case Token::Kind::TK_SHREQ: return Token::Kind::TK_SHR;
2112 case Token::Kind::TK_BITWISEOREQ: return Token::Kind::TK_BITWISEOR;
2113 case Token::Kind::TK_BITWISEXOREQ: return Token::Kind::TK_BITWISEXOR;
2114 case Token::Kind::TK_BITWISEANDEQ: return Token::Kind::TK_BITWISEAND;
Brian Osman401a0092020-09-10 14:47:24 -04002115 default: return op;
2116 }
2117}
2118
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002119Position Compiler::position(int offset) {
Ethan Nicholas3c729892020-12-07 12:47:17 -05002120 if (fSource && offset >= 0) {
2121 int line = 1;
2122 int column = 1;
2123 for (int i = 0; i < offset; i++) {
2124 if ((*fSource)[i] == '\n') {
2125 ++line;
2126 column = 1;
2127 }
2128 else {
2129 ++column;
2130 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002131 }
Ethan Nicholas3c729892020-12-07 12:47:17 -05002132 return Position(line, column);
2133 } else {
2134 return Position(-1, -1);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002135 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002136}
2137
2138void Compiler::error(int offset, String msg) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002139 fErrorCount++;
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002140 Position pos = this->position(offset);
Ethan Nicholas3c729892020-12-07 12:47:17 -05002141 fErrorText += "error: " + (pos.fLine >= 1 ? to_string(pos.fLine) + ": " : "") + msg + "\n";
ethannicholasb3058bd2016-07-01 08:22:01 -07002142}
2143
Ethan Nicholas95046142021-01-07 10:57:27 -05002144String Compiler::errorText(bool showCount) {
2145 if (showCount) {
2146 this->writeErrorCount();
2147 }
Ethan Nicholas00543112018-07-31 09:44:36 -04002148 fErrorCount = 0;
Ethan Nicholas0df1b042017-03-31 13:56:23 -04002149 String result = fErrorText;
Ethan Nicholas95046142021-01-07 10:57:27 -05002150 fErrorText = "";
ethannicholasb3058bd2016-07-01 08:22:01 -07002151 return result;
2152}
2153
2154void Compiler::writeErrorCount() {
2155 if (fErrorCount) {
2156 fErrorText += to_string(fErrorCount) + " error";
2157 if (fErrorCount > 1) {
2158 fErrorText += "s";
2159 }
2160 fErrorText += "\n";
2161 }
2162}
2163
John Stilesa6841be2020-08-06 14:11:56 -04002164} // namespace SkSL