blob: ecdcd10f6ed2626c308b7ae95003ca442a79b659 [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
Ethan Nicholas55a63af2021-05-18 10:12:58 -040013#include "include/sksl/DSLCore.h"
John Stiles270cec22021-02-17 12:59:36 -050014#include "src/core/SkScopeExit.h"
Leon Scrogginsb66214e2021-02-11 17:14:18 -050015#include "src/core/SkTraceEvent.h"
John Stilesb92641c2020-08-31 18:09:01 -040016#include "src/sksl/SkSLAnalysis.h"
John Stilesf3a28db2021-03-10 23:00:47 -050017#include "src/sksl/SkSLConstantFolder.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "src/sksl/SkSLIRGenerator.h"
Brian Osman00185012021-02-04 16:07:11 -050019#include "src/sksl/SkSLOperators.h"
John Stiles270cec22021-02-17 12:59:36 -050020#include "src/sksl/SkSLProgramSettings.h"
Ethan Nicholasc18bb512020-07-28 14:46:53 -040021#include "src/sksl/SkSLRehydrator.h"
John Stiles3738ef52021-04-13 10:41:57 -040022#include "src/sksl/codegen/SkSLCPPCodeGenerator.h"
John Stiles82ab3402021-04-13 17:13:03 -040023#include "src/sksl/codegen/SkSLDSLCPPCodeGenerator.h"
John Stiles3738ef52021-04-13 10:41:57 -040024#include "src/sksl/codegen/SkSLGLSLCodeGenerator.h"
25#include "src/sksl/codegen/SkSLHCodeGenerator.h"
26#include "src/sksl/codegen/SkSLMetalCodeGenerator.h"
27#include "src/sksl/codegen/SkSLSPIRVCodeGenerator.h"
28#include "src/sksl/codegen/SkSLSPIRVtoHLSL.h"
Ethan Nicholas55a63af2021-05-18 10:12:58 -040029#include "src/sksl/dsl/priv/DSLWriter.h"
30#include "src/sksl/dsl/priv/DSL_priv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050031#include "src/sksl/ir/SkSLEnum.h"
32#include "src/sksl/ir/SkSLExpression.h"
33#include "src/sksl/ir/SkSLExpressionStatement.h"
34#include "src/sksl/ir/SkSLFunctionCall.h"
35#include "src/sksl/ir/SkSLIntLiteral.h"
36#include "src/sksl/ir/SkSLModifiersDeclaration.h"
37#include "src/sksl/ir/SkSLNop.h"
38#include "src/sksl/ir/SkSLSymbolTable.h"
39#include "src/sksl/ir/SkSLTernaryExpression.h"
40#include "src/sksl/ir/SkSLUnresolvedFunction.h"
41#include "src/sksl/ir/SkSLVarDeclarations.h"
John Stilese6150002020-10-05 12:03:53 -040042#include "src/utils/SkBitSet.h"
ethannicholasb3058bd2016-07-01 08:22:01 -070043
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -040044#include <fstream>
45
Ethan Nicholasa11035b2019-11-26 16:27:47 -050046#if !defined(SKSL_STANDALONE) & SK_SUPPORT_GPU
47#include "include/gpu/GrContextOptions.h"
48#include "src/gpu/GrShaderCaps.h"
49#endif
50
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -040051#ifdef SK_ENABLE_SPIRV_VALIDATION
52#include "spirv-tools/libspirv.hpp"
53#endif
54
Brian Osman3d87e9f2020-10-08 11:50:22 -040055#if defined(SKSL_STANDALONE)
Ethan Nicholasc18bb512020-07-28 14:46:53 -040056
Brian Osman3d87e9f2020-10-08 11:50:22 -040057// In standalone mode, we load the textual sksl source files. GN generates or copies these files
58// to the skslc executable directory. The "data" in this mode is just the filename.
59#define MODULE_DATA(name) MakeModulePath("sksl_" #name ".sksl")
60
61#else
62
63// At runtime, we load the dehydrated sksl data files. The data is a (pointer, size) pair.
Ethan Nicholasc18bb512020-07-28 14:46:53 -040064#include "src/sksl/generated/sksl_fp.dehydrated.sksl"
65#include "src/sksl/generated/sksl_frag.dehydrated.sksl"
66#include "src/sksl/generated/sksl_geom.dehydrated.sksl"
67#include "src/sksl/generated/sksl_gpu.dehydrated.sksl"
Brian Osmanb06301e2020-11-06 11:45:36 -050068#include "src/sksl/generated/sksl_public.dehydrated.sksl"
John Stilesf7f36ae2021-06-08 14:06:22 -040069#include "src/sksl/generated/sksl_rt_blend.dehydrated.sksl"
Brian Osmancbb60bd2021-04-12 09:49:20 -040070#include "src/sksl/generated/sksl_rt_colorfilter.dehydrated.sksl"
71#include "src/sksl/generated/sksl_rt_shader.dehydrated.sksl"
Ethan Nicholasc18bb512020-07-28 14:46:53 -040072#include "src/sksl/generated/sksl_vert.dehydrated.sksl"
73
Brian Osman3d87e9f2020-10-08 11:50:22 -040074#define MODULE_DATA(name) MakeModuleData(SKSL_INCLUDE_sksl_##name,\
75 SKSL_INCLUDE_sksl_##name##_LENGTH)
Ethan Nicholasc18bb512020-07-28 14:46:53 -040076
77#endif
Ethan Nicholas0d997662019-04-08 09:46:01 -040078
ethannicholasb3058bd2016-07-01 08:22:01 -070079namespace SkSL {
80
John Stiles7247b482021-03-08 10:40:35 -050081// These flags allow tools like Viewer or Nanobench to override the compiler's ProgramSettings.
John Stiles2ee4d7a2021-03-30 10:30:47 -040082Compiler::OverrideFlag Compiler::sOptimizer = OverrideFlag::kDefault;
83Compiler::OverrideFlag Compiler::sInliner = OverrideFlag::kDefault;
John Stiles8ef4d6c2021-03-05 16:01:45 -050084
John Stiles47c0a742021-02-09 09:30:35 -050085using RefKind = VariableReference::RefKind;
86
Brian Osman88cda172020-10-09 12:05:16 -040087class AutoSource {
88public:
89 AutoSource(Compiler* compiler, const String* source)
John Stilesa289ac22021-05-06 07:35:35 -040090 : fCompiler(compiler) {
91 SkASSERT(!fCompiler->fSource);
Brian Osman88cda172020-10-09 12:05:16 -040092 fCompiler->fSource = source;
93 }
94
John Stilesa289ac22021-05-06 07:35:35 -040095 ~AutoSource() {
96 fCompiler->fSource = nullptr;
97 }
Brian Osman88cda172020-10-09 12:05:16 -040098
99 Compiler* fCompiler;
Brian Osman88cda172020-10-09 12:05:16 -0400100};
101
John Stilesa935c3f2021-02-25 10:35:49 -0500102class AutoProgramConfig {
103public:
104 AutoProgramConfig(std::shared_ptr<Context>& context, ProgramConfig* config)
105 : fContext(context.get()) {
106 SkASSERT(!fContext->fConfig);
107 fContext->fConfig = config;
108 }
109
110 ~AutoProgramConfig() {
111 fContext->fConfig = nullptr;
112 }
113
114 Context* fContext;
115};
116
John Stiles10d39d92021-05-04 16:13:14 -0400117class AutoModifiersPool {
118public:
119 AutoModifiersPool(std::shared_ptr<Context>& context, ModifiersPool* modifiersPool)
120 : fContext(context.get()) {
121 SkASSERT(!fContext->fModifiersPool);
122 fContext->fModifiersPool = modifiersPool;
123 }
124
125 ~AutoModifiersPool() {
126 fContext->fModifiersPool = nullptr;
127 }
128
129 Context* fContext;
130};
131
John Stilesd6a5f4492021-02-11 15:46:11 -0500132Compiler::Compiler(const ShaderCapsClass* caps)
John Stilesc1a98b82021-02-24 13:35:02 -0500133 : fContext(std::make_shared<Context>(/*errors=*/*this, *caps))
John Stilesa47b3512021-05-04 16:15:00 -0400134 , fInliner(fContext.get()) {
John Stilesc1a98b82021-02-24 13:35:02 -0500135 SkASSERT(caps);
John Stiles7c3515b2020-10-16 18:38:39 -0400136 fRootSymbolTable = std::make_shared<SymbolTable>(this, /*builtin=*/true);
Brian Osmanb06301e2020-11-06 11:45:36 -0500137 fPrivateSymbolTable = std::make_shared<SymbolTable>(fRootSymbolTable, /*builtin=*/true);
John Stilesc1a98b82021-02-24 13:35:02 -0500138 fIRGenerator = std::make_unique<IRGenerator>(fContext.get());
ethannicholasb3058bd2016-07-01 08:22:01 -0700139
John Stiles54e7c052021-01-11 14:22:36 -0500140#define TYPE(t) fContext->fTypes.f ## t .get()
ethannicholasb3058bd2016-07-01 08:22:01 -0700141
Brian Osmanb06301e2020-11-06 11:45:36 -0500142 const SkSL::Symbol* rootTypes[] = {
143 TYPE(Void),
Brian Salomonbf7b6202016-11-11 16:08:03 -0500144
Brian Osmanb06301e2020-11-06 11:45:36 -0500145 TYPE( Float), TYPE( Float2), TYPE( Float3), TYPE( Float4),
146 TYPE( Half), TYPE( Half2), TYPE( Half3), TYPE( Half4),
147 TYPE( Int), TYPE( Int2), TYPE( Int3), TYPE( Int4),
Brian Osmanb06301e2020-11-06 11:45:36 -0500148 TYPE( Bool), TYPE( Bool2), TYPE( Bool3), TYPE( Bool4),
Brian Salomon2a51de82016-11-16 12:06:01 -0500149
Brian Osmanc0f2b642020-12-22 13:35:55 -0500150 TYPE(Float2x2), TYPE(Float3x3), TYPE(Float4x4),
Brian Osmanc63f4312020-12-23 11:44:14 -0500151 TYPE( Half2x2), TYPE( Half3x3), TYPE(Half4x4),
Greg Daniel64773e62016-11-22 09:44:03 -0500152
Brian Osmanc63f4312020-12-23 11:44:14 -0500153 TYPE(SquareMat), TYPE(SquareHMat),
ethannicholasb3058bd2016-07-01 08:22:01 -0700154
Brian Osman20fad322020-12-23 12:42:33 -0500155 TYPE(GenType), TYPE(GenHType), TYPE(GenIType), TYPE(GenBType),
156 TYPE(Vec), TYPE(HVec), TYPE(IVec), TYPE(BVec),
Brian Osmanb06301e2020-11-06 11:45:36 -0500157
Brian Osman14d00962021-04-02 17:04:35 -0400158 TYPE(ColorFilter),
159 TYPE(Shader),
Brian Osmanb06301e2020-11-06 11:45:36 -0500160 };
161
162 const SkSL::Symbol* privateTypes[] = {
Brian Osman20fad322020-12-23 12:42:33 -0500163 TYPE( UInt), TYPE( UInt2), TYPE( UInt3), TYPE( UInt4),
164 TYPE( Short), TYPE( Short2), TYPE( Short3), TYPE( Short4),
165 TYPE(UShort), TYPE(UShort2), TYPE(UShort3), TYPE(UShort4),
Brian Osman20fad322020-12-23 12:42:33 -0500166
167 TYPE(GenUType), TYPE(UVec),
Ethan Nicholas722c83e2021-05-04 11:39:30 -0400168 TYPE(SVec), TYPE(USVec),
Brian Osman20fad322020-12-23 12:42:33 -0500169
Brian Osmanc0f2b642020-12-22 13:35:55 -0500170 TYPE(Float2x3), TYPE(Float2x4),
171 TYPE(Float3x2), TYPE(Float3x4),
172 TYPE(Float4x2), TYPE(Float4x3),
173
Brian Osmanc63f4312020-12-23 11:44:14 -0500174 TYPE(Half2x3), TYPE(Half2x4),
175 TYPE(Half3x2), TYPE(Half3x4),
176 TYPE(Half4x2), TYPE(Half4x3),
177
Brian Osmanc0f2b642020-12-22 13:35:55 -0500178 TYPE(Mat), TYPE(HMat),
179
Brian Osmanb06301e2020-11-06 11:45:36 -0500180 TYPE(Sampler1D), TYPE(Sampler2D), TYPE(Sampler3D),
181 TYPE(SamplerExternalOES),
Brian Osmanb06301e2020-11-06 11:45:36 -0500182 TYPE(Sampler2DRect),
Brian Osmanb06301e2020-11-06 11:45:36 -0500183
184 TYPE(ISampler2D),
Brian Osmanb06301e2020-11-06 11:45:36 -0500185 TYPE(SubpassInput), TYPE(SubpassInputMS),
186
Brian Osmanb06301e2020-11-06 11:45:36 -0500187 TYPE(Sampler),
188 TYPE(Texture2D),
Brian Osman14d00962021-04-02 17:04:35 -0400189
190 TYPE(FragmentProcessor),
Brian Osmanb06301e2020-11-06 11:45:36 -0500191 };
192
193 for (const SkSL::Symbol* type : rootTypes) {
194 fRootSymbolTable->addWithoutOwnership(type);
195 }
196 for (const SkSL::Symbol* type : privateTypes) {
197 fPrivateSymbolTable->addWithoutOwnership(type);
198 }
199
200#undef TYPE
ethannicholasb3058bd2016-07-01 08:22:01 -0700201
Brian Osman3887a012020-09-30 13:22:27 -0400202 // sk_Caps is "builtin", but all references to it are resolved to Settings, so we don't need to
203 // treat it as builtin (ie, no need to clone it into the Program).
John Stiles10d39d92021-05-04 16:13:14 -0400204 fPrivateSymbolTable->add(std::make_unique<Variable>(/*offset=*/-1,
John Stilesa47b3512021-05-04 16:15:00 -0400205 fCoreModifiers.add(Modifiers{}),
John Stiles10d39d92021-05-04 16:13:14 -0400206 "sk_Caps",
207 fContext->fTypes.fSkCaps.get(),
208 /*builtin=*/false,
209 Variable::Storage::kGlobal));
Ethan Nicholas3605ace2016-11-21 15:59:48 -0500210
Brian Osman3d87e9f2020-10-08 11:50:22 -0400211 fRootModule = {fRootSymbolTable, /*fIntrinsics=*/nullptr};
Brian Osmanb06301e2020-11-06 11:45:36 -0500212 fPrivateModule = {fPrivateSymbolTable, /*fIntrinsics=*/nullptr};
ethannicholasb3058bd2016-07-01 08:22:01 -0700213}
214
John Stilesdd13dba2020-10-29 10:45:34 -0400215Compiler::~Compiler() {}
ethannicholasb3058bd2016-07-01 08:22:01 -0700216
Brian Osman56269982020-11-20 12:38:07 -0500217const ParsedModule& Compiler::loadGPUModule() {
218 if (!fGPUModule.fSymbols) {
John Stilesdbd4e6f2021-02-16 13:29:15 -0500219 fGPUModule = this->parseModule(ProgramKind::kFragment, MODULE_DATA(gpu), fPrivateModule);
Brian Osman56269982020-11-20 12:38:07 -0500220 }
221 return fGPUModule;
222}
223
224const ParsedModule& Compiler::loadFragmentModule() {
225 if (!fFragmentModule.fSymbols) {
John Stilesdbd4e6f2021-02-16 13:29:15 -0500226 fFragmentModule = this->parseModule(ProgramKind::kFragment, MODULE_DATA(frag),
Brian Osman56269982020-11-20 12:38:07 -0500227 this->loadGPUModule());
228 }
229 return fFragmentModule;
230}
231
232const ParsedModule& Compiler::loadVertexModule() {
233 if (!fVertexModule.fSymbols) {
John Stilesdbd4e6f2021-02-16 13:29:15 -0500234 fVertexModule = this->parseModule(ProgramKind::kVertex, MODULE_DATA(vert),
Brian Osman56269982020-11-20 12:38:07 -0500235 this->loadGPUModule());
236 }
237 return fVertexModule;
238}
239
Brian Osman88cda172020-10-09 12:05:16 -0400240const ParsedModule& Compiler::loadGeometryModule() {
Brian Osman3d87e9f2020-10-08 11:50:22 -0400241 if (!fGeometryModule.fSymbols) {
John Stilesdbd4e6f2021-02-16 13:29:15 -0500242 fGeometryModule = this->parseModule(ProgramKind::kGeometry, MODULE_DATA(geom),
Brian Osman56269982020-11-20 12:38:07 -0500243 this->loadGPUModule());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400244 }
Brian Osman88cda172020-10-09 12:05:16 -0400245 return fGeometryModule;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400246}
247
Brian Osman88cda172020-10-09 12:05:16 -0400248const ParsedModule& Compiler::loadFPModule() {
Brian Osman3d87e9f2020-10-08 11:50:22 -0400249 if (!fFPModule.fSymbols) {
John Stilesdbd4e6f2021-02-16 13:29:15 -0500250 fFPModule = this->parseModule(ProgramKind::kFragmentProcessor, MODULE_DATA(fp),
Brian Osman56269982020-11-20 12:38:07 -0500251 this->loadGPUModule());
Brian Osman8e2ef022020-09-30 13:26:43 -0400252 }
Brian Osman88cda172020-10-09 12:05:16 -0400253 return fFPModule;
Brian Osman8e2ef022020-09-30 13:26:43 -0400254}
255
Brian Osmanb06301e2020-11-06 11:45:36 -0500256const ParsedModule& Compiler::loadPublicModule() {
257 if (!fPublicModule.fSymbols) {
John Stilesdbd4e6f2021-02-16 13:29:15 -0500258 fPublicModule = this->parseModule(ProgramKind::kGeneric, MODULE_DATA(public), fRootModule);
Brian Osmanb06301e2020-11-06 11:45:36 -0500259 }
260 return fPublicModule;
261}
262
Brian Osmancbb60bd2021-04-12 09:49:20 -0400263static void add_glsl_type_aliases(SkSL::SymbolTable* symbols, const SkSL::BuiltinTypes& types) {
264 // Add some aliases to the runtime effect modules so that it's friendlier, and more like GLSL
265 symbols->addAlias("vec2", types.fFloat2.get());
266 symbols->addAlias("vec3", types.fFloat3.get());
267 symbols->addAlias("vec4", types.fFloat4.get());
268
269 symbols->addAlias("ivec2", types.fInt2.get());
270 symbols->addAlias("ivec3", types.fInt3.get());
271 symbols->addAlias("ivec4", types.fInt4.get());
272
273 symbols->addAlias("bvec2", types.fBool2.get());
274 symbols->addAlias("bvec3", types.fBool3.get());
275 symbols->addAlias("bvec4", types.fBool4.get());
276
277 symbols->addAlias("mat2", types.fFloat2x2.get());
278 symbols->addAlias("mat3", types.fFloat3x3.get());
279 symbols->addAlias("mat4", types.fFloat4x4.get());
280}
281
Brian Osmancbb60bd2021-04-12 09:49:20 -0400282const ParsedModule& Compiler::loadRuntimeColorFilterModule() {
283 if (!fRuntimeColorFilterModule.fSymbols) {
284 fRuntimeColorFilterModule = this->parseModule(ProgramKind::kRuntimeColorFilter,
285 MODULE_DATA(rt_colorfilter),
286 this->loadPublicModule());
287 add_glsl_type_aliases(fRuntimeColorFilterModule.fSymbols.get(), fContext->fTypes);
288 }
289 return fRuntimeColorFilterModule;
290}
291
292const ParsedModule& Compiler::loadRuntimeShaderModule() {
293 if (!fRuntimeShaderModule.fSymbols) {
294 fRuntimeShaderModule = this->parseModule(
295 ProgramKind::kRuntimeShader, MODULE_DATA(rt_shader), this->loadPublicModule());
296 add_glsl_type_aliases(fRuntimeShaderModule.fSymbols.get(), fContext->fTypes);
297 }
298 return fRuntimeShaderModule;
299}
300
John Stilesf7f36ae2021-06-08 14:06:22 -0400301const ParsedModule& Compiler::loadRuntimeBlendModule() {
302 if (!fRuntimeBlendModule.fSymbols) {
303 fRuntimeBlendModule = this->parseModule(
304 ProgramKind::kRuntimeBlend, MODULE_DATA(rt_blend), this->loadPublicModule());
305 add_glsl_type_aliases(fRuntimeBlendModule.fSymbols.get(), fContext->fTypes);
306 }
307 return fRuntimeBlendModule;
308}
309
John Stilesdbd4e6f2021-02-16 13:29:15 -0500310const ParsedModule& Compiler::moduleForProgramKind(ProgramKind kind) {
Brian Osman88cda172020-10-09 12:05:16 -0400311 switch (kind) {
Brian Osmancbb60bd2021-04-12 09:49:20 -0400312 case ProgramKind::kVertex: return this->loadVertexModule(); break;
313 case ProgramKind::kFragment: return this->loadFragmentModule(); break;
314 case ProgramKind::kGeometry: return this->loadGeometryModule(); break;
315 case ProgramKind::kFragmentProcessor: return this->loadFPModule(); break;
Brian Osmancbb60bd2021-04-12 09:49:20 -0400316 case ProgramKind::kRuntimeColorFilter: return this->loadRuntimeColorFilterModule(); break;
317 case ProgramKind::kRuntimeShader: return this->loadRuntimeShaderModule(); break;
John Stilesf7f36ae2021-06-08 14:06:22 -0400318 case ProgramKind::kRuntimeBlend: return this->loadRuntimeBlendModule(); break;
Brian Osmancbb60bd2021-04-12 09:49:20 -0400319 case ProgramKind::kGeneric: return this->loadPublicModule(); break;
Brian Osman88cda172020-10-09 12:05:16 -0400320 }
321 SkUNREACHABLE;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400322}
323
John Stilesdbd4e6f2021-02-16 13:29:15 -0500324LoadedModule Compiler::loadModule(ProgramKind kind,
Brian Osman3d87e9f2020-10-08 11:50:22 -0400325 ModuleData data,
John Stilesa935c3f2021-02-25 10:35:49 -0500326 std::shared_ptr<SymbolTable> base,
327 bool dehydrate) {
328 if (dehydrate) {
329 // NOTE: This is a workaround. When dehydrating includes, skslc doesn't know which module
330 // it's preparing, nor what the correct base module is. We can't use 'Root', because many
331 // GPU intrinsics reference private types, like samplers or textures. Today, 'Private' does
332 // contain the union of all known types, so this is safe. If we ever have types that only
333 // exist in 'Public' (for example), this logic needs to be smarter (by choosing the correct
334 // base for the module we're compiling).
Brian Osmanb06301e2020-11-06 11:45:36 -0500335 base = fPrivateSymbolTable;
Brian Osman3d87e9f2020-10-08 11:50:22 -0400336 }
John Stilesa935c3f2021-02-25 10:35:49 -0500337 SkASSERT(base);
338
John Stilesa47b3512021-05-04 16:15:00 -0400339 // Put the core-module modifier pool into the context.
340 AutoModifiersPool autoPool(fContext, &fCoreModifiers);
John Stiles10d39d92021-05-04 16:13:14 -0400341
John Stilesa935c3f2021-02-25 10:35:49 -0500342 // Built-in modules always use default program settings.
Ethan Nicholas55a63af2021-05-18 10:12:58 -0400343 Program::Settings settings;
344 settings.fReplaceSettings = !dehydrate;
Brian Osman3d87e9f2020-10-08 11:50:22 -0400345
346#if defined(SKSL_STANDALONE)
347 SkASSERT(data.fPath);
348 std::ifstream in(data.fPath);
John Stilesd51c9792021-03-18 11:40:14 -0400349 String text{std::istreambuf_iterator<char>(in), std::istreambuf_iterator<char>()};
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -0400350 if (in.rdstate()) {
Brian Osman3d87e9f2020-10-08 11:50:22 -0400351 printf("error reading %s\n", data.fPath);
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -0400352 abort();
353 }
Brian Osmane498b3c2020-09-23 14:42:11 -0400354 const String* source = fRootSymbolTable->takeOwnershipOfString(std::move(text));
John Stilesd1204642021-02-17 16:30:02 -0500355
Brian Osman88cda172020-10-09 12:05:16 -0400356 ParsedModule baseModule = {base, /*fIntrinsics=*/nullptr};
Ethan Nicholas55a63af2021-05-18 10:12:58 -0400357 std::vector<std::unique_ptr<ProgramElement>> elements;
358 std::vector<const ProgramElement*> sharedElements;
359 dsl::StartModule(this, kind, settings, baseModule);
360 AutoSource as(this, source);
John Stilesd1204642021-02-17 16:30:02 -0500361 IRGenerator::IRBundle ir = fIRGenerator->convertProgram(baseModule, /*isBuiltinCode=*/true,
Ethan Nicholas55a63af2021-05-18 10:12:58 -0400362 source->c_str(), source->length());
Brian Osman133724c2020-10-28 14:14:39 -0400363 SkASSERT(ir.fSharedElements.empty());
Brian Osman0006ad02020-11-18 15:38:39 -0500364 LoadedModule module = { kind, std::move(ir.fSymbolTable), std::move(ir.fElements) };
Ethan Nicholas55a63af2021-05-18 10:12:58 -0400365 dsl::End();
Ethan Nicholas8da1e652019-05-24 11:01:59 -0400366 if (this->fErrorCount) {
367 printf("Unexpected errors: %s\n", this->fErrorText.c_str());
Brian Osman3d87e9f2020-10-08 11:50:22 -0400368 SkDEBUGFAILF("%s %s\n", data.fPath, this->fErrorText.c_str());
Ethan Nicholas8da1e652019-05-24 11:01:59 -0400369 }
Brian Osman3d87e9f2020-10-08 11:50:22 -0400370#else
Ethan Nicholas55a63af2021-05-18 10:12:58 -0400371 ProgramConfig config;
372 config.fKind = kind;
373 config.fSettings = settings;
374 AutoProgramConfig autoConfig(fContext, &config);
Brian Osman3d87e9f2020-10-08 11:50:22 -0400375 SkASSERT(data.fData && (data.fSize != 0));
John Stiles10d39d92021-05-04 16:13:14 -0400376 Rehydrator rehydrator(fContext.get(), base, data.fData, data.fSize);
Brian Osman0006ad02020-11-18 15:38:39 -0500377 LoadedModule module = { kind, rehydrator.symbolTable(), rehydrator.elements() };
Brian Osman3d87e9f2020-10-08 11:50:22 -0400378#endif
379
380 return module;
381}
382
John Stilesdbd4e6f2021-02-16 13:29:15 -0500383ParsedModule Compiler::parseModule(ProgramKind kind, ModuleData data, const ParsedModule& base) {
John Stilesa935c3f2021-02-25 10:35:49 -0500384 LoadedModule module = this->loadModule(kind, data, base.fSymbols, /*dehydrate=*/false);
Brian Osman0006ad02020-11-18 15:38:39 -0500385 this->optimize(module);
Brian Osman3d87e9f2020-10-08 11:50:22 -0400386
387 // For modules that just declare (but don't define) intrinsic functions, there will be no new
388 // program elements. In that case, we can share our parent's intrinsic map:
Brian Osman0006ad02020-11-18 15:38:39 -0500389 if (module.fElements.empty()) {
John Stiles10d39d92021-05-04 16:13:14 -0400390 return ParsedModule{module.fSymbols, base.fIntrinsics};
Brian Osman3d87e9f2020-10-08 11:50:22 -0400391 }
392
393 auto intrinsics = std::make_shared<IRIntrinsicMap>(base.fIntrinsics.get());
394
395 // Now, transfer all of the program elements to an intrinsic map. This maps certain types of
396 // global objects to the declaring ProgramElement.
Brian Osman0006ad02020-11-18 15:38:39 -0500397 for (std::unique_ptr<ProgramElement>& element : module.fElements) {
Brian Osman3d87e9f2020-10-08 11:50:22 -0400398 switch (element->kind()) {
399 case ProgramElement::Kind::kFunction: {
400 const FunctionDefinition& f = element->as<FunctionDefinition>();
Ethan Nicholas0a5d0962020-10-14 13:33:18 -0400401 SkASSERT(f.declaration().isBuiltin());
402 intrinsics->insertOrDie(f.declaration().description(), std::move(element));
Brian Osman3d87e9f2020-10-08 11:50:22 -0400403 break;
404 }
John Stiles569249b2020-11-03 12:18:22 -0500405 case ProgramElement::Kind::kFunctionPrototype: {
406 // These are already in the symbol table.
407 break;
408 }
Brian Osman3d87e9f2020-10-08 11:50:22 -0400409 case ProgramElement::Kind::kEnum: {
410 const Enum& e = element->as<Enum>();
411 SkASSERT(e.isBuiltin());
Ethan Nicholasd2e09602021-06-10 11:21:59 -0400412 intrinsics->insertOrDie(String(e.typeName()), std::move(element));
Brian Osman3d87e9f2020-10-08 11:50:22 -0400413 break;
414 }
415 case ProgramElement::Kind::kGlobalVar: {
Ethan Nicholasc51f33e2020-10-13 13:49:44 -0400416 const GlobalVarDeclaration& global = element->as<GlobalVarDeclaration>();
417 const Variable& var = global.declaration()->as<VarDeclaration>().var();
418 SkASSERT(var.isBuiltin());
Ethan Nicholasd2e09602021-06-10 11:21:59 -0400419 intrinsics->insertOrDie(String(var.name()), std::move(element));
Brian Osman3d87e9f2020-10-08 11:50:22 -0400420 break;
421 }
422 case ProgramElement::Kind::kInterfaceBlock: {
Ethan Nicholaseaf47882020-10-15 10:10:08 -0400423 const Variable& var = element->as<InterfaceBlock>().variable();
424 SkASSERT(var.isBuiltin());
Ethan Nicholasd2e09602021-06-10 11:21:59 -0400425 intrinsics->insertOrDie(String(var.name()), std::move(element));
Brian Osman3d87e9f2020-10-08 11:50:22 -0400426 break;
427 }
428 default:
429 printf("Unsupported element: %s\n", element->description().c_str());
430 SkASSERT(false);
431 break;
432 }
433 }
434
John Stiles10d39d92021-05-04 16:13:14 -0400435 return ParsedModule{module.fSymbols, std::move(intrinsics)};
Ethan Nicholas8da1e652019-05-24 11:01:59 -0400436}
437
Brian Osman32d53552020-09-23 13:55:20 -0400438std::unique_ptr<Program> Compiler::convertProgram(
John Stilesdbd4e6f2021-02-16 13:29:15 -0500439 ProgramKind kind,
Brian Osman32d53552020-09-23 13:55:20 -0400440 String text,
Ethan Nicholas55a63af2021-05-18 10:12:58 -0400441 Program::Settings settings) {
Brian Osman7a20b5c2021-03-15 16:23:33 -0400442 TRACE_EVENT0("skia.shaders", "SkSL::Compiler::convertProgram");
Leon Scrogginsb66214e2021-02-11 17:14:18 -0500443
Ethan Nicholas55a63af2021-05-18 10:12:58 -0400444 SkASSERT(!settings.fExternalFunctions || (kind == ProgramKind::kGeneric));
Ethan Nicholas91164d12019-05-15 15:29:54 -0400445
Brian Osman0006ad02020-11-18 15:38:39 -0500446 // Loading and optimizing our base module might reset the inliner, so do that first,
447 // *then* configure the inliner with the settings for this program.
448 const ParsedModule& baseModule = this->moduleForProgramKind(kind);
449
John Stiles2ee4d7a2021-03-30 10:30:47 -0400450 // Honor our optimization-override flags.
451 switch (sOptimizer) {
452 case OverrideFlag::kDefault:
453 break;
454 case OverrideFlag::kOff:
Ethan Nicholas55a63af2021-05-18 10:12:58 -0400455 settings.fOptimize = false;
John Stiles2ee4d7a2021-03-30 10:30:47 -0400456 break;
457 case OverrideFlag::kOn:
Ethan Nicholas55a63af2021-05-18 10:12:58 -0400458 settings.fOptimize = true;
John Stiles2ee4d7a2021-03-30 10:30:47 -0400459 break;
460 }
461
462 switch (sInliner) {
463 case OverrideFlag::kDefault:
464 break;
465 case OverrideFlag::kOff:
Ethan Nicholas55a63af2021-05-18 10:12:58 -0400466 settings.fInlineThreshold = 0;
John Stiles2ee4d7a2021-03-30 10:30:47 -0400467 break;
468 case OverrideFlag::kOn:
Ethan Nicholas55a63af2021-05-18 10:12:58 -0400469 if (settings.fInlineThreshold == 0) {
470 settings.fInlineThreshold = kDefaultInlineThreshold;
John Stiles2ee4d7a2021-03-30 10:30:47 -0400471 }
472 break;
473 }
John Stiles7247b482021-03-08 10:40:35 -0500474
475 // Disable optimization settings that depend on a parent setting which has been disabled.
Ethan Nicholas55a63af2021-05-18 10:12:58 -0400476 settings.fInlineThreshold *= (int)settings.fOptimize;
477 settings.fRemoveDeadFunctions &= settings.fOptimize;
478 settings.fRemoveDeadVariables &= settings.fOptimize;
John Stiles7247b482021-03-08 10:40:35 -0500479
ethannicholasb3058bd2016-07-01 08:22:01 -0700480 fErrorText = "";
481 fErrorCount = 0;
John Stiles10d39d92021-05-04 16:13:14 -0400482 fInliner.reset();
Brian Osman88cda172020-10-09 12:05:16 -0400483
John Stiles10d39d92021-05-04 16:13:14 -0400484 auto textPtr = std::make_unique<String>(std::move(text));
John Stilesa289ac22021-05-06 07:35:35 -0400485 AutoSource as(this, textPtr.get());
Brian Osman88cda172020-10-09 12:05:16 -0400486
Ethan Nicholas55a63af2021-05-18 10:12:58 -0400487 dsl::Start(this, kind, settings);
John Stilesd1204642021-02-17 16:30:02 -0500488 IRGenerator::IRBundle ir = fIRGenerator->convertProgram(baseModule, /*isBuiltinCode=*/false,
Ethan Nicholas55a63af2021-05-18 10:12:58 -0400489 textPtr->c_str(), textPtr->size());
490 // Ideally, we would just use DSLWriter::ReleaseProgram and not have to do any manual mucking
491 // about with the memory pool, but we've got some impedance mismatches to solve first
492 Pool* memoryPool = dsl::DSLWriter::MemoryPool().get();
John Stiles270cec22021-02-17 12:59:36 -0500493 auto program = std::make_unique<Program>(std::move(textPtr),
Ethan Nicholas55a63af2021-05-18 10:12:58 -0400494 std::move(dsl::DSLWriter::GetProgramConfig()),
John Stiles5c7bb322020-10-22 11:09:15 -0400495 fContext,
496 std::move(ir.fElements),
Brian Osman133724c2020-10-28 14:14:39 -0400497 std::move(ir.fSharedElements),
Ethan Nicholas55a63af2021-05-18 10:12:58 -0400498 std::move(dsl::DSLWriter::GetModifiersPool()),
John Stiles5c7bb322020-10-22 11:09:15 -0400499 std::move(ir.fSymbolTable),
Ethan Nicholas55a63af2021-05-18 10:12:58 -0400500 std::move(dsl::DSLWriter::MemoryPool()),
John Stiles5c7bb322020-10-22 11:09:15 -0400501 ir.fInputs);
502 bool success = false;
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500503 if (fErrorCount) {
John Stiles5c7bb322020-10-22 11:09:15 -0400504 // Do not return programs that failed to compile.
John Stiles7247b482021-03-08 10:40:35 -0500505 } else if (!this->optimize(*program)) {
John Stiles5c7bb322020-10-22 11:09:15 -0400506 // Do not return programs that failed to optimize.
507 } else {
508 // We have a successful program!
509 success = true;
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500510 }
Ethan Nicholas55a63af2021-05-18 10:12:58 -0400511 dsl::End();
512 if (memoryPool) {
513 memoryPool->detachFromThread();
Brian Osman28f702c2021-02-02 11:52:07 -0500514 }
John Stiles5c7bb322020-10-22 11:09:15 -0400515 return success ? std::move(program) : nullptr;
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500516}
517
John Stilesbb1505f2021-02-12 09:17:53 -0500518void Compiler::verifyStaticTests(const Program& program) {
519 class StaticTestVerifier : public ProgramVisitor {
520 public:
521 StaticTestVerifier(ErrorReporter* r) : fReporter(r) {}
522
523 using ProgramVisitor::visitProgramElement;
524
525 bool visitStatement(const Statement& stmt) override {
526 switch (stmt.kind()) {
527 case Statement::Kind::kIf:
528 if (stmt.as<IfStatement>().isStatic()) {
529 fReporter->error(stmt.fOffset, "static if has non-static test");
530 }
531 break;
532
533 case Statement::Kind::kSwitch:
534 if (stmt.as<SwitchStatement>().isStatic()) {
535 fReporter->error(stmt.fOffset, "static switch has non-static test");
536 }
537 break;
538
539 default:
540 break;
541 }
542 return INHERITED::visitStatement(stmt);
543 }
544
John Stiles59e34562021-02-12 16:56:39 -0500545 bool visitExpression(const Expression&) override {
546 // We aren't looking for anything inside an Expression, so skip them entirely.
547 return false;
548 }
549
John Stilesbb1505f2021-02-12 09:17:53 -0500550 private:
551 using INHERITED = ProgramVisitor;
552 ErrorReporter* fReporter;
553 };
554
555 // If invalid static tests are permitted, we don't need to check anything.
John Stilesd1204642021-02-17 16:30:02 -0500556 if (fContext->fConfig->fSettings.fPermitInvalidStaticTests) {
John Stilesbb1505f2021-02-12 09:17:53 -0500557 return;
558 }
559
560 // Check all of the program's owned elements. (Built-in elements are assumed to be valid.)
561 StaticTestVerifier visitor{this};
562 for (const std::unique_ptr<ProgramElement>& element : program.ownedElements()) {
563 if (element->is<FunctionDefinition>()) {
564 visitor.visitProgramElement(*element);
565 }
566 }
567}
568
Brian Osman0006ad02020-11-18 15:38:39 -0500569bool Compiler::optimize(LoadedModule& module) {
570 SkASSERT(!fErrorCount);
Brian Osman0006ad02020-11-18 15:38:39 -0500571
John Stiles270cec22021-02-17 12:59:36 -0500572 // Create a temporary program configuration with default settings.
573 ProgramConfig config;
574 config.fKind = module.fKind;
John Stilesa935c3f2021-02-25 10:35:49 -0500575 AutoProgramConfig autoConfig(fContext, &config);
John Stiles270cec22021-02-17 12:59:36 -0500576
John Stilesd1204642021-02-17 16:30:02 -0500577 // Reset the Inliner.
John Stiles10d39d92021-05-04 16:13:14 -0400578 fInliner.reset();
John Stiles270cec22021-02-17 12:59:36 -0500579
580 std::unique_ptr<ProgramUsage> usage = Analysis::GetUsage(module);
Brian Osman0006ad02020-11-18 15:38:39 -0500581
582 while (fErrorCount == 0) {
Brian Osman0006ad02020-11-18 15:38:39 -0500583 // Perform inline-candidate analysis and inline any functions deemed suitable.
John Stilesf3a28db2021-03-10 23:00:47 -0500584 if (!fInliner.analyze(module.fElements, module.fSymbols, usage.get())) {
Brian Osman0006ad02020-11-18 15:38:39 -0500585 break;
586 }
587 }
588 return fErrorCount == 0;
589}
590
John Stiles0bfeae62021-03-11 09:09:42 -0500591bool Compiler::removeDeadFunctions(Program& program, ProgramUsage* usage) {
592 bool madeChanges = false;
593
594 if (program.fConfig->fSettings.fRemoveDeadFunctions) {
595 auto isDeadFunction = [&](const ProgramElement* element) {
596 if (!element->is<FunctionDefinition>()) {
597 return false;
598 }
599 const FunctionDefinition& fn = element->as<FunctionDefinition>();
John Stilese8da4d22021-03-24 09:19:45 -0400600 if (fn.declaration().isMain() || usage->get(fn.declaration()) > 0) {
John Stiles0bfeae62021-03-11 09:09:42 -0500601 return false;
602 }
603 usage->remove(*element);
604 madeChanges = true;
605 return true;
606 };
607
608 program.fElements.erase(std::remove_if(program.fElements.begin(),
609 program.fElements.end(),
610 [&](const std::unique_ptr<ProgramElement>& element) {
611 return isDeadFunction(element.get());
612 }),
613 program.fElements.end());
614 program.fSharedElements.erase(std::remove_if(program.fSharedElements.begin(),
615 program.fSharedElements.end(),
616 isDeadFunction),
617 program.fSharedElements.end());
618 }
619 return madeChanges;
620}
621
622bool Compiler::removeDeadGlobalVariables(Program& program, ProgramUsage* usage) {
623 bool madeChanges = false;
624
625 if (program.fConfig->fSettings.fRemoveDeadVariables) {
626 auto isDeadVariable = [&](const ProgramElement* element) {
627 if (!element->is<GlobalVarDeclaration>()) {
628 return false;
629 }
630 const GlobalVarDeclaration& global = element->as<GlobalVarDeclaration>();
631 const VarDeclaration& varDecl = global.declaration()->as<VarDeclaration>();
632 if (!usage->isDead(varDecl.var())) {
633 return false;
634 }
635 madeChanges = true;
636 return true;
637 };
638
639 program.fElements.erase(std::remove_if(program.fElements.begin(),
640 program.fElements.end(),
641 [&](const std::unique_ptr<ProgramElement>& element) {
642 return isDeadVariable(element.get());
643 }),
644 program.fElements.end());
645 program.fSharedElements.erase(std::remove_if(program.fSharedElements.begin(),
646 program.fSharedElements.end(),
647 isDeadVariable),
648 program.fSharedElements.end());
649 }
650 return madeChanges;
651}
652
John Stiles26541872021-03-16 12:19:54 -0400653bool Compiler::removeDeadLocalVariables(Program& program, ProgramUsage* usage) {
654 class DeadLocalVariableEliminator : public ProgramWriter {
655 public:
656 DeadLocalVariableEliminator(const Context& context, ProgramUsage* usage)
657 : fContext(context)
658 , fUsage(usage) {}
659
660 using ProgramWriter::visitProgramElement;
661
662 bool visitExpressionPtr(std::unique_ptr<Expression>& expr) override {
663 // We don't need to look inside expressions at all.
664 return false;
665 }
666
667 bool visitStatementPtr(std::unique_ptr<Statement>& stmt) override {
668 if (stmt->is<VarDeclaration>()) {
669 VarDeclaration& varDecl = stmt->as<VarDeclaration>();
670 const Variable* var = &varDecl.var();
671 ProgramUsage::VariableCounts* counts = fUsage->fVariableCounts.find(var);
672 SkASSERT(counts);
673 SkASSERT(counts->fDeclared);
674 if (CanEliminate(var, *counts)) {
675 if (var->initialValue()) {
676 // The variable has an initial-value expression, which might have side
677 // effects. ExpressionStatement::Make will preserve side effects, but
678 // replaces pure expressions with Nop.
679 fUsage->remove(stmt.get());
680 stmt = ExpressionStatement::Make(fContext, std::move(varDecl.value()));
681 fUsage->add(stmt.get());
682 } else {
683 // The variable has no initial-value and can be cleanly eliminated.
684 fUsage->remove(stmt.get());
685 stmt = std::make_unique<Nop>();
686 }
687 fMadeChanges = true;
688 }
689 return false;
690 }
691 return INHERITED::visitStatementPtr(stmt);
692 }
693
694 static bool CanEliminate(const Variable* var, const ProgramUsage::VariableCounts& counts) {
695 if (!counts.fDeclared || counts.fRead || var->storage() != VariableStorage::kLocal) {
696 return false;
697 }
698 if (var->initialValue()) {
699 SkASSERT(counts.fWrite >= 1);
700 return counts.fWrite == 1;
701 } else {
702 return counts.fWrite == 0;
703 }
704 }
705
706 bool fMadeChanges = false;
707 const Context& fContext;
708 ProgramUsage* fUsage;
709
710 using INHERITED = ProgramWriter;
711 };
712
713 DeadLocalVariableEliminator visitor{*fContext, usage};
714
715 if (program.fConfig->fSettings.fRemoveDeadVariables) {
716 for (auto& [var, counts] : usage->fVariableCounts) {
717 if (DeadLocalVariableEliminator::CanEliminate(var, counts)) {
718 // This program contains at least one dead local variable.
719 // Scan the program for any dead local variables and eliminate them all.
720 for (std::unique_ptr<ProgramElement>& pe : program.ownedElements()) {
721 if (pe->is<FunctionDefinition>()) {
722 visitor.visitProgramElement(*pe);
723 }
724 }
725 break;
726 }
727 }
728 }
729
730 return visitor.fMadeChanges;
731}
732
John Stiles25be58e2021-05-20 14:38:40 -0400733void Compiler::removeUnreachableCode(Program& program, ProgramUsage* usage) {
734 class UnreachableCodeEliminator : public ProgramWriter {
735 public:
736 UnreachableCodeEliminator(const Context& context, ProgramUsage* usage)
737 : fContext(context)
738 , fUsage(usage) {
739 fFoundFunctionExit.push(false);
740 fFoundLoopExit.push(false);
741 }
742
743 using ProgramWriter::visitProgramElement;
744
745 bool visitExpressionPtr(std::unique_ptr<Expression>& expr) override {
746 // We don't need to look inside expressions at all.
747 return false;
748 }
749
750 bool visitStatementPtr(std::unique_ptr<Statement>& stmt) override {
751 if (fFoundFunctionExit.top() || fFoundLoopExit.top()) {
752 // If we already found an exit in this section, anything beyond it is dead code.
753 if (!stmt->is<Nop>()) {
754 // Eliminate the dead statement by substituting a Nop.
755 fUsage->remove(stmt.get());
756 stmt = std::make_unique<Nop>();
757 }
758 return false;
759 }
760
761 switch (stmt->kind()) {
762 case Statement::Kind::kReturn:
763 case Statement::Kind::kDiscard:
764 // We found a function exit on this path.
765 fFoundFunctionExit.top() = true;
766 break;
767
768 case Statement::Kind::kBreak:
769 case Statement::Kind::kContinue:
770 // We found a loop exit on this path. Note that we skip over switch statements
771 // completely when eliminating code, so any `break` statement would be breaking
772 // out of a loop, not out of a switch.
773 fFoundLoopExit.top() = true;
774 break;
775
776 case Statement::Kind::kExpression:
777 case Statement::Kind::kInlineMarker:
778 case Statement::Kind::kNop:
779 case Statement::Kind::kVarDeclaration:
780 // These statements don't affect control flow.
781 break;
782
783 case Statement::Kind::kBlock:
784 // Blocks are on the straight-line path and don't affect control flow.
785 return INHERITED::visitStatementPtr(stmt);
786
787 case Statement::Kind::kDo: {
788 // Function-exits are allowed to propagate outside of a do-loop, because it
789 // always executes its body at least once.
790 fFoundLoopExit.push(false);
791 bool result = INHERITED::visitStatementPtr(stmt);
792 fFoundLoopExit.pop();
793 return result;
794 }
795 case Statement::Kind::kFor: {
796 // Function-exits are not allowed to propagate out, because a for-loop or while-
797 // loop could potentially run zero times.
798 fFoundFunctionExit.push(false);
799 fFoundLoopExit.push(false);
800 bool result = INHERITED::visitStatementPtr(stmt);
801 fFoundLoopExit.pop();
802 fFoundFunctionExit.pop();
803 return result;
804 }
805 case Statement::Kind::kIf: {
806 // This statement is conditional and encloses two inner sections of code.
807 // If both sides contain a function-exit or loop-exit, that exit is allowed to
808 // propagate out.
809 IfStatement& ifStmt = stmt->as<IfStatement>();
810
811 fFoundFunctionExit.push(false);
812 fFoundLoopExit.push(false);
813 bool result = (ifStmt.ifTrue() && this->visitStatementPtr(ifStmt.ifTrue()));
814 bool foundFunctionExitOnTrue = fFoundFunctionExit.top();
815 bool foundLoopExitOnTrue = fFoundLoopExit.top();
816 fFoundFunctionExit.pop();
817 fFoundLoopExit.pop();
818
819 fFoundFunctionExit.push(false);
820 fFoundLoopExit.push(false);
821 result |= (ifStmt.ifFalse() && this->visitStatementPtr(ifStmt.ifFalse()));
822 bool foundFunctionExitOnFalse = fFoundFunctionExit.top();
823 bool foundLoopExitOnFalse = fFoundLoopExit.top();
824 fFoundFunctionExit.pop();
825 fFoundLoopExit.pop();
826
827 fFoundFunctionExit.top() |= foundFunctionExitOnTrue && foundFunctionExitOnFalse;
828 fFoundLoopExit.top() |= foundLoopExitOnTrue && foundLoopExitOnFalse;
829 return result;
830 }
831 case Statement::Kind::kSwitch:
832 case Statement::Kind::kSwitchCase:
833 // We skip past switch statements entirely when scanning for dead code. Their
834 // control flow is quite complex and we already do a good job of flattening out
835 // switches on constant values.
836 break;
837 }
838
839 return false;
840 }
841
842 const Context& fContext;
843 ProgramUsage* fUsage;
844 std::stack<bool> fFoundFunctionExit;
845 std::stack<bool> fFoundLoopExit;
846
847 using INHERITED = ProgramWriter;
848 };
849
850 for (std::unique_ptr<ProgramElement>& pe : program.ownedElements()) {
851 if (pe->is<FunctionDefinition>()) {
852 UnreachableCodeEliminator visitor{*fContext, usage};
853 visitor.visitProgramElement(*pe);
854 }
855 }
856}
857
Ethan Nicholas00543112018-07-31 09:44:36 -0400858bool Compiler::optimize(Program& program) {
John Stiles7247b482021-03-08 10:40:35 -0500859 // The optimizer only needs to run when it is enabled.
860 if (!program.fConfig->fSettings.fOptimize) {
861 return true;
862 }
863
Ethan Nicholas00543112018-07-31 09:44:36 -0400864 SkASSERT(!fErrorCount);
Brian Osman010ce6a2020-10-19 16:34:10 -0400865 ProgramUsage* usage = program.fUsage.get();
John Stiles7954d6c2020-09-01 10:53:02 -0400866
John Stilesb6664582021-03-19 09:46:00 -0400867 if (fErrorCount == 0) {
John Stiles87fc6572021-04-01 14:56:34 +0000868 // Run the inliner only once; it is expensive! Multiple passes can occasionally shake out
869 // more wins, but it's diminishing returns.
870 fInliner.analyze(program.ownedElements(), program.fSymbols, usage);
Ethan Nicholas34b19c52020-09-14 11:33:47 -0400871
John Stilesb6664582021-03-19 09:46:00 -0400872 while (this->removeDeadFunctions(program, usage)) {
873 // Removing dead functions may cause more functions to become unreferenced. Try again.
Ethan Nicholas34b19c52020-09-14 11:33:47 -0400874 }
John Stilesb6664582021-03-19 09:46:00 -0400875 while (this->removeDeadLocalVariables(program, usage)) {
876 // Removing dead variables may cause more variables to become unreferenced. Try again.
877 }
John Stiles25be58e2021-05-20 14:38:40 -0400878 // Unreachable code can confuse some drivers, so it's worth removing. (skia:12012)
879 this->removeUnreachableCode(program, usage);
880
John Stilesb6664582021-03-19 09:46:00 -0400881 if (program.fConfig->fKind != ProgramKind::kFragmentProcessor) {
882 this->removeDeadGlobalVariables(program, usage);
Ethan Nicholas0dc80872019-02-08 15:46:24 -0500883 }
Ethan Nicholas00543112018-07-31 09:44:36 -0400884 }
John Stilesbb1505f2021-02-12 09:17:53 -0500885
886 if (fErrorCount == 0) {
887 this->verifyStaticTests(program);
888 }
889
Ethan Nicholas00543112018-07-31 09:44:36 -0400890 return fErrorCount == 0;
891}
892
Brian Osmanfb32ddf2019-06-18 10:14:20 -0400893#if defined(SKSL_STANDALONE) || SK_SUPPORT_GPU
894
Ethan Nicholas00543112018-07-31 09:44:36 -0400895bool Compiler::toSPIRV(Program& program, OutputStream& out) {
Brian Osman7a20b5c2021-03-15 16:23:33 -0400896 TRACE_EVENT0("skia.shaders", "SkSL::Compiler::toSPIRV");
John Stilesa289ac22021-05-06 07:35:35 -0400897 AutoSource as(this, program.fSource.get());
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -0400898#ifdef SK_ENABLE_SPIRV_VALIDATION
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400899 StringStream buffer;
Brian Osman8b43dad2020-10-09 13:31:42 -0400900 SPIRVCodeGenerator cg(fContext.get(), &program, this, &buffer);
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -0400901 bool result = cg.generateCode();
John Stiles270cec22021-02-17 12:59:36 -0500902 if (result && program.fConfig->fSettings.fValidateSPIRV) {
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -0400903 spvtools::SpirvTools tools(SPV_ENV_VULKAN_1_0);
Ethan Nicholas762466e2017-06-29 10:03:38 -0400904 const String& data = buffer.str();
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400905 SkASSERT(0 == data.size() % 4);
Brian Osman8d09d4a2020-11-24 15:51:06 -0500906 String errors;
907 auto dumpmsg = [&errors](spv_message_level_t, const char*, const spv_position_t&,
908 const char* m) {
909 errors.appendf("SPIR-V validation error: %s\n", m);
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -0400910 };
911 tools.SetMessageConsumer(dumpmsg);
Brian Osman8d09d4a2020-11-24 15:51:06 -0500912
913 // Verify that the SPIR-V we produced is valid. At runtime, we will abort() with a message
914 // explaining the error. In standalone mode (skslc), we will send the message, plus the
915 // entire disassembled SPIR-V (for easier context & debugging) as *our* error message.
916 result = tools.Validate((const uint32_t*) data.c_str(), data.size() / 4);
917
918 if (!result) {
919#if defined(SKSL_STANDALONE)
920 // Convert the string-stream to a SPIR-V disassembly.
921 std::string disassembly;
922 if (tools.Disassemble((const uint32_t*)data.data(), data.size() / 4, &disassembly)) {
923 errors.append(disassembly);
924 }
925 this->error(-1, errors);
926#else
927 SkDEBUGFAILF("%s", errors.c_str());
928#endif
929 }
Ethan Nicholas762466e2017-06-29 10:03:38 -0400930 out.write(data.c_str(), data.size());
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -0400931 }
932#else
Brian Osman8b43dad2020-10-09 13:31:42 -0400933 SPIRVCodeGenerator cg(fContext.get(), &program, this, &out);
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500934 bool result = cg.generateCode();
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -0400935#endif
Ethan Nicholasce33f102016-12-09 17:22:59 -0500936 return result;
937}
938
Ethan Nicholas00543112018-07-31 09:44:36 -0400939bool Compiler::toSPIRV(Program& program, String* out) {
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400940 StringStream buffer;
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500941 bool result = this->toSPIRV(program, buffer);
942 if (result) {
Ethan Nicholas762466e2017-06-29 10:03:38 -0400943 *out = buffer.str();
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500944 }
945 return result;
946}
947
Ethan Nicholas00543112018-07-31 09:44:36 -0400948bool Compiler::toGLSL(Program& program, OutputStream& out) {
Brian Osman7a20b5c2021-03-15 16:23:33 -0400949 TRACE_EVENT0("skia.shaders", "SkSL::Compiler::toGLSL");
Brian Osman88cda172020-10-09 12:05:16 -0400950 AutoSource as(this, program.fSource.get());
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400951 GLSLCodeGenerator cg(fContext.get(), &program, this, &out);
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500952 bool result = cg.generateCode();
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500953 return result;
954}
955
Ethan Nicholas00543112018-07-31 09:44:36 -0400956bool Compiler::toGLSL(Program& program, String* out) {
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400957 StringStream buffer;
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500958 bool result = this->toGLSL(program, buffer);
959 if (result) {
Ethan Nicholas762466e2017-06-29 10:03:38 -0400960 *out = buffer.str();
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500961 }
962 return result;
963}
964
Brian Osmanc0243912020-02-19 15:35:26 -0500965bool Compiler::toHLSL(Program& program, String* out) {
966 String spirv;
967 if (!this->toSPIRV(program, &spirv)) {
968 return false;
969 }
970
971 return SPIRVtoHLSL(spirv, out);
972}
973
Ethan Nicholas00543112018-07-31 09:44:36 -0400974bool Compiler::toMetal(Program& program, OutputStream& out) {
Brian Osman7a20b5c2021-03-15 16:23:33 -0400975 TRACE_EVENT0("skia.shaders", "SkSL::Compiler::toMetal");
John Stilesa289ac22021-05-06 07:35:35 -0400976 AutoSource as(this, program.fSource.get());
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400977 MetalCodeGenerator cg(fContext.get(), &program, this, &out);
Ethan Nicholascc305772017-10-13 16:17:45 -0400978 bool result = cg.generateCode();
Ethan Nicholascc305772017-10-13 16:17:45 -0400979 return result;
980}
981
Ethan Nicholas00543112018-07-31 09:44:36 -0400982bool Compiler::toMetal(Program& program, String* out) {
Timothy Liangb8eeb802018-07-23 16:46:16 -0400983 StringStream buffer;
984 bool result = this->toMetal(program, buffer);
985 if (result) {
986 *out = buffer.str();
987 }
988 return result;
989}
990
Greg Daniela28ea672020-09-25 11:12:56 -0400991#if defined(SKSL_STANDALONE) || GR_TEST_UTILS
Ethan Nicholas00543112018-07-31 09:44:36 -0400992bool Compiler::toCPP(Program& program, String name, OutputStream& out) {
Brian Osman88cda172020-10-09 12:05:16 -0400993 AutoSource as(this, program.fSource.get());
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400994 CPPCodeGenerator cg(fContext.get(), &program, this, name, &out);
Ethan Nicholas762466e2017-06-29 10:03:38 -0400995 bool result = cg.generateCode();
Ethan Nicholas762466e2017-06-29 10:03:38 -0400996 return result;
997}
998
John Stiles82ab3402021-04-13 17:13:03 -0400999bool Compiler::toDSLCPP(Program& program, String name, OutputStream& out) {
1000 AutoSource as(this, program.fSource.get());
1001 DSLCPPCodeGenerator cg(fContext.get(), &program, this, name, &out);
1002 bool result = cg.generateCode();
1003 return result;
1004}
1005
Ethan Nicholas00543112018-07-31 09:44:36 -04001006bool Compiler::toH(Program& program, String name, OutputStream& out) {
Brian Osman88cda172020-10-09 12:05:16 -04001007 AutoSource as(this, program.fSource.get());
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001008 HCodeGenerator cg(fContext.get(), &program, this, name, &out);
Ethan Nicholas762466e2017-06-29 10:03:38 -04001009 bool result = cg.generateCode();
Ethan Nicholas00543112018-07-31 09:44:36 -04001010 return result;
1011}
Greg Daniela28ea672020-09-25 11:12:56 -04001012#endif // defined(SKSL_STANDALONE) || GR_TEST_UTILS
Ethan Nicholas00543112018-07-31 09:44:36 -04001013
Ethan Nicholas2a479a52020-08-18 16:29:45 -04001014#endif // defined(SKSL_STANDALONE) || SK_SUPPORT_GPU
Brian Osman2e29ab52019-09-20 12:19:11 -04001015
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001016Position Compiler::position(int offset) {
Ethan Nicholas3c729892020-12-07 12:47:17 -05001017 if (fSource && offset >= 0) {
1018 int line = 1;
1019 int column = 1;
1020 for (int i = 0; i < offset; i++) {
1021 if ((*fSource)[i] == '\n') {
1022 ++line;
1023 column = 1;
1024 }
1025 else {
1026 ++column;
1027 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001028 }
Ethan Nicholas3c729892020-12-07 12:47:17 -05001029 return Position(line, column);
1030 } else {
1031 return Position(-1, -1);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001032 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001033}
1034
1035void Compiler::error(int offset, String msg) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001036 fErrorCount++;
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001037 Position pos = this->position(offset);
John Stiles8d3642e2021-01-22 09:50:04 -05001038 fErrorTextLength.push_back(fErrorText.length());
Ethan Nicholas3c729892020-12-07 12:47:17 -05001039 fErrorText += "error: " + (pos.fLine >= 1 ? to_string(pos.fLine) + ": " : "") + msg + "\n";
ethannicholasb3058bd2016-07-01 08:22:01 -07001040}
1041
John Stiles8d3642e2021-01-22 09:50:04 -05001042void Compiler::setErrorCount(int c) {
1043 if (c < fErrorCount) {
1044 fErrorText.resize(fErrorTextLength[c]);
1045 fErrorTextLength.resize(c);
1046 fErrorCount = c;
1047 }
1048}
1049
Ethan Nicholas95046142021-01-07 10:57:27 -05001050String Compiler::errorText(bool showCount) {
1051 if (showCount) {
1052 this->writeErrorCount();
1053 }
Ethan Nicholas00543112018-07-31 09:44:36 -04001054 fErrorCount = 0;
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001055 String result = fErrorText;
Ethan Nicholas95046142021-01-07 10:57:27 -05001056 fErrorText = "";
ethannicholasb3058bd2016-07-01 08:22:01 -07001057 return result;
1058}
1059
1060void Compiler::writeErrorCount() {
1061 if (fErrorCount) {
1062 fErrorText += to_string(fErrorCount) + " error";
1063 if (fErrorCount > 1) {
1064 fErrorText += "s";
1065 }
1066 fErrorText += "\n";
1067 }
1068}
1069
John Stilesa6841be2020-08-06 14:11:56 -04001070} // namespace SkSL