Skip to content

Commit ce64442

Browse files
authored
[P4Testgen] Reenable Tofino passes without actually linking the full Tofino compiler library (p4lang#5320)
Signed-off-by: fruffy <fruffy@nyu.edu>
1 parent 02c4fa2 commit ce64442

7 files changed

Lines changed: 25 additions & 32 deletions

File tree

backends/p4tools/modules/testgen/targets/tofino/CMakeLists.txt

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,6 @@ endif()
4545
# that. Those transitive dependencies turn out to be most of the compiler!
4646
set(
4747
TESTGEN_LIBS
48-
# tofinobackend
49-
# bfn_p4runtime
50-
# p4cgraphs
51-
# We do not use the target library variable here because top-level testgen will not find it.
52-
# boost_system
5348
${TESTGEN_LIBS}
5449
PARENT_SCOPE
5550
)

backends/p4tools/modules/testgen/targets/tofino/target.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,12 @@
2222
#include <cstddef>
2323
#include <map>
2424
#include <string>
25-
#include <utility>
2625
#include <vector>
2726

2827
#include "backends/p4tools/common/lib/namespace_context.h"
2928
#include "backends/p4tools/common/lib/util.h"
29+
// TOOD: We can only use this pass because it is header-only.
30+
#include "backends/tofino/bf-p4c/midend/rewrite_egress_intrinsic_metadata_header.h"
3031
#include "ir/ir.h"
3132
#include "lib/cstring.h"
3233
#include "lib/exceptions.h"
@@ -71,14 +72,14 @@ P4::FrontEnd AbstractTofinoTestgenTarget::mkFrontEnd() const {
7172
MidEnd AbstractTofinoTestgenTarget::mkMidEnd(const CompilerOptions &options) const {
7273
// We need to initialize the device to be able to use Tofino compiler passes
7374
// FIXME: Reenable this?
74-
// Device::init(spec.deviceName);
75+
Device::init(spec.deviceName, {});
7576
auto midEnd = CompilerTarget::mkMidEnd(options);
76-
// auto *refMap = midEnd.getRefMap();
77-
// auto *typeMap = midEnd.getTypeMap();
77+
auto *refMap = midEnd.getRefMap();
78+
auto *typeMap = midEnd.getTypeMap();
7879
midEnd.addPasses({
7980
// Remove all unused fields in the egress intrinsic metadata
8081
// FIXME: Reenable this.
81-
// new BFN::RewriteEgressIntrinsicMetadataHeader(refMap, typeMap),
82+
new BFN::RewriteEgressIntrinsicMetadataHeader(refMap, typeMap),
8283
// Remove trailing '$' in '$valid$' key matches and
8384
// replace subscript operator in header stack indices
8485
// with '$'

backends/p4tools/modules/testgen/targets/tofino/test/Tofino2PTFXfail.cmake

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ p4tools_add_xfail_reason(
9393
p4tools_add_xfail_reason(
9494
"testgen-tofino2-ptf"
9595
"P4Testgen Bug: Unable to find var .* in the symbolic environment"
96-
tna_simple_switch.p4
9796
)
9897

9998
p4tools_add_xfail_reason(

backends/p4tools/modules/testgen/targets/tofino/test/TofinoPTFXfail.cmake

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,6 @@ p4tools_add_xfail_reason(
285285
p4tools_add_xfail_reason(
286286
"testgen-tofino-ptf"
287287
"The validity bit of .* is tainted"
288-
tna_simple_switch.p4
289288
)
290289

291290
p4tools_add_xfail_reason(

backends/p4tools/modules/testgen/targets/tofino/tofino/cmd_stepper.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,8 @@ void TofinoCmdStepper::initializeTargetEnvironment(ExecutionState &nextState) co
158158

159159
std::optional<const Constraint *> TofinoCmdStepper::startParserImpl(
160160
const IR::P4Parser *parser, ExecutionState &nextState) const {
161-
auto programINfo = getProgramInfo();
162-
auto gress = programINfo.getGress(parser);
161+
auto programInfo = getProgramInfo();
162+
auto gress = programInfo.getGress(parser);
163163

164164
const auto *nineBitType = IR::Type_Bits::get(9);
165165
const auto *oneBitType = IR::Type_Bits::get(1);
@@ -187,25 +187,25 @@ std::optional<const Constraint *> TofinoCmdStepper::startParserImpl(
187187
case INGRESS: {
188188
// bypass_egress is not active at the beginning.
189189
const auto &bypassExpr =
190-
programINfo.getParserParamVar(parser, oneBitType, 4, "bypass_egress"_cs);
190+
programInfo.getParserParamVar(parser, oneBitType, 4, "bypass_egress"_cs);
191191
nextState.set(bypassExpr, IR::Constant::get(oneBitType, 0));
192192

193193
// Check whether the parser error was referenced in this particular pipe.
194-
const auto *pipes = programINfo.getPipes();
195-
const auto *decl = pipes->at(programINfo.getPipeIdx(parser)).pipes.at("IngressT"_cs);
194+
const auto *pipes = programInfo.getPipes();
195+
const auto *decl = pipes->at(programInfo.getPipeIdx(parser)).pipes.at("IngressT"_cs);
196196
const auto *p4control = decl->checkedTo<IR::P4Control>();
197197
CheckParserError checkParserError;
198198
p4control->apply(checkParserError);
199199
nextState.setProperty("parserErrReferenced"_cs, checkParserError.hasParserError());
200200
// Tofino implicitly drops packets if they are not set during the ingress or egress
201201
// pipeline.
202202
nextState.set(
203-
programINfo.getParserParamVar(parser, nineBitType, 4, "ucast_egress_port"_cs),
203+
programInfo.getParserParamVar(parser, nineBitType, 4, "ucast_egress_port"_cs),
204204
new IR::UninitializedTaintExpression(nineBitType));
205205
// Initialize parser_err with no error and set the parser error label.
206206
// This label is used by some core externs to control target's parser error.
207207
const auto &parserErrorLabelExpr =
208-
programINfo.getParserParamVar(parser, parserErrorType, 5, "parser_err"_cs);
208+
programInfo.getParserParamVar(parser, parserErrorType, 5, "parser_err"_cs);
209209
nextState.set(parserErrorLabelExpr, IR::Constant::get(parserErrorType, 0));
210210
nextState.setParserErrorLabel(parserErrorLabelExpr);
211211
nextState.setProperty("gress"_cs, static_cast<uint64_t>(gress_t::INGRESS));
@@ -216,12 +216,12 @@ std::optional<const Constraint *> TofinoCmdStepper::startParserImpl(
216216
// Retrieve the egress port associated with this parser.
217217
// Tofino implicitly drops packets if they are not set during the ingress or egress
218218
// pipeline.
219-
nextState.set(programINfo.getParserParamVar(parser, nineBitType, 3, "egress_port"_cs),
219+
nextState.set(programInfo.getParserParamVar(parser, nineBitType, 3, "egress_port"_cs),
220220
new IR::UninitializedTaintExpression(nineBitType));
221221
// Initialize parser_err with no error and set the parser error label.
222222
// This label is used by some core externs to control target's parser error.
223223
const auto &parserErrorLabelExpr =
224-
programINfo.getParserParamVar(parser, parserErrorType, 3, "parser_err"_cs);
224+
programInfo.getParserParamVar(parser, parserErrorType, 4, "parser_err"_cs);
225225
nextState.set(parserErrorLabelExpr, IR::Constant::get(parserErrorType, 0));
226226
nextState.setParserErrorLabel(parserErrorLabelExpr);
227227
nextState.setProperty("gress"_cs, static_cast<uint64_t>(gress_t::EGRESS));
@@ -283,7 +283,7 @@ std::map<Continuation::Exception, Continuation> TofinoCmdStepper::getExceptionHa
283283
case EGRESS: {
284284
result.emplace(Continuation::Exception::PacketTooShort, Continuation::Body({}));
285285
const auto &parserErrorVariable =
286-
programInfo.getParserParamVar(parser, parserErrorType, 3, "parser_err"_cs);
286+
programInfo.getParserParamVar(parser, parserErrorType, 4, "parser_err"_cs);
287287
const auto *noMatchConst = IR::Constant::get(parserErrorVariable->type,
288288
TofinoConstants::PARSER_ERROR_NO_MATCH);
289289
const auto *noMatchAssign = new IR::AssignmentStatement(

backends/p4tools/modules/testgen/targets/tofino/tofino2/cmd_stepper.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ std::optional<const Constraint *> JBayCmdStepper::startParserImpl(const IR::P4Pa
218218
// Initialize parser_err with no error and set the parser error label.
219219
// This label is used by some core externs to control target's parser error.
220220
const auto &parserErrorVariable =
221-
programInfo.getParserParamVar(parser, parserErrorType, 3, "parser_err"_cs);
221+
programInfo.getParserParamVar(parser, parserErrorType, 4, "parser_err"_cs);
222222
nextState.set(parserErrorVariable, IR::Constant::get(parserErrorType, 0));
223223
nextState.setParserErrorLabel(parserErrorVariable);
224224
nextState.setProperty("gress"_cs, static_cast<uint64_t>(gress_t::EGRESS));
@@ -282,7 +282,7 @@ std::map<Continuation::Exception, Continuation> JBayCmdStepper::getExceptionHand
282282
// Models PARSER_ERROR_NO_TCAM parser error.
283283
// Set the parser error label to PARSER_ERROR_NO_MATCH in case of a no-match.
284284
const auto &parserErrorVariable =
285-
programInfo.getParserParamVar(parser, parserErrorType, 5, "parser_err"_cs);
285+
programInfo.getParserParamVar(parser, parserErrorType, 4, "parser_err"_cs);
286286
const auto *noMatchConst =
287287
IR::Constant::get(parserErrorVariable->type, JBayConstants::PARSER_ERROR_NO_MATCH);
288288
const auto *noMatchAssign = new IR::AssignmentStatement(

backends/tofino/bf-p4c/midend/rewrite_egress_intrinsic_metadata_header.h

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,18 @@
2020
#define BACKENDS_TOFINO_BF_P4C_MIDEND_REWRITE_EGRESS_INTRINSIC_METADATA_HEADER_H_
2121

2222
#include "backends/tofino/bf-p4c/specs/device.h"
23-
24-
namespace P4 {
25-
class TypeMap;
26-
class ReferenceMap;
27-
} // namespace P4
23+
#include "frontends/p4/typeChecking/typeChecker.h"
24+
#include "ir/ir.h"
25+
#include "ir/pass_manager.h"
26+
#include "ir/visitor.h"
2827

2928
namespace BFN {
3029

3130
/**
3231
* \ingroup midend
3332
* \brief Pass that updates egress intrinsic metadata.
3433
*/
35-
class RewriteEgressIntrinsicMetadataHeader : public PassManager {
34+
class RewriteEgressIntrinsicMetadataHeader : public P4::PassManager {
3635
struct CollectUsedFields : public Inspector {
3736
std::set<cstring> used_fields;
3837
const IR::Type_Header *eg_intr_md = nullptr;
@@ -57,7 +56,7 @@ class RewriteEgressIntrinsicMetadataHeader : public PassManager {
5756
}
5857
};
5958

60-
struct RewriteHeader : public Transform {
59+
struct RewriteHeader : public P4::Transform {
6160
const CollectUsedFields &used_fields;
6261
P4::TypeMap *typeMap;
6362

@@ -155,7 +154,7 @@ class RewriteEgressIntrinsicMetadataHeader : public PassManager {
155154
collectUsedFields,
156155
new RewriteHeader(*collectUsedFields, typeMap),
157156
new P4::ClearTypeMap(typeMap),
158-
new BFN::TypeChecking(refMap, typeMap, true),
157+
new TypeChecking(refMap, typeMap, true),
159158
});
160159
}
161160
};

0 commit comments

Comments
 (0)