Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test for ContantFolding + policy #4421

Merged
merged 2 commits into from
Feb 14, 2024
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: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ set (GTEST_UNITTEST_SOURCES
gtest/call_graph_test.cpp
gtest/complex_bitwise.cpp
gtest/constant_expr_test.cpp
gtest/constant_folding.cpp
gtest/cstring.cpp
gtest/diagnostics.cpp
gtest/dumpjson.cpp
Expand Down
136 changes: 136 additions & 0 deletions test/gtest/constant_folding.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
#include <gtest/gtest.h>

#include "frontends/common/constantFolding.h"
#include "frontends/common/parseInput.h"
#include "frontends/common/resolveReferences/referenceMap.h"
#include "frontends/common/resolveReferences/resolveReferences.h"
#include "helpers.h"
#include "ir/ir.h"
#include "ir/pass_manager.h"
#include "lib/log.h"

using namespace P4;

namespace Test {

struct P4CFrontend : P4CTest {
void addPasses(std::initializer_list<PassManager::VisitorRef> passes) { pm.addPasses(passes); }

const IR::Node *parseAndProcess(std::string program) {
const auto *pgm = P4::parseP4String(program, CompilerOptions::FrontendVersion::P4_16);
EXPECT_TRUE(pgm);
EXPECT_EQ(::errorCount(), 0);
if (!pgm) {
return nullptr;
}
return pgm->apply(pm);
}

PassManager pm;
};

struct P4CConstantFoldingValidation : P4CFrontend {
void createPasses(P4::ConstantFoldingPolicy *policy) {
addPasses({
new P4::ResolveReferences(&refMap),
new P4::ConstantFolding(&refMap, nullptr, policy),
});
}

P4::ReferenceMap refMap;
};

/**
* Filter to prevent processing of fields in the "my_hdr_stack_1_t" struct
*/
class ConstantFoldFilter : public P4::ConstantFoldingPolicy {
public:
const IR::Node *hook(Visitor &v, IR::PathExpression *pe) override {
auto ts = v.findContext<IR::Type_Struct>();
if (ts && ts->name == "my_hdr_stack_1_t") {
return pe;
}
return nullptr;
}
};

const IR::StructField *getStructField(const IR::P4Program *program, cstring struct_name,
cstring field_name) {
for (const auto *d : *program->getDeclarations()) {
const auto *ts = d->to<IR::Type_Struct>();
if (ts && ts->name == struct_name) {
for (const auto *f : ts->fields) {
const auto *sf = f->to<IR::StructField>();
if (sf && sf->name == field_name) {
return sf;
}
}
}
}
return nullptr;
}

const std::string constantFoldFilterCode() {
static const std::string code = P4_SOURCE(R"(
const bit<16> CONST_1 = 3;
const bit<16> CONST_2 = 4;

header my_hdr_t {
bit<16> field;
}

struct my_hdr_stack_1_t {
my_hdr_t[CONST_1] hdr;
}

struct my_hdr_stack_2_t {
my_hdr_t[CONST_2] hdr;
}
)");

return code;
}

// Constant folding without any filter
TEST_F(P4CConstantFoldingValidation, no_filter) {
// Create the passes without any ConstantFoldingPolicy
createPasses(nullptr);

auto *program = parseAndProcess(constantFoldFilterCode())->to<IR::P4Program>();
ASSERT_TRUE(program);

// my_hdr_stack_1_t.hdr should be a Constant
auto *sf_1 = getStructField(program, "my_hdr_stack_1_t", "hdr");
ASSERT_TRUE(sf_1);
auto *ts_1 = sf_1->type->to<IR::Type_Stack>();
ASSERT_TRUE(ts_1->size->is<IR::Constant>());

// my_hdr_stack_2_t.hdr should be a Constant
auto *sf_2 = getStructField(program, "my_hdr_stack_2_t", "hdr");
ASSERT_TRUE(sf_2);
auto *ts_2 = sf_2->type->to<IR::Type_Stack>();
ASSERT_TRUE(ts_2->size->is<IR::Constant>());
}

// Constant folding with filtering active
TEST_F(P4CConstantFoldingValidation, filter) {
// Create the passes with a filter to skip processing of some structs
createPasses(new ConstantFoldFilter());

auto *program = parseAndProcess(constantFoldFilterCode())->to<IR::P4Program>();
ASSERT_TRUE(program);

// my_hdr_stack_1_t.hdr should be a Constant
auto *sf_1 = getStructField(program, "my_hdr_stack_1_t", "hdr");
ASSERT_TRUE(sf_1);
auto *ts_1 = sf_1->type->to<IR::Type_Stack>();
EXPECT_TRUE(ts_1->size->is<IR::PathExpression>());

// my_hdr_stack_2_t.hdr should be a Constant
auto *sf_2 = getStructField(program, "my_hdr_stack_2_t", "hdr");
ASSERT_TRUE(sf_2);
auto *ts_2 = sf_2->type->to<IR::Type_Stack>();
EXPECT_TRUE(ts_2->size->is<IR::Constant>());
}

} // namespace Test
Loading