Skip to content

Commit

Permalink
mca allocation (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilipDeegan authored Oct 7, 2024
1 parent e133333 commit 0a112d5
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 4 deletions.
8 changes: 4 additions & 4 deletions inc/mkn/kul/env.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ auto GET_AS(std::string const& s, T const& def) {
if (EXISTS(s.c_str())) {
T t;
std::stringstream ss(GET(s.c_str()));
if (ss.fail()) KTHROW(Exception);
if (ss.fail()) KEXCEPTION("mkn::kul::env::GET_AS failed");
ss >> t;
return t;
}
Expand All @@ -62,16 +62,16 @@ class Var {
public:
enum Mode { APPE = 0, PREP, REPL };

Var(const std::string _n, const std::string _v, const Mode _m) : n(_n), v(_v), m(_m) {}
Var(std::string const _n, std::string const _v, Mode const _m) : n(_n), v(_v), m(_m) {}
Var(Var const& e) : n(e.n), v(e.v), m(e.m) {}
char const* name() const { return n.c_str(); }
char const* value() const { return v.c_str(); }
Mode mode() const { return m; }
const std::string toString() const {
std::string const toString() const {
std::string var(value());
mkn::kul::String::REPLACE_ALL(var, EOL(), "");
mkn::kul::String::TRIM(var);
const std::string ev(env::GET(name()));
std::string const ev(env::GET(name()));
if (!ev.empty()) {
if (mode() == Mode::PREP)
var = var + mkn::kul::env::SEP() + ev;
Expand Down
74 changes: 74 additions & 0 deletions test/instrument/alloc.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#ifndef _MKN_TEST_INSTRUMENT_ALLOC_HPP_
#define _MKN_TEST_INSTRUMENT_ALLOC_HPP_

#include "mkn/kul/dbg.hpp"
#include "mkn/kul/vector.hpp"

#include <array>
#include <cstddef>
#include <algorithm>

template <std::size_t size = 8>
struct S {
S() {
for (std::size_t i = 0; i < size; i++) vars[i] = i;
}

bool operator==(S const& that) const {
return std::equal(vars.begin(), vars.end(), that.vars.begin());
}

bool operator!=(S const& that) const { return !(*this == that); }
auto& operator[](std::uint8_t const i) const { return vars[i]; }
std::array<std::size_t, size> vars;
};

template <typename V>
auto copy_construct(V const& v) {
KUL_DBG_FUNC_ENTER;
V out{v};
return out;
}

template <typename V>
auto copy_operator_equal(V const& v) {
KUL_DBG_FUNC_ENTER;
V out;
out.reserve(v.capacity());
out = v;
return out;
}

template <typename V>
auto copy_operator_equal_super(V const& v) {
KUL_DBG_FUNC_ENTER;
V out;
out.reserve(v.capacity());
mkn::kul::as_super(out) = mkn::kul::as_super(v);
return out;
}

template <typename V>
auto copy_manual(V const& v) {
KUL_DBG_FUNC_ENTER;
V out;
out.reserve(v.capacity());
out.resize(v.size());
std::copy(v.begin(), v.end(), out.begin());
return out;
}

template <typename V>
auto make_vector(std::size_t const& size) {
KUL_DBG_FUNC_ENTER;
return V(size);
}
template <typename V0, typename V1>
auto make_vector_from(V1 const& v1) {
KUL_DBG_FUNC_ENTER;
V0 v(v1.size());
std::copy(v1.begin(), v1.end(), v.begin());
return v;
}

#endif /*_MKN_TEST_INSTRUMENT_ALLOC_HPP_*/
18 changes: 18 additions & 0 deletions test/instrument/non_constructing_vector.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include <stdexcept>
#include <string_view>
#include "test/instrument/alloc.hpp"

std::string_view constexpr RUN =
R"(
mkn -p base -M test/instrument/non_constructing_vector.cpp -Og 0 build -b . run -a -fPIC -x clang -m "clang.llvm-mca{link{bin: llvm-mca-19, out: non_vec.txt}}"
)";

template <typename T>
void go() {
constexpr static std::size_t N = 2000000;
auto const std_vec = make_vector<std::vector<T>>(N);
auto const std_vec2 = make_vector_from<mkn::kul::NonConstructingVector<T>>(std_vec);
if (std_vec2[0][0] != 0) throw std::runtime_error("do not optimize out");
}

int main() { go<S<8>>(); }
18 changes: 18 additions & 0 deletions test/instrument/std_vector.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include <stdexcept>
#include <string_view>
#include "test/instrument/alloc.hpp"

std::string_view constexpr RUN =
R"(
mkn -p base -M test/instrument/std_vector.cpp -Og 0 build -b . run -a -fPIC -x clang -m "clang.llvm-mca{link{bin: llvm-mca-19, out: std_vec.txt}}"
)";

template <typename T>
void go() {
constexpr static std::size_t N = 2000000;
auto const std_vec = make_vector<std::vector<T>>(N);
auto const std_vec2 = make_vector_from<std::vector<T>>(std_vec);
if (std_vec2[0][0] != 0) throw std::runtime_error("do not optimize out");
}

int main() { go<S<8>>(); }

0 comments on commit 0a112d5

Please sign in to comment.