Skip to content

Commit

Permalink
Rethink the optional implementation. Add a ternary match which can be…
Browse files Browse the repository at this point in the history
… either a wildcard or an exact match.
  • Loading branch information
fruffy committed Jun 22, 2023
1 parent 179b2e1 commit 0d1ad79
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions backends/p4tools/modules/testgen/targets/bmv2/table_stepper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,29 @@ const IR::Expression *Bmv2V1ModelTableStepper::computeTargetMatchType(

// TODO: We consider optional match types to be a no-op, but we could make them exact matches.
if (keyProperties.matchType == BMv2Constants::MATCH_KIND_OPT) {
// We can recover from taint by simply not adding the optional match.
// Create a new symbolic variable that corresponds to the key expression.
cstring keyName = properties.tableName + "_key_" + keyProperties.name;
const auto *ctrlPlaneKey = ToolsVariables::getSymbolicVariable(keyExpr->type, keyName);
// We can recover from taint by simply not adding the optional match.
// Create a new symbolic variable that corresponds to the key expression.
cstring maskName = properties.tableName + "_mask_" + keyProperties.name;
const IR::Expression *ternaryMask = nullptr;
// We can recover from taint by inserting a ternary match that is 0.
const auto *wildCard = IR::getConstant(keyExpr->type, 0);
if (keyProperties.isTainted) {
matches->emplace(keyProperties.name,
new Optional(keyProperties.key, ctrlPlaneKey, false));
} else {
const IR::Expression *keyExpr = keyProperties.key->expression;
matches->emplace(keyProperties.name,
new Optional(keyProperties.key, ctrlPlaneKey, true));
hitCondition = new IR::LAnd(hitCondition, new IR::Equ(keyExpr, ctrlPlaneKey));
new Ternary(keyProperties.key, ctrlPlaneKey, wildCard));
return hitCondition;
}
return hitCondition;
const auto *fullMatch = IR::getMaxValueConstant(keyExpr->type);
ternaryMask = ToolsVariables::getSymbolicVariable(keyExpr->type, maskName);
auto *maskCond =
new IR::LOr(new IR::Equ(ternaryMask, wildCard), new IR::Equ(ternaryMask, fullMatch));
matches->emplace(keyProperties.name,
new Ternary(keyProperties.key, ctrlPlaneKey, wildCard));
return new IR::LAnd(
hitCondition,
new IR::LAnd(maskCond, new IR::Equ(new IR::BAnd(keyExpr, ternaryMask),
new IR::BAnd(ctrlPlaneKey, ternaryMask))));
}
// Action selector entries are not part of the match.
if (keyProperties.matchType == BMv2Constants::MATCH_KIND_SELECTOR) {
Expand Down

0 comments on commit 0d1ad79

Please sign in to comment.