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