diff --git a/CMakeLists.txt b/CMakeLists.txt index 00536c733fa..cb89acdf83a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -71,6 +71,9 @@ OPTION (ENABLE_IWYU "Enable checking includes with IWYU" OFF) # Support a legacy option. TODO: Remove? OPTION (ENABLE_UNIFIED_COMPILATION "Enable CMAKE_UNITY_BUILD" OFF) +# Enable DumpPipe pass output +OPTION(ENABLE_DUMP_PIPE "Enable DumpPipe pass output" ON) + set (P4C_DRIVER_NAME "p4c" CACHE STRING "Customize the name of the driver script") set(MAX_LOGGING_LEVEL 10 CACHE STRING "Control the maximum logging level for -T logs") @@ -189,6 +192,11 @@ if (BUILD_AUTO_VAR_INIT_PATTERN) add_cxx_compiler_option ("-ftrivial-auto-var-init=pattern") endif () +# Enable DumpPipe pass output +if (ENABLE_DUMP_PIPE) + add_definitions("-DENABLE_DUMP_PIPE=1") +endif () + # Required tools and libraries. find_package (PythonInterp 3 REQUIRED) find_package (FLEX 2.0 REQUIRED) diff --git a/backends/p4test/midend.cpp b/backends/p4test/midend.cpp index 8c34a5d6f61..92434a3dd48 100644 --- a/backends/p4test/midend.cpp +++ b/backends/p4test/midend.cpp @@ -28,6 +28,7 @@ limitations under the License. #include "frontends/p4/typeChecking/typeChecker.h" #include "frontends/p4/typeMap.h" #include "frontends/p4/unusedDeclarations.h" +#include "ir/dump.h" #include "midend/actionSynthesis.h" #include "midend/compileTimeOps.h" #include "midend/complexComparison.h" @@ -87,7 +88,8 @@ MidEnd::MidEnd(P4TestOptions &options, std::ostream *outStream) { auto defUse = new P4::ComputeDefUse; addPasses( - {options.ndebug ? new P4::RemoveAssertAssume(&typeMap) : nullptr, + {new P4::DumpPipe("MidEnd start"), + options.ndebug ? new P4::RemoveAssertAssume(&typeMap) : nullptr, new P4::RemoveMiss(&typeMap), new P4::EliminateNewtype(&typeMap), new P4::EliminateInvalidHeaders(&typeMap), @@ -171,7 +173,8 @@ MidEnd::MidEnd(P4TestOptions &options, std::ostream *outStream) { [this, evaluator]() { toplevel = evaluator->getToplevelBlock(); }, new P4::FlattenHeaderUnion(&refMap, &typeMap, options.loopsUnrolling), new P4::SimplifyControlFlow(&typeMap, true), - new P4::MidEndLast()}); + new P4::MidEndLast(), + new P4::DumpPipe("MidEnd end")}); if (options.listMidendPasses) { listPasses(*outStream, cstring::newline); *outStream << std::endl; diff --git a/backends/tofino/bf-p4c/CMakeLists.txt b/backends/tofino/bf-p4c/CMakeLists.txt index 412b5f3818b..24c05933ecc 100644 --- a/backends/tofino/bf-p4c/CMakeLists.txt +++ b/backends/tofino/bf-p4c/CMakeLists.txt @@ -150,7 +150,6 @@ set (BF_P4C_BACKEND_COMMON_SRCS common/check_field_corruption.cpp common/check_for_unimplemented_features.cpp common/check_uninitialized_read.cpp - common/dump_pipe.cpp common/pragma/collect_global_pragma.cpp common/elim_unused.cpp common/empty_tableseq.cpp diff --git a/backends/tofino/bf-p4c/backend.cpp b/backends/tofino/bf-p4c/backend.cpp index b6827fe75a4..29f0617ab5f 100644 --- a/backends/tofino/bf-p4c/backend.cpp +++ b/backends/tofino/bf-p4c/backend.cpp @@ -128,6 +128,7 @@ #include "backends/tofino/bf-p4c/phv/split_padding.h" #include "backends/tofino/bf-p4c/phv/utils/slice_alloc.h" #include "backends/tofino/bf-p4c/phv/v2/metadata_initialization.h" +#include "ir/dump.h" #include "ir/pass_manager.h" #include "lib/indent.h" @@ -212,7 +213,7 @@ Backend::Backend(const BFN_Options &o, int pipe_id) : nullptr; addPasses({ - new DumpPipe("Initial table graph"), + new P4::DumpPipe("Initial table graph"), flexibleLogging, LOGGING(4) ? new DumpParser("begin_backend") : nullptr, new AdjustByteCountSetup, @@ -258,7 +259,7 @@ Backend::Backend(const BFN_Options &o, int pipe_id) new CollectPhvInfo(phv), new GatherReductionOrReqs(deps.red_info), new InstructionSelection(options, phv, deps.red_info), - new DumpPipe("After InstructionSelection"), + new P4::DumpPipe("After InstructionSelection"), new FindDependencyGraph(phv, deps, &options, "program_graph"_cs, "After Instruction Selection"_cs), options.decaf ? &decaf : nullptr, @@ -274,7 +275,7 @@ Backend::Backend(const BFN_Options &o, int pipe_id) new AutoAlias(phv, *pragmaAlias, *noOverlay), new Alias(phv, *pragmaAlias), new CollectPhvInfo(phv), - new DumpPipe("After Alias"), + new P4::DumpPipe("After Alias"), // This is the backtracking point from table placement to PHV allocation. Based on a // container conflict-free PHV allocation, we generate a number of no-pack conflicts between // fields (these are fields written in different nonmutually exclusive actions in the same @@ -283,7 +284,7 @@ Backend::Backend(const BFN_Options &o, int pipe_id) // metadata packing. &mau_backtracker, new ResolveSizeOfOperator(), - new DumpPipe("After ResolveSizeOfOperator"), + new P4::DumpPipe("After ResolveSizeOfOperator"), // Run after bridged metadata packing as bridged packing updates the parser state. new CollectPhvInfo(phv), new ParserCopyProp(phv), @@ -352,7 +353,7 @@ Backend::Backend(const BFN_Options &o, int pipe_id) // with table info // Trivial PHV alloc => table alloc ==================> PHV alloc ===> redo table. new AddInitsInMAU(phv, mauInitFields, false), - new DumpPipe("Before phv_analysis"), + new P4::DumpPipe("Before phv_analysis"), new DumpTableFlowGraph(phv), options.alt_phv_alloc ? new PassManager({ @@ -444,7 +445,7 @@ Backend::Backend(const BFN_Options &o, int pipe_id) // tables to be split across stages. new GeneratePrimitiveInfo(phv, primNode), &table_alloc, - new DumpPipe("After TableAlloc"), + new P4::DumpPipe("After TableAlloc"), &table_summary, // Rerun defuse analysis here so that table placements are used to correctly calculate live // ranges output in the assembly. @@ -461,7 +462,7 @@ Backend::Backend(const BFN_Options &o, int pipe_id) : nullptr, new InstructionAdjustment(phv, deps.red_info), &nextTblProp, // Must be run after all modifications to the table graph have finished! - new DumpPipe("Final table graph"), + new P4::DumpPipe("Final table graph"), new CheckFieldCorruption(defuse, phv, PHV_Analysis->get_pragmas()), new AdjustExtract(phv), phvLoggingDefUseInfo, diff --git a/backends/tofino/bf-p4c/common/dump_pipe.cpp b/backends/tofino/bf-p4c/common/dump_pipe.cpp deleted file mode 100644 index 02c04596ea7..00000000000 --- a/backends/tofino/bf-p4c/common/dump_pipe.cpp +++ /dev/null @@ -1,46 +0,0 @@ -/** - * Copyright (C) 2024 Intel Corporation - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy - * of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - * - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#include - -#include "ir/dump.h" -#include "lib/gc.h" -#include "utils.h" - -using namespace P4; - -#if BAREFOOT_INTERNAL -bool DumpPipe::preorder(const IR::Node *pipe) { - if (LOGGING(1)) { - if (heading) { - LOG1("-------------------------------------------------"); - LOG1(heading); - LOG1("-------------------------------------------------"); - size_t maxMem = 0; - size_t memUsed = gc_mem_inuse(&maxMem) / (1024 * 1024); - maxMem = maxMem / (1024 * 1024); - LOG1("*** mem in use = " << memUsed << "MB, heap size = " << maxMem << "MB"); - } - if (LOGGING(2)) - dump(::Log::Detail::fileLogOutput(__FILE__), pipe); - else - LOG1(*pipe); - } - return false; -} -#endif // BAREFOOT_INTERNAL diff --git a/backends/tofino/bf-p4c/common/utils.h b/backends/tofino/bf-p4c/common/utils.h index 07544d82bd7..fa038a990e7 100644 --- a/backends/tofino/bf-p4c/common/utils.h +++ b/backends/tofino/bf-p4c/common/utils.h @@ -36,15 +36,6 @@ using namespace P4; -struct DumpPipe : public Inspector { - const char *heading; - DumpPipe() : heading(nullptr) {} - explicit DumpPipe(const char *h) : heading(h) {} -#if BAREFOOT_INTERNAL - bool preorder(const IR::Node *pipe) override; -#endif // BAREFOOT_INTERNAL -}; - /// Used to end the compiler after a fatal error is raised. Usually, this just trows an exception /// \ref Util::CompilationError. In internal builds when a program contains "expect error" and all /// the errors are expected (including the fatal one) then this function just exits (calls diff --git a/backends/tofino/bf-p4c/mau/mau_alloc.cpp b/backends/tofino/bf-p4c/mau/mau_alloc.cpp index 1f168511d3c..7df57778d1e 100644 --- a/backends/tofino/bf-p4c/mau/mau_alloc.cpp +++ b/backends/tofino/bf-p4c/mau/mau_alloc.cpp @@ -30,6 +30,7 @@ #include "backends/tofino/bf-p4c/mau/table_placement.h" #include "backends/tofino/bf-p4c/mau/table_seqdeps.h" #include "backends/tofino/bf-p4c/phv/mau_backtracker.h" +#include "ir/dump.h" int TableAllocPass::table_placement_round = 1; @@ -39,27 +40,38 @@ TableAllocPass::TableAllocPass(const BFN_Options &options, PhvInfo &phv, Depende : Logging::PassManager("table_placement_"_cs), att_info(phv), options(options) { lc = LayoutChoices::create(phv, deps.red_info, att_info); siaa = new SharedIndirectAttachedAnalysis(mutex, ignore, action_mutex, *lc); - addPasses( - {new GatewayOpt(phv), // must be before TableLayout? or just TablePlacement? - new TableLayout(phv, *lc, att_info), // catches IXBar::realign backtracks - new AssignActionHandle(phv), new MeterALU::Format(phv, *lc), - new TableFindSeqDependencies(phv), new FindDependencyGraph(phv, deps), - new SpreadGatewayAcrossSeq(phv), new CheckTableNameDuplicate, - new TableFindSeqDependencies(phv), new CheckTableNameDuplicate, - new FindDependencyGraph(phv, deps, &options, ""_cs, "Before Table Placement"_cs, &summary), - new DumpJsonGraph(deps, jsonGraph, "Before Table Placement"_cs, false), &ignore, &mutex, - &action_mutex, siaa, new DumpPipe("Before TablePlacement"), - new TablePlacement(options, deps, mutex, phv, *lc, *siaa, att_info, summary, - mau_backtracker), - new DumpPipe("After TablePlacement"), - new FindDependencyGraph(phv, deps, &options, ""_cs, "After Table Placement"_cs, &summary), - new TableDependencyGraphSummary(deps), new CheckTableNameDuplicate, - new TableFindSeqDependencies(phv), // not needed? - new AssignCounterLRTValues(), new CheckTableNameDuplicate, new AdjustIXBarExpression, - // RemoveNoopGateway can be removed after MultipleApply2 is in - new PassIf( - [&options] { return !Device::hasLongBranches() || options.disable_long_branch; }, - {new RemoveNoopGateway})}); + addPasses({ + new GatewayOpt(phv), // must be before TableLayout? or just TablePlacement? + new TableLayout(phv, *lc, att_info), // catches IXBar::realign backtracks + new AssignActionHandle(phv), + new MeterALU::Format(phv, *lc), + new TableFindSeqDependencies(phv), + new FindDependencyGraph(phv, deps), + new SpreadGatewayAcrossSeq(phv), + new CheckTableNameDuplicate, + new TableFindSeqDependencies(phv), + new CheckTableNameDuplicate, + new FindDependencyGraph(phv, deps, &options, ""_cs, "Before Table Placement"_cs, &summary), + new DumpJsonGraph(deps, jsonGraph, "Before Table Placement"_cs, false), + &ignore, + &mutex, + &action_mutex, + siaa, + new P4::DumpPipe("Before TablePlacement"), + new TablePlacement(options, deps, mutex, phv, *lc, *siaa, att_info, summary, + mau_backtracker), + new P4::DumpPipe("After TablePlacement"), + new FindDependencyGraph(phv, deps, &options, ""_cs, "After Table Placement"_cs, &summary), + new TableDependencyGraphSummary(deps), + new CheckTableNameDuplicate, + new TableFindSeqDependencies(phv), // not needed? + new AssignCounterLRTValues(), + new CheckTableNameDuplicate, + new AdjustIXBarExpression, + // RemoveNoopGateway can be removed after MultipleApply2 is in + new PassIf([&options] { return !Device::hasLongBranches() || options.disable_long_branch; }, + {new RemoveNoopGateway}), + }); setName("Table Alloc"); } diff --git a/backends/tofino/bf-p4c/phv/v2/metadata_initialization.cpp b/backends/tofino/bf-p4c/phv/v2/metadata_initialization.cpp index ae9953d357a..e230bb2617c 100644 --- a/backends/tofino/bf-p4c/phv/v2/metadata_initialization.cpp +++ b/backends/tofino/bf-p4c/phv/v2/metadata_initialization.cpp @@ -18,6 +18,8 @@ #include "metadata_initialization.h" +#include "ir/dump.h" + namespace { /** A helper class that maps phv::Field to a IR::Expression. @@ -794,7 +796,16 @@ PHV::v2::MetadataInitialization::MetadataInitialization(MauBacktracker &backtrac collect_pov_protected_field->pov_protected_fields); auto *apply_init_insert = new ApplyMetadataInitialization(*gen_init_plans, *field_to_expr, *table_write_info); - addPasses({new DumpPipe("before v2 metadata initialization"), collect_pov_protected_field, - pa_no_init, mutex, tfg_builder, field_to_expr, table_write_info, gen_init_plans, - apply_init_insert, new DumpPipe("after v2 metadata initialization")}); + addPasses({ + new P4::DumpPipe("before v2 metadata initialization"), + collect_pov_protected_field, + pa_no_init, + mutex, + tfg_builder, + field_to_expr, + table_write_info, + gen_init_plans, + apply_init_insert, + new P4::DumpPipe("after v2 metadata initialization"), + }); } diff --git a/backends/tofino/scripts/package_p4c_for_tofino.sh b/backends/tofino/scripts/package_p4c_for_tofino.sh index a1d8097442c..0f9d88a5622 100755 --- a/backends/tofino/scripts/package_p4c_for_tofino.sh +++ b/backends/tofino/scripts/package_p4c_for_tofino.sh @@ -14,7 +14,7 @@ else fi install_prefix=/usr/local -barefoot_internal="-DENABLE_BAREFOOT_INTERNAL=OFF" +barefoot_internal="-DENABLE_BAREFOOT_INTERNAL=OFF -DENABLE_DUMP_PIPE=OFF" enable_cb="-DENABLE_CLOUDBREAK=OFF" enable_fr="-DENABLE_FLATROCK=OFF" pgo=false diff --git a/backends/tofino/scripts/package_sources.sh b/backends/tofino/scripts/package_sources.sh index e9497bf15ae..dad19a1ff7e 100755 --- a/backends/tofino/scripts/package_sources.sh +++ b/backends/tofino/scripts/package_sources.sh @@ -17,7 +17,7 @@ else fi install_prefix=/usr/local -barefoot_internal="-DENABLE_BAREFOOT_INTERNAL=OFF" +barefoot_internal="-DENABLE_BAREFOOT_INTERNAL=OFF -DENABLE_DUMP_PIPE=OFF" pgo=false lto=false diff --git a/ir/dump.cpp b/ir/dump.cpp index ea37fb03fce..db224305b7e 100644 --- a/ir/dump.cpp +++ b/ir/dump.cpp @@ -22,6 +22,7 @@ limitations under the License. #include "ir/node.h" #include "ir/visitor.h" #include "lib/cstring.h" +#include "lib/gc.h" #include "lib/indent.h" #include "lib/source_file.h" @@ -138,4 +139,25 @@ std::string dumpToString(const IR::Node *n) { return str.str(); } +bool DumpPipe::preorder(const IR::Node *pipe) { +#if ENABLE_DUMP_PIPE + if (LOGGING(1)) { + if (heading) { + LOG1("-------------------------------------------------"); + LOG1(heading); + LOG1("-------------------------------------------------"); + size_t maxMem = 0; + size_t memUsed = gc_mem_inuse(&maxMem) / (1024 * 1024); + maxMem = maxMem / (1024 * 1024); + LOG1("*** mem in use = " << memUsed << "MB, heap size = " << maxMem << "MB"); + } + if (LOGGING(2)) + dump(Log::Detail::fileLogOutput(__FILE__), pipe); + else + LOG1(*pipe); + } +#endif // ENABLE_DUMP_PIPE + return false; +} + } // namespace P4 diff --git a/ir/dump.h b/ir/dump.h index 792b9f80d83..ac6bc939e73 100644 --- a/ir/dump.h +++ b/ir/dump.h @@ -69,6 +69,13 @@ inline std::ostream &operator<<(std::ostream &out, const Dump &d) { return out; } +struct DumpPipe : public Inspector { + const char *heading; + DumpPipe() : heading(nullptr) {} + explicit DumpPipe(const char *h) : heading(h) {} + bool preorder(const IR::Node *pipe) override; +}; + } // namespace P4 #endif /* IR_DUMP_H_ */