forked from libxsmm/parlooper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjit_compile.cpp
64 lines (61 loc) · 2.08 KB
/
jit_compile.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/******************************************************************************
* Copyright (c) Intel Corporation - All rights reserved. *
* This file is part of the LIBXSMM library. *
* *
* For information on the license, see the LICENSE file. *
* Further information: https://github.com/libxsmm/libxsmm/ *
* SPDX-License-Identifier: BSD-3-Clause *
******************************************************************************/
#include "jit_compile.h"
#include <dlfcn.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string>
void* jit_compile_and_load(
const std::string filename,
const std::string flags) {
char libname[] = "/tmp/ppx_XXXXXX";
int fd = mkstemp(libname);
unlink(libname);
char fdname[50];
sprintf(fdname, "/proc/self/fd/%d", fd);
auto cmd = std::string("g++ -shared -fPIC -x c++ ") + flags;
cmd = cmd + " -o " + libname + " " + filename;
printf("JIT COMPILE: %s\n", cmd.c_str());
int ret = system(cmd.c_str());
if (ret != 0)
return NULL;
auto handle = dlopen(libname, RTLD_LAZY | RTLD_NODELETE);
if (!handle) {
fputs(dlerror(), stderr);
return NULL;
}
return handle;
}
void* jit_from_file(
const std::string filename,
const std::string flags,
const std::string func_name) {
void* handle = jit_compile_and_load(filename, flags);
if (handle == NULL)
return NULL;
void* func = dlsym(handle, func_name.c_str());
if (func == NULL) {
printf("Unable to find '%s' symbol in JIT COMPILE\n", func_name.c_str());
}
dlclose(handle);
return func;
}
void* jit_from_str(
const std::string src,
const std::string flags,
const std::string func_name) {
char filename[] = "/tmp/ppx_XXXXXX";
int fd = mkstemp(filename);
unlink(filename);
char fdname[50];
sprintf(fdname, "/proc/self/fd/%d", fd);
write(fd, src.c_str(), src.length());
return jit_from_file(fdname, flags, func_name);
}