Skip to content

Commit

Permalink
fix linux ci
Browse files Browse the repository at this point in the history
  • Loading branch information
zhen8838 committed Nov 6, 2024
1 parent 3a0ab36 commit 02fdffa
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -241,12 +241,13 @@ EClass Ddfs(IEquality equival)
}

var root = Ddfs(equivalents);
#if DEBUG
using (var stream = Diagnostics.DumpScope.Current.OpenFile("egraph.dot"))
if (Diagnostics.DumpScope.Current.IsEnabled(Diagnostics.DumpFlags.EGraphCost))
{
EGraphPrinter.DumpEgraphAsDot(graph, stream);
using (var stream = Diagnostics.DumpScope.Current.OpenFile("egraph.dot"))
{
EGraphPrinter.DumpEgraphAsDot(graph, stream);
}
}
#endif

var constrains = new EGraphExtractConstrains[] { SingleNodeMemoryExtractConstrains };
var post = graph.Extract(root, CompileOptions, null, constrains);
Expand Down
7 changes: 5 additions & 2 deletions src/Native/include/nncase/io_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,14 @@ template <typename T, std::size_t Alignment> class aligned_allocator {
aligned_allocator(const aligned_allocator<U, Alignment> &) noexcept {}

T *allocate(std::size_t n) {
if (n > std::numeric_limits<std::size_t>::max() / sizeof(T))
size_t mask = alignment - 1;
size_t bytes = n * sizeof(T);
size_t aligned_bytes = bytes + (-bytes & mask);
if (aligned_bytes > std::numeric_limits<std::size_t>::max())
throw std::bad_alloc();

if (auto ptr =
static_cast<T *>(aligned_alloc(alignment, n * sizeof(T))))
static_cast<T *>(aligned_alloc(alignment, aligned_bytes)))
return ptr;

throw std::bad_alloc();
Expand Down
33 changes: 20 additions & 13 deletions src/Native/src/runtime/cpu/loaders/elf/elf_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@
#if defined(__linux__)
#include <chrono>
#include <dlfcn.h>
#include <fstream>
#include <fcntl.h>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <sys/mman.h>
#include <unistd.h>
#endif

using namespace nncase::runtime;
Expand All @@ -40,12 +41,18 @@ static void *alloccb(el_ctx *ctx, Elf_Addr phys, Elf_Addr virt, Elf_Addr size) {
return (void *)virt;
}

elf_loader::elf_loader() noexcept : buffer_(nullptr) { ctx_.pread = bpread; }
elf_loader::elf_loader() noexcept
: buffer_(nullptr), image_(nullptr), handle_(nullptr) {
ctx_.pread = bpread;
}

elf_loader::~elf_loader() {
if (buffer_) {
free(buffer_);
}
if (handle_) {
dlclose(handle_);
}
}

void elf_loader::load(std::span<const std::byte> elf) {
Expand All @@ -71,30 +78,30 @@ void elf_loader::load(std::span<const std::byte> elf) {
std::stringstream ss;
ss << std::put_time(std::localtime(&in_time_t), "%Y%m%d_%H%M%S");
std::string kernel_so = "/tmp/" + ss.str() + ".so";
std::ofstream outputFile(kernel_so, std::ios::out | std::ios::binary);
if (!outputFile) {
auto outputFile = open(kernel_so.c_str(), O_WRONLY | O_CREAT, 0666);
if (outputFile == -1) {
std::cerr << "cannot create file:" << kernel_so << std::endl;
throw std::runtime_error("cannot create file:" + kernel_so);
}

outputFile.write((char *)ctx_.elf, elf.size());
if (write(outputFile, (char *)ctx_.elf, elf.size()) == -1) {
throw std::runtime_error("write file:" + kernel_so);
}

if (!outputFile.good()) {
std::cerr << "error writing file" << std::endl;
outputFile.close();
throw std::runtime_error("error writing file");
if (close(outputFile) == -1) {
throw std::runtime_error("close file:" + kernel_so);
}

void *handle = dlopen(kernel_so.c_str(), RTLD_LAZY);
if (!handle) {
handle_ = dlopen(kernel_so.c_str(), RTLD_NOW);
if (!handle_) {
fprintf(stderr, "Error: %s\n", dlerror());
exit(EXIT_FAILURE);
}

entry_ = dlsym(handle, "kernel_entry");
entry_ = dlsym(handle_, "kernel_entry");
const char *dlsym_error = dlerror();
if (dlsym_error) {
dlclose(handle);
dlclose(handle_);
throw std::runtime_error("dlsym error:" + std::string(dlsym_error));
}
#endif
Expand Down
1 change: 1 addition & 0 deletions src/Native/src/runtime/cpu/loaders/elf/elf_loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class elf_loader {
private:
std::byte *buffer_;
std::byte *image_;
void *handle_;
el_ctx ctx_;
void *entry_;
};
Expand Down

0 comments on commit 02fdffa

Please sign in to comment.