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] Support emitting memcpys for fields #1195

Merged
merged 1 commit 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
1 change: 1 addition & 0 deletions clang/include/clang/CIR/MissingFeatures.h
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ struct MissingFeatures {

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

// Misc
static bool cacheRecordLayouts() { return false; }
Expand Down
59 changes: 46 additions & 13 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 @@ -307,8 +341,7 @@ class ConstructorMemcpyizer : public FieldMemcpyizer {
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 Down Expand Up @@ -446,7 +479,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
92 changes: 92 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,95 @@ 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;
}
Loading