2013-05-27 23:15:31 +00: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-23 03:58:42 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2017-06-29 14:52:43 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2013-05-27 23:15:31 +00:00
|
|
|
#include "rustllvm.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 19:10:43 +00:00
|
|
|
#include "llvm/Analysis/TargetLibraryInfo.h"
|
|
|
|
#include "llvm/Analysis/TargetTransformInfo.h"
|
2016-08-03 19:37:57 +00:00
|
|
|
#include "llvm/IR/AutoUpgrade.h"
|
2017-06-29 14:52:43 +00:00
|
|
|
#include "llvm/IR/AssemblyAnnotationWriter.h"
|
2016-12-30 11:22:11 +00:00
|
|
|
#include "llvm/Support/CBindingWrapping.h"
|
|
|
|
#include "llvm/Support/FileSystem.h"
|
|
|
|
#include "llvm/Support/Host.h"
|
2015-07-16 22:48:16 +00:00
|
|
|
#include "llvm/Target/TargetMachine.h"
|
|
|
|
#include "llvm/Target/TargetSubtargetInfo.h"
|
2013-08-23 03:58:42 +00:00
|
|
|
#include "llvm/Transforms/IPO/PassManagerBuilder.h"
|
2013-05-27 23:15:31 +00:00
|
|
|
|
2016-09-24 16:37:04 +00:00
|
|
|
#if LLVM_VERSION_GE(4, 0)
|
|
|
|
#include "llvm/Transforms/IPO/AlwaysInliner.h"
|
|
|
|
#endif
|
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 19:10:43 +00:00
|
|
|
|
2013-08-23 03:58:42 +00:00
|
|
|
#include "llvm-c/Transforms/PassManagerBuilder.h"
|
2013-05-27 23:15:31 +00:00
|
|
|
|
2013-08-23 03:58:42 +00: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 19:10:43 +00:00
|
|
|
using namespace llvm::legacy;
|
2013-05-27 23:15:31 +00:00
|
|
|
|
2013-08-23 03:58:42 +00:00
|
|
|
extern cl::opt<bool> EnableARMEHABI;
|
2013-05-27 23:15:31 +00:00
|
|
|
|
2013-08-23 03:58:42 +00:00
|
|
|
typedef struct LLVMOpaquePass *LLVMPassRef;
|
|
|
|
typedef struct LLVMOpaqueTargetMachine *LLVMTargetMachineRef;
|
|
|
|
|
|
|
|
DEFINE_STDCXX_CONVERSION_FUNCTIONS(Pass, LLVMPassRef)
|
|
|
|
DEFINE_STDCXX_CONVERSION_FUNCTIONS(TargetMachine, LLVMTargetMachineRef)
|
2016-12-30 11:22:11 +00:00
|
|
|
DEFINE_STDCXX_CONVERSION_FUNCTIONS(PassManagerBuilder,
|
|
|
|
LLVMPassManagerBuilderRef)
|
2013-05-27 23:15:31 +00:00
|
|
|
|
2016-12-30 11:22:11 +00:00
|
|
|
extern "C" void LLVMInitializePasses() {
|
2013-05-29 08:08:20 +00:00
|
|
|
PassRegistry &Registry = *PassRegistry::getPassRegistry();
|
|
|
|
initializeCore(Registry);
|
|
|
|
initializeCodeGen(Registry);
|
|
|
|
initializeScalarOpts(Registry);
|
|
|
|
initializeVectorization(Registry);
|
|
|
|
initializeIPO(Registry);
|
|
|
|
initializeAnalysis(Registry);
|
2016-09-24 14:44:21 +00:00
|
|
|
#if LLVM_VERSION_EQ(3, 7)
|
2013-05-29 08:08:20 +00:00
|
|
|
initializeIPA(Registry);
|
2015-10-24 09:42:23 +00:00
|
|
|
#endif
|
2013-05-29 08:08:20 +00:00
|
|
|
initializeTransformUtils(Registry);
|
|
|
|
initializeInstCombine(Registry);
|
|
|
|
initializeInstrumentation(Registry);
|
|
|
|
initializeTarget(Registry);
|
|
|
|
}
|
2013-05-27 23:15:31 +00:00
|
|
|
|
2016-08-01 21:16:16 +00:00
|
|
|
enum class LLVMRustPassKind {
|
|
|
|
Other,
|
2016-01-25 01:22:24 +00:00
|
|
|
Function,
|
|
|
|
Module,
|
|
|
|
};
|
|
|
|
|
2016-12-31 17:01:23 +00:00
|
|
|
static LLVMRustPassKind toRust(PassKind Kind) {
|
|
|
|
switch (Kind) {
|
2016-08-01 21:16:16 +00:00
|
|
|
case PT_Function:
|
2016-12-30 11:22:11 +00:00
|
|
|
return LLVMRustPassKind::Function;
|
2016-08-01 21:16:16 +00:00
|
|
|
case PT_Module:
|
2016-12-30 11:22:11 +00:00
|
|
|
return LLVMRustPassKind::Module;
|
2016-08-01 21:16:16 +00:00
|
|
|
default:
|
2016-12-30 11:22:11 +00:00
|
|
|
return LLVMRustPassKind::Other;
|
2016-08-01 21:16:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-30 11:22:11 +00:00
|
|
|
extern "C" LLVMPassRef LLVMRustFindAndCreatePass(const char *PassName) {
|
|
|
|
StringRef SR(PassName);
|
|
|
|
PassRegistry *PR = PassRegistry::getPassRegistry();
|
2013-05-27 23:15:31 +00:00
|
|
|
|
2016-12-30 11:22:11 +00:00
|
|
|
const PassInfo *PI = PR->getPassInfo(SR);
|
|
|
|
if (PI) {
|
|
|
|
return wrap(PI->createPass());
|
|
|
|
}
|
2016-12-30 12:21:21 +00:00
|
|
|
return nullptr;
|
2016-01-25 01:22:24 +00:00
|
|
|
}
|
|
|
|
|
2016-12-31 17:01:23 +00:00
|
|
|
extern "C" LLVMRustPassKind LLVMRustPassKind(LLVMPassRef RustPass) {
|
|
|
|
assert(RustPass);
|
|
|
|
Pass *Pass = unwrap(RustPass);
|
|
|
|
return toRust(Pass->getPassKind());
|
2016-01-25 01:22:24 +00:00
|
|
|
}
|
|
|
|
|
2016-12-31 17:01:23 +00:00
|
|
|
extern "C" void LLVMRustAddPass(LLVMPassManagerRef PMR, LLVMPassRef RustPass) {
|
|
|
|
assert(RustPass);
|
|
|
|
Pass *Pass = unwrap(RustPass);
|
|
|
|
PassManagerBase *PMB = unwrap(PMR);
|
|
|
|
PMB->add(Pass);
|
2013-08-23 03:58:42 +00:00
|
|
|
}
|
|
|
|
|
2016-02-16 16:07:30 +00: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
|
|
|
|
|
2016-08-28 18:18:28 +00:00
|
|
|
#ifdef LLVM_COMPONENT_SYSTEMZ
|
|
|
|
#define SUBTARGET_SYSTEMZ SUBTARGET(SystemZ)
|
|
|
|
#else
|
|
|
|
#define SUBTARGET_SYSTEMZ
|
|
|
|
#endif
|
|
|
|
|
2016-11-09 21:56:10 +00:00
|
|
|
#ifdef LLVM_COMPONENT_MSP430
|
|
|
|
#define SUBTARGET_MSP430 SUBTARGET(MSP430)
|
|
|
|
#else
|
|
|
|
#define SUBTARGET_MSP430
|
|
|
|
#endif
|
|
|
|
|
2016-12-03 16:53:31 +00:00
|
|
|
#ifdef LLVM_COMPONENT_SPARC
|
|
|
|
#define SUBTARGET_SPARC SUBTARGET(Sparc)
|
|
|
|
#else
|
|
|
|
#define SUBTARGET_SPARC
|
|
|
|
#endif
|
|
|
|
|
2017-04-09 06:03:31 +00:00
|
|
|
#ifdef LLVM_COMPONENT_HEXAGON
|
|
|
|
#define SUBTARGET_HEXAGON SUBTARGET(Hexagon)
|
|
|
|
#else
|
|
|
|
#define SUBTARGET_HEXAGON
|
|
|
|
#endif
|
|
|
|
|
2016-12-30 11:22:11 +00:00
|
|
|
#define GEN_SUBTARGETS \
|
|
|
|
SUBTARGET_X86 \
|
|
|
|
SUBTARGET_ARM \
|
|
|
|
SUBTARGET_AARCH64 \
|
|
|
|
SUBTARGET_MIPS \
|
|
|
|
SUBTARGET_PPC \
|
|
|
|
SUBTARGET_SYSTEMZ \
|
2017-01-01 03:40:10 +00:00
|
|
|
SUBTARGET_MSP430 \
|
2017-04-09 06:03:31 +00:00
|
|
|
SUBTARGET_SPARC \
|
|
|
|
SUBTARGET_HEXAGON
|
2016-12-30 11:22:11 +00:00
|
|
|
|
|
|
|
#define SUBTARGET(x) \
|
|
|
|
namespace llvm { \
|
|
|
|
extern const SubtargetFeatureKV x##FeatureKV[]; \
|
|
|
|
extern const SubtargetFeatureKV x##SubTypeKV[]; \
|
2016-02-16 16:07:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GEN_SUBTARGETS
|
|
|
|
#undef SUBTARGET
|
|
|
|
|
2016-12-30 11:22:11 +00:00
|
|
|
extern "C" bool LLVMRustHasFeature(LLVMTargetMachineRef TM,
|
2016-12-31 17:01:23 +00:00
|
|
|
const char *Feature) {
|
2017-08-01 01:10:01 +00:00
|
|
|
#if LLVM_RUSTLLVM
|
2016-12-30 11:22:11 +00:00
|
|
|
TargetMachine *Target = unwrap(TM);
|
|
|
|
const MCSubtargetInfo *MCInfo = Target->getMCSubtargetInfo();
|
|
|
|
const FeatureBitset &Bits = MCInfo->getFeatureBits();
|
2017-07-28 02:03:23 +00:00
|
|
|
const ArrayRef<SubtargetFeatureKV> FeatTable = MCInfo->getFeatureTable();
|
2016-02-16 16:07:30 +00:00
|
|
|
|
2017-07-28 02:03:23 +00:00
|
|
|
for (auto &FeatureEntry : FeatTable)
|
|
|
|
if (!strcmp(FeatureEntry.Key, Feature))
|
|
|
|
return (Bits & FeatureEntry.Value) == FeatureEntry.Value;
|
|
|
|
#endif
|
|
|
|
return false;
|
2016-02-16 16:07:30 +00:00
|
|
|
}
|
|
|
|
|
2016-08-01 21:16:16 +00:00
|
|
|
enum class LLVMRustCodeModel {
|
2016-12-30 11:22:11 +00:00
|
|
|
Other,
|
|
|
|
Default,
|
|
|
|
JITDefault,
|
|
|
|
Small,
|
|
|
|
Kernel,
|
|
|
|
Medium,
|
|
|
|
Large,
|
2016-08-01 21:16:16 +00:00
|
|
|
};
|
|
|
|
|
2016-12-31 17:01:23 +00:00
|
|
|
static CodeModel::Model fromRust(LLVMRustCodeModel Model) {
|
|
|
|
switch (Model) {
|
2016-12-30 11:22:11 +00:00
|
|
|
case LLVMRustCodeModel::Default:
|
|
|
|
return CodeModel::Default;
|
|
|
|
case LLVMRustCodeModel::JITDefault:
|
|
|
|
return CodeModel::JITDefault;
|
|
|
|
case LLVMRustCodeModel::Small:
|
|
|
|
return CodeModel::Small;
|
|
|
|
case LLVMRustCodeModel::Kernel:
|
|
|
|
return CodeModel::Kernel;
|
|
|
|
case LLVMRustCodeModel::Medium:
|
|
|
|
return CodeModel::Medium;
|
|
|
|
case LLVMRustCodeModel::Large:
|
|
|
|
return CodeModel::Large;
|
|
|
|
default:
|
|
|
|
llvm_unreachable("Bad CodeModel.");
|
2016-08-01 21:16:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
enum class LLVMRustCodeGenOptLevel {
|
2016-12-30 11:22:11 +00:00
|
|
|
Other,
|
|
|
|
None,
|
|
|
|
Less,
|
|
|
|
Default,
|
|
|
|
Aggressive,
|
2016-08-01 21:16:16 +00:00
|
|
|
};
|
|
|
|
|
2016-12-31 17:01:23 +00:00
|
|
|
static CodeGenOpt::Level fromRust(LLVMRustCodeGenOptLevel Level) {
|
|
|
|
switch (Level) {
|
2016-12-30 11:22:11 +00:00
|
|
|
case LLVMRustCodeGenOptLevel::None:
|
|
|
|
return CodeGenOpt::None;
|
|
|
|
case LLVMRustCodeGenOptLevel::Less:
|
|
|
|
return CodeGenOpt::Less;
|
|
|
|
case LLVMRustCodeGenOptLevel::Default:
|
|
|
|
return CodeGenOpt::Default;
|
|
|
|
case LLVMRustCodeGenOptLevel::Aggressive:
|
|
|
|
return CodeGenOpt::Aggressive;
|
|
|
|
default:
|
|
|
|
llvm_unreachable("Bad CodeGenOptLevel.");
|
2016-08-01 21:16:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-28 22:21:59 +00:00
|
|
|
enum class LLVMRustRelocMode {
|
|
|
|
Default,
|
|
|
|
Static,
|
|
|
|
PIC,
|
|
|
|
DynamicNoPic,
|
|
|
|
ROPI,
|
|
|
|
RWPI,
|
|
|
|
ROPIRWPI,
|
|
|
|
};
|
|
|
|
|
|
|
|
#if LLVM_VERSION_LE(3, 8)
|
|
|
|
static Reloc::Model fromRust(LLVMRustRelocMode RustReloc) {
|
|
|
|
#else
|
|
|
|
static Optional<Reloc::Model> fromRust(LLVMRustRelocMode RustReloc) {
|
|
|
|
#endif
|
|
|
|
switch (RustReloc) {
|
|
|
|
case LLVMRustRelocMode::Default:
|
|
|
|
#if LLVM_VERSION_LE(3, 8)
|
|
|
|
return Reloc::Default;
|
|
|
|
#else
|
|
|
|
return None;
|
|
|
|
#endif
|
|
|
|
case LLVMRustRelocMode::Static:
|
|
|
|
return Reloc::Static;
|
|
|
|
case LLVMRustRelocMode::PIC:
|
|
|
|
return Reloc::PIC_;
|
|
|
|
case LLVMRustRelocMode::DynamicNoPic:
|
|
|
|
return Reloc::DynamicNoPIC;
|
|
|
|
#if LLVM_VERSION_GE(4, 0)
|
|
|
|
case LLVMRustRelocMode::ROPI:
|
|
|
|
return Reloc::ROPI;
|
|
|
|
case LLVMRustRelocMode::RWPI:
|
|
|
|
return Reloc::RWPI;
|
|
|
|
case LLVMRustRelocMode::ROPIRWPI:
|
|
|
|
return Reloc::ROPI_RWPI;
|
2017-08-08 08:17:33 +00:00
|
|
|
#else
|
2017-04-28 22:21:59 +00:00
|
|
|
default:
|
2017-08-08 08:17:33 +00:00
|
|
|
break;
|
|
|
|
#endif
|
2017-04-28 22:21:59 +00:00
|
|
|
}
|
2017-08-08 08:17:33 +00:00
|
|
|
llvm_unreachable("Bad RelocModel.");
|
2017-04-28 22:21:59 +00:00
|
|
|
}
|
|
|
|
|
2016-07-24 09:49:10 +00:00
|
|
|
#if LLVM_RUSTLLVM
|
2016-07-10 14:22:13 +00:00
|
|
|
/// getLongestEntryLength - Return the length of the longest entry in the table.
|
|
|
|
///
|
|
|
|
static size_t getLongestEntryLength(ArrayRef<SubtargetFeatureKV> Table) {
|
|
|
|
size_t MaxLen = 0;
|
|
|
|
for (auto &I : Table)
|
|
|
|
MaxLen = std::max(MaxLen, std::strlen(I.Key));
|
|
|
|
return MaxLen;
|
|
|
|
}
|
|
|
|
|
2016-12-30 11:22:11 +00:00
|
|
|
extern "C" void LLVMRustPrintTargetCPUs(LLVMTargetMachineRef TM) {
|
|
|
|
const TargetMachine *Target = unwrap(TM);
|
|
|
|
const MCSubtargetInfo *MCInfo = Target->getMCSubtargetInfo();
|
|
|
|
const ArrayRef<SubtargetFeatureKV> CPUTable = MCInfo->getCPUTable();
|
|
|
|
unsigned MaxCPULen = getLongestEntryLength(CPUTable);
|
|
|
|
|
|
|
|
printf("Available CPUs for this target:\n");
|
|
|
|
for (auto &CPU : CPUTable)
|
|
|
|
printf(" %-*s - %s.\n", MaxCPULen, CPU.Key, CPU.Desc);
|
|
|
|
printf("\n");
|
2016-07-10 14:22:13 +00:00
|
|
|
}
|
|
|
|
|
2016-12-30 11:22:11 +00:00
|
|
|
extern "C" void LLVMRustPrintTargetFeatures(LLVMTargetMachineRef TM) {
|
|
|
|
const TargetMachine *Target = unwrap(TM);
|
|
|
|
const MCSubtargetInfo *MCInfo = Target->getMCSubtargetInfo();
|
|
|
|
const ArrayRef<SubtargetFeatureKV> FeatTable = MCInfo->getFeatureTable();
|
|
|
|
unsigned MaxFeatLen = getLongestEntryLength(FeatTable);
|
|
|
|
|
|
|
|
printf("Available features for this target:\n");
|
|
|
|
for (auto &Feature : FeatTable)
|
|
|
|
printf(" %-*s - %s.\n", MaxFeatLen, Feature.Key, Feature.Desc);
|
|
|
|
printf("\n");
|
|
|
|
|
|
|
|
printf("Use +feature to enable a feature, or -feature to disable it.\n"
|
|
|
|
"For example, rustc -C -target-cpu=mycpu -C "
|
|
|
|
"target-feature=+feature1,-feature2\n\n");
|
2016-07-10 14:22:13 +00:00
|
|
|
}
|
|
|
|
|
2016-07-24 09:49:10 +00:00
|
|
|
#else
|
|
|
|
|
2016-12-30 11:22:11 +00:00
|
|
|
extern "C" void LLVMRustPrintTargetCPUs(LLVMTargetMachineRef) {
|
|
|
|
printf("Target CPU help is not supported by this LLVM version.\n\n");
|
2016-07-24 09:49:10 +00:00
|
|
|
}
|
|
|
|
|
2016-12-30 11:22:11 +00:00
|
|
|
extern "C" void LLVMRustPrintTargetFeatures(LLVMTargetMachineRef) {
|
|
|
|
printf("Target features help is not supported by this LLVM version.\n\n");
|
2016-07-24 09:49:10 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-12-30 11:22:11 +00:00
|
|
|
extern "C" LLVMTargetMachineRef LLVMRustCreateTargetMachine(
|
2016-12-31 17:01:23 +00:00
|
|
|
const char *TripleStr, const char *CPU, const char *Feature,
|
2017-04-28 22:21:59 +00:00
|
|
|
LLVMRustCodeModel RustCM, LLVMRustRelocMode RustReloc,
|
2016-12-31 17:01:23 +00:00
|
|
|
LLVMRustCodeGenOptLevel RustOptLevel, bool UseSoftFloat,
|
2016-12-30 11:22:11 +00:00
|
|
|
bool PositionIndependentExecutable, bool FunctionSections,
|
|
|
|
bool DataSections) {
|
2016-07-24 20:31:16 +00:00
|
|
|
|
2016-12-31 17:01:23 +00:00
|
|
|
auto CM = fromRust(RustCM);
|
|
|
|
auto OptLevel = fromRust(RustOptLevel);
|
2017-04-28 22:21:59 +00:00
|
|
|
auto RM = fromRust(RustReloc);
|
2016-07-24 20:31:16 +00:00
|
|
|
|
2016-12-30 11:22:11 +00:00
|
|
|
std::string Error;
|
2016-12-31 17:01:23 +00:00
|
|
|
Triple Trip(Triple::normalize(TripleStr));
|
2016-12-30 11:22:11 +00:00
|
|
|
const llvm::Target *TheTarget =
|
|
|
|
TargetRegistry::lookupTarget(Trip.getTriple(), Error);
|
2016-12-30 12:21:21 +00:00
|
|
|
if (TheTarget == nullptr) {
|
2016-12-30 11:22:11 +00:00
|
|
|
LLVMRustSetLastError(Error.c_str());
|
2016-12-30 12:21:21 +00:00
|
|
|
return nullptr;
|
2016-12-30 11:22:11 +00:00
|
|
|
}
|
2013-08-23 03:58:42 +00:00
|
|
|
|
2016-12-31 17:01:23 +00:00
|
|
|
StringRef RealCPU = CPU;
|
|
|
|
if (RealCPU == "native") {
|
|
|
|
RealCPU = sys::getHostCPUName();
|
2016-12-30 11:22:11 +00:00
|
|
|
}
|
2015-03-10 00:46:45 +00:00
|
|
|
|
2016-12-30 11:22:11 +00:00
|
|
|
TargetOptions Options;
|
2016-09-24 14:44:21 +00:00
|
|
|
#if LLVM_VERSION_LE(3, 8)
|
2016-12-30 11:22:11 +00:00
|
|
|
Options.PositionIndependentExecutable = PositionIndependentExecutable;
|
2016-07-12 22:41:40 +00:00
|
|
|
#endif
|
|
|
|
|
2016-12-30 11:22:11 +00:00
|
|
|
Options.FloatABIType = FloatABI::Default;
|
|
|
|
if (UseSoftFloat) {
|
|
|
|
Options.FloatABIType = FloatABI::Soft;
|
|
|
|
}
|
|
|
|
Options.DataSections = DataSections;
|
|
|
|
Options.FunctionSections = FunctionSections;
|
|
|
|
|
|
|
|
TargetMachine *TM = TheTarget->createTargetMachine(
|
2016-12-31 17:01:23 +00:00
|
|
|
Trip.getTriple(), RealCPU, Feature, Options, RM, CM, OptLevel);
|
2016-12-30 11:22:11 +00:00
|
|
|
return wrap(TM);
|
2013-08-23 03:58:42 +00:00
|
|
|
}
|
|
|
|
|
2016-12-30 11:22:11 +00:00
|
|
|
extern "C" void LLVMRustDisposeTargetMachine(LLVMTargetMachineRef TM) {
|
|
|
|
delete unwrap(TM);
|
2013-08-23 03:58:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2016-12-30 11:22:11 +00:00
|
|
|
extern "C" void LLVMRustAddAnalysisPasses(LLVMTargetMachineRef TM,
|
|
|
|
LLVMPassManagerRef PMR,
|
|
|
|
LLVMModuleRef M) {
|
|
|
|
PassManagerBase *PM = unwrap(PMR);
|
|
|
|
PM->add(
|
|
|
|
createTargetTransformInfoWrapperPass(unwrap(TM)->getTargetIRAnalysis()));
|
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 19:10:43 +00:00
|
|
|
}
|
|
|
|
|
2016-12-30 11:22:11 +00:00
|
|
|
extern "C" void LLVMRustConfigurePassManagerBuilder(
|
2016-12-31 17:01:23 +00:00
|
|
|
LLVMPassManagerBuilderRef PMBR, LLVMRustCodeGenOptLevel OptLevel,
|
2016-12-30 11:22:11 +00:00
|
|
|
bool MergeFunctions, bool SLPVectorize, bool LoopVectorize) {
|
|
|
|
// Ignore mergefunc for now as enabling it causes crashes.
|
2016-12-31 17:01:23 +00:00
|
|
|
// unwrap(PMBR)->MergeFunctions = MergeFunctions;
|
|
|
|
unwrap(PMBR)->SLPVectorize = SLPVectorize;
|
|
|
|
unwrap(PMBR)->OptLevel = fromRust(OptLevel);
|
|
|
|
unwrap(PMBR)->LoopVectorize = LoopVectorize;
|
2013-08-23 03:58:42 +00: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.
|
2016-12-31 17:01:23 +00:00
|
|
|
extern "C" void LLVMRustAddBuilderLibraryInfo(LLVMPassManagerBuilderRef PMBR,
|
2016-12-30 11:22:11 +00:00
|
|
|
LLVMModuleRef M,
|
|
|
|
bool DisableSimplifyLibCalls) {
|
|
|
|
Triple TargetTriple(unwrap(M)->getTargetTriple());
|
|
|
|
TargetLibraryInfoImpl *TLI = new TargetLibraryInfoImpl(TargetTriple);
|
|
|
|
if (DisableSimplifyLibCalls)
|
|
|
|
TLI->disableAllFunctions();
|
2016-12-31 17:01:23 +00:00
|
|
|
unwrap(PMBR)->LibraryInfo = TLI;
|
2013-08-23 03:58:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Unfortunately, the LLVM C API doesn't provide a way to create the
|
|
|
|
// TargetLibraryInfo pass, so we use this method to do so.
|
2016-12-31 17:01:23 +00:00
|
|
|
extern "C" void LLVMRustAddLibraryInfo(LLVMPassManagerRef PMR, LLVMModuleRef M,
|
2016-12-30 11:22:11 +00:00
|
|
|
bool DisableSimplifyLibCalls) {
|
|
|
|
Triple TargetTriple(unwrap(M)->getTargetTriple());
|
|
|
|
TargetLibraryInfoImpl TLII(TargetTriple);
|
|
|
|
if (DisableSimplifyLibCalls)
|
|
|
|
TLII.disableAllFunctions();
|
2016-12-31 17:01:23 +00:00
|
|
|
unwrap(PMR)->add(new TargetLibraryInfoWrapperPass(TLII));
|
2013-08-23 03:58:42 +00: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.
|
2016-12-31 17:01:23 +00:00
|
|
|
extern "C" void LLVMRustRunFunctionPassManager(LLVMPassManagerRef PMR,
|
2016-12-30 11:22:11 +00:00
|
|
|
LLVMModuleRef M) {
|
|
|
|
llvm::legacy::FunctionPassManager *P =
|
2016-12-31 17:01:23 +00:00
|
|
|
unwrap<llvm::legacy::FunctionPassManager>(PMR);
|
2016-12-30 11:22:11 +00:00
|
|
|
P->doInitialization();
|
|
|
|
|
|
|
|
// Upgrade all calls to old intrinsics first.
|
|
|
|
for (Module::iterator I = unwrap(M)->begin(), E = unwrap(M)->end(); I != E;)
|
|
|
|
UpgradeCallsToIntrinsic(&*I++); // must be post-increment, as we remove
|
|
|
|
|
|
|
|
for (Module::iterator I = unwrap(M)->begin(), E = unwrap(M)->end(); I != E;
|
|
|
|
++I)
|
|
|
|
if (!I->isDeclaration())
|
|
|
|
P->run(*I);
|
|
|
|
|
|
|
|
P->doFinalization();
|
2013-08-23 03:58:42 +00:00
|
|
|
}
|
|
|
|
|
2016-12-30 11:22:11 +00:00
|
|
|
extern "C" void LLVMRustSetLLVMOptions(int Argc, char **Argv) {
|
|
|
|
// 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.
|
2016-12-31 17:01:23 +00:00
|
|
|
static bool Initialized = false;
|
|
|
|
if (Initialized)
|
2016-12-30 11:22:11 +00:00
|
|
|
return;
|
2016-12-31 17:01:23 +00:00
|
|
|
Initialized = true;
|
2016-12-30 11:22:11 +00:00
|
|
|
cl::ParseCommandLineOptions(Argc, Argv);
|
2013-08-23 03:58:42 +00:00
|
|
|
}
|
|
|
|
|
2016-08-01 21:16:16 +00:00
|
|
|
enum class LLVMRustFileType {
|
2016-12-30 11:22:11 +00:00
|
|
|
Other,
|
|
|
|
AssemblyFile,
|
|
|
|
ObjectFile,
|
2016-08-01 21:16:16 +00:00
|
|
|
};
|
|
|
|
|
2016-12-31 17:01:23 +00:00
|
|
|
static TargetMachine::CodeGenFileType fromRust(LLVMRustFileType Type) {
|
|
|
|
switch (Type) {
|
2016-12-30 11:22:11 +00:00
|
|
|
case LLVMRustFileType::AssemblyFile:
|
|
|
|
return TargetMachine::CGFT_AssemblyFile;
|
|
|
|
case LLVMRustFileType::ObjectFile:
|
|
|
|
return TargetMachine::CGFT_ObjectFile;
|
|
|
|
default:
|
|
|
|
llvm_unreachable("Bad FileType.");
|
2016-08-01 21:16:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" LLVMRustResult
|
2016-12-30 11:22:11 +00:00
|
|
|
LLVMRustWriteOutputFile(LLVMTargetMachineRef Target, LLVMPassManagerRef PMR,
|
2016-12-31 17:01:23 +00:00
|
|
|
LLVMModuleRef M, const char *Path,
|
|
|
|
LLVMRustFileType RustFileType) {
|
2016-07-12 22:42:20 +00:00
|
|
|
llvm::legacy::PassManager *PM = unwrap<llvm::legacy::PassManager>(PMR);
|
2016-12-31 17:01:23 +00:00
|
|
|
auto FileType = fromRust(RustFileType);
|
2013-08-23 03:58:42 +00:00
|
|
|
|
|
|
|
std::string ErrorInfo;
|
2014-09-30 21:20:22 +00:00
|
|
|
std::error_code EC;
|
2016-12-31 17:01:23 +00:00
|
|
|
raw_fd_ostream OS(Path, EC, sys::fs::F_None);
|
2014-09-30 21:20:22 +00:00
|
|
|
if (EC)
|
|
|
|
ErrorInfo = EC.message();
|
2013-08-23 03:58:42 +00:00
|
|
|
if (ErrorInfo != "") {
|
2014-04-15 14:25:22 +00:00
|
|
|
LLVMRustSetLastError(ErrorInfo.c_str());
|
2016-08-01 21:16:16 +00:00
|
|
|
return LLVMRustResult::Failure;
|
2013-08-23 03:58:42 +00: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 19:10:43 +00:00
|
|
|
unwrap(Target)->addPassesToEmitFile(*PM, OS, FileType, false);
|
2013-08-23 03:58:42 +00: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 19:10:43 +00:00
|
|
|
|
2015-10-07 22:11:25 +00: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 19:10:43 +00: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;
|
2016-08-01 21:16:16 +00:00
|
|
|
return LLVMRustResult::Success;
|
2013-08-23 03:58:42 +00:00
|
|
|
}
|
|
|
|
|
2017-06-29 14:52:43 +00:00
|
|
|
|
|
|
|
// Callback to demangle function name
|
|
|
|
// Parameters:
|
|
|
|
// * name to be demangled
|
|
|
|
// * name len
|
|
|
|
// * output buffer
|
|
|
|
// * output buffer len
|
|
|
|
// Returns len of demangled string, or 0 if demangle failed.
|
|
|
|
typedef size_t (*DemangleFn)(const char*, size_t, char*, size_t);
|
|
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class RustAssemblyAnnotationWriter : public AssemblyAnnotationWriter {
|
|
|
|
DemangleFn Demangle;
|
|
|
|
std::vector<char> Buf;
|
|
|
|
|
|
|
|
public:
|
|
|
|
RustAssemblyAnnotationWriter(DemangleFn Demangle) : Demangle(Demangle) {}
|
|
|
|
|
|
|
|
// Return empty string if demangle failed
|
|
|
|
// or if name does not need to be demangled
|
|
|
|
StringRef CallDemangle(StringRef name) {
|
|
|
|
if (!Demangle) {
|
|
|
|
return StringRef();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Buf.size() < name.size() * 2) {
|
|
|
|
// Semangled name usually shorter than mangled,
|
|
|
|
// but allocate twice as much memory just in case
|
|
|
|
Buf.resize(name.size() * 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto R = Demangle(name.data(), name.size(), Buf.data(), Buf.size());
|
|
|
|
if (!R) {
|
|
|
|
// Demangle failed.
|
|
|
|
return StringRef();
|
|
|
|
}
|
|
|
|
|
|
|
|
auto Demangled = StringRef(Buf.data(), R);
|
|
|
|
if (Demangled == name) {
|
|
|
|
// Do not print anything if demangled name is equal to mangled.
|
|
|
|
return StringRef();
|
|
|
|
}
|
|
|
|
|
|
|
|
return Demangled;
|
|
|
|
}
|
|
|
|
|
|
|
|
void emitFunctionAnnot(const Function *F,
|
|
|
|
formatted_raw_ostream &OS) override {
|
|
|
|
StringRef Demangled = CallDemangle(F->getName());
|
|
|
|
if (Demangled.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
OS << "; " << Demangled << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
void emitInstructionAnnot(const Instruction *I,
|
|
|
|
formatted_raw_ostream &OS) override {
|
|
|
|
const char *Name;
|
|
|
|
const Value *Value;
|
|
|
|
if (const CallInst *CI = dyn_cast<CallInst>(I)) {
|
|
|
|
Name = "call";
|
|
|
|
Value = CI->getCalledValue();
|
|
|
|
} else if (const InvokeInst* II = dyn_cast<InvokeInst>(I)) {
|
|
|
|
Name = "invoke";
|
|
|
|
Value = II->getCalledValue();
|
|
|
|
} else {
|
|
|
|
// Could demangle more operations, e. g.
|
|
|
|
// `store %place, @function`.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Value->hasName()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
StringRef Demangled = CallDemangle(Value->getName());
|
|
|
|
if (Demangled.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
OS << "; " << Name << " " << Demangled << "\n";
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class RustPrintModulePass : public ModulePass {
|
|
|
|
raw_ostream* OS;
|
|
|
|
DemangleFn Demangle;
|
|
|
|
public:
|
|
|
|
static char ID;
|
|
|
|
RustPrintModulePass() : ModulePass(ID), OS(nullptr), Demangle(nullptr) {}
|
|
|
|
RustPrintModulePass(raw_ostream &OS, DemangleFn Demangle)
|
|
|
|
: ModulePass(ID), OS(&OS), Demangle(Demangle) {}
|
|
|
|
|
|
|
|
bool runOnModule(Module &M) override {
|
|
|
|
RustAssemblyAnnotationWriter AW(Demangle);
|
|
|
|
|
|
|
|
M.print(*OS, &AW, false);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
|
|
|
AU.setPreservesAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
static StringRef name() { return "RustPrintModulePass"; }
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
void initializeRustPrintModulePassPass(PassRegistry&);
|
|
|
|
}
|
|
|
|
|
|
|
|
char RustPrintModulePass::ID = 0;
|
|
|
|
INITIALIZE_PASS(RustPrintModulePass, "print-rust-module",
|
|
|
|
"Print rust module to stderr", false, false)
|
|
|
|
|
2016-12-30 11:22:11 +00:00
|
|
|
extern "C" void LLVMRustPrintModule(LLVMPassManagerRef PMR, LLVMModuleRef M,
|
2017-06-29 14:52:43 +00:00
|
|
|
const char *Path, DemangleFn Demangle) {
|
2016-07-12 22:42:20 +00:00
|
|
|
llvm::legacy::PassManager *PM = unwrap<llvm::legacy::PassManager>(PMR);
|
2013-08-23 03:58:42 +00:00
|
|
|
std::string ErrorInfo;
|
2014-02-26 22:06:27 +00:00
|
|
|
|
2014-09-30 21:20:22 +00:00
|
|
|
std::error_code EC;
|
2016-12-31 17:01:23 +00:00
|
|
|
raw_fd_ostream OS(Path, EC, sys::fs::F_None);
|
2014-09-30 21:20:22 +00:00
|
|
|
if (EC)
|
|
|
|
ErrorInfo = EC.message();
|
2014-02-26 22:06:27 +00:00
|
|
|
|
2013-08-23 03:58:42 +00:00
|
|
|
formatted_raw_ostream FOS(OS);
|
2014-02-26 22:06:27 +00:00
|
|
|
|
2017-06-29 14:52:43 +00:00
|
|
|
PM->add(new RustPrintModulePass(FOS, Demangle));
|
2014-02-26 22:06:27 +00:00
|
|
|
|
2013-08-23 03:58:42 +00:00
|
|
|
PM->run(*unwrap(M));
|
|
|
|
}
|
|
|
|
|
2016-12-30 11:22:11 +00:00
|
|
|
extern "C" void LLVMRustPrintPasses() {
|
|
|
|
LLVMInitializePasses();
|
|
|
|
struct MyListener : PassRegistrationListener {
|
2016-12-31 17:01:23 +00:00
|
|
|
void passEnumerate(const PassInfo *Info) {
|
2016-11-27 13:48:47 +00:00
|
|
|
#if LLVM_VERSION_GE(4, 0)
|
2016-12-31 17:01:23 +00:00
|
|
|
StringRef PassArg = Info->getPassArgument();
|
|
|
|
StringRef PassName = Info->getPassName();
|
2016-12-30 11:22:11 +00:00
|
|
|
if (!PassArg.empty()) {
|
|
|
|
// These unsigned->signed casts could theoretically overflow, but
|
|
|
|
// realistically never will (and even if, the result is implementation
|
|
|
|
// defined rather plain UB).
|
|
|
|
printf("%15.*s - %.*s\n", (int)PassArg.size(), PassArg.data(),
|
|
|
|
(int)PassName.size(), PassName.data());
|
|
|
|
}
|
2016-11-27 13:48:47 +00:00
|
|
|
#else
|
2016-12-31 17:01:23 +00:00
|
|
|
if (Info->getPassArgument() && *Info->getPassArgument()) {
|
|
|
|
printf("%15s - %s\n", Info->getPassArgument(), Info->getPassName());
|
2016-12-30 11:22:11 +00:00
|
|
|
}
|
2016-11-27 13:48:47 +00:00
|
|
|
#endif
|
2016-12-30 11:22:11 +00:00
|
|
|
}
|
2016-12-31 17:01:23 +00:00
|
|
|
} Listener;
|
2013-08-23 03:58:42 +00:00
|
|
|
|
2016-12-30 11:22:11 +00:00
|
|
|
PassRegistry *PR = PassRegistry::getPassRegistry();
|
2016-12-31 17:01:23 +00:00
|
|
|
PR->enumerateWith(&Listener);
|
2013-05-27 23:15:31 +00:00
|
|
|
}
|
2013-06-19 22:18:25 +00:00
|
|
|
|
2016-12-31 17:01:23 +00:00
|
|
|
extern "C" void LLVMRustAddAlwaysInlinePass(LLVMPassManagerBuilderRef PMBR,
|
2016-12-30 11:22:11 +00:00
|
|
|
bool AddLifetimes) {
|
2016-09-24 16:37:04 +00:00
|
|
|
#if LLVM_VERSION_GE(4, 0)
|
2016-12-31 17:01:23 +00:00
|
|
|
unwrap(PMBR)->Inliner = llvm::createAlwaysInlinerLegacyPass(AddLifetimes);
|
2016-09-24 16:37:04 +00:00
|
|
|
#else
|
2016-12-31 17:01:23 +00:00
|
|
|
unwrap(PMBR)->Inliner = createAlwaysInlinerPass(AddLifetimes);
|
2016-09-24 16:37:04 +00:00
|
|
|
#endif
|
2013-06-19 22:18:25 +00: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 07:19:29 +00:00
|
|
|
|
2016-12-31 17:01:23 +00:00
|
|
|
extern "C" void LLVMRustRunRestrictionPass(LLVMModuleRef M, char **Symbols,
|
|
|
|
size_t Len) {
|
2016-12-30 11:22:11 +00:00
|
|
|
llvm::legacy::PassManager passes;
|
2016-07-12 22:42:44 +00:00
|
|
|
|
2016-09-24 14:44:21 +00:00
|
|
|
#if LLVM_VERSION_LE(3, 8)
|
2016-12-31 17:01:23 +00:00
|
|
|
ArrayRef<const char *> Ref(Symbols, Len);
|
|
|
|
passes.add(llvm::createInternalizePass(Ref));
|
2016-07-12 22:42:44 +00:00
|
|
|
#else
|
2016-12-30 11:22:11 +00:00
|
|
|
auto PreserveFunctions = [=](const GlobalValue &GV) {
|
2016-12-31 17:01:23 +00:00
|
|
|
for (size_t I = 0; I < Len; I++) {
|
|
|
|
if (GV.getName() == Symbols[I]) {
|
2016-12-30 11:22:11 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
2016-07-12 22:42:44 +00:00
|
|
|
|
2016-12-30 11:22:11 +00:00
|
|
|
passes.add(llvm::createInternalizePass(PreserveFunctions));
|
2016-07-12 22:42:44 +00:00
|
|
|
#endif
|
|
|
|
|
2016-12-30 11:22:11 +00:00
|
|
|
passes.run(*unwrap(M));
|
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 07:19:29 +00:00
|
|
|
}
|
2013-12-11 07:27:15 +00:00
|
|
|
|
2016-12-30 11:22:11 +00: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);
|
2016-12-30 12:21:21 +00:00
|
|
|
if (F == nullptr)
|
2016-12-30 11:22:11 +00:00
|
|
|
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();
|
2013-12-11 07:27:15 +00:00
|
|
|
}
|
2016-12-30 11:22:11 +00:00
|
|
|
}
|
2013-12-11 07:27:15 +00:00
|
|
|
}
|
2016-12-30 11:22:11 +00:00
|
|
|
}
|
2013-12-11 07:27:15 +00:00
|
|
|
}
|
2015-07-16 22:48:16 +00:00
|
|
|
|
|
|
|
extern "C" void
|
|
|
|
LLVMRustSetDataLayoutFromTargetMachine(LLVMModuleRef Module,
|
|
|
|
LLVMTargetMachineRef TMR) {
|
2016-12-30 11:22:11 +00:00
|
|
|
TargetMachine *Target = unwrap(TMR);
|
|
|
|
unwrap(Module)->setDataLayout(Target->createDataLayout());
|
2015-07-16 22:48:16 +00:00
|
|
|
}
|
|
|
|
|
2016-12-30 11:22:11 +00:00
|
|
|
extern "C" LLVMTargetDataRef LLVMRustGetModuleDataLayout(LLVMModuleRef M) {
|
|
|
|
return wrap(&unwrap(M)->getDataLayout());
|
2015-07-16 22:48:16 +00:00
|
|
|
}
|
2016-07-12 22:41:40 +00:00
|
|
|
|
2016-12-30 11:22:11 +00:00
|
|
|
extern "C" void LLVMRustSetModulePIELevel(LLVMModuleRef M) {
|
2016-09-24 14:44:21 +00:00
|
|
|
#if LLVM_VERSION_GE(3, 9)
|
2016-12-30 11:22:11 +00:00
|
|
|
unwrap(M)->setPIELevel(PIELevel::Level::Large);
|
2016-07-12 22:41:40 +00:00
|
|
|
#endif
|
|
|
|
}
|