Skip to content

Commit

Permalink
Merge pull request #442 from bluescarni/pr/cleanups
Browse files Browse the repository at this point in the history
llvm_state cleanups
  • Loading branch information
bluescarni authored Aug 4, 2024
2 parents 3df1333 + ee42757 commit f078fc3
Show file tree
Hide file tree
Showing 49 changed files with 1 addition and 413 deletions.
6 changes: 0 additions & 6 deletions benchmark/penc_comparison.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,6 @@ void run_benchmark(unsigned order)

builder.CreateRetVoid();

// Verify.
s.verify_function(f);

// Restore the original insertion block.
builder.SetInsertPoint(orig_bb);

Expand Down Expand Up @@ -130,9 +127,6 @@ void run_benchmark(unsigned order)

builder.CreateRetVoid();

// Verify.
s.verify_function(f);

// Compile.
s.compile();

Expand Down
3 changes: 0 additions & 3 deletions include/heyoka/detail/taylor_common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,6 @@ llvm::Function *taylor_c_diff_func_numpar(llvm_state &s, llvm::Type *fp_t, std::
// Return the result.
builder.CreateRet(builder.CreateLoad(val_t, retval));

// Verify.
s.verify_function(f);

// Restore the original insertion block.
builder.SetInsertPoint(orig_bb);
}
Expand Down
4 changes: 0 additions & 4 deletions include/heyoka/llvm_state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -315,12 +315,8 @@ class HEYOKA_DLL_PUBLIC llvm_state

[[nodiscard]] std::string get_ir() const;
[[nodiscard]] std::string get_bc() const;
void dump_object_code(const std::string &) const;
[[nodiscard]] const std::string &get_object_code() const;

void verify_function(const std::string &);
void verify_function(llvm::Function *);

[[nodiscard]] bool is_compiled() const;

