Skip to content

Commit

Permalink
Testing bits for the code model option.
Browse files Browse the repository at this point in the history
  • Loading branch information
bluescarni committed Jul 30, 2024
1 parent 7caff09 commit 9c611e1
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 6 deletions.
5 changes: 4 additions & 1 deletion src/llvm_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -416,9 +416,12 @@ struct llvm_state::jit {

// Setup the code model.
switch (c_model) {
// NOTE: tiny code model not supported.
// LCOV_EXCL_START
case code_model::tiny:
jtmb->setCodeModel(llvm::CodeModel::Tiny);
break;
// LCOV_EXCL_STOP
case code_model::small:
jtmb->setCodeModel(llvm::CodeModel::Small);
break;
Expand All @@ -431,8 +434,8 @@ struct llvm_state::jit {
case code_model::large:
jtmb->setCodeModel(llvm::CodeModel::Large);
break;
default:
// LCOV_EXCL_START
default:
// NOTE: we should never end up here.
assert(false);
;
Expand Down
81 changes: 76 additions & 5 deletions test/llvm_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ TEST_CASE("copy semantics")

// Copy without compilation.
{
llvm_state s{kw::mname = "sample state", kw::opt_level = 2u, kw::fast_math = true, kw::slp_vectorize = true};
llvm_state s{kw::mname = "sample state", kw::opt_level = 2u, kw::fast_math = true, kw::slp_vectorize = true,
kw::code_model = code_model::medium};

detail::add_cfunc<double>(s, "cf", {x * y, y * x}, {x, y}, 1, false, false, false, 0, false);

Expand All @@ -159,6 +160,7 @@ TEST_CASE("copy semantics")
REQUIRE(s2.fast_math());
REQUIRE(!s2.is_compiled());
REQUIRE(s2.get_slp_vectorize());
REQUIRE(s2.get_code_model() == code_model::medium);

REQUIRE(s2.get_ir() == orig_ir);
REQUIRE(s2.get_bc() == orig_bc);
Expand All @@ -175,6 +177,15 @@ TEST_CASE("copy semantics")

REQUIRE(outputs[0] == 6);
REQUIRE(outputs[1] == 6);

llvm_state s3;
s3 = s2;
REQUIRE(s3.module_name() == "sample state");
REQUIRE(s3.get_opt_level() == 2u);
REQUIRE(s3.fast_math());
REQUIRE(!s3.is_compiled());
REQUIRE(s3.get_slp_vectorize());
REQUIRE(s3.get_code_model() == code_model::medium);
}
}

Expand Down Expand Up @@ -218,8 +229,8 @@ TEST_CASE("s11n")
oa << s;
}

s = llvm_state{kw::mname = "sample state", kw::opt_level = 2u, kw::fast_math = true, kw::force_avx512 = true,
kw::slp_vectorize = true};
s = llvm_state{kw::mname = "sample state", kw::opt_level = 2u, kw::fast_math = true,
kw::force_avx512 = true, kw::slp_vectorize = true, kw::code_model = code_model::medium};

{
boost::archive::binary_iarchive ia(ss);
Expand All @@ -235,6 +246,7 @@ TEST_CASE("s11n")
REQUIRE(s.fast_math() == false);
REQUIRE(s.force_avx512() == false);
REQUIRE(!s.get_slp_vectorize());
REQUIRE(s.get_code_model() == code_model::small);
}

// Compiled state.
Expand Down Expand Up @@ -289,8 +301,8 @@ TEST_CASE("make_similar")
{
auto [x, y] = make_vars("x", "y");

llvm_state s{kw::mname = "sample state", kw::opt_level = 2u, kw::fast_math = true, kw::force_avx512 = true,
kw::slp_vectorize = true};
llvm_state s{kw::mname = "sample state", kw::opt_level = 2u, kw::fast_math = true,
kw::force_avx512 = true, kw::slp_vectorize = true, kw::code_model = code_model::medium};

detail::add_cfunc<double>(s, "cf", {x * y, y * x}, {x, y}, 1, false, false, false, 0, false);

Expand All @@ -312,6 +324,7 @@ TEST_CASE("make_similar")
REQUIRE(!s2.is_compiled());
REQUIRE(s.get_ir() != s2.get_ir());
REQUIRE(s2.get_slp_vectorize());
REQUIRE(s2.get_code_model() == code_model::medium);
}

TEST_CASE("force_avx512")
Expand Down Expand Up @@ -416,3 +429,61 @@ TEST_CASE("existing trigger")
// Check that the second function was properly cleaned up.
REQUIRE(std::distance(s.module().begin(), s.module().end()) == 1);
}

TEST_CASE("code model")
{
using Catch::Matchers::Message;

{
llvm_state s;
REQUIRE(s.get_code_model() == code_model::small);

std::ostringstream oss;
oss << s.get_code_model();
REQUIRE(oss.str() == "small");
}

// NOTE: tiny code model not supported.
{
std::ostringstream oss;
oss << code_model::tiny;
REQUIRE(oss.str() == "tiny");
}

{
llvm_state s{kw::code_model = code_model::kernel};
REQUIRE(s.get_code_model() == code_model::kernel);

std::ostringstream oss;
oss << s.get_code_model();
REQUIRE(oss.str() == "kernel");
}

{
llvm_state s{kw::code_model = code_model::medium};
REQUIRE(s.get_code_model() == code_model::medium);

std::ostringstream oss;
oss << s.get_code_model();
REQUIRE(oss.str() == "medium");
}

{
llvm_state s{kw::code_model = code_model::large};
REQUIRE(s.get_code_model() == code_model::large);

std::ostringstream oss;
oss << s.get_code_model();
REQUIRE(oss.str() == "large");
}

{
std::ostringstream oss;
oss << code_model{100};
REQUIRE(oss.str() == "invalid");
}

REQUIRE_THROWS_MATCHES(
llvm_state{kw::code_model = code_model{100}}, std::invalid_argument,
Message("An invalid code model enumerator with a value of 100 was passed to the constructor of an llvm_state"));
}
19 changes: 19 additions & 0 deletions test/llvm_state_mem_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,25 @@ TEST_CASE("slp_vectorize test")
REQUIRE(llvm_state::get_memcache_size() > new_size);
}

// Same test for the slp_vectorize option.
TEST_CASE("code_model test")
{
llvm_state::clear_memcache();
llvm_state::set_memcache_limit(2048ull * 1024u * 1024u);

auto ta = taylor_adaptive<double>{model::pendulum(), {1., 0.}, kw::tol = 1e-11};
const auto size11 = llvm_state::get_memcache_size();

ta = taylor_adaptive<double>{model::pendulum(), {1., 0.}, kw::tol = 1e-11, kw::code_model = code_model::medium};
REQUIRE(llvm_state::get_memcache_size() > size11);

const auto new_size = llvm_state::get_memcache_size();

ta = taylor_adaptive<double>{
model::pendulum(), {1., 0.}, kw::tol = 1e-11, kw::code_model = code_model::medium, kw::force_avx512 = true};
REQUIRE(llvm_state::get_memcache_size() > new_size);
}

// Bug: in compact mode, global variables used to be created in random
// order, which would lead to logically-identical modules considered
// different by the cache machinery due to the different declaration order.
Expand Down

0 comments on commit 9c611e1

Please sign in to comment.