Skip to content

Commit

Permalink
Review comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
fruffy committed Nov 22, 2023
1 parent 8f349d0 commit a73efad
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 34 deletions.
23 changes: 13 additions & 10 deletions backends/p4tools/common/lib/gen_eq.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
#include "ir/vector.h"
#include "lib/exceptions.h"

namespace P4Tools {
namespace P4Tools::GenEq {

const IR::Expression *GenEq::resolveSingletonList(const IR::Expression *expr) {
/// Recursively resolve lists of size 1 by returning the expression contained within.
const IR::Expression *resolveSingletonList(const IR::Expression *expr) {
if (const auto *listExpr = expr->to<IR::BaseListExpression>()) {
if (listExpr->size() == 1) {
return resolveSingletonList(listExpr->components.at(0));
Expand All @@ -22,8 +23,9 @@ const IR::Expression *GenEq::resolveSingletonList(const IR::Expression *expr) {
return expr;
}

const IR::Expression *GenEq::equateListTypes(const IR::Expression *left,
const IR::Expression *right) {
/// Flatten and compare two lists.
/// Members of struct expressions are ordered and compared based on the underlying type.
const IR::Expression *equateListTypes(const IR::Expression *left, const IR::Expression *right) {
std::vector<const IR::Expression *> leftElems = IR::flattenListOrStructExpression(left);
std::vector<const IR::Expression *> rightElems = IR::flattenListOrStructExpression(right);

Expand All @@ -48,7 +50,12 @@ const IR::Expression *GenEq::equateListTypes(const IR::Expression *left,
return result;
}

const IR::Expression *GenEq::equate(const IR::Expression *left, const IR::Expression *right) {
/// Construct an equality expression.
const IR::Equ *mkEq(const IR::Expression *e1, const IR::Expression *e2) {
return new IR::Equ(IR::Type::Boolean::get(), e1, e2);
}

const IR::Expression *equate(const IR::Expression *left, const IR::Expression *right) {
// First, recursively unroll any singleton elements.
left = resolveSingletonList(left);
right = resolveSingletonList(right);
Expand Down Expand Up @@ -83,8 +90,4 @@ const IR::Expression *GenEq::equate(const IR::Expression *left, const IR::Expres
return mkEq(left, right);
}

const IR::Equ *GenEq::mkEq(const IR::Expression *e1, const IR::Expression *e2) {
return new IR::Equ(IR::Type::Boolean::get(), e1, e2);
}

} // namespace P4Tools
} // namespace P4Tools::GenEq
26 changes: 5 additions & 21 deletions backends/p4tools/common/lib/gen_eq.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,14 @@

#include "ir/ir.h"

namespace P4Tools {
namespace P4Tools::GenEq {

/// Generates an semantic equality on two input expressions, recursing into lists and structs.
/// Generates a semantic equality on two input expressions, recursing into lists and structs.
/// This supports fuzzy matching on singleton lists: singleton lists are considered the same as
/// their singleton elements. This is implemented by eagerly recursing into singleton lists before
/// attempting to generate the equality.
class GenEq {
public:
static const IR::Expression *equate(const IR::Expression *left, const IR::Expression *right);
/// attempting to generate the equality. The expression types need to be semantically compatible.
const IR::Expression *equate(const IR::Expression *left, const IR::Expression *right);

private:
/// Recursively resolve lists of size 1 by returning the expression contained within.
static const IR::Expression *resolveSingletonList(const IR::Expression *expr);

/// Flatten and compare two lists.
/// Important, this equation assumes that struct expressions have been ordered.
/// This calculation does not match the names of the struct expressions.
static const IR::Expression *equateListTypes(const IR::Expression *left,
const IR::Expression *right);

/// Convenience method for producing a typed Eq node on the given expressions.
static const IR::Equ *mkEq(const IR::Expression *e1, const IR::Expression *e2);
};

} // namespace P4Tools
} // namespace P4Tools::GenEq

#endif /* BACKENDS_P4TOOLS_COMMON_LIB_GEN_EQ_H_ */
5 changes: 2 additions & 3 deletions ir/irutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,9 @@ const IR::Expression *getDefaultValue(const IR::Type *type, const Util::SourceIn
std::vector<const Expression *> flattenStructExpression(const StructExpression *structExpr) {
std::vector<const Expression *> exprList;
// Ensure that the underlying type is a Type_StructLike.
// TODO: How do fail gracefully if we get a Type_Name?
const auto *structType = structExpr->type->to<IR::Type_StructLike>();
BUG_CHECK(structType != nullptr, "%1%: expected a struct type, received %2%", structExpr->type,
structExpr->node_type_name());
BUG_CHECK(structType != nullptr, "%1%: expected a struct-like type, received %2%",
structExpr->type, structExpr->node_type_name());

// We use the underlying struct type, which will gives us the right field ordering.
for (const auto *typeField : structType->fields) {
Expand Down

0 comments on commit a73efad

Please sign in to comment.