-
Notifications
You must be signed in to change notification settings - Fork 314
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
base: main
Are you sure you want to change the base?
Allow pragmas outside of global scope #490
Conversation
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.
Makes sense to me.
If a pragma appears outside global scope, is its impact scoped locally or globally? |
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. |
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. |
From the spec on pragma,
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.