Skip to content

Expr::getExprLoc() is slow #140876

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
tbaederr opened this issue May 21, 2025 · 5 comments · May be fixed by #141058
Open

Expr::getExprLoc() is slow #140876

tbaederr opened this issue May 21, 2025 · 5 comments · May be fixed by #141058
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" slow-compile

Comments

@tbaederr
Copy link
Contributor

This first came up when the implementation of CallExpr::getBeginLoc() was changed and we saw a surprising drop in compile times.

We call Expr::getExprLoc() (and various other functions returning a SourceLocation) all the time. The assumption is that the call is cheap and that it's fine to do even if the source location isn't used in the end.

Changing the implementation of Expr::getExprLoc() to just return SourceLocation() improves compile times considerably: https://llvm-compile-time-tracker.com/compare.php?from=a71f56d63f569ca0ed543d3d01c684ca6ac7dffd&to=2510892ea46a84a284e0cde9f2a4edae3cdb183d&stat=instructions:u

tl;dr: Up to 15%.

Quick experiment with callgrind when compiling SemaExpr.cpp:

Image

We should improve this.

@tbaederr tbaederr added slow-compile clang:frontend Language frontend issues, e.g. anything involving "Sema" labels May 21, 2025
@llvmbot
Copy link
Member

llvmbot commented May 21, 2025

@llvm/issue-subscribers-clang-frontend

Author: Timm Baeder (tbaederr)

This first came up when the implementation of `CallExpr::getBeginLoc()` was changed and we saw a surprising drop in compile times.

We call Expr::getExprLoc() (and various other functions returning a SourceLocation) all the time. The assumption is that the call is cheap and that it's fine to do even if the source location isn't used in the end.

Changing the implementation of Expr::getExprLoc() to just return SourceLocation() improves compile times considerably: https://llvm-compile-time-tracker.com/compare.php?from=a71f56d63f569ca0ed543d3d01c684ca6ac7dffd&to=2510892ea46a84a284e0cde9f2a4edae3cdb183d&stat=instructions:u

tl;dr: Up to 15%.

Quick experiment with callgrind when compiling SemaExpr.cpp:

Image

We should improve this.

@zyn0217
Copy link
Contributor

zyn0217 commented May 21, 2025

@HighCommander4 @cor3ntin

I wonder if we can have a bit in CallExprBitfields to track whether the callee is an ExplicitObjectMemberFunction, so that we can get rid of the call to getCalleeDecl

@cor3ntin
Copy link
Contributor

@zyn0217 If it helps, sure :) But I don't know how easy it is to track that from construction.
getCalleeDecl is also super inefficient. I push a thing to compile-time-tracker to improve getCalleeDecl, we'll see

I wonder if we can find a better way to handle/remove the special case for explicit object member functions

@cor3ntin
Copy link
Contributor

@zygoloid
Copy link
Collaborator

For what it's worth, this is the reason why the code in ExprConstant.cpp generally passes around a const Expr *E instead of a SourceLocation to code that performs some checks and might diagnose -- we used to pass around SourceLocations and it turned out we were spending a significant portion of our time during compile-time evaluation in getExprLoc.

We could generalize the approach there, and create an ExprLoc type that can be used in place of a SourceLocation but is internally represented as a const Expr*. Though it'd obviously be better if we can make getExprLoc itself faster.

@cor3ntin cor3ntin linked a pull request May 22, 2025 that will close this issue
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" slow-compile
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants