mirror of
https://github.com/pytorch/pytorch.git
synced 2025-10-20 12:54:11 +08:00
[pytorch] convert code analyzer to a binary (#33102)
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/33102 Add a simple main() to build code analyzer as a binary. This enables easier integration with FB internal build environment. ghstack-source-id: 97958658 Test Plan: - CI Differential Revision: D19798560 Pulled By: ljk53 fbshipit-source-id: 126230e3bf7568046a309e8a6785230f820e0222
This commit is contained in:
committed by
Facebook Github Bot
parent
e8c4f5a74b
commit
495c1df510
@ -8,15 +8,15 @@ add_definitions(${LLVM_DEFINITIONS})
|
||||
include_directories(${LLVM_INCLUDE_DIRS})
|
||||
link_directories(${LLVM_LIBRARY_DIRS})
|
||||
|
||||
add_library(OpDependencyPass MODULE
|
||||
# Main executable
|
||||
add_executable(analyzer
|
||||
analyzer.cpp
|
||||
op_dependency.cpp
|
||||
)
|
||||
|
||||
set_target_properties(OpDependencyPass PROPERTIES
|
||||
set_target_properties(analyzer PROPERTIES
|
||||
COMPILE_FLAGS "-fno-rtti -O3")
|
||||
|
||||
if(APPLE)
|
||||
set_target_properties(OpDependencyPass PROPERTIES
|
||||
LINK_FLAGS "-undefined dynamic_lookup"
|
||||
)
|
||||
endif(APPLE)
|
||||
llvm_map_components_to_libnames(llvm_libs core irreader support)
|
||||
|
||||
target_link_libraries(analyzer ${llvm_libs})
|
||||
|
32
tools/code_analyzer/analyzer.cpp
Normal file
32
tools/code_analyzer/analyzer.cpp
Normal file
@ -0,0 +1,32 @@
|
||||
#include "llvm/IR/LLVMContext.h"
|
||||
#include "llvm/IR/Module.h"
|
||||
#include "llvm/IRReader/IRReader.h"
|
||||
#include "llvm/Pass.h"
|
||||
#include "llvm/PassRegistry.h"
|
||||
#include "llvm/Support/CommandLine.h"
|
||||
#include "llvm/Support/SourceMgr.h"
|
||||
|
||||
using namespace llvm;
|
||||
|
||||
namespace {
|
||||
|
||||
cl::opt<std::string> InputFilename(
|
||||
cl::Positional,
|
||||
cl::desc("<input bitcode file>"),
|
||||
cl::init("-"),
|
||||
cl::value_desc("filename"));
|
||||
|
||||
} // namespace
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
LLVMContext Context;
|
||||
cl::ParseCommandLineOptions(argc, argv);
|
||||
SMDiagnostic Err;
|
||||
std::unique_ptr<Module> M = parseIRFile(InputFilename, Err, Context);
|
||||
|
||||
auto opDependencyPass = PassRegistry::getPassRegistry()
|
||||
->getPassInfo(StringRef("op_dependency"))
|
||||
->createPass();
|
||||
static_cast<ModulePass*>(opDependencyPass)->runOnModule(*M);
|
||||
return 0;
|
||||
}
|
@ -72,21 +72,10 @@ build_test_project() {
|
||||
}
|
||||
|
||||
call_analyzer() {
|
||||
echo "Analyze: ${INPUT}"
|
||||
|
||||
"${LLVM_DIR}/bin/opt" \
|
||||
-load="${BUILD_ROOT}/libOpDependencyPass.so" \
|
||||
-op_dependency \
|
||||
-disable-output \
|
||||
-op_schema_pattern="^(_aten|_prim|aten|quantized|profiler|_test)::[^ ]+" \
|
||||
-op_register_pattern="c10::RegisterOperators::(op|checkSchemaAndRegisterOp_)" \
|
||||
-op_invoke_pattern="c10::Dispatcher::findSchema|callOp" \
|
||||
-format="${FORMAT}" \
|
||||
${EXTRA_ANALYZER_FLAGS} \
|
||||
"${INPUT}" \
|
||||
> "${OUTPUT}"
|
||||
|
||||
echo "Result: ${OUTPUT}"
|
||||
ANALYZER_BIN="${BUILD_ROOT}/analyzer" \
|
||||
INPUT="${INPUT}" OUTPUT="${OUTPUT}" FORMAT="${FORMAT}" \
|
||||
EXTRA_ANALYZER_FLAGS="${EXTRA_ANALYZER_FLAGS}" \
|
||||
"${ANALYZER_SRC_HOME}/run_analyzer.sh"
|
||||
}
|
||||
|
||||
analyze_torch_mobile() {
|
||||
|
20
tools/code_analyzer/run_analyzer.sh
Executable file
20
tools/code_analyzer/run_analyzer.sh
Executable file
@ -0,0 +1,20 @@
|
||||
#!/bin/bash
|
||||
##############################################################################
|
||||
# Invoke code analyzer binary with pre-defined parameters for LibTorch.
|
||||
# This script should be called via build.sh. Do NOT use it directly.
|
||||
##############################################################################
|
||||
|
||||
set -exu
|
||||
|
||||
echo "Analyze: ${INPUT}"
|
||||
|
||||
"${ANALYZER_BIN}" \
|
||||
-op_schema_pattern="^(_aten|_prim|aten|quantized|profiler|_test)::[^ ]+" \
|
||||
-op_register_pattern="c10::RegisterOperators::(op|checkSchemaAndRegisterOp_)" \
|
||||
-op_invoke_pattern="c10::Dispatcher::findSchema|callOp" \
|
||||
-format="${FORMAT}" \
|
||||
${EXTRA_ANALYZER_FLAGS} \
|
||||
"${INPUT}" \
|
||||
> "${OUTPUT}"
|
||||
|
||||
echo "Result: ${OUTPUT}"
|
Reference in New Issue
Block a user