2013-05-27 18:15:31 -05:00
|
|
|
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2013-08-22 22:58:42 -05:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2013-05-27 18:15:31 -05:00
|
|
|
#include "rustllvm.h"
|
|
|
|
|
2013-08-22 22:58:42 -05:00
|
|
|
#include "llvm/Support/CBindingWrapping.h"
|
|
|
|
#include "llvm/Target/TargetLibraryInfo.h"
|
|
|
|
#include "llvm/Transforms/IPO/PassManagerBuilder.h"
|
2013-05-27 18:15:31 -05:00
|
|
|
|
2013-08-22 22:58:42 -05:00
|
|
|
#include "llvm-c/Transforms/PassManagerBuilder.h"
|
2013-05-27 18:15:31 -05:00
|
|
|
|
2013-08-22 22:58:42 -05:00
|
|
|
using namespace llvm;
|
2013-05-27 18:15:31 -05:00
|
|
|
|
2013-08-22 22:58:42 -05:00
|
|
|
extern cl::opt<bool> EnableARMEHABI;
|
2013-05-27 18:15:31 -05:00
|
|
|
|
2013-08-22 22:58:42 -05:00
|
|
|
typedef struct LLVMOpaquePass *LLVMPassRef;
|
|
|
|
typedef struct LLVMOpaqueTargetMachine *LLVMTargetMachineRef;
|
|
|
|
|
|
|
|
DEFINE_STDCXX_CONVERSION_FUNCTIONS(Pass, LLVMPassRef)
|
|
|
|
DEFINE_STDCXX_CONVERSION_FUNCTIONS(TargetMachine, LLVMTargetMachineRef)
|
|
|
|
DEFINE_STDCXX_CONVERSION_FUNCTIONS(PassManagerBuilder, LLVMPassManagerBuilderRef)
|
2013-05-27 18:15:31 -05:00
|
|
|
|
2013-08-22 22:58:42 -05:00
|
|
|
extern "C" void
|
|
|
|
LLVMInitializePasses() {
|
2013-05-29 03:08:20 -05:00
|
|
|
PassRegistry &Registry = *PassRegistry::getPassRegistry();
|
|
|
|
initializeCore(Registry);
|
|
|
|
initializeCodeGen(Registry);
|
|
|
|
initializeScalarOpts(Registry);
|
|
|
|
initializeVectorization(Registry);
|
|
|
|
initializeIPO(Registry);
|
|
|
|
initializeAnalysis(Registry);
|
|
|
|
initializeIPA(Registry);
|
|
|
|
initializeTransformUtils(Registry);
|
|
|
|
initializeInstCombine(Registry);
|
|
|
|
initializeInstrumentation(Registry);
|
|
|
|
initializeTarget(Registry);
|
|
|
|
}
|
2013-05-27 18:15:31 -05:00
|
|
|
|
2013-08-22 22:58:42 -05:00
|
|
|
extern "C" bool
|
|
|
|
LLVMRustAddPass(LLVMPassManagerRef PM, const char *PassName) {
|
|
|
|
PassManagerBase *pm = unwrap(PM);
|
2013-05-27 18:15:31 -05:00
|
|
|
|
2013-05-29 03:08:20 -05:00
|
|
|
StringRef SR(PassName);
|
2013-08-22 22:58:42 -05:00
|
|
|
PassRegistry *PR = PassRegistry::getPassRegistry();
|
2013-05-27 18:15:31 -05:00
|
|
|
|
2013-08-22 22:58:42 -05:00
|
|
|
const PassInfo *PI = PR->getPassInfo(SR);
|
2013-05-29 03:08:20 -05:00
|
|
|
if (PI) {
|
2013-08-22 22:58:42 -05:00
|
|
|
pm->add(PI->createPass());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" LLVMTargetMachineRef
|
|
|
|
LLVMRustCreateTargetMachine(const char *triple,
|
|
|
|
const char *cpu,
|
|
|
|
const char *feature,
|
|
|
|
CodeModel::Model CM,
|
|
|
|
Reloc::Model RM,
|
|
|
|
CodeGenOpt::Level OptLevel,
|
2013-09-30 00:20:52 -05:00
|
|
|
bool EnableSegmentedStacks,
|
2014-01-28 13:59:28 -06:00
|
|
|
bool UseSoftFloat,
|
|
|
|
bool NoFramePointerElim) {
|
2013-08-22 22:58:42 -05:00
|
|
|
std::string Error;
|
|
|
|
Triple Trip(Triple::normalize(triple));
|
|
|
|
const llvm::Target *TheTarget = TargetRegistry::lookupTarget(Trip.getTriple(),
|
|
|
|
Error);
|
|
|
|
if (TheTarget == NULL) {
|
|
|
|
LLVMRustError = Error.c_str();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
TargetOptions Options;
|
2014-01-28 13:59:28 -06:00
|
|
|
Options.NoFramePointerElim = NoFramePointerElim;
|
2013-08-22 22:58:42 -05:00
|
|
|
Options.EnableSegmentedStacks = EnableSegmentedStacks;
|
2013-12-27 14:31:49 -06:00
|
|
|
Options.FloatABIType = FloatABI::Default;
|
2013-09-30 00:20:52 -05:00
|
|
|
Options.UseSoftFloat = UseSoftFloat;
|
|
|
|
if (UseSoftFloat) {
|
|
|
|
Options.FloatABIType = FloatABI::Soft;
|
|
|
|
}
|
2013-08-22 22:58:42 -05:00
|
|
|
|
|
|
|
TargetMachine *TM = TheTarget->createTargetMachine(Trip.getTriple(),
|
|
|
|
cpu,
|
|
|
|
feature,
|
|
|
|
Options,
|
|
|
|
RM,
|
|
|
|
CM,
|
|
|
|
OptLevel);
|
|
|
|
return wrap(TM);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" void
|
|
|
|
LLVMRustDisposeTargetMachine(LLVMTargetMachineRef TM) {
|
|
|
|
delete unwrap(TM);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unfortunately, LLVM doesn't expose a C API to add the corresponding analysis
|
|
|
|
// passes for a target to a pass manager. We export that functionality through
|
|
|
|
// this function.
|
|
|
|
extern "C" void
|
|
|
|
LLVMRustAddAnalysisPasses(LLVMTargetMachineRef TM,
|
|
|
|
LLVMPassManagerRef PMR,
|
|
|
|
LLVMModuleRef M) {
|
|
|
|
PassManagerBase *PM = unwrap(PMR);
|
|
|
|
PM->add(new DataLayout(unwrap(M)));
|
|
|
|
unwrap(TM)->addAnalysisPasses(*PM);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unfortunately, the LLVM C API doesn't provide a way to set the `LibraryInfo`
|
|
|
|
// field of a PassManagerBuilder, we expose our own method of doing so.
|
|
|
|
extern "C" void
|
|
|
|
LLVMRustAddBuilderLibraryInfo(LLVMPassManagerBuilderRef PMB, LLVMModuleRef M) {
|
|
|
|
Triple TargetTriple(unwrap(M)->getTargetTriple());
|
|
|
|
unwrap(PMB)->LibraryInfo = new TargetLibraryInfo(TargetTriple);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unfortunately, the LLVM C API doesn't provide a way to create the
|
|
|
|
// TargetLibraryInfo pass, so we use this method to do so.
|
|
|
|
extern "C" void
|
|
|
|
LLVMRustAddLibraryInfo(LLVMPassManagerRef PMB, LLVMModuleRef M) {
|
|
|
|
Triple TargetTriple(unwrap(M)->getTargetTriple());
|
|
|
|
unwrap(PMB)->add(new TargetLibraryInfo(TargetTriple));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unfortunately, the LLVM C API doesn't provide an easy way of iterating over
|
|
|
|
// all the functions in a module, so we do that manually here. You'll find
|
|
|
|
// similar code in clang's BackendUtil.cpp file.
|
|
|
|
extern "C" void
|
|
|
|
LLVMRustRunFunctionPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) {
|
|
|
|
FunctionPassManager *P = unwrap<FunctionPassManager>(PM);
|
|
|
|
P->doInitialization();
|
|
|
|
for (Module::iterator I = unwrap(M)->begin(),
|
|
|
|
E = unwrap(M)->end(); I != E; ++I)
|
|
|
|
if (!I->isDeclaration())
|
|
|
|
P->run(*I);
|
|
|
|
P->doFinalization();
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" void
|
2013-08-30 19:56:04 -05:00
|
|
|
LLVMRustSetLLVMOptions(int Argc, char **Argv) {
|
2013-08-22 22:58:42 -05:00
|
|
|
// Initializing the command-line options more than once is not allowed. So,
|
|
|
|
// check if they've already been initialized. (This could happen if we're
|
|
|
|
// being called from rustpkg, for example). If the arguments change, then
|
|
|
|
// that's just kinda unfortunate.
|
|
|
|
static bool initialized = false;
|
|
|
|
if (initialized) return;
|
|
|
|
initialized = true;
|
2013-08-30 19:56:04 -05:00
|
|
|
cl::ParseCommandLineOptions(Argc, Argv);
|
2013-08-22 22:58:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" bool
|
|
|
|
LLVMRustWriteOutputFile(LLVMTargetMachineRef Target,
|
|
|
|
LLVMPassManagerRef PMR,
|
|
|
|
LLVMModuleRef M,
|
|
|
|
const char *path,
|
|
|
|
TargetMachine::CodeGenFileType FileType) {
|
|
|
|
PassManager *PM = unwrap<PassManager>(PMR);
|
|
|
|
|
|
|
|
std::string ErrorInfo;
|
|
|
|
raw_fd_ostream OS(path, ErrorInfo, sys::fs::F_Binary);
|
|
|
|
if (ErrorInfo != "") {
|
|
|
|
LLVMRustError = ErrorInfo.c_str();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
formatted_raw_ostream FOS(OS);
|
|
|
|
|
|
|
|
unwrap(Target)->addPassesToEmitFile(*PM, FOS, FileType, false);
|
|
|
|
PM->run(*unwrap(M));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" void
|
|
|
|
LLVMRustPrintModule(LLVMPassManagerRef PMR,
|
|
|
|
LLVMModuleRef M,
|
|
|
|
const char* path) {
|
|
|
|
PassManager *PM = unwrap<PassManager>(PMR);
|
|
|
|
std::string ErrorInfo;
|
|
|
|
raw_fd_ostream OS(path, ErrorInfo, sys::fs::F_Binary);
|
|
|
|
formatted_raw_ostream FOS(OS);
|
2014-01-27 14:45:48 -06:00
|
|
|
PM->add(createPrintModulePass(FOS));
|
2013-08-22 22:58:42 -05:00
|
|
|
PM->run(*unwrap(M));
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" void
|
|
|
|
LLVMRustPrintPasses() {
|
|
|
|
LLVMInitializePasses();
|
|
|
|
struct MyListener : PassRegistrationListener {
|
|
|
|
void passEnumerate(const PassInfo *info) {
|
|
|
|
if (info->getPassArgument() && *info->getPassArgument()) {
|
|
|
|
printf("%15s - %s\n", info->getPassArgument(),
|
|
|
|
info->getPassName());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} listener;
|
|
|
|
|
|
|
|
PassRegistry *PR = PassRegistry::getPassRegistry();
|
|
|
|
PR->enumerateWith(&listener);
|
2013-05-27 18:15:31 -05:00
|
|
|
}
|
2013-06-19 17:18:25 -05:00
|
|
|
|
2013-08-22 22:58:42 -05:00
|
|
|
extern "C" void
|
|
|
|
LLVMRustAddAlwaysInlinePass(LLVMPassManagerBuilderRef PMB, bool AddLifetimes) {
|
|
|
|
unwrap(PMB)->Inliner = createAlwaysInlinerPass(AddLifetimes);
|
2013-06-19 17:18:25 -05:00
|
|
|
}
|
Implement LTO
This commit implements LTO for rust leveraging LLVM's passes. What this means
is:
* When compiling an rlib, in addition to insdering foo.o into the archive, also
insert foo.bc (the LLVM bytecode) of the optimized module.
* When the compiler detects the -Z lto option, it will attempt to perform LTO on
a staticlib or binary output. The compiler will emit an error if a dylib or
rlib output is being generated.
* The actual act of performing LTO is as follows:
1. Force all upstream libraries to have an rlib version available.
2. Load the bytecode of each upstream library from the rlib.
3. Link all this bytecode into the current LLVM module (just using llvm
apis)
4. Run an internalization pass which internalizes all symbols except those
found reachable for the local crate of compilation.
5. Run the LLVM LTO pass manager over this entire module
6a. If assembling an archive, then add all upstream rlibs into the output
archive. This ignores all of the object/bitcode/metadata files rust
generated and placed inside the rlibs.
6b. If linking a binary, create copies of all upstream rlibs, remove the
rust-generated object-file, and then link everything as usual.
As I have explained in #10741, this process is excruciatingly slow, so this is
*not* turned on by default, and it is also why I have decided to hide it behind
a -Z flag for now. The good news is that the binary sizes are about as small as
they can be as a result of LTO, so it's definitely working.
Closes #10741
Closes #10740
2013-12-03 01:19:29 -06:00
|
|
|
|
|
|
|
extern "C" void
|
|
|
|
LLVMRustRunRestrictionPass(LLVMModuleRef M, char **symbols, size_t len) {
|
|
|
|
PassManager passes;
|
|
|
|
ArrayRef<const char*> ref(symbols, len);
|
|
|
|
passes.add(llvm::createInternalizePass(ref));
|
|
|
|
passes.run(*unwrap(M));
|
|
|
|
}
|
2013-12-11 01:27:15 -06:00
|
|
|
|
|
|
|
extern "C" void
|
|
|
|
LLVMRustMarkAllFunctionsNounwind(LLVMModuleRef M) {
|
|
|
|
for (Module::iterator GV = unwrap(M)->begin(),
|
|
|
|
E = unwrap(M)->end(); GV != E; ++GV) {
|
|
|
|
GV->setDoesNotThrow();
|
|
|
|
Function *F = dyn_cast<Function>(GV);
|
|
|
|
if (F == NULL)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for (Function::iterator B = F->begin(), BE = F->end(); B != BE; ++B) {
|
|
|
|
for (BasicBlock::iterator I = B->begin(), IE = B->end();
|
|
|
|
I != IE; ++I) {
|
|
|
|
if (isa<InvokeInst>(I)) {
|
|
|
|
InvokeInst *CI = cast<InvokeInst>(I);
|
|
|
|
CI->setDoesNotThrow();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|