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