forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for include-what-you-use (pytorch#71114)
Summary: Pull Request resolved: pytorch#71114 `include-what-you-use` or `iwyu` is a clang-based tool that looks at the code's AST to figure out which symbols need to be included and with the help of user-defined mappings it suggests the include files that are actually needed. This is very nice for the per-operator headers build because it give you a list of exactly the `ATen/ops` headers needed by the file. You still need to manually write the include-guards etc. but at least this automates the most tedious part. The header mappings aren't perfect yet so it will still suggest you include basic c10 components everywhere instead of taking it transitively from `TensorBase.h`. However, this does provide some useful mappings and removes bad include paths from the build system that were causing bad suggestions. Test Plan: Imported from OSS Reviewed By: ngimel Differential Revision: D33949901 Pulled By: malfet fbshipit-source-id: d5b015ef9e168bee4b8717b8e87ccc0608da62a1 (cherry picked from commit ecb2ffb)
- Loading branch information
1 parent
36385be
commit e90f558
Showing
20 changed files
with
191 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[ | ||
{ ref: "system.imp" }, | ||
{ ref: "c10.imp" }, | ||
{ ref: "aten.imp" }, | ||
{ ref: "qnnpack.imp" }, | ||
{ ref: "torch.imp" }, | ||
{ ref: "gtest.imp" }, | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
[ | ||
{ include: ["@<ATen/cpu/vec/vec512/.*>", private, "<ATen/cpu/vec/vec.h>", public] }, | ||
{ include: ["@<ATen/cpu/vec/vec256/.*>", private, "<ATen/cpu/vec/vec.h>", public] }, | ||
{ include: ["<ATen/cpu/vec/vec_base.h>", private, "<ATen/cpu/vec/vec.h>", public] }, | ||
{ include: ["<ATen/cpu/vec/functional_base.h>", private, "<ATen/cpu/vec/functional.h>", public] }, | ||
{ include: ["<ATen/cpu/vec/functional_bfloat16.h>", private, "<ATen/cpu/vec/functional.h>", public] }, | ||
|
||
{ include: ["<ATen/core/Dict_inl.h>", private, "<ATen/core/Dict.h>", public] }, | ||
{ include: ["<ATen/core/ivalue_inl.h>", private, "<ATen/core/ivalue.h>", public] }, | ||
{ include: ["<ATen/core/DimVector.h>", public, "<ATen/DimVector.h>", public] }, | ||
{ include: ["<ATen/core/Dimname.h>", public, "<ATen/Dimname.h>", public] }, | ||
{ include: ["<ATen/core/symbol.h>", public, "<ATen/core/Dimname.h>", public] }, | ||
{ include: ["<ATen/TensorIterator.h>", public, "<ATen/core/TensorIterator.h>", public] }, | ||
|
||
{ include: ["<c10/core/WrapDimMinimal.h>", public, "<ATen/WrapDimUtils.h>", public] }, | ||
{ include: ["<c10/util/SmallVector.h>", public, "<ATen/core/DimVector.h>", public] }, | ||
|
||
{ include: ["<c10/util/MaybeOwned.h>", public, "<ATen/core/TensorBase.h>", public] }, | ||
{ include: ["<c10/core/TensorImpl.h>", public, "<ATen/core/TensorBase.h>", public] }, | ||
{ include: ["<c10/core/UndefinedTensorImpl.h>", public, "<ATen/core/TensorBase.h>", public] }, | ||
{ include: ["<c10/core/TensorOptions.h>", public, "<ATen/core/TensorBase.h>", public] }, | ||
|
||
{ include: ["<ATen/core/TensorBase.h>", public, "<ATen/core/Tensor.h>", public] }, | ||
{ include: ["<ATen/core/TensorBody.h>", private, "<ATen/core/Tensor.h>", public] }, | ||
{ symbol: ["at::Tensor", private, "<ATen/core/Tensor.h>", public] }, | ||
|
||
{ include: ["<ATen/Parallel-inl.h>", private, "<ATen/Parallel.h>", public] }, | ||
{ include: ["<ATen/ParallelOpenMP.h>", private, "<ATen/Parallel.h>", public] }, | ||
{ include: ["<ATen/ParallelNative.h>", private, "<ATen/Parallel.h>", public] }, | ||
{ include: ["<ATen/ParallelNativeTBB.h>", private, "<ATen/Parallel.h>", public] }, | ||
|
||
{ include: ["<qnnpack/common.h>", public, "<pack_block_sparse.h>", public] }, | ||
{ include: ["<qnnpack/math.h>", public, "<pack_block_sparse.h>", public] }, | ||
{ include: ["<qnnpack/operator.h>", public, "<qnnpack_func.h>", public] }, | ||
{ include: ["<qnnpack/log.h>", public, "<pytorch_qnnpack.h>", public] }, | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
[ | ||
{ include: [ "<c10/macros/cmake_macros.h>", private, "<c10/macros/Macros.h>", public ] }, | ||
{ include: [ "<c10/macros/Export.h>", public, "<c10/macros/Macros.h>", public ] }, | ||
|
||
{ include: [ "<c10/util/BFloat16-inl.h>", private, "<c10/util/BFloat16.h>", public ] }, | ||
{ include: [ "<c10/util/Half-inl.h>", private, "<c10/util/Half.h>", public ] }, | ||
|
||
{ include: [ "<c10/util/complex_math.h>", private, "<c10/util/complex.h>", public ] }, | ||
{ include: [ "<c10/util/complex_utils.h>", private, "<c10/util/complex.h>", public ] }, | ||
|
||
{ include: [ "<c10/util/logging_is_google_glog.h>", private, "<c10/util/Logging.h>", public ] }, | ||
{ include: [ "<c10/util/logging_is_not_google_glog.h>", private, "<c10/util/Logging.h>", public ] }, | ||
|
||
{ include: [ "<c10/util/qint32.h>", public, "<c10/core/ScalarType.h>", public ] }, | ||
{ include: [ "<c10/util/qint8.h>", public, "<c10/core/ScalarType.h>", public ] }, | ||
{ include: [ "<c10/util/quint2x4.h>", public, "<c10/core/ScalarType.h>", public ] }, | ||
{ include: [ "<c10/util/quint4x4.h>", public, "<c10/core/ScalarType.h>", public ] }, | ||
{ include: [ "<c10/util/quint8.h>", public, "<c10/core/ScalarType.h>", public ] }, | ||
|
||
{ include: ["<c10/core/Backend.h>", public, "<c10/core/TensorOptions.h>", public] }, | ||
{ include: ["<c10/core/Device.h>", public, "<c10/core/TensorOptions.h>", public] }, | ||
{ include: ["<c10/core/DefaultDtype.h>", public, "<c10/core/TensorOptions.h>", public] }, | ||
{ include: ["<c10/core/Layout.h>", public, "<c10/core/TensorOptions.h>", public] }, | ||
{ include: ["<c10/core/MemoryFormat.h>", public, "<c10/core/TensorOptions.h>", public] }, | ||
{ include: ["<c10/core/ScalarType.h>", public, "<c10/core/TensorOptions.h>", public] }, | ||
{ include: ["<c10/util/Optional.h>", public, "<c10/core/TensorOptions.h>", public] }, | ||
|
||
{ include: ["<c10/core/Allocator.h>", public, "<c10/core/StorageImpl.h>", public] }, | ||
{ include: ["<c10/core/Device.h>", public, "<c10/core/StorageImpl.h>", public] }, | ||
{ include: ["<c10/core/ScalarType.h>", public, "<c10/core/StorageImpl.h>", public] }, | ||
{ include: ["<c10/core/StorageImpl.h>", public, "<c10/core/Storage.h>", public] }, | ||
|
||
{ include: ["<c10/util/ArrayRef.h>", public, "<c10/core/TensorImpl.h>", public] }, | ||
{ include: ["<c10/core/DispatchKeySet.h>", public, "<c10/core/TensorImpl.h>", public] }, | ||
{ include: ["<c10/core/Layout.h>", public, "<c10/core/TensorImpl.h>", public] }, | ||
{ include: ["<c10/core/Storage.h>", public, "<c10/core/TensorImpl.h>", public] }, | ||
{ include: ["<c10/core/MemoryFormat.h>", public, "<c10/core/TensorImpl.h>", public] }, | ||
|
||
{ symbol: [ "c10::complex", private, "<c10/util/complex.h>", public ] }, | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import sys | ||
import re | ||
|
||
QUOTE_INCLUDE_RE = re.compile(r'^#include "(.*)"') | ||
ANGLE_INCLUDE_RE = re.compile(r'^#include <(.*)>') | ||
|
||
# By default iwyu will pick the C include, but we prefer the C++ headers | ||
STD_C_HEADER_MAP = { | ||
"<assert.h>": "<cassert>", | ||
"<complex.h>": "<ccomplex>", | ||
"<ctype.h>": "<cctype>", | ||
"<errno.h>": "<cerrno>", | ||
"<fenv.h>": "<cfenv>", | ||
"<float.h>": "<cfloat>", | ||
"<inttypes.h>": "<cinttypes>", | ||
"<iso646.h>": "<ciso646>", | ||
"<limits.h>": "<climits>", | ||
"<locale.h>": "<clocale>", | ||
"<math.h>": "<cmath>", | ||
"<setjmp.h>": "<csetjmp>", | ||
"<signal.h>": "<csignal>", | ||
"<stdalign.h>": "<cstdalign>", | ||
"<stdarg.h>": "<cstdarg>", | ||
"<stdbool.h>": "<cstdbool>", | ||
"<stddef.h>": "<cstddef>", | ||
"<stdint.h>": "<cstdint>", | ||
"<stdio.h>": "<cstdio>", | ||
"<stdlib.h>": "<cstdlib>", | ||
"<string.h>": "<cstring>", | ||
"<tgmath.h>": "<ctgmath>", | ||
"<time.h>": "<ctime>", | ||
"<uchar.h>": "<cuchar>", | ||
"<wchar.h>": "<cwchar>", | ||
"<wctype.h>": "<cwctype>", | ||
} | ||
|
||
def main() -> None: | ||
for line in sys.stdin: | ||
# Convert all quoted includes to angle brackets | ||
match = QUOTE_INCLUDE_RE.match(line) | ||
if match is not None: | ||
print(f"#include <{match.group(1)}>{line[match.end(0):]}", end='') | ||
continue | ||
|
||
match = ANGLE_INCLUDE_RE.match(line) | ||
if match is not None: | ||
path = f"<{match.group(1)}>" | ||
new_path = STD_C_HEADER_MAP.get(path, path) | ||
tail = line[match.end(0):] | ||
if len(tail) > 1: | ||
tail = ' ' + tail | ||
print(f"#include {new_path}{tail}", end='') | ||
continue | ||
|
||
print(line, end='') | ||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
[ | ||
{ include: [ "@<gtest/internal/.*>", private, "<gtest/gtest.h>", public ] }, | ||
{ include: [ "@<gtest/[^/]*>", public, "<gtest/gtest.h>", public ] }, | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[ | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# Run include-what-you-use on a file or folder | ||
# e.g. tools/iwyu/run.sh aten/src/ATen/native/sparse/SparseBlas.cpp | ||
# Which will print suggested changes to the console | ||
# | ||
# Currently the include mappings aren't good enough to trust iwyu's | ||
# output e.g. we probably just want to include Tensor.h and trust it | ||
# brings in the c10 headers. So, for now, use iwyu as a guide and | ||
# update includes manually. | ||
|
||
TORCH_ROOT=$(dirname $(dirname $(dirname $(readlink -f $0)))) | ||
|
||
iwyu_tool -p $TORCH_ROOT/build $@ -- -Wno-unknown-warning-option -Xiwyu \ | ||
--no_fwd_decls -Xiwyu --mapping_file=$TORCH_ROOT/tools/iwyu/all.imp \ | ||
| python $TORCH_ROOT/tools/iwyu/fixup.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# Toolchain-specific mappings. These ones seem to be missing from iwyu defaults | ||
[ | ||
{ include: [ "<bits/struct_stat.h>", private, "<sys/stat.h>", public ] }, | ||
{ include: [ "<bits/stdint-uintn.h>", private, "<cstdint>", public ] }, | ||
{ include: [ "<bits/stdint-intn.h>", private, "<cstdint>", public ] }, | ||
{ include: [ "<bits/pthreadtypes.h>", private, "<pthread.h>", public ] }, | ||
{ include: [ "<bits/exception.h>", private, "<stdexcept>", public ] }, | ||
{ include: [ "<bits/refwrap.h>", private, "<reference_wrapper>", public ] }, | ||
{ include: [ "<bits/shared_ptr.h>", private, "<memory>", public ] }, | ||
{ include: [ "<bits/std_function.h>", private, "<functional>", public ] }, | ||
|
||
{ symbol: [ "size_t", private, "<cstddef>", public ] }, | ||
{ symbol: [ "ptrdiff_t", private, "<cstddef>", public ] }, | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[ | ||
] |