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