Repository navigation
Conversation
Remove 3116 #include lines that clang-tidy's misc-include-cleaner reports as unused, in 1728 files under stan/math and test/, plus 7 blank lines left doubled by the removals. Include-only aggregator headers are unchanged; the diff only removes lines. A flagged line is kept when removing it would cut a file off from something it uses through that include: a header include-cleaner says the file needs, a declaration the file references (including every overload candidate of a dependent call or using-declaration), a template specialization for types the file still reaches, a standard header it got through a Stan header, or a header its standalone compile needs. rev/fwd overload headers keep their prim counterpart.
- prim/fun/plus.hpp: std::forward needs <utility> via prim/meta.hpp - prim/functor/apply.hpp: <functional> for std::get - rev/core/recover_memory_nested.hpp: deleting chainable_alloc needs its definition - vector_rng_test_helper.hpp: NegativeBinomial2LogTestRig.hpp, included after it, uses VectorIntRNGTestRig - opencl_context.hpp, kernel_generator/elt_function_cl.hpp: <fstream> and <set> for the std::swap overloads they declare
A qualified call (stan::math::size, stan::math::sum, using math::exp) is looked up where the calling template is defined, so the overloads it needs must already be declared at that point. On develop some of them were, only because an includer included them first: - the OpenCL size overload before prim/err's elementwise checks, through the matrix_cl.hpp and opencl/prim/size.hpp includes of OpenCL headers - the fwd sum, exp, log and lgamma overloads before the prim distribution and test helpers that call them qualified, among them reduce_sum_part2_test.cpp's test_ad.hpp include, which the thread tests on Jenkins failed without These lines are restored so the order is develop's again.
Jenkins Console Log Machine informationDistributor ID: Ubuntu Description: Ubuntu 20.04.3 LTS Release: 20.04 Codename: focal CPU: Architecture: x86_64 CPU op-mode(s): 32-bit, 64-bit Byte Order: Little Endian Address sizes: 52 bits physical, 57 bits virtual CPU(s): 192 On-line CPU(s) list: 0-191 Thread(s) per core: 2 Core(s) per socket: 48 Socket(s): 2 NUMA node(s): 2 Vendor ID: AuthenticAMD CPU family: 25 Model: 17 Model name: AMD EPYC 9474F 48-Core Processor Stepping: 1 Frequency boost: enabled CPU MHz: 1498.750 CPU max MHz: 4114.4229 CPU min MHz: 1500.0000 BogoMIPS: 7189.39 Virtualization: AMD-V L1d cache: 3 MiB L1i cache: 3 MiB L2 cache: 96 MiB L3 cache: 512 MiB NUMA node0 CPU(s): 0-47,96-143 NUMA node1 CPU(s): 48-95,144-191 Vulnerability Gather data sampling: Not affected Vulnerability Indirect target selection: Not affected Vulnerability Itlb multihit: Not affected Vulnerability L1tf: Not affected Vulnerability Mds: Not affected Vulnerability Meltdown: Not affected Vulnerability Mmio stale data: Not affected Vulnerability Old microcode: Not affected Vulnerability Reg file data sampling: Not affected Vulnerability Retbleed: Not affected Vulnerability Spec rstack overflow: Mitigation; Safe RET Vulnerability Spec store bypass: Mitigation; Speculative Store Bypass disabled via prctl Vulnerability Spectre v1: Mitigation; usercopy/swapgs barriers and __user pointer sanitization Vulnerability Spectre v2: Mitigation; Enhanced / Automatic IBRS; IBPB conditional; STIBP always-on; PBRSB-eIBRS Not affected; BHI Not affected Vulnerability Srbds: Not affected Vulnerability Tsa: Mitigation; Clear CPU buffers Vulnerability Tsx async abort: Not affected Vulnerability Vmscape: Mitigation; IBPB before exit to userspace Flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt pdpe1gb rdtscp lm constant_tsc rep_good amd_lbr_v2 nopl xtopology nonstop_tsc cpuid extd_apicid aperfmperf rapl pni pclmulqdq monitor ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt aes xsave avx f16c rdrand lahf_lm cmp_legacy svm extapic cr8_legacy abm sse4a misalignsse 3dnowprefetch osvw ibs skinit wdt tce topoext perfctr_core perfctr_nb bpext perfctr_llc mwaitx cpb cat_l3 cdp_l3 hw_pstate ssbd mba perfmon_v2 ibrs ibpb stibp ibrs_enhanced vmmcall fsgsbase bmi1 avx2 smep bmi2 erms invpcid cqm rdt_a avx512f avx512dq rdseed adx smap avx512ifma clflushopt clwb avx512cd sha_ni avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local user_shstk avx512_bf16 clzero irperf xsaveerptr rdpru wbnoinvd amd_ppin cppc arat npt lbrv svm_lock nrip_save tsc_scale vmcb_clean flushbyasid decodeassists pausefilter pfthreshold avic v_vmsave_vmload vgif x2avic v_spec_ctrl vnmi avx512vbmi umip pku ospke avx512_vbmi2 gfni vaes vpclmulqdq avx512_vnni avx512_bitalg avx512_vpopcntdq la57 rdpid overflow_recov succor smca fsrm flush_l1d debug_swap G++: g++ (Ubuntu 9.4.0-1ubuntu1~20.04) 9.4.0 Copyright (C) 2019 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. Clang: clang version 10.0.0-4ubuntu1 Target: x86_64-pc-linux-gnu Thread model: posix InstalledDir: /usr/bin |
Jenkins Console Log Machine informationDistributor ID: Ubuntu Description: Ubuntu 20.04.3 LTS Release: 20.04 Codename: focal CPU: Architecture: x86_64 CPU op-mode(s): 32-bit, 64-bit Byte Order: Little Endian Address sizes: 52 bits physical, 57 bits virtual CPU(s): 192 On-line CPU(s) list: 0-191 Thread(s) per core: 2 Core(s) per socket: 48 Socket(s): 2 NUMA node(s): 2 Vendor ID: AuthenticAMD CPU family: 25 Model: 17 Model name: AMD EPYC 9474F 48-Core Processor Stepping: 1 Frequency boost: enabled CPU MHz: 1495.300 CPU max MHz: 4114.4229 CPU min MHz: 1500.0000 BogoMIPS: 7189.39 Virtualization: AMD-V L1d cache: 3 MiB L1i cache: 3 MiB L2 cache: 96 MiB L3 cache: 512 MiB NUMA node0 CPU(s): 0-47,96-143 NUMA node1 CPU(s): 48-95,144-191 Vulnerability Gather data sampling: Not affected Vulnerability Indirect target selection: Not affected Vulnerability Itlb multihit: Not affected Vulnerability L1tf: Not affected Vulnerability Mds: Not affected Vulnerability Meltdown: Not affected Vulnerability Mmio stale data: Not affected Vulnerability Old microcode: Not affected Vulnerability Reg file data sampling: Not affected Vulnerability Retbleed: Not affected Vulnerability Spec rstack overflow: Mitigation; Safe RET Vulnerability Spec store bypass: Mitigation; Speculative Store Bypass disabled via prctl Vulnerability Spectre v1: Mitigation; usercopy/swapgs barriers and __user pointer sanitization Vulnerability Spectre v2: Mitigation; Enhanced / Automatic IBRS; IBPB conditional; STIBP always-on; PBRSB-eIBRS Not affected; BHI Not affected Vulnerability Srbds: Not affected Vulnerability Tsa: Mitigation; Clear CPU buffers Vulnerability Tsx async abort: Not affected Vulnerability Vmscape: Mitigation; IBPB before exit to userspace Flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt pdpe1gb rdtscp lm constant_tsc rep_good amd_lbr_v2 nopl xtopology nonstop_tsc cpuid extd_apicid aperfmperf rapl pni pclmulqdq monitor ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt aes xsave avx f16c rdrand lahf_lm cmp_legacy svm extapic cr8_legacy abm sse4a misalignsse 3dnowprefetch osvw ibs skinit wdt tce topoext perfctr_core perfctr_nb bpext perfctr_llc mwaitx cpb cat_l3 cdp_l3 hw_pstate ssbd mba perfmon_v2 ibrs ibpb stibp ibrs_enhanced vmmcall fsgsbase bmi1 avx2 smep bmi2 erms invpcid cqm rdt_a avx512f avx512dq rdseed adx smap avx512ifma clflushopt clwb avx512cd sha_ni avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local user_shstk avx512_bf16 clzero irperf xsaveerptr rdpru wbnoinvd amd_ppin cppc arat npt lbrv svm_lock nrip_save tsc_scale vmcb_clean flushbyasid decodeassists pausefilter pfthreshold avic v_vmsave_vmload vgif x2avic v_spec_ctrl vnmi avx512vbmi umip pku ospke avx512_vbmi2 gfni vaes vpclmulqdq avx512_vnni avx512_bitalg avx512_vpopcntdq la57 rdpid overflow_recov succor smca fsrm flush_l1d debug_swap G++: g++ (Ubuntu 9.4.0-1ubuntu1~20.04) 9.4.0 Copyright (C) 2019 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. Clang: clang version 10.0.0-4ubuntu1 Target: x86_64-pc-linux-gnu Thread model: posix InstalledDir: /usr/bin |
|
More out of curiosity than anything, can you share the script that did the bulk of this? |
|
@WardBrian Sure, see this remove_unused_includes.py It was meant as a one-timer, so it's obviously sloppy, but mostly gets the job done. The core idea is using The script mostly goes through the filesystem tree and calls this with some additional conditions that are kinda ad-hoc based on where it was failing or inappropriate. It reports |
|
I added some more details to the #3474 discussion. |
This was done mostly with a deterministic script, but also with the help of claude Opus5.5 for some nontrivial instances.
Summary
This removes 3074
#includelines that are not used, in 1702 files understan/math/andtest/, plus 6 blank lines that the removals left doubled. The diff only removes lines; include-only aggregator headers (prim/fun.hpp,rev.hpp, …) are unchanged.Tool. The candidates are the lines that clang-tidy 22's
misc-include-cleanerreports as unused. It was run:STAN_OPENCL,STAN_THREADSandSTAN_MPI;In the analysis copy only, the aggregators,
prim/fun/Eigen.hppand Eigen's module headers carry IWYU export pragmas. So, for example,#include <stan/math/prim/meta.hpp>counts as used exactly when something fromprim/meta/is used. Files with other preprocessor conditions or with macro definitions were reviewed by hand.What is kept. include-cleaner sees only references it can resolve. A flagged line therefore stays when one of the checks below finds that removing it would cut a file off from something it uses through that include. Each check compares against develop's include closures (
clang -M), and a failing check keeps removed lines on develop's include chain to the lost header.usingdeclaration. include-cleaner treats those candidates as ambiguous and never reports them as missing. This keeps, for example,neg_binomial_lccdfvisible inneg_binomial_2_lccdf.hppandlog1m_expinordered_logistic_lpmf.hpp.stan::math::size(x),using math::exp;), saw overloads in develop only because its includer included them first. Those overloads still come first, so that the OpenCLsizeoverload precedesprim/err's elementwise checks in OpenCL headers.var,fvarandmatrix_cltrait andstdspecializations, and the prim trait specializations for Eigen,std::vectorandstd::complex.rev/fun/log.hpp: they instantiate prim templates withvarand need the rev overloads those templates call.ordered_logistic_lpmf.hppkeepsis_integer.hpp. That is the only path fromstan/math.hpptostan::math::is_integer, whichprim/fun.hppdoes not list.matrix_exp_action_handler.hppkeepsunsupported/Eigen/MatrixFunctionsfor its dependent.pow()call.In total 669 of the 3743 flagged lines are kept.
Tests
No tests are added; the change removes includes only. Checked locally with clang 15 in both configurations above:
stan/passes the header check as Jenkins runs it: forced into an empty translation unit, with-Werror -Wunused-local-typedefs. The exceptions are the headers that also fail on develop:eigen_plugins.h, and the MPI headers underSTAN_MPI's deleted-destructor warning, which pass with that warning off.test_ad.hpp,multiple_translation_units_includes.hpp, every test undertest/unit/math/primandtest/unit/math/opencl, and thereduce_summix tests compile on their own (-fsyntax-only).runChecks.py,make cpplintand clang-format 10 report nothing new.Left to CI:
Side Effects
Code outside the library that relied on Stan headers for unrelated includes may need its own:
STAN_OPENCL,stan/math.hppand theprim/rev/fwd/mixaggregators no longer bring in<deque>and<queue>. WithSTAN_OPENCLthey still do.STAN_OPENCL,prim.hppandfwd.hppno longer includeopencl/rev/to_arena.hpp.stan/math/prim.hppandstan/math/fwd.hppno longer bring in the KINSOL headers;stan/math.hpp,rev.hppandmix.hppstill do.No file in
stan-dev/stan'ssrc/stanuses any of these without including them itself. Single headers now include less, so including one of them alone gives fewer transitively. This PR touches many include blocks and will conflict with other open PRs that edit includes. The conflicts are only in those lines.Release notes
Removed unused
#includedirectives across the library and its tests; no functional change.Checklist
Copyright holder: Jáchym Barvínek jachymb@gmail.com
The copyright holder is typically you or your assignee, such as a university or company. By submitting this pull request, the copyright holder is agreeing to the license the submitted work under the following licenses:
- Code: BSD 3-clause (https://opensource.org/licenses/BSD-3-Clause)
- Documentation: CC-BY 4.0 (https://creativecommons.org/licenses/by/4.0/)
the basic tests are passing
./runTests.py test/unit)make test-headers)make test-math-dependencies)make doxygen)make cpplint)the code is written in idiomatic C++ and changes are documented in the doxygen
the new changes are tested