Refactoring macros in op_registry.h
Created by: JiayiFeng
In op_registry.h
, we use many macros to help use register operators and their kernels, like:
#define REGISTER_OP_KERNEL(type, DEVICE_TYPE, PlaceType, ...) \
STATIC_ASSERT_GLOBAL_NAMESPACE( \
__reg_op_kernel_##type##_##DEVICE_TYPE##__, \
"REGISTER_OP_KERNEL must be in global namespace"); \
struct __op_kernel_register__##type##__##DEVICE_TYPE##__ { \
__op_kernel_register__##type##__##DEVICE_TYPE##__() { \
::paddle::framework::OperatorWithKernel::OpKernelKey key; \
key.place_ = PlaceType(); \
::paddle::framework::OperatorWithKernel::AllOpKernels()[#type][key] \
.reset(new __VA_ARGS__()); \
} \
}; \
static __op_kernel_register__##type##__##DEVICE_TYPE##__ \
__reg_kernel_##type##__##DEVICE_TYPE##__; \
int __op_kernel_register_##type##_handle_##DEVICE_TYPE##__() { return 0; }
The registry process will be executed in the constructor of the registrar:
static __op_kernel_register__##type##__##DEVICE_TYPE##__ \
__reg_kernel_##type##__##DEVICE_TYPE##__`
The registrar is only initilized but never used. To make sure it will be linked, we use another series of macro USE_XXX
to invoke the handle function:
int __op_kernel_register_##type##_handle_##DEVICE_TYPE##__() { return 0; }
If the cpp linker does link jobs based on modules, everything is fine. However, some linkers work based on symbols, which may cause the unexpected result that handle functions are linked in while the registrars are still out.
So a refactoring of these macros in needed, to make sure that all parts can be registered and linked properly.