Author: Peter Collingbourne
Date: 2025-05-23T17:02:49-07:00
New Revision: b20801646a72b936983261b249e2bc97987445e4

URL: 
https://github.com/llvm/llvm-project/commit/b20801646a72b936983261b249e2bc97987445e4
DIFF: 
https://github.com/llvm/llvm-project/commit/b20801646a72b936983261b249e2bc97987445e4.diff

LOG: 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: https://github.com/llvm/llvm-project/pull/140312

Added: 
    

Modified: 
    clang/lib/CodeGen/CGBuiltin.cpp
    clang/test/CodeGenCXX/cxx2c-trivially-relocatable.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp
index 809ffe549be88..ca29a6dbe9c9a 100644
--- a/clang/lib/CodeGen/CGBuiltin.cpp
+++ b/clang/lib/CodeGen/CGBuiltin.cpp
@@ -4453,6 +4453,14 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl 
GD, unsigned BuiltinID,
     Address Dest = EmitPointerWithAlignment(E->getArg(0));
     Address Src = EmitPointerWithAlignment(E->getArg(1));
     Value *SizeVal = EmitScalarExpr(E->getArg(2));
+    if (BuiltinIDIfNoAsmLabel == Builtin::BI__builtin_trivially_relocate)
+      SizeVal = Builder.CreateMul(
+          SizeVal,
+          ConstantInt::get(
+              SizeVal->getType(),
+              getContext()
+                  
.getTypeSizeInChars(E->getArg(0)->getType()->getPointeeType())
+                  .getQuantity()));
     EmitArgCheck(TCK_Store, Dest, E->getArg(0), 0);
     EmitArgCheck(TCK_Load, Src, E->getArg(1), 1);
     Builder.CreateMemMove(Dest, Src, SizeVal, false);

diff  --git a/clang/test/CodeGenCXX/cxx2c-trivially-relocatable.cpp 
b/clang/test/CodeGenCXX/cxx2c-trivially-relocatable.cpp
index 17144cffb6476..465e539d363e8 100644
--- a/clang/test/CodeGenCXX/cxx2c-trivially-relocatable.cpp
+++ b/clang/test/CodeGenCXX/cxx2c-trivially-relocatable.cpp
@@ -1,5 +1,7 @@
 // RUN: %clang_cc1 -std=c++26 -triple x86_64-linux-gnu -emit-llvm -o - %s | 
FileCheck %s
 
+typedef __SIZE_TYPE__ size_t;
+
 struct S trivially_relocatable_if_eligible {
     S(const S&);
     ~S();
@@ -8,9 +10,13 @@ struct S trivially_relocatable_if_eligible {
 };
 
 // CHECK: @_Z4testP1SS0_
-// CHECK: call void @llvm.memmove.p0.p0.i64
-// CHECK-NOT: __builtin
-// CHECK: ret
-void test(S* source, S* dest) {
+void test(S* source, S* dest, size_t count) {
+    // CHECK: call void @llvm.memmove.p0.p0.i64({{.*}}, i64 8
+    // CHECK-NOT: __builtin
     __builtin_trivially_relocate(dest, source, 1);
+    // CHECK: [[A:%.*]] = load i64, ptr %count.addr
+    // CHECK: [[M:%.*]] = mul i64 [[A]], 8
+    // CHECK: call void @llvm.memmove.p0.p0.i64({{.*}}, i64 [[M]]
+    __builtin_trivially_relocate(dest, source, count);
+    // CHECK: ret
 };


        
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to