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