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