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 | |
John Stiles | 270cec2 | 2021-02-17 12:59:36 -0500 | [diff] [blame] | 13 | #include "src/core/SkScopeExit.h" |
Leon Scroggins | b66214e | 2021-02-11 17:14:18 -0500 | [diff] [blame] | 14 | #include "src/core/SkTraceEvent.h" |
John Stiles | b92641c | 2020-08-31 18:09:01 -0400 | [diff] [blame] | 15 | #include "src/sksl/SkSLAnalysis.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 16 | #include "src/sksl/SkSLCPPCodeGenerator.h" |
John Stiles | f3a28db | 2021-03-10 23:00:47 -0500 | [diff] [blame] | 17 | #include "src/sksl/SkSLConstantFolder.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 18 | #include "src/sksl/SkSLGLSLCodeGenerator.h" |
| 19 | #include "src/sksl/SkSLHCodeGenerator.h" |
| 20 | #include "src/sksl/SkSLIRGenerator.h" |
| 21 | #include "src/sksl/SkSLMetalCodeGenerator.h" |
Brian Osman | 0018501 | 2021-02-04 16:07:11 -0500 | [diff] [blame] | 22 | #include "src/sksl/SkSLOperators.h" |
John Stiles | 270cec2 | 2021-02-17 12:59:36 -0500 | [diff] [blame] | 23 | #include "src/sksl/SkSLProgramSettings.h" |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 24 | #include "src/sksl/SkSLRehydrator.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 25 | #include "src/sksl/SkSLSPIRVCodeGenerator.h" |
Brian Osman | c024391 | 2020-02-19 15:35:26 -0500 | [diff] [blame] | 26 | #include "src/sksl/SkSLSPIRVtoHLSL.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 27 | #include "src/sksl/ir/SkSLEnum.h" |
| 28 | #include "src/sksl/ir/SkSLExpression.h" |
| 29 | #include "src/sksl/ir/SkSLExpressionStatement.h" |
| 30 | #include "src/sksl/ir/SkSLFunctionCall.h" |
| 31 | #include "src/sksl/ir/SkSLIntLiteral.h" |
| 32 | #include "src/sksl/ir/SkSLModifiersDeclaration.h" |
| 33 | #include "src/sksl/ir/SkSLNop.h" |
| 34 | #include "src/sksl/ir/SkSLSymbolTable.h" |
| 35 | #include "src/sksl/ir/SkSLTernaryExpression.h" |
| 36 | #include "src/sksl/ir/SkSLUnresolvedFunction.h" |
| 37 | #include "src/sksl/ir/SkSLVarDeclarations.h" |
John Stiles | e615000 | 2020-10-05 12:03:53 -0400 | [diff] [blame] | 38 | #include "src/utils/SkBitSet.h" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 39 | |
Ethan Nicholas | b33fa3f | 2020-08-06 13:00:19 -0400 | [diff] [blame] | 40 | #include <fstream> |
| 41 | |
Ethan Nicholas | a11035b | 2019-11-26 16:27:47 -0500 | [diff] [blame] | 42 | #if !defined(SKSL_STANDALONE) & SK_SUPPORT_GPU |
| 43 | #include "include/gpu/GrContextOptions.h" |
| 44 | #include "src/gpu/GrShaderCaps.h" |
| 45 | #endif |
| 46 | |
Ethan Nicholas | a6ae1f7 | 2017-03-16 09:56:54 -0400 | [diff] [blame] | 47 | #ifdef SK_ENABLE_SPIRV_VALIDATION |
| 48 | #include "spirv-tools/libspirv.hpp" |
| 49 | #endif |
| 50 | |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 51 | #if defined(SKSL_STANDALONE) |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 52 | |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 53 | // In standalone mode, we load the textual sksl source files. GN generates or copies these files |
| 54 | // to the skslc executable directory. The "data" in this mode is just the filename. |
| 55 | #define MODULE_DATA(name) MakeModulePath("sksl_" #name ".sksl") |
| 56 | |
| 57 | #else |
| 58 | |
| 59 | // 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] | 60 | #include "src/sksl/generated/sksl_fp.dehydrated.sksl" |
| 61 | #include "src/sksl/generated/sksl_frag.dehydrated.sksl" |
| 62 | #include "src/sksl/generated/sksl_geom.dehydrated.sksl" |
| 63 | #include "src/sksl/generated/sksl_gpu.dehydrated.sksl" |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 64 | #include "src/sksl/generated/sksl_public.dehydrated.sksl" |
Brian Osman | 9194675 | 2020-12-21 13:20:40 -0500 | [diff] [blame] | 65 | #include "src/sksl/generated/sksl_runtime.dehydrated.sksl" |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 66 | #include "src/sksl/generated/sksl_vert.dehydrated.sksl" |
| 67 | |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 68 | #define MODULE_DATA(name) MakeModuleData(SKSL_INCLUDE_sksl_##name,\ |
| 69 | SKSL_INCLUDE_sksl_##name##_LENGTH) |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 70 | |
| 71 | #endif |
Ethan Nicholas | 0d99766 | 2019-04-08 09:46:01 -0400 | [diff] [blame] | 72 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 73 | namespace SkSL { |
| 74 | |
John Stiles | 7247b48 | 2021-03-08 10:40:35 -0500 | [diff] [blame] | 75 | // Set these flags to `false` to disable optimization passes unilaterally. |
| 76 | // These flags allow tools like Viewer or Nanobench to override the compiler's ProgramSettings. |
| 77 | bool gSkSLOptimizer = true; |
| 78 | bool gSkSLInliner = true; |
John Stiles | 0bfeae6 | 2021-03-11 09:09:42 -0500 | [diff] [blame] | 79 | bool gSkSLDeadCodeElimination = true; |
John Stiles | 8ef4d6c | 2021-03-05 16:01:45 -0500 | [diff] [blame] | 80 | |
John Stiles | 47c0a74 | 2021-02-09 09:30:35 -0500 | [diff] [blame] | 81 | using RefKind = VariableReference::RefKind; |
| 82 | |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 83 | class AutoSource { |
| 84 | public: |
| 85 | AutoSource(Compiler* compiler, const String* source) |
| 86 | : fCompiler(compiler), fOldSource(fCompiler->fSource) { |
| 87 | fCompiler->fSource = source; |
| 88 | } |
| 89 | |
| 90 | ~AutoSource() { fCompiler->fSource = fOldSource; } |
| 91 | |
| 92 | Compiler* fCompiler; |
| 93 | const String* fOldSource; |
| 94 | }; |
| 95 | |
John Stiles | a935c3f | 2021-02-25 10:35:49 -0500 | [diff] [blame] | 96 | class AutoProgramConfig { |
| 97 | public: |
| 98 | AutoProgramConfig(std::shared_ptr<Context>& context, ProgramConfig* config) |
| 99 | : fContext(context.get()) { |
| 100 | SkASSERT(!fContext->fConfig); |
| 101 | fContext->fConfig = config; |
| 102 | } |
| 103 | |
| 104 | ~AutoProgramConfig() { |
| 105 | fContext->fConfig = nullptr; |
| 106 | } |
| 107 | |
| 108 | Context* fContext; |
| 109 | }; |
| 110 | |
John Stiles | d6a5f449 | 2021-02-11 15:46:11 -0500 | [diff] [blame] | 111 | Compiler::Compiler(const ShaderCapsClass* caps) |
John Stiles | c1a98b8 | 2021-02-24 13:35:02 -0500 | [diff] [blame] | 112 | : fContext(std::make_shared<Context>(/*errors=*/*this, *caps)) |
John Stiles | 7b92044 | 2020-12-17 10:43:41 -0500 | [diff] [blame] | 113 | , fInliner(fContext.get()) |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 114 | , fErrorCount(0) { |
John Stiles | c1a98b8 | 2021-02-24 13:35:02 -0500 | [diff] [blame] | 115 | SkASSERT(caps); |
John Stiles | 7c3515b | 2020-10-16 18:38:39 -0400 | [diff] [blame] | 116 | fRootSymbolTable = std::make_shared<SymbolTable>(this, /*builtin=*/true); |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 117 | fPrivateSymbolTable = std::make_shared<SymbolTable>(fRootSymbolTable, /*builtin=*/true); |
John Stiles | c1a98b8 | 2021-02-24 13:35:02 -0500 | [diff] [blame] | 118 | fIRGenerator = std::make_unique<IRGenerator>(fContext.get()); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 119 | |
John Stiles | 54e7c05 | 2021-01-11 14:22:36 -0500 | [diff] [blame] | 120 | #define TYPE(t) fContext->fTypes.f ## t .get() |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 121 | |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 122 | const SkSL::Symbol* rootTypes[] = { |
| 123 | TYPE(Void), |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 124 | |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 125 | TYPE( Float), TYPE( Float2), TYPE( Float3), TYPE( Float4), |
| 126 | TYPE( Half), TYPE( Half2), TYPE( Half3), TYPE( Half4), |
| 127 | TYPE( Int), TYPE( Int2), TYPE( Int3), TYPE( Int4), |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 128 | TYPE( Bool), TYPE( Bool2), TYPE( Bool3), TYPE( Bool4), |
Brian Salomon | 2a51de8 | 2016-11-16 12:06:01 -0500 | [diff] [blame] | 129 | |
Brian Osman | c0f2b64 | 2020-12-22 13:35:55 -0500 | [diff] [blame] | 130 | TYPE(Float2x2), TYPE(Float3x3), TYPE(Float4x4), |
Brian Osman | c63f431 | 2020-12-23 11:44:14 -0500 | [diff] [blame] | 131 | TYPE( Half2x2), TYPE( Half3x3), TYPE(Half4x4), |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 132 | |
Brian Osman | c63f431 | 2020-12-23 11:44:14 -0500 | [diff] [blame] | 133 | TYPE(SquareMat), TYPE(SquareHMat), |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 134 | |
Brian Osman | 20fad32 | 2020-12-23 12:42:33 -0500 | [diff] [blame] | 135 | TYPE(GenType), TYPE(GenHType), TYPE(GenIType), TYPE(GenBType), |
| 136 | TYPE(Vec), TYPE(HVec), TYPE(IVec), TYPE(BVec), |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 137 | |
| 138 | TYPE(FragmentProcessor), |
| 139 | }; |
| 140 | |
| 141 | const SkSL::Symbol* privateTypes[] = { |
Brian Osman | 20fad32 | 2020-12-23 12:42:33 -0500 | [diff] [blame] | 142 | TYPE( UInt), TYPE( UInt2), TYPE( UInt3), TYPE( UInt4), |
| 143 | TYPE( Short), TYPE( Short2), TYPE( Short3), TYPE( Short4), |
| 144 | TYPE(UShort), TYPE(UShort2), TYPE(UShort3), TYPE(UShort4), |
| 145 | TYPE( Byte), TYPE( Byte2), TYPE( Byte3), TYPE( Byte4), |
| 146 | TYPE( UByte), TYPE( UByte2), TYPE( UByte3), TYPE( UByte4), |
| 147 | |
| 148 | TYPE(GenUType), TYPE(UVec), |
| 149 | TYPE(SVec), TYPE(USVec), TYPE(ByteVec), TYPE(UByteVec), |
| 150 | |
Brian Osman | c0f2b64 | 2020-12-22 13:35:55 -0500 | [diff] [blame] | 151 | TYPE(Float2x3), TYPE(Float2x4), |
| 152 | TYPE(Float3x2), TYPE(Float3x4), |
| 153 | TYPE(Float4x2), TYPE(Float4x3), |
| 154 | |
Brian Osman | c63f431 | 2020-12-23 11:44:14 -0500 | [diff] [blame] | 155 | TYPE(Half2x3), TYPE(Half2x4), |
| 156 | TYPE(Half3x2), TYPE(Half3x4), |
| 157 | TYPE(Half4x2), TYPE(Half4x3), |
| 158 | |
Brian Osman | c0f2b64 | 2020-12-22 13:35:55 -0500 | [diff] [blame] | 159 | TYPE(Mat), TYPE(HMat), |
| 160 | |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 161 | TYPE(Sampler1D), TYPE(Sampler2D), TYPE(Sampler3D), |
| 162 | TYPE(SamplerExternalOES), |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 163 | TYPE(Sampler2DRect), |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 164 | |
| 165 | TYPE(ISampler2D), |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 166 | TYPE(SubpassInput), TYPE(SubpassInputMS), |
| 167 | |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 168 | TYPE(Sampler), |
| 169 | TYPE(Texture2D), |
| 170 | }; |
| 171 | |
| 172 | for (const SkSL::Symbol* type : rootTypes) { |
| 173 | fRootSymbolTable->addWithoutOwnership(type); |
| 174 | } |
| 175 | for (const SkSL::Symbol* type : privateTypes) { |
| 176 | fPrivateSymbolTable->addWithoutOwnership(type); |
| 177 | } |
| 178 | |
| 179 | #undef TYPE |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 180 | |
Brian Osman | 3887a01 | 2020-09-30 13:22:27 -0400 | [diff] [blame] | 181 | // sk_Caps is "builtin", but all references to it are resolved to Settings, so we don't need to |
| 182 | // treat it as builtin (ie, no need to clone it into the Program). |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 183 | fPrivateSymbolTable->add( |
| 184 | std::make_unique<Variable>(/*offset=*/-1, |
John Stiles | 586df95 | 2020-11-12 18:27:13 -0500 | [diff] [blame] | 185 | fIRGenerator->fModifiers->addToPool(Modifiers()), |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 186 | "sk_Caps", |
John Stiles | 54e7c05 | 2021-01-11 14:22:36 -0500 | [diff] [blame] | 187 | fContext->fTypes.fSkCaps.get(), |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 188 | /*builtin=*/false, |
| 189 | Variable::Storage::kGlobal)); |
Ethan Nicholas | 3605ace | 2016-11-21 15:59:48 -0500 | [diff] [blame] | 190 | |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 191 | fRootModule = {fRootSymbolTable, /*fIntrinsics=*/nullptr}; |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 192 | fPrivateModule = {fPrivateSymbolTable, /*fIntrinsics=*/nullptr}; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 193 | } |
| 194 | |
John Stiles | dd13dba | 2020-10-29 10:45:34 -0400 | [diff] [blame] | 195 | Compiler::~Compiler() {} |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 196 | |
Brian Osman | 5626998 | 2020-11-20 12:38:07 -0500 | [diff] [blame] | 197 | const ParsedModule& Compiler::loadGPUModule() { |
| 198 | if (!fGPUModule.fSymbols) { |
John Stiles | dbd4e6f | 2021-02-16 13:29:15 -0500 | [diff] [blame] | 199 | fGPUModule = this->parseModule(ProgramKind::kFragment, MODULE_DATA(gpu), fPrivateModule); |
Brian Osman | 5626998 | 2020-11-20 12:38:07 -0500 | [diff] [blame] | 200 | } |
| 201 | return fGPUModule; |
| 202 | } |
| 203 | |
| 204 | const ParsedModule& Compiler::loadFragmentModule() { |
| 205 | if (!fFragmentModule.fSymbols) { |
John Stiles | dbd4e6f | 2021-02-16 13:29:15 -0500 | [diff] [blame] | 206 | fFragmentModule = this->parseModule(ProgramKind::kFragment, MODULE_DATA(frag), |
Brian Osman | 5626998 | 2020-11-20 12:38:07 -0500 | [diff] [blame] | 207 | this->loadGPUModule()); |
| 208 | } |
| 209 | return fFragmentModule; |
| 210 | } |
| 211 | |
| 212 | const ParsedModule& Compiler::loadVertexModule() { |
| 213 | if (!fVertexModule.fSymbols) { |
John Stiles | dbd4e6f | 2021-02-16 13:29:15 -0500 | [diff] [blame] | 214 | fVertexModule = this->parseModule(ProgramKind::kVertex, MODULE_DATA(vert), |
Brian Osman | 5626998 | 2020-11-20 12:38:07 -0500 | [diff] [blame] | 215 | this->loadGPUModule()); |
| 216 | } |
| 217 | return fVertexModule; |
| 218 | } |
| 219 | |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 220 | const ParsedModule& Compiler::loadGeometryModule() { |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 221 | if (!fGeometryModule.fSymbols) { |
John Stiles | dbd4e6f | 2021-02-16 13:29:15 -0500 | [diff] [blame] | 222 | fGeometryModule = this->parseModule(ProgramKind::kGeometry, MODULE_DATA(geom), |
Brian Osman | 5626998 | 2020-11-20 12:38:07 -0500 | [diff] [blame] | 223 | this->loadGPUModule()); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 224 | } |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 225 | return fGeometryModule; |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 226 | } |
| 227 | |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 228 | const ParsedModule& Compiler::loadFPModule() { |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 229 | if (!fFPModule.fSymbols) { |
John Stiles | dbd4e6f | 2021-02-16 13:29:15 -0500 | [diff] [blame] | 230 | fFPModule = this->parseModule(ProgramKind::kFragmentProcessor, MODULE_DATA(fp), |
Brian Osman | 5626998 | 2020-11-20 12:38:07 -0500 | [diff] [blame] | 231 | this->loadGPUModule()); |
Brian Osman | 8e2ef02 | 2020-09-30 13:26:43 -0400 | [diff] [blame] | 232 | } |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 233 | return fFPModule; |
Brian Osman | 8e2ef02 | 2020-09-30 13:26:43 -0400 | [diff] [blame] | 234 | } |
| 235 | |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 236 | const ParsedModule& Compiler::loadPublicModule() { |
| 237 | if (!fPublicModule.fSymbols) { |
John Stiles | dbd4e6f | 2021-02-16 13:29:15 -0500 | [diff] [blame] | 238 | fPublicModule = this->parseModule(ProgramKind::kGeneric, MODULE_DATA(public), fRootModule); |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 239 | } |
| 240 | return fPublicModule; |
| 241 | } |
| 242 | |
Brian Osman | 9194675 | 2020-12-21 13:20:40 -0500 | [diff] [blame] | 243 | const ParsedModule& Compiler::loadRuntimeEffectModule() { |
| 244 | if (!fRuntimeEffectModule.fSymbols) { |
John Stiles | dbd4e6f | 2021-02-16 13:29:15 -0500 | [diff] [blame] | 245 | fRuntimeEffectModule = this->parseModule(ProgramKind::kRuntimeEffect, MODULE_DATA(runtime), |
Brian Osman | 9194675 | 2020-12-21 13:20:40 -0500 | [diff] [blame] | 246 | this->loadPublicModule()); |
Brian Osman | f1319c3 | 2020-10-13 09:34:23 -0400 | [diff] [blame] | 247 | |
Brian Osman | 9194675 | 2020-12-21 13:20:40 -0500 | [diff] [blame] | 248 | // Add some aliases to the runtime effect module so that it's friendlier, and more like GLSL |
John Stiles | 54e7c05 | 2021-01-11 14:22:36 -0500 | [diff] [blame] | 249 | fRuntimeEffectModule.fSymbols->addAlias("shader", fContext->fTypes.fFragmentProcessor.get()); |
Brian Osman | f1319c3 | 2020-10-13 09:34:23 -0400 | [diff] [blame] | 250 | |
John Stiles | 54e7c05 | 2021-01-11 14:22:36 -0500 | [diff] [blame] | 251 | fRuntimeEffectModule.fSymbols->addAlias("vec2", fContext->fTypes.fFloat2.get()); |
| 252 | fRuntimeEffectModule.fSymbols->addAlias("vec3", fContext->fTypes.fFloat3.get()); |
| 253 | fRuntimeEffectModule.fSymbols->addAlias("vec4", fContext->fTypes.fFloat4.get()); |
Brian Osman | f1319c3 | 2020-10-13 09:34:23 -0400 | [diff] [blame] | 254 | |
John Stiles | 54e7c05 | 2021-01-11 14:22:36 -0500 | [diff] [blame] | 255 | fRuntimeEffectModule.fSymbols->addAlias("bvec2", fContext->fTypes.fBool2.get()); |
| 256 | fRuntimeEffectModule.fSymbols->addAlias("bvec3", fContext->fTypes.fBool3.get()); |
| 257 | fRuntimeEffectModule.fSymbols->addAlias("bvec4", fContext->fTypes.fBool4.get()); |
Brian Osman | f1319c3 | 2020-10-13 09:34:23 -0400 | [diff] [blame] | 258 | |
John Stiles | 54e7c05 | 2021-01-11 14:22:36 -0500 | [diff] [blame] | 259 | fRuntimeEffectModule.fSymbols->addAlias("mat2", fContext->fTypes.fFloat2x2.get()); |
| 260 | fRuntimeEffectModule.fSymbols->addAlias("mat3", fContext->fTypes.fFloat3x3.get()); |
| 261 | fRuntimeEffectModule.fSymbols->addAlias("mat4", fContext->fTypes.fFloat4x4.get()); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 262 | } |
Brian Osman | 9194675 | 2020-12-21 13:20:40 -0500 | [diff] [blame] | 263 | return fRuntimeEffectModule; |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 264 | } |
| 265 | |
John Stiles | dbd4e6f | 2021-02-16 13:29:15 -0500 | [diff] [blame] | 266 | const ParsedModule& Compiler::moduleForProgramKind(ProgramKind kind) { |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 267 | switch (kind) { |
John Stiles | dbd4e6f | 2021-02-16 13:29:15 -0500 | [diff] [blame] | 268 | case ProgramKind::kVertex: return this->loadVertexModule(); break; |
| 269 | case ProgramKind::kFragment: return this->loadFragmentModule(); break; |
| 270 | case ProgramKind::kGeometry: return this->loadGeometryModule(); break; |
| 271 | case ProgramKind::kFragmentProcessor: return this->loadFPModule(); break; |
| 272 | case ProgramKind::kRuntimeEffect: return this->loadRuntimeEffectModule(); break; |
| 273 | case ProgramKind::kGeneric: return this->loadPublicModule(); break; |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 274 | } |
| 275 | SkUNREACHABLE; |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 276 | } |
| 277 | |
John Stiles | dbd4e6f | 2021-02-16 13:29:15 -0500 | [diff] [blame] | 278 | LoadedModule Compiler::loadModule(ProgramKind kind, |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 279 | ModuleData data, |
John Stiles | a935c3f | 2021-02-25 10:35:49 -0500 | [diff] [blame] | 280 | std::shared_ptr<SymbolTable> base, |
| 281 | bool dehydrate) { |
| 282 | if (dehydrate) { |
| 283 | // NOTE: This is a workaround. When dehydrating includes, skslc doesn't know which module |
| 284 | // it's preparing, nor what the correct base module is. We can't use 'Root', because many |
| 285 | // GPU intrinsics reference private types, like samplers or textures. Today, 'Private' does |
| 286 | // contain the union of all known types, so this is safe. If we ever have types that only |
| 287 | // exist in 'Public' (for example), this logic needs to be smarter (by choosing the correct |
| 288 | // base for the module we're compiling). |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 289 | base = fPrivateSymbolTable; |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 290 | } |
John Stiles | a935c3f | 2021-02-25 10:35:49 -0500 | [diff] [blame] | 291 | SkASSERT(base); |
| 292 | |
| 293 | // Built-in modules always use default program settings. |
| 294 | ProgramConfig config; |
| 295 | config.fKind = kind; |
| 296 | config.fSettings.fReplaceSettings = !dehydrate; |
| 297 | AutoProgramConfig autoConfig(fContext, &config); |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 298 | |
| 299 | #if defined(SKSL_STANDALONE) |
| 300 | SkASSERT(data.fPath); |
| 301 | std::ifstream in(data.fPath); |
John Stiles | d51c979 | 2021-03-18 11:40:14 -0400 | [diff] [blame] | 302 | String text{std::istreambuf_iterator<char>(in), std::istreambuf_iterator<char>()}; |
Ethan Nicholas | b33fa3f | 2020-08-06 13:00:19 -0400 | [diff] [blame] | 303 | if (in.rdstate()) { |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 304 | printf("error reading %s\n", data.fPath); |
Ethan Nicholas | b33fa3f | 2020-08-06 13:00:19 -0400 | [diff] [blame] | 305 | abort(); |
| 306 | } |
Brian Osman | e498b3c | 2020-09-23 14:42:11 -0400 | [diff] [blame] | 307 | const String* source = fRootSymbolTable->takeOwnershipOfString(std::move(text)); |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 308 | AutoSource as(this, source); |
John Stiles | d120464 | 2021-02-17 16:30:02 -0500 | [diff] [blame] | 309 | |
John Stiles | 881a10c | 2020-09-19 10:13:24 -0400 | [diff] [blame] | 310 | SkASSERT(fIRGenerator->fCanInline); |
| 311 | fIRGenerator->fCanInline = false; |
John Stiles | d120464 | 2021-02-17 16:30:02 -0500 | [diff] [blame] | 312 | |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 313 | ParsedModule baseModule = {base, /*fIntrinsics=*/nullptr}; |
John Stiles | d120464 | 2021-02-17 16:30:02 -0500 | [diff] [blame] | 314 | IRGenerator::IRBundle ir = fIRGenerator->convertProgram(baseModule, /*isBuiltinCode=*/true, |
| 315 | source->c_str(), source->length(), |
| 316 | /*externalFunctions=*/nullptr); |
Brian Osman | 133724c | 2020-10-28 14:14:39 -0400 | [diff] [blame] | 317 | SkASSERT(ir.fSharedElements.empty()); |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 318 | LoadedModule module = { kind, std::move(ir.fSymbolTable), std::move(ir.fElements) }; |
John Stiles | 881a10c | 2020-09-19 10:13:24 -0400 | [diff] [blame] | 319 | fIRGenerator->fCanInline = true; |
Ethan Nicholas | 8da1e65 | 2019-05-24 11:01:59 -0400 | [diff] [blame] | 320 | if (this->fErrorCount) { |
| 321 | printf("Unexpected errors: %s\n", this->fErrorText.c_str()); |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 322 | SkDEBUGFAILF("%s %s\n", data.fPath, this->fErrorText.c_str()); |
Ethan Nicholas | 8da1e65 | 2019-05-24 11:01:59 -0400 | [diff] [blame] | 323 | } |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 324 | fModifiers.push_back(std::move(ir.fModifiers)); |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 325 | #else |
| 326 | SkASSERT(data.fData && (data.fSize != 0)); |
| 327 | Rehydrator rehydrator(fContext.get(), fIRGenerator->fModifiers.get(), base, this, |
| 328 | data.fData, data.fSize); |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 329 | LoadedModule module = { kind, rehydrator.symbolTable(), rehydrator.elements() }; |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 330 | fModifiers.push_back(fIRGenerator->releaseModifiers()); |
| 331 | #endif |
| 332 | |
| 333 | return module; |
| 334 | } |
| 335 | |
John Stiles | dbd4e6f | 2021-02-16 13:29:15 -0500 | [diff] [blame] | 336 | ParsedModule Compiler::parseModule(ProgramKind kind, ModuleData data, const ParsedModule& base) { |
John Stiles | a935c3f | 2021-02-25 10:35:49 -0500 | [diff] [blame] | 337 | LoadedModule module = this->loadModule(kind, data, base.fSymbols, /*dehydrate=*/false); |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 338 | this->optimize(module); |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 339 | |
| 340 | // For modules that just declare (but don't define) intrinsic functions, there will be no new |
| 341 | // 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] | 342 | if (module.fElements.empty()) { |
| 343 | return {module.fSymbols, base.fIntrinsics}; |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | auto intrinsics = std::make_shared<IRIntrinsicMap>(base.fIntrinsics.get()); |
| 347 | |
| 348 | // Now, transfer all of the program elements to an intrinsic map. This maps certain types of |
| 349 | // global objects to the declaring ProgramElement. |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 350 | for (std::unique_ptr<ProgramElement>& element : module.fElements) { |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 351 | switch (element->kind()) { |
| 352 | case ProgramElement::Kind::kFunction: { |
| 353 | const FunctionDefinition& f = element->as<FunctionDefinition>(); |
Ethan Nicholas | 0a5d096 | 2020-10-14 13:33:18 -0400 | [diff] [blame] | 354 | SkASSERT(f.declaration().isBuiltin()); |
| 355 | intrinsics->insertOrDie(f.declaration().description(), std::move(element)); |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 356 | break; |
| 357 | } |
John Stiles | 569249b | 2020-11-03 12:18:22 -0500 | [diff] [blame] | 358 | case ProgramElement::Kind::kFunctionPrototype: { |
| 359 | // These are already in the symbol table. |
| 360 | break; |
| 361 | } |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 362 | case ProgramElement::Kind::kEnum: { |
| 363 | const Enum& e = element->as<Enum>(); |
| 364 | SkASSERT(e.isBuiltin()); |
| 365 | intrinsics->insertOrDie(e.typeName(), std::move(element)); |
| 366 | break; |
| 367 | } |
| 368 | case ProgramElement::Kind::kGlobalVar: { |
Ethan Nicholas | c51f33e | 2020-10-13 13:49:44 -0400 | [diff] [blame] | 369 | const GlobalVarDeclaration& global = element->as<GlobalVarDeclaration>(); |
| 370 | const Variable& var = global.declaration()->as<VarDeclaration>().var(); |
| 371 | SkASSERT(var.isBuiltin()); |
| 372 | intrinsics->insertOrDie(var.name(), std::move(element)); |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 373 | break; |
| 374 | } |
| 375 | case ProgramElement::Kind::kInterfaceBlock: { |
Ethan Nicholas | eaf4788 | 2020-10-15 10:10:08 -0400 | [diff] [blame] | 376 | const Variable& var = element->as<InterfaceBlock>().variable(); |
| 377 | SkASSERT(var.isBuiltin()); |
| 378 | intrinsics->insertOrDie(var.name(), std::move(element)); |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 379 | break; |
| 380 | } |
| 381 | default: |
| 382 | printf("Unsupported element: %s\n", element->description().c_str()); |
| 383 | SkASSERT(false); |
| 384 | break; |
| 385 | } |
| 386 | } |
| 387 | |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 388 | return {module.fSymbols, std::move(intrinsics)}; |
Ethan Nicholas | 8da1e65 | 2019-05-24 11:01:59 -0400 | [diff] [blame] | 389 | } |
| 390 | |
Brian Osman | 32d5355 | 2020-09-23 13:55:20 -0400 | [diff] [blame] | 391 | std::unique_ptr<Program> Compiler::convertProgram( |
John Stiles | dbd4e6f | 2021-02-16 13:29:15 -0500 | [diff] [blame] | 392 | ProgramKind kind, |
Brian Osman | 32d5355 | 2020-09-23 13:55:20 -0400 | [diff] [blame] | 393 | String text, |
| 394 | const Program::Settings& settings, |
Brian Osman | be0b3b7 | 2021-01-06 14:27:35 -0500 | [diff] [blame] | 395 | const std::vector<std::unique_ptr<ExternalFunction>>* externalFunctions) { |
Brian Osman | 7a20b5c | 2021-03-15 16:23:33 -0400 | [diff] [blame] | 396 | TRACE_EVENT0("skia.shaders", "SkSL::Compiler::convertProgram"); |
Leon Scroggins | b66214e | 2021-02-11 17:14:18 -0500 | [diff] [blame] | 397 | |
John Stiles | dbd4e6f | 2021-02-16 13:29:15 -0500 | [diff] [blame] | 398 | SkASSERT(!externalFunctions || (kind == ProgramKind::kGeneric)); |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 399 | |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 400 | // Loading and optimizing our base module might reset the inliner, so do that first, |
| 401 | // *then* configure the inliner with the settings for this program. |
| 402 | const ParsedModule& baseModule = this->moduleForProgramKind(kind); |
| 403 | |
John Stiles | 270cec2 | 2021-02-17 12:59:36 -0500 | [diff] [blame] | 404 | // Update our context to point to the program configuration for the duration of compilation. |
| 405 | auto config = std::make_unique<ProgramConfig>(ProgramConfig{kind, settings}); |
John Stiles | a935c3f | 2021-02-25 10:35:49 -0500 | [diff] [blame] | 406 | AutoProgramConfig autoConfig(fContext, config.get()); |
John Stiles | 270cec2 | 2021-02-17 12:59:36 -0500 | [diff] [blame] | 407 | |
John Stiles | 7247b48 | 2021-03-08 10:40:35 -0500 | [diff] [blame] | 408 | // Honor our global optimization-disable flags. |
| 409 | config->fSettings.fOptimize &= gSkSLOptimizer; |
John Stiles | 7247b48 | 2021-03-08 10:40:35 -0500 | [diff] [blame] | 410 | config->fSettings.fInlineThreshold *= (int)gSkSLInliner; |
John Stiles | 0bfeae6 | 2021-03-11 09:09:42 -0500 | [diff] [blame] | 411 | config->fSettings.fRemoveDeadVariables &= gSkSLDeadCodeElimination; |
| 412 | config->fSettings.fRemoveDeadFunctions &= gSkSLDeadCodeElimination; |
John Stiles | 7247b48 | 2021-03-08 10:40:35 -0500 | [diff] [blame] | 413 | |
| 414 | // Disable optimization settings that depend on a parent setting which has been disabled. |
John Stiles | 7247b48 | 2021-03-08 10:40:35 -0500 | [diff] [blame] | 415 | config->fSettings.fInlineThreshold *= (int)config->fSettings.fOptimize; |
John Stiles | 0bfeae6 | 2021-03-11 09:09:42 -0500 | [diff] [blame] | 416 | config->fSettings.fRemoveDeadFunctions &= config->fSettings.fOptimize; |
| 417 | config->fSettings.fRemoveDeadVariables &= config->fSettings.fOptimize; |
John Stiles | 7247b48 | 2021-03-08 10:40:35 -0500 | [diff] [blame] | 418 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 419 | fErrorText = ""; |
| 420 | fErrorCount = 0; |
John Stiles | d120464 | 2021-02-17 16:30:02 -0500 | [diff] [blame] | 421 | fInliner.reset(fIRGenerator->fModifiers.get()); |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 422 | |
| 423 | // Not using AutoSource, because caller is likely to call errorText() if we fail to compile |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 424 | std::unique_ptr<String> textPtr(new String(std::move(text))); |
| 425 | fSource = textPtr.get(); |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 426 | |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 427 | // Enable node pooling while converting and optimizing the program for a performance boost. |
| 428 | // The Program will take ownership of the pool. |
Brian Osman | 28f702c | 2021-02-02 11:52:07 -0500 | [diff] [blame] | 429 | std::unique_ptr<Pool> pool; |
John Stiles | c1a98b8 | 2021-02-24 13:35:02 -0500 | [diff] [blame] | 430 | if (fContext->fCaps.useNodePools()) { |
Brian Osman | 28f702c | 2021-02-02 11:52:07 -0500 | [diff] [blame] | 431 | pool = Pool::Create(); |
| 432 | pool->attachToThread(); |
| 433 | } |
John Stiles | d120464 | 2021-02-17 16:30:02 -0500 | [diff] [blame] | 434 | IRGenerator::IRBundle ir = fIRGenerator->convertProgram(baseModule, /*isBuiltinCode=*/false, |
| 435 | textPtr->c_str(), textPtr->size(), |
| 436 | externalFunctions); |
John Stiles | 270cec2 | 2021-02-17 12:59:36 -0500 | [diff] [blame] | 437 | auto program = std::make_unique<Program>(std::move(textPtr), |
| 438 | std::move(config), |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 439 | fContext, |
| 440 | std::move(ir.fElements), |
Brian Osman | 133724c | 2020-10-28 14:14:39 -0400 | [diff] [blame] | 441 | std::move(ir.fSharedElements), |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 442 | std::move(ir.fModifiers), |
| 443 | std::move(ir.fSymbolTable), |
| 444 | std::move(pool), |
| 445 | ir.fInputs); |
| 446 | bool success = false; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 447 | if (fErrorCount) { |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 448 | // Do not return programs that failed to compile. |
John Stiles | 7247b48 | 2021-03-08 10:40:35 -0500 | [diff] [blame] | 449 | } else if (!this->optimize(*program)) { |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 450 | // Do not return programs that failed to optimize. |
| 451 | } else { |
| 452 | // We have a successful program! |
| 453 | success = true; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 454 | } |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 455 | |
Brian Osman | 28f702c | 2021-02-02 11:52:07 -0500 | [diff] [blame] | 456 | if (program->fPool) { |
| 457 | program->fPool->detachFromThread(); |
| 458 | } |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 459 | return success ? std::move(program) : nullptr; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 460 | } |
| 461 | |
John Stiles | bb1505f | 2021-02-12 09:17:53 -0500 | [diff] [blame] | 462 | void Compiler::verifyStaticTests(const Program& program) { |
| 463 | class StaticTestVerifier : public ProgramVisitor { |
| 464 | public: |
| 465 | StaticTestVerifier(ErrorReporter* r) : fReporter(r) {} |
| 466 | |
| 467 | using ProgramVisitor::visitProgramElement; |
| 468 | |
| 469 | bool visitStatement(const Statement& stmt) override { |
| 470 | switch (stmt.kind()) { |
| 471 | case Statement::Kind::kIf: |
| 472 | if (stmt.as<IfStatement>().isStatic()) { |
| 473 | fReporter->error(stmt.fOffset, "static if has non-static test"); |
| 474 | } |
| 475 | break; |
| 476 | |
| 477 | case Statement::Kind::kSwitch: |
| 478 | if (stmt.as<SwitchStatement>().isStatic()) { |
| 479 | fReporter->error(stmt.fOffset, "static switch has non-static test"); |
| 480 | } |
| 481 | break; |
| 482 | |
| 483 | default: |
| 484 | break; |
| 485 | } |
| 486 | return INHERITED::visitStatement(stmt); |
| 487 | } |
| 488 | |
John Stiles | 59e3456 | 2021-02-12 16:56:39 -0500 | [diff] [blame] | 489 | bool visitExpression(const Expression&) override { |
| 490 | // We aren't looking for anything inside an Expression, so skip them entirely. |
| 491 | return false; |
| 492 | } |
| 493 | |
John Stiles | bb1505f | 2021-02-12 09:17:53 -0500 | [diff] [blame] | 494 | private: |
| 495 | using INHERITED = ProgramVisitor; |
| 496 | ErrorReporter* fReporter; |
| 497 | }; |
| 498 | |
| 499 | // 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] | 500 | if (fContext->fConfig->fSettings.fPermitInvalidStaticTests) { |
John Stiles | bb1505f | 2021-02-12 09:17:53 -0500 | [diff] [blame] | 501 | return; |
| 502 | } |
| 503 | |
| 504 | // Check all of the program's owned elements. (Built-in elements are assumed to be valid.) |
| 505 | StaticTestVerifier visitor{this}; |
| 506 | for (const std::unique_ptr<ProgramElement>& element : program.ownedElements()) { |
| 507 | if (element->is<FunctionDefinition>()) { |
| 508 | visitor.visitProgramElement(*element); |
| 509 | } |
| 510 | } |
| 511 | } |
| 512 | |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 513 | bool Compiler::optimize(LoadedModule& module) { |
| 514 | SkASSERT(!fErrorCount); |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 515 | |
John Stiles | 270cec2 | 2021-02-17 12:59:36 -0500 | [diff] [blame] | 516 | // Create a temporary program configuration with default settings. |
| 517 | ProgramConfig config; |
| 518 | config.fKind = module.fKind; |
John Stiles | a935c3f | 2021-02-25 10:35:49 -0500 | [diff] [blame] | 519 | AutoProgramConfig autoConfig(fContext, &config); |
John Stiles | 270cec2 | 2021-02-17 12:59:36 -0500 | [diff] [blame] | 520 | |
John Stiles | d120464 | 2021-02-17 16:30:02 -0500 | [diff] [blame] | 521 | // Reset the Inliner. |
| 522 | fInliner.reset(fModifiers.back().get()); |
John Stiles | 270cec2 | 2021-02-17 12:59:36 -0500 | [diff] [blame] | 523 | |
| 524 | std::unique_ptr<ProgramUsage> usage = Analysis::GetUsage(module); |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 525 | |
| 526 | while (fErrorCount == 0) { |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 527 | // Perform inline-candidate analysis and inline any functions deemed suitable. |
John Stiles | f3a28db | 2021-03-10 23:00:47 -0500 | [diff] [blame] | 528 | if (!fInliner.analyze(module.fElements, module.fSymbols, usage.get())) { |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 529 | break; |
| 530 | } |
| 531 | } |
| 532 | return fErrorCount == 0; |
| 533 | } |
| 534 | |
John Stiles | 0bfeae6 | 2021-03-11 09:09:42 -0500 | [diff] [blame] | 535 | bool Compiler::removeDeadFunctions(Program& program, ProgramUsage* usage) { |
| 536 | bool madeChanges = false; |
| 537 | |
| 538 | if (program.fConfig->fSettings.fRemoveDeadFunctions) { |
| 539 | auto isDeadFunction = [&](const ProgramElement* element) { |
| 540 | if (!element->is<FunctionDefinition>()) { |
| 541 | return false; |
| 542 | } |
| 543 | const FunctionDefinition& fn = element->as<FunctionDefinition>(); |
| 544 | if (fn.declaration().name() == "main" || usage->get(fn.declaration()) > 0) { |
| 545 | return false; |
| 546 | } |
| 547 | usage->remove(*element); |
| 548 | madeChanges = true; |
| 549 | return true; |
| 550 | }; |
| 551 | |
| 552 | program.fElements.erase(std::remove_if(program.fElements.begin(), |
| 553 | program.fElements.end(), |
| 554 | [&](const std::unique_ptr<ProgramElement>& element) { |
| 555 | return isDeadFunction(element.get()); |
| 556 | }), |
| 557 | program.fElements.end()); |
| 558 | program.fSharedElements.erase(std::remove_if(program.fSharedElements.begin(), |
| 559 | program.fSharedElements.end(), |
| 560 | isDeadFunction), |
| 561 | program.fSharedElements.end()); |
| 562 | } |
| 563 | return madeChanges; |
| 564 | } |
| 565 | |
| 566 | bool Compiler::removeDeadGlobalVariables(Program& program, ProgramUsage* usage) { |
| 567 | bool madeChanges = false; |
| 568 | |
| 569 | if (program.fConfig->fSettings.fRemoveDeadVariables) { |
| 570 | auto isDeadVariable = [&](const ProgramElement* element) { |
| 571 | if (!element->is<GlobalVarDeclaration>()) { |
| 572 | return false; |
| 573 | } |
| 574 | const GlobalVarDeclaration& global = element->as<GlobalVarDeclaration>(); |
| 575 | const VarDeclaration& varDecl = global.declaration()->as<VarDeclaration>(); |
| 576 | if (!usage->isDead(varDecl.var())) { |
| 577 | return false; |
| 578 | } |
| 579 | madeChanges = true; |
| 580 | return true; |
| 581 | }; |
| 582 | |
| 583 | program.fElements.erase(std::remove_if(program.fElements.begin(), |
| 584 | program.fElements.end(), |
| 585 | [&](const std::unique_ptr<ProgramElement>& element) { |
| 586 | return isDeadVariable(element.get()); |
| 587 | }), |
| 588 | program.fElements.end()); |
| 589 | program.fSharedElements.erase(std::remove_if(program.fSharedElements.begin(), |
| 590 | program.fSharedElements.end(), |
| 591 | isDeadVariable), |
| 592 | program.fSharedElements.end()); |
| 593 | } |
| 594 | return madeChanges; |
| 595 | } |
| 596 | |
John Stiles | 2654187 | 2021-03-16 12:19:54 -0400 | [diff] [blame] | 597 | bool Compiler::removeDeadLocalVariables(Program& program, ProgramUsage* usage) { |
| 598 | class DeadLocalVariableEliminator : public ProgramWriter { |
| 599 | public: |
| 600 | DeadLocalVariableEliminator(const Context& context, ProgramUsage* usage) |
| 601 | : fContext(context) |
| 602 | , fUsage(usage) {} |
| 603 | |
| 604 | using ProgramWriter::visitProgramElement; |
| 605 | |
| 606 | bool visitExpressionPtr(std::unique_ptr<Expression>& expr) override { |
| 607 | // We don't need to look inside expressions at all. |
| 608 | return false; |
| 609 | } |
| 610 | |
| 611 | bool visitStatementPtr(std::unique_ptr<Statement>& stmt) override { |
| 612 | if (stmt->is<VarDeclaration>()) { |
| 613 | VarDeclaration& varDecl = stmt->as<VarDeclaration>(); |
| 614 | const Variable* var = &varDecl.var(); |
| 615 | ProgramUsage::VariableCounts* counts = fUsage->fVariableCounts.find(var); |
| 616 | SkASSERT(counts); |
| 617 | SkASSERT(counts->fDeclared); |
| 618 | if (CanEliminate(var, *counts)) { |
| 619 | if (var->initialValue()) { |
| 620 | // The variable has an initial-value expression, which might have side |
| 621 | // effects. ExpressionStatement::Make will preserve side effects, but |
| 622 | // replaces pure expressions with Nop. |
| 623 | fUsage->remove(stmt.get()); |
| 624 | stmt = ExpressionStatement::Make(fContext, std::move(varDecl.value())); |
| 625 | fUsage->add(stmt.get()); |
| 626 | } else { |
| 627 | // The variable has no initial-value and can be cleanly eliminated. |
| 628 | fUsage->remove(stmt.get()); |
| 629 | stmt = std::make_unique<Nop>(); |
| 630 | } |
| 631 | fMadeChanges = true; |
| 632 | } |
| 633 | return false; |
| 634 | } |
| 635 | return INHERITED::visitStatementPtr(stmt); |
| 636 | } |
| 637 | |
| 638 | static bool CanEliminate(const Variable* var, const ProgramUsage::VariableCounts& counts) { |
| 639 | if (!counts.fDeclared || counts.fRead || var->storage() != VariableStorage::kLocal) { |
| 640 | return false; |
| 641 | } |
| 642 | if (var->initialValue()) { |
| 643 | SkASSERT(counts.fWrite >= 1); |
| 644 | return counts.fWrite == 1; |
| 645 | } else { |
| 646 | return counts.fWrite == 0; |
| 647 | } |
| 648 | } |
| 649 | |
| 650 | bool fMadeChanges = false; |
| 651 | const Context& fContext; |
| 652 | ProgramUsage* fUsage; |
| 653 | |
| 654 | using INHERITED = ProgramWriter; |
| 655 | }; |
| 656 | |
| 657 | DeadLocalVariableEliminator visitor{*fContext, usage}; |
| 658 | |
| 659 | if (program.fConfig->fSettings.fRemoveDeadVariables) { |
| 660 | for (auto& [var, counts] : usage->fVariableCounts) { |
| 661 | if (DeadLocalVariableEliminator::CanEliminate(var, counts)) { |
| 662 | // This program contains at least one dead local variable. |
| 663 | // Scan the program for any dead local variables and eliminate them all. |
| 664 | for (std::unique_ptr<ProgramElement>& pe : program.ownedElements()) { |
| 665 | if (pe->is<FunctionDefinition>()) { |
| 666 | visitor.visitProgramElement(*pe); |
| 667 | } |
| 668 | } |
| 669 | break; |
| 670 | } |
| 671 | } |
| 672 | } |
| 673 | |
| 674 | return visitor.fMadeChanges; |
| 675 | } |
| 676 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 677 | bool Compiler::optimize(Program& program) { |
John Stiles | 7247b48 | 2021-03-08 10:40:35 -0500 | [diff] [blame] | 678 | // The optimizer only needs to run when it is enabled. |
| 679 | if (!program.fConfig->fSettings.fOptimize) { |
| 680 | return true; |
| 681 | } |
| 682 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 683 | SkASSERT(!fErrorCount); |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 684 | ProgramUsage* usage = program.fUsage.get(); |
John Stiles | 7954d6c | 2020-09-01 10:53:02 -0400 | [diff] [blame] | 685 | |
John Stiles | b666458 | 2021-03-19 09:46:00 -0400 | [diff] [blame^] | 686 | if (fErrorCount == 0) { |
| 687 | // Run the inliner only once; it is expensive! Multiple passes can occasionally shake out |
| 688 | // more wins, but it's diminishing returns. |
| 689 | fInliner.analyze(program.ownedElements(), program.fSymbols, usage); |
Ethan Nicholas | 34b19c5 | 2020-09-14 11:33:47 -0400 | [diff] [blame] | 690 | |
John Stiles | b666458 | 2021-03-19 09:46:00 -0400 | [diff] [blame^] | 691 | while (this->removeDeadFunctions(program, usage)) { |
| 692 | // Removing dead functions may cause more functions to become unreferenced. Try again. |
Ethan Nicholas | 34b19c5 | 2020-09-14 11:33:47 -0400 | [diff] [blame] | 693 | } |
John Stiles | b666458 | 2021-03-19 09:46:00 -0400 | [diff] [blame^] | 694 | while (this->removeDeadLocalVariables(program, usage)) { |
| 695 | // Removing dead variables may cause more variables to become unreferenced. Try again. |
| 696 | } |
| 697 | if (program.fConfig->fKind != ProgramKind::kFragmentProcessor) { |
| 698 | this->removeDeadGlobalVariables(program, usage); |
Ethan Nicholas | 0dc8087 | 2019-02-08 15:46:24 -0500 | [diff] [blame] | 699 | } |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 700 | } |
John Stiles | bb1505f | 2021-02-12 09:17:53 -0500 | [diff] [blame] | 701 | |
| 702 | if (fErrorCount == 0) { |
| 703 | this->verifyStaticTests(program); |
| 704 | } |
| 705 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 706 | return fErrorCount == 0; |
| 707 | } |
| 708 | |
Brian Osman | fb32ddf | 2019-06-18 10:14:20 -0400 | [diff] [blame] | 709 | #if defined(SKSL_STANDALONE) || SK_SUPPORT_GPU |
| 710 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 711 | bool Compiler::toSPIRV(Program& program, OutputStream& out) { |
Brian Osman | 7a20b5c | 2021-03-15 16:23:33 -0400 | [diff] [blame] | 712 | TRACE_EVENT0("skia.shaders", "SkSL::Compiler::toSPIRV"); |
Ethan Nicholas | a6ae1f7 | 2017-03-16 09:56:54 -0400 | [diff] [blame] | 713 | #ifdef SK_ENABLE_SPIRV_VALIDATION |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 714 | StringStream buffer; |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 715 | AutoSource as(this, program.fSource.get()); |
Brian Osman | 8b43dad | 2020-10-09 13:31:42 -0400 | [diff] [blame] | 716 | SPIRVCodeGenerator cg(fContext.get(), &program, this, &buffer); |
Ethan Nicholas | a6ae1f7 | 2017-03-16 09:56:54 -0400 | [diff] [blame] | 717 | bool result = cg.generateCode(); |
John Stiles | 270cec2 | 2021-02-17 12:59:36 -0500 | [diff] [blame] | 718 | if (result && program.fConfig->fSettings.fValidateSPIRV) { |
Ethan Nicholas | a6ae1f7 | 2017-03-16 09:56:54 -0400 | [diff] [blame] | 719 | spvtools::SpirvTools tools(SPV_ENV_VULKAN_1_0); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 720 | const String& data = buffer.str(); |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 721 | SkASSERT(0 == data.size() % 4); |
Brian Osman | 8d09d4a | 2020-11-24 15:51:06 -0500 | [diff] [blame] | 722 | String errors; |
| 723 | auto dumpmsg = [&errors](spv_message_level_t, const char*, const spv_position_t&, |
| 724 | const char* m) { |
| 725 | errors.appendf("SPIR-V validation error: %s\n", m); |
Ethan Nicholas | a6ae1f7 | 2017-03-16 09:56:54 -0400 | [diff] [blame] | 726 | }; |
| 727 | tools.SetMessageConsumer(dumpmsg); |
Brian Osman | 8d09d4a | 2020-11-24 15:51:06 -0500 | [diff] [blame] | 728 | |
| 729 | // Verify that the SPIR-V we produced is valid. At runtime, we will abort() with a message |
| 730 | // explaining the error. In standalone mode (skslc), we will send the message, plus the |
| 731 | // entire disassembled SPIR-V (for easier context & debugging) as *our* error message. |
| 732 | result = tools.Validate((const uint32_t*) data.c_str(), data.size() / 4); |
| 733 | |
| 734 | if (!result) { |
| 735 | #if defined(SKSL_STANDALONE) |
| 736 | // Convert the string-stream to a SPIR-V disassembly. |
| 737 | std::string disassembly; |
| 738 | if (tools.Disassemble((const uint32_t*)data.data(), data.size() / 4, &disassembly)) { |
| 739 | errors.append(disassembly); |
| 740 | } |
| 741 | this->error(-1, errors); |
| 742 | #else |
| 743 | SkDEBUGFAILF("%s", errors.c_str()); |
| 744 | #endif |
| 745 | } |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 746 | out.write(data.c_str(), data.size()); |
Ethan Nicholas | a6ae1f7 | 2017-03-16 09:56:54 -0400 | [diff] [blame] | 747 | } |
| 748 | #else |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 749 | AutoSource as(this, program.fSource.get()); |
Brian Osman | 8b43dad | 2020-10-09 13:31:42 -0400 | [diff] [blame] | 750 | SPIRVCodeGenerator cg(fContext.get(), &program, this, &out); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 751 | bool result = cg.generateCode(); |
Ethan Nicholas | a6ae1f7 | 2017-03-16 09:56:54 -0400 | [diff] [blame] | 752 | #endif |
Ethan Nicholas | ce33f10 | 2016-12-09 17:22:59 -0500 | [diff] [blame] | 753 | return result; |
| 754 | } |
| 755 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 756 | bool Compiler::toSPIRV(Program& program, String* out) { |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 757 | StringStream buffer; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 758 | bool result = this->toSPIRV(program, buffer); |
| 759 | if (result) { |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 760 | *out = buffer.str(); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 761 | } |
| 762 | return result; |
| 763 | } |
| 764 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 765 | bool Compiler::toGLSL(Program& program, OutputStream& out) { |
Brian Osman | 7a20b5c | 2021-03-15 16:23:33 -0400 | [diff] [blame] | 766 | TRACE_EVENT0("skia.shaders", "SkSL::Compiler::toGLSL"); |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 767 | AutoSource as(this, program.fSource.get()); |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 768 | GLSLCodeGenerator cg(fContext.get(), &program, this, &out); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 769 | bool result = cg.generateCode(); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 770 | return result; |
| 771 | } |
| 772 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 773 | bool Compiler::toGLSL(Program& program, String* out) { |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 774 | StringStream buffer; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 775 | bool result = this->toGLSL(program, buffer); |
| 776 | if (result) { |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 777 | *out = buffer.str(); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 778 | } |
| 779 | return result; |
| 780 | } |
| 781 | |
Brian Osman | c024391 | 2020-02-19 15:35:26 -0500 | [diff] [blame] | 782 | bool Compiler::toHLSL(Program& program, String* out) { |
| 783 | String spirv; |
| 784 | if (!this->toSPIRV(program, &spirv)) { |
| 785 | return false; |
| 786 | } |
| 787 | |
| 788 | return SPIRVtoHLSL(spirv, out); |
| 789 | } |
| 790 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 791 | bool Compiler::toMetal(Program& program, OutputStream& out) { |
Brian Osman | 7a20b5c | 2021-03-15 16:23:33 -0400 | [diff] [blame] | 792 | TRACE_EVENT0("skia.shaders", "SkSL::Compiler::toMetal"); |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 793 | MetalCodeGenerator cg(fContext.get(), &program, this, &out); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 794 | bool result = cg.generateCode(); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 795 | return result; |
| 796 | } |
| 797 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 798 | bool Compiler::toMetal(Program& program, String* out) { |
Timothy Liang | b8eeb80 | 2018-07-23 16:46:16 -0400 | [diff] [blame] | 799 | StringStream buffer; |
| 800 | bool result = this->toMetal(program, buffer); |
| 801 | if (result) { |
| 802 | *out = buffer.str(); |
| 803 | } |
| 804 | return result; |
| 805 | } |
| 806 | |
Greg Daniel | a28ea67 | 2020-09-25 11:12:56 -0400 | [diff] [blame] | 807 | #if defined(SKSL_STANDALONE) || GR_TEST_UTILS |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 808 | bool Compiler::toCPP(Program& program, String name, OutputStream& out) { |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 809 | AutoSource as(this, program.fSource.get()); |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 810 | CPPCodeGenerator cg(fContext.get(), &program, this, name, &out); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 811 | bool result = cg.generateCode(); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 812 | return result; |
| 813 | } |
| 814 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 815 | bool Compiler::toH(Program& program, String name, OutputStream& out) { |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 816 | AutoSource as(this, program.fSource.get()); |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 817 | HCodeGenerator cg(fContext.get(), &program, this, name, &out); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 818 | bool result = cg.generateCode(); |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 819 | return result; |
| 820 | } |
Greg Daniel | a28ea67 | 2020-09-25 11:12:56 -0400 | [diff] [blame] | 821 | #endif // defined(SKSL_STANDALONE) || GR_TEST_UTILS |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 822 | |
Ethan Nicholas | 2a479a5 | 2020-08-18 16:29:45 -0400 | [diff] [blame] | 823 | #endif // defined(SKSL_STANDALONE) || SK_SUPPORT_GPU |
Brian Osman | 2e29ab5 | 2019-09-20 12:19:11 -0400 | [diff] [blame] | 824 | |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 825 | Position Compiler::position(int offset) { |
Ethan Nicholas | 3c72989 | 2020-12-07 12:47:17 -0500 | [diff] [blame] | 826 | if (fSource && offset >= 0) { |
| 827 | int line = 1; |
| 828 | int column = 1; |
| 829 | for (int i = 0; i < offset; i++) { |
| 830 | if ((*fSource)[i] == '\n') { |
| 831 | ++line; |
| 832 | column = 1; |
| 833 | } |
| 834 | else { |
| 835 | ++column; |
| 836 | } |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 837 | } |
Ethan Nicholas | 3c72989 | 2020-12-07 12:47:17 -0500 | [diff] [blame] | 838 | return Position(line, column); |
| 839 | } else { |
| 840 | return Position(-1, -1); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 841 | } |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 842 | } |
| 843 | |
| 844 | void Compiler::error(int offset, String msg) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 845 | fErrorCount++; |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 846 | Position pos = this->position(offset); |
John Stiles | 8d3642e | 2021-01-22 09:50:04 -0500 | [diff] [blame] | 847 | fErrorTextLength.push_back(fErrorText.length()); |
Ethan Nicholas | 3c72989 | 2020-12-07 12:47:17 -0500 | [diff] [blame] | 848 | fErrorText += "error: " + (pos.fLine >= 1 ? to_string(pos.fLine) + ": " : "") + msg + "\n"; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 849 | } |
| 850 | |
John Stiles | 8d3642e | 2021-01-22 09:50:04 -0500 | [diff] [blame] | 851 | void Compiler::setErrorCount(int c) { |
| 852 | if (c < fErrorCount) { |
| 853 | fErrorText.resize(fErrorTextLength[c]); |
| 854 | fErrorTextLength.resize(c); |
| 855 | fErrorCount = c; |
| 856 | } |
| 857 | } |
| 858 | |
Ethan Nicholas | 9504614 | 2021-01-07 10:57:27 -0500 | [diff] [blame] | 859 | String Compiler::errorText(bool showCount) { |
| 860 | if (showCount) { |
| 861 | this->writeErrorCount(); |
| 862 | } |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 863 | fErrorCount = 0; |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 864 | String result = fErrorText; |
Ethan Nicholas | 9504614 | 2021-01-07 10:57:27 -0500 | [diff] [blame] | 865 | fErrorText = ""; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 866 | return result; |
| 867 | } |
| 868 | |
| 869 | void Compiler::writeErrorCount() { |
| 870 | if (fErrorCount) { |
| 871 | fErrorText += to_string(fErrorCount) + " error"; |
| 872 | if (fErrorCount > 1) { |
| 873 | fErrorText += "s"; |
| 874 | } |
| 875 | fErrorText += "\n"; |
| 876 | } |
| 877 | } |
| 878 | |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 879 | } // namespace SkSL |