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"
|
2014-05-20 16:42:20 -05:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2015-03-09 19:46:45 -05:00
|
|
|
#include "llvm/Support/Host.h"
|
rustc: Update LLVM
This commit updates the LLVM submodule in use to the current HEAD of the LLVM
repository. This is primarily being done to start picking up unwinding support
for MSVC, which is currently unimplemented in the revision of LLVM we are using.
Along the way a few changes had to be made:
* As usual, lots of C++ debuginfo bindings in LLVM changed, so there were some
significant changes to our RustWrapper.cpp
* As usual, some pass management changed in LLVM, so clang was re-scrutinized to
ensure that we're doing the same thing as clang.
* Some optimization options are now passed directly into the
`PassManagerBuilder` instead of through CLI switches to LLVM.
* The `NoFramePointerElim` option was removed from LLVM, favoring instead the
`no-frame-pointer-elim` function attribute instead.
Additionally, LLVM has picked up some new optimizations which required fixing an
existing soundness hole in the IR we generate. It appears that the current LLVM
we use does not expose this hole. When an enum is moved, the previous slot in
memory is overwritten with a bit pattern corresponding to "dropped". When the
drop glue for this slot is run, however, the switch on the discriminant can
often start executing the `unreachable` block of the switch due to the
discriminant now being outside the normal range. This was patched over locally
for now by having the `unreachable` block just change to a `ret void`.
2015-05-14 14:10:43 -05:00
|
|
|
#include "llvm/Analysis/TargetLibraryInfo.h"
|
|
|
|
#include "llvm/Analysis/TargetTransformInfo.h"
|
2015-07-16 17:48:16 -05:00
|
|
|
#include "llvm/Target/TargetMachine.h"
|
|
|
|
#include "llvm/Target/TargetSubtargetInfo.h"
|
2013-08-22 22:58:42 -05:00
|
|
|
#include "llvm/Transforms/IPO/PassManagerBuilder.h"
|
2013-05-27 18:15:31 -05:00
|
|
|
|
rustc: Update LLVM
This commit updates the LLVM submodule in use to the current HEAD of the LLVM
repository. This is primarily being done to start picking up unwinding support
for MSVC, which is currently unimplemented in the revision of LLVM we are using.
Along the way a few changes had to be made:
* As usual, lots of C++ debuginfo bindings in LLVM changed, so there were some
significant changes to our RustWrapper.cpp
* As usual, some pass management changed in LLVM, so clang was re-scrutinized to
ensure that we're doing the same thing as clang.
* Some optimization options are now passed directly into the
`PassManagerBuilder` instead of through CLI switches to LLVM.
* The `NoFramePointerElim` option was removed from LLVM, favoring instead the
`no-frame-pointer-elim` function attribute instead.
Additionally, LLVM has picked up some new optimizations which required fixing an
existing soundness hole in the IR we generate. It appears that the current LLVM
we use does not expose this hole. When an enum is moved, the previous slot in
memory is overwritten with a bit pattern corresponding to "dropped". When the
drop glue for this slot is run, however, the switch on the discriminant can
often start executing the `unreachable` block of the switch due to the
discriminant now being outside the normal range. This was patched over locally
for now by having the `unreachable` block just change to a `ret void`.
2015-05-14 14:10:43 -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;
|
rustc: Update LLVM
This commit updates the LLVM submodule in use to the current HEAD of the LLVM
repository. This is primarily being done to start picking up unwinding support
for MSVC, which is currently unimplemented in the revision of LLVM we are using.
Along the way a few changes had to be made:
* As usual, lots of C++ debuginfo bindings in LLVM changed, so there were some
significant changes to our RustWrapper.cpp
* As usual, some pass management changed in LLVM, so clang was re-scrutinized to
ensure that we're doing the same thing as clang.
* Some optimization options are now passed directly into the
`PassManagerBuilder` instead of through CLI switches to LLVM.
* The `NoFramePointerElim` option was removed from LLVM, favoring instead the
`no-frame-pointer-elim` function attribute instead.
Additionally, LLVM has picked up some new optimizations which required fixing an
existing soundness hole in the IR we generate. It appears that the current LLVM
we use does not expose this hole. When an enum is moved, the previous slot in
memory is overwritten with a bit pattern corresponding to "dropped". When the
drop glue for this slot is run, however, the switch on the discriminant can
often start executing the `unreachable` block of the switch due to the
discriminant now being outside the normal range. This was patched over locally
for now by having the `unreachable` block just change to a `ret void`.
2015-05-14 14:10:43 -05:00
|
|
|
using namespace llvm::legacy;
|
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);
|
2016-06-09 08:41:17 -05:00
|
|
|
#if LLVM_VERSION_MINOR == 7
|
2013-05-29 03:08:20 -05:00
|
|
|
initializeIPA(Registry);
|
2015-10-24 04:42:23 -05:00
|
|
|
#endif
|
2013-05-29 03:08:20 -05:00
|
|
|
initializeTransformUtils(Registry);
|
|
|
|
initializeInstCombine(Registry);
|
|
|
|
initializeInstrumentation(Registry);
|
|
|
|
initializeTarget(Registry);
|
|
|
|
}
|
2013-05-27 18:15:31 -05:00
|
|
|
|
|
|
|
|
2016-01-24 19:22:24 -06:00
|
|
|
enum class SupportedPassKind {
|
|
|
|
Function,
|
|
|
|
Module,
|
|
|
|
Unsupported
|
|
|
|
};
|
|
|
|
|
|
|
|
extern "C" Pass*
|
|
|
|
LLVMRustFindAndCreatePass(const char *PassName) {
|
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) {
|
2016-01-24 19:22:24 -06:00
|
|
|
return PI->createPass();
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" SupportedPassKind
|
|
|
|
LLVMRustPassKind(Pass *pass) {
|
|
|
|
assert(pass);
|
|
|
|
PassKind passKind = pass->getPassKind();
|
|
|
|
if (passKind == PT_Module) {
|
|
|
|
return SupportedPassKind::Module;
|
|
|
|
} else if (passKind == PT_Function) {
|
|
|
|
return SupportedPassKind::Function;
|
|
|
|
} else {
|
|
|
|
return SupportedPassKind::Unsupported;
|
2013-08-22 22:58:42 -05:00
|
|
|
}
|
2016-01-24 19:22:24 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" void
|
|
|
|
LLVMRustAddPass(LLVMPassManagerRef PM, Pass *pass) {
|
|
|
|
assert(pass);
|
|
|
|
PassManagerBase *pm = unwrap(PM);
|
|
|
|
pm->add(pass);
|
2013-08-22 22:58:42 -05:00
|
|
|
}
|
|
|
|
|
2016-02-16 10:07:30 -06:00
|
|
|
#ifdef LLVM_COMPONENT_X86
|
|
|
|
#define SUBTARGET_X86 SUBTARGET(X86)
|
|
|
|
#else
|
|
|
|
#define SUBTARGET_X86
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef LLVM_COMPONENT_ARM
|
|
|
|
#define SUBTARGET_ARM SUBTARGET(ARM)
|
|
|
|
#else
|
|
|
|
#define SUBTARGET_ARM
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef LLVM_COMPONENT_AARCH64
|
|
|
|
#define SUBTARGET_AARCH64 SUBTARGET(AArch64)
|
|
|
|
#else
|
|
|
|
#define SUBTARGET_AARCH64
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef LLVM_COMPONENT_MIPS
|
|
|
|
#define SUBTARGET_MIPS SUBTARGET(Mips)
|
|
|
|
#else
|
|
|
|
#define SUBTARGET_MIPS
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef LLVM_COMPONENT_POWERPC
|
|
|
|
#define SUBTARGET_PPC SUBTARGET(PPC)
|
|
|
|
#else
|
|
|
|
#define SUBTARGET_PPC
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define GEN_SUBTARGETS \
|
|
|
|
SUBTARGET_X86 \
|
|
|
|
SUBTARGET_ARM \
|
|
|
|
SUBTARGET_AARCH64 \
|
|
|
|
SUBTARGET_MIPS \
|
|
|
|
SUBTARGET_PPC
|
|
|
|
|
|
|
|
#define SUBTARGET(x) namespace llvm { \
|
|
|
|
extern const SubtargetFeatureKV x##FeatureKV[]; \
|
|
|
|
extern const SubtargetFeatureKV x##SubTypeKV[]; \
|
|
|
|
}
|
|
|
|
|
|
|
|
GEN_SUBTARGETS
|
|
|
|
#undef SUBTARGET
|
|
|
|
|
|
|
|
extern "C" bool
|
|
|
|
LLVMRustHasFeature(LLVMTargetMachineRef TM,
|
|
|
|
const char *feature) {
|
|
|
|
TargetMachine *Target = unwrap(TM);
|
|
|
|
const MCSubtargetInfo *MCInfo = Target->getMCSubtargetInfo();
|
|
|
|
const FeatureBitset &Bits = MCInfo->getFeatureBits();
|
|
|
|
const llvm::SubtargetFeatureKV *FeatureEntry;
|
|
|
|
|
|
|
|
#define SUBTARGET(x) \
|
|
|
|
if (MCInfo->isCPUStringValid(x##SubTypeKV[0].Key)) { \
|
|
|
|
FeatureEntry = x##FeatureKV; \
|
|
|
|
} else
|
|
|
|
|
|
|
|
GEN_SUBTARGETS {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#undef SUBTARGET
|
|
|
|
|
|
|
|
while (strcmp(feature, FeatureEntry->Key) != 0)
|
|
|
|
FeatureEntry++;
|
|
|
|
|
|
|
|
return (Bits & FeatureEntry->Value) == FeatureEntry->Value;
|
|
|
|
}
|
|
|
|
|
2013-08-22 22:58:42 -05:00
|
|
|
extern "C" LLVMTargetMachineRef
|
|
|
|
LLVMRustCreateTargetMachine(const char *triple,
|
|
|
|
const char *cpu,
|
|
|
|
const char *feature,
|
|
|
|
CodeModel::Model CM,
|
|
|
|
Reloc::Model RM,
|
|
|
|
CodeGenOpt::Level OptLevel,
|
2014-01-28 13:59:28 -06:00
|
|
|
bool UseSoftFloat,
|
2014-08-09 11:43:45 -05:00
|
|
|
bool PositionIndependentExecutable,
|
rustc: Enable -f{function,data}-sections
The compiler has previously been producing binaries on the order of 1.8MB for
hello world programs "fn main() {}". This is largely a result of the compilation
model used by compiling entire libraries into a single object file and because
static linking is favored by default.
When linking, linkers will pull in the entire contents of an object file if any
symbol from the object file is used. This means that if any symbol from a rust
library is used, the entire library is pulled in unconditionally, regardless of
whether the library is used or not.
Traditional C/C++ projects do not normally encounter these large executable
problems because their archives (rust's rlibs) are composed of many objects.
Because of this, linkers can eliminate entire objects from being in the final
executable. With rustc, however, the linker does not have the opportunity to
leave out entire object files.
In order to get similar benefits from dead code stripping at link time, this
commit enables the -ffunction-sections and -fdata-sections flags in LLVM, as
well as passing --gc-sections to the linker *by default*. This means that each
function and each global will be placed into its own section, allowing the
linker to GC all unused functions and data symbols.
By enabling these flags, rust is able to generate much smaller binaries default.
On linux, a hello world binary went from 1.8MB to 597K (a 67% reduction in
size). The output size of dynamic libraries remained constant, but the output
size of rlibs increased, as seen below:
libarena - 2.27% bigger ( 292872 => 299508)
libcollections - 0.64% bigger ( 6765884 => 6809076)
libflate - 0.83% bigger ( 186516 => 188060)
libfourcc - 14.71% bigger ( 307290 => 352498)
libgetopts - 4.42% bigger ( 761468 => 795102)
libglob - 2.73% bigger ( 899932 => 924542)
libgreen - 9.63% bigger ( 1281718 => 1405124)
libhexfloat - 13.88% bigger ( 333738 => 380060)
liblibc - 10.79% bigger ( 551280 => 610736)
liblog - 10.93% bigger ( 218208 => 242060)
libnative - 8.26% bigger ( 1362096 => 1474658)
libnum - 2.34% bigger ( 2583400 => 2643916)
librand - 1.72% bigger ( 1608684 => 1636394)
libregex - 6.50% bigger ( 1747768 => 1861398)
librustc - 4.21% bigger (151820192 => 158218924)
librustdoc - 8.96% bigger ( 13142604 => 14320544)
librustuv - 4.13% bigger ( 4366896 => 4547304)
libsemver - 2.66% bigger ( 396166 => 406686)
libserialize - 1.91% bigger ( 6878396 => 7009822)
libstd - 3.59% bigger ( 39485286 => 40902218)
libsync - 3.95% bigger ( 1386390 => 1441204)
libsyntax - 4.96% bigger ( 35757202 => 37530798)
libterm - 13.99% bigger ( 924580 => 1053902)
libtest - 6.04% bigger ( 2455720 => 2604092)
libtime - 2.84% bigger ( 1075708 => 1106242)
liburl - 6.53% bigger ( 590458 => 629004)
libuuid - 4.63% bigger ( 326350 => 341466)
libworkcache - 8.45% bigger ( 1230702 => 1334750)
This increase in size is a result of encoding many more section names into each
object file (rlib). These increases are moderate enough that this change seems
worthwhile to me, due to the drastic improvements seen in the final artifacts.
The overall increase of the stage2 target folder (not the size of an install)
went from 337MB to 348MB (3% increase).
Additionally, linking is generally slower when executed with all these new
sections plus the --gc-sections flag. The stage0 compiler takes 1.4s to link the
`rustc` binary, where the stage1 compiler takes 1.9s to link the binary. Three
megabytes are shaved off the binary. I found this increase in link time to be
acceptable relative to the benefits of code size gained.
This commit only enables --gc-sections for *executables*, not dynamic libraries.
LLVM does all the heavy lifting when producing an object file for a dynamic
library, so there is little else for the linker to do (remember that we only
have one object file).
I conducted similar experiments by putting a *module's* functions and data
symbols into its own section (granularity moved to a module level instead of a
function/static level). The size benefits of a hello world were seen to be on
the order of 400K rather than 1.2MB. It seemed that enough benefit was gained
using ffunction-sections that this route was less desirable, despite the lesser
increases in binary rlib size.
2014-04-28 19:17:18 -05:00
|
|
|
bool FunctionSections,
|
|
|
|
bool DataSections) {
|
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) {
|
2014-04-15 09:25:22 -05:00
|
|
|
LLVMRustSetLastError(Error.c_str());
|
2013-08-22 22:58:42 -05:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-03-09 19:46:45 -05:00
|
|
|
StringRef real_cpu = cpu;
|
|
|
|
if (real_cpu == "native") {
|
|
|
|
real_cpu = sys::getHostCPUName();
|
|
|
|
}
|
|
|
|
|
2013-08-22 22:58:42 -05:00
|
|
|
TargetOptions Options;
|
2014-08-09 11:43:45 -05:00
|
|
|
Options.PositionIndependentExecutable = PositionIndependentExecutable;
|
2013-12-27 14:31:49 -06:00
|
|
|
Options.FloatABIType = FloatABI::Default;
|
2013-09-30 00:20:52 -05:00
|
|
|
if (UseSoftFloat) {
|
|
|
|
Options.FloatABIType = FloatABI::Soft;
|
|
|
|
}
|
rustc: Update LLVM
This commit updates the LLVM submodule in use to the current HEAD of the LLVM
repository. This is primarily being done to start picking up unwinding support
for MSVC, which is currently unimplemented in the revision of LLVM we are using.
Along the way a few changes had to be made:
* As usual, lots of C++ debuginfo bindings in LLVM changed, so there were some
significant changes to our RustWrapper.cpp
* As usual, some pass management changed in LLVM, so clang was re-scrutinized to
ensure that we're doing the same thing as clang.
* Some optimization options are now passed directly into the
`PassManagerBuilder` instead of through CLI switches to LLVM.
* The `NoFramePointerElim` option was removed from LLVM, favoring instead the
`no-frame-pointer-elim` function attribute instead.
Additionally, LLVM has picked up some new optimizations which required fixing an
existing soundness hole in the IR we generate. It appears that the current LLVM
we use does not expose this hole. When an enum is moved, the previous slot in
memory is overwritten with a bit pattern corresponding to "dropped". When the
drop glue for this slot is run, however, the switch on the discriminant can
often start executing the `unreachable` block of the switch due to the
discriminant now being outside the normal range. This was patched over locally
for now by having the `unreachable` block just change to a `ret void`.
2015-05-14 14:10:43 -05:00
|
|
|
Options.DataSections = DataSections;
|
|
|
|
Options.FunctionSections = FunctionSections;
|
2013-08-22 22:58:42 -05:00
|
|
|
|
|
|
|
TargetMachine *TM = TheTarget->createTargetMachine(Trip.getTriple(),
|
2015-03-09 19:46:45 -05:00
|
|
|
real_cpu,
|
2013-08-22 22:58:42 -05:00
|
|
|
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);
|
rustc: Update LLVM
This commit updates the LLVM submodule in use to the current HEAD of the LLVM
repository. This is primarily being done to start picking up unwinding support
for MSVC, which is currently unimplemented in the revision of LLVM we are using.
Along the way a few changes had to be made:
* As usual, lots of C++ debuginfo bindings in LLVM changed, so there were some
significant changes to our RustWrapper.cpp
* As usual, some pass management changed in LLVM, so clang was re-scrutinized to
ensure that we're doing the same thing as clang.
* Some optimization options are now passed directly into the
`PassManagerBuilder` instead of through CLI switches to LLVM.
* The `NoFramePointerElim` option was removed from LLVM, favoring instead the
`no-frame-pointer-elim` function attribute instead.
Additionally, LLVM has picked up some new optimizations which required fixing an
existing soundness hole in the IR we generate. It appears that the current LLVM
we use does not expose this hole. When an enum is moved, the previous slot in
memory is overwritten with a bit pattern corresponding to "dropped". When the
drop glue for this slot is run, however, the switch on the discriminant can
often start executing the `unreachable` block of the switch due to the
discriminant now being outside the normal range. This was patched over locally
for now by having the `unreachable` block just change to a `ret void`.
2015-05-14 14:10:43 -05:00
|
|
|
PM->add(createTargetTransformInfoWrapperPass(
|
|
|
|
unwrap(TM)->getTargetIRAnalysis()));
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" void
|
|
|
|
LLVMRustConfigurePassManagerBuilder(LLVMPassManagerBuilderRef PMB,
|
|
|
|
CodeGenOpt::Level OptLevel,
|
|
|
|
bool MergeFunctions,
|
|
|
|
bool SLPVectorize,
|
|
|
|
bool LoopVectorize) {
|
|
|
|
// Ignore mergefunc for now as enabling it causes crashes.
|
|
|
|
//unwrap(PMB)->MergeFunctions = MergeFunctions;
|
|
|
|
unwrap(PMB)->SLPVectorize = SLPVectorize;
|
|
|
|
unwrap(PMB)->OptLevel = OptLevel;
|
|
|
|
unwrap(PMB)->LoopVectorize = LoopVectorize;
|
2013-08-22 22:58:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2014-05-14 13:24:12 -05:00
|
|
|
LLVMRustAddBuilderLibraryInfo(LLVMPassManagerBuilderRef PMB,
|
|
|
|
LLVMModuleRef M,
|
|
|
|
bool DisableSimplifyLibCalls) {
|
2013-08-22 22:58:42 -05:00
|
|
|
Triple TargetTriple(unwrap(M)->getTargetTriple());
|
rustc: Update LLVM
This commit updates the LLVM submodule in use to the current HEAD of the LLVM
repository. This is primarily being done to start picking up unwinding support
for MSVC, which is currently unimplemented in the revision of LLVM we are using.
Along the way a few changes had to be made:
* As usual, lots of C++ debuginfo bindings in LLVM changed, so there were some
significant changes to our RustWrapper.cpp
* As usual, some pass management changed in LLVM, so clang was re-scrutinized to
ensure that we're doing the same thing as clang.
* Some optimization options are now passed directly into the
`PassManagerBuilder` instead of through CLI switches to LLVM.
* The `NoFramePointerElim` option was removed from LLVM, favoring instead the
`no-frame-pointer-elim` function attribute instead.
Additionally, LLVM has picked up some new optimizations which required fixing an
existing soundness hole in the IR we generate. It appears that the current LLVM
we use does not expose this hole. When an enum is moved, the previous slot in
memory is overwritten with a bit pattern corresponding to "dropped". When the
drop glue for this slot is run, however, the switch on the discriminant can
often start executing the `unreachable` block of the switch due to the
discriminant now being outside the normal range. This was patched over locally
for now by having the `unreachable` block just change to a `ret void`.
2015-05-14 14:10:43 -05:00
|
|
|
TargetLibraryInfoImpl *TLI = new TargetLibraryInfoImpl(TargetTriple);
|
2014-05-14 13:24:12 -05:00
|
|
|
if (DisableSimplifyLibCalls)
|
|
|
|
TLI->disableAllFunctions();
|
|
|
|
unwrap(PMB)->LibraryInfo = TLI;
|
2013-08-22 22:58:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2014-05-14 13:24:12 -05:00
|
|
|
LLVMRustAddLibraryInfo(LLVMPassManagerRef PMB,
|
|
|
|
LLVMModuleRef M,
|
|
|
|
bool DisableSimplifyLibCalls) {
|
2013-08-22 22:58:42 -05:00
|
|
|
Triple TargetTriple(unwrap(M)->getTargetTriple());
|
rustc: Update LLVM
This commit updates the LLVM submodule in use to the current HEAD of the LLVM
repository. This is primarily being done to start picking up unwinding support
for MSVC, which is currently unimplemented in the revision of LLVM we are using.
Along the way a few changes had to be made:
* As usual, lots of C++ debuginfo bindings in LLVM changed, so there were some
significant changes to our RustWrapper.cpp
* As usual, some pass management changed in LLVM, so clang was re-scrutinized to
ensure that we're doing the same thing as clang.
* Some optimization options are now passed directly into the
`PassManagerBuilder` instead of through CLI switches to LLVM.
* The `NoFramePointerElim` option was removed from LLVM, favoring instead the
`no-frame-pointer-elim` function attribute instead.
Additionally, LLVM has picked up some new optimizations which required fixing an
existing soundness hole in the IR we generate. It appears that the current LLVM
we use does not expose this hole. When an enum is moved, the previous slot in
memory is overwritten with a bit pattern corresponding to "dropped". When the
drop glue for this slot is run, however, the switch on the discriminant can
often start executing the `unreachable` block of the switch due to the
discriminant now being outside the normal range. This was patched over locally
for now by having the `unreachable` block just change to a `ret void`.
2015-05-14 14:10:43 -05:00
|
|
|
TargetLibraryInfoImpl TLII(TargetTriple);
|
|
|
|
if (DisableSimplifyLibCalls)
|
|
|
|
TLII.disableAllFunctions();
|
|
|
|
unwrap(PMB)->add(new TargetLibraryInfoWrapperPass(TLII));
|
2013-08-22 22:58:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
2016-07-12 17:42:20 -05:00
|
|
|
llvm::legacy::FunctionPassManager *P = unwrap<llvm::legacy::FunctionPassManager>(PM);
|
2013-08-22 22:58:42 -05:00
|
|
|
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) {
|
2016-07-12 17:42:20 -05:00
|
|
|
llvm::legacy::PassManager *PM = unwrap<llvm::legacy::PassManager>(PMR);
|
2013-08-22 22:58:42 -05:00
|
|
|
|
|
|
|
std::string ErrorInfo;
|
2014-09-30 16:20:22 -05:00
|
|
|
std::error_code EC;
|
|
|
|
raw_fd_ostream OS(path, EC, sys::fs::F_None);
|
|
|
|
if (EC)
|
|
|
|
ErrorInfo = EC.message();
|
2013-08-22 22:58:42 -05:00
|
|
|
if (ErrorInfo != "") {
|
2014-04-15 09:25:22 -05:00
|
|
|
LLVMRustSetLastError(ErrorInfo.c_str());
|
2013-08-22 22:58:42 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
rustc: Update LLVM
This commit updates the LLVM submodule in use to the current HEAD of the LLVM
repository. This is primarily being done to start picking up unwinding support
for MSVC, which is currently unimplemented in the revision of LLVM we are using.
Along the way a few changes had to be made:
* As usual, lots of C++ debuginfo bindings in LLVM changed, so there were some
significant changes to our RustWrapper.cpp
* As usual, some pass management changed in LLVM, so clang was re-scrutinized to
ensure that we're doing the same thing as clang.
* Some optimization options are now passed directly into the
`PassManagerBuilder` instead of through CLI switches to LLVM.
* The `NoFramePointerElim` option was removed from LLVM, favoring instead the
`no-frame-pointer-elim` function attribute instead.
Additionally, LLVM has picked up some new optimizations which required fixing an
existing soundness hole in the IR we generate. It appears that the current LLVM
we use does not expose this hole. When an enum is moved, the previous slot in
memory is overwritten with a bit pattern corresponding to "dropped". When the
drop glue for this slot is run, however, the switch on the discriminant can
often start executing the `unreachable` block of the switch due to the
discriminant now being outside the normal range. This was patched over locally
for now by having the `unreachable` block just change to a `ret void`.
2015-05-14 14:10:43 -05:00
|
|
|
unwrap(Target)->addPassesToEmitFile(*PM, OS, FileType, false);
|
2013-08-22 22:58:42 -05:00
|
|
|
PM->run(*unwrap(M));
|
rustc: Update LLVM
This commit updates the LLVM submodule in use to the current HEAD of the LLVM
repository. This is primarily being done to start picking up unwinding support
for MSVC, which is currently unimplemented in the revision of LLVM we are using.
Along the way a few changes had to be made:
* As usual, lots of C++ debuginfo bindings in LLVM changed, so there were some
significant changes to our RustWrapper.cpp
* As usual, some pass management changed in LLVM, so clang was re-scrutinized to
ensure that we're doing the same thing as clang.
* Some optimization options are now passed directly into the
`PassManagerBuilder` instead of through CLI switches to LLVM.
* The `NoFramePointerElim` option was removed from LLVM, favoring instead the
`no-frame-pointer-elim` function attribute instead.
Additionally, LLVM has picked up some new optimizations which required fixing an
existing soundness hole in the IR we generate. It appears that the current LLVM
we use does not expose this hole. When an enum is moved, the previous slot in
memory is overwritten with a bit pattern corresponding to "dropped". When the
drop glue for this slot is run, however, the switch on the discriminant can
often start executing the `unreachable` block of the switch due to the
discriminant now being outside the normal range. This was patched over locally
for now by having the `unreachable` block just change to a `ret void`.
2015-05-14 14:10:43 -05:00
|
|
|
|
2015-10-07 17:11:25 -05:00
|
|
|
// Apparently `addPassesToEmitFile` adds a pointer to our on-the-stack output
|
rustc: Update LLVM
This commit updates the LLVM submodule in use to the current HEAD of the LLVM
repository. This is primarily being done to start picking up unwinding support
for MSVC, which is currently unimplemented in the revision of LLVM we are using.
Along the way a few changes had to be made:
* As usual, lots of C++ debuginfo bindings in LLVM changed, so there were some
significant changes to our RustWrapper.cpp
* As usual, some pass management changed in LLVM, so clang was re-scrutinized to
ensure that we're doing the same thing as clang.
* Some optimization options are now passed directly into the
`PassManagerBuilder` instead of through CLI switches to LLVM.
* The `NoFramePointerElim` option was removed from LLVM, favoring instead the
`no-frame-pointer-elim` function attribute instead.
Additionally, LLVM has picked up some new optimizations which required fixing an
existing soundness hole in the IR we generate. It appears that the current LLVM
we use does not expose this hole. When an enum is moved, the previous slot in
memory is overwritten with a bit pattern corresponding to "dropped". When the
drop glue for this slot is run, however, the switch on the discriminant can
often start executing the `unreachable` block of the switch due to the
discriminant now being outside the normal range. This was patched over locally
for now by having the `unreachable` block just change to a `ret void`.
2015-05-14 14:10:43 -05:00
|
|
|
// stream (OS), so the only real safe place to delete this is here? Don't we
|
|
|
|
// wish this was written in Rust?
|
|
|
|
delete PM;
|
2013-08-22 22:58:42 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" void
|
|
|
|
LLVMRustPrintModule(LLVMPassManagerRef PMR,
|
|
|
|
LLVMModuleRef M,
|
|
|
|
const char* path) {
|
2016-07-12 17:42:20 -05:00
|
|
|
llvm::legacy::PassManager *PM = unwrap<llvm::legacy::PassManager>(PMR);
|
2013-08-22 22:58:42 -05:00
|
|
|
std::string ErrorInfo;
|
2014-02-26 16:06:27 -06:00
|
|
|
|
2014-09-30 16:20:22 -05:00
|
|
|
std::error_code EC;
|
|
|
|
raw_fd_ostream OS(path, EC, sys::fs::F_None);
|
|
|
|
if (EC)
|
|
|
|
ErrorInfo = EC.message();
|
2014-02-26 16:06:27 -06:00
|
|
|
|
2013-08-22 22:58:42 -05:00
|
|
|
formatted_raw_ostream FOS(OS);
|
2014-02-26 16:06:27 -06:00
|
|
|
|
2014-01-27 14:45:48 -06:00
|
|
|
PM->add(createPrintModulePass(FOS));
|
2014-02-26 16:06:27 -06:00
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-07-16 17:48:16 -05:00
|
|
|
|
|
|
|
extern "C" void
|
|
|
|
LLVMRustSetDataLayoutFromTargetMachine(LLVMModuleRef Module,
|
|
|
|
LLVMTargetMachineRef TMR) {
|
|
|
|
TargetMachine *Target = unwrap(TMR);
|
2015-10-13 01:11:59 -05:00
|
|
|
unwrap(Module)->setDataLayout(Target->createDataLayout());
|
2015-07-16 17:48:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" LLVMTargetDataRef
|
|
|
|
LLVMRustGetModuleDataLayout(LLVMModuleRef M) {
|
|
|
|
return wrap(&unwrap(M)->getDataLayout());
|
|
|
|
}
|