blob: 26b8c99f7311be11eac8075da24d161ba6b01baf [file] [log] [blame]
ethannicholasb3058bd2016-07-01 08:22:01 -07001/*
2 * Copyright 2016 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
Mike Klein6ad99092016-10-26 10:35:22 -04007
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/sksl/SkSLCompiler.h"
ethannicholasb3058bd2016-07-01 08:22:01 -07009
John Stilesfbd050b2020-08-03 13:21:46 -040010#include <memory>
John Stilesb8e010c2020-08-11 18:05:39 -040011#include <unordered_set>
John Stilesfbd050b2020-08-03 13:21:46 -040012
John Stilesb92641c2020-08-31 18:09:01 -040013#include "src/sksl/SkSLAnalysis.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "src/sksl/SkSLByteCodeGenerator.h"
15#include "src/sksl/SkSLCFGGenerator.h"
16#include "src/sksl/SkSLCPPCodeGenerator.h"
17#include "src/sksl/SkSLGLSLCodeGenerator.h"
18#include "src/sksl/SkSLHCodeGenerator.h"
19#include "src/sksl/SkSLIRGenerator.h"
20#include "src/sksl/SkSLMetalCodeGenerator.h"
21#include "src/sksl/SkSLPipelineStageCodeGenerator.h"
Ethan Nicholasc18bb512020-07-28 14:46:53 -040022#include "src/sksl/SkSLRehydrator.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "src/sksl/SkSLSPIRVCodeGenerator.h"
Brian Osmanc0243912020-02-19 15:35:26 -050024#include "src/sksl/SkSLSPIRVtoHLSL.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "src/sksl/ir/SkSLEnum.h"
26#include "src/sksl/ir/SkSLExpression.h"
27#include "src/sksl/ir/SkSLExpressionStatement.h"
28#include "src/sksl/ir/SkSLFunctionCall.h"
29#include "src/sksl/ir/SkSLIntLiteral.h"
30#include "src/sksl/ir/SkSLModifiersDeclaration.h"
31#include "src/sksl/ir/SkSLNop.h"
32#include "src/sksl/ir/SkSLSymbolTable.h"
33#include "src/sksl/ir/SkSLTernaryExpression.h"
34#include "src/sksl/ir/SkSLUnresolvedFunction.h"
35#include "src/sksl/ir/SkSLVarDeclarations.h"
John Stilese6150002020-10-05 12:03:53 -040036#include "src/utils/SkBitSet.h"
ethannicholasb3058bd2016-07-01 08:22:01 -070037
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -040038#include <fstream>
39
Ethan Nicholasa11035b2019-11-26 16:27:47 -050040#if !defined(SKSL_STANDALONE) & SK_SUPPORT_GPU
41#include "include/gpu/GrContextOptions.h"
42#include "src/gpu/GrShaderCaps.h"
43#endif
44
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -040045#ifdef SK_ENABLE_SPIRV_VALIDATION
46#include "spirv-tools/libspirv.hpp"
47#endif
48
Brian Osmandd496172020-08-08 08:17:18 -040049#if !SKSL_STANDALONE
Ethan Nicholasc18bb512020-07-28 14:46:53 -040050
51#include "src/sksl/generated/sksl_fp.dehydrated.sksl"
52#include "src/sksl/generated/sksl_frag.dehydrated.sksl"
53#include "src/sksl/generated/sksl_geom.dehydrated.sksl"
54#include "src/sksl/generated/sksl_gpu.dehydrated.sksl"
55#include "src/sksl/generated/sksl_interp.dehydrated.sksl"
56#include "src/sksl/generated/sksl_pipeline.dehydrated.sksl"
57#include "src/sksl/generated/sksl_vert.dehydrated.sksl"
58
59#else
60
Brian Osmandd496172020-08-08 08:17:18 -040061// GN generates or copies all of these files to the skslc executable directory
62static const char SKSL_GPU_INCLUDE[] = "sksl_gpu.sksl";
63static const char SKSL_INTERP_INCLUDE[] = "sksl_interp.sksl";
64static const char SKSL_VERT_INCLUDE[] = "sksl_vert.sksl";
65static const char SKSL_FRAG_INCLUDE[] = "sksl_frag.sksl";
66static const char SKSL_GEOM_INCLUDE[] = "sksl_geom.sksl";
67static const char SKSL_FP_INCLUDE[] = "sksl_fp.sksl";
68static const char SKSL_PIPELINE_INCLUDE[] = "sksl_pipeline.sksl";
Ethan Nicholasc18bb512020-07-28 14:46:53 -040069
70#endif
Ethan Nicholas0d997662019-04-08 09:46:01 -040071
ethannicholasb3058bd2016-07-01 08:22:01 -070072namespace SkSL {
73
Ethan Nicholasdb80f692019-11-22 14:06:12 -050074static void grab_intrinsics(std::vector<std::unique_ptr<ProgramElement>>* src,
John Stiles810c8cf2020-08-26 19:46:27 -040075 IRIntrinsicMap* target) {
Brian Osmanafa18ee2020-10-07 17:47:45 -040076 for (std::unique_ptr<ProgramElement>& element : *src) {
Ethan Nicholase6592142020-09-08 10:22:09 -040077 switch (element->kind()) {
78 case ProgramElement::Kind::kFunction: {
Brian Osmanafa18ee2020-10-07 17:47:45 -040079 const FunctionDefinition& f = element->as<FunctionDefinition>();
Ethan Nicholased84b732020-10-08 11:45:44 -040080 SkASSERT(f.fDeclaration.isBuiltin());
Brian Osman2b469eb2020-09-21 11:32:10 -040081 target->insertOrDie(f.fDeclaration.description(), std::move(element));
Ethan Nicholasdb80f692019-11-22 14:06:12 -050082 break;
83 }
Ethan Nicholase6592142020-09-08 10:22:09 -040084 case ProgramElement::Kind::kEnum: {
Brian Osmanafa18ee2020-10-07 17:47:45 -040085 const Enum& e = element->as<Enum>();
86 SkASSERT(e.isBuiltin());
Ethan Nicholasd83ded82020-09-29 17:05:54 -040087 target->insertOrDie(e.typeName(), std::move(element));
Ethan Nicholasdb80f692019-11-22 14:06:12 -050088 break;
89 }
Brian Osmanc0213602020-10-06 14:43:32 -040090 case ProgramElement::Kind::kGlobalVar: {
Brian Osmanafa18ee2020-10-07 17:47:45 -040091 const Variable* var = element->as<GlobalVarDeclaration>().fDecl->fVar;
92 SkASSERT(var->isBuiltin());
Ethan Nicholase2c49992020-10-05 11:49:11 -040093 target->insertOrDie(var->name(), std::move(element));
Brian Osmanafa18ee2020-10-07 17:47:45 -040094 break;
95 }
96 case ProgramElement::Kind::kInterfaceBlock: {
97 const Variable* var = element->as<InterfaceBlock>().fVariable;
98 SkASSERT(var->isBuiltin());
99 target->insertOrDie(var->name(), std::move(element));
Brian Osman8e2ef022020-09-30 13:26:43 -0400100 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;
Ethan Nicholased84b732020-10-08 11:45:44 -0400114 fnDecl.callCount() = 0;
John Stilesbc0c29e2020-09-28 13:13:40 -0400115 }
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);
John Stilesb8cc6652020-10-08 09:12:07 -0400126 #define ADD_TYPE(t) fRootSymbolTable->addWithoutOwnership(fContext->f ## t ## _Type.get())
ethannicholasb3058bd2016-07-01 08:22:01 -0700127 ADD_TYPE(Void);
128 ADD_TYPE(Float);
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400129 ADD_TYPE(Float2);
130 ADD_TYPE(Float3);
131 ADD_TYPE(Float4);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400132 ADD_TYPE(Half);
133 ADD_TYPE(Half2);
134 ADD_TYPE(Half3);
135 ADD_TYPE(Half4);
ethannicholasb3058bd2016-07-01 08:22:01 -0700136 ADD_TYPE(Int);
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400137 ADD_TYPE(Int2);
138 ADD_TYPE(Int3);
139 ADD_TYPE(Int4);
ethannicholasb3058bd2016-07-01 08:22:01 -0700140 ADD_TYPE(UInt);
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400141 ADD_TYPE(UInt2);
142 ADD_TYPE(UInt3);
143 ADD_TYPE(UInt4);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400144 ADD_TYPE(Short);
145 ADD_TYPE(Short2);
146 ADD_TYPE(Short3);
147 ADD_TYPE(Short4);
148 ADD_TYPE(UShort);
149 ADD_TYPE(UShort2);
150 ADD_TYPE(UShort3);
151 ADD_TYPE(UShort4);
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400152 ADD_TYPE(Byte);
153 ADD_TYPE(Byte2);
154 ADD_TYPE(Byte3);
155 ADD_TYPE(Byte4);
156 ADD_TYPE(UByte);
157 ADD_TYPE(UByte2);
158 ADD_TYPE(UByte3);
159 ADD_TYPE(UByte4);
ethannicholasb3058bd2016-07-01 08:22:01 -0700160 ADD_TYPE(Bool);
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400161 ADD_TYPE(Bool2);
162 ADD_TYPE(Bool3);
163 ADD_TYPE(Bool4);
164 ADD_TYPE(Float2x2);
165 ADD_TYPE(Float2x3);
166 ADD_TYPE(Float2x4);
167 ADD_TYPE(Float3x2);
168 ADD_TYPE(Float3x3);
169 ADD_TYPE(Float3x4);
170 ADD_TYPE(Float4x2);
171 ADD_TYPE(Float4x3);
172 ADD_TYPE(Float4x4);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400173 ADD_TYPE(Half2x2);
174 ADD_TYPE(Half2x3);
175 ADD_TYPE(Half2x4);
176 ADD_TYPE(Half3x2);
177 ADD_TYPE(Half3x3);
178 ADD_TYPE(Half3x4);
179 ADD_TYPE(Half4x2);
180 ADD_TYPE(Half4x3);
181 ADD_TYPE(Half4x4);
ethannicholasb3058bd2016-07-01 08:22:01 -0700182 ADD_TYPE(GenType);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400183 ADD_TYPE(GenHType);
ethannicholasb3058bd2016-07-01 08:22:01 -0700184 ADD_TYPE(GenIType);
185 ADD_TYPE(GenUType);
186 ADD_TYPE(GenBType);
187 ADD_TYPE(Mat);
188 ADD_TYPE(Vec);
189 ADD_TYPE(GVec);
190 ADD_TYPE(GVec2);
191 ADD_TYPE(GVec3);
192 ADD_TYPE(GVec4);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400193 ADD_TYPE(HVec);
ethannicholasb3058bd2016-07-01 08:22:01 -0700194 ADD_TYPE(IVec);
195 ADD_TYPE(UVec);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -0400196 ADD_TYPE(SVec);
197 ADD_TYPE(USVec);
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400198 ADD_TYPE(ByteVec);
199 ADD_TYPE(UByteVec);
ethannicholasb3058bd2016-07-01 08:22:01 -0700200 ADD_TYPE(BVec);
201
202 ADD_TYPE(Sampler1D);
203 ADD_TYPE(Sampler2D);
204 ADD_TYPE(Sampler3D);
ethannicholas5961bc92016-10-12 06:39:56 -0700205 ADD_TYPE(SamplerExternalOES);
ethannicholasb3058bd2016-07-01 08:22:01 -0700206 ADD_TYPE(SamplerCube);
207 ADD_TYPE(Sampler2DRect);
208 ADD_TYPE(Sampler1DArray);
209 ADD_TYPE(Sampler2DArray);
210 ADD_TYPE(SamplerCubeArray);
211 ADD_TYPE(SamplerBuffer);
212 ADD_TYPE(Sampler2DMS);
213 ADD_TYPE(Sampler2DMSArray);
214
Brian Salomonbf7b6202016-11-11 16:08:03 -0500215 ADD_TYPE(ISampler2D);
216
Brian Salomon2a51de82016-11-16 12:06:01 -0500217 ADD_TYPE(Image2D);
218 ADD_TYPE(IImage2D);
219
Greg Daniel64773e62016-11-22 09:44:03 -0500220 ADD_TYPE(SubpassInput);
221 ADD_TYPE(SubpassInputMS);
222
ethannicholasb3058bd2016-07-01 08:22:01 -0700223 ADD_TYPE(GSampler1D);
224 ADD_TYPE(GSampler2D);
225 ADD_TYPE(GSampler3D);
226 ADD_TYPE(GSamplerCube);
227 ADD_TYPE(GSampler2DRect);
228 ADD_TYPE(GSampler1DArray);
229 ADD_TYPE(GSampler2DArray);
230 ADD_TYPE(GSamplerCubeArray);
231 ADD_TYPE(GSamplerBuffer);
232 ADD_TYPE(GSampler2DMS);
233 ADD_TYPE(GSampler2DMSArray);
234
235 ADD_TYPE(Sampler1DShadow);
236 ADD_TYPE(Sampler2DShadow);
237 ADD_TYPE(SamplerCubeShadow);
238 ADD_TYPE(Sampler2DRectShadow);
239 ADD_TYPE(Sampler1DArrayShadow);
240 ADD_TYPE(Sampler2DArrayShadow);
241 ADD_TYPE(SamplerCubeArrayShadow);
242 ADD_TYPE(GSampler2DArrayShadow);
243 ADD_TYPE(GSamplerCubeArrayShadow);
Ethan Nicholasc9472af2017-10-10 16:30:21 -0400244 ADD_TYPE(FragmentProcessor);
Stephen Whiteff5d7a22019-07-26 17:42:06 -0400245 ADD_TYPE(Sampler);
246 ADD_TYPE(Texture2D);
ethannicholasb3058bd2016-07-01 08:22:01 -0700247
Brian Osman28590d52020-03-23 16:59:08 -0400248 StringFragment fpAliasName("shader");
John Stiles49a547f2020-10-06 16:14:37 -0400249 fRootSymbolTable->addAlias(fpAliasName, fContext->fFragmentProcessor_Type.get());
Brian Osman28590d52020-03-23 16:59:08 -0400250
Brian Osman3887a012020-09-30 13:22:27 -0400251 // sk_Caps is "builtin", but all references to it are resolved to Settings, so we don't need to
252 // treat it as builtin (ie, no need to clone it into the Program).
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700253 StringFragment skCapsName("sk_Caps");
John Stilesb8cc6652020-10-08 09:12:07 -0400254 fRootSymbolTable->add(std::make_unique<Variable>(/*offset=*/-1,
Ethan Nicholas041fd0a2020-10-07 16:42:04 -0400255 fIRGenerator->fModifiers->handle(Modifiers()),
256 skCapsName, fContext->fSkCaps_Type.get(),
Brian Osman3887a012020-09-30 13:22:27 -0400257 /*builtin=*/false, Variable::kGlobal_Storage));
Ethan Nicholas3605ace2016-11-21 15:59:48 -0500258
Brian Osman00a8b5b2020-10-02 09:06:04 -0400259 fIRGenerator->fIntrinsics = nullptr;
260 std::vector<std::unique_ptr<ProgramElement>> gpuElements;
Brian Osmanafa18ee2020-10-07 17:47:45 -0400261 std::vector<std::unique_ptr<ProgramElement>> vertElements;
Brian Osman00a8b5b2020-10-02 09:06:04 -0400262 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,
Brian Osmanafa18ee2020-10-07 17:47:45 -0400267 &vertElements, &fVertexSymbolTable);
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -0400268 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 {
Ethan Nicholas041fd0a2020-10-07 16:42:04 -0400272 Rehydrator rehydrator(&fIRGenerator->fContext, fIRGenerator->fModifiers.get(),
273 fRootSymbolTable, this, SKSL_INCLUDE_sksl_gpu,
Brian Osmaneac49832020-09-18 11:49:22 -0400274 SKSL_INCLUDE_sksl_gpu_LENGTH);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400275 fGpuSymbolTable = rehydrator.symbolTable();
Brian Osman00a8b5b2020-10-02 09:06:04 -0400276 gpuElements = rehydrator.elements();
Ethan Nicholas041fd0a2020-10-07 16:42:04 -0400277 fModifiers.push_back(fIRGenerator->releaseModifiers());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400278 }
279 {
Ethan Nicholas041fd0a2020-10-07 16:42:04 -0400280 Rehydrator rehydrator(&fIRGenerator->fContext, fIRGenerator->fModifiers.get(),
281 fGpuSymbolTable, this, SKSL_INCLUDE_sksl_vert,
Brian Osmaneac49832020-09-18 11:49:22 -0400282 SKSL_INCLUDE_sksl_vert_LENGTH);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400283 fVertexSymbolTable = rehydrator.symbolTable();
Brian Osmanafa18ee2020-10-07 17:47:45 -0400284 vertElements = rehydrator.elements();
Ethan Nicholas041fd0a2020-10-07 16:42:04 -0400285 fModifiers.push_back(fIRGenerator->releaseModifiers());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400286 }
287 {
Ethan Nicholas041fd0a2020-10-07 16:42:04 -0400288 Rehydrator rehydrator(&fIRGenerator->fContext, fIRGenerator->fModifiers.get(),
289 fGpuSymbolTable, this, SKSL_INCLUDE_sksl_frag,
Brian Osmaneac49832020-09-18 11:49:22 -0400290 SKSL_INCLUDE_sksl_frag_LENGTH);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400291 fFragmentSymbolTable = rehydrator.symbolTable();
Brian Osman00a8b5b2020-10-02 09:06:04 -0400292 fragElements = rehydrator.elements();
Ethan Nicholas041fd0a2020-10-07 16:42:04 -0400293 fModifiers.push_back(fIRGenerator->releaseModifiers());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400294 }
295#endif
John Stilesbc0c29e2020-09-28 13:13:40 -0400296 // Call counts are used to track dead-stripping and inlinability within the program being
297 // currently compiled, and always should start at zero for a new program. Zero out any call
298 // counts that were registered during the assembly of the intrinsics/include data. (If we
299 // actually use calls from inside the intrinsics, we will clone them into the program and they
300 // will get new call counts.)
Brian Osman00a8b5b2020-10-02 09:06:04 -0400301 reset_call_counts(&gpuElements);
Brian Osmanafa18ee2020-10-07 17:47:45 -0400302 reset_call_counts(&vertElements);
Brian Osman00a8b5b2020-10-02 09:06:04 -0400303 reset_call_counts(&fragElements);
John Stilesbc0c29e2020-09-28 13:13:40 -0400304
Brian Osman00a8b5b2020-10-02 09:06:04 -0400305 fGPUIntrinsics = std::make_unique<IRIntrinsicMap>(/*parent=*/nullptr);
306 grab_intrinsics(&gpuElements, fGPUIntrinsics.get());
307
Brian Osmanafa18ee2020-10-07 17:47:45 -0400308 fVertexIntrinsics = std::make_unique<IRIntrinsicMap>(fGPUIntrinsics.get());
309 grab_intrinsics(&vertElements, fVertexIntrinsics.get());
310
Brian Osman00a8b5b2020-10-02 09:06:04 -0400311 fFragmentIntrinsics = std::make_unique<IRIntrinsicMap>(fGPUIntrinsics.get());
312 grab_intrinsics(&fragElements, fFragmentIntrinsics.get());
ethannicholasb3058bd2016-07-01 08:22:01 -0700313}
314
John Stiles656427a2020-08-27 15:26:26 -0400315Compiler::~Compiler() {}
ethannicholasb3058bd2016-07-01 08:22:01 -0700316
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400317void Compiler::loadGeometryIntrinsics() {
318 if (fGeometrySymbolTable) {
319 return;
320 }
Brian Osmanafa18ee2020-10-07 17:47:45 -0400321 fGeometryIntrinsics = std::make_unique<IRIntrinsicMap>(fGPUIntrinsics.get());
322 std::vector<std::unique_ptr<ProgramElement>> geomElements;
Brian Osmandd496172020-08-08 08:17:18 -0400323 #if !SKSL_STANDALONE
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400324 {
Ethan Nicholas041fd0a2020-10-07 16:42:04 -0400325 Rehydrator rehydrator(&fIRGenerator->fContext, fIRGenerator->fModifiers.get(),
326 fGpuSymbolTable, this, SKSL_INCLUDE_sksl_geom,
327 SKSL_INCLUDE_sksl_geom_LENGTH);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400328 fGeometrySymbolTable = rehydrator.symbolTable();
Brian Osmanafa18ee2020-10-07 17:47:45 -0400329 geomElements = rehydrator.elements();
Ethan Nicholas041fd0a2020-10-07 16:42:04 -0400330 fModifiers.push_back(fIRGenerator->releaseModifiers());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400331 }
332 #else
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -0400333 this->processIncludeFile(Program::kGeometry_Kind, SKSL_GEOM_INCLUDE, fGpuSymbolTable,
Brian Osmanafa18ee2020-10-07 17:47:45 -0400334 &geomElements, &fGeometrySymbolTable);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400335 #endif
Brian Osmanafa18ee2020-10-07 17:47:45 -0400336 grab_intrinsics(&geomElements, fGeometryIntrinsics.get());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400337}
338
Brian Osman8e2ef022020-09-30 13:26:43 -0400339void Compiler::loadFPIntrinsics() {
340 if (fFPSymbolTable) {
341 return;
342 }
343 fFPIntrinsics = std::make_unique<IRIntrinsicMap>(fGPUIntrinsics.get());
344 std::vector<std::unique_ptr<ProgramElement>> fpElements;
345 #if !SKSL_STANDALONE
346 {
Ethan Nicholas041fd0a2020-10-07 16:42:04 -0400347 Rehydrator rehydrator(&fIRGenerator->fContext, fIRGenerator->fModifiers.get(),
348 fGpuSymbolTable, this, SKSL_INCLUDE_sksl_fp,
Brian Osman8e2ef022020-09-30 13:26:43 -0400349 SKSL_INCLUDE_sksl_fp_LENGTH);
350 fFPSymbolTable = rehydrator.symbolTable();
351 fpElements = rehydrator.elements();
352 }
353 #else
354 this->processIncludeFile(Program::kFragmentProcessor_Kind, SKSL_FP_INCLUDE, fGpuSymbolTable,
355 &fpElements, &fFPSymbolTable);
356 #endif
357 grab_intrinsics(&fpElements, fFPIntrinsics.get());
Brian Osman8e2ef022020-09-30 13:26:43 -0400358}
359
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400360void Compiler::loadPipelineIntrinsics() {
361 if (fPipelineSymbolTable) {
362 return;
363 }
Brian Osman00a8b5b2020-10-02 09:06:04 -0400364 fPipelineIntrinsics = std::make_unique<IRIntrinsicMap>(fGPUIntrinsics.get());
Brian Osman8e2ef022020-09-30 13:26:43 -0400365 std::vector<std::unique_ptr<ProgramElement>> pipelineIntrinics;
Brian Osmandd496172020-08-08 08:17:18 -0400366 #if !SKSL_STANDALONE
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400367 {
Ethan Nicholas041fd0a2020-10-07 16:42:04 -0400368 Rehydrator rehydrator(&fIRGenerator->fContext, fIRGenerator->fModifiers.get(),
369 fGpuSymbolTable, this, SKSL_INCLUDE_sksl_pipeline,
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400370 SKSL_INCLUDE_sksl_pipeline_LENGTH);
371 fPipelineSymbolTable = rehydrator.symbolTable();
Brian Osman8e2ef022020-09-30 13:26:43 -0400372 pipelineIntrinics = rehydrator.elements();
Ethan Nicholas041fd0a2020-10-07 16:42:04 -0400373 fModifiers.push_back(fIRGenerator->releaseModifiers());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400374 }
375 #else
376 this->processIncludeFile(Program::kPipelineStage_Kind, SKSL_PIPELINE_INCLUDE,
Brian Osman8e2ef022020-09-30 13:26:43 -0400377 fGpuSymbolTable, &pipelineIntrinics, &fPipelineSymbolTable);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400378 #endif
Brian Osman8e2ef022020-09-30 13:26:43 -0400379 grab_intrinsics(&pipelineIntrinics, fPipelineIntrinsics.get());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400380}
381
382void Compiler::loadInterpreterIntrinsics() {
383 if (fInterpreterSymbolTable) {
384 return;
385 }
Brian Osman00a8b5b2020-10-02 09:06:04 -0400386 fInterpreterIntrinsics = std::make_unique<IRIntrinsicMap>(/*parent=*/nullptr);
387 std::vector<std::unique_ptr<ProgramElement>> interpElements;
Brian Osmandd496172020-08-08 08:17:18 -0400388 #if !SKSL_STANDALONE
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400389 {
Ethan Nicholas041fd0a2020-10-07 16:42:04 -0400390 Rehydrator rehydrator(&fIRGenerator->fContext, fIRGenerator->fModifiers.get(),
391 fRootSymbolTable, this, SKSL_INCLUDE_sksl_interp,
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400392 SKSL_INCLUDE_sksl_interp_LENGTH);
393 fInterpreterSymbolTable = rehydrator.symbolTable();
Brian Osman00a8b5b2020-10-02 09:06:04 -0400394 interpElements = rehydrator.elements();
Ethan Nicholas041fd0a2020-10-07 16:42:04 -0400395 fModifiers.push_back(fIRGenerator->releaseModifiers());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400396 }
397 #else
398 this->processIncludeFile(Program::kGeneric_Kind, SKSL_INTERP_INCLUDE,
Brian Osman00a8b5b2020-10-02 09:06:04 -0400399 fIRGenerator->fSymbolTable, &interpElements,
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -0400400 &fInterpreterSymbolTable);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400401 #endif
Brian Osman00a8b5b2020-10-02 09:06:04 -0400402 grab_intrinsics(&interpElements, fInterpreterIntrinsics.get());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400403}
404
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -0400405void Compiler::processIncludeFile(Program::Kind kind, const char* path,
Ethan Nicholas8da1e652019-05-24 11:01:59 -0400406 std::shared_ptr<SymbolTable> base,
407 std::vector<std::unique_ptr<ProgramElement>>* outElements,
408 std::shared_ptr<SymbolTable>* outSymbolTable) {
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -0400409 std::ifstream in(path);
Brian Osmane498b3c2020-09-23 14:42:11 -0400410 std::unique_ptr<String> text = std::make_unique<String>(std::istreambuf_iterator<char>(in),
411 std::istreambuf_iterator<char>());
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -0400412 if (in.rdstate()) {
413 printf("error reading %s\n", path);
414 abort();
415 }
Brian Osmane498b3c2020-09-23 14:42:11 -0400416 const String* source = fRootSymbolTable->takeOwnershipOfString(std::move(text));
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -0400417 fSource = source;
Ethan Nicholas8da1e652019-05-24 11:01:59 -0400418 Program::Settings settings;
Ethan Nicholasa11035b2019-11-26 16:27:47 -0500419#if !defined(SKSL_STANDALONE) & SK_SUPPORT_GPU
420 GrContextOptions opts;
421 GrShaderCaps caps(opts);
422 settings.fCaps = &caps;
423#endif
John Stiles881a10c2020-09-19 10:13:24 -0400424 SkASSERT(fIRGenerator->fCanInline);
425 fIRGenerator->fCanInline = false;
Brian Osmane3dcb132020-10-08 10:27:29 -0400426 fIRGenerator->start(&settings, base ? base : fRootSymbolTable, true);
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -0400427 fIRGenerator->convertProgram(kind, source->c_str(), source->length(), outElements);
John Stiles881a10c2020-09-19 10:13:24 -0400428 fIRGenerator->fCanInline = true;
Ethan Nicholas8da1e652019-05-24 11:01:59 -0400429 if (this->fErrorCount) {
430 printf("Unexpected errors: %s\n", this->fErrorText.c_str());
John Stiles88f3b682020-10-06 13:37:52 -0400431 SkDEBUGFAILF("%s %s\n", path, this->fErrorText.c_str());
Ethan Nicholas8da1e652019-05-24 11:01:59 -0400432 }
Ethan Nicholas8da1e652019-05-24 11:01:59 -0400433 *outSymbolTable = fIRGenerator->fSymbolTable;
Ethan Nicholasa11035b2019-11-26 16:27:47 -0500434#ifdef SK_DEBUG
435 fSource = nullptr;
436#endif
Ethan Nicholas041fd0a2020-10-07 16:42:04 -0400437 fModifiers.push_back(fIRGenerator->releaseModifiers());
Brian Osmane498b3c2020-09-23 14:42:11 -0400438 fIRGenerator->finish();
Ethan Nicholas8da1e652019-05-24 11:01:59 -0400439}
440
ethannicholas22f939e2016-10-13 13:25:34 -0700441// add the definition created by assigning to the lvalue to the definition set
Ethan Nicholas86a43402017-01-19 13:32:00 -0500442void Compiler::addDefinition(const Expression* lvalue, std::unique_ptr<Expression>* expr,
443 DefinitionMap* definitions) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400444 switch (lvalue->kind()) {
445 case Expression::Kind::kVariableReference: {
Ethan Nicholas78686922020-10-08 06:46:27 -0400446 const Variable& var = *lvalue->as<VariableReference>().variable();
Ethan Nicholas041fd0a2020-10-07 16:42:04 -0400447 if (var.storage() == Variable::kLocal_Storage) {
ethannicholas22f939e2016-10-13 13:25:34 -0700448 (*definitions)[&var] = expr;
449 }
450 break;
451 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400452 case Expression::Kind::kSwizzle:
ethannicholas22f939e2016-10-13 13:25:34 -0700453 // We consider the variable written to as long as at least some of its components have
454 // been written to. This will lead to some false negatives (we won't catch it if you
455 // write to foo.x and then read foo.y), but being stricter could lead to false positives
Mike Klein6ad99092016-10-26 10:35:22 -0400456 // (we write to foo.x, and then pass foo to a function which happens to only read foo.x,
457 // 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 -0700458 // more complicated whole-program analysis. This is probably good enough.
John Stilesa5a97b42020-08-18 11:19:07 -0400459 this->addDefinition(lvalue->as<Swizzle>().fBase.get(),
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400460 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
ethannicholas22f939e2016-10-13 13:25:34 -0700461 definitions);
462 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400463 case Expression::Kind::kIndex:
ethannicholas22f939e2016-10-13 13:25:34 -0700464 // see comments in Swizzle
John Stilesa5a97b42020-08-18 11:19:07 -0400465 this->addDefinition(lvalue->as<IndexExpression>().fBase.get(),
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400466 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
ethannicholas22f939e2016-10-13 13:25:34 -0700467 definitions);
468 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400469 case Expression::Kind::kFieldAccess:
ethannicholas22f939e2016-10-13 13:25:34 -0700470 // see comments in Swizzle
John Stilesa5a97b42020-08-18 11:19:07 -0400471 this->addDefinition(lvalue->as<FieldAccess>().fBase.get(),
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400472 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
ethannicholas22f939e2016-10-13 13:25:34 -0700473 definitions);
474 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400475 case Expression::Kind::kTernary:
Ethan Nicholasa583b812018-01-18 13:32:11 -0500476 // To simplify analysis, we just pretend that we write to both sides of the ternary.
477 // This allows for false positives (meaning we fail to detect that a variable might not
478 // have been assigned), but is preferable to false negatives.
Ethan Nicholasdd218162020-10-08 05:48:01 -0400479 this->addDefinition(lvalue->as<TernaryExpression>().ifTrue().get(),
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400480 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
Ethan Nicholasa583b812018-01-18 13:32:11 -0500481 definitions);
Ethan Nicholasdd218162020-10-08 05:48:01 -0400482 this->addDefinition(lvalue->as<TernaryExpression>().ifFalse().get(),
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400483 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
Ethan Nicholasa583b812018-01-18 13:32:11 -0500484 definitions);
485 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400486 case Expression::Kind::kExternalValue:
Ethan Nicholas91164d12019-05-15 15:29:54 -0400487 break;
ethannicholas22f939e2016-10-13 13:25:34 -0700488 default:
489 // not an lvalue, can't happen
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400490 SkASSERT(false);
ethannicholas22f939e2016-10-13 13:25:34 -0700491 }
492}
493
494// add local variables defined by this node to the set
Mike Klein6ad99092016-10-26 10:35:22 -0400495void Compiler::addDefinitions(const BasicBlock::Node& node,
Ethan Nicholas86a43402017-01-19 13:32:00 -0500496 DefinitionMap* definitions) {
John Stiles70025e52020-09-28 16:08:58 -0400497 if (node.isExpression()) {
498 Expression* expr = node.expression()->get();
499 switch (expr->kind()) {
500 case Expression::Kind::kBinary: {
501 BinaryExpression* b = &expr->as<BinaryExpression>();
502 if (b->getOperator() == Token::Kind::TK_EQ) {
503 this->addDefinition(&b->left(), &b->rightPointer(), definitions);
504 } else if (Compiler::IsAssignment(b->getOperator())) {
505 this->addDefinition(
506 &b->left(),
507 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
508 definitions);
Ethan Nicholas86a43402017-01-19 13:32:00 -0500509
ethannicholas22f939e2016-10-13 13:25:34 -0700510 }
John Stiles70025e52020-09-28 16:08:58 -0400511 break;
ethannicholas22f939e2016-10-13 13:25:34 -0700512 }
John Stiles70025e52020-09-28 16:08:58 -0400513 case Expression::Kind::kFunctionCall: {
514 const FunctionCall& c = expr->as<FunctionCall>();
Ethan Nicholased84b732020-10-08 11:45:44 -0400515 const std::vector<Variable*>& parameters = c.function().parameters();
516 for (size_t i = 0; i < parameters.size(); ++i) {
517 if (parameters[i]->modifiers().fFlags & Modifiers::kOut_Flag) {
John Stiles70025e52020-09-28 16:08:58 -0400518 this->addDefinition(
Ethan Nicholas0dec9922020-10-05 15:51:52 -0400519 c.arguments()[i].get(),
John Stiles70025e52020-09-28 16:08:58 -0400520 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
521 definitions);
522 }
523 }
524 break;
525 }
526 case Expression::Kind::kPrefix: {
527 const PrefixExpression* p = &expr->as<PrefixExpression>();
528 if (p->fOperator == Token::Kind::TK_MINUSMINUS ||
529 p->fOperator == Token::Kind::TK_PLUSPLUS) {
530 this->addDefinition(
531 p->fOperand.get(),
532 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
533 definitions);
534 }
535 break;
536 }
537 case Expression::Kind::kPostfix: {
538 const PostfixExpression* p = &expr->as<PostfixExpression>();
539 if (p->fOperator == Token::Kind::TK_MINUSMINUS ||
540 p->fOperator == Token::Kind::TK_PLUSPLUS) {
541 this->addDefinition(
542 p->fOperand.get(),
543 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
544 definitions);
545 }
546 break;
547 }
548 case Expression::Kind::kVariableReference: {
549 const VariableReference* v = &expr->as<VariableReference>();
Ethan Nicholas78686922020-10-08 06:46:27 -0400550 if (v->refKind() != VariableReference::kRead_RefKind) {
John Stiles70025e52020-09-28 16:08:58 -0400551 this->addDefinition(
552 v,
553 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
554 definitions);
555 }
556 break;
557 }
558 default:
559 break;
ethannicholas22f939e2016-10-13 13:25:34 -0700560 }
John Stiles70025e52020-09-28 16:08:58 -0400561 } else if (node.isStatement()) {
562 Statement* stmt = node.statement()->get();
563 if (stmt->is<VarDeclaration>()) {
564 VarDeclaration& vd = stmt->as<VarDeclaration>();
565 if (vd.fValue) {
566 (*definitions)[vd.fVar] = &vd.fValue;
ethannicholas22f939e2016-10-13 13:25:34 -0700567 }
ethannicholas22f939e2016-10-13 13:25:34 -0700568 }
569 }
570}
571
John Stilese6150002020-10-05 12:03:53 -0400572void Compiler::scanCFG(CFG* cfg, BlockId blockId, SkBitSet* processedSet) {
ethannicholas22f939e2016-10-13 13:25:34 -0700573 BasicBlock& block = cfg->fBlocks[blockId];
574
575 // compute definitions after this block
Ethan Nicholas86a43402017-01-19 13:32:00 -0500576 DefinitionMap after = block.fBefore;
ethannicholas22f939e2016-10-13 13:25:34 -0700577 for (const BasicBlock::Node& n : block.fNodes) {
578 this->addDefinitions(n, &after);
579 }
580
581 // propagate definitions to exits
582 for (BlockId exitId : block.fExits) {
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400583 if (exitId == blockId) {
584 continue;
585 }
ethannicholas22f939e2016-10-13 13:25:34 -0700586 BasicBlock& exit = cfg->fBlocks[exitId];
587 for (const auto& pair : after) {
Ethan Nicholas86a43402017-01-19 13:32:00 -0500588 std::unique_ptr<Expression>* e1 = pair.second;
589 auto found = exit.fBefore.find(pair.first);
590 if (found == exit.fBefore.end()) {
John Stilese6150002020-10-05 12:03:53 -0400591 // exit has no definition for it, just copy it and reprocess exit block
592 processedSet->reset(exitId);
ethannicholas22f939e2016-10-13 13:25:34 -0700593 exit.fBefore[pair.first] = e1;
594 } else {
Ethan Nicholas86a43402017-01-19 13:32:00 -0500595 // exit has a (possibly different) value already defined
596 std::unique_ptr<Expression>* e2 = exit.fBefore[pair.first];
ethannicholas22f939e2016-10-13 13:25:34 -0700597 if (e1 != e2) {
John Stilese6150002020-10-05 12:03:53 -0400598 // definition has changed, merge and reprocess the exit block
599 processedSet->reset(exitId);
Ethan Nicholasaf197692017-02-27 13:26:45 -0500600 if (e1 && e2) {
601 exit.fBefore[pair.first] =
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400602 (std::unique_ptr<Expression>*) &fContext->fDefined_Expression;
Ethan Nicholasaf197692017-02-27 13:26:45 -0500603 } else {
604 exit.fBefore[pair.first] = nullptr;
605 }
ethannicholas22f939e2016-10-13 13:25:34 -0700606 }
607 }
608 }
609 }
610}
611
612// returns a map which maps all local variables in the function to null, indicating that their value
613// is initially unknown
Ethan Nicholas86a43402017-01-19 13:32:00 -0500614static DefinitionMap compute_start_state(const CFG& cfg) {
615 DefinitionMap result;
Mike Klein6ad99092016-10-26 10:35:22 -0400616 for (const auto& block : cfg.fBlocks) {
617 for (const auto& node : block.fNodes) {
John Stiles70025e52020-09-28 16:08:58 -0400618 if (node.isStatement()) {
Ethan Nicholascb670962017-04-20 19:31:52 -0400619 const Statement* s = node.statement()->get();
Brian Osmanc0213602020-10-06 14:43:32 -0400620 if (s->is<VarDeclaration>()) {
621 result[s->as<VarDeclaration>().fVar] = nullptr;
ethannicholas22f939e2016-10-13 13:25:34 -0700622 }
623 }
624 }
625 }
626 return result;
627}
628
Ethan Nicholascb670962017-04-20 19:31:52 -0400629/**
630 * Returns true if assigning to this lvalue has no effect.
631 */
632static bool is_dead(const Expression& lvalue) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400633 switch (lvalue.kind()) {
634 case Expression::Kind::kVariableReference:
Ethan Nicholas78686922020-10-08 06:46:27 -0400635 return lvalue.as<VariableReference>().variable()->dead();
Ethan Nicholase6592142020-09-08 10:22:09 -0400636 case Expression::Kind::kSwizzle:
John Stilesa5a97b42020-08-18 11:19:07 -0400637 return is_dead(*lvalue.as<Swizzle>().fBase);
Ethan Nicholase6592142020-09-08 10:22:09 -0400638 case Expression::Kind::kFieldAccess:
John Stilesa5a97b42020-08-18 11:19:07 -0400639 return is_dead(*lvalue.as<FieldAccess>().fBase);
Ethan Nicholase6592142020-09-08 10:22:09 -0400640 case Expression::Kind::kIndex: {
John Stilesa5a97b42020-08-18 11:19:07 -0400641 const IndexExpression& idx = lvalue.as<IndexExpression>();
Ethan Nicholas2a099da2020-01-02 14:40:54 -0500642 return is_dead(*idx.fBase) &&
643 !idx.fIndex->hasProperty(Expression::Property::kSideEffects);
Ethan Nicholascb670962017-04-20 19:31:52 -0400644 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400645 case Expression::Kind::kTernary: {
John Stilesa5a97b42020-08-18 11:19:07 -0400646 const TernaryExpression& t = lvalue.as<TernaryExpression>();
Ethan Nicholasdd218162020-10-08 05:48:01 -0400647 return !t.test()->hasSideEffects() && is_dead(*t.ifTrue()) && is_dead(*t.ifFalse());
Ethan Nicholasa583b812018-01-18 13:32:11 -0500648 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400649 case Expression::Kind::kExternalValue:
Ethan Nicholas91164d12019-05-15 15:29:54 -0400650 return false;
Ethan Nicholascb670962017-04-20 19:31:52 -0400651 default:
Ethan Nicholas2a099da2020-01-02 14:40:54 -0500652#ifdef SK_DEBUG
Ethan Nicholascb670962017-04-20 19:31:52 -0400653 ABORT("invalid lvalue: %s\n", lvalue.description().c_str());
Ethan Nicholas2a099da2020-01-02 14:40:54 -0500654#endif
655 return false;
Ethan Nicholascb670962017-04-20 19:31:52 -0400656 }
657}
ethannicholas22f939e2016-10-13 13:25:34 -0700658
Ethan Nicholascb670962017-04-20 19:31:52 -0400659/**
660 * Returns true if this is an assignment which can be collapsed down to just the right hand side due
661 * to a dead target and lack of side effects on the left hand side.
662 */
663static bool dead_assignment(const BinaryExpression& b) {
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400664 if (!Compiler::IsAssignment(b.getOperator())) {
Ethan Nicholascb670962017-04-20 19:31:52 -0400665 return false;
666 }
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400667 return is_dead(b.left());
Ethan Nicholascb670962017-04-20 19:31:52 -0400668}
669
670void Compiler::computeDataFlow(CFG* cfg) {
671 cfg->fBlocks[cfg->fStart].fBefore = compute_start_state(*cfg);
John Stilese6150002020-10-05 12:03:53 -0400672
673 // We set bits in the "processed" set after a block has been scanned.
674 SkBitSet processedSet(cfg->fBlocks.size());
675 while (SkBitSet::OptionalIndex blockId = processedSet.findFirstUnset()) {
676 processedSet.set(*blockId);
677 this->scanCFG(cfg, *blockId, &processedSet);
ethannicholas22f939e2016-10-13 13:25:34 -0700678 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400679}
680
681/**
682 * Attempts to replace the expression pointed to by iter with a new one (in both the CFG and the
683 * IR). If the expression can be cleanly removed, returns true and updates the iterator to point to
684 * the newly-inserted element. Otherwise updates only the IR and returns false (and the CFG will
685 * need to be regenerated).
686 */
John Stilesafbf8992020-08-18 10:08:21 -0400687static bool try_replace_expression(BasicBlock* b,
688 std::vector<BasicBlock::Node>::iterator* iter,
689 std::unique_ptr<Expression>* newExpression) {
Ethan Nicholascb670962017-04-20 19:31:52 -0400690 std::unique_ptr<Expression>* target = (*iter)->expression();
691 if (!b->tryRemoveExpression(iter)) {
692 *target = std::move(*newExpression);
693 return false;
694 }
695 *target = std::move(*newExpression);
696 return b->tryInsertExpression(iter, target);
697}
698
699/**
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400700 * Returns true if the expression is a constant numeric literal with the specified value, or a
701 * constant vector with all elements equal to the specified value.
Ethan Nicholascb670962017-04-20 19:31:52 -0400702 */
Ethan Nicholasa3f22f12020-10-01 12:13:17 -0400703template <typename T = SKSL_FLOAT>
John Stiles9d944232020-08-19 09:56:49 -0400704static bool is_constant(const Expression& expr, T value) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400705 switch (expr.kind()) {
706 case Expression::Kind::kIntLiteral:
Ethan Nicholase96cdd12020-09-28 16:27:18 -0400707 return expr.as<IntLiteral>().value() == value;
John Stiles9d944232020-08-19 09:56:49 -0400708
Ethan Nicholase6592142020-09-08 10:22:09 -0400709 case Expression::Kind::kFloatLiteral:
Ethan Nicholasa3f22f12020-10-01 12:13:17 -0400710 return expr.as<FloatLiteral>().value() == value;
John Stiles9d944232020-08-19 09:56:49 -0400711
Ethan Nicholase6592142020-09-08 10:22:09 -0400712 case Expression::Kind::kConstructor: {
John Stiles9d944232020-08-19 09:56:49 -0400713 const Constructor& constructor = expr.as<Constructor>();
714 if (constructor.isCompileTimeConstant()) {
Ethan Nicholas30d30222020-09-11 12:27:26 -0400715 const Type& constructorType = constructor.type();
716 bool isFloat = constructorType.columns() > 1
717 ? constructorType.componentType().isFloat()
718 : constructorType.isFloat();
719 switch (constructorType.typeKind()) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400720 case Type::TypeKind::kVector:
Ethan Nicholas30d30222020-09-11 12:27:26 -0400721 for (int i = 0; i < constructorType.columns(); ++i) {
John Stiles9d944232020-08-19 09:56:49 -0400722 if (isFloat) {
723 if (constructor.getFVecComponent(i) != value) {
724 return false;
725 }
726 } else {
727 if (constructor.getIVecComponent(i) != value) {
728 return false;
729 }
730 }
Ethan Nicholasd188c182019-06-10 15:55:38 -0400731 }
John Stiles9d944232020-08-19 09:56:49 -0400732 return true;
733
Ethan Nicholase6592142020-09-08 10:22:09 -0400734 case Type::TypeKind::kScalar:
Ethan Nicholasf70f0442020-09-29 12:41:35 -0400735 SkASSERT(constructor.arguments().size() == 1);
736 return is_constant<T>(*constructor.arguments()[0], value);
John Stiles9d944232020-08-19 09:56:49 -0400737
738 default:
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400739 return false;
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400740 }
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400741 }
742 return false;
743 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400744 default:
745 return false;
746 }
747}
748
749/**
750 * Collapses the binary expression pointed to by iter down to just the right side (in both the IR
751 * and CFG structures).
752 */
John Stilesafbf8992020-08-18 10:08:21 -0400753static void delete_left(BasicBlock* b,
754 std::vector<BasicBlock::Node>::iterator* iter,
755 bool* outUpdated,
756 bool* outNeedsRescan) {
Ethan Nicholascb670962017-04-20 19:31:52 -0400757 *outUpdated = true;
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400758 std::unique_ptr<Expression>* target = (*iter)->expression();
John Stilesa5a97b42020-08-18 11:19:07 -0400759 BinaryExpression& bin = (*target)->as<BinaryExpression>();
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400760 Expression& left = bin.left();
761 std::unique_ptr<Expression>& rightPointer = bin.rightPointer();
762 SkASSERT(!left.hasSideEffects());
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400763 bool result;
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400764 if (bin.getOperator() == Token::Kind::TK_EQ) {
765 result = b->tryRemoveLValueBefore(iter, &left);
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400766 } else {
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400767 result = b->tryRemoveExpressionBefore(iter, &left);
Ethan Nicholascb670962017-04-20 19:31:52 -0400768 }
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400769 *target = std::move(rightPointer);
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400770 if (!result) {
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400771 *outNeedsRescan = true;
772 return;
773 }
774 if (*iter == b->fNodes.begin()) {
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400775 *outNeedsRescan = true;
776 return;
777 }
778 --(*iter);
John Stiles70025e52020-09-28 16:08:58 -0400779 if (!(*iter)->isExpression() || (*iter)->expression() != &rightPointer) {
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400780 *outNeedsRescan = true;
781 return;
782 }
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400783 *iter = b->fNodes.erase(*iter);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400784 SkASSERT((*iter)->expression() == target);
Ethan Nicholascb670962017-04-20 19:31:52 -0400785}
786
787/**
788 * Collapses the binary expression pointed to by iter down to just the left side (in both the IR and
789 * CFG structures).
790 */
John Stilesafbf8992020-08-18 10:08:21 -0400791static void delete_right(BasicBlock* b,
792 std::vector<BasicBlock::Node>::iterator* iter,
793 bool* outUpdated,
794 bool* outNeedsRescan) {
Ethan Nicholascb670962017-04-20 19:31:52 -0400795 *outUpdated = true;
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400796 std::unique_ptr<Expression>* target = (*iter)->expression();
John Stilesa5a97b42020-08-18 11:19:07 -0400797 BinaryExpression& bin = (*target)->as<BinaryExpression>();
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400798 std::unique_ptr<Expression>& leftPointer = bin.leftPointer();
799 Expression& right = bin.right();
800 SkASSERT(!right.hasSideEffects());
801 if (!b->tryRemoveExpressionBefore(iter, &right)) {
802 *target = std::move(leftPointer);
Ethan Nicholascb670962017-04-20 19:31:52 -0400803 *outNeedsRescan = true;
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400804 return;
Ethan Nicholascb670962017-04-20 19:31:52 -0400805 }
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400806 *target = std::move(leftPointer);
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400807 if (*iter == b->fNodes.begin()) {
808 *outNeedsRescan = true;
809 return;
810 }
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400811 --(*iter);
John Stiles70025e52020-09-28 16:08:58 -0400812 if ((!(*iter)->isExpression() || (*iter)->expression() != &leftPointer)) {
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400813 *outNeedsRescan = true;
814 return;
815 }
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400816 *iter = b->fNodes.erase(*iter);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400817 SkASSERT((*iter)->expression() == target);
Ethan Nicholascb670962017-04-20 19:31:52 -0400818}
819
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400820/**
821 * Constructs the specified type using a single argument.
822 */
Ethan Nicholas30d30222020-09-11 12:27:26 -0400823static std::unique_ptr<Expression> construct(const Type* type, std::unique_ptr<Expression> v) {
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400824 std::vector<std::unique_ptr<Expression>> args;
825 args.push_back(std::move(v));
Ethan Nicholase6592142020-09-08 10:22:09 -0400826 std::unique_ptr<Expression> result = std::make_unique<Constructor>(-1, type, std::move(args));
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400827 return result;
828}
829
830/**
831 * Used in the implementations of vectorize_left and vectorize_right. Given a vector type and an
832 * expression x, deletes the expression pointed to by iter and replaces it with <type>(x).
833 */
834static void vectorize(BasicBlock* b,
835 std::vector<BasicBlock::Node>::iterator* iter,
836 const Type& type,
837 std::unique_ptr<Expression>* otherExpression,
838 bool* outUpdated,
839 bool* outNeedsRescan) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400840 SkASSERT((*(*iter)->expression())->kind() == Expression::Kind::kBinary);
841 SkASSERT(type.typeKind() == Type::TypeKind::kVector);
Ethan Nicholas30d30222020-09-11 12:27:26 -0400842 SkASSERT((*otherExpression)->type().typeKind() == Type::TypeKind::kScalar);
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400843 *outUpdated = true;
844 std::unique_ptr<Expression>* target = (*iter)->expression();
845 if (!b->tryRemoveExpression(iter)) {
Ethan Nicholas30d30222020-09-11 12:27:26 -0400846 *target = construct(&type, std::move(*otherExpression));
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400847 *outNeedsRescan = true;
848 } else {
Ethan Nicholas30d30222020-09-11 12:27:26 -0400849 *target = construct(&type, std::move(*otherExpression));
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400850 if (!b->tryInsertExpression(iter, target)) {
851 *outNeedsRescan = true;
852 }
853 }
854}
855
856/**
857 * Given a binary expression of the form x <op> vec<n>(y), deletes the right side and vectorizes the
858 * left to yield vec<n>(x).
859 */
860static void vectorize_left(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.right().type(), &bin.leftPointer(), outUpdated, outNeedsRescan);
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400866}
867
868/**
869 * Given a binary expression of the form vec<n>(x) <op> y, deletes the left side and vectorizes the
870 * right to yield vec<n>(y).
871 */
872static void vectorize_right(BasicBlock* b,
873 std::vector<BasicBlock::Node>::iterator* iter,
874 bool* outUpdated,
875 bool* outNeedsRescan) {
John Stilesa5a97b42020-08-18 11:19:07 -0400876 BinaryExpression& bin = (*(*iter)->expression())->as<BinaryExpression>();
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400877 vectorize(b, iter, bin.left().type(), &bin.rightPointer(), outUpdated, outNeedsRescan);
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400878}
879
880// Mark that an expression which we were writing to is no longer being written to
John Stilesa5a97b42020-08-18 11:19:07 -0400881static void clear_write(Expression& expr) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400882 switch (expr.kind()) {
883 case Expression::Kind::kVariableReference: {
John Stilesa5a97b42020-08-18 11:19:07 -0400884 expr.as<VariableReference>().setRefKind(VariableReference::kRead_RefKind);
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400885 break;
886 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400887 case Expression::Kind::kFieldAccess:
John Stilesa5a97b42020-08-18 11:19:07 -0400888 clear_write(*expr.as<FieldAccess>().fBase);
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400889 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400890 case Expression::Kind::kSwizzle:
John Stilesa5a97b42020-08-18 11:19:07 -0400891 clear_write(*expr.as<Swizzle>().fBase);
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400892 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400893 case Expression::Kind::kIndex:
John Stilesa5a97b42020-08-18 11:19:07 -0400894 clear_write(*expr.as<IndexExpression>().fBase);
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400895 break;
896 default:
897 ABORT("shouldn't be writing to this kind of expression\n");
898 break;
899 }
900}
901
Ethan Nicholascb670962017-04-20 19:31:52 -0400902void Compiler::simplifyExpression(DefinitionMap& definitions,
903 BasicBlock& b,
904 std::vector<BasicBlock::Node>::iterator* iter,
905 std::unordered_set<const Variable*>* undefinedVariables,
906 bool* outUpdated,
907 bool* outNeedsRescan) {
908 Expression* expr = (*iter)->expression()->get();
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400909 SkASSERT(expr);
Ethan Nicholascb670962017-04-20 19:31:52 -0400910 if ((*iter)->fConstantPropagation) {
911 std::unique_ptr<Expression> optimized = expr->constantPropagate(*fIRGenerator, definitions);
912 if (optimized) {
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400913 *outUpdated = true;
Ethan Nicholas30d30222020-09-11 12:27:26 -0400914 optimized = fIRGenerator->coerce(std::move(optimized), expr->type());
Ethan Nicholas1d881522020-09-11 09:32:54 -0400915 SkASSERT(optimized);
Ethan Nicholascb670962017-04-20 19:31:52 -0400916 if (!try_replace_expression(&b, iter, &optimized)) {
917 *outNeedsRescan = true;
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400918 return;
Ethan Nicholascb670962017-04-20 19:31:52 -0400919 }
John Stiles70025e52020-09-28 16:08:58 -0400920 SkASSERT((*iter)->isExpression());
Ethan Nicholascb670962017-04-20 19:31:52 -0400921 expr = (*iter)->expression()->get();
Ethan Nicholascb670962017-04-20 19:31:52 -0400922 }
923 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400924 switch (expr->kind()) {
925 case Expression::Kind::kVariableReference: {
John Stilesa5a97b42020-08-18 11:19:07 -0400926 const VariableReference& ref = expr->as<VariableReference>();
Ethan Nicholas78686922020-10-08 06:46:27 -0400927 const Variable* var = ref.variable();
Ethan Nicholas8f7e28f2018-03-26 14:24:27 -0400928 if (ref.refKind() != VariableReference::kWrite_RefKind &&
929 ref.refKind() != VariableReference::kPointer_RefKind &&
Ethan Nicholas041fd0a2020-10-07 16:42:04 -0400930 var->storage() == Variable::kLocal_Storage && !definitions[var] &&
Brian Osman79457ef2020-09-24 15:01:27 -0400931 (*undefinedVariables).find(var) == (*undefinedVariables).end()) {
932 (*undefinedVariables).insert(var);
Ethan Nicholas82a62d22017-11-07 14:42:10 +0000933 this->error(expr->fOffset,
Ethan Nicholase2c49992020-10-05 11:49:11 -0400934 "'" + var->name() + "' has not been assigned");
Ethan Nicholascb670962017-04-20 19:31:52 -0400935 }
936 break;
937 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400938 case Expression::Kind::kTernary: {
John Stiles403a3632020-08-20 12:11:48 -0400939 TernaryExpression* t = &expr->as<TernaryExpression>();
Ethan Nicholasdd218162020-10-08 05:48:01 -0400940 if (t->test()->is<BoolLiteral>()) {
Ethan Nicholascb670962017-04-20 19:31:52 -0400941 // ternary has a constant test, replace it with either the true or
942 // false branch
Ethan Nicholasdd218162020-10-08 05:48:01 -0400943 if (t->test()->as<BoolLiteral>().value()) {
944 (*iter)->setExpression(std::move(t->ifTrue()));
Ethan Nicholascb670962017-04-20 19:31:52 -0400945 } else {
Ethan Nicholasdd218162020-10-08 05:48:01 -0400946 (*iter)->setExpression(std::move(t->ifFalse()));
Ethan Nicholascb670962017-04-20 19:31:52 -0400947 }
948 *outUpdated = true;
949 *outNeedsRescan = true;
950 }
951 break;
952 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400953 case Expression::Kind::kBinary: {
John Stiles403a3632020-08-20 12:11:48 -0400954 BinaryExpression* bin = &expr->as<BinaryExpression>();
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400955 if (dead_assignment(*bin)) {
956 delete_left(&b, iter, outUpdated, outNeedsRescan);
957 break;
958 }
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400959 Expression& left = bin->left();
960 Expression& right = bin->right();
961 const Type& leftType = left.type();
962 const Type& rightType = right.type();
Ethan Nicholasc2371a42017-05-05 10:04:06 -0400963 // collapse useless expressions like x * 1 or x + 0
Ethan Nicholas30d30222020-09-11 12:27:26 -0400964 if (((leftType.typeKind() != Type::TypeKind::kScalar) &&
965 (leftType.typeKind() != Type::TypeKind::kVector)) ||
966 ((rightType.typeKind() != Type::TypeKind::kScalar) &&
967 (rightType.typeKind() != Type::TypeKind::kVector))) {
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400968 break;
969 }
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400970 switch (bin->getOperator()) {
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -0400971 case Token::Kind::TK_STAR:
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400972 if (is_constant(left, 1)) {
Ethan Nicholas30d30222020-09-11 12:27:26 -0400973 if (leftType.typeKind() == Type::TypeKind::kVector &&
974 rightType.typeKind() == Type::TypeKind::kScalar) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400975 // float4(1) * x -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400976 vectorize_right(&b, iter, outUpdated, outNeedsRescan);
977 } else {
978 // 1 * x -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400979 // 1 * float4(x) -> float4(x)
980 // float4(1) * float4(x) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400981 delete_left(&b, iter, outUpdated, outNeedsRescan);
982 }
983 }
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400984 else if (is_constant(left, 0)) {
Ethan Nicholas30d30222020-09-11 12:27:26 -0400985 if (leftType.typeKind() == Type::TypeKind::kScalar &&
986 rightType.typeKind() == Type::TypeKind::kVector &&
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400987 !right.hasSideEffects()) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400988 // 0 * float4(x) -> float4(0)
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400989 vectorize_left(&b, iter, outUpdated, outNeedsRescan);
990 } else {
991 // 0 * x -> 0
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400992 // float4(0) * x -> float4(0)
993 // float4(0) * float4(x) -> float4(0)
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400994 if (!right.hasSideEffects()) {
Ethan Nicholas51493ee2017-12-11 12:34:33 -0500995 delete_right(&b, iter, outUpdated, outNeedsRescan);
996 }
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400997 }
Ethan Nicholascb670962017-04-20 19:31:52 -0400998 }
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400999 else if (is_constant(right, 1)) {
Ethan Nicholas30d30222020-09-11 12:27:26 -04001000 if (leftType.typeKind() == Type::TypeKind::kScalar &&
1001 rightType.typeKind() == Type::TypeKind::kVector) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001002 // x * float4(1) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001003 vectorize_left(&b, iter, outUpdated, outNeedsRescan);
1004 } else {
1005 // x * 1 -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001006 // float4(x) * 1 -> float4(x)
1007 // float4(x) * float4(1) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001008 delete_right(&b, iter, outUpdated, outNeedsRescan);
1009 }
1010 }
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -04001011 else if (is_constant(right, 0)) {
Ethan Nicholas30d30222020-09-11 12:27:26 -04001012 if (leftType.typeKind() == Type::TypeKind::kVector &&
1013 rightType.typeKind() == Type::TypeKind::kScalar &&
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -04001014 !left.hasSideEffects()) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001015 // float4(x) * 0 -> float4(0)
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001016 vectorize_right(&b, iter, outUpdated, outNeedsRescan);
1017 } else {
1018 // x * 0 -> 0
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001019 // x * float4(0) -> float4(0)
1020 // float4(x) * float4(0) -> float4(0)
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -04001021 if (!left.hasSideEffects()) {
Ethan Nicholas51493ee2017-12-11 12:34:33 -05001022 delete_left(&b, iter, outUpdated, outNeedsRescan);
1023 }
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001024 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001025 }
1026 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001027 case Token::Kind::TK_PLUS:
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -04001028 if (is_constant(left, 0)) {
Ethan Nicholas30d30222020-09-11 12:27:26 -04001029 if (leftType.typeKind() == Type::TypeKind::kVector &&
1030 rightType.typeKind() == Type::TypeKind::kScalar) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001031 // float4(0) + x -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001032 vectorize_right(&b, iter, outUpdated, outNeedsRescan);
1033 } else {
1034 // 0 + x -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001035 // 0 + float4(x) -> float4(x)
1036 // float4(0) + float4(x) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001037 delete_left(&b, iter, outUpdated, outNeedsRescan);
1038 }
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -04001039 } else if (is_constant(right, 0)) {
Ethan Nicholas30d30222020-09-11 12:27:26 -04001040 if (leftType.typeKind() == Type::TypeKind::kScalar &&
1041 rightType.typeKind() == Type::TypeKind::kVector) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001042 // x + float4(0) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001043 vectorize_left(&b, iter, outUpdated, outNeedsRescan);
1044 } else {
1045 // x + 0 -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001046 // float4(x) + 0 -> float4(x)
1047 // float4(x) + float4(0) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001048 delete_right(&b, iter, outUpdated, outNeedsRescan);
1049 }
Ethan Nicholas56e42712017-04-21 10:23:37 -04001050 }
1051 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001052 case Token::Kind::TK_MINUS:
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -04001053 if (is_constant(right, 0)) {
Ethan Nicholas30d30222020-09-11 12:27:26 -04001054 if (leftType.typeKind() == Type::TypeKind::kScalar &&
1055 rightType.typeKind() == Type::TypeKind::kVector) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001056 // x - float4(0) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001057 vectorize_left(&b, iter, outUpdated, outNeedsRescan);
1058 } else {
1059 // x - 0 -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001060 // float4(x) - 0 -> float4(x)
1061 // float4(x) - float4(0) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001062 delete_right(&b, iter, outUpdated, outNeedsRescan);
1063 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001064 }
1065 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001066 case Token::Kind::TK_SLASH:
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -04001067 if (is_constant(right, 1)) {
Ethan Nicholas30d30222020-09-11 12:27:26 -04001068 if (leftType.typeKind() == Type::TypeKind::kScalar &&
1069 rightType.typeKind() == Type::TypeKind::kVector) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001070 // x / float4(1) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001071 vectorize_left(&b, iter, outUpdated, outNeedsRescan);
1072 } else {
1073 // x / 1 -> x
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001074 // float4(x) / 1 -> float4(x)
1075 // float4(x) / float4(1) -> float4(x)
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001076 delete_right(&b, iter, outUpdated, outNeedsRescan);
1077 }
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -04001078 } else if (is_constant(left, 0)) {
Ethan Nicholas30d30222020-09-11 12:27:26 -04001079 if (leftType.typeKind() == Type::TypeKind::kScalar &&
1080 rightType.typeKind() == Type::TypeKind::kVector &&
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -04001081 !right.hasSideEffects()) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001082 // 0 / float4(x) -> float4(0)
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001083 vectorize_left(&b, iter, outUpdated, outNeedsRescan);
1084 } else {
1085 // 0 / x -> 0
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001086 // float4(0) / x -> float4(0)
1087 // float4(0) / float4(x) -> float4(0)
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -04001088 if (!right.hasSideEffects()) {
Ethan Nicholas51493ee2017-12-11 12:34:33 -05001089 delete_right(&b, iter, outUpdated, outNeedsRescan);
1090 }
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001091 }
1092 }
1093 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001094 case Token::Kind::TK_PLUSEQ:
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -04001095 if (is_constant(right, 0)) {
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_MINUSEQ:
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -04001101 if (is_constant(right, 0)) {
1102 clear_write(left);
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001103 delete_right(&b, iter, outUpdated, outNeedsRescan);
1104 }
1105 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001106 case Token::Kind::TK_STAREQ:
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -04001107 if (is_constant(right, 1)) {
1108 clear_write(left);
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001109 delete_right(&b, iter, outUpdated, outNeedsRescan);
1110 }
1111 break;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001112 case Token::Kind::TK_SLASHEQ:
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -04001113 if (is_constant(right, 1)) {
1114 clear_write(left);
Ethan Nicholascb670962017-04-20 19:31:52 -04001115 delete_right(&b, iter, outUpdated, outNeedsRescan);
1116 }
1117 break;
1118 default:
1119 break;
1120 }
Ethan Nicholas409f6f02019-09-17 12:34:39 -04001121 break;
1122 }
Ethan Nicholase6592142020-09-08 10:22:09 -04001123 case Expression::Kind::kSwizzle: {
John Stiles403a3632020-08-20 12:11:48 -04001124 Swizzle& s = expr->as<Swizzle>();
Ethan Nicholas409f6f02019-09-17 12:34:39 -04001125 // detect identity swizzles like foo.rgba
Ethan Nicholas30d30222020-09-11 12:27:26 -04001126 if ((int) s.fComponents.size() == s.fBase->type().columns()) {
Ethan Nicholas409f6f02019-09-17 12:34:39 -04001127 bool identity = true;
1128 for (int i = 0; i < (int) s.fComponents.size(); ++i) {
1129 if (s.fComponents[i] != i) {
1130 identity = false;
1131 break;
1132 }
1133 }
1134 if (identity) {
1135 *outUpdated = true;
1136 if (!try_replace_expression(&b, iter, &s.fBase)) {
1137 *outNeedsRescan = true;
1138 return;
1139 }
John Stiles70025e52020-09-28 16:08:58 -04001140 SkASSERT((*iter)->isExpression());
Ethan Nicholas409f6f02019-09-17 12:34:39 -04001141 break;
1142 }
1143 }
1144 // detect swizzles of swizzles, e.g. replace foo.argb.r000 with foo.a000
Ethan Nicholase6592142020-09-08 10:22:09 -04001145 if (s.fBase->kind() == Expression::Kind::kSwizzle) {
John Stilesa5a97b42020-08-18 11:19:07 -04001146 Swizzle& base = s.fBase->as<Swizzle>();
Ethan Nicholas409f6f02019-09-17 12:34:39 -04001147 std::vector<int> final;
1148 for (int c : s.fComponents) {
Brian Osman25647672020-09-15 15:16:56 -04001149 final.push_back(base.fComponents[c]);
Ethan Nicholas409f6f02019-09-17 12:34:39 -04001150 }
1151 *outUpdated = true;
1152 std::unique_ptr<Expression> replacement(new Swizzle(*fContext, base.fBase->clone(),
1153 std::move(final)));
1154 if (!try_replace_expression(&b, iter, &replacement)) {
1155 *outNeedsRescan = true;
1156 return;
1157 }
John Stiles70025e52020-09-28 16:08:58 -04001158 SkASSERT((*iter)->isExpression());
Ethan Nicholas409f6f02019-09-17 12:34:39 -04001159 }
John Stiles30212b72020-06-11 17:55:07 -04001160 break;
Ethan Nicholascb670962017-04-20 19:31:52 -04001161 }
1162 default:
1163 break;
1164 }
1165}
1166
John Stiles92219b42020-06-15 12:32:24 -04001167// Returns true if this statement could potentially execute a break at the current level. We ignore
1168// nested loops and switches, since any breaks inside of them will merely break the loop / switch.
John Stilesb92641c2020-08-31 18:09:01 -04001169static bool contains_conditional_break(Statement& stmt) {
1170 class ContainsConditionalBreak : public ProgramVisitor {
1171 public:
1172 bool visitStatement(const Statement& stmt) override {
Ethan Nicholase6592142020-09-08 10:22:09 -04001173 switch (stmt.kind()) {
1174 case Statement::Kind::kBlock:
John Stilesb92641c2020-08-31 18:09:01 -04001175 return this->INHERITED::visitStatement(stmt);
1176
Ethan Nicholase6592142020-09-08 10:22:09 -04001177 case Statement::Kind::kBreak:
John Stilesb92641c2020-08-31 18:09:01 -04001178 return fInConditional > 0;
1179
Ethan Nicholase6592142020-09-08 10:22:09 -04001180 case Statement::Kind::kIf: {
John Stilesb92641c2020-08-31 18:09:01 -04001181 ++fInConditional;
1182 bool result = this->INHERITED::visitStatement(stmt);
1183 --fInConditional;
1184 return result;
1185 }
1186
1187 default:
1188 return false;
1189 }
1190 }
1191
1192 int fInConditional = 0;
1193 using INHERITED = ProgramVisitor;
1194 };
1195
1196 return ContainsConditionalBreak{}.visitStatement(stmt);
John Stiles92219b42020-06-15 12:32:24 -04001197}
1198
Ethan Nicholas5005a222018-08-24 13:06:27 -04001199// returns true if this statement definitely executes a break at the current level (we ignore
1200// nested loops and switches, since any breaks inside of them will merely break the loop / switch)
John Stilesb92641c2020-08-31 18:09:01 -04001201static bool contains_unconditional_break(Statement& stmt) {
1202 class ContainsUnconditionalBreak : public ProgramVisitor {
1203 public:
1204 bool visitStatement(const Statement& stmt) override {
Ethan Nicholase6592142020-09-08 10:22:09 -04001205 switch (stmt.kind()) {
1206 case Statement::Kind::kBlock:
John Stilesb92641c2020-08-31 18:09:01 -04001207 return this->INHERITED::visitStatement(stmt);
1208
Ethan Nicholase6592142020-09-08 10:22:09 -04001209 case Statement::Kind::kBreak:
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001210 return true;
John Stilesb92641c2020-08-31 18:09:01 -04001211
1212 default:
1213 return false;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001214 }
John Stilesb92641c2020-08-31 18:09:01 -04001215 }
John Stiles92219b42020-06-15 12:32:24 -04001216
John Stilesb92641c2020-08-31 18:09:01 -04001217 using INHERITED = ProgramVisitor;
1218 };
John Stiles92219b42020-06-15 12:32:24 -04001219
John Stilesb92641c2020-08-31 18:09:01 -04001220 return ContainsUnconditionalBreak{}.visitStatement(stmt);
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001221}
1222
John Stiles92219b42020-06-15 12:32:24 -04001223static void move_all_but_break(std::unique_ptr<Statement>& stmt,
1224 std::vector<std::unique_ptr<Statement>>* target) {
Ethan Nicholase6592142020-09-08 10:22:09 -04001225 switch (stmt->kind()) {
1226 case Statement::Kind::kBlock: {
John Stiles92219b42020-06-15 12:32:24 -04001227 // Recurse into the block.
1228 Block& block = static_cast<Block&>(*stmt);
1229
1230 std::vector<std::unique_ptr<Statement>> blockStmts;
Ethan Nicholas7bd60432020-09-25 14:31:59 -04001231 blockStmts.reserve(block.children().size());
1232 for (std::unique_ptr<Statement>& stmt : block.children()) {
1233 move_all_but_break(stmt, &blockStmts);
Ethan Nicholas739e1ca2020-06-11 12:16:14 -04001234 }
John Stiles92219b42020-06-15 12:32:24 -04001235
1236 target->push_back(std::make_unique<Block>(block.fOffset, std::move(blockStmts),
Ethan Nicholas7bd60432020-09-25 14:31:59 -04001237 block.symbolTable(), block.isScope()));
Ethan Nicholas739e1ca2020-06-11 12:16:14 -04001238 break;
John Stiles92219b42020-06-15 12:32:24 -04001239 }
1240
Ethan Nicholase6592142020-09-08 10:22:09 -04001241 case Statement::Kind::kBreak:
John Stiles92219b42020-06-15 12:32:24 -04001242 // Do not append a break to the target.
1243 break;
1244
Ethan Nicholas739e1ca2020-06-11 12:16:14 -04001245 default:
John Stiles92219b42020-06-15 12:32:24 -04001246 // Append normal statements to the target.
1247 target->push_back(std::move(stmt));
1248 break;
Ethan Nicholas739e1ca2020-06-11 12:16:14 -04001249 }
1250}
1251
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001252// Returns a block containing all of the statements that will be run if the given case matches
1253// (which, owing to the statements being owned by unique_ptrs, means the switch itself will be
1254// broken by this call and must then be discarded).
1255// Returns null (and leaves the switch unmodified) if no such simple reduction is possible, such as
1256// when break statements appear inside conditionals.
John Stiles92219b42020-06-15 12:32:24 -04001257static std::unique_ptr<Statement> block_for_case(SwitchStatement* switchStatement,
1258 SwitchCase* caseToCapture) {
1259 // We have to be careful to not move any of the pointers until after we're sure we're going to
1260 // succeed, so before we make any changes at all, we check the switch-cases to decide on a plan
1261 // of action. First, find the switch-case we are interested in.
1262 auto iter = switchStatement->fCases.begin();
1263 for (; iter != switchStatement->fCases.end(); ++iter) {
1264 if (iter->get() == caseToCapture) {
1265 break;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001266 }
John Stiles92219b42020-06-15 12:32:24 -04001267 }
1268
1269 // Next, walk forward through the rest of the switch. If we find a conditional break, we're
1270 // stuck and can't simplify at all. If we find an unconditional break, we have a range of
1271 // statements that we can use for simplification.
1272 auto startIter = iter;
1273 Statement* unconditionalBreakStmt = nullptr;
1274 for (; iter != switchStatement->fCases.end(); ++iter) {
1275 for (std::unique_ptr<Statement>& stmt : (*iter)->fStatements) {
1276 if (contains_conditional_break(*stmt)) {
1277 // We can't reduce switch-cases to a block when they have conditional breaks.
1278 return nullptr;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001279 }
John Stiles92219b42020-06-15 12:32:24 -04001280
1281 if (contains_unconditional_break(*stmt)) {
1282 // We found an unconditional break. We can use this block, but we need to strip
1283 // out the break statement.
1284 unconditionalBreakStmt = stmt.get();
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001285 break;
1286 }
1287 }
John Stiles92219b42020-06-15 12:32:24 -04001288
1289 if (unconditionalBreakStmt != nullptr) {
1290 break;
1291 }
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001292 }
John Stiles92219b42020-06-15 12:32:24 -04001293
1294 // We fell off the bottom of the switch or encountered a break. We know the range of statements
1295 // that we need to move over, and we know it's safe to do so.
1296 std::vector<std::unique_ptr<Statement>> caseStmts;
1297
1298 // We can move over most of the statements as-is.
1299 while (startIter != iter) {
1300 for (std::unique_ptr<Statement>& stmt : (*startIter)->fStatements) {
1301 caseStmts.push_back(std::move(stmt));
1302 }
1303 ++startIter;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001304 }
John Stiles92219b42020-06-15 12:32:24 -04001305
1306 // If we found an unconditional break at the end, we need to move what we can while avoiding
1307 // that break.
1308 if (unconditionalBreakStmt != nullptr) {
1309 for (std::unique_ptr<Statement>& stmt : (*startIter)->fStatements) {
1310 if (stmt.get() == unconditionalBreakStmt) {
1311 move_all_but_break(stmt, &caseStmts);
1312 unconditionalBreakStmt = nullptr;
1313 break;
1314 }
1315
1316 caseStmts.push_back(std::move(stmt));
1317 }
1318 }
1319
1320 SkASSERT(unconditionalBreakStmt == nullptr); // Verify that we fixed the unconditional break.
1321
1322 // Return our newly-synthesized block.
1323 return std::make_unique<Block>(/*offset=*/-1, std::move(caseStmts), switchStatement->fSymbols);
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001324}
1325
Ethan Nicholascb670962017-04-20 19:31:52 -04001326void Compiler::simplifyStatement(DefinitionMap& definitions,
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001327 BasicBlock& b,
1328 std::vector<BasicBlock::Node>::iterator* iter,
1329 std::unordered_set<const Variable*>* undefinedVariables,
1330 bool* outUpdated,
1331 bool* outNeedsRescan) {
Ethan Nicholascb670962017-04-20 19:31:52 -04001332 Statement* stmt = (*iter)->statement()->get();
Ethan Nicholase6592142020-09-08 10:22:09 -04001333 switch (stmt->kind()) {
1334 case Statement::Kind::kVarDeclaration: {
John Stilesa5a97b42020-08-18 11:19:07 -04001335 const auto& varDecl = stmt->as<VarDeclaration>();
Ethan Nicholas82a62d22017-11-07 14:42:10 +00001336 if (varDecl.fVar->dead() &&
1337 (!varDecl.fValue ||
1338 !varDecl.fValue->hasSideEffects())) {
1339 if (varDecl.fValue) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001340 SkASSERT((*iter)->statement()->get() == stmt);
Ethan Nicholas82a62d22017-11-07 14:42:10 +00001341 if (!b.tryRemoveExpressionBefore(iter, varDecl.fValue.get())) {
1342 *outNeedsRescan = true;
Ethan Nicholascb670962017-04-20 19:31:52 -04001343 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001344 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001345 (*iter)->setStatement(std::unique_ptr<Statement>(new Nop()));
Ethan Nicholas82a62d22017-11-07 14:42:10 +00001346 *outUpdated = true;
Ethan Nicholascb670962017-04-20 19:31:52 -04001347 }
1348 break;
1349 }
Ethan Nicholase6592142020-09-08 10:22:09 -04001350 case Statement::Kind::kIf: {
John Stilesa5a97b42020-08-18 11:19:07 -04001351 IfStatement& i = stmt->as<IfStatement>();
Ethan Nicholas8c44eca2020-10-07 16:47:09 -04001352 if (i.test()->kind() == Expression::Kind::kBoolLiteral) {
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001353 // constant if, collapse down to a single branch
Ethan Nicholas8c44eca2020-10-07 16:47:09 -04001354 if (i.test()->as<BoolLiteral>().value()) {
1355 SkASSERT(i.ifTrue());
1356 (*iter)->setStatement(std::move(i.ifTrue()));
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001357 } else {
Ethan Nicholas8c44eca2020-10-07 16:47:09 -04001358 if (i.ifFalse()) {
1359 (*iter)->setStatement(std::move(i.ifFalse()));
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001360 } else {
1361 (*iter)->setStatement(std::unique_ptr<Statement>(new Nop()));
1362 }
1363 }
1364 *outUpdated = true;
1365 *outNeedsRescan = true;
1366 break;
1367 }
Ethan Nicholas8c44eca2020-10-07 16:47:09 -04001368 if (i.ifFalse() && i.ifFalse()->isEmpty()) {
Ethan Nicholascb670962017-04-20 19:31:52 -04001369 // else block doesn't do anything, remove it
Ethan Nicholas8c44eca2020-10-07 16:47:09 -04001370 i.ifFalse().reset();
Ethan Nicholascb670962017-04-20 19:31:52 -04001371 *outUpdated = true;
1372 *outNeedsRescan = true;
1373 }
Ethan Nicholas8c44eca2020-10-07 16:47:09 -04001374 if (!i.ifFalse() && i.ifTrue()->isEmpty()) {
Ethan Nicholascb670962017-04-20 19:31:52 -04001375 // if block doesn't do anything, no else block
Ethan Nicholas8c44eca2020-10-07 16:47:09 -04001376 if (i.test()->hasSideEffects()) {
Ethan Nicholascb670962017-04-20 19:31:52 -04001377 // test has side effects, keep it
1378 (*iter)->setStatement(std::unique_ptr<Statement>(
Ethan Nicholas8c44eca2020-10-07 16:47:09 -04001379 new ExpressionStatement(std::move(i.test()))));
Ethan Nicholascb670962017-04-20 19:31:52 -04001380 } else {
1381 // no if, no else, no test side effects, kill the whole if
1382 // statement
1383 (*iter)->setStatement(std::unique_ptr<Statement>(new Nop()));
1384 }
1385 *outUpdated = true;
1386 *outNeedsRescan = true;
1387 }
1388 break;
1389 }
Ethan Nicholase6592142020-09-08 10:22:09 -04001390 case Statement::Kind::kSwitch: {
John Stilesa5a97b42020-08-18 11:19:07 -04001391 SwitchStatement& s = stmt->as<SwitchStatement>();
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001392 int64_t switchValue;
1393 if (fIRGenerator->getConstantInt(*s.fValue, &switchValue)) {
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001394 // switch is constant, replace it with the case that matches
1395 bool found = false;
1396 SwitchCase* defaultCase = nullptr;
John Stiles9d944232020-08-19 09:56:49 -04001397 for (const std::unique_ptr<SwitchCase>& c : s.fCases) {
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001398 if (!c->fValue) {
1399 defaultCase = c.get();
1400 continue;
1401 }
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001402 int64_t caseValue;
1403 SkAssertResult(fIRGenerator->getConstantInt(*c->fValue, &caseValue));
1404 if (caseValue == switchValue) {
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001405 std::unique_ptr<Statement> newBlock = block_for_case(&s, c.get());
1406 if (newBlock) {
1407 (*iter)->setStatement(std::move(newBlock));
John Stiles9d944232020-08-19 09:56:49 -04001408 found = true;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001409 break;
1410 } else {
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -04001411 if (s.fIsStatic && !(fFlags & kPermitInvalidStaticTests_Flag)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001412 this->error(s.fOffset,
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001413 "static switch contains non-static conditional break");
1414 s.fIsStatic = false;
1415 }
1416 return; // can't simplify
1417 }
1418 }
1419 }
1420 if (!found) {
1421 // no matching case. use default if it exists, or kill the whole thing
1422 if (defaultCase) {
1423 std::unique_ptr<Statement> newBlock = block_for_case(&s, defaultCase);
1424 if (newBlock) {
1425 (*iter)->setStatement(std::move(newBlock));
1426 } else {
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -04001427 if (s.fIsStatic && !(fFlags & kPermitInvalidStaticTests_Flag)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001428 this->error(s.fOffset,
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001429 "static switch contains non-static conditional break");
1430 s.fIsStatic = false;
1431 }
1432 return; // can't simplify
1433 }
1434 } else {
1435 (*iter)->setStatement(std::unique_ptr<Statement>(new Nop()));
1436 }
1437 }
1438 *outUpdated = true;
1439 *outNeedsRescan = true;
1440 }
1441 break;
1442 }
Ethan Nicholase6592142020-09-08 10:22:09 -04001443 case Statement::Kind::kExpression: {
John Stilesa5a97b42020-08-18 11:19:07 -04001444 ExpressionStatement& e = stmt->as<ExpressionStatement>();
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001445 SkASSERT((*iter)->statement()->get() == &e);
Ethan Nicholasd503a5a2020-09-30 09:29:55 -04001446 if (!e.expression()->hasSideEffects()) {
Ethan Nicholascb670962017-04-20 19:31:52 -04001447 // Expression statement with no side effects, kill it
Ethan Nicholasd503a5a2020-09-30 09:29:55 -04001448 if (!b.tryRemoveExpressionBefore(iter, e.expression().get())) {
Ethan Nicholascb670962017-04-20 19:31:52 -04001449 *outNeedsRescan = true;
1450 }
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001451 SkASSERT((*iter)->statement()->get() == stmt);
Ethan Nicholascb670962017-04-20 19:31:52 -04001452 (*iter)->setStatement(std::unique_ptr<Statement>(new Nop()));
1453 *outUpdated = true;
1454 }
1455 break;
1456 }
1457 default:
1458 break;
1459 }
1460}
1461
John Stiles0cc193a2020-09-09 09:39:34 -04001462bool Compiler::scanCFG(FunctionDefinition& f) {
1463 bool madeChanges = false;
1464
Ethan Nicholascb670962017-04-20 19:31:52 -04001465 CFG cfg = CFGGenerator().getCFG(f);
1466 this->computeDataFlow(&cfg);
ethannicholas22f939e2016-10-13 13:25:34 -07001467
1468 // check for unreachable code
1469 for (size_t i = 0; i < cfg.fBlocks.size(); i++) {
John Stiles0cc193a2020-09-09 09:39:34 -04001470 const BasicBlock& block = cfg.fBlocks[i];
John Stiles61e75e32020-10-01 15:42:37 -04001471 if (i != cfg.fStart && !block.fIsReachable && block.fNodes.size()) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001472 int offset;
John Stiles0cc193a2020-09-09 09:39:34 -04001473 const BasicBlock::Node& node = block.fNodes[0];
John Stiles70025e52020-09-28 16:08:58 -04001474 if (node.isStatement()) {
1475 offset = (*node.statement())->fOffset;
1476 } else {
1477 offset = (*node.expression())->fOffset;
1478 if ((*node.expression())->is<BoolLiteral>()) {
1479 // Function inlining can generate do { ... } while(false) loops which always
1480 // break, so the boolean condition is considered unreachable. Since not being
1481 // able to reach a literal is a non-issue in the first place, we don't report an
1482 // error in this case.
1483 continue;
1484 }
Ethan Nicholas86a43402017-01-19 13:32:00 -05001485 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001486 this->error(offset, String("unreachable"));
ethannicholas22f939e2016-10-13 13:25:34 -07001487 }
1488 }
1489 if (fErrorCount) {
John Stiles0cc193a2020-09-09 09:39:34 -04001490 return madeChanges;
ethannicholas22f939e2016-10-13 13:25:34 -07001491 }
1492
Ethan Nicholascb670962017-04-20 19:31:52 -04001493 // check for dead code & undefined variables, perform constant propagation
1494 std::unordered_set<const Variable*> undefinedVariables;
1495 bool updated;
1496 bool needsRescan = false;
1497 do {
1498 if (needsRescan) {
1499 cfg = CFGGenerator().getCFG(f);
1500 this->computeDataFlow(&cfg);
1501 needsRescan = false;
Ethan Nicholas113628d2017-02-02 16:11:39 -05001502 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001503
1504 updated = false;
Ethan Nicholas1de14812020-06-19 15:32:49 -04001505 bool first = true;
Ethan Nicholascb670962017-04-20 19:31:52 -04001506 for (BasicBlock& b : cfg.fBlocks) {
John Stiles61e75e32020-10-01 15:42:37 -04001507 if (!first && !b.fIsReachable) {
Ethan Nicholas1de14812020-06-19 15:32:49 -04001508 // Block was reachable before optimization, but has since become unreachable. In
1509 // addition to being dead code, it's broken - since control flow can't reach it, no
1510 // prior variable definitions can reach it, and therefore variables might look to
Brian Osmanc0213602020-10-06 14:43:32 -04001511 // have not been properly assigned. Kill it by replacing all statements with Nops.
Ethan Nicholas1de14812020-06-19 15:32:49 -04001512 for (BasicBlock::Node& node : b.fNodes) {
John Stiles70025e52020-09-28 16:08:58 -04001513 if (node.isStatement() && !(*node.statement())->is<Nop>()) {
John Stiles0cc193a2020-09-09 09:39:34 -04001514 node.setStatement(std::make_unique<Nop>());
1515 madeChanges = true;
Ethan Nicholas1de14812020-06-19 15:32:49 -04001516 }
1517 }
1518 continue;
1519 }
1520 first = false;
Ethan Nicholascb670962017-04-20 19:31:52 -04001521 DefinitionMap definitions = b.fBefore;
1522
1523 for (auto iter = b.fNodes.begin(); iter != b.fNodes.end() && !needsRescan; ++iter) {
John Stiles70025e52020-09-28 16:08:58 -04001524 if (iter->isExpression()) {
Ethan Nicholascb670962017-04-20 19:31:52 -04001525 this->simplifyExpression(definitions, b, &iter, &undefinedVariables, &updated,
1526 &needsRescan);
1527 } else {
1528 this->simplifyStatement(definitions, b, &iter, &undefinedVariables, &updated,
John Stiles0cc193a2020-09-09 09:39:34 -04001529 &needsRescan);
Ethan Nicholascb670962017-04-20 19:31:52 -04001530 }
Ethan Nicholas4b330df2017-05-17 10:52:55 -04001531 if (needsRescan) {
1532 break;
1533 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001534 this->addDefinitions(*iter, &definitions);
1535 }
Brian Osman01a3eb42020-09-14 11:32:49 -04001536
1537 if (needsRescan) {
1538 break;
1539 }
Ethan Nicholascb670962017-04-20 19:31:52 -04001540 }
John Stiles0cc193a2020-09-09 09:39:34 -04001541 madeChanges |= updated;
Ethan Nicholascb670962017-04-20 19:31:52 -04001542 } while (updated);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001543 SkASSERT(!needsRescan);
ethannicholas22f939e2016-10-13 13:25:34 -07001544
Ethan Nicholas91a10532017-06-22 11:24:38 -04001545 // verify static ifs & switches, clean up dead variable decls
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001546 for (BasicBlock& b : cfg.fBlocks) {
Ethan Nicholas91a10532017-06-22 11:24:38 -04001547 for (auto iter = b.fNodes.begin(); iter != b.fNodes.end() && !needsRescan;) {
John Stiles70025e52020-09-28 16:08:58 -04001548 if (iter->isStatement()) {
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001549 const Statement& s = **iter->statement();
Ethan Nicholase6592142020-09-08 10:22:09 -04001550 switch (s.kind()) {
1551 case Statement::Kind::kIf:
Ethan Nicholas8c44eca2020-10-07 16:47:09 -04001552 if (s.as<IfStatement>().isStatic() &&
Ethan Nicholas6e1cbc02017-07-14 10:12:15 -04001553 !(fFlags & kPermitInvalidStaticTests_Flag)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001554 this->error(s.fOffset, "static if has non-static test");
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001555 }
Ethan Nicholas91a10532017-06-22 11:24:38 -04001556 ++iter;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001557 break;
Ethan Nicholase6592142020-09-08 10:22:09 -04001558 case Statement::Kind::kSwitch:
John Stilesa5a97b42020-08-18 11:19:07 -04001559 if (s.as<SwitchStatement>().fIsStatic &&
John Stiles0cc193a2020-09-09 09:39:34 -04001560 !(fFlags & kPermitInvalidStaticTests_Flag)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001561 this->error(s.fOffset, "static switch has non-static test");
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001562 }
Ethan Nicholas91a10532017-06-22 11:24:38 -04001563 ++iter;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001564 break;
1565 default:
Ethan Nicholas91a10532017-06-22 11:24:38 -04001566 ++iter;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001567 break;
1568 }
Ethan Nicholas91a10532017-06-22 11:24:38 -04001569 } else {
1570 ++iter;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001571 }
1572 }
1573 }
1574
ethannicholas22f939e2016-10-13 13:25:34 -07001575 // check for missing return
Ethan Nicholased84b732020-10-08 11:45:44 -04001576 if (f.fDeclaration.returnType() != *fContext->fVoid_Type) {
John Stiles61e75e32020-10-01 15:42:37 -04001577 if (cfg.fBlocks[cfg.fExit].fIsReachable) {
Ethan Nicholase2c49992020-10-05 11:49:11 -04001578 this->error(f.fOffset, String("function '" + String(f.fDeclaration.name()) +
Ethan Nicholasdb80f692019-11-22 14:06:12 -05001579 "' can exit without returning a value"));
ethannicholas22f939e2016-10-13 13:25:34 -07001580 }
1581 }
John Stiles0cc193a2020-09-09 09:39:34 -04001582
1583 return madeChanges;
ethannicholas22f939e2016-10-13 13:25:34 -07001584}
1585
Brian Osman32d53552020-09-23 13:55:20 -04001586std::unique_ptr<Program> Compiler::convertProgram(
1587 Program::Kind kind,
1588 String text,
1589 const Program::Settings& settings,
1590 const std::vector<std::unique_ptr<ExternalValue>>* externalValues) {
1591 SkASSERT(!externalValues || (kind == Program::kGeneric_Kind));
Ethan Nicholas91164d12019-05-15 15:29:54 -04001592
ethannicholasb3058bd2016-07-01 08:22:01 -07001593 fErrorText = "";
1594 fErrorCount = 0;
Ethan Nicholas041fd0a2020-10-07 16:42:04 -04001595 fInliner.reset(&fIRGenerator->fContext, fIRGenerator->fModifiers.get(), &settings);
ethannicholasd598f792016-07-25 10:08:54 -07001596 std::vector<std::unique_ptr<ProgramElement>> elements;
ethannicholasb3058bd2016-07-01 08:22:01 -07001597 switch (kind) {
1598 case Program::kVertex_Kind:
Brian Osmanafa18ee2020-10-07 17:47:45 -04001599 fIRGenerator->fIntrinsics = fVertexIntrinsics.get();
Brian Osmane3dcb132020-10-08 10:27:29 -04001600 fIRGenerator->start(&settings, fVertexSymbolTable);
ethannicholasb3058bd2016-07-01 08:22:01 -07001601 break;
1602 case Program::kFragment_Kind:
Brian Osman00a8b5b2020-10-02 09:06:04 -04001603 fIRGenerator->fIntrinsics = fFragmentIntrinsics.get();
Brian Osmane3dcb132020-10-08 10:27:29 -04001604 fIRGenerator->start(&settings, fFragmentSymbolTable);
ethannicholasb3058bd2016-07-01 08:22:01 -07001605 break;
Ethan Nicholas52cad152017-02-16 16:37:32 -05001606 case Program::kGeometry_Kind:
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001607 this->loadGeometryIntrinsics();
Brian Osmanafa18ee2020-10-07 17:47:45 -04001608 fIRGenerator->fIntrinsics = fGeometryIntrinsics.get();
Brian Osmane3dcb132020-10-08 10:27:29 -04001609 fIRGenerator->start(&settings, fGeometrySymbolTable);
Ethan Nicholas52cad152017-02-16 16:37:32 -05001610 break;
Brian Osman8e2ef022020-09-30 13:26:43 -04001611 case Program::kFragmentProcessor_Kind:
1612 this->loadFPIntrinsics();
Brian Osman8e2ef022020-09-30 13:26:43 -04001613 fIRGenerator->fIntrinsics = fFPIntrinsics.get();
Brian Osmane3dcb132020-10-08 10:27:29 -04001614 fIRGenerator->start(&settings, fFPSymbolTable);
Ethan Nicholas762466e2017-06-29 10:03:38 -04001615 break;
Ethan Nicholas8da1e652019-05-24 11:01:59 -04001616 case Program::kPipelineStage_Kind:
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001617 this->loadPipelineIntrinsics();
Brian Osman8e2ef022020-09-30 13:26:43 -04001618 fIRGenerator->fIntrinsics = fPipelineIntrinsics.get();
Brian Osmane3dcb132020-10-08 10:27:29 -04001619 fIRGenerator->start(&settings, fPipelineSymbolTable);
Ethan Nicholas8da1e652019-05-24 11:01:59 -04001620 break;
Ethan Nicholas746035a2019-04-23 13:31:09 -04001621 case Program::kGeneric_Kind:
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001622 this->loadInterpreterIntrinsics();
John Stiles810c8cf2020-08-26 19:46:27 -04001623 fIRGenerator->fIntrinsics = fInterpreterIntrinsics.get();
Brian Osmane3dcb132020-10-08 10:27:29 -04001624 fIRGenerator->start(&settings, fInterpreterSymbolTable);
Ethan Nicholas0d997662019-04-08 09:46:01 -04001625 break;
ethannicholasb3058bd2016-07-01 08:22:01 -07001626 }
Brian Osman32d53552020-09-23 13:55:20 -04001627 if (externalValues) {
1628 // Add any external values to the symbol table. IRGenerator::start() has pushed a table, so
1629 // we're only making these visible to the current Program.
1630 for (const auto& ev : *externalValues) {
John Stilesb8cc6652020-10-08 09:12:07 -04001631 fIRGenerator->fSymbolTable->addWithoutOwnership(ev.get());
Brian Osman32d53552020-09-23 13:55:20 -04001632 }
1633 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001634 std::unique_ptr<String> textPtr(new String(std::move(text)));
1635 fSource = textPtr.get();
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001636 fIRGenerator->convertProgram(kind, textPtr->c_str(), textPtr->size(), &elements);
John Stilesfbd050b2020-08-03 13:21:46 -04001637 auto result = std::make_unique<Program>(kind,
1638 std::move(textPtr),
1639 settings,
1640 fContext,
John Stilesfbd050b2020-08-03 13:21:46 -04001641 std::move(elements),
Ethan Nicholas041fd0a2020-10-07 16:42:04 -04001642 fIRGenerator->releaseModifiers(),
John Stilesfbd050b2020-08-03 13:21:46 -04001643 fIRGenerator->fSymbolTable,
1644 fIRGenerator->fInputs);
Brian Osmane498b3c2020-09-23 14:42:11 -04001645 fIRGenerator->finish();
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001646 if (fErrorCount) {
1647 return nullptr;
1648 }
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001649 if (settings.fOptimize && !this->optimize(*result)) {
1650 return nullptr;
1651 }
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001652 return result;
1653}
1654
Ethan Nicholas00543112018-07-31 09:44:36 -04001655bool Compiler::optimize(Program& program) {
1656 SkASSERT(!fErrorCount);
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001657 fIRGenerator->fKind = program.fKind;
1658 fIRGenerator->fSettings = &program.fSettings;
John Stiles7954d6c2020-09-01 10:53:02 -04001659
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001660 while (fErrorCount == 0) {
1661 bool madeChanges = false;
John Stiles7954d6c2020-09-01 10:53:02 -04001662
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001663 // Scan and optimize based on the control-flow graph for each function.
1664 for (ProgramElement& element : program) {
1665 if (element.is<FunctionDefinition>()) {
1666 madeChanges |= this->scanCFG(element.as<FunctionDefinition>());
1667 }
1668 }
1669
1670 // Perform inline-candidate analysis and inline any functions deemed suitable.
John Stiles881a10c2020-09-19 10:13:24 -04001671 madeChanges |= fInliner.analyze(program);
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001672
1673 // Remove dead functions. We wait until after analysis so that we still report errors,
1674 // even in unused code.
1675 if (program.fSettings.fRemoveDeadFunctions) {
1676 program.fElements.erase(
1677 std::remove_if(program.fElements.begin(),
1678 program.fElements.end(),
1679 [&](const std::unique_ptr<ProgramElement>& element) {
1680 if (!element->is<FunctionDefinition>()) {
1681 return false;
1682 }
1683 const auto& fn = element->as<FunctionDefinition>();
Ethan Nicholased84b732020-10-08 11:45:44 -04001684 bool dead = fn.fDeclaration.callCount() == 0 &&
Ethan Nicholase2c49992020-10-05 11:49:11 -04001685 fn.fDeclaration.name() != "main";
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001686 madeChanges |= dead;
1687 return dead;
1688 }),
1689 program.fElements.end());
1690 }
1691
1692 if (program.fKind != Program::kFragmentProcessor_Kind) {
Brian Osmanc0213602020-10-06 14:43:32 -04001693 // Remove declarations of dead global variables
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001694 program.fElements.erase(
1695 std::remove_if(program.fElements.begin(), program.fElements.end(),
1696 [&](const std::unique_ptr<ProgramElement>& element) {
Brian Osmanc0213602020-10-06 14:43:32 -04001697 if (!element->is<GlobalVarDeclaration>()) {
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001698 return false;
1699 }
Brian Osmanc0213602020-10-06 14:43:32 -04001700 const auto& varDecl = element->as<GlobalVarDeclaration>();
1701 bool dead = varDecl.fDecl->fVar->dead();
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001702 madeChanges |= dead;
1703 return dead;
1704 }),
1705 program.fElements.end());
1706 }
John Stiles73a6bff2020-09-09 13:40:37 -04001707
Ethan Nicholas34b19c52020-09-14 11:33:47 -04001708 if (!madeChanges) {
1709 break;
Ethan Nicholas0dc80872019-02-08 15:46:24 -05001710 }
Ethan Nicholas00543112018-07-31 09:44:36 -04001711 }
Ethan Nicholas041fd0a2020-10-07 16:42:04 -04001712 program.finish();
Ethan Nicholas00543112018-07-31 09:44:36 -04001713 return fErrorCount == 0;
1714}
1715
Brian Osmanfb32ddf2019-06-18 10:14:20 -04001716#if defined(SKSL_STANDALONE) || SK_SUPPORT_GPU
1717
Ethan Nicholas00543112018-07-31 09:44:36 -04001718bool Compiler::toSPIRV(Program& program, OutputStream& out) {
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001719#ifdef SK_ENABLE_SPIRV_VALIDATION
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001720 StringStream buffer;
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001721 fSource = program.fSource.get();
Ethan Nicholas041fd0a2020-10-07 16:42:04 -04001722 SPIRVCodeGenerator cg(&fIRGenerator->fContext, fIRGenerator->fModifiers.get(), &program, this,
1723 &buffer);
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001724 bool result = cg.generateCode();
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001725 fSource = nullptr;
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001726 if (result) {
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001727 spvtools::SpirvTools tools(SPV_ENV_VULKAN_1_0);
Ethan Nicholas762466e2017-06-29 10:03:38 -04001728 const String& data = buffer.str();
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001729 SkASSERT(0 == data.size() % 4);
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001730 auto dumpmsg = [](spv_message_level_t, const char*, const spv_position_t&, const char* m) {
1731 SkDebugf("SPIR-V validation error: %s\n", m);
1732 };
1733 tools.SetMessageConsumer(dumpmsg);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001734 // Verify that the SPIR-V we produced is valid. If this SkASSERT fails, check the logs prior
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001735 // to the failure to see the validation errors.
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001736 SkAssertResult(tools.Validate((const uint32_t*) data.c_str(), data.size() / 4));
Ethan Nicholas762466e2017-06-29 10:03:38 -04001737 out.write(data.c_str(), data.size());
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001738 }
1739#else
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001740 fSource = program.fSource.get();
Ethan Nicholas041fd0a2020-10-07 16:42:04 -04001741 SPIRVCodeGenerator cg(&fIRGenerator->fContext, fIRGenerator->fModifiers.get(), &program, this,
1742 &out);
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001743 bool result = cg.generateCode();
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001744 fSource = nullptr;
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -04001745#endif
Ethan Nicholasce33f102016-12-09 17:22:59 -05001746 return result;
1747}
1748
Ethan Nicholas00543112018-07-31 09:44:36 -04001749bool Compiler::toSPIRV(Program& program, String* out) {
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001750 StringStream buffer;
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001751 bool result = this->toSPIRV(program, buffer);
1752 if (result) {
Ethan Nicholas762466e2017-06-29 10:03:38 -04001753 *out = buffer.str();
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001754 }
1755 return result;
1756}
1757
Ethan Nicholas00543112018-07-31 09:44:36 -04001758bool Compiler::toGLSL(Program& program, OutputStream& out) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001759 fSource = program.fSource.get();
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001760 GLSLCodeGenerator cg(fContext.get(), &program, this, &out);
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001761 bool result = cg.generateCode();
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001762 fSource = nullptr;
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001763 return result;
1764}
1765
Ethan Nicholas00543112018-07-31 09:44:36 -04001766bool Compiler::toGLSL(Program& program, String* out) {
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001767 StringStream buffer;
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001768 bool result = this->toGLSL(program, buffer);
1769 if (result) {
Ethan Nicholas762466e2017-06-29 10:03:38 -04001770 *out = buffer.str();
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001771 }
1772 return result;
1773}
1774
Brian Osmanc0243912020-02-19 15:35:26 -05001775bool Compiler::toHLSL(Program& program, String* out) {
1776 String spirv;
1777 if (!this->toSPIRV(program, &spirv)) {
1778 return false;
1779 }
1780
1781 return SPIRVtoHLSL(spirv, out);
1782}
1783
Ethan Nicholas00543112018-07-31 09:44:36 -04001784bool Compiler::toMetal(Program& program, OutputStream& out) {
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001785 MetalCodeGenerator cg(fContext.get(), &program, this, &out);
Ethan Nicholascc305772017-10-13 16:17:45 -04001786 bool result = cg.generateCode();
Ethan Nicholascc305772017-10-13 16:17:45 -04001787 return result;
1788}
1789
Ethan Nicholas00543112018-07-31 09:44:36 -04001790bool Compiler::toMetal(Program& program, String* out) {
Timothy Liangb8eeb802018-07-23 16:46:16 -04001791 StringStream buffer;
1792 bool result = this->toMetal(program, buffer);
1793 if (result) {
1794 *out = buffer.str();
1795 }
1796 return result;
1797}
1798
Greg Daniela28ea672020-09-25 11:12:56 -04001799#if defined(SKSL_STANDALONE) || GR_TEST_UTILS
Ethan Nicholas00543112018-07-31 09:44:36 -04001800bool Compiler::toCPP(Program& program, String name, OutputStream& out) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001801 fSource = program.fSource.get();
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001802 CPPCodeGenerator cg(fContext.get(), &program, this, name, &out);
Ethan Nicholas762466e2017-06-29 10:03:38 -04001803 bool result = cg.generateCode();
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001804 fSource = nullptr;
Ethan Nicholas762466e2017-06-29 10:03:38 -04001805 return result;
1806}
1807
Ethan Nicholas00543112018-07-31 09:44:36 -04001808bool Compiler::toH(Program& program, String name, OutputStream& out) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001809 fSource = program.fSource.get();
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001810 HCodeGenerator cg(fContext.get(), &program, this, name, &out);
Ethan Nicholas762466e2017-06-29 10:03:38 -04001811 bool result = cg.generateCode();
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001812 fSource = nullptr;
Ethan Nicholas00543112018-07-31 09:44:36 -04001813 return result;
1814}
Greg Daniela28ea672020-09-25 11:12:56 -04001815#endif // defined(SKSL_STANDALONE) || GR_TEST_UTILS
Ethan Nicholas00543112018-07-31 09:44:36 -04001816
Ethan Nicholas2a479a52020-08-18 16:29:45 -04001817#endif // defined(SKSL_STANDALONE) || SK_SUPPORT_GPU
Brian Osman2e29ab52019-09-20 12:19:11 -04001818
1819#if !defined(SKSL_STANDALONE) && SK_SUPPORT_GPU
Brian Osmana4b91692020-08-10 14:26:16 -04001820bool Compiler::toPipelineStage(Program& program, PipelineStageArgs* outArgs) {
Ethan Nicholas00543112018-07-31 09:44:36 -04001821 fSource = program.fSource.get();
1822 StringStream buffer;
Brian Osman300fe1d2020-01-23 15:42:43 -05001823 PipelineStageCodeGenerator cg(fContext.get(), &program, this, &buffer, outArgs);
Ethan Nicholas00543112018-07-31 09:44:36 -04001824 bool result = cg.generateCode();
1825 fSource = nullptr;
1826 if (result) {
Brian Osman107c6662019-12-30 15:02:30 -05001827 outArgs->fCode = buffer.str();
Ethan Nicholas00543112018-07-31 09:44:36 -04001828 }
Ethan Nicholas762466e2017-06-29 10:03:38 -04001829 return result;
1830}
Brian Osmanfb32ddf2019-06-18 10:14:20 -04001831#endif
1832
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001833std::unique_ptr<ByteCode> Compiler::toByteCode(Program& program) {
Mike Klein853d4ed2020-08-20 00:41:00 +00001834#if defined(SK_ENABLE_SKSL_INTERPRETER)
Brian Osman808f0212020-01-21 15:36:47 -05001835 fSource = program.fSource.get();
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001836 std::unique_ptr<ByteCode> result(new ByteCode());
Brian Osmanb08cc022020-04-02 11:38:40 -04001837 ByteCodeGenerator cg(fContext.get(), &program, this, result.get());
1838 bool success = cg.generateCode();
1839 fSource = nullptr;
1840 if (success) {
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001841 return result;
1842 }
Mike Klein853d4ed2020-08-20 00:41:00 +00001843#else
1844 ABORT("ByteCode interpreter not enabled");
1845#endif
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001846 return nullptr;
1847}
1848
Brian Osman401a0092020-09-10 14:47:24 -04001849const char* Compiler::OperatorName(Token::Kind op) {
1850 switch (op) {
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001851 case Token::Kind::TK_PLUS: return "+";
1852 case Token::Kind::TK_MINUS: return "-";
1853 case Token::Kind::TK_STAR: return "*";
1854 case Token::Kind::TK_SLASH: return "/";
1855 case Token::Kind::TK_PERCENT: return "%";
1856 case Token::Kind::TK_SHL: return "<<";
1857 case Token::Kind::TK_SHR: return ">>";
1858 case Token::Kind::TK_LOGICALNOT: return "!";
1859 case Token::Kind::TK_LOGICALAND: return "&&";
1860 case Token::Kind::TK_LOGICALOR: return "||";
1861 case Token::Kind::TK_LOGICALXOR: return "^^";
1862 case Token::Kind::TK_BITWISENOT: return "~";
1863 case Token::Kind::TK_BITWISEAND: return "&";
1864 case Token::Kind::TK_BITWISEOR: return "|";
1865 case Token::Kind::TK_BITWISEXOR: return "^";
1866 case Token::Kind::TK_EQ: return "=";
1867 case Token::Kind::TK_EQEQ: return "==";
1868 case Token::Kind::TK_NEQ: return "!=";
1869 case Token::Kind::TK_LT: return "<";
1870 case Token::Kind::TK_GT: return ">";
1871 case Token::Kind::TK_LTEQ: return "<=";
1872 case Token::Kind::TK_GTEQ: return ">=";
1873 case Token::Kind::TK_PLUSEQ: return "+=";
1874 case Token::Kind::TK_MINUSEQ: return "-=";
1875 case Token::Kind::TK_STAREQ: return "*=";
1876 case Token::Kind::TK_SLASHEQ: return "/=";
1877 case Token::Kind::TK_PERCENTEQ: return "%=";
1878 case Token::Kind::TK_SHLEQ: return "<<=";
1879 case Token::Kind::TK_SHREQ: return ">>=";
1880 case Token::Kind::TK_LOGICALANDEQ: return "&&=";
1881 case Token::Kind::TK_LOGICALOREQ: return "||=";
1882 case Token::Kind::TK_LOGICALXOREQ: return "^^=";
1883 case Token::Kind::TK_BITWISEANDEQ: return "&=";
1884 case Token::Kind::TK_BITWISEOREQ: return "|=";
1885 case Token::Kind::TK_BITWISEXOREQ: return "^=";
1886 case Token::Kind::TK_PLUSPLUS: return "++";
1887 case Token::Kind::TK_MINUSMINUS: return "--";
1888 case Token::Kind::TK_COMMA: return ",";
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001889 default:
Brian Osman401a0092020-09-10 14:47:24 -04001890 ABORT("unsupported operator: %d\n", (int) op);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001891 }
1892}
1893
1894
1895bool Compiler::IsAssignment(Token::Kind op) {
1896 switch (op) {
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04001897 case Token::Kind::TK_EQ: // fall through
1898 case Token::Kind::TK_PLUSEQ: // fall through
1899 case Token::Kind::TK_MINUSEQ: // fall through
1900 case Token::Kind::TK_STAREQ: // fall through
1901 case Token::Kind::TK_SLASHEQ: // fall through
1902 case Token::Kind::TK_PERCENTEQ: // fall through
1903 case Token::Kind::TK_SHLEQ: // fall through
1904 case Token::Kind::TK_SHREQ: // fall through
1905 case Token::Kind::TK_BITWISEOREQ: // fall through
1906 case Token::Kind::TK_BITWISEXOREQ: // fall through
1907 case Token::Kind::TK_BITWISEANDEQ: // fall through
1908 case Token::Kind::TK_LOGICALOREQ: // fall through
1909 case Token::Kind::TK_LOGICALXOREQ: // fall through
1910 case Token::Kind::TK_LOGICALANDEQ:
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001911 return true;
1912 default:
1913 return false;
1914 }
1915}
1916
Brian Osman401a0092020-09-10 14:47:24 -04001917Token::Kind Compiler::RemoveAssignment(Token::Kind op) {
1918 switch (op) {
1919 case Token::Kind::TK_PLUSEQ: return Token::Kind::TK_PLUS;
1920 case Token::Kind::TK_MINUSEQ: return Token::Kind::TK_MINUS;
1921 case Token::Kind::TK_STAREQ: return Token::Kind::TK_STAR;
1922 case Token::Kind::TK_SLASHEQ: return Token::Kind::TK_SLASH;
1923 case Token::Kind::TK_PERCENTEQ: return Token::Kind::TK_PERCENT;
1924 case Token::Kind::TK_SHLEQ: return Token::Kind::TK_SHL;
1925 case Token::Kind::TK_SHREQ: return Token::Kind::TK_SHR;
1926 case Token::Kind::TK_BITWISEOREQ: return Token::Kind::TK_BITWISEOR;
1927 case Token::Kind::TK_BITWISEXOREQ: return Token::Kind::TK_BITWISEXOR;
1928 case Token::Kind::TK_BITWISEANDEQ: return Token::Kind::TK_BITWISEAND;
1929 case Token::Kind::TK_LOGICALOREQ: return Token::Kind::TK_LOGICALOR;
1930 case Token::Kind::TK_LOGICALXOREQ: return Token::Kind::TK_LOGICALXOR;
1931 case Token::Kind::TK_LOGICALANDEQ: return Token::Kind::TK_LOGICALAND;
1932 default: return op;
1933 }
1934}
1935
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001936Position Compiler::position(int offset) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001937 SkASSERT(fSource);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001938 int line = 1;
1939 int column = 1;
1940 for (int i = 0; i < offset; i++) {
1941 if ((*fSource)[i] == '\n') {
1942 ++line;
1943 column = 1;
1944 }
1945 else {
1946 ++column;
1947 }
1948 }
1949 return Position(line, column);
1950}
1951
1952void Compiler::error(int offset, String msg) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001953 fErrorCount++;
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001954 Position pos = this->position(offset);
1955 fErrorText += "error: " + to_string(pos.fLine) + ": " + msg.c_str() + "\n";
ethannicholasb3058bd2016-07-01 08:22:01 -07001956}
1957
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001958String Compiler::errorText() {
Ethan Nicholas00543112018-07-31 09:44:36 -04001959 this->writeErrorCount();
1960 fErrorCount = 0;
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001961 String result = fErrorText;
ethannicholasb3058bd2016-07-01 08:22:01 -07001962 return result;
1963}
1964
1965void Compiler::writeErrorCount() {
1966 if (fErrorCount) {
1967 fErrorText += to_string(fErrorCount) + " error";
1968 if (fErrorCount > 1) {
1969 fErrorText += "s";
1970 }
1971 fErrorText += "\n";
1972 }
1973}
1974
John Stilesa6841be2020-08-06 14:11:56 -04001975} // namespace SkSL