Skip to content

Commit 3d16a0f

Browse files
committed
[CIR][CIRGen] Support more member init variations
1 parent dae8726 commit 3d16a0f

File tree

11 files changed

+90
-16
lines changed

11 files changed

+90
-16
lines changed

clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,16 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
115115
return getPointerTo(::mlir::cir::VoidType::get(getContext()), cirAS);
116116
}
117117

118+
mlir::cir::MethodAttr getMethodAttr(mlir::cir::MethodType ty,
119+
mlir::cir::FuncOp methodFuncOp) {
120+
auto methodFuncSymbolRef = mlir::FlatSymbolRefAttr::get(methodFuncOp);
121+
return mlir::cir::MethodAttr::get(ty, methodFuncSymbolRef);
122+
}
123+
124+
mlir::cir::MethodAttr getNullMethodAttr(mlir::cir::MethodType ty) {
125+
return mlir::cir::MethodAttr::get(ty);
126+
}
127+
118128
mlir::cir::BoolAttr getCIRBoolAttr(bool state) {
119129
return mlir::cir::BoolAttr::get(getContext(), getBoolTy(), state);
120130
}
@@ -142,6 +152,8 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
142152
return getConstNullPtrAttr(ptrTy);
143153
if (auto structTy = mlir::dyn_cast<mlir::cir::StructType>(ty))
144154
return getZeroAttr(structTy);
155+
if (auto methodTy = mlir::dyn_cast<mlir::cir::MethodType>(ty))
156+
return getNullMethodAttr(methodTy);
145157
if (mlir::isa<mlir::cir::BoolType>(ty)) {
146158
return getCIRBoolAttr(false);
147159
}

clang/lib/CIR/CodeGen/CIRGenBuilder.h

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -251,16 +251,6 @@ class CIRGenBuilderTy : public CIRBaseBuilderTy {
251251
return mlir::cir::DataMemberAttr::get(getContext(), ty, std::nullopt);
252252
}
253253

254-
mlir::cir::MethodAttr getMethodAttr(mlir::cir::MethodType ty,
255-
mlir::cir::FuncOp methodFuncOp) {
256-
auto methodFuncSymbolRef = mlir::FlatSymbolRefAttr::get(methodFuncOp);
257-
return mlir::cir::MethodAttr::get(ty, methodFuncSymbolRef);
258-
}
259-
260-
mlir::cir::MethodAttr getNullMethodAttr(mlir::cir::MethodType ty) {
261-
return mlir::cir::MethodAttr::get(ty);
262-
}
263-
264254
// TODO(cir): Once we have CIR float types, replace this by something like a
265255
// NullableValueInterface to allow for type-independent queries.
266256
bool isNullValue(mlir::Attribute attr) const {

clang/lib/CIR/CodeGen/CIRGenCXXABI.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,9 @@ mlir::cir::GlobalLinkageKind CIRGenCXXABI::getCXXDestructorLinkage(
7979

8080
std::vector<CharUnits> CIRGenCXXABI::getVBPtrOffsets(const CXXRecordDecl *RD) {
8181
return std::vector<CharUnits>();
82+
}
83+
84+
bool CIRGenCXXABI::isZeroInitializable(const MemberPointerType *MPT) {
85+
// Fake answer.
86+
return true;
8287
}

clang/lib/CIR/CodeGen/CIRGenCXXABI.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,10 @@ class CIRGenCXXABI {
268268
virtual RecordArgABI
269269
getRecordArgABI(const clang::CXXRecordDecl *RD) const = 0;
270270

271+
/// Return true if the given member pointer can be zero-initialized
272+
/// (in the C++ sense) with an LLVM zeroinitializer.
273+
virtual bool isZeroInitializable(const MemberPointerType *MPT);
274+
271275
/// Gets the offsets of all the virtual base pointers in a given class.
272276
virtual std::vector<CharUnits> getVBPtrOffsets(const CXXRecordDecl *RD);
273277

clang/lib/CIR/CodeGen/CIRGenClass.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,12 @@ static void buildLValueForAnyFieldInitialization(CIRGenFunction &CGF,
205205
LValue &LHS) {
206206
FieldDecl *Field = MemberInit->getAnyMember();
207207
if (MemberInit->isIndirectMemberInitializer()) {
208-
llvm_unreachable("NYI");
208+
// If we are initializing an anonymous union field, drill down to the field.
209+
IndirectFieldDecl *IndirectField = MemberInit->getIndirectMember();
210+
for (const auto *I : IndirectField->chain()) {
211+
auto *fd = cast<clang::FieldDecl>(I);
212+
LHS = CGF.buildLValueForFieldInitialization(LHS, fd, fd->getName());
213+
}
209214
} else {
210215
LHS = CGF.buildLValueForFieldInitialization(LHS, Field, Field->getName());
211216
}

clang/lib/CIR/CodeGen/CIRGenExpr.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2647,7 +2647,7 @@ RValue CIRGenFunction::convertTempToRValue(Address addr, clang::QualType type,
26472647
case TEK_Complex:
26482648
llvm_unreachable("NYI");
26492649
case TEK_Aggregate:
2650-
llvm_unreachable("NYI");
2650+
return lvalue.asAggregateRValue();
26512651
case TEK_Scalar:
26522652
return RValue::get(buildLoadOfScalar(lvalue, loc));
26532653
}

clang/lib/CIR/CodeGen/CIRGenExprAgg.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -282,9 +282,7 @@ class AggExprEmitter : public StmtVisitor<AggExprEmitter> {
282282
llvm::Value *outerBegin = nullptr) {
283283
llvm_unreachable("NYI");
284284
}
285-
void VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
286-
llvm_unreachable("NYI");
287-
}
285+
void VisitImplicitValueInitExpr(ImplicitValueInitExpr *E);
288286
void VisitNoInitExpr(NoInitExpr *E) { llvm_unreachable("NYI"); }
289287
void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) {
290288
CIRGenFunction::CXXDefaultArgExprScope Scope(CGF, DAE);
@@ -1462,6 +1460,14 @@ void AggExprEmitter::VisitCXXInheritedCtorInitExpr(
14621460
E->inheritedFromVBase(), E);
14631461
}
14641462

1463+
void AggExprEmitter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
1464+
QualType T = E->getType();
1465+
mlir::Location loc = CGF.getLoc(E->getSourceRange());
1466+
AggValueSlot Slot = EnsureSlot(loc, T);
1467+
buildNullInitializationToLValue(loc,
1468+
CGF.makeAddrLValue(Slot.getAddress(), T));
1469+
}
1470+
14651471
//===----------------------------------------------------------------------===//
14661472
// Helpers and dispatcher
14671473
//===----------------------------------------------------------------------===//

clang/lib/CIR/CodeGen/CIRGenItaniumCXXABI.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ class CIRGenItaniumCXXABI : public cir::CIRGenCXXABI {
122122
}
123123

124124
bool classifyReturnType(CIRGenFunctionInfo &FI) const override;
125+
bool isZeroInitializable(const MemberPointerType *MPT) override;
125126

126127
AddedStructorArgCounts
127128
buildStructorSignature(GlobalDecl GD,
@@ -2633,3 +2634,9 @@ CIRGenItaniumCXXABI::buildVirtualMethodAttr(mlir::cir::MethodType MethodTy,
26332634

26342635
return mlir::cir::MethodAttr::get(MethodTy, VTableOffset);
26352636
}
2637+
2638+
/// The Itanium ABI requires non-zero initialization only for data
2639+
/// member pointers, for which '0' is a valid offset.
2640+
bool CIRGenItaniumCXXABI::isZeroInitializable(const MemberPointerType *MPT) {
2641+
return MPT->isMemberFunctionPointer();
2642+
}

clang/lib/CIR/CodeGen/CIRGenTypes.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "CIRGenTypes.h"
2+
#include "CIRGenCXXABI.h"
23
#include "CIRGenCall.h"
34
#include "CIRGenFunctionInfo.h"
45
#include "CIRGenModule.h"
@@ -919,7 +920,7 @@ bool CIRGenTypes::isZeroInitializable(QualType T) {
919920

920921
// We have to ask the ABI about member pointers.
921922
if (const MemberPointerType *MPT = T->getAs<MemberPointerType>())
922-
llvm_unreachable("NYI");
923+
return TheCXXABI.isZeroInitializable(MPT);
923924

924925
// Everything else is okay.
925926
return true;

clang/lib/CIR/CodeGen/CIRGenValue.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,10 @@ class LValue {
397397
tbaaInfo);
398398
return R;
399399
}
400+
401+
RValue asAggregateRValue() const {
402+
return RValue::getAggregate(getAddress(), isVolatileQualified());
403+
}
400404
};
401405

402406
/// An aggregate value slot.

0 commit comments

Comments
 (0)