blob: 512a401650cc932cacb483472adb3c895cf26106 [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"
Leon Scrogginsb66214e2021-02-11 17:14:18 -050014#include "src/core/SkTraceEvent.h"
John Stilesf3a28db2021-03-10 23:00:47 -050015#include "src/sksl/SkSLConstantFolder.h"
Ethan Nicholasdd2fdea2021-07-20 15:23:04 -040016#include "src/sksl/SkSLDSLParser.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "src/sksl/SkSLIRGenerator.h"
Ethan Nicholas58014fa2021-09-29 17:18:18 -040018#include "src/sksl/SkSLIntrinsicMap.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"
Ethan Nicholasc8452722021-10-07 10:47:32 -040022#include "src/sksl/SkSLThreadContext.h"
John Stiles3738ef52021-04-13 10:41:57 -040023#include "src/sksl/codegen/SkSLGLSLCodeGenerator.h"
John Stiles3738ef52021-04-13 10:41:57 -040024#include "src/sksl/codegen/SkSLMetalCodeGenerator.h"
25#include "src/sksl/codegen/SkSLSPIRVCodeGenerator.h"
26#include "src/sksl/codegen/SkSLSPIRVtoHLSL.h"
Ethan Nicholas55a63af2021-05-18 10:12:58 -040027#include "src/sksl/dsl/priv/DSLWriter.h"
28#include "src/sksl/dsl/priv/DSL_priv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050029#include "src/sksl/ir/SkSLExpression.h"
30#include "src/sksl/ir/SkSLExpressionStatement.h"
31#include "src/sksl/ir/SkSLFunctionCall.h"
John Stiles7591d4b2021-09-13 13:32:06 -040032#include "src/sksl/ir/SkSLLiteral.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050033#include "src/sksl/ir/SkSLModifiersDeclaration.h"
34#include "src/sksl/ir/SkSLNop.h"
35#include "src/sksl/ir/SkSLSymbolTable.h"
36#include "src/sksl/ir/SkSLTernaryExpression.h"
37#include "src/sksl/ir/SkSLUnresolvedFunction.h"
38#include "src/sksl/ir/SkSLVarDeclarations.h"
Ethan Nicholas17807552021-10-01 09:42:36 -040039#include "src/sksl/transform/SkSLProgramWriter.h"
John Stiles71766372021-10-06 15:54:55 -040040#include "src/sksl/transform/SkSLTransform.h"
John Stilese6150002020-10-05 12:03:53 -040041#include "src/utils/SkBitSet.h"
ethannicholasb3058bd2016-07-01 08:22:01 -070042
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -040043#include <fstream>
44
Ethan Nicholasa11035b2019-11-26 16:27:47 -050045#if !defined(SKSL_STANDALONE) & SK_SUPPORT_GPU
46#include "include/gpu/GrContextOptions.h"
47#include "src/gpu/GrShaderCaps.h"
48#endif
49
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -040050#ifdef SK_ENABLE_SPIRV_VALIDATION
51#include "spirv-tools/libspirv.hpp"
52#endif
53
Brian Osman3d87e9f2020-10-08 11:50:22 -040054#if defined(SKSL_STANDALONE)
Ethan Nicholasc18bb512020-07-28 14:46:53 -040055
Brian Osman3d87e9f2020-10-08 11:50:22 -040056// In standalone mode, we load the textual sksl source files. GN generates or copies these files
57// to the skslc executable directory. The "data" in this mode is just the filename.
58#define MODULE_DATA(name) MakeModulePath("sksl_" #name ".sksl")
59
60#else
61
62// At runtime, we load the dehydrated sksl data files. The data is a (pointer, size) pair.
Ethan Nicholasc18bb512020-07-28 14:46:53 -040063#include "src/sksl/generated/sksl_frag.dehydrated.sksl"
Ethan Nicholasc18bb512020-07-28 14:46:53 -040064#include "src/sksl/generated/sksl_gpu.dehydrated.sksl"
Brian Osmanb06301e2020-11-06 11:45:36 -050065#include "src/sksl/generated/sksl_public.dehydrated.sksl"
Brian Osmancbb60bd2021-04-12 09:49:20 -040066#include "src/sksl/generated/sksl_rt_shader.dehydrated.sksl"
Ethan Nicholasc18bb512020-07-28 14:46:53 -040067#include "src/sksl/generated/sksl_vert.dehydrated.sksl"
68
Brian Osman3d87e9f2020-10-08 11:50:22 -040069#define MODULE_DATA(name) MakeModuleData(SKSL_INCLUDE_sksl_##name,\
70 SKSL_INCLUDE_sksl_##name##_LENGTH)
Ethan Nicholasc18bb512020-07-28 14:46:53 -040071
72#endif
Ethan Nicholas0d997662019-04-08 09:46:01 -040073
ethannicholasb3058bd2016-07-01 08:22:01 -070074namespace SkSL {
75
John Stiles7247b482021-03-08 10:40:35 -050076// These flags allow tools like Viewer or Nanobench to override the compiler's ProgramSettings.
John Stiles2ee4d7a2021-03-30 10:30:47 -040077Compiler::OverrideFlag Compiler::sOptimizer = OverrideFlag::kDefault;
78Compiler::OverrideFlag Compiler::sInliner = OverrideFlag::kDefault;
John Stiles8ef4d6c2021-03-05 16:01:45 -050079
John Stiles47c0a742021-02-09 09:30:35 -050080using RefKind = VariableReference::RefKind;
81
Brian Osman88cda172020-10-09 12:05:16 -040082class AutoSource {
83public:
Ethan Nicholasb449fff2021-08-04 15:06:37 -040084 AutoSource(Compiler* compiler, const char* source)
John Stilesa289ac22021-05-06 07:35:35 -040085 : fCompiler(compiler) {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -040086 SkASSERT(!fCompiler->errorReporter().source());
87 fCompiler->errorReporter().setSource(source);
Brian Osman88cda172020-10-09 12:05:16 -040088 }
89
John Stilesa289ac22021-05-06 07:35:35 -040090 ~AutoSource() {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -040091 fCompiler->errorReporter().setSource(nullptr);
John Stilesa289ac22021-05-06 07:35:35 -040092 }
Brian Osman88cda172020-10-09 12:05:16 -040093
94 Compiler* fCompiler;
Brian Osman88cda172020-10-09 12:05:16 -040095};
96
John Stilesa935c3f2021-02-25 10:35:49 -050097class AutoProgramConfig {
98public:
99 AutoProgramConfig(std::shared_ptr<Context>& context, ProgramConfig* config)
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -0400100 : fContext(context.get())
101 , fOldConfig(fContext->fConfig) {
John Stilesa935c3f2021-02-25 10:35:49 -0500102 fContext->fConfig = config;
103 }
104
105 ~AutoProgramConfig() {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -0400106 fContext->fConfig = fOldConfig;
John Stilesa935c3f2021-02-25 10:35:49 -0500107 }
108
109 Context* fContext;
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -0400110 ProgramConfig* fOldConfig;
John Stilesa935c3f2021-02-25 10:35:49 -0500111};
112
John Stiles10d39d92021-05-04 16:13:14 -0400113class AutoModifiersPool {
114public:
115 AutoModifiersPool(std::shared_ptr<Context>& context, ModifiersPool* modifiersPool)
116 : fContext(context.get()) {
117 SkASSERT(!fContext->fModifiersPool);
118 fContext->fModifiersPool = modifiersPool;
119 }
120
121 ~AutoModifiersPool() {
122 fContext->fModifiersPool = nullptr;
123 }
124
125 Context* fContext;
126};
127
John Stilesd6a5f4492021-02-11 15:46:11 -0500128Compiler::Compiler(const ShaderCapsClass* caps)
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -0400129 : fErrorReporter(this)
John Stiles2dda4b62021-09-16 13:18:10 -0400130 , fContext(std::make_shared<Context>(fErrorReporter, *caps, fMangler))
John Stilesa47b3512021-05-04 16:15:00 -0400131 , fInliner(fContext.get()) {
John Stilesc1a98b82021-02-24 13:35:02 -0500132 SkASSERT(caps);
John Stilesb624b722021-08-13 12:16:13 -0400133 fRootModule.fSymbols = this->makeRootSymbolTable();
134 fPrivateModule.fSymbols = this->makePrivateSymbolTable(fRootModule.fSymbols);
John Stilesc1a98b82021-02-24 13:35:02 -0500135 fIRGenerator = std::make_unique<IRGenerator>(fContext.get());
John Stilesb624b722021-08-13 12:16:13 -0400136}
137
138Compiler::~Compiler() {}
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
John Stilesb624b722021-08-13 12:16:13 -0400142std::shared_ptr<SymbolTable> Compiler::makeRootSymbolTable() {
Ethan Nicholasc7774a72021-08-27 15:34:05 -0400143 auto rootSymbolTable = std::make_shared<SymbolTable>(*fContext, /*builtin=*/true);
John Stilesb624b722021-08-13 12:16:13 -0400144
Brian Osmanb06301e2020-11-06 11:45:36 -0500145 const SkSL::Symbol* rootTypes[] = {
146 TYPE(Void),
Brian Salomonbf7b6202016-11-11 16:08:03 -0500147
Brian Osmanb06301e2020-11-06 11:45:36 -0500148 TYPE( Float), TYPE( Float2), TYPE( Float3), TYPE( Float4),
149 TYPE( Half), TYPE( Half2), TYPE( Half3), TYPE( Half4),
150 TYPE( Int), TYPE( Int2), TYPE( Int3), TYPE( Int4),
John Stiles823c5042021-08-17 12:09:00 -0400151 TYPE( UInt), TYPE( UInt2), TYPE( UInt3), TYPE( UInt4),
152 TYPE( Short), TYPE( Short2), TYPE( Short3), TYPE( Short4),
153 TYPE(UShort), TYPE(UShort2), TYPE(UShort3), TYPE(UShort4),
Brian Osmanb06301e2020-11-06 11:45:36 -0500154 TYPE( Bool), TYPE( Bool2), TYPE( Bool3), TYPE( Bool4),
Brian Salomon2a51de82016-11-16 12:06:01 -0500155
John Stiles823c5042021-08-17 12:09:00 -0400156 TYPE(Float2x2), TYPE(Float2x3), TYPE(Float2x4),
157 TYPE(Float3x2), TYPE(Float3x3), TYPE(Float3x4),
158 TYPE(Float4x2), TYPE(Float4x3), TYPE(Float4x4),
159
160 TYPE(Half2x2), TYPE(Half2x3), TYPE(Half2x4),
161 TYPE(Half3x2), TYPE(Half3x3), TYPE(Half3x4),
162 TYPE(Half4x2), TYPE(Half4x3), TYPE(Half4x4),
Greg Daniel64773e62016-11-22 09:44:03 -0500163
Brian Osmanc63f4312020-12-23 11:44:14 -0500164 TYPE(SquareMat), TYPE(SquareHMat),
John Stiles823c5042021-08-17 12:09:00 -0400165 TYPE(Mat), TYPE(HMat),
ethannicholasb3058bd2016-07-01 08:22:01 -0700166
John Stiles823c5042021-08-17 12:09:00 -0400167 // TODO(skia:12349): generic short/ushort
168 TYPE(GenType), TYPE(GenIType), TYPE(GenUType),
169 TYPE(GenHType), /* (GenSType) (GenUSType) */
170 TYPE(GenBType),
171
172 TYPE(Vec), TYPE(IVec), TYPE(UVec),
173 TYPE(HVec), TYPE(SVec), TYPE(USVec),
174 TYPE(BVec),
Brian Osmanb06301e2020-11-06 11:45:36 -0500175
Brian Osman14d00962021-04-02 17:04:35 -0400176 TYPE(ColorFilter),
177 TYPE(Shader),
John Stilesbb2ef922021-07-26 08:32:07 -0400178 TYPE(Blender),
Brian Osmanb06301e2020-11-06 11:45:36 -0500179 };
180
John Stilesb624b722021-08-13 12:16:13 -0400181 for (const SkSL::Symbol* type : rootTypes) {
182 rootSymbolTable->addWithoutOwnership(type);
183 }
184
185 return rootSymbolTable;
186}
187
188std::shared_ptr<SymbolTable> Compiler::makePrivateSymbolTable(std::shared_ptr<SymbolTable> parent) {
189 auto privateSymbolTable = std::make_shared<SymbolTable>(parent, /*builtin=*/true);
190
Brian Osmanb06301e2020-11-06 11:45:36 -0500191 const SkSL::Symbol* privateTypes[] = {
192 TYPE(Sampler1D), TYPE(Sampler2D), TYPE(Sampler3D),
193 TYPE(SamplerExternalOES),
Brian Osmanb06301e2020-11-06 11:45:36 -0500194 TYPE(Sampler2DRect),
Brian Osmanb06301e2020-11-06 11:45:36 -0500195
196 TYPE(ISampler2D),
Brian Osmanb06301e2020-11-06 11:45:36 -0500197 TYPE(SubpassInput), TYPE(SubpassInputMS),
198
Brian Osmanb06301e2020-11-06 11:45:36 -0500199 TYPE(Sampler),
200 TYPE(Texture2D),
201 };
202
Brian Osmanb06301e2020-11-06 11:45:36 -0500203 for (const SkSL::Symbol* type : privateTypes) {
John Stilesb624b722021-08-13 12:16:13 -0400204 privateSymbolTable->addWithoutOwnership(type);
Brian Osmanb06301e2020-11-06 11:45:36 -0500205 }
206
Brian Osman3887a012020-09-30 13:22:27 -0400207 // sk_Caps is "builtin", but all references to it are resolved to Settings, so we don't need to
208 // treat it as builtin (ie, no need to clone it into the Program).
Ethan Nicholas89cfde12021-09-27 11:20:34 -0400209 privateSymbolTable->add(std::make_unique<Variable>(/*line=*/-1,
John Stilesb624b722021-08-13 12:16:13 -0400210 fCoreModifiers.add(Modifiers{}),
211 "sk_Caps",
212 fContext->fTypes.fSkCaps.get(),
213 /*builtin=*/false,
214 Variable::Storage::kGlobal));
Ethan Nicholas3605ace2016-11-21 15:59:48 -0500215
John Stilesb624b722021-08-13 12:16:13 -0400216 return privateSymbolTable;
ethannicholasb3058bd2016-07-01 08:22:01 -0700217}
218
John Stilesb624b722021-08-13 12:16:13 -0400219#undef TYPE
ethannicholasb3058bd2016-07-01 08:22:01 -0700220
Brian Osman56269982020-11-20 12:38:07 -0500221const ParsedModule& Compiler::loadGPUModule() {
222 if (!fGPUModule.fSymbols) {
John Stilesdbd4e6f2021-02-16 13:29:15 -0500223 fGPUModule = this->parseModule(ProgramKind::kFragment, MODULE_DATA(gpu), fPrivateModule);
Brian Osman56269982020-11-20 12:38:07 -0500224 }
225 return fGPUModule;
226}
227
228const ParsedModule& Compiler::loadFragmentModule() {
229 if (!fFragmentModule.fSymbols) {
John Stilesdbd4e6f2021-02-16 13:29:15 -0500230 fFragmentModule = this->parseModule(ProgramKind::kFragment, MODULE_DATA(frag),
Brian Osman56269982020-11-20 12:38:07 -0500231 this->loadGPUModule());
232 }
233 return fFragmentModule;
234}
235
236const ParsedModule& Compiler::loadVertexModule() {
237 if (!fVertexModule.fSymbols) {
John Stilesdbd4e6f2021-02-16 13:29:15 -0500238 fVertexModule = this->parseModule(ProgramKind::kVertex, MODULE_DATA(vert),
Brian Osman56269982020-11-20 12:38:07 -0500239 this->loadGPUModule());
240 }
241 return fVertexModule;
242}
243
Brian Osmancbb60bd2021-04-12 09:49:20 -0400244static void add_glsl_type_aliases(SkSL::SymbolTable* symbols, const SkSL::BuiltinTypes& types) {
245 // Add some aliases to the runtime effect modules so that it's friendlier, and more like GLSL
246 symbols->addAlias("vec2", types.fFloat2.get());
247 symbols->addAlias("vec3", types.fFloat3.get());
248 symbols->addAlias("vec4", types.fFloat4.get());
249
250 symbols->addAlias("ivec2", types.fInt2.get());
251 symbols->addAlias("ivec3", types.fInt3.get());
252 symbols->addAlias("ivec4", types.fInt4.get());
253
254 symbols->addAlias("bvec2", types.fBool2.get());
255 symbols->addAlias("bvec3", types.fBool3.get());
256 symbols->addAlias("bvec4", types.fBool4.get());
257
258 symbols->addAlias("mat2", types.fFloat2x2.get());
259 symbols->addAlias("mat3", types.fFloat3x3.get());
260 symbols->addAlias("mat4", types.fFloat4x4.get());
261}
262
Brian Osmana8b897b2021-08-30 16:40:44 -0400263const ParsedModule& Compiler::loadPublicModule() {
264 if (!fPublicModule.fSymbols) {
265 fPublicModule = this->parseModule(ProgramKind::kGeneric, MODULE_DATA(public), fRootModule);
266 add_glsl_type_aliases(fPublicModule.fSymbols.get(), fContext->fTypes);
Brian Osmancbb60bd2021-04-12 09:49:20 -0400267 }
Brian Osmana8b897b2021-08-30 16:40:44 -0400268 return fPublicModule;
Brian Osmancbb60bd2021-04-12 09:49:20 -0400269}
270
271const ParsedModule& Compiler::loadRuntimeShaderModule() {
272 if (!fRuntimeShaderModule.fSymbols) {
273 fRuntimeShaderModule = this->parseModule(
274 ProgramKind::kRuntimeShader, MODULE_DATA(rt_shader), this->loadPublicModule());
Brian Osmancbb60bd2021-04-12 09:49:20 -0400275 }
276 return fRuntimeShaderModule;
277}
278
John Stilesdbd4e6f2021-02-16 13:29:15 -0500279const ParsedModule& Compiler::moduleForProgramKind(ProgramKind kind) {
Brian Osman88cda172020-10-09 12:05:16 -0400280 switch (kind) {
Brian Osmana8b897b2021-08-30 16:40:44 -0400281 case ProgramKind::kVertex: return this->loadVertexModule(); break;
282 case ProgramKind::kFragment: return this->loadFragmentModule(); break;
283 case ProgramKind::kRuntimeColorFilter: return this->loadPublicModule(); break;
284 case ProgramKind::kRuntimeShader: return this->loadRuntimeShaderModule(); break;
285 case ProgramKind::kRuntimeBlender: return this->loadPublicModule(); break;
286 case ProgramKind::kGeneric: return this->loadPublicModule(); break;
Brian Osman88cda172020-10-09 12:05:16 -0400287 }
288 SkUNREACHABLE;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400289}
290
John Stilesdbd4e6f2021-02-16 13:29:15 -0500291LoadedModule Compiler::loadModule(ProgramKind kind,
Brian Osman3d87e9f2020-10-08 11:50:22 -0400292 ModuleData data,
John Stilesa935c3f2021-02-25 10:35:49 -0500293 std::shared_ptr<SymbolTable> base,
294 bool dehydrate) {
295 if (dehydrate) {
296 // NOTE: This is a workaround. When dehydrating includes, skslc doesn't know which module
297 // it's preparing, nor what the correct base module is. We can't use 'Root', because many
298 // GPU intrinsics reference private types, like samplers or textures. Today, 'Private' does
299 // contain the union of all known types, so this is safe. If we ever have types that only
300 // exist in 'Public' (for example), this logic needs to be smarter (by choosing the correct
301 // base for the module we're compiling).
John Stilesb624b722021-08-13 12:16:13 -0400302 base = fPrivateModule.fSymbols;
Brian Osman3d87e9f2020-10-08 11:50:22 -0400303 }
John Stilesa935c3f2021-02-25 10:35:49 -0500304 SkASSERT(base);
305
John Stilesa47b3512021-05-04 16:15:00 -0400306 // Put the core-module modifier pool into the context.
307 AutoModifiersPool autoPool(fContext, &fCoreModifiers);
John Stiles10d39d92021-05-04 16:13:14 -0400308
John Stilesa935c3f2021-02-25 10:35:49 -0500309 // Built-in modules always use default program settings.
Ethan Nicholas55a63af2021-05-18 10:12:58 -0400310 Program::Settings settings;
311 settings.fReplaceSettings = !dehydrate;
Brian Osman3d87e9f2020-10-08 11:50:22 -0400312
313#if defined(SKSL_STANDALONE)
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -0400314 SkASSERT(this->errorCount() == 0);
Brian Osman3d87e9f2020-10-08 11:50:22 -0400315 SkASSERT(data.fPath);
316 std::ifstream in(data.fPath);
John Stilesd51c9792021-03-18 11:40:14 -0400317 String text{std::istreambuf_iterator<char>(in), std::istreambuf_iterator<char>()};
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -0400318 if (in.rdstate()) {
Brian Osman3d87e9f2020-10-08 11:50:22 -0400319 printf("error reading %s\n", data.fPath);
Ethan Nicholasb33fa3f2020-08-06 13:00:19 -0400320 abort();
321 }
Brian Osman88cda172020-10-09 12:05:16 -0400322 ParsedModule baseModule = {base, /*fIntrinsics=*/nullptr};
Ethan Nicholas051aeb72021-09-24 16:39:19 -0400323 LoadedModule result = DSLParser(this, settings, kind,
324 std::move(text)).moduleInheritingFrom(std::move(baseModule));
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -0400325 if (this->errorCount()) {
Ethan Nicholas8da1e652019-05-24 11:01:59 -0400326 printf("Unexpected errors: %s\n", this->fErrorText.c_str());
Brian Osman3d87e9f2020-10-08 11:50:22 -0400327 SkDEBUGFAILF("%s %s\n", data.fPath, this->fErrorText.c_str());
Ethan Nicholas8da1e652019-05-24 11:01:59 -0400328 }
Brian Osman3d87e9f2020-10-08 11:50:22 -0400329#else
Ethan Nicholas55a63af2021-05-18 10:12:58 -0400330 ProgramConfig config;
John Stilesc8764892021-10-04 11:56:30 -0400331 config.fIsBuiltinCode = true;
Ethan Nicholas55a63af2021-05-18 10:12:58 -0400332 config.fKind = kind;
333 config.fSettings = settings;
334 AutoProgramConfig autoConfig(fContext, &config);
Brian Osman3d87e9f2020-10-08 11:50:22 -0400335 SkASSERT(data.fData && (data.fSize != 0));
John Stiles10d39d92021-05-04 16:13:14 -0400336 Rehydrator rehydrator(fContext.get(), base, data.fData, data.fSize);
Ethan Nicholas051aeb72021-09-24 16:39:19 -0400337 LoadedModule result = { kind, rehydrator.symbolTable(), rehydrator.elements() };
Brian Osman3d87e9f2020-10-08 11:50:22 -0400338#endif
339
Ethan Nicholas051aeb72021-09-24 16:39:19 -0400340 return result;
Brian Osman3d87e9f2020-10-08 11:50:22 -0400341}
342
John Stilesdbd4e6f2021-02-16 13:29:15 -0500343ParsedModule Compiler::parseModule(ProgramKind kind, ModuleData data, const ParsedModule& base) {
John Stilesa935c3f2021-02-25 10:35:49 -0500344 LoadedModule module = this->loadModule(kind, data, base.fSymbols, /*dehydrate=*/false);
Brian Osman0006ad02020-11-18 15:38:39 -0500345 this->optimize(module);
Brian Osman3d87e9f2020-10-08 11:50:22 -0400346
347 // For modules that just declare (but don't define) intrinsic functions, there will be no new
348 // program elements. In that case, we can share our parent's intrinsic map:
Brian Osman0006ad02020-11-18 15:38:39 -0500349 if (module.fElements.empty()) {
John Stiles10d39d92021-05-04 16:13:14 -0400350 return ParsedModule{module.fSymbols, base.fIntrinsics};
Brian Osman3d87e9f2020-10-08 11:50:22 -0400351 }
352
Ethan Nicholas58014fa2021-09-29 17:18:18 -0400353 auto intrinsics = std::make_shared<IntrinsicMap>(base.fIntrinsics.get());
Brian Osman3d87e9f2020-10-08 11:50:22 -0400354
355 // Now, transfer all of the program elements to an intrinsic map. This maps certain types of
356 // global objects to the declaring ProgramElement.
Brian Osman0006ad02020-11-18 15:38:39 -0500357 for (std::unique_ptr<ProgramElement>& element : module.fElements) {
Brian Osman3d87e9f2020-10-08 11:50:22 -0400358 switch (element->kind()) {
359 case ProgramElement::Kind::kFunction: {
360 const FunctionDefinition& f = element->as<FunctionDefinition>();
Ethan Nicholas0a5d0962020-10-14 13:33:18 -0400361 SkASSERT(f.declaration().isBuiltin());
362 intrinsics->insertOrDie(f.declaration().description(), std::move(element));
Brian Osman3d87e9f2020-10-08 11:50:22 -0400363 break;
364 }
John Stiles569249b2020-11-03 12:18:22 -0500365 case ProgramElement::Kind::kFunctionPrototype: {
366 // These are already in the symbol table.
367 break;
368 }
Brian Osman3d87e9f2020-10-08 11:50:22 -0400369 case ProgramElement::Kind::kGlobalVar: {
Ethan Nicholasc51f33e2020-10-13 13:49:44 -0400370 const GlobalVarDeclaration& global = element->as<GlobalVarDeclaration>();
371 const Variable& var = global.declaration()->as<VarDeclaration>().var();
372 SkASSERT(var.isBuiltin());
Ethan Nicholasd2e09602021-06-10 11:21:59 -0400373 intrinsics->insertOrDie(String(var.name()), std::move(element));
Brian Osman3d87e9f2020-10-08 11:50:22 -0400374 break;
375 }
376 case ProgramElement::Kind::kInterfaceBlock: {
Ethan Nicholaseaf47882020-10-15 10:10:08 -0400377 const Variable& var = element->as<InterfaceBlock>().variable();
378 SkASSERT(var.isBuiltin());
Ethan Nicholasd2e09602021-06-10 11:21:59 -0400379 intrinsics->insertOrDie(String(var.name()), std::move(element));
Brian Osman3d87e9f2020-10-08 11:50:22 -0400380 break;
381 }
382 default:
383 printf("Unsupported element: %s\n", element->description().c_str());
384 SkASSERT(false);
385 break;
386 }
387 }
388
John Stiles10d39d92021-05-04 16:13:14 -0400389 return ParsedModule{module.fSymbols, std::move(intrinsics)};
Ethan Nicholas8da1e652019-05-24 11:01:59 -0400390}
391
John Stilesc8764892021-10-04 11:56:30 -0400392std::unique_ptr<Program> Compiler::convertProgram(ProgramKind kind,
393 String text,
394 Program::Settings settings) {
Brian Osman7a20b5c2021-03-15 16:23:33 -0400395 TRACE_EVENT0("skia.shaders", "SkSL::Compiler::convertProgram");
Leon Scrogginsb66214e2021-02-11 17:14:18 -0500396
Ethan Nicholas55a63af2021-05-18 10:12:58 -0400397 SkASSERT(!settings.fExternalFunctions || (kind == ProgramKind::kGeneric));
Ethan Nicholas91164d12019-05-15 15:29:54 -0400398
John Stiles2ee4d7a2021-03-30 10:30:47 -0400399 // Honor our optimization-override flags.
400 switch (sOptimizer) {
401 case OverrideFlag::kDefault:
402 break;
403 case OverrideFlag::kOff:
Ethan Nicholas55a63af2021-05-18 10:12:58 -0400404 settings.fOptimize = false;
John Stiles2ee4d7a2021-03-30 10:30:47 -0400405 break;
406 case OverrideFlag::kOn:
Ethan Nicholas55a63af2021-05-18 10:12:58 -0400407 settings.fOptimize = true;
John Stiles2ee4d7a2021-03-30 10:30:47 -0400408 break;
409 }
410
411 switch (sInliner) {
412 case OverrideFlag::kDefault:
413 break;
414 case OverrideFlag::kOff:
Ethan Nicholas55a63af2021-05-18 10:12:58 -0400415 settings.fInlineThreshold = 0;
John Stiles2ee4d7a2021-03-30 10:30:47 -0400416 break;
417 case OverrideFlag::kOn:
Ethan Nicholas55a63af2021-05-18 10:12:58 -0400418 if (settings.fInlineThreshold == 0) {
419 settings.fInlineThreshold = kDefaultInlineThreshold;
John Stiles2ee4d7a2021-03-30 10:30:47 -0400420 }
421 break;
422 }
John Stiles7247b482021-03-08 10:40:35 -0500423
424 // Disable optimization settings that depend on a parent setting which has been disabled.
Ethan Nicholas55a63af2021-05-18 10:12:58 -0400425 settings.fInlineThreshold *= (int)settings.fOptimize;
426 settings.fRemoveDeadFunctions &= settings.fOptimize;
427 settings.fRemoveDeadVariables &= settings.fOptimize;
John Stiles7247b482021-03-08 10:40:35 -0500428
John Stilesaddccaf2021-08-02 19:03:30 -0400429 // Runtime effects always allow narrowing conversions.
430 if (ProgramConfig::IsRuntimeEffect(kind)) {
431 settings.fAllowNarrowingConversions = true;
432 }
433
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -0400434 this->resetErrors();
John Stiles10d39d92021-05-04 16:13:14 -0400435 fInliner.reset();
Brian Osman88cda172020-10-09 12:05:16 -0400436
Ethan Nicholasdd2fdea2021-07-20 15:23:04 -0400437 settings.fDSLMangling = false;
Ethan Nicholas051aeb72021-09-24 16:39:19 -0400438 return DSLParser(this, settings, kind, std::move(text)).program();
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500439}
440
Brian Osman0006ad02020-11-18 15:38:39 -0500441bool Compiler::optimize(LoadedModule& module) {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -0400442 SkASSERT(!this->errorCount());
Brian Osman0006ad02020-11-18 15:38:39 -0500443
John Stiles270cec22021-02-17 12:59:36 -0500444 // Create a temporary program configuration with default settings.
445 ProgramConfig config;
John Stilesc8764892021-10-04 11:56:30 -0400446 config.fIsBuiltinCode = true;
John Stiles270cec22021-02-17 12:59:36 -0500447 config.fKind = module.fKind;
John Stilesa935c3f2021-02-25 10:35:49 -0500448 AutoProgramConfig autoConfig(fContext, &config);
John Stiles270cec22021-02-17 12:59:36 -0500449
John Stilesd1204642021-02-17 16:30:02 -0500450 // Reset the Inliner.
John Stiles10d39d92021-05-04 16:13:14 -0400451 fInliner.reset();
John Stiles270cec22021-02-17 12:59:36 -0500452
453 std::unique_ptr<ProgramUsage> usage = Analysis::GetUsage(module);
Brian Osman0006ad02020-11-18 15:38:39 -0500454
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -0400455 while (this->errorCount() == 0) {
Brian Osman0006ad02020-11-18 15:38:39 -0500456 // Perform inline-candidate analysis and inline any functions deemed suitable.
John Stiles3ff77f42021-09-06 22:17:58 -0400457 if (!this->runInliner(module.fElements, module.fSymbols, usage.get())) {
Brian Osman0006ad02020-11-18 15:38:39 -0500458 break;
459 }
460 }
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -0400461 return this->errorCount() == 0;
Brian Osman0006ad02020-11-18 15:38:39 -0500462}
463
Ethan Nicholas00543112018-07-31 09:44:36 -0400464bool Compiler::optimize(Program& program) {
John Stiles7247b482021-03-08 10:40:35 -0500465 // The optimizer only needs to run when it is enabled.
466 if (!program.fConfig->fSettings.fOptimize) {
467 return true;
468 }
469
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -0400470 SkASSERT(!this->errorCount());
Brian Osman010ce6a2020-10-19 16:34:10 -0400471 ProgramUsage* usage = program.fUsage.get();
John Stiles7954d6c2020-09-01 10:53:02 -0400472
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -0400473 if (this->errorCount() == 0) {
John Stiles87fc6572021-04-01 14:56:34 +0000474 // Run the inliner only once; it is expensive! Multiple passes can occasionally shake out
475 // more wins, but it's diminishing returns.
John Stilesbcaacec2021-10-06 20:06:55 -0400476 this->runInliner(program.fOwnedElements, program.fSymbols, usage);
Ethan Nicholas34b19c52020-09-14 11:33:47 -0400477
John Stiles80970762021-09-18 12:42:10 -0400478 // Unreachable code can confuse some drivers, so it's worth removing. (skia:12012)
John Stiles042a1972021-10-06 20:37:39 -0400479 Transform::EliminateUnreachableCode(program, usage);
John Stiles80970762021-09-18 12:42:10 -0400480
John Stiles042a1972021-10-06 20:37:39 -0400481 while (Transform::EliminateDeadFunctions(program, usage)) {
John Stilesb6664582021-03-19 09:46:00 -0400482 // Removing dead functions may cause more functions to become unreferenced. Try again.
Ethan Nicholas34b19c52020-09-14 11:33:47 -0400483 }
John Stilesa7827d62021-10-06 16:51:57 -0400484 while (Transform::EliminateDeadLocalVariables(program, usage)) {
John Stilesb6664582021-03-19 09:46:00 -0400485 // Removing dead variables may cause more variables to become unreferenced. Try again.
486 }
John Stiles25be58e2021-05-20 14:38:40 -0400487
John Stiles042a1972021-10-06 20:37:39 -0400488 Transform::EliminateDeadGlobalVariables(program, usage);
Ethan Nicholas00543112018-07-31 09:44:36 -0400489 }
John Stilesbb1505f2021-02-12 09:17:53 -0500490
John Stiles2ecc5952021-09-01 14:41:36 -0400491 return this->errorCount() == 0;
492}
493
John Stiles3ff77f42021-09-06 22:17:58 -0400494bool Compiler::runInliner(const std::vector<std::unique_ptr<ProgramElement>>& elements,
495 std::shared_ptr<SymbolTable> symbols,
496 ProgramUsage* usage) {
497 // The program's SymbolTable was taken out of the IRGenerator when the program was bundled, but
498 // the inliner relies (indirectly) on having a valid SymbolTable in the IRGenerator.
499 // In particular, inlining can turn a non-optimizable expression like `normalize(myVec)` into
500 // `normalize(vec2(7))`, which is now optimizable. The optimizer can use DSL to simplify this
501 // expression--e.g., in the case of normalize, using DSL's Length(). The DSL relies on
502 // irGenerator.convertIdentifier() to look up `length`. convertIdentifier() needs a valid symbol
503 // table to find the declaration of `length`. To allow this chain of events to succeed, we
504 // re-insert the program's symbol table back into the IRGenerator temporarily.
505 SkASSERT(!fIRGenerator->fSymbolTable);
506 fIRGenerator->fSymbolTable = symbols;
507
508 bool result = fInliner.analyze(elements, symbols, usage);
509
510 fIRGenerator->fSymbolTable = nullptr;
511 return result;
512}
513
John Stiles2ecc5952021-09-01 14:41:36 -0400514bool Compiler::finalize(Program& program) {
515 // Do a pass looking for @if/@switch statements that didn't optimize away, or dangling
516 // FunctionReference or TypeReference expressions. Report these as errors.
517 Analysis::VerifyStaticTestsAndExpressions(program);
518
John Stiles9d82e612021-09-03 08:39:54 -0400519 // Verify that the program conforms to ES2 limitations.
John Stiles2ecc5952021-09-01 14:41:36 -0400520 if (fContext->fConfig->strictES2Mode() && this->errorCount() == 0) {
John Stiles9d82e612021-09-03 08:39:54 -0400521 // Enforce Appendix A, Section 5 of the GLSL ES 1.00 spec -- Indexing. This logic assumes
522 // that all loops meet the criteria of Section 4, and if they don't, could crash.
John Stilesbcaacec2021-10-06 20:06:55 -0400523 for (const auto& pe : program.fOwnedElements) {
John Stiles2ecc5952021-09-01 14:41:36 -0400524 Analysis::ValidateIndexingForES2(*pe, this->errorReporter());
525 }
John Stiles9d82e612021-09-03 08:39:54 -0400526 // Verify that the program size is reasonable after unrolling and inlining. This also
527 // issues errors for static recursion and overly-deep function-call chains.
John Stiles61e5e202021-09-02 09:56:31 -0400528 Analysis::CheckProgramUnrolledSize(program);
John Stilesbb1505f2021-02-12 09:17:53 -0500529 }
530
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -0400531 return this->errorCount() == 0;
Ethan Nicholas00543112018-07-31 09:44:36 -0400532}
533
Brian Osmanfb32ddf2019-06-18 10:14:20 -0400534#if defined(SKSL_STANDALONE) || SK_SUPPORT_GPU
535
Ethan Nicholas00543112018-07-31 09:44:36 -0400536bool Compiler::toSPIRV(Program& program, OutputStream& out) {
Brian Osman7a20b5c2021-03-15 16:23:33 -0400537 TRACE_EVENT0("skia.shaders", "SkSL::Compiler::toSPIRV");
Ethan Nicholasb449fff2021-08-04 15:06:37 -0400538 AutoSource as(this, program.fSource->c_str());
Brian Salomond8d85b92021-07-07 09:41:17 -0400539 ProgramSettings settings;
540 settings.fDSLUseMemoryPool = false;
541 dsl::Start(this, program.fConfig->fKind, settings);
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -0400542 dsl::SetErrorReporter(&fErrorReporter);
Ethan Nicholasc8452722021-10-07 10:47:32 -0400543 ThreadContext::IRGenerator().fSymbolTable = program.fSymbols;
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -0400544#ifdef SK_ENABLE_SPIRV_VALIDATION
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400545 StringStream buffer;
Ethan Nicholas3abc6c62021-08-13 11:20:09 -0400546 SPIRVCodeGenerator cg(fContext.get(), &program, &buffer);
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -0400547 bool result = cg.generateCode();
John Stiles270cec22021-02-17 12:59:36 -0500548 if (result && program.fConfig->fSettings.fValidateSPIRV) {
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -0400549 spvtools::SpirvTools tools(SPV_ENV_VULKAN_1_0);
Ethan Nicholas762466e2017-06-29 10:03:38 -0400550 const String& data = buffer.str();
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400551 SkASSERT(0 == data.size() % 4);
Brian Osman8d09d4a2020-11-24 15:51:06 -0500552 String errors;
553 auto dumpmsg = [&errors](spv_message_level_t, const char*, const spv_position_t&,
554 const char* m) {
555 errors.appendf("SPIR-V validation error: %s\n", m);
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -0400556 };
557 tools.SetMessageConsumer(dumpmsg);
Brian Osman8d09d4a2020-11-24 15:51:06 -0500558
559 // Verify that the SPIR-V we produced is valid. At runtime, we will abort() with a message
560 // explaining the error. In standalone mode (skslc), we will send the message, plus the
561 // entire disassembled SPIR-V (for easier context & debugging) as *our* error message.
562 result = tools.Validate((const uint32_t*) data.c_str(), data.size() / 4);
563
564 if (!result) {
565#if defined(SKSL_STANDALONE)
566 // Convert the string-stream to a SPIR-V disassembly.
567 std::string disassembly;
568 if (tools.Disassemble((const uint32_t*)data.data(), data.size() / 4, &disassembly)) {
569 errors.append(disassembly);
570 }
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -0400571 this->errorReporter().error(-1, errors);
Ethan Nicholasc973d262021-09-17 16:10:29 -0400572 this->errorReporter().reportPendingErrors(PositionInfo());
Brian Osman8d09d4a2020-11-24 15:51:06 -0500573#else
574 SkDEBUGFAILF("%s", errors.c_str());
575#endif
576 }
Ethan Nicholas762466e2017-06-29 10:03:38 -0400577 out.write(data.c_str(), data.size());
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -0400578 }
579#else
Ethan Nicholas3abc6c62021-08-13 11:20:09 -0400580 SPIRVCodeGenerator cg(fContext.get(), &program, &out);
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500581 bool result = cg.generateCode();
Ethan Nicholasa6ae1f72017-03-16 09:56:54 -0400582#endif
Brian Salomond8d85b92021-07-07 09:41:17 -0400583 dsl::End();
Ethan Nicholasce33f102016-12-09 17:22:59 -0500584 return result;
585}
586
Ethan Nicholas00543112018-07-31 09:44:36 -0400587bool Compiler::toSPIRV(Program& program, String* out) {
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400588 StringStream buffer;
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500589 bool result = this->toSPIRV(program, buffer);
590 if (result) {
Ethan Nicholas762466e2017-06-29 10:03:38 -0400591 *out = buffer.str();
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500592 }
593 return result;
594}
595
Ethan Nicholas00543112018-07-31 09:44:36 -0400596bool Compiler::toGLSL(Program& program, OutputStream& out) {
Brian Osman7a20b5c2021-03-15 16:23:33 -0400597 TRACE_EVENT0("skia.shaders", "SkSL::Compiler::toGLSL");
Ethan Nicholasb449fff2021-08-04 15:06:37 -0400598 AutoSource as(this, program.fSource->c_str());
Ethan Nicholas3abc6c62021-08-13 11:20:09 -0400599 GLSLCodeGenerator cg(fContext.get(), &program, &out);
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500600 bool result = cg.generateCode();
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500601 return result;
602}
603
Ethan Nicholas00543112018-07-31 09:44:36 -0400604bool Compiler::toGLSL(Program& program, String* out) {
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400605 StringStream buffer;
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500606 bool result = this->toGLSL(program, buffer);
607 if (result) {
Ethan Nicholas762466e2017-06-29 10:03:38 -0400608 *out = buffer.str();
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500609 }
610 return result;
611}
612
Brian Osmanc0243912020-02-19 15:35:26 -0500613bool Compiler::toHLSL(Program& program, String* out) {
614 String spirv;
615 if (!this->toSPIRV(program, &spirv)) {
616 return false;
617 }
618
619 return SPIRVtoHLSL(spirv, out);
620}
621
Ethan Nicholas00543112018-07-31 09:44:36 -0400622bool Compiler::toMetal(Program& program, OutputStream& out) {
Brian Osman7a20b5c2021-03-15 16:23:33 -0400623 TRACE_EVENT0("skia.shaders", "SkSL::Compiler::toMetal");
Ethan Nicholasb449fff2021-08-04 15:06:37 -0400624 AutoSource as(this, program.fSource->c_str());
Ethan Nicholas3abc6c62021-08-13 11:20:09 -0400625 MetalCodeGenerator cg(fContext.get(), &program, &out);
Ethan Nicholascc305772017-10-13 16:17:45 -0400626 bool result = cg.generateCode();
Ethan Nicholascc305772017-10-13 16:17:45 -0400627 return result;
628}
629
Ethan Nicholas00543112018-07-31 09:44:36 -0400630bool Compiler::toMetal(Program& program, String* out) {
Timothy Liangb8eeb802018-07-23 16:46:16 -0400631 StringStream buffer;
632 bool result = this->toMetal(program, buffer);
633 if (result) {
634 *out = buffer.str();
635 }
636 return result;
637}
638
Ethan Nicholas2a479a52020-08-18 16:29:45 -0400639#endif // defined(SKSL_STANDALONE) || SK_SUPPORT_GPU
Brian Osman2e29ab52019-09-20 12:19:11 -0400640
Ethan Nicholas32724122021-09-07 13:49:07 -0400641void Compiler::handleError(skstd::string_view msg, PositionInfo pos) {
Ethan Nicholasa40ddcd2021-08-06 09:17:18 -0400642 fErrorText += "error: " + (pos.line() >= 1 ? to_string(pos.line()) + ": " : "") + msg + "\n";
ethannicholasb3058bd2016-07-01 08:22:01 -0700643}
644
Ethan Nicholas95046142021-01-07 10:57:27 -0500645String Compiler::errorText(bool showCount) {
646 if (showCount) {
647 this->writeErrorCount();
648 }
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400649 String result = fErrorText;
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -0400650 this->resetErrors();
ethannicholasb3058bd2016-07-01 08:22:01 -0700651 return result;
652}
653
654void Compiler::writeErrorCount() {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -0400655 int count = this->errorCount();
656 if (count) {
657 fErrorText += to_string(count) + " error";
658 if (count > 1) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700659 fErrorText += "s";
660 }
661 fErrorText += "\n";
662 }
663}
664
John Stilesa6841be2020-08-06 14:11:56 -0400665} // namespace SkSL