llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

performance optimization: special-case deduction for variables of type `auto`

Open zygoloid opened this issue 8 months ago • 11 comments

General-purpose auto deduction involves some heavy machinery: we synthesize a template, perform a substitution on the type, perform deduction against the substituted type, perform another substitution, and so on. All of that adds up to a significant fraction of the total compile time if auto is used heavily.

But in places where auto is used heavily, the complete type is very frequently simply auto (or const auto), in which case we don't actually need any of that heavy machinery. Clang should detect the special case where the type is just auto (Context.AutoDeductTy) and the initializer is not an InitListExpr, and compute the decayed type of the initializer and directly build the deduced type.

$ for i in $(seq 1000000); do echo "auto v$i = 0;"; done >! ~/auto.cpp
$ for i in $(seq 1000000); do echo "int v$i = 0;"; done >! ~/int.cpp  
$ time clang++ ~/auto.cpp -fsyntax-only                               
clang++ ~/auto.cpp -fsyntax-only  6.57s user 0.36s system 99% cpu 6.935 total
$ time clang++ ~/int.cpp -fsyntax-only                                
clang++ ~/int.cpp -fsyntax-only  4.40s user 0.22s system 99% cpu 4.629 total

zygoloid avatar Mar 12 '26 23:03 zygoloid

Hi!

This issue may be a good introductory issue for people new to working on LLVM. If you would like to work on this issue, your first steps are:

  1. Check that no other contributor is working on this issue. If someone is assigned to the issue or claimed to be working on it, ping the person. After one week without a response, the assignee may be changed.
  2. Leave a comment indicating that you are working on the issue, or just create a pull request after following the steps below. Mention this issue in the description of the pull request.
  3. Fix the issue locally.
  4. Run the test suite locally. Remember that the subdirectories under test/ create fine-grained testing targets, so you can e.g. use make check-clang-ast to only run Clang's AST tests.
  5. Create a Git commit.
  6. Run git clang-format HEAD~1 to format your changes.
  7. Open a pull request to the upstream repository on GitHub. Detailed instructions can be found in GitHub's documentation. Mention this issue in the description of the pull request.

If you have any further questions about this issue, don't hesitate to ask via a comment in the thread below.

llvmbot avatar Mar 12 '26 23:03 llvmbot

@llvm/issue-subscribers-c-11

Author: Richard Smith (zygoloid)

General-purpose `auto` deduction involves some [heavy machinery](https://github.com/llvm/llvm-project/blob/2d70dbdb357d2b4080b30d35a105442337bc1fdd/clang/lib/Sema/SemaTemplateDeduction.cpp#L5193): we synthesize a template, perform a substitution on the type, perform deduction against the substituted type, perform another substitution, and so on. All of that adds up to [a significant fraction of the total compile time](https://hachyderm.io/@lritter@mastodon.gamedev.place/116216130998393300) if `auto` is used heavily.

But in places where auto is used heavily, the complete type is very frequently simply auto (or const auto), in which case we don't actually need any of that heavy machinery. Clang should detect the special case where the type is just auto (Context.AutoDeductTy) and the initializer is not an InitListExpr, and compute the decayed type of the initializer and directly build the deduced type.

$ for i in $(seq 1000000); do echo "auto v$i = 0;"; done >! ~/auto.cpp
$ for i in $(seq 1000000); do echo "int v$i = 0;"; done >! ~/int.cpp  
$ time clang++ ~/auto.cpp -fsyntax-only                               
clang++ ~/auto.cpp -fsyntax-only  6.57s user 0.36s system 99% cpu 6.935 total
$ time clang++ ~/int.cpp -fsyntax-only                                
clang++ ~/int.cpp -fsyntax-only  4.40s user 0.22s system 99% cpu 4.629 total

llvmbot avatar Mar 12 '26 23:03 llvmbot

@llvm/issue-subscribers-clang-frontend

Author: Richard Smith (zygoloid)

General-purpose `auto` deduction involves some [heavy machinery](https://github.com/llvm/llvm-project/blob/2d70dbdb357d2b4080b30d35a105442337bc1fdd/clang/lib/Sema/SemaTemplateDeduction.cpp#L5193): we synthesize a template, perform a substitution on the type, perform deduction against the substituted type, perform another substitution, and so on. All of that adds up to [a significant fraction of the total compile time](https://hachyderm.io/@lritter@mastodon.gamedev.place/116216130998393300) if `auto` is used heavily.

But in places where auto is used heavily, the complete type is very frequently simply auto (or const auto), in which case we don't actually need any of that heavy machinery. Clang should detect the special case where the type is just auto (Context.AutoDeductTy) and the initializer is not an InitListExpr, and compute the decayed type of the initializer and directly build the deduced type.

$ for i in $(seq 1000000); do echo "auto v$i = 0;"; done >! ~/auto.cpp
$ for i in $(seq 1000000); do echo "int v$i = 0;"; done >! ~/int.cpp  
$ time clang++ ~/auto.cpp -fsyntax-only                               
clang++ ~/auto.cpp -fsyntax-only  6.57s user 0.36s system 99% cpu 6.935 total
$ time clang++ ~/int.cpp -fsyntax-only                                
clang++ ~/int.cpp -fsyntax-only  4.40s user 0.22s system 99% cpu 4.629 total

llvmbot avatar Mar 12 '26 23:03 llvmbot

@llvm/issue-subscribers-good-first-issue

Author: Richard Smith (zygoloid)

General-purpose `auto` deduction involves some [heavy machinery](https://github.com/llvm/llvm-project/blob/2d70dbdb357d2b4080b30d35a105442337bc1fdd/clang/lib/Sema/SemaTemplateDeduction.cpp#L5193): we synthesize a template, perform a substitution on the type, perform deduction against the substituted type, perform another substitution, and so on. All of that adds up to [a significant fraction of the total compile time](https://hachyderm.io/@lritter@mastodon.gamedev.place/116216130998393300) if `auto` is used heavily.

But in places where auto is used heavily, the complete type is very frequently simply auto (or const auto), in which case we don't actually need any of that heavy machinery. Clang should detect the special case where the type is just auto (Context.AutoDeductTy) and the initializer is not an InitListExpr, and compute the decayed type of the initializer and directly build the deduced type.

$ for i in $(seq 1000000); do echo "auto v$i = 0;"; done >! ~/auto.cpp
$ for i in $(seq 1000000); do echo "int v$i = 0;"; done >! ~/int.cpp  
$ time clang++ ~/auto.cpp -fsyntax-only                               
clang++ ~/auto.cpp -fsyntax-only  6.57s user 0.36s system 99% cpu 6.935 total
$ time clang++ ~/int.cpp -fsyntax-only                                
clang++ ~/int.cpp -fsyntax-only  4.40s user 0.22s system 99% cpu 4.629 total

llvmbot avatar Mar 12 '26 23:03 llvmbot

Can I work on this issue?

0bVdnt avatar Mar 13 '26 04:03 0bVdnt

Can I work on this issue?

MaxGud10 avatar Mar 13 '26 11:03 MaxGud10

Anyone is free to work on the issue (just post a PR) but given that two of you asked, please coordinate before pushing a PR so we do not get duplicate PRs fixing the same thing.

AaronBallman avatar Mar 13 '26 11:03 AaronBallman

can I help with anything?

specialistway avatar Mar 13 '26 14:03 specialistway

Hi, I'd also be interested in working on this. Since there are already three people who asked, I wanted to coordinate before starting. If nobody has begun working on it, I'd happy to take a shot.

heturing avatar Mar 14 '26 08:03 heturing

i want to work for this issue

purnima-nlp avatar Jul 13 '26 15:07 purnima-nlp

@zygoloid after seeing that heturing's PR seemed to not have much of an impact on compilation times (as per the llvm compile time tracker mentioned in #188196), do you think their logic is correct or is something missing? I would like to work on this but I think it is likely I will end up providing a patch very similar to #188196

nots1dd avatar Jul 28 '26 13:07 nots1dd