-
-
Notifications
You must be signed in to change notification settings - Fork 3k
[mypyc] Add SetElement op for initializing struct values #19437
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -34,6 +34,7 @@ class to enable the new behavior. Sometimes adding a new abstract | |
| from mypyc.ir.rtypes import ( | ||
| RArray, | ||
| RInstance, | ||
| RStruct, | ||
| RTuple, | ||
| RType, | ||
| RVoid, | ||
|
|
@@ -244,6 +245,25 @@ def __init__(self, value: bytes, line: int = -1) -> None: | |
| self.line = line | ||
|
|
||
|
|
||
| @final | ||
| class Undef(Value): | ||
| """An undefined value. | ||
|
|
||
| Use Undef() as the initial value followed by one or more SetElement | ||
| ops to initialize a struct. Pseudocode example: | ||
|
|
||
| r0 = set_element undef MyStruct, "field1", f1 | ||
| r1 = set_element r0, "field2", f2 | ||
| # r1 now has new struct value with two fields set | ||
|
|
||
| Warning: The generated code can use an arbitrary runtime values for | ||
| this (likely not explicitly initialized). | ||
| """ | ||
|
|
||
| def __init__(self, rtype: RType) -> None: | ||
| self.type = rtype | ||
|
|
||
|
|
||
| class Op(Value): | ||
| """Abstract base class for all IR operations. | ||
|
|
||
|
|
@@ -1636,6 +1656,39 @@ def accept(self, visitor: OpVisitor[T]) -> T: | |
| return visitor.visit_get_element_ptr(self) | ||
|
|
||
|
|
||
| @final | ||
| class SetElement(RegisterOp): | ||
| """Set the value of a struct element. | ||
|
|
||
| This evaluates to a new struct with the changed value. | ||
|
|
||
| Use together with Undef to initialize a fresh struct value | ||
| (see Undef for more details). | ||
| """ | ||
|
|
||
| error_kind = ERR_NEVER | ||
|
|
||
| def __init__(self, src: Value, field: str, item: Value, line: int = -1) -> None: | ||
| super().__init__(line) | ||
| assert isinstance(src.type, RStruct), src.type | ||
| self.type = src.type | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Irrelevant as it won't play nice with |
||
| self.src = src | ||
| self.item = item | ||
| self.field = field | ||
|
|
||
| def sources(self) -> list[Value]: | ||
| return [self.src] | ||
|
|
||
| def set_sources(self, new: list[Value]) -> None: | ||
| (self.src,) = new | ||
|
|
||
| def stolen(self) -> list[Value]: | ||
| return [self.src] | ||
|
|
||
| def accept(self, visitor: OpVisitor[T]) -> T: | ||
| return visitor.visit_set_element(self) | ||
|
|
||
|
|
||
| @final | ||
| class LoadAddress(RegisterOp): | ||
| """Get the address of a value: result = (type)&src | ||
|
|
@@ -1908,6 +1961,10 @@ def visit_set_mem(self, op: SetMem) -> T: | |
| def visit_get_element_ptr(self, op: GetElementPtr) -> T: | ||
| raise NotImplementedError | ||
|
|
||
| @abstractmethod | ||
| def visit_set_element(self, op: SetElement) -> T: | ||
| raise NotImplementedError | ||
|
|
||
| @abstractmethod | ||
| def visit_load_address(self, op: LoadAddress) -> T: | ||
| raise NotImplementedError | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
this is unclear.
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.
Updated.