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