Skip to content

Commit 71a8d31

Browse files
committed
Auto merge of #25627 - murarth:execution-engine-fix, r=nrc
* Removes `RustJITMemoryManager` from public API. This was really sort of an implementation detail to begin with. * `__morestack` is linked to C++ wrapper code and this pointer is used when resolving the symbol for `ExecutionEngine` code. * `__morestack_addr` is also resolved for `ExecutionEngine` code. This function is sometimes referenced in LLVM-generated code, but was not able to be resolved on Mac OS systems. * Added Windows support to `ExecutionEngine` API. * Added a test for basic `ExecutionEngine` functionality.
2 parents a35ea4d + 021e483 commit 71a8d31

File tree

5 files changed

+293
-30
lines changed

5 files changed

+293
-30
lines changed

Diff for: src/librustc_llvm/lib.rs

+1-7
Original file line numberDiff line numberDiff line change
@@ -478,9 +478,6 @@ pub type BuilderRef = *mut Builder_opaque;
478478
pub enum ExecutionEngine_opaque {}
479479
pub type ExecutionEngineRef = *mut ExecutionEngine_opaque;
480480
#[allow(missing_copy_implementations)]
481-
pub enum RustJITMemoryManager_opaque {}
482-
pub type RustJITMemoryManagerRef = *mut RustJITMemoryManager_opaque;
483-
#[allow(missing_copy_implementations)]
484481
pub enum MemoryBuffer_opaque {}
485482
pub type MemoryBufferRef = *mut MemoryBuffer_opaque;
486483
#[allow(missing_copy_implementations)]
@@ -1090,10 +1087,7 @@ extern {
10901087
pub fn LLVMDisposeBuilder(Builder: BuilderRef);
10911088

10921089
/* Execution engine */
1093-
pub fn LLVMRustCreateJITMemoryManager(morestack: *const ())
1094-
-> RustJITMemoryManagerRef;
1095-
pub fn LLVMBuildExecutionEngine(Mod: ModuleRef,
1096-
MM: RustJITMemoryManagerRef) -> ExecutionEngineRef;
1090+
pub fn LLVMBuildExecutionEngine(Mod: ModuleRef) -> ExecutionEngineRef;
10971091
pub fn LLVMDisposeExecutionEngine(EE: ExecutionEngineRef);
10981092
pub fn LLVMExecutionEngineFinalizeObject(EE: ExecutionEngineRef);
10991093
pub fn LLVMRustLoadDynamicLibrary(path: *const c_char) -> Bool;

Diff for: src/rustllvm/ExecutionEngineWrapper.cpp

+35-15
Original file line numberDiff line numberDiff line change
@@ -16,34 +16,38 @@ using namespace llvm;
1616
using namespace llvm::sys;
1717
using namespace llvm::object;
1818

19+
// libmorestack is not used on Windows
20+
#ifndef _WIN32
21+
extern "C" void __morestack(void);
22+
23+
static void* morestack_addr() {
24+
return reinterpret_cast<void*>(__morestack);
25+
}
26+
#endif
27+
1928
class RustJITMemoryManager : public SectionMemoryManager
2029
{
2130
typedef SectionMemoryManager Base;
2231

23-
const void *morestack;
24-
2532
public:
2633

27-
RustJITMemoryManager(const void *morestack_ptr)
28-
: morestack(morestack_ptr)
29-
{}
34+
RustJITMemoryManager() {}
3035

3136
uint64_t getSymbolAddress(const std::string &Name) override
3237
{
38+
#ifndef _WIN32
3339
if (Name == "__morestack" || Name == "___morestack")
34-
return reinterpret_cast<uint64_t>(morestack);
40+
return reinterpret_cast<uint64_t>(__morestack);
41+
if (Name == "__morestack_addr" || Name == "___morestack_addr")
42+
return reinterpret_cast<uint64_t>(morestack_addr);
43+
#endif
3544

3645
return Base::getSymbolAddress(Name);
3746
}
3847
};
3948

4049
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(RustJITMemoryManager, LLVMRustJITMemoryManagerRef)
4150

42-
extern "C" LLVMRustJITMemoryManagerRef LLVMRustCreateJITMemoryManager(void *morestack)
43-
{
44-
return wrap(new RustJITMemoryManager(morestack));
45-
}
46-
4751
extern "C" LLVMBool LLVMRustLoadDynamicLibrary(const char *path)
4852
{
4953
std::string err;
@@ -60,6 +64,13 @@ extern "C" LLVMBool LLVMRustLoadDynamicLibrary(const char *path)
6064
extern "C" void LLVMExecutionEngineAddModule(
6165
LLVMExecutionEngineRef eeref, LLVMModuleRef mref)
6266
{
67+
#ifdef _WIN32
68+
// On Windows, MCJIT must generate ELF objects
69+
std::string target = getProcessTriple();
70+
target += "-elf";
71+
target = Triple::normalize(target);
72+
unwrap(mref)->setTargetTriple(target);
73+
#endif
6374
LLVMAddModule(eeref, mref);
6475
}
6576

@@ -74,27 +85,36 @@ extern "C" LLVMBool LLVMExecutionEngineRemoveModule(
7485
return ee->removeModule(m);
7586
}
7687

77-
extern "C" LLVMExecutionEngineRef LLVMBuildExecutionEngine(
78-
LLVMModuleRef mod, LLVMRustJITMemoryManagerRef mref)
88+
extern "C" LLVMExecutionEngineRef LLVMBuildExecutionEngine(LLVMModuleRef mod)
7989
{
8090
// These are necessary for code generation to work properly.
8191
InitializeNativeTarget();
8292
InitializeNativeTargetAsmPrinter();
8393
InitializeNativeTargetAsmParser();
8494

95+
#ifdef _WIN32
96+
// On Windows, MCJIT must generate ELF objects
97+
std::string target = getProcessTriple();
98+
target += "-elf";
99+
target = Triple::normalize(target);
100+
unwrap(mod)->setTargetTriple(target);
101+
#endif
102+
85103
std::string error_str;
86104
TargetOptions options;
87105

88106
options.JITEmitDebugInfo = true;
89107
options.NoFramePointerElim = true;
90108

109+
RustJITMemoryManager *mm = new RustJITMemoryManager;
110+
91111
ExecutionEngine *ee =
92112
#if LLVM_VERSION_MINOR >= 6
93113
EngineBuilder(std::unique_ptr<Module>(unwrap(mod)))
94-
.setMCJITMemoryManager(std::unique_ptr<RustJITMemoryManager>(unwrap(mref)))
114+
.setMCJITMemoryManager(std::unique_ptr<RustJITMemoryManager>(mm))
95115
#else
96116
EngineBuilder(unwrap(mod))
97-
.setMCJITMemoryManager(unwrap(mref))
117+
.setMCJITMemoryManager(mm)
98118
#endif
99119
.setEngineKind(EngineKind::JIT)
100120
.setErrorStr(&error_str)

Diff for: src/rustllvm/rustllvm.h

-8
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,6 @@
5151
#include "llvm/IR/DIBuilder.h"
5252
#include "llvm/Linker/Linker.h"
5353

54-
// Used by RustMCJITMemoryManager::getPointerToNamedFunction()
55-
// to get around glibc issues. See the function for more information.
56-
#ifdef __linux__
57-
#include <sys/stat.h>
58-
#include <fcntl.h>
59-
#include <unistd.h>
60-
#endif
61-
6254
void LLVMRustSetLastError(const char*);
6355

6456
typedef struct OpaqueRustString *RustStringRef;

Diff for: src/test/run-make/execution-engine/Makefile

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
-include ../tools.mk
2+
3+
# This is a basic test of LLVM ExecutionEngine functionality using compiled
4+
# Rust code built using the `rustc` crate.
5+
6+
all:
7+
$(RUSTC) test.rs
8+
$(call RUN,test $(RUSTC))

0 commit comments

Comments
 (0)