diff --git a/include/ceres/internal/autodiff.h b/include/ceres/internal/autodiff.h index 3505c855b..060332115 100644 --- a/include/ceres/internal/autodiff.h +++ b/include/ceres/internal/autodiff.h @@ -184,7 +184,7 @@ namespace internal { template struct Make1stOrderPerturbation { public: - static void Apply(const T* src, JetT* dst) { + inline static void Apply(const T* src, JetT* dst) { if (j == 0) { DCHECK(src); DCHECK(dst); @@ -217,7 +217,7 @@ struct Make1stOrderPerturbations, ParameterIdx, Offset> { template - static void Apply(T const* const* parameters, JetT* x) { + inline static void Apply(T const* const* parameters, JetT* x) { Make1stOrderPerturbation<0, N, Offset, T, JetT>::Apply( parameters[ParameterIdx], x + Offset); Make1stOrderPerturbations, @@ -280,7 +280,7 @@ struct Take1stOrderParts, ParameterIdx, Offset> { template - static void Apply(int num_outputs, JetT* output, T** jacobians) { + inline static void Apply(int num_outputs, JetT* output, T** jacobians) { if (jacobians[ParameterIdx]) { Take1stOrderPart(num_outputs, output, jacobians[ParameterIdx]); } diff --git a/internal/ceres/autodiff_benchmarks/CMakeLists.txt b/internal/ceres/autodiff_benchmarks/CMakeLists.txt index 06b3cf6a6..610ebc334 100644 --- a/internal/ceres/autodiff_benchmarks/CMakeLists.txt +++ b/internal/ceres/autodiff_benchmarks/CMakeLists.txt @@ -1,6 +1,9 @@ # TODO: Add support for other compilers if(CMAKE_CXX_COMPILER_ID MATCHES "Clang") - list(APPEND CERES_BENCHMARK_FLAGS "-mllvm" "-inline-threshold=1000000") + # Increase the inlining threshold only for those functions marked with an + # inline hint. This is typically far more realistic to significantly increase + # in a large code-base than -inline-threshold as that has a larger scope. + list(APPEND CERES_BENCHMARK_FLAGS "-mllvm" "-inlinehint-threshold=1000000") endif() add_executable(autodiff_benchmarks autodiff_benchmarks.cc) diff --git a/internal/ceres/autodiff_benchmarks/autodiff_benchmarks.cc b/internal/ceres/autodiff_benchmarks/autodiff_benchmarks.cc index 2ab15981f..f8b2a21f5 100644 --- a/internal/ceres/autodiff_benchmarks/autodiff_benchmarks.cc +++ b/internal/ceres/autodiff_benchmarks/autodiff_benchmarks.cc @@ -57,7 +57,7 @@ struct CostFunctionToFunctor { : cost_function(std::forward<_Args>(__args)...) {} template - bool operator()(_Args&&... __args) const { + inline bool operator()(_Args&&... __args) const { return cost_function(std::forward<_Args>(__args)...); } @@ -171,7 +171,7 @@ struct Rat43CostFunctor { Rat43CostFunctor(const double x, const double y) : x_(x), y_(y) {} template - bool operator()(const T* parameters, T* residuals) const { + inline bool operator()(const T* parameters, T* residuals) const { const T& b1 = parameters[0]; const T& b2 = parameters[1]; const T& b3 = parameters[2]; diff --git a/internal/ceres/autodiff_benchmarks/brdf_cost_function.h b/internal/ceres/autodiff_benchmarks/brdf_cost_function.h index 4134181d2..eba093277 100644 --- a/internal/ceres/autodiff_benchmarks/brdf_cost_function.h +++ b/internal/ceres/autodiff_benchmarks/brdf_cost_function.h @@ -48,14 +48,14 @@ struct Brdf { Brdf() {} template - bool operator()(const T* const material, - const T* const c_ptr, - const T* const n_ptr, - const T* const v_ptr, - const T* const l_ptr, - const T* const x_ptr, - const T* const y_ptr, - T* residual) const { + inline bool operator()(const T* const material, + const T* const c_ptr, + const T* const n_ptr, + const T* const v_ptr, + const T* const l_ptr, + const T* const x_ptr, + const T* const y_ptr, + T* residual) const { using Vec3 = Eigen::Matrix; T metallic = material[0]; @@ -154,19 +154,19 @@ struct Brdf { } template - T SchlickFresnel(const T& u) const { + inline T SchlickFresnel(const T& u) const { T m = T(1) - u; const T m2 = m * m; return m2 * m2 * m; // (1-u)^5 } template - T Aspect(const T& anisotropic) const { + inline T Aspect(const T& anisotropic) const { return T(sqrt(T(1) - anisotropic * T(0.9))); } template - T SmithG_GGX(const T& n_dot_v, const T& alpha_g) const { + inline T SmithG_GGX(const T& n_dot_v, const T& alpha_g) const { const T a = alpha_g * alpha_g; const T b = n_dot_v * n_dot_v; return T(1) / (n_dot_v + T(sqrt(a + b - a * b))); @@ -175,7 +175,7 @@ struct Brdf { // Generalized-Trowbridge-Reitz (GTR) Microfacet Distribution // See paper, Appendix B template - T GTR1(const T& n_dot_h, const T& a) const { + inline T GTR1(const T& n_dot_h, const T& a) const { T result = T(0); if (a >= T(1)) { @@ -189,7 +189,7 @@ struct Brdf { } template - T GTR2Aniso(const T& n_dot_h, + inline T GTR2Aniso(const T& n_dot_h, const T& h_dot_x, const T& h_dot_y, const T& ax, @@ -205,9 +205,10 @@ struct Brdf { } template - typename Derived1::PlainObject Lerp(const Eigen::MatrixBase& a, - const Eigen::MatrixBase& b, - typename Derived1::Scalar alpha) const { + inline typename Derived1::PlainObject + Lerp(const Eigen::MatrixBase& a, + const Eigen::MatrixBase& b, + typename Derived1::Scalar alpha) const { return (typename Derived1::Scalar(1) - alpha) * a + alpha * b; } diff --git a/internal/ceres/autodiff_benchmarks/constant_cost_function.h b/internal/ceres/autodiff_benchmarks/constant_cost_function.h index 00f39d6a9..caa0431d8 100644 --- a/internal/ceres/autodiff_benchmarks/constant_cost_function.h +++ b/internal/ceres/autodiff_benchmarks/constant_cost_function.h @@ -40,7 +40,7 @@ template struct ConstantCostFunction : public ceres::SizedCostFunction<1, kParameterBlockSize> { template - bool operator()(const T* const x, T* residuals) const { + inline bool operator()(const T* const x, T* residuals) const { residuals[0] = T(5); return true; } diff --git a/internal/ceres/autodiff_benchmarks/linear_cost_functions.h b/internal/ceres/autodiff_benchmarks/linear_cost_functions.h index 4ead04a4b..2f2552f3e 100644 --- a/internal/ceres/autodiff_benchmarks/linear_cost_functions.h +++ b/internal/ceres/autodiff_benchmarks/linear_cost_functions.h @@ -38,7 +38,7 @@ namespace ceres { struct Linear1CostFunction { template - bool operator()(const T* const x, T* residuals) const { + inline bool operator()(const T* const x, T* residuals) const { residuals[0] = x[0] + T(10); return true; } @@ -46,7 +46,7 @@ struct Linear1CostFunction { struct Linear10CostFunction { template - bool operator()(const T* const x, T* residuals) const { + inline bool operator()(const T* const x, T* residuals) const { for (int i = 0; i < 10; ++i) { residuals[i] = x[i] + T(i); } diff --git a/internal/ceres/autodiff_benchmarks/photometric_error.h b/internal/ceres/autodiff_benchmarks/photometric_error.h index 9107ca0f9..8ed278d3a 100644 --- a/internal/ceres/autodiff_benchmarks/photometric_error.h +++ b/internal/ceres/autodiff_benchmarks/photometric_error.h @@ -102,8 +102,8 @@ struct PhotometricError { intrinsics_(intrinsics) {} template - bool Project(Eigen::Matrix& proj, - const Eigen::Matrix& p) const { + inline bool Project(Eigen::Matrix& proj, + const Eigen::Matrix& p) const { const double& fx = intrinsics_[0]; const double& fy = intrinsics_[1]; const double& cx = intrinsics_[2]; @@ -136,10 +136,10 @@ struct PhotometricError { } template - bool operator()(const T* const pose_host_ptr, - const T* const pose_target_ptr, - const T* const idist_ptr, - T* residuals_ptr) const { + inline bool operator()(const T* const pose_host_ptr, + const T* const pose_target_ptr, + const T* const idist_ptr, + T* residuals_ptr) const { Eigen::Map> q_w_h(pose_host_ptr); Eigen::Map> t_w_h(pose_host_ptr + 4); Eigen::Map> q_w_t(pose_target_ptr); diff --git a/internal/ceres/autodiff_benchmarks/relative_pose_error.h b/internal/ceres/autodiff_benchmarks/relative_pose_error.h index 93b28c74e..b5c1a936b 100644 --- a/internal/ceres/autodiff_benchmarks/relative_pose_error.h +++ b/internal/ceres/autodiff_benchmarks/relative_pose_error.h @@ -48,9 +48,9 @@ struct RelativePoseError { : meas_q_i_j_(q_i_j), meas_t_i_j_(t_i_j) {} template - bool operator()(const T* const pose_i_ptr, - const T* const pose_j_ptr, - T* residuals_ptr) const { + inline bool operator()(const T* const pose_i_ptr, + const T* const pose_j_ptr, + T* residuals_ptr) const { Eigen::Map> q_w_i(pose_i_ptr); Eigen::Map> t_w_i(pose_i_ptr + 4); Eigen::Map> q_w_j(pose_j_ptr); diff --git a/internal/ceres/autodiff_benchmarks/snavely_reprojection_error.h b/internal/ceres/autodiff_benchmarks/snavely_reprojection_error.h index 8da8bea65..795342f71 100644 --- a/internal/ceres/autodiff_benchmarks/snavely_reprojection_error.h +++ b/internal/ceres/autodiff_benchmarks/snavely_reprojection_error.h @@ -42,9 +42,9 @@ struct SnavelyReprojectionError { SnavelyReprojectionError() = default; template - bool operator()(const T* const camera, - const T* const point, - T* residuals) const { + inline bool operator()(const T* const camera, + const T* const point, + T* residuals) const { T ox = T(observed_x); T oy = T(observed_y);