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 | 47c0a74 | 2021-02-09 09:30:35 -0500 | [diff] [blame] | 75 | using RefKind = VariableReference::RefKind; |
| 76 | |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 77 | class AutoSource { |
| 78 | public: |
| 79 | AutoSource(Compiler* compiler, const String* source) |
| 80 | : fCompiler(compiler), fOldSource(fCompiler->fSource) { |
| 81 | fCompiler->fSource = source; |
| 82 | } |
| 83 | |
| 84 | ~AutoSource() { fCompiler->fSource = fOldSource; } |
| 85 | |
| 86 | Compiler* fCompiler; |
| 87 | const String* fOldSource; |
| 88 | }; |
| 89 | |
John Stiles | d6a5f449 | 2021-02-11 15:46:11 -0500 | [diff] [blame] | 90 | Compiler::Compiler(const ShaderCapsClass* caps) |
John Stiles | b30151e | 2021-01-11 16:13:08 -0500 | [diff] [blame] | 91 | : fContext(std::make_shared<Context>(/*errors=*/*this)) |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 92 | , fCaps(caps) |
John Stiles | 7b92044 | 2020-12-17 10:43:41 -0500 | [diff] [blame] | 93 | , fInliner(fContext.get()) |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 94 | , fErrorCount(0) { |
| 95 | SkASSERT(fCaps); |
John Stiles | 7c3515b | 2020-10-16 18:38:39 -0400 | [diff] [blame] | 96 | fRootSymbolTable = std::make_shared<SymbolTable>(this, /*builtin=*/true); |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 97 | fPrivateSymbolTable = std::make_shared<SymbolTable>(fRootSymbolTable, /*builtin=*/true); |
John Stiles | b30151e | 2021-01-11 16:13:08 -0500 | [diff] [blame] | 98 | fIRGenerator = std::make_unique<IRGenerator>(fContext.get(), fCaps); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 99 | |
John Stiles | 54e7c05 | 2021-01-11 14:22:36 -0500 | [diff] [blame] | 100 | #define TYPE(t) fContext->fTypes.f ## t .get() |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 101 | |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 102 | const SkSL::Symbol* rootTypes[] = { |
| 103 | TYPE(Void), |
Brian Salomon | bf7b620 | 2016-11-11 16:08:03 -0500 | [diff] [blame] | 104 | |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 105 | TYPE( Float), TYPE( Float2), TYPE( Float3), TYPE( Float4), |
| 106 | TYPE( Half), TYPE( Half2), TYPE( Half3), TYPE( Half4), |
| 107 | TYPE( Int), TYPE( Int2), TYPE( Int3), TYPE( Int4), |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 108 | TYPE( Bool), TYPE( Bool2), TYPE( Bool3), TYPE( Bool4), |
Brian Salomon | 2a51de8 | 2016-11-16 12:06:01 -0500 | [diff] [blame] | 109 | |
Brian Osman | c0f2b64 | 2020-12-22 13:35:55 -0500 | [diff] [blame] | 110 | TYPE(Float2x2), TYPE(Float3x3), TYPE(Float4x4), |
Brian Osman | c63f431 | 2020-12-23 11:44:14 -0500 | [diff] [blame] | 111 | TYPE( Half2x2), TYPE( Half3x3), TYPE(Half4x4), |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 112 | |
Brian Osman | c63f431 | 2020-12-23 11:44:14 -0500 | [diff] [blame] | 113 | TYPE(SquareMat), TYPE(SquareHMat), |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 114 | |
Brian Osman | 20fad32 | 2020-12-23 12:42:33 -0500 | [diff] [blame] | 115 | TYPE(GenType), TYPE(GenHType), TYPE(GenIType), TYPE(GenBType), |
| 116 | TYPE(Vec), TYPE(HVec), TYPE(IVec), TYPE(BVec), |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 117 | |
| 118 | TYPE(FragmentProcessor), |
| 119 | }; |
| 120 | |
| 121 | const SkSL::Symbol* privateTypes[] = { |
Brian Osman | 20fad32 | 2020-12-23 12:42:33 -0500 | [diff] [blame] | 122 | TYPE( UInt), TYPE( UInt2), TYPE( UInt3), TYPE( UInt4), |
| 123 | TYPE( Short), TYPE( Short2), TYPE( Short3), TYPE( Short4), |
| 124 | TYPE(UShort), TYPE(UShort2), TYPE(UShort3), TYPE(UShort4), |
| 125 | TYPE( Byte), TYPE( Byte2), TYPE( Byte3), TYPE( Byte4), |
| 126 | TYPE( UByte), TYPE( UByte2), TYPE( UByte3), TYPE( UByte4), |
| 127 | |
| 128 | TYPE(GenUType), TYPE(UVec), |
| 129 | TYPE(SVec), TYPE(USVec), TYPE(ByteVec), TYPE(UByteVec), |
| 130 | |
Brian Osman | c0f2b64 | 2020-12-22 13:35:55 -0500 | [diff] [blame] | 131 | TYPE(Float2x3), TYPE(Float2x4), |
| 132 | TYPE(Float3x2), TYPE(Float3x4), |
| 133 | TYPE(Float4x2), TYPE(Float4x3), |
| 134 | |
Brian Osman | c63f431 | 2020-12-23 11:44:14 -0500 | [diff] [blame] | 135 | TYPE(Half2x3), TYPE(Half2x4), |
| 136 | TYPE(Half3x2), TYPE(Half3x4), |
| 137 | TYPE(Half4x2), TYPE(Half4x3), |
| 138 | |
Brian Osman | c0f2b64 | 2020-12-22 13:35:55 -0500 | [diff] [blame] | 139 | TYPE(Mat), TYPE(HMat), |
| 140 | |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 141 | TYPE(Sampler1D), TYPE(Sampler2D), TYPE(Sampler3D), |
| 142 | TYPE(SamplerExternalOES), |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 143 | TYPE(Sampler2DRect), |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 144 | |
| 145 | TYPE(ISampler2D), |
| 146 | TYPE(Image2D), TYPE(IImage2D), |
| 147 | TYPE(SubpassInput), TYPE(SubpassInputMS), |
| 148 | |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 149 | TYPE(Sampler), |
| 150 | TYPE(Texture2D), |
| 151 | }; |
| 152 | |
| 153 | for (const SkSL::Symbol* type : rootTypes) { |
| 154 | fRootSymbolTable->addWithoutOwnership(type); |
| 155 | } |
| 156 | for (const SkSL::Symbol* type : privateTypes) { |
| 157 | fPrivateSymbolTable->addWithoutOwnership(type); |
| 158 | } |
| 159 | |
| 160 | #undef TYPE |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 161 | |
Brian Osman | 3887a01 | 2020-09-30 13:22:27 -0400 | [diff] [blame] | 162 | // sk_Caps is "builtin", but all references to it are resolved to Settings, so we don't need to |
| 163 | // 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] | 164 | fPrivateSymbolTable->add( |
| 165 | std::make_unique<Variable>(/*offset=*/-1, |
John Stiles | 586df95 | 2020-11-12 18:27:13 -0500 | [diff] [blame] | 166 | fIRGenerator->fModifiers->addToPool(Modifiers()), |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 167 | "sk_Caps", |
John Stiles | 54e7c05 | 2021-01-11 14:22:36 -0500 | [diff] [blame] | 168 | fContext->fTypes.fSkCaps.get(), |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 169 | /*builtin=*/false, |
| 170 | Variable::Storage::kGlobal)); |
Ethan Nicholas | 3605ace | 2016-11-21 15:59:48 -0500 | [diff] [blame] | 171 | |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 172 | fRootModule = {fRootSymbolTable, /*fIntrinsics=*/nullptr}; |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 173 | fPrivateModule = {fPrivateSymbolTable, /*fIntrinsics=*/nullptr}; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 174 | } |
| 175 | |
John Stiles | dd13dba | 2020-10-29 10:45:34 -0400 | [diff] [blame] | 176 | Compiler::~Compiler() {} |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 177 | |
Brian Osman | 5626998 | 2020-11-20 12:38:07 -0500 | [diff] [blame] | 178 | const ParsedModule& Compiler::loadGPUModule() { |
| 179 | if (!fGPUModule.fSymbols) { |
John Stiles | dbd4e6f | 2021-02-16 13:29:15 -0500 | [diff] [blame] | 180 | fGPUModule = this->parseModule(ProgramKind::kFragment, MODULE_DATA(gpu), fPrivateModule); |
Brian Osman | 5626998 | 2020-11-20 12:38:07 -0500 | [diff] [blame] | 181 | } |
| 182 | return fGPUModule; |
| 183 | } |
| 184 | |
| 185 | const ParsedModule& Compiler::loadFragmentModule() { |
| 186 | if (!fFragmentModule.fSymbols) { |
John Stiles | dbd4e6f | 2021-02-16 13:29:15 -0500 | [diff] [blame] | 187 | fFragmentModule = this->parseModule(ProgramKind::kFragment, MODULE_DATA(frag), |
Brian Osman | 5626998 | 2020-11-20 12:38:07 -0500 | [diff] [blame] | 188 | this->loadGPUModule()); |
| 189 | } |
| 190 | return fFragmentModule; |
| 191 | } |
| 192 | |
| 193 | const ParsedModule& Compiler::loadVertexModule() { |
| 194 | if (!fVertexModule.fSymbols) { |
John Stiles | dbd4e6f | 2021-02-16 13:29:15 -0500 | [diff] [blame] | 195 | fVertexModule = this->parseModule(ProgramKind::kVertex, MODULE_DATA(vert), |
Brian Osman | 5626998 | 2020-11-20 12:38:07 -0500 | [diff] [blame] | 196 | this->loadGPUModule()); |
| 197 | } |
| 198 | return fVertexModule; |
| 199 | } |
| 200 | |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 201 | const ParsedModule& Compiler::loadGeometryModule() { |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 202 | if (!fGeometryModule.fSymbols) { |
John Stiles | dbd4e6f | 2021-02-16 13:29:15 -0500 | [diff] [blame] | 203 | fGeometryModule = this->parseModule(ProgramKind::kGeometry, MODULE_DATA(geom), |
Brian Osman | 5626998 | 2020-11-20 12:38:07 -0500 | [diff] [blame] | 204 | this->loadGPUModule()); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 205 | } |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 206 | return fGeometryModule; |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 207 | } |
| 208 | |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 209 | const ParsedModule& Compiler::loadFPModule() { |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 210 | if (!fFPModule.fSymbols) { |
John Stiles | dbd4e6f | 2021-02-16 13:29:15 -0500 | [diff] [blame] | 211 | fFPModule = this->parseModule(ProgramKind::kFragmentProcessor, MODULE_DATA(fp), |
Brian Osman | 5626998 | 2020-11-20 12:38:07 -0500 | [diff] [blame] | 212 | this->loadGPUModule()); |
Brian Osman | 8e2ef02 | 2020-09-30 13:26:43 -0400 | [diff] [blame] | 213 | } |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 214 | return fFPModule; |
Brian Osman | 8e2ef02 | 2020-09-30 13:26:43 -0400 | [diff] [blame] | 215 | } |
| 216 | |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 217 | const ParsedModule& Compiler::loadPublicModule() { |
| 218 | if (!fPublicModule.fSymbols) { |
John Stiles | dbd4e6f | 2021-02-16 13:29:15 -0500 | [diff] [blame] | 219 | fPublicModule = this->parseModule(ProgramKind::kGeneric, MODULE_DATA(public), fRootModule); |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 220 | } |
| 221 | return fPublicModule; |
| 222 | } |
| 223 | |
Brian Osman | 9194675 | 2020-12-21 13:20:40 -0500 | [diff] [blame] | 224 | const ParsedModule& Compiler::loadRuntimeEffectModule() { |
| 225 | if (!fRuntimeEffectModule.fSymbols) { |
John Stiles | dbd4e6f | 2021-02-16 13:29:15 -0500 | [diff] [blame] | 226 | fRuntimeEffectModule = this->parseModule(ProgramKind::kRuntimeEffect, MODULE_DATA(runtime), |
Brian Osman | 9194675 | 2020-12-21 13:20:40 -0500 | [diff] [blame] | 227 | this->loadPublicModule()); |
Brian Osman | f1319c3 | 2020-10-13 09:34:23 -0400 | [diff] [blame] | 228 | |
Brian Osman | 9194675 | 2020-12-21 13:20:40 -0500 | [diff] [blame] | 229 | // 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] | 230 | fRuntimeEffectModule.fSymbols->addAlias("shader", fContext->fTypes.fFragmentProcessor.get()); |
Brian Osman | f1319c3 | 2020-10-13 09:34:23 -0400 | [diff] [blame] | 231 | |
John Stiles | 54e7c05 | 2021-01-11 14:22:36 -0500 | [diff] [blame] | 232 | fRuntimeEffectModule.fSymbols->addAlias("vec2", fContext->fTypes.fFloat2.get()); |
| 233 | fRuntimeEffectModule.fSymbols->addAlias("vec3", fContext->fTypes.fFloat3.get()); |
| 234 | fRuntimeEffectModule.fSymbols->addAlias("vec4", fContext->fTypes.fFloat4.get()); |
Brian Osman | f1319c3 | 2020-10-13 09:34:23 -0400 | [diff] [blame] | 235 | |
John Stiles | 54e7c05 | 2021-01-11 14:22:36 -0500 | [diff] [blame] | 236 | fRuntimeEffectModule.fSymbols->addAlias("bvec2", fContext->fTypes.fBool2.get()); |
| 237 | fRuntimeEffectModule.fSymbols->addAlias("bvec3", fContext->fTypes.fBool3.get()); |
| 238 | fRuntimeEffectModule.fSymbols->addAlias("bvec4", fContext->fTypes.fBool4.get()); |
Brian Osman | f1319c3 | 2020-10-13 09:34:23 -0400 | [diff] [blame] | 239 | |
John Stiles | 54e7c05 | 2021-01-11 14:22:36 -0500 | [diff] [blame] | 240 | fRuntimeEffectModule.fSymbols->addAlias("mat2", fContext->fTypes.fFloat2x2.get()); |
| 241 | fRuntimeEffectModule.fSymbols->addAlias("mat3", fContext->fTypes.fFloat3x3.get()); |
| 242 | fRuntimeEffectModule.fSymbols->addAlias("mat4", fContext->fTypes.fFloat4x4.get()); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 243 | } |
Brian Osman | 9194675 | 2020-12-21 13:20:40 -0500 | [diff] [blame] | 244 | return fRuntimeEffectModule; |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 245 | } |
| 246 | |
John Stiles | dbd4e6f | 2021-02-16 13:29:15 -0500 | [diff] [blame] | 247 | const ParsedModule& Compiler::moduleForProgramKind(ProgramKind kind) { |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 248 | switch (kind) { |
John Stiles | dbd4e6f | 2021-02-16 13:29:15 -0500 | [diff] [blame] | 249 | case ProgramKind::kVertex: return this->loadVertexModule(); break; |
| 250 | case ProgramKind::kFragment: return this->loadFragmentModule(); break; |
| 251 | case ProgramKind::kGeometry: return this->loadGeometryModule(); break; |
| 252 | case ProgramKind::kFragmentProcessor: return this->loadFPModule(); break; |
| 253 | case ProgramKind::kRuntimeEffect: return this->loadRuntimeEffectModule(); break; |
| 254 | case ProgramKind::kGeneric: return this->loadPublicModule(); break; |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 255 | } |
| 256 | SkUNREACHABLE; |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 257 | } |
| 258 | |
John Stiles | dbd4e6f | 2021-02-16 13:29:15 -0500 | [diff] [blame] | 259 | LoadedModule Compiler::loadModule(ProgramKind kind, |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 260 | ModuleData data, |
| 261 | std::shared_ptr<SymbolTable> base) { |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 262 | if (!base) { |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 263 | // NOTE: This is a workaround. The only time 'base' is null is when dehydrating includes. |
| 264 | // In that case, skslc doesn't know which module it's preparing, nor what the correct base |
| 265 | // module is. We can't use 'Root', because many GPU intrinsics reference private types, |
| 266 | // like samplers or textures. Today, 'Private' does contain the union of all known types, |
| 267 | // so this is safe. If we ever have types that only exist in 'Public' (for example), this |
| 268 | // logic needs to be smarter (by choosing the correct base for the module we're compiling). |
| 269 | base = fPrivateSymbolTable; |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | #if defined(SKSL_STANDALONE) |
| 273 | SkASSERT(data.fPath); |
| 274 | std::ifstream in(data.fPath); |
Brian Osman | e498b3c | 2020-09-23 14:42:11 -0400 | [diff] [blame] | 275 | std::unique_ptr<String> text = std::make_unique<String>(std::istreambuf_iterator<char>(in), |
| 276 | std::istreambuf_iterator<char>()); |
Ethan Nicholas | b33fa3f | 2020-08-06 13:00:19 -0400 | [diff] [blame] | 277 | if (in.rdstate()) { |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 278 | printf("error reading %s\n", data.fPath); |
Ethan Nicholas | b33fa3f | 2020-08-06 13:00:19 -0400 | [diff] [blame] | 279 | abort(); |
| 280 | } |
Brian Osman | e498b3c | 2020-09-23 14:42:11 -0400 | [diff] [blame] | 281 | const String* source = fRootSymbolTable->takeOwnershipOfString(std::move(text)); |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 282 | AutoSource as(this, source); |
John Stiles | d120464 | 2021-02-17 16:30:02 -0500 | [diff] [blame] | 283 | |
John Stiles | 881a10c | 2020-09-19 10:13:24 -0400 | [diff] [blame] | 284 | SkASSERT(fIRGenerator->fCanInline); |
| 285 | fIRGenerator->fCanInline = false; |
John Stiles | d120464 | 2021-02-17 16:30:02 -0500 | [diff] [blame] | 286 | |
| 287 | ProgramConfig config; |
| 288 | config.fKind = kind; |
| 289 | config.fSettings.fReplaceSettings = false; |
| 290 | |
| 291 | fContext->fConfig = &config; |
| 292 | SK_AT_SCOPE_EXIT(fContext->fConfig = nullptr); |
John Stiles | dbd4e6f | 2021-02-16 13:29:15 -0500 | [diff] [blame] | 293 | |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 294 | ParsedModule baseModule = {base, /*fIntrinsics=*/nullptr}; |
John Stiles | d120464 | 2021-02-17 16:30:02 -0500 | [diff] [blame] | 295 | IRGenerator::IRBundle ir = fIRGenerator->convertProgram(baseModule, /*isBuiltinCode=*/true, |
| 296 | source->c_str(), source->length(), |
| 297 | /*externalFunctions=*/nullptr); |
Brian Osman | 133724c | 2020-10-28 14:14:39 -0400 | [diff] [blame] | 298 | SkASSERT(ir.fSharedElements.empty()); |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 299 | LoadedModule module = { kind, std::move(ir.fSymbolTable), std::move(ir.fElements) }; |
John Stiles | 881a10c | 2020-09-19 10:13:24 -0400 | [diff] [blame] | 300 | fIRGenerator->fCanInline = true; |
Ethan Nicholas | 8da1e65 | 2019-05-24 11:01:59 -0400 | [diff] [blame] | 301 | if (this->fErrorCount) { |
| 302 | printf("Unexpected errors: %s\n", this->fErrorText.c_str()); |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 303 | SkDEBUGFAILF("%s %s\n", data.fPath, this->fErrorText.c_str()); |
Ethan Nicholas | 8da1e65 | 2019-05-24 11:01:59 -0400 | [diff] [blame] | 304 | } |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 305 | fModifiers.push_back(std::move(ir.fModifiers)); |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 306 | #else |
| 307 | SkASSERT(data.fData && (data.fSize != 0)); |
| 308 | Rehydrator rehydrator(fContext.get(), fIRGenerator->fModifiers.get(), base, this, |
| 309 | data.fData, data.fSize); |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 310 | LoadedModule module = { kind, rehydrator.symbolTable(), rehydrator.elements() }; |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 311 | fModifiers.push_back(fIRGenerator->releaseModifiers()); |
| 312 | #endif |
| 313 | |
| 314 | return module; |
| 315 | } |
| 316 | |
John Stiles | dbd4e6f | 2021-02-16 13:29:15 -0500 | [diff] [blame] | 317 | ParsedModule Compiler::parseModule(ProgramKind kind, ModuleData data, const ParsedModule& base) { |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 318 | LoadedModule module = this->loadModule(kind, data, base.fSymbols); |
| 319 | this->optimize(module); |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 320 | |
| 321 | // For modules that just declare (but don't define) intrinsic functions, there will be no new |
| 322 | // 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] | 323 | if (module.fElements.empty()) { |
| 324 | return {module.fSymbols, base.fIntrinsics}; |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | auto intrinsics = std::make_shared<IRIntrinsicMap>(base.fIntrinsics.get()); |
| 328 | |
| 329 | // Now, transfer all of the program elements to an intrinsic map. This maps certain types of |
| 330 | // global objects to the declaring ProgramElement. |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 331 | for (std::unique_ptr<ProgramElement>& element : module.fElements) { |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 332 | switch (element->kind()) { |
| 333 | case ProgramElement::Kind::kFunction: { |
| 334 | const FunctionDefinition& f = element->as<FunctionDefinition>(); |
Ethan Nicholas | 0a5d096 | 2020-10-14 13:33:18 -0400 | [diff] [blame] | 335 | SkASSERT(f.declaration().isBuiltin()); |
| 336 | intrinsics->insertOrDie(f.declaration().description(), std::move(element)); |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 337 | break; |
| 338 | } |
John Stiles | 569249b | 2020-11-03 12:18:22 -0500 | [diff] [blame] | 339 | case ProgramElement::Kind::kFunctionPrototype: { |
| 340 | // These are already in the symbol table. |
| 341 | break; |
| 342 | } |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 343 | case ProgramElement::Kind::kEnum: { |
| 344 | const Enum& e = element->as<Enum>(); |
| 345 | SkASSERT(e.isBuiltin()); |
| 346 | intrinsics->insertOrDie(e.typeName(), std::move(element)); |
| 347 | break; |
| 348 | } |
| 349 | case ProgramElement::Kind::kGlobalVar: { |
Ethan Nicholas | c51f33e | 2020-10-13 13:49:44 -0400 | [diff] [blame] | 350 | const GlobalVarDeclaration& global = element->as<GlobalVarDeclaration>(); |
| 351 | const Variable& var = global.declaration()->as<VarDeclaration>().var(); |
| 352 | SkASSERT(var.isBuiltin()); |
| 353 | intrinsics->insertOrDie(var.name(), std::move(element)); |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 354 | break; |
| 355 | } |
| 356 | case ProgramElement::Kind::kInterfaceBlock: { |
Ethan Nicholas | eaf4788 | 2020-10-15 10:10:08 -0400 | [diff] [blame] | 357 | const Variable& var = element->as<InterfaceBlock>().variable(); |
| 358 | SkASSERT(var.isBuiltin()); |
| 359 | intrinsics->insertOrDie(var.name(), std::move(element)); |
Brian Osman | 3d87e9f | 2020-10-08 11:50:22 -0400 | [diff] [blame] | 360 | break; |
| 361 | } |
| 362 | default: |
| 363 | printf("Unsupported element: %s\n", element->description().c_str()); |
| 364 | SkASSERT(false); |
| 365 | break; |
| 366 | } |
| 367 | } |
| 368 | |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 369 | return {module.fSymbols, std::move(intrinsics)}; |
Ethan Nicholas | 8da1e65 | 2019-05-24 11:01:59 -0400 | [diff] [blame] | 370 | } |
| 371 | |
John Stiles | e615000 | 2020-10-05 12:03:53 -0400 | [diff] [blame] | 372 | void Compiler::scanCFG(CFG* cfg, BlockId blockId, SkBitSet* processedSet) { |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 373 | BasicBlock& block = cfg->fBlocks[blockId]; |
| 374 | |
| 375 | // compute definitions after this block |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 376 | DefinitionMap after = block.fBefore; |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 377 | for (const BasicBlock::Node& n : block.fNodes) { |
John Stiles | e8a2492 | 2021-02-08 17:54:08 -0500 | [diff] [blame] | 378 | after.addDefinitions(*fContext, n); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | // propagate definitions to exits |
| 382 | for (BlockId exitId : block.fExits) { |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 383 | if (exitId == blockId) { |
| 384 | continue; |
| 385 | } |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 386 | BasicBlock& exit = cfg->fBlocks[exitId]; |
John Stiles | 65b4827 | 2020-12-22 17:18:34 -0500 | [diff] [blame] | 387 | for (const auto& [var, e1] : after) { |
John Stiles | 796cdb7 | 2020-10-08 12:06:53 -0400 | [diff] [blame] | 388 | std::unique_ptr<Expression>** exitDef = exit.fBefore.find(var); |
| 389 | if (!exitDef) { |
John Stiles | e615000 | 2020-10-05 12:03:53 -0400 | [diff] [blame] | 390 | // exit has no definition for it, just copy it and reprocess exit block |
| 391 | processedSet->reset(exitId); |
John Stiles | e8a2492 | 2021-02-08 17:54:08 -0500 | [diff] [blame] | 392 | exit.fBefore.set(var, e1); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 393 | } else { |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 394 | // exit has a (possibly different) value already defined |
John Stiles | 796cdb7 | 2020-10-08 12:06:53 -0400 | [diff] [blame] | 395 | std::unique_ptr<Expression>* e2 = *exitDef; |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 396 | if (e1 != e2) { |
John Stiles | e615000 | 2020-10-05 12:03:53 -0400 | [diff] [blame] | 397 | // definition has changed, merge and reprocess the exit block |
| 398 | processedSet->reset(exitId); |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 399 | if (e1 && e2) { |
John Stiles | 796cdb7 | 2020-10-08 12:06:53 -0400 | [diff] [blame] | 400 | *exitDef = (std::unique_ptr<Expression>*)&fContext->fDefined_Expression; |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 401 | } else { |
John Stiles | 796cdb7 | 2020-10-08 12:06:53 -0400 | [diff] [blame] | 402 | *exitDef = nullptr; |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 403 | } |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 404 | } |
| 405 | } |
John Stiles | 65b4827 | 2020-12-22 17:18:34 -0500 | [diff] [blame] | 406 | } |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 407 | } |
| 408 | } |
| 409 | |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 410 | /** |
| 411 | * Returns true if assigning to this lvalue has no effect. |
| 412 | */ |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 413 | static bool is_dead(const Expression& lvalue, ProgramUsage* usage) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 414 | switch (lvalue.kind()) { |
| 415 | case Expression::Kind::kVariableReference: |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 416 | return usage->isDead(*lvalue.as<VariableReference>().variable()); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 417 | case Expression::Kind::kSwizzle: |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 418 | return is_dead(*lvalue.as<Swizzle>().base(), usage); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 419 | case Expression::Kind::kFieldAccess: |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 420 | return is_dead(*lvalue.as<FieldAccess>().base(), usage); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 421 | case Expression::Kind::kIndex: { |
John Stiles | a5a97b4 | 2020-08-18 11:19:07 -0400 | [diff] [blame] | 422 | const IndexExpression& idx = lvalue.as<IndexExpression>(); |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 423 | return is_dead(*idx.base(), usage) && |
Ethan Nicholas | 2a4952d | 2020-10-08 15:35:56 -0400 | [diff] [blame] | 424 | !idx.index()->hasProperty(Expression::Property::kSideEffects); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 425 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 426 | case Expression::Kind::kTernary: { |
John Stiles | a5a97b4 | 2020-08-18 11:19:07 -0400 | [diff] [blame] | 427 | const TernaryExpression& t = lvalue.as<TernaryExpression>(); |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 428 | return !t.test()->hasSideEffects() && |
| 429 | is_dead(*t.ifTrue(), usage) && |
| 430 | is_dead(*t.ifFalse(), usage); |
Ethan Nicholas | a583b81 | 2018-01-18 13:32:11 -0500 | [diff] [blame] | 431 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 432 | default: |
John Stiles | eada7bc | 2021-02-02 16:29:32 -0500 | [diff] [blame] | 433 | SkDEBUGFAILF("invalid lvalue: %s\n", lvalue.description().c_str()); |
Ethan Nicholas | 2a099da | 2020-01-02 14:40:54 -0500 | [diff] [blame] | 434 | return false; |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 435 | } |
| 436 | } |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 437 | |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 438 | /** |
| 439 | * Returns true if this is an assignment which can be collapsed down to just the right hand side due |
| 440 | * to a dead target and lack of side effects on the left hand side. |
| 441 | */ |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 442 | static bool dead_assignment(const BinaryExpression& b, ProgramUsage* usage) { |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 443 | if (!b.getOperator().isAssignment()) { |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 444 | return false; |
| 445 | } |
John Stiles | 2d4f959 | 2020-10-30 10:29:12 -0400 | [diff] [blame] | 446 | return is_dead(*b.left(), usage); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 447 | } |
| 448 | |
John Stiles | 0ac6c15 | 2021-02-10 14:04:24 -0500 | [diff] [blame] | 449 | /** |
| 450 | * Returns true if both expression trees are the same. The left side is expected to be an lvalue. |
| 451 | * This only needs to check for trees that can plausibly terminate in a variable, so some basic |
| 452 | * candidates like `FloatLiteral` are missing. |
| 453 | */ |
| 454 | static bool is_matching_expression_tree(const Expression& left, const Expression& right) { |
| 455 | if (left.kind() != right.kind() || left.type() != right.type()) { |
| 456 | return false; |
| 457 | } |
| 458 | |
| 459 | switch (left.kind()) { |
| 460 | case Expression::Kind::kIntLiteral: |
| 461 | return left.as<IntLiteral>().value() == right.as<IntLiteral>().value(); |
| 462 | |
| 463 | case Expression::Kind::kFieldAccess: |
| 464 | return left.as<FieldAccess>().fieldIndex() == right.as<FieldAccess>().fieldIndex() && |
| 465 | is_matching_expression_tree(*left.as<FieldAccess>().base(), |
| 466 | *right.as<FieldAccess>().base()); |
| 467 | |
| 468 | case Expression::Kind::kIndex: |
| 469 | return is_matching_expression_tree(*left.as<IndexExpression>().index(), |
| 470 | *right.as<IndexExpression>().index()) && |
| 471 | is_matching_expression_tree(*left.as<IndexExpression>().base(), |
| 472 | *right.as<IndexExpression>().base()); |
| 473 | |
| 474 | case Expression::Kind::kSwizzle: |
| 475 | return left.as<Swizzle>().components() == right.as<Swizzle>().components() && |
| 476 | is_matching_expression_tree(*left.as<Swizzle>().base(), |
| 477 | *right.as<Swizzle>().base()); |
| 478 | |
| 479 | case Expression::Kind::kVariableReference: |
| 480 | return left.as<VariableReference>().variable() == |
| 481 | right.as<VariableReference>().variable(); |
| 482 | |
| 483 | default: |
| 484 | return false; |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | static bool self_assignment(const BinaryExpression& b) { |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 489 | return b.getOperator().kind() == Token::Kind::TK_EQ && |
John Stiles | 0ac6c15 | 2021-02-10 14:04:24 -0500 | [diff] [blame] | 490 | is_matching_expression_tree(*b.left(), *b.right()); |
| 491 | } |
| 492 | |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 493 | void Compiler::computeDataFlow(CFG* cfg) { |
John Stiles | e8a2492 | 2021-02-08 17:54:08 -0500 | [diff] [blame] | 494 | cfg->fBlocks[cfg->fStart].fBefore.computeStartState(*cfg); |
John Stiles | e615000 | 2020-10-05 12:03:53 -0400 | [diff] [blame] | 495 | |
| 496 | // We set bits in the "processed" set after a block has been scanned. |
| 497 | SkBitSet processedSet(cfg->fBlocks.size()); |
| 498 | while (SkBitSet::OptionalIndex blockId = processedSet.findFirstUnset()) { |
| 499 | processedSet.set(*blockId); |
| 500 | this->scanCFG(cfg, *blockId, &processedSet); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 501 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | /** |
| 505 | * Attempts to replace the expression pointed to by iter with a new one (in both the CFG and the |
| 506 | * IR). If the expression can be cleanly removed, returns true and updates the iterator to point to |
| 507 | * the newly-inserted element. Otherwise updates only the IR and returns false (and the CFG will |
| 508 | * need to be regenerated). |
| 509 | */ |
John Stiles | afbf899 | 2020-08-18 10:08:21 -0400 | [diff] [blame] | 510 | static bool try_replace_expression(BasicBlock* b, |
| 511 | std::vector<BasicBlock::Node>::iterator* iter, |
| 512 | std::unique_ptr<Expression>* newExpression) { |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 513 | std::unique_ptr<Expression>* target = (*iter)->expression(); |
| 514 | if (!b->tryRemoveExpression(iter)) { |
| 515 | *target = std::move(*newExpression); |
| 516 | return false; |
| 517 | } |
| 518 | *target = std::move(*newExpression); |
| 519 | return b->tryInsertExpression(iter, target); |
| 520 | } |
| 521 | |
| 522 | /** |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 523 | * Returns true if the expression is a constant numeric literal with the specified value, or a |
| 524 | * constant vector with all elements equal to the specified value. |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 525 | */ |
Ethan Nicholas | a3f22f1 | 2020-10-01 12:13:17 -0400 | [diff] [blame] | 526 | template <typename T = SKSL_FLOAT> |
John Stiles | 9d94423 | 2020-08-19 09:56:49 -0400 | [diff] [blame] | 527 | static bool is_constant(const Expression& expr, T value) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 528 | switch (expr.kind()) { |
| 529 | case Expression::Kind::kIntLiteral: |
Ethan Nicholas | e96cdd1 | 2020-09-28 16:27:18 -0400 | [diff] [blame] | 530 | return expr.as<IntLiteral>().value() == value; |
John Stiles | 9d94423 | 2020-08-19 09:56:49 -0400 | [diff] [blame] | 531 | |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 532 | case Expression::Kind::kFloatLiteral: |
Ethan Nicholas | a3f22f1 | 2020-10-01 12:13:17 -0400 | [diff] [blame] | 533 | return expr.as<FloatLiteral>().value() == value; |
John Stiles | 9d94423 | 2020-08-19 09:56:49 -0400 | [diff] [blame] | 534 | |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 535 | case Expression::Kind::kConstructor: { |
John Stiles | 9d94423 | 2020-08-19 09:56:49 -0400 | [diff] [blame] | 536 | const Constructor& constructor = expr.as<Constructor>(); |
| 537 | if (constructor.isCompileTimeConstant()) { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 538 | const Type& constructorType = constructor.type(); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 539 | switch (constructorType.typeKind()) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 540 | case Type::TypeKind::kVector: |
John Stiles | bc75ebb | 2020-11-24 12:04:47 -0500 | [diff] [blame] | 541 | if (constructor.componentType().isFloat()) { |
| 542 | for (int i = 0; i < constructorType.columns(); ++i) { |
John Stiles | 9d94423 | 2020-08-19 09:56:49 -0400 | [diff] [blame] | 543 | if (constructor.getFVecComponent(i) != value) { |
| 544 | return false; |
| 545 | } |
John Stiles | bc75ebb | 2020-11-24 12:04:47 -0500 | [diff] [blame] | 546 | } |
| 547 | return true; |
| 548 | } else if (constructor.componentType().isInteger()) { |
| 549 | for (int i = 0; i < constructorType.columns(); ++i) { |
John Stiles | 9d94423 | 2020-08-19 09:56:49 -0400 | [diff] [blame] | 550 | if (constructor.getIVecComponent(i) != value) { |
| 551 | return false; |
| 552 | } |
| 553 | } |
John Stiles | bc75ebb | 2020-11-24 12:04:47 -0500 | [diff] [blame] | 554 | return true; |
Ethan Nicholas | d188c18 | 2019-06-10 15:55:38 -0400 | [diff] [blame] | 555 | } |
John Stiles | bc75ebb | 2020-11-24 12:04:47 -0500 | [diff] [blame] | 556 | // Other types (e.g. boolean) might occur, but aren't supported here. |
| 557 | return false; |
John Stiles | 9d94423 | 2020-08-19 09:56:49 -0400 | [diff] [blame] | 558 | |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 559 | case Type::TypeKind::kScalar: |
Ethan Nicholas | f70f044 | 2020-09-29 12:41:35 -0400 | [diff] [blame] | 560 | SkASSERT(constructor.arguments().size() == 1); |
| 561 | return is_constant<T>(*constructor.arguments()[0], value); |
John Stiles | 9d94423 | 2020-08-19 09:56:49 -0400 | [diff] [blame] | 562 | |
| 563 | default: |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 564 | return false; |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 565 | } |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 566 | } |
| 567 | return false; |
| 568 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 569 | default: |
| 570 | return false; |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | /** |
| 575 | * Collapses the binary expression pointed to by iter down to just the right side (in both the IR |
| 576 | * and CFG structures). |
| 577 | */ |
John Stiles | afbf899 | 2020-08-18 10:08:21 -0400 | [diff] [blame] | 578 | static void delete_left(BasicBlock* b, |
| 579 | std::vector<BasicBlock::Node>::iterator* iter, |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 580 | Compiler::OptimizationContext* optimizationContext) { |
| 581 | optimizationContext->fUpdated = true; |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 582 | std::unique_ptr<Expression>* target = (*iter)->expression(); |
John Stiles | a5a97b4 | 2020-08-18 11:19:07 -0400 | [diff] [blame] | 583 | BinaryExpression& bin = (*target)->as<BinaryExpression>(); |
John Stiles | 2d4f959 | 2020-10-30 10:29:12 -0400 | [diff] [blame] | 584 | Expression& left = *bin.left(); |
| 585 | std::unique_ptr<Expression>& rightPointer = bin.right(); |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 586 | SkASSERT(!left.hasSideEffects()); |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 587 | bool result; |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 588 | if (bin.getOperator().kind() == Token::Kind::TK_EQ) { |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 589 | result = b->tryRemoveLValueBefore(iter, &left); |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 590 | } else { |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 591 | result = b->tryRemoveExpressionBefore(iter, &left); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 592 | } |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 593 | // Remove references within LHS. |
| 594 | optimizationContext->fUsage->remove(&left); |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 595 | *target = std::move(rightPointer); |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 596 | if (!result) { |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 597 | optimizationContext->fNeedsRescan = true; |
Ethan Nicholas | 4b330df | 2017-05-17 10:52:55 -0400 | [diff] [blame] | 598 | return; |
| 599 | } |
| 600 | if (*iter == b->fNodes.begin()) { |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 601 | optimizationContext->fNeedsRescan = true; |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 602 | return; |
| 603 | } |
| 604 | --(*iter); |
John Stiles | 70025e5 | 2020-09-28 16:08:58 -0400 | [diff] [blame] | 605 | if (!(*iter)->isExpression() || (*iter)->expression() != &rightPointer) { |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 606 | optimizationContext->fNeedsRescan = true; |
Ethan Nicholas | 4b330df | 2017-05-17 10:52:55 -0400 | [diff] [blame] | 607 | return; |
| 608 | } |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 609 | *iter = b->fNodes.erase(*iter); |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 610 | SkASSERT((*iter)->expression() == target); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | /** |
| 614 | * Collapses the binary expression pointed to by iter down to just the left side (in both the IR and |
| 615 | * CFG structures). |
| 616 | */ |
John Stiles | afbf899 | 2020-08-18 10:08:21 -0400 | [diff] [blame] | 617 | static void delete_right(BasicBlock* b, |
| 618 | std::vector<BasicBlock::Node>::iterator* iter, |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 619 | Compiler::OptimizationContext* optimizationContext) { |
| 620 | optimizationContext->fUpdated = true; |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 621 | std::unique_ptr<Expression>* target = (*iter)->expression(); |
John Stiles | a5a97b4 | 2020-08-18 11:19:07 -0400 | [diff] [blame] | 622 | BinaryExpression& bin = (*target)->as<BinaryExpression>(); |
John Stiles | 2d4f959 | 2020-10-30 10:29:12 -0400 | [diff] [blame] | 623 | std::unique_ptr<Expression>& leftPointer = bin.left(); |
| 624 | Expression& right = *bin.right(); |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 625 | SkASSERT(!right.hasSideEffects()); |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 626 | // Remove references within RHS. |
| 627 | optimizationContext->fUsage->remove(&right); |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 628 | if (!b->tryRemoveExpressionBefore(iter, &right)) { |
| 629 | *target = std::move(leftPointer); |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 630 | optimizationContext->fNeedsRescan = true; |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 631 | return; |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 632 | } |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 633 | *target = std::move(leftPointer); |
Ethan Nicholas | 4b330df | 2017-05-17 10:52:55 -0400 | [diff] [blame] | 634 | if (*iter == b->fNodes.begin()) { |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 635 | optimizationContext->fNeedsRescan = true; |
Ethan Nicholas | 4b330df | 2017-05-17 10:52:55 -0400 | [diff] [blame] | 636 | return; |
| 637 | } |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 638 | --(*iter); |
John Stiles | 70025e5 | 2020-09-28 16:08:58 -0400 | [diff] [blame] | 639 | if ((!(*iter)->isExpression() || (*iter)->expression() != &leftPointer)) { |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 640 | optimizationContext->fNeedsRescan = true; |
Ethan Nicholas | 4b330df | 2017-05-17 10:52:55 -0400 | [diff] [blame] | 641 | return; |
| 642 | } |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 643 | *iter = b->fNodes.erase(*iter); |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 644 | SkASSERT((*iter)->expression() == target); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 645 | } |
| 646 | |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 647 | /** |
| 648 | * Constructs the specified type using a single argument. |
| 649 | */ |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 650 | 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] | 651 | ExpressionArray args; |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 652 | args.push_back(std::move(v)); |
John Stiles | 54f0049 | 2021-02-19 11:46:10 -0500 | [diff] [blame] | 653 | return std::make_unique<Constructor>(/*offset=*/-1, *type, std::move(args)); |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 654 | } |
| 655 | |
| 656 | /** |
| 657 | * Used in the implementations of vectorize_left and vectorize_right. Given a vector type and an |
| 658 | * expression x, deletes the expression pointed to by iter and replaces it with <type>(x). |
| 659 | */ |
| 660 | static void vectorize(BasicBlock* b, |
| 661 | std::vector<BasicBlock::Node>::iterator* iter, |
| 662 | const Type& type, |
| 663 | std::unique_ptr<Expression>* otherExpression, |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 664 | Compiler::OptimizationContext* optimizationContext) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 665 | SkASSERT((*(*iter)->expression())->kind() == Expression::Kind::kBinary); |
John Stiles | 9aeed13 | 2020-11-24 17:36:06 -0500 | [diff] [blame] | 666 | SkASSERT(type.isVector()); |
| 667 | SkASSERT((*otherExpression)->type().isScalar()); |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 668 | optimizationContext->fUpdated = true; |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 669 | std::unique_ptr<Expression>* target = (*iter)->expression(); |
| 670 | if (!b->tryRemoveExpression(iter)) { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 671 | *target = construct(&type, std::move(*otherExpression)); |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 672 | optimizationContext->fNeedsRescan = true; |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 673 | } else { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 674 | *target = construct(&type, std::move(*otherExpression)); |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 675 | if (!b->tryInsertExpression(iter, target)) { |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 676 | optimizationContext->fNeedsRescan = true; |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 677 | } |
| 678 | } |
| 679 | } |
| 680 | |
| 681 | /** |
| 682 | * Given a binary expression of the form x <op> vec<n>(y), deletes the right side and vectorizes the |
| 683 | * left to yield vec<n>(x). |
| 684 | */ |
| 685 | static void vectorize_left(BasicBlock* b, |
| 686 | std::vector<BasicBlock::Node>::iterator* iter, |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 687 | Compiler::OptimizationContext* optimizationContext) { |
John Stiles | a5a97b4 | 2020-08-18 11:19:07 -0400 | [diff] [blame] | 688 | BinaryExpression& bin = (*(*iter)->expression())->as<BinaryExpression>(); |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 689 | // Remove references within RHS. Vectorization of LHS doesn't change reference counts. |
John Stiles | 2d4f959 | 2020-10-30 10:29:12 -0400 | [diff] [blame] | 690 | optimizationContext->fUsage->remove(bin.right().get()); |
| 691 | vectorize(b, iter, bin.right()->type(), &bin.left(), optimizationContext); |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 692 | } |
| 693 | |
| 694 | /** |
| 695 | * Given a binary expression of the form vec<n>(x) <op> y, deletes the left side and vectorizes the |
| 696 | * right to yield vec<n>(y). |
| 697 | */ |
| 698 | static void vectorize_right(BasicBlock* b, |
| 699 | std::vector<BasicBlock::Node>::iterator* iter, |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 700 | Compiler::OptimizationContext* optimizationContext) { |
John Stiles | a5a97b4 | 2020-08-18 11:19:07 -0400 | [diff] [blame] | 701 | BinaryExpression& bin = (*(*iter)->expression())->as<BinaryExpression>(); |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 702 | // Remove references within LHS. Vectorization of RHS doesn't change reference counts. |
John Stiles | 2d4f959 | 2020-10-30 10:29:12 -0400 | [diff] [blame] | 703 | optimizationContext->fUsage->remove(bin.left().get()); |
| 704 | vectorize(b, iter, bin.left()->type(), &bin.right(), optimizationContext); |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 705 | } |
| 706 | |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 707 | void Compiler::simplifyExpression(DefinitionMap& definitions, |
| 708 | BasicBlock& b, |
| 709 | std::vector<BasicBlock::Node>::iterator* iter, |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 710 | OptimizationContext* optimizationContext) { |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 711 | Expression* expr = (*iter)->expression()->get(); |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 712 | SkASSERT(expr); |
John Stiles | 108bbe2 | 2020-11-18 11:10:38 -0500 | [diff] [blame] | 713 | |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 714 | if ((*iter)->fConstantPropagation) { |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 715 | std::unique_ptr<Expression> optimized = expr->constantPropagate(*fIRGenerator, |
| 716 | definitions); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 717 | if (optimized) { |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 718 | optimizationContext->fUpdated = true; |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 719 | optimized = fIRGenerator->coerce(std::move(optimized), expr->type()); |
Ethan Nicholas | 1d88152 | 2020-09-11 09:32:54 -0400 | [diff] [blame] | 720 | SkASSERT(optimized); |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 721 | // Remove references within 'expr', add references within 'optimized' |
| 722 | optimizationContext->fUsage->replace(expr, optimized.get()); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 723 | if (!try_replace_expression(&b, iter, &optimized)) { |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 724 | optimizationContext->fNeedsRescan = true; |
Ethan Nicholas | 4b330df | 2017-05-17 10:52:55 -0400 | [diff] [blame] | 725 | return; |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 726 | } |
John Stiles | 70025e5 | 2020-09-28 16:08:58 -0400 | [diff] [blame] | 727 | SkASSERT((*iter)->isExpression()); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 728 | expr = (*iter)->expression()->get(); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 729 | } |
| 730 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 731 | switch (expr->kind()) { |
| 732 | case Expression::Kind::kVariableReference: { |
John Stiles | a5a97b4 | 2020-08-18 11:19:07 -0400 | [diff] [blame] | 733 | const VariableReference& ref = expr->as<VariableReference>(); |
Ethan Nicholas | 7868692 | 2020-10-08 06:46:27 -0400 | [diff] [blame] | 734 | const Variable* var = ref.variable(); |
John Stiles | 66c53b9 | 2021-02-20 08:00:43 -0500 | [diff] [blame] | 735 | if (fContext->fConfig->fSettings.fDeadCodeElimination && |
| 736 | ref.refKind() != VariableReference::RefKind::kWrite && |
Ethan Nicholas | 453f67f | 2020-10-09 10:43:45 -0400 | [diff] [blame] | 737 | ref.refKind() != VariableReference::RefKind::kPointer && |
John Stiles | e8a2492 | 2021-02-08 17:54:08 -0500 | [diff] [blame] | 738 | var->storage() == Variable::Storage::kLocal && !definitions.get(var) && |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 739 | optimizationContext->fSilences.find(var) == optimizationContext->fSilences.end()) { |
| 740 | optimizationContext->fSilences.insert(var); |
Ethan Nicholas | 82a62d2 | 2017-11-07 14:42:10 +0000 | [diff] [blame] | 741 | this->error(expr->fOffset, |
Ethan Nicholas | e2c4999 | 2020-10-05 11:49:11 -0400 | [diff] [blame] | 742 | "'" + var->name() + "' has not been assigned"); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 743 | } |
| 744 | break; |
| 745 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 746 | case Expression::Kind::kTernary: { |
John Stiles | 403a363 | 2020-08-20 12:11:48 -0400 | [diff] [blame] | 747 | TernaryExpression* t = &expr->as<TernaryExpression>(); |
Ethan Nicholas | dd21816 | 2020-10-08 05:48:01 -0400 | [diff] [blame] | 748 | if (t->test()->is<BoolLiteral>()) { |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 749 | // ternary has a constant test, replace it with either the true or |
| 750 | // false branch |
Ethan Nicholas | dd21816 | 2020-10-08 05:48:01 -0400 | [diff] [blame] | 751 | if (t->test()->as<BoolLiteral>().value()) { |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 752 | (*iter)->setExpression(std::move(t->ifTrue()), optimizationContext->fUsage); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 753 | } else { |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 754 | (*iter)->setExpression(std::move(t->ifFalse()), optimizationContext->fUsage); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 755 | } |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 756 | optimizationContext->fUpdated = true; |
| 757 | optimizationContext->fNeedsRescan = true; |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 758 | } |
| 759 | break; |
| 760 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 761 | case Expression::Kind::kBinary: { |
John Stiles | 403a363 | 2020-08-20 12:11:48 -0400 | [diff] [blame] | 762 | BinaryExpression* bin = &expr->as<BinaryExpression>(); |
John Stiles | 0ac6c15 | 2021-02-10 14:04:24 -0500 | [diff] [blame] | 763 | if (dead_assignment(*bin, optimizationContext->fUsage) || self_assignment(*bin)) { |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 764 | delete_left(&b, iter, optimizationContext); |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 765 | break; |
| 766 | } |
John Stiles | 2d4f959 | 2020-10-30 10:29:12 -0400 | [diff] [blame] | 767 | Expression& left = *bin->left(); |
| 768 | Expression& right = *bin->right(); |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 769 | const Type& leftType = left.type(); |
| 770 | const Type& rightType = right.type(); |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 771 | // collapse useless expressions like x * 1 or x + 0 |
John Stiles | 9aeed13 | 2020-11-24 17:36:06 -0500 | [diff] [blame] | 772 | if ((!leftType.isScalar() && !leftType.isVector()) || |
| 773 | (!rightType.isScalar() && !rightType.isVector())) { |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 774 | break; |
| 775 | } |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 776 | switch (bin->getOperator().kind()) { |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 777 | case Token::Kind::TK_STAR: |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 778 | if (is_constant(left, 1)) { |
John Stiles | 9aeed13 | 2020-11-24 17:36:06 -0500 | [diff] [blame] | 779 | if (leftType.isVector() && rightType.isScalar()) { |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 780 | // float4(1) * x -> float4(x) |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 781 | vectorize_right(&b, iter, optimizationContext); |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 782 | } else { |
| 783 | // 1 * x -> x |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 784 | // 1 * float4(x) -> float4(x) |
| 785 | // float4(1) * float4(x) -> float4(x) |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 786 | delete_left(&b, iter, optimizationContext); |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 787 | } |
| 788 | } |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 789 | else if (is_constant(left, 0)) { |
John Stiles | 9aeed13 | 2020-11-24 17:36:06 -0500 | [diff] [blame] | 790 | if (leftType.isScalar() && rightType.isVector() && |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 791 | !right.hasSideEffects()) { |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 792 | // 0 * float4(x) -> float4(0) |
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 | // 0 * x -> 0 |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 796 | // float4(0) * x -> float4(0) |
| 797 | // float4(0) * float4(x) -> float4(0) |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 798 | if (!right.hasSideEffects()) { |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 799 | delete_right(&b, iter, optimizationContext); |
Ethan Nicholas | 51493ee | 2017-12-11 12:34:33 -0500 | [diff] [blame] | 800 | } |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 801 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 802 | } |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 803 | else if (is_constant(right, 1)) { |
John Stiles | 9aeed13 | 2020-11-24 17:36:06 -0500 | [diff] [blame] | 804 | if (leftType.isScalar() && rightType.isVector()) { |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 805 | // x * float4(1) -> float4(x) |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 806 | vectorize_left(&b, iter, optimizationContext); |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 807 | } else { |
| 808 | // x * 1 -> x |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 809 | // float4(x) * 1 -> float4(x) |
| 810 | // float4(x) * float4(1) -> float4(x) |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 811 | delete_right(&b, iter, optimizationContext); |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 812 | } |
| 813 | } |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 814 | else if (is_constant(right, 0)) { |
John Stiles | 9aeed13 | 2020-11-24 17:36:06 -0500 | [diff] [blame] | 815 | if (leftType.isVector() && rightType.isScalar() && !left.hasSideEffects()) { |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 816 | // float4(x) * 0 -> float4(0) |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 817 | vectorize_right(&b, iter, optimizationContext); |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 818 | } else { |
| 819 | // x * 0 -> 0 |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 820 | // x * float4(0) -> float4(0) |
| 821 | // float4(x) * float4(0) -> float4(0) |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 822 | if (!left.hasSideEffects()) { |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 823 | delete_left(&b, iter, optimizationContext); |
Ethan Nicholas | 51493ee | 2017-12-11 12:34:33 -0500 | [diff] [blame] | 824 | } |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 825 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 826 | } |
| 827 | break; |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 828 | case Token::Kind::TK_PLUS: |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 829 | if (is_constant(left, 0)) { |
John Stiles | 9aeed13 | 2020-11-24 17:36:06 -0500 | [diff] [blame] | 830 | if (leftType.isVector() && rightType.isScalar()) { |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 831 | // float4(0) + x -> float4(x) |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 832 | vectorize_right(&b, iter, optimizationContext); |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 833 | } else { |
| 834 | // 0 + x -> x |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 835 | // 0 + float4(x) -> float4(x) |
| 836 | // float4(0) + float4(x) -> float4(x) |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 837 | delete_left(&b, iter, optimizationContext); |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 838 | } |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 839 | } else 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 | 56e4271 | 2017-04-21 10:23:37 -0400 | [diff] [blame] | 849 | } |
| 850 | break; |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 851 | case Token::Kind::TK_MINUS: |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 852 | if (is_constant(right, 0)) { |
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(0) -> 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 - 0 -> x |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 858 | // float4(x) - 0 -> float4(x) |
| 859 | // float4(x) - float4(0) -> 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 | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 862 | } |
| 863 | break; |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 864 | case Token::Kind::TK_SLASH: |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 865 | if (is_constant(right, 1)) { |
John Stiles | 9aeed13 | 2020-11-24 17:36:06 -0500 | [diff] [blame] | 866 | if (leftType.isScalar() && rightType.isVector()) { |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 867 | // x / float4(1) -> float4(x) |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 868 | vectorize_left(&b, iter, optimizationContext); |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 869 | } else { |
| 870 | // x / 1 -> x |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 871 | // float4(x) / 1 -> float4(x) |
| 872 | // float4(x) / float4(1) -> float4(x) |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 873 | delete_right(&b, iter, optimizationContext); |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 874 | } |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 875 | } else if (is_constant(left, 0)) { |
John Stiles | 9aeed13 | 2020-11-24 17:36:06 -0500 | [diff] [blame] | 876 | if (leftType.isScalar() && rightType.isVector() && |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 877 | !right.hasSideEffects()) { |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 878 | // 0 / float4(x) -> float4(0) |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 879 | vectorize_left(&b, iter, optimizationContext); |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 880 | } else { |
| 881 | // 0 / x -> 0 |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 882 | // float4(0) / x -> float4(0) |
| 883 | // float4(0) / float4(x) -> float4(0) |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 884 | if (!right.hasSideEffects()) { |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 885 | delete_right(&b, iter, optimizationContext); |
Ethan Nicholas | 51493ee | 2017-12-11 12:34:33 -0500 | [diff] [blame] | 886 | } |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 887 | } |
| 888 | } |
| 889 | break; |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 890 | case Token::Kind::TK_PLUSEQ: |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 891 | if (is_constant(right, 0)) { |
John Stiles | 47c0a74 | 2021-02-09 09:30:35 -0500 | [diff] [blame] | 892 | Analysis::UpdateRefKind(&left, RefKind::kRead); |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 893 | delete_right(&b, iter, optimizationContext); |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 894 | } |
| 895 | break; |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 896 | case Token::Kind::TK_MINUSEQ: |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 897 | if (is_constant(right, 0)) { |
John Stiles | 47c0a74 | 2021-02-09 09:30:35 -0500 | [diff] [blame] | 898 | Analysis::UpdateRefKind(&left, RefKind::kRead); |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 899 | delete_right(&b, iter, optimizationContext); |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 900 | } |
| 901 | break; |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 902 | case Token::Kind::TK_STAREQ: |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 903 | if (is_constant(right, 1)) { |
John Stiles | 47c0a74 | 2021-02-09 09:30:35 -0500 | [diff] [blame] | 904 | Analysis::UpdateRefKind(&left, RefKind::kRead); |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 905 | delete_right(&b, iter, optimizationContext); |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 906 | } |
| 907 | break; |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 908 | case Token::Kind::TK_SLASHEQ: |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 909 | if (is_constant(right, 1)) { |
John Stiles | 47c0a74 | 2021-02-09 09:30:35 -0500 | [diff] [blame] | 910 | Analysis::UpdateRefKind(&left, RefKind::kRead); |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 911 | delete_right(&b, iter, optimizationContext); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 912 | } |
| 913 | break; |
| 914 | default: |
| 915 | break; |
| 916 | } |
Ethan Nicholas | 409f6f0 | 2019-09-17 12:34:39 -0400 | [diff] [blame] | 917 | break; |
| 918 | } |
John Stiles | f5c1d04 | 2020-11-21 23:26:07 -0500 | [diff] [blame] | 919 | case Expression::Kind::kConstructor: { |
| 920 | // Find constructors embedded inside constructors and flatten them out where possible. |
| 921 | // - float4(float2(1, 2), 3, 4) --> float4(1, 2, 3, 4) |
| 922 | // - float4(w, float3(sin(x), cos(y), tan(z))) --> float4(w, sin(x), cos(y), tan(z)) |
| 923 | // Leave single-argument constructors alone, though. These might be casts or splats. |
| 924 | Constructor& c = expr->as<Constructor>(); |
| 925 | if (c.type().columns() > 1) { |
| 926 | // Inspect each constructor argument to see if it's a candidate for flattening. |
| 927 | // Remember matched arguments in a bitfield, "argsToOptimize". |
| 928 | int argsToOptimize = 0; |
| 929 | int currBit = 1; |
| 930 | for (const std::unique_ptr<Expression>& arg : c.arguments()) { |
| 931 | if (arg->is<Constructor>()) { |
| 932 | Constructor& inner = arg->as<Constructor>(); |
| 933 | if (inner.arguments().size() > 1 && |
| 934 | inner.type().componentType() == c.type().componentType()) { |
| 935 | argsToOptimize |= currBit; |
| 936 | } |
| 937 | } |
| 938 | currBit <<= 1; |
| 939 | } |
| 940 | if (argsToOptimize) { |
| 941 | // We found at least one argument that could be flattened out. Re-walk the |
| 942 | // constructor args and flatten the candidates we found during our initial pass. |
| 943 | ExpressionArray flattened; |
| 944 | flattened.reserve_back(c.type().columns()); |
| 945 | currBit = 1; |
| 946 | for (const std::unique_ptr<Expression>& arg : c.arguments()) { |
| 947 | if (argsToOptimize & currBit) { |
| 948 | Constructor& inner = arg->as<Constructor>(); |
| 949 | for (const std::unique_ptr<Expression>& innerArg : inner.arguments()) { |
| 950 | flattened.push_back(innerArg->clone()); |
| 951 | } |
| 952 | } else { |
| 953 | flattened.push_back(arg->clone()); |
| 954 | } |
| 955 | currBit <<= 1; |
| 956 | } |
John Stiles | 54f0049 | 2021-02-19 11:46:10 -0500 | [diff] [blame] | 957 | std::unique_ptr<Expression> replacement = std::make_unique<Constructor>( |
| 958 | c.fOffset, c.type(), std::move(flattened)); |
John Stiles | 1b91c0e | 2021-02-11 11:43:09 -0500 | [diff] [blame] | 959 | // We're replacing an expression with a cloned version; we'll need a rescan. |
| 960 | // No fUsage change: `float2(float(x), y)` and `float2(x, y)` have equivalent |
| 961 | // reference counts. |
| 962 | try_replace_expression(&b, iter, &replacement); |
John Stiles | f5c1d04 | 2020-11-21 23:26:07 -0500 | [diff] [blame] | 963 | optimizationContext->fUpdated = true; |
John Stiles | 1b91c0e | 2021-02-11 11:43:09 -0500 | [diff] [blame] | 964 | optimizationContext->fNeedsRescan = true; |
John Stiles | f5c1d04 | 2020-11-21 23:26:07 -0500 | [diff] [blame] | 965 | break; |
| 966 | } |
| 967 | } |
| 968 | break; |
| 969 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 970 | case Expression::Kind::kSwizzle: { |
John Stiles | 403a363 | 2020-08-20 12:11:48 -0400 | [diff] [blame] | 971 | Swizzle& s = expr->as<Swizzle>(); |
John Stiles | 108bbe2 | 2020-11-18 11:10:38 -0500 | [diff] [blame] | 972 | // Detect identity swizzles like `foo.rgba`. |
Ethan Nicholas | 6b4d581 | 2020-10-12 16:11:51 -0400 | [diff] [blame] | 973 | if ((int) s.components().size() == s.base()->type().columns()) { |
Ethan Nicholas | 409f6f0 | 2019-09-17 12:34:39 -0400 | [diff] [blame] | 974 | bool identity = true; |
Ethan Nicholas | 6b4d581 | 2020-10-12 16:11:51 -0400 | [diff] [blame] | 975 | for (int i = 0; i < (int) s.components().size(); ++i) { |
| 976 | if (s.components()[i] != i) { |
Ethan Nicholas | 409f6f0 | 2019-09-17 12:34:39 -0400 | [diff] [blame] | 977 | identity = false; |
| 978 | break; |
| 979 | } |
| 980 | } |
| 981 | if (identity) { |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 982 | optimizationContext->fUpdated = true; |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 983 | // No fUsage change: foo.rgba and foo have equivalent reference counts |
Ethan Nicholas | 6b4d581 | 2020-10-12 16:11:51 -0400 | [diff] [blame] | 984 | if (!try_replace_expression(&b, iter, &s.base())) { |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 985 | optimizationContext->fNeedsRescan = true; |
Ethan Nicholas | 409f6f0 | 2019-09-17 12:34:39 -0400 | [diff] [blame] | 986 | return; |
| 987 | } |
John Stiles | 70025e5 | 2020-09-28 16:08:58 -0400 | [diff] [blame] | 988 | SkASSERT((*iter)->isExpression()); |
Ethan Nicholas | 409f6f0 | 2019-09-17 12:34:39 -0400 | [diff] [blame] | 989 | break; |
| 990 | } |
| 991 | } |
John Stiles | 108bbe2 | 2020-11-18 11:10:38 -0500 | [diff] [blame] | 992 | // Detect swizzles of swizzles, e.g. replace `foo.argb.r000` with `foo.a000`. |
| 993 | if (s.base()->is<Swizzle>()) { |
Ethan Nicholas | 6b4d581 | 2020-10-12 16:11:51 -0400 | [diff] [blame] | 994 | Swizzle& base = s.base()->as<Swizzle>(); |
John Stiles | 750109b | 2020-10-30 13:45:46 -0400 | [diff] [blame] | 995 | ComponentArray final; |
Ethan Nicholas | 6b4d581 | 2020-10-12 16:11:51 -0400 | [diff] [blame] | 996 | for (int c : s.components()) { |
| 997 | final.push_back(base.components()[c]); |
Ethan Nicholas | 409f6f0 | 2019-09-17 12:34:39 -0400 | [diff] [blame] | 998 | } |
Ethan Nicholas | 6b4d581 | 2020-10-12 16:11:51 -0400 | [diff] [blame] | 999 | std::unique_ptr<Expression> replacement(new Swizzle(*fContext, base.base()->clone(), |
John Stiles | 750109b | 2020-10-30 13:45:46 -0400 | [diff] [blame] | 1000 | final)); |
John Stiles | d2f51b1 | 2021-01-07 18:12:31 -0500 | [diff] [blame] | 1001 | // 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] | 1002 | // 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] | 1003 | try_replace_expression(&b, iter, &replacement); |
| 1004 | optimizationContext->fUpdated = true; |
| 1005 | optimizationContext->fNeedsRescan = true; |
John Stiles | 108bbe2 | 2020-11-18 11:10:38 -0500 | [diff] [blame] | 1006 | break; |
| 1007 | } |
| 1008 | // Optimize swizzles of constructors. |
| 1009 | if (s.base()->is<Constructor>()) { |
| 1010 | Constructor& base = s.base()->as<Constructor>(); |
| 1011 | std::unique_ptr<Expression> replacement; |
| 1012 | const Type& componentType = base.type().componentType(); |
| 1013 | int swizzleSize = s.components().size(); |
| 1014 | |
| 1015 | // The IR generator has already converted any zero/one swizzle components into |
| 1016 | // constructors containing zero/one args. Confirm that this is true by checking that |
| 1017 | // our swizzle components are all `xyzw` (values 0 through 3). |
| 1018 | SkASSERT(std::all_of(s.components().begin(), s.components().end(), |
| 1019 | [](int8_t c) { return c >= 0 && c <= 3; })); |
| 1020 | |
John Stiles | 9aeed13 | 2020-11-24 17:36:06 -0500 | [diff] [blame] | 1021 | if (base.arguments().size() == 1 && base.arguments().front()->type().isScalar()) { |
John Stiles | 108bbe2 | 2020-11-18 11:10:38 -0500 | [diff] [blame] | 1022 | // `half4(scalar).zyy` can be optimized to `half3(scalar)`. The swizzle |
| 1023 | // components don't actually matter since all fields are the same. |
John Stiles | f0cb733 | 2021-01-08 18:39:00 -0500 | [diff] [blame] | 1024 | const Expression& argument = *base.arguments().front(); |
| 1025 | const Type& constructorType = componentType.toCompound(*fContext, swizzleSize, |
| 1026 | /*rows=*/1); |
| 1027 | replacement = Constructor::SimplifyConversion(constructorType, argument); |
| 1028 | if (!replacement) { |
| 1029 | ExpressionArray newArgs; |
| 1030 | newArgs.push_back(argument.clone()); |
John Stiles | 54f0049 | 2021-02-19 11:46:10 -0500 | [diff] [blame] | 1031 | replacement = std::make_unique<Constructor>(base.fOffset, constructorType, |
John Stiles | f0cb733 | 2021-01-08 18:39:00 -0500 | [diff] [blame] | 1032 | std::move(newArgs)); |
| 1033 | } |
John Stiles | 108bbe2 | 2020-11-18 11:10:38 -0500 | [diff] [blame] | 1034 | |
John Stiles | a60ac0c | 2020-12-22 08:59:51 -0500 | [diff] [blame] | 1035 | // We're replacing an expression with a cloned version; we'll need a rescan. |
| 1036 | // There's no fUsage change: `half4(foo).xy` and `half2(foo)` have equivalent |
| 1037 | // reference counts. |
| 1038 | try_replace_expression(&b, iter, &replacement); |
John Stiles | 0777ac4 | 2020-11-19 11:06:47 -0500 | [diff] [blame] | 1039 | optimizationContext->fUpdated = true; |
John Stiles | a60ac0c | 2020-12-22 08:59:51 -0500 | [diff] [blame] | 1040 | optimizationContext->fNeedsRescan = true; |
John Stiles | 108bbe2 | 2020-11-18 11:10:38 -0500 | [diff] [blame] | 1041 | break; |
| 1042 | } |
| 1043 | |
John Stiles | 0777ac4 | 2020-11-19 11:06:47 -0500 | [diff] [blame] | 1044 | // Swizzles can duplicate some elements and discard others, e.g. |
| 1045 | // `half4(1, 2, 3, 4).xxz` --> `half3(1, 1, 3)`. However, there are constraints: |
| 1046 | // - Expressions with side effects need to occur exactly once, even if they |
| 1047 | // would otherwise be swizzle-eliminated |
| 1048 | // - Non-trivial expressions should not be repeated, but elimination is OK. |
| 1049 | // |
| 1050 | // Look up the argument for the constructor at each index. This is typically simple |
| 1051 | // but for weird cases like `half4(bar.yz, half2(foo))`, it can be harder than it |
| 1052 | // seems. This example would result in: |
| 1053 | // argMap[0] = {.fArgIndex = 0, .fComponent = 0} (bar.yz .x) |
| 1054 | // argMap[1] = {.fArgIndex = 0, .fComponent = 1} (bar.yz .y) |
| 1055 | // argMap[2] = {.fArgIndex = 1, .fComponent = 0} (half2(foo) .x) |
| 1056 | // argMap[3] = {.fArgIndex = 1, .fComponent = 1} (half2(foo) .y) |
| 1057 | struct ConstructorArgMap { |
| 1058 | int8_t fArgIndex; |
| 1059 | int8_t fComponent; |
| 1060 | }; |
| 1061 | |
| 1062 | int numConstructorArgs = base.type().columns(); |
| 1063 | ConstructorArgMap argMap[4] = {}; |
| 1064 | int writeIdx = 0; |
| 1065 | for (int argIdx = 0; argIdx < (int) base.arguments().size(); ++argIdx) { |
| 1066 | const Expression& expr = *base.arguments()[argIdx]; |
| 1067 | int argWidth = expr.type().columns(); |
| 1068 | for (int componentIdx = 0; componentIdx < argWidth; ++componentIdx) { |
| 1069 | argMap[writeIdx].fArgIndex = argIdx; |
| 1070 | argMap[writeIdx].fComponent = componentIdx; |
| 1071 | ++writeIdx; |
| 1072 | } |
| 1073 | } |
| 1074 | SkASSERT(writeIdx == numConstructorArgs); |
| 1075 | |
| 1076 | // Count up the number of times each constructor argument is used by the |
| 1077 | // swizzle. |
| 1078 | // `half4(bar.yz, half2(foo)).xwxy` -> { 3, 1 } |
| 1079 | // - bar.yz is referenced 3 times, by `.x_xy` |
| 1080 | // - half(foo) is referenced 1 time, by `._w__` |
| 1081 | int8_t exprUsed[4] = {}; |
| 1082 | for (int c : s.components()) { |
| 1083 | exprUsed[argMap[c].fArgIndex]++; |
| 1084 | } |
| 1085 | |
| 1086 | bool safeToOptimize = true; |
| 1087 | for (int index = 0; index < numConstructorArgs; ++index) { |
| 1088 | int8_t constructorArgIndex = argMap[index].fArgIndex; |
| 1089 | const Expression& baseArg = *base.arguments()[constructorArgIndex]; |
| 1090 | |
| 1091 | // Check that non-trivial expressions are not swizzled in more than once. |
John Stiles | c30fbca | 2020-11-19 16:25:49 -0500 | [diff] [blame] | 1092 | if (exprUsed[constructorArgIndex] > 1 && |
| 1093 | !Analysis::IsTrivialExpression(baseArg)) { |
John Stiles | 0777ac4 | 2020-11-19 11:06:47 -0500 | [diff] [blame] | 1094 | safeToOptimize = false; |
| 1095 | break; |
| 1096 | } |
| 1097 | // Check that side-effect-bearing expressions are swizzled in exactly once. |
| 1098 | if (exprUsed[constructorArgIndex] != 1 && baseArg.hasSideEffects()) { |
| 1099 | safeToOptimize = false; |
| 1100 | break; |
| 1101 | } |
| 1102 | } |
| 1103 | |
| 1104 | if (safeToOptimize) { |
John Stiles | d9076cb | 2020-11-19 12:18:36 -0500 | [diff] [blame] | 1105 | struct ReorderedArgument { |
| 1106 | int8_t fArgIndex; |
| 1107 | ComponentArray fComponents; |
| 1108 | }; |
| 1109 | SkSTArray<4, ReorderedArgument> reorderedArgs; |
John Stiles | 0777ac4 | 2020-11-19 11:06:47 -0500 | [diff] [blame] | 1110 | for (int c : s.components()) { |
| 1111 | const ConstructorArgMap& argument = argMap[c]; |
| 1112 | const Expression& baseArg = *base.arguments()[argument.fArgIndex]; |
| 1113 | |
John Stiles | 9aeed13 | 2020-11-24 17:36:06 -0500 | [diff] [blame] | 1114 | if (baseArg.type().isScalar()) { |
John Stiles | d9076cb | 2020-11-19 12:18:36 -0500 | [diff] [blame] | 1115 | // This argument is a scalar; add it to the list as-is. |
John Stiles | 0777ac4 | 2020-11-19 11:06:47 -0500 | [diff] [blame] | 1116 | SkASSERT(argument.fComponent == 0); |
John Stiles | d9076cb | 2020-11-19 12:18:36 -0500 | [diff] [blame] | 1117 | reorderedArgs.push_back({argument.fArgIndex, |
| 1118 | ComponentArray{}}); |
John Stiles | 0777ac4 | 2020-11-19 11:06:47 -0500 | [diff] [blame] | 1119 | } else { |
John Stiles | d9076cb | 2020-11-19 12:18:36 -0500 | [diff] [blame] | 1120 | // This argument is a component from a vector. |
John Stiles | 0777ac4 | 2020-11-19 11:06:47 -0500 | [diff] [blame] | 1121 | SkASSERT(argument.fComponent < baseArg.type().columns()); |
John Stiles | d9076cb | 2020-11-19 12:18:36 -0500 | [diff] [blame] | 1122 | if (reorderedArgs.empty() || |
| 1123 | reorderedArgs.back().fArgIndex != argument.fArgIndex) { |
| 1124 | // This can't be combined with the previous argument. Add a new one. |
| 1125 | reorderedArgs.push_back({argument.fArgIndex, |
| 1126 | ComponentArray{argument.fComponent}}); |
| 1127 | } else { |
| 1128 | // Since we know this argument uses components, it should already |
| 1129 | // have at least one component set. |
| 1130 | SkASSERT(!reorderedArgs.back().fComponents.empty()); |
| 1131 | // Build up the current argument with one more component. |
| 1132 | reorderedArgs.back().fComponents.push_back(argument.fComponent); |
| 1133 | } |
John Stiles | 0777ac4 | 2020-11-19 11:06:47 -0500 | [diff] [blame] | 1134 | } |
| 1135 | } |
John Stiles | d9076cb | 2020-11-19 12:18:36 -0500 | [diff] [blame] | 1136 | |
| 1137 | // Convert our reordered argument list to an actual array of expressions, with |
| 1138 | // the new order and any new inner swizzles that need to be applied. Note that |
| 1139 | // we expect followup passes to clean up the inner swizzles. |
| 1140 | ExpressionArray newArgs; |
| 1141 | newArgs.reserve_back(swizzleSize); |
| 1142 | for (const ReorderedArgument& reorderedArg : reorderedArgs) { |
| 1143 | const Expression& baseArg = *base.arguments()[reorderedArg.fArgIndex]; |
| 1144 | if (reorderedArg.fComponents.empty()) { |
| 1145 | newArgs.push_back(baseArg.clone()); |
| 1146 | } else { |
| 1147 | newArgs.push_back(std::make_unique<Swizzle>(*fContext, baseArg.clone(), |
| 1148 | reorderedArg.fComponents)); |
| 1149 | } |
| 1150 | } |
| 1151 | |
| 1152 | // Create a new constructor. |
John Stiles | 0777ac4 | 2020-11-19 11:06:47 -0500 | [diff] [blame] | 1153 | replacement = std::make_unique<Constructor>( |
| 1154 | base.fOffset, |
John Stiles | 54f0049 | 2021-02-19 11:46:10 -0500 | [diff] [blame] | 1155 | componentType.toCompound(*fContext, swizzleSize, /*rows=*/1), |
John Stiles | 0777ac4 | 2020-11-19 11:06:47 -0500 | [diff] [blame] | 1156 | std::move(newArgs)); |
| 1157 | |
John Stiles | a60ac0c | 2020-12-22 08:59:51 -0500 | [diff] [blame] | 1158 | // Remove references within 'expr', add references within 'replacement.' |
John Stiles | 0777ac4 | 2020-11-19 11:06:47 -0500 | [diff] [blame] | 1159 | optimizationContext->fUsage->replace(expr, replacement.get()); |
John Stiles | a60ac0c | 2020-12-22 08:59:51 -0500 | [diff] [blame] | 1160 | |
| 1161 | // We're replacing an expression with a cloned version; we'll need a rescan. |
| 1162 | try_replace_expression(&b, iter, &replacement); |
| 1163 | optimizationContext->fUpdated = true; |
| 1164 | optimizationContext->fNeedsRescan = true; |
John Stiles | 0777ac4 | 2020-11-19 11:06:47 -0500 | [diff] [blame] | 1165 | } |
John Stiles | 108bbe2 | 2020-11-18 11:10:38 -0500 | [diff] [blame] | 1166 | break; |
Ethan Nicholas | 409f6f0 | 2019-09-17 12:34:39 -0400 | [diff] [blame] | 1167 | } |
John Stiles | 30212b7 | 2020-06-11 17:55:07 -0400 | [diff] [blame] | 1168 | break; |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1169 | } |
| 1170 | default: |
| 1171 | break; |
| 1172 | } |
| 1173 | } |
| 1174 | |
John Stiles | 04ca41a | 2021-02-23 09:58:04 -0500 | [diff] [blame] | 1175 | static bool contains_exit(Statement& stmt, bool conditionalExits) { |
| 1176 | class ContainsExit : public ProgramVisitor { |
John Stiles | b92641c | 2020-08-31 18:09:01 -0400 | [diff] [blame] | 1177 | public: |
John Stiles | 04ca41a | 2021-02-23 09:58:04 -0500 | [diff] [blame] | 1178 | ContainsExit(bool e) : fConditionalExits(e) {} |
| 1179 | |
John Stiles | b92641c | 2020-08-31 18:09:01 -0400 | [diff] [blame] | 1180 | bool visitStatement(const Statement& stmt) override { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1181 | switch (stmt.kind()) { |
| 1182 | case Statement::Kind::kBlock: |
John Stiles | bb1505f | 2021-02-12 09:17:53 -0500 | [diff] [blame] | 1183 | return INHERITED::visitStatement(stmt); |
John Stiles | b92641c | 2020-08-31 18:09:01 -0400 | [diff] [blame] | 1184 | |
John Stiles | 04ca41a | 2021-02-23 09:58:04 -0500 | [diff] [blame] | 1185 | case Statement::Kind::kReturn: |
| 1186 | // Returns are an early exit regardless of the surrounding control structures. |
| 1187 | return fConditionalExits ? fInConditional : !fInConditional; |
| 1188 | |
| 1189 | case Statement::Kind::kContinue: |
| 1190 | // Continues are an early exit from switches, but not loops. |
| 1191 | return !fInLoop && |
| 1192 | (fConditionalExits ? fInConditional : !fInConditional); |
| 1193 | |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1194 | case Statement::Kind::kBreak: |
John Stiles | 04ca41a | 2021-02-23 09:58:04 -0500 | [diff] [blame] | 1195 | // Breaks cannot escape from switches or loops. |
| 1196 | return !fInLoop && !fInSwitch && |
| 1197 | (fConditionalExits ? fInConditional : !fInConditional); |
John Stiles | b92641c | 2020-08-31 18:09:01 -0400 | [diff] [blame] | 1198 | |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1199 | case Statement::Kind::kIf: { |
John Stiles | b92641c | 2020-08-31 18:09:01 -0400 | [diff] [blame] | 1200 | ++fInConditional; |
John Stiles | bb1505f | 2021-02-12 09:17:53 -0500 | [diff] [blame] | 1201 | bool result = INHERITED::visitStatement(stmt); |
John Stiles | b92641c | 2020-08-31 18:09:01 -0400 | [diff] [blame] | 1202 | --fInConditional; |
| 1203 | return result; |
| 1204 | } |
| 1205 | |
John Stiles | 04ca41a | 2021-02-23 09:58:04 -0500 | [diff] [blame] | 1206 | case Statement::Kind::kFor: |
| 1207 | case Statement::Kind::kDo: { |
| 1208 | // Loops are treated as conditionals because a loop could potentially execute |
| 1209 | // zero times. We don't have a straightforward way to determine that a loop |
| 1210 | // definitely executes at least once. |
| 1211 | ++fInConditional; |
| 1212 | ++fInLoop; |
| 1213 | bool result = INHERITED::visitStatement(stmt); |
| 1214 | --fInLoop; |
| 1215 | --fInConditional; |
| 1216 | return result; |
| 1217 | } |
| 1218 | |
| 1219 | case Statement::Kind::kSwitch: { |
| 1220 | ++fInSwitch; |
| 1221 | bool result = INHERITED::visitStatement(stmt); |
| 1222 | --fInSwitch; |
| 1223 | return result; |
| 1224 | } |
| 1225 | |
John Stiles | b92641c | 2020-08-31 18:09:01 -0400 | [diff] [blame] | 1226 | default: |
| 1227 | return false; |
| 1228 | } |
| 1229 | } |
| 1230 | |
John Stiles | 04ca41a | 2021-02-23 09:58:04 -0500 | [diff] [blame] | 1231 | bool fConditionalExits = false; |
John Stiles | b92641c | 2020-08-31 18:09:01 -0400 | [diff] [blame] | 1232 | int fInConditional = 0; |
John Stiles | 04ca41a | 2021-02-23 09:58:04 -0500 | [diff] [blame] | 1233 | int fInLoop = 0; |
| 1234 | int fInSwitch = 0; |
John Stiles | b92641c | 2020-08-31 18:09:01 -0400 | [diff] [blame] | 1235 | using INHERITED = ProgramVisitor; |
| 1236 | }; |
| 1237 | |
John Stiles | 04ca41a | 2021-02-23 09:58:04 -0500 | [diff] [blame] | 1238 | return ContainsExit{conditionalExits}.visitStatement(stmt); |
John Stiles | 92219b4 | 2020-06-15 12:32:24 -0400 | [diff] [blame] | 1239 | } |
| 1240 | |
John Stiles | 04ca41a | 2021-02-23 09:58:04 -0500 | [diff] [blame] | 1241 | // Finds unconditional exits from a switch-case. Returns true if this statement unconditionally |
| 1242 | // causes an exit from this switch (via continue, break or return). |
| 1243 | static bool contains_unconditional_exit(Statement& stmt) { |
| 1244 | return contains_exit(stmt, /*conditionalExits=*/false); |
| 1245 | } |
John Stiles | b92641c | 2020-08-31 18:09:01 -0400 | [diff] [blame] | 1246 | |
John Stiles | 04ca41a | 2021-02-23 09:58:04 -0500 | [diff] [blame] | 1247 | // Finds conditional exits from a switch-case. Returns true if this statement contains a conditional |
| 1248 | // that wraps a potential exit from the switch (via continue, break or return). |
| 1249 | static bool contains_conditional_exit(Statement& stmt) { |
| 1250 | return contains_exit(stmt, /*conditionalExits=*/true); |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1251 | } |
| 1252 | |
John Stiles | 8f2a0cf | 2020-10-13 12:48:21 -0400 | [diff] [blame] | 1253 | static void move_all_but_break(std::unique_ptr<Statement>& stmt, StatementArray* target) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1254 | switch (stmt->kind()) { |
| 1255 | case Statement::Kind::kBlock: { |
John Stiles | 92219b4 | 2020-06-15 12:32:24 -0400 | [diff] [blame] | 1256 | // Recurse into the block. |
| 1257 | Block& block = static_cast<Block&>(*stmt); |
| 1258 | |
John Stiles | 8f2a0cf | 2020-10-13 12:48:21 -0400 | [diff] [blame] | 1259 | StatementArray blockStmts; |
John Stiles | f4bda74 | 2020-10-14 16:57:41 -0400 | [diff] [blame] | 1260 | blockStmts.reserve_back(block.children().size()); |
Ethan Nicholas | 7bd6043 | 2020-09-25 14:31:59 -0400 | [diff] [blame] | 1261 | for (std::unique_ptr<Statement>& stmt : block.children()) { |
| 1262 | move_all_but_break(stmt, &blockStmts); |
Ethan Nicholas | 739e1ca | 2020-06-11 12:16:14 -0400 | [diff] [blame] | 1263 | } |
John Stiles | 92219b4 | 2020-06-15 12:32:24 -0400 | [diff] [blame] | 1264 | |
| 1265 | target->push_back(std::make_unique<Block>(block.fOffset, std::move(blockStmts), |
Ethan Nicholas | 7bd6043 | 2020-09-25 14:31:59 -0400 | [diff] [blame] | 1266 | block.symbolTable(), block.isScope())); |
Ethan Nicholas | 739e1ca | 2020-06-11 12:16:14 -0400 | [diff] [blame] | 1267 | break; |
John Stiles | 92219b4 | 2020-06-15 12:32:24 -0400 | [diff] [blame] | 1268 | } |
| 1269 | |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1270 | case Statement::Kind::kBreak: |
John Stiles | 92219b4 | 2020-06-15 12:32:24 -0400 | [diff] [blame] | 1271 | // Do not append a break to the target. |
| 1272 | break; |
| 1273 | |
Ethan Nicholas | 739e1ca | 2020-06-11 12:16:14 -0400 | [diff] [blame] | 1274 | default: |
John Stiles | 92219b4 | 2020-06-15 12:32:24 -0400 | [diff] [blame] | 1275 | // Append normal statements to the target. |
| 1276 | target->push_back(std::move(stmt)); |
| 1277 | break; |
Ethan Nicholas | 739e1ca | 2020-06-11 12:16:14 -0400 | [diff] [blame] | 1278 | } |
| 1279 | } |
| 1280 | |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1281 | // Returns a block containing all of the statements that will be run if the given case matches |
| 1282 | // (which, owing to the statements being owned by unique_ptrs, means the switch itself will be |
| 1283 | // broken by this call and must then be discarded). |
| 1284 | // Returns null (and leaves the switch unmodified) if no such simple reduction is possible, such as |
| 1285 | // when break statements appear inside conditionals. |
John Stiles | 92219b4 | 2020-06-15 12:32:24 -0400 | [diff] [blame] | 1286 | static std::unique_ptr<Statement> block_for_case(SwitchStatement* switchStatement, |
| 1287 | SwitchCase* caseToCapture) { |
| 1288 | // We have to be careful to not move any of the pointers until after we're sure we're going to |
| 1289 | // succeed, so before we make any changes at all, we check the switch-cases to decide on a plan |
| 1290 | // of action. First, find the switch-case we are interested in. |
Ethan Nicholas | 01b05e5 | 2020-10-22 15:53:41 -0400 | [diff] [blame] | 1291 | auto iter = switchStatement->cases().begin(); |
| 1292 | for (; iter != switchStatement->cases().end(); ++iter) { |
John Stiles | 2d4f959 | 2020-10-30 10:29:12 -0400 | [diff] [blame] | 1293 | if (iter->get() == caseToCapture) { |
John Stiles | 92219b4 | 2020-06-15 12:32:24 -0400 | [diff] [blame] | 1294 | break; |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1295 | } |
John Stiles | 92219b4 | 2020-06-15 12:32:24 -0400 | [diff] [blame] | 1296 | } |
| 1297 | |
| 1298 | // Next, walk forward through the rest of the switch. If we find a conditional break, we're |
| 1299 | // stuck and can't simplify at all. If we find an unconditional break, we have a range of |
| 1300 | // statements that we can use for simplification. |
| 1301 | auto startIter = iter; |
John Stiles | 04ca41a | 2021-02-23 09:58:04 -0500 | [diff] [blame] | 1302 | Statement* stripBreakStmt = nullptr; |
Ethan Nicholas | 01b05e5 | 2020-10-22 15:53:41 -0400 | [diff] [blame] | 1303 | for (; iter != switchStatement->cases().end(); ++iter) { |
John Stiles | 2d4f959 | 2020-10-30 10:29:12 -0400 | [diff] [blame] | 1304 | for (std::unique_ptr<Statement>& stmt : (*iter)->statements()) { |
John Stiles | 04ca41a | 2021-02-23 09:58:04 -0500 | [diff] [blame] | 1305 | if (contains_conditional_exit(*stmt)) { |
| 1306 | // We can't reduce switch-cases to a block when they have conditional exits. |
John Stiles | 92219b4 | 2020-06-15 12:32:24 -0400 | [diff] [blame] | 1307 | return nullptr; |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1308 | } |
John Stiles | 04ca41a | 2021-02-23 09:58:04 -0500 | [diff] [blame] | 1309 | if (contains_unconditional_exit(*stmt)) { |
| 1310 | // We found an unconditional exit. We can use this block, but we need to strip |
| 1311 | // out a break statement if it has one. |
| 1312 | stripBreakStmt = stmt.get(); |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1313 | break; |
| 1314 | } |
| 1315 | } |
John Stiles | 92219b4 | 2020-06-15 12:32:24 -0400 | [diff] [blame] | 1316 | |
John Stiles | 04ca41a | 2021-02-23 09:58:04 -0500 | [diff] [blame] | 1317 | if (stripBreakStmt) { |
John Stiles | 92219b4 | 2020-06-15 12:32:24 -0400 | [diff] [blame] | 1318 | break; |
| 1319 | } |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1320 | } |
John Stiles | 92219b4 | 2020-06-15 12:32:24 -0400 | [diff] [blame] | 1321 | |
| 1322 | // We fell off the bottom of the switch or encountered a break. We know the range of statements |
| 1323 | // that we need to move over, and we know it's safe to do so. |
John Stiles | 8f2a0cf | 2020-10-13 12:48:21 -0400 | [diff] [blame] | 1324 | StatementArray caseStmts; |
John Stiles | 92219b4 | 2020-06-15 12:32:24 -0400 | [diff] [blame] | 1325 | |
| 1326 | // We can move over most of the statements as-is. |
| 1327 | while (startIter != iter) { |
John Stiles | 2d4f959 | 2020-10-30 10:29:12 -0400 | [diff] [blame] | 1328 | for (std::unique_ptr<Statement>& stmt : (*startIter)->statements()) { |
John Stiles | 92219b4 | 2020-06-15 12:32:24 -0400 | [diff] [blame] | 1329 | caseStmts.push_back(std::move(stmt)); |
| 1330 | } |
| 1331 | ++startIter; |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1332 | } |
John Stiles | 92219b4 | 2020-06-15 12:32:24 -0400 | [diff] [blame] | 1333 | |
John Stiles | 04ca41a | 2021-02-23 09:58:04 -0500 | [diff] [blame] | 1334 | // For the last statement, we need to move what we can, stopping at a break if there is one. |
| 1335 | if (stripBreakStmt != nullptr) { |
John Stiles | 2d4f959 | 2020-10-30 10:29:12 -0400 | [diff] [blame] | 1336 | for (std::unique_ptr<Statement>& stmt : (*startIter)->statements()) { |
John Stiles | 04ca41a | 2021-02-23 09:58:04 -0500 | [diff] [blame] | 1337 | if (stmt.get() == stripBreakStmt) { |
John Stiles | 92219b4 | 2020-06-15 12:32:24 -0400 | [diff] [blame] | 1338 | move_all_but_break(stmt, &caseStmts); |
John Stiles | 04ca41a | 2021-02-23 09:58:04 -0500 | [diff] [blame] | 1339 | stripBreakStmt = nullptr; |
John Stiles | 92219b4 | 2020-06-15 12:32:24 -0400 | [diff] [blame] | 1340 | break; |
| 1341 | } |
| 1342 | |
| 1343 | caseStmts.push_back(std::move(stmt)); |
| 1344 | } |
| 1345 | } |
| 1346 | |
John Stiles | 04ca41a | 2021-02-23 09:58:04 -0500 | [diff] [blame] | 1347 | SkASSERT(stripBreakStmt == nullptr); // Verify that we fixed the unconditional break. |
John Stiles | 92219b4 | 2020-06-15 12:32:24 -0400 | [diff] [blame] | 1348 | |
| 1349 | // Return our newly-synthesized block. |
Ethan Nicholas | 01b05e5 | 2020-10-22 15:53:41 -0400 | [diff] [blame] | 1350 | return std::make_unique<Block>(/*offset=*/-1, std::move(caseStmts), switchStatement->symbols()); |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1351 | } |
| 1352 | |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1353 | void Compiler::simplifyStatement(DefinitionMap& definitions, |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1354 | BasicBlock& b, |
| 1355 | std::vector<BasicBlock::Node>::iterator* iter, |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 1356 | OptimizationContext* optimizationContext) { |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 1357 | ProgramUsage* usage = optimizationContext->fUsage; |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1358 | Statement* stmt = (*iter)->statement()->get(); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1359 | switch (stmt->kind()) { |
| 1360 | case Statement::Kind::kVarDeclaration: { |
John Stiles | a5a97b4 | 2020-08-18 11:19:07 -0400 | [diff] [blame] | 1361 | const auto& varDecl = stmt->as<VarDeclaration>(); |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 1362 | if (usage->isDead(varDecl.var()) && |
Ethan Nicholas | c51f33e | 2020-10-13 13:49:44 -0400 | [diff] [blame] | 1363 | (!varDecl.value() || |
| 1364 | !varDecl.value()->hasSideEffects())) { |
| 1365 | if (varDecl.value()) { |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 1366 | SkASSERT((*iter)->statement()->get() == stmt); |
Ethan Nicholas | c51f33e | 2020-10-13 13:49:44 -0400 | [diff] [blame] | 1367 | if (!b.tryRemoveExpressionBefore(iter, varDecl.value().get())) { |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 1368 | optimizationContext->fNeedsRescan = true; |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1369 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1370 | } |
Ethan Nicholas | 5b9b0db | 2021-01-21 13:12:01 -0500 | [diff] [blame] | 1371 | // There can still be (soon to be removed) references to the variable at this point. |
| 1372 | // Allowing the VarDeclaration to be destroyed here will break those variable's |
| 1373 | // initialValue()s, so we hang on to them until optimization is finished. |
| 1374 | std::unique_ptr<Statement> old = (*iter)->setStatement(std::make_unique<Nop>(), |
| 1375 | usage); |
| 1376 | optimizationContext->fOwnedStatements.push_back(std::move(old)); |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 1377 | optimizationContext->fUpdated = true; |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1378 | } |
| 1379 | break; |
| 1380 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1381 | case Statement::Kind::kIf: { |
John Stiles | a5a97b4 | 2020-08-18 11:19:07 -0400 | [diff] [blame] | 1382 | IfStatement& i = stmt->as<IfStatement>(); |
Ethan Nicholas | 8c44eca | 2020-10-07 16:47:09 -0400 | [diff] [blame] | 1383 | if (i.test()->kind() == Expression::Kind::kBoolLiteral) { |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1384 | // constant if, collapse down to a single branch |
Ethan Nicholas | 8c44eca | 2020-10-07 16:47:09 -0400 | [diff] [blame] | 1385 | if (i.test()->as<BoolLiteral>().value()) { |
| 1386 | SkASSERT(i.ifTrue()); |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 1387 | (*iter)->setStatement(std::move(i.ifTrue()), usage); |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1388 | } else { |
Ethan Nicholas | 8c44eca | 2020-10-07 16:47:09 -0400 | [diff] [blame] | 1389 | if (i.ifFalse()) { |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 1390 | (*iter)->setStatement(std::move(i.ifFalse()), usage); |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1391 | } else { |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 1392 | (*iter)->setStatement(std::make_unique<Nop>(), usage); |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1393 | } |
| 1394 | } |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 1395 | optimizationContext->fUpdated = true; |
| 1396 | optimizationContext->fNeedsRescan = true; |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1397 | break; |
| 1398 | } |
Ethan Nicholas | 8c44eca | 2020-10-07 16:47:09 -0400 | [diff] [blame] | 1399 | if (i.ifFalse() && i.ifFalse()->isEmpty()) { |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1400 | // else block doesn't do anything, remove it |
Ethan Nicholas | 8c44eca | 2020-10-07 16:47:09 -0400 | [diff] [blame] | 1401 | i.ifFalse().reset(); |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 1402 | optimizationContext->fUpdated = true; |
| 1403 | optimizationContext->fNeedsRescan = true; |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1404 | } |
Ethan Nicholas | 8c44eca | 2020-10-07 16:47:09 -0400 | [diff] [blame] | 1405 | if (!i.ifFalse() && i.ifTrue()->isEmpty()) { |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1406 | // if block doesn't do anything, no else block |
Ethan Nicholas | 8c44eca | 2020-10-07 16:47:09 -0400 | [diff] [blame] | 1407 | if (i.test()->hasSideEffects()) { |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1408 | // test has side effects, keep it |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 1409 | (*iter)->setStatement( |
| 1410 | std::make_unique<ExpressionStatement>(std::move(i.test())), usage); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1411 | } else { |
| 1412 | // no if, no else, no test side effects, kill the whole if |
| 1413 | // statement |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 1414 | (*iter)->setStatement(std::make_unique<Nop>(), usage); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1415 | } |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 1416 | optimizationContext->fUpdated = true; |
| 1417 | optimizationContext->fNeedsRescan = true; |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1418 | } |
| 1419 | break; |
| 1420 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1421 | case Statement::Kind::kSwitch: { |
John Stiles | a5a97b4 | 2020-08-18 11:19:07 -0400 | [diff] [blame] | 1422 | SwitchStatement& s = stmt->as<SwitchStatement>(); |
Ethan Nicholas | 34b19c5 | 2020-09-14 11:33:47 -0400 | [diff] [blame] | 1423 | int64_t switchValue; |
Ethan Nicholas | c0f9815 | 2021-02-05 16:21:10 -0500 | [diff] [blame] | 1424 | if (ConstantFolder::GetConstantInt(*s.value(), &switchValue)) { |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1425 | // switch is constant, replace it with the case that matches |
| 1426 | bool found = false; |
| 1427 | SwitchCase* defaultCase = nullptr; |
John Stiles | 2d4f959 | 2020-10-30 10:29:12 -0400 | [diff] [blame] | 1428 | for (const std::unique_ptr<SwitchCase>& c : s.cases()) { |
| 1429 | if (!c->value()) { |
| 1430 | defaultCase = c.get(); |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1431 | continue; |
| 1432 | } |
Ethan Nicholas | 34b19c5 | 2020-09-14 11:33:47 -0400 | [diff] [blame] | 1433 | int64_t caseValue; |
Ethan Nicholas | c0f9815 | 2021-02-05 16:21:10 -0500 | [diff] [blame] | 1434 | SkAssertResult(ConstantFolder::GetConstantInt(*c->value(), &caseValue)); |
Ethan Nicholas | 34b19c5 | 2020-09-14 11:33:47 -0400 | [diff] [blame] | 1435 | if (caseValue == switchValue) { |
John Stiles | 2d4f959 | 2020-10-30 10:29:12 -0400 | [diff] [blame] | 1436 | std::unique_ptr<Statement> newBlock = block_for_case(&s, c.get()); |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1437 | if (newBlock) { |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 1438 | (*iter)->setStatement(std::move(newBlock), usage); |
John Stiles | 9d94423 | 2020-08-19 09:56:49 -0400 | [diff] [blame] | 1439 | found = true; |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1440 | break; |
| 1441 | } else { |
John Stiles | d6a5f449 | 2021-02-11 15:46:11 -0500 | [diff] [blame] | 1442 | if (s.isStatic() && |
John Stiles | d120464 | 2021-02-17 16:30:02 -0500 | [diff] [blame] | 1443 | !fContext->fConfig->fSettings.fPermitInvalidStaticTests) { |
John Stiles | d6a5f449 | 2021-02-11 15:46:11 -0500 | [diff] [blame] | 1444 | auto [iter, didInsert] = optimizationContext->fSilences.insert(&s); |
| 1445 | if (didInsert) { |
| 1446 | this->error(s.fOffset, "static switch contains non-static " |
John Stiles | 04ca41a | 2021-02-23 09:58:04 -0500 | [diff] [blame] | 1447 | "conditional exit"); |
John Stiles | d6a5f449 | 2021-02-11 15:46:11 -0500 | [diff] [blame] | 1448 | } |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1449 | } |
| 1450 | return; // can't simplify |
| 1451 | } |
| 1452 | } |
| 1453 | } |
| 1454 | if (!found) { |
| 1455 | // no matching case. use default if it exists, or kill the whole thing |
| 1456 | if (defaultCase) { |
| 1457 | std::unique_ptr<Statement> newBlock = block_for_case(&s, defaultCase); |
| 1458 | if (newBlock) { |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 1459 | (*iter)->setStatement(std::move(newBlock), usage); |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1460 | } else { |
John Stiles | d6a5f449 | 2021-02-11 15:46:11 -0500 | [diff] [blame] | 1461 | if (s.isStatic() && |
John Stiles | d120464 | 2021-02-17 16:30:02 -0500 | [diff] [blame] | 1462 | !fContext->fConfig->fSettings.fPermitInvalidStaticTests) { |
John Stiles | d6a5f449 | 2021-02-11 15:46:11 -0500 | [diff] [blame] | 1463 | auto [iter, didInsert] = optimizationContext->fSilences.insert(&s); |
| 1464 | if (didInsert) { |
| 1465 | this->error(s.fOffset, "static switch contains non-static " |
John Stiles | 04ca41a | 2021-02-23 09:58:04 -0500 | [diff] [blame] | 1466 | "conditional exit"); |
John Stiles | d6a5f449 | 2021-02-11 15:46:11 -0500 | [diff] [blame] | 1467 | } |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1468 | } |
| 1469 | return; // can't simplify |
| 1470 | } |
| 1471 | } else { |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 1472 | (*iter)->setStatement(std::make_unique<Nop>(), usage); |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1473 | } |
| 1474 | } |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 1475 | optimizationContext->fUpdated = true; |
| 1476 | optimizationContext->fNeedsRescan = true; |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1477 | } |
| 1478 | break; |
| 1479 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1480 | case Statement::Kind::kExpression: { |
John Stiles | a5a97b4 | 2020-08-18 11:19:07 -0400 | [diff] [blame] | 1481 | ExpressionStatement& e = stmt->as<ExpressionStatement>(); |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 1482 | SkASSERT((*iter)->statement()->get() == &e); |
Ethan Nicholas | d503a5a | 2020-09-30 09:29:55 -0400 | [diff] [blame] | 1483 | if (!e.expression()->hasSideEffects()) { |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1484 | // Expression statement with no side effects, kill it |
Ethan Nicholas | d503a5a | 2020-09-30 09:29:55 -0400 | [diff] [blame] | 1485 | if (!b.tryRemoveExpressionBefore(iter, e.expression().get())) { |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 1486 | optimizationContext->fNeedsRescan = true; |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1487 | } |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 1488 | SkASSERT((*iter)->statement()->get() == stmt); |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 1489 | (*iter)->setStatement(std::make_unique<Nop>(), usage); |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 1490 | optimizationContext->fUpdated = true; |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1491 | } |
| 1492 | break; |
| 1493 | } |
| 1494 | default: |
| 1495 | break; |
| 1496 | } |
| 1497 | } |
| 1498 | |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 1499 | bool Compiler::scanCFG(FunctionDefinition& f, ProgramUsage* usage) { |
John Stiles | 0cc193a | 2020-09-09 09:39:34 -0400 | [diff] [blame] | 1500 | bool madeChanges = false; |
| 1501 | |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1502 | CFG cfg = CFGGenerator().getCFG(f); |
| 1503 | this->computeDataFlow(&cfg); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 1504 | |
John Stiles | 66c53b9 | 2021-02-20 08:00:43 -0500 | [diff] [blame] | 1505 | if (fContext->fConfig->fSettings.fDeadCodeElimination) { |
| 1506 | // Check for unreachable code. |
| 1507 | for (size_t i = 0; i < cfg.fBlocks.size(); i++) { |
| 1508 | const BasicBlock& block = cfg.fBlocks[i]; |
| 1509 | if (!block.fIsReachable && !block.fAllowUnreachable && block.fNodes.size()) { |
| 1510 | const BasicBlock::Node& node = block.fNodes[0]; |
| 1511 | int offset = node.isStatement() ? (*node.statement())->fOffset |
| 1512 | : (*node.expression())->fOffset; |
| 1513 | this->error(offset, String("unreachable")); |
| 1514 | } |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 1515 | } |
| 1516 | } |
| 1517 | if (fErrorCount) { |
John Stiles | 0cc193a | 2020-09-09 09:39:34 -0400 | [diff] [blame] | 1518 | return madeChanges; |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 1519 | } |
| 1520 | |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1521 | // check for dead code & undefined variables, perform constant propagation |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 1522 | OptimizationContext optimizationContext; |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 1523 | optimizationContext.fUsage = usage; |
John Stiles | 7d3f089 | 2020-11-03 11:35:01 -0500 | [diff] [blame] | 1524 | SkBitSet eliminatedBlockIds(cfg.fBlocks.size()); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1525 | do { |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 1526 | if (optimizationContext.fNeedsRescan) { |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1527 | cfg = CFGGenerator().getCFG(f); |
| 1528 | this->computeDataFlow(&cfg); |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 1529 | optimizationContext.fNeedsRescan = false; |
Ethan Nicholas | 113628d | 2017-02-02 16:11:39 -0500 | [diff] [blame] | 1530 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1531 | |
John Stiles | 7d3f089 | 2020-11-03 11:35:01 -0500 | [diff] [blame] | 1532 | eliminatedBlockIds.reset(); |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 1533 | optimizationContext.fUpdated = false; |
John Stiles | 7d3f089 | 2020-11-03 11:35:01 -0500 | [diff] [blame] | 1534 | |
| 1535 | for (BlockId blockId = 0; blockId < cfg.fBlocks.size(); ++blockId) { |
| 1536 | if (eliminatedBlockIds.test(blockId)) { |
| 1537 | // We reached a block ID that might have been eliminated. Be cautious and rescan. |
| 1538 | optimizationContext.fUpdated = true; |
| 1539 | optimizationContext.fNeedsRescan = true; |
| 1540 | break; |
| 1541 | } |
| 1542 | |
| 1543 | BasicBlock& b = cfg.fBlocks[blockId]; |
John Stiles | 66c53b9 | 2021-02-20 08:00:43 -0500 | [diff] [blame] | 1544 | if (fContext->fConfig->fSettings.fDeadCodeElimination) { |
| 1545 | if (blockId > 0 && !b.fIsReachable) { |
| 1546 | // Block was reachable before optimization, but has since become unreachable. In |
| 1547 | // addition to being dead code, it's broken - since control flow can't reach it, |
| 1548 | // no prior variable definitions can reach it, and therefore variables might |
| 1549 | // look to have not been properly assigned. Kill it by replacing all statements |
| 1550 | // with Nops. |
| 1551 | for (BasicBlock::Node& node : b.fNodes) { |
| 1552 | if (node.isStatement() && !(*node.statement())->is<Nop>()) { |
| 1553 | // Eliminating a node runs the risk of eliminating that node's exits as |
| 1554 | // well. Keep track of this and do a rescan if we are about to access |
| 1555 | // one of these. |
| 1556 | for (BlockId id : b.fExits) { |
| 1557 | eliminatedBlockIds.set(id); |
| 1558 | } |
| 1559 | node.setStatement(std::make_unique<Nop>(), usage); |
| 1560 | madeChanges = true; |
John Stiles | 7d3f089 | 2020-11-03 11:35:01 -0500 | [diff] [blame] | 1561 | } |
Ethan Nicholas | 1de1481 | 2020-06-19 15:32:49 -0400 | [diff] [blame] | 1562 | } |
John Stiles | 66c53b9 | 2021-02-20 08:00:43 -0500 | [diff] [blame] | 1563 | continue; |
Ethan Nicholas | 1de1481 | 2020-06-19 15:32:49 -0400 | [diff] [blame] | 1564 | } |
Ethan Nicholas | 1de1481 | 2020-06-19 15:32:49 -0400 | [diff] [blame] | 1565 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1566 | DefinitionMap definitions = b.fBefore; |
| 1567 | |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 1568 | for (auto iter = b.fNodes.begin(); iter != b.fNodes.end() && |
| 1569 | !optimizationContext.fNeedsRescan; ++iter) { |
John Stiles | 70025e5 | 2020-09-28 16:08:58 -0400 | [diff] [blame] | 1570 | if (iter->isExpression()) { |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 1571 | this->simplifyExpression(definitions, b, &iter, &optimizationContext); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1572 | } else { |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 1573 | this->simplifyStatement(definitions, b, &iter, &optimizationContext); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1574 | } |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 1575 | if (optimizationContext.fNeedsRescan) { |
Ethan Nicholas | 4b330df | 2017-05-17 10:52:55 -0400 | [diff] [blame] | 1576 | break; |
| 1577 | } |
John Stiles | e8a2492 | 2021-02-08 17:54:08 -0500 | [diff] [blame] | 1578 | definitions.addDefinitions(*fContext, *iter); |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1579 | } |
Brian Osman | 01a3eb4 | 2020-09-14 11:32:49 -0400 | [diff] [blame] | 1580 | |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 1581 | if (optimizationContext.fNeedsRescan) { |
Brian Osman | 01a3eb4 | 2020-09-14 11:32:49 -0400 | [diff] [blame] | 1582 | break; |
| 1583 | } |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1584 | } |
Ethan Nicholas | cdeae8c | 2020-10-22 14:39:46 -0400 | [diff] [blame] | 1585 | madeChanges |= optimizationContext.fUpdated; |
| 1586 | } while (optimizationContext.fUpdated); |
| 1587 | SkASSERT(!optimizationContext.fNeedsRescan); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 1588 | |
| 1589 | // check for missing return |
John Stiles | 54e7c05 | 2021-01-11 14:22:36 -0500 | [diff] [blame] | 1590 | if (f.declaration().returnType() != *fContext->fTypes.fVoid) { |
John Stiles | 61e75e3 | 2020-10-01 15:42:37 -0400 | [diff] [blame] | 1591 | if (cfg.fBlocks[cfg.fExit].fIsReachable) { |
Ethan Nicholas | 0a5d096 | 2020-10-14 13:33:18 -0400 | [diff] [blame] | 1592 | this->error(f.fOffset, String("function '" + String(f.declaration().name()) + |
Ethan Nicholas | db80f69 | 2019-11-22 14:06:12 -0500 | [diff] [blame] | 1593 | "' can exit without returning a value")); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 1594 | } |
| 1595 | } |
John Stiles | 0cc193a | 2020-09-09 09:39:34 -0400 | [diff] [blame] | 1596 | |
| 1597 | return madeChanges; |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 1598 | } |
| 1599 | |
Brian Osman | 32d5355 | 2020-09-23 13:55:20 -0400 | [diff] [blame] | 1600 | std::unique_ptr<Program> Compiler::convertProgram( |
John Stiles | dbd4e6f | 2021-02-16 13:29:15 -0500 | [diff] [blame] | 1601 | ProgramKind kind, |
Brian Osman | 32d5355 | 2020-09-23 13:55:20 -0400 | [diff] [blame] | 1602 | String text, |
| 1603 | const Program::Settings& settings, |
Brian Osman | be0b3b7 | 2021-01-06 14:27:35 -0500 | [diff] [blame] | 1604 | const std::vector<std::unique_ptr<ExternalFunction>>* externalFunctions) { |
Leon Scroggins | b66214e | 2021-02-11 17:14:18 -0500 | [diff] [blame] | 1605 | ATRACE_ANDROID_FRAMEWORK("SkSL::Compiler::convertProgram"); |
| 1606 | |
John Stiles | dbd4e6f | 2021-02-16 13:29:15 -0500 | [diff] [blame] | 1607 | SkASSERT(!externalFunctions || (kind == ProgramKind::kGeneric)); |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 1608 | |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 1609 | // Loading and optimizing our base module might reset the inliner, so do that first, |
| 1610 | // *then* configure the inliner with the settings for this program. |
| 1611 | const ParsedModule& baseModule = this->moduleForProgramKind(kind); |
| 1612 | |
John Stiles | 270cec2 | 2021-02-17 12:59:36 -0500 | [diff] [blame] | 1613 | // Update our context to point to the program configuration for the duration of compilation. |
| 1614 | auto config = std::make_unique<ProgramConfig>(ProgramConfig{kind, settings}); |
| 1615 | |
| 1616 | SkASSERT(!fContext->fConfig); |
| 1617 | fContext->fConfig = config.get(); |
| 1618 | SK_AT_SCOPE_EXIT(fContext->fConfig = nullptr); |
| 1619 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1620 | fErrorText = ""; |
| 1621 | fErrorCount = 0; |
John Stiles | d120464 | 2021-02-17 16:30:02 -0500 | [diff] [blame] | 1622 | fInliner.reset(fIRGenerator->fModifiers.get()); |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 1623 | |
| 1624 | // 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] | 1625 | std::unique_ptr<String> textPtr(new String(std::move(text))); |
| 1626 | fSource = textPtr.get(); |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 1627 | |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 1628 | // Enable node pooling while converting and optimizing the program for a performance boost. |
| 1629 | // The Program will take ownership of the pool. |
Brian Osman | 28f702c | 2021-02-02 11:52:07 -0500 | [diff] [blame] | 1630 | std::unique_ptr<Pool> pool; |
| 1631 | if (fCaps->useNodePools()) { |
| 1632 | pool = Pool::Create(); |
| 1633 | pool->attachToThread(); |
| 1634 | } |
John Stiles | d120464 | 2021-02-17 16:30:02 -0500 | [diff] [blame] | 1635 | IRGenerator::IRBundle ir = fIRGenerator->convertProgram(baseModule, /*isBuiltinCode=*/false, |
| 1636 | textPtr->c_str(), textPtr->size(), |
| 1637 | externalFunctions); |
John Stiles | 270cec2 | 2021-02-17 12:59:36 -0500 | [diff] [blame] | 1638 | auto program = std::make_unique<Program>(std::move(textPtr), |
| 1639 | std::move(config), |
Brian Osman | d7e7659 | 2020-11-02 12:26:22 -0500 | [diff] [blame] | 1640 | fCaps, |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 1641 | fContext, |
| 1642 | std::move(ir.fElements), |
Brian Osman | 133724c | 2020-10-28 14:14:39 -0400 | [diff] [blame] | 1643 | std::move(ir.fSharedElements), |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 1644 | std::move(ir.fModifiers), |
| 1645 | std::move(ir.fSymbolTable), |
| 1646 | std::move(pool), |
| 1647 | ir.fInputs); |
| 1648 | bool success = false; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1649 | if (fErrorCount) { |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 1650 | // Do not return programs that failed to compile. |
Brian Osman | 4f065e2 | 2021-02-13 01:06:03 +0000 | [diff] [blame] | 1651 | } else if (settings.fOptimize && !this->optimize(*program)) { |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 1652 | // Do not return programs that failed to optimize. |
| 1653 | } else { |
| 1654 | // We have a successful program! |
| 1655 | success = true; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1656 | } |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 1657 | |
Brian Osman | 28f702c | 2021-02-02 11:52:07 -0500 | [diff] [blame] | 1658 | if (program->fPool) { |
| 1659 | program->fPool->detachFromThread(); |
| 1660 | } |
John Stiles | 5c7bb32 | 2020-10-22 11:09:15 -0400 | [diff] [blame] | 1661 | return success ? std::move(program) : nullptr; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1662 | } |
| 1663 | |
John Stiles | bb1505f | 2021-02-12 09:17:53 -0500 | [diff] [blame] | 1664 | void Compiler::verifyStaticTests(const Program& program) { |
| 1665 | class StaticTestVerifier : public ProgramVisitor { |
| 1666 | public: |
| 1667 | StaticTestVerifier(ErrorReporter* r) : fReporter(r) {} |
| 1668 | |
| 1669 | using ProgramVisitor::visitProgramElement; |
| 1670 | |
| 1671 | bool visitStatement(const Statement& stmt) override { |
| 1672 | switch (stmt.kind()) { |
| 1673 | case Statement::Kind::kIf: |
| 1674 | if (stmt.as<IfStatement>().isStatic()) { |
| 1675 | fReporter->error(stmt.fOffset, "static if has non-static test"); |
| 1676 | } |
| 1677 | break; |
| 1678 | |
| 1679 | case Statement::Kind::kSwitch: |
| 1680 | if (stmt.as<SwitchStatement>().isStatic()) { |
| 1681 | fReporter->error(stmt.fOffset, "static switch has non-static test"); |
| 1682 | } |
| 1683 | break; |
| 1684 | |
| 1685 | default: |
| 1686 | break; |
| 1687 | } |
| 1688 | return INHERITED::visitStatement(stmt); |
| 1689 | } |
| 1690 | |
John Stiles | 59e3456 | 2021-02-12 16:56:39 -0500 | [diff] [blame] | 1691 | bool visitExpression(const Expression&) override { |
| 1692 | // We aren't looking for anything inside an Expression, so skip them entirely. |
| 1693 | return false; |
| 1694 | } |
| 1695 | |
John Stiles | bb1505f | 2021-02-12 09:17:53 -0500 | [diff] [blame] | 1696 | private: |
| 1697 | using INHERITED = ProgramVisitor; |
| 1698 | ErrorReporter* fReporter; |
| 1699 | }; |
| 1700 | |
| 1701 | // 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] | 1702 | if (fContext->fConfig->fSettings.fPermitInvalidStaticTests) { |
John Stiles | bb1505f | 2021-02-12 09:17:53 -0500 | [diff] [blame] | 1703 | return; |
| 1704 | } |
| 1705 | |
| 1706 | // Check all of the program's owned elements. (Built-in elements are assumed to be valid.) |
| 1707 | StaticTestVerifier visitor{this}; |
| 1708 | for (const std::unique_ptr<ProgramElement>& element : program.ownedElements()) { |
| 1709 | if (element->is<FunctionDefinition>()) { |
| 1710 | visitor.visitProgramElement(*element); |
| 1711 | } |
| 1712 | } |
| 1713 | } |
| 1714 | |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 1715 | bool Compiler::optimize(LoadedModule& module) { |
| 1716 | SkASSERT(!fErrorCount); |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 1717 | |
John Stiles | 270cec2 | 2021-02-17 12:59:36 -0500 | [diff] [blame] | 1718 | // Create a temporary program configuration with default settings. |
| 1719 | ProgramConfig config; |
| 1720 | config.fKind = module.fKind; |
| 1721 | |
| 1722 | // Update our context to point to this configuration for the duration of compilation. |
| 1723 | SkASSERT(!fContext->fConfig); |
| 1724 | fContext->fConfig = &config; |
| 1725 | SK_AT_SCOPE_EXIT(fContext->fConfig = nullptr); |
| 1726 | |
John Stiles | d120464 | 2021-02-17 16:30:02 -0500 | [diff] [blame] | 1727 | // Reset the Inliner. |
| 1728 | fInliner.reset(fModifiers.back().get()); |
John Stiles | 270cec2 | 2021-02-17 12:59:36 -0500 | [diff] [blame] | 1729 | |
| 1730 | std::unique_ptr<ProgramUsage> usage = Analysis::GetUsage(module); |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 1731 | |
| 1732 | while (fErrorCount == 0) { |
| 1733 | bool madeChanges = false; |
| 1734 | |
| 1735 | // Scan and optimize based on the control-flow graph for each function. |
| 1736 | for (const auto& element : module.fElements) { |
| 1737 | if (element->is<FunctionDefinition>()) { |
| 1738 | madeChanges |= this->scanCFG(element->as<FunctionDefinition>(), usage.get()); |
| 1739 | } |
| 1740 | } |
| 1741 | |
| 1742 | // Perform inline-candidate analysis and inline any functions deemed suitable. |
John Stiles | 7804758 | 2020-12-16 16:17:41 -0500 | [diff] [blame] | 1743 | madeChanges |= fInliner.analyze(module.fElements, module.fSymbols, usage.get()); |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 1744 | |
| 1745 | if (!madeChanges) { |
| 1746 | break; |
| 1747 | } |
| 1748 | } |
| 1749 | return fErrorCount == 0; |
| 1750 | } |
| 1751 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 1752 | bool Compiler::optimize(Program& program) { |
| 1753 | SkASSERT(!fErrorCount); |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 1754 | ProgramUsage* usage = program.fUsage.get(); |
John Stiles | 7954d6c | 2020-09-01 10:53:02 -0400 | [diff] [blame] | 1755 | |
Ethan Nicholas | 34b19c5 | 2020-09-14 11:33:47 -0400 | [diff] [blame] | 1756 | while (fErrorCount == 0) { |
| 1757 | bool madeChanges = false; |
John Stiles | 7954d6c | 2020-09-01 10:53:02 -0400 | [diff] [blame] | 1758 | |
Ethan Nicholas | 34b19c5 | 2020-09-14 11:33:47 -0400 | [diff] [blame] | 1759 | // Scan and optimize based on the control-flow graph for each function. |
Brian Osman | 133724c | 2020-10-28 14:14:39 -0400 | [diff] [blame] | 1760 | for (const auto& element : program.ownedElements()) { |
Brian Osman | 1179fcf | 2020-10-08 16:04:40 -0400 | [diff] [blame] | 1761 | if (element->is<FunctionDefinition>()) { |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 1762 | madeChanges |= this->scanCFG(element->as<FunctionDefinition>(), usage); |
Ethan Nicholas | 34b19c5 | 2020-09-14 11:33:47 -0400 | [diff] [blame] | 1763 | } |
| 1764 | } |
| 1765 | |
| 1766 | // Perform inline-candidate analysis and inline any functions deemed suitable. |
John Stiles | 7804758 | 2020-12-16 16:17:41 -0500 | [diff] [blame] | 1767 | madeChanges |= fInliner.analyze(program.ownedElements(), program.fSymbols, usage); |
Ethan Nicholas | 34b19c5 | 2020-09-14 11:33:47 -0400 | [diff] [blame] | 1768 | |
| 1769 | // Remove dead functions. We wait until after analysis so that we still report errors, |
| 1770 | // even in unused code. |
John Stiles | 270cec2 | 2021-02-17 12:59:36 -0500 | [diff] [blame] | 1771 | if (program.fConfig->fSettings.fRemoveDeadFunctions) { |
Brian Osman | 133724c | 2020-10-28 14:14:39 -0400 | [diff] [blame] | 1772 | auto isDeadFunction = [&](const ProgramElement* element) { |
| 1773 | if (!element->is<FunctionDefinition>()) { |
| 1774 | return false; |
| 1775 | } |
| 1776 | const FunctionDefinition& fn = element->as<FunctionDefinition>(); |
| 1777 | if (fn.declaration().name() != "main" && usage->get(fn.declaration()) == 0) { |
| 1778 | usage->remove(*element); |
| 1779 | madeChanges = true; |
| 1780 | return true; |
| 1781 | } |
| 1782 | return false; |
| 1783 | }; |
Ethan Nicholas | 34b19c5 | 2020-09-14 11:33:47 -0400 | [diff] [blame] | 1784 | program.fElements.erase( |
Brian Osman | 133724c | 2020-10-28 14:14:39 -0400 | [diff] [blame] | 1785 | std::remove_if(program.fElements.begin(), program.fElements.end(), |
Ethan Nicholas | 34b19c5 | 2020-09-14 11:33:47 -0400 | [diff] [blame] | 1786 | [&](const std::unique_ptr<ProgramElement>& element) { |
Brian Osman | 133724c | 2020-10-28 14:14:39 -0400 | [diff] [blame] | 1787 | return isDeadFunction(element.get()); |
Ethan Nicholas | 34b19c5 | 2020-09-14 11:33:47 -0400 | [diff] [blame] | 1788 | }), |
| 1789 | program.fElements.end()); |
Brian Osman | 133724c | 2020-10-28 14:14:39 -0400 | [diff] [blame] | 1790 | program.fSharedElements.erase( |
| 1791 | std::remove_if(program.fSharedElements.begin(), program.fSharedElements.end(), |
| 1792 | isDeadFunction), |
| 1793 | program.fSharedElements.end()); |
Ethan Nicholas | 34b19c5 | 2020-09-14 11:33:47 -0400 | [diff] [blame] | 1794 | } |
| 1795 | |
John Stiles | 270cec2 | 2021-02-17 12:59:36 -0500 | [diff] [blame] | 1796 | if (program.fConfig->fKind != ProgramKind::kFragmentProcessor) { |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 1797 | // Remove declarations of dead global variables |
Brian Osman | 133724c | 2020-10-28 14:14:39 -0400 | [diff] [blame] | 1798 | auto isDeadVariable = [&](const ProgramElement* element) { |
| 1799 | if (!element->is<GlobalVarDeclaration>()) { |
| 1800 | return false; |
| 1801 | } |
| 1802 | const GlobalVarDeclaration& global = element->as<GlobalVarDeclaration>(); |
| 1803 | const VarDeclaration& varDecl = global.declaration()->as<VarDeclaration>(); |
| 1804 | if (usage->isDead(varDecl.var())) { |
| 1805 | madeChanges = true; |
| 1806 | return true; |
| 1807 | } |
| 1808 | return false; |
| 1809 | }; |
Ethan Nicholas | 34b19c5 | 2020-09-14 11:33:47 -0400 | [diff] [blame] | 1810 | program.fElements.erase( |
| 1811 | std::remove_if(program.fElements.begin(), program.fElements.end(), |
| 1812 | [&](const std::unique_ptr<ProgramElement>& element) { |
Brian Osman | 133724c | 2020-10-28 14:14:39 -0400 | [diff] [blame] | 1813 | return isDeadVariable(element.get()); |
Ethan Nicholas | 34b19c5 | 2020-09-14 11:33:47 -0400 | [diff] [blame] | 1814 | }), |
| 1815 | program.fElements.end()); |
Brian Osman | 133724c | 2020-10-28 14:14:39 -0400 | [diff] [blame] | 1816 | program.fSharedElements.erase( |
| 1817 | std::remove_if(program.fSharedElements.begin(), program.fSharedElements.end(), |
| 1818 | isDeadVariable), |
| 1819 | program.fSharedElements.end()); |
Ethan Nicholas | 34b19c5 | 2020-09-14 11:33:47 -0400 | [diff] [blame] | 1820 | } |
John Stiles | 73a6bff | 2020-09-09 13:40:37 -0400 | [diff] [blame] | 1821 | |
Ethan Nicholas | 34b19c5 | 2020-09-14 11:33:47 -0400 | [diff] [blame] | 1822 | if (!madeChanges) { |
| 1823 | break; |
Ethan Nicholas | 0dc8087 | 2019-02-08 15:46:24 -0500 | [diff] [blame] | 1824 | } |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 1825 | } |
John Stiles | bb1505f | 2021-02-12 09:17:53 -0500 | [diff] [blame] | 1826 | |
| 1827 | if (fErrorCount == 0) { |
| 1828 | this->verifyStaticTests(program); |
| 1829 | } |
| 1830 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 1831 | return fErrorCount == 0; |
| 1832 | } |
| 1833 | |
Brian Osman | fb32ddf | 2019-06-18 10:14:20 -0400 | [diff] [blame] | 1834 | #if defined(SKSL_STANDALONE) || SK_SUPPORT_GPU |
| 1835 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 1836 | bool Compiler::toSPIRV(Program& program, OutputStream& out) { |
Ethan Nicholas | a6ae1f7 | 2017-03-16 09:56:54 -0400 | [diff] [blame] | 1837 | #ifdef SK_ENABLE_SPIRV_VALIDATION |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1838 | StringStream buffer; |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 1839 | AutoSource as(this, program.fSource.get()); |
Brian Osman | 8b43dad | 2020-10-09 13:31:42 -0400 | [diff] [blame] | 1840 | SPIRVCodeGenerator cg(fContext.get(), &program, this, &buffer); |
Ethan Nicholas | a6ae1f7 | 2017-03-16 09:56:54 -0400 | [diff] [blame] | 1841 | bool result = cg.generateCode(); |
John Stiles | 270cec2 | 2021-02-17 12:59:36 -0500 | [diff] [blame] | 1842 | if (result && program.fConfig->fSettings.fValidateSPIRV) { |
Ethan Nicholas | a6ae1f7 | 2017-03-16 09:56:54 -0400 | [diff] [blame] | 1843 | spvtools::SpirvTools tools(SPV_ENV_VULKAN_1_0); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1844 | const String& data = buffer.str(); |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 1845 | SkASSERT(0 == data.size() % 4); |
Brian Osman | 8d09d4a | 2020-11-24 15:51:06 -0500 | [diff] [blame] | 1846 | String errors; |
| 1847 | auto dumpmsg = [&errors](spv_message_level_t, const char*, const spv_position_t&, |
| 1848 | const char* m) { |
| 1849 | errors.appendf("SPIR-V validation error: %s\n", m); |
Ethan Nicholas | a6ae1f7 | 2017-03-16 09:56:54 -0400 | [diff] [blame] | 1850 | }; |
| 1851 | tools.SetMessageConsumer(dumpmsg); |
Brian Osman | 8d09d4a | 2020-11-24 15:51:06 -0500 | [diff] [blame] | 1852 | |
| 1853 | // Verify that the SPIR-V we produced is valid. At runtime, we will abort() with a message |
| 1854 | // explaining the error. In standalone mode (skslc), we will send the message, plus the |
| 1855 | // entire disassembled SPIR-V (for easier context & debugging) as *our* error message. |
| 1856 | result = tools.Validate((const uint32_t*) data.c_str(), data.size() / 4); |
| 1857 | |
| 1858 | if (!result) { |
| 1859 | #if defined(SKSL_STANDALONE) |
| 1860 | // Convert the string-stream to a SPIR-V disassembly. |
| 1861 | std::string disassembly; |
| 1862 | if (tools.Disassemble((const uint32_t*)data.data(), data.size() / 4, &disassembly)) { |
| 1863 | errors.append(disassembly); |
| 1864 | } |
| 1865 | this->error(-1, errors); |
| 1866 | #else |
| 1867 | SkDEBUGFAILF("%s", errors.c_str()); |
| 1868 | #endif |
| 1869 | } |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1870 | out.write(data.c_str(), data.size()); |
Ethan Nicholas | a6ae1f7 | 2017-03-16 09:56:54 -0400 | [diff] [blame] | 1871 | } |
| 1872 | #else |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 1873 | AutoSource as(this, program.fSource.get()); |
Brian Osman | 8b43dad | 2020-10-09 13:31:42 -0400 | [diff] [blame] | 1874 | SPIRVCodeGenerator cg(fContext.get(), &program, this, &out); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1875 | bool result = cg.generateCode(); |
Ethan Nicholas | a6ae1f7 | 2017-03-16 09:56:54 -0400 | [diff] [blame] | 1876 | #endif |
Ethan Nicholas | ce33f10 | 2016-12-09 17:22:59 -0500 | [diff] [blame] | 1877 | return result; |
| 1878 | } |
| 1879 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 1880 | bool Compiler::toSPIRV(Program& program, String* out) { |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1881 | StringStream buffer; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1882 | bool result = this->toSPIRV(program, buffer); |
| 1883 | if (result) { |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1884 | *out = buffer.str(); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1885 | } |
| 1886 | return result; |
| 1887 | } |
| 1888 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 1889 | bool Compiler::toGLSL(Program& program, OutputStream& out) { |
Leon Scroggins | b66214e | 2021-02-11 17:14:18 -0500 | [diff] [blame] | 1890 | ATRACE_ANDROID_FRAMEWORK("SkSL::Compiler::toGLSL"); |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 1891 | AutoSource as(this, program.fSource.get()); |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 1892 | GLSLCodeGenerator cg(fContext.get(), &program, this, &out); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1893 | bool result = cg.generateCode(); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1894 | return result; |
| 1895 | } |
| 1896 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 1897 | bool Compiler::toGLSL(Program& program, String* out) { |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1898 | StringStream buffer; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1899 | bool result = this->toGLSL(program, buffer); |
| 1900 | if (result) { |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1901 | *out = buffer.str(); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1902 | } |
| 1903 | return result; |
| 1904 | } |
| 1905 | |
Brian Osman | c024391 | 2020-02-19 15:35:26 -0500 | [diff] [blame] | 1906 | bool Compiler::toHLSL(Program& program, String* out) { |
| 1907 | String spirv; |
| 1908 | if (!this->toSPIRV(program, &spirv)) { |
| 1909 | return false; |
| 1910 | } |
| 1911 | |
| 1912 | return SPIRVtoHLSL(spirv, out); |
| 1913 | } |
| 1914 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 1915 | bool Compiler::toMetal(Program& program, OutputStream& out) { |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 1916 | MetalCodeGenerator cg(fContext.get(), &program, this, &out); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1917 | bool result = cg.generateCode(); |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 1918 | return result; |
| 1919 | } |
| 1920 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 1921 | bool Compiler::toMetal(Program& program, String* out) { |
Timothy Liang | b8eeb80 | 2018-07-23 16:46:16 -0400 | [diff] [blame] | 1922 | StringStream buffer; |
| 1923 | bool result = this->toMetal(program, buffer); |
| 1924 | if (result) { |
| 1925 | *out = buffer.str(); |
| 1926 | } |
| 1927 | return result; |
| 1928 | } |
| 1929 | |
Greg Daniel | a28ea67 | 2020-09-25 11:12:56 -0400 | [diff] [blame] | 1930 | #if defined(SKSL_STANDALONE) || GR_TEST_UTILS |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 1931 | bool Compiler::toCPP(Program& program, String name, OutputStream& out) { |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 1932 | AutoSource as(this, program.fSource.get()); |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 1933 | CPPCodeGenerator cg(fContext.get(), &program, this, name, &out); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1934 | bool result = cg.generateCode(); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1935 | return result; |
| 1936 | } |
| 1937 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 1938 | bool Compiler::toH(Program& program, String name, OutputStream& out) { |
Brian Osman | 88cda17 | 2020-10-09 12:05:16 -0400 | [diff] [blame] | 1939 | AutoSource as(this, program.fSource.get()); |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 1940 | HCodeGenerator cg(fContext.get(), &program, this, name, &out); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1941 | bool result = cg.generateCode(); |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 1942 | return result; |
| 1943 | } |
Greg Daniel | a28ea67 | 2020-09-25 11:12:56 -0400 | [diff] [blame] | 1944 | #endif // defined(SKSL_STANDALONE) || GR_TEST_UTILS |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 1945 | |
Ethan Nicholas | 2a479a5 | 2020-08-18 16:29:45 -0400 | [diff] [blame] | 1946 | #endif // defined(SKSL_STANDALONE) || SK_SUPPORT_GPU |
Brian Osman | 2e29ab5 | 2019-09-20 12:19:11 -0400 | [diff] [blame] | 1947 | |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1948 | Position Compiler::position(int offset) { |
Ethan Nicholas | 3c72989 | 2020-12-07 12:47:17 -0500 | [diff] [blame] | 1949 | if (fSource && offset >= 0) { |
| 1950 | int line = 1; |
| 1951 | int column = 1; |
| 1952 | for (int i = 0; i < offset; i++) { |
| 1953 | if ((*fSource)[i] == '\n') { |
| 1954 | ++line; |
| 1955 | column = 1; |
| 1956 | } |
| 1957 | else { |
| 1958 | ++column; |
| 1959 | } |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1960 | } |
Ethan Nicholas | 3c72989 | 2020-12-07 12:47:17 -0500 | [diff] [blame] | 1961 | return Position(line, column); |
| 1962 | } else { |
| 1963 | return Position(-1, -1); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1964 | } |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1965 | } |
| 1966 | |
| 1967 | void Compiler::error(int offset, String msg) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1968 | fErrorCount++; |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1969 | Position pos = this->position(offset); |
John Stiles | 8d3642e | 2021-01-22 09:50:04 -0500 | [diff] [blame] | 1970 | fErrorTextLength.push_back(fErrorText.length()); |
Ethan Nicholas | 3c72989 | 2020-12-07 12:47:17 -0500 | [diff] [blame] | 1971 | fErrorText += "error: " + (pos.fLine >= 1 ? to_string(pos.fLine) + ": " : "") + msg + "\n"; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1972 | } |
| 1973 | |
John Stiles | 8d3642e | 2021-01-22 09:50:04 -0500 | [diff] [blame] | 1974 | void Compiler::setErrorCount(int c) { |
| 1975 | if (c < fErrorCount) { |
| 1976 | fErrorText.resize(fErrorTextLength[c]); |
| 1977 | fErrorTextLength.resize(c); |
| 1978 | fErrorCount = c; |
| 1979 | } |
| 1980 | } |
| 1981 | |
Ethan Nicholas | 9504614 | 2021-01-07 10:57:27 -0500 | [diff] [blame] | 1982 | String Compiler::errorText(bool showCount) { |
| 1983 | if (showCount) { |
| 1984 | this->writeErrorCount(); |
| 1985 | } |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 1986 | fErrorCount = 0; |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1987 | String result = fErrorText; |
Ethan Nicholas | 9504614 | 2021-01-07 10:57:27 -0500 | [diff] [blame] | 1988 | fErrorText = ""; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1989 | return result; |
| 1990 | } |
| 1991 | |
| 1992 | void Compiler::writeErrorCount() { |
| 1993 | if (fErrorCount) { |
| 1994 | fErrorText += to_string(fErrorCount) + " error"; |
| 1995 | if (fErrorCount > 1) { |
| 1996 | fErrorText += "s"; |
| 1997 | } |
| 1998 | fErrorText += "\n"; |
| 1999 | } |
| 2000 | } |
| 2001 | |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 2002 | } // namespace SkSL |