-
Notifications
You must be signed in to change notification settings - Fork 36
(Closes #1957) Added an Intrinsic2CodeTrans metatransformation #3577
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
Open
LonelyCat124
wants to merge
13
commits into
master
Choose a base branch
from
1957_intrinsic2code_metatransformation
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
11412cf
Added an Intrinsic2CodeTrans metatransformation
LonelyCat124 69d76e6
fix a bug when multiple subtransforamtions have the same option
LonelyCat124 ae9f554
Merge branch 'master' into 1957_intrinsic2code_metatransformation
LonelyCat124 8a211c2
Fixed name clash
LonelyCat124 9189de5
Removed bad files
LonelyCat124 56215e5
Update naming scheme for the Intrinsic2Code transformation duo
LonelyCat124 66de29e
linting
LonelyCat124 af1fbb7
Uses Intrinsics as keys instead of their names
LonelyCat124 0aa1c65
Added logging into the intrinsic2code trans
LonelyCat124 7987734
Adds additional ArrayIntrinsic2LoopTrans
LonelyCat124 0385312
linting
LonelyCat124 af72f17
Update to docs
LonelyCat124 225d5fc
Merge branch 'master' into 1957_intrinsic2code_metatransformation
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
Empty file.
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,33 @@ | ||
| module stringop | ||
| implicit none | ||
| public | ||
|
|
||
| contains | ||
| subroutine cmpblank(str) | ||
| use profile_psy_data_mod, only : profile_PSyDataType | ||
| character(len=*), intent(inout) :: str | ||
| integer :: lcc | ||
| integer :: ipb | ||
| type(profile_PSyDataType), save, target :: profile_psy_data | ||
|
|
||
| CALL profile_psy_data % PreStart("stringop", "cmpblank-r0", 0, 0) | ||
| lcc = LEN_TRIM(str) | ||
| ipb = 1 | ||
| do while (.true.) | ||
| if (ipb >= lcc) then | ||
| ! PSyclone CodeBlock (unsupported code) reason: | ||
| ! - Unsupported statement: Exit_Stmt | ||
| EXIT | ||
| end if | ||
| if (str(ipb:ipb + 1) == ' ') then | ||
| str(ipb + 1:) = str(ipb + 2:lcc) | ||
| lcc = lcc - 1 | ||
| else | ||
| ipb = ipb + 1 | ||
| end if | ||
| end do | ||
| CALL profile_psy_data % PostEnd | ||
|
|
||
| end subroutine cmpblank | ||
|
|
||
| end module stringop | ||
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,33 @@ | ||
| MODULE stringop | ||
|
arporter marked this conversation as resolved.
Outdated
|
||
| !$AGRIF_DO_NOT_TREAT | ||
| !- | ||
| !$Id: stringop.f90 2281 2010-10-15 14:21:13Z smasson $ | ||
| !- | ||
| ! This software is governed by the CeCILL license | ||
| ! See IOIPSL/IOIPSL_License_CeCILL.txt | ||
| !--------------------------------------------------------------------- | ||
| CONTAINS | ||
| != | ||
| SUBROUTINE cmpblank (str) | ||
| !--------------------------------------------------------------------- | ||
| !- Compact blanks | ||
| !--------------------------------------------------------------------- | ||
| CHARACTER(LEN=*),INTENT(inout) :: str | ||
| !- | ||
| INTEGER :: lcc,ipb | ||
| !--------------------------------------------------------------------- | ||
| lcc = LEN_TRIM(str) | ||
| ipb = 1 | ||
| DO | ||
| IF (ipb >= lcc) EXIT | ||
| IF (str(ipb:ipb+1) == ' ') THEN | ||
| str(ipb+1:) = str(ipb+2:lcc) | ||
| lcc = lcc-1 | ||
| ELSE | ||
| ipb = ipb+1 | ||
| ENDIF | ||
| ENDDO | ||
| !---------------------- | ||
| END SUBROUTINE cmpblank | ||
| !=== | ||
| END MODULE stringop | ||
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
77 changes: 77 additions & 0 deletions
77
src/psyclone/psyir/transformations/metatransformations/intrinsic2code_metatrans.py
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,77 @@ | ||
| # ----------------------------------------------------------------------------- | ||
| # SPDX-FileCopyrightText: Copyright (c) 2026 Science and Technology | ||
| # Facilities Council | ||
| # SPDX-License-Identifier: BSD-3-Clause | ||
| # See the full LICENSE file in the project root for details. | ||
| # ----------------------------------------------------------------------------- | ||
|
|
||
| '''This module contains the Intrinsic2CodeTrans metatransformation.''' | ||
|
|
||
| from psyclone.psyGen import Transformation | ||
| from psyclone.psyir.nodes import IntrinsicCall | ||
| from psyclone.psyir.transformations.intrinsics.maxval2loop_trans\ | ||
| import Maxval2LoopTrans | ||
| from psyclone.psyir.transformations.intrinsics.minval2loop_trans\ | ||
| import Minval2LoopTrans | ||
| from psyclone.psyir.transformations.intrinsics.sum2loop_trans\ | ||
| import Sum2LoopTrans | ||
| from psyclone.psyir.transformations.intrinsics.product2loop_trans\ | ||
| import Product2LoopTrans | ||
| from psyclone.utils import transformation_documentation_wrapper | ||
|
|
||
|
|
||
| @transformation_documentation_wrapper | ||
| class Intrinsic2CodeMetaTrans(Transformation): | ||
|
arporter marked this conversation as resolved.
Outdated
|
||
| '''This metatransformation applies any of the Intrinsic2Code | ||
| transformations to the provided input. The available transformations are | ||
| Maxval2LoopTrans, Sum2LoopTrans, Minval2LoopTrans, or Product2LoopTrans. | ||
|
|
||
| ''' | ||
| _SUB_TRANSFORMATIONS = [Maxval2LoopTrans, Sum2LoopTrans, | ||
| Minval2LoopTrans, Product2LoopTrans] | ||
|
|
||
| # Create a map of intrinsic names to the appropriate Intrinsic2Code | ||
| # transformation. | ||
| intrinsic_to_trans = {"MAXVAL": Maxval2LoopTrans, | ||
| "SUM": Sum2LoopTrans, | ||
| "MINVAL": Minval2LoopTrans, | ||
| "PRODUCT": Product2LoopTrans} | ||
|
|
||
| def validate(self, node: IntrinsicCall, **kwargs) -> None: | ||
| ''' | ||
| Validates the input options. | ||
|
|
||
| :param node: the IntrinsicCall to be transformed. | ||
|
|
||
| :raises TypeError: if the input node is not an IntrinsicCall. | ||
| ''' | ||
| # Validate the provided options are allowed and typed correctly. | ||
| self.validate_options(**kwargs) | ||
|
|
||
| if not isinstance(node, IntrinsicCall): | ||
| raise TypeError( | ||
| f"Input node to {self.name} must be an IntrinsicCall but " | ||
| f"received '{type(node).__name__}'." | ||
| ) | ||
|
|
||
| def apply(self, node: IntrinsicCall, **kwargs) -> None: | ||
| ''' | ||
| Applies the appropriate Intrinsic2Code transformation to the provided | ||
| input node. | ||
|
|
||
| :param node: the IntrinsicCall to be transformed. | ||
| ''' | ||
| # Split the options for the subtransformations. The options are | ||
| # returned in the order of the _SUB_TRANSFORMATIONS list. | ||
| kwargs_dict = {} | ||
| local_kwargs, kwargs_dict["MAXVAL"], kwargs_dict["SUM"], \ | ||
| kwargs_dict["MINVAL"], kwargs_dict["PRODUCT"] = \ | ||
| self.split_kwargs(**kwargs) | ||
|
|
||
| self.validate(node, **local_kwargs) | ||
|
|
||
| # If the intrinsic is one of the supported intrinsics then | ||
| # apply the relevant transformation. | ||
| if node.intrinsic.name in Intrinsic2CodeMetaTrans.intrinsic_to_trans: | ||
|
arporter marked this conversation as resolved.
Outdated
|
||
| Intrinsic2CodeMetaTrans.intrinsic_to_trans[node.intrinsic.name]().\ | ||
| apply(node, **kwargs_dict[node.intrinsic.name]) | ||
73 changes: 73 additions & 0 deletions
73
...psyclone/tests/psyir/transformations/metatransformations/intrinsic2code_metatrans_test.py
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,73 @@ | ||
| # ----------------------------------------------------------------------------- | ||
| # SPDX-FileCopyrightText: Copyright (c) 2026 Science and Technology | ||
| # Facilities Council | ||
| # SPDX-License-Identifier: BSD-3-Clause | ||
| # See the full LICENSE file in the project root for details. | ||
| # ----------------------------------------------------------------------------- | ||
|
|
||
| '''This module contains the tests for the Intrinsic2CodeMetaTrans | ||
| metatransformation.''' | ||
|
|
||
| import pytest | ||
| from psyclone.psyir.nodes import IntrinsicCall | ||
| from psyclone.psyir.transformations import Intrinsic2CodeMetaTrans | ||
|
|
||
|
|
||
| def test_intrinsic2code_trans_validate(fortran_reader): | ||
| ''' | ||
| Tests the validate method of the Intrinsic2CodeMetaTrans | ||
| metatransformation. | ||
| ''' | ||
| with pytest.raises(TypeError) as err: | ||
| Intrinsic2CodeMetaTrans().validate(123) | ||
| assert ("Input node to Intrinsic2CodeMetaTrans must be an IntrinsicCall " | ||
| "but received 'int'." in str(err.value)) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("code, expected", [ | ||
| ("j = MAXVAL(i)", | ||
| """ reduction_var = -HUGE(reduction_var) | ||
| do idx = LBOUND(i, dim=1), UBOUND(i, dim=1), 1 | ||
| reduction_var = MAX(reduction_var, i(idx)) | ||
| enddo | ||
| j = reduction_var"""), | ||
| ("j = MINVAL(i)", | ||
| """ reduction_var = HUGE(reduction_var) | ||
| do idx = LBOUND(i, dim=1), UBOUND(i, dim=1), 1 | ||
| reduction_var = MIN(reduction_var, i(idx)) | ||
| enddo | ||
| j = reduction_var"""), | ||
| ("j = PRODUCT(i)", | ||
| """ reduction_var = 1 | ||
| do idx = LBOUND(i, dim=1), UBOUND(i, dim=1), 1 | ||
| reduction_var = reduction_var * i(idx) | ||
| enddo | ||
| j = reduction_var"""), | ||
| ("j = SUM(i)", | ||
| """reduction_var = 0 | ||
| do idx = LBOUND(i, dim=1), UBOUND(i, dim=1), 1 | ||
| reduction_var = reduction_var + i(idx) | ||
| enddo | ||
| j = reduction_var"""), | ||
| ("j = UBOUND(i)", "j = UBOUND(i)"), | ||
| ]) | ||
| def test_intrinsic2code_trans_apply(fortran_reader, fortran_writer, | ||
| code, expected): | ||
| '''Test the apply function of the Intrinsic2CodeMetaTrans | ||
| metatransformation. | ||
| ''' | ||
| code = f"""subroutine test | ||
| integer, dimension(:) :: i | ||
| integer :: j | ||
|
|
||
| {code} | ||
|
|
||
| end subroutine test""" | ||
| psyir = fortran_reader.psyir_from_source(code) | ||
| intrinsic = psyir.walk(IntrinsicCall)[0] | ||
| Intrinsic2CodeMetaTrans().apply(intrinsic) | ||
|
|
||
| out = fortran_writer(psyir) | ||
| print(out) | ||
| correct = f"{expected}" | ||
| assert correct in out |
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.