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

Added syntax derived from Stefan Teleman's #446 #450

Merged
merged 6 commits into from
Aug 16, 2023

Conversation

jwoehr
Copy link
Collaborator

@jwoehr jwoehr commented Feb 21, 2023

Summary

Details and comments

@jwoehr jwoehr requested review from jakelishman and a team February 21, 2023 00:06
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
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm confused by the word "duration" here. Would it be okay to say "it is unspecified whether the memory allocation of the an array is static or obtained via dynamic allocation"?

Copy link
Contributor

@steleman steleman Feb 23, 2023

Choose a reason for hiding this comment

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

The Standardese / Officialese term for memory storage obtained via stack allocation is static storage duration.

It's static because, well, it's not dynamic (as in malloc(3C ) and friends).

Duration in this context refers to the amount of time the allocated memory block is alive - meaning in scope. static storage duration means the allocation is alive for as long as the object is in scope, and it gets automatically reclaimed by the operating system when the object goes out of scope.

As opposed to dynamic allocation, where the allocated memory block is - in principle - alive for the entire duration of the program, unless the program itself releases it back to the operating system.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Added language per this discussion

blakejohnson
blakejohnson previously approved these changes Feb 23, 2023
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.

Other than one bit of language confusion, I am good with these changes.

Co-authored-by: Blake Johnson <blakejohnson04@gmail.com>
blakejohnson
blakejohnson previously approved these changes Feb 28, 2023
Comment on lines 726 to 729
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 undefined.
Copy link
Contributor

Choose a reason for hiding this comment

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

Its unclear if partial initialization is permitted here only for the outer list or if partial inner dimensions are allowed too. Ie it seems clear that I can do:

array[int [8], 3, 2] arr = {{1, 2}}; // arr={{1, 2}, {?, ?}, {?, ?}}

but can I also do staggered partial initialization?

array[int [8], 3, 2] arr = {{}, {1,2}, {3}}; // arr={{?, ?}, {1, 2}, {3, ?}}

This seems a bit weird?

source/language/types.rst Outdated Show resolved Hide resolved
Comment on lines 739 to 748
It is unspecified whether the memory allocation of an array is of static storage
duration, (meaning the allocation is alive for as long as the object is in scope and
gets automatically reclaimed by the runtime when the object goes out of scope)
or it is obtained via dynamic allocation (meaning the allocation persists through the lifetime
of the program, unless the program itself releases it back to the operating system).
This property is implementation dependent.

If the array memory is obtained via dynamic allocation, the implementation is
responsible for reclaiming the memory when the array object goes out of scope
(garbage collection).
Copy link
Contributor

@levbishop levbishop Mar 29, 2023

Choose a reason for hiding this comment

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

Since all arrays are declared in global scope maybe the real question is whether the compiler chooses to do lifetime analysis and only allocate the array (some time before) when it is first used and deallocate (some time after) the last usage?

source/language/types.rst Outdated Show resolved Hide resolved
Comment on lines 731 to 733
As a special case, a direct initializer list declaration of the form:

``array[<type>, <size>] <identifier> = { <X> };``
Copy link
Contributor

Choose a reason for hiding this comment

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

I can see value in having syntax for initializing all elements to the same value, but this special case syntax that overlaps with the partial-initialization syntax seems unnecessary. (Especially since I imagine the most common form of partial initialization would be to initialize just the first element of say a list of accumulated deltas, which usage is precluded by this special case).

But maybe its ok since you can think of this as both partial initialization (where the unspecified elements happen to be initialized to X) and as constant initialization of the whole array. Assuming we don't care about the cost of initializing more than was needed maybe it's ok. Otherwise, maybe:

Suggested change
As a special case, a direct initializer list declaration of the form:
``array[<type>, <size>] <identifier> = { <X> };``
As a special case, a direct initializer list declaration of the form:
``array[<type>=<X>, <size>] <identifier>;``

might be another option for constant initialization of the whole array?

Copy link
Contributor

Choose a reason for hiding this comment

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

I'll chime in and say that I'm not a fan of supporting both partial initialization and the array[<type>, <size>] <identifier> = { <X> }; initialization above, for the reason that I think the semantics of partial initialization should be basically monotonic (more data specified the initializer list means more elements get initialized). Or to turn this around: it would come as a surprise to me (if I hadn't read the spec) that array[int, 5] = {3,2} has more undefined elements than array[int, 5] = {3}.

jwoehr and others added 2 commits March 29, 2023 10:02
Co-authored-by: Lev Bishop <18673315+levbishop@users.noreply.github.com>
Co-authored-by: Lev Bishop <18673315+levbishop@users.noreply.github.com>
Comment on lines 731 to 733
As a special case, a direct initializer list declaration of the form:

``array[<type>, <size>] <identifier> = { <X> };``
Copy link
Contributor

Choose a reason for hiding this comment

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

I'll chime in and say that I'm not a fan of supporting both partial initialization and the array[<type>, <size>] <identifier> = { <X> }; initialization above, for the reason that I think the semantics of partial initialization should be basically monotonic (more data specified the initializer list means more elements get initialized). Or to turn this around: it would come as a surprise to me (if I hadn't read the spec) that array[int, 5] = {3,2} has more undefined elements than array[int, 5] = {3}.

gets automatically reclaimed by the runtime when the object goes out of scope)
or it is obtained via dynamic allocation (meaning the allocation persists through the lifetime
of the program, unless the program itself releases it back to the operating system).
This property is implementation dependent.
Copy link
Contributor

Choose a reason for hiding this comment

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

It's not clear to me why this needs to be said. There's nothing here that I disagree with, but I just don't see how to operationalize it -- I don't know of any other part of the spec that talks about storage classes or memory management. It seems like a user just has no way of knowing the difference here, and so probably wouldn't care.

Clarify per TSC discussion 2023-08-02
@jwoehr jwoehr merged commit ddd7a70 into openqasm:main Aug 16, 2023
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants