Skip to content

[RFC] AggExprEmitter::VisitArrayInitLoopExpr implementation #1055

@smeenai

Description

@smeenai

I'm seeking implementation advice for the CIRGen implementation of AggExprEmitter::VisitArrayInitLoopExpr. The ArrayInitLoopExpr docs explain the context; I'm specifically concerned with the copy constructor use case (e.g. https://godbolt.org/z/66anjPxa5 shows the initialization loop emitted by CodeGen).

For starters, CodeGen has logic to convert such initializations to a memcpy where applicable. ClangIR generally avoids this though (e.g. https://godbolt.org/z/W9n11czTc), so I assume that's the case here too. We may want to flesh out ConstructorMemcpyizer to still emit memcpy for non-record fields, but that's unrelated to this particular issue.

The CodeGen implementation of VisitArrayInitLoopExpr emits a loop to initialize each element. I'd started implementing it similarly in CIRGen, and then came across the cir.array.ctor op, which serves a pretty similar purpose. My questions are:

  • Does it make sense to use cir.array.ctor to implement VisitArrayInitLoopExpr? (The rest of this issue can be ignored if the answer is no.)
  • cir.array.ctor currently expects each member to be initialized by calling a constructor. Should we (a) change it to allow arbitrary initialization so that we can also use it for arrays of non-record types, (b) leave the op alone and emit a loop inside VisitArrayInitLoopExpr for non-record types, or (c) match CodeGen and emit memcpy for non-record types, so that VisitArrayInitLoopExpr is never entered for them? My inclination is either (a) or (c); (b) seems like a weird middle ground that's a bunch of extra work for no gain.
  • How should cleanups be handled? The one current user of cir.array.ctor bails out if there's cleanups. I'm thinking the cleanups should be handled during the lowering of the cir.array.ctor op itself instead of at the op creation site, but I haven't thought that through fully.
  • The op doesn't handle multi-dimensional arrays yet (https://godbolt.org/z/3zh3b1fso), which would be something else required for completeness.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions