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