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

Improve initialization: e.g. cir.init_array and cir.init_struct ops #443

Open
bcardosolopes opened this issue Feb 2, 2024 · 2 comments
Open

Comments

@bcardosolopes
Copy link
Member

          Note for the future: if we get a `cir.init_array` and `cir.init_struct` ops I believe we can better represent these.

Originally posted by @bcardosolopes in #441 (comment)

@YazZz1k
Copy link
Contributor

YazZz1k commented Mar 4, 2024

I started to investigate a ways how we can support this opcodes in CIR. And I can't recognize what semantic such opcodes should have.
If the cir.init_array just takes a list of elements values like follows:

  <array_ptr> = cir.init_array(<list of array elements>)

I can't think of a way to use the cir.init_aray for case when we have to initialize the element with ctor.
For example, which CIR should we emit for this cpp code snippet?

CppClass arr[] = {{...}};

I suppose it should be smth like:

%el = cir.call CppClassCtor(%this, ...)
%arr = cir.init_array(%el)

But we obviously can't call the ctor because we don't know the address of element being constructed.

In this reason cir.init_array have to take smth like a initilalization list instead of values. And in example below we should emit the CIR like:

%arr = cir.init_array(cir.init-list(type: CppClass, args: { ... }))

But this construction looks too complex for lowering in LoweringPrepare.

So I can't imagine a way how to implement this opcodes in CIR =(
@bcardosolopes Do you have any idea how to do this?

@bcardosolopes
Copy link
Member Author

I can't think of a way to use the cir.init_aray for case when we have to initialize the element with ctor.

For the more simple examples of ctor, I just recently added support for that, see ArrayCtor and ArrayDtor. These operations could be extended to support more cases and or similar ones can also be created. Maybe this will give you some extra ideas?

The T arr[] = { {0, x}, {0, 0} }; example from test/CIR/CodeGen/array-init.c is a bit more complicated (there are probably more compelling easier ones) and already applies some high level heuristics: direct zero init on the full array + single store for x. My take is that we should have a more generic representation and during lowering prepare we pick the heuristic strategy (in this case, it'd be where we decide to use a full zero + single store).

I'll give more thought to this one but let me know if you have more ideas in the meantime.

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

No branches or pull requests

2 participants