Skip to content

Commit

Permalink
[P4Testgen] Add a compiler pass to resolve Type_Name in StructExpress…
Browse files Browse the repository at this point in the history
…ions. (#4215)

* Relax constraints for valid types of struct expressions.

* Add a pass which converts struct expressions with Type_Header to header expressions.

* Revert "Relax constraints for valid types of struct expressions."

This reverts commit 9052ad1.
  • Loading branch information
fruffy committed Nov 21, 2023
1 parent 2cb372d commit 85c22c1
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 1 deletion.
1 change: 1 addition & 0 deletions backends/p4tools/common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ set(

compiler/compiler_target.cpp
compiler/convert_hs_index.cpp
compiler/convert_struct_expr.cpp
compiler/convert_varbits.cpp
compiler/midend.cpp
compiler/reachability.cpp
Expand Down
25 changes: 25 additions & 0 deletions backends/p4tools/common/compiler/convert_struct_expr.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include "backends/p4tools/common/compiler/convert_struct_expr.h"

#include "ir/irutils.h"

namespace P4Tools {

const IR::Node *ConvertStructExpr::postorder(IR::StructExpression *structExpr) {
auto structType = structExpr->type;
bool resolved = false;
if (structType->is<IR::Type_Name>()) {
structType = typeMap->getTypeType(structType, true);
resolved = true;
}
if (resolved) {
return new IR::StructExpression(structExpr->srcInfo, structType, structExpr->type,
structExpr->components);
}
return structExpr;
}

ConvertStructExpr::ConvertStructExpr(const P4::TypeMap *typeMap) : typeMap(typeMap) {
setName("ConvertStructExpr");
visitDagOnce = false;
}
} // namespace P4Tools
23 changes: 23 additions & 0 deletions backends/p4tools/common/compiler/convert_struct_expr.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#ifndef BACKENDS_P4TOOLS_COMMON_COMPILER_CONVERT_STRUCT_EXPR_H_
#define BACKENDS_P4TOOLS_COMMON_COMPILER_CONVERT_STRUCT_EXPR_H_

#include "frontends/p4/typeMap.h"
#include "ir/ir.h"
#include "ir/node.h"
#include "ir/visitor.h"

namespace P4Tools {

class ConvertStructExpr : public Transform {
private:
const P4::TypeMap *typeMap;

public:
explicit ConvertStructExpr(const P4::TypeMap *typeMap);

const IR::Node *postorder(IR::StructExpression *expr) override;
};

} // namespace P4Tools

#endif /* BACKENDS_P4TOOLS_COMMON_COMPILER_CONVERT_STRUCT_EXPR_H_ */
4 changes: 3 additions & 1 deletion backends/p4tools/common/compiler/midend.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "backends/p4tools/common/compiler/midend.h"

#include "backends/p4tools/common/compiler/convert_struct_expr.h"
#include "backends/p4tools/common/compiler/convert_varbits.h"
#include "frontends/common/constantFolding.h"
#include "frontends/common/options.h"
Expand Down Expand Up @@ -30,7 +31,6 @@
#include "midend/orderArguments.h"
#include "midend/parserUnroll.h"
#include "midend/removeLeftSlices.h"
#include "midend/removeMiss.h"
#include "midend/removeSelectBooleans.h"
#include "midend/replaceSelectRange.h"
#include "midend/simplifyBitwise.h"
Expand Down Expand Up @@ -162,6 +162,8 @@ void MidEnd::addDefaultPasses() {
new P4::HSIndexSimplifier(&refMap, &typeMap),
// Convert Type_Varbits into a type that contains information about the assigned width.
new ConvertVarbits(),
// Convert any StructExpressions with Type_Header into a HeaderExpression.
new ConvertStructExpr(&typeMap),
// Cast all boolean table keys with a bit<1>.
new P4::CastBooleanTableKeys(),
});
Expand Down

0 comments on commit 85c22c1

Please sign in to comment.