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