Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@tbaederr
Copy link
Contributor

@tbaederr tbaederr commented May 6, 2025

Fixes #138653

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels May 6, 2025
@llvmbot
Copy link
Member

llvmbot commented May 6, 2025

@llvm/pr-subscribers-clang

Author: Timm Baeder (tbaederr)

Changes

Fixes #138653


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

1 Files Affected:

  • (modified) clang/lib/AST/ExprConstant.cpp (+5)
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index e5950f461e4b2..46a87fd6c7e1c 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -11788,6 +11788,11 @@ bool ArrayExprEvaluator::VisitCXXParenListOrInitListExpr(
   LLVM_DEBUG(llvm::dbgs() << "The number of elements to initialize: "
                           << NumEltsToInit << ".\n");
 
+  if (!Info.CheckArraySize(ExprToVisit->getExprLoc(),
+                           CAT->getNumAddressingBits(Info.Ctx), NumEltsToInit,
+                           /*Diag=*/true))
+    return false;
+
   Result = APValue(APValue::UninitArray(), NumEltsToInit, NumElts);
 
   // If the array was previously zero-initialized, preserve the

@tbaederr
Copy link
Contributor Author

tbaederr commented May 6, 2025

That fixes compile-time computation, but this example still breaks in codegen:

#include <array>
using namespace std;
constexpr size_t kMemoryChunk = 1024 * 8;
constexpr size_t kNumberOfIterations = 2000000;
constexpr size_t kThreadsNumber = 2 * kMemoryChunk;
struct S {};

int main() {
    array<S, kThreadsNumber * kNumberOfIterations> futures{};
}

here:

SmallVector<llvm::Constant *, 16> Elts;
if (fillC && fillC->isNullValue())
Elts.reserve(NumInitableElts + 1);
else
Elts.reserve(NumElements);

because NumElements is 32768000000.

Edit: Hmm, but that's only if S is empty.

@tbaederr
Copy link
Contributor Author

tbaederr commented May 6, 2025

I have not added the test since I thought it might take too long, but it's probably not a problem since we do the check before even touching the array...

If I could come up with a reproducer that doesn't require <future>...

Copy link
Member

@Sirraide Sirraide left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to also have tests to make sure we do emit the warning if the array is too large

@Sirraide
Copy link
Member

Sirraide commented May 7, 2025

I have not added the test since I thought it might take too long, but it's probably not a problem since we do the check before even touching the array...

Can’t you set the eval steps to like 100 or sth to avoid it taking too long or am I missing something here?

@tbaederr
Copy link
Contributor Author

tbaederr commented May 7, 2025

That comment is obsolete, I've added two test cases that work

@tbaederr tbaederr merged commit d35ad58 into llvm:main May 9, 2025
11 checks passed
tbaederr added a commit that referenced this pull request May 9, 2025
This reverts commit d35ad58.

This breaks the clang build:
https://lab.llvm.org/buildbot/#/builders/132/builds/1033

/home/buildbot-worker/bbroot/clang-riscv-rva23-evl-vec-2stage/stage2/lib/Target/RISCV/RISCVGenGlobalISel.inc:1512:44: note: cannot allocate array; evaluated array bound 2431270 exceeds the limit (1048576); use '-fconstexpr-steps' to increase this limit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

ICE / LLVM ERROR: Out of memory when compiling large std::array<shared_future<...>> with additional unexpected behaviours (C++23, Clang 19–21)

4 participants