Skip to content

Conversation

@Tsche
Copy link
Contributor

@Tsche Tsche commented Sep 24, 2025

To address @AaronBallman's feedback from #143785 this patch implements an explicit opt-out for -fconstexpr-steps by setting -fconstexpr-steps=0.

This does not change any defaults, but gives users an easy way to opt out of this limit altogether (and instead let the compiler reach the system's resource limits).

Currently users set constexpr-steps to some arbitrary high number (and I mean arbitrary - see the tables in the previous PR). This isn't actually opting out of the limit though - you're still bound by the upper bound of the counter's type. If you have enough resources to evaluate more than 18446744073709551615 steps that's bad news.

In any case, =0 conveys the intent clearer. This is in line with how we handle other flags, ie -ftemplate-backtrace-limit or -ferror-limit.

Please advise if a similar opt-out would be desirable for -fconstexpr-depth (and possibly -ftemplate-depth?).

@github-actions
Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" clang:bytecode Issues for the clang bytecode constexpr interpreter labels Sep 24, 2025
@llvmbot
Copy link
Member

llvmbot commented Sep 24, 2025

@llvm/pr-subscribers-clang

Author: None (Tsche)

Changes

To address @AaronBallman's feedback from #143785 this patch implements an explicit opt-out for -fconstexpr-steps by setting -fconstexpr-steps=0.

This does not change any defaults, but gives users an easy way to opt out of this limit altogether (and instead let the compiler reach the system's resource limits).

Currently users set constexpr-steps to some arbitrary high number (and I mean arbitrary - see the tables in the previous PR). This isn't actually opting out of the limit though - you're still bound by the upper bound of the counter's type. If you have enough resources to evaluate more than 18446744073709551615 steps that's bad news.

In any case, =0 conveys the intent clearer. This is in line with how we handle other flags, ie -ftemplate-backtrace-limit or -ferror-limit.

Please advise if a similar opt-out would be desirable for -fconstexpr-depth (and possibly -ftemplate-depth?).


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

3 Files Affected:

  • (modified) clang/docs/UsersManual.rst (+1-1)
  • (modified) clang/lib/AST/ByteCode/Interp.h (+3)
  • (modified) clang/lib/AST/ExprConstant.cpp (+4-1)
diff --git a/clang/docs/UsersManual.rst b/clang/docs/UsersManual.rst
index a8bbf146431ea..1a062475728dd 100644
--- a/clang/docs/UsersManual.rst
+++ b/clang/docs/UsersManual.rst
@@ -4028,7 +4028,7 @@ Controlling implementation limits
   Sets the limit for the number of full-expressions evaluated in a single
   constant expression evaluation. This also controls the maximum size
   of array and dynamic array allocation that can be constant evaluated.
-  The default is 1048576.
+  The default is 1048576, and the limit can be disabled with `-fconstexpr-steps=0`..
 
 .. option:: -ftemplate-depth=N
 
diff --git a/clang/lib/AST/ByteCode/Interp.h b/clang/lib/AST/ByteCode/Interp.h
index b3b4b998439cc..deba2d294abe9 100644
--- a/clang/lib/AST/ByteCode/Interp.h
+++ b/clang/lib/AST/ByteCode/Interp.h
@@ -3686,6 +3686,9 @@ inline bool CheckDestruction(InterpState &S, CodePtr OpPC) {
 
 inline bool CheckArraySize(InterpState &S, CodePtr OpPC, uint64_t NumElems) {
   uint64_t Limit = S.getLangOpts().ConstexprStepLimit;
+  if (Limit == 0)
+    return true;
+
   if (NumElems > Limit) {
     S.FFDiag(S.Current->getSource(OpPC),
              diag::note_constexpr_new_exceeds_limits)
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index d10e2afeb2341..0fe3fce5b64a8 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -990,7 +990,7 @@ namespace {
       // of arrays to avoid exhausting the system resources, as initialization
       // of each element is likely to take some number of steps anyway.
       uint64_t Limit = Ctx.getLangOpts().ConstexprStepLimit;
-      if (ElemCount > Limit) {
+      if (Limit != 0 && ElemCount > Limit) {
         if (Diag)
           FFDiag(Loc, diag::note_constexpr_new_exceeds_limits)
               << ElemCount << Limit;
@@ -1016,6 +1016,9 @@ namespace {
     }
 
     bool nextStep(const Stmt *S) {
+      if (Ctx.getLangOpts().ConstexprStepLimit == 0)
+        return true;
+
       if (!StepsLeft) {
         FFDiag(S->getBeginLoc(), diag::note_constexpr_step_limit_exceeded);
         return false;

Copy link
Contributor

@cor3ntin cor3ntin left a comment

Choose a reason for hiding this comment

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

I think the idea makes sense
Can you update the docs in Options.td, UserManual.rst?
We should say somewhere that =0 will consume all available resources and somewhat discourage it.

@Tsche
Copy link
Contributor Author

Tsche commented Sep 24, 2025

Thanks. I've added the (0 = no limit) note to the driver's help text. The changes to UserManual.rst had already been made.

We should say somewhere that =0 will consume all available resources and somewhat discourage it.

Is this not implied? What else is it supposed to do if I disable self-imposed limits?

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.

This is still missing a release note

@Tsche
Copy link
Contributor Author

Tsche commented Sep 24, 2025

This is still missing a release note

Damn, forgot about it again. I've added it now :)

@Tsche Tsche requested review from Sirraide and cor3ntin October 8, 2025 11:46
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.

I think keepEvaluatingAfterFailure() also needs to be updated.

Copy link
Collaborator

@AaronBallman AaronBallman left a comment

Choose a reason for hiding this comment

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

LGTM!

@AaronBallman AaronBallman merged commit ec2d6ad into llvm:main Oct 13, 2025
11 checks passed
@github-actions
Copy link

@Tsche Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

akadutta pushed a commit to akadutta/llvm-project that referenced this pull request Oct 14, 2025
To address @AaronBallman's feedback from
llvm#143785 this patch implements
an explicit opt-out for `-fconstexpr-steps` by setting
`-fconstexpr-steps=0`.

This does not change any defaults, but gives users an easy way to opt
out of this limit altogether (and instead let the compiler reach the
system's resource limits).

Currently users set `constexpr-steps` to some arbitrary high number (and
I mean _arbitrary_ - see the tables in the previous PR). This isn't
actually opting out of the limit though - you're still bound by the
upper bound of the counter's type. If you have enough resources to
evaluate more than 18446744073709551615 steps that's bad news.

In any case, `=0` conveys the intent clearer. This is in line with how
we handle other flags, ie `-ftemplate-backtrace-limit` or
`-ferror-limit`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:bytecode Issues for the clang bytecode constexpr interpreter 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.

5 participants