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