Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion eval/compiler/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,6 @@ cc_test(
"//base:ast",
"//common:expr",
"//common:value",
"//eval/eval:const_value_step",
"//eval/eval:create_list_step",
"//eval/eval:create_map_step",
"//eval/eval:evaluator_core",
Expand Down
4 changes: 1 addition & 3 deletions eval/compiler/constant_folding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ using ::cel::builtin::kOr;
using ::cel::builtin::kTernary;
using ::cel::runtime_internal::ConvertConstant;
using ::google::api::expr::runtime::CreateConstValueDirectStep;
using ::google::api::expr::runtime::CreateConstValueStep;
using ::google::api::expr::runtime::EvaluationListener;
using ::google::api::expr::runtime::ExecutionFrame;
using ::google::api::expr::runtime::ExecutionPath;
Expand Down Expand Up @@ -243,8 +242,7 @@ absl::Status ConstantFoldingExtension::OnPostVisit(PlannerContext& context,

// Otherwise make a stack machine plan.
ExecutionPath new_plan;
new_plan.push_back(ExpressionStep::MakeGenericStep(
CreateConstValueStep(std::move(value)), node.id()));
new_plan.push_back(ExpressionStep::MakeConstant(value, node.id()));

return context.ReplaceSubplan(node, std::move(new_plan));
}
Expand Down
98 changes: 48 additions & 50 deletions eval/compiler/constant_folding_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
#include "common/value.h"
#include "eval/compiler/flat_expr_builder_extensions.h"
#include "eval/compiler/resolver.h"
#include "eval/eval/const_value_step.h"
#include "eval/eval/create_list_step.h"
#include "eval/eval/create_map_step.h"
#include "eval/eval/evaluator_core.h"
Expand Down Expand Up @@ -58,7 +57,6 @@ using ::cel::runtime_internal::IssueCollector;
using ::cel::runtime_internal::NewTestingRuntimeEnv;
using ::cel::expr::ParsedExpr;
using ::google::api::expr::parser::Parse;
using ::google::api::expr::runtime::CreateConstValueStep;
using ::google::api::expr::runtime::CreateCreateListStep;
using ::google::api::expr::runtime::CreateCreateStructStepForMap;
using ::google::api::expr::runtime::ExecutionPath;
Expand Down Expand Up @@ -116,25 +114,25 @@ TEST_F(UpdatedConstantFoldingTest, SkipsTernary) {
program_builder.EnterSubexpression(&call);
// condition
program_builder.EnterSubexpression(&condition);
program_builder.AddStep(ExpressionStep::MakeGenericStep(
CreateConstValueStep(cel::BoolValue(true)), condition.id()));
program_builder.AddStep(
ExpressionStep::MakeConstant(cel::BoolValue(true), condition.id()));
program_builder.ExitSubexpression(&condition);

// true
program_builder.EnterSubexpression(&true_branch);
program_builder.AddStep(ExpressionStep::MakeGenericStep(
CreateConstValueStep(cel::BoolValue(true)), true_branch.id()));
program_builder.AddStep(
ExpressionStep::MakeConstant(cel::BoolValue(true), true_branch.id()));
program_builder.ExitSubexpression(&true_branch);

// false
program_builder.EnterSubexpression(&false_branch);
program_builder.AddStep(ExpressionStep::MakeGenericStep(
CreateConstValueStep(cel::BoolValue(true)), false_branch.id()));
program_builder.AddStep(
ExpressionStep::MakeConstant(cel::BoolValue(true), false_branch.id()));
program_builder.ExitSubexpression(&false_branch);

// ternary.
program_builder.AddStep(ExpressionStep::MakeGenericStep(
CreateConstValueStep(cel::NullValue()), call.id()));
program_builder.AddStep(
ExpressionStep::MakeConstant(cel::NullValue(), call.id()));
program_builder.ExitSubexpression(&call);

std::shared_ptr<google::protobuf::Arena> arena;
Expand Down Expand Up @@ -179,20 +177,20 @@ TEST_F(UpdatedConstantFoldingTest, SkipsOr) {

// left
program_builder.EnterSubexpression(&left_condition);
program_builder.AddStep(ExpressionStep::MakeGenericStep(
CreateConstValueStep(cel::BoolValue(false)), left_condition.id()));
program_builder.AddStep(
ExpressionStep::MakeConstant(cel::BoolValue(false), left_condition.id()));
program_builder.ExitSubexpression(&left_condition);

// right
program_builder.EnterSubexpression(&right_condition);
program_builder.AddStep(ExpressionStep::MakeGenericStep(
CreateConstValueStep(cel::BoolValue(true)), right_condition.id()));
program_builder.AddStep(
ExpressionStep::MakeConstant(cel::BoolValue(true), right_condition.id()));
program_builder.ExitSubexpression(&right_condition);

// op
// Just a placeholder.
program_builder.AddStep(ExpressionStep::MakeGenericStep(
CreateConstValueStep(cel::NullValue()), call.id()));
program_builder.AddStep(
ExpressionStep::MakeConstant(cel::NullValue(), call.id()));
program_builder.ExitSubexpression(&call);

std::shared_ptr<google::protobuf::Arena> arena;
Expand Down Expand Up @@ -234,20 +232,20 @@ TEST_F(UpdatedConstantFoldingTest, SkipsAnd) {

// left
program_builder.EnterSubexpression(&left_condition);
program_builder.AddStep(ExpressionStep::MakeGenericStep(
CreateConstValueStep(cel::BoolValue(true)), left_condition.id()));
program_builder.AddStep(
ExpressionStep::MakeConstant(cel::BoolValue(true), left_condition.id()));
program_builder.ExitSubexpression(&left_condition);

// right
program_builder.EnterSubexpression(&right_condition);
program_builder.AddStep(ExpressionStep::MakeGenericStep(
CreateConstValueStep(cel::BoolValue(false)), right_condition.id()));
program_builder.AddStep(ExpressionStep::MakeConstant(cel::BoolValue(false),
right_condition.id()));
program_builder.ExitSubexpression(&right_condition);

// op
// Just a placeholder.
program_builder.AddStep(ExpressionStep::MakeGenericStep(
CreateConstValueStep(cel::NullValue()), call.id()));
program_builder.AddStep(
ExpressionStep::MakeConstant(cel::NullValue(), call.id()));
program_builder.ExitSubexpression(&call);

std::shared_ptr<google::protobuf::Arena> arena;
Expand Down Expand Up @@ -289,14 +287,14 @@ TEST_F(UpdatedConstantFoldingTest, CreatesList) {

// elem one
program_builder.EnterSubexpression(&elem_one);
program_builder.AddStep(ExpressionStep::MakeGenericStep(
CreateConstValueStep(cel::IntValue(1L)), elem_one.id()));
program_builder.AddStep(
ExpressionStep::MakeConstant(cel::IntValue(1L), elem_one.id()));
program_builder.ExitSubexpression(&elem_one);

// elem two
program_builder.EnterSubexpression(&elem_two);
program_builder.AddStep(ExpressionStep::MakeGenericStep(
CreateConstValueStep(cel::IntValue(2L)), elem_two.id()));
program_builder.AddStep(
ExpressionStep::MakeConstant(cel::IntValue(2L), elem_two.id()));
program_builder.ExitSubexpression(&elem_two);

// createlist
Expand Down Expand Up @@ -349,32 +347,32 @@ TEST_F(UpdatedConstantFoldingTest, CreatesLargeList) {

// 0
ASSERT_TRUE(program_builder.EnterSubexpression(&elem0) != nullptr);
program_builder.AddStep(ExpressionStep::MakeGenericStep(
CreateConstValueStep(cel::IntValue(1L)), elem0.id()));
program_builder.AddStep(
ExpressionStep::MakeConstant(cel::IntValue(1L), elem0.id()));
program_builder.ExitSubexpression(&elem0);

// 1
ASSERT_TRUE(program_builder.EnterSubexpression(&elem1));
program_builder.AddStep(ExpressionStep::MakeGenericStep(
CreateConstValueStep(cel::IntValue(2L)), elem1.id()));
program_builder.AddStep(
ExpressionStep::MakeConstant(cel::IntValue(2L), elem1.id()));
program_builder.ExitSubexpression(&elem1);

// 2
ASSERT_TRUE(program_builder.EnterSubexpression(&elem2) != nullptr);
program_builder.AddStep(ExpressionStep::MakeGenericStep(
CreateConstValueStep(cel::IntValue(3L)), elem2.id()));
program_builder.AddStep(
ExpressionStep::MakeConstant(cel::IntValue(3L), elem2.id()));
program_builder.ExitSubexpression(&elem2);

// 3
ASSERT_TRUE(program_builder.EnterSubexpression(&elem3) != nullptr);
program_builder.AddStep(ExpressionStep::MakeGenericStep(
CreateConstValueStep(cel::IntValue(4L)), elem3.id()));
program_builder.AddStep(
ExpressionStep::MakeConstant(cel::IntValue(4L), elem3.id()));
program_builder.ExitSubexpression(&elem3);

// 4
ASSERT_TRUE(program_builder.EnterSubexpression(&elem4) != nullptr);
program_builder.AddStep(ExpressionStep::MakeGenericStep(
CreateConstValueStep(cel::IntValue(5L)), elem4.id()));
program_builder.AddStep(
ExpressionStep::MakeConstant(cel::IntValue(5L), elem4.id()));
program_builder.ExitSubexpression(&elem4);

// createlist
Expand Down Expand Up @@ -428,14 +426,14 @@ TEST_F(UpdatedConstantFoldingTest, CreatesMap) {

// key
program_builder.EnterSubexpression(&key);
program_builder.AddStep(ExpressionStep::MakeGenericStep(
CreateConstValueStep(cel::IntValue(1L)), key.id()));
program_builder.AddStep(
ExpressionStep::MakeConstant(cel::IntValue(1L), key.id()));
program_builder.ExitSubexpression(&key);

// value
program_builder.EnterSubexpression(&value);
program_builder.AddStep(ExpressionStep::MakeGenericStep(
CreateConstValueStep(cel::IntValue(2L)), value.id()));
program_builder.AddStep(
ExpressionStep::MakeConstant(cel::IntValue(2L), value.id()));
program_builder.ExitSubexpression(&value);

// create map
Expand Down Expand Up @@ -484,14 +482,14 @@ TEST_F(UpdatedConstantFoldingTest, CreatesInvalidMap) {

// key
program_builder.EnterSubexpression(&key);
program_builder.AddStep(ExpressionStep::MakeGenericStep(
CreateConstValueStep(cel::DoubleValue(1.0)), key.id()));
program_builder.AddStep(
ExpressionStep::MakeConstant(cel::DoubleValue(1.0), key.id()));
program_builder.ExitSubexpression(&key);

// value
program_builder.EnterSubexpression(&value);
program_builder.AddStep(ExpressionStep::MakeGenericStep(
CreateConstValueStep(cel::IntValue(2L)), value.id()));
program_builder.AddStep(
ExpressionStep::MakeConstant(cel::IntValue(2L), value.id()));
program_builder.ExitSubexpression(&value);

// create map
Expand Down Expand Up @@ -539,20 +537,20 @@ TEST_F(UpdatedConstantFoldingTest, ErrorsOnUnexpectedOrder) {
program_builder.EnterSubexpression(&call);
// left
program_builder.EnterSubexpression(&left_condition);
program_builder.AddStep(ExpressionStep::MakeGenericStep(
CreateConstValueStep(cel::BoolValue(true)), left_condition.id()));
program_builder.AddStep(
ExpressionStep::MakeConstant(cel::BoolValue(true), left_condition.id()));
program_builder.ExitSubexpression(&left_condition);

// right
program_builder.EnterSubexpression(&right_condition);
program_builder.AddStep(ExpressionStep::MakeGenericStep(
CreateConstValueStep(cel::BoolValue(false)), right_condition.id()));
program_builder.AddStep(ExpressionStep::MakeConstant(cel::BoolValue(false),
right_condition.id()));
program_builder.ExitSubexpression(&right_condition);

// op
// Just a placeholder.
program_builder.AddStep(ExpressionStep::MakeGenericStep(
CreateConstValueStep(cel::NullValue()), call.id()));
program_builder.AddStep(
ExpressionStep::MakeConstant(cel::NullValue(), call.id()));
program_builder.ExitSubexpression(&call);

std::shared_ptr<google::protobuf::Arena> arena;
Expand Down
4 changes: 2 additions & 2 deletions eval/compiler/flat_expr_builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -730,8 +730,8 @@ class FlatExprVisitor : public cel::AstVisitor {
return;
}

AddStep(CreateConstValueStep(std::move(converted_value).value()),
expr.id());
AddStep(ExpressionStep::MakeConstant(std::move(converted_value).value(),
expr.id()));
}

struct SlotLookupResult {
Expand Down
29 changes: 20 additions & 9 deletions eval/compiler/flat_expr_builder_extensions_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,17 @@ using ::testing::ElementsAre;
using ::testing::IsEmpty;
using ::testing::Optional;

class TestStepLogic : public ExpressionStepLogic {
public:
absl::Status Evaluate(ExecutionFrame* frame) const override {
return absl::OkStatus();
}
};

std::unique_ptr<ExpressionStepLogic> MakeTestStepLogic() {
return std::make_unique<TestStepLogic>();
}

using Subexpression = ProgramBuilder::Subexpression;

class PlannerContextTest : public testing::Test {
Expand Down Expand Up @@ -94,9 +105,9 @@ struct SimpleTreeSteps {
absl::StatusOr<SimpleTreeSteps> InitSimpleTree(
const Expr& a, const Expr& b, const Expr& c,
ProgramBuilder& program_builder) {
auto a_step = CreateConstValueStep(cel::NullValue());
auto b_step = CreateConstValueStep(cel::NullValue());
auto c_step = CreateConstValueStep(cel::NullValue());
auto a_step = MakeTestStepLogic();
auto b_step = MakeTestStepLogic();
auto c_step = MakeTestStepLogic();

SimpleTreeSteps result{a_step.get(), b_step.get(), c_step.get()};

Expand Down Expand Up @@ -163,7 +174,7 @@ TEST_F(PlannerContextTest, ReplacePlan) {

ExecutionPath new_a;

auto new_a_step = CreateConstValueStep(cel::NullValue());
auto new_a_step = MakeTestStepLogic();
const ExpressionStepLogic* new_a_step_ptr = new_a_step.get();
new_a.push_back(ExpressionStep::MakeGenericStep(std::move(new_a_step), -1));

Expand Down Expand Up @@ -253,10 +264,10 @@ TEST_F(PlannerContextTest, ReplacePlanUpdatesSibling) {

ExecutionPath new_b;

auto b1_step = CreateConstValueStep(cel::NullValue());
auto b1_step = MakeTestStepLogic();
const ExpressionStepLogic* b1_step_ptr = b1_step.get();
new_b.push_back(ExpressionStep::MakeGenericStep(std::move(b1_step), -1));
auto b2_step = CreateConstValueStep(cel::NullValue());
auto b2_step = MakeTestStepLogic();
const ExpressionStepLogic* b2_step_ptr = b2_step.get();
new_b.push_back(ExpressionStep::MakeGenericStep(std::move(b2_step), -1));

Expand Down Expand Up @@ -302,7 +313,7 @@ TEST_F(PlannerContextTest, AddSubplanStep) {
ASSERT_OK_AND_ASSIGN(auto plan_steps,
InitSimpleTree(a, b, c, program_builder));

auto b2_step = CreateConstValueStep(cel::NullValue());
auto b2_step = MakeTestStepLogic();

const ExpressionStepLogic* b2_step_ptr = b2_step.get();

Expand Down Expand Up @@ -331,7 +342,7 @@ TEST_F(PlannerContextTest, AddSubplanStepFailsOnUnknownNode) {

ASSERT_THAT(InitSimpleTree(a, b, c, program_builder).status(), IsOk());

auto b2_step = CreateConstValueStep(cel::NullValue());
auto b2_step = MakeTestStepLogic();

std::shared_ptr<google::protobuf::Arena> arena;
PlannerContext context(env_, resolver_, options_,
Expand Down Expand Up @@ -478,7 +489,7 @@ TEST_F(ProgramBuilderTest, ExtractWorks) {
program_builder.EnterSubexpression(&b);
program_builder.ExitSubexpression(&b);

auto a_step = CreateConstValueStep(cel::NullValue());
auto a_step = MakeTestStepLogic();
program_builder.AddStep(
ExpressionStep::MakeGenericStep(std::move(a_step), -1));
program_builder.EnterSubexpression(&c);
Expand Down
11 changes: 5 additions & 6 deletions eval/compiler/regex_precompilation_optimization.cc
Original file line number Diff line number Diff line change
Expand Up @@ -194,12 +194,11 @@ class RegexPrecompilationOptimization : public ProgramOptimizer {
} else {
// otherwise stack-machine program.
ExecutionPathView re_plan = context.GetSubplan(re_expr);
if (re_plan.size() == 1 && re_plan[0].IsGenericStep() &&
re_plan[0].GetGenericStep()->GetNativeTypeId() ==
NativeTypeId::For<CompilerConstantStep>()) {
constant =
down_cast<const CompilerConstantStep*>(re_plan[0].GetGenericStep())
->value();
if (re_plan.size() == 1) {
cel::Value val;
if (GetIfConstant(re_plan[0], val)) {
constant = std::move(val);
}
}
}

Expand Down
Loading
Loading