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/SkSLCFGGenerator.h" |
| 17 | #include "src/sksl/SkSLCPPCodeGenerator.h" |
| 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 | 8ef4d6c | 2021-03-05 16:01:45 -0500 | [diff] [blame] | 79 | bool gSkSLControlFlowAnalysis = true; |
| 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); |
Brian Osman | e498b3c | 2020-09-23 14:42:11 -0400 | [diff] [blame] | 302 | std::unique_ptr<String> text = std::make_unique<String>(std::istreambuf_iterator<char>(in), |
| 303 | std::istreambuf_iterator<char>()); |
Ethan Nicholas | b33fa3f | 2020-08-06 13:00:19 -0400 | [diff] [blame] | 304 | if (in.rdstate()) { |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 305 | printf("error reading %s\n", data.fPath); |
Ethan Nicholas | b33fa3f | 2020-08-06 13:00:19 -0400 | [diff] [blame] | 306 | abort(); |
| 307 | } |
Brian Osman | e498b3c | 2020-09-23 14:42:11 -0400 | [diff] [blame] | 308 | const String* source = fRootSymbolTable->takeOwnershipOfString(std::move(text)); |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 309 | AutoSource as(this, source); |
John Stiles | d120464 | 2021-02-17 16:30:02 -0500 | [diff] [blame] | 310 | |
John Stiles | 881a10c | 2020-09-19 10:13:24 -0400 | [diff] [blame] | 311 | SkASSERT(fIRGenerator->fCanInline); |
| 312 | fIRGenerator->fCanInline = false; |
John Stiles | d120464 | 2021-02-17 16:30:02 -0500 | [diff] [blame] | 313 | |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 314 | ParsedModule baseModule = {base, /*fIntrinsics=*/nullptr}; |
John Stiles | d120464 | 2021-02-17 16:30:02 -0500 | [diff] [blame] | 315 | IRGenerator::IRBundle ir = fIRGenerator->convertProgram(baseModule, /*isBuiltinCode=*/true, |
| 316 | source->c_str(), source->length(), |
| 317 | /*externalFunctions=*/nullptr); |
Brian Osman | 133724c | 2020-10-28 14:14:39 -0400 | [diff] [blame] | 318 | SkASSERT(ir.fSharedElements.empty()); |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 319 | LoadedModule module = { kind, std::move(ir.fSymbolTable), std::move(ir.fElements) }; |
John Stiles | 881a10c | 2020-09-19 10:13:24 -0400 | [diff] [blame] | 320 | fIRGenerator->fCanInline = true; |
Ethan Nicholas | 8da1e65 | 2019-05-24 11:01:59 -0400 | [diff] [blame] | 321 | if (this->fErrorCount) { |
| 322 | printf("Unexpected errors: %s\n", this->fErrorText.c_str()); |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 323 | SkDEBUGFAILF("%s %s\n", data.fPath, this->fErrorText.c_str()); |
Ethan Nicholas | 8da1e65 | 2019-05-24 11:01:59 -0400 | [diff] [blame] | 324 | } |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 325 | fModifiers.push_back(std::move(ir.fModifiers)); |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 326 | #else |
| 327 | SkASSERT(data.fData && (data.fSize != 0)); |
| 328 | Rehydrator rehydrator(fContext.get(), fIRGenerator->fModifiers.get(), base, this, |
| 329 | data.fData, data.fSize); |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 330 | LoadedModule module = { kind, rehydrator.symbolTable(), rehydrator.elements() }; |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 331 | fModifiers.push_back(fIRGenerator->releaseModifiers()); |
| 332 | #endif |
| 333 | |
| 334 | return module; |
| 335 | } |
| 336 | |
John Stiles | dbd4e6f | 2021-02-16 13:29:15 -0500 | [diff] [blame] | 337 | ParsedModule Compiler::parseModule(ProgramKind kind, ModuleData data, const ParsedModule& base) { |
John Stiles | a935c3f | 2021-02-25 10:35:49 -0500 | [diff] [blame] | 338 | LoadedModule module = this->loadModule(kind, data, base.fSymbols, /*dehydrate=*/false); |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 339 | this->optimize(module); |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 340 | |
| 341 | // For modules that just declare (but don't define) intrinsic functions, there will be no new |
| 342 | // 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] | 343 | if (module.fElements.empty()) { |
| 344 | return {module.fSymbols, base.fIntrinsics}; |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | auto intrinsics = std::make_shared<IRIntrinsicMap>(base.fIntrinsics.get()); |
| 348 | |
| 349 | // Now, transfer all of the program elements to an intrinsic map. This maps certain types of |
| 350 | // global objects to the declaring ProgramElement. |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 351 | for (std::unique_ptr<ProgramElement>& element : module.fElements) { |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 352 | switch (element->kind()) { |
| 353 | case ProgramElement::Kind::kFunction: { |
| 354 | const FunctionDefinition& f = element->as<FunctionDefinition>(); |
Ethan Nicholas | 0a5d096 | 2020-10-14 13:33:18 -0400 | [diff] [blame] | 355 | SkASSERT(f.declaration().isBuiltin()); |
| 356 | intrinsics->insertOrDie(f.declaration().description(), std::move(element)); |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 357 | break; |
| 358 | } |
John Stiles | 569249b | 2020-11-03 12:18:22 -0500 | [diff] [blame] | 359 | case ProgramElement::Kind::kFunctionPrototype: { |
| 360 | // These are already in the symbol table. |
| 361 | break; |
| 362 | } |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 363 | case ProgramElement::Kind::kEnum: { |
| 364 | const Enum& e = element->as<Enum>(); |
| 365 | SkASSERT(e.isBuiltin()); |
| 366 | intrinsics->insertOrDie(e.typeName(), std::move(element)); |
| 367 | break; |
| 368 | } |
| 369 | case ProgramElement::Kind::kGlobalVar: { |
Ethan Nicholas | c51f33e | 2020-10-13 13:49:44 -0400 | [diff] [blame] | 370 | const GlobalVarDeclaration& global = element->as<GlobalVarDeclaration>(); |
| 371 | const Variable& var = global.declaration()->as<VarDeclaration>().var(); |
| 372 | SkASSERT(var.isBuiltin()); |
| 373 | intrinsics->insertOrDie(var.name(), std::move(element)); |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 374 | break; |
| 375 | } |
| 376 | case ProgramElement::Kind::kInterfaceBlock: { |
Ethan Nicholas | eaf4788 | 2020-10-15 10:10:08 -0400 | [diff] [blame] | 377 | const Variable& var = element->as<InterfaceBlock>().variable(); |
| 378 | SkASSERT(var.isBuiltin()); |
| 379 | intrinsics->insertOrDie(var.name(), std::move(element)); |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 380 | break; |
| 381 | } |
| 382 | default: |
| 383 | printf("Unsupported element: %s\n", element->description().c_str()); |
| 384 | SkASSERT(false); |
| 385 | break; |
| 386 | } |
| 387 | } |
| 388 | |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 389 | return {module.fSymbols, std::move(intrinsics)}; |
Ethan Nicholas | 8da1e65 | 2019-05-24 11:01:59 -0400 | [diff] [blame] | 390 | } |
| 391 | |
John Stiles | e615000 | 2020-10-05 12:03:53 -0400 | [diff] [blame] | 392 | void Compiler::scanCFG(CFG* cfg, BlockId blockId, SkBitSet* processedSet) { |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 393 | BasicBlock& block = cfg->fBlocks[blockId]; |
| 394 | |
| 395 | // compute definitions after this block |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 396 | DefinitionMap after = block.fBefore; |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 397 | for (const BasicBlock::Node& n : block.fNodes) { |
John Stiles | e8a2492 | 2021-02-08 17:54:08 -0500 | [diff] [blame] | 398 | after.addDefinitions(*fContext, n); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | // propagate definitions to exits |
| 402 | for (BlockId exitId : block.fExits) { |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 403 | if (exitId == blockId) { |
| 404 | continue; |
| 405 | } |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 406 | BasicBlock& exit = cfg->fBlocks[exitId]; |
John Stiles | 65b4827 | 2020-12-22 17:18:34 -0500 | [diff] [blame] | 407 | for (const auto& [var, e1] : after) { |
John Stiles | 796cdb7 | 2020-10-08 12:06:53 -0400 | [diff] [blame] | 408 | std::unique_ptr<Expression>** exitDef = exit.fBefore.find(var); |
| 409 | if (!exitDef) { |
John Stiles | e615000 | 2020-10-05 12:03:53 -0400 | [diff] [blame] | 410 | // exit has no definition for it, just copy it and reprocess exit block |
| 411 | processedSet->reset(exitId); |
John Stiles | e8a2492 | 2021-02-08 17:54:08 -0500 | [diff] [blame] | 412 | exit.fBefore.set(var, e1); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 413 | } else { |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 414 | // exit has a (possibly different) value already defined |
John Stiles | 796cdb7 | 2020-10-08 12:06:53 -0400 | [diff] [blame] | 415 | std::unique_ptr<Expression>* e2 = *exitDef; |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 416 | if (e1 != e2) { |
John Stiles | e615000 | 2020-10-05 12:03:53 -0400 | [diff] [blame] | 417 | // definition has changed, merge and reprocess the exit block |
| 418 | processedSet->reset(exitId); |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 419 | if (e1 && e2) { |
John Stiles | 796cdb7 | 2020-10-08 12:06:53 -0400 | [diff] [blame] | 420 | *exitDef = (std::unique_ptr<Expression>*)&fContext->fDefined_Expression; |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 421 | } else { |
John Stiles | 796cdb7 | 2020-10-08 12:06:53 -0400 | [diff] [blame] | 422 | *exitDef = nullptr; |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 423 | } |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 424 | } |
| 425 | } |
John Stiles | 65b4827 | 2020-12-22 17:18:34 -0500 | [diff] [blame] | 426 | } |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 427 | } |
| 428 | } |
| 429 | |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 430 | /** |
| 431 | * Returns true if assigning to this lvalue has no effect. |
| 432 | */ |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 433 | static bool is_dead(const Expression& lvalue, ProgramUsage* usage) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 434 | switch (lvalue.kind()) { |
| 435 | case Expression::Kind::kVariableReference: |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 436 | return usage->isDead(*lvalue.as<VariableReference>().variable()); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 437 | case Expression::Kind::kSwizzle: |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 438 | return is_dead(*lvalue.as<Swizzle>().base(), usage); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 439 | case Expression::Kind::kFieldAccess: |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 440 | return is_dead(*lvalue.as<FieldAccess>().base(), usage); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 441 | case Expression::Kind::kIndex: { |
John Stiles | a5a97b4 | 2020-08-18 11:19:07 -0400 | [diff] [blame] | 442 | const IndexExpression& idx = lvalue.as<IndexExpression>(); |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 443 | return is_dead(*idx.base(), usage) && |
Ethan Nicholas | 2a4952d | 2020-10-08 15:35:56 -0400 | [diff] [blame] | 444 | !idx.index()->hasProperty(Expression::Property::kSideEffects); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 445 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 446 | case Expression::Kind::kTernary: { |
John Stiles | a5a97b4 | 2020-08-18 11:19:07 -0400 | [diff] [blame] | 447 | const TernaryExpression& t = lvalue.as<TernaryExpression>(); |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 448 | return !t.test()->hasSideEffects() && |
| 449 | is_dead(*t.ifTrue(), usage) && |
| 450 | is_dead(*t.ifFalse(), usage); |
Ethan Nicholas | a583b81 | 2018-01-18 13:32:11 -0500 | [diff] [blame] | 451 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 452 | default: |
John Stiles | eada7bc | 2021-02-02 16:29:32 -0500 | [diff] [blame] | 453 | SkDEBUGFAILF("invalid lvalue: %s\n", lvalue.description().c_str()); |
Ethan Nicholas | 2a099da | 2020-01-02 14:40:54 -0500 | [diff] [blame] | 454 | return false; |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 455 | } |
| 456 | } |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 457 | |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 458 | /** |
| 459 | * Returns true if this is an assignment which can be collapsed down to just the right hand side due |
| 460 | * to a dead target and lack of side effects on the left hand side. |
| 461 | */ |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 462 | static bool dead_assignment(const BinaryExpression& b, ProgramUsage* usage) { |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 463 | if (!b.getOperator().isAssignment()) { |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 464 | return false; |
| 465 | } |
John Stiles | 2d4f959 | 2020-10-30 10:29:12 -0400 | [diff] [blame] | 466 | return is_dead(*b.left(), usage); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 467 | } |
| 468 | |
John Stiles | 0ac6c15 | 2021-02-10 14:04:24 -0500 | [diff] [blame] | 469 | static bool self_assignment(const BinaryExpression& b) { |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 470 | return b.getOperator().kind() == Token::Kind::TK_EQ && |
John Stiles | 95d0bad | 2021-03-01 17:02:28 -0500 | [diff] [blame] | 471 | Analysis::IsSelfAssignment(*b.left(), *b.right()); |
John Stiles | 0ac6c15 | 2021-02-10 14:04:24 -0500 | [diff] [blame] | 472 | } |
| 473 | |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 474 | void Compiler::computeDataFlow(CFG* cfg) { |
John Stiles | e8a2492 | 2021-02-08 17:54:08 -0500 | [diff] [blame] | 475 | cfg->fBlocks[cfg->fStart].fBefore.computeStartState(*cfg); |
John Stiles | e615000 | 2020-10-05 12:03:53 -0400 | [diff] [blame] | 476 | |
| 477 | // We set bits in the "processed" set after a block has been scanned. |
| 478 | SkBitSet processedSet(cfg->fBlocks.size()); |
| 479 | while (SkBitSet::OptionalIndex blockId = processedSet.findFirstUnset()) { |
| 480 | processedSet.set(*blockId); |
| 481 | this->scanCFG(cfg, *blockId, &processedSet); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 482 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | /** |
| 486 | * Attempts to replace the expression pointed to by iter with a new one (in both the CFG and the |
| 487 | * IR). If the expression can be cleanly removed, returns true and updates the iterator to point to |
| 488 | * the newly-inserted element. Otherwise updates only the IR and returns false (and the CFG will |
| 489 | * need to be regenerated). |
| 490 | */ |
John Stiles | afbf899 | 2020-08-18 10:08:21 -0400 | [diff] [blame] | 491 | static bool try_replace_expression(BasicBlock* b, |
| 492 | std::vector<BasicBlock::Node>::iterator* iter, |
| 493 | std::unique_ptr<Expression>* newExpression) { |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 494 | std::unique_ptr<Expression>* target = (*iter)->expression(); |
| 495 | if (!b->tryRemoveExpression(iter)) { |
| 496 | *target = std::move(*newExpression); |
| 497 | return false; |
| 498 | } |
| 499 | *target = std::move(*newExpression); |
| 500 | return b->tryInsertExpression(iter, target); |
| 501 | } |
| 502 | |
| 503 | /** |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 504 | * Returns true if the expression is a constant numeric literal with the specified value, or a |
| 505 | * constant vector with all elements equal to the specified value. |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 506 | */ |
Ethan Nicholas | a3f22f1 | 2020-10-01 12:13:17 -0400 | [diff] [blame] | 507 | template <typename T = SKSL_FLOAT> |
John Stiles | 9d94423 | 2020-08-19 09:56:49 -0400 | [diff] [blame] | 508 | static bool is_constant(const Expression& expr, T value) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 509 | switch (expr.kind()) { |
| 510 | case Expression::Kind::kIntLiteral: |
Ethan Nicholas | e96cdd1 | 2020-09-28 16:27:18 -0400 | [diff] [blame] | 511 | return expr.as<IntLiteral>().value() == value; |
John Stiles | 9d94423 | 2020-08-19 09:56:49 -0400 | [diff] [blame] | 512 | |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 513 | case Expression::Kind::kFloatLiteral: |
Ethan Nicholas | a3f22f1 | 2020-10-01 12:13:17 -0400 | [diff] [blame] | 514 | return expr.as<FloatLiteral>().value() == value; |
John Stiles | 9d94423 | 2020-08-19 09:56:49 -0400 | [diff] [blame] | 515 | |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 516 | case Expression::Kind::kConstructor: { |
John Stiles | 9d94423 | 2020-08-19 09:56:49 -0400 | [diff] [blame] | 517 | const Constructor& constructor = expr.as<Constructor>(); |
| 518 | if (constructor.isCompileTimeConstant()) { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 519 | const Type& constructorType = constructor.type(); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 520 | switch (constructorType.typeKind()) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 521 | case Type::TypeKind::kVector: |
John Stiles | bc75ebb | 2020-11-24 12:04:47 -0500 | [diff] [blame] | 522 | if (constructor.componentType().isFloat()) { |
| 523 | for (int i = 0; i < constructorType.columns(); ++i) { |
John Stiles | 9d94423 | 2020-08-19 09:56:49 -0400 | [diff] [blame] | 524 | if (constructor.getFVecComponent(i) != value) { |
| 525 | return false; |
| 526 | } |
John Stiles | bc75ebb | 2020-11-24 12:04:47 -0500 | [diff] [blame] | 527 | } |
| 528 | return true; |
| 529 | } else if (constructor.componentType().isInteger()) { |
| 530 | for (int i = 0; i < constructorType.columns(); ++i) { |
John Stiles | 9d94423 | 2020-08-19 09:56:49 -0400 | [diff] [blame] | 531 | if (constructor.getIVecComponent(i) != value) { |
| 532 | return false; |
| 533 | } |
| 534 | } |
John Stiles | bc75ebb | 2020-11-24 12:04:47 -0500 | [diff] [blame] | 535 | return true; |
Ethan Nicholas | d188c18 | 2019-06-10 15:55:38 -0400 | [diff] [blame] | 536 | } |
John Stiles | bc75ebb | 2020-11-24 12:04:47 -0500 | [diff] [blame] | 537 | // Other types (e.g. boolean) might occur, but aren't supported here. |
| 538 | return false; |
John Stiles | 9d94423 | 2020-08-19 09:56:49 -0400 | [diff] [blame] | 539 | |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 540 | case Type::TypeKind::kScalar: |
Ethan Nicholas | f70f044 | 2020-09-29 12:41:35 -0400 | [diff] [blame] | 541 | SkASSERT(constructor.arguments().size() == 1); |
| 542 | return is_constant<T>(*constructor.arguments()[0], value); |
John Stiles | 9d94423 | 2020-08-19 09:56:49 -0400 | [diff] [blame] | 543 | |
| 544 | default: |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 545 | return false; |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 546 | } |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 547 | } |
| 548 | return false; |
| 549 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 550 | default: |
| 551 | return false; |
| 552 | } |
| 553 | } |
| 554 | |
| 555 | /** |
| 556 | * Collapses the binary expression pointed to by iter down to just the right side (in both the IR |
| 557 | * and CFG structures). |
| 558 | */ |
John Stiles | afbf899 | 2020-08-18 10:08:21 -0400 | [diff] [blame] | 559 | static void delete_left(BasicBlock* b, |
| 560 | std::vector<BasicBlock::Node>::iterator* iter, |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 561 | Compiler::OptimizationContext* optimizationContext) { |
| 562 | optimizationContext->fUpdated = true; |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 563 | std::unique_ptr<Expression>* target = (*iter)->expression(); |
John Stiles | a5a97b4 | 2020-08-18 11:19:07 -0400 | [diff] [blame] | 564 | BinaryExpression& bin = (*target)->as<BinaryExpression>(); |
John Stiles | 2d4f959 | 2020-10-30 10:29:12 -0400 | [diff] [blame] | 565 | Expression& left = *bin.left(); |
| 566 | std::unique_ptr<Expression>& rightPointer = bin.right(); |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 567 | SkASSERT(!left.hasSideEffects()); |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 568 | bool result; |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 569 | if (bin.getOperator().kind() == Token::Kind::TK_EQ) { |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 570 | result = b->tryRemoveLValueBefore(iter, &left); |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 571 | } else { |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 572 | result = b->tryRemoveExpressionBefore(iter, &left); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 573 | } |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 574 | // Remove references within LHS. |
| 575 | optimizationContext->fUsage->remove(&left); |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 576 | *target = std::move(rightPointer); |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 577 | if (!result) { |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 578 | optimizationContext->fNeedsRescan = true; |
Ethan Nicholas | 4b330df | 2017-05-17 10:52:55 -0400 | [diff] [blame] | 579 | return; |
| 580 | } |
| 581 | if (*iter == b->fNodes.begin()) { |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 582 | optimizationContext->fNeedsRescan = true; |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 583 | return; |
| 584 | } |
| 585 | --(*iter); |
John Stiles | 70025e5 | 2020-09-28 16:08:58 -0400 | [diff] [blame] | 586 | if (!(*iter)->isExpression() || (*iter)->expression() != &rightPointer) { |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 587 | optimizationContext->fNeedsRescan = true; |
Ethan Nicholas | 4b330df | 2017-05-17 10:52:55 -0400 | [diff] [blame] | 588 | return; |
| 589 | } |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 590 | *iter = b->fNodes.erase(*iter); |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 591 | SkASSERT((*iter)->expression() == target); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | /** |
| 595 | * Collapses the binary expression pointed to by iter down to just the left side (in both the IR and |
| 596 | * CFG structures). |
| 597 | */ |
John Stiles | afbf899 | 2020-08-18 10:08:21 -0400 | [diff] [blame] | 598 | static void delete_right(BasicBlock* b, |
| 599 | std::vector<BasicBlock::Node>::iterator* iter, |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 600 | Compiler::OptimizationContext* optimizationContext) { |
| 601 | optimizationContext->fUpdated = true; |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 602 | std::unique_ptr<Expression>* target = (*iter)->expression(); |
John Stiles | a5a97b4 | 2020-08-18 11:19:07 -0400 | [diff] [blame] | 603 | BinaryExpression& bin = (*target)->as<BinaryExpression>(); |
John Stiles | 2d4f959 | 2020-10-30 10:29:12 -0400 | [diff] [blame] | 604 | std::unique_ptr<Expression>& leftPointer = bin.left(); |
| 605 | Expression& right = *bin.right(); |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 606 | SkASSERT(!right.hasSideEffects()); |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 607 | // Remove references within RHS. |
| 608 | optimizationContext->fUsage->remove(&right); |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 609 | if (!b->tryRemoveExpressionBefore(iter, &right)) { |
| 610 | *target = std::move(leftPointer); |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 611 | optimizationContext->fNeedsRescan = true; |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 612 | return; |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 613 | } |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 614 | *target = std::move(leftPointer); |
Ethan Nicholas | 4b330df | 2017-05-17 10:52:55 -0400 | [diff] [blame] | 615 | if (*iter == b->fNodes.begin()) { |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 616 | optimizationContext->fNeedsRescan = true; |
Ethan Nicholas | 4b330df | 2017-05-17 10:52:55 -0400 | [diff] [blame] | 617 | return; |
| 618 | } |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 619 | --(*iter); |
John Stiles | 70025e5 | 2020-09-28 16:08:58 -0400 | [diff] [blame] | 620 | if ((!(*iter)->isExpression() || (*iter)->expression() != &leftPointer)) { |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 621 | optimizationContext->fNeedsRescan = true; |
Ethan Nicholas | 4b330df | 2017-05-17 10:52:55 -0400 | [diff] [blame] | 622 | return; |
| 623 | } |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 624 | *iter = b->fNodes.erase(*iter); |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 625 | SkASSERT((*iter)->expression() == target); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 626 | } |
| 627 | |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 628 | /** |
| 629 | * Constructs the specified type using a single argument. |
| 630 | */ |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 631 | static std::unique_ptr<Expression> construct(const Type* type, std::unique_ptr<Expression> v) { |
John Stiles | 8e3b6be | 2020-10-13 11:14:08 -0400 | [diff] [blame] | 632 | ExpressionArray args; |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 633 | args.push_back(std::move(v)); |
John Stiles | 54f0049 | 2021-02-19 11:46:10 -0500 | [diff] [blame] | 634 | return std::make_unique<Constructor>(/*offset=*/-1, *type, std::move(args)); |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | /** |
| 638 | * Used in the implementations of vectorize_left and vectorize_right. Given a vector type and an |
| 639 | * expression x, deletes the expression pointed to by iter and replaces it with <type>(x). |
| 640 | */ |
| 641 | static void vectorize(BasicBlock* b, |
| 642 | std::vector<BasicBlock::Node>::iterator* iter, |
| 643 | const Type& type, |
| 644 | std::unique_ptr<Expression>* otherExpression, |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 645 | Compiler::OptimizationContext* optimizationContext) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 646 | SkASSERT((*(*iter)->expression())->kind() == Expression::Kind::kBinary); |
John Stiles | 9aeed13 | 2020-11-24 17:36:06 -0500 | [diff] [blame] | 647 | SkASSERT(type.isVector()); |
| 648 | SkASSERT((*otherExpression)->type().isScalar()); |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 649 | optimizationContext->fUpdated = true; |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 650 | std::unique_ptr<Expression>* target = (*iter)->expression(); |
| 651 | if (!b->tryRemoveExpression(iter)) { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 652 | *target = construct(&type, std::move(*otherExpression)); |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 653 | optimizationContext->fNeedsRescan = true; |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 654 | } else { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 655 | *target = construct(&type, std::move(*otherExpression)); |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 656 | if (!b->tryInsertExpression(iter, target)) { |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 657 | optimizationContext->fNeedsRescan = true; |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 658 | } |
| 659 | } |
| 660 | } |
| 661 | |
| 662 | /** |
| 663 | * Given a binary expression of the form x <op> vec<n>(y), deletes the right side and vectorizes the |
| 664 | * left to yield vec<n>(x). |
| 665 | */ |
| 666 | static void vectorize_left(BasicBlock* b, |
| 667 | std::vector<BasicBlock::Node>::iterator* iter, |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 668 | Compiler::OptimizationContext* optimizationContext) { |
John Stiles | a5a97b4 | 2020-08-18 11:19:07 -0400 | [diff] [blame] | 669 | BinaryExpression& bin = (*(*iter)->expression())->as<BinaryExpression>(); |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 670 | // Remove references within RHS. Vectorization of LHS doesn't change reference counts. |
John Stiles | 2d4f959 | 2020-10-30 10:29:12 -0400 | [diff] [blame] | 671 | optimizationContext->fUsage->remove(bin.right().get()); |
| 672 | vectorize(b, iter, bin.right()->type(), &bin.left(), optimizationContext); |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 673 | } |
| 674 | |
| 675 | /** |
| 676 | * Given a binary expression of the form vec<n>(x) <op> y, deletes the left side and vectorizes the |
| 677 | * right to yield vec<n>(y). |
| 678 | */ |
| 679 | static void vectorize_right(BasicBlock* b, |
| 680 | std::vector<BasicBlock::Node>::iterator* iter, |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 681 | Compiler::OptimizationContext* optimizationContext) { |
John Stiles | a5a97b4 | 2020-08-18 11:19:07 -0400 | [diff] [blame] | 682 | BinaryExpression& bin = (*(*iter)->expression())->as<BinaryExpression>(); |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 683 | // Remove references within LHS. Vectorization of RHS doesn't change reference counts. |
John Stiles | 2d4f959 | 2020-10-30 10:29:12 -0400 | [diff] [blame] | 684 | optimizationContext->fUsage->remove(bin.left().get()); |
| 685 | vectorize(b, iter, bin.left()->type(), &bin.right(), optimizationContext); |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 686 | } |
| 687 | |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 688 | void Compiler::simplifyExpression(DefinitionMap& definitions, |
| 689 | BasicBlock& b, |
| 690 | std::vector<BasicBlock::Node>::iterator* iter, |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 691 | OptimizationContext* optimizationContext) { |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 692 | Expression* expr = (*iter)->expression()->get(); |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 693 | SkASSERT(expr); |
John Stiles | 108bbe2 | 2020-11-18 11:10:38 -0500 | [diff] [blame] | 694 | |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 695 | if ((*iter)->fConstantPropagation) { |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 696 | std::unique_ptr<Expression> optimized = expr->constantPropagate(*fIRGenerator, |
| 697 | definitions); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 698 | if (optimized) { |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 699 | optimizationContext->fUpdated = true; |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 700 | optimized = fIRGenerator->coerce(std::move(optimized), expr->type()); |
Ethan Nicholas | 1d88152 | 2020-09-11 09:32:54 -0400 | [diff] [blame] | 701 | SkASSERT(optimized); |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 702 | // Remove references within 'expr', add references within 'optimized' |
| 703 | optimizationContext->fUsage->replace(expr, optimized.get()); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 704 | if (!try_replace_expression(&b, iter, &optimized)) { |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 705 | optimizationContext->fNeedsRescan = true; |
Ethan Nicholas | 4b330df | 2017-05-17 10:52:55 -0400 | [diff] [blame] | 706 | return; |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 707 | } |
John Stiles | 70025e5 | 2020-09-28 16:08:58 -0400 | [diff] [blame] | 708 | SkASSERT((*iter)->isExpression()); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 709 | expr = (*iter)->expression()->get(); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 710 | } |
| 711 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 712 | switch (expr->kind()) { |
| 713 | case Expression::Kind::kVariableReference: { |
John Stiles | a5a97b4 | 2020-08-18 11:19:07 -0400 | [diff] [blame] | 714 | const VariableReference& ref = expr->as<VariableReference>(); |
Ethan Nicholas | 7868692 | 2020-10-08 06:46:27 -0400 | [diff] [blame] | 715 | const Variable* var = ref.variable(); |
John Stiles | 7247b48 | 2021-03-08 10:40:35 -0500 | [diff] [blame^] | 716 | if (fContext->fConfig->fSettings.fDeadCodeElimination && |
John Stiles | 66c53b9 | 2021-02-20 08:00:43 -0500 | [diff] [blame] | 717 | ref.refKind() != VariableReference::RefKind::kWrite && |
Ethan Nicholas | 453f67f | 2020-10-09 10:43:45 -0400 | [diff] [blame] | 718 | ref.refKind() != VariableReference::RefKind::kPointer && |
John Stiles | e8a2492 | 2021-02-08 17:54:08 -0500 | [diff] [blame] | 719 | var->storage() == Variable::Storage::kLocal && !definitions.get(var) && |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 720 | optimizationContext->fSilences.find(var) == optimizationContext->fSilences.end()) { |
| 721 | optimizationContext->fSilences.insert(var); |
Ethan Nicholas | 82a62d2 | 2017-11-07 14:42:10 +0000 | [diff] [blame] | 722 | this->error(expr->fOffset, |
Ethan Nicholas | e2c4999 | 2020-10-05 11:49:11 -0400 | [diff] [blame] | 723 | "'" + var->name() + "' has not been assigned"); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 724 | } |
| 725 | break; |
| 726 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 727 | case Expression::Kind::kTernary: { |
John Stiles | 28054ad | 2021-03-05 15:22:48 -0500 | [diff] [blame] | 728 | // TODO(skia:11319): this optimization logic is redundant with the optimization code |
| 729 | // found in SkSLTernaryExpression.cpp. |
| 730 | |
John Stiles | 403a363 | 2020-08-20 12:11:48 -0400 | [diff] [blame] | 731 | TernaryExpression* t = &expr->as<TernaryExpression>(); |
Ethan Nicholas | dd21816 | 2020-10-08 05:48:01 -0400 | [diff] [blame] | 732 | if (t->test()->is<BoolLiteral>()) { |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 733 | // ternary has a constant test, replace it with either the true or |
| 734 | // false branch |
Ethan Nicholas | dd21816 | 2020-10-08 05:48:01 -0400 | [diff] [blame] | 735 | if (t->test()->as<BoolLiteral>().value()) { |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 736 | (*iter)->setExpression(std::move(t->ifTrue()), optimizationContext->fUsage); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 737 | } else { |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 738 | (*iter)->setExpression(std::move(t->ifFalse()), optimizationContext->fUsage); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 739 | } |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 740 | optimizationContext->fUpdated = true; |
| 741 | optimizationContext->fNeedsRescan = true; |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 742 | } |
| 743 | break; |
| 744 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 745 | case Expression::Kind::kBinary: { |
John Stiles | 403a363 | 2020-08-20 12:11:48 -0400 | [diff] [blame] | 746 | BinaryExpression* bin = &expr->as<BinaryExpression>(); |
John Stiles | 0ac6c15 | 2021-02-10 14:04:24 -0500 | [diff] [blame] | 747 | if (dead_assignment(*bin, optimizationContext->fUsage) || self_assignment(*bin)) { |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 748 | delete_left(&b, iter, optimizationContext); |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 749 | break; |
| 750 | } |
John Stiles | 8f440b4 | 2021-03-05 16:48:56 -0500 | [diff] [blame] | 751 | |
| 752 | // TODO(skia:11319): this optimization logic is redundant with the optimization code |
| 753 | // found in ConstantFolder.cpp. |
John Stiles | 2d4f959 | 2020-10-30 10:29:12 -0400 | [diff] [blame] | 754 | Expression& left = *bin->left(); |
| 755 | Expression& right = *bin->right(); |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 756 | const Type& leftType = left.type(); |
| 757 | const Type& rightType = right.type(); |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 758 | // collapse useless expressions like x * 1 or x + 0 |
John Stiles | 9aeed13 | 2020-11-24 17:36:06 -0500 | [diff] [blame] | 759 | if ((!leftType.isScalar() && !leftType.isVector()) || |
| 760 | (!rightType.isScalar() && !rightType.isVector())) { |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 761 | break; |
| 762 | } |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 763 | switch (bin->getOperator().kind()) { |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 764 | case Token::Kind::TK_STAR: |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 765 | if (is_constant(left, 1)) { |
John Stiles | 9aeed13 | 2020-11-24 17:36:06 -0500 | [diff] [blame] | 766 | if (leftType.isVector() && rightType.isScalar()) { |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 767 | // float4(1) * x -> float4(x) |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 768 | vectorize_right(&b, iter, optimizationContext); |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 769 | } else { |
| 770 | // 1 * x -> x |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 771 | // 1 * float4(x) -> float4(x) |
| 772 | // float4(1) * float4(x) -> float4(x) |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 773 | delete_left(&b, iter, optimizationContext); |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 774 | } |
| 775 | } |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 776 | else if (is_constant(left, 0)) { |
John Stiles | 9aeed13 | 2020-11-24 17:36:06 -0500 | [diff] [blame] | 777 | if (leftType.isScalar() && rightType.isVector() && |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 778 | !right.hasSideEffects()) { |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 779 | // 0 * float4(x) -> float4(0) |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 780 | vectorize_left(&b, iter, optimizationContext); |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 781 | } else { |
| 782 | // 0 * x -> 0 |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 783 | // float4(0) * x -> float4(0) |
| 784 | // float4(0) * float4(x) -> float4(0) |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 785 | if (!right.hasSideEffects()) { |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 786 | delete_right(&b, iter, optimizationContext); |
Ethan Nicholas | 51493ee | 2017-12-11 12:34:33 -0500 | [diff] [blame] | 787 | } |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 788 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 789 | } |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 790 | else if (is_constant(right, 1)) { |
John Stiles | 9aeed13 | 2020-11-24 17:36:06 -0500 | [diff] [blame] | 791 | if (leftType.isScalar() && rightType.isVector()) { |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 792 | // x * float4(1) -> float4(x) |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 793 | vectorize_left(&b, iter, optimizationContext); |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 794 | } else { |
| 795 | // x * 1 -> x |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 796 | // float4(x) * 1 -> float4(x) |
| 797 | // float4(x) * float4(1) -> float4(x) |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 798 | delete_right(&b, iter, optimizationContext); |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 799 | } |
| 800 | } |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 801 | else if (is_constant(right, 0)) { |
John Stiles | 9aeed13 | 2020-11-24 17:36:06 -0500 | [diff] [blame] | 802 | if (leftType.isVector() && rightType.isScalar() && !left.hasSideEffects()) { |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 803 | // float4(x) * 0 -> float4(0) |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 804 | vectorize_right(&b, iter, optimizationContext); |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 805 | } else { |
| 806 | // x * 0 -> 0 |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 807 | // x * float4(0) -> float4(0) |
| 808 | // float4(x) * float4(0) -> float4(0) |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 809 | if (!left.hasSideEffects()) { |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 810 | delete_left(&b, iter, optimizationContext); |
Ethan Nicholas | 51493ee | 2017-12-11 12:34:33 -0500 | [diff] [blame] | 811 | } |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 812 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 813 | } |
| 814 | break; |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 815 | case Token::Kind::TK_PLUS: |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 816 | if (is_constant(left, 0)) { |
John Stiles | 9aeed13 | 2020-11-24 17:36:06 -0500 | [diff] [blame] | 817 | if (leftType.isVector() && rightType.isScalar()) { |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 818 | // float4(0) + x -> float4(x) |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 819 | vectorize_right(&b, iter, optimizationContext); |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 820 | } else { |
| 821 | // 0 + x -> x |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 822 | // 0 + float4(x) -> float4(x) |
| 823 | // float4(0) + float4(x) -> float4(x) |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 824 | delete_left(&b, iter, optimizationContext); |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 825 | } |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 826 | } else if (is_constant(right, 0)) { |
John Stiles | 9aeed13 | 2020-11-24 17:36:06 -0500 | [diff] [blame] | 827 | if (leftType.isScalar() && rightType.isVector()) { |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 828 | // x + float4(0) -> float4(x) |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 829 | vectorize_left(&b, iter, optimizationContext); |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 830 | } else { |
| 831 | // x + 0 -> x |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 832 | // float4(x) + 0 -> float4(x) |
| 833 | // float4(x) + float4(0) -> float4(x) |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 834 | delete_right(&b, iter, optimizationContext); |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 835 | } |
Ethan Nicholas | 56e4271 | 2017-04-21 10:23:37 -0400 | [diff] [blame] | 836 | } |
| 837 | break; |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 838 | case Token::Kind::TK_MINUS: |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 839 | if (is_constant(right, 0)) { |
John Stiles | 9aeed13 | 2020-11-24 17:36:06 -0500 | [diff] [blame] | 840 | if (leftType.isScalar() && rightType.isVector()) { |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 841 | // x - float4(0) -> float4(x) |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 842 | vectorize_left(&b, iter, optimizationContext); |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 843 | } else { |
| 844 | // x - 0 -> x |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 845 | // float4(x) - 0 -> float4(x) |
| 846 | // float4(x) - float4(0) -> float4(x) |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 847 | delete_right(&b, iter, optimizationContext); |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 848 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 849 | } |
| 850 | break; |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 851 | case Token::Kind::TK_SLASH: |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 852 | if (is_constant(right, 1)) { |
John Stiles | 9aeed13 | 2020-11-24 17:36:06 -0500 | [diff] [blame] | 853 | if (leftType.isScalar() && rightType.isVector()) { |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 854 | // x / float4(1) -> float4(x) |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 855 | vectorize_left(&b, iter, optimizationContext); |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 856 | } else { |
| 857 | // x / 1 -> x |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 858 | // float4(x) / 1 -> float4(x) |
| 859 | // float4(x) / float4(1) -> float4(x) |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 860 | delete_right(&b, iter, optimizationContext); |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 861 | } |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 862 | } else if (is_constant(left, 0)) { |
John Stiles | 9aeed13 | 2020-11-24 17:36:06 -0500 | [diff] [blame] | 863 | if (leftType.isScalar() && rightType.isVector() && |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 864 | !right.hasSideEffects()) { |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 865 | // 0 / float4(x) -> float4(0) |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 866 | vectorize_left(&b, iter, optimizationContext); |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 867 | } else { |
| 868 | // 0 / x -> 0 |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 869 | // float4(0) / x -> float4(0) |
| 870 | // float4(0) / float4(x) -> float4(0) |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 871 | if (!right.hasSideEffects()) { |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 872 | delete_right(&b, iter, optimizationContext); |
Ethan Nicholas | 51493ee | 2017-12-11 12:34:33 -0500 | [diff] [blame] | 873 | } |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 874 | } |
| 875 | } |
| 876 | break; |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 877 | case Token::Kind::TK_PLUSEQ: |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 878 | if (is_constant(right, 0)) { |
John Stiles | 47c0a74 | 2021-02-09 09:30:35 -0500 | [diff] [blame] | 879 | Analysis::UpdateRefKind(&left, RefKind::kRead); |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 880 | delete_right(&b, iter, optimizationContext); |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 881 | } |
| 882 | break; |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 883 | case Token::Kind::TK_MINUSEQ: |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 884 | if (is_constant(right, 0)) { |
John Stiles | 47c0a74 | 2021-02-09 09:30:35 -0500 | [diff] [blame] | 885 | Analysis::UpdateRefKind(&left, RefKind::kRead); |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 886 | delete_right(&b, iter, optimizationContext); |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 887 | } |
| 888 | break; |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 889 | case Token::Kind::TK_STAREQ: |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 890 | if (is_constant(right, 1)) { |
John Stiles | 47c0a74 | 2021-02-09 09:30:35 -0500 | [diff] [blame] | 891 | Analysis::UpdateRefKind(&left, RefKind::kRead); |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 892 | delete_right(&b, iter, optimizationContext); |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 893 | } |
| 894 | break; |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 895 | case Token::Kind::TK_SLASHEQ: |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 896 | if (is_constant(right, 1)) { |
John Stiles | 47c0a74 | 2021-02-09 09:30:35 -0500 | [diff] [blame] | 897 | Analysis::UpdateRefKind(&left, RefKind::kRead); |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 898 | delete_right(&b, iter, optimizationContext); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 899 | } |
| 900 | break; |
| 901 | default: |
| 902 | break; |
| 903 | } |
Ethan Nicholas | 409f6f0 | 2019-09-17 12:34:39 -0400 | [diff] [blame] | 904 | break; |
| 905 | } |
John Stiles | f5c1d04 | 2020-11-21 23:26:07 -0500 | [diff] [blame] | 906 | case Expression::Kind::kConstructor: { |
John Stiles | b9e4f64 | 2021-03-05 09:11:38 -0500 | [diff] [blame] | 907 | // TODO(skia:11319): this optimization logic is redundant with the optimization code |
| 908 | // found in SkSLConstructor.cpp. |
| 909 | |
John Stiles | f5c1d04 | 2020-11-21 23:26:07 -0500 | [diff] [blame] | 910 | // Find constructors embedded inside constructors and flatten them out where possible. |
| 911 | // - float4(float2(1, 2), 3, 4) --> float4(1, 2, 3, 4) |
| 912 | // - float4(w, float3(sin(x), cos(y), tan(z))) --> float4(w, sin(x), cos(y), tan(z)) |
| 913 | // Leave single-argument constructors alone, though. These might be casts or splats. |
| 914 | Constructor& c = expr->as<Constructor>(); |
| 915 | if (c.type().columns() > 1) { |
| 916 | // Inspect each constructor argument to see if it's a candidate for flattening. |
| 917 | // Remember matched arguments in a bitfield, "argsToOptimize". |
| 918 | int argsToOptimize = 0; |
| 919 | int currBit = 1; |
| 920 | for (const std::unique_ptr<Expression>& arg : c.arguments()) { |
| 921 | if (arg->is<Constructor>()) { |
| 922 | Constructor& inner = arg->as<Constructor>(); |
| 923 | if (inner.arguments().size() > 1 && |
| 924 | inner.type().componentType() == c.type().componentType()) { |
| 925 | argsToOptimize |= currBit; |
| 926 | } |
| 927 | } |
| 928 | currBit <<= 1; |
| 929 | } |
| 930 | if (argsToOptimize) { |
| 931 | // We found at least one argument that could be flattened out. Re-walk the |
| 932 | // constructor args and flatten the candidates we found during our initial pass. |
| 933 | ExpressionArray flattened; |
| 934 | flattened.reserve_back(c.type().columns()); |
| 935 | currBit = 1; |
| 936 | for (const std::unique_ptr<Expression>& arg : c.arguments()) { |
| 937 | if (argsToOptimize & currBit) { |
| 938 | Constructor& inner = arg->as<Constructor>(); |
| 939 | for (const std::unique_ptr<Expression>& innerArg : inner.arguments()) { |
| 940 | flattened.push_back(innerArg->clone()); |
| 941 | } |
| 942 | } else { |
| 943 | flattened.push_back(arg->clone()); |
| 944 | } |
| 945 | currBit <<= 1; |
| 946 | } |
John Stiles | 54f0049 | 2021-02-19 11:46:10 -0500 | [diff] [blame] | 947 | std::unique_ptr<Expression> replacement = std::make_unique<Constructor>( |
| 948 | c.fOffset, c.type(), std::move(flattened)); |
John Stiles | 1b91c0e | 2021-02-11 11:43:09 -0500 | [diff] [blame] | 949 | // We're replacing an expression with a cloned version; we'll need a rescan. |
| 950 | // No fUsage change: `float2(float(x), y)` and `float2(x, y)` have equivalent |
| 951 | // reference counts. |
| 952 | try_replace_expression(&b, iter, &replacement); |
John Stiles | f5c1d04 | 2020-11-21 23:26:07 -0500 | [diff] [blame] | 953 | optimizationContext->fUpdated = true; |
John Stiles | 1b91c0e | 2021-02-11 11:43:09 -0500 | [diff] [blame] | 954 | optimizationContext->fNeedsRescan = true; |
John Stiles | f5c1d04 | 2020-11-21 23:26:07 -0500 | [diff] [blame] | 955 | break; |
| 956 | } |
| 957 | } |
| 958 | break; |
| 959 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 960 | case Expression::Kind::kSwizzle: { |
John Stiles | f04e09c | 2021-03-05 13:13:14 -0500 | [diff] [blame] | 961 | // TODO(skia:11319): this optimization logic is redundant with the optimization code |
| 962 | // found in SkSLSwizzle.cpp. |
| 963 | |
John Stiles | 403a363 | 2020-08-20 12:11:48 -0400 | [diff] [blame] | 964 | Swizzle& s = expr->as<Swizzle>(); |
John Stiles | 108bbe2 | 2020-11-18 11:10:38 -0500 | [diff] [blame] | 965 | // Detect identity swizzles like `foo.rgba`. |
Ethan Nicholas | 6b4d581 | 2020-10-12 16:11:51 -0400 | [diff] [blame] | 966 | if ((int) s.components().size() == s.base()->type().columns()) { |
Ethan Nicholas | 409f6f0 | 2019-09-17 12:34:39 -0400 | [diff] [blame] | 967 | bool identity = true; |
Ethan Nicholas | 6b4d581 | 2020-10-12 16:11:51 -0400 | [diff] [blame] | 968 | for (int i = 0; i < (int) s.components().size(); ++i) { |
| 969 | if (s.components()[i] != i) { |
Ethan Nicholas | 409f6f0 | 2019-09-17 12:34:39 -0400 | [diff] [blame] | 970 | identity = false; |
| 971 | break; |
| 972 | } |
| 973 | } |
| 974 | if (identity) { |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 975 | optimizationContext->fUpdated = true; |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 976 | // No fUsage change: foo.rgba and foo have equivalent reference counts |
Ethan Nicholas | 6b4d581 | 2020-10-12 16:11:51 -0400 | [diff] [blame] | 977 | if (!try_replace_expression(&b, iter, &s.base())) { |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 978 | optimizationContext->fNeedsRescan = true; |
Ethan Nicholas | 409f6f0 | 2019-09-17 12:34:39 -0400 | [diff] [blame] | 979 | return; |
| 980 | } |
John Stiles | 70025e5 | 2020-09-28 16:08:58 -0400 | [diff] [blame] | 981 | SkASSERT((*iter)->isExpression()); |
Ethan Nicholas | 409f6f0 | 2019-09-17 12:34:39 -0400 | [diff] [blame] | 982 | break; |
| 983 | } |
| 984 | } |
John Stiles | 108bbe2 | 2020-11-18 11:10:38 -0500 | [diff] [blame] | 985 | // Detect swizzles of swizzles, e.g. replace `foo.argb.r000` with `foo.a000`. |
| 986 | if (s.base()->is<Swizzle>()) { |
Ethan Nicholas | 6b4d581 | 2020-10-12 16:11:51 -0400 | [diff] [blame] | 987 | Swizzle& base = s.base()->as<Swizzle>(); |
John Stiles | 750109b | 2020-10-30 13:45:46 -0400 | [diff] [blame] | 988 | ComponentArray final; |
Ethan Nicholas | 6b4d581 | 2020-10-12 16:11:51 -0400 | [diff] [blame] | 989 | for (int c : s.components()) { |
| 990 | final.push_back(base.components()[c]); |
Ethan Nicholas | 409f6f0 | 2019-09-17 12:34:39 -0400 | [diff] [blame] | 991 | } |
Ethan Nicholas | 6b4d581 | 2020-10-12 16:11:51 -0400 | [diff] [blame] | 992 | std::unique_ptr<Expression> replacement(new Swizzle(*fContext, base.base()->clone(), |
John Stiles | 750109b | 2020-10-30 13:45:46 -0400 | [diff] [blame] | 993 | final)); |
John Stiles | d2f51b1 | 2021-01-07 18:12:31 -0500 | [diff] [blame] | 994 | // We're replacing an expression with a cloned version; we'll need a rescan. |
John Stiles | 108bbe2 | 2020-11-18 11:10:38 -0500 | [diff] [blame] | 995 | // No fUsage change: `foo.gbr.gbr` and `foo.brg` have equivalent reference counts |
John Stiles | d2f51b1 | 2021-01-07 18:12:31 -0500 | [diff] [blame] | 996 | try_replace_expression(&b, iter, &replacement); |
| 997 | optimizationContext->fUpdated = true; |
| 998 | optimizationContext->fNeedsRescan = true; |
John Stiles | 108bbe2 | 2020-11-18 11:10:38 -0500 | [diff] [blame] | 999 | break; |
| 1000 | } |
| 1001 | // Optimize swizzles of constructors. |
| 1002 | if (s.base()->is<Constructor>()) { |
| 1003 | Constructor& base = s.base()->as<Constructor>(); |
| 1004 | std::unique_ptr<Expression> replacement; |
| 1005 | const Type& componentType = base.type().componentType(); |
| 1006 | int swizzleSize = s.components().size(); |
| 1007 | |
| 1008 | // The IR generator has already converted any zero/one swizzle components into |
| 1009 | // constructors containing zero/one args. Confirm that this is true by checking that |
| 1010 | // our swizzle components are all `xyzw` (values 0 through 3). |
| 1011 | SkASSERT(std::all_of(s.components().begin(), s.components().end(), |
| 1012 | [](int8_t c) { return c >= 0 && c <= 3; })); |
| 1013 | |
John Stiles | 9aeed13 | 2020-11-24 17:36:06 -0500 | [diff] [blame] | 1014 | if (base.arguments().size() == 1 && base.arguments().front()->type().isScalar()) { |
John Stiles | 108bbe2 | 2020-11-18 11:10:38 -0500 | [diff] [blame] | 1015 | // `half4(scalar).zyy` can be optimized to `half3(scalar)`. The swizzle |
| 1016 | // components don't actually matter since all fields are the same. |
John Stiles | f0cb733 | 2021-01-08 18:39:00 -0500 | [diff] [blame] | 1017 | const Expression& argument = *base.arguments().front(); |
| 1018 | const Type& constructorType = componentType.toCompound(*fContext, swizzleSize, |
| 1019 | /*rows=*/1); |
| 1020 | replacement = Constructor::SimplifyConversion(constructorType, argument); |
| 1021 | if (!replacement) { |
| 1022 | ExpressionArray newArgs; |
| 1023 | newArgs.push_back(argument.clone()); |
John Stiles | 54f0049 | 2021-02-19 11:46:10 -0500 | [diff] [blame] | 1024 | replacement = std::make_unique<Constructor>(base.fOffset, constructorType, |
John Stiles | f0cb733 | 2021-01-08 18:39:00 -0500 | [diff] [blame] | 1025 | std::move(newArgs)); |
| 1026 | } |
John Stiles | 108bbe2 | 2020-11-18 11:10:38 -0500 | [diff] [blame] | 1027 | |
John Stiles | a60ac0c | 2020-12-22 08:59:51 -0500 | [diff] [blame] | 1028 | // We're replacing an expression with a cloned version; we'll need a rescan. |
| 1029 | // There's no fUsage change: `half4(foo).xy` and `half2(foo)` have equivalent |
| 1030 | // reference counts. |
| 1031 | try_replace_expression(&b, iter, &replacement); |
John Stiles | 0777ac4 | 2020-11-19 11:06:47 -0500 | [diff] [blame] | 1032 | optimizationContext->fUpdated = true; |
John Stiles | a60ac0c | 2020-12-22 08:59:51 -0500 | [diff] [blame] | 1033 | optimizationContext->fNeedsRescan = true; |
John Stiles | 108bbe2 | 2020-11-18 11:10:38 -0500 | [diff] [blame] | 1034 | break; |
| 1035 | } |
| 1036 | |
John Stiles | 0777ac4 | 2020-11-19 11:06:47 -0500 | [diff] [blame] | 1037 | // Swizzles can duplicate some elements and discard others, e.g. |
| 1038 | // `half4(1, 2, 3, 4).xxz` --> `half3(1, 1, 3)`. However, there are constraints: |
| 1039 | // - Expressions with side effects need to occur exactly once, even if they |
| 1040 | // would otherwise be swizzle-eliminated |
| 1041 | // - Non-trivial expressions should not be repeated, but elimination is OK. |
| 1042 | // |
| 1043 | // Look up the argument for the constructor at each index. This is typically simple |
| 1044 | // but for weird cases like `half4(bar.yz, half2(foo))`, it can be harder than it |
| 1045 | // seems. This example would result in: |
| 1046 | // argMap[0] = {.fArgIndex = 0, .fComponent = 0} (bar.yz .x) |
| 1047 | // argMap[1] = {.fArgIndex = 0, .fComponent = 1} (bar.yz .y) |
| 1048 | // argMap[2] = {.fArgIndex = 1, .fComponent = 0} (half2(foo) .x) |
| 1049 | // argMap[3] = {.fArgIndex = 1, .fComponent = 1} (half2(foo) .y) |
| 1050 | struct ConstructorArgMap { |
| 1051 | int8_t fArgIndex; |
| 1052 | int8_t fComponent; |
| 1053 | }; |
| 1054 | |
| 1055 | int numConstructorArgs = base.type().columns(); |
| 1056 | ConstructorArgMap argMap[4] = {}; |
| 1057 | int writeIdx = 0; |
| 1058 | for (int argIdx = 0; argIdx < (int) base.arguments().size(); ++argIdx) { |
| 1059 | const Expression& expr = *base.arguments()[argIdx]; |
| 1060 | int argWidth = expr.type().columns(); |
| 1061 | for (int componentIdx = 0; componentIdx < argWidth; ++componentIdx) { |
| 1062 | argMap[writeIdx].fArgIndex = argIdx; |
| 1063 | argMap[writeIdx].fComponent = componentIdx; |
| 1064 | ++writeIdx; |
| 1065 | } |
| 1066 | } |
| 1067 | SkASSERT(writeIdx == numConstructorArgs); |
| 1068 | |
| 1069 | // Count up the number of times each constructor argument is used by the |
| 1070 | // swizzle. |
| 1071 | // `half4(bar.yz, half2(foo)).xwxy` -> { 3, 1 } |
| 1072 | // - bar.yz is referenced 3 times, by `.x_xy` |
| 1073 | // - half(foo) is referenced 1 time, by `._w__` |
| 1074 | int8_t exprUsed[4] = {}; |
| 1075 | for (int c : s.components()) { |
| 1076 | exprUsed[argMap[c].fArgIndex]++; |
| 1077 | } |
| 1078 | |
| 1079 | bool safeToOptimize = true; |
| 1080 | for (int index = 0; index < numConstructorArgs; ++index) { |
| 1081 | int8_t constructorArgIndex = argMap[index].fArgIndex; |
| 1082 | const Expression& baseArg = *base.arguments()[constructorArgIndex]; |
| 1083 | |
| 1084 | // Check that non-trivial expressions are not swizzled in more than once. |
John Stiles | c30fbca | 2020-11-19 16:25:49 -0500 | [diff] [blame] | 1085 | if (exprUsed[constructorArgIndex] > 1 && |
| 1086 | !Analysis::IsTrivialExpression(baseArg)) { |
John Stiles | 0777ac4 | 2020-11-19 11:06:47 -0500 | [diff] [blame] | 1087 | safeToOptimize = false; |
| 1088 | break; |
| 1089 | } |
| 1090 | // Check that side-effect-bearing expressions are swizzled in exactly once. |
| 1091 | if (exprUsed[constructorArgIndex] != 1 && baseArg.hasSideEffects()) { |
| 1092 | safeToOptimize = false; |
| 1093 | break; |
| 1094 | } |
| 1095 | } |
| 1096 | |
| 1097 | if (safeToOptimize) { |
John Stiles | d9076cb | 2020-11-19 12:18:36 -0500 | [diff] [blame] | 1098 | struct ReorderedArgument { |
| 1099 | int8_t fArgIndex; |
| 1100 | ComponentArray fComponents; |
| 1101 | }; |
| 1102 | SkSTArray<4, ReorderedArgument> reorderedArgs; |
John Stiles | 0777ac4 | 2020-11-19 11:06:47 -0500 | [diff] [blame] | 1103 | for (int c : s.components()) { |
| 1104 | const ConstructorArgMap& argument = argMap[c]; |
| 1105 | const Expression& baseArg = *base.arguments()[argument.fArgIndex]; |
| 1106 | |
John Stiles | 9aeed13 | 2020-11-24 17:36:06 -0500 | [diff] [blame] | 1107 | if (baseArg.type().isScalar()) { |
John Stiles | d9076cb | 2020-11-19 12:18:36 -0500 | [diff] [blame] | 1108 | // This argument is a scalar; add it to the list as-is. |
John Stiles | 0777ac4 | 2020-11-19 11:06:47 -0500 | [diff] [blame] | 1109 | SkASSERT(argument.fComponent == 0); |
John Stiles | d9076cb | 2020-11-19 12:18:36 -0500 | [diff] [blame] | 1110 | reorderedArgs.push_back({argument.fArgIndex, |
| 1111 | ComponentArray{}}); |
John Stiles | 0777ac4 | 2020-11-19 11:06:47 -0500 | [diff] [blame] | 1112 | } else { |
John Stiles | d9076cb | 2020-11-19 12:18:36 -0500 | [diff] [blame] | 1113 | // This argument is a component from a vector. |
John Stiles | 0777ac4 | 2020-11-19 11:06:47 -0500 | [diff] [blame] | 1114 | SkASSERT(argument.fComponent < baseArg.type().columns()); |
John Stiles | d9076cb | 2020-11-19 12:18:36 -0500 | [diff] [blame] | 1115 | if (reorderedArgs.empty() || |
| 1116 | reorderedArgs.back().fArgIndex != argument.fArgIndex) { |
| 1117 | // This can't be combined with the previous argument. Add a new one. |
| 1118 | reorderedArgs.push_back({argument.fArgIndex, |
| 1119 | ComponentArray{argument.fComponent}}); |
| 1120 | } else { |
| 1121 | // Since we know this argument uses components, it should already |
| 1122 | // have at least one component set. |
| 1123 | SkASSERT(!reorderedArgs.back().fComponents.empty()); |
| 1124 | // Build up the current argument with one more component. |
| 1125 | reorderedArgs.back().fComponents.push_back(argument.fComponent); |
| 1126 | } |
John Stiles | 0777ac4 | 2020-11-19 11:06:47 -0500 | [diff] [blame] | 1127 | } |
| 1128 | } |
John Stiles | d9076cb | 2020-11-19 12:18:36 -0500 | [diff] [blame] | 1129 | |
| 1130 | // Convert our reordered argument list to an actual array of expressions, with |
| 1131 | // the new order and any new inner swizzles that need to be applied. Note that |
| 1132 | // we expect followup passes to clean up the inner swizzles. |
| 1133 | ExpressionArray newArgs; |
| 1134 | newArgs.reserve_back(swizzleSize); |
| 1135 | for (const ReorderedArgument& reorderedArg : reorderedArgs) { |
| 1136 | const Expression& baseArg = *base.arguments()[reorderedArg.fArgIndex]; |
| 1137 | if (reorderedArg.fComponents.empty()) { |
| 1138 | newArgs.push_back(baseArg.clone()); |
| 1139 | } else { |
| 1140 | newArgs.push_back(std::make_unique<Swizzle>(*fContext, baseArg.clone(), |
| 1141 | reorderedArg.fComponents)); |
| 1142 | } |
| 1143 | } |
| 1144 | |
| 1145 | // Create a new constructor. |
John Stiles | 0777ac4 | 2020-11-19 11:06:47 -0500 | [diff] [blame] | 1146 | replacement = std::make_unique<Constructor>( |
| 1147 | base.fOffset, |
John Stiles | 54f0049 | 2021-02-19 11:46:10 -0500 | [diff] [blame] | 1148 | componentType.toCompound(*fContext, swizzleSize, /*rows=*/1), |
John Stiles | 0777ac4 | 2020-11-19 11:06:47 -0500 | [diff] [blame] | 1149 | std::move(newArgs)); |
| 1150 | |
John Stiles | a60ac0c | 2020-12-22 08:59:51 -0500 | [diff] [blame] | 1151 | // Remove references within 'expr', add references within 'replacement.' |
John Stiles | 0777ac4 | 2020-11-19 11:06:47 -0500 | [diff] [blame] | 1152 | optimizationContext->fUsage->replace(expr, replacement.get()); |
John Stiles | a60ac0c | 2020-12-22 08:59:51 -0500 | [diff] [blame] | 1153 | |
| 1154 | // We're replacing an expression with a cloned version; we'll need a rescan. |
| 1155 | try_replace_expression(&b, iter, &replacement); |
| 1156 | optimizationContext->fUpdated = true; |
| 1157 | optimizationContext->fNeedsRescan = true; |
John Stiles | 0777ac4 | 2020-11-19 11:06:47 -0500 | [diff] [blame] | 1158 | } |
John Stiles | 108bbe2 | 2020-11-18 11:10:38 -0500 | [diff] [blame] | 1159 | break; |
Ethan Nicholas | 409f6f0 | 2019-09-17 12:34:39 -0400 | [diff] [blame] | 1160 | } |
John Stiles | 30212b7 | 2020-06-11 17:55:07 -0400 | [diff] [blame] | 1161 | break; |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1162 | } |
| 1163 | default: |
| 1164 | break; |
| 1165 | } |
| 1166 | } |
| 1167 | |
| 1168 | void Compiler::simplifyStatement(DefinitionMap& definitions, |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1169 | BasicBlock& b, |
| 1170 | std::vector<BasicBlock::Node>::iterator* iter, |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 1171 | OptimizationContext* optimizationContext) { |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 1172 | ProgramUsage* usage = optimizationContext->fUsage; |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1173 | Statement* stmt = (*iter)->statement()->get(); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1174 | switch (stmt->kind()) { |
| 1175 | case Statement::Kind::kVarDeclaration: { |
John Stiles | a5a97b4 | 2020-08-18 11:19:07 -0400 | [diff] [blame] | 1176 | const auto& varDecl = stmt->as<VarDeclaration>(); |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 1177 | if (usage->isDead(varDecl.var()) && |
Ethan Nicholas | c51f33e | 2020-10-13 13:49:44 -0400 | [diff] [blame] | 1178 | (!varDecl.value() || |
| 1179 | !varDecl.value()->hasSideEffects())) { |
| 1180 | if (varDecl.value()) { |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 1181 | SkASSERT((*iter)->statement()->get() == stmt); |
Ethan Nicholas | c51f33e | 2020-10-13 13:49:44 -0400 | [diff] [blame] | 1182 | if (!b.tryRemoveExpressionBefore(iter, varDecl.value().get())) { |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 1183 | optimizationContext->fNeedsRescan = true; |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1184 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1185 | } |
Ethan Nicholas | 5b9b0db | 2021-01-21 13:12:01 -0500 | [diff] [blame] | 1186 | // There can still be (soon to be removed) references to the variable at this point. |
| 1187 | // Allowing the VarDeclaration to be destroyed here will break those variable's |
| 1188 | // initialValue()s, so we hang on to them until optimization is finished. |
| 1189 | std::unique_ptr<Statement> old = (*iter)->setStatement(std::make_unique<Nop>(), |
| 1190 | usage); |
| 1191 | optimizationContext->fOwnedStatements.push_back(std::move(old)); |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 1192 | optimizationContext->fUpdated = true; |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1193 | } |
| 1194 | break; |
| 1195 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1196 | case Statement::Kind::kIf: { |
John Stiles | 4633c91 | 2021-03-03 16:13:45 -0500 | [diff] [blame] | 1197 | // TODO(skia:11319): this optimization logic is redundant with the optimization code |
| 1198 | // found in IfStatement.cpp. |
John Stiles | a5a97b4 | 2020-08-18 11:19:07 -0400 | [diff] [blame] | 1199 | IfStatement& i = stmt->as<IfStatement>(); |
Ethan Nicholas | 8c44eca | 2020-10-07 16:47:09 -0400 | [diff] [blame] | 1200 | if (i.test()->kind() == Expression::Kind::kBoolLiteral) { |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1201 | // constant if, collapse down to a single branch |
Ethan Nicholas | 8c44eca | 2020-10-07 16:47:09 -0400 | [diff] [blame] | 1202 | if (i.test()->as<BoolLiteral>().value()) { |
| 1203 | SkASSERT(i.ifTrue()); |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 1204 | (*iter)->setStatement(std::move(i.ifTrue()), usage); |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1205 | } else { |
Ethan Nicholas | 8c44eca | 2020-10-07 16:47:09 -0400 | [diff] [blame] | 1206 | if (i.ifFalse()) { |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 1207 | (*iter)->setStatement(std::move(i.ifFalse()), usage); |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1208 | } else { |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 1209 | (*iter)->setStatement(std::make_unique<Nop>(), usage); |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1210 | } |
| 1211 | } |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 1212 | optimizationContext->fUpdated = true; |
| 1213 | optimizationContext->fNeedsRescan = true; |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1214 | break; |
| 1215 | } |
Ethan Nicholas | 8c44eca | 2020-10-07 16:47:09 -0400 | [diff] [blame] | 1216 | if (i.ifFalse() && i.ifFalse()->isEmpty()) { |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1217 | // else block doesn't do anything, remove it |
Ethan Nicholas | 8c44eca | 2020-10-07 16:47:09 -0400 | [diff] [blame] | 1218 | i.ifFalse().reset(); |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 1219 | optimizationContext->fUpdated = true; |
| 1220 | optimizationContext->fNeedsRescan = true; |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1221 | } |
Ethan Nicholas | 8c44eca | 2020-10-07 16:47:09 -0400 | [diff] [blame] | 1222 | if (!i.ifFalse() && i.ifTrue()->isEmpty()) { |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1223 | // if block doesn't do anything, no else block |
Ethan Nicholas | 8c44eca | 2020-10-07 16:47:09 -0400 | [diff] [blame] | 1224 | if (i.test()->hasSideEffects()) { |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1225 | // test has side effects, keep it |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 1226 | (*iter)->setStatement( |
| 1227 | std::make_unique<ExpressionStatement>(std::move(i.test())), usage); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1228 | } else { |
| 1229 | // no if, no else, no test side effects, kill the whole if |
| 1230 | // statement |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 1231 | (*iter)->setStatement(std::make_unique<Nop>(), usage); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1232 | } |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 1233 | optimizationContext->fUpdated = true; |
| 1234 | optimizationContext->fNeedsRescan = true; |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1235 | } |
| 1236 | break; |
| 1237 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1238 | case Statement::Kind::kSwitch: { |
John Stiles | 642cde2 | 2021-02-23 14:57:01 -0500 | [diff] [blame] | 1239 | // TODO(skia:11319): this optimization logic is redundant with the static-switch |
| 1240 | // optimization code found in SwitchStatement.cpp. |
John Stiles | a5a97b4 | 2020-08-18 11:19:07 -0400 | [diff] [blame] | 1241 | SwitchStatement& s = stmt->as<SwitchStatement>(); |
Ethan Nicholas | 34b19c5 | 2020-09-14 11:33:47 -0400 | [diff] [blame] | 1242 | int64_t switchValue; |
Ethan Nicholas | c0f9815 | 2021-02-05 16:21:10 -0500 | [diff] [blame] | 1243 | if (ConstantFolder::GetConstantInt(*s.value(), &switchValue)) { |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1244 | // switch is constant, replace it with the case that matches |
| 1245 | bool found = false; |
| 1246 | SwitchCase* defaultCase = nullptr; |
John Stiles | 2d4f959 | 2020-10-30 10:29:12 -0400 | [diff] [blame] | 1247 | for (const std::unique_ptr<SwitchCase>& c : s.cases()) { |
| 1248 | if (!c->value()) { |
| 1249 | defaultCase = c.get(); |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1250 | continue; |
| 1251 | } |
Ethan Nicholas | 34b19c5 | 2020-09-14 11:33:47 -0400 | [diff] [blame] | 1252 | int64_t caseValue; |
Ethan Nicholas | c0f9815 | 2021-02-05 16:21:10 -0500 | [diff] [blame] | 1253 | SkAssertResult(ConstantFolder::GetConstantInt(*c->value(), &caseValue)); |
Ethan Nicholas | 34b19c5 | 2020-09-14 11:33:47 -0400 | [diff] [blame] | 1254 | if (caseValue == switchValue) { |
John Stiles | 642cde2 | 2021-02-23 14:57:01 -0500 | [diff] [blame] | 1255 | std::unique_ptr<Statement> newBlock = |
| 1256 | SwitchStatement::BlockForCase(&s.cases(), c.get(), s.symbols()); |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1257 | if (newBlock) { |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 1258 | (*iter)->setStatement(std::move(newBlock), usage); |
John Stiles | 9d94423 | 2020-08-19 09:56:49 -0400 | [diff] [blame] | 1259 | found = true; |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1260 | break; |
| 1261 | } else { |
John Stiles | d6a5f449 | 2021-02-11 15:46:11 -0500 | [diff] [blame] | 1262 | if (s.isStatic() && |
John Stiles | d120464 | 2021-02-17 16:30:02 -0500 | [diff] [blame] | 1263 | !fContext->fConfig->fSettings.fPermitInvalidStaticTests) { |
John Stiles | d6a5f449 | 2021-02-11 15:46:11 -0500 | [diff] [blame] | 1264 | auto [iter, didInsert] = optimizationContext->fSilences.insert(&s); |
| 1265 | if (didInsert) { |
| 1266 | this->error(s.fOffset, "static switch contains non-static " |
John Stiles | 04ca41a | 2021-02-23 09:58:04 -0500 | [diff] [blame] | 1267 | "conditional exit"); |
John Stiles | d6a5f449 | 2021-02-11 15:46:11 -0500 | [diff] [blame] | 1268 | } |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1269 | } |
| 1270 | return; // can't simplify |
| 1271 | } |
| 1272 | } |
| 1273 | } |
| 1274 | if (!found) { |
| 1275 | // no matching case. use default if it exists, or kill the whole thing |
| 1276 | if (defaultCase) { |
John Stiles | 642cde2 | 2021-02-23 14:57:01 -0500 | [diff] [blame] | 1277 | std::unique_ptr<Statement> newBlock = |
| 1278 | SwitchStatement::BlockForCase(&s.cases(), defaultCase, s.symbols()); |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1279 | if (newBlock) { |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 1280 | (*iter)->setStatement(std::move(newBlock), usage); |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1281 | } else { |
John Stiles | d6a5f449 | 2021-02-11 15:46:11 -0500 | [diff] [blame] | 1282 | if (s.isStatic() && |
John Stiles | d120464 | 2021-02-17 16:30:02 -0500 | [diff] [blame] | 1283 | !fContext->fConfig->fSettings.fPermitInvalidStaticTests) { |
John Stiles | d6a5f449 | 2021-02-11 15:46:11 -0500 | [diff] [blame] | 1284 | auto [iter, didInsert] = optimizationContext->fSilences.insert(&s); |
| 1285 | if (didInsert) { |
| 1286 | this->error(s.fOffset, "static switch contains non-static " |
John Stiles | 04ca41a | 2021-02-23 09:58:04 -0500 | [diff] [blame] | 1287 | "conditional exit"); |
John Stiles | d6a5f449 | 2021-02-11 15:46:11 -0500 | [diff] [blame] | 1288 | } |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1289 | } |
| 1290 | return; // can't simplify |
| 1291 | } |
| 1292 | } else { |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 1293 | (*iter)->setStatement(std::make_unique<Nop>(), usage); |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1294 | } |
| 1295 | } |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 1296 | optimizationContext->fUpdated = true; |
| 1297 | optimizationContext->fNeedsRescan = true; |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1298 | } |
| 1299 | break; |
| 1300 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1301 | case Statement::Kind::kExpression: { |
John Stiles | a5a97b4 | 2020-08-18 11:19:07 -0400 | [diff] [blame] | 1302 | ExpressionStatement& e = stmt->as<ExpressionStatement>(); |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 1303 | SkASSERT((*iter)->statement()->get() == &e); |
Ethan Nicholas | d503a5a | 2020-09-30 09:29:55 -0400 | [diff] [blame] | 1304 | if (!e.expression()->hasSideEffects()) { |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1305 | // Expression statement with no side effects, kill it |
Ethan Nicholas | d503a5a | 2020-09-30 09:29:55 -0400 | [diff] [blame] | 1306 | if (!b.tryRemoveExpressionBefore(iter, e.expression().get())) { |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 1307 | optimizationContext->fNeedsRescan = true; |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1308 | } |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 1309 | SkASSERT((*iter)->statement()->get() == stmt); |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 1310 | (*iter)->setStatement(std::make_unique<Nop>(), usage); |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 1311 | optimizationContext->fUpdated = true; |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1312 | } |
| 1313 | break; |
| 1314 | } |
| 1315 | default: |
| 1316 | break; |
| 1317 | } |
| 1318 | } |
| 1319 | |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 1320 | bool Compiler::scanCFG(FunctionDefinition& f, ProgramUsage* usage) { |
John Stiles | 0cc193a | 2020-09-09 09:39:34 -0400 | [diff] [blame] | 1321 | bool madeChanges = false; |
| 1322 | |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1323 | CFG cfg = CFGGenerator().getCFG(f); |
| 1324 | this->computeDataFlow(&cfg); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 1325 | |
John Stiles | 7247b48 | 2021-03-08 10:40:35 -0500 | [diff] [blame^] | 1326 | if (fContext->fConfig->fSettings.fDeadCodeElimination) { |
John Stiles | 66c53b9 | 2021-02-20 08:00:43 -0500 | [diff] [blame] | 1327 | // Check for unreachable code. |
| 1328 | for (size_t i = 0; i < cfg.fBlocks.size(); i++) { |
| 1329 | const BasicBlock& block = cfg.fBlocks[i]; |
| 1330 | if (!block.fIsReachable && !block.fAllowUnreachable && block.fNodes.size()) { |
| 1331 | const BasicBlock::Node& node = block.fNodes[0]; |
| 1332 | int offset = node.isStatement() ? (*node.statement())->fOffset |
| 1333 | : (*node.expression())->fOffset; |
| 1334 | this->error(offset, String("unreachable")); |
| 1335 | } |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 1336 | } |
| 1337 | } |
| 1338 | if (fErrorCount) { |
John Stiles | 0cc193a | 2020-09-09 09:39:34 -0400 | [diff] [blame] | 1339 | return madeChanges; |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 1340 | } |
| 1341 | |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1342 | // check for dead code & undefined variables, perform constant propagation |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 1343 | OptimizationContext optimizationContext; |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 1344 | optimizationContext.fUsage = usage; |
John Stiles | 7d3f089 | 2020-11-03 11:35:01 -0500 | [diff] [blame] | 1345 | SkBitSet eliminatedBlockIds(cfg.fBlocks.size()); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1346 | do { |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 1347 | if (optimizationContext.fNeedsRescan) { |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1348 | cfg = CFGGenerator().getCFG(f); |
| 1349 | this->computeDataFlow(&cfg); |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 1350 | optimizationContext.fNeedsRescan = false; |
Ethan Nicholas | 113628d | 2017-02-02 16:11:39 -0500 | [diff] [blame] | 1351 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1352 | |
John Stiles | 7d3f089 | 2020-11-03 11:35:01 -0500 | [diff] [blame] | 1353 | eliminatedBlockIds.reset(); |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 1354 | optimizationContext.fUpdated = false; |
John Stiles | 7d3f089 | 2020-11-03 11:35:01 -0500 | [diff] [blame] | 1355 | |
| 1356 | for (BlockId blockId = 0; blockId < cfg.fBlocks.size(); ++blockId) { |
| 1357 | if (eliminatedBlockIds.test(blockId)) { |
| 1358 | // We reached a block ID that might have been eliminated. Be cautious and rescan. |
| 1359 | optimizationContext.fUpdated = true; |
| 1360 | optimizationContext.fNeedsRescan = true; |
| 1361 | break; |
| 1362 | } |
| 1363 | |
| 1364 | BasicBlock& b = cfg.fBlocks[blockId]; |
John Stiles | 7247b48 | 2021-03-08 10:40:35 -0500 | [diff] [blame^] | 1365 | if (fContext->fConfig->fSettings.fDeadCodeElimination) { |
John Stiles | 66c53b9 | 2021-02-20 08:00:43 -0500 | [diff] [blame] | 1366 | if (blockId > 0 && !b.fIsReachable) { |
| 1367 | // Block was reachable before optimization, but has since become unreachable. In |
| 1368 | // addition to being dead code, it's broken - since control flow can't reach it, |
| 1369 | // no prior variable definitions can reach it, and therefore variables might |
| 1370 | // look to have not been properly assigned. Kill it by replacing all statements |
| 1371 | // with Nops. |
| 1372 | for (BasicBlock::Node& node : b.fNodes) { |
| 1373 | if (node.isStatement() && !(*node.statement())->is<Nop>()) { |
| 1374 | // Eliminating a node runs the risk of eliminating that node's exits as |
| 1375 | // well. Keep track of this and do a rescan if we are about to access |
| 1376 | // one of these. |
| 1377 | for (BlockId id : b.fExits) { |
| 1378 | eliminatedBlockIds.set(id); |
| 1379 | } |
| 1380 | node.setStatement(std::make_unique<Nop>(), usage); |
| 1381 | madeChanges = true; |
John Stiles | 7d3f089 | 2020-11-03 11:35:01 -0500 | [diff] [blame] | 1382 | } |
Ethan Nicholas | 1de1481 | 2020-06-19 15:32:49 -0400 | [diff] [blame] | 1383 | } |
John Stiles | 66c53b9 | 2021-02-20 08:00:43 -0500 | [diff] [blame] | 1384 | continue; |
Ethan Nicholas | 1de1481 | 2020-06-19 15:32:49 -0400 | [diff] [blame] | 1385 | } |
Ethan Nicholas | 1de1481 | 2020-06-19 15:32:49 -0400 | [diff] [blame] | 1386 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1387 | DefinitionMap definitions = b.fBefore; |
| 1388 | |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 1389 | for (auto iter = b.fNodes.begin(); iter != b.fNodes.end() && |
| 1390 | !optimizationContext.fNeedsRescan; ++iter) { |
John Stiles | 70025e5 | 2020-09-28 16:08:58 -0400 | [diff] [blame] | 1391 | if (iter->isExpression()) { |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 1392 | this->simplifyExpression(definitions, b, &iter, &optimizationContext); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1393 | } else { |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 1394 | this->simplifyStatement(definitions, b, &iter, &optimizationContext); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1395 | } |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 1396 | if (optimizationContext.fNeedsRescan) { |
Ethan Nicholas | 4b330df | 2017-05-17 10:52:55 -0400 | [diff] [blame] | 1397 | break; |
| 1398 | } |
John Stiles | e8a2492 | 2021-02-08 17:54:08 -0500 | [diff] [blame] | 1399 | definitions.addDefinitions(*fContext, *iter); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1400 | } |
Brian Osman | 01a3eb4 | 2020-09-14 11:32:49 -0400 | [diff] [blame] | 1401 | |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 1402 | if (optimizationContext.fNeedsRescan) { |
Brian Osman | 01a3eb4 | 2020-09-14 11:32:49 -0400 | [diff] [blame] | 1403 | break; |
| 1404 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1405 | } |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 1406 | madeChanges |= optimizationContext.fUpdated; |
| 1407 | } while (optimizationContext.fUpdated); |
| 1408 | SkASSERT(!optimizationContext.fNeedsRescan); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 1409 | |
John Stiles | 0cc193a | 2020-09-09 09:39:34 -0400 | [diff] [blame] | 1410 | return madeChanges; |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 1411 | } |
| 1412 | |
Brian Osman | 32d5355 | 2020-09-23 13:55:20 -0400 | [diff] [blame] | 1413 | std::unique_ptr<Program> Compiler::convertProgram( |
John Stiles | dbd4e6f | 2021-02-16 13:29:15 -0500 | [diff] [blame] | 1414 | ProgramKind kind, |
Brian Osman | 32d5355 | 2020-09-23 13:55:20 -0400 | [diff] [blame] | 1415 | String text, |
| 1416 | const Program::Settings& settings, |
Brian Osman | be0b3b7 | 2021-01-06 14:27:35 -0500 | [diff] [blame] | 1417 | const std::vector<std::unique_ptr<ExternalFunction>>* externalFunctions) { |
Brian Osman | d010f65 | 2021-02-24 13:59:39 -0500 | [diff] [blame] | 1418 | TRACE_EVENT0("skia.gpu", "SkSL::Compiler::convertProgram"); |
Leon Scroggins | b66214e | 2021-02-11 17:14:18 -0500 | [diff] [blame] | 1419 | |
John Stiles | dbd4e6f | 2021-02-16 13:29:15 -0500 | [diff] [blame] | 1420 | SkASSERT(!externalFunctions || (kind == ProgramKind::kGeneric)); |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 1421 | |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 1422 | // Loading and optimizing our base module might reset the inliner, so do that first, |
| 1423 | // *then* configure the inliner with the settings for this program. |
| 1424 | const ParsedModule& baseModule = this->moduleForProgramKind(kind); |
| 1425 | |
John Stiles | 270cec2 | 2021-02-17 12:59:36 -0500 | [diff] [blame] | 1426 | // Update our context to point to the program configuration for the duration of compilation. |
| 1427 | auto config = std::make_unique<ProgramConfig>(ProgramConfig{kind, settings}); |
John Stiles | a935c3f | 2021-02-25 10:35:49 -0500 | [diff] [blame] | 1428 | AutoProgramConfig autoConfig(fContext, config.get()); |
John Stiles | 270cec2 | 2021-02-17 12:59:36 -0500 | [diff] [blame] | 1429 | |
John Stiles | 7247b48 | 2021-03-08 10:40:35 -0500 | [diff] [blame^] | 1430 | // Honor our global optimization-disable flags. |
| 1431 | config->fSettings.fOptimize &= gSkSLOptimizer; |
| 1432 | config->fSettings.fControlFlowAnalysis &= gSkSLControlFlowAnalysis; |
| 1433 | config->fSettings.fInlineThreshold *= (int)gSkSLInliner; |
| 1434 | |
| 1435 | // Disable optimization settings that depend on a parent setting which has been disabled. |
| 1436 | config->fSettings.fControlFlowAnalysis &= config->fSettings.fOptimize; |
| 1437 | config->fSettings.fInlineThreshold *= (int)config->fSettings.fOptimize; |
| 1438 | config->fSettings.fDeadCodeElimination &= config->fSettings.fControlFlowAnalysis; |
| 1439 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1440 | fErrorText = ""; |
| 1441 | fErrorCount = 0; |
John Stiles | d120464 | 2021-02-17 16:30:02 -0500 | [diff] [blame] | 1442 | fInliner.reset(fIRGenerator->fModifiers.get()); |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 1443 | |
| 1444 | // 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] | 1445 | std::unique_ptr<String> textPtr(new String(std::move(text))); |
| 1446 | fSource = textPtr.get(); |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 1447 | |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 1448 | // Enable node pooling while converting and optimizing the program for a performance boost. |
| 1449 | // The Program will take ownership of the pool. |
Brian Osman | 28f702c | 2021-02-02 11:52:07 -0500 | [diff] [blame] | 1450 | std::unique_ptr<Pool> pool; |
John Stiles | c1a98b8 | 2021-02-24 13:35:02 -0500 | [diff] [blame] | 1451 | if (fContext->fCaps.useNodePools()) { |
Brian Osman | 28f702c | 2021-02-02 11:52:07 -0500 | [diff] [blame] | 1452 | pool = Pool::Create(); |
| 1453 | pool->attachToThread(); |
| 1454 | } |
John Stiles | d120464 | 2021-02-17 16:30:02 -0500 | [diff] [blame] | 1455 | IRGenerator::IRBundle ir = fIRGenerator->convertProgram(baseModule, /*isBuiltinCode=*/false, |
| 1456 | textPtr->c_str(), textPtr->size(), |
| 1457 | externalFunctions); |
John Stiles | 270cec2 | 2021-02-17 12:59:36 -0500 | [diff] [blame] | 1458 | auto program = std::make_unique<Program>(std::move(textPtr), |
| 1459 | std::move(config), |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 1460 | fContext, |
| 1461 | std::move(ir.fElements), |
Brian Osman | 133724c | 2020-10-28 14:14:39 -0400 | [diff] [blame] | 1462 | std::move(ir.fSharedElements), |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 1463 | std::move(ir.fModifiers), |
| 1464 | std::move(ir.fSymbolTable), |
| 1465 | std::move(pool), |
| 1466 | ir.fInputs); |
| 1467 | bool success = false; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1468 | if (fErrorCount) { |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 1469 | // Do not return programs that failed to compile. |
John Stiles | 7247b48 | 2021-03-08 10:40:35 -0500 | [diff] [blame^] | 1470 | } else if (!this->optimize(*program)) { |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 1471 | // Do not return programs that failed to optimize. |
| 1472 | } else { |
| 1473 | // We have a successful program! |
| 1474 | success = true; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1475 | } |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 1476 | |
Brian Osman | 28f702c | 2021-02-02 11:52:07 -0500 | [diff] [blame] | 1477 | if (program->fPool) { |
| 1478 | program->fPool->detachFromThread(); |
| 1479 | } |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 1480 | return success ? std::move(program) : nullptr; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1481 | } |
| 1482 | |
John Stiles | bb1505f | 2021-02-12 09:17:53 -0500 | [diff] [blame] | 1483 | void Compiler::verifyStaticTests(const Program& program) { |
| 1484 | class StaticTestVerifier : public ProgramVisitor { |
| 1485 | public: |
| 1486 | StaticTestVerifier(ErrorReporter* r) : fReporter(r) {} |
| 1487 | |
| 1488 | using ProgramVisitor::visitProgramElement; |
| 1489 | |
| 1490 | bool visitStatement(const Statement& stmt) override { |
| 1491 | switch (stmt.kind()) { |
| 1492 | case Statement::Kind::kIf: |
| 1493 | if (stmt.as<IfStatement>().isStatic()) { |
| 1494 | fReporter->error(stmt.fOffset, "static if has non-static test"); |
| 1495 | } |
| 1496 | break; |
| 1497 | |
| 1498 | case Statement::Kind::kSwitch: |
| 1499 | if (stmt.as<SwitchStatement>().isStatic()) { |
| 1500 | fReporter->error(stmt.fOffset, "static switch has non-static test"); |
| 1501 | } |
| 1502 | break; |
| 1503 | |
| 1504 | default: |
| 1505 | break; |
| 1506 | } |
| 1507 | return INHERITED::visitStatement(stmt); |
| 1508 | } |
| 1509 | |
John Stiles | 59e3456 | 2021-02-12 16:56:39 -0500 | [diff] [blame] | 1510 | bool visitExpression(const Expression&) override { |
| 1511 | // We aren't looking for anything inside an Expression, so skip them entirely. |
| 1512 | return false; |
| 1513 | } |
| 1514 | |
John Stiles | bb1505f | 2021-02-12 09:17:53 -0500 | [diff] [blame] | 1515 | private: |
| 1516 | using INHERITED = ProgramVisitor; |
| 1517 | ErrorReporter* fReporter; |
| 1518 | }; |
| 1519 | |
| 1520 | // 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] | 1521 | if (fContext->fConfig->fSettings.fPermitInvalidStaticTests) { |
John Stiles | bb1505f | 2021-02-12 09:17:53 -0500 | [diff] [blame] | 1522 | return; |
| 1523 | } |
| 1524 | |
| 1525 | // Check all of the program's owned elements. (Built-in elements are assumed to be valid.) |
| 1526 | StaticTestVerifier visitor{this}; |
| 1527 | for (const std::unique_ptr<ProgramElement>& element : program.ownedElements()) { |
| 1528 | if (element->is<FunctionDefinition>()) { |
| 1529 | visitor.visitProgramElement(*element); |
| 1530 | } |
| 1531 | } |
| 1532 | } |
| 1533 | |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 1534 | bool Compiler::optimize(LoadedModule& module) { |
| 1535 | SkASSERT(!fErrorCount); |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 1536 | |
John Stiles | 270cec2 | 2021-02-17 12:59:36 -0500 | [diff] [blame] | 1537 | // Create a temporary program configuration with default settings. |
| 1538 | ProgramConfig config; |
| 1539 | config.fKind = module.fKind; |
John Stiles | a935c3f | 2021-02-25 10:35:49 -0500 | [diff] [blame] | 1540 | AutoProgramConfig autoConfig(fContext, &config); |
John Stiles | 270cec2 | 2021-02-17 12:59:36 -0500 | [diff] [blame] | 1541 | |
John Stiles | d120464 | 2021-02-17 16:30:02 -0500 | [diff] [blame] | 1542 | // Reset the Inliner. |
| 1543 | fInliner.reset(fModifiers.back().get()); |
John Stiles | 270cec2 | 2021-02-17 12:59:36 -0500 | [diff] [blame] | 1544 | |
| 1545 | std::unique_ptr<ProgramUsage> usage = Analysis::GetUsage(module); |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 1546 | |
| 1547 | while (fErrorCount == 0) { |
| 1548 | bool madeChanges = false; |
| 1549 | |
| 1550 | // Scan and optimize based on the control-flow graph for each function. |
John Stiles | 7247b48 | 2021-03-08 10:40:35 -0500 | [diff] [blame^] | 1551 | if (config.fSettings.fControlFlowAnalysis) { |
John Stiles | a935c3f | 2021-02-25 10:35:49 -0500 | [diff] [blame] | 1552 | for (const auto& element : module.fElements) { |
| 1553 | if (element->is<FunctionDefinition>()) { |
| 1554 | madeChanges |= this->scanCFG(element->as<FunctionDefinition>(), usage.get()); |
| 1555 | } |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 1556 | } |
| 1557 | } |
| 1558 | |
| 1559 | // Perform inline-candidate analysis and inline any functions deemed suitable. |
John Stiles | 7804758 | 2020-12-16 16:17:41 -0500 | [diff] [blame] | 1560 | madeChanges |= fInliner.analyze(module.fElements, module.fSymbols, usage.get()); |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 1561 | |
| 1562 | if (!madeChanges) { |
| 1563 | break; |
| 1564 | } |
| 1565 | } |
| 1566 | return fErrorCount == 0; |
| 1567 | } |
| 1568 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 1569 | bool Compiler::optimize(Program& program) { |
John Stiles | 7247b48 | 2021-03-08 10:40:35 -0500 | [diff] [blame^] | 1570 | // The optimizer only needs to run when it is enabled. |
| 1571 | if (!program.fConfig->fSettings.fOptimize) { |
| 1572 | return true; |
| 1573 | } |
| 1574 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 1575 | SkASSERT(!fErrorCount); |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 1576 | ProgramUsage* usage = program.fUsage.get(); |
John Stiles | 7954d6c | 2020-09-01 10:53:02 -0400 | [diff] [blame] | 1577 | |
Ethan Nicholas | 34b19c5 | 2020-09-14 11:33:47 -0400 | [diff] [blame] | 1578 | while (fErrorCount == 0) { |
| 1579 | bool madeChanges = false; |
John Stiles | 7954d6c | 2020-09-01 10:53:02 -0400 | [diff] [blame] | 1580 | |
Ethan Nicholas | 34b19c5 | 2020-09-14 11:33:47 -0400 | [diff] [blame] | 1581 | // Scan and optimize based on the control-flow graph for each function. |
John Stiles | 7247b48 | 2021-03-08 10:40:35 -0500 | [diff] [blame^] | 1582 | if (program.fConfig->fSettings.fControlFlowAnalysis) { |
John Stiles | 0c7312a | 2021-02-24 11:31:32 -0500 | [diff] [blame] | 1583 | for (const auto& element : program.ownedElements()) { |
| 1584 | if (element->is<FunctionDefinition>()) { |
| 1585 | madeChanges |= this->scanCFG(element->as<FunctionDefinition>(), usage); |
| 1586 | } |
Ethan Nicholas | 34b19c5 | 2020-09-14 11:33:47 -0400 | [diff] [blame] | 1587 | } |
| 1588 | } |
| 1589 | |
| 1590 | // Perform inline-candidate analysis and inline any functions deemed suitable. |
John Stiles | 7804758 | 2020-12-16 16:17:41 -0500 | [diff] [blame] | 1591 | madeChanges |= fInliner.analyze(program.ownedElements(), program.fSymbols, usage); |
Ethan Nicholas | 34b19c5 | 2020-09-14 11:33:47 -0400 | [diff] [blame] | 1592 | |
| 1593 | // Remove dead functions. We wait until after analysis so that we still report errors, |
| 1594 | // even in unused code. |
John Stiles | 270cec2 | 2021-02-17 12:59:36 -0500 | [diff] [blame] | 1595 | if (program.fConfig->fSettings.fRemoveDeadFunctions) { |
Brian Osman | 133724c | 2020-10-28 14:14:39 -0400 | [diff] [blame] | 1596 | auto isDeadFunction = [&](const ProgramElement* element) { |
| 1597 | if (!element->is<FunctionDefinition>()) { |
| 1598 | return false; |
| 1599 | } |
| 1600 | const FunctionDefinition& fn = element->as<FunctionDefinition>(); |
| 1601 | if (fn.declaration().name() != "main" && usage->get(fn.declaration()) == 0) { |
| 1602 | usage->remove(*element); |
| 1603 | madeChanges = true; |
| 1604 | return true; |
| 1605 | } |
| 1606 | return false; |
| 1607 | }; |
Ethan Nicholas | 34b19c5 | 2020-09-14 11:33:47 -0400 | [diff] [blame] | 1608 | program.fElements.erase( |
Brian Osman | 133724c | 2020-10-28 14:14:39 -0400 | [diff] [blame] | 1609 | std::remove_if(program.fElements.begin(), program.fElements.end(), |
Ethan Nicholas | 34b19c5 | 2020-09-14 11:33:47 -0400 | [diff] [blame] | 1610 | [&](const std::unique_ptr<ProgramElement>& element) { |
Brian Osman | 133724c | 2020-10-28 14:14:39 -0400 | [diff] [blame] | 1611 | return isDeadFunction(element.get()); |
Ethan Nicholas | 34b19c5 | 2020-09-14 11:33:47 -0400 | [diff] [blame] | 1612 | }), |
| 1613 | program.fElements.end()); |
Brian Osman | 133724c | 2020-10-28 14:14:39 -0400 | [diff] [blame] | 1614 | program.fSharedElements.erase( |
| 1615 | std::remove_if(program.fSharedElements.begin(), program.fSharedElements.end(), |
| 1616 | isDeadFunction), |
| 1617 | program.fSharedElements.end()); |
Ethan Nicholas | 34b19c5 | 2020-09-14 11:33:47 -0400 | [diff] [blame] | 1618 | } |
| 1619 | |
John Stiles | 270cec2 | 2021-02-17 12:59:36 -0500 | [diff] [blame] | 1620 | if (program.fConfig->fKind != ProgramKind::kFragmentProcessor) { |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 1621 | // Remove declarations of dead global variables |
Brian Osman | 133724c | 2020-10-28 14:14:39 -0400 | [diff] [blame] | 1622 | auto isDeadVariable = [&](const ProgramElement* element) { |
| 1623 | if (!element->is<GlobalVarDeclaration>()) { |
| 1624 | return false; |
| 1625 | } |
| 1626 | const GlobalVarDeclaration& global = element->as<GlobalVarDeclaration>(); |
| 1627 | const VarDeclaration& varDecl = global.declaration()->as<VarDeclaration>(); |
| 1628 | if (usage->isDead(varDecl.var())) { |
| 1629 | madeChanges = true; |
| 1630 | return true; |
| 1631 | } |
| 1632 | return false; |
| 1633 | }; |
Ethan Nicholas | 34b19c5 | 2020-09-14 11:33:47 -0400 | [diff] [blame] | 1634 | program.fElements.erase( |
| 1635 | std::remove_if(program.fElements.begin(), program.fElements.end(), |
| 1636 | [&](const std::unique_ptr<ProgramElement>& element) { |
Brian Osman | 133724c | 2020-10-28 14:14:39 -0400 | [diff] [blame] | 1637 | return isDeadVariable(element.get()); |
Ethan Nicholas | 34b19c5 | 2020-09-14 11:33:47 -0400 | [diff] [blame] | 1638 | }), |
| 1639 | program.fElements.end()); |
Brian Osman | 133724c | 2020-10-28 14:14:39 -0400 | [diff] [blame] | 1640 | program.fSharedElements.erase( |
| 1641 | std::remove_if(program.fSharedElements.begin(), program.fSharedElements.end(), |
| 1642 | isDeadVariable), |
| 1643 | program.fSharedElements.end()); |
Ethan Nicholas | 34b19c5 | 2020-09-14 11:33:47 -0400 | [diff] [blame] | 1644 | } |
John Stiles | 73a6bff | 2020-09-09 13:40:37 -0400 | [diff] [blame] | 1645 | |
Ethan Nicholas | 34b19c5 | 2020-09-14 11:33:47 -0400 | [diff] [blame] | 1646 | if (!madeChanges) { |
| 1647 | break; |
Ethan Nicholas | 0dc8087 | 2019-02-08 15:46:24 -0500 | [diff] [blame] | 1648 | } |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 1649 | } |
John Stiles | bb1505f | 2021-02-12 09:17:53 -0500 | [diff] [blame] | 1650 | |
| 1651 | if (fErrorCount == 0) { |
| 1652 | this->verifyStaticTests(program); |
| 1653 | } |
| 1654 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 1655 | return fErrorCount == 0; |
| 1656 | } |
| 1657 | |
Brian Osman | fb32ddf | 2019-06-18 10:14:20 -0400 | [diff] [blame] | 1658 | #if defined(SKSL_STANDALONE) || SK_SUPPORT_GPU |
| 1659 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 1660 | bool Compiler::toSPIRV(Program& program, OutputStream& out) { |
Ethan Nicholas | a6ae1f7 | 2017-03-16 09:56:54 -0400 | [diff] [blame] | 1661 | #ifdef SK_ENABLE_SPIRV_VALIDATION |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1662 | StringStream buffer; |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 1663 | AutoSource as(this, program.fSource.get()); |
Brian Osman | 8b43dad | 2020-10-09 13:31:42 -0400 | [diff] [blame] | 1664 | SPIRVCodeGenerator cg(fContext.get(), &program, this, &buffer); |
Ethan Nicholas | a6ae1f7 | 2017-03-16 09:56:54 -0400 | [diff] [blame] | 1665 | bool result = cg.generateCode(); |
John Stiles | 270cec2 | 2021-02-17 12:59:36 -0500 | [diff] [blame] | 1666 | if (result && program.fConfig->fSettings.fValidateSPIRV) { |
Ethan Nicholas | a6ae1f7 | 2017-03-16 09:56:54 -0400 | [diff] [blame] | 1667 | spvtools::SpirvTools tools(SPV_ENV_VULKAN_1_0); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1668 | const String& data = buffer.str(); |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 1669 | SkASSERT(0 == data.size() % 4); |
Brian Osman | 8d09d4a | 2020-11-24 15:51:06 -0500 | [diff] [blame] | 1670 | String errors; |
| 1671 | auto dumpmsg = [&errors](spv_message_level_t, const char*, const spv_position_t&, |
| 1672 | const char* m) { |
| 1673 | errors.appendf("SPIR-V validation error: %s\n", m); |
Ethan Nicholas | a6ae1f7 | 2017-03-16 09:56:54 -0400 | [diff] [blame] | 1674 | }; |
| 1675 | tools.SetMessageConsumer(dumpmsg); |
Brian Osman | 8d09d4a | 2020-11-24 15:51:06 -0500 | [diff] [blame] | 1676 | |
| 1677 | // Verify that the SPIR-V we produced is valid. At runtime, we will abort() with a message |
| 1678 | // explaining the error. In standalone mode (skslc), we will send the message, plus the |
| 1679 | // entire disassembled SPIR-V (for easier context & debugging) as *our* error message. |
| 1680 | result = tools.Validate((const uint32_t*) data.c_str(), data.size() / 4); |
| 1681 | |
| 1682 | if (!result) { |
| 1683 | #if defined(SKSL_STANDALONE) |
| 1684 | // Convert the string-stream to a SPIR-V disassembly. |
| 1685 | std::string disassembly; |
| 1686 | if (tools.Disassemble((const uint32_t*)data.data(), data.size() / 4, &disassembly)) { |
| 1687 | errors.append(disassembly); |
| 1688 | } |
| 1689 | this->error(-1, errors); |
| 1690 | #else |
| 1691 | SkDEBUGFAILF("%s", errors.c_str()); |
| 1692 | #endif |
| 1693 | } |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1694 | out.write(data.c_str(), data.size()); |
Ethan Nicholas | a6ae1f7 | 2017-03-16 09:56:54 -0400 | [diff] [blame] | 1695 | } |
| 1696 | #else |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 1697 | AutoSource as(this, program.fSource.get()); |
Brian Osman | 8b43dad | 2020-10-09 13:31:42 -0400 | [diff] [blame] | 1698 | SPIRVCodeGenerator cg(fContext.get(), &program, this, &out); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1699 | bool result = cg.generateCode(); |
Ethan Nicholas | a6ae1f7 | 2017-03-16 09:56:54 -0400 | [diff] [blame] | 1700 | #endif |
Ethan Nicholas | ce33f10 | 2016-12-09 17:22:59 -0500 | [diff] [blame] | 1701 | return result; |
| 1702 | } |
| 1703 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 1704 | bool Compiler::toSPIRV(Program& program, String* out) { |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1705 | StringStream buffer; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1706 | bool result = this->toSPIRV(program, buffer); |
| 1707 | if (result) { |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1708 | *out = buffer.str(); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1709 | } |
| 1710 | return result; |
| 1711 | } |
| 1712 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 1713 | bool Compiler::toGLSL(Program& program, OutputStream& out) { |
Brian Osman | d010f65 | 2021-02-24 13:59:39 -0500 | [diff] [blame] | 1714 | TRACE_EVENT0("skia.gpu", "SkSL::Compiler::toGLSL"); |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 1715 | AutoSource as(this, program.fSource.get()); |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 1716 | GLSLCodeGenerator cg(fContext.get(), &program, this, &out); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1717 | bool result = cg.generateCode(); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1718 | return result; |
| 1719 | } |
| 1720 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 1721 | bool Compiler::toGLSL(Program& program, String* out) { |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1722 | StringStream buffer; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1723 | bool result = this->toGLSL(program, buffer); |
| 1724 | if (result) { |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1725 | *out = buffer.str(); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1726 | } |
| 1727 | return result; |
| 1728 | } |
| 1729 | |
Brian Osman | c024391 | 2020-02-19 15:35:26 -0500 | [diff] [blame] | 1730 | bool Compiler::toHLSL(Program& program, String* out) { |
| 1731 | String spirv; |
| 1732 | if (!this->toSPIRV(program, &spirv)) { |
| 1733 | return false; |
| 1734 | } |
| 1735 | |
| 1736 | return SPIRVtoHLSL(spirv, out); |
| 1737 | } |
| 1738 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 1739 | bool Compiler::toMetal(Program& program, OutputStream& out) { |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 1740 | MetalCodeGenerator cg(fContext.get(), &program, this, &out); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1741 | bool result = cg.generateCode(); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1742 | return result; |
| 1743 | } |
| 1744 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 1745 | bool Compiler::toMetal(Program& program, String* out) { |
Timothy Liang | b8eeb80 | 2018-07-23 16:46:16 -0400 | [diff] [blame] | 1746 | StringStream buffer; |
| 1747 | bool result = this->toMetal(program, buffer); |
| 1748 | if (result) { |
| 1749 | *out = buffer.str(); |
| 1750 | } |
| 1751 | return result; |
| 1752 | } |
| 1753 | |
Greg Daniel | a28ea67 | 2020-09-25 11:12:56 -0400 | [diff] [blame] | 1754 | #if defined(SKSL_STANDALONE) || GR_TEST_UTILS |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 1755 | bool Compiler::toCPP(Program& program, String name, OutputStream& out) { |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 1756 | AutoSource as(this, program.fSource.get()); |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 1757 | CPPCodeGenerator cg(fContext.get(), &program, this, name, &out); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1758 | bool result = cg.generateCode(); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1759 | return result; |
| 1760 | } |
| 1761 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 1762 | bool Compiler::toH(Program& program, String name, OutputStream& out) { |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 1763 | AutoSource as(this, program.fSource.get()); |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 1764 | HCodeGenerator cg(fContext.get(), &program, this, name, &out); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1765 | bool result = cg.generateCode(); |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 1766 | return result; |
| 1767 | } |
Greg Daniel | a28ea67 | 2020-09-25 11:12:56 -0400 | [diff] [blame] | 1768 | #endif // defined(SKSL_STANDALONE) || GR_TEST_UTILS |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 1769 | |
Ethan Nicholas | 2a479a5 | 2020-08-18 16:29:45 -0400 | [diff] [blame] | 1770 | #endif // defined(SKSL_STANDALONE) || SK_SUPPORT_GPU |
Brian Osman | 2e29ab5 | 2019-09-20 12:19:11 -0400 | [diff] [blame] | 1771 | |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1772 | Position Compiler::position(int offset) { |
Ethan Nicholas | 3c72989 | 2020-12-07 12:47:17 -0500 | [diff] [blame] | 1773 | if (fSource && offset >= 0) { |
| 1774 | int line = 1; |
| 1775 | int column = 1; |
| 1776 | for (int i = 0; i < offset; i++) { |
| 1777 | if ((*fSource)[i] == '\n') { |
| 1778 | ++line; |
| 1779 | column = 1; |
| 1780 | } |
| 1781 | else { |
| 1782 | ++column; |
| 1783 | } |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1784 | } |
Ethan Nicholas | 3c72989 | 2020-12-07 12:47:17 -0500 | [diff] [blame] | 1785 | return Position(line, column); |
| 1786 | } else { |
| 1787 | return Position(-1, -1); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1788 | } |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1789 | } |
| 1790 | |
| 1791 | void Compiler::error(int offset, String msg) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1792 | fErrorCount++; |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1793 | Position pos = this->position(offset); |
John Stiles | 8d3642e | 2021-01-22 09:50:04 -0500 | [diff] [blame] | 1794 | fErrorTextLength.push_back(fErrorText.length()); |
Ethan Nicholas | 3c72989 | 2020-12-07 12:47:17 -0500 | [diff] [blame] | 1795 | fErrorText += "error: " + (pos.fLine >= 1 ? to_string(pos.fLine) + ": " : "") + msg + "\n"; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1796 | } |
| 1797 | |
John Stiles | 8d3642e | 2021-01-22 09:50:04 -0500 | [diff] [blame] | 1798 | void Compiler::setErrorCount(int c) { |
| 1799 | if (c < fErrorCount) { |
| 1800 | fErrorText.resize(fErrorTextLength[c]); |
| 1801 | fErrorTextLength.resize(c); |
| 1802 | fErrorCount = c; |
| 1803 | } |
| 1804 | } |
| 1805 | |
Ethan Nicholas | 9504614 | 2021-01-07 10:57:27 -0500 | [diff] [blame] | 1806 | String Compiler::errorText(bool showCount) { |
| 1807 | if (showCount) { |
| 1808 | this->writeErrorCount(); |
| 1809 | } |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 1810 | fErrorCount = 0; |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1811 | String result = fErrorText; |
Ethan Nicholas | 9504614 | 2021-01-07 10:57:27 -0500 | [diff] [blame] | 1812 | fErrorText = ""; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1813 | return result; |
| 1814 | } |
| 1815 | |
| 1816 | void Compiler::writeErrorCount() { |
| 1817 | if (fErrorCount) { |
| 1818 | fErrorText += to_string(fErrorCount) + " error"; |
| 1819 | if (fErrorCount > 1) { |
| 1820 | fErrorText += "s"; |
| 1821 | } |
| 1822 | fErrorText += "\n"; |
| 1823 | } |
| 1824 | } |
| 1825 | |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 1826 | } // namespace SkSL |