diff --git a/src/sksl/SkSLIRGenerator.cpp b/src/sksl/SkSLIRGenerator.cpp index d9ce249eb3..3bbb091d56 100644 --- a/src/sksl/SkSLIRGenerator.cpp +++ b/src/sksl/SkSLIRGenerator.cpp @@ -498,42 +498,6 @@ std::unique_ptr IRGenerator::convertModifiersDeclaration(c return std::make_unique(modifiers); } -std::unique_ptr IRGenerator::convertIf(const ASTNode& n) { - SkASSERT(n.fKind == ASTNode::Kind::kIf); - auto iter = n.begin(); - std::unique_ptr test = this->coerce(this->convertExpression(*(iter++)), - *fContext.fBool_Type); - if (!test) { - return nullptr; - } - std::unique_ptr ifTrue = this->convertStatement(*(iter++)); - if (!ifTrue) { - return nullptr; - } - std::unique_ptr ifFalse; - if (iter != n.end()) { - ifFalse = this->convertStatement(*(iter++)); - if (!ifFalse) { - return nullptr; - } - } - if (test->fKind == Expression::kBoolLiteral_Kind) { - // static boolean value, fold down to a single branch - if (test->as().fValue) { - return ifTrue; - } else if (ifFalse) { - return ifFalse; - } else { - // False & no else clause. Not an error, so don't return null! - std::vector> empty; - return std::unique_ptr(new Block(n.fOffset, std::move(empty), - fSymbolTable)); - } - } - return std::unique_ptr(new IfStatement(n.fOffset, n.getBool(), std::move(test), - std::move(ifTrue), std::move(ifFalse))); -} - static void ensure_scoped_blocks(Statement* stmt) { // No changes necessary if this statement isn't actually a block. if (stmt->fKind != Statement::kBlock_Kind) { @@ -570,6 +534,43 @@ static void ensure_scoped_blocks(Statement* stmt) { } } +std::unique_ptr IRGenerator::convertIf(const ASTNode& n) { + SkASSERT(n.fKind == ASTNode::Kind::kIf); + auto iter = n.begin(); + std::unique_ptr test = this->coerce(this->convertExpression(*(iter++)), + *fContext.fBool_Type); + if (!test) { + return nullptr; + } + std::unique_ptr ifTrue = this->convertStatement(*(iter++)); + if (!ifTrue) { + return nullptr; + } + ensure_scoped_blocks(ifTrue.get()); + std::unique_ptr ifFalse; + if (iter != n.end()) { + ifFalse = this->convertStatement(*(iter++)); + if (!ifFalse) { + return nullptr; + } + ensure_scoped_blocks(ifFalse.get()); + } + if (test->fKind == Expression::kBoolLiteral_Kind) { + // static boolean value, fold down to a single branch + if (test->as().fValue) { + return ifTrue; + } else if (ifFalse) { + return ifFalse; + } else { + // False & no else clause. Not an error, so don't return null! + std::vector> empty; + return std::make_unique(n.fOffset, std::move(empty), fSymbolTable); + } + } + return std::make_unique(n.fOffset, n.getBool(), + std::move(test), std::move(ifTrue), std::move(ifFalse)); +} + std::unique_ptr IRGenerator::convertFor(const ASTNode& f) { SkASSERT(f.fKind == ASTNode::Kind::kFor); AutoLoopLevel level(this); diff --git a/tests/SkSLFPTest.cpp b/tests/SkSLFPTest.cpp index bc74146efe..9f49561706 100644 --- a/tests/SkSLFPTest.cpp +++ b/tests/SkSLFPTest.cpp @@ -1432,8 +1432,6 @@ if (_0_ifTest) %s = half4(1.0); else %s = half4(0.5); } DEF_TEST(SkSLFPInlinedIfBodyMustBeInAScope, r) { - // NOTE: this test exposes a bug with the inliner. The inlined function body is not wrapped in a - // scope, so the inlined code is emitted outside of the if-body. test(r, *SkSL::ShaderCapsFactory::Default(), R"__SkSL__( @@ -1452,13 +1450,14 @@ DEF_TEST(SkSLFPInlinedIfBodyMustBeInAScope, r) { /*expectedCPP=*/{ R"__Cpp__(fragBuilder->codeAppendf( R"SkSL(half4 c = %s; -if (c.x >= 0.5) half4 _0_ifBody; -{ - _0_ifBody = %s + half4(0.125); +if (c.x >= 0.5) { + half4 _0_ifBody; + { + _0_ifBody = %s + half4(0.125); + } + + c = _0_ifBody; } - -c = _0_ifBody; - %s = c; )SkSL" , args.fUniformHandler->getUniformCStr(colorVar), args.fUniformHandler->getUniformCStr(colorVar), args.fUniformHandler->getUniformCStr(colorVar)); @@ -1466,8 +1465,6 @@ c = _0_ifBody; } DEF_TEST(SkSLFPInlinedElseBodyMustBeInAScope, r) { - // NOTE: this test exposes a bug with the inliner. The inlined function body is not wrapped in a - // scope, so the inlined code is emitted outside of the else-body. test(r, *SkSL::ShaderCapsFactory::Default(), R"__SkSL__( @@ -1489,13 +1486,14 @@ DEF_TEST(SkSLFPInlinedElseBodyMustBeInAScope, r) { R"__Cpp__(fragBuilder->codeAppendf( R"SkSL(half4 c = %s; if (c.x >= 0.5) { -} else half4 _0_elseBody; -{ - _0_elseBody = %s + half4(0.125); +} else { + half4 _0_elseBody; + { + _0_elseBody = %s + half4(0.125); + } + + c = _0_elseBody; } - -c = _0_elseBody; - %s = c; )SkSL" , args.fUniformHandler->getUniformCStr(colorVar), args.fUniformHandler->getUniformCStr(colorVar), args.fUniformHandler->getUniformCStr(colorVar));