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