diff --git a/backends/bmv2/pna_nic/midend.cpp b/backends/bmv2/pna_nic/midend.cpp index 5617a509be0..275dfd994bd 100644 --- a/backends/bmv2/pna_nic/midend.cpp +++ b/backends/bmv2/pna_nic/midend.cpp @@ -130,7 +130,7 @@ PnaNicMidEnd::PnaNicMidEnd(CompilerOptions &options, std::ostream *outStream) new P4::StrengthReduction(&typeMap), new P4::EliminateTuples(&typeMap), new P4::SimplifyComparisons(&typeMap), - new P4::CopyStructures(&typeMap), + new P4::CopyStructures(&typeMap, P4::CopyStructuresConfig()), new P4::NestedStructs(&typeMap), new P4::SimplifySelectList(&typeMap), new P4::RemoveSelectBooleans(&typeMap), diff --git a/backends/bmv2/psa_switch/midend.cpp b/backends/bmv2/psa_switch/midend.cpp index 643fd878295..8e3400004d3 100644 --- a/backends/bmv2/psa_switch/midend.cpp +++ b/backends/bmv2/psa_switch/midend.cpp @@ -131,7 +131,7 @@ PsaSwitchMidEnd::PsaSwitchMidEnd(CompilerOptions &options, std::ostream *outStre new P4::StrengthReduction(&typeMap), new P4::EliminateTuples(&typeMap), new P4::SimplifyComparisons(&typeMap), - new P4::CopyStructures(&typeMap), + new P4::CopyStructures(&typeMap, P4::CopyStructuresConfig()), new P4::NestedStructs(&typeMap), new P4::SimplifySelectList(&typeMap), new P4::RemoveSelectBooleans(&typeMap), diff --git a/backends/bmv2/simple_switch/midend.cpp b/backends/bmv2/simple_switch/midend.cpp index 0b09c609fcd..04d28fda13a 100644 --- a/backends/bmv2/simple_switch/midend.cpp +++ b/backends/bmv2/simple_switch/midend.cpp @@ -97,7 +97,7 @@ SimpleSwitchMidEnd::SimpleSwitchMidEnd(CompilerOptions &options, std::ostream *o new P4::StrengthReduction(&typeMap), new P4::EliminateTuples(&typeMap), new P4::SimplifyComparisons(&typeMap), - new P4::CopyStructures(&typeMap), + new P4::CopyStructures(&typeMap, P4::CopyStructuresConfig()), new P4::NestedStructs(&typeMap), new P4::SimplifySelectList(&typeMap), new P4::RemoveSelectBooleans(&typeMap), diff --git a/backends/dpdk/midend.cpp b/backends/dpdk/midend.cpp index 89cfd232f48..9662baf8189 100644 --- a/backends/dpdk/midend.cpp +++ b/backends/dpdk/midend.cpp @@ -194,7 +194,11 @@ DpdkMidEnd::DpdkMidEnd(CompilerOptions &options, std::ostream *outStream) { new P4::StrengthReduction(&typeMap), new P4::EliminateTuples(&typeMap), new P4::SimplifyComparisons(&typeMap), - new P4::CopyStructures(&typeMap, false /* errorOnMethodCall */), + new P4::CopyStructures(&typeMap, P4::CopyStructuresConfig{ + /*errorOnMethodCall*/ false, + /*copyHeaders*/ false, + /*expandUnions*/ true, + }), new P4::NestedStructs(&typeMap), new P4::SimplifySelectList(&typeMap), new P4::RemoveSelectBooleans(&typeMap), diff --git a/backends/p4test/midend.cpp b/backends/p4test/midend.cpp index 4c1da8b857e..0de10b3063c 100644 --- a/backends/p4test/midend.cpp +++ b/backends/p4test/midend.cpp @@ -104,7 +104,11 @@ MidEnd::MidEnd(CompilerOptions &options, std::ostream *outStream) { new P4::EliminateTuples(&typeMap), new P4::FlattenLogMsg(&typeMap), new P4::SimplifyComparisons(&typeMap), - new P4::CopyStructures(&typeMap, false), + new P4::CopyStructures(&typeMap, P4::CopyStructuresConfig{ + /*errorOnMethodCall*/ false, + /*copyHeaders*/ false, + /*expandUnions*/ true, + }), new P4::NestedStructs(&typeMap), new P4::StrengthReduction(&typeMap), new P4::SimplifySelectList(&typeMap), diff --git a/backends/p4tools/common/compiler/midend.cpp b/backends/p4tools/common/compiler/midend.cpp index 6d58f28ca76..48b163afc83 100644 --- a/backends/p4tools/common/compiler/midend.cpp +++ b/backends/p4tools/common/compiler/midend.cpp @@ -121,7 +121,13 @@ void MidEnd::addDefaultPasses() { new P4::SimplifyComparisons(&typeMap), // Expand header and struct assignments into sequences of field assignments. new PassRepeated({ - new P4::CopyStructures(&typeMap, false, true, nullptr), + new P4::CopyStructures(&typeMap, + { + /*errorOnMethodCall*/ false, + /*copyHeaders*/ true, + /*expandUnions*/ false, + }, + nullptr), }), new P4::RemoveParserControlFlow(&typeMap), // Flatten nested list expressions. diff --git a/backends/tofino/bf-p4c/arch/psa/psa.cpp b/backends/tofino/bf-p4c/arch/psa/psa.cpp index dd430a6a501..3d6859dbefb 100644 --- a/backends/tofino/bf-p4c/arch/psa/psa.cpp +++ b/backends/tofino/bf-p4c/arch/psa/psa.cpp @@ -1070,7 +1070,7 @@ PortableSwitchTranslation::PortableSwitchTranslation(P4::ReferenceMap *refMap, P "psa_direct_meter"_cs, "psa_idle_timeout"_cs, "psa_empty_group_action"_cs}), new P4::ConvertEnums(typeMap, new PSA::PacketPathTo8Bits), - new P4::CopyStructures(typeMap), + new P4::CopyStructures(typeMap, P4::CopyStructuresConfig()), new BFN::TypeChecking(refMap, typeMap, true), evaluator, new VisitFunctor( diff --git a/backends/tofino/bf-p4c/midend/copy_header.cpp b/backends/tofino/bf-p4c/midend/copy_header.cpp index 80625f6fc27..c8595928150 100644 --- a/backends/tofino/bf-p4c/midend/copy_header.cpp +++ b/backends/tofino/bf-p4c/midend/copy_header.cpp @@ -265,7 +265,11 @@ BFN::CopyHeaders::CopyHeaders(P4::ReferenceMap *refMap, P4::TypeMap *typeMap, passes.emplace_back(typeChecking); // `errorOnMethodCall = false` viz don't flag functions returning structs as an error. // E.g. Phase0 extern function returns a header struct. - passes.emplace_back(new P4::DoCopyStructures(typeMap, false)); - passes.emplace_back(typeChecking); + new P4::CopyStructures(typeMap, P4::DoCopyStructures{ + /*errorOnMethodCall*/ false, + /*copyHeaders*/ false, + /*expandUnions*/ false, + }), + passes.emplace_back(typeChecking); passes.emplace_back(new DoCopyHeaders(typeMap)); } diff --git a/backends/ubpf/midend.cpp b/backends/ubpf/midend.cpp index ec7e3605351..2f66525be05 100644 --- a/backends/ubpf/midend.cpp +++ b/backends/ubpf/midend.cpp @@ -93,7 +93,7 @@ const IR::ToplevelBlock *MidEnd::run(EbpfOptions &options, const IR::P4Program * new P4::StrengthReduction(&typeMap), }), new P4::SimplifyComparisons(&typeMap), - new P4::CopyStructures(&typeMap), + new P4::CopyStructures(&typeMap, P4::CopyStructuresConfig()), new P4::LocalCopyPropagation(&typeMap), new P4::SimplifySelectList(&typeMap), new P4::MoveDeclarations(), // more may have been introduced diff --git a/frontends/p4/toP4/toP4.cpp b/frontends/p4/toP4/toP4.cpp index 0ef1a2a6984..117fe8bcfdf 100644 --- a/frontends/p4/toP4/toP4.cpp +++ b/frontends/p4/toP4/toP4.cpp @@ -660,7 +660,7 @@ bool ToP4::preorder(const IR::Type_Error *d) { }) ->toVector(); if (!isDeclaration || !userErrors.empty()) { - builder.append("error"); + builder.append("error "); if (!isDeclaration) { return false; diff --git a/midend/copyStructures.cpp b/midend/copyStructures.cpp index d4d3334b4b4..b479eaf6e50 100644 --- a/midend/copyStructures.cpp +++ b/midend/copyStructures.cpp @@ -73,13 +73,14 @@ const IR::Node *DoCopyStructures::postorder(IR::AssignmentStatement *statement) FIXME: this is not correct for header unions and should be fixed. The fix bellow, commented-out, causes problems elsewhere. https://github.com/p4lang/p4c/issues/3842 - if (ltype->is()) - return statement; */ + if (!config.expandUnions && ltype->is()) { + return statement; + } // Do not copy structures for method calls. if (statement->right->is()) { - if (errorOnMethodCall) { + if (config.errorOnMethodCall) { ::P4::error(ErrorType::ERR_UNSUPPORTED_ON_TARGET, "%1%: functions or methods returning structures " "are not supported on this target", @@ -98,7 +99,7 @@ const IR::Node *DoCopyStructures::postorder(IR::AssignmentStatement *statement) retval.push_back( new IR::AssignmentStatement(statement->srcInfo, left, right->expression)); } - } else if (copyHeaders && ltype->is()) { + } else if (config.copyHeaders && ltype->is()) { const auto *header = ltype->checkedTo(); // Build a "src.isValid()" call. const auto *isSrcValidCall = new IR::MethodCallExpression( diff --git a/midend/copyStructures.h b/midend/copyStructures.h index 78fcdba6999..b2940dcbd76 100644 --- a/midend/copyStructures.h +++ b/midend/copyStructures.h @@ -22,6 +22,23 @@ limitations under the License. namespace P4 { +struct CopyStructuresConfig { + /// Specific targets may allow functions or methods to return structs. + /// Such methods will not be converted in this pass. Setting the + /// errorOnMethodCall flag will produce an error message if such a + /// method is encountered. + bool errorOnMethodCall = true; + + /// Do not only copy normal structures but also perform copy assignments for headers. + bool copyHeaders = false; + /// Also expand header union assignments. + /// TODO: This is currently disabled by default because validity of header unions is not + /// correctly preserved. Only the validity of the last member in the union is preserved in the + /// assignment. + /// Some passes, such as FlattenHeaderUnion requires this expansion. + bool expandUnions = false; +}; + /** * Convert assignments between structures to assignments between fields * @@ -53,19 +70,15 @@ namespace P4 { * */ class DoCopyStructures : public Transform { + /// The type map. TypeMap *typeMap; - /// Specific targets may allow functions or methods to return structs. - /// Such methods will not be converted in this pass. Setting the - /// errorOnMethodCall flag will produce an error message if such a - /// method is encountered. - bool errorOnMethodCall; - /// Do not only copy normal structures but also perform copy assignments for headers. - bool copyHeaders; + /// Configuration options. + CopyStructuresConfig config; public: - explicit DoCopyStructures(TypeMap *typeMap, bool errorOnMethodCall, bool copyHeaders = false) - : typeMap(typeMap), errorOnMethodCall(errorOnMethodCall), copyHeaders(copyHeaders) { + explicit DoCopyStructures(TypeMap *typeMap, CopyStructuresConfig configuration) + : typeMap(typeMap), config(configuration) { CHECK_NULL(typeMap); setName("DoCopyStructures"); } @@ -116,15 +129,15 @@ class RemoveAliases : public Transform { class CopyStructures : public PassRepeated { public: - explicit CopyStructures(TypeMap *typeMap, bool errorOnMethodCall = true, - bool copyHeaders = false, TypeChecking *typeChecking = nullptr) { + explicit CopyStructures(TypeMap *typeMap, CopyStructuresConfig config, + TypeChecking *typeChecking = nullptr) { CHECK_NULL(typeMap); setName("CopyStructures"); if (typeChecking == nullptr) typeChecking = new TypeChecking(nullptr, typeMap); passes.emplace_back(typeChecking); passes.emplace_back(new RemoveAliases(typeMap)); passes.emplace_back(typeChecking); - passes.emplace_back(new DoCopyStructures(typeMap, errorOnMethodCall, copyHeaders)); + passes.emplace_back(new DoCopyStructures(typeMap, config)); } }; diff --git a/test/gtest/midend_pass.cpp b/test/gtest/midend_pass.cpp index d6752ee7ae0..319bb140978 100644 --- a/test/gtest/midend_pass.cpp +++ b/test/gtest/midend_pass.cpp @@ -83,7 +83,7 @@ MidEnd::MidEnd(CompilerOptions &options, std::ostream *outStream) { new P4::StrengthReduction(&typeMap), new P4::EliminateTuples(&typeMap), new P4::SimplifyComparisons(&typeMap), - new P4::CopyStructures(&typeMap), + new P4::CopyStructures(&typeMap, P4::CopyStructuresConfig()), new P4::NestedStructs(&typeMap), new P4::StrengthReduction(&typeMap), new P4::SimplifySelectList(&typeMap), diff --git a/testdata/p4_16_samples_outputs/invalid-hdr-warnings5-midend.p4 b/testdata/p4_16_samples_outputs/invalid-hdr-warnings5-midend.p4 index ef01e66a43c..6d48acbffed 100644 --- a/testdata/p4_16_samples_outputs/invalid-hdr-warnings5-midend.p4 +++ b/testdata/p4_16_samples_outputs/invalid-hdr-warnings5-midend.p4 @@ -10,6 +10,12 @@ header Header2 { bit<16> data; } +header_union Union { + Header1 h1; + Header2 h2; + Header1 h3; +} + struct H { Header1 h1; Header1 u1_h1; @@ -77,9 +83,11 @@ control IngressI(inout H hdr, inout M meta, inout standard_metadata_t smeta) { @name("u1_0_h1") Header1 u1_0_h1_0; @name("u1_0_h2") Header2 u1_0_h2_0; @name("u1_0_h3") Header1 u1_0_h3_0; + @name("IngressI.u1") Union u1; @name("u2_0_h1") Header1 u2_0_h1_0; @name("u2_0_h2") Header2 u2_0_h2_0; @name("u2_0_h3") Header1 u2_0_h3_0; + @name("IngressI.u2") Union u2; @hidden @name("invalidhdrwarnings5l58") action invalidhdrwarnings5l58_0() { u1_0_h1_0.setInvalid(); u1_0_h2_0.setInvalid(); @@ -97,30 +105,7 @@ control IngressI(inout H hdr, inout M meta, inout standard_metadata_t smeta) { u1_0_h2_0.setValid(); u1_0_h1_0.setInvalid(); u1_0_h3_0.setInvalid(); - if (u1_0_h1_0.isValid()) { - u2_0_h1_0.setValid(); - u2_0_h1_0 = u1_0_h1_0; - u2_0_h2_0.setInvalid(); - u2_0_h3_0.setInvalid(); - } else { - u2_0_h1_0.setInvalid(); - } - if (u1_0_h2_0.isValid()) { - u2_0_h2_0.setValid(); - u2_0_h2_0 = u1_0_h2_0; - u2_0_h1_0.setInvalid(); - u2_0_h3_0.setInvalid(); - } else { - u2_0_h2_0.setInvalid(); - } - if (u1_0_h3_0.isValid()) { - u2_0_h3_0.setValid(); - u2_0_h3_0 = u1_0_h3_0; - u2_0_h1_0.setInvalid(); - u2_0_h2_0.setInvalid(); - } else { - u2_0_h3_0.setInvalid(); - } + u2 = u1; u2_0_h2_0.setValid(); u2_0_h2_0.data = 16w1; u2_0_h1_0.setInvalid(); diff --git a/testdata/p4_16_samples_outputs/invalid-hdr-warnings6-midend.p4 b/testdata/p4_16_samples_outputs/invalid-hdr-warnings6-midend.p4 index ac70b00a5a5..e9bb01ce91c 100644 --- a/testdata/p4_16_samples_outputs/invalid-hdr-warnings6-midend.p4 +++ b/testdata/p4_16_samples_outputs/invalid-hdr-warnings6-midend.p4 @@ -105,30 +105,7 @@ control IngressI(inout H hdr, inout M meta, inout standard_metadata_t smeta) { u_1[0].h2.setValid(); u_1[0].h1.setInvalid(); u_1[0].h3.setInvalid(); - if (u_1[1w0].h1.isValid()) { - u_1[1].h1.setValid(); - u_1[1].h1 = u_1[1w0].h1; - u_1[1].h2.setInvalid(); - u_1[1].h3.setInvalid(); - } else { - u_1[1].h1.setInvalid(); - } - if (u_1[1w0].h2.isValid()) { - u_1[1].h2.setValid(); - u_1[1].h2 = u_1[1w0].h2; - u_1[1].h1.setInvalid(); - u_1[1].h3.setInvalid(); - } else { - u_1[1].h2.setInvalid(); - } - if (u_1[1w0].h3.isValid()) { - u_1[1].h3.setValid(); - u_1[1].h3 = u_1[1w0].h3; - u_1[1].h1.setInvalid(); - u_1[1].h2.setInvalid(); - } else { - u_1[1].h3.setInvalid(); - } + u_1[1] = u_1[1w0]; u_1[1].h2.setValid(); u_1[1].h2.data = 16w1; u_1[1].h1.setInvalid(); diff --git a/testdata/p4_16_samples_outputs/invalid-hdr-warnings7-midend.p4 b/testdata/p4_16_samples_outputs/invalid-hdr-warnings7-midend.p4 index c04eb2309af..168f2876823 100644 --- a/testdata/p4_16_samples_outputs/invalid-hdr-warnings7-midend.p4 +++ b/testdata/p4_16_samples_outputs/invalid-hdr-warnings7-midend.p4 @@ -16,19 +16,23 @@ package top(ct _ct); control c(inout bit<32> x) { H1 u_0_h1; H2 u_0_h2; + @name("c.u") U u_0; @name("c.hs") H1[2] hs_0; @name("c.us") U[2] us_0; H1 u1_0_h1; H2 u1_0_h2; + @name("c.u1") U u1_0; @name("c.hs1") H1[2] hs1_0; @name("c.us1") U[2] us1_0; H1 u1_1_h1; H2 u1_1_h2; + @name("c.u1") U u1_1; @name("c.hs1") H1[2] hs1_1; @name("c.us1") U[2] us1_1; @name("c.us1") U[2] us1_2; H1 u1_3_h1; H2 u1_3_h2; + @name("c.u1") U u1_3; @name("c.hs1") H1[2] hs1_3; @name("c.us1") U[2] us1_3; @name("c.initialize") action initialize() { @@ -56,96 +60,18 @@ control c(inout bit<32> x) { us1_0[0].h2.setInvalid(); us1_0[0].h2.setValid(); us1_0[0].h1.setInvalid(); - if (u1_0_h1.isValid()) { - u_0_h1.setValid(); - u_0_h1 = u1_0_h1; - u_0_h2.setInvalid(); - } else { - u_0_h1.setInvalid(); - } - if (u1_0_h2.isValid()) { - u_0_h2.setValid(); - u_0_h2 = u1_0_h2; - u_0_h1.setInvalid(); - } else { - u_0_h2.setInvalid(); - } + u_0 = u1_0; hs_0[0] = hs1_0[0]; hs_0[1] = hs1_0[1]; - if (us1_0[0].h1.isValid()) { - us_0[0].h1.setValid(); - us_0[0].h1 = us1_0[0].h1; - us_0[0].h2.setInvalid(); - } else { - us_0[0].h1.setInvalid(); - } - if (us1_0[0].h2.isValid()) { - us_0[0].h2.setValid(); - us_0[0].h2 = us1_0[0].h2; - us_0[0].h1.setInvalid(); - } else { - us_0[0].h2.setInvalid(); - } - if (us1_0[1].h1.isValid()) { - us_0[1].h1.setValid(); - us_0[1].h1 = us1_0[1].h1; - us_0[1].h2.setInvalid(); - } else { - us_0[1].h1.setInvalid(); - } - if (us1_0[1].h2.isValid()) { - us_0[1].h2.setValid(); - us_0[1].h2 = us1_0[1].h2; - us_0[1].h1.setInvalid(); - } else { - us_0[1].h2.setInvalid(); - } + us_0[0] = us1_0[0]; + us_0[1] = us1_0[1]; } @name("c.inout_action1") action inout_action1() { - if (u_0_h1.isValid()) { - u1_1_h1.setValid(); - u1_1_h1 = u_0_h1; - u1_1_h2.setInvalid(); - } else { - u1_1_h1.setInvalid(); - } - if (u_0_h2.isValid()) { - u1_1_h2.setValid(); - u1_1_h2 = u_0_h2; - u1_1_h1.setInvalid(); - } else { - u1_1_h2.setInvalid(); - } + u1_1 = u_0; hs1_1[0] = hs_0[0]; hs1_1[1] = hs_0[1]; - if (us_0[0].h1.isValid()) { - us1_1[0].h1.setValid(); - us1_1[0].h1 = us_0[0].h1; - us1_1[0].h2.setInvalid(); - } else { - us1_1[0].h1.setInvalid(); - } - if (us_0[0].h2.isValid()) { - us1_1[0].h2.setValid(); - us1_1[0].h2 = us_0[0].h2; - us1_1[0].h1.setInvalid(); - } else { - us1_1[0].h2.setInvalid(); - } - if (us_0[1].h1.isValid()) { - us1_1[1].h1.setValid(); - us1_1[1].h1 = us_0[1].h1; - us1_1[1].h2.setInvalid(); - } else { - us1_1[1].h1.setInvalid(); - } - if (us_0[1].h2.isValid()) { - us1_1[1].h2.setValid(); - us1_1[1].h2 = us_0[1].h2; - us1_1[1].h1.setInvalid(); - } else { - us1_1[1].h2.setInvalid(); - } + us1_1[0] = us_0[0]; + us1_1[1] = us_0[1]; u1_1_h1.setValid(); u1_1_h1.a = 32w1; u1_1_h2.setInvalid(); @@ -165,129 +91,25 @@ control c(inout bit<32> x) { u1_1_h2.setInvalid(); us1_1[0].h1.setValid(); us1_1[0].h2.setInvalid(); - if (u1_1_h1.isValid()) { - u_0_h1.setValid(); - u_0_h1 = u1_1_h1; - u_0_h2.setInvalid(); - } else { - u_0_h1.setInvalid(); - } - if (u1_1_h2.isValid()) { - u_0_h2.setValid(); - u_0_h2 = u1_1_h2; - u_0_h1.setInvalid(); - } else { - u_0_h2.setInvalid(); - } + u_0 = u1_1; hs_0[0] = hs1_1[0]; hs_0[1] = hs1_1[1]; - if (us1_1[0].h1.isValid()) { - us_0[0].h1.setValid(); - us_0[0].h1 = us1_1[0].h1; - us_0[0].h2.setInvalid(); - } else { - us_0[0].h1.setInvalid(); - } - if (us1_1[0].h2.isValid()) { - us_0[0].h2.setValid(); - us_0[0].h2 = us1_1[0].h2; - us_0[0].h1.setInvalid(); - } else { - us_0[0].h2.setInvalid(); - } + us_0[0] = us1_1[0]; } @name("c.inout_action2") action inout_action2() { - if (us_0[0].h1.isValid()) { - us1_2[0].h1.setValid(); - us1_2[0].h1 = us_0[0].h1; - us1_2[0].h2.setInvalid(); - } else { - us1_2[0].h1.setInvalid(); - } - if (us_0[0].h2.isValid()) { - us1_2[0].h2.setValid(); - us1_2[0].h2 = us_0[0].h2; - us1_2[0].h1.setInvalid(); - } else { - us1_2[0].h2.setInvalid(); - } - if (us_0[1].h1.isValid()) { - us1_2[1].h1.setValid(); - us1_2[1].h1 = us_0[1].h1; - us1_2[1].h2.setInvalid(); - } else { - us1_2[1].h1.setInvalid(); - } - if (us_0[1].h2.isValid()) { - us1_2[1].h2.setValid(); - us1_2[1].h2 = us_0[1].h2; - us1_2[1].h1.setInvalid(); - } else { - us1_2[1].h2.setInvalid(); - } + us1_2[0] = us_0[0]; + us1_2[1] = us_0[1]; us1_2[1w1].h1.setInvalid(); us1_2[1w1].h2.setValid(); us1_2[1w1].h1.setInvalid(); - if (us1_2[1].h1.isValid()) { - us_0[1].h1.setValid(); - us_0[1].h1 = us1_2[1].h1; - us_0[1].h2.setInvalid(); - } else { - us_0[1].h1.setInvalid(); - } - if (us1_2[1].h2.isValid()) { - us_0[1].h2.setValid(); - us_0[1].h2 = us1_2[1].h2; - us_0[1].h1.setInvalid(); - } else { - us_0[1].h2.setInvalid(); - } + us_0[1] = us1_2[1]; } @name("c.xor") action xor() { - if (u_0_h1.isValid()) { - u1_3_h1.setValid(); - u1_3_h1 = u_0_h1; - u1_3_h2.setInvalid(); - } else { - u1_3_h1.setInvalid(); - } - if (u_0_h2.isValid()) { - u1_3_h2.setValid(); - u1_3_h2 = u_0_h2; - u1_3_h1.setInvalid(); - } else { - u1_3_h2.setInvalid(); - } + u1_3 = u_0; hs1_3[0] = hs_0[0]; hs1_3[1] = hs_0[1]; - if (us_0[0].h1.isValid()) { - us1_3[0].h1.setValid(); - us1_3[0].h1 = us_0[0].h1; - us1_3[0].h2.setInvalid(); - } else { - us1_3[0].h1.setInvalid(); - } - if (us_0[0].h2.isValid()) { - us1_3[0].h2.setValid(); - us1_3[0].h2 = us_0[0].h2; - us1_3[0].h1.setInvalid(); - } else { - us1_3[0].h2.setInvalid(); - } - if (us_0[1].h1.isValid()) { - us1_3[1].h1.setValid(); - us1_3[1].h1 = us_0[1].h1; - us1_3[1].h2.setInvalid(); - } else { - us1_3[1].h1.setInvalid(); - } - if (us_0[1].h2.isValid()) { - us1_3[1].h2.setValid(); - us1_3[1].h2 = us_0[1].h2; - us1_3[1].h1.setInvalid(); - } else { - us1_3[1].h2.setInvalid(); - } + us1_3[0] = us_0[0]; + us1_3[1] = us_0[1]; x = u1_3_h1.a ^ u1_3_h2.a ^ hs1_3[0].a ^ hs1_3[1].a ^ us1_3[0].h1.a ^ us1_3[0].h2.a ^ us1_3[1].h1.a ^ us1_3[1].h2.a; } @hidden action invalidhdrwarnings7l13() { diff --git a/testdata/p4_16_samples_outputs/invalid-hdr-warnings8-midend.p4 b/testdata/p4_16_samples_outputs/invalid-hdr-warnings8-midend.p4 index 0b141b56ebe..e4ac2755bb0 100644 --- a/testdata/p4_16_samples_outputs/invalid-hdr-warnings8-midend.p4 +++ b/testdata/p4_16_samples_outputs/invalid-hdr-warnings8-midend.p4 @@ -16,22 +16,27 @@ package top(ct _ct); control c(inout bit<32> x) { H1 u_0_h1; H2 u_0_h2; + @name("c.u") U u_0; @name("c.hs") H1[2] hs_0; @name("c.us") U[2] us_0; H1 u1_2_h1; H2 u1_2_h2; + @name("c.u1") U u1_2; @name("c.hs1") H1[2] hs1_2; @name("c.us1") U[2] us1_2; H1 u1_3_h1; H2 u1_3_h2; + @name("c.u1") U u1_3; @name("c.hs1") H1[2] hs1_3; @name("c.us1") U[2] us1_3; H1 u1_5_h1; H2 u1_5_h2; + @name("c.u1") U u1_5; @name("c.hs1") H1[2] hs1_5; @name("c.us1") U[2] us1_5; H1 u1_6_h1; H2 u1_6_h2; + @name("c.u1") U u1_6; @name("c.hs1") H1[2] hs1_6; @name("c.us1") U[2] us1_6; @name("c.inout_action2") action inout_action2() { @@ -59,50 +64,11 @@ control c(inout bit<32> x) { us1_3[0].h2.setInvalid(); us1_3[0].h2.setValid(); us1_3[0].h1.setInvalid(); - if (u1_3_h1.isValid()) { - u1_2_h1.setValid(); - u1_2_h1 = u1_3_h1; - u1_2_h2.setInvalid(); - } else { - u1_2_h1.setInvalid(); - } - if (u1_3_h2.isValid()) { - u1_2_h2.setValid(); - u1_2_h2 = u1_3_h2; - u1_2_h1.setInvalid(); - } else { - u1_2_h2.setInvalid(); - } + u1_2 = u1_3; hs1_2[0] = hs1_3[0]; hs1_2[1] = hs1_3[1]; - if (us1_3[0].h1.isValid()) { - us1_2[0].h1.setValid(); - us1_2[0].h1 = us1_3[0].h1; - us1_2[0].h2.setInvalid(); - } else { - us1_2[0].h1.setInvalid(); - } - if (us1_3[0].h2.isValid()) { - us1_2[0].h2.setValid(); - us1_2[0].h2 = us1_3[0].h2; - us1_2[0].h1.setInvalid(); - } else { - us1_2[0].h2.setInvalid(); - } - if (us1_3[1].h1.isValid()) { - us1_2[1].h1.setValid(); - us1_2[1].h1 = us1_3[1].h1; - us1_2[1].h2.setInvalid(); - } else { - us1_2[1].h1.setInvalid(); - } - if (us1_3[1].h2.isValid()) { - us1_2[1].h2.setValid(); - us1_2[1].h2 = us1_3[1].h2; - us1_2[1].h1.setInvalid(); - } else { - us1_2[1].h2.setInvalid(); - } + us1_2[0] = us1_3[0]; + us1_2[1] = us1_3[1]; u1_2_h1.setValid(); u1_2_h1.a = 32w1; u1_2_h2.setInvalid(); @@ -122,50 +88,11 @@ control c(inout bit<32> x) { u1_2_h2.setInvalid(); us1_2[0].h1.setValid(); us1_2[0].h2.setInvalid(); - if (u1_2_h1.isValid()) { - u1_5_h1.setValid(); - u1_5_h1 = u1_2_h1; - u1_5_h2.setInvalid(); - } else { - u1_5_h1.setInvalid(); - } - if (u1_2_h2.isValid()) { - u1_5_h2.setValid(); - u1_5_h2 = u1_2_h2; - u1_5_h1.setInvalid(); - } else { - u1_5_h2.setInvalid(); - } + u1_5 = u1_2; hs1_5[0] = hs1_2[0]; hs1_5[1] = hs1_2[1]; - if (us1_2[0].h1.isValid()) { - us1_5[0].h1.setValid(); - us1_5[0].h1 = us1_2[0].h1; - us1_5[0].h2.setInvalid(); - } else { - us1_5[0].h1.setInvalid(); - } - if (us1_2[0].h2.isValid()) { - us1_5[0].h2.setValid(); - us1_5[0].h2 = us1_2[0].h2; - us1_5[0].h1.setInvalid(); - } else { - us1_5[0].h2.setInvalid(); - } - if (us1_3[1].h1.isValid()) { - us1_5[1].h1.setValid(); - us1_5[1].h1 = us1_3[1].h1; - us1_5[1].h2.setInvalid(); - } else { - us1_5[1].h1.setInvalid(); - } - if (us1_3[1].h2.isValid()) { - us1_5[1].h2.setValid(); - us1_5[1].h2 = us1_3[1].h2; - us1_5[1].h1.setInvalid(); - } else { - us1_5[1].h2.setInvalid(); - } + us1_5[0] = us1_2[0]; + us1_5[1] = us1_3[1]; u1_5_h1.setValid(); u1_5_h1.a = 32w1; u1_5_h2.setInvalid(); @@ -183,96 +110,18 @@ control c(inout bit<32> x) { us1_5[1w1].h1.setInvalid(); us1_5[1w1].h2.setValid(); us1_5[1w1].h1.setInvalid(); - if (u1_5_h1.isValid()) { - u_0_h1.setValid(); - u_0_h1 = u1_5_h1; - u_0_h2.setInvalid(); - } else { - u_0_h1.setInvalid(); - } - if (u1_5_h2.isValid()) { - u_0_h2.setValid(); - u_0_h2 = u1_5_h2; - u_0_h1.setInvalid(); - } else { - u_0_h2.setInvalid(); - } + u_0 = u1_5; hs_0[0] = hs1_5[0]; hs_0[1] = hs1_5[1]; - if (us1_5[0].h1.isValid()) { - us_0[0].h1.setValid(); - us_0[0].h1 = us1_5[0].h1; - us_0[0].h2.setInvalid(); - } else { - us_0[0].h1.setInvalid(); - } - if (us1_5[0].h2.isValid()) { - us_0[0].h2.setValid(); - us_0[0].h2 = us1_5[0].h2; - us_0[0].h1.setInvalid(); - } else { - us_0[0].h2.setInvalid(); - } - if (us1_5[1].h1.isValid()) { - us_0[1].h1.setValid(); - us_0[1].h1 = us1_5[1].h1; - us_0[1].h2.setInvalid(); - } else { - us_0[1].h1.setInvalid(); - } - if (us1_5[1].h2.isValid()) { - us_0[1].h2.setValid(); - us_0[1].h2 = us1_5[1].h2; - us_0[1].h1.setInvalid(); - } else { - us_0[1].h2.setInvalid(); - } + us_0[0] = us1_5[0]; + us_0[1] = us1_5[1]; } @name("c.xor") action xor() { - if (u_0_h1.isValid()) { - u1_6_h1.setValid(); - u1_6_h1 = u_0_h1; - u1_6_h2.setInvalid(); - } else { - u1_6_h1.setInvalid(); - } - if (u_0_h2.isValid()) { - u1_6_h2.setValid(); - u1_6_h2 = u_0_h2; - u1_6_h1.setInvalid(); - } else { - u1_6_h2.setInvalid(); - } + u1_6 = u_0; hs1_6[0] = hs_0[0]; hs1_6[1] = hs_0[1]; - if (us_0[0].h1.isValid()) { - us1_6[0].h1.setValid(); - us1_6[0].h1 = us_0[0].h1; - us1_6[0].h2.setInvalid(); - } else { - us1_6[0].h1.setInvalid(); - } - if (us_0[0].h2.isValid()) { - us1_6[0].h2.setValid(); - us1_6[0].h2 = us_0[0].h2; - us1_6[0].h1.setInvalid(); - } else { - us1_6[0].h2.setInvalid(); - } - if (us_0[1].h1.isValid()) { - us1_6[1].h1.setValid(); - us1_6[1].h1 = us_0[1].h1; - us1_6[1].h2.setInvalid(); - } else { - us1_6[1].h1.setInvalid(); - } - if (us_0[1].h2.isValid()) { - us1_6[1].h2.setValid(); - us1_6[1].h2 = us_0[1].h2; - us1_6[1].h1.setInvalid(); - } else { - us1_6[1].h2.setInvalid(); - } + us1_6[0] = us_0[0]; + us1_6[1] = us_0[1]; x = u1_6_h1.a ^ u1_6_h2.a ^ hs1_6[0].a ^ hs1_6[1].a ^ us1_6[0].h1.a ^ us1_6[0].h2.a ^ us1_6[1].h1.a ^ us1_6[1].h2.a; } @hidden action invalidhdrwarnings8l13() { diff --git a/testdata/p4_16_samples_outputs/invalid-union-midend.p4 b/testdata/p4_16_samples_outputs/invalid-union-midend.p4 index 1318626ea23..695263099ea 100644 --- a/testdata/p4_16_samples_outputs/invalid-union-midend.p4 +++ b/testdata/p4_16_samples_outputs/invalid-union-midend.p4 @@ -13,86 +13,54 @@ header_union HU { } parser p(inout HU hu) { - @name("ih_e") E ih_e_0; - @name("ih_h") H ih_h_0; + HU ih; state start { - transition select(ih_e_0.isValid()) { - true: start_true; - false: start_false; - } - } - state start_true { - hu.e.setValid(); - hu.e = ih_e_0; - hu.h.setInvalid(); - transition start_join; - } - state start_false { - hu.e.setInvalid(); - transition start_join; - } - state start_join { - transition select(ih_h_0.isValid()) { - true: start_true_0; - false: start_false_0; - } - } - state start_true_0 { - hu.h.setValid(); - hu.h = ih_h_0; - hu.e.setInvalid(); - transition start_join_0; - } - state start_false_0 { - hu.h.setInvalid(); - transition start_join_0; - } - state start_join_0 { + hu = ih; transition accept; } } control c(inout HU hu) { - @name("c.h") H h_1; - @name("c.a") action a_0() { + @name("c.h") H h_0; + @name("c.a") action a() { } - @hidden @name("invalidunion27") action invalidunion27_0() { - h_1.setValid(); - h_1.f = 32w0; - if (h_1.isValid()) { + @hidden action invalidunion27() { + h_0.setValid(); + h_0.f = 32w0; + if (h_0.isValid()) { hu.h.setValid(); - hu.h = h_1; + hu.h = h_0; hu.e.setInvalid(); } else { hu.h.setInvalid(); } } - @hidden @name("tbl_a") table tbl_a_0 { + @hidden table tbl_a { actions = { - a_0(); + a(); } - const default_action = a_0(); + const default_action = a(); } - @hidden @name("tbl_invalidunion27") table tbl_invalidunion27_0 { + @hidden table tbl_invalidunion27 { actions = { - invalidunion27_0(); + invalidunion27(); } - const default_action = invalidunion27_0(); + const default_action = invalidunion27(); } - @name("tmp") bit<32> tmp_0; + bit<32> tmp; apply { - tbl_a_0.apply(); - tmp_0 = 32w0; + tbl_a.apply(); + tmp = 32w0; if (hu.e.isValid()) { - tmp_0 = tmp_0 + 32w1; + tmp = tmp + 32w1; } if (hu.h.isValid()) { - tmp_0 = tmp_0 + 32w1; + tmp = tmp + 32w1; } - if (tmp_0 == 32w1) { + if (tmp == 32w1) { ; } else { - tbl_invalidunion27_0.apply(); + tbl_invalidunion27.apply(); } } } diff --git a/testdata/p4_16_samples_outputs/issue3001-1-midend.p4 b/testdata/p4_16_samples_outputs/issue3001-1-midend.p4 index ae020e06da5..76f4d0b0a11 100644 --- a/testdata/p4_16_samples_outputs/issue3001-1-midend.p4 +++ b/testdata/p4_16_samples_outputs/issue3001-1-midend.p4 @@ -12,6 +12,10 @@ header H { bit<8> x; } +header_union U { + H h; +} + struct headers_t { ethernet_t ethernet; H h1; @@ -36,9 +40,11 @@ control verifyChecksum(inout headers_t hdr, inout metadata_t meta) { control ingressImpl(inout headers_t hdr, inout metadata_t meta, inout standard_metadata_t stdmeta) { @name("ingressImpl.tmp") H tmp; H tmp_1_h; + @name("ingressImpl.tmp_1") U tmp_1; @name("ingressImpl.tmp_3") H[1] tmp_3; @name("ingressImpl.h") H h_0; H u_0_h; + @name("ingressImpl.u") U u_0; @name("ingressImpl.s") H[1] s_0; @hidden action issue30011l84() { log_msg("hdr.ethernet is valid"); @@ -64,12 +70,7 @@ control ingressImpl(inout headers_t hdr, inout metadata_t meta, inout standard_m } @hidden action issue30011l45() { u_0_h.setInvalid(); - if (u_0_h.isValid()) { - tmp_1_h.setValid(); - tmp_1_h = u_0_h; - } else { - tmp_1_h.setInvalid(); - } + tmp_1 = u_0; } @hidden action issue30011l109() { log_msg("h() returned a header stack with valid element 0"); diff --git a/testdata/p4_16_samples_outputs/pna-dpdk-header-union-stack-midend.p4 b/testdata/p4_16_samples_outputs/pna-dpdk-header-union-stack-midend.p4 index a8deb061cf7..06cef364ea7 100644 --- a/testdata/p4_16_samples_outputs/pna-dpdk-header-union-stack-midend.p4 +++ b/testdata/p4_16_samples_outputs/pna-dpdk-header-union-stack-midend.p4 @@ -85,30 +85,7 @@ control ingress(inout H hdr, inout M meta, in pna_main_input_metadata_t istd, in hdr.u[0].h2.setValid(); hdr.u[0].h1.setInvalid(); hdr.u[0].h3.setInvalid(); - if (hdr.u[0].h1.isValid()) { - hdr.u[1].h1.setValid(); - hdr.u[1].h1 = hdr.u[0].h1; - hdr.u[1].h2.setInvalid(); - hdr.u[1].h3.setInvalid(); - } else { - hdr.u[1].h1.setInvalid(); - } - if (hdr.u[0].h2.isValid()) { - hdr.u[1].h2.setValid(); - hdr.u[1].h2 = hdr.u[0].h2; - hdr.u[1].h1.setInvalid(); - hdr.u[1].h3.setInvalid(); - } else { - hdr.u[1].h2.setInvalid(); - } - if (hdr.u[0].h3.isValid()) { - hdr.u[1].h3.setValid(); - hdr.u[1].h3 = hdr.u[0].h3; - hdr.u[1].h1.setInvalid(); - hdr.u[1].h2.setInvalid(); - } else { - hdr.u[1].h3.setInvalid(); - } + hdr.u[1] = hdr.u[0]; hdr.u[1].h1.setValid(); hdr.u[1].h1.data = 32w1; hdr.u[1].h2.setInvalid(); diff --git a/testdata/p4_16_samples_outputs/pna-dpdk-header-union-stack1-midend.p4 b/testdata/p4_16_samples_outputs/pna-dpdk-header-union-stack1-midend.p4 index e8dd55ca688..e0b405d00b1 100644 --- a/testdata/p4_16_samples_outputs/pna-dpdk-header-union-stack1-midend.p4 +++ b/testdata/p4_16_samples_outputs/pna-dpdk-header-union-stack1-midend.p4 @@ -87,30 +87,7 @@ control ingress(inout H hdr, inout M meta, in pna_main_input_metadata_t istd, in hdr.u[0].h2.setValid(); hdr.u[0].h1.setInvalid(); hdr.u[0].h3.setInvalid(); - if (hdr.u[0].h1.isValid()) { - hdr.u[1].h1.setValid(); - hdr.u[1].h1 = hdr.u[0].h1; - hdr.u[1].h2.setInvalid(); - hdr.u[1].h3.setInvalid(); - } else { - hdr.u[1].h1.setInvalid(); - } - if (hdr.u[0].h2.isValid()) { - hdr.u[1].h2.setValid(); - hdr.u[1].h2 = hdr.u[0].h2; - hdr.u[1].h1.setInvalid(); - hdr.u[1].h3.setInvalid(); - } else { - hdr.u[1].h2.setInvalid(); - } - if (hdr.u[0].h3.isValid()) { - hdr.u[1].h3.setValid(); - hdr.u[1].h3 = hdr.u[0].h3; - hdr.u[1].h1.setInvalid(); - hdr.u[1].h2.setInvalid(); - } else { - hdr.u[1].h3.setInvalid(); - } + hdr.u[1] = hdr.u[0]; hdr.u[1].h1.setValid(); hdr.u[1].h1.data = 32w1; hdr.u[1].h2.setInvalid(); diff --git a/testdata/p4_16_samples_outputs/pna-dpdk-header-union-stack2-midend.p4 b/testdata/p4_16_samples_outputs/pna-dpdk-header-union-stack2-midend.p4 index 99ae42c543e..17e932a1a03 100644 --- a/testdata/p4_16_samples_outputs/pna-dpdk-header-union-stack2-midend.p4 +++ b/testdata/p4_16_samples_outputs/pna-dpdk-header-union-stack2-midend.p4 @@ -92,30 +92,7 @@ control ingress(inout H hdr, inout M meta, in pna_main_input_metadata_t istd, in u_1[0].h2.setValid(); u_1[0].h1.setInvalid(); u_1[0].h3.setInvalid(); - if (u_1[0].h1.isValid()) { - u_1[1].h1.setValid(); - u_1[1].h1 = u_1[0].h1; - u_1[1].h2.setInvalid(); - u_1[1].h3.setInvalid(); - } else { - u_1[1].h1.setInvalid(); - } - if (u_1[0].h2.isValid()) { - u_1[1].h2.setValid(); - u_1[1].h2 = u_1[0].h2; - u_1[1].h1.setInvalid(); - u_1[1].h3.setInvalid(); - } else { - u_1[1].h2.setInvalid(); - } - if (u_1[0].h3.isValid()) { - u_1[1].h3.setValid(); - u_1[1].h3 = u_1[0].h3; - u_1[1].h1.setInvalid(); - u_1[1].h2.setInvalid(); - } else { - u_1[1].h3.setInvalid(); - } + u_1[1] = u_1[0]; u_1[1].h2.setValid(); u_1[1].h2.data = 16w1; u_1[1].h1.setInvalid(); diff --git a/testdata/p4_16_samples_outputs/pna-dpdk-header-union-stack2.p4-error b/testdata/p4_16_samples_outputs/pna-dpdk-header-union-stack2.p4-error new file mode 100644 index 00000000000..51968affab9 --- /dev/null +++ b/testdata/p4_16_samples_outputs/pna-dpdk-header-union-stack2.p4-error @@ -0,0 +1,33 @@ +pna-dpdk-header-union-stack2.p4(30): [--Wwarn=invalid_header] warning: accessing a field of an invalid header hdr.u[0].h1 + hdr.u[0].h1.data = 1; + ^^^^^^^^^^^ +pna-dpdk-header-union-stack2.p4(35): [--Wwarn=invalid_header] warning: accessing a field of an invalid header hdr.u[0].h3 + hdr.u[0].h3.data = 1; + ^^^^^^^^^^^ +pna-dpdk-header-union-stack2.p4(49): [--Wwarn=invalid_header] warning: accessing a field of a potentially invalid header hdr.u[0].h1 + hdr.u[0].h1.data = 1; + ^^^^^^^^^^^ +pna-dpdk-header-union-stack2.p4(50): [--Wwarn=invalid_header] warning: accessing a field of a potentially invalid header hdr.u[0].h2 + hdr.u[0].h2.data = 1; + ^^^^^^^^^^^ +pna-dpdk-header-union-stack2.p4(51): [--Wwarn=invalid_header] warning: accessing a field of an invalid header hdr.u[0].h3 + hdr.u[0].h3.data = 1; + ^^^^^^^^^^^ +pna-dpdk-header-union-stack2.p4(68): [--Wwarn=invalid_header] warning: accessing a field of an invalid header u[1].h1 + u[1].h1.data = 1; + ^^^^^^^ +pna-dpdk-header-union-stack2.p4(77): [--Wwarn=invalid_header] warning: accessing a field of a potentially invalid header u[0].h1 + u[0].h1.data = 1; + ^^^^^^^ +pna-dpdk-header-union-stack2.p4(78): [--Wwarn=invalid_header] warning: accessing a field of a potentially invalid header u[0].h2 + u[0].h2.data = 1; + ^^^^^^^ +pna-dpdk-header-union-stack2.p4(82): [--Wwarn=invalid_header] warning: accessing a field of an invalid header u[0].h1 + u[0].h1.data = 1; + ^^^^^^^ +pna-dpdk-header-union-stack2.p4(83): [--Wwarn=invalid_header] warning: accessing a field of an invalid header u[0].h2 + u[0].h2.data = 1; + ^^^^^^^ +pna-dpdk-header-union-stack2.p4(84): [--Wwarn=invalid_header] warning: accessing a field of an invalid header u[0].h3 + u[0].h3.data = 1; + ^^^^^^^ diff --git a/testdata/p4_16_samples_outputs/pna-dpdk-header-union-stack2.p4.bfrt.json b/testdata/p4_16_samples_outputs/pna-dpdk-header-union-stack2.p4.bfrt.json new file mode 100644 index 00000000000..b6d66a630db --- /dev/null +++ b/testdata/p4_16_samples_outputs/pna-dpdk-header-union-stack2.p4.bfrt.json @@ -0,0 +1,5 @@ +{ + "schema_version" : "1.0.0", + "tables" : [], + "learn_filters" : [] +} \ No newline at end of file diff --git a/testdata/p4_16_samples_outputs/pna-dpdk-header-union-stack2.p4.spec b/testdata/p4_16_samples_outputs/pna-dpdk-header-union-stack2.p4.spec new file mode 100644 index 00000000000..fb235fbb207 --- /dev/null +++ b/testdata/p4_16_samples_outputs/pna-dpdk-header-union-stack2.p4.spec @@ -0,0 +1,99 @@ + +struct Header1 { + bit<32> data +} + +struct Header2 { + bit<16> data +} + +header h1 instanceof Header1 +header u0_h1 instanceof Header1 +header u0_h2 instanceof Header2 +header u0_h3 instanceof Header1 +header u1_h1 instanceof Header1 +header u1_h2 instanceof Header2 +header u1_h3 instanceof Header1 +header MainControlT_u_1_h1 instanceof Header1 +header MainControlT_u_1_h2 instanceof Header2 +header MainControlT_u_1_h3 instanceof Header1 +header MainControlT_u_2_h1 instanceof Header1 +header MainControlT_u_2_h2 instanceof Header2 +header MainControlT_u_2_h3 instanceof Header1 + +struct M { + bit<32> pna_main_input_metadata_input_port + bit<32> pna_main_output_metadata_output_port + bit<8> MainParserT_parser_tmp +} +metadata instanceof M + +regarray direction size 0x100 initval 0 +apply { + rx m.pna_main_input_metadata_input_port + validate h.u0_h1 + invalidate h.u0_h2 + invalidate h.u0_h3 + extract h.u0_h3 + validate h.u0_h3 + mov h.u0_h3.data 0x1 + invalidate h.u0_h1 + invalidate h.u0_h2 + mov m.MainParserT_parser_tmp 1 + jmpv LABEL_END h.u0_h3 + mov m.MainParserT_parser_tmp 0 + LABEL_END : jmpeq PARSERIMPL_START_TRUE m.MainParserT_parser_tmp 1 + invalidate h.u0_h1 + jmp PARSERIMPL_START_JOIN + PARSERIMPL_START_TRUE : validate h.u0_h1 + jmpnv LABEL_FALSE h.u0_h3 + validate h.u0_h1 + jmp LABEL_END_0 + LABEL_FALSE : invalidate h.u0_h1 + LABEL_END_0 : mov h.u0_h1.data h.u0_h3.data + invalidate h.u0_h2 + invalidate h.u0_h3 + PARSERIMPL_START_JOIN : validate h.u0_h1 + mov h.u0_h1.data 0x1 + invalidate h.u0_h2 + invalidate h.u0_h3 + validate h.u0_h1 + mov h.u0_h1.data 0x1 + invalidate h.u0_h2 + invalidate h.u0_h3 + validate h.u0_h2 + mov h.u0_h2.data 0x1 + invalidate h.u0_h1 + invalidate h.u0_h3 + validate h.u0_h3 + mov h.u0_h3.data 0x1 + invalidate h.u0_h1 + invalidate h.u0_h2 + invalidate h.MainControlT_u_1_h1 + invalidate h.MainControlT_u_1_h2 + invalidate h.MainControlT_u_1_h3 + invalidate h.MainControlT_u_2_h1 + invalidate h.MainControlT_u_2_h2 + invalidate h.MainControlT_u_2_h3 + validate h.MainControlT_u_1_h1 + invalidate h.MainControlT_u_1_h2 + invalidate h.MainControlT_u_1_h3 + validate h.MainControlT_u_1_h1 + mov h.MainControlT_u_1_h1.data 0x1 + invalidate h.MainControlT_u_1_h2 + invalidate h.MainControlT_u_1_h3 + validate h.MainControlT_u_1_h2 + invalidate h.MainControlT_u_1_h1 + invalidate h.MainControlT_u_1_h3 + validate h.MainControlT_u_2_h2 + mov h.MainControlT_u_2_h2.data 0x1 + invalidate h.MainControlT_u_2_h1 + invalidate h.MainControlT_u_2_h3 + validate h.MainControlT_u_1_h2 + invalidate h.MainControlT_u_1_h1 + invalidate h.MainControlT_u_1_h3 + invalidate h.MainControlT_u_1_h3 + tx m.pna_main_output_metadata_output_port +} + + diff --git a/testdata/p4_16_samples_outputs/pna-dpdk-invalid-hdr-warnings5-midend.p4 b/testdata/p4_16_samples_outputs/pna-dpdk-invalid-hdr-warnings5-midend.p4 index 47e49b8d22a..59af302074e 100644 --- a/testdata/p4_16_samples_outputs/pna-dpdk-invalid-hdr-warnings5-midend.p4 +++ b/testdata/p4_16_samples_outputs/pna-dpdk-invalid-hdr-warnings5-midend.p4 @@ -9,6 +9,12 @@ header Header2 { bit<16> data; } +header_union Union { + Header1 h1; + Header2 h2; + Header1 h3; +} + struct H { Header1 h1; Header1 u1_h1; @@ -76,9 +82,11 @@ control ingress(inout H hdr, inout M meta, in pna_main_input_metadata_t istd, in @name("u1_0_h1") Header1 u1_0_h1_0; @name("u1_0_h2") Header2 u1_0_h2_0; @name("u1_0_h3") Header1 u1_0_h3_0; + @name("ingress.u1") Union u1; @name("u2_0_h1") Header1 u2_0_h1_0; @name("u2_0_h2") Header2 u2_0_h2_0; @name("u2_0_h3") Header1 u2_0_h3_0; + @name("ingress.u2") Union u2; @hidden @name("pnadpdkinvalidhdrwarnings5l60") action pnadpdkinvalidhdrwarnings5l60_0() { u1_0_h1_0.setInvalid(); u1_0_h2_0.setInvalid(); @@ -96,30 +104,7 @@ control ingress(inout H hdr, inout M meta, in pna_main_input_metadata_t istd, in u1_0_h2_0.setValid(); u1_0_h1_0.setInvalid(); u1_0_h3_0.setInvalid(); - if (u1_0_h1_0.isValid()) { - u2_0_h1_0.setValid(); - u2_0_h1_0 = u1_0_h1_0; - u2_0_h2_0.setInvalid(); - u2_0_h3_0.setInvalid(); - } else { - u2_0_h1_0.setInvalid(); - } - if (u1_0_h2_0.isValid()) { - u2_0_h2_0.setValid(); - u2_0_h2_0 = u1_0_h2_0; - u2_0_h1_0.setInvalid(); - u2_0_h3_0.setInvalid(); - } else { - u2_0_h2_0.setInvalid(); - } - if (u1_0_h3_0.isValid()) { - u2_0_h3_0.setValid(); - u2_0_h3_0 = u1_0_h3_0; - u2_0_h1_0.setInvalid(); - u2_0_h2_0.setInvalid(); - } else { - u2_0_h3_0.setInvalid(); - } + u2 = u1; u2_0_h2_0.setValid(); u2_0_h2_0.data = 16w1; u2_0_h1_0.setInvalid(); diff --git a/testdata/p4_16_samples_outputs/pna-dpdk-invalid-hdr-warnings5.p4-error b/testdata/p4_16_samples_outputs/pna-dpdk-invalid-hdr-warnings5.p4-error new file mode 100644 index 00000000000..a5190f2c882 --- /dev/null +++ b/testdata/p4_16_samples_outputs/pna-dpdk-invalid-hdr-warnings5.p4-error @@ -0,0 +1,33 @@ +pna-dpdk-invalid-hdr-warnings5.p4(31): [--Wwarn=invalid_header] warning: accessing a field of an invalid header hdr.u1.h1 + hdr.u1.h1.data = 1; + ^^^^^^^^^ +pna-dpdk-invalid-hdr-warnings5.p4(36): [--Wwarn=invalid_header] warning: accessing a field of an invalid header hdr.u1.h3 + hdr.u1.h3.data = 1; + ^^^^^^^^^ +pna-dpdk-invalid-hdr-warnings5.p4(50): [--Wwarn=invalid_header] warning: accessing a field of a potentially invalid header hdr.u1.h1 + hdr.u1.h1.data = 1; + ^^^^^^^^^ +pna-dpdk-invalid-hdr-warnings5.p4(51): [--Wwarn=invalid_header] warning: accessing a field of a potentially invalid header hdr.u1.h2 + hdr.u1.h2.data = 1; + ^^^^^^^^^ +pna-dpdk-invalid-hdr-warnings5.p4(52): [--Wwarn=invalid_header] warning: accessing a field of an invalid header hdr.u1.h3 + hdr.u1.h3.data = 1; + ^^^^^^^^^ +pna-dpdk-invalid-hdr-warnings5.p4(70): [--Wwarn=invalid_header] warning: accessing a field of an invalid header u2.h1 + u2.h1.data = 1; + ^^^^^ +pna-dpdk-invalid-hdr-warnings5.p4(79): [--Wwarn=invalid_header] warning: accessing a field of a potentially invalid header u1.h1 + u1.h1.data = 1; + ^^^^^ +pna-dpdk-invalid-hdr-warnings5.p4(80): [--Wwarn=invalid_header] warning: accessing a field of a potentially invalid header u1.h2 + u1.h2.data = 1; + ^^^^^ +pna-dpdk-invalid-hdr-warnings5.p4(84): [--Wwarn=invalid_header] warning: accessing a field of an invalid header u1.h1 + u1.h1.data = 1; + ^^^^^ +pna-dpdk-invalid-hdr-warnings5.p4(85): [--Wwarn=invalid_header] warning: accessing a field of an invalid header u1.h2 + u1.h2.data = 1; + ^^^^^ +pna-dpdk-invalid-hdr-warnings5.p4(86): [--Wwarn=invalid_header] warning: accessing a field of an invalid header u1.h3 + u1.h3.data = 1; + ^^^^^ diff --git a/testdata/p4_16_samples_outputs/pna-dpdk-invalid-hdr-warnings5.p4.bfrt.json b/testdata/p4_16_samples_outputs/pna-dpdk-invalid-hdr-warnings5.p4.bfrt.json new file mode 100644 index 00000000000..b6d66a630db --- /dev/null +++ b/testdata/p4_16_samples_outputs/pna-dpdk-invalid-hdr-warnings5.p4.bfrt.json @@ -0,0 +1,5 @@ +{ + "schema_version" : "1.0.0", + "tables" : [], + "learn_filters" : [] +} \ No newline at end of file diff --git a/testdata/p4_16_samples_outputs/pna-dpdk-invalid-hdr-warnings5.p4.spec b/testdata/p4_16_samples_outputs/pna-dpdk-invalid-hdr-warnings5.p4.spec new file mode 100644 index 00000000000..b747685d59a --- /dev/null +++ b/testdata/p4_16_samples_outputs/pna-dpdk-invalid-hdr-warnings5.p4.spec @@ -0,0 +1,99 @@ + +struct Header1 { + bit<32> data +} + +struct Header2 { + bit<16> data +} + +header h1 instanceof Header1 +header u1_h1 instanceof Header1 +header u1_h2 instanceof Header2 +header u1_h3 instanceof Header1 +header u2_h1 instanceof Header1 +header u2_h2 instanceof Header2 +header u2_h3 instanceof Header1 +header MainControlT_u1_0_h1 instanceof Header1 +header MainControlT_u1_0_h2 instanceof Header2 +header MainControlT_u1_0_h3 instanceof Header1 +header MainControlT_u2_0_h1 instanceof Header1 +header MainControlT_u2_0_h2 instanceof Header2 +header MainControlT_u2_0_h3 instanceof Header1 + +struct M { + bit<32> pna_main_input_metadata_input_port + bit<32> pna_main_output_metadata_output_port + bit<8> MainParserT_parser_tmp +} +metadata instanceof M + +regarray direction size 0x100 initval 0 +apply { + rx m.pna_main_input_metadata_input_port + validate h.u1_h1 + invalidate h.u1_h2 + invalidate h.u1_h3 + extract h.u1_h3 + validate h.u1_h3 + mov h.u1_h3.data 0x1 + invalidate h.u1_h1 + invalidate h.u1_h2 + mov m.MainParserT_parser_tmp 1 + jmpv LABEL_END h.u1_h3 + mov m.MainParserT_parser_tmp 0 + LABEL_END : jmpeq PARSERIMPL_START_TRUE m.MainParserT_parser_tmp 1 + invalidate h.u1_h1 + jmp PARSERIMPL_START_JOIN + PARSERIMPL_START_TRUE : validate h.u1_h1 + jmpnv LABEL_FALSE h.u1_h3 + validate h.u1_h1 + jmp LABEL_END_0 + LABEL_FALSE : invalidate h.u1_h1 + LABEL_END_0 : mov h.u1_h1.data h.u1_h3.data + invalidate h.u1_h2 + invalidate h.u1_h3 + PARSERIMPL_START_JOIN : validate h.u1_h1 + mov h.u1_h1.data 0x1 + invalidate h.u1_h2 + invalidate h.u1_h3 + validate h.u1_h1 + mov h.u1_h1.data 0x1 + invalidate h.u1_h2 + invalidate h.u1_h3 + validate h.u1_h2 + mov h.u1_h2.data 0x1 + invalidate h.u1_h1 + invalidate h.u1_h3 + validate h.u1_h3 + mov h.u1_h3.data 0x1 + invalidate h.u1_h1 + invalidate h.u1_h2 + invalidate h.MainControlT_u1_0_h1 + invalidate h.MainControlT_u1_0_h2 + invalidate h.MainControlT_u1_0_h3 + invalidate h.MainControlT_u2_0_h1 + invalidate h.MainControlT_u2_0_h2 + invalidate h.MainControlT_u2_0_h3 + validate h.MainControlT_u1_0_h1 + invalidate h.MainControlT_u1_0_h2 + invalidate h.MainControlT_u1_0_h3 + validate h.MainControlT_u1_0_h1 + mov h.MainControlT_u1_0_h1.data 0x1 + invalidate h.MainControlT_u1_0_h2 + invalidate h.MainControlT_u1_0_h3 + validate h.MainControlT_u1_0_h2 + invalidate h.MainControlT_u1_0_h1 + invalidate h.MainControlT_u1_0_h3 + validate h.MainControlT_u2_0_h2 + mov h.MainControlT_u2_0_h2.data 0x1 + invalidate h.MainControlT_u2_0_h1 + invalidate h.MainControlT_u2_0_h3 + validate h.MainControlT_u1_0_h2 + invalidate h.MainControlT_u1_0_h1 + invalidate h.MainControlT_u1_0_h3 + invalidate h.MainControlT_u1_0_h3 + tx m.pna_main_output_metadata_output_port +} + + diff --git a/testdata/p4_16_samples_outputs/pna-dpdk-invalid-hdr-warnings6-midend.p4 b/testdata/p4_16_samples_outputs/pna-dpdk-invalid-hdr-warnings6-midend.p4 index 8d553381196..808adc42751 100644 --- a/testdata/p4_16_samples_outputs/pna-dpdk-invalid-hdr-warnings6-midend.p4 +++ b/testdata/p4_16_samples_outputs/pna-dpdk-invalid-hdr-warnings6-midend.p4 @@ -104,30 +104,7 @@ control ingress(inout H hdr, inout M meta, in pna_main_input_metadata_t istd, in u_1[0].h2.setValid(); u_1[0].h1.setInvalid(); u_1[0].h3.setInvalid(); - if (u_1[1w0].h1.isValid()) { - u_1[1].h1.setValid(); - u_1[1].h1 = u_1[1w0].h1; - u_1[1].h2.setInvalid(); - u_1[1].h3.setInvalid(); - } else { - u_1[1].h1.setInvalid(); - } - if (u_1[1w0].h2.isValid()) { - u_1[1].h2.setValid(); - u_1[1].h2 = u_1[1w0].h2; - u_1[1].h1.setInvalid(); - u_1[1].h3.setInvalid(); - } else { - u_1[1].h2.setInvalid(); - } - if (u_1[1w0].h3.isValid()) { - u_1[1].h3.setValid(); - u_1[1].h3 = u_1[1w0].h3; - u_1[1].h1.setInvalid(); - u_1[1].h2.setInvalid(); - } else { - u_1[1].h3.setInvalid(); - } + u_1[1] = u_1[1w0]; u_1[1].h2.setValid(); u_1[1].h2.data = 16w1; u_1[1].h1.setInvalid();