[clang] [clang][bytecode] Optimize initializing arrays from a string literal (PR #247508)
Timm Baeder via cfe-commits
cfe-commits at lists.llvm.org
Tue Dec 1 00:31:18 PST 2026
https://github.com/tbaederr created https://github.com/llvm/llvm-project/pull/247508
Add a `CopyString` op instead of copying each element on its own. This saves us ~15% on the new `AttrDocTable.cpp`.
>From d1aa30960ed37401d32db25f69124e056e0a5095 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Tue, 1 Dec 2026 08:12:30 +0100
Subject: [PATCH] CopyString
---
clang/lib/AST/ByteCode/Compiler.cpp | 43 ++++++++---------------------
clang/lib/AST/ByteCode/Interp.h | 22 +++++++++++++++
clang/lib/AST/ByteCode/Opcodes.td | 6 ++++
3 files changed, 40 insertions(+), 31 deletions(-)
diff --git a/clang/lib/AST/ByteCode/Compiler.cpp b/clang/lib/AST/ByteCode/Compiler.cpp
index 0455118fc50853..0e94000c80cb91 100644
--- a/clang/lib/AST/ByteCode/Compiler.cpp
+++ b/clang/lib/AST/ByteCode/Compiler.cpp
@@ -3213,51 +3213,32 @@ bool Compiler<Emitter>::VisitStringLiteral(const StringLiteral *E) {
if (DiscardResult)
return true;
+ if (!this->emitGetStringPtr(E, E))
+ return false;
+
+ // If we're not initializing, the new string pointer above is all we need.
if (!Initializing)
- return this->emitGetStringPtr(E, E);
+ return true;
- // We are initializing an array on the stack.
const ConstantArrayType *CAT =
Ctx.getASTContext().getAsConstantArrayType(E->getType());
assert(CAT && "a string literal that's not a constant array?");
+ PrimType T = classifyPrim(CAT->getElementType());
// If the initializer string is too long, a diagnostic has already been
// emitted. Read only the array length from the string literal.
unsigned ArraySize = CAT->getZExtSize();
unsigned N = std::min(ArraySize, E->getLength());
- unsigned CharWidth = E->getCharByteWidth();
- for (unsigned I = 0; I != N; ++I) {
- uint32_t CodeUnit = E->getCodeUnit(I);
-
- if (CharWidth == 1) {
- this->emitConstSint8(CodeUnit, E);
- this->emitInitElemSint8(I, E);
- } else if (CharWidth == 2) {
- this->emitConstUint16(CodeUnit, E);
- this->emitInitElemUint16(I, E);
- } else if (CharWidth == 4) {
- this->emitConstUint32(CodeUnit, E);
- this->emitInitElemUint32(I, E);
- } else {
- llvm_unreachable("unsupported character width");
- }
- }
+ if (!this->emitCopyString(T, E))
+ return false;
// Fill up the rest of the char array with NUL bytes.
for (unsigned I = N; I != ArraySize; ++I) {
- if (CharWidth == 1) {
- this->emitConstSint8(0, E);
- this->emitInitElemSint8(I, E);
- } else if (CharWidth == 2) {
- this->emitConstUint16(0, E);
- this->emitInitElemUint16(I, E);
- } else if (CharWidth == 4) {
- this->emitConstUint32(0, E);
- this->emitInitElemUint32(I, E);
- } else {
- llvm_unreachable("unsupported character width");
- }
+ if (!this->emitConst(0, T, E))
+ return false;
+ if (!this->emitInitElem(T, I, E))
+ return false;
}
return true;
diff --git a/clang/lib/AST/ByteCode/Interp.h b/clang/lib/AST/ByteCode/Interp.h
index f43c6c9227200d..2dafb68beb70c7 100644
--- a/clang/lib/AST/ByteCode/Interp.h
+++ b/clang/lib/AST/ByteCode/Interp.h
@@ -3665,6 +3665,28 @@ inline bool GetStringPtr(InterpState &S, const Expr *Base) {
return true;
}
+template <PrimType Name, class T = typename PrimConv<Name>::T>
+inline bool CopyString(InterpState &S) {
+ const Pointer Src = S.Stk.pop<Pointer>();
+ const Pointer Dst = S.Stk.peek<Pointer>();
+
+ assert(Src.isStringPointer());
+ if (!Dst.isBlockPointer() || Dst.isUnknownSizeArray())
+ return false;
+
+ unsigned SrcSize = Src.getNumElems();
+ unsigned DstStart = 0;
+ unsigned DstSize = Dst.getNumElems();
+
+ unsigned N = std::min(SrcSize, DstSize - DstStart);
+ for (unsigned I = 0; I != N; ++I) {
+ unsigned Index = DstStart + I;
+ Dst.elem<T>(Index) = Src.loadElem<T>(I);
+ Dst.initializeElement(Index);
+ }
+ return true;
+}
+
bool GetMemberPtr(InterpState &S, const ValueDecl *D);
bool GetMemberPtrBase(InterpState &S);
bool GetMemberPtrDecl(InterpState &S);
diff --git a/clang/lib/AST/ByteCode/Opcodes.td b/clang/lib/AST/ByteCode/Opcodes.td
index 18c265a5dee5c1..00ebdbf6aa1dde 100644
--- a/clang/lib/AST/ByteCode/Opcodes.td
+++ b/clang/lib/AST/ByteCode/Opcodes.td
@@ -1079,6 +1079,12 @@ def GetStringPtr : SuccessOpcode {
let Args = [ArgExpr];
}
+def CopyString : Opcode {
+ let Types = [FixedSizeIntegralTypeClass];
+ let HasGroup = 1;
+ let NeedsOpPC = 0;
+}
+
def InvalidAccessIndexAttr : Opcode {
let Args = [ArgValueDecl, ArgTypePtr];
}
More information about the cfe-commits
mailing list