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