[clang] [clang][bytecode] Implement __builtin_wmem{cpy, move} (PR #135969)

via cfe-commits cfe-commits at lists.llvm.org
Wed Apr 16 07:27:44 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Timm Baeder (tbaederr)

<details>
<summary>Changes</summary>



---
Full diff: https://github.com/llvm/llvm-project/pull/135969.diff


2 Files Affected:

- (modified) clang/lib/AST/ByteCode/InterpBuiltin.cpp (+22-8) 
- (modified) clang/test/AST/ByteCode/builtin-functions.cpp (+62) 


``````````diff
diff --git a/clang/lib/AST/ByteCode/InterpBuiltin.cpp b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
index bde416d98edd3..850564f1540ba 100644
--- a/clang/lib/AST/ByteCode/InterpBuiltin.cpp
+++ b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
@@ -1792,14 +1792,18 @@ static bool interp__builtin_memcpy(InterpState &S, CodePtr OpPC,
   Pointer DestPtr = getParam<Pointer>(Frame, 0);
   const ASTContext &ASTCtx = S.getASTContext();
   const Pointer &SrcPtr = getParam<Pointer>(Frame, 1);
-  const APSInt &Size =
-      peekToAPSInt(S.Stk, *S.getContext().classify(Call->getArg(2)));
+  APSInt Size = peekToAPSInt(S.Stk, *S.getContext().classify(Call->getArg(2)));
   assert(!Size.isSigned() && "memcpy and friends take an unsigned size");
 
   if (ID == Builtin::BImemcpy || ID == Builtin::BImemmove)
     diagnoseNonConstexprBuiltin(S, OpPC, ID);
 
-  bool Move = (ID == Builtin::BI__builtin_memmove || ID == Builtin::BImemmove);
+  bool Move =
+      (ID == Builtin::BI__builtin_memmove || ID == Builtin::BImemmove ||
+       ID == Builtin::BI__builtin_wmemmove || ID == Builtin::BIwmemmove);
+  bool WChar = ID == Builtin::BIwmemcpy || ID == Builtin::BIwmemmove ||
+               ID == Builtin::BI__builtin_wmemcpy ||
+               ID == Builtin::BI__builtin_wmemmove;
 
   // If the size is zero, we treat this as always being a valid no-op.
   if (Size.isZero()) {
@@ -1810,7 +1814,7 @@ static bool interp__builtin_memcpy(InterpState &S, CodePtr OpPC,
   if (SrcPtr.isZero() || DestPtr.isZero()) {
     Pointer DiagPtr = (SrcPtr.isZero() ? SrcPtr : DestPtr);
     S.FFDiag(S.Current->getSource(OpPC), diag::note_constexpr_memcpy_null)
-        << /*IsMove=*/Move << /*IsWchar=*/false << !SrcPtr.isZero()
+        << /*IsMove=*/Move << /*IsWchar=*/WChar << !SrcPtr.isZero()
         << DiagPtr.toDiagnosticString(ASTCtx);
     return false;
   }
@@ -1822,7 +1826,7 @@ static bool interp__builtin_memcpy(InterpState &S, CodePtr OpPC,
                    ? std::to_string(SrcPtr.getIntegerRepresentation())
                    : std::to_string(DestPtr.getIntegerRepresentation());
     S.FFDiag(S.Current->getSource(OpPC), diag::note_constexpr_memcpy_null)
-        << Move << false << DestPtr.isIntegralPointer() << DiagVal;
+        << Move << WChar << DestPtr.isIntegralPointer() << DiagVal;
     return false;
   }
 
@@ -1841,11 +1845,17 @@ static bool interp__builtin_memcpy(InterpState &S, CodePtr OpPC,
   }
   unsigned DestElemSize = ASTCtx.getTypeSizeInChars(DestElemType).getQuantity();
 
+  if (WChar) {
+    uint64_t WCharSize =
+        ASTCtx.getTypeSizeInChars(ASTCtx.getWCharType()).getQuantity();
+    Size *= APSInt(APInt(Size.getBitWidth(), WCharSize, /*IsSigned=*/false),
+                   /*IsUnsigend=*/true);
+  }
+
   if (Size.urem(DestElemSize) != 0) {
     S.FFDiag(S.Current->getSource(OpPC),
              diag::note_constexpr_memcpy_unsupported)
-        << Move << /*IsWchar=*/false << 0 << DestElemType << Size
-        << DestElemSize;
+        << Move << WChar << 0 << DestElemType << Size << DestElemSize;
     return false;
   }
 
@@ -1873,7 +1883,7 @@ static bool interp__builtin_memcpy(InterpState &S, CodePtr OpPC,
     APInt N = Size.udiv(DestElemSize);
     S.FFDiag(S.Current->getSource(OpPC),
              diag::note_constexpr_memcpy_unsupported)
-        << Move << /*IsWChar*/ false << (Size.ugt(RemainingSrcBytes) ? 1 : 2)
+        << Move << WChar << (Size.ugt(RemainingSrcBytes) ? 1 : 2)
         << DestElemType << toString(N, 10, /*Signed=*/false);
     return false;
   }
@@ -2591,8 +2601,12 @@ bool InterpretBuiltin(InterpState &S, CodePtr OpPC, const Function *F,
 
   case Builtin::BI__builtin_memcpy:
   case Builtin::BImemcpy:
+  case Builtin::BI__builtin_wmemcpy:
+  case Builtin::BIwmemcpy:
   case Builtin::BI__builtin_memmove:
   case Builtin::BImemmove:
+  case Builtin::BI__builtin_wmemmove:
+  case Builtin::BIwmemmove:
     if (!interp__builtin_memcpy(S, OpPC, Frame, F, Call))
       return false;
     break;
diff --git a/clang/test/AST/ByteCode/builtin-functions.cpp b/clang/test/AST/ByteCode/builtin-functions.cpp
index 40f7a18119751..a57b4530d2264 100644
--- a/clang/test/AST/ByteCode/builtin-functions.cpp
+++ b/clang/test/AST/ByteCode/builtin-functions.cpp
@@ -30,6 +30,7 @@ extern "C" {
   extern wchar_t *wcschr(const wchar_t *s, wchar_t c);
   extern int wcscmp(const wchar_t *s1, const wchar_t *s2);
   extern int wcsncmp(const wchar_t *s1, const wchar_t *s2, size_t n);
+  extern wchar_t *wmemcpy(wchar_t *d, const wchar_t *s, size_t n);
 }
 
 namespace strcmp {
@@ -1592,6 +1593,67 @@ namespace WMemChr {
                                               // both-note {{non-constexpr function 'wcschr' cannot be used in a constant expression}}
 }
 
+namespace WMemCpy {
+  template<typename T>
+  constexpr T result(T (&arr)[4]) {
+    return arr[0] * 1000 + arr[1] * 100 + arr[2] * 10 + arr[3];
+  }
+  constexpr int test_wmemcpy(int a, int b, int n) {
+    wchar_t arr[4] = {1, 2, 3, 4};
+    __builtin_wmemcpy(arr + a, arr + b, n);
+    // both-note at -1 2{{overlapping memory regions}}
+    // both-note at -2 {{source is not a contiguous array of at least 2 elements of type 'wchar_t'}}
+    // both-note at -3 {{destination is not a contiguous array of at least 3 elements of type 'wchar_t'}}
+    return result(arr);
+  }
+  static_assert(test_wmemcpy(1, 2, 1) == 1334);
+  static_assert(test_wmemcpy(2, 1, 1) == 1224);
+  static_assert(test_wmemcpy(0, 1, 2) == 2334); // both-error {{constant}} both-note {{in call}}
+  static_assert(test_wmemcpy(1, 0, 2) == 1124); // both-error {{constant}} both-note {{in call}}
+  static_assert(test_wmemcpy(1, 2, 1) == 1334);
+  static_assert(test_wmemcpy(0, 3, 1) == 4234);
+  static_assert(test_wmemcpy(0, 3, 2) == 4234); // both-error {{constant}} both-note {{in call}}
+  static_assert(test_wmemcpy(2, 0, 3) == 4234); // both-error {{constant}} both-note {{in call}}
+
+  wchar_t global;
+  constexpr wchar_t *null = 0;
+  static_assert(__builtin_wmemcpy(&global, null, sizeof(wchar_t))); // both-error {{}} \
+                                                                    // both-note {{source of 'wmemcpy' is nullptr}}
+  static_assert(__builtin_wmemcpy(null, &global, sizeof(wchar_t))); // both-error {{}} \
+                                                                    // both-note {{destination of 'wmemcpy' is nullptr}}
+}
+
+namespace WMemMove {
+  template<typename T>
+  constexpr T result(T (&arr)[4]) {
+    return arr[0] * 1000 + arr[1] * 100 + arr[2] * 10 + arr[3];
+  }
+
+  constexpr int test_wmemmove(int a, int b, int n) {
+    wchar_t arr[4] = {1, 2, 3, 4};
+    __builtin_wmemmove(arr + a, arr + b, n);
+    // both-note at -1 {{source is not a contiguous array of at least 2 elements of type 'wchar_t'}}
+    // both-note at -2 {{destination is not a contiguous array of at least 3 elements of type 'wchar_t'}}
+    return result(arr);
+  }
+
+  static_assert(test_wmemmove(1, 2, 1) == 1334);
+  static_assert(test_wmemmove(2, 1, 1) == 1224);
+  static_assert(test_wmemmove(0, 1, 2) == 2334);
+  static_assert(test_wmemmove(1, 0, 2) == 1124);
+  static_assert(test_wmemmove(1, 2, 1) == 1334);
+  static_assert(test_wmemmove(0, 3, 1) == 4234);
+  static_assert(test_wmemmove(0, 3, 2) == 4234); // both-error {{constant}} both-note {{in call}}
+  static_assert(test_wmemmove(2, 0, 3) == 4234); // both-error {{constant}} both-note {{in call}}
+
+  wchar_t global;
+  constexpr wchar_t *null = 0;
+  static_assert(__builtin_wmemmove(&global, null, sizeof(wchar_t))); // both-error {{}} \
+                                                                     // both-note {{source of 'wmemmove' is nullptr}}
+  static_assert(__builtin_wmemmove(null, &global, sizeof(wchar_t))); // both-error {{}} \
+                                                                     // both-note {{destination of 'wmemmove' is nullptr}}
+}
+
 namespace Invalid {
   constexpr int test() { // both-error {{never produces a constant expression}}
     __builtin_abort(); // both-note 2{{subexpression not valid in a constant expression}}

``````````

</details>


https://github.com/llvm/llvm-project/pull/135969


More information about the cfe-commits mailing list