[clang] [Clang] Instantiate functions from constant evaluation. (PR #205557)

James Y Knight via cfe-commits cfe-commits at lists.llvm.org
Tue Jul 21 12:58:01 PDT 2026


jyknight wrote:

Since, with the current PR, you only can see issues in edge-cases (e.g. where the constexpr template is defined after it's mentioned by a constexpr function), in order to better test all the behaviors, I've deleted the eager instantiation via the following patch, and then run ninja check-clang.
```
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -19152,11 +19152,6 @@ void Sema::MarkFunctionReferenced(SourceLocation Loc, FunctionDecl *Func,
               CodeSynthesisContexts.size())
             PendingLocalImplicitInstantiations.push_back(
                 std::make_pair(Func, PointOfInstantiation));
-          else if (Func->isConstexpr())
-            // Do not defer instantiations of constexpr functions, to avoid the
-            // expression evaluator needing to call back into Sema if it sees a
-            // call to such a function.
-            InstantiateFunctionDefinition(PointOfInstantiation, Func);
           else {
             Func->setInstantiationIsPending(true);
             PendingInstantiations.push_back(
```

IIUC, with a correct change here, lazy instantiation should be triggered in all cases where it's necessary, and so deleting the above should only change some of the notes around where instantiation was triggered, but not otherwise break any code.

However, that's not the case. There's a lot of errors reported. Looking at them, I see at least the following seemingly-distinct issues:
- annotate attribute doesn't instantiate when evaluating its (required-constant) argument
- enable_if attribute doesn't instantiate when evaluating its (required-constant) argument
- non-constexpr globals don't instantiate (as mentioned in previous review comment)
- There's some weird issue where a "friend" declaration with explicit template-args gets in the way of finding the actual definition of constexpr function to instantiate.
- narrowing conversion in braces or initializer-list is not being permitted when it should be (only allowed for constants, and without instantiation we can't tell if we have a constant)
- `__builtin_matrix_column_major_load` "must be a constant unsigned integer expression"
- Some issues with "immediate function {} used before it is defined" related to consteval functions.
- Also, some crashes in the -fexperimental-new-constant-interpreter mode.

https://github.com/llvm/llvm-project/pull/205557


More information about the cfe-commits mailing list