-
Notifications
You must be signed in to change notification settings - Fork 455
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
IR node pointers are not consistent? #5131
Comments
A clarification question, what do you mean with consistent? In general, the pointers should be consistent for a single IR tree. But it often happens that the entire program is cloned from compiler pass to compiler pass, which means that previous pointers are stale. I personally do not use @ChrisDodd can likely clarify. |
In general, it is preferable to use names rather than node pointers, since, once you run the This is where The way modifications to the IR work, it is always done "copy-on-write" -- once a node is "in" the IR, it will never change -- any modification will be done by cloning the node and modifying the copy, and then "relinking" it into its parents, which involves modifying (and thus cloning) all of them, up to the root IR node (usually a P4Program). So while a Modifier or Transform is running, there may be two instances of a node (the "before" and "after") version available -- the ones passed to the preorder/postorder methods are always the "after" (they're clones, that why they are not This is why many passes are a two-step process -- first an Inspector that does name lookups (often with Perhaps there should be a version of |
I would expect that nodes representing specific things (types, functions, parsers, controls, packages, etc) remain the same if the IR is not modified through the use of a Transform or similar.
Yes, I'm aware that the tree can change when it's modified by a pass, and that makes sense. But this behavior happens even when no modifications have been made, e.g. just walking the IR with an Inspector. Nodes which should refer to the same "abstract object" in the program do not match up.
This matches my understanding as well. But I'm using an Inspector. The behavior in my post occurs even when no modifications are done. Unless
Yes, this is what I'm doing. But the nodes do not match what I've looked up previously. I do have multiple "layers" of Inspectors. Does an Inspector clone the whole tree when it starts visiting? I can't really share a significant amount of code, but I can at least show a small example. On the top level, I have something like this:
Later, when encountering
This fails as the map does not contain the extern type node that was looked up earlier. The IR has not been modified between these two pieces of code. It's very odd, because I do something similar to this for P4Parser and P4Control, and that works fine. 🤷 |
From what I've read in the documentation, it should be viable to use pointers to IR nodes so long as you make sure to get the original node when using nodes directly from a visitor callback.
However, I'm noticing extreme inconsistency in IR behavior that makes it impossible to use pointers to nodes for almost anything. I can understand why many backends simply use names of things as their map keys, because referring to things by node just doesn't work.
If I walk the IR of my P4Program using a visitor and stop on Type_Extern to grab the type of e.g. packet_in, I get one node:
If I then later have a
b.extract(h.ethernet)
in a parser, and useMethodInstance::resolve
to get the information from the MethodCallExpression, I get a totally different one from the ExternMethod:If I instead drill down into the expression of the MCE and check the type of the Member subexpression itself:
It's a totally different node again! These nodes are all the same packet_in; I've printed them and checked.
Is it supposed to be like this? At no point do I modify the IR, but it still does this. It's very frustrating to use the IR when it behaves like this.
The text was updated successfully, but these errors were encountered: