Files
Richard Barnes 4b7c6760bf Remove unreachable breaks in caffe2 flagged by -Wunreachable-code-break (#202094)
Summary:
D127333113 enables `-Wunreachable-code-break` through the PACKAGE files of `fbcode/cad_agent` and its siblings, which covers `fbcode/caffe2`. This diff fixes the code that warning flags in `caffe2` so the PACKAGE change builds cleanly on top of it. It stacks on D127367476, which fixes ATen `Convolution.cpp` separately because many other directories need that fix too.

Most of the removed `break`s follow statements that always throw (`TORCH_CHECK(false, ...)`, `TORCH_CHECK_BUFFER(false, ...)`, `TORCH_INTERNAL_ASSERT(false, ...)`, `C10_THROW_ERROR`, `CAFFE_THROW`, `throw`) or a `return`, including `} break;` after case blocks whose paths all return: ATen `DLConvertor`, Vulkan `Pipeline`/`Tensor`/`BinaryOp`, `c10/cuda/CUDAFunctions.cpp` and its hipified copy, `caffe2/python/pybind_state_ideep.cc`, `caffe2/operators/pool_op_cudnn.cc`, `c10d/ProcessGroupNCCL.cpp`, `nativert/graph/GraphSignature.cpp`, `autograd/python_variable.cpp`, the JIT frontend, serialization, passes, runtime, backends and tensorexpr files, and `test/cpp/jit/test_backend_compiler_preprocess.cpp`.

In `jit/python/pybind_utils.cpp`, the `RRefType` case block returns with `USE_RPC` and throws without it, so its `} break;` becomes `}`.

With those breaks gone, the trailing `return` after the switch in `function_schema_parser.cpp` (`parseDefaultValue`), `dtype_analysis.cpp` and `onnx/function_extraction.cpp` becomes the first unreachable statement (`-Wunreachable-code-return`), so it is removed too: every path through those switches already returns or throws.

Control flow is unchanged everywhere. `fbcode/caffe2` and its dirsync mirror `xplat/caffe2` are edited identically.

Test Plan:
With the PACKAGE change from D127333113 applied on top (so the new warning was active):
- `buck2 build fbcode//mode/dev --keep-going fbcode//caffe2:_ATen-cpu fbcode//caffe2:torch_vulkan_api fbcode//caffe2:ATen-vulkan fbcode//caffe2/c10/cuda:cuda fbcode//caffe2/caffe2/python:caffe2_pybind11_state fbcode//caffe2/test/cpp/jit:jit fbcode//caffe2:_ATen-core fbcode//caffe2:_libtorch fbcode//caffe2:_C_impl_cpu fbcode//caffe2:xnnpack_backend` succeeds.
- `buck2 build fbandroid/mode/dev fbsource//xplat/caffe2:{torch_core,torch_mobile_train,torch_mobile_deserialize,torch_vulkan_api,aten_vulkan,aten_cpu,flatbuffers_serializer_mobile,pt_ops_full}Android` succeeds.

Reviewed By: dtolnay

Differential Revision: D127367930

Pull Request resolved: https://github.com/pytorch/pytorch/pull/202094
Approved by: https://github.com/d4l3k, https://github.com/Skylion007
2026-10-26 21:56:00 +00:00
..
…

JIT C++ Tests

Adding a new test

First, create a new test file. Test files should have be placed in this directory, with a name that starts with test_, like test_foo.cpp.

In general a single test suite

Add your test file to the JIT_TEST_SRCS list in test/cpp/jit/CMakeLists.txt.

A test file may look like:

#include <gtest/gtest.h>

using namespace ::torch::jit

TEST(FooTest, BarBaz) {
   // ...
}

// Append '_CUDA' to the test case name will automatically filter it out if CUDA
// is not compiled.
TEST(FooTest, NeedsAGpu_CUDA) {
   // ...
}

// Similarly, if only one GPU is detected, tests with `_MultiCUDA` at the end
// will not be run.
TEST(FooTest, NeedsMultipleGpus_MultiCUDA) {
   // ...
}

Building and running the tests

The following commands assume you are in PyTorch root.

# ... Build PyTorch from source, e.g.
python -m pip install --no-build-isolation -v -e .
# (re)build just the binary
ninja -C build bin/test_jit
# run tests
build/bin/test_jit --gtest_filter='glob_style_filter*'