blob: b4bb2ac73f6f49d1d09bb4889835138d25713531 [file] [log] [blame]
ethannicholasb3058bd2016-07-01 08:22:01 -07001/*
2 * Copyright 2016 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
Mike Klein6ad99092016-10-26 10:35:22 -04007
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/sksl/SkSLCompiler.h"
ethannicholasb3058bd2016-07-01 08:22:01 -07009
John Stilesfbd050b2020-08-03 13:21:46 -040010#include <memory>
John Stilesb8e010c2020-08-11 18:05:39 -040011#include <unordered_set>
John Stilesfbd050b2020-08-03 13:21:46 -040012
John Stilesb92641c2020-08-31 18:09:01 -040013#include "src/sksl/SkSLAnalysis.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "src/sksl/SkSLByteCodeGenerator.h"
15#include "src/sksl/SkSLCFGGenerator.h"
16#include "src/sksl/SkSLCPPCodeGenerator.h"
17#include "src/sksl/SkSLGLSLCodeGenerator.h"
18#include "src/sksl/SkSLHCodeGenerator.h"
19#include "src/sksl/SkSLIRGenerator.h"
20#include "src/sksl/SkSLMetalCodeGenerator.h"
21#include "src/sksl/SkSLPipelineStageCodeGenerator.h"
Ethan Nicholasc18bb512020-07-28 14:46:53 -040022#include "src/sksl/SkSLRehydrator.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "src/sksl/SkSLSPIRVCodeGenerator.h"
Brian Osmanc0243912020-02-19 15:35:26 -050024#include "src/sksl/SkSLSPIRVtoHLSL.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "src/sksl/ir/SkSLEnum.h"
26#include "src/sksl/ir/SkSLExpression.h"
27#include "src/sksl/ir/SkSLExpressionStatement.h"
28#include "src/sksl/ir/SkSLFunctionCall.h"
29#include "src/sksl/ir/SkSLIntLiteral.h"
30#include "src/sksl/ir/SkSLModifiersDeclaration.h"
31#include "src/sksl/ir/SkSLNop.h"
32#include "src/sksl/ir/SkSLSymbolTable.h"
33#include "src/sksl/ir/SkSLTernaryExpression.h"
34#include "src/sksl/ir/SkSLUnresolvedFunction.h"
35#include "src/sksl/ir/SkSLVarDeclarations.h"
ethannicholasb3058bd2016-07-01 08:22:01 -070036
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -040037#include <fstream>
38
Ethan Nicholasa11035b2019-11-26 16:27:47 -050039#if !defined(SKSL_STANDALONE) & SK_SUPPORT_GPU
40#include "include/gpu/GrContextOptions.h"
41#include "src/gpu/GrShaderCaps.h"
42#endif
43
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -040044#ifdef SK_ENABLE_SPIRV_VALIDATION
45#include "spirv-tools/libspirv.hpp"
46#endif
47
Brian Osmandd496172020-08-08 08:17:18 -040048#if !SKSL_STANDALONE
Ethan Nicholasc18bb512020-07-28 14:46:53 -040049
50#include "src/sksl/generated/sksl_fp.dehydrated.sksl"
51#include "src/sksl/generated/sksl_frag.dehydrated.sksl"
52#include "src/sksl/generated/sksl_geom.dehydrated.sksl"
53#include "src/sksl/generated/sksl_gpu.dehydrated.sksl"
54#include "src/sksl/generated/sksl_interp.dehydrated.sksl"
55#include "src/sksl/generated/sksl_pipeline.dehydrated.sksl"
56#include "src/sksl/generated/sksl_vert.dehydrated.sksl"
57
58#else
59
Brian Osmandd496172020-08-08 08:17:18 -040060// GN generates or copies all of these files to the skslc executable directory
61static const char SKSL_GPU_INCLUDE[] = "sksl_gpu.sksl";
62static const char SKSL_INTERP_INCLUDE[] = "sksl_interp.sksl";
63static const char SKSL_VERT_INCLUDE[] = "sksl_vert.sksl";
64static const char SKSL_FRAG_INCLUDE[] = "sksl_frag.sksl";
65static const char SKSL_GEOM_INCLUDE[] = "sksl_geom.sksl";
66static const char SKSL_FP_INCLUDE[] = "sksl_fp.sksl";
67static const char SKSL_PIPELINE_INCLUDE[] = "sksl_pipeline.sksl";
Ethan Nicholasc18bb512020-07-28 14:46:53 -040068
69#endif
Ethan Nicholas0d997662019-04-08 09:46:01 -040070
ethannicholasb3058bd2016-07-01 08:22:01 -070071namespace SkSL {
72
Ethan Nicholasdb80f692019-11-22 14:06:12 -050073static void grab_intrinsics(std::vector<std::unique_ptr<ProgramElement>>* src,
John Stiles810c8cf2020-08-26 19:46:27 -040074 IRIntrinsicMap* target) {
Brian Osman08f986d2020-05-13 17:06:46 -040075 for (auto iter = src->begin(); iter != src->end(); ) {
76 std::unique_ptr<ProgramElement>& element = *iter;
Ethan Nicholase6592142020-09-08 10:22:09 -040077 switch (element->kind()) {
78 case ProgramElement::Kind::kFunction: {
John Stiles3dc0da62020-08-19 17:48:31 -040079 FunctionDefinition& f = element->as<FunctionDefinition>();
Brian Osman08f986d2020-05-13 17:06:46 -040080 SkASSERT(f.fDeclaration.fBuiltin);
Brian Osman2b469eb2020-09-21 11:32:10 -040081 target->insertOrDie(f.fDeclaration.description(), std::move(element));
Brian Osman08f986d2020-05-13 17:06:46 -040082 iter = src->erase(iter);
Ethan Nicholasdb80f692019-11-22 14:06:12 -050083 break;
84 }
Ethan Nicholase6592142020-09-08 10:22:09 -040085 case ProgramElement::Kind::kEnum: {
John Stiles3dc0da62020-08-19 17:48:31 -040086 Enum& e = element->as<Enum>();
Ethan Nicholasd83ded82020-09-29 17:05:54 -040087 target->insertOrDie(e.typeName(), std::move(element));
Brian Osman08f986d2020-05-13 17:06:46 -040088 iter = src->erase(iter);
Ethan Nicholasdb80f692019-11-22 14:06:12 -050089 break;
90 }
Brian Osman8e2ef022020-09-30 13:26:43 -040091 case ProgramElement::Kind::kVar: {
92 // TODO: For now, we only support one variable per declaration. We map names to
93 // declarations, and each declaration pulls in all of it's variables, so this rule
94 // ensures that we never pull in variables that aren't actually used.
95 const VarDeclarations& vd = element->as<VarDeclarations>();
96 SkASSERT(vd.fVars.size() == 1);
97 const Variable* var = vd.fVars[0]->as<VarDeclaration>().fVar;
Brian Osman58384ad2020-10-02 22:15:22 +000098 target->insertOrDie(var->fName, std::move(element));
Brian Osman8e2ef022020-09-30 13:26:43 -040099 iter = src->erase(iter);
100 break;
101 }
Ethan Nicholasdb80f692019-11-22 14:06:12 -0500102 default:
Brian Osman00a8b5b2020-10-02 09:06:04 -0400103 printf("Unsupported element: %s\n", element->description().c_str());
104 SkASSERT(false);
Brian Osman2b469eb2020-09-21 11:32:10 -0400105 break;
Ethan Nicholasdb80f692019-11-22 14:06:12 -0500106 }
107 }
108}
109
John Stilesbc0c29e2020-09-28 13:13:40 -0400110static void reset_call_counts(std::vector<std::unique_ptr<ProgramElement>>* src) {
111 for (std::unique_ptr<ProgramElement>& element : *src) {
112 if (element->is<FunctionDefinition>()) {
113 const FunctionDeclaration& fnDecl = element->as<FunctionDefinition>().fDeclaration;
114 fnDecl.fCallCount = 0;
115 }
116 }
117}
118
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -0400119Compiler::Compiler(Flags flags)
Brian Osman00a8b5b2020-10-02 09:06:04 -0400120: fFlags(flags)
John Stiles810c8cf2020-08-26 19:46:27 -0400121, fContext(std::make_shared<Context>())
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -0400122, fErrorCount(0) {
Brian Osmaneac49832020-09-18 11:49:22 -0400123 fRootSymbolTable = std::make_shared<SymbolTable>(this);
John Stiles941fc712020-09-19 12:47:10 +0000124 fIRGenerator =
125 std::make_unique<IRGenerator>(fContext.get(), &fInliner, fRootSymbolTable, *this);
Brian Osman58384ad2020-10-02 22:15:22 +0000126 #define ADD_TYPE(t) fRootSymbolTable->addWithoutOwnership(fContext->f ## t ## _Type->fName, \
Brian Osmaneac49832020-09-18 11:49:22 -0400127 fContext->f ## t ## _Type.get())
ethannicholasb3058bd2016-07-01 08:22:01 -0700128 ADD_TYPE(Void);
129 ADD_TYPE(Float);
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400130 ADD_TYPE(Float2);
131 ADD_TYPE(Float3);
132 ADD_TYPE(Float4);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400133 ADD_TYPE(Half);
134 ADD_TYPE(Half2);
135 ADD_TYPE(Half3);
136 ADD_TYPE(Half4);
ethannicholasb3058bd2016-07-01 08:22:01 -0700137 ADD_TYPE(Int);
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400138 ADD_TYPE(Int2);
139 ADD_TYPE(Int3);
140 ADD_TYPE(Int4);
ethannicholasb3058bd2016-07-01 08:22:01 -0700141 ADD_TYPE(UInt);
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400142 ADD_TYPE(UInt2);
143 ADD_TYPE(UInt3);
144 ADD_TYPE(UInt4);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400145 ADD_TYPE(Short);
146 ADD_TYPE(Short2);
147 ADD_TYPE(Short3);
148 ADD_TYPE(Short4);
149 ADD_TYPE(UShort);
150 ADD_TYPE(UShort2);
151 ADD_TYPE(UShort3);
152 ADD_TYPE(UShort4);
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400153 ADD_TYPE(Byte);
154 ADD_TYPE(Byte2);
155 ADD_TYPE(Byte3);
156 ADD_TYPE(Byte4);
157 ADD_TYPE(UByte);
158 ADD_TYPE(UByte2);
159 ADD_TYPE(UByte3);
160 ADD_TYPE(UByte4);
ethannicholasb3058bd2016-07-01 08:22:01 -0700161 ADD_TYPE(Bool);
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400162 ADD_TYPE(Bool2);
163 ADD_TYPE(Bool3);
164 ADD_TYPE(Bool4);
165 ADD_TYPE(Float2x2);
166 ADD_TYPE(Float2x3);
167 ADD_TYPE(Float2x4);
168 ADD_TYPE(Float3x2);
169 ADD_TYPE(Float3x3);
170 ADD_TYPE(Float3x4);
171 ADD_TYPE(Float4x2);
172 ADD_TYPE(Float4x3);
173 ADD_TYPE(Float4x4);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400174 ADD_TYPE(Half2x2);
175 ADD_TYPE(Half2x3);
176 ADD_TYPE(Half2x4);
177 ADD_TYPE(Half3x2);
178 ADD_TYPE(Half3x3);
179 ADD_TYPE(Half3x4);
180 ADD_TYPE(Half4x2);
181 ADD_TYPE(Half4x3);
182 ADD_TYPE(Half4x4);
ethannicholasb3058bd2016-07-01 08:22:01 -0700183 ADD_TYPE(GenType);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400184 ADD_TYPE(GenHType);
ethannicholasb3058bd2016-07-01 08:22:01 -0700185 ADD_TYPE(GenIType);
186 ADD_TYPE(GenUType);
187 ADD_TYPE(GenBType);
188 ADD_TYPE(Mat);
189 ADD_TYPE(Vec);
190 ADD_TYPE(GVec);
191 ADD_TYPE(GVec2);
192 ADD_TYPE(GVec3);
193 ADD_TYPE(GVec4);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400194 ADD_TYPE(HVec);
ethannicholasb3058bd2016-07-01 08:22:01 -0700195 ADD_TYPE(IVec);
196 ADD_TYPE(UVec);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400197 ADD_TYPE(SVec);
198 ADD_TYPE(USVec);
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400199 ADD_TYPE(ByteVec);
200 ADD_TYPE(UByteVec);
ethannicholasb3058bd2016-07-01 08:22:01 -0700201 ADD_TYPE(BVec);
202
203 ADD_TYPE(Sampler1D);
204 ADD_TYPE(Sampler2D);
205 ADD_TYPE(Sampler3D);
ethannicholas5961bc92016-10-12 06:39:56 -0700206 ADD_TYPE(SamplerExternalOES);
ethannicholasb3058bd2016-07-01 08:22:01 -0700207 ADD_TYPE(SamplerCube);
208 ADD_TYPE(Sampler2DRect);
209 ADD_TYPE(Sampler1DArray);
210 ADD_TYPE(Sampler2DArray);
211 ADD_TYPE(SamplerCubeArray);
212 ADD_TYPE(SamplerBuffer);
213 ADD_TYPE(Sampler2DMS);
214 ADD_TYPE(Sampler2DMSArray);
215
Brian Salomonbf7b6202016-11-11 16:08:03 -0500216 ADD_TYPE(ISampler2D);
217
Brian Salomon2a51de82016-11-16 12:06:01 -0500218 ADD_TYPE(Image2D);
219 ADD_TYPE(IImage2D);
220
Greg Daniel64773e62016-11-22 09:44:03 -0500221 ADD_TYPE(SubpassInput);
222 ADD_TYPE(SubpassInputMS);
223
ethannicholasb3058bd2016-07-01 08:22:01 -0700224 ADD_TYPE(GSampler1D);
225 ADD_TYPE(GSampler2D);
226 ADD_TYPE(GSampler3D);
227 ADD_TYPE(GSamplerCube);
228 ADD_TYPE(GSampler2DRect);
229 ADD_TYPE(GSampler1DArray);
230 ADD_TYPE(GSampler2DArray);
231 ADD_TYPE(GSamplerCubeArray);
232 ADD_TYPE(GSamplerBuffer);
233 ADD_TYPE(GSampler2DMS);
234 ADD_TYPE(GSampler2DMSArray);
235
236 ADD_TYPE(Sampler1DShadow);
237 ADD_TYPE(Sampler2DShadow);
238 ADD_TYPE(SamplerCubeShadow);
239 ADD_TYPE(Sampler2DRectShadow);
240 ADD_TYPE(Sampler1DArrayShadow);
241 ADD_TYPE(Sampler2DArrayShadow);
242 ADD_TYPE(SamplerCubeArrayShadow);
243 ADD_TYPE(GSampler2DArrayShadow);
244 ADD_TYPE(GSamplerCubeArrayShadow);
Ethan Nicholasc9472af2017-10-10 16:30:21 -0400245 ADD_TYPE(FragmentProcessor);
Stephen Whiteff5d7a22019-07-26 17:42:06 -0400246 ADD_TYPE(Sampler);
247 ADD_TYPE(Texture2D);
ethannicholasb3058bd2016-07-01 08:22:01 -0700248
Brian Osman28590d52020-03-23 16:59:08 -0400249 StringFragment fpAliasName("shader");
Brian Osmaneac49832020-09-18 11:49:22 -0400250 fRootSymbolTable->addWithoutOwnership(fpAliasName, fContext->fFragmentProcessor_Type.get());
Brian Osman28590d52020-03-23 16:59:08 -0400251
Brian Osman3887a012020-09-30 13:22:27 -0400252 // sk_Caps is "builtin", but all references to it are resolved to Settings, so we don't need to
253 // treat it as builtin (ie, no need to clone it into the Program).
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700254 StringFragment skCapsName("sk_Caps");
Brian Osman3887a012020-09-30 13:22:27 -0400255 fRootSymbolTable->add(skCapsName,
256 std::make_unique<Variable>(/*offset=*/-1, Modifiers(), skCapsName,
257 fContext->fSkCaps_Type.get(),
258 /*builtin=*/false, Variable::kGlobal_Storage));
Ethan Nicholas3605ace2016-11-21 15:59:48 -0500259
Brian Osman00a8b5b2020-10-02 09:06:04 -0400260 fIRGenerator->fIntrinsics = nullptr;
261 std::vector<std::unique_ptr<ProgramElement>> gpuElements;
262 std::vector<std::unique_ptr<ProgramElement>> fragElements;
Brian Osmandd496172020-08-08 08:17:18 -0400263#if SKSL_STANDALONE
Brian Osmaneac49832020-09-18 11:49:22 -0400264 this->processIncludeFile(Program::kFragment_Kind, SKSL_GPU_INCLUDE, fRootSymbolTable,
Brian Osman00a8b5b2020-10-02 09:06:04 -0400265 &gpuElements, &fGpuSymbolTable);
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -0400266 this->processIncludeFile(Program::kVertex_Kind, SKSL_VERT_INCLUDE, fGpuSymbolTable,
267 &fVertexInclude, &fVertexSymbolTable);
268 this->processIncludeFile(Program::kFragment_Kind, SKSL_FRAG_INCLUDE, fGpuSymbolTable,
Brian Osman00a8b5b2020-10-02 09:06:04 -0400269 &fragElements, &fFragmentSymbolTable);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400270#else
271 {
Brian Osmaneac49832020-09-18 11:49:22 -0400272 Rehydrator rehydrator(fContext.get(), fRootSymbolTable, this, SKSL_INCLUDE_sksl_gpu,
273 SKSL_INCLUDE_sksl_gpu_LENGTH);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400274 fGpuSymbolTable = rehydrator.symbolTable();
Brian Osman00a8b5b2020-10-02 09:06:04 -0400275 gpuElements = rehydrator.elements();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400276 }
277 {
278 Rehydrator rehydrator(fContext.get(), fGpuSymbolTable, this, SKSL_INCLUDE_sksl_vert,
Brian Osmaneac49832020-09-18 11:49:22 -0400279 SKSL_INCLUDE_sksl_vert_LENGTH);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400280 fVertexSymbolTable = rehydrator.symbolTable();
281 fVertexInclude = rehydrator.elements();
282 }
283 {
284 Rehydrator rehydrator(fContext.get(), fGpuSymbolTable, this, SKSL_INCLUDE_sksl_frag,
Brian Osmaneac49832020-09-18 11:49:22 -0400285 SKSL_INCLUDE_sksl_frag_LENGTH);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400286 fFragmentSymbolTable = rehydrator.symbolTable();
Brian Osman00a8b5b2020-10-02 09:06:04 -0400287 fragElements = rehydrator.elements();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400288 }
289#endif
John Stilesbc0c29e2020-09-28 13:13:40 -0400290 // Call counts are used to track dead-stripping and inlinability within the program being
291 // currently compiled, and always should start at zero for a new program. Zero out any call
292 // counts that were registered during the assembly of the intrinsics/include data. (If we
293 // actually use calls from inside the intrinsics, we will clone them into the program and they
294 // will get new call counts.)
Brian Osman00a8b5b2020-10-02 09:06:04 -0400295 reset_call_counts(&gpuElements);
John Stilesbc0c29e2020-09-28 13:13:40 -0400296 reset_call_counts(&fVertexInclude);
Brian Osman00a8b5b2020-10-02 09:06:04 -0400297 reset_call_counts(&fragElements);
John Stilesbc0c29e2020-09-28 13:13:40 -0400298
Brian Osman00a8b5b2020-10-02 09:06:04 -0400299 fGPUIntrinsics = std::make_unique<IRIntrinsicMap>(/*parent=*/nullptr);
300 grab_intrinsics(&gpuElements, fGPUIntrinsics.get());
301
302 fFragmentIntrinsics = std::make_unique<IRIntrinsicMap>(fGPUIntrinsics.get());
303 grab_intrinsics(&fragElements, fFragmentIntrinsics.get());
ethannicholasb3058bd2016-07-01 08:22:01 -0700304}
305
John Stiles656427a2020-08-27 15:26:26 -0400306Compiler::~Compiler() {}
ethannicholasb3058bd2016-07-01 08:22:01 -0700307
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400308void Compiler::loadGeometryIntrinsics() {
309 if (fGeometrySymbolTable) {
310 return;
311 }
Brian Osmandd496172020-08-08 08:17:18 -0400312 #if !SKSL_STANDALONE
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400313 {
314 Rehydrator rehydrator(fContext.get(), fGpuSymbolTable, this, SKSL_INCLUDE_sksl_geom,
315 SKSL_INCLUDE_sksl_geom_LENGTH);
316 fGeometrySymbolTable = rehydrator.symbolTable();
317 fGeometryInclude = rehydrator.elements();
318 }
319 #else
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -0400320 this->processIncludeFile(Program::kGeometry_Kind, SKSL_GEOM_INCLUDE, fGpuSymbolTable,
321 &fGeometryInclude, &fGeometrySymbolTable);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400322 #endif
323}
324
Brian Osman8e2ef022020-09-30 13:26:43 -0400325void Compiler::loadFPIntrinsics() {
326 if (fFPSymbolTable) {
327 return;
328 }
329 fFPIntrinsics = std::make_unique<IRIntrinsicMap>(fGPUIntrinsics.get());
330 std::vector<std::unique_ptr<ProgramElement>> fpElements;
331 #if !SKSL_STANDALONE
332 {
333 Rehydrator rehydrator(fContext.get(), fGpuSymbolTable, this, SKSL_INCLUDE_sksl_fp,
334 SKSL_INCLUDE_sksl_fp_LENGTH);
335 fFPSymbolTable = rehydrator.symbolTable();
336 fpElements = rehydrator.elements();
337 }
338 #else
339 this->processIncludeFile(Program::kFragmentProcessor_Kind, SKSL_FP_INCLUDE, fGpuSymbolTable,
340 &fpElements, &fFPSymbolTable);
341 #endif
342 grab_intrinsics(&fpElements, fFPIntrinsics.get());
Brian Osman8e2ef022020-09-30 13:26:43 -0400343}
344
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400345void Compiler::loadPipelineIntrinsics() {
346 if (fPipelineSymbolTable) {
347 return;
348 }
Brian Osman00a8b5b2020-10-02 09:06:04 -0400349 fPipelineIntrinsics = std::make_unique<IRIntrinsicMap>(fGPUIntrinsics.get());
Brian Osman8e2ef022020-09-30 13:26:43 -0400350 std::vector<std::unique_ptr<ProgramElement>> pipelineIntrinics;
Brian Osmandd496172020-08-08 08:17:18 -0400351 #if !SKSL_STANDALONE
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400352 {
353 Rehydrator rehydrator(fContext.get(), fGpuSymbolTable, this,
354 SKSL_INCLUDE_sksl_pipeline,
355 SKSL_INCLUDE_sksl_pipeline_LENGTH);
356 fPipelineSymbolTable = rehydrator.symbolTable();
Brian Osman8e2ef022020-09-30 13:26:43 -0400357 pipelineIntrinics = rehydrator.elements();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400358 }
359 #else
360 this->processIncludeFile(Program::kPipelineStage_Kind, SKSL_PIPELINE_INCLUDE,
Brian Osman8e2ef022020-09-30 13:26:43 -0400361 fGpuSymbolTable, &pipelineIntrinics, &fPipelineSymbolTable);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400362 #endif
Brian Osman8e2ef022020-09-30 13:26:43 -0400363 grab_intrinsics(&pipelineIntrinics, fPipelineIntrinsics.get());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400364}
365
366void Compiler::loadInterpreterIntrinsics() {
367 if (fInterpreterSymbolTable) {
368 return;
369 }
Brian Osman00a8b5b2020-10-02 09:06:04 -0400370 fInterpreterIntrinsics = std::make_unique<IRIntrinsicMap>(/*parent=*/nullptr);
371 std::vector<std::unique_ptr<ProgramElement>> interpElements;
Brian Osmandd496172020-08-08 08:17:18 -0400372 #if !SKSL_STANDALONE
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400373 {
Brian Osmaneac49832020-09-18 11:49:22 -0400374 Rehydrator rehydrator(fContext.get(), fRootSymbolTable, this,
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400375 SKSL_INCLUDE_sksl_interp,
376 SKSL_INCLUDE_sksl_interp_LENGTH);
377 fInterpreterSymbolTable = rehydrator.symbolTable();
Brian Osman00a8b5b2020-10-02 09:06:04 -0400378 interpElements = rehydrator.elements();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400379 }
380 #else
381 this->processIncludeFile(Program::kGeneric_Kind, SKSL_INTERP_INCLUDE,
Brian Osman00a8b5b2020-10-02 09:06:04 -0400382 fIRGenerator->fSymbolTable, &interpElements,
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -0400383 &fInterpreterSymbolTable);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400384 #endif
Brian Osman00a8b5b2020-10-02 09:06:04 -0400385 grab_intrinsics(&interpElements, fInterpreterIntrinsics.get());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400386}
387
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -0400388void Compiler::processIncludeFile(Program::Kind kind, const char* path,
Ethan Nicholas8da1e652019-05-24 11:01:59 -0400389 std::shared_ptr<SymbolTable> base,
390 std::vector<std::unique_ptr<ProgramElement>>* outElements,
391 std::shared_ptr<SymbolTable>* outSymbolTable) {
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -0400392 std::ifstream in(path);
Brian Osmane498b3c2020-09-23 14:42:11 -0400393 std::unique_ptr<String> text = std::make_unique<String>(std::istreambuf_iterator<char>(in),
394 std::istreambuf_iterator<char>());
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -0400395 if (in.rdstate()) {
396 printf("error reading %s\n", path);
397 abort();
398 }
Brian Osmane498b3c2020-09-23 14:42:11 -0400399 const String* source = fRootSymbolTable->takeOwnershipOfString(std::move(text));
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -0400400 fSource = source;
Ethan Nicholas8da1e652019-05-24 11:01:59 -0400401 Program::Settings settings;
Ethan Nicholasa11035b2019-11-26 16:27:47 -0500402#if !defined(SKSL_STANDALONE) & SK_SUPPORT_GPU
403 GrContextOptions opts;
404 GrShaderCaps caps(opts);
405 settings.fCaps = &caps;
406#endif
John Stiles881a10c2020-09-19 10:13:24 -0400407 SkASSERT(fIRGenerator->fCanInline);
408 fIRGenerator->fCanInline = false;
Brian Osmane498b3c2020-09-23 14:42:11 -0400409 fIRGenerator->start(&settings, base ? base : fRootSymbolTable, nullptr, true);
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -0400410 fIRGenerator->convertProgram(kind, source->c_str(), source->length(), outElements);
John Stiles881a10c2020-09-19 10:13:24 -0400411 fIRGenerator->fCanInline = true;
Ethan Nicholas8da1e652019-05-24 11:01:59 -0400412 if (this->fErrorCount) {
413 printf("Unexpected errors: %s\n", this->fErrorText.c_str());
414 }
415 SkASSERT(!fErrorCount);
Ethan Nicholas8da1e652019-05-24 11:01:59 -0400416 *outSymbolTable = fIRGenerator->fSymbolTable;
Ethan Nicholasa11035b2019-11-26 16:27:47 -0500417#ifdef SK_DEBUG
418 fSource = nullptr;
419#endif
Brian Osmane498b3c2020-09-23 14:42:11 -0400420 fIRGenerator->finish();
Ethan Nicholas8da1e652019-05-24 11:01:59 -0400421}
422
ethannicholas22f939e2016-10-13 13:25:34 -0700423// add the definition created by assigning to the lvalue to the definition set
Ethan Nicholas86a43402017-01-19 13:32:00 -0500424void Compiler::addDefinition(const Expression* lvalue, std::unique_ptr<Expression>* expr,
425 DefinitionMap* definitions) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400426 switch (lvalue->kind()) {
427 case Expression::Kind::kVariableReference: {
Brian Osman79457ef2020-09-24 15:01:27 -0400428 const Variable& var = *lvalue->as<VariableReference>().fVariable;
ethannicholas22f939e2016-10-13 13:25:34 -0700429 if (var.fStorage == Variable::kLocal_Storage) {
430 (*definitions)[&var] = expr;
431 }
432 break;
433 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400434 case Expression::Kind::kSwizzle:
ethannicholas22f939e2016-10-13 13:25:34 -0700435 // We consider the variable written to as long as at least some of its components have
436 // been written to. This will lead to some false negatives (we won't catch it if you
437 // write to foo.x and then read foo.y), but being stricter could lead to false positives
Mike Klein6ad99092016-10-26 10:35:22 -0400438 // (we write to foo.x, and then pass foo to a function which happens to only read foo.x,
439 // 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 -0700440 // more complicated whole-program analysis. This is probably good enough.
John Stilesa5a97b42020-08-18 11:19:07 -0400441 this->addDefinition(lvalue->as<Swizzle>().fBase.get(),
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400442 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
ethannicholas22f939e2016-10-13 13:25:34 -0700443 definitions);
444 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400445 case Expression::Kind::kIndex:
ethannicholas22f939e2016-10-13 13:25:34 -0700446 // see comments in Swizzle
John Stilesa5a97b42020-08-18 11:19:07 -0400447 this->addDefinition(lvalue->as<IndexExpression>().fBase.get(),
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400448 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
ethannicholas22f939e2016-10-13 13:25:34 -0700449 definitions);
450 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400451 case Expression::Kind::kFieldAccess:
ethannicholas22f939e2016-10-13 13:25:34 -0700452 // see comments in Swizzle
John Stilesa5a97b42020-08-18 11:19:07 -0400453 this->addDefinition(lvalue->as<FieldAccess>().fBase.get(),
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400454 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
ethannicholas22f939e2016-10-13 13:25:34 -0700455 definitions);
456 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400457 case Expression::Kind::kTernary:
Ethan Nicholasa583b812018-01-18 13:32:11 -0500458 // To simplify analysis, we just pretend that we write to both sides of the ternary.
459 // This allows for false positives (meaning we fail to detect that a variable might not
460 // have been assigned), but is preferable to false negatives.
John Stilesa5a97b42020-08-18 11:19:07 -0400461 this->addDefinition(lvalue->as<TernaryExpression>().fIfTrue.get(),
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400462 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
Ethan Nicholasa583b812018-01-18 13:32:11 -0500463 definitions);
John Stilesa5a97b42020-08-18 11:19:07 -0400464 this->addDefinition(lvalue->as<TernaryExpression>().fIfFalse.get(),
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400465 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
Ethan Nicholasa583b812018-01-18 13:32:11 -0500466 definitions);
467 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400468 case Expression::Kind::kExternalValue:
Ethan Nicholas91164d12019-05-15 15:29:54 -0400469 break;
ethannicholas22f939e2016-10-13 13:25:34 -0700470 default:
471 // not an lvalue, can't happen
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400472 SkASSERT(false);
ethannicholas22f939e2016-10-13 13:25:34 -0700473 }
474}
475
476// add local variables defined by this node to the set
Mike Klein6ad99092016-10-26 10:35:22 -0400477void Compiler::addDefinitions(const BasicBlock::Node& node,
Ethan Nicholas86a43402017-01-19 13:32:00 -0500478 DefinitionMap* definitions) {
John Stiles70025e52020-09-28 16:08:58 -0400479 if (node.isExpression()) {
480 Expression* expr = node.expression()->get();
481 switch (expr->kind()) {
482 case Expression::Kind::kBinary: {
483 BinaryExpression* b = &expr->as<BinaryExpression>();
484 if (b->getOperator() == Token::Kind::TK_EQ) {
485 this->addDefinition(&b->left(), &b->rightPointer(), definitions);
486 } else if (Compiler::IsAssignment(b->getOperator())) {
487 this->addDefinition(
488 &b->left(),
489 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
490 definitions);
Ethan Nicholas86a43402017-01-19 13:32:00 -0500491
ethannicholas22f939e2016-10-13 13:25:34 -0700492 }
John Stiles70025e52020-09-28 16:08:58 -0400493 break;
ethannicholas22f939e2016-10-13 13:25:34 -0700494 }
John Stiles70025e52020-09-28 16:08:58 -0400495 case Expression::Kind::kFunctionCall: {
496 const FunctionCall& c = expr->as<FunctionCall>();
497 for (size_t i = 0; i < c.fFunction.fParameters.size(); ++i) {
498 if (c.fFunction.fParameters[i]->fModifiers.fFlags & Modifiers::kOut_Flag) {
499 this->addDefinition(
500 c.fArguments[i].get(),
501 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
502 definitions);
503 }
504 }
505 break;
506 }
507 case Expression::Kind::kPrefix: {
508 const PrefixExpression* p = &expr->as<PrefixExpression>();
509 if (p->fOperator == Token::Kind::TK_MINUSMINUS ||
510 p->fOperator == Token::Kind::TK_PLUSPLUS) {
511 this->addDefinition(
512 p->fOperand.get(),
513 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
514 definitions);
515 }
516 break;
517 }
518 case Expression::Kind::kPostfix: {
519 const PostfixExpression* p = &expr->as<PostfixExpression>();
520 if (p->fOperator == Token::Kind::TK_MINUSMINUS ||
521 p->fOperator == Token::Kind::TK_PLUSPLUS) {
522 this->addDefinition(
523 p->fOperand.get(),
524 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
525 definitions);
526 }
527 break;
528 }
529 case Expression::Kind::kVariableReference: {
530 const VariableReference* v = &expr->as<VariableReference>();
531 if (v->fRefKind != VariableReference::kRead_RefKind) {
532 this->addDefinition(
533 v,
534 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
535 definitions);
536 }
537 break;
538 }
539 default:
540 break;
ethannicholas22f939e2016-10-13 13:25:34 -0700541 }
John Stiles70025e52020-09-28 16:08:58 -0400542 } else if (node.isStatement()) {
543 Statement* stmt = node.statement()->get();
544 if (stmt->is<VarDeclaration>()) {
545 VarDeclaration& vd = stmt->as<VarDeclaration>();
546 if (vd.fValue) {
547 (*definitions)[vd.fVar] = &vd.fValue;
ethannicholas22f939e2016-10-13 13:25:34 -0700548 }
ethannicholas22f939e2016-10-13 13:25:34 -0700549 }
550 }
551}
552
553void Compiler::scanCFG(CFG* cfg, BlockId blockId, std::set<BlockId>* workList) {
554 BasicBlock& block = cfg->fBlocks[blockId];
555
556 // compute definitions after this block
Ethan Nicholas86a43402017-01-19 13:32:00 -0500557 DefinitionMap after = block.fBefore;
ethannicholas22f939e2016-10-13 13:25:34 -0700558 for (const BasicBlock::Node& n : block.fNodes) {
559 this->addDefinitions(n, &after);
560 }
561
562 // propagate definitions to exits
563 for (BlockId exitId : block.fExits) {
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400564 if (exitId == blockId) {
565 continue;
566 }
ethannicholas22f939e2016-10-13 13:25:34 -0700567 BasicBlock& exit = cfg->fBlocks[exitId];
568 for (const auto& pair : after) {
Ethan Nicholas86a43402017-01-19 13:32:00 -0500569 std::unique_ptr<Expression>* e1 = pair.second;
570 auto found = exit.fBefore.find(pair.first);
571 if (found == exit.fBefore.end()) {
572 // exit has no definition for it, just copy it
573 workList->insert(exitId);
ethannicholas22f939e2016-10-13 13:25:34 -0700574 exit.fBefore[pair.first] = e1;
575 } else {
Ethan Nicholas86a43402017-01-19 13:32:00 -0500576 // exit has a (possibly different) value already defined
577 std::unique_ptr<Expression>* e2 = exit.fBefore[pair.first];
ethannicholas22f939e2016-10-13 13:25:34 -0700578 if (e1 != e2) {
579 // definition has changed, merge and add exit block to worklist
580 workList->insert(exitId);
Ethan Nicholasaf197692017-02-27 13:26:45 -0500581 if (e1 && e2) {
582 exit.fBefore[pair.first] =
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400583 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression;
Ethan Nicholasaf197692017-02-27 13:26:45 -0500584 } else {
585 exit.fBefore[pair.first] = nullptr;
586 }
ethannicholas22f939e2016-10-13 13:25:34 -0700587 }
588 }
589 }
590 }
591}
592
593// returns a map which maps all local variables in the function to null, indicating that their value
594// is initially unknown
Ethan Nicholas86a43402017-01-19 13:32:00 -0500595static DefinitionMap compute_start_state(const CFG& cfg) {
596 DefinitionMap result;
Mike Klein6ad99092016-10-26 10:35:22 -0400597 for (const auto& block : cfg.fBlocks) {
598 for (const auto& node : block.fNodes) {
John Stiles70025e52020-09-28 16:08:58 -0400599 if (node.isStatement()) {
Ethan Nicholascb670962017-04-20 19:31:52 -0400600 const Statement* s = node.statement()->get();
Brian Osman79457ef2020-09-24 15:01:27 -0400601 if (s->is<VarDeclarationsStatement>()) {
John Stilesa5a97b42020-08-18 11:19:07 -0400602 const VarDeclarationsStatement* vd = &s->as<VarDeclarationsStatement>();
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000603 for (const auto& decl : vd->fDeclaration->fVars) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400604 if (decl->kind() == Statement::Kind::kVarDeclaration) {
John Stilesa5a97b42020-08-18 11:19:07 -0400605 result[decl->as<VarDeclaration>().fVar] = nullptr;
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000606 }
Mike Klein6ad99092016-10-26 10:35:22 -0400607 }
ethannicholas22f939e2016-10-13 13:25:34 -0700608 }
609 }
610 }
611 }
612 return result;
613}
614
Ethan Nicholascb670962017-04-20 19:31:52 -0400615/**
616 * Returns true if assigning to this lvalue has no effect.
617 */
618static bool is_dead(const Expression& lvalue) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400619 switch (lvalue.kind()) {
620 case Expression::Kind::kVariableReference:
Brian Osman79457ef2020-09-24 15:01:27 -0400621 return lvalue.as<VariableReference>().fVariable->dead();
Ethan Nicholase6592142020-09-08 10:22:09 -0400622 case Expression::Kind::kSwizzle:
John Stilesa5a97b42020-08-18 11:19:07 -0400623 return is_dead(*lvalue.as<Swizzle>().fBase);
Ethan Nicholase6592142020-09-08 10:22:09 -0400624 case Expression::Kind::kFieldAccess:
John Stilesa5a97b42020-08-18 11:19:07 -0400625 return is_dead(*lvalue.as<FieldAccess>().fBase);
Ethan Nicholase6592142020-09-08 10:22:09 -0400626 case Expression::Kind::kIndex: {
John Stilesa5a97b42020-08-18 11:19:07 -0400627 const IndexExpression& idx = lvalue.as<IndexExpression>();
Ethan Nicholas2a099da2020-01-02 14:40:54 -0500628 return is_dead(*idx.fBase) &&
629 !idx.fIndex->hasProperty(Expression::Property::kSideEffects);
Ethan Nicholascb670962017-04-20 19:31:52 -0400630 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400631 case Expression::Kind::kTernary: {
John Stilesa5a97b42020-08-18 11:19:07 -0400632 const TernaryExpression& t = lvalue.as<TernaryExpression>();
Ethan Nicholasa583b812018-01-18 13:32:11 -0500633 return !t.fTest->hasSideEffects() && is_dead(*t.fIfTrue) && is_dead(*t.fIfFalse);
634 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400635 case Expression::Kind::kExternalValue:
Ethan Nicholas91164d12019-05-15 15:29:54 -0400636 return false;
Ethan Nicholascb670962017-04-20 19:31:52 -0400637 default:
Ethan Nicholas2a099da2020-01-02 14:40:54 -0500638#ifdef SK_DEBUG
Ethan Nicholascb670962017-04-20 19:31:52 -0400639 ABORT("invalid lvalue: %s\n", lvalue.description().c_str());
Ethan Nicholas2a099da2020-01-02 14:40:54 -0500640#endif
641 return false;
Ethan Nicholascb670962017-04-20 19:31:52 -0400642 }
643}
ethannicholas22f939e2016-10-13 13:25:34 -0700644
Ethan Nicholascb670962017-04-20 19:31:52 -0400645/**
646 * Returns true if this is an assignment which can be collapsed down to just the right hand side due
647 * to a dead target and lack of side effects on the left hand side.
648 */
649static bool dead_assignment(const BinaryExpression& b) {
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400650 if (!Compiler::IsAssignment(b.getOperator())) {
Ethan Nicholascb670962017-04-20 19:31:52 -0400651 return false;
652 }
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400653 return is_dead(b.left());
Ethan Nicholascb670962017-04-20 19:31:52 -0400654}
655
656void Compiler::computeDataFlow(CFG* cfg) {
657 cfg->fBlocks[cfg->fStart].fBefore = compute_start_state(*cfg);
ethannicholas22f939e2016-10-13 13:25:34 -0700658 std::set<BlockId> workList;
Ethan Nicholascb670962017-04-20 19:31:52 -0400659 for (BlockId i = 0; i < cfg->fBlocks.size(); i++) {
ethannicholas22f939e2016-10-13 13:25:34 -0700660 workList.insert(i);
661 }
662 while (workList.size()) {
663 BlockId next = *workList.begin();
664 workList.erase(workList.begin());
Ethan Nicholascb670962017-04-20 19:31:52 -0400665 this->scanCFG(cfg, next, &workList);
ethannicholas22f939e2016-10-13 13:25:34 -0700666 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400667}
668
669/**
670 * Attempts to replace the expression pointed to by iter with a new one (in both the CFG and the
671 * IR). If the expression can be cleanly removed, returns true and updates the iterator to point to
672 * the newly-inserted element. Otherwise updates only the IR and returns false (and the CFG will
673 * need to be regenerated).
674 */
John Stilesafbf8992020-08-18 10:08:21 -0400675static bool try_replace_expression(BasicBlock* b,
676 std::vector<BasicBlock::Node>::iterator* iter,
677 std::unique_ptr<Expression>* newExpression) {
Ethan Nicholascb670962017-04-20 19:31:52 -0400678 std::unique_ptr<Expression>* target = (*iter)->expression();
679 if (!b->tryRemoveExpression(iter)) {
680 *target = std::move(*newExpression);
681 return false;
682 }
683 *target = std::move(*newExpression);
684 return b->tryInsertExpression(iter, target);
685}
686
687/**
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400688 * Returns true if the expression is a constant numeric literal with the specified value, or a
689 * constant vector with all elements equal to the specified value.
Ethan Nicholascb670962017-04-20 19:31:52 -0400690 */
Ethan Nicholasa3f22f12020-10-01 12:13:17 -0400691template <typename T = SKSL_FLOAT>
John Stiles9d944232020-08-19 09:56:49 -0400692static bool is_constant(const Expression& expr, T value) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400693 switch (expr.kind()) {
694 case Expression::Kind::kIntLiteral:
Ethan Nicholase96cdd12020-09-28 16:27:18 -0400695 return expr.as<IntLiteral>().value() == value;
John Stiles9d944232020-08-19 09:56:49 -0400696
Ethan Nicholase6592142020-09-08 10:22:09 -0400697 case Expression::Kind::kFloatLiteral:
Ethan Nicholasa3f22f12020-10-01 12:13:17 -0400698 return expr.as<FloatLiteral>().value() == value;
John Stiles9d944232020-08-19 09:56:49 -0400699
Ethan Nicholase6592142020-09-08 10:22:09 -0400700 case Expression::Kind::kConstructor: {
John Stiles9d944232020-08-19 09:56:49 -0400701 const Constructor& constructor = expr.as<Constructor>();
702 if (constructor.isCompileTimeConstant()) {
Ethan Nicholas30d30222020-09-11 12:27:26 -0400703 const Type& constructorType = constructor.type();
704 bool isFloat = constructorType.columns() > 1
705 ? constructorType.componentType().isFloat()
706 : constructorType.isFloat();
707 switch (constructorType.typeKind()) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400708 case Type::TypeKind::kVector:
Ethan Nicholas30d30222020-09-11 12:27:26 -0400709 for (int i = 0; i < constructorType.columns(); ++i) {
John Stiles9d944232020-08-19 09:56:49 -0400710 if (isFloat) {
711 if (constructor.getFVecComponent(i) != value) {
712 return false;
713 }
714 } else {
715 if (constructor.getIVecComponent(i) != value) {
716 return false;
717 }
718 }
Ethan Nicholasd188c182019-06-10 15:55:38 -0400719 }
John Stiles9d944232020-08-19 09:56:49 -0400720 return true;
721
Ethan Nicholase6592142020-09-08 10:22:09 -0400722 case Type::TypeKind::kScalar:
Ethan Nicholasf70f0442020-09-29 12:41:35 -0400723 SkASSERT(constructor.arguments().size() == 1);
724 return is_constant<T>(*constructor.arguments()[0], value);
John Stiles9d944232020-08-19 09:56:49 -0400725
726 default:
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400727 return false;
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400728 }
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400729 }
730 return false;
731 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400732 default:
733 return false;
734 }
735}
736
737/**
738 * Collapses the binary expression pointed to by iter down to just the right side (in both the IR
739 * and CFG structures).
740 */
John Stilesafbf8992020-08-18 10:08:21 -0400741static void delete_left(BasicBlock* b,
742 std::vector<BasicBlock::Node>::iterator* iter,
743 bool* outUpdated,
744 bool* outNeedsRescan) {
Ethan Nicholascb670962017-04-20 19:31:52 -0400745 *outUpdated = true;
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400746 std::unique_ptr<Expression>* target = (*iter)->expression();
John Stilesa5a97b42020-08-18 11:19:07 -0400747 BinaryExpression& bin = (*target)->as<BinaryExpression>();
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400748 Expression& left = bin.left();
749 std::unique_ptr<Expression>& rightPointer = bin.rightPointer();
750 SkASSERT(!left.hasSideEffects());
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400751 bool result;
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400752 if (bin.getOperator() == Token::Kind::TK_EQ) {
753 result = b->tryRemoveLValueBefore(iter, &left);
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400754 } else {
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400755 result = b->tryRemoveExpressionBefore(iter, &left);
Ethan Nicholascb670962017-04-20 19:31:52 -0400756 }
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400757 *target = std::move(rightPointer);
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400758 if (!result) {
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400759 *outNeedsRescan = true;
760 return;
761 }
762 if (*iter == b->fNodes.begin()) {
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400763 *outNeedsRescan = true;
764 return;
765 }
766 --(*iter);
John Stiles70025e52020-09-28 16:08:58 -0400767 if (!(*iter)->isExpression() || (*iter)->expression() != &rightPointer) {
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400768 *outNeedsRescan = true;
769 return;
770 }
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400771 *iter = b->fNodes.erase(*iter);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400772 SkASSERT((*iter)->expression() == target);
Ethan Nicholascb670962017-04-20 19:31:52 -0400773}
774
775/**
776 * Collapses the binary expression pointed to by iter down to just the left side (in both the IR and
777 * CFG structures).
778 */
John Stilesafbf8992020-08-18 10:08:21 -0400779static void delete_right(BasicBlock* b,
780 std::vector<BasicBlock::Node>::iterator* iter,
781 bool* outUpdated,
782 bool* outNeedsRescan) {
Ethan Nicholascb670962017-04-20 19:31:52 -0400783 *outUpdated = true;
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400784 std::unique_ptr<Expression>* target = (*iter)->expression();
John Stilesa5a97b42020-08-18 11:19:07 -0400785 BinaryExpression& bin = (*target)->as<BinaryExpression>();
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400786 std::unique_ptr<Expression>& leftPointer = bin.leftPointer();
787 Expression& right = bin.right();
788 SkASSERT(!right.hasSideEffects());
789 if (!b->tryRemoveExpressionBefore(iter, &right)) {
790 *target = std::move(leftPointer);
Ethan Nicholascb670962017-04-20 19:31:52 -0400791 *outNeedsRescan = true;
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400792 return;
Ethan Nicholascb670962017-04-20 19:31:52 -0400793 }
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400794 *target = std::move(leftPointer);
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400795 if (*iter == b->fNodes.begin()) {
796 *outNeedsRescan = true;
797 return;
798 }
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400799 --(*iter);
John Stiles70025e52020-09-28 16:08:58 -0400800 if ((!(*iter)->isExpression() || (*iter)->expression() != &leftPointer)) {
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400801 *outNeedsRescan = true;
802 return;
803 }
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400804 *iter = b->fNodes.erase(*iter);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400805 SkASSERT((*iter)->expression() == target);
Ethan Nicholascb670962017-04-20 19:31:52 -0400806}
807
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400808/**
809 * Constructs the specified type using a single argument.
810 */
Ethan Nicholas30d30222020-09-11 12:27:26 -0400811static std::unique_ptr<Expression> construct(const Type* type, std::unique_ptr<Expression> v) {
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400812 std::vector<std::unique_ptr<Expression>> args;
813 args.push_back(std::move(v));
Ethan Nicholase6592142020-09-08 10:22:09 -0400814 std::unique_ptr<Expression> result = std::make_unique<Constructor>(-1, type, std::move(args));
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400815 return result;
816}
817
818/**
819 * Used in the implementations of vectorize_left and vectorize_right. Given a vector type and an
820 * expression x, deletes the expression pointed to by iter and replaces it with <type>(x).
821 */
822static void vectorize(BasicBlock* b,
823 std::vector<BasicBlock::Node>::iterator* iter,
824 const Type& type,
825 std::unique_ptr<Expression>* otherExpression,
826 bool* outUpdated,
827 bool* outNeedsRescan) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400828 SkASSERT((*(*iter)->expression())->kind() == Expression::Kind::kBinary);
829 SkASSERT(type.typeKind() == Type::TypeKind::kVector);
Ethan Nicholas30d30222020-09-11 12:27:26 -0400830 SkASSERT((*otherExpression)->type().typeKind() == Type::TypeKind::kScalar);
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400831 *outUpdated = true;
832 std::unique_ptr<Expression>* target = (*iter)->expression();
833 if (!b->tryRemoveExpression(iter)) {
Ethan Nicholas30d30222020-09-11 12:27:26 -0400834 *target = construct(&type, std::move(*otherExpression));
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400835 *outNeedsRescan = true;
836 } else {
Ethan Nicholas30d30222020-09-11 12:27:26 -0400837 *target = construct(&type, std::move(*otherExpression));
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400838 if (!b->tryInsertExpression(iter, target)) {
839 *outNeedsRescan = true;
840 }
841 }
842}
843
844/**
845 * Given a binary expression of the form x <op> vec<n>(y), deletes the right side and vectorizes the
846 * left to yield vec<n>(x).
847 */
848static void vectorize_left(BasicBlock* b,
849 std::vector<BasicBlock::Node>::iterator* iter,
850 bool* outUpdated,
851 bool* outNeedsRescan) {
John Stilesa5a97b42020-08-18 11:19:07 -0400852 BinaryExpression& bin = (*(*iter)->expression())->as<BinaryExpression>();
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400853 vectorize(b, iter, bin.right().type(), &bin.leftPointer(), outUpdated, outNeedsRescan);
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400854}
855
856/**
857 * Given a binary expression of the form vec<n>(x) <op> y, deletes the left side and vectorizes the
858 * right to yield vec<n>(y).
859 */
860static void vectorize_right(BasicBlock* b,
861 std::vector<BasicBlock::Node>::iterator* iter,
862 bool* outUpdated,
863 bool* outNeedsRescan) {
John Stilesa5a97b42020-08-18 11:19:07 -0400864 BinaryExpression& bin = (*(*iter)->expression())->as<BinaryExpression>();
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400865 vectorize(b, iter, bin.left().type(), &bin.rightPointer(), outUpdated, outNeedsRescan);
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400866}
867
868// Mark that an expression which we were writing to is no longer being written to
John Stilesa5a97b42020-08-18 11:19:07 -0400869static void clear_write(Expression& expr) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400870 switch (expr.kind()) {
871 case Expression::Kind::kVariableReference: {
John Stilesa5a97b42020-08-18 11:19:07 -0400872 expr.as<VariableReference>().setRefKind(VariableReference::kRead_RefKind);
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400873 break;
874 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400875 case Expression::Kind::kFieldAccess:
John Stilesa5a97b42020-08-18 11:19:07 -0400876 clear_write(*expr.as<FieldAccess>().fBase);
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400877 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400878 case Expression::Kind::kSwizzle:
John Stilesa5a97b42020-08-18 11:19:07 -0400879 clear_write(*expr.as<Swizzle>().fBase);
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400880 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400881 case Expression::Kind::kIndex:
John Stilesa5a97b42020-08-18 11:19:07 -0400882 clear_write(*expr.as<IndexExpression>().fBase);
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400883 break;
884 default:
885 ABORT("shouldn't be writing to this kind of expression\n");
886 break;
887 }
888}
889
Ethan Nicholascb670962017-04-20 19:31:52 -0400890void Compiler::simplifyExpression(DefinitionMap& definitions,
891 BasicBlock& b,
892 std::vector<BasicBlock::Node>::iterator* iter,
893 std::unordered_set<const Variable*>* undefinedVariables,
894 bool* outUpdated,
895 bool* outNeedsRescan) {
896 Expression* expr = (*iter)->expression()->get();
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400897 SkASSERT(expr);
Ethan Nicholascb670962017-04-20 19:31:52 -0400898 if ((*iter)->fConstantPropagation) {
899 std::unique_ptr<Expression> optimized = expr->constantPropagate(*fIRGenerator, definitions);
900 if (optimized) {
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400901 *outUpdated = true;
Ethan Nicholas30d30222020-09-11 12:27:26 -0400902 optimized = fIRGenerator->coerce(std::move(optimized), expr->type());
Ethan Nicholas1d881522020-09-11 09:32:54 -0400903 SkASSERT(optimized);
Ethan Nicholascb670962017-04-20 19:31:52 -0400904 if (!try_replace_expression(&b, iter, &optimized)) {
905 *outNeedsRescan = true;
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400906 return;
Ethan Nicholascb670962017-04-20 19:31:52 -0400907 }
John Stiles70025e52020-09-28 16:08:58 -0400908 SkASSERT((*iter)->isExpression());
Ethan Nicholascb670962017-04-20 19:31:52 -0400909 expr = (*iter)->expression()->get();
Ethan Nicholascb670962017-04-20 19:31:52 -0400910 }
911 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400912 switch (expr->kind()) {
913 case Expression::Kind::kVariableReference: {
John Stilesa5a97b42020-08-18 11:19:07 -0400914 const VariableReference& ref = expr->as<VariableReference>();
Brian Osman79457ef2020-09-24 15:01:27 -0400915 const Variable* var = ref.fVariable;
Ethan Nicholas8f7e28f2018-03-26 14:24:27 -0400916 if (ref.refKind() != VariableReference::kWrite_RefKind &&
917 ref.refKind() != VariableReference::kPointer_RefKind &&
Brian Osman79457ef2020-09-24 15:01:27 -0400918 var->fStorage == Variable::kLocal_Storage && !definitions[var] &&
919 (*undefinedVariables).find(var) == (*undefinedVariables).end()) {
920 (*undefinedVariables).insert(var);
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000921 this->error(expr->fOffset,
Brian Osman58384ad2020-10-02 22:15:22 +0000922 "'" + var->fName + "' has not been assigned");
Ethan Nicholascb670962017-04-20 19:31:52 -0400923 }
924 break;
925 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400926 case Expression::Kind::kTernary: {
John Stiles403a3632020-08-20 12:11:48 -0400927 TernaryExpression* t = &expr->as<TernaryExpression>();
Ethan Nicholase6592142020-09-08 10:22:09 -0400928 if (t->fTest->kind() == Expression::Kind::kBoolLiteral) {
Ethan Nicholascb670962017-04-20 19:31:52 -0400929 // ternary has a constant test, replace it with either the true or
930 // false branch
Ethan Nicholas59d660c2020-09-28 09:18:15 -0400931 if (t->fTest->as<BoolLiteral>().value()) {
Ethan Nicholascb670962017-04-20 19:31:52 -0400932 (*iter)->setExpression(std::move(t->fIfTrue));
933 } else {
934 (*iter)->setExpression(std::move(t->fIfFalse));
935 }
936 *outUpdated = true;
937 *outNeedsRescan = true;
938 }
939 break;
940 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400941 case Expression::Kind::kBinary: {
John Stiles403a3632020-08-20 12:11:48 -0400942 BinaryExpression* bin = &expr->as<BinaryExpression>();
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400943 if (dead_assignment(*bin)) {
944 delete_left(&b, iter, outUpdated, outNeedsRescan);
945 break;
946 }
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400947 Expression& left = bin->left();
948 Expression& right = bin->right();
949 const Type& leftType = left.type();
950 const Type& rightType = right.type();
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400951 // collapse useless expressions like x * 1 or x + 0
Ethan Nicholas30d30222020-09-11 12:27:26 -0400952 if (((leftType.typeKind() != Type::TypeKind::kScalar) &&
953 (leftType.typeKind() != Type::TypeKind::kVector)) ||
954 ((rightType.typeKind() != Type::TypeKind::kScalar) &&
955 (rightType.typeKind() != Type::TypeKind::kVector))) {
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400956 break;
957 }
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400958 switch (bin->getOperator()) {
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400959 case Token::Kind::TK_STAR:
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400960 if (is_constant(left, 1)) {
Ethan Nicholas30d30222020-09-11 12:27:26 -0400961 if (leftType.typeKind() == Type::TypeKind::kVector &&
962 rightType.typeKind() == Type::TypeKind::kScalar) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400963 // float4(1) * x -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400964 vectorize_right(&b, iter, outUpdated, outNeedsRescan);
965 } else {
966 // 1 * x -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400967 // 1 * float4(x) -> float4(x)
968 // float4(1) * float4(x) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400969 delete_left(&b, iter, outUpdated, outNeedsRescan);
970 }
971 }
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400972 else if (is_constant(left, 0)) {
Ethan Nicholas30d30222020-09-11 12:27:26 -0400973 if (leftType.typeKind() == Type::TypeKind::kScalar &&
974 rightType.typeKind() == Type::TypeKind::kVector &&
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400975 !right.hasSideEffects()) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400976 // 0 * float4(x) -> float4(0)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400977 vectorize_left(&b, iter, outUpdated, outNeedsRescan);
978 } else {
979 // 0 * x -> 0
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400980 // float4(0) * x -> float4(0)
981 // float4(0) * float4(x) -> float4(0)
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400982 if (!right.hasSideEffects()) {
Ethan Nicholas51493ee2017-12-11 12:34:33 -0500983 delete_right(&b, iter, outUpdated, outNeedsRescan);
984 }
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400985 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400986 }
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400987 else if (is_constant(right, 1)) {
Ethan Nicholas30d30222020-09-11 12:27:26 -0400988 if (leftType.typeKind() == Type::TypeKind::kScalar &&
989 rightType.typeKind() == Type::TypeKind::kVector) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400990 // x * float4(1) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400991 vectorize_left(&b, iter, outUpdated, outNeedsRescan);
992 } else {
993 // x * 1 -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400994 // float4(x) * 1 -> float4(x)
995 // float4(x) * float4(1) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400996 delete_right(&b, iter, outUpdated, outNeedsRescan);
997 }
998 }
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400999 else if (is_constant(right, 0)) {
Ethan Nicholas30d30222020-09-11 12:27:26 -04001000 if (leftType.typeKind() == Type::TypeKind::kVector &&
1001 rightType.typeKind() == Type::TypeKind::kScalar &&
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -04001002 !left.hasSideEffects()) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001003 // float4(x) * 0 -> float4(0)
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001004 vectorize_right(&b, iter, outUpdated, outNeedsRescan);
1005 } else {
1006 // x * 0 -> 0
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001007 // x * float4(0) -> float4(0)
1008 // float4(x) * float4(0) -> float4(0)
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -04001009 if (!left.hasSideEffects()) {
Ethan Nicholas51493ee2017-12-11 12:34:33 -05001010 delete_left(&b, iter, outUpdated, outNeedsRescan);
1011 }
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001012 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001013 }
1014 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001015 case Token::Kind::TK_PLUS:
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -04001016 if (is_constant(left, 0)) {
Ethan Nicholas30d30222020-09-11 12:27:26 -04001017 if (leftType.typeKind() == Type::TypeKind::kVector &&
1018 rightType.typeKind() == Type::TypeKind::kScalar) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001019 // float4(0) + x -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001020 vectorize_right(&b, iter, outUpdated, outNeedsRescan);
1021 } else {
1022 // 0 + x -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001023 // 0 + float4(x) -> float4(x)
1024 // float4(0) + float4(x) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001025 delete_left(&b, iter, outUpdated, outNeedsRescan);
1026 }
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -04001027 } else if (is_constant(right, 0)) {
Ethan Nicholas30d30222020-09-11 12:27:26 -04001028 if (leftType.typeKind() == Type::TypeKind::kScalar &&
1029 rightType.typeKind() == Type::TypeKind::kVector) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001030 // x + float4(0) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001031 vectorize_left(&b, iter, outUpdated, outNeedsRescan);
1032 } else {
1033 // x + 0 -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001034 // float4(x) + 0 -> float4(x)
1035 // float4(x) + float4(0) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001036 delete_right(&b, iter, outUpdated, outNeedsRescan);
1037 }
Ethan Nicholas56e42712017-04-21 10:23:37 -04001038 }
1039 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001040 case Token::Kind::TK_MINUS:
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -04001041 if (is_constant(right, 0)) {
Ethan Nicholas30d30222020-09-11 12:27:26 -04001042 if (leftType.typeKind() == Type::TypeKind::kScalar &&
1043 rightType.typeKind() == Type::TypeKind::kVector) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001044 // x - float4(0) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001045 vectorize_left(&b, iter, outUpdated, outNeedsRescan);
1046 } else {
1047 // x - 0 -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001048 // float4(x) - 0 -> float4(x)
1049 // float4(x) - float4(0) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001050 delete_right(&b, iter, outUpdated, outNeedsRescan);
1051 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001052 }
1053 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001054 case Token::Kind::TK_SLASH:
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -04001055 if (is_constant(right, 1)) {
Ethan Nicholas30d30222020-09-11 12:27:26 -04001056 if (leftType.typeKind() == Type::TypeKind::kScalar &&
1057 rightType.typeKind() == Type::TypeKind::kVector) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001058 // x / float4(1) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001059 vectorize_left(&b, iter, outUpdated, outNeedsRescan);
1060 } else {
1061 // x / 1 -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001062 // float4(x) / 1 -> float4(x)
1063 // float4(x) / float4(1) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001064 delete_right(&b, iter, outUpdated, outNeedsRescan);
1065 }
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -04001066 } else if (is_constant(left, 0)) {
Ethan Nicholas30d30222020-09-11 12:27:26 -04001067 if (leftType.typeKind() == Type::TypeKind::kScalar &&
1068 rightType.typeKind() == Type::TypeKind::kVector &&
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -04001069 !right.hasSideEffects()) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001070 // 0 / float4(x) -> float4(0)
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001071 vectorize_left(&b, iter, outUpdated, outNeedsRescan);
1072 } else {
1073 // 0 / x -> 0
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001074 // float4(0) / x -> float4(0)
1075 // float4(0) / float4(x) -> float4(0)
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -04001076 if (!right.hasSideEffects()) {
Ethan Nicholas51493ee2017-12-11 12:34:33 -05001077 delete_right(&b, iter, outUpdated, outNeedsRescan);
1078 }
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001079 }
1080 }
1081 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001082 case Token::Kind::TK_PLUSEQ:
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -04001083 if (is_constant(right, 0)) {
1084 clear_write(left);
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001085 delete_right(&b, iter, outUpdated, outNeedsRescan);
1086 }
1087 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001088 case Token::Kind::TK_MINUSEQ:
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -04001089 if (is_constant(right, 0)) {
1090 clear_write(left);
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001091 delete_right(&b, iter, outUpdated, outNeedsRescan);
1092 }
1093 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001094 case Token::Kind::TK_STAREQ:
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -04001095 if (is_constant(right, 1)) {
1096 clear_write(left);
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001097 delete_right(&b, iter, outUpdated, outNeedsRescan);
1098 }
1099 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001100 case Token::Kind::TK_SLASHEQ:
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -04001101 if (is_constant(right, 1)) {
1102 clear_write(left);
Ethan Nicholascb670962017-04-20 19:31:52 -04001103 delete_right(&b, iter, outUpdated, outNeedsRescan);
1104 }
1105 break;
1106 default:
1107 break;
1108 }
Ethan Nicholas409f6f02019-09-17 12:34:39 -04001109 break;
1110 }
Ethan Nicholase6592142020-09-08 10:22:09 -04001111 case Expression::Kind::kSwizzle: {
John Stiles403a3632020-08-20 12:11:48 -04001112 Swizzle& s = expr->as<Swizzle>();
Ethan Nicholas409f6f02019-09-17 12:34:39 -04001113 // detect identity swizzles like foo.rgba
Ethan Nicholas30d30222020-09-11 12:27:26 -04001114 if ((int) s.fComponents.size() == s.fBase->type().columns()) {
Ethan Nicholas409f6f02019-09-17 12:34:39 -04001115 bool identity = true;
1116 for (int i = 0; i < (int) s.fComponents.size(); ++i) {
1117 if (s.fComponents[i] != i) {
1118 identity = false;
1119 break;
1120 }
1121 }
1122 if (identity) {
1123 *outUpdated = true;
1124 if (!try_replace_expression(&b, iter, &s.fBase)) {
1125 *outNeedsRescan = true;
1126 return;
1127 }
John Stiles70025e52020-09-28 16:08:58 -04001128 SkASSERT((*iter)->isExpression());
Ethan Nicholas409f6f02019-09-17 12:34:39 -04001129 break;
1130 }
1131 }
1132 // detect swizzles of swizzles, e.g. replace foo.argb.r000 with foo.a000
Ethan Nicholase6592142020-09-08 10:22:09 -04001133 if (s.fBase->kind() == Expression::Kind::kSwizzle) {
John Stilesa5a97b42020-08-18 11:19:07 -04001134 Swizzle& base = s.fBase->as<Swizzle>();
Ethan Nicholas409f6f02019-09-17 12:34:39 -04001135 std::vector<int> final;
1136 for (int c : s.fComponents) {
Brian Osman25647672020-09-15 15:16:56 -04001137 final.push_back(base.fComponents[c]);
Ethan Nicholas409f6f02019-09-17 12:34:39 -04001138 }
1139 *outUpdated = true;
1140 std::unique_ptr<Expression> replacement(new Swizzle(*fContext, base.fBase->clone(),
1141 std::move(final)));
1142 if (!try_replace_expression(&b, iter, &replacement)) {
1143 *outNeedsRescan = true;
1144 return;
1145 }
John Stiles70025e52020-09-28 16:08:58 -04001146 SkASSERT((*iter)->isExpression());
Ethan Nicholas409f6f02019-09-17 12:34:39 -04001147 }
John Stiles30212b72020-06-11 17:55:07 -04001148 break;
Ethan Nicholascb670962017-04-20 19:31:52 -04001149 }
1150 default:
1151 break;
1152 }
1153}
1154
John Stiles92219b42020-06-15 12:32:24 -04001155// Returns true if this statement could potentially execute a break at the current level. We ignore
1156// nested loops and switches, since any breaks inside of them will merely break the loop / switch.
John Stilesb92641c2020-08-31 18:09:01 -04001157static bool contains_conditional_break(Statement& stmt) {
1158 class ContainsConditionalBreak : public ProgramVisitor {
1159 public:
1160 bool visitStatement(const Statement& stmt) override {
Ethan Nicholase6592142020-09-08 10:22:09 -04001161 switch (stmt.kind()) {
1162 case Statement::Kind::kBlock:
John Stilesb92641c2020-08-31 18:09:01 -04001163 return this->INHERITED::visitStatement(stmt);
1164
Ethan Nicholase6592142020-09-08 10:22:09 -04001165 case Statement::Kind::kBreak:
John Stilesb92641c2020-08-31 18:09:01 -04001166 return fInConditional > 0;
1167
Ethan Nicholase6592142020-09-08 10:22:09 -04001168 case Statement::Kind::kIf: {
John Stilesb92641c2020-08-31 18:09:01 -04001169 ++fInConditional;
1170 bool result = this->INHERITED::visitStatement(stmt);
1171 --fInConditional;
1172 return result;
1173 }
1174
1175 default:
1176 return false;
1177 }
1178 }
1179
1180 int fInConditional = 0;
1181 using INHERITED = ProgramVisitor;
1182 };
1183
1184 return ContainsConditionalBreak{}.visitStatement(stmt);
John Stiles92219b42020-06-15 12:32:24 -04001185}
1186
Ethan Nicholas5005a222018-08-24 13:06:27 -04001187// returns true if this statement definitely executes a break at the current level (we ignore
1188// nested loops and switches, since any breaks inside of them will merely break the loop / switch)
John Stilesb92641c2020-08-31 18:09:01 -04001189static bool contains_unconditional_break(Statement& stmt) {
1190 class ContainsUnconditionalBreak : public ProgramVisitor {
1191 public:
1192 bool visitStatement(const Statement& stmt) override {
Ethan Nicholase6592142020-09-08 10:22:09 -04001193 switch (stmt.kind()) {
1194 case Statement::Kind::kBlock:
John Stilesb92641c2020-08-31 18:09:01 -04001195 return this->INHERITED::visitStatement(stmt);
1196
Ethan Nicholase6592142020-09-08 10:22:09 -04001197 case Statement::Kind::kBreak:
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001198 return true;
John Stilesb92641c2020-08-31 18:09:01 -04001199
1200 default:
1201 return false;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001202 }
John Stilesb92641c2020-08-31 18:09:01 -04001203 }
John Stiles92219b42020-06-15 12:32:24 -04001204
John Stilesb92641c2020-08-31 18:09:01 -04001205 using INHERITED = ProgramVisitor;
1206 };
John Stiles92219b42020-06-15 12:32:24 -04001207
John Stilesb92641c2020-08-31 18:09:01 -04001208 return ContainsUnconditionalBreak{}.visitStatement(stmt);
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001209}
1210
John Stiles92219b42020-06-15 12:32:24 -04001211static void move_all_but_break(std::unique_ptr<Statement>& stmt,
1212 std::vector<std::unique_ptr<Statement>>* target) {
Ethan Nicholase6592142020-09-08 10:22:09 -04001213 switch (stmt->kind()) {
1214 case Statement::Kind::kBlock: {
John Stiles92219b42020-06-15 12:32:24 -04001215 // Recurse into the block.
1216 Block& block = static_cast<Block&>(*stmt);
1217
1218 std::vector<std::unique_ptr<Statement>> blockStmts;
Ethan Nicholas7bd60432020-09-25 14:31:59 -04001219 blockStmts.reserve(block.children().size());
1220 for (std::unique_ptr<Statement>& stmt : block.children()) {
1221 move_all_but_break(stmt, &blockStmts);
Ethan Nicholas739e1ca2020-06-11 12:16:14 -04001222 }
John Stiles92219b42020-06-15 12:32:24 -04001223
1224 target->push_back(std::make_unique<Block>(block.fOffset, std::move(blockStmts),
Ethan Nicholas7bd60432020-09-25 14:31:59 -04001225 block.symbolTable(), block.isScope()));
Ethan Nicholas739e1ca2020-06-11 12:16:14 -04001226 break;
John Stiles92219b42020-06-15 12:32:24 -04001227 }
1228
Ethan Nicholase6592142020-09-08 10:22:09 -04001229 case Statement::Kind::kBreak:
John Stiles92219b42020-06-15 12:32:24 -04001230 // Do not append a break to the target.
1231 break;
1232
Ethan Nicholas739e1ca2020-06-11 12:16:14 -04001233 default:
John Stiles92219b42020-06-15 12:32:24 -04001234 // Append normal statements to the target.
1235 target->push_back(std::move(stmt));
1236 break;
Ethan Nicholas739e1ca2020-06-11 12:16:14 -04001237 }
1238}
1239
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001240// Returns a block containing all of the statements that will be run if the given case matches
1241// (which, owing to the statements being owned by unique_ptrs, means the switch itself will be
1242// broken by this call and must then be discarded).
1243// Returns null (and leaves the switch unmodified) if no such simple reduction is possible, such as
1244// when break statements appear inside conditionals.
John Stiles92219b42020-06-15 12:32:24 -04001245static std::unique_ptr<Statement> block_for_case(SwitchStatement* switchStatement,
1246 SwitchCase* caseToCapture) {
1247 // We have to be careful to not move any of the pointers until after we're sure we're going to
1248 // succeed, so before we make any changes at all, we check the switch-cases to decide on a plan
1249 // of action. First, find the switch-case we are interested in.
1250 auto iter = switchStatement->fCases.begin();
1251 for (; iter != switchStatement->fCases.end(); ++iter) {
1252 if (iter->get() == caseToCapture) {
1253 break;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001254 }
John Stiles92219b42020-06-15 12:32:24 -04001255 }
1256
1257 // Next, walk forward through the rest of the switch. If we find a conditional break, we're
1258 // stuck and can't simplify at all. If we find an unconditional break, we have a range of
1259 // statements that we can use for simplification.
1260 auto startIter = iter;
1261 Statement* unconditionalBreakStmt = nullptr;
1262 for (; iter != switchStatement->fCases.end(); ++iter) {
1263 for (std::unique_ptr<Statement>& stmt : (*iter)->fStatements) {
1264 if (contains_conditional_break(*stmt)) {
1265 // We can't reduce switch-cases to a block when they have conditional breaks.
1266 return nullptr;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001267 }
John Stiles92219b42020-06-15 12:32:24 -04001268
1269 if (contains_unconditional_break(*stmt)) {
1270 // We found an unconditional break. We can use this block, but we need to strip
1271 // out the break statement.
1272 unconditionalBreakStmt = stmt.get();
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001273 break;
1274 }
1275 }
John Stiles92219b42020-06-15 12:32:24 -04001276
1277 if (unconditionalBreakStmt != nullptr) {
1278 break;
1279 }
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001280 }
John Stiles92219b42020-06-15 12:32:24 -04001281
1282 // We fell off the bottom of the switch or encountered a break. We know the range of statements
1283 // that we need to move over, and we know it's safe to do so.
1284 std::vector<std::unique_ptr<Statement>> caseStmts;
1285
1286 // We can move over most of the statements as-is.
1287 while (startIter != iter) {
1288 for (std::unique_ptr<Statement>& stmt : (*startIter)->fStatements) {
1289 caseStmts.push_back(std::move(stmt));
1290 }
1291 ++startIter;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001292 }
John Stiles92219b42020-06-15 12:32:24 -04001293
1294 // If we found an unconditional break at the end, we need to move what we can while avoiding
1295 // that break.
1296 if (unconditionalBreakStmt != nullptr) {
1297 for (std::unique_ptr<Statement>& stmt : (*startIter)->fStatements) {
1298 if (stmt.get() == unconditionalBreakStmt) {
1299 move_all_but_break(stmt, &caseStmts);
1300 unconditionalBreakStmt = nullptr;
1301 break;
1302 }
1303
1304 caseStmts.push_back(std::move(stmt));
1305 }
1306 }
1307
1308 SkASSERT(unconditionalBreakStmt == nullptr); // Verify that we fixed the unconditional break.
1309
1310 // Return our newly-synthesized block.
1311 return std::make_unique<Block>(/*offset=*/-1, std::move(caseStmts), switchStatement->fSymbols);
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001312}
1313
Ethan Nicholascb670962017-04-20 19:31:52 -04001314void Compiler::simplifyStatement(DefinitionMap& definitions,
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001315 BasicBlock& b,
1316 std::vector<BasicBlock::Node>::iterator* iter,
1317 std::unordered_set<const Variable*>* undefinedVariables,
1318 bool* outUpdated,
1319 bool* outNeedsRescan) {
Ethan Nicholascb670962017-04-20 19:31:52 -04001320 Statement* stmt = (*iter)->statement()->get();
Ethan Nicholase6592142020-09-08 10:22:09 -04001321 switch (stmt->kind()) {
1322 case Statement::Kind::kVarDeclaration: {
John Stilesa5a97b42020-08-18 11:19:07 -04001323 const auto& varDecl = stmt->as<VarDeclaration>();
Ethan Nicholas82a62d22017-11-07 14:42:10 +00001324 if (varDecl.fVar->dead() &&
1325 (!varDecl.fValue ||
1326 !varDecl.fValue->hasSideEffects())) {
1327 if (varDecl.fValue) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001328 SkASSERT((*iter)->statement()->get() == stmt);
Ethan Nicholas82a62d22017-11-07 14:42:10 +00001329 if (!b.tryRemoveExpressionBefore(iter, varDecl.fValue.get())) {
1330 *outNeedsRescan = true;
Ethan Nicholascb670962017-04-20 19:31:52 -04001331 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001332 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001333 (*iter)->setStatement(std::unique_ptr<Statement>(new Nop()));
Ethan Nicholas82a62d22017-11-07 14:42:10 +00001334 *outUpdated = true;
Ethan Nicholascb670962017-04-20 19:31:52 -04001335 }
1336 break;
1337 }
Ethan Nicholase6592142020-09-08 10:22:09 -04001338 case Statement::Kind::kIf: {
John Stilesa5a97b42020-08-18 11:19:07 -04001339 IfStatement& i = stmt->as<IfStatement>();
Ethan Nicholase6592142020-09-08 10:22:09 -04001340 if (i.fTest->kind() == Expression::Kind::kBoolLiteral) {
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001341 // constant if, collapse down to a single branch
Ethan Nicholas59d660c2020-09-28 09:18:15 -04001342 if (i.fTest->as<BoolLiteral>().value()) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001343 SkASSERT(i.fIfTrue);
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001344 (*iter)->setStatement(std::move(i.fIfTrue));
1345 } else {
1346 if (i.fIfFalse) {
1347 (*iter)->setStatement(std::move(i.fIfFalse));
1348 } else {
1349 (*iter)->setStatement(std::unique_ptr<Statement>(new Nop()));
1350 }
1351 }
1352 *outUpdated = true;
1353 *outNeedsRescan = true;
1354 break;
1355 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001356 if (i.fIfFalse && i.fIfFalse->isEmpty()) {
1357 // else block doesn't do anything, remove it
1358 i.fIfFalse.reset();
1359 *outUpdated = true;
1360 *outNeedsRescan = true;
1361 }
1362 if (!i.fIfFalse && i.fIfTrue->isEmpty()) {
1363 // if block doesn't do anything, no else block
1364 if (i.fTest->hasSideEffects()) {
1365 // test has side effects, keep it
1366 (*iter)->setStatement(std::unique_ptr<Statement>(
1367 new ExpressionStatement(std::move(i.fTest))));
1368 } else {
1369 // no if, no else, no test side effects, kill the whole if
1370 // statement
1371 (*iter)->setStatement(std::unique_ptr<Statement>(new Nop()));
1372 }
1373 *outUpdated = true;
1374 *outNeedsRescan = true;
1375 }
1376 break;
1377 }
Ethan Nicholase6592142020-09-08 10:22:09 -04001378 case Statement::Kind::kSwitch: {
John Stilesa5a97b42020-08-18 11:19:07 -04001379 SwitchStatement& s = stmt->as<SwitchStatement>();
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001380 int64_t switchValue;
1381 if (fIRGenerator->getConstantInt(*s.fValue, &switchValue)) {
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001382 // switch is constant, replace it with the case that matches
1383 bool found = false;
1384 SwitchCase* defaultCase = nullptr;
John Stiles9d944232020-08-19 09:56:49 -04001385 for (const std::unique_ptr<SwitchCase>& c : s.fCases) {
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001386 if (!c->fValue) {
1387 defaultCase = c.get();
1388 continue;
1389 }
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001390 int64_t caseValue;
1391 SkAssertResult(fIRGenerator->getConstantInt(*c->fValue, &caseValue));
1392 if (caseValue == switchValue) {
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001393 std::unique_ptr<Statement> newBlock = block_for_case(&s, c.get());
1394 if (newBlock) {
1395 (*iter)->setStatement(std::move(newBlock));
John Stiles9d944232020-08-19 09:56:49 -04001396 found = true;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001397 break;
1398 } else {
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -04001399 if (s.fIsStatic && !(fFlags & kPermitInvalidStaticTests_Flag)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001400 this->error(s.fOffset,
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001401 "static switch contains non-static conditional break");
1402 s.fIsStatic = false;
1403 }
1404 return; // can't simplify
1405 }
1406 }
1407 }
1408 if (!found) {
1409 // no matching case. use default if it exists, or kill the whole thing
1410 if (defaultCase) {
1411 std::unique_ptr<Statement> newBlock = block_for_case(&s, defaultCase);
1412 if (newBlock) {
1413 (*iter)->setStatement(std::move(newBlock));
1414 } else {
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -04001415 if (s.fIsStatic && !(fFlags & kPermitInvalidStaticTests_Flag)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001416 this->error(s.fOffset,
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001417 "static switch contains non-static conditional break");
1418 s.fIsStatic = false;
1419 }
1420 return; // can't simplify
1421 }
1422 } else {
1423 (*iter)->setStatement(std::unique_ptr<Statement>(new Nop()));
1424 }
1425 }
1426 *outUpdated = true;
1427 *outNeedsRescan = true;
1428 }
1429 break;
1430 }
Ethan Nicholase6592142020-09-08 10:22:09 -04001431 case Statement::Kind::kExpression: {
John Stilesa5a97b42020-08-18 11:19:07 -04001432 ExpressionStatement& e = stmt->as<ExpressionStatement>();
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001433 SkASSERT((*iter)->statement()->get() == &e);
Ethan Nicholasd503a5a2020-09-30 09:29:55 -04001434 if (!e.expression()->hasSideEffects()) {
Ethan Nicholascb670962017-04-20 19:31:52 -04001435 // Expression statement with no side effects, kill it
Ethan Nicholasd503a5a2020-09-30 09:29:55 -04001436 if (!b.tryRemoveExpressionBefore(iter, e.expression().get())) {
Ethan Nicholascb670962017-04-20 19:31:52 -04001437 *outNeedsRescan = true;
1438 }
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001439 SkASSERT((*iter)->statement()->get() == stmt);
Ethan Nicholascb670962017-04-20 19:31:52 -04001440 (*iter)->setStatement(std::unique_ptr<Statement>(new Nop()));
1441 *outUpdated = true;
1442 }
1443 break;
1444 }
1445 default:
1446 break;
1447 }
1448}
1449
John Stiles0cc193a2020-09-09 09:39:34 -04001450bool Compiler::scanCFG(FunctionDefinition& f) {
1451 bool madeChanges = false;
1452
Ethan Nicholascb670962017-04-20 19:31:52 -04001453 CFG cfg = CFGGenerator().getCFG(f);
1454 this->computeDataFlow(&cfg);
ethannicholas22f939e2016-10-13 13:25:34 -07001455
1456 // check for unreachable code
1457 for (size_t i = 0; i < cfg.fBlocks.size(); i++) {
John Stiles0cc193a2020-09-09 09:39:34 -04001458 const BasicBlock& block = cfg.fBlocks[i];
John Stiles61e75e32020-10-01 15:42:37 -04001459 if (i != cfg.fStart && !block.fIsReachable && block.fNodes.size()) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001460 int offset;
John Stiles0cc193a2020-09-09 09:39:34 -04001461 const BasicBlock::Node& node = block.fNodes[0];
John Stiles70025e52020-09-28 16:08:58 -04001462 if (node.isStatement()) {
1463 offset = (*node.statement())->fOffset;
1464 } else {
1465 offset = (*node.expression())->fOffset;
1466 if ((*node.expression())->is<BoolLiteral>()) {
1467 // Function inlining can generate do { ... } while(false) loops which always
1468 // break, so the boolean condition is considered unreachable. Since not being
1469 // able to reach a literal is a non-issue in the first place, we don't report an
1470 // error in this case.
1471 continue;
1472 }
Ethan Nicholas86a43402017-01-19 13:32:00 -05001473 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001474 this->error(offset, String("unreachable"));
ethannicholas22f939e2016-10-13 13:25:34 -07001475 }
1476 }
1477 if (fErrorCount) {
John Stiles0cc193a2020-09-09 09:39:34 -04001478 return madeChanges;
ethannicholas22f939e2016-10-13 13:25:34 -07001479 }
1480
Ethan Nicholascb670962017-04-20 19:31:52 -04001481 // check for dead code & undefined variables, perform constant propagation
1482 std::unordered_set<const Variable*> undefinedVariables;
1483 bool updated;
1484 bool needsRescan = false;
1485 do {
1486 if (needsRescan) {
1487 cfg = CFGGenerator().getCFG(f);
1488 this->computeDataFlow(&cfg);
1489 needsRescan = false;
Ethan Nicholas113628d2017-02-02 16:11:39 -05001490 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001491
1492 updated = false;
Ethan Nicholas1de14812020-06-19 15:32:49 -04001493 bool first = true;
Ethan Nicholascb670962017-04-20 19:31:52 -04001494 for (BasicBlock& b : cfg.fBlocks) {
John Stiles61e75e32020-10-01 15:42:37 -04001495 if (!first && !b.fIsReachable) {
Ethan Nicholas1de14812020-06-19 15:32:49 -04001496 // Block was reachable before optimization, but has since become unreachable. In
1497 // addition to being dead code, it's broken - since control flow can't reach it, no
1498 // prior variable definitions can reach it, and therefore variables might look to
1499 // have not been properly assigned. Kill it.
Brian Osmandb16c482020-09-09 15:15:06 -04001500
1501 // We need to do this in two steps. For any variable declarations, the node list
1502 // will contain statement nodes for each VarDeclaration, and then a statement for
1503 // the VarDeclarationsStatement. When we replace the VDS with a Nop, we delete the
1504 // storage of the unique_ptr that the VD nodes are pointing to. So we remove those
1505 // from the node list entirely, first.
John Stiles70025e52020-09-28 16:08:58 -04001506 b.fNodes.erase(std::remove_if(b.fNodes.begin(), b.fNodes.end(),
1507 [](const BasicBlock::Node& node) {
1508 return node.isStatement() &&
1509 (*node.statement())->is<VarDeclaration>();
1510 }),
1511 b.fNodes.end());
Brian Osmandb16c482020-09-09 15:15:06 -04001512
1513 // Now replace any remaining statements in the block with Nops.
Ethan Nicholas1de14812020-06-19 15:32:49 -04001514 for (BasicBlock::Node& node : b.fNodes) {
John Stiles70025e52020-09-28 16:08:58 -04001515 if (node.isStatement() && !(*node.statement())->is<Nop>()) {
John Stiles0cc193a2020-09-09 09:39:34 -04001516 node.setStatement(std::make_unique<Nop>());
1517 madeChanges = true;
Ethan Nicholas1de14812020-06-19 15:32:49 -04001518 }
1519 }
1520 continue;
1521 }
1522 first = false;
Ethan Nicholascb670962017-04-20 19:31:52 -04001523 DefinitionMap definitions = b.fBefore;
1524
1525 for (auto iter = b.fNodes.begin(); iter != b.fNodes.end() && !needsRescan; ++iter) {
John Stiles70025e52020-09-28 16:08:58 -04001526 if (iter->isExpression()) {
Ethan Nicholascb670962017-04-20 19:31:52 -04001527 this->simplifyExpression(definitions, b, &iter, &undefinedVariables, &updated,
1528 &needsRescan);
1529 } else {
1530 this->simplifyStatement(definitions, b, &iter, &undefinedVariables, &updated,
John Stiles0cc193a2020-09-09 09:39:34 -04001531 &needsRescan);
Ethan Nicholascb670962017-04-20 19:31:52 -04001532 }
Ethan Nicholas4b330df2017-05-17 10:52:55 -04001533 if (needsRescan) {
1534 break;
1535 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001536 this->addDefinitions(*iter, &definitions);
1537 }
Brian Osman01a3eb42020-09-14 11:32:49 -04001538
1539 if (needsRescan) {
1540 break;
1541 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001542 }
John Stiles0cc193a2020-09-09 09:39:34 -04001543 madeChanges |= updated;
Ethan Nicholascb670962017-04-20 19:31:52 -04001544 } while (updated);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001545 SkASSERT(!needsRescan);
ethannicholas22f939e2016-10-13 13:25:34 -07001546
Ethan Nicholas91a10532017-06-22 11:24:38 -04001547 // verify static ifs & switches, clean up dead variable decls
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001548 for (BasicBlock& b : cfg.fBlocks) {
1549 DefinitionMap definitions = b.fBefore;
1550
Ethan Nicholas91a10532017-06-22 11:24:38 -04001551 for (auto iter = b.fNodes.begin(); iter != b.fNodes.end() && !needsRescan;) {
John Stiles70025e52020-09-28 16:08:58 -04001552 if (iter->isStatement()) {
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001553 const Statement& s = **iter->statement();
Ethan Nicholase6592142020-09-08 10:22:09 -04001554 switch (s.kind()) {
1555 case Statement::Kind::kIf:
John Stilesa5a97b42020-08-18 11:19:07 -04001556 if (s.as<IfStatement>().fIsStatic &&
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -04001557 !(fFlags & kPermitInvalidStaticTests_Flag)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001558 this->error(s.fOffset, "static if has non-static test");
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001559 }
Ethan Nicholas91a10532017-06-22 11:24:38 -04001560 ++iter;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001561 break;
Ethan Nicholase6592142020-09-08 10:22:09 -04001562 case Statement::Kind::kSwitch:
John Stilesa5a97b42020-08-18 11:19:07 -04001563 if (s.as<SwitchStatement>().fIsStatic &&
John Stiles0cc193a2020-09-09 09:39:34 -04001564 !(fFlags & kPermitInvalidStaticTests_Flag)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001565 this->error(s.fOffset, "static switch has non-static test");
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001566 }
Ethan Nicholas91a10532017-06-22 11:24:38 -04001567 ++iter;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001568 break;
Ethan Nicholase6592142020-09-08 10:22:09 -04001569 case Statement::Kind::kVarDeclarations: {
John Stilesa5a97b42020-08-18 11:19:07 -04001570 VarDeclarations& decls = *s.as<VarDeclarationsStatement>().fDeclaration;
John Stiles0cc193a2020-09-09 09:39:34 -04001571 decls.fVars.erase(
1572 std::remove_if(decls.fVars.begin(), decls.fVars.end(),
1573 [&](const std::unique_ptr<Statement>& var) {
1574 bool nop = var->is<Nop>();
1575 madeChanges |= nop;
1576 return nop;
1577 }),
1578 decls.fVars.end());
1579 if (decls.fVars.empty()) {
Ethan Nicholas82a62d22017-11-07 14:42:10 +00001580 iter = b.fNodes.erase(iter);
1581 } else {
1582 ++iter;
1583 }
1584 break;
1585 }
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001586 default:
Ethan Nicholas91a10532017-06-22 11:24:38 -04001587 ++iter;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001588 break;
1589 }
Ethan Nicholas91a10532017-06-22 11:24:38 -04001590 } else {
1591 ++iter;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001592 }
1593 }
1594 }
1595
ethannicholas22f939e2016-10-13 13:25:34 -07001596 // check for missing return
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001597 if (f.fDeclaration.fReturnType != *fContext->fVoid_Type) {
John Stiles61e75e32020-10-01 15:42:37 -04001598 if (cfg.fBlocks[cfg.fExit].fIsReachable) {
Brian Osman58384ad2020-10-02 22:15:22 +00001599 this->error(f.fOffset, String("function '" + String(f.fDeclaration.fName) +
Ethan Nicholasdb80f692019-11-22 14:06:12 -05001600 "' can exit without returning a value"));
ethannicholas22f939e2016-10-13 13:25:34 -07001601 }
1602 }
John Stiles0cc193a2020-09-09 09:39:34 -04001603
1604 return madeChanges;
ethannicholas22f939e2016-10-13 13:25:34 -07001605}
1606
Brian Osman32d53552020-09-23 13:55:20 -04001607std::unique_ptr<Program> Compiler::convertProgram(
1608 Program::Kind kind,
1609 String text,
1610 const Program::Settings& settings,
1611 const std::vector<std::unique_ptr<ExternalValue>>* externalValues) {
1612 SkASSERT(!externalValues || (kind == Program::kGeneric_Kind));
Ethan Nicholas91164d12019-05-15 15:29:54 -04001613
ethannicholasb3058bd2016-07-01 08:22:01 -07001614 fErrorText = "";
1615 fErrorCount = 0;
John Stiles7b463002020-08-31 17:29:21 -04001616 fInliner.reset(context(), settings);
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001617 std::vector<std::unique_ptr<ProgramElement>>* inherited;
ethannicholasd598f792016-07-25 10:08:54 -07001618 std::vector<std::unique_ptr<ProgramElement>> elements;
ethannicholasb3058bd2016-07-01 08:22:01 -07001619 switch (kind) {
1620 case Program::kVertex_Kind:
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001621 inherited = &fVertexInclude;
John Stiles810c8cf2020-08-26 19:46:27 -04001622 fIRGenerator->fIntrinsics = fGPUIntrinsics.get();
Brian Osmane498b3c2020-09-23 14:42:11 -04001623 fIRGenerator->start(&settings, fVertexSymbolTable, inherited);
ethannicholasb3058bd2016-07-01 08:22:01 -07001624 break;
1625 case Program::kFragment_Kind:
Brian Osman00a8b5b2020-10-02 09:06:04 -04001626 inherited = nullptr;
1627 fIRGenerator->fIntrinsics = fFragmentIntrinsics.get();
1628 fIRGenerator->start(&settings, fFragmentSymbolTable, /*inherited=*/nullptr);
ethannicholasb3058bd2016-07-01 08:22:01 -07001629 break;
Ethan Nicholas52cad152017-02-16 16:37:32 -05001630 case Program::kGeometry_Kind:
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001631 this->loadGeometryIntrinsics();
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001632 inherited = &fGeometryInclude;
John Stiles810c8cf2020-08-26 19:46:27 -04001633 fIRGenerator->fIntrinsics = fGPUIntrinsics.get();
Brian Osmane498b3c2020-09-23 14:42:11 -04001634 fIRGenerator->start(&settings, fGeometrySymbolTable, inherited);
Ethan Nicholas52cad152017-02-16 16:37:32 -05001635 break;
Brian Osman8e2ef022020-09-30 13:26:43 -04001636 case Program::kFragmentProcessor_Kind:
1637 this->loadFPIntrinsics();
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04001638 inherited = nullptr;
Brian Osman8e2ef022020-09-30 13:26:43 -04001639 fIRGenerator->fIntrinsics = fFPIntrinsics.get();
1640 fIRGenerator->start(&settings, fFPSymbolTable, /*inherited=*/nullptr);
Ethan Nicholas762466e2017-06-29 10:03:38 -04001641 break;
Ethan Nicholas8da1e652019-05-24 11:01:59 -04001642 case Program::kPipelineStage_Kind:
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001643 this->loadPipelineIntrinsics();
Brian Osman8e2ef022020-09-30 13:26:43 -04001644 inherited = nullptr;
1645 fIRGenerator->fIntrinsics = fPipelineIntrinsics.get();
1646 fIRGenerator->start(&settings, fPipelineSymbolTable, /*inherited=*/nullptr);
Ethan Nicholas8da1e652019-05-24 11:01:59 -04001647 break;
Ethan Nicholas746035a2019-04-23 13:31:09 -04001648 case Program::kGeneric_Kind:
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001649 this->loadInterpreterIntrinsics();
Brian Osmaneac49832020-09-18 11:49:22 -04001650 inherited = nullptr;
John Stiles810c8cf2020-08-26 19:46:27 -04001651 fIRGenerator->fIntrinsics = fInterpreterIntrinsics.get();
Brian Osmane498b3c2020-09-23 14:42:11 -04001652 fIRGenerator->start(&settings, fInterpreterSymbolTable, /*inherited=*/nullptr);
Ethan Nicholas0d997662019-04-08 09:46:01 -04001653 break;
ethannicholasb3058bd2016-07-01 08:22:01 -07001654 }
Brian Osman32d53552020-09-23 13:55:20 -04001655 if (externalValues) {
1656 // Add any external values to the symbol table. IRGenerator::start() has pushed a table, so
1657 // we're only making these visible to the current Program.
1658 for (const auto& ev : *externalValues) {
Brian Osman58384ad2020-10-02 22:15:22 +00001659 fIRGenerator->fSymbolTable->addWithoutOwnership(ev->fName, ev.get());
Brian Osman32d53552020-09-23 13:55:20 -04001660 }
1661 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001662 std::unique_ptr<String> textPtr(new String(std::move(text)));
1663 fSource = textPtr.get();
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001664 fIRGenerator->convertProgram(kind, textPtr->c_str(), textPtr->size(), &elements);
John Stilesfbd050b2020-08-03 13:21:46 -04001665 auto result = std::make_unique<Program>(kind,
1666 std::move(textPtr),
1667 settings,
1668 fContext,
1669 inherited,
1670 std::move(elements),
1671 fIRGenerator->fSymbolTable,
1672 fIRGenerator->fInputs);
Brian Osmane498b3c2020-09-23 14:42:11 -04001673 fIRGenerator->finish();
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001674 if (fErrorCount) {
1675 return nullptr;
1676 }
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001677 if (settings.fOptimize && !this->optimize(*result)) {
1678 return nullptr;
1679 }
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001680 return result;
1681}
1682
Ethan Nicholas00543112018-07-31 09:44:36 -04001683bool Compiler::optimize(Program& program) {
1684 SkASSERT(!fErrorCount);
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001685 fIRGenerator->fKind = program.fKind;
1686 fIRGenerator->fSettings = &program.fSettings;
John Stiles7954d6c2020-09-01 10:53:02 -04001687
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001688 while (fErrorCount == 0) {
1689 bool madeChanges = false;
John Stiles7954d6c2020-09-01 10:53:02 -04001690
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001691 // Scan and optimize based on the control-flow graph for each function.
1692 for (ProgramElement& element : program) {
1693 if (element.is<FunctionDefinition>()) {
1694 madeChanges |= this->scanCFG(element.as<FunctionDefinition>());
1695 }
1696 }
1697
1698 // Perform inline-candidate analysis and inline any functions deemed suitable.
John Stiles881a10c2020-09-19 10:13:24 -04001699 madeChanges |= fInliner.analyze(program);
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001700
1701 // Remove dead functions. We wait until after analysis so that we still report errors,
1702 // even in unused code.
1703 if (program.fSettings.fRemoveDeadFunctions) {
1704 program.fElements.erase(
1705 std::remove_if(program.fElements.begin(),
1706 program.fElements.end(),
1707 [&](const std::unique_ptr<ProgramElement>& element) {
1708 if (!element->is<FunctionDefinition>()) {
1709 return false;
1710 }
1711 const auto& fn = element->as<FunctionDefinition>();
1712 bool dead = fn.fDeclaration.fCallCount == 0 &&
Brian Osman58384ad2020-10-02 22:15:22 +00001713 fn.fDeclaration.fName != "main";
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001714 madeChanges |= dead;
1715 return dead;
1716 }),
1717 program.fElements.end());
1718 }
1719
1720 if (program.fKind != Program::kFragmentProcessor_Kind) {
1721 // Remove dead variables.
John Stileseadfc3b2020-09-02 14:12:41 -04001722 for (ProgramElement& element : program) {
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001723 if (!element.is<VarDeclarations>()) {
1724 continue;
Ethan Nicholas0dc80872019-02-08 15:46:24 -05001725 }
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001726 VarDeclarations& vars = element.as<VarDeclarations>();
1727 vars.fVars.erase(
1728 std::remove_if(vars.fVars.begin(), vars.fVars.end(),
1729 [&](const std::unique_ptr<Statement>& stmt) {
1730 bool dead = stmt->as<VarDeclaration>().fVar->dead();
John Stiles73a6bff2020-09-09 13:40:37 -04001731 madeChanges |= dead;
1732 return dead;
1733 }),
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001734 vars.fVars.end());
John Stiles73a6bff2020-09-09 13:40:37 -04001735 }
1736
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001737 // Remove empty variable declarations with no variables left inside of them.
1738 program.fElements.erase(
1739 std::remove_if(program.fElements.begin(), program.fElements.end(),
1740 [&](const std::unique_ptr<ProgramElement>& element) {
1741 if (!element->is<VarDeclarations>()) {
1742 return false;
1743 }
1744 bool dead = element->as<VarDeclarations>().fVars.empty();
1745 madeChanges |= dead;
1746 return dead;
1747 }),
1748 program.fElements.end());
1749 }
John Stiles73a6bff2020-09-09 13:40:37 -04001750
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001751 if (!madeChanges) {
1752 break;
Ethan Nicholas0dc80872019-02-08 15:46:24 -05001753 }
Ethan Nicholas00543112018-07-31 09:44:36 -04001754 }
1755 return fErrorCount == 0;
1756}
1757
Brian Osmanfb32ddf2019-06-18 10:14:20 -04001758#if defined(SKSL_STANDALONE) || SK_SUPPORT_GPU
1759
Ethan Nicholas00543112018-07-31 09:44:36 -04001760bool Compiler::toSPIRV(Program& program, OutputStream& out) {
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001761#ifdef SK_ENABLE_SPIRV_VALIDATION
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001762 StringStream buffer;
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001763 fSource = program.fSource.get();
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001764 SPIRVCodeGenerator cg(fContext.get(), &program, this, &buffer);
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001765 bool result = cg.generateCode();
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001766 fSource = nullptr;
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001767 if (result) {
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001768 spvtools::SpirvTools tools(SPV_ENV_VULKAN_1_0);
Ethan Nicholas762466e2017-06-29 10:03:38 -04001769 const String& data = buffer.str();
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001770 SkASSERT(0 == data.size() % 4);
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001771 auto dumpmsg = [](spv_message_level_t, const char*, const spv_position_t&, const char* m) {
1772 SkDebugf("SPIR-V validation error: %s\n", m);
1773 };
1774 tools.SetMessageConsumer(dumpmsg);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001775 // Verify that the SPIR-V we produced is valid. If this SkASSERT fails, check the logs prior
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001776 // to the failure to see the validation errors.
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001777 SkAssertResult(tools.Validate((const uint32_t*) data.c_str(), data.size() / 4));
Ethan Nicholas762466e2017-06-29 10:03:38 -04001778 out.write(data.c_str(), data.size());
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001779 }
1780#else
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001781 fSource = program.fSource.get();
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001782 SPIRVCodeGenerator cg(fContext.get(), &program, this, &out);
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001783 bool result = cg.generateCode();
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001784 fSource = nullptr;
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001785#endif
Ethan Nicholasce33f102016-12-09 17:22:59 -05001786 return result;
1787}
1788
Ethan Nicholas00543112018-07-31 09:44:36 -04001789bool Compiler::toSPIRV(Program& program, String* out) {
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001790 StringStream buffer;
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001791 bool result = this->toSPIRV(program, buffer);
1792 if (result) {
Ethan Nicholas762466e2017-06-29 10:03:38 -04001793 *out = buffer.str();
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001794 }
1795 return result;
1796}
1797
Ethan Nicholas00543112018-07-31 09:44:36 -04001798bool Compiler::toGLSL(Program& program, OutputStream& out) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001799 fSource = program.fSource.get();
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001800 GLSLCodeGenerator cg(fContext.get(), &program, this, &out);
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001801 bool result = cg.generateCode();
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001802 fSource = nullptr;
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001803 return result;
1804}
1805
Ethan Nicholas00543112018-07-31 09:44:36 -04001806bool Compiler::toGLSL(Program& program, String* out) {
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001807 StringStream buffer;
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001808 bool result = this->toGLSL(program, buffer);
1809 if (result) {
Ethan Nicholas762466e2017-06-29 10:03:38 -04001810 *out = buffer.str();
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001811 }
1812 return result;
1813}
1814
Brian Osmanc0243912020-02-19 15:35:26 -05001815bool Compiler::toHLSL(Program& program, String* out) {
1816 String spirv;
1817 if (!this->toSPIRV(program, &spirv)) {
1818 return false;
1819 }
1820
1821 return SPIRVtoHLSL(spirv, out);
1822}
1823
Ethan Nicholas00543112018-07-31 09:44:36 -04001824bool Compiler::toMetal(Program& program, OutputStream& out) {
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001825 MetalCodeGenerator cg(fContext.get(), &program, this, &out);
Ethan Nicholascc305772017-10-13 16:17:45 -04001826 bool result = cg.generateCode();
Ethan Nicholascc305772017-10-13 16:17:45 -04001827 return result;
1828}
1829
Ethan Nicholas00543112018-07-31 09:44:36 -04001830bool Compiler::toMetal(Program& program, String* out) {
Timothy Liangb8eeb802018-07-23 16:46:16 -04001831 StringStream buffer;
1832 bool result = this->toMetal(program, buffer);
1833 if (result) {
1834 *out = buffer.str();
1835 }
1836 return result;
1837}
1838
Greg Daniela28ea672020-09-25 11:12:56 -04001839#if defined(SKSL_STANDALONE) || GR_TEST_UTILS
Ethan Nicholas00543112018-07-31 09:44:36 -04001840bool Compiler::toCPP(Program& program, String name, OutputStream& out) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001841 fSource = program.fSource.get();
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001842 CPPCodeGenerator cg(fContext.get(), &program, this, name, &out);
Ethan Nicholas762466e2017-06-29 10:03:38 -04001843 bool result = cg.generateCode();
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001844 fSource = nullptr;
Ethan Nicholas762466e2017-06-29 10:03:38 -04001845 return result;
1846}
1847
Ethan Nicholas00543112018-07-31 09:44:36 -04001848bool Compiler::toH(Program& program, String name, OutputStream& out) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001849 fSource = program.fSource.get();
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001850 HCodeGenerator cg(fContext.get(), &program, this, name, &out);
Ethan Nicholas762466e2017-06-29 10:03:38 -04001851 bool result = cg.generateCode();
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001852 fSource = nullptr;
Ethan Nicholas00543112018-07-31 09:44:36 -04001853 return result;
1854}
Greg Daniela28ea672020-09-25 11:12:56 -04001855#endif // defined(SKSL_STANDALONE) || GR_TEST_UTILS
Ethan Nicholas00543112018-07-31 09:44:36 -04001856
Ethan Nicholas2a479a52020-08-18 16:29:45 -04001857#endif // defined(SKSL_STANDALONE) || SK_SUPPORT_GPU
Brian Osman2e29ab52019-09-20 12:19:11 -04001858
1859#if !defined(SKSL_STANDALONE) && SK_SUPPORT_GPU
Brian Osmana4b91692020-08-10 14:26:16 -04001860bool Compiler::toPipelineStage(Program& program, PipelineStageArgs* outArgs) {
Ethan Nicholas00543112018-07-31 09:44:36 -04001861 fSource = program.fSource.get();
1862 StringStream buffer;
Brian Osman300fe1d2020-01-23 15:42:43 -05001863 PipelineStageCodeGenerator cg(fContext.get(), &program, this, &buffer, outArgs);
Ethan Nicholas00543112018-07-31 09:44:36 -04001864 bool result = cg.generateCode();
1865 fSource = nullptr;
1866 if (result) {
Brian Osman107c6662019-12-30 15:02:30 -05001867 outArgs->fCode = buffer.str();
Ethan Nicholas00543112018-07-31 09:44:36 -04001868 }
Ethan Nicholas762466e2017-06-29 10:03:38 -04001869 return result;
1870}
Brian Osmanfb32ddf2019-06-18 10:14:20 -04001871#endif
1872
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001873std::unique_ptr<ByteCode> Compiler::toByteCode(Program& program) {
Mike Klein853d4ed2020-08-20 00:41:00 +00001874#if defined(SK_ENABLE_SKSL_INTERPRETER)
Brian Osman808f0212020-01-21 15:36:47 -05001875 fSource = program.fSource.get();
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001876 std::unique_ptr<ByteCode> result(new ByteCode());
Brian Osmanb08cc022020-04-02 11:38:40 -04001877 ByteCodeGenerator cg(fContext.get(), &program, this, result.get());
1878 bool success = cg.generateCode();
1879 fSource = nullptr;
1880 if (success) {
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001881 return result;
1882 }
Mike Klein853d4ed2020-08-20 00:41:00 +00001883#else
1884 ABORT("ByteCode interpreter not enabled");
1885#endif
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001886 return nullptr;
1887}
1888
Brian Osman401a0092020-09-10 14:47:24 -04001889const char* Compiler::OperatorName(Token::Kind op) {
1890 switch (op) {
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001891 case Token::Kind::TK_PLUS: return "+";
1892 case Token::Kind::TK_MINUS: return "-";
1893 case Token::Kind::TK_STAR: return "*";
1894 case Token::Kind::TK_SLASH: return "/";
1895 case Token::Kind::TK_PERCENT: return "%";
1896 case Token::Kind::TK_SHL: return "<<";
1897 case Token::Kind::TK_SHR: return ">>";
1898 case Token::Kind::TK_LOGICALNOT: return "!";
1899 case Token::Kind::TK_LOGICALAND: return "&&";
1900 case Token::Kind::TK_LOGICALOR: return "||";
1901 case Token::Kind::TK_LOGICALXOR: return "^^";
1902 case Token::Kind::TK_BITWISENOT: return "~";
1903 case Token::Kind::TK_BITWISEAND: return "&";
1904 case Token::Kind::TK_BITWISEOR: return "|";
1905 case Token::Kind::TK_BITWISEXOR: return "^";
1906 case Token::Kind::TK_EQ: return "=";
1907 case Token::Kind::TK_EQEQ: return "==";
1908 case Token::Kind::TK_NEQ: return "!=";
1909 case Token::Kind::TK_LT: return "<";
1910 case Token::Kind::TK_GT: return ">";
1911 case Token::Kind::TK_LTEQ: return "<=";
1912 case Token::Kind::TK_GTEQ: return ">=";
1913 case Token::Kind::TK_PLUSEQ: return "+=";
1914 case Token::Kind::TK_MINUSEQ: return "-=";
1915 case Token::Kind::TK_STAREQ: return "*=";
1916 case Token::Kind::TK_SLASHEQ: return "/=";
1917 case Token::Kind::TK_PERCENTEQ: return "%=";
1918 case Token::Kind::TK_SHLEQ: return "<<=";
1919 case Token::Kind::TK_SHREQ: return ">>=";
1920 case Token::Kind::TK_LOGICALANDEQ: return "&&=";
1921 case Token::Kind::TK_LOGICALOREQ: return "||=";
1922 case Token::Kind::TK_LOGICALXOREQ: return "^^=";
1923 case Token::Kind::TK_BITWISEANDEQ: return "&=";
1924 case Token::Kind::TK_BITWISEOREQ: return "|=";
1925 case Token::Kind::TK_BITWISEXOREQ: return "^=";
1926 case Token::Kind::TK_PLUSPLUS: return "++";
1927 case Token::Kind::TK_MINUSMINUS: return "--";
1928 case Token::Kind::TK_COMMA: return ",";
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001929 default:
Brian Osman401a0092020-09-10 14:47:24 -04001930 ABORT("unsupported operator: %d\n", (int) op);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001931 }
1932}
1933
1934
1935bool Compiler::IsAssignment(Token::Kind op) {
1936 switch (op) {
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001937 case Token::Kind::TK_EQ: // fall through
1938 case Token::Kind::TK_PLUSEQ: // fall through
1939 case Token::Kind::TK_MINUSEQ: // fall through
1940 case Token::Kind::TK_STAREQ: // fall through
1941 case Token::Kind::TK_SLASHEQ: // fall through
1942 case Token::Kind::TK_PERCENTEQ: // fall through
1943 case Token::Kind::TK_SHLEQ: // fall through
1944 case Token::Kind::TK_SHREQ: // fall through
1945 case Token::Kind::TK_BITWISEOREQ: // fall through
1946 case Token::Kind::TK_BITWISEXOREQ: // fall through
1947 case Token::Kind::TK_BITWISEANDEQ: // fall through
1948 case Token::Kind::TK_LOGICALOREQ: // fall through
1949 case Token::Kind::TK_LOGICALXOREQ: // fall through
1950 case Token::Kind::TK_LOGICALANDEQ:
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001951 return true;
1952 default:
1953 return false;
1954 }
1955}
1956
Brian Osman401a0092020-09-10 14:47:24 -04001957Token::Kind Compiler::RemoveAssignment(Token::Kind op) {
1958 switch (op) {
1959 case Token::Kind::TK_PLUSEQ: return Token::Kind::TK_PLUS;
1960 case Token::Kind::TK_MINUSEQ: return Token::Kind::TK_MINUS;
1961 case Token::Kind::TK_STAREQ: return Token::Kind::TK_STAR;
1962 case Token::Kind::TK_SLASHEQ: return Token::Kind::TK_SLASH;
1963 case Token::Kind::TK_PERCENTEQ: return Token::Kind::TK_PERCENT;
1964 case Token::Kind::TK_SHLEQ: return Token::Kind::TK_SHL;
1965 case Token::Kind::TK_SHREQ: return Token::Kind::TK_SHR;
1966 case Token::Kind::TK_BITWISEOREQ: return Token::Kind::TK_BITWISEOR;
1967 case Token::Kind::TK_BITWISEXOREQ: return Token::Kind::TK_BITWISEXOR;
1968 case Token::Kind::TK_BITWISEANDEQ: return Token::Kind::TK_BITWISEAND;
1969 case Token::Kind::TK_LOGICALOREQ: return Token::Kind::TK_LOGICALOR;
1970 case Token::Kind::TK_LOGICALXOREQ: return Token::Kind::TK_LOGICALXOR;
1971 case Token::Kind::TK_LOGICALANDEQ: return Token::Kind::TK_LOGICALAND;
1972 default: return op;
1973 }
1974}
1975
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001976Position Compiler::position(int offset) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001977 SkASSERT(fSource);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001978 int line = 1;
1979 int column = 1;
1980 for (int i = 0; i < offset; i++) {
1981 if ((*fSource)[i] == '\n') {
1982 ++line;
1983 column = 1;
1984 }
1985 else {
1986 ++column;
1987 }
1988 }
1989 return Position(line, column);
1990}
1991
1992void Compiler::error(int offset, String msg) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001993 fErrorCount++;
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001994 Position pos = this->position(offset);
1995 fErrorText += "error: " + to_string(pos.fLine) + ": " + msg.c_str() + "\n";
ethannicholasb3058bd2016-07-01 08:22:01 -07001996}
1997
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001998String Compiler::errorText() {
Ethan Nicholas00543112018-07-31 09:44:36 -04001999 this->writeErrorCount();
2000 fErrorCount = 0;
Ethan Nicholas0df1b042017-03-31 13:56:23 -04002001 String result = fErrorText;
ethannicholasb3058bd2016-07-01 08:22:01 -07002002 return result;
2003}
2004
2005void Compiler::writeErrorCount() {
2006 if (fErrorCount) {
2007 fErrorText += to_string(fErrorCount) + " error";
2008 if (fErrorCount > 1) {
2009 fErrorText += "s";
2010 }
2011 fErrorText += "\n";
2012 }
2013}
2014
John Stilesa6841be2020-08-06 14:11:56 -04002015} // namespace SkSL