diff --git a/BUILD.bazel b/BUILD.bazel index 61aa22c986..d519afbf2b 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -239,6 +239,8 @@ cc_library( "@com_github_p4lang_p4runtime//:p4runtime_cc_proto", "@com_github_p4lang_p4runtime//:p4types_cc_proto", "@com_google_absl//absl/container:flat_hash_map", + "@com_google_absl//absl/container:flat_hash_set", + "@com_google_absl//absl/container:inlined_vector", "@com_google_protobuf//:protobuf", ], ) diff --git a/CMakeLists.txt b/CMakeLists.txt index cb4e69fd76..48ff15c570 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -203,15 +203,17 @@ endif() # we require -pthread to make std::call_once work, even if we're not using threads... set(THREADS_PREFER_PTHREAD_FLAG ON) find_package(Threads REQUIRED) -set (P4C_LIB_DEPS "${P4C_LIB_DEPS};${CMAKE_THREAD_LIBS_INIT}") +list (APPEND P4C_LIB_DEPS ${CMAKE_THREAD_LIBS_INIT}) include_directories(SYSTEM ${Boost_INCLUDE_DIRS}) include_directories(SYSTEM ${PROTOBUF_INCLUDE_DIRS}) include_directories(SYSTEM ${LIBGC_INCLUDE_DIR}) set (HAVE_LIBBOOST_IOSTREAMS 1) -set (P4C_LIB_DEPS "${P4C_LIB_DEPS};${Boost_LIBRARIES}") +list (APPEND P4C_LIB_DEPS ${Boost_LIBRARIES}) if (ENABLE_GC) - set (P4C_LIB_DEPS "${P4C_LIB_DEPS};${LIBGC_LIBRARIES}") + list (APPEND P4C_LIB_DEPS ${LIBGC_LIBRARIES}) endif () +set (P4C_ABSL_LIBRARIES absl::flat_hash_map absl::flat_hash_set) +list (APPEND P4C_LIB_DEPS ${P4C_ABSL_LIBRARIES}) # other required libraries p4c_add_library (rt clock_gettime HAVE_CLOCK_GETTIME) @@ -507,6 +509,7 @@ add_custom_target(genIR DEPENDS ${IR_GENERATED_SRCS}) set_source_files_properties(${IR_GENERATOR} PROPERTIES GENERATED TRUE) add_library(ir-generated OBJECT ${IR_GENERATED_SRCS} ${EXTENSION_IR_SOURCES}) add_dependencies(ir-generated ir genIR) +target_link_libraries(ir-generated ${P4C_LIB_DEPS}) ######################################## IR Generation End ######################################## diff --git a/backends/bmv2/CMakeLists.txt b/backends/bmv2/CMakeLists.txt index 034e2a095c..33f3e376a1 100644 --- a/backends/bmv2/CMakeLists.txt +++ b/backends/bmv2/CMakeLists.txt @@ -83,7 +83,7 @@ set (BMV2_BACKEND_COMMON_HDRS set (IR_DEF_FILES ${IR_DEF_FILES} ${CMAKE_CURRENT_SOURCE_DIR}/bmv2.def PARENT_SCOPE) add_library(bmv2backend STATIC ${BMV2_BACKEND_COMMON_SRCS}) -add_dependencies(bmv2backend ir-generated frontend) +target_link_libraries(bmv2backend ir-generated frontend) add_executable(p4c-bm2-ss ${BMV2_SIMPLE_SWITCH_SRCS}) target_link_libraries (p4c-bm2-ss bmv2backend ${P4C_LIBRARIES} ${P4C_LIB_DEPS}) diff --git a/backends/dpdk/CMakeLists.txt b/backends/dpdk/CMakeLists.txt index 560559ad4c..6331e9a42a 100644 --- a/backends/dpdk/CMakeLists.txt +++ b/backends/dpdk/CMakeLists.txt @@ -99,8 +99,7 @@ endforeach() set(EXTENSION_IR_SOURCES ${EXTENSION_IR_SOURCES} ${QUAL_DPDK_IR_SRCS} PARENT_SCOPE) add_executable(p4c-dpdk ${P4C_DPDK_SOURCES}) -target_link_libraries (p4c-dpdk dpdk_runtime ${P4C_LIBRARIES} ${P4C_LIB_DEPS}) -add_dependencies(p4c-dpdk dpdk_runtime frontend) +target_link_libraries (p4c-dpdk dpdk_runtime frontend ${P4C_LIBRARIES} ${P4C_LIB_DEPS}) install (TARGETS p4c-dpdk RUNTIME DESTINATION ${P4C_RUNTIME_OUTPUT_DIRECTORY}) diff --git a/backends/graphs/CMakeLists.txt b/backends/graphs/CMakeLists.txt index 1e1e7c52f5..2d05d43d59 100644 --- a/backends/graphs/CMakeLists.txt +++ b/backends/graphs/CMakeLists.txt @@ -30,11 +30,10 @@ set (GRAPHS_HDRS ) add_library(p4cgraphs STATIC ${GRAPHS_SRCS} ${EXTENSION_P4_14_CONV_SOURCES}) -add_dependencies(p4cgraphs ir-generated frontend) +target_link_libraries(p4cgraphs ir-generated frontend) add_executable(p4c-graphs p4c-graphs.cpp) -target_link_libraries (p4c-graphs p4cgraphs ${P4C_LIBRARIES} ${P4C_LIB_DEPS}) -add_dependencies(p4c-graphs ir-generated frontend) +target_link_libraries (p4c-graphs p4cgraphs ir-generated frontend ${P4C_LIBRARIES} ${P4C_LIB_DEPS}) install (TARGETS p4c-graphs RUNTIME DESTINATION ${P4C_RUNTIME_OUTPUT_DIRECTORY}) diff --git a/backends/p4tools/common/CMakeLists.txt b/backends/p4tools/common/CMakeLists.txt index c7181cf1d8..2e611b42f9 100644 --- a/backends/p4tools/common/CMakeLists.txt +++ b/backends/p4tools/common/CMakeLists.txt @@ -41,6 +41,7 @@ add_p4tools_library(p4tools-common ${P4C_TOOLS_COMMON_SOURCES}) target_link_libraries( p4tools-common PUBLIC ${P4TOOLS_Z3_LIB} + PRIVATE frontend ) target_include_directories( @@ -54,9 +55,5 @@ target_include_directories( PUBLIC "${P4C_BINARY_DIR}" ) -add_dependencies(p4tools-common ir-generated frontend) - # Add control-plane-specific extensions. add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/control_plane) - - diff --git a/frontends/CMakeLists.txt b/frontends/CMakeLists.txt index c2704d2461..e23c1d0afa 100644 --- a/frontends/CMakeLists.txt +++ b/frontends/CMakeLists.txt @@ -259,4 +259,6 @@ if (FLEX_INCLUDE_DIRS) endif () add_library (frontend STATIC ${FRONTEND_SOURCES}) add_dependencies (frontend frontend-parser-gen) -target_link_libraries (frontend frontend-parser-gen) +target_link_libraries (frontend frontend-parser-gen + absl::flat_hash_set + absl::flat_hash_map) diff --git a/frontends/common/resolveReferences/referenceMap.cpp b/frontends/common/resolveReferences/referenceMap.cpp index 57e9dbf80c..68469ce9b3 100644 --- a/frontends/common/resolveReferences/referenceMap.cpp +++ b/frontends/common/resolveReferences/referenceMap.cpp @@ -35,7 +35,7 @@ void ReferenceMap::clear() { program = nullptr; used.clear(); thisToDeclaration.clear(); - for (auto &reserved : P4::reservedWords) usedNames.insert({reserved, 0}); + for (auto &reserved : P4::reservedWords) usedNames.emplace(reserved, 0); ProgramMap::clear(); } @@ -43,7 +43,7 @@ void ReferenceMap::setDeclaration(const IR::Path *path, const IR::IDeclaration * CHECK_NULL(path); CHECK_NULL(decl); LOG3("Resolved " << dbp(path) << " to " << dbp(decl)); - auto previous = get(pathToDeclaration, path); + const auto *previous = get(pathToDeclaration, path); if (previous != nullptr && previous != decl) BUG("%1% already resolved to %2% instead of %3%", dbp(path), dbp(previous), dbp(decl->getNode())); @@ -56,7 +56,7 @@ void ReferenceMap::setDeclaration(const IR::This *pointer, const IR::IDeclaratio CHECK_NULL(pointer); CHECK_NULL(decl); LOG3("Resolved " << dbp(pointer) << " to " << dbp(decl)); - auto previous = get(thisToDeclaration, pointer); + const auto *previous = get(thisToDeclaration, pointer); if (previous != nullptr && previous != decl) BUG("%1% already resolved to %2% instead of %3%", dbp(pointer), dbp(previous), dbp(decl)); thisToDeclaration.emplace(pointer, decl); @@ -64,7 +64,7 @@ void ReferenceMap::setDeclaration(const IR::This *pointer, const IR::IDeclaratio const IR::IDeclaration *ReferenceMap::getDeclaration(const IR::This *pointer, bool notNull) const { CHECK_NULL(pointer); - auto result = get(thisToDeclaration, pointer); + const auto *result = get(thisToDeclaration, pointer); if (result) LOG3("Looking up " << dbp(pointer) << " found " << dbp(result)); @@ -77,7 +77,7 @@ const IR::IDeclaration *ReferenceMap::getDeclaration(const IR::This *pointer, bo const IR::IDeclaration *ReferenceMap::getDeclaration(const IR::Path *path, bool notNull) const { CHECK_NULL(path); - auto result = get(pathToDeclaration, path); + const auto *result = get(pathToDeclaration, path); if (result) LOG3("Looking up " << dbp(path) << " found " << dbp(result)); @@ -108,7 +108,7 @@ cstring ReferenceMap::newName(cstring base) { cstring name = base; if (usedNames.count(name)) name = cstring::make_unique(usedNames, name, usedNames[base], '_'); - usedNames.insert({name, 0}); + usedNames.emplace(name, 0); return name; } @@ -127,7 +127,7 @@ cstring MinimalNameGenerator::newName(cstring base) { cstring name = base; if (usedNames.count(name)) name = cstring::make_unique(usedNames, name, usedNames[base], '_'); - usedNames.insert({name, 0}); + usedNames.emplace(name, 0); return name; } diff --git a/frontends/common/resolveReferences/referenceMap.h b/frontends/common/resolveReferences/referenceMap.h index 32cc951c31..c07e2afeab 100644 --- a/frontends/common/resolveReferences/referenceMap.h +++ b/frontends/common/resolveReferences/referenceMap.h @@ -17,11 +17,12 @@ limitations under the License. #ifndef COMMON_RESOLVEREFERENCES_REFERENCEMAP_H_ #define COMMON_RESOLVEREFERENCES_REFERENCEMAP_H_ +#include "absl/container/flat_hash_map.h" +#include "absl/container/flat_hash_set.h" #include "frontends/common/programMap.h" #include "ir/ir.h" #include "ir/visitor.h" #include "lib/cstring.h" -#include "lib/map.h" namespace P4 { @@ -34,7 +35,7 @@ class NameGenerator { class MinimalNameGenerator : public NameGenerator, public Inspector { /// All names used in the program. Key is a name, value represents how many times /// this name was used as a base for newly generated unique names. - std::unordered_map usedNames; + absl::flat_hash_map usedNames; void postorder(const IR::Path *p) override { usedName(p->name.name); } void postorder(const IR::Type_Declaration *t) override { usedName(t->name.name); } void postorder(const IR::Declaration *d) override { usedName(d->name.name); } @@ -67,17 +68,17 @@ class ReferenceMap final : public ProgramMap, public NameGenerator, public Decla bool isv1; /// Maps paths in the program to declarations. - ordered_map pathToDeclaration; + absl::flat_hash_map pathToDeclaration; /// Set containing all declarations in the program. - std::set used; + absl::flat_hash_set used; /// Map from `This` to declarations (an experimental feature). - std::map thisToDeclaration; + absl::flat_hash_map thisToDeclaration; /// All names used in the program. Key is a name, value represents how many times /// this name was used as a base for newly generated unique names. - std::unordered_map usedNames; + absl::flat_hash_map usedNames; public: ReferenceMap(); @@ -116,7 +117,7 @@ class ReferenceMap final : public ProgramMap, public NameGenerator, public Decla bool isUsed(const IR::IDeclaration *decl) const { return used.count(decl) > 0; } /// Indicate that @p name is used in the program. - void usedName(cstring name) { usedNames.insert({name, 0}); } + void usedName(cstring name) { usedNames.emplace(name, 0); } }; } // namespace P4 diff --git a/frontends/common/resolveReferences/resolveReferences.h b/frontends/common/resolveReferences/resolveReferences.h index 81bca2b828..55ece6e512 100644 --- a/frontends/common/resolveReferences/resolveReferences.h +++ b/frontends/common/resolveReferences/resolveReferences.h @@ -17,9 +17,9 @@ limitations under the License. #ifndef COMMON_RESOLVEREFERENCES_RESOLVEREFERENCES_H_ #define COMMON_RESOLVEREFERENCES_RESOLVEREFERENCES_H_ +#include "absl/container/flat_hash_map.h" #include "ir/ir.h" #include "lib/cstring.h" -#include "lib/exceptions.h" #include "lib/iterator_range.h" #include "referenceMap.h" @@ -41,10 +41,12 @@ class ResolutionContext : virtual public Visitor, public DeclarationLookup { std::unordered_multimap &memoizeDeclsByName( const IR::INamespace *ns) const; - mutable std::unordered_map> + mutable absl::flat_hash_map, + Util::Hash> namespaceDecls; - mutable std::unordered_map> + mutable absl::flat_hash_map, + Util::Hash> namespaceDeclNames; protected: diff --git a/frontends/p4/def_use.cpp b/frontends/p4/def_use.cpp index 3a714d52b4..efab7c5f16 100644 --- a/frontends/p4/def_use.cpp +++ b/frontends/p4/def_use.cpp @@ -253,14 +253,22 @@ bool LocationSet::overlaps(const LocationSet *other) const { return false; } +void ProgramPoints::add(const ProgramPoints *from) { + points.insert(from->points.begin(), from->points.end()); +} + const ProgramPoints *ProgramPoints::merge(const ProgramPoints *with) const { - auto result = new ProgramPoints(points); - for (auto p : with->points) result->points.emplace(p); + auto *result = new ProgramPoints(points); + result->points.insert(with->points.begin(), with->points.end()); return result; } ProgramPoint::ProgramPoint(const ProgramPoint &context, const IR::Node *node) { - for (auto e : context.stack) stack.push_back(e); + assign(context, node); +} + +void ProgramPoint::assign(const ProgramPoint &context, const IR::Node *node) { + stack.assign(context.stack.begin(), context.stack.end()); stack.push_back(node); } @@ -325,10 +333,10 @@ void Definitions::removeLocation(const StorageLocation *location) { } const ProgramPoints *Definitions::getPoints(const LocationSet *locations) const { - const ProgramPoints *result = new ProgramPoints(); - for (auto sl : *locations->canonicalize()) { - auto points = getPoints(sl->to()); - result = result->merge(points); + ProgramPoints *result = new ProgramPoints(); + for (const auto *sl : *locations->canonicalize()) { + const auto *points = getPoints(sl->to()); + result->add(points); } return result; } diff --git a/frontends/p4/def_use.h b/frontends/p4/def_use.h index 3aab517deb..8b39e4d969 100644 --- a/frontends/p4/def_use.h +++ b/frontends/p4/def_use.h @@ -17,14 +17,15 @@ limitations under the License. #ifndef FRONTENDS_P4_DEF_USE_H_ #define FRONTENDS_P4_DEF_USE_H_ -#include // IWYU pragma: keep -#include - -#include "frontends/p4/typeChecking/typeChecker.h" +#include "absl/container/flat_hash_set.h" +#include "absl/container/inlined_vector.h" +#include "frontends/common/resolveReferences/referenceMap.h" #include "ir/ir.h" #include "lib/alloc_trace.h" +#include "lib/hash.h" #include "lib/hvec_map.h" #include "lib/ordered_set.h" +#include "typeMap.h" namespace P4 { @@ -129,7 +130,7 @@ class StructLocation : public WithFieldsLocation { /// Interface for locations that support an index operation class IndexedLocation : public StorageLocation { protected: - std::vector elements; + absl::InlinedVector elements; friend class StorageFactory; void createElement(unsigned index, StorageLocation *element) { @@ -145,8 +146,8 @@ class IndexedLocation : public StorageLocation { elements.resize(it->getSize()); } void addElement(unsigned index, LocationSet *result) const; - std::vector::const_iterator begin() const { return elements.cbegin(); } - std::vector::const_iterator end() const { return elements.cend(); } + auto begin() const { return elements.cbegin(); } + auto end() const { return elements.cend(); } DECLARE_TYPEINFO(IndexedLocation, StorageLocation); }; @@ -278,14 +279,14 @@ class ProgramPoint : public IHasDbPrint { /// the previous context. E.g., a stack [Function] is the context before /// the function, while [Function, nullptr] is the context after the /// function terminates. - std::vector stack; + absl::InlinedVector stack; // Has inline space for 8 nodes public: ProgramPoint() = default; ProgramPoint(const ProgramPoint &other) : stack(other.stack) {} explicit ProgramPoint(const IR::Node *node) { CHECK_NULL(node); - stack.push_back(node); + assign(node); } ProgramPoint(const ProgramPoint &context, const IR::Node *node); /// A point logically before the function/control/action start. @@ -313,10 +314,13 @@ class ProgramPoint : public IHasDbPrint { out << "[[" << l << "]]"; } } + void assign(const ProgramPoint &context, const IR::Node *node); + void assign(const IR::Node *node) { stack.assign({node}); } + void clear() { stack.clear(); } const IR::Node *last() const { return stack.empty() ? nullptr : stack.back(); } bool isBeforeStart() const { return stack.empty(); } - std::vector::const_iterator begin() const { return stack.begin(); } - std::vector::const_iterator end() const { return stack.end(); } + auto begin() const { return stack.begin(); } + auto end() const { return stack.end(); } ProgramPoint &operator=(const ProgramPoint &) = default; ProgramPoint &operator=(ProgramPoint &&) = default; }; @@ -332,9 +336,16 @@ struct hash { }; } // namespace std +namespace Util { +template <> +struct Hasher { + size_t operator()(const P4::ProgramPoint &p) const { return p.hash(); } +}; +} // namespace Util + namespace P4 { class ProgramPoints : public IHasDbPrint { - typedef std::unordered_set Points; + typedef absl::flat_hash_set Points; Points points; explicit ProgramPoints(const Points &points) : points(points) {} @@ -342,6 +353,7 @@ class ProgramPoints : public IHasDbPrint { ProgramPoints() = default; explicit ProgramPoints(ProgramPoint point) { points.emplace(point); } void add(ProgramPoint point) { points.emplace(point); } + void add(const ProgramPoints *from); const ProgramPoints *merge(const ProgramPoints *with) const; bool operator==(const ProgramPoints &other) const; void dbprint(std::ostream &out) const override { diff --git a/frontends/p4/simplifyDefUse.cpp b/frontends/p4/simplifyDefUse.cpp index 915b8343e1..fe914dbac3 100644 --- a/frontends/p4/simplifyDefUse.cpp +++ b/frontends/p4/simplifyDefUse.cpp @@ -16,12 +16,14 @@ limitations under the License. #include "simplifyDefUse.h" +#include "absl/container/flat_hash_set.h" #include "frontends/p4/def_use.h" #include "frontends/p4/methodInstance.h" #include "frontends/p4/parserCallGraph.h" #include "frontends/p4/sideEffects.h" #include "frontends/p4/tableApply.h" #include "frontends/p4/ternaryBool.h" +#include "lib/hash.h" namespace P4 { @@ -30,7 +32,7 @@ namespace { class HasUses { // Set of program points whose left-hand sides are used elsewhere // in the program together with their use count - std::set used; + absl::flat_hash_set used; class SliceTracker { const IR::Slice *trackedSlice = nullptr; @@ -369,7 +371,7 @@ class FindUninitialized : public Inspector { ProgramPoint currentPoint; // context of the current expression/statement /// For some simple expresssions keep here the read location sets. /// This does not include location sets read by subexpressions. - std::map readLocations; + absl::flat_hash_map readLocations; HasUses *hasUses; // output /// If true the current statement is unreachable bool unreachable = false; @@ -379,7 +381,7 @@ class FindUninitialized : public Inspector { bool reportInvalidHeaders = true; const LocationSet *getReads(const IR::Expression *expression, bool nonNull = false) const { - auto result = ::get(readLocations, expression); + const auto *result = ::get(readLocations, expression); if (nonNull) BUG_CHECK(result != nullptr, "no locations known for %1%", dbp(expression)); return result; } @@ -390,11 +392,10 @@ class FindUninitialized : public Inspector { LOG3(expression << " reads " << loc); CHECK_NULL(expression); CHECK_NULL(loc); - readLocations.erase(expression); - readLocations.emplace(expression, loc); + readLocations[expression] = loc; } bool setCurrent(const IR::Statement *statement) { - currentPoint = ProgramPoint(context, statement); + currentPoint.assign(context, statement); return false; } profile_t init_apply(const IR::Node *root) override { @@ -434,11 +435,11 @@ class FindUninitialized : public Inspector { bool preorder(const IR::ParserState *state) override { LOG3("FU Visiting state " << state->name); - context = ProgramPoint(state); - currentPoint = ProgramPoint(state); // point before the first statement + context.assign(state); + currentPoint.assign(state); // point before the first statement visit(state->components, "components"); if (state->selectExpression != nullptr) visit(state->selectExpression); - context = ProgramPoint(); + context.clear(); return false; } @@ -490,7 +491,7 @@ class FindUninitialized : public Inspector { bool preorder(const IR::P4Control *control) override { LOG3("FU Visiting control " << control->name << "[" << control->id << "]"); BUG_CHECK(context.isBeforeStart(), "non-empty context in FindUnitialized::P4Control"); - currentPoint = ProgramPoint(control); + currentPoint.assign(control); headerDefs->clear(); initHeaderParams(control->getApplyMethodType()->parameters); visitVirtualMethods(control->controlLocals); @@ -513,7 +514,7 @@ class FindUninitialized : public Inspector { } LOG3("FU Visiting function " << dbp(func) << " called by " << context); LOG5(func); - auto point = ProgramPoint(context, func); + ProgramPoint point(context, func); currentPoint = point; initHeaderParams(func->type->parameters); visit(func->body); @@ -561,7 +562,7 @@ class FindUninitialized : public Inspector { bool preorder(const IR::P4Parser *parser) override { LOG3("FU Visiting parser " << parser->name << "[" << parser->id << "]"); - currentPoint = ProgramPoint(parser); + currentPoint.assign(parser); headerDefs->clear(); initHeaderParams(parser->getApplyMethodType()->parameters); visitVirtualMethods(parser->parserLocals); @@ -623,9 +624,9 @@ class FindUninitialized : public Inspector { headerDefs = inputHeaderDefs[acceptState]; unreachable = false; - auto accept = ProgramPoint(parser->getDeclByName(IR::ParserState::accept)->getNode()); + ProgramPoint accept(parser->getDeclByName(IR::ParserState::accept)->getNode()); auto acceptdefs = definitions->getDefinitions(accept, true); - auto reject = ProgramPoint(parser->getDeclByName(IR::ParserState::reject)->getNode()); + ProgramPoint reject(parser->getDeclByName(IR::ParserState::reject)->getNode()); auto rejectdefs = definitions->getDefinitions(reject, true); auto outputDefs = acceptdefs->joinDefinitions(rejectdefs); @@ -884,7 +885,7 @@ class FindUninitialized : public Inspector { auto saveHeaderDefsBeforeCondition = headerDefs->clone(); visit(statement->condition); auto saveHeaderDefsAfterCondition = headerDefs->clone(); - currentPoint = ProgramPoint(context, statement->condition); + currentPoint.assign(context, statement->condition); auto saveCurrent = currentPoint; auto saveUnreachable = unreachable; visit(statement->ifTrue); @@ -914,7 +915,7 @@ class FindUninitialized : public Inspector { visit(statement->expression); auto saveHeaderDefsAfterExpr = headerDefs->clone(); HeaderDefinitions *finalHeaderDefs = nullptr; - currentPoint = ProgramPoint(context, statement->expression); + currentPoint.assign(context, statement->expression); auto saveCurrent = currentPoint; auto saveUnreachable = unreachable; for (auto c : statement->cases) { @@ -1115,7 +1116,7 @@ class FindUninitialized : public Inspector { BUG_CHECK(findContext() == nullptr, "Unexpected action"); LOG3("FU Visiting action " << action); unreachable = false; - currentPoint = ProgramPoint(context, action); + currentPoint.assign(context, action); visit(action->body); checkOutParameters(action, action->parameters, getCurrentDefinitions()); LOG3("FU Returning from " << action); @@ -1124,7 +1125,7 @@ class FindUninitialized : public Inspector { bool preorder(const IR::P4Table *table) override { LOG3("FU Visiting " << table->name); - auto savePoint = ProgramPoint(context, table); + ProgramPoint savePoint(context, table); currentPoint = savePoint; auto saveHeaderDefsBeforeKey = headerDefs->clone(); auto key = table->getKey(); diff --git a/frontends/p4/typeMap.cpp b/frontends/p4/typeMap.cpp index 0fbf43eeb1..196ed2a4b5 100644 --- a/frontends/p4/typeMap.cpp +++ b/frontends/p4/typeMap.cpp @@ -16,8 +16,6 @@ limitations under the License. #include "typeMap.h" -#include "lib/map.h" - namespace P4 { bool TypeMap::typeIsEmpty(const IR::Type *type) const { @@ -101,8 +99,8 @@ void TypeMap::checkPrecondition(const IR::Node *element, const IR::Type *type) c void TypeMap::setType(const IR::Node *element, const IR::Type *type) { checkPrecondition(element, type); - auto it = typeMap.find(element); - if (it != typeMap.end()) { + auto [it, inserted] = typeMap.emplace(element, type); + if (!inserted) { const IR::Type *existingType = it->second; if (!implicitlyConvertibleTo(type, existingType)) BUG("Changing type of %1% in type map from %2% to %3%", dbp(element), dbp(existingType), @@ -110,12 +108,11 @@ void TypeMap::setType(const IR::Node *element, const IR::Type *type) { return; } LOG3("setType " << dbp(element) << " => " << dbp(type)); - typeMap.emplace(element, type); } const IR::Type *TypeMap::getType(const IR::Node *element, bool notNull) const { CHECK_NULL(element); - auto result = get(typeMap, element); + const auto *result = get(typeMap, element); LOG4("Looking up type for " << dbp(element) << " => " << dbp(result)); if (notNull && result == nullptr) BUG_CHECK(errorCount() > 0, "Could not find type for %1%", dbp(element)); @@ -126,8 +123,9 @@ const IR::Type *TypeMap::getType(const IR::Node *element, bool notNull) const { const IR::Type *TypeMap::getTypeType(const IR::Node *element, bool notNull) const { CHECK_NULL(element); auto result = getType(element, notNull); - BUG_CHECK(result->is(), "%1%: expected a TypeType", result); - return result->to()->type; + auto typeType = result->to(); + BUG_CHECK(typeType, "%1%: expected a TypeType", result); + return typeType->type; } void TypeMap::addSubstitutions(const TypeVariableSubstitution *tvs) { diff --git a/frontends/p4/typeMap.h b/frontends/p4/typeMap.h index 47bb7e9d5d..e0d636b6d3 100644 --- a/frontends/p4/typeMap.h +++ b/frontends/p4/typeMap.h @@ -17,9 +17,10 @@ limitations under the License. #ifndef FRONTENDS_P4_TYPEMAP_H_ #define FRONTENDS_P4_TYPEMAP_H_ +#include "absl/container/flat_hash_map.h" +#include "absl/container/flat_hash_set.h" #include "frontends/common/programMap.h" #include "frontends/p4/typeChecking/typeSubstitution.h" -#include "lib/ordered_set.h" namespace P4 { /** @@ -38,7 +39,6 @@ Objects that have a type in the map: - type declarations - map name to the actual type */ class TypeMap final : public ProgramMap { - protected: // We want to have the same canonical type for two // different tuples, lists, stacks, or p4lists with the same signature. std::vector canonicalTuples; @@ -47,13 +47,13 @@ class TypeMap final : public ProgramMap { std::vector canonicalLists; // Map each node to its canonical type - ordered_map typeMap; + absl::flat_hash_map typeMap; // All left-values in the program. - ordered_set leftValues; + absl::flat_hash_set leftValues; // All compile-time constants. A compile-time constant // is not necessarily a constant - it could be a directionless // parameter as well. - ordered_set constants; + absl::flat_hash_set constants; // For each type variable in the program the actual // type that is substituted for it. TypeVariableSubstitution allTypeVariables; diff --git a/midend/CMakeLists.txt b/midend/CMakeLists.txt index 4e9e621ec0..e98b3fdb7e 100644 --- a/midend/CMakeLists.txt +++ b/midend/CMakeLists.txt @@ -114,4 +114,5 @@ set (MIDEND_HDRS add_library (midend STATIC ${MIDEND_SRCS}) +target_link_libraries(midend frontend) # For TypeMap / RefMap add_dependencies(midend genIR ir-generated)