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

Remove IR::Annotations and make IAnnotated to carry annotations inline #4992

Merged
merged 5 commits into from
Nov 9, 2024

Conversation

asl
Copy link
Contributor

@asl asl commented Nov 1, 2024

Lots of cleanups and fixes here and there

@asl asl added core Topics concerning the core segments of the compiler (frontend, midend, parser) run-validation Use this tag to trigger a Validation CI run. run-sanitizer Use this tag to run a Clang+Sanitzers CI run. breaking-change This change may break assumptions of compiler back ends. labels Nov 1, 2024
@asl
Copy link
Contributor Author

asl commented Nov 1, 2024

This is PR on top of #4971 for simplicity.

As mentioned in #4974, IR::Annotations incur a huge overhead, and in general does not make any sense: it's just another level of indirection. So, this just implements old FIXME, plus does some cleanup here and there.

The impact of # of allocations is huge.
Before:
image
After:
Screenshot 2024-11-01 at 15 49 14

So, we're 10% less of memory allocations by both total size and number.

The runtime impact with GC disabled is huge as well:

Command Mean [s] Min [s] Max [s] Relative
test/gtestp4c-nogc-main --gtest_filter=P4CParserUnroll.switch_20160512 3.240 ± 0.048 3.195 3.422 1.10 ± 0.03
test/gtestp4c-nogc --gtest_filter=P4CParserUnroll.switch_20160512 2.948 ± 0.061 2.855 3.041 1.00

So, we're 10% faster here (!)

Runtime impact with GC on is much less visible due to huge GC overhead (1.5x):

Command Mean [s] Min [s] Max [s] Relative
test/gtestp4c-main --gtest_filter=P4CParserUnroll.switch_20160512 4.584 ± 0.099 4.312 4.897 1.02 ± 0.03
test/gtestp4c --gtest_filter=P4CParserUnroll.switch_20160512 4.512 ± 0.072 4.309 4.649 1.00

@asl
Copy link
Contributor Author

asl commented Nov 1, 2024

sanitizers are broken after #4989

@@ -154,7 +157,7 @@ class P4Control : Type_Declaration, INestedNamespace, ISimpleNamespace, IApply,

