Skip to content

Commit b208016

Browse files
authored
CodeGen: Fix implementation of __builtin_trivially_relocate.
The builtin is documented to copy `count` elements, but the implementation copies `count` bytes. Fix that. Reviewers: cor3ntin, ojhunt Pull Request: #140312
1 parent 26fe803 commit b208016

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

clang/lib/CodeGen/CGBuiltin.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4453,6 +4453,14 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
44534453
Address Dest = EmitPointerWithAlignment(E->getArg(0));
44544454
Address Src = EmitPointerWithAlignment(E->getArg(1));
44554455
Value *SizeVal = EmitScalarExpr(E->getArg(2));
4456+
if (BuiltinIDIfNoAsmLabel == Builtin::BI__builtin_trivially_relocate)
4457+
SizeVal = Builder.CreateMul(
4458+
SizeVal,
4459+
ConstantInt::get(
4460+
SizeVal->getType(),
4461+
getContext()
4462+
.getTypeSizeInChars(E->getArg(0)->getType()->getPointeeType())
4463+
.getQuantity()));
44564464
EmitArgCheck(TCK_Store, Dest, E->getArg(0), 0);
44574465
EmitArgCheck(TCK_Load, Src, E->getArg(1), 1);
44584466
Builder.CreateMemMove(Dest, Src, SizeVal, false);
Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// RUN: %clang_cc1 -std=c++26 -triple x86_64-linux-gnu -emit-llvm -o - %s | FileCheck %s
22

3+
typedef __SIZE_TYPE__ size_t;
4+
35
struct S trivially_relocatable_if_eligible {
46
S(const S&);
57
~S();
@@ -8,9 +10,13 @@ struct S trivially_relocatable_if_eligible {
810
};
911

1012
// CHECK: @_Z4testP1SS0_
11-
// CHECK: call void @llvm.memmove.p0.p0.i64
12-
// CHECK-NOT: __builtin
13-
// CHECK: ret
14-
void test(S* source, S* dest) {
13+
void test(S* source, S* dest, size_t count) {
14+
// CHECK: call void @llvm.memmove.p0.p0.i64({{.*}}, i64 8
15+
// CHECK-NOT: __builtin
1516
__builtin_trivially_relocate(dest, source, 1);
17+
// CHECK: [[A:%.*]] = load i64, ptr %count.addr
18+
// CHECK: [[M:%.*]] = mul i64 [[A]], 8
19+
// CHECK: call void @llvm.memmove.p0.p0.i64({{.*}}, i64 [[M]]
20+
__builtin_trivially_relocate(dest, source, count);
21+
// CHECK: ret
1622
};

0 commit comments

Comments
 (0)