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

[arrays]: The OQ3 Spec mentions references but does not define what they are #446

Open
steleman opened this issue Feb 16, 2023 · 3 comments
Labels
clarify specification Change text of spec, not (mainly) semantics of spec

Comments

@steleman
Copy link
Contributor

What is the expected enhancement?

The OpenQASM3 Spec for Arrays states the following:

All array parameters are passed as references [ ... ]

The reference Type is not defined anywhere in the current Spec.

For clarity and completeness, we should define the reference Type, and its properties.

@steleman steleman added the enhancement default GH label label Feb 16, 2023
@steleman steleman changed the title [arrays]: The OQ3 Spec mentions references but does not define what they are [arrays]: The OQ3 Spec mentions references but does not define what they are Feb 16, 2023
@jwoehr
Copy link
Collaborator

jwoehr commented Feb 20, 2023

@steleman can you provide a model PR that would allow the spec to indicate more rigorously that array parameters are passed by reference as we all more or less understand the sense of things?

@steleman
Copy link
Contributor Author

steleman commented Feb 20, 2023

Arrays are a special and degenerate type of a fixed-size tensor. Unlike tensors,
arrays cannot be resized or reshaped.

The size of an array is constant and immutable, and is recorded once, at
declaration time.

Arrays allocate a contiguous block of memory of suitable size and alignment to
accommodate the storage of its elements.

If the array declaration is direct-initialized via initializer list, its elements
are initialized to the values provided by the initializer list.  Otherwise, the
memory allocated is not initialized, and is assumed to be garbage.

Partial initialization via initializer list is allowed - i.e. the initializer
list is incomplete, and does not provide values for all the elements of the array.
In this case, the elements that are not initialized via initializer list are not
initialized, and their values are assumed to be garbage.

As a special case, a direct initializer list declaration of the form:

  array[<type>, <size>] <identifier> = { <X> };

will initialize all the elements of the array to <X>, where <X> is of a type
suitable for initializing an element of the array, and if and only if the array
element type supports direct initialization from such a type.

It is unspecified whether the memory allocation of an array is of static storage
duration, or it is obtained via dynamic allocation. This property is implementation
dependent.

If the array memory is obtained via dynamic allocation, the implementation is
responsible for re-claiming the memory when the array object goes out of scope
(garbage collection).

Arrays may be passed as parameters or arguments to functions.

When passed as argument to a function, an array shall not cause a copy spill on
the stack. Several mechanisms are available to implement this constraint:

  - Use the C model: an array implicitly decays to a pointer to its first
    element, and only this pointer is passed as argument. For the array type
    case, a pointer is defined as the memory address of the first element of
    the array. This pointer may cause a copy spill on the stack. This does not
    imply that pointer types are generally normative to the language. This
    implicit conversion from array to pointer and back may remain opaque to the
    user.

  - Use the Fortran model: every array is described by an Array Descriptor.
    An Array Descriptor is an opaque type that contains descriptive metadata
    describing its corresponding array, including size and memory address
    boundaries. For function argument passing, only the Array Descriptor is
    actually passed to the function and copy-spilled on the stack.
    The implementation details of an Array Descriptor are unspecified and
    implementation dependent, however: size and memory address boundaries are
    normative.

An implementation is free to choose alternate mechanisms for passing arrays
as arguments to functions, within the constraints outlined above. The two
examples outlined above are not normative.

Arrays are statically typed, and cannot implicitly convert to or from any other type.

I think this also covers the constructor / destructor PR.

@jwoehr
Copy link
Collaborator

jwoehr commented Feb 23, 2023

@steleman can you please comment on @blakejohnson 's question to #450 ?

jwoehr added a commit that referenced this issue Aug 16, 2023
Added syntax derived from Stefan Teleman's #446
jlapeyre pushed a commit to jlapeyre/openqasm that referenced this issue Nov 8, 2023
@jlapeyre jlapeyre added clarify specification Change text of spec, not (mainly) semantics of spec and removed enhancement default GH label 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
Projects
None yet
Development

No branches or pull requests

3 participants