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