void compile();
Expand Down
6 changes: 0 additions & 6 deletions src/continuous_output.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -294,9 +294,6 @@ void continuous_output<T>::add_c_out_function(std::uint32_t order, std::uint32_t
// Create the return value.
builder.CreateRetVoid();

// Verify the function.
m_llvm_state.verify_function(f);

// Compile.
m_llvm_state.compile();

Expand Down Expand Up @@ -981,9 +978,6 @@ void continuous_output_batch<T>::add_c_out_function(std::uint32_t order, std::ui
// Create the return value.
builder.CreateRetVoid();

// Verify the function.
m_llvm_state.verify_function(f);

// Compile.
m_llvm_state.compile();

Expand Down
9 changes: 0 additions & 9 deletions src/detail/div.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -273,9 +273,6 @@ llvm::Function *taylor_c_diff_func_div_impl(llvm_state &s, llvm::Type *fp_t, con
// Create the return value.
builder.CreateRet(llvm_fdiv(s, ret, taylor_c_diff_numparam_codegen(s, fp_t, n, num, par_ptr, batch_size)));

// Verify.
s.verify_function(f);

// Restore the original insertion block.
builder.SetInsertPoint(orig_bb);
}
Expand Down Expand Up @@ -367,9 +364,6 @@ llvm::Function *taylor_c_diff_func_div_impl(llvm_state &s, llvm::Type *fp_t, con
// Return the result.
builder.CreateRet(builder.CreateLoad(val_t, retval));

// Verify.
s.verify_function(f);

// Restore the original insertion block.
builder.SetInsertPoint(orig_bb);
}
Expand Down Expand Up @@ -436,9 +430,6 @@ llvm::Function *taylor_c_diff_func_div_impl(llvm_state &s, llvm::Type *fp_t, con
builder.CreateRet(
llvm_fdiv(s, ret, taylor_c_load_diff(s, val_t, diff_ptr, n_uvars, builder.getInt32(0), var_idx1)));

// Verify.
s.verify_function(f);

// Restore the original insertion block.
builder.SetInsertPoint(orig_bb);
}
Expand Down
9 changes: 0 additions & 9 deletions src/detail/event_detection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -440,9 +440,6 @@ llvm::Function *add_poly_translator_1(llvm_state &s, llvm::Type *fp_t, std::uint
// Create the return value.
builder.CreateRetVoid();

// Verify the function.
s.verify_function(f);

// Restore the original insertion block.
builder.SetInsertPoint(orig_bb);

Expand Down Expand Up @@ -633,9 +630,6 @@ llvm::Function *llvm_add_poly_rtscc(llvm_state &s, llvm::Type *fp_t, std::uint32
// Return.
builder.CreateRetVoid();

// Verify.
s.verify_function(f);

// Restore the original insertion block.
builder.SetInsertPoint(orig_bb);

Expand Down Expand Up @@ -758,9 +752,6 @@ llvm::Function *llvm_add_fex_check(llvm_state &s, llvm::Type *fp_t, std::uint32_
// Return.
builder.CreateRetVoid();

// Verify.
s.verify_function(f);

// Restore the original insertion block.
builder.SetInsertPoint(orig_bb);

Expand Down
3 changes: 0 additions & 3 deletions src/detail/llvm_helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2495,9 +2495,6 @@ llvm::Function *llvm_add_csc(llvm_state &s, llvm::Type *scal_t, std::uint32_t n,
// Return.
builder.CreateRetVoid();

// Verify.
s.verify_function(f);

// Restore the original insertion block.
builder.SetInsertPoint(orig_bb);
}
Expand Down
12 changes: 0 additions & 12 deletions src/detail/llvm_helpers_celmec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -602,9 +602,6 @@ llvm::Function *llvm_add_inv_kep_E(llvm_state &s, llvm::Type *fp_t, std::uint32_
// Return the result.
builder.CreateRet(builder.CreateLoad(tp, retval));

// Verify.
s.verify_function(f);

// Restore the original insertion block.
builder.SetInsertPoint(orig_bb);

Expand Down Expand Up @@ -678,9 +675,6 @@ void llvm_add_inv_kep_E_wrapper(llvm_state &s, llvm::Type *scal_t, std::uint32_t
// Return.
builder.CreateRetVoid();

// Verify.
s.verify_function(f);

// Restore the original insertion block.
builder.SetInsertPoint(orig_bb);
}
Expand Down Expand Up @@ -996,9 +990,6 @@ llvm::Function *llvm_add_inv_kep_F(llvm_state &s, llvm::Type *fp_t, std::uint32_
// Return the result.
builder.CreateRet(ret);

// Verify.
s.verify_function(f);

// Restore the original insertion block.
builder.SetInsertPoint(orig_bb);

Expand Down Expand Up @@ -1307,9 +1298,6 @@ llvm::Function *llvm_add_inv_kep_DE(llvm_state &s, llvm::Type *fp_t, std::uint32
// Return the result.
builder.CreateRet(ret);

// Verify.
s.verify_function(f);

// Restore the original insertion block.
builder.SetInsertPoint(orig_bb);

Expand Down
10 changes: 0 additions & 10 deletions src/detail/real_helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -351,8 +351,6 @@ llvm::Function *real_nary_op(llvm_state &s, llvm::Type *fp_t, const std::string

builder.CreateRet(res);

s.verify_function(f);

builder.SetInsertPoint(orig_bb);
}

Expand Down Expand Up @@ -417,8 +415,6 @@ std::pair<llvm::Value *, llvm::Value *> llvm_real_sincos(llvm_state &s, llvm::Va

builder.CreateRet(res);

s.verify_function(f);

builder.SetInsertPoint(orig_bb);
}

Expand Down Expand Up @@ -478,8 +474,6 @@ llvm::Function *real_nary_cmp(llvm_state &s, llvm::Type *fp_t, const std::string
// Truncate the result to a boolean and return.
builder.CreateRet(builder.CreateTrunc(cmp_ret, builder.getInt1Ty()));

s.verify_function(f);

builder.SetInsertPoint(orig_bb);
}

Expand Down Expand Up @@ -686,8 +680,6 @@ llvm::Value *llvm_real_ui_to_fp(llvm_state &s, llvm::Value *n, llvm::Type *fp_t)

builder.CreateRet(res);

s.verify_function(f);

builder.SetInsertPoint(orig_bb);
}

Expand Down Expand Up @@ -753,8 +745,6 @@ llvm::Value *llvm_real_sgn(llvm_state &s, llvm::Value *x)

builder.CreateRet(ret);

s.verify_function(f);

builder.SetInsertPoint(orig_bb);
}

Expand Down
9 changes: 0 additions & 9 deletions src/detail/sub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -272,9 +272,6 @@ llvm::Function *taylor_c_diff_func_sub_impl(llvm_state &s, llvm::Type *fp_t, con
// Return the result.
builder.CreateRet(builder.CreateLoad(val_t, retval));

// Verify.
s.verify_function(f);

// Restore the original insertion block.
builder.SetInsertPoint(orig_bb);
}
Expand Down Expand Up @@ -345,9 +342,6 @@ llvm::Function *taylor_c_diff_func_sub_impl(llvm_state &s, llvm::Type *fp_t, con
// Return the result.
builder.CreateRet(builder.CreateLoad(val_t, retval));

// Verify.
s.verify_function(f);

// Restore the original insertion block.
builder.SetInsertPoint(orig_bb);
}
Expand Down Expand Up @@ -402,9 +396,6 @@ llvm::Function *taylor_c_diff_func_sub_impl(llvm_state &s, llvm::Type *fp_t, con
// Create the return value.
builder.CreateRet(llvm_fsub(s, v0, v1));

// Verify.
s.verify_function(f);

// Restore the original insertion block.
builder.SetInsertPoint(orig_bb);
}
Expand Down
3 changes: 0 additions & 3 deletions src/detail/sum_sq.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -507,9 +507,6 @@ llvm::Function *sum_sq_taylor_c_diff_func_impl(llvm_state &s, llvm::Type *fp_t,
// Create the return value.
builder.CreateRet(builder.CreateLoad(val_t, retval));

// Verify.
s.verify_function(f);

// Restore the original insertion block.
builder.SetInsertPoint(orig_bb);
}
Expand Down
3 changes: 0 additions & 3 deletions src/detail/tm_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -321,9 +321,6 @@ void add_tm_func_nc_mode(llvm_state &st, const std::vector<T> &state, const var_

// Create the return value.
builder.CreateRetVoid();

// Verify the function.
st.verify_function(f);
}

} // namespace
Expand Down
3 changes: 0 additions & 3 deletions src/expression_cfunc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1528,9 +1528,6 @@ auto add_cfunc_impl(llvm_state &s, const std::string &name, const F &fn, std::ui
// Finish off the function.
builder.CreateRetVoid();

// Verify it.
s.verify_function(f);

// Restore the original insertion block.
builder.SetInsertPoint(orig_bb);

Expand Down
3 changes: 0 additions & 3 deletions src/func.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -931,9 +931,6 @@ llvm::Function *llvm_c_eval_func_helper(const std::string &name,
// Return it.
builder.CreateRet(ret);

// Verify.
s.verify_function(f);

// Restore the original insertion block.
builder.SetInsertPoint(orig_bb);
}
Expand Down
52 changes: 0 additions & 52 deletions src/llvm_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#include <charconv>
#include <cstddef>
#include <cstdint>
#include <fstream>
#include <ios>
#include <limits>
#include <memory>
Expand Down Expand Up @@ -1251,40 +1250,6 @@ void llvm_state::check_compiled(const char *f) const
}
}

void llvm_state::verify_function(llvm::Function *f)
{
check_uncompiled(__func__);

if (f == nullptr) {
throw std::invalid_argument("Cannot verify a null function pointer");
}

std::string err_report;
llvm::raw_string_ostream ostr(err_report);
if (llvm::verifyFunction(*f, &ostr)) {
// Remove function before throwing.
const auto fname = std::string(f->getName());
f->eraseFromParent();

throw std::invalid_argument(fmt::format(
"The verification of the function '{}' failed. The full error message:\n{}", fname, ostr.str()));
}
}

void llvm_state::verify_function(const std::string &name)
{
check_uncompiled(__func__);

// Lookup the function in the module.
auto *f = m_module->getFunction(name);
if (f == nullptr) {
throw std::invalid_argument(fmt::format("The function '{}' does not exist in the module", name));
}

// Run the actual check.
verify_function(f);
}

void llvm_state::optimise()
{
// NOTE: we used to fetch the target triple from the lljit object,
Expand Down Expand Up @@ -1519,23 +1484,6 @@ std::string llvm_state::get_bc() const
}
}

// LCOV_EXCL_START

void llvm_state::dump_object_code(const std::string &filename) const
{
const auto &oc = get_object_code();

std::ofstream ofs;
// NOTE: turn on exceptions, and overwrite any existing content.
ofs.exceptions(std::ifstream::failbit | std::ifstream::badbit);
ofs.open(filename, std::ios_base::out | std::ios::trunc);

// Write out the binary data to ofs.
ofs.write(oc.data(), boost::numeric_cast<std::streamsize>(oc.size()));
}

// LCOV_EXCL_STOP

const std::string &llvm_state::get_object_code() const
{
if (!is_compiled()) {
Expand Down
3 changes: 0 additions & 3 deletions src/math/acos.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -355,9 +355,6 @@ llvm::Function *taylor_c_diff_func_acos_impl(llvm_state &s, llvm::Type *fp_t, co
// Return the result.
builder.CreateRet(builder.CreateLoad(val_t, retval));

// Verify.
s.verify_function(f);

// Restore the original insertion block.
builder.SetInsertPoint(orig_bb);
}
Expand Down
3 changes: 0 additions & 3 deletions src/math/acosh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -346,9 +346,6 @@ llvm::Function *taylor_c_diff_func_acosh_impl(llvm_state &s, llvm::Type *fp_t, c
// Return the result.
builder.CreateRet(builder.CreateLoad(val_t, retval));

// Verify.
s.verify_function(f);

// Restore the original insertion block.
builder.SetInsertPoint(orig_bb);
}
Expand Down
3 changes: 0 additions & 3 deletions src/math/asin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -353,9 +353,6 @@ llvm::Function *taylor_c_diff_func_asin_impl(llvm_state &s, llvm::Type *fp_t, co
// Return the result.
builder.CreateRet(builder.CreateLoad(val_t, retval));

// Verify.
s.verify_function(f);

// Restore the original insertion block.
builder.SetInsertPoint(orig_bb);
}
Expand Down
Loading

0 comments on commit f078fc3

Please sign in to comment.