mirror of
https://github.com/pytorch/pytorch.git
synced 2026-10-28 18:56:51 +08:00
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
76 lines
2.8 KiB
C++
76 lines
2.8 KiB
C++
#include <torch/csrc/jit/backends/backend.h>
|
|
#include <torch/csrc/jit/backends/backend_preprocess.h>
|
|
#include <torch/csrc/jit/passes/dead_code_elimination.h>
|
|
#include <torch/csrc/jit/passes/inliner.h>
|
|
|
|
namespace torch {
|
|
namespace jit {
|
|
namespace {
|
|
// For this backend, the actual compilation happens in preprocess function AOT.
|
|
// Put here for demonstration of backend
|
|
// as a whole piece. It's used when compilation is required. A dummy function
|
|
// can be passed when there's no usage of compilation in runtime backend lib.
|
|
c10::IValue preprocess(
|
|
const Module& mod,
|
|
const c10::Dict<IValue, IValue>& method_compile_spec,
|
|
const BackendDebugHandleGenerator& generate_debug_handles) {
|
|
// The output of this process would produce a dictionary
|
|
// Key: method name.
|
|
// Val: compiled blob (represented by a string).
|
|
c10::Dict<IValue, IValue> compiled(StringType::get(), StringType::get());
|
|
|
|
for (const auto& method : mod.get_methods()) {
|
|
auto graph = toGraphFunction(method.function()).graph()->copy();
|
|
// Must inline the graph for debug info map.
|
|
Inline(*graph);
|
|
// This is here because to test module hierarchy we will have
|
|
// getattr nodes which after inlining dont serve any purpose.
|
|
// Without removing them we will run into compilation errors.
|
|
// So eliminate deadcode just remove those getattr nodes.
|
|
EliminateDeadCode(graph);
|
|
// NOLINTNEXTLINE(performance-unnecessary-copy-initialization)
|
|
auto key = method.name();
|
|
auto node_debug_handles = generate_debug_handles(graph);
|
|
std::stringstream ss;
|
|
for (const auto& node : graph->nodes()) {
|
|
switch (node->kind()) {
|
|
case prim::Constant:
|
|
ss << node->kind().toDisplayString() << '#'
|
|
<< toIValue(node->output()).value();
|
|
ss << "<debug_handle>" << node_debug_handles[node];
|
|
break;
|
|
// NOLINTNEXTLINE(bugprone-branch-clone)
|
|
case aten::add:
|
|
ss << node->kind().toQualString();
|
|
ss << "<debug_handle>" << node_debug_handles[node];
|
|
break;
|
|
case aten::sub:
|
|
ss << node->kind().toQualString();
|
|
ss << "<debug_handle>" << node_debug_handles[node];
|
|
break;
|
|
default:
|
|
TORCH_CHECK(
|
|
false,
|
|
"The node of ",
|
|
node->kind().toQualString(),
|
|
" is not supported in this compiler. Source code: ",
|
|
node->sourceRange().str());
|
|
}
|
|
ss << ',';
|
|
}
|
|
std::string blob = std::move(ss).str();
|
|
if (!blob.empty()) {
|
|
blob.pop_back();
|
|
}
|
|
compiled.insert(method.name(), blob);
|
|
}
|
|
return compiled;
|
|
}
|
|
|
|
constexpr auto backend_name = "backend_with_compiler_demo";
|
|
static auto pre_reg = backend_preprocess_register(backend_name, preprocess);
|
|
} // namespace
|
|
|
|
} // namespace jit
|
|
} // namespace torch
|