Skip to content
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

Allow pragmas outside of global scope #490

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

braised-babbage
Copy link
Contributor

@braised-babbage braised-babbage commented Nov 9, 2023

From the spec on pragma,

Pragma directives start with pragma and continue to the end of line. The text after pragma is a single string, and parsing is left to the specific implementation. Implementations may optionally choose to support the older #pragma keyword as a custom extension. Pragmas should be processed as soon as they are encountered; if a pragma is not supported by a compiler pass it should be ignored and preserved intact for future passes. Pragmas should avoid stateful or positional interactions to avoid unexpected behaviors between included source files. If the position is relevant to a pragma, an annotation should be used instead.

The grammar places no restrictions on where pragmas may appear. However, the Python reference parser currently raises an exception if a pragma occurs outside of the global scope. I do not see the point of this restriction. If the meaning of a pragma is independent of its position, then one could simply allow them anywhere.

This PR changes the parser to not treat pragmas outside of the global scope as an error. I have added a clarifying line to the spec indicating that this is allowed.

To slightly motivate why I prefer the flexibility of a "pragma anywhere" approach, I will say that we often work with OpenQASM programs which are produced by mechanically combining various AST fragments. If any of these AST fragments includes a pragma, then their composition is not so simple. For example, we may wish to produce a defcal "on-the-fly", and as it stands the restriction on pragma placement requires that any pragmas associated with the defcal get percolated to the program toplevel via some side channel. It would be much more convenient to allow defcal bodies to carry pragma statements. To give another example, we sometimes take a sequence of statements and wrap this in a loop (e.g. to scan over a parameter as part of a larger experiment) -- again life is simpler if pragmas don't have to bubble up to the toplevel.

Copy link
Contributor

@blakejohnson blakejohnson left a comment

Choose a reason for hiding this comment

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

Makes sense to me.

@jwoehr
Copy link
Collaborator

jwoehr commented Dec 20, 2023

If a pragma appears outside global scope, is its impact scoped locally or globally?

@jakelishman
Copy link
Contributor

Jack: if I recall correctly, that question is the reason the spec originally only allowed pragmas at the global scope. I'm fairly sure it was discussed, and there wasn't at the time a known use-case for local pragmas, so they were made global-only in order to avoid needing to answer that question. I think it was approximately assumed that annotations could serve a more local purpose.

That said, I don't feel strongly, and my recollection is that the original reason for not allowing it was a lack of compelling use-case more than a technical reason. Provided we do define how a local pragma is to be interpreted (even if just to say "a compiler is free to interpret this entirely however it likes"), I'm fine with it.

@jwoehr
Copy link
Collaborator

jwoehr commented Dec 20, 2023

Provided we do define how a local pragma is to be interpreted (even if just to say "a compiler is free to interpret this entirely however it likes"), I'm fine with it.

Yes, that is the important point. Is a local pragma local in effect or global?

@braised-babbage
Copy link
Contributor Author

Provided we do define how a local pragma is to be interpreted (even if just to say "a compiler is free to interpret this entirely however it likes"), I'm fine with it.

Yes, that is the important point. Is a local pragma local in effect or global?

My view is that a pragma should be a kind of global signal to the compiler, regardless of whether it appeared in some particular lexical scope. If at some point we introduce a notion of separate compilation, I would be happy to consider pragmas applying to a particular compilation unit.

Probably the most ubiquitous notion of "pragma" is the one from C/C++ where the effect is global but position matters (i.e. pragmas apply as the file is processed top-to-bottom). Some of these are truly used as global signals, and others are just turned on and off for some portion of the program. In this respect I think the OpenQASM notion of annotation is a nice alternative mechanism to communicate information to the compiler with a clear & bounded extent. But if pragmas apply only to the scope block they are in, they would not offer much beyond what we can do with annotations.

@jlapeyre jlapeyre added Reference Parser ANLTR-based Python reference parser clarify specification Change text of spec, not (mainly) semantics of spec labels Jan 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clarify specification Change text of spec, not (mainly) semantics of spec Reference Parser ANLTR-based Python reference parser
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants