ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1 | /* |
| 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 Klein | 6ad9909 | 2016-10-26 10:35:22 -0400 | [diff] [blame] | 7 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "src/sksl/SkSLCompiler.h" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 9 | |
John Stiles | fbd050b | 2020-08-03 13:21:46 -0400 | [diff] [blame] | 10 | #include <memory> |
John Stiles | b8e010c | 2020-08-11 18:05:39 -0400 | [diff] [blame] | 11 | #include <unordered_set> |
John Stiles | fbd050b | 2020-08-03 13:21:46 -0400 | [diff] [blame] | 12 | |
Ethan Nicholas | 55a63af | 2021-05-18 10:12:58 -0400 | [diff] [blame] | 13 | #include "include/sksl/DSLCore.h" |
John Stiles | 270cec2 | 2021-02-17 12:59:36 -0500 | [diff] [blame] | 14 | #include "src/core/SkScopeExit.h" |
Leon Scroggins | b66214e | 2021-02-11 17:14:18 -0500 | [diff] [blame] | 15 | #include "src/core/SkTraceEvent.h" |
John Stiles | b92641c | 2020-08-31 18:09:01 -0400 | [diff] [blame] | 16 | #include "src/sksl/SkSLAnalysis.h" |
John Stiles | f3a28db | 2021-03-10 23:00:47 -0500 | [diff] [blame] | 17 | #include "src/sksl/SkSLConstantFolder.h" |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 18 | #include "src/sksl/SkSLDSLParser.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 19 | #include "src/sksl/SkSLIRGenerator.h" |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 20 | #include "src/sksl/SkSLOperators.h" |
John Stiles | 270cec2 | 2021-02-17 12:59:36 -0500 | [diff] [blame] | 21 | #include "src/sksl/SkSLProgramSettings.h" |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 22 | #include "src/sksl/SkSLRehydrator.h" |
John Stiles | 3738ef5 | 2021-04-13 10:41:57 -0400 | [diff] [blame] | 23 | #include "src/sksl/codegen/SkSLGLSLCodeGenerator.h" |
John Stiles | 3738ef5 | 2021-04-13 10:41:57 -0400 | [diff] [blame] | 24 | #include "src/sksl/codegen/SkSLMetalCodeGenerator.h" |
| 25 | #include "src/sksl/codegen/SkSLSPIRVCodeGenerator.h" |
| 26 | #include "src/sksl/codegen/SkSLSPIRVtoHLSL.h" |
Ethan Nicholas | 55a63af | 2021-05-18 10:12:58 -0400 | [diff] [blame] | 27 | #include "src/sksl/dsl/priv/DSLWriter.h" |
| 28 | #include "src/sksl/dsl/priv/DSL_priv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 29 | #include "src/sksl/ir/SkSLExpression.h" |
| 30 | #include "src/sksl/ir/SkSLExpressionStatement.h" |
| 31 | #include "src/sksl/ir/SkSLFunctionCall.h" |
| 32 | #include "src/sksl/ir/SkSLIntLiteral.h" |
| 33 | #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" |
John Stiles | e615000 | 2020-10-05 12:03:53 -0400 | [diff] [blame] | 39 | #include "src/utils/SkBitSet.h" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 40 | |
Ethan Nicholas | b33fa3f | 2020-08-06 13:00:19 -0400 | [diff] [blame] | 41 | #include <fstream> |
| 42 | |
Ethan Nicholas | a11035b | 2019-11-26 16:27:47 -0500 | [diff] [blame] | 43 | #if !defined(SKSL_STANDALONE) & SK_SUPPORT_GPU |
| 44 | #include "include/gpu/GrContextOptions.h" |
| 45 | #include "src/gpu/GrShaderCaps.h" |
| 46 | #endif |
| 47 | |
Ethan Nicholas | a6ae1f7 | 2017-03-16 09:56:54 -0400 | [diff] [blame] | 48 | #ifdef SK_ENABLE_SPIRV_VALIDATION |
| 49 | #include "spirv-tools/libspirv.hpp" |
| 50 | #endif |
| 51 | |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 52 | #if defined(SKSL_STANDALONE) |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 53 | |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 54 | // In standalone mode, we load the textual sksl source files. GN generates or copies these files |
| 55 | // to the skslc executable directory. The "data" in this mode is just the filename. |
| 56 | #define MODULE_DATA(name) MakeModulePath("sksl_" #name ".sksl") |
| 57 | |
| 58 | #else |
| 59 | |
| 60 | // At runtime, we load the dehydrated sksl data files. The data is a (pointer, size) pair. |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 61 | #include "src/sksl/generated/sksl_frag.dehydrated.sksl" |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 62 | #include "src/sksl/generated/sksl_gpu.dehydrated.sksl" |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 63 | #include "src/sksl/generated/sksl_public.dehydrated.sksl" |
John Stiles | f7f36ae | 2021-06-08 14:06:22 -0400 | [diff] [blame] | 64 | #include "src/sksl/generated/sksl_rt_blend.dehydrated.sksl" |
Brian Osman | cbb60bd | 2021-04-12 09:49:20 -0400 | [diff] [blame] | 65 | #include "src/sksl/generated/sksl_rt_colorfilter.dehydrated.sksl" |
| 66 | #include "src/sksl/generated/sksl_rt_shader.dehydrated.sksl" |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 67 | #include "src/sksl/generated/sksl_vert.dehydrated.sksl" |
| 68 | |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 69 | #define MODULE_DATA(name) MakeModuleData(SKSL_INCLUDE_sksl_##name,\ |
| 70 | SKSL_INCLUDE_sksl_##name##_LENGTH) |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 71 | |
| 72 | #endif |
Ethan Nicholas | 0d99766 | 2019-04-08 09:46:01 -0400 | [diff] [blame] | 73 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 74 | namespace SkSL { |
| 75 | |
John Stiles | 7247b48 | 2021-03-08 10:40:35 -0500 | [diff] [blame] | 76 | // These flags allow tools like Viewer or Nanobench to override the compiler's ProgramSettings. |
John Stiles | 2ee4d7a | 2021-03-30 10:30:47 -0400 | [diff] [blame] | 77 | Compiler::OverrideFlag Compiler::sOptimizer = OverrideFlag::kDefault; |
| 78 | Compiler::OverrideFlag Compiler::sInliner = OverrideFlag::kDefault; |
John Stiles | 8ef4d6c | 2021-03-05 16:01:45 -0500 | [diff] [blame] | 79 | |
John Stiles | 47c0a74 | 2021-02-09 09:30:35 -0500 | [diff] [blame] | 80 | using RefKind = VariableReference::RefKind; |
| 81 | |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 82 | class AutoSource { |
| 83 | public: |
Ethan Nicholas | b449fff | 2021-08-04 15:06:37 -0400 | [diff] [blame] | 84 | AutoSource(Compiler* compiler, const char* source) |
John Stiles | a289ac2 | 2021-05-06 07:35:35 -0400 | [diff] [blame] | 85 | : fCompiler(compiler) { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 86 | SkASSERT(!fCompiler->errorReporter().source()); |
| 87 | fCompiler->errorReporter().setSource(source); |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 88 | } |
| 89 | |
John Stiles | a289ac2 | 2021-05-06 07:35:35 -0400 | [diff] [blame] | 90 | ~AutoSource() { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 91 | fCompiler->errorReporter().setSource(nullptr); |
John Stiles | a289ac2 | 2021-05-06 07:35:35 -0400 | [diff] [blame] | 92 | } |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 93 | |
| 94 | Compiler* fCompiler; |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 95 | }; |
| 96 | |
John Stiles | a935c3f | 2021-02-25 10:35:49 -0500 | [diff] [blame] | 97 | class AutoProgramConfig { |
| 98 | public: |
| 99 | AutoProgramConfig(std::shared_ptr<Context>& context, ProgramConfig* config) |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 100 | : fContext(context.get()) |
| 101 | , fOldConfig(fContext->fConfig) { |
John Stiles | a935c3f | 2021-02-25 10:35:49 -0500 | [diff] [blame] | 102 | fContext->fConfig = config; |
| 103 | } |
| 104 | |
| 105 | ~AutoProgramConfig() { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 106 | fContext->fConfig = fOldConfig; |
John Stiles | a935c3f | 2021-02-25 10:35:49 -0500 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | Context* fContext; |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 110 | ProgramConfig* fOldConfig; |
John Stiles | a935c3f | 2021-02-25 10:35:49 -0500 | [diff] [blame] | 111 | }; |
| 112 | |
John Stiles | 10d39d9 | 2021-05-04 16:13:14 -0400 | [diff] [blame] | 113 | class AutoModifiersPool { |
| 114 | public: |
| 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 Stiles | d6a5f449 | 2021-02-11 15:46:11 -0500 | [diff] [blame] | 128 | Compiler::Compiler(const ShaderCapsClass* caps) |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 129 | : fErrorReporter(this) |
| 130 | , fContext(std::make_shared<Context>(fErrorReporter, *caps)) |
John Stiles | a47b351 | 2021-05-04 16:15:00 -0400 | [diff] [blame] | 131 | , fInliner(fContext.get()) { |
John Stiles | c1a98b8 | 2021-02-24 13:35:02 -0500 | [diff] [blame] | 132 | SkASSERT(caps); |
John Stiles | b624b72 | 2021-08-13 12:16:13 -0400 | [diff] [blame] | 133 | fRootModule.fSymbols = this->makeRootSymbolTable(); |
| 134 | fPrivateModule.fSymbols = this->makePrivateSymbolTable(fRootModule.fSymbols); |
John Stiles | c1a98b8 | 2021-02-24 13:35:02 -0500 | [diff] [blame] | 135 | fIRGenerator = std::make_unique<IRGenerator>(fContext.get()); |
John Stiles | b624b72 | 2021-08-13 12:16:13 -0400 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | Compiler::~Compiler() {} |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 139 | |
John Stiles | 54e7c05 | 2021-01-11 14:22:36 -0500 | [diff] [blame] | 140 | #define TYPE(t) fContext->fTypes.f ## t .get() |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 141 | |
John Stiles | b624b72 | 2021-08-13 12:16:13 -0400 | [diff] [blame] | 142 | std::shared_ptr<SymbolTable> Compiler::makeRootSymbolTable() { |
Ethan Nicholas | c7774a7 | 2021-08-27 15:34:05 -0400 | [diff] [blame^] | 143 | auto rootSymbolTable = std::make_shared<SymbolTable>(*fContext, /*builtin=*/true); |
John Stiles | b624b72 | 2021-08-13 12:16:13 -0400 | [diff] [blame] | 144 | |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 145 | const SkSL::Symbol* rootTypes[] = { |
| 146 | TYPE(Void), |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 147 | |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 148 | 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 Stiles | 823c504 | 2021-08-17 12:09:00 -0400 | [diff] [blame] | 151 | 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 Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 154 | TYPE( Bool), TYPE( Bool2), TYPE( Bool3), TYPE( Bool4), |
Brian Salomon | 2a51de8 | 2016-11-16 12:06:01 -0500 | [diff] [blame] | 155 | |
John Stiles | 823c504 | 2021-08-17 12:09:00 -0400 | [diff] [blame] | 156 | 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 Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 163 | |
Brian Osman | c63f431 | 2020-12-23 11:44:14 -0500 | [diff] [blame] | 164 | TYPE(SquareMat), TYPE(SquareHMat), |
John Stiles | 823c504 | 2021-08-17 12:09:00 -0400 | [diff] [blame] | 165 | TYPE(Mat), TYPE(HMat), |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 166 | |
John Stiles | 823c504 | 2021-08-17 12:09:00 -0400 | [diff] [blame] | 167 | // 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 Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 175 | |
Brian Osman | 14d0096 | 2021-04-02 17:04:35 -0400 | [diff] [blame] | 176 | TYPE(ColorFilter), |
| 177 | TYPE(Shader), |
John Stiles | bb2ef92 | 2021-07-26 08:32:07 -0400 | [diff] [blame] | 178 | TYPE(Blender), |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 179 | }; |
| 180 | |
John Stiles | b624b72 | 2021-08-13 12:16:13 -0400 | [diff] [blame] | 181 | for (const SkSL::Symbol* type : rootTypes) { |
| 182 | rootSymbolTable->addWithoutOwnership(type); |
| 183 | } |
| 184 | |
| 185 | return rootSymbolTable; |
| 186 | } |
| 187 | |
| 188 | std::shared_ptr<SymbolTable> Compiler::makePrivateSymbolTable(std::shared_ptr<SymbolTable> parent) { |
| 189 | auto privateSymbolTable = std::make_shared<SymbolTable>(parent, /*builtin=*/true); |
| 190 | |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 191 | const SkSL::Symbol* privateTypes[] = { |
| 192 | TYPE(Sampler1D), TYPE(Sampler2D), TYPE(Sampler3D), |
| 193 | TYPE(SamplerExternalOES), |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 194 | TYPE(Sampler2DRect), |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 195 | |
| 196 | TYPE(ISampler2D), |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 197 | TYPE(SubpassInput), TYPE(SubpassInputMS), |
| 198 | |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 199 | TYPE(Sampler), |
| 200 | TYPE(Texture2D), |
| 201 | }; |
| 202 | |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 203 | for (const SkSL::Symbol* type : privateTypes) { |
John Stiles | b624b72 | 2021-08-13 12:16:13 -0400 | [diff] [blame] | 204 | privateSymbolTable->addWithoutOwnership(type); |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 205 | } |
| 206 | |
Brian Osman | 3887a01 | 2020-09-30 13:22:27 -0400 | [diff] [blame] | 207 | // 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). |
John Stiles | b624b72 | 2021-08-13 12:16:13 -0400 | [diff] [blame] | 209 | privateSymbolTable->add(std::make_unique<Variable>(/*offset=*/-1, |
| 210 | fCoreModifiers.add(Modifiers{}), |
| 211 | "sk_Caps", |
| 212 | fContext->fTypes.fSkCaps.get(), |
| 213 | /*builtin=*/false, |
| 214 | Variable::Storage::kGlobal)); |
Ethan Nicholas | 3605ace | 2016-11-21 15:59:48 -0500 | [diff] [blame] | 215 | |
John Stiles | b624b72 | 2021-08-13 12:16:13 -0400 | [diff] [blame] | 216 | return privateSymbolTable; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 217 | } |
| 218 | |
John Stiles | b624b72 | 2021-08-13 12:16:13 -0400 | [diff] [blame] | 219 | #undef TYPE |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 220 | |
Brian Osman | 5626998 | 2020-11-20 12:38:07 -0500 | [diff] [blame] | 221 | const ParsedModule& Compiler::loadGPUModule() { |
| 222 | if (!fGPUModule.fSymbols) { |
John Stiles | dbd4e6f | 2021-02-16 13:29:15 -0500 | [diff] [blame] | 223 | fGPUModule = this->parseModule(ProgramKind::kFragment, MODULE_DATA(gpu), fPrivateModule); |
Brian Osman | 5626998 | 2020-11-20 12:38:07 -0500 | [diff] [blame] | 224 | } |
| 225 | return fGPUModule; |
| 226 | } |
| 227 | |
| 228 | const ParsedModule& Compiler::loadFragmentModule() { |
| 229 | if (!fFragmentModule.fSymbols) { |
John Stiles | dbd4e6f | 2021-02-16 13:29:15 -0500 | [diff] [blame] | 230 | fFragmentModule = this->parseModule(ProgramKind::kFragment, MODULE_DATA(frag), |
Brian Osman | 5626998 | 2020-11-20 12:38:07 -0500 | [diff] [blame] | 231 | this->loadGPUModule()); |
| 232 | } |
| 233 | return fFragmentModule; |
| 234 | } |
| 235 | |
| 236 | const ParsedModule& Compiler::loadVertexModule() { |
| 237 | if (!fVertexModule.fSymbols) { |
John Stiles | dbd4e6f | 2021-02-16 13:29:15 -0500 | [diff] [blame] | 238 | fVertexModule = this->parseModule(ProgramKind::kVertex, MODULE_DATA(vert), |
Brian Osman | 5626998 | 2020-11-20 12:38:07 -0500 | [diff] [blame] | 239 | this->loadGPUModule()); |
| 240 | } |
| 241 | return fVertexModule; |
| 242 | } |
| 243 | |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 244 | const ParsedModule& Compiler::loadPublicModule() { |
| 245 | if (!fPublicModule.fSymbols) { |
John Stiles | dbd4e6f | 2021-02-16 13:29:15 -0500 | [diff] [blame] | 246 | fPublicModule = this->parseModule(ProgramKind::kGeneric, MODULE_DATA(public), fRootModule); |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 247 | } |
| 248 | return fPublicModule; |
| 249 | } |
| 250 | |
Brian Osman | cbb60bd | 2021-04-12 09:49:20 -0400 | [diff] [blame] | 251 | static void add_glsl_type_aliases(SkSL::SymbolTable* symbols, const SkSL::BuiltinTypes& types) { |
| 252 | // Add some aliases to the runtime effect modules so that it's friendlier, and more like GLSL |
| 253 | symbols->addAlias("vec2", types.fFloat2.get()); |
| 254 | symbols->addAlias("vec3", types.fFloat3.get()); |
| 255 | symbols->addAlias("vec4", types.fFloat4.get()); |
| 256 | |
| 257 | symbols->addAlias("ivec2", types.fInt2.get()); |
| 258 | symbols->addAlias("ivec3", types.fInt3.get()); |
| 259 | symbols->addAlias("ivec4", types.fInt4.get()); |
| 260 | |
| 261 | symbols->addAlias("bvec2", types.fBool2.get()); |
| 262 | symbols->addAlias("bvec3", types.fBool3.get()); |
| 263 | symbols->addAlias("bvec4", types.fBool4.get()); |
| 264 | |
| 265 | symbols->addAlias("mat2", types.fFloat2x2.get()); |
| 266 | symbols->addAlias("mat3", types.fFloat3x3.get()); |
| 267 | symbols->addAlias("mat4", types.fFloat4x4.get()); |
| 268 | } |
| 269 | |
Brian Osman | cbb60bd | 2021-04-12 09:49:20 -0400 | [diff] [blame] | 270 | const ParsedModule& Compiler::loadRuntimeColorFilterModule() { |
| 271 | if (!fRuntimeColorFilterModule.fSymbols) { |
| 272 | fRuntimeColorFilterModule = this->parseModule(ProgramKind::kRuntimeColorFilter, |
| 273 | MODULE_DATA(rt_colorfilter), |
| 274 | this->loadPublicModule()); |
| 275 | add_glsl_type_aliases(fRuntimeColorFilterModule.fSymbols.get(), fContext->fTypes); |
| 276 | } |
| 277 | return fRuntimeColorFilterModule; |
| 278 | } |
| 279 | |
| 280 | const ParsedModule& Compiler::loadRuntimeShaderModule() { |
| 281 | if (!fRuntimeShaderModule.fSymbols) { |
| 282 | fRuntimeShaderModule = this->parseModule( |
| 283 | ProgramKind::kRuntimeShader, MODULE_DATA(rt_shader), this->loadPublicModule()); |
| 284 | add_glsl_type_aliases(fRuntimeShaderModule.fSymbols.get(), fContext->fTypes); |
| 285 | } |
| 286 | return fRuntimeShaderModule; |
| 287 | } |
| 288 | |
John Stiles | 2d8b835 | 2021-06-16 11:33:13 -0400 | [diff] [blame] | 289 | const ParsedModule& Compiler::loadRuntimeBlenderModule() { |
| 290 | if (!fRuntimeBlenderModule.fSymbols) { |
| 291 | fRuntimeBlenderModule = this->parseModule( |
| 292 | ProgramKind::kRuntimeBlender, MODULE_DATA(rt_blend), this->loadPublicModule()); |
| 293 | add_glsl_type_aliases(fRuntimeBlenderModule.fSymbols.get(), fContext->fTypes); |
John Stiles | f7f36ae | 2021-06-08 14:06:22 -0400 | [diff] [blame] | 294 | } |
John Stiles | 2d8b835 | 2021-06-16 11:33:13 -0400 | [diff] [blame] | 295 | return fRuntimeBlenderModule; |
John Stiles | f7f36ae | 2021-06-08 14:06:22 -0400 | [diff] [blame] | 296 | } |
| 297 | |
John Stiles | dbd4e6f | 2021-02-16 13:29:15 -0500 | [diff] [blame] | 298 | const ParsedModule& Compiler::moduleForProgramKind(ProgramKind kind) { |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 299 | switch (kind) { |
Brian Osman | cbb60bd | 2021-04-12 09:49:20 -0400 | [diff] [blame] | 300 | case ProgramKind::kVertex: return this->loadVertexModule(); break; |
| 301 | case ProgramKind::kFragment: return this->loadFragmentModule(); break; |
Brian Osman | cbb60bd | 2021-04-12 09:49:20 -0400 | [diff] [blame] | 302 | case ProgramKind::kRuntimeColorFilter: return this->loadRuntimeColorFilterModule(); break; |
| 303 | case ProgramKind::kRuntimeShader: return this->loadRuntimeShaderModule(); break; |
John Stiles | 2d8b835 | 2021-06-16 11:33:13 -0400 | [diff] [blame] | 304 | case ProgramKind::kRuntimeBlender: return this->loadRuntimeBlenderModule(); break; |
Brian Osman | cbb60bd | 2021-04-12 09:49:20 -0400 | [diff] [blame] | 305 | case ProgramKind::kGeneric: return this->loadPublicModule(); break; |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 306 | } |
| 307 | SkUNREACHABLE; |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 308 | } |
| 309 | |
John Stiles | dbd4e6f | 2021-02-16 13:29:15 -0500 | [diff] [blame] | 310 | LoadedModule Compiler::loadModule(ProgramKind kind, |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 311 | ModuleData data, |
John Stiles | a935c3f | 2021-02-25 10:35:49 -0500 | [diff] [blame] | 312 | std::shared_ptr<SymbolTable> base, |
| 313 | bool dehydrate) { |
| 314 | if (dehydrate) { |
| 315 | // NOTE: This is a workaround. When dehydrating includes, skslc doesn't know which module |
| 316 | // it's preparing, nor what the correct base module is. We can't use 'Root', because many |
| 317 | // GPU intrinsics reference private types, like samplers or textures. Today, 'Private' does |
| 318 | // contain the union of all known types, so this is safe. If we ever have types that only |
| 319 | // exist in 'Public' (for example), this logic needs to be smarter (by choosing the correct |
| 320 | // base for the module we're compiling). |
John Stiles | b624b72 | 2021-08-13 12:16:13 -0400 | [diff] [blame] | 321 | base = fPrivateModule.fSymbols; |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 322 | } |
John Stiles | a935c3f | 2021-02-25 10:35:49 -0500 | [diff] [blame] | 323 | SkASSERT(base); |
| 324 | |
John Stiles | a47b351 | 2021-05-04 16:15:00 -0400 | [diff] [blame] | 325 | // Put the core-module modifier pool into the context. |
| 326 | AutoModifiersPool autoPool(fContext, &fCoreModifiers); |
John Stiles | 10d39d9 | 2021-05-04 16:13:14 -0400 | [diff] [blame] | 327 | |
John Stiles | a935c3f | 2021-02-25 10:35:49 -0500 | [diff] [blame] | 328 | // Built-in modules always use default program settings. |
Ethan Nicholas | 55a63af | 2021-05-18 10:12:58 -0400 | [diff] [blame] | 329 | Program::Settings settings; |
| 330 | settings.fReplaceSettings = !dehydrate; |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 331 | |
| 332 | #if defined(SKSL_STANDALONE) |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 333 | SkASSERT(this->errorCount() == 0); |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 334 | SkASSERT(data.fPath); |
| 335 | std::ifstream in(data.fPath); |
John Stiles | d51c979 | 2021-03-18 11:40:14 -0400 | [diff] [blame] | 336 | String text{std::istreambuf_iterator<char>(in), std::istreambuf_iterator<char>()}; |
Ethan Nicholas | b33fa3f | 2020-08-06 13:00:19 -0400 | [diff] [blame] | 337 | if (in.rdstate()) { |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 338 | printf("error reading %s\n", data.fPath); |
Ethan Nicholas | b33fa3f | 2020-08-06 13:00:19 -0400 | [diff] [blame] | 339 | abort(); |
| 340 | } |
John Stiles | b624b72 | 2021-08-13 12:16:13 -0400 | [diff] [blame] | 341 | const String* source = fRootModule.fSymbols->takeOwnershipOfString(std::move(text)); |
John Stiles | d120464 | 2021-02-17 16:30:02 -0500 | [diff] [blame] | 342 | |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 343 | ParsedModule baseModule = {base, /*fIntrinsics=*/nullptr}; |
Ethan Nicholas | 55a63af | 2021-05-18 10:12:58 -0400 | [diff] [blame] | 344 | std::vector<std::unique_ptr<ProgramElement>> elements; |
| 345 | std::vector<const ProgramElement*> sharedElements; |
| 346 | dsl::StartModule(this, kind, settings, baseModule); |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 347 | dsl::SetErrorReporter(&this->errorReporter()); |
Ethan Nicholas | b449fff | 2021-08-04 15:06:37 -0400 | [diff] [blame] | 348 | AutoSource as(this, source->c_str()); |
John Stiles | d120464 | 2021-02-17 16:30:02 -0500 | [diff] [blame] | 349 | IRGenerator::IRBundle ir = fIRGenerator->convertProgram(baseModule, /*isBuiltinCode=*/true, |
Ethan Nicholas | 6823b50 | 2021-06-15 11:42:07 -0400 | [diff] [blame] | 350 | *source); |
Brian Osman | 133724c | 2020-10-28 14:14:39 -0400 | [diff] [blame] | 351 | SkASSERT(ir.fSharedElements.empty()); |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 352 | LoadedModule module = { kind, std::move(ir.fSymbolTable), std::move(ir.fElements) }; |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 353 | if (this->errorCount()) { |
Ethan Nicholas | 8da1e65 | 2019-05-24 11:01:59 -0400 | [diff] [blame] | 354 | printf("Unexpected errors: %s\n", this->fErrorText.c_str()); |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 355 | SkDEBUGFAILF("%s %s\n", data.fPath, this->fErrorText.c_str()); |
Ethan Nicholas | 8da1e65 | 2019-05-24 11:01:59 -0400 | [diff] [blame] | 356 | } |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 357 | dsl::End(); |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 358 | #else |
Ethan Nicholas | 55a63af | 2021-05-18 10:12:58 -0400 | [diff] [blame] | 359 | ProgramConfig config; |
| 360 | config.fKind = kind; |
| 361 | config.fSettings = settings; |
| 362 | AutoProgramConfig autoConfig(fContext, &config); |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 363 | SkASSERT(data.fData && (data.fSize != 0)); |
John Stiles | 10d39d9 | 2021-05-04 16:13:14 -0400 | [diff] [blame] | 364 | Rehydrator rehydrator(fContext.get(), base, data.fData, data.fSize); |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 365 | LoadedModule module = { kind, rehydrator.symbolTable(), rehydrator.elements() }; |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 366 | #endif |
| 367 | |
| 368 | return module; |
| 369 | } |
| 370 | |
John Stiles | dbd4e6f | 2021-02-16 13:29:15 -0500 | [diff] [blame] | 371 | ParsedModule Compiler::parseModule(ProgramKind kind, ModuleData data, const ParsedModule& base) { |
John Stiles | a935c3f | 2021-02-25 10:35:49 -0500 | [diff] [blame] | 372 | LoadedModule module = this->loadModule(kind, data, base.fSymbols, /*dehydrate=*/false); |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 373 | this->optimize(module); |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 374 | |
| 375 | // For modules that just declare (but don't define) intrinsic functions, there will be no new |
| 376 | // program elements. In that case, we can share our parent's intrinsic map: |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 377 | if (module.fElements.empty()) { |
John Stiles | 10d39d9 | 2021-05-04 16:13:14 -0400 | [diff] [blame] | 378 | return ParsedModule{module.fSymbols, base.fIntrinsics}; |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | auto intrinsics = std::make_shared<IRIntrinsicMap>(base.fIntrinsics.get()); |
| 382 | |
| 383 | // Now, transfer all of the program elements to an intrinsic map. This maps certain types of |
| 384 | // global objects to the declaring ProgramElement. |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 385 | for (std::unique_ptr<ProgramElement>& element : module.fElements) { |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 386 | switch (element->kind()) { |
| 387 | case ProgramElement::Kind::kFunction: { |
| 388 | const FunctionDefinition& f = element->as<FunctionDefinition>(); |
Ethan Nicholas | 0a5d096 | 2020-10-14 13:33:18 -0400 | [diff] [blame] | 389 | SkASSERT(f.declaration().isBuiltin()); |
| 390 | intrinsics->insertOrDie(f.declaration().description(), std::move(element)); |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 391 | break; |
| 392 | } |
John Stiles | 569249b | 2020-11-03 12:18:22 -0500 | [diff] [blame] | 393 | case ProgramElement::Kind::kFunctionPrototype: { |
| 394 | // These are already in the symbol table. |
| 395 | break; |
| 396 | } |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 397 | case ProgramElement::Kind::kGlobalVar: { |
Ethan Nicholas | c51f33e | 2020-10-13 13:49:44 -0400 | [diff] [blame] | 398 | const GlobalVarDeclaration& global = element->as<GlobalVarDeclaration>(); |
| 399 | const Variable& var = global.declaration()->as<VarDeclaration>().var(); |
| 400 | SkASSERT(var.isBuiltin()); |
Ethan Nicholas | d2e0960 | 2021-06-10 11:21:59 -0400 | [diff] [blame] | 401 | intrinsics->insertOrDie(String(var.name()), std::move(element)); |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 402 | break; |
| 403 | } |
| 404 | case ProgramElement::Kind::kInterfaceBlock: { |
Ethan Nicholas | eaf4788 | 2020-10-15 10:10:08 -0400 | [diff] [blame] | 405 | const Variable& var = element->as<InterfaceBlock>().variable(); |
| 406 | SkASSERT(var.isBuiltin()); |
Ethan Nicholas | d2e0960 | 2021-06-10 11:21:59 -0400 | [diff] [blame] | 407 | intrinsics->insertOrDie(String(var.name()), std::move(element)); |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 408 | break; |
| 409 | } |
| 410 | default: |
| 411 | printf("Unsupported element: %s\n", element->description().c_str()); |
| 412 | SkASSERT(false); |
| 413 | break; |
| 414 | } |
| 415 | } |
| 416 | |
John Stiles | 10d39d9 | 2021-05-04 16:13:14 -0400 | [diff] [blame] | 417 | return ParsedModule{module.fSymbols, std::move(intrinsics)}; |
Ethan Nicholas | 8da1e65 | 2019-05-24 11:01:59 -0400 | [diff] [blame] | 418 | } |
| 419 | |
Brian Osman | 32d5355 | 2020-09-23 13:55:20 -0400 | [diff] [blame] | 420 | std::unique_ptr<Program> Compiler::convertProgram( |
John Stiles | dbd4e6f | 2021-02-16 13:29:15 -0500 | [diff] [blame] | 421 | ProgramKind kind, |
Brian Osman | 32d5355 | 2020-09-23 13:55:20 -0400 | [diff] [blame] | 422 | String text, |
Ethan Nicholas | 55a63af | 2021-05-18 10:12:58 -0400 | [diff] [blame] | 423 | Program::Settings settings) { |
Brian Osman | 7a20b5c | 2021-03-15 16:23:33 -0400 | [diff] [blame] | 424 | TRACE_EVENT0("skia.shaders", "SkSL::Compiler::convertProgram"); |
Leon Scroggins | b66214e | 2021-02-11 17:14:18 -0500 | [diff] [blame] | 425 | |
Ethan Nicholas | 55a63af | 2021-05-18 10:12:58 -0400 | [diff] [blame] | 426 | SkASSERT(!settings.fExternalFunctions || (kind == ProgramKind::kGeneric)); |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 427 | |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 428 | #if !SKSL_DSL_PARSER |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 429 | // Loading and optimizing our base module might reset the inliner, so do that first, |
| 430 | // *then* configure the inliner with the settings for this program. |
| 431 | const ParsedModule& baseModule = this->moduleForProgramKind(kind); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 432 | #endif |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 433 | |
John Stiles | 2ee4d7a | 2021-03-30 10:30:47 -0400 | [diff] [blame] | 434 | // Honor our optimization-override flags. |
| 435 | switch (sOptimizer) { |
| 436 | case OverrideFlag::kDefault: |
| 437 | break; |
| 438 | case OverrideFlag::kOff: |
Ethan Nicholas | 55a63af | 2021-05-18 10:12:58 -0400 | [diff] [blame] | 439 | settings.fOptimize = false; |
John Stiles | 2ee4d7a | 2021-03-30 10:30:47 -0400 | [diff] [blame] | 440 | break; |
| 441 | case OverrideFlag::kOn: |
Ethan Nicholas | 55a63af | 2021-05-18 10:12:58 -0400 | [diff] [blame] | 442 | settings.fOptimize = true; |
John Stiles | 2ee4d7a | 2021-03-30 10:30:47 -0400 | [diff] [blame] | 443 | break; |
| 444 | } |
| 445 | |
| 446 | switch (sInliner) { |
| 447 | case OverrideFlag::kDefault: |
| 448 | break; |
| 449 | case OverrideFlag::kOff: |
Ethan Nicholas | 55a63af | 2021-05-18 10:12:58 -0400 | [diff] [blame] | 450 | settings.fInlineThreshold = 0; |
John Stiles | 2ee4d7a | 2021-03-30 10:30:47 -0400 | [diff] [blame] | 451 | break; |
| 452 | case OverrideFlag::kOn: |
Ethan Nicholas | 55a63af | 2021-05-18 10:12:58 -0400 | [diff] [blame] | 453 | if (settings.fInlineThreshold == 0) { |
| 454 | settings.fInlineThreshold = kDefaultInlineThreshold; |
John Stiles | 2ee4d7a | 2021-03-30 10:30:47 -0400 | [diff] [blame] | 455 | } |
| 456 | break; |
| 457 | } |
John Stiles | 7247b48 | 2021-03-08 10:40:35 -0500 | [diff] [blame] | 458 | |
| 459 | // Disable optimization settings that depend on a parent setting which has been disabled. |
Ethan Nicholas | 55a63af | 2021-05-18 10:12:58 -0400 | [diff] [blame] | 460 | settings.fInlineThreshold *= (int)settings.fOptimize; |
| 461 | settings.fRemoveDeadFunctions &= settings.fOptimize; |
| 462 | settings.fRemoveDeadVariables &= settings.fOptimize; |
John Stiles | 7247b48 | 2021-03-08 10:40:35 -0500 | [diff] [blame] | 463 | |
John Stiles | addccaf | 2021-08-02 19:03:30 -0400 | [diff] [blame] | 464 | // Runtime effects always allow narrowing conversions. |
| 465 | if (ProgramConfig::IsRuntimeEffect(kind)) { |
| 466 | settings.fAllowNarrowingConversions = true; |
| 467 | } |
| 468 | |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 469 | this->resetErrors(); |
John Stiles | 10d39d9 | 2021-05-04 16:13:14 -0400 | [diff] [blame] | 470 | fInliner.reset(); |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 471 | |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 472 | #if SKSL_DSL_PARSER |
| 473 | settings.fDSLMangling = false; |
| 474 | return DSLParser(this, settings, kind, text).program(); |
| 475 | #else |
John Stiles | 10d39d9 | 2021-05-04 16:13:14 -0400 | [diff] [blame] | 476 | auto textPtr = std::make_unique<String>(std::move(text)); |
Ethan Nicholas | b449fff | 2021-08-04 15:06:37 -0400 | [diff] [blame] | 477 | AutoSource as(this, textPtr->c_str()); |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 478 | |
Ethan Nicholas | 55a63af | 2021-05-18 10:12:58 -0400 | [diff] [blame] | 479 | dsl::Start(this, kind, settings); |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 480 | dsl::SetErrorReporter(&fErrorReporter); |
John Stiles | d120464 | 2021-02-17 16:30:02 -0500 | [diff] [blame] | 481 | IRGenerator::IRBundle ir = fIRGenerator->convertProgram(baseModule, /*isBuiltinCode=*/false, |
Ethan Nicholas | 6823b50 | 2021-06-15 11:42:07 -0400 | [diff] [blame] | 482 | *textPtr); |
Ethan Nicholas | 4f3e6a2 | 2021-06-15 09:17:05 -0400 | [diff] [blame] | 483 | // Ideally, we would just use dsl::ReleaseProgram and not have to do any manual mucking about |
| 484 | // with the memory pool, but we've got some impedance mismatches to solve first |
Ethan Nicholas | 55a63af | 2021-05-18 10:12:58 -0400 | [diff] [blame] | 485 | Pool* memoryPool = dsl::DSLWriter::MemoryPool().get(); |
John Stiles | 270cec2 | 2021-02-17 12:59:36 -0500 | [diff] [blame] | 486 | auto program = std::make_unique<Program>(std::move(textPtr), |
Ethan Nicholas | 55a63af | 2021-05-18 10:12:58 -0400 | [diff] [blame] | 487 | std::move(dsl::DSLWriter::GetProgramConfig()), |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 488 | fContext, |
| 489 | std::move(ir.fElements), |
Brian Osman | 133724c | 2020-10-28 14:14:39 -0400 | [diff] [blame] | 490 | std::move(ir.fSharedElements), |
Ethan Nicholas | 55a63af | 2021-05-18 10:12:58 -0400 | [diff] [blame] | 491 | std::move(dsl::DSLWriter::GetModifiersPool()), |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 492 | std::move(ir.fSymbolTable), |
Ethan Nicholas | 55a63af | 2021-05-18 10:12:58 -0400 | [diff] [blame] | 493 | std::move(dsl::DSLWriter::MemoryPool()), |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 494 | ir.fInputs); |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 495 | this->errorReporter().reportPendingErrors(PositionInfo()); |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 496 | bool success = false; |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 497 | if (this->errorCount()) { |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 498 | // Do not return programs that failed to compile. |
John Stiles | 7247b48 | 2021-03-08 10:40:35 -0500 | [diff] [blame] | 499 | } else if (!this->optimize(*program)) { |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 500 | // Do not return programs that failed to optimize. |
| 501 | } else { |
| 502 | // We have a successful program! |
| 503 | success = true; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 504 | } |
Ethan Nicholas | 55a63af | 2021-05-18 10:12:58 -0400 | [diff] [blame] | 505 | dsl::End(); |
| 506 | if (memoryPool) { |
| 507 | memoryPool->detachFromThread(); |
Brian Osman | 28f702c | 2021-02-02 11:52:07 -0500 | [diff] [blame] | 508 | } |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 509 | return success ? std::move(program) : nullptr; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 510 | #endif // SKSL_DSL_PARSER |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 511 | } |
| 512 | |
John Stiles | bb1505f | 2021-02-12 09:17:53 -0500 | [diff] [blame] | 513 | void Compiler::verifyStaticTests(const Program& program) { |
| 514 | class StaticTestVerifier : public ProgramVisitor { |
| 515 | public: |
| 516 | StaticTestVerifier(ErrorReporter* r) : fReporter(r) {} |
| 517 | |
| 518 | using ProgramVisitor::visitProgramElement; |
| 519 | |
| 520 | bool visitStatement(const Statement& stmt) override { |
| 521 | switch (stmt.kind()) { |
| 522 | case Statement::Kind::kIf: |
| 523 | if (stmt.as<IfStatement>().isStatic()) { |
| 524 | fReporter->error(stmt.fOffset, "static if has non-static test"); |
| 525 | } |
| 526 | break; |
| 527 | |
| 528 | case Statement::Kind::kSwitch: |
| 529 | if (stmt.as<SwitchStatement>().isStatic()) { |
| 530 | fReporter->error(stmt.fOffset, "static switch has non-static test"); |
| 531 | } |
| 532 | break; |
| 533 | |
| 534 | default: |
| 535 | break; |
| 536 | } |
| 537 | return INHERITED::visitStatement(stmt); |
| 538 | } |
| 539 | |
John Stiles | 59e3456 | 2021-02-12 16:56:39 -0500 | [diff] [blame] | 540 | bool visitExpression(const Expression&) override { |
| 541 | // We aren't looking for anything inside an Expression, so skip them entirely. |
| 542 | return false; |
| 543 | } |
| 544 | |
John Stiles | bb1505f | 2021-02-12 09:17:53 -0500 | [diff] [blame] | 545 | private: |
| 546 | using INHERITED = ProgramVisitor; |
| 547 | ErrorReporter* fReporter; |
| 548 | }; |
| 549 | |
| 550 | // If invalid static tests are permitted, we don't need to check anything. |
John Stiles | d120464 | 2021-02-17 16:30:02 -0500 | [diff] [blame] | 551 | if (fContext->fConfig->fSettings.fPermitInvalidStaticTests) { |
John Stiles | bb1505f | 2021-02-12 09:17:53 -0500 | [diff] [blame] | 552 | return; |
| 553 | } |
| 554 | |
| 555 | // Check all of the program's owned elements. (Built-in elements are assumed to be valid.) |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 556 | StaticTestVerifier visitor{&this->errorReporter()}; |
John Stiles | bb1505f | 2021-02-12 09:17:53 -0500 | [diff] [blame] | 557 | for (const std::unique_ptr<ProgramElement>& element : program.ownedElements()) { |
| 558 | if (element->is<FunctionDefinition>()) { |
| 559 | visitor.visitProgramElement(*element); |
| 560 | } |
| 561 | } |
| 562 | } |
| 563 | |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 564 | bool Compiler::optimize(LoadedModule& module) { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 565 | SkASSERT(!this->errorCount()); |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 566 | |
John Stiles | 270cec2 | 2021-02-17 12:59:36 -0500 | [diff] [blame] | 567 | // Create a temporary program configuration with default settings. |
| 568 | ProgramConfig config; |
| 569 | config.fKind = module.fKind; |
John Stiles | a935c3f | 2021-02-25 10:35:49 -0500 | [diff] [blame] | 570 | AutoProgramConfig autoConfig(fContext, &config); |
John Stiles | 270cec2 | 2021-02-17 12:59:36 -0500 | [diff] [blame] | 571 | |
John Stiles | d120464 | 2021-02-17 16:30:02 -0500 | [diff] [blame] | 572 | // Reset the Inliner. |
John Stiles | 10d39d9 | 2021-05-04 16:13:14 -0400 | [diff] [blame] | 573 | fInliner.reset(); |
John Stiles | 270cec2 | 2021-02-17 12:59:36 -0500 | [diff] [blame] | 574 | |
| 575 | std::unique_ptr<ProgramUsage> usage = Analysis::GetUsage(module); |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 576 | |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 577 | while (this->errorCount() == 0) { |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 578 | // Perform inline-candidate analysis and inline any functions deemed suitable. |
John Stiles | f3a28db | 2021-03-10 23:00:47 -0500 | [diff] [blame] | 579 | if (!fInliner.analyze(module.fElements, module.fSymbols, usage.get())) { |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 580 | break; |
| 581 | } |
| 582 | } |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 583 | return this->errorCount() == 0; |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 584 | } |
| 585 | |
John Stiles | 0bfeae6 | 2021-03-11 09:09:42 -0500 | [diff] [blame] | 586 | bool Compiler::removeDeadFunctions(Program& program, ProgramUsage* usage) { |
| 587 | bool madeChanges = false; |
| 588 | |
| 589 | if (program.fConfig->fSettings.fRemoveDeadFunctions) { |
| 590 | auto isDeadFunction = [&](const ProgramElement* element) { |
| 591 | if (!element->is<FunctionDefinition>()) { |
| 592 | return false; |
| 593 | } |
| 594 | const FunctionDefinition& fn = element->as<FunctionDefinition>(); |
John Stiles | e8da4d2 | 2021-03-24 09:19:45 -0400 | [diff] [blame] | 595 | if (fn.declaration().isMain() || usage->get(fn.declaration()) > 0) { |
John Stiles | 0bfeae6 | 2021-03-11 09:09:42 -0500 | [diff] [blame] | 596 | return false; |
| 597 | } |
| 598 | usage->remove(*element); |
| 599 | madeChanges = true; |
| 600 | return true; |
| 601 | }; |
| 602 | |
| 603 | program.fElements.erase(std::remove_if(program.fElements.begin(), |
| 604 | program.fElements.end(), |
| 605 | [&](const std::unique_ptr<ProgramElement>& element) { |
| 606 | return isDeadFunction(element.get()); |
| 607 | }), |
| 608 | program.fElements.end()); |
| 609 | program.fSharedElements.erase(std::remove_if(program.fSharedElements.begin(), |
| 610 | program.fSharedElements.end(), |
| 611 | isDeadFunction), |
| 612 | program.fSharedElements.end()); |
| 613 | } |
| 614 | return madeChanges; |
| 615 | } |
| 616 | |
| 617 | bool Compiler::removeDeadGlobalVariables(Program& program, ProgramUsage* usage) { |
| 618 | bool madeChanges = false; |
| 619 | |
| 620 | if (program.fConfig->fSettings.fRemoveDeadVariables) { |
| 621 | auto isDeadVariable = [&](const ProgramElement* element) { |
| 622 | if (!element->is<GlobalVarDeclaration>()) { |
| 623 | return false; |
| 624 | } |
| 625 | const GlobalVarDeclaration& global = element->as<GlobalVarDeclaration>(); |
| 626 | const VarDeclaration& varDecl = global.declaration()->as<VarDeclaration>(); |
| 627 | if (!usage->isDead(varDecl.var())) { |
| 628 | return false; |
| 629 | } |
| 630 | madeChanges = true; |
| 631 | return true; |
| 632 | }; |
| 633 | |
| 634 | program.fElements.erase(std::remove_if(program.fElements.begin(), |
| 635 | program.fElements.end(), |
| 636 | [&](const std::unique_ptr<ProgramElement>& element) { |
| 637 | return isDeadVariable(element.get()); |
| 638 | }), |
| 639 | program.fElements.end()); |
| 640 | program.fSharedElements.erase(std::remove_if(program.fSharedElements.begin(), |
| 641 | program.fSharedElements.end(), |
| 642 | isDeadVariable), |
| 643 | program.fSharedElements.end()); |
| 644 | } |
| 645 | return madeChanges; |
| 646 | } |
| 647 | |
John Stiles | 2654187 | 2021-03-16 12:19:54 -0400 | [diff] [blame] | 648 | bool Compiler::removeDeadLocalVariables(Program& program, ProgramUsage* usage) { |
| 649 | class DeadLocalVariableEliminator : public ProgramWriter { |
| 650 | public: |
| 651 | DeadLocalVariableEliminator(const Context& context, ProgramUsage* usage) |
| 652 | : fContext(context) |
| 653 | , fUsage(usage) {} |
| 654 | |
| 655 | using ProgramWriter::visitProgramElement; |
| 656 | |
| 657 | bool visitExpressionPtr(std::unique_ptr<Expression>& expr) override { |
| 658 | // We don't need to look inside expressions at all. |
| 659 | return false; |
| 660 | } |
| 661 | |
| 662 | bool visitStatementPtr(std::unique_ptr<Statement>& stmt) override { |
| 663 | if (stmt->is<VarDeclaration>()) { |
| 664 | VarDeclaration& varDecl = stmt->as<VarDeclaration>(); |
| 665 | const Variable* var = &varDecl.var(); |
| 666 | ProgramUsage::VariableCounts* counts = fUsage->fVariableCounts.find(var); |
| 667 | SkASSERT(counts); |
| 668 | SkASSERT(counts->fDeclared); |
| 669 | if (CanEliminate(var, *counts)) { |
| 670 | if (var->initialValue()) { |
| 671 | // The variable has an initial-value expression, which might have side |
| 672 | // effects. ExpressionStatement::Make will preserve side effects, but |
| 673 | // replaces pure expressions with Nop. |
| 674 | fUsage->remove(stmt.get()); |
| 675 | stmt = ExpressionStatement::Make(fContext, std::move(varDecl.value())); |
| 676 | fUsage->add(stmt.get()); |
| 677 | } else { |
| 678 | // The variable has no initial-value and can be cleanly eliminated. |
| 679 | fUsage->remove(stmt.get()); |
| 680 | stmt = std::make_unique<Nop>(); |
| 681 | } |
| 682 | fMadeChanges = true; |
| 683 | } |
| 684 | return false; |
| 685 | } |
| 686 | return INHERITED::visitStatementPtr(stmt); |
| 687 | } |
| 688 | |
| 689 | static bool CanEliminate(const Variable* var, const ProgramUsage::VariableCounts& counts) { |
| 690 | if (!counts.fDeclared || counts.fRead || var->storage() != VariableStorage::kLocal) { |
| 691 | return false; |
| 692 | } |
| 693 | if (var->initialValue()) { |
| 694 | SkASSERT(counts.fWrite >= 1); |
| 695 | return counts.fWrite == 1; |
| 696 | } else { |
| 697 | return counts.fWrite == 0; |
| 698 | } |
| 699 | } |
| 700 | |
| 701 | bool fMadeChanges = false; |
| 702 | const Context& fContext; |
| 703 | ProgramUsage* fUsage; |
| 704 | |
| 705 | using INHERITED = ProgramWriter; |
| 706 | }; |
| 707 | |
| 708 | DeadLocalVariableEliminator visitor{*fContext, usage}; |
| 709 | |
| 710 | if (program.fConfig->fSettings.fRemoveDeadVariables) { |
| 711 | for (auto& [var, counts] : usage->fVariableCounts) { |
| 712 | if (DeadLocalVariableEliminator::CanEliminate(var, counts)) { |
| 713 | // This program contains at least one dead local variable. |
| 714 | // Scan the program for any dead local variables and eliminate them all. |
| 715 | for (std::unique_ptr<ProgramElement>& pe : program.ownedElements()) { |
| 716 | if (pe->is<FunctionDefinition>()) { |
| 717 | visitor.visitProgramElement(*pe); |
| 718 | } |
| 719 | } |
| 720 | break; |
| 721 | } |
| 722 | } |
| 723 | } |
| 724 | |
| 725 | return visitor.fMadeChanges; |
| 726 | } |
| 727 | |
John Stiles | 25be58e | 2021-05-20 14:38:40 -0400 | [diff] [blame] | 728 | void Compiler::removeUnreachableCode(Program& program, ProgramUsage* usage) { |
| 729 | class UnreachableCodeEliminator : public ProgramWriter { |
| 730 | public: |
| 731 | UnreachableCodeEliminator(const Context& context, ProgramUsage* usage) |
| 732 | : fContext(context) |
| 733 | , fUsage(usage) { |
| 734 | fFoundFunctionExit.push(false); |
| 735 | fFoundLoopExit.push(false); |
| 736 | } |
| 737 | |
| 738 | using ProgramWriter::visitProgramElement; |
| 739 | |
| 740 | bool visitExpressionPtr(std::unique_ptr<Expression>& expr) override { |
| 741 | // We don't need to look inside expressions at all. |
| 742 | return false; |
| 743 | } |
| 744 | |
| 745 | bool visitStatementPtr(std::unique_ptr<Statement>& stmt) override { |
| 746 | if (fFoundFunctionExit.top() || fFoundLoopExit.top()) { |
| 747 | // If we already found an exit in this section, anything beyond it is dead code. |
| 748 | if (!stmt->is<Nop>()) { |
| 749 | // Eliminate the dead statement by substituting a Nop. |
| 750 | fUsage->remove(stmt.get()); |
| 751 | stmt = std::make_unique<Nop>(); |
| 752 | } |
| 753 | return false; |
| 754 | } |
| 755 | |
| 756 | switch (stmt->kind()) { |
| 757 | case Statement::Kind::kReturn: |
| 758 | case Statement::Kind::kDiscard: |
| 759 | // We found a function exit on this path. |
| 760 | fFoundFunctionExit.top() = true; |
| 761 | break; |
| 762 | |
| 763 | case Statement::Kind::kBreak: |
| 764 | case Statement::Kind::kContinue: |
| 765 | // We found a loop exit on this path. Note that we skip over switch statements |
| 766 | // completely when eliminating code, so any `break` statement would be breaking |
| 767 | // out of a loop, not out of a switch. |
| 768 | fFoundLoopExit.top() = true; |
| 769 | break; |
| 770 | |
| 771 | case Statement::Kind::kExpression: |
| 772 | case Statement::Kind::kInlineMarker: |
| 773 | case Statement::Kind::kNop: |
| 774 | case Statement::Kind::kVarDeclaration: |
| 775 | // These statements don't affect control flow. |
| 776 | break; |
| 777 | |
| 778 | case Statement::Kind::kBlock: |
| 779 | // Blocks are on the straight-line path and don't affect control flow. |
| 780 | return INHERITED::visitStatementPtr(stmt); |
| 781 | |
| 782 | case Statement::Kind::kDo: { |
| 783 | // Function-exits are allowed to propagate outside of a do-loop, because it |
| 784 | // always executes its body at least once. |
| 785 | fFoundLoopExit.push(false); |
| 786 | bool result = INHERITED::visitStatementPtr(stmt); |
| 787 | fFoundLoopExit.pop(); |
| 788 | return result; |
| 789 | } |
| 790 | case Statement::Kind::kFor: { |
| 791 | // Function-exits are not allowed to propagate out, because a for-loop or while- |
| 792 | // loop could potentially run zero times. |
| 793 | fFoundFunctionExit.push(false); |
| 794 | fFoundLoopExit.push(false); |
| 795 | bool result = INHERITED::visitStatementPtr(stmt); |
| 796 | fFoundLoopExit.pop(); |
| 797 | fFoundFunctionExit.pop(); |
| 798 | return result; |
| 799 | } |
| 800 | case Statement::Kind::kIf: { |
| 801 | // This statement is conditional and encloses two inner sections of code. |
| 802 | // If both sides contain a function-exit or loop-exit, that exit is allowed to |
| 803 | // propagate out. |
| 804 | IfStatement& ifStmt = stmt->as<IfStatement>(); |
| 805 | |
| 806 | fFoundFunctionExit.push(false); |
| 807 | fFoundLoopExit.push(false); |
| 808 | bool result = (ifStmt.ifTrue() && this->visitStatementPtr(ifStmt.ifTrue())); |
| 809 | bool foundFunctionExitOnTrue = fFoundFunctionExit.top(); |
| 810 | bool foundLoopExitOnTrue = fFoundLoopExit.top(); |
| 811 | fFoundFunctionExit.pop(); |
| 812 | fFoundLoopExit.pop(); |
| 813 | |
| 814 | fFoundFunctionExit.push(false); |
| 815 | fFoundLoopExit.push(false); |
| 816 | result |= (ifStmt.ifFalse() && this->visitStatementPtr(ifStmt.ifFalse())); |
| 817 | bool foundFunctionExitOnFalse = fFoundFunctionExit.top(); |
| 818 | bool foundLoopExitOnFalse = fFoundLoopExit.top(); |
| 819 | fFoundFunctionExit.pop(); |
| 820 | fFoundLoopExit.pop(); |
| 821 | |
| 822 | fFoundFunctionExit.top() |= foundFunctionExitOnTrue && foundFunctionExitOnFalse; |
| 823 | fFoundLoopExit.top() |= foundLoopExitOnTrue && foundLoopExitOnFalse; |
| 824 | return result; |
| 825 | } |
| 826 | case Statement::Kind::kSwitch: |
| 827 | case Statement::Kind::kSwitchCase: |
| 828 | // We skip past switch statements entirely when scanning for dead code. Their |
| 829 | // control flow is quite complex and we already do a good job of flattening out |
| 830 | // switches on constant values. |
| 831 | break; |
| 832 | } |
| 833 | |
| 834 | return false; |
| 835 | } |
| 836 | |
| 837 | const Context& fContext; |
| 838 | ProgramUsage* fUsage; |
| 839 | std::stack<bool> fFoundFunctionExit; |
| 840 | std::stack<bool> fFoundLoopExit; |
| 841 | |
| 842 | using INHERITED = ProgramWriter; |
| 843 | }; |
| 844 | |
| 845 | for (std::unique_ptr<ProgramElement>& pe : program.ownedElements()) { |
| 846 | if (pe->is<FunctionDefinition>()) { |
| 847 | UnreachableCodeEliminator visitor{*fContext, usage}; |
| 848 | visitor.visitProgramElement(*pe); |
| 849 | } |
| 850 | } |
| 851 | } |
| 852 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 853 | bool Compiler::optimize(Program& program) { |
John Stiles | 7247b48 | 2021-03-08 10:40:35 -0500 | [diff] [blame] | 854 | // The optimizer only needs to run when it is enabled. |
| 855 | if (!program.fConfig->fSettings.fOptimize) { |
| 856 | return true; |
| 857 | } |
| 858 | |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 859 | SkASSERT(!this->errorCount()); |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 860 | ProgramUsage* usage = program.fUsage.get(); |
John Stiles | 7954d6c | 2020-09-01 10:53:02 -0400 | [diff] [blame] | 861 | |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 862 | if (this->errorCount() == 0) { |
John Stiles | 87fc657 | 2021-04-01 14:56:34 +0000 | [diff] [blame] | 863 | // Run the inliner only once; it is expensive! Multiple passes can occasionally shake out |
| 864 | // more wins, but it's diminishing returns. |
| 865 | fInliner.analyze(program.ownedElements(), program.fSymbols, usage); |
Ethan Nicholas | 34b19c5 | 2020-09-14 11:33:47 -0400 | [diff] [blame] | 866 | |
John Stiles | b666458 | 2021-03-19 09:46:00 -0400 | [diff] [blame] | 867 | while (this->removeDeadFunctions(program, usage)) { |
| 868 | // Removing dead functions may cause more functions to become unreferenced. Try again. |
Ethan Nicholas | 34b19c5 | 2020-09-14 11:33:47 -0400 | [diff] [blame] | 869 | } |
John Stiles | b666458 | 2021-03-19 09:46:00 -0400 | [diff] [blame] | 870 | while (this->removeDeadLocalVariables(program, usage)) { |
| 871 | // Removing dead variables may cause more variables to become unreferenced. Try again. |
| 872 | } |
John Stiles | 25be58e | 2021-05-20 14:38:40 -0400 | [diff] [blame] | 873 | // Unreachable code can confuse some drivers, so it's worth removing. (skia:12012) |
| 874 | this->removeUnreachableCode(program, usage); |
| 875 | |
Brian Osman | 8c26479 | 2021-07-01 16:41:27 -0400 | [diff] [blame] | 876 | this->removeDeadGlobalVariables(program, usage); |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 877 | } |
John Stiles | bb1505f | 2021-02-12 09:17:53 -0500 | [diff] [blame] | 878 | |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 879 | if (this->errorCount() == 0) { |
John Stiles | bb1505f | 2021-02-12 09:17:53 -0500 | [diff] [blame] | 880 | this->verifyStaticTests(program); |
| 881 | } |
| 882 | |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 883 | return this->errorCount() == 0; |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 884 | } |
| 885 | |
Brian Osman | fb32ddf | 2019-06-18 10:14:20 -0400 | [diff] [blame] | 886 | #if defined(SKSL_STANDALONE) || SK_SUPPORT_GPU |
| 887 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 888 | bool Compiler::toSPIRV(Program& program, OutputStream& out) { |
Brian Osman | 7a20b5c | 2021-03-15 16:23:33 -0400 | [diff] [blame] | 889 | TRACE_EVENT0("skia.shaders", "SkSL::Compiler::toSPIRV"); |
Ethan Nicholas | b449fff | 2021-08-04 15:06:37 -0400 | [diff] [blame] | 890 | AutoSource as(this, program.fSource->c_str()); |
Brian Salomon | d8d85b9 | 2021-07-07 09:41:17 -0400 | [diff] [blame] | 891 | ProgramSettings settings; |
| 892 | settings.fDSLUseMemoryPool = false; |
| 893 | dsl::Start(this, program.fConfig->fKind, settings); |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 894 | dsl::SetErrorReporter(&fErrorReporter); |
Brian Salomon | d8d85b9 | 2021-07-07 09:41:17 -0400 | [diff] [blame] | 895 | dsl::DSLWriter::IRGenerator().fSymbolTable = program.fSymbols; |
Ethan Nicholas | a6ae1f7 | 2017-03-16 09:56:54 -0400 | [diff] [blame] | 896 | #ifdef SK_ENABLE_SPIRV_VALIDATION |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 897 | StringStream buffer; |
Ethan Nicholas | 3abc6c6 | 2021-08-13 11:20:09 -0400 | [diff] [blame] | 898 | SPIRVCodeGenerator cg(fContext.get(), &program, &buffer); |
Ethan Nicholas | a6ae1f7 | 2017-03-16 09:56:54 -0400 | [diff] [blame] | 899 | bool result = cg.generateCode(); |
John Stiles | 270cec2 | 2021-02-17 12:59:36 -0500 | [diff] [blame] | 900 | if (result && program.fConfig->fSettings.fValidateSPIRV) { |
Ethan Nicholas | a6ae1f7 | 2017-03-16 09:56:54 -0400 | [diff] [blame] | 901 | spvtools::SpirvTools tools(SPV_ENV_VULKAN_1_0); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 902 | const String& data = buffer.str(); |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 903 | SkASSERT(0 == data.size() % 4); |
Brian Osman | 8d09d4a | 2020-11-24 15:51:06 -0500 | [diff] [blame] | 904 | String errors; |
| 905 | auto dumpmsg = [&errors](spv_message_level_t, const char*, const spv_position_t&, |
| 906 | const char* m) { |
| 907 | errors.appendf("SPIR-V validation error: %s\n", m); |
Ethan Nicholas | a6ae1f7 | 2017-03-16 09:56:54 -0400 | [diff] [blame] | 908 | }; |
| 909 | tools.SetMessageConsumer(dumpmsg); |
Brian Osman | 8d09d4a | 2020-11-24 15:51:06 -0500 | [diff] [blame] | 910 | |
| 911 | // Verify that the SPIR-V we produced is valid. At runtime, we will abort() with a message |
| 912 | // explaining the error. In standalone mode (skslc), we will send the message, plus the |
| 913 | // entire disassembled SPIR-V (for easier context & debugging) as *our* error message. |
| 914 | result = tools.Validate((const uint32_t*) data.c_str(), data.size() / 4); |
| 915 | |
| 916 | if (!result) { |
| 917 | #if defined(SKSL_STANDALONE) |
| 918 | // Convert the string-stream to a SPIR-V disassembly. |
| 919 | std::string disassembly; |
| 920 | if (tools.Disassemble((const uint32_t*)data.data(), data.size() / 4, &disassembly)) { |
| 921 | errors.append(disassembly); |
| 922 | } |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 923 | this->errorReporter().error(-1, errors); |
Brian Osman | 8d09d4a | 2020-11-24 15:51:06 -0500 | [diff] [blame] | 924 | #else |
| 925 | SkDEBUGFAILF("%s", errors.c_str()); |
| 926 | #endif |
| 927 | } |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 928 | out.write(data.c_str(), data.size()); |
Ethan Nicholas | a6ae1f7 | 2017-03-16 09:56:54 -0400 | [diff] [blame] | 929 | } |
| 930 | #else |
Ethan Nicholas | 3abc6c6 | 2021-08-13 11:20:09 -0400 | [diff] [blame] | 931 | SPIRVCodeGenerator cg(fContext.get(), &program, &out); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 932 | bool result = cg.generateCode(); |
Ethan Nicholas | a6ae1f7 | 2017-03-16 09:56:54 -0400 | [diff] [blame] | 933 | #endif |
Brian Salomon | d8d85b9 | 2021-07-07 09:41:17 -0400 | [diff] [blame] | 934 | dsl::End(); |
Ethan Nicholas | ce33f10 | 2016-12-09 17:22:59 -0500 | [diff] [blame] | 935 | return result; |
| 936 | } |
| 937 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 938 | bool Compiler::toSPIRV(Program& program, String* out) { |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 939 | StringStream buffer; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 940 | bool result = this->toSPIRV(program, buffer); |
| 941 | if (result) { |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 942 | *out = buffer.str(); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 943 | } |
| 944 | return result; |
| 945 | } |
| 946 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 947 | bool Compiler::toGLSL(Program& program, OutputStream& out) { |
Brian Osman | 7a20b5c | 2021-03-15 16:23:33 -0400 | [diff] [blame] | 948 | TRACE_EVENT0("skia.shaders", "SkSL::Compiler::toGLSL"); |
Ethan Nicholas | b449fff | 2021-08-04 15:06:37 -0400 | [diff] [blame] | 949 | AutoSource as(this, program.fSource->c_str()); |
Ethan Nicholas | 3abc6c6 | 2021-08-13 11:20:09 -0400 | [diff] [blame] | 950 | GLSLCodeGenerator cg(fContext.get(), &program, &out); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 951 | bool result = cg.generateCode(); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 952 | return result; |
| 953 | } |
| 954 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 955 | bool Compiler::toGLSL(Program& program, String* out) { |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 956 | StringStream buffer; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 957 | bool result = this->toGLSL(program, buffer); |
| 958 | if (result) { |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 959 | *out = buffer.str(); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 960 | } |
| 961 | return result; |
| 962 | } |
| 963 | |
Brian Osman | c024391 | 2020-02-19 15:35:26 -0500 | [diff] [blame] | 964 | bool Compiler::toHLSL(Program& program, String* out) { |
| 965 | String spirv; |
| 966 | if (!this->toSPIRV(program, &spirv)) { |
| 967 | return false; |
| 968 | } |
| 969 | |
| 970 | return SPIRVtoHLSL(spirv, out); |
| 971 | } |
| 972 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 973 | bool Compiler::toMetal(Program& program, OutputStream& out) { |
Brian Osman | 7a20b5c | 2021-03-15 16:23:33 -0400 | [diff] [blame] | 974 | TRACE_EVENT0("skia.shaders", "SkSL::Compiler::toMetal"); |
Ethan Nicholas | b449fff | 2021-08-04 15:06:37 -0400 | [diff] [blame] | 975 | AutoSource as(this, program.fSource->c_str()); |
Ethan Nicholas | 3abc6c6 | 2021-08-13 11:20:09 -0400 | [diff] [blame] | 976 | MetalCodeGenerator cg(fContext.get(), &program, &out); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 977 | bool result = cg.generateCode(); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 978 | return result; |
| 979 | } |
| 980 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 981 | bool Compiler::toMetal(Program& program, String* out) { |
Timothy Liang | b8eeb80 | 2018-07-23 16:46:16 -0400 | [diff] [blame] | 982 | StringStream buffer; |
| 983 | bool result = this->toMetal(program, buffer); |
| 984 | if (result) { |
| 985 | *out = buffer.str(); |
| 986 | } |
| 987 | return result; |
| 988 | } |
| 989 | |
Ethan Nicholas | 2a479a5 | 2020-08-18 16:29:45 -0400 | [diff] [blame] | 990 | #endif // defined(SKSL_STANDALONE) || SK_SUPPORT_GPU |
Brian Osman | 2e29ab5 | 2019-09-20 12:19:11 -0400 | [diff] [blame] | 991 | |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 992 | void Compiler::handleError(const char* msg, PositionInfo pos) { |
Ethan Nicholas | b449fff | 2021-08-04 15:06:37 -0400 | [diff] [blame] | 993 | if (strstr(msg, POISON_TAG)) { |
Ethan Nicholas | 549c6b8 | 2021-06-25 12:31:44 -0400 | [diff] [blame] | 994 | // don't report errors on poison values |
| 995 | return; |
| 996 | } |
Ethan Nicholas | a40ddcd | 2021-08-06 09:17:18 -0400 | [diff] [blame] | 997 | fErrorText += "error: " + (pos.line() >= 1 ? to_string(pos.line()) + ": " : "") + msg + "\n"; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 998 | } |
| 999 | |
Ethan Nicholas | 9504614 | 2021-01-07 10:57:27 -0500 | [diff] [blame] | 1000 | String Compiler::errorText(bool showCount) { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 1001 | this->errorReporter().reportPendingErrors(PositionInfo()); |
Ethan Nicholas | 9504614 | 2021-01-07 10:57:27 -0500 | [diff] [blame] | 1002 | if (showCount) { |
| 1003 | this->writeErrorCount(); |
| 1004 | } |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1005 | String result = fErrorText; |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 1006 | this->resetErrors(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1007 | return result; |
| 1008 | } |
| 1009 | |
| 1010 | void Compiler::writeErrorCount() { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 1011 | int count = this->errorCount(); |
| 1012 | if (count) { |
| 1013 | fErrorText += to_string(count) + " error"; |
| 1014 | if (count > 1) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1015 | fErrorText += "s"; |
| 1016 | } |
| 1017 | fErrorText += "\n"; |
| 1018 | } |
| 1019 | } |
| 1020 | |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 1021 | } // namespace SkSL |