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