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