Skip to content

Commit

Permalink
[software] Fix clang warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
sant0s12 committed May 26, 2024
1 parent e28c55d commit 6a90772
Show file tree
Hide file tree
Showing 12 changed files with 159 additions and 134 deletions.
2 changes: 1 addition & 1 deletion software/runtime/kmp/barrier.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "barrier.hpp"

namespace kmp {
Barrier::Barrier(uint32_t numThreads) : numThreads(numThreads) {}
Barrier::Barrier(uint32_t numThreads) : barrier(0), numThreads(numThreads) {}

Barrier::~Barrier() { DEBUG_PRINT("Destroying barrier at %p\n", this); }

Expand Down
18 changes: 15 additions & 3 deletions software/runtime/kmp/cppsupport.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <cstdlib>
#include <mutex>

// Comment so that <mutex> is imported before
Expand Down Expand Up @@ -25,13 +26,24 @@ void *operator new[](size_t size) { return operator new(size); }
void operator delete[](void *ptr) noexcept { return operator delete(ptr); }

namespace std {
void __throw_bad_alloc() { printf("Bad alloc\n"); }
void __throw_length_error(const char *msg) { printf("Length error: %s\n", msg); }
void __throw_bad_alloc() {
printf("Bad alloc\n");
abort();
}

void __throw_length_error(const char *msg) {
printf("Length error: %s\n", msg);
abort();
}

void __throw_bad_optional_access() { printf("Bad optional access\n"); }
} // namespace std

extern "C" void abort() {
printf("Aborting\n");
while (true) {
mempool_wfi();
}
}

extern "C" int __cxa_atexit(void (*func)(void *), void *arg, void *dso_handle) {
Expand All @@ -45,5 +57,5 @@ extern "C" void __assert_func(const char *file, int line, const char *func,
const char *failedexpr) {
printf("Assertion failed: %s, file %s, line %d, function %s\n", failedexpr,
file, line, func);
mempool_wfi();
abort();
}
122 changes: 66 additions & 56 deletions software/runtime/kmp/entrypoints.cpp
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
#include "kmp/runtime.hpp"
#include "kmp/types.h"
#include "kmp/team.hpp"
#include "kmp/types.h"

using kmp::Mutex;

extern "C" {
#include "runtime.h"

// NOLINTBEGIN(bugprone-reserved-identifier)

void __kmpc_barrier(ident_t *loc, kmp_int32 global_tid) {
// DEBUG_PRINT("__kmpc_barrier called by %d\n", global_tid);
kmp::runtime::getCurrentThread(global_tid)
void __kmpc_barrier(ident_t * /*loc*/, kmp_int32 global_tid) {
kmp::runtime::getCurrentThread(static_cast<kmp_uint32>(global_tid))
.getCurrentTeam()
->getBarrier()
.wait();
};

// Parallel
void __kmpc_fork_call(ident_t *loc, kmp_int32 argc, kmpc_micro microtask, ...) {
// NOLINTBEGIN(cppcoreguidelines-pro-type-vararg,cppcoreguidelines-pro-bounds-array-to-pointer-decay)
void __kmpc_fork_call(ident_t * /*loc*/, kmp_int32 argc, kmpc_micro microtask,
...) {
// NOLINTBEGIN(cppcoreguidelines-pro-bounds-array-to-pointer-decay)
va_list args;
va_start(args, microtask);
kmp::Microtask kmpMicrotask(microtask, args, argc);
va_end(args);
// NOLINTEND(cppcoreguidelines-pro-type-vararg,cppcoreguidelines-pro-bounds-array-to-pointer-decay)
// NOLINTEND(cppcoreguidelines-pro-bounds-array-to-pointer-decay)

kmp::runtime::getCurrentThread().forkCall(std::move(kmpMicrotask));
};
Expand All @@ -47,22 +47,24 @@ void __kmpc_for_static_init_4u(ident_t *loc, kmp_int32 gtid,
pupper, pstride, incr, chunk);
};

void __kmpc_for_static_init_8(ident_t *loc, kmp_int32 gtid, kmp_int32 schedtype,
kmp_int64 *plastiter, kmp_int64 *plower,
kmp_int64 *pupper, kmp_int64 *pstride,
kmp_int64 incr, kmp_int64 chunk) {
void __kmpc_for_static_init_8(ident_t * /*loc*/, kmp_int32 /*gtid*/,
kmp_int32 /*schedtype*/,
kmp_int64 * /*plastiter*/, kmp_int64 * /*plower*/,
kmp_int64 * /*pupper*/, kmp_int64 * /*pstride*/,
kmp_int64 /*incr*/, kmp_int64 /*chunk*/) {
assert(false && "Unsupported loop index type");
};

void __kmpc_for_static_init_8u(ident_t *loc, kmp_int32 gtid,
kmp_int32 schedtype, kmp_uint64 *plastiter,
kmp_uint64 *plower, kmp_uint64 *pupper,
kmp_int64 *pstride, kmp_int64 incr,
kmp_int64 chunk) {
void __kmpc_for_static_init_8u(ident_t * /*loc*/, kmp_int32 /*gtid*/,
kmp_int32 /*schedtype*/,
kmp_uint64 * /*plastiter*/,
kmp_uint64 * /*plower*/, kmp_uint64 * /*pupper*/,
kmp_int64 * /*pstride*/, kmp_int64 /*incr*/,
kmp_int64 /*chunk*/) {
assert(false && "Unsupported loop index type");
};

void __kmpc_for_static_fini(ident_t *loc, kmp_int32 global_tid){};
void __kmpc_for_static_fini(ident_t * /*loc*/, kmp_int32 /*global_tid*/){};

// Dynamic loops
void __kmpc_dispatch_init_4(ident_t *loc, kmp_int32 gtid, kmp_int32 schedtype,
Expand All @@ -83,75 +85,84 @@ void __kmpc_dispatch_init_4u(ident_t *loc, kmp_int32 gtid, kmp_int32 schedtype,
upper, incr, chunk);
}

void __kmpc_dispatch_init_8(ident_t *loc, kmp_int64 gtid,
kmp_sched_type schedtype, kmp_int64 lower,
kmp_int64 upper, kmp_int64 incr, kmp_int64 chunk) {
void __kmpc_dispatch_init_8(ident_t * /*loc*/, kmp_int64 /*gtid*/,
kmp_sched_type /*schedtype*/, kmp_int64 /*lower*/,
kmp_int64 /*upper*/, kmp_int64 /*incr*/,
kmp_int64 /*chunk*/) {
assert(false && "Unsupported loop index type");
}

void __kmpc_dispatch_init_8u(ident_t *loc, kmp_int64 gtid,
kmp_sched_type schedtype, kmp_uint64 lower,
kmp_uint64 upper, kmp_int64 incr,
kmp_int64 chunk) {
void __kmpc_dispatch_init_8u(ident_t * /*loc*/, kmp_int64 /*gtid*/,
kmp_sched_type /*schedtype*/, kmp_uint64 /*lower*/,
kmp_uint64 /*upper*/, kmp_int64 /*incr*/,
kmp_int64 /*chunk*/) {
assert(false && "Unsupported loop index type");
}

int __kmpc_dispatch_next_4(ident_t *loc, kmp_int32 gtid, kmp_int32 *plastiter,
kmp_int32 *plower, kmp_int32 *pupper,
kmp_int32 *pstride) {
return kmp::runtime::getCurrentThread().getCurrentTeam()->dispatchNext(
loc, gtid, plastiter, plower, pupper, pstride);
return static_cast<int>(
kmp::runtime::getCurrentThread().getCurrentTeam()->dispatchNext(
loc, gtid, plastiter, plower, pupper, pstride));
}

int __kmpc_dispatch_next_4u(ident_t *loc, kmp_int32 gtid, kmp_int32 *plastiter,
kmp_uint32 *plower, kmp_uint32 *pupper,
kmp_int32 *pstride) {
return kmp::runtime::getCurrentThread().getCurrentTeam()->dispatchNext(
loc, gtid, plastiter, plower, pupper, pstride);
return static_cast<int>(
kmp::runtime::getCurrentThread().getCurrentTeam()->dispatchNext(
loc, gtid, plastiter, plower, pupper, pstride));
}

int __kmpc_dispatch_next_8(ident_t *loc, kmp_int64 gtid, kmp_int64 *plastiter,
kmp_int64 *plower, kmp_int64 *pupper,
kmp_int64 *pstride) {
int __kmpc_dispatch_next_8(ident_t * /*loc*/, kmp_int64 /*gtid*/,
kmp_int64 * /*plastiter*/, kmp_int64 * /*plower*/,
kmp_int64 * /*pupper*/, kmp_int64 * /*pstride*/) {
assert(false && "Unsupported loop index type");
}

int __kmpc_dispatch_next_8u(ident_t *loc, kmp_int64 gtid, kmp_int64 *plastiter,
kmp_uint64 *plower, kmp_uint64 *pupper,
kmp_int64 *pstride) {
int __kmpc_dispatch_next_8u(ident_t * /*loc*/, kmp_int64 /*gtid*/,
kmp_int64 * /*plastiter*/, kmp_uint64 * /*plower*/,
kmp_uint64 * /*pupper*/, kmp_int64 * /*pstride*/) {
assert(false && "Unsupported loop index type");
}

void __kmpc_push_num_threads(ident_t *loc, kmp_int32 global_tid,
void __kmpc_push_num_threads(ident_t * /*loc*/, kmp_int32 /*global_tid*/,
kmp_int32 num_threads) {
kmp::runtime::getCurrentThread().requestNumThreads(num_threads);
};

// Critical sections
void __kmpc_critical(ident_t *, kmp_int32 gtid, kmp_critical_name *crit) {
void __kmpc_critical(ident_t * /*unused*/, kmp_int32 /*gtid*/,
kmp_critical_name *crit) {
static_assert(sizeof(kmp::Mutex) <= sizeof(kmp_critical_name));

// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
kmp::Mutex *mutex = reinterpret_cast<kmp::Mutex *>(*crit);
mutex->lock();
};

void __kmpc_end_critical(ident_t *, kmp_int32 gtid, kmp_critical_name *crit) {
kmp::Mutex *mutex = reinterpret_cast<kmp::Mutex *>(*crit);
void __kmpc_end_critical(ident_t * /*unused*/, kmp_int32 /*gtid*/,
kmp_critical_name *crit) {

// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Mutex *mutex = reinterpret_cast<kmp::Mutex *>(*crit);
mutex->unlock();
};

// Master
kmp_int32 __kmpc_master(ident_t *loc, int32_t gtid) {
kmp_int32 __kmpc_master(ident_t * /*loc*/, kmp_int32 /*gtid*/) {
return static_cast<kmp_int32>(kmp::runtime::getCurrentThread().getTid() == 0);
};

void __kmpc_end_master(ident_t *loc, int32_t gtid){/* NOOP */};
void __kmpc_end_master(ident_t * /*loc*/, kmp_int32 /*gtid*/){/* NOOP */};

// Single (same as master for now)
kmp_int32 __kmpc_single(ident_t *loc, int32_t gtid) {
kmp_int32 __kmpc_single(ident_t * /*loc*/, kmp_int32 /*gtid*/) {
return static_cast<kmp_int32>(kmp::runtime::getCurrentThread().getTid() == 0);
};

void __kmpc_end_single(ident_t *loc, int32_t gtid){/* NOOP */};
void __kmpc_end_single(ident_t * /*loc*/, kmp_int32 /*gtid*/){/* NOOP */};

// Copyprivate
void __kmpc_copyprivate(ident_t *loc, kmp_int32 gtid, size_t cpy_size,
Expand All @@ -162,16 +173,17 @@ void __kmpc_copyprivate(ident_t *loc, kmp_int32 gtid, size_t cpy_size,
};

// Reduction
kmp_int32
__kmpc_reduce_nowait(ident_t *loc, kmp_int32 global_tid, kmp_int32 num_vars,
size_t reduce_size, void *reduce_data,
void (*reduce_func)(void *lhs_data, void *rhs_data),
kmp_critical_name *lck) {
kmp_int32 __kmpc_reduce_nowait(ident_t * /*loc*/, kmp_int32 /*global_tid*/,
kmp_int32 /*num_vars*/, size_t /*reduce_size*/,
void * /*reduce_data*/,
void (* /*reduce_func*/)(void *lhs_data,
void *rhs_data),
kmp_critical_name * /*lck*/) {
return 2; // Atomic reduction
}

void __kmpc_end_reduce_nowait(ident_t *loc, kmp_int32 global_tid,
kmp_critical_name *lck) {
void __kmpc_end_reduce_nowait(ident_t * /*loc*/, kmp_int32 /*global_tid*/,
kmp_critical_name * /*lck*/) {
/* NOOP */
}

Expand All @@ -185,13 +197,11 @@ kmp_int32 __kmpc_reduce(ident_t *loc, kmp_int32 global_tid, kmp_int32 num_vars,
}

void __kmpc_end_reduce(ident_t *loc, kmp_int32 global_tid,
kmp_critical_name *lck) {
kmp_critical_name * /*lck*/) {
return __kmpc_barrier(loc, global_tid);
}

kmp_int32 __kmpc_global_thread_num(ident_t *loc) {
return mempool_get_core_id();
kmp_int32 __kmpc_global_thread_num(ident_t * /*loc*/) {
return static_cast<kmp_int32>(mempool_get_core_id());
};

// NOLINTEND(bugprone-reserved-identifier)
}
15 changes: 9 additions & 6 deletions software/runtime/kmp/main_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ extern "C" {

// https://etherealwake.com/2021/09/crt-startup/
typedef void (*init_func)(void);
extern init_func __init_array_start[];
extern init_func __init_array_end[];
extern init_func *__init_array_start;
extern init_func *__init_array_end;

static inline void initGlobals() {
uint32_t n = __init_array_end - __init_array_start;
for (size_t i = 0; i < n; i++) {
//NOLINTNEXTLINE(*-narrowing-conversions)
int32_t len = __init_array_end - __init_array_start;
for (int32_t i = 0; i < len; i++) {

// NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
__init_array_start[i]();
}
}
Expand All @@ -23,7 +26,7 @@ std::atomic<bool> initLock = true;
extern "C" int __wrap_main() {
const mempool_id_t core_id = mempool_get_core_id();
if (core_id == 0) {
printf("Running OpenMP program on %d cores\n", mempool_get_core_count());
DEBUG_PRINT("Running OpenMP program on %d cores\n", mempool_get_core_count());

// Init heap allocators
mempool_init(0);
Expand All @@ -36,7 +39,7 @@ extern "C" int __wrap_main() {

initLock = false;

printf("Init done\n");
DEBUG_PRINT("Init done\n");

// Run the program
__real_main();
Expand Down
4 changes: 0 additions & 4 deletions software/runtime/kmp/runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@
#include <array>
#include <utility>

extern "C" {
#include "runtime.h"
}

namespace kmp {

namespace runtime {
Expand Down
14 changes: 6 additions & 8 deletions software/runtime/kmp/runtime.hpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
#pragma once

#include "etl/error_handler.h"
#include "kmp/thread.hpp"
#include "kmp/types.h"
#include "etl/error_handler.h"

extern "C" {
#include "runtime.h"
}

// NOLINTNEXTLINE(bugprone-reserved-identifier)
extern void __assert_func(const char *file, int line, const char *func,
const char *failedexpr);
static inline void assertWrapper(const etl::exception &e) {
__assert_func(e.file_name(), e.line_number(), "n/a", e.what());

static inline void assertWrapper(const etl::exception &exception) {
__assert_func(exception.file_name(), exception.line_number(), "n/a",
exception.what());
};

namespace kmp {
Expand All @@ -33,7 +31,7 @@ static inline void init() {

static inline void runThread(kmp_uint32 core_id) { threads[core_id].run(); };

static inline Thread &getCurrentThread(kmp_int32 gtid) {
static inline Thread &getCurrentThread(kmp_uint32 gtid) {
return threads[gtid];
};

Expand Down
6 changes: 2 additions & 4 deletions software/runtime/kmp/task.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Microtask::Microtask(kmpc_micro func, va_list args, kmp_int32 argc)
return;
}

this->args = new void *[argc];
this->args = new void *[static_cast<kmp_uint32>(argc)];

DEBUG_PRINT("Microtask constructor, args: %p\n", this->args);

Expand Down Expand Up @@ -49,9 +49,7 @@ Microtask &Microtask::operator=(Microtask &&other) noexcept {
return *this;
};

Microtask::~Microtask() {
delete[] args;
};
Microtask::~Microtask() { delete[] args; };

void Microtask::run() const {
kmp_int32 gtid = static_cast<kmp_int32>(mempool_get_core_id());
Expand Down
7 changes: 3 additions & 4 deletions software/runtime/kmp/team.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@

namespace kmp {

Team::Team(kmp_int32 masterGtid, kmp_uint32 numThreads,
Team::Team(kmp_uint32 masterGtid, kmp_uint32 numThreads,
std::optional<Task> implicitTask)
: numThreads(numThreads), barrier(numThreads),
dynamicSchedule{.valid = false}, implicitTask(std::move(implicitTask)),
copyPrivateData(nullptr), masterGtid(masterGtid) {}
: masterGtid(masterGtid), numThreads(numThreads), barrier(numThreads),
implicitTask(std::move(implicitTask)) {}

} // namespace kmp
Loading

0 comments on commit 6a90772

Please sign in to comment.