[clang] [clang][bytecode] Initialize global strings via memcpy (PR #140789)
Timm Baeder via cfe-commits
cfe-commits at lists.llvm.org
Tue May 20 12:47:14 PDT 2025
https://github.com/tbaederr created https://github.com/llvm/llvm-project/pull/140789
If we know the char width is 1, we can just copy
the data over instead of going through the Pointer API.
>From a4209fc8d9e93d5a7e080bd3cfab0d6cef37a102 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Sat, 10 May 2025 07:01:26 +0200
Subject: [PATCH] [clang][bytecode] Initialize global strings via memcpy
If we know the char width is 1, we can just copy
the data over instead of going through the Pointer API.
---
clang/lib/AST/ByteCode/Program.cpp | 52 +++++++++++++++++-------------
1 file changed, 29 insertions(+), 23 deletions(-)
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();
More information about the cfe-commits
mailing list