[clang] a7579b2 - [Clang] Improve compile times when forming a DeclRef outside of a capturing scope.
Corentin Jabot via cfe-commits
cfe-commits at lists.llvm.org
Mon May 8 09:41:30 PDT 2023
Author: Corentin Jabot
Date: 2023-05-08T18:41:24+02:00
New Revision: a7579b25df78a9f53d62300020d4ae3c47666634
URL: https://github.com/llvm/llvm-project/commit/a7579b25df78a9f53d62300020d4ae3c47666634
DIFF: https://github.com/llvm/llvm-project/commit/a7579b25df78a9f53d62300020d4ae3c47666634.diff
LOG: [Clang] Improve compile times when forming a DeclRef outside of a capturing scope.
The logic of whether an entity needs to be captured has become
quite complex and the recent changes in https://reviews.llvm.org/D124351
ad a mesurable negative impact on compile times.
However, in the absence of capturing scopes (lambda, block, region)
we usually can avoid running most of that logic
(except that we do need to diagnostic when a nested function
refers to a local variable in the scope of the outer function.).
This patch track whether there is currently an active capturing
scope and exit `tryCaptureVariable` early when there isn't.
Reviewed By: aaron.ballman
Differential Revision: https://reviews.llvm.org/D150038
Added:
Modified:
clang/include/clang/Sema/Sema.h
clang/lib/Sema/Sema.cpp
clang/lib/Sema/SemaExpr.cpp
Removed:
################################################################################
diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index 92f3e6f0ff848..c6facd4e9e4af 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -806,6 +806,9 @@ class Sema final {
/// context.
unsigned FunctionScopesStart = 0;
+ /// Track the number of currently active capturing scopes.
+ unsigned CapturingFunctionScopes = 0;
+
ArrayRef<sema::FunctionScopeInfo*> getFunctionScopes() const {
return llvm::ArrayRef(FunctionScopes.begin() + FunctionScopesStart,
FunctionScopes.end());
diff --git a/clang/lib/Sema/Sema.cpp b/clang/lib/Sema/Sema.cpp
index 07ece4078474e..831e9db6e4f07 100644
--- a/clang/lib/Sema/Sema.cpp
+++ b/clang/lib/Sema/Sema.cpp
@@ -2138,11 +2138,13 @@ void Sema::PushFunctionScope() {
void Sema::PushBlockScope(Scope *BlockScope, BlockDecl *Block) {
FunctionScopes.push_back(new BlockScopeInfo(getDiagnostics(),
BlockScope, Block));
+ CapturingFunctionScopes++;
}
LambdaScopeInfo *Sema::PushLambdaScope() {
LambdaScopeInfo *const LSI = new LambdaScopeInfo(getDiagnostics());
FunctionScopes.push_back(LSI);
+ CapturingFunctionScopes++;
return LSI;
}
@@ -2264,6 +2266,8 @@ Sema::PopFunctionScopeInfo(const AnalysisBasedWarnings::Policy *WP,
void Sema::PoppedFunctionScopeDeleter::
operator()(sema::FunctionScopeInfo *Scope) const {
+ if (!Scope->isPlainFunction())
+ Self->CapturingFunctionScopes--;
// Stash the function scope for later reuse if it's for a normal function.
if (Scope->isPlainFunction() && !Self->CachedFunctionScope)
Self->CachedFunctionScope.reset(Scope);
@@ -2694,6 +2698,7 @@ void Sema::PushCapturedRegionScope(Scope *S, CapturedDecl *CD, RecordDecl *RD,
OpenMPCaptureLevel);
CSI->ReturnType = Context.VoidTy;
FunctionScopes.push_back(CSI);
+ CapturingFunctionScopes++;
}
CapturedRegionScopeInfo *Sema::getCurCapturedRegion() {
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index 622b36407495b..ec0f31e489d5b 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -19221,6 +19221,15 @@ bool Sema::tryCaptureVariable(
// An init-capture is notionally from the context surrounding its
// declaration, but its parent DC is the lambda class.
DeclContext *VarDC = Var->getDeclContext();
+ DeclContext *DC = CurContext;
+
+ // tryCaptureVariable is called every time a DeclRef is formed,
+ // it can therefore have non-negigible impact on performances.
+ // For local variables and when there is no capturing scope,
+ // we can bailout early.
+ if (CapturingFunctionScopes == 0 && (!BuildAndDiagnose || VarDC == DC))
+ return true;
+
const auto *VD = dyn_cast<VarDecl>(Var);
if (VD) {
if (VD->isInitCapture())
@@ -19230,7 +19239,6 @@ bool Sema::tryCaptureVariable(
}
assert(VD && "Cannot capture a null variable");
- DeclContext *DC = CurContext;
const unsigned MaxFunctionScopesIndex = FunctionScopeIndexToStopAt
? *FunctionScopeIndexToStopAt : FunctionScopes.size() - 1;
// We need to sync up the Declaration Context with the
More information about the cfe-commits
mailing list