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