/// A P4-16 action
class P4Action : Declaration, ISimpleNamespace, IAnnotated, IFunctional {
optional Annotations annotations = Annotations::empty;
Copy link
Contributor

@ChrisDodd ChrisDodd Nov 6, 2024

Choose a reason for hiding this comment

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

Would we get most/all of the memory savings by just using inline here instead of getting rid of IR::Annotations completely? That might be a simpler change.

Copy link
Contributor Author

@asl asl Nov 6, 2024

Choose a reason for hiding this comment

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

Not all, as IR::Annotations will be still be a Node and it will still be cloned. Though "invisible" now when outer node is cloned by itself. Plus, there was already a FIXME for this change. I think additional wrapper is not required as annotation is a property of the Node itself, so why do we need to have additional level of indirection? For convenience we can certainly do using Annotations = IR::Vector<IR::Annotation> here, yes.

Plus I already fixed couple of bugs when it was Annotations (being a Node) queried for the presence of an Annotation. Certainly the check returned always false. Now only IAnnotated objects could be queried about annotations.

In addition to this: usually we only have 1-2 annotations max (e.g. @name and that's it). So we can store them inline using e.g. absl::InlineVector as underlying storage. This would remove almost all Annotation-related memory allocations. But first we'd need to reduce the size of Allocation itself IMO

Copy link
Contributor

Choose a reason for hiding this comment

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

I think additional wrapper is not required as annotation is a property of the Node itself, so why do we need to have additional level of indirection?

I agree.

@asl
Copy link
Contributor Author

asl commented Nov 6, 2024

I rebased the branch after abseil string helpers were merged in. Now it should show the changes more cleanly

Copy link
Contributor

@vlstill vlstill left a comment

Choose a reason for hiding this comment

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

Looks good, thank you. I went over ir, midend and most of frontend changes (except mainly of P4-14).

ir/annotations.cpp Outdated Show resolved Hide resolved
Comment on lines +330 to +334
bool needsParsing : 1;

/// If this is true this is a structured annotation, and there are some
/// constraints on its contents.
bool structured : 1;
Copy link
Contributor

Choose a reason for hiding this comment

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

Does the bit-field really decrease size of the struct? Since there are the Vector/IndexedVector members, I would expect the alignment to be 64bit and therefore it would not really meatter if the 2 bools are 2 bits or 2 bytes. Or is there another reason to use bit-field?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am planning to fix this in subsequent PRs :) We really need a discriminated union here as sizeof(Annotation) is terrible large. For now I just put it here not to forget.

@@ -154,7 +157,7 @@ class P4Control : Type_Declaration, INestedNamespace, ISimpleNamespace, IApply,

/// A P4-16 action
class P4Action : Declaration, ISimpleNamespace, IAnnotated, IFunctional {
optional Annotations annotations = Annotations::empty;
Copy link
Contributor

Choose a reason for hiding this comment

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

I think additional wrapper is not required as annotation is a property of the Node itself, so why do we need to have additional level of indirection?

I agree.

midend/flattenInterfaceStructs.h Outdated Show resolved Hide resolved
frontends/p4/sideEffects.h Outdated Show resolved Hide resolved
@asl asl requested a review from vlstill November 8, 2024 07:05
Signed-off-by: Anton Korobeynikov <[email protected]>
initializer_list while there

Signed-off-by: Anton Korobeynikov <[email protected]>
Signed-off-by: Anton Korobeynikov <[email protected]>
Lots of cleanups and fixes here and there

Signed-off-by: Anton Korobeynikov <[email protected]>
Signed-off-by: Anton Korobeynikov <[email protected]>
@asl
Copy link
Contributor Author

asl commented Nov 9, 2024

validate-p4 fixes are in p4gauntlet/toz3#41

@asl asl added this pull request to the merge queue Nov 9, 2024
Merged via the queue into p4lang:main with commit 6f7353f Nov 9, 2024
17 of 19 checks passed
@asl asl deleted the annotations-clones branch November 9, 2024 09:00
@fruffy
Copy link
Collaborator

fruffy commented Nov 9, 2024

@smolkaj fyi. This might influence the way you work with annotations.

@@ -98,7 +98,7 @@ table as {
actions {
a1
a2
set_exception
set_exception @defaultonly
Copy link
Collaborator

@fruffy fruffy Nov 9, 2024

Choose a reason for hiding this comment

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

This actually changes the semantics of the program, what change caused this? It looks like this fixes a bug, but would be good to know where this was a problem.

Copy link
Contributor

Choose a reason for hiding this comment

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

It almost had to have been a change in the DPDK back end code that fixed this, but I haven't tried to narrow down which of those changes it was. The frontend and midend output files have this annotation before and after this PR.

Copy link
Contributor Author

@asl asl Nov 9, 2024

Choose a reason for hiding this comment

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

This was an obvious bug IMO, see https://github.com/p4lang/p4c/pull/4992/files#diff-2dded02099b0c47be6b37c34d45999cbe59f2ed1e408e6fcc7b73455dff71caaL417

Essentially the checks there were always false as it was IR::Annotations that was queried, and not the original node.

@vlstill
Copy link
Contributor

vlstill commented Nov 13, 2024

@asl, apparently this breaks dbprint of annotated types, e.g. header of a function now can look like this in dbprint: { }void foo({ }in int<16> bar, { }in int<16> baz, { }out int<16> boo). Presumably the problem is that if annotations member is just printed, it will be printed using IR::Vector::dbprint, which is printed as bracket-encodes list with , separators.

@asl
Copy link
Contributor Author

asl commented Nov 13, 2024

@vlstill The debug printing of annotations was pretty broken before this PR. In particular it only prints expr, so unparsed or structured annotations are not printed at all(!).

I'm working on the proper fix when porting Annotation to disjoint union. Stay tuned :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking-change This change may break assumptions of compiler back ends. core Topics concerning the core segments of the compiler (frontend, midend, parser) run-sanitizer Use this tag to run a Clang+Sanitzers CI run. run-validation Use this tag to trigger a Validation CI run.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants