提交 013bb14f 编写于 作者: M Megvii Engine Team

build(tablegen): pregen opdef tablegen targets

GitOrigin-RevId: ab7564df14099007d188af4ba7cff28a0cda526c
上级 f12b75c0
...@@ -66,8 +66,8 @@ target_link_libraries(${MODULE_NAME} PRIVATE nlohmann_json::nlohmann_json) ...@@ -66,8 +66,8 @@ target_link_libraries(${MODULE_NAME} PRIVATE nlohmann_json::nlohmann_json)
target_include_directories( target_include_directories(
${MODULE_NAME} ${MODULE_NAME}
PUBLIC src/include PUBLIC src/include
PRIVATE ${PYTHON_INCLUDE_DIRS} ${NUMPY_INCLUDE_DIR} ${MGB_OPDEF_OUT_DIR} PRIVATE ${PYTHON_INCLUDE_DIRS} ${NUMPY_INCLUDE_DIR} ${CPP_REDIS_INCLUDES})
${CPP_REDIS_INCLUDES}) target_link_libraries(${MODULE_NAME} PRIVATE mgb_opdef_inc)
target_compile_definitions(${MODULE_NAME} PRIVATE MODULE_NAME=${MODULE_NAME}) target_compile_definitions(${MODULE_NAME} PRIVATE MODULE_NAME=${MODULE_NAME})
target_compile_options(${MODULE_NAME} PRIVATE -Wno-unused-parameter) target_compile_options(${MODULE_NAME} PRIVATE -Wno-unused-parameter)
if(CXX_SUPPORT_WCLASS_MEMACCESS) if(CXX_SUPPORT_WCLASS_MEMACCESS)
......
# mgb tablegen executable set(MGB_OPDEF_OUT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/generated)
set(TABLE_TARGET mgb-mlir-autogen) set(MGB_OPDEF_OPS_SRC ${CMAKE_SOURCE_DIR}/src/core/include/megbrain/ir/ops.td)
file(GLOB_RECURSE SRCS ${CMAKE_CURRENT_SOURCE_DIR}/*.h set(MGB_OPDEF_PARAMS_SRC ${CMAKE_SOURCE_DIR}/dnn/scripts/opr_param_defs.py)
# we set CMAKE_CONFIGURE_DEPENDS so that when source files or hash.txt was modified,
# cmake configure would be triggered to update ${MD5_MISMATCH}
execute_process(
COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_SOURCE_DIR}/checkhash.cmake
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
ERROR_QUIET
RESULT_VARIABLE MD5_MISMATCH)
if(${MD5_MISMATCH})
# mgb tablegen executable
set(TABLE_TARGET mgb-mlir-autogen)
file(GLOB_RECURSE SRCS ${CMAKE_CURRENT_SOURCE_DIR}/*.h
${CMAKE_CURRENT_SOURCE_DIR}/*.cpp) ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp)
add_executable(${TABLE_TARGET} ${SRCS}) add_executable(${TABLE_TARGET} ${SRCS})
target_include_directories(${TABLE_TARGET} PRIVATE ${MLIR_LLVM_INCLUDE_DIR}) target_include_directories(${TABLE_TARGET} PRIVATE ${MLIR_LLVM_INCLUDE_DIR})
target_link_libraries(${TABLE_TARGET} PRIVATE LLVMTableGen MLIRTableGen LLVMSupport) target_link_libraries(${TABLE_TARGET} PRIVATE LLVMTableGen MLIRTableGen LLVMSupport)
set(MGB_TABLEGEN_EXE ${TABLE_TARGET}) set(MGB_TABLEGEN_EXE ${TABLE_TARGET})
# generate megbrain opdef c header and python bindings basically same as
# third_party/llvm-project/llvm/cmake/modules/TableGen.cmake but change output folder
# and add extra dependency
set(LLVM_SOURCE_DIR ${CMAKE_SOURCE_DIR}/third_party/llvm-project/llvm)
set(LLVM_BINARY_DIR ${CMAKE_BINARY_DIR}/third_party/llvm-project/llvm)
set(MLIR_SOURCE_DIR ${CMAKE_SOURCE_DIR}/third_party/llvm-project/mlir)
set(MLIR_BINARY_DIR ${CMAKE_BINARY_DIR}/third_party/llvm-project/mlir)
set(MGB_TABLEGEN_INCLUDES
-I${LLVM_SOURCE_DIR}/include
-I${LLVM_BINARY_DIR}/include
-I${MLIR_SOURCE_DIR}/include
-I${MLIR_BINARY_DIR}/include
-I${CMAKE_SOURCE_DIR}/src/core/include/megbrain/ir
-I${CMAKE_BINARY_DIR}/src/core/include/megbrain/ir)
set(MGB_TABLEGEN_FLAGS --write-if-changed)
set(MGB_TABLEGEN_TARGETS)
# generate megbrain opdef c header and python bindings function(tablegen_opdef target output)
set(LLVM_TARGET_DEFINITIONS ${MGE_IR_DIR}/ops.td) add_custom_target(
tablegen(MGB opdef.h.inl ${MGE_IR_INCLUDE_DIRS} "--gen-cpp-header") mgb_opdef_${target}
tablegen(MGB opdef.cpp.inl ${MGE_IR_INCLUDE_DIRS} "--gen-cpp-body") COMMAND
tablegen(MGB opdef.py.inl ${MGE_IR_INCLUDE_DIRS} "--gen-python-binding") ${MGB_TABLEGEN_EXE} ${MGB_TABLEGEN_INCLUDES} --gen-${target}
tablegen(MGB opdef.cpy.inl ${MGE_IR_INCLUDE_DIRS} "--gen-python-c-extension") ${MGB_OPDEF_OPS_SRC} ${MGB_TABLEGEN_FLAGS} -o ${MGB_OPDEF_OUT_DIR}/${output}
tablegen(MGB enum_macro.h ${MGE_IR_INCLUDE_DIRS} "--gen-enum-list-macro") DEPENDS param_defs_tblgen)
add_custom_target(mgb_opdef ALL DEPENDS opdef.h.inl opdef.cpp.inl opdef.py.inl set(MGB_TABLEGEN_TARGETS
opdef.cpy.inl enum_macro.h param_defs_tblgen) ${MGB_TABLEGEN_TARGETS} mgb_opdef_${target}
set(MGB_OPDEF_OUT_DIR
${CMAKE_CURRENT_BINARY_DIR}
PARENT_SCOPE) PARENT_SCOPE)
endfunction()
tablegen_opdef(cpp-header opdef.h.inl)
tablegen_opdef(cpp-body opdef.cpp.inl)
tablegen_opdef(python-binding opdef.py.inl)
tablegen_opdef(python-c-extension opdef.cpy.inl)
tablegen_opdef(enum-list-macro enum_macro.h)
add_custom_target(
mgb_opdef_genhash
${CMAKE_COMMAND} -P genhash.cmake
DEPENDS ${MGB_TABLEGEN_TARGETS}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
add_custom_target(mgb_opdef DEPENDS ${MGB_TABLEGEN_TARGETS} mgb_opdef_genhash)
else()
# add extra dependencies for auto reconfiguration
set_property(
DIRECTORY
APPEND
PROPERTY CMAKE_CONFIGURE_DEPENDS
${MGB_OPDEF_OPS_SRC}
${MGB_OPDEF_PARAMS_SRC}
generated/opdef.h.inl
generated/opdef.cpp.inl
generated/opdef.py.inl
generated/opdef.cpy.inl
generated/enum_macro
generated/hash.txt)
# additional check for safety
add_custom_target(
mgb_opdef_checkhash
${CMAKE_COMMAND} -P ${CMAKE_CURRENT_SOURCE_DIR}/checkhash.cmake
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
add_custom_target(mgb_opdef DEPENDS mgb_opdef_checkhash)
endif()
add_library(mgb_opdef_inc INTERFACE)
target_include_directories(mgb_opdef_inc INTERFACE ${MGB_OPDEF_OUT_DIR})
add_dependencies(mgb_opdef_inc mgb_opdef)
...@@ -3,8 +3,9 @@ ...@@ -3,8 +3,9 @@
#include "./targets/pybind11.h" #include "./targets/pybind11.h"
#include "./targets/python_c_extension.h" #include "./targets/python_c_extension.h"
using llvm::raw_ostream; namespace {
using llvm::RecordKeeper;
using namespace mlir::tblgen;
enum ActionType { None, CppHeader, CppBody, Pybind, CPython, EnumListMacro }; enum ActionType { None, CppHeader, CppBody, Pybind, CPython, EnumListMacro };
...@@ -24,25 +25,35 @@ llvm::cl::opt<ActionType> action( ...@@ -24,25 +25,35 @@ llvm::cl::opt<ActionType> action(
EnumListMacro, "gen-enum-list-macro", EnumListMacro, "gen-enum-list-macro",
"Generate enum param list macro"))); "Generate enum param list macro")));
using namespace mlir::tblgen; template <llvm::TableGenMainFn* MainFn>
llvm::TableGenMainFn* WrapMain() {
return [](llvm::raw_ostream& os, llvm::RecordKeeper& records) -> bool {
os << "// clang-format off\n";
auto ret = MainFn(os, records);
os << "// clang-format on\n";
return ret;
};
}
} // namespace
int main(int argc, char** argv) { int main(int argc, char** argv) {
llvm::InitLLVM y(argc, argv); llvm::InitLLVM y(argc, argv);
llvm::cl::ParseCommandLineOptions(argc, argv); llvm::cl::ParseCommandLineOptions(argc, argv);
if (action == ActionType::CppHeader) { if (action == ActionType::CppHeader) {
return TableGenMain(argv[0], &gen_op_def_c_header); return TableGenMain(argv[0], WrapMain<&gen_op_def_c_header>());
} }
if (action == ActionType::CppBody) { if (action == ActionType::CppBody) {
return TableGenMain(argv[0], &gen_op_def_c_body); return TableGenMain(argv[0], WrapMain<&gen_op_def_c_body>());
} }
if (action == ActionType::Pybind) { if (action == ActionType::Pybind) {
return TableGenMain(argv[0], &gen_op_def_pybind11); return TableGenMain(argv[0], WrapMain<&gen_op_def_pybind11>());
} }
if (action == ActionType::CPython) { if (action == ActionType::CPython) {
return TableGenMain(argv[0], &gen_op_def_python_c_extension); return TableGenMain(argv[0], WrapMain<&gen_op_def_python_c_extension>());
} }
if (action == ActionType::EnumListMacro) { if (action == ActionType::EnumListMacro) {
return TableGenMain(argv[0], &gen_enum_param_list_macro); return TableGenMain(argv[0], WrapMain<&gen_enum_param_list_macro>());
} }
return -1; return -1;
} }
set(SOURCES
../../dnn/scripts/opr_param_defs.py
../../src/core/include/megbrain/ir/ops.td
generated/opdef.h.inl
generated/opdef.cpp.inl
generated/opdef.py.inl
generated/opdef.cpy.inl
generated/enum_macro.h)
execute_process(COMMAND ${CMAKE_COMMAND} -E md5sum ${SOURCES}
OUTPUT_VARIABLE GENERATED_HASH_CONTENT)
file(READ generated/hash.txt HASH_CONTENT)
if(NOT "${GENERATED_HASH_CONTENT}" STREQUAL "${HASH_CONTENT}")
message(FATAL_ERROR "File ops.td was changed, please rerun cmake configure")
endif()
// clang-format off
#define FOR_EACH_ENUM_PARAM(cb) \
cb(::megdnn::param::PoolingV0::Mode); \
cb(::megdnn::param::Convolution::Format); \
cb(::megdnn::param::Argsort::Order); \
cb(::megdnn::param::ConvBiasV0::NonlineMode); \
cb(::megdnn::param::ConvolutionV0::Mode); \
cb(::megdnn::param::ConvolutionV0::Sparse); \
cb(::megdnn::param::ConvolutionV1::ComputeMode); \
cb(::megdnn::param::BN::ParamDim); \
cb(::megdnn::param::BN::FwdMode); \
cb(::megdnn::param::MatrixMulV1::ComputeMode); \
cb(::megdnn::param::MatrixMul::Format); \
cb(::megdnn::param::CollectiveComm::Mode); \
cb(::megdnn::param::Convolution3D::Mode); \
cb(::megdnn::param::Convolution3D::Sparse); \
cb(::megdnn::param::Convolution3D::DataType); \
cb(::megdnn::param::Convolution3D::Format); \
cb(::megdnn::param::ConvolutionV0::Format); \
cb(::megdnn::param::CvtColor::Mode); \
cb(::megdnn::param::Elemwise::Mode); \
cb(::megdnn::param::ElemwiseMultiType::Mode); \
cb(::megdnn::param::Padding::PaddingMode); \
cb(::megdnn::param::RNNCell::NonlineMode); \
cb(::megdnn::param::ROIAlignV0::Mode); \
cb(::megdnn::param::ROIPooling::Mode); \
cb(::megdnn::param::Reduce::Mode); \
cb(::megdnn::param::Reduce::DataType); \
cb(::megdnn::param::WarpPerspectiveV1::InterpolationMode); \
cb(::megdnn::param::WarpPerspectiveV1::BorderMode); \
cb(::megdnn::param::TopK::Mode);
#define FOR_EACH_BIT_COMBINED_ENUM_PARAM(cb) \
cb(::megdnn::param::ExecutionPolicy::Strategy);
// clang-format on
0df57b38e71a4d1882ed6c24f3a26b57 ../../dnn/scripts/opr_param_defs.py
759bfbf27fd3f0dd6b6edf06377e1d6b ../../src/core/include/megbrain/ir/ops.td
c613316001b5f0294ede198f5563f041 generated/opdef.h.inl
a1f7f13c909f9d4c173277f4ed28fb61 generated/opdef.cpp.inl
cf48f9ca352fabaeb6c846c11c6b1662 generated/opdef.py.inl
12365b938f564e5b3639d309f7c83414 generated/opdef.cpy.inl
71e1462bf4d882e2615c3c632cb671cc generated/enum_macro.h
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
set(SOURCES
../../dnn/scripts/opr_param_defs.py
../../src/core/include/megbrain/ir/ops.td
generated/opdef.h.inl
generated/opdef.cpp.inl
generated/opdef.py.inl
generated/opdef.cpy.inl
generated/enum_macro.h)
execute_process(COMMAND ${CMAKE_COMMAND} -E md5sum ${SOURCES}
OUTPUT_VARIABLE HASH_CONTENT)
message(STATUS "Generating hash.txt for opdefs")
file(WRITE generated/hash.txt "${HASH_CONTENT}")
...@@ -12,9 +12,9 @@ endif() ...@@ -12,9 +12,9 @@ endif()
# TODO: turn python binding into a static/object library # TODO: turn python binding into a static/object library
add_executable(imperative_test ${SOURCES} ${SRCS}) add_executable(imperative_test ${SOURCES} ${SRCS})
add_dependencies(imperative_test mgb_opdef) add_dependencies(imperative_test mgb_opdef)
target_include_directories( target_include_directories(imperative_test PRIVATE ${MGB_TEST_DIR}/include
imperative_test PRIVATE ${MGB_TEST_DIR}/include ../src/include ${MGB_OPDEF_OUT_DIR} ../src/include ${CPP_REDIS_INCLUDES})
${CPP_REDIS_INCLUDES}) target_link_libraries(imperative_test mgb_opdef_inc)
# Python binding # Python binding
target_include_directories( target_include_directories(
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册