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