Skip to content

Commit

Permalink
Merge pull request #440 from bluescarni/pr/code_model
Browse files Browse the repository at this point in the history
Add LLVM option to select the code model
  • Loading branch information
bluescarni authored Jul 30, 2024
2 parents 6355a10 + db6feac commit 6c0a9c8
Show file tree
Hide file tree
Showing 28 changed files with 415 additions and 67 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/gha_ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ jobs:
- name: Build
shell: pwsh
run: |
conda install -y cmake llvmdev tbb-devel tbb libboost-devel xtensor xtensor-blas blas blas-devel 'fmt<11' spdlog sleef zlib libzlib 'mppp=1.*'
conda install -y cmake llvmdev tbb-devel tbb libboost-devel xtensor xtensor-blas blas blas-devel fmt spdlog sleef zlib libzlib 'mppp=1.*'
mkdir build
cd build
cmake ../ -G "Visual Studio 17 2022" -A x64 -DHEYOKA_BUILD_TESTS=yes -DHEYOKA_WITH_MPPP=yes -DHEYOKA_BUILD_TUTORIALS=ON -DHEYOKA_ENABLE_IPO=yes -DBoost_NO_BOOST_CMAKE=ON -DHEYOKA_WITH_SLEEF=yes
cmake ../ -G "Visual Studio 17 2022" -A x64 -DHEYOKA_BUILD_TESTS=yes -DHEYOKA_WITH_MPPP=yes -DHEYOKA_BUILD_TUTORIALS=ON -DHEYOKA_ENABLE_IPO=yes -DHEYOKA_WITH_SLEEF=yes
cmake --build . --config Release -j2
copy Release\heyoka.dll test\Release\
conda_release_static:
Expand Down
15 changes: 13 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ endif()

# Mandatory dependency on fmt.
set(_HEYOKA_MIN_SUPPORTED_FMT_VERSION 9)
set(_HEYOKA_MAX_SUPPORTED_FMT_VERSION 10)
set(_HEYOKA_MAX_SUPPORTED_FMT_VERSION 11)
find_package(fmt REQUIRED CONFIG)
if(${fmt_VERSION_MAJOR} VERSION_LESS ${_HEYOKA_MIN_SUPPORTED_FMT_VERSION} OR
${fmt_VERSION_MAJOR} VERSION_GREATER ${_HEYOKA_MAX_SUPPORTED_FMT_VERSION})
Expand All @@ -503,7 +503,18 @@ target_link_libraries(heyoka PRIVATE TBB::tbb)
# Mandatory dependency on Boost.
# NOTE: need 1.69 for safe numerics.
set(_HEYOKA_MIN_BOOST_VERSION "1.69")
find_package(Boost ${_HEYOKA_MIN_BOOST_VERSION} REQUIRED serialization)
# NOTE: we look for Boost in CONFIG mode first, as that has become the official supported way
# of locating Boost in recent Boost/CMake versions. If we fail, we try again in
# MODULE mode as last resort.
find_package(Boost ${_HEYOKA_MIN_BOOST_VERSION} QUIET COMPONENTS serialization CONFIG)
if(NOT ${Boost_FOUND})
message(STATUS "Boost not found in CONFIG mode, retrying in MODULE mode.")
find_package(Boost ${_HEYOKA_MIN_BOOST_VERSION} QUIET MODULE COMPONENTS serialization)
endif()
if(NOT ${Boost_FOUND})
message(FATAL_ERROR "Could not locate Boost in either CONFIG or MODULE mode.")
endif()
message(STATUS "Found Boost version ${Boost_VERSION}.")
target_link_libraries(heyoka PUBLIC Boost::boost Boost::serialization)
# NOTE: quench warnings from Boost when building the library.
target_compile_definitions(heyoka PRIVATE BOOST_ALLOW_DEPRECATED_HEADERS)
Expand Down
6 changes: 6 additions & 0 deletions config.hpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@

#endif

#if defined(__x86_64__) || defined(_M_AMD64) || defined(__i386__) || defined(_M_IX86)

#define HEYOKA_ARCH_X86

#endif

// Maximum number of blocks that can be processed in parallel
// when computing the Taylor derivatives in parallel mode.
#define HEYOKA_CM_PAR_MAX_INVOKE_N 20
Expand Down
21 changes: 21 additions & 0 deletions doc/changelog.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,27 @@
Changelog
=========

5.2.0 (unreleased)
------------------

New
~~~

- Add the possibility of specifying the LLVM code model
used for JIT compilation
(`#440 <https://github.com/bluescarni/heyoka/pull/440>`__).
- Enable support for fmt 11
(`#440 <https://github.com/bluescarni/heyoka/pull/440>`__).

Fix
~~~

- Fix build system warnings when using recent versions of
CMake and Boost
(`#440 <https://github.com/bluescarni/heyoka/pull/440>`__).
- Fix compilation on FreeBSD
(`#439 <https://github.com/bluescarni/heyoka/pull/439>`__).

5.1.0 (2024-07-21)
------------------

Expand Down
2 changes: 1 addition & 1 deletion doc/install.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ heyoka has the following **mandatory** dependencies:

* the `LLVM <https://llvm.org/>`__ compiler infrastructure library (version >=13 and <=18),
* the `Boost <https://www.boost.org/>`__ C++ libraries (version >=1.69),
* the `{fmt} <https://fmt.dev/latest/index.html>`__ library (version >=9 and <=10),
* the `{fmt} <https://fmt.dev/latest/index.html>`__ library (version >=9 and <=11),
* the `spdlog <https://github.com/gabime/spdlog>`__ library,
* the `TBB <https://github.com/oneapi-src/oneTBB>`__ library.

Expand Down
13 changes: 12 additions & 1 deletion heyoka-config.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,18 @@ if(${heyoka_WITH_MPPP})
endif()

# Mandatory public dependencies on Boost and fmt.
find_package(Boost @_HEYOKA_MIN_BOOST_VERSION@ REQUIRED serialization)

# NOTE: we look for Boost in CONFIG mode first, as that has become the official supported way
# of locating Boost in recent Boost/CMake versions. If we fail, we try again in
# MODULE mode as last resort.
find_package(Boost @_HEYOKA_MIN_BOOST_VERSION@ QUIET COMPONENTS serialization CONFIG)
if(NOT ${Boost_FOUND})
find_package(Boost @_HEYOKA_MIN_BOOST_VERSION@ QUIET MODULE COMPONENTS serialization)
endif()
if(NOT ${Boost_FOUND})
message(FATAL_ERROR "Could not locate Boost in either CONFIG or MODULE mode.")
endif()

find_package(fmt REQUIRED CONFIG)
if(${fmt_VERSION_MAJOR} VERSION_LESS @_HEYOKA_MIN_SUPPORTED_FMT_VERSION@ OR
${fmt_VERSION_MAJOR} VERSION_GREATER @_HEYOKA_MAX_SUPPORTED_FMT_VERSION@)
Expand Down
1 change: 1 addition & 0 deletions include/heyoka/kw.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ IGOR_MAKE_NAMED_ARGUMENT(fast_math);
// and LLVM learns to discriminate good and bad implementations.
IGOR_MAKE_NAMED_ARGUMENT(force_avx512);
IGOR_MAKE_NAMED_ARGUMENT(slp_vectorize);
IGOR_MAKE_NAMED_ARGUMENT(code_model);

// cfunc API.
IGOR_MAKE_NAMED_ARGUMENT(batch_size);
Expand Down
50 changes: 46 additions & 4 deletions include/heyoka/llvm_state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

#include <boost/numeric/conversion/cast.hpp>

#include <fmt/ostream.h>

#if defined(HEYOKA_HAVE_REAL128)

#include <mp++/real128.hpp>
Expand Down Expand Up @@ -108,6 +110,25 @@ HEYOKA_DLL_PUBLIC std::uint32_t recommended_simd_size<mppp::real>();

#endif

// Code model.
enum class code_model : unsigned { tiny, small, kernel, medium, large };

HEYOKA_DLL_PUBLIC std::ostream &operator<<(std::ostream &, code_model);

HEYOKA_END_NAMESPACE

// fmt formatter for taylor_outcome, implemented on top of the streaming operator.
namespace fmt
{

template <>
struct formatter<heyoka::code_model> : fmt::ostream_formatter {
};

} // namespace fmt

HEYOKA_BEGIN_NAMESPACE

class HEYOKA_DLL_PUBLIC llvm_state
{
friend HEYOKA_DLL_PUBLIC std::ostream &operator<<(std::ostream &, const llvm_state &);
Expand All @@ -125,6 +146,7 @@ class HEYOKA_DLL_PUBLIC llvm_state
bool m_fast_math;
bool m_force_avx512;
bool m_slp_vectorize;
code_model m_c_model;
std::string m_module_name;

// Serialization.
Expand All @@ -148,6 +170,9 @@ class HEYOKA_DLL_PUBLIC llvm_state
// the [0, 3] range.
static unsigned clamp_opt_level(unsigned);

// Helper to validate the code model enumerator,
static void validate_code_model(code_model);

// Implementation details for the variadic constructor.
template <typename... KwArgs>
static auto kw_args_ctor_impl(const KwArgs &...kw_args)
Expand Down Expand Up @@ -224,9 +249,24 @@ class HEYOKA_DLL_PUBLIC llvm_state
}
}();

return std::tuple{std::move(mod_name), opt_level, fmath, force_avx512, slp_vectorize};
// Code model (defaults to small).
auto c_model = [&p]() {
if constexpr (p.has(kw::code_model)) {
if constexpr (std::same_as<std::remove_cvref_t<decltype(p(kw::code_model))>, code_model>) {
return p(kw::code_model);
} else {
static_assert(detail::always_false_v<KwArgs...>,
"Invalid type for the 'code_model' keyword argument.");
}
} else {
return code_model::small;
}
}();
validate_code_model(c_model);

return std::tuple{std::move(mod_name), opt_level, fmath, force_avx512, slp_vectorize, c_model};
}
explicit llvm_state(std::tuple<std::string, unsigned, bool, bool, bool> &&);
explicit llvm_state(std::tuple<std::string, unsigned, bool, bool, bool, code_model> &&);

// Small shared helper to setup the math flags in the builder at the
// end of a constructor.
Expand Down Expand Up @@ -267,6 +307,7 @@ class HEYOKA_DLL_PUBLIC llvm_state
[[nodiscard]] bool force_avx512() const;
[[nodiscard]] unsigned get_opt_level() const;
[[nodiscard]] bool get_slp_vectorize() const;
[[nodiscard]] code_model get_code_model() const;

[[nodiscard]] std::string get_ir() const;
[[nodiscard]] std::string get_bc() const;
Expand Down Expand Up @@ -311,7 +352,8 @@ HEYOKA_END_NAMESPACE
// - version 1: got rid of the inline_functions setting;
// - version 2: added the force_avx512 setting;
// - version 3: added the bitcode snapshot, simplified
// compilation logic, slp_vectorize flag.
BOOST_CLASS_VERSION(heyoka::llvm_state, 3)
// compilation logic, slp_vectorize flag;
// - version 4: added the code_model option.
BOOST_CLASS_VERSION(heyoka::llvm_state, 4)

#endif
1 change: 1 addition & 0 deletions src/detail/llvm_helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include <boost/numeric/conversion/cast.hpp>

#include <fmt/format.h>
#include <fmt/ranges.h>

#include <llvm/Config/llvm-config.h>
#include <llvm/IR/Attributes.h>
Expand Down
Loading

0 comments on commit 6c0a9c8

Please sign in to comment.