From ffba524ff800628fb550ba555157f3b2cde1d37e Mon Sep 17 00:00:00 2001 From: John Stiles Date: Fri, 7 May 2021 10:50:22 -0400 Subject: [PATCH] Add variation of GetConstantValueForVariable which handles ownership. The new function `MakeConstantValueForVariable` behaves the same, except it takes unique_ptrs as inputs and returns a unique_ptr as output. We already had this behavior coded up in two places and I realized I could use this functionality in other spots as well. Change-Id: I2f0c43e8555a3c89bf9a1eef7030b422355ec2a3 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/405680 Auto-Submit: John Stiles Commit-Queue: Ethan Nicholas Reviewed-by: Ethan Nicholas --- src/sksl/SkSLConstantFolder.cpp | 9 +++++++++ src/sksl/SkSLConstantFolder.h | 7 +++++++ src/sksl/ir/SkSLConstructorCompound.cpp | 5 +---- src/sksl/ir/SkSLConstructorSplat.cpp | 5 +---- 4 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/sksl/SkSLConstantFolder.cpp b/src/sksl/SkSLConstantFolder.cpp index 6573d3f402..83d4ebb50f 100644 --- a/src/sksl/SkSLConstantFolder.cpp +++ b/src/sksl/SkSLConstantFolder.cpp @@ -233,6 +233,15 @@ const Expression* ConstantFolder::GetConstantValueForVariable(const Expression& return &inExpr; } +std::unique_ptr ConstantFolder::MakeConstantValueForVariable( + std::unique_ptr expr) { + const Expression* constantExpr = GetConstantValueForVariable(*expr); + if (constantExpr != expr.get()) { + expr = constantExpr->clone(); + } + return expr; +} + static std::unique_ptr simplify_no_op_arithmetic(const Context& context, const Expression& left, Operator op, diff --git a/src/sksl/SkSLConstantFolder.h b/src/sksl/SkSLConstantFolder.h index d82f3a3404..992030a7fe 100644 --- a/src/sksl/SkSLConstantFolder.h +++ b/src/sksl/SkSLConstantFolder.h @@ -43,6 +43,13 @@ public: */ static const Expression* GetConstantValueForVariable(const Expression& value); + /** + * If the expression is a const variable with a known compile-time-constant value, returns a + * clone of that value. If not, returns the original expression as-is. + */ + static std::unique_ptr MakeConstantValueForVariable( + std::unique_ptr expr); + /** * Reports an error and returns true if op is a division / mod operator and right is zero or * contains a zero element. diff --git a/src/sksl/ir/SkSLConstructorCompound.cpp b/src/sksl/ir/SkSLConstructorCompound.cpp index 834c9d3760..0560044148 100644 --- a/src/sksl/ir/SkSLConstructorCompound.cpp +++ b/src/sksl/ir/SkSLConstructorCompound.cpp @@ -76,10 +76,7 @@ std::unique_ptr ConstructorCompound::Make(const Context& context, // Replace constant variables with their corresponding values, so `float2(one, two)` can // compile down to `float2(1.0, 2.0)` (the latter is a compile-time constant). for (std::unique_ptr& arg : args) { - const Expression* value = ConstantFolder::GetConstantValueForVariable(*arg); - if (value != arg.get()) { - arg = value->clone(); - } + arg = ConstantFolder::MakeConstantValueForVariable(std::move(arg)); } } diff --git a/src/sksl/ir/SkSLConstructorSplat.cpp b/src/sksl/ir/SkSLConstructorSplat.cpp index 669dcd1b34..41451c140b 100644 --- a/src/sksl/ir/SkSLConstructorSplat.cpp +++ b/src/sksl/ir/SkSLConstructorSplat.cpp @@ -25,10 +25,7 @@ std::unique_ptr ConstructorSplat::Make(const Context& context, if (context.fConfig->fSettings.fOptimize) { // Replace constant variables with their corresponding values, so `float3(five)` can // compile down to `float3(5.0)` (the latter is a compile-time constant). - const Expression* value = ConstantFolder::GetConstantValueForVariable(*arg); - if (value != arg.get()) { - arg = value->clone(); - } + arg = ConstantFolder::MakeConstantValueForVariable(std::move(arg)); } SkASSERT(type.isVector());