Skip to content

Conversation

@tbaederr
Copy link
Contributor

If we know the char width is 1, we can just copy
the data over instead of going through the Pointer API.

If we know the char width is 1, we can just copy
the data over instead of going through the Pointer API.
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" clang:bytecode Issues for the clang bytecode constexpr interpreter labels May 20, 2025
@llvmbot
Copy link
Member

llvmbot commented May 20, 2025

@llvm/pr-subscribers-clang

Author: Timm Baeder (tbaederr)

Changes

If we know the char width is 1, we can just copy
the data over instead of going through the Pointer API.


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

1 Files Affected:

  • (modified) clang/lib/AST/ByteCode/Program.cpp (+29-23)
diff --git a/clang/lib/AST/ByteCode/Program.cpp b/clang/lib/AST/ByteCode/Program.cpp
index 8b0b07f42e3f3..5ac0f59f32d4e 100644
--- a/clang/lib/AST/ByteCode/Program.cpp
+++ b/clang/lib/AST/ByteCode/Program.cpp
@@ -68,32 +68,38 @@ unsigned Program::createGlobalString(const StringLiteral *S, const Expr *Base) {
                                        /*isExtern=*/false);
   G->block()->invokeCtor();
 
-  new (G->block()->rawData()) InlineDescriptor(Desc);
+  new (G->block()->rawData())
+      GlobalInlineDescriptor{GlobalInitState::Initialized};
   Globals.push_back(G);
 
-  // Construct the string in storage.
   const Pointer Ptr(G->block());
-  for (unsigned I = 0; I <= StringLength; ++I) {
-    Pointer Field = Ptr.atIndex(I);
-    const uint32_t CodePoint = I == StringLength ? 0 : S->getCodeUnit(I);
-    switch (CharType) {
-    case PT_Sint8: {
-      using T = PrimConv<PT_Sint8>::T;
-      Field.deref<T>() = T::from(CodePoint, BitWidth);
-      break;
-    }
-    case PT_Uint16: {
-      using T = PrimConv<PT_Uint16>::T;
-      Field.deref<T>() = T::from(CodePoint, BitWidth);
-      break;
-    }
-    case PT_Uint32: {
-      using T = PrimConv<PT_Uint32>::T;
-      Field.deref<T>() = T::from(CodePoint, BitWidth);
-      break;
-    }
-    default:
-      llvm_unreachable("unsupported character type");
+  if (CharWidth == 1) {
+    std::memcpy(&Ptr.atIndex(0).deref<char>(), S->getString().data(),
+                StringLength);
+  } else {
+    // Construct the string in storage.
+    for (unsigned I = 0; I <= StringLength; ++I) {
+      Pointer Field = Ptr.atIndex(I);
+      const uint32_t CodePoint = I == StringLength ? 0 : S->getCodeUnit(I);
+      switch (CharType) {
+      case PT_Sint8: {
+        using T = PrimConv<PT_Sint8>::T;
+        Field.deref<T>() = T::from(CodePoint, BitWidth);
+        break;
+      }
+      case PT_Uint16: {
+        using T = PrimConv<PT_Uint16>::T;
+        Field.deref<T>() = T::from(CodePoint, BitWidth);
+        break;
+      }
+      case PT_Uint32: {
+        using T = PrimConv<PT_Uint32>::T;
+        Field.deref<T>() = T::from(CodePoint, BitWidth);
+        break;
+      }
+      default:
+        llvm_unreachable("unsupported character type");
+      }
     }
   }
   Ptr.initialize();

@tbaederr tbaederr merged commit 645846d into llvm:main May 21, 2025
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:bytecode Issues for the clang bytecode constexpr interpreter clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants