[clang] [CIR] Unblock simple C++ structure support (PR #138368)
Andy Kaylor via cfe-commits
cfe-commits at lists.llvm.org
Mon May 5 11:04:20 PDT 2025
================
@@ -365,10 +365,15 @@ mlir::Attribute ConstantEmitter::tryEmitPrivateForVarInit(const VarDecl &d) {
if (!d.hasLocalStorage()) {
QualType ty = cgm.getASTContext().getBaseElementType(d.getType());
if (ty->isRecordType())
- if (d.getInit() && isa<CXXConstructExpr>(d.getInit())) {
- cgm.errorNYI(d.getInit()->getBeginLoc(),
- "tryEmitPrivateForVarInit CXXConstructExpr");
- return {};
+ if (const CXXConstructExpr *e =
+ dyn_cast_or_null<CXXConstructExpr>(d.getInit())) {
+ const CXXConstructorDecl *cd = e->getConstructor();
----------------
andykaylor wrote:
Classic codegen does assume that `cd` is non-null here. I'm pretty sure that if the `dyn_cast_or_null` gives us a `CXXConstructorExpr` we can count on if having a `CXXConstructorDecl`.
The AST handles any decisions about which constructors are to be called. Currently, the incubator just represents constructors, when needed, as `cir.call` operations. It may be worth doing more than that. In the case of code like this:
```
void f() {
MyClass obj = func();
}
```
The AST says this:
```
-FunctionDecl <line:12:1, line:14:1> line:12:6 f 'void ()'
`-CompoundStmt <col:10, line:14:1>
`-DeclStmt <line:13:5, col:25>
`-VarDecl <col:5, col:24> col:13 obj 'MyClass' cinit
`-CallExpr <col:19, col:24> 'MyClass'
`-ImplicitCastExpr <col:19> 'MyClass (*)()' <FunctionToPointerDecay>
`-DeclRefExpr <col:19> 'MyClass ()' lvalue Function 0x216ac150 'func' 'MyClass ()'
```
And the incubator generates this CIR:
```
cir.func @_Z1fv() {
%0 = cir.alloca !rec_MyClass, !cir.ptr<!rec_MyClass>, ["obj", init] {alignment = 1 : i64}
%1 = cir.call @_Z4funcv() : () -> !rec_MyClass
cir.store %1, %0 : !rec_MyClass, !cir.ptr<!rec_MyClass>
cir.return
}
```
Is your question whether we should do something here to explicitly show that the copy constructor was elided?
https://github.com/llvm/llvm-project/pull/138368
More information about the cfe-commits
mailing list