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