Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

frontend: Fix SpecializeGenericTypes #5133

Open
wants to merge 16 commits into
base: main
Choose a base branch
from

Conversation

vlstill
Copy link
Contributor

@vlstill vlstill commented Feb 19, 2025

A significant rewrite of SpecializeGenericTypes. Namely it fixes problems occurring in more complex specializations where a specialized type might depend on another specialized type and their order of insertion can be incorrect. Instead the types are now inspected to check for their dependencies and inserted just after all of the dependencies. I've also change the naming scheme of the specializations so that simple once are more readable (although for complex once the types quickly becomes ugly).

A caveat is that many passes seem to use very similar, but slightly different patterns and these passes still contain bugs (e.g. EliminateTuples. SpecializeGenericFunctions). Ideally, we would want to factor out the common parts and implement only the specific once, but I don't have time for that (at least not now). Namely at least the insertion part should be factorable for any pass that inserts global objects that can have dependencies.

Without the changes, the added test fails with

1217: Error compiling
1217: In file: ./p4c/frontends/p4/typeChecking/typeChecker.cpp:144
1217: type-spec-nested.p4(3): Could not find type of <Type_Struct>(3478) S2_0/2 struct S2_0 {
1217:   int<6> x;
1217:   int<6> y; }
1217: struct S2<T> {
1217:        ^^

The reason is that the specialization of S2 is inserted after specialization of S1 which refers to it.

@vlstill vlstill added bug This behavior is unintended and should be fixed. core Topics concerning the core segments of the compiler (frontend, midend, parser) labels Feb 19, 2025
@vlstill vlstill self-assigned this Feb 19, 2025
@vlstill vlstill force-pushed the fix-nested-generic-spec branch from 8d17ce1 to 988e1b6 Compare February 19, 2025 19:45
Always place the specialized structs *right before* the original.

Signed-off-by: Vladimír Štill <[email protected]>
Signed-off-by: Vladimír Štill <[email protected]>
Signed-off-by: Vladimír Štill <[email protected]>
Signed-off-by: Vladimír Štill <[email protected]>
Signed-off-by: Vladimír Štill <[email protected]>
Signed-off-by: Vladimír Štill <[email protected]>
Signed-off-by: Vladimír Štill <[email protected]>
Signed-off-by: Vladimír Štill <[email protected]>
Signed-off-by: Vladimír Štill <[email protected]>
@vlstill vlstill changed the title frontend: Fix and simplify specializeGenericTypes frontend: Fix specializeGenericTypes Feb 21, 2025
@vlstill vlstill force-pushed the fix-nested-generic-spec branch 2 times, most recently from d13e3d7 to 22142e6 Compare February 21, 2025 19:19
Signed-off-by: Vladimír Štill <[email protected]>
@vlstill
Copy link
Contributor Author

vlstill commented Feb 21, 2025

@fruffy or anyone familiar with Bazel, any idea what is behind the bazel failure (e.g. here)? I can see it did not find absl/strings/str_replace.h, but I don't know how to find it as there is no @com_google_absl//absl/strings:str_replace.

@vlstill vlstill changed the title frontend: Fix specializeGenericTypes frontend: Fix SpecializeGenericTypes Feb 21, 2025
Co-authored-by: Anton Korobeynikov <[email protected]>
Signed-off-by: Vladimír Štill <[email protected]>
Signed-off-by: Vladimír Štill <[email protected]>
@vlstill vlstill requested a review from ChrisDodd February 24, 2025 08:50
@vlstill vlstill requested a review from fruffy February 24, 2025 08:50
Co-authored-by: Anton Korobeynikov <[email protected]>
Signed-off-by: Vladimír Štill <[email protected]>
Copy link
Contributor

@asl asl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall, looks good to me. See some minor notes and questions.

/// @brief Get a candidate name for the instantiation.
std::string name() const;

bool operator<(const SpecSignature &other) const {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, to double check: we're enforcing lexicographical order of SpecSignatures in TypeSpecializationMap. Maybe we should put signatures with less arguments first? Not sure if this would matter much.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing depends on the order, except that it has to be a total order for the sake of std::map. The insertion order is always driven by the "dependencies" that have to be defined first, and only when they are met for multiple elements then the order of the TypeSpecializationMap::map decides -- and even then I've left this to be ordered_map to make the order invariant on renaming of types. There is probably a better way to do this, but maybe that can be investigated in #5139.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. My point is that with cstring:operator< we order everything lexicographically. But maybe we do not need to perform that amount of string comparisons.

Signed-off-by: Vladimír Štill <[email protected]>
@asl
Copy link
Contributor

asl commented Feb 27, 2025

@vlstill Looks like generic-struct.p4 is failing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This behavior is unintended and should be fixed. core Topics concerning the core segments of the compiler (frontend, midend, parser)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants