Skip to content

Conversation

@LonelyCat124
Copy link
Collaborator

@LonelyCat124 LonelyCat124 commented Dec 9, 2025

Example and doc TODO

Basic implementation of PSyDirective including reading and writing.

At the moment I implemented it as a Standalone Directive (as it doesn't make sense to me to be a RegionDirective) but that could be more difficult to use in some ways, but I'll put together an example and see.

@codecov
Copy link

codecov bot commented Dec 9, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.91%. Comparing base (63d4c22) to head (6cb1b14).
⚠️ Report is 11 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #3255   +/-   ##
=======================================
  Coverage   99.91%   99.91%           
=======================================
  Files         376      377    +1     
  Lines       53512    53547   +35     
=======================================
+ Hits        53467    53502   +35     
  Misses         45       45           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@sergisiso
Copy link
Collaborator

I still don't think we need psyclone (!$psy) specific directives, I thought the plan was to add a generic Directive node, so that we could do psyir.walk(Directive) as an slight improvement to the current psyir.walk(Codeblock) (which tbh is almost the same as this PR implements, just with a slighly different _directive_handler)

@LonelyCat124
Copy link
Collaborator Author

We could do that instead, but it would mean more false positives when people are looking for something specific to their script, e.g. after psyir.walk(???Directive) (name is not straightforward as we can't make it Directive since thats a necessary abstract class, along with StandaloneDirective and RegionDirective nodes. The other pain point is taking things out of CodeBlocks is it could be misinterpreted as support for them, and having non-RegionDirective nodes having to be StandaloneDirective subclasses.

I would maybe with ok with doing some GenericDirective and excluding all directives that start omp, ompx, omx, acc if you think that would be better?

@sergisiso
Copy link
Collaborator

sergisiso commented Dec 9, 2025

name is not straightforward as we can't make it Directive since thats a necessary abstract class, ...
The other pain point is taking things out of CodeBlocks is it could be misinterpreted as support for them
I would maybe with ok with doing some GenericDirective

Good points, what about [Unrecognised/Unsupported/Unknown]Directive (being sibling of Standalone/RegionDirective)?

So it is clear that we don't support them and if we ever try to parse OpenMP, OpenACC into their own Directive the name will still make sense for those that we have not done yet. (my preferd is UnknownDirective, but I am unsure)

but it would mean more false positives when people are looking for something specific to their script

I don't understand this. It will be better than the current Codeblock, and it would allow searching for "ivdep" or other know compiler directive that may exist in the code.

excluding all directives that start omp, ompx, omx, acc

Why excluding those?

@LonelyCat124
Copy link
Collaborator Author

Yeah I agree with most of that, so I'll modify this PR towards that.

My reasoning for ignoring omp/acc/etc. directives is party that keeping them as CodeBlocks means they'd keep any current properties that CodeBlocks have (i.e. disabling transformations etc.) and also make it clear that they're not meant to be used as part of any tree manipulation programming, but are just for keeping elements of the source code that PSyclone doesn't or can't support.

@LonelyCat124
Copy link
Collaborator Author

@sergisiso this is ready for review, but no rush to do so.

@LonelyCat124 LonelyCat124 marked this pull request as ready for review December 15, 2025 10:33
Copy link
Collaborator

@sergisiso sergisiso left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@LonelyCat124 I like how this is implemented now, and especially having an example to showcase it. There are just minor comments now.

"""Test that the FortranReader stored a directive before an allocate as a
CodeBlock."""
"""Test that the FortranReader stored a directive before an allocate as an
UnknownDirective."""
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Revert (this one is still a Codeblock)

from psyclone.psyir.nodes.directive import StandaloneDirective


class UnknownDirective(StandaloneDirective):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe StandaloneDirective is the right choice, but why not just Directive?

Directive representing PSyclone-specific directives in the tree.
:param directive_string: The content after the !$psy part of this
node in the tree.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

param kwargs is missing

:param directive_string: The content after the !$psy part of this
node in the tree.
'''
_children_valid_format = ""
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we typically write "<LeafNode>" as format for leafs

Authors: A. B. G. Chalk, STFC Daresbury Lab
-->

# PSyclone PSyIR directives.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe:

# Use custom directives

This directory contains an example of how write PSyclone scripts to transform code
by taking advantage of information provided by the use of custom Fortran code directives.

I would also call the directory "custom_directives" (although the node name is Unknown, the use case is probably what people will look for in the examples)


The example can be compiled using the Makefile, but this is a simple operation:
``psyclone --keep-directive -s omp.py directive_filtering.F90``. The
``-keep-directives`` option is essential to enabling the directive of interest
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

double dash?

from psyclone.psyir.nodes import Loop, Routine, UnknownDirective


# Find the PSyDirectives, check what the directive is for and then find the
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe "Find the custom directive and add the associated loop to the list of loops to skip. Parallelise the rest."

And better as the method docstring instead of a comment, to make pylint happy.

Comment on lines +34 to +35
# Author: M. Naylor, University of Cambridge, UK
# Modified: A. B. G. Chalk, STFC Daresbury Lab
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only you as author

end do

! Don't parallelise this next loop due to the string comparison
!$psy no_par
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer a different string than "psy" for the example, maybe "mydirective" or "custom_directive" to not confuse anybody just looking at this file that this is a psyclone directive (change the readme with the same).

# -----------------------------------------------------------------------------
# Author: M. Naylor, University of Cambridge, UK
# Modified: A. B. G. Chalk, STFC Daresbury Lab

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We typically add "transform, compile and run" standardised target names to all examples, and you will need to make ../Makefile recurse to it (at least for the transform target) so that this is tested in the CI.

Also, it will be good to mention the example in the parent folder README

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants