From 0d1ad791951668f579bc3fec26feac6212f38695 Mon Sep 17 00:00:00 2001 From: fruffy Date: Thu, 22 Jun 2023 11:37:56 -0400 Subject: [PATCH] Rethink the optional implementation. Add a ternary match which can be either a wildcard or an exact match. --- .../testgen/targets/bmv2/table_stepper.cpp | 27 ++++++++++++------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/backends/p4tools/modules/testgen/targets/bmv2/table_stepper.cpp b/backends/p4tools/modules/testgen/targets/bmv2/table_stepper.cpp index 44782b7aead..1945f30804f 100644 --- a/backends/p4tools/modules/testgen/targets/bmv2/table_stepper.cpp +++ b/backends/p4tools/modules/testgen/targets/bmv2/table_stepper.cpp @@ -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) {