aboutsummaryrefslogtreecommitdiff
path: root/clang/lib/AST/ByteCode/Interp.cpp
diff options
context:
space:
mode:
authorTimm Baeder <tbaeder@redhat.com>2025-06-17 18:31:06 +0200
committerGitHub <noreply@github.com>2025-06-17 18:31:06 +0200
commitc66be289901b3f035187d391e80e3610d7d6232e (patch)
treea703b857bb4b2441ca2fe7a8bbcf873c1080d7ab /clang/lib/AST/ByteCode/Interp.cpp
parent65d590e8d012df9dabbf8b3ec929fd1543c7398a (diff)
downloadllvm-c66be289901b3f035187d391e80e3610d7d6232e.zip
llvm-c66be289901b3f035187d391e80e3610d7d6232e.tar.gz
llvm-c66be289901b3f035187d391e80e3610d7d6232e.tar.bz2
[clang][bytecode] Allocate IntegralAP and Floating types using an allocator (#144246)
Both `APInt` and `APFloat` will heap-allocate memory themselves using the system allocator when the size of their data exceeds 64 bits. This is why clang has `APNumericStorage`, which allocates its memory using an allocator (via `ASTContext`) instead. Calling `getValue()` on an ast node like that will then create a new `APInt`/`APFloat` , which will copy the data (in the `APFloat` case, we even copy it twice). That's sad but whatever. In the bytecode interpreter, we have a similar problem. Large integers and floating-point values are placement-new allocated into the `InterpStack` (or into the bytecode, which is a `vector<std::byte>`). When we then later interrupt interpretation, we don't run the destructor for all items on the stack, which means we leak the memory the `APInt`/`APFloat` (which backs the `IntegralAP`/`Floating` the interpreter uses). Fix this by using an approach similar to the one used in the AST. Add an allocator to `InterpState`, which is used for temporaries and local values. Those values will be freed at the end of interpretation. For global variables, we need to promote the values to global lifetime, which we do via `InitGlobal` and `FinishInitGlobal` ops. Interestingly, this results in a slight _improvement_ in compile times: https://llvm-compile-time-tracker.com/compare.php?from=6bfcdda9b1ddf0900f82f7e30cb5e3253a791d50&to=88d1d899127b408f0fb0f385c2c58e6283195049&stat=instructions:u (but don't ask me why). Fixes https://github.com/llvm/llvm-project/issues/139012
Diffstat (limited to 'clang/lib/AST/ByteCode/Interp.cpp')
-rw-r--r--clang/lib/AST/ByteCode/Interp.cpp106
1 files changed, 102 insertions, 4 deletions
diff --git a/clang/lib/AST/ByteCode/Interp.cpp b/clang/lib/AST/ByteCode/Interp.cpp
index 5c8abff..1e2032f 100644
--- a/clang/lib/AST/ByteCode/Interp.cpp
+++ b/clang/lib/AST/ByteCode/Interp.cpp
@@ -1935,8 +1935,10 @@ bool CastPointerIntegralAP(InterpState &S, CodePtr OpPC, uint32_t BitWidth) {
if (!CheckPointerToIntegralCast(S, OpPC, Ptr, BitWidth))
return false;
- S.Stk.push<IntegralAP<false>>(
- IntegralAP<false>::from(Ptr.getIntegerRepresentation(), BitWidth));
+ auto Result = S.allocAP<IntegralAP<false>>(BitWidth);
+ Result.copy(APInt(BitWidth, Ptr.getIntegerRepresentation()));
+
+ S.Stk.push<IntegralAP<false>>(Result);
return true;
}
@@ -1946,8 +1948,10 @@ bool CastPointerIntegralAPS(InterpState &S, CodePtr OpPC, uint32_t BitWidth) {
if (!CheckPointerToIntegralCast(S, OpPC, Ptr, BitWidth))
return false;
- S.Stk.push<IntegralAP<true>>(
- IntegralAP<true>::from(Ptr.getIntegerRepresentation(), BitWidth));
+ auto Result = S.allocAP<IntegralAP<true>>(BitWidth);
+ Result.copy(APInt(BitWidth, Ptr.getIntegerRepresentation()));
+
+ S.Stk.push<IntegralAP<true>>(Result);
return true;
}
@@ -2053,6 +2057,100 @@ bool arePotentiallyOverlappingStringLiterals(const Pointer &LHS,
return Shorter == Longer.take_front(Shorter.size());
}
+static void copyPrimitiveMemory(InterpState &S, const Pointer &Ptr,
+ PrimType T) {
+
+ if (T == PT_IntAPS) {
+ auto &Val = Ptr.deref<IntegralAP<true>>();
+ if (!Val.singleWord()) {
+ uint64_t *NewMemory = new (S.P) uint64_t[Val.numWords()];
+ Val.take(NewMemory);
+ }
+ } else if (T == PT_IntAP) {
+ auto &Val = Ptr.deref<IntegralAP<false>>();
+ if (!Val.singleWord()) {
+ uint64_t *NewMemory = new (S.P) uint64_t[Val.numWords()];
+ Val.take(NewMemory);
+ }
+ } else if (T == PT_Float) {
+ auto &Val = Ptr.deref<Floating>();
+ if (!Val.singleWord()) {
+ uint64_t *NewMemory = new (S.P) uint64_t[Val.numWords()];
+ Val.take(NewMemory);
+ }
+ }
+}
+
+template <typename T>
+static void copyPrimitiveMemory(InterpState &S, const Pointer &Ptr) {
+ assert(needsAlloc<T>());
+ auto &Val = Ptr.deref<T>();
+ if (!Val.singleWord()) {
+ uint64_t *NewMemory = new (S.P) uint64_t[Val.numWords()];
+ Val.take(NewMemory);
+ }
+}
+
+static void finishGlobalRecurse(InterpState &S, const Pointer &Ptr) {
+ if (const Record *R = Ptr.getRecord()) {
+ for (const Record::Field &Fi : R->fields()) {
+ if (Fi.Desc->isPrimitive()) {
+ TYPE_SWITCH_ALLOC(Fi.Desc->getPrimType(), {
+ copyPrimitiveMemory<T>(S, Ptr.atField(Fi.Offset));
+ });
+ copyPrimitiveMemory(S, Ptr.atField(Fi.Offset), Fi.Desc->getPrimType());
+ } else
+ finishGlobalRecurse(S, Ptr.atField(Fi.Offset));
+ }
+ return;
+ }
+
+ if (const Descriptor *D = Ptr.getFieldDesc(); D && D->isArray()) {
+ unsigned NumElems = D->getNumElems();
+ if (NumElems == 0)
+ return;
+
+ if (D->isPrimitiveArray()) {
+ PrimType PT = D->getPrimType();
+ if (!needsAlloc(PT))
+ return;
+ assert(NumElems >= 1);
+ const Pointer EP = Ptr.atIndex(0);
+ bool AllSingleWord = true;
+ TYPE_SWITCH_ALLOC(PT, {
+ if (!EP.deref<T>().singleWord()) {
+ copyPrimitiveMemory<T>(S, EP);
+ AllSingleWord = false;
+ }
+ });
+ if (AllSingleWord)
+ return;
+ for (unsigned I = 1; I != D->getNumElems(); ++I) {
+ const Pointer EP = Ptr.atIndex(I);
+ copyPrimitiveMemory(S, EP, PT);
+ }
+ } else {
+ assert(D->isCompositeArray());
+ for (unsigned I = 0; I != D->getNumElems(); ++I) {
+ const Pointer EP = Ptr.atIndex(I).narrow();
+ finishGlobalRecurse(S, EP);
+ }
+ }
+ }
+}
+
+bool FinishInitGlobal(InterpState &S, CodePtr OpPC) {
+ const Pointer &Ptr = S.Stk.pop<Pointer>();
+
+ finishGlobalRecurse(S, Ptr);
+ if (Ptr.canBeInitialized()) {
+ Ptr.initialize();
+ Ptr.activate();
+ }
+
+ return true;
+}
+
// https://github.com/llvm/llvm-project/issues/102513
#if defined(_MSC_VER) && !defined(__clang__) && !defined(NDEBUG)
#pragma optimize("", off)