Skip to content

Commit f37fbc7

Browse files
z0w0brson
authored andcommitted
jit: Add passes and cleanup code
1 parent b9a3bf6 commit f37fbc7

File tree

4 files changed

+26
-44
lines changed

4 files changed

+26
-44
lines changed

src/rustc/back/link.rs

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -146,27 +146,14 @@ mod write {
146146
if opts.jit {
147147
// If we are using JIT, go ahead and create and
148148
// execute the engine now.
149-
150-
/*llvm::LLVMAddBasicAliasAnalysisPass(pm.llpm);
151-
llvm::LLVMAddInstructionCombiningPass(pm.llpm);
152-
llvm::LLVMAddReassociatePass(pm.llpm);
153-
llvm::LLVMAddGVNPass(pm.llpm);
154-
llvm::LLVMAddCFGSimplificationPass(pm.llpm);*/
155-
156149
// JIT execution takes ownership of the module,
157-
// so don't dispose and return. Due to a weird bug
158-
// with dynamic libraries, we need to separate jitting
159-
// into two functions and load crates inbetween.
160-
161-
if !llvm::LLVMRustPrepareJIT(pm.llpm,
162-
llmod,
163-
CodeGenOptLevel,
164-
true) {
165-
llvm_err(sess, ~"Could not JIT");
166-
}
150+
// so don't dispose and return.
167151

168152
// We need to tell LLVM where to resolve all linked
169153
// symbols from. The equivalent of -lstd, -lcore, etc.
154+
// By default the JIT will resolve symbols from the std and
155+
// core linked into rustc. We don't want that,
156+
// incase the user wants to use an older std library.
170157
/*let cstore = sess.cstore;
171158
for cstore::get_used_crate_files(cstore).each |cratepath| {
172159
debug!{"linking: %s", cratepath};
@@ -181,7 +168,10 @@ mod write {
181168
});
182169
}*/
183170

184-
if !llvm::LLVMRustExecuteJIT() {
171+
if !llvm::LLVMRustJIT(pm.llpm,
172+
llmod,
173+
CodeGenOptLevel,
174+
true) {
185175
llvm_err(sess, ~"Could not JIT");
186176
}
187177

src/rustc/lib/llvm.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -950,15 +950,12 @@ extern mod llvm {
950950
/** Load a shared library to resolve symbols against. */
951951
fn LLVMRustLoadLibrary(Filename: *c_char) -> bool;
952952

953-
/** Create the JIT engine. */
954-
fn LLVMRustPrepareJIT(PM: PassManagerRef,
953+
/** Create and execute the JIT engine. */
954+
fn LLVMRustJIT(PM: PassManagerRef,
955955
M: ModuleRef,
956956
OptLevel: c_int,
957957
EnableSegmentedStacks: bool) -> bool;
958958

959-
/** Execute the JIT engine. */
960-
fn LLVMRustExecuteJIT() -> bool;
961-
962959
/** Parses the bitcode in the given memory buffer. */
963960
fn LLVMRustParseBitcode(MemBuf: MemoryBufferRef) -> ModuleRef;
964961

src/rustllvm/RustWrapper.cpp

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
#include "llvm/LLVMContext.h"
1616
#include "llvm/Linker.h"
1717
#include "llvm/PassManager.h"
18+
#include "llvm/Analysis/Verifier.h"
19+
#include "llvm/Analysis/Passes.h"
20+
#include "llvm/Transforms/Scalar.h"
1821
#include "llvm/ADT/Triple.h"
1922
#include "llvm/Assembly/Parser.h"
2023
#include "llvm/Assembly/PrintModulePass.h"
@@ -35,7 +38,6 @@
3538
#include "llvm/ExecutionEngine/JITMemoryManager.h"
3639
#include "llvm/ExecutionEngine/MCJIT.h"
3740
#include "llvm/ExecutionEngine/Interpreter.h"
38-
#include "llvm/ExecutionEngine/GenericValue.h"
3941
#include "llvm-c/Core.h"
4042
#include "llvm-c/BitReader.h"
4143
#include "llvm-c/Object.h"
@@ -291,11 +293,8 @@ RustMCJITMemoryManager::~RustMCJITMemoryManager() {
291293
free(AllocatedDataMem[i].base());
292294
}
293295

294-
// Separated functions because loading libraries before creating
295-
// an execution engine seems to break stuff.
296-
297296
extern "C" bool
298-
LLVMRustPrepareJIT(LLVMPassManagerRef PMR,
297+
LLVMRustJIT(LLVMPassManagerRef PMR,
299298
LLVMModuleRef M,
300299
CodeGenOpt::Level OptLevel,
301300
bool EnableSegmentedStacks) {
@@ -309,8 +308,16 @@ LLVMRustPrepareJIT(LLVMPassManagerRef PMR,
309308
Options.JITEmitDebugInfo = true;
310309
Options.NoFramePointerElim = true;
311310
Options.EnableSegmentedStacks = EnableSegmentedStacks;
311+
PassManager *PM = unwrap<PassManager>(PMR);
312312

313-
unwrap<PassManager>(PMR)->run(*unwrap(M));
313+
PM->add(createBasicAliasAnalysisPass());
314+
PM->add(createInstructionCombiningPass());
315+
PM->add(createReassociatePass());
316+
PM->add(createGVNPass());
317+
PM->add(createPromoteMemoryToRegisterPass());
318+
PM->add(createCFGSimplificationPass());
319+
PM->add(createFunctionInliningPass());
320+
PM->run(*unwrap(M));
314321

315322
RustMCJITMemoryManager* MM = new RustMCJITMemoryManager();
316323
EE = EngineBuilder(unwrap(M))
@@ -326,29 +333,18 @@ LLVMRustPrepareJIT(LLVMPassManagerRef PMR,
326333
}
327334

328335
MM->invalidateInstructionCache();
329-
330-
return true;
331-
}
332-
333-
extern "C" bool
334-
LLVMRustExecuteJIT() {
335-
assert(EE);
336-
337-
std::string Err;
338336
Function* func = EE->FindFunctionNamed("main");
339337

340338
if(!func || Err != "") {
341339
LLVMRustError = Err.c_str();
342340
return false;
343341
}
344342

345-
//std::vector<GenericValue> args;
346-
typedef int (*entry_t)(int, int);
347-
entry_t entry = (entry_t) EE->getPointerToFunction(func);
343+
typedef int (*Entry)(int, int);
344+
Entry entry = (Entry) EE->getPointerToFunction(func);
348345

349346
assert(entry);
350347
entry(0, 0);
351-
//EE->runFunction(func, args);
352348

353349
return true;
354350
}

src/rustllvm/rustllvm.def.in

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ LLVMRustGetLastError
55
LLVMRustConstSmallInt
66
LLVMRustConstInt
77
LLVMRustLoadLibrary
8-
LLVMRustPrepareJIT
9-
LLVMRustExecuteJIT
8+
LLVMRustJIT
109
LLVMRustParseBitcode
1110
LLVMRustParseAssemblyFile
1211
LLVMRustPrintPassTimings

0 commit comments

Comments
 (0)