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

[CIR][CIRGen] Emit memcpys for copy constructors #1197

Merged
merged 3 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion clang/include/clang/CIR/MissingFeatures.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@ struct MissingFeatures {
static bool shouldSplitConstantStore() { return false; }
static bool shouldCreateMemCpyFromGlobal() { return false; }
static bool shouldReverseUnaryCondOnBoolExpr() { return false; }
static bool fieldMemcpyizerBuildMemcpy() { return false; }
static bool isTrivialCtorOrDtor() { return false; }
static bool isMemcpyEquivalentSpecialMember() { return false; }
static bool constructABIArgDirectExtend() { return false; }
Expand Down Expand Up @@ -174,6 +173,7 @@ struct MissingFeatures {

// ABIInfo queries.
static bool useTargetLoweringABIInfo() { return false; }
static bool isEmptyFieldForLayout() { return false; }

// Misc
static bool cacheRecordLayouts() { return false; }
Expand Down
120 changes: 77 additions & 43 deletions clang/lib/CIR/CodeGen/CIRGenClass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,13 @@ class CopyingValueRepresentation {

class FieldMemcpyizer {
public:
FieldMemcpyizer(CIRGenFunction &CGF, const CXXRecordDecl *ClassDecl,
FieldMemcpyizer(CIRGenFunction &CGF, const CXXMethodDecl *MethodDecl,
const VarDecl *SrcRec)
: CGF(CGF), ClassDecl(ClassDecl),
// SrcRec(SrcRec),
: CGF(CGF), MethodDecl(MethodDecl), ClassDecl(MethodDecl->getParent()),
SrcRec(SrcRec),
RecLayout(CGF.getContext().getASTRecordLayout(ClassDecl)),
FirstField(nullptr), LastField(nullptr), FirstFieldOffset(0),
LastFieldOffset(0), LastAddedFieldIndex(0) {
(void)SrcRec;
}
LastFieldOffset(0), LastAddedFieldIndex(0) {}

bool isMemcpyableField(FieldDecl *F) const {
// Never memcpy fields when we are adding poised paddings.
Expand All @@ -115,11 +113,11 @@ class FieldMemcpyizer {
Qualifiers Qual = F->getType().getQualifiers();
if (Qual.hasVolatile() || Qual.hasObjCLifetime())
return false;

return true;
}

void addMemcpyableField(FieldDecl *F) {
assert(!cir::MissingFeatures::isEmptyFieldForLayout());
if (F->isZeroSize(CGF.getContext()))
return;
if (!FirstField)
Expand Down Expand Up @@ -148,18 +146,54 @@ class FieldMemcpyizer {
return;
}

llvm_unreachable("NYI");
uint64_t firstByteOffset;
if (FirstField->isBitField()) {
const CIRGenRecordLayout &rl =
CGF.getTypes().getCIRGenRecordLayout(FirstField->getParent());
const CIRGenBitFieldInfo &bfInfo = rl.getBitFieldInfo(FirstField);
// FirstFieldOffset is not appropriate for bitfields,
// we need to use the storage offset instead.
firstByteOffset = CGF.getContext().toBits(bfInfo.StorageOffset);
} else {
firstByteOffset = FirstFieldOffset;
}

CharUnits memcpySize = getMemcpySize(firstByteOffset);
QualType recordTy = CGF.getContext().getTypeDeclType(ClassDecl);
Address thisPtr = CGF.LoadCXXThisAddress();
LValue destLv = CGF.makeAddrLValue(thisPtr, recordTy);
LValue dest = CGF.emitLValueForFieldInitialization(destLv, FirstField,
FirstField->getName());
cir::LoadOp srcPtr = CGF.getBuilder().createLoad(
CGF.getLoc(MethodDecl->getLocation()), CGF.GetAddrOfLocalVar(SrcRec));
LValue srcLv = CGF.MakeNaturalAlignAddrLValue(srcPtr, recordTy);
LValue src = CGF.emitLValueForFieldInitialization(srcLv, FirstField,
FirstField->getName());

emitMemcpyIR(dest.isBitField() ? dest.getBitFieldAddress()
: dest.getAddress(),
src.isBitField() ? src.getBitFieldAddress() : src.getAddress(),
memcpySize);
reset();
}

void reset() { FirstField = nullptr; }

protected:
CIRGenFunction &CGF;
const CXXMethodDecl *MethodDecl;
const CXXRecordDecl *ClassDecl;

private:
void emitMemcpyIR(Address DestPtr, Address SrcPtr, CharUnits Size) {
llvm_unreachable("NYI");
mlir::Location loc = CGF.getLoc(MethodDecl->getLocation());
cir::ConstantOp sizeOp =
CGF.getBuilder().getConstInt(loc, CGF.SizeTy, Size.getQuantity());
mlir::Value dest =
CGF.getBuilder().createBitcast(DestPtr.getPointer(), CGF.VoidPtrTy);
mlir::Value src =
CGF.getBuilder().createBitcast(SrcPtr.getPointer(), CGF.VoidPtrTy);
CGF.getBuilder().createMemCpy(loc, dest, src, sizeOp);
}

void addInitialField(FieldDecl *F) {
Expand Down Expand Up @@ -192,7 +226,7 @@ class FieldMemcpyizer {
}
}

// const VarDecl *SrcRec;
const VarDecl *SrcRec;
const ASTRecordLayout &RecLayout;
FieldDecl *FirstField;
FieldDecl *LastField;
Expand Down Expand Up @@ -299,16 +333,29 @@ class ConstructorMemcpyizer : public FieldMemcpyizer {
bool isMemberInitMemcpyable(CXXCtorInitializer *MemberInit) const {
if (!MemcpyableCtor)
return false;
FieldDecl *field = MemberInit->getMember();
assert(field && "No field for member init.");
QualType fieldType = field->getType();
CXXConstructExpr *ce = dyn_cast<CXXConstructExpr>(MemberInit->getInit());

// Bail out on any members of record type (unlike CodeGen, which emits a
// memcpy for trivially-copyable record types).
if (ce || (fieldType->isArrayType() &&
CGF.getContext().getBaseElementType(fieldType)->isRecordType()))
return false;

assert(!cir::MissingFeatures::fieldMemcpyizerBuildMemcpy());
return false;
// Bail out on volatile fields.
if (!isMemcpyableField(field))
return false;

// Otherwise we're good.
return true;
}

public:
ConstructorMemcpyizer(CIRGenFunction &CGF, const CXXConstructorDecl *CD,
FunctionArgList &Args)
: FieldMemcpyizer(CGF, CD->getParent(),
getTrivialCopySource(CGF, CD, Args)),
: FieldMemcpyizer(CGF, CD, getTrivialCopySource(CGF, CD, Args)),
ConstructorDecl(CD),
MemcpyableCtor(CD->isDefaulted() && CD->isCopyOrMoveConstructor() &&
CGF.getLangOpts().getGC() == LangOptions::NonGC),
Expand All @@ -330,7 +377,10 @@ class ConstructorMemcpyizer : public FieldMemcpyizer {
// This memcpy is too small to be worthwhile. Fall back on default
// codegen.
if (!AggregatedInits.empty()) {
llvm_unreachable("NYI");
CopyingValueRepresentation cvr(CGF);
emitMemberInitializer(CGF, ConstructorDecl->getParent(),
AggregatedInits[0], ConstructorDecl, Args);
AggregatedInits.clear();
}
reset();
return;
Expand All @@ -342,21 +392,14 @@ class ConstructorMemcpyizer : public FieldMemcpyizer {
}

void pushEHDestructors() {
Address ThisPtr = CGF.LoadCXXThisAddress();
QualType RecordTy = CGF.getContext().getTypeDeclType(ClassDecl);
LValue LHS = CGF.makeAddrLValue(ThisPtr, RecordTy);
(void)LHS;

for (unsigned i = 0; i < AggregatedInits.size(); ++i) {
CXXCtorInitializer *MemberInit = AggregatedInits[i];
QualType FieldType = MemberInit->getAnyMember()->getType();
QualType::DestructionKind dtorKind = FieldType.isDestructedType();
if (!CGF.needsEHCleanup(dtorKind))
continue;
LValue FieldLHS = LHS;
emitLValueForAnyFieldInitialization(CGF, MemberInit, FieldLHS);
CGF.pushEHDestroy(dtorKind, FieldLHS.getAddress(), FieldType);
#ifndef NDEBUG
for (CXXCtorInitializer *memberInit : AggregatedInits) {
QualType fieldType = memberInit->getAnyMember()->getType();
QualType::DestructionKind dtorKind = fieldType.isDestructedType();
assert(!CGF.needsEHCleanup(dtorKind) &&
"Non-record types shouldn't need EH cleanup");
}
#endif
}

void finish() { emitAggregatedInits(); }
Expand Down Expand Up @@ -396,20 +439,11 @@ class AssignmentMemcpyizer : public FieldMemcpyizer {
}
return nullptr;
} else if (CXXMemberCallExpr *MCE = dyn_cast<CXXMemberCallExpr>(S)) {
CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(MCE->getCalleeDecl());
if (!(MD && isMemcpyEquivalentSpecialMember(MD)))
return nullptr;
MemberExpr *IOA = dyn_cast<MemberExpr>(MCE->getImplicitObjectArgument());
if (!IOA)
return nullptr;
FieldDecl *Field = dyn_cast<FieldDecl>(IOA->getMemberDecl());
if (!Field || !isMemcpyableField(Field))
return nullptr;
MemberExpr *Arg0 = dyn_cast<MemberExpr>(MCE->getArg(0));
if (!Arg0 || Field != dyn_cast<FieldDecl>(Arg0->getMemberDecl()))
return nullptr;
return Field;
// We want to represent all calls explicitly for analysis purposes.
return nullptr;
} else if (CallExpr *CE = dyn_cast<CallExpr>(S)) {
// TODO(cir): https://github.com/llvm/clangir/issues/1177: This can result
// in memcpys instead of calls to trivial member functions.
FunctionDecl *FD = dyn_cast<FunctionDecl>(CE->getCalleeDecl());
if (!FD || FD->getBuiltinID() != Builtin::BI__builtin_memcpy)
return nullptr;
Expand Down Expand Up @@ -446,7 +480,7 @@ class AssignmentMemcpyizer : public FieldMemcpyizer {
public:
AssignmentMemcpyizer(CIRGenFunction &CGF, const CXXMethodDecl *AD,
FunctionArgList &Args)
: FieldMemcpyizer(CGF, AD->getParent(), Args[Args.size() - 1]),
: FieldMemcpyizer(CGF, AD, Args[Args.size() - 1]),
AssignmentsMemcpyable(CGF.getLangOpts().getGC() == LangOptions::NonGC) {
assert(Args.size() == 2);
}
Expand Down
122 changes: 122 additions & 0 deletions clang/test/CIR/CodeGen/assign-operator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,125 @@ int main() {
// CHECK: %2 = cir.load %0 : !cir.ptr<!s32i>, !s32i
// CHECK: cir.return %2 : !s32i
// CHECK: }

struct HasNonTrivialAssignOp {
HasNonTrivialAssignOp &operator=(const HasNonTrivialAssignOp &);
};

struct ContainsNonTrivial {
HasNonTrivialAssignOp start;
int i;
int *j;
HasNonTrivialAssignOp middle;
int k : 4;
int l : 4;
int m : 4;
HasNonTrivialAssignOp end;
ContainsNonTrivial &operator=(const ContainsNonTrivial &);
};

// CHECK-LABEL: cir.func @_ZN18ContainsNonTrivialaSERKS_(
// CHECK-NEXT: %[[#THIS:]] = cir.alloca !cir.ptr<!ty_ContainsNonTrivial>
// CHECK-NEXT: %[[#OTHER:]] = cir.alloca !cir.ptr<!ty_ContainsNonTrivial>
// CHECK-NEXT: %[[#RETVAL:]] = cir.alloca !cir.ptr<!ty_ContainsNonTrivial>
// CHECK-NEXT: cir.store %arg0, %[[#THIS]]
// CHECK-NEXT: cir.store %arg1, %[[#OTHER]]
// CHECK-NEXT: %[[#THIS_LOAD:]] = cir.load deref %[[#THIS]]
// CHECK-NEXT: %[[#THIS_START:]] = cir.get_member %[[#THIS_LOAD]][0] {name = "start"}
// CHECK-NEXT: %[[#OTHER_LOAD:]] = cir.load %[[#OTHER]]
// CHECK-NEXT: %[[#OTHER_START:]] = cir.get_member %[[#OTHER_LOAD]][0] {name = "start"}
// CHECK-NEXT: cir.call @_ZN21HasNonTrivialAssignOpaSERKS_(%[[#THIS_START]], %[[#OTHER_START]])
// CHECK-NEXT: %[[#THIS_I:]] = cir.get_member %[[#THIS_LOAD]][2] {name = "i"}
// CHECK-NEXT: %[[#OTHER_LOAD:]] = cir.load %[[#OTHER]]
// CHECK-NEXT: %[[#OTHER_I:]] = cir.get_member %[[#OTHER_LOAD]][2] {name = "i"}
// CHECK-NEXT: %[[#MEMCPY_SIZE:]] = cir.const #cir.int<12> : !u64i
// CHECK-NEXT: %[[#THIS_I_CAST:]] = cir.cast(bitcast, %[[#THIS_I]] : !cir.ptr<!s32i>), !cir.ptr<!void>
// CHECK-NEXT: %[[#OTHER_I_CAST:]] = cir.cast(bitcast, %[[#OTHER_I]] : !cir.ptr<!s32i>), !cir.ptr<!void>
// CHECK-NEXT: cir.libc.memcpy %[[#MEMCPY_SIZE]] bytes from %[[#OTHER_I_CAST]] to %[[#THIS_I_CAST]]
// CHECK-NEXT: %[[#THIS_MIDDLE:]] = cir.get_member %[[#THIS_LOAD]][4] {name = "middle"}
// CHECK-NEXT: %[[#OTHER_LOAD:]] = cir.load %[[#OTHER]]
// CHECK-NEXT: %[[#OTHER_MIDDLE:]] = cir.get_member %[[#OTHER_LOAD]][4] {name = "middle"}
// CHECK-NEXT: cir.call @_ZN21HasNonTrivialAssignOpaSERKS_(%[[#THIS_MIDDLE]], %[[#OTHER_MIDDLE]])
// CHECK-NEXT: %[[#THIS_K:]] = cir.get_member %[[#THIS_LOAD]][5] {name = "k"}
// CHECK-NEXT: %[[#OTHER_LOAD:]] = cir.load %[[#OTHER]]
// CHECK-NEXT: %[[#OTHER_K:]] = cir.get_member %[[#OTHER_LOAD]][5] {name = "k"}
// CHECK-NEXT: %[[#MEMCPY_SIZE:]] = cir.const #cir.int<2> : !u64i
// CHECK-NEXT: %[[#THIS_K_CAST:]] = cir.cast(bitcast, %[[#THIS_K]] : !cir.ptr<!u16i>), !cir.ptr<!void>
// CHECK-NEXT: %[[#OTHER_K_CAST:]] = cir.cast(bitcast, %[[#OTHER_K]] : !cir.ptr<!u16i>), !cir.ptr<!void>
// CHECK-NEXT: cir.libc.memcpy %[[#MEMCPY_SIZE]] bytes from %[[#OTHER_K_CAST]] to %[[#THIS_K_CAST]]
// CHECK-NEXT: %[[#THIS_END:]] = cir.get_member %[[#THIS_LOAD]][6] {name = "end"}
// CHECK-NEXT: %[[#OTHER_LOAD:]] = cir.load %[[#OTHER]]
// CHECK-NEXT: %[[#OTHER_END:]] = cir.get_member %[[#OTHER_LOAD]][6] {name = "end"}
// CHECK-NEXT: cir.call @_ZN21HasNonTrivialAssignOpaSERKS_(%[[#THIS_END]], %[[#OTHER_END]])
// CHECK-NEXT: cir.store %[[#THIS_LOAD]], %[[#RETVAL]]
// CHECK-NEXT: %[[#RETVAL_LOAD:]] = cir.load %[[#RETVAL]]
// CHECK-NEXT: cir.return %[[#RETVAL_LOAD]]
// CHECK-NEXT: }
ContainsNonTrivial &
ContainsNonTrivial::operator=(const ContainsNonTrivial &) = default;

struct Trivial {
int i;
int *j;
double k;
int l[3];
};

// CHECK-LABEL: cir.func linkonce_odr @_ZN7TrivialaSERKS_(
// CHECK-NEXT: %[[#THIS:]] = cir.alloca !cir.ptr<!ty_Trivial>
// CHECK-NEXT: %[[#OTHER:]] = cir.alloca !cir.ptr<!ty_Trivial>
// CHECK-NEXT: %[[#RETVAL:]] = cir.alloca !cir.ptr<!ty_Trivial>
// CHECK-NEXT: cir.store %arg0, %[[#THIS]]
// CHECK-NEXT: cir.store %arg1, %[[#OTHER]]
// CHECK-NEXT: %[[#THIS_LOAD:]] = cir.load deref %[[#THIS]]
// CHECK-NEXT: %[[#THIS_I:]] = cir.get_member %[[#THIS_LOAD]][0] {name = "i"}
// CHECK-NEXT: %[[#OTHER_LOAD:]] = cir.load %[[#OTHER]]
// CHECK-NEXT: %[[#OTHER_I:]] = cir.get_member %[[#OTHER_LOAD]][0] {name = "i"}
// Note that tail padding bytes are not included.
// CHECK-NEXT: %[[#MEMCPY_SIZE:]] = cir.const #cir.int<36> : !u64i
// CHECK-NEXT: %[[#THIS_I_CAST:]] = cir.cast(bitcast, %[[#THIS_I]] : !cir.ptr<!s32i>), !cir.ptr<!void>
// CHECK-NEXT: %[[#OTHER_I_CAST:]] = cir.cast(bitcast, %[[#OTHER_I]] : !cir.ptr<!s32i>), !cir.ptr<!void>
// CHECK-NEXT: cir.libc.memcpy %[[#MEMCPY_SIZE]] bytes from %[[#OTHER_I_CAST]] to %[[#THIS_I_CAST]]
// CHECK-NEXT: cir.store %[[#THIS_LOAD]], %[[#RETVAL]]
// CHECK-NEXT: cir.br ^bb1
// CHECK-NEXT: ^bb1:
// CHECK-NEXT: %[[#RETVAL_LOAD:]] = cir.load %[[#RETVAL]]
// CHECK-NEXT: cir.return %[[#RETVAL_LOAD]]
// CHECK-NEXT: }

// We should explicitly call operator= even for trivial types.
// CHECK-LABEL: cir.func @_Z11copyTrivialR7TrivialS0_(
// CHECK: cir.call @_ZN7TrivialaSERKS_(
void copyTrivial(Trivial &a, Trivial &b) {
a = b;
}

struct ContainsTrivial {
Trivial t1;
Trivial t2;
ContainsTrivial &operator=(const ContainsTrivial &);
};

// We should explicitly call operator= even for trivial types.
// CHECK-LABEL: cir.func @_ZN15ContainsTrivialaSERKS_(
// CHECK: cir.call @_ZN7TrivialaSERKS_(
// CHECK: cir.call @_ZN7TrivialaSERKS_(
ContainsTrivial &ContainsTrivial::operator=(const ContainsTrivial &) = default;

struct ContainsTrivialArray {
Trivial arr[2];
ContainsTrivialArray &operator=(const ContainsTrivialArray &);
};

// We should be calling operator= here but don't currently.
// CHECK-LABEL: cir.func @_ZN20ContainsTrivialArrayaSERKS_(
// CHECK: %[[#THIS_LOAD:]] = cir.load deref %[[#]]
// CHECK-NEXT: %[[#THIS_ARR:]] = cir.get_member %[[#THIS_LOAD]][0] {name = "arr"}
// CHECK-NEXT: %[[#THIS_ARR_CAST:]] = cir.cast(bitcast, %[[#THIS_ARR]] : !cir.ptr<!cir.array<!ty_Trivial x 2>>), !cir.ptr<!void>
// CHECK-NEXT: %[[#OTHER_LOAD:]] = cir.load %[[#]]
// CHECK-NEXT: %[[#OTHER_ARR:]] = cir.get_member %[[#OTHER_LOAD]][0] {name = "arr"}
// CHECK-NEXT: %[[#OTHER_ARR_CAST:]] = cir.cast(bitcast, %[[#OTHER_ARR]] : !cir.ptr<!cir.array<!ty_Trivial x 2>>), !cir.ptr<!void>
// CHECK-NEXT: %[[#MEMCPY_SIZE:]] = cir.const #cir.int<80> : !u64i
// CHECK-NEXT: cir.libc.memcpy %[[#MEMCPY_SIZE]] bytes from %[[#OTHER_ARR_CAST]] to %[[#THIS_ARR_CAST]]
ContainsTrivialArray &
ContainsTrivialArray::operator=(const ContainsTrivialArray &) = default;
Loading
Loading