performance optimization: special-case deduction for variables of type `auto`
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
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:
- 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.
- 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.
- Fix the issue locally.
- Run the test suite locally. Remember that the subdirectories under
test/create fine-grained testing targets, so you can e.g. usemake check-clang-astto only run Clang's AST tests. - Create a Git commit.
- Run
git clang-format HEAD~1to format your changes. - 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.
@llvm/issue-subscribers-c-11
Author: Richard Smith (zygoloid)
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
@llvm/issue-subscribers-clang-frontend
Author: Richard Smith (zygoloid)
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
@llvm/issue-subscribers-good-first-issue
Author: Richard Smith (zygoloid)
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
Can I work on this issue?
Can I work on this issue?
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.
can I help with anything?
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.
i want to work for this issue
@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