[clang] [clang][bytecode] Initialize global strings via memcpy (PR #140789)
via cfe-commits
cfe-commits at lists.llvm.org
Tue May 20 12:47:51 PDT 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Timm Baeder (tbaederr)
<details>
<summary>Changes</summary>
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
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();
``````````
</details>
https://github.com/llvm/llvm-project/pull/140789
More information about the cfe-commits
mailing list