-
Notifications
You must be signed in to change notification settings - Fork 214
Add target module and old_to_new mapping to CloneReplacer #2976
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
base: main
Are you sure you want to change the base?
Conversation
0d83df2
to
cf625a8
Compare
cf625a8
to
8c72089
Compare
xls/dslx/frontend/ast_cloner.h
Outdated
absl::AnyInvocable<absl::StatusOr<std::optional<AstNode*>>(const AstNode*)>; | ||
absl::AnyInvocable<absl::StatusOr<std::optional<AstNode*>>( | ||
const AstNode*, Module*, | ||
const absl::flat_hash_map<const AstNode*, AstNode*>&)>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a replacer somewhere that actually wants to use the old_to_new argument?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am going to implement one using this. Basically, when we want to create a new node with some name ref, we want the reference points to something that is in the new module instead of the old one. I would love to use this mapping to resolve it.
UpdateDo not clone non-owned nodesThe core issue is that the cloner should not clone non‑owned subnodes. This has led to problems like the one addressed in #2978. I added commits that consistently avoid cloning non‑owned nodes. Concretely, for references, we either rebind to a cloned target if one exists or preserve the original pointer. With this in place, we can remove Chaining of the replacersThe new commits also refine replacer chaining.
These cover all the remaining usages after removing Threading old_to_new mappingPreviously, only the top‑level replacement was added to This isn’t strictly required (DSLX ASTs are mostly trees), but it makes replacers more robust and predictable. Feedback welcome. Alternative DesignWe recognize the ownership subtleties and, in some cases, the desire for transitive cloning. If the stricter “don’t clone non‑owned nodes” policy feels too limiting, another option is to let the replacer decide case‑by‑case, potentially using extra context (e.g., type inference). One approach would be a replacer object with two callables: one for replacement and one predicate for whether a node should be cloned. Alternatively, we can keep the current “optional replacement” interface and encode that choice in the replacer’s logic. Happy to explore this further. |
Depends on #2975.
This pull request enhances the
CloneReplacer
interface by adding:old_to_new
mapping.These contexts are necessary to ensure that the newly created AST nodes are associated with the correct module.
After the changes, I temporarily inserted
VerifyClone
after most call sites of the cloning APIs. All of them passed except for this one: https://github.com/xlsynth/xlsynth/blob/0d83df265de1dd4391cc5f30b2a232b7209da302/xls/dslx/type_system_v2/inference_table.cc#L629-L631.This failure would happen even without this PR. I wonder if it is acceptable to create a malformed module solely for type checking, or should we fix this case as well.