-
Notifications
You must be signed in to change notification settings - Fork 34
(Closes #2721) Add ArrayConstructor to PSyIR
#3458
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 all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
65993e2
Add basic support for array constructors in PSyIR
mn416 95cdeea
Fix mistakes in `ArrayConstructor` and it's backend
mn416 e534772
Add some tests for nested constructors
mn416 2daa946
Implement `ArrayConstructor.datatype` and improve coverage
mn416 adab76c
flake8
mn416 d7229e4
Add tests for derived types inside array constructors
mn416 9779325
Merge branch 'master' into mn416-array-constructor
LonelyCat124 1ccb589
Use type hints for new array constructor code in frontend and backend
mn416 5d8a9d0
Code style improvements for new array-constructor code
mn416 55f1216
Replace a TODO with a comment
mn416 d50bd60
When possible, return a more precise array size for array constructors
mn416 6bb8284
Revert "When possible, return a more precise array size for array con…
mn416 524dc55
Add comment about the size of an array constructor
mn416 f0ab040
Merge branch 'master' into mn416-array-constructor
LonelyCat124 fc24bca
#2721 update changelog
LonelyCat124 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
Some comments aren't visible on the classic Files Changed page.
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 |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| # ----------------------------------------------------------------------------- | ||
| # BSD 3-Clause License | ||
| # | ||
| # Copyright (c) 2026, Science and Technology Facilities Council. | ||
| # All rights reserved. | ||
| # | ||
| # Redistribution and use in source and binary forms, with or without | ||
| # modification, are permitted provided that the following conditions are met: | ||
| # | ||
| # * Redistributions of source code must retain the above copyright notice, this | ||
| # list of conditions and the following disclaimer. | ||
| # | ||
| # * Redistributions in binary form must reproduce the above copyright notice, | ||
| # this list of conditions and the following disclaimer in the documentation | ||
| # and/or other materials provided with the distribution. | ||
| # | ||
| # * Neither the name of the copyright holder nor the names of its | ||
| # contributors may be used to endorse or promote products derived from | ||
| # this software without specific prior written permission. | ||
| # | ||
| # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
| # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
| # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | ||
| # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | ||
| # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | ||
| # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | ||
| # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
| # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
| # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN | ||
| # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
| # POSSIBILITY OF SUCH DAMAGE. | ||
| # ----------------------------------------------------------------------------- | ||
| # Authors M. Naylor, University of Cambridge, UK | ||
| # ----------------------------------------------------------------------------- | ||
|
|
||
| ''' This module contains the ArrayConstructor node implementation.''' | ||
|
|
||
| from __future__ import annotations | ||
| from typing import Union, TYPE_CHECKING | ||
| from psyclone.psyir.symbols import ( | ||
| UnresolvedType, DataType, ScalarType, ArrayType, DataTypeSymbol) | ||
| from psyclone.psyir.nodes.datanode import DataNode | ||
| if TYPE_CHECKING: | ||
| from psyclone.psyir.nodes import Node | ||
|
|
||
|
|
||
| class ArrayConstructor(DataNode): | ||
| ''' | ||
| Node representing an array constructor. | ||
| ''' | ||
|
|
||
| # Textual description of the node. | ||
| _children_valid_format = "[DataNode]*" | ||
| _text_name = "ArrayConstructor" | ||
| _colour = "yellow" | ||
|
|
||
| def __init__(self, **kwargs): | ||
| super().__init__(**kwargs) | ||
|
|
||
| @staticmethod | ||
| def create(elems: list[DataNode]) -> ArrayConstructor: | ||
| '''Create an ArrayConstructor instance representing an array | ||
| with the given elements. | ||
|
|
||
| :param elems: the elements of the array being constructed. | ||
| :returns: an ArrayConstructor instance. | ||
|
|
||
| :raises GenerationError: if the arguments are not of the \ | ||
| expected type. | ||
| ''' | ||
| array_cons = ArrayConstructor() | ||
| for elem in elems: | ||
| array_cons.children.append(elem) | ||
| return array_cons | ||
|
|
||
| @staticmethod | ||
| def _validate_child(position: int, child: Node) -> bool: | ||
| ''' | ||
| :param position: the position to be validated. | ||
| :param child: a child to be validated. | ||
|
|
||
| :return: whether the given child and position are valid for this node. | ||
| ''' | ||
| return isinstance(child, DataNode) | ||
|
|
||
| @property | ||
| def datatype(self) -> Union[DataType, DataTypeSymbol]: | ||
| ''' | ||
| :returns: the type of this array constructor. | ||
| ''' | ||
| # The result of an array constructor is always a rank-1 array. | ||
| # We look through the children to find the array-element type. | ||
| elem_type = UnresolvedType() | ||
| for child in self.children: | ||
| if isinstance(child.datatype, ArrayType): | ||
| elem_type = child.datatype.elemental_type | ||
| break | ||
| elif isinstance(child.datatype, ScalarType): | ||
| elem_type = child.datatype | ||
| break | ||
| elif isinstance(child.datatype, DataTypeSymbol): | ||
| elem_type = child.datatype | ||
| break | ||
|
|
||
| # In general, the array size of an array constructor is not known | ||
| # statically, either due to use of implied do (which is not | ||
| # yet supported in PSyIR), or due to a Reference to an array | ||
| # whose size is not known statically, or due to a function/intrinsic | ||
| # call. It would be possible to chase down these nodes to determine | ||
| # a more precise size but it's not clear if it's worth the added | ||
| # complexity. So for now, we uniformly return a runtime-known size. | ||
| return ArrayType(elem_type, [ArrayType.Extent.ATTRIBUTE]) | ||
|
|
||
| def node_str(self, colour: bool = True) -> str: | ||
| ''' | ||
| Construct a text representation of this node, optionally containing | ||
| colour control codes. | ||
|
|
||
| :param colour: whether or not to include colour control codes. | ||
|
|
||
| :returns: description of this PSyIR node. | ||
| ''' | ||
| return f"{self.coloured_name(colour)}[]" | ||
|
|
||
|
|
||
| # For AutoAPI documentation generation | ||
| __all__ = ['ArrayConstructor'] | ||
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.