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-05-25 22:23:12 +00:00
|
|
|
#include "llvm/IR/IRBuilder.h"
|
2013-05-27 23:15:31 +00:00
|
|
|
#include "llvm/IR/InlineAsm.h"
|
|
|
|
#include "llvm/IR/LLVMContext.h"
|
2013-05-25 22:23:12 +00:00
|
|
|
#include "llvm/IR/Module.h"
|
2013-05-27 23:15:31 +00:00
|
|
|
#include "llvm/IR/InlineAsm.h"
|
|
|
|
#include "llvm/IR/LLVMContext.h"
|
|
|
|
#include "llvm/Analysis/Passes.h"
|
|
|
|
#include "llvm/Analysis/Lint.h"
|
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
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
2013-05-27 23:15:31 +00:00
|
|
|
#include "llvm/ADT/Triple.h"
|
|
|
|
#include "llvm/ADT/DenseSet.h"
|
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/Support/FormattedStream.h"
|
|
|
|
#include "llvm/Support/Timer.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include "llvm/Support/TargetSelect.h"
|
|
|
|
#include "llvm/Support/TargetRegistry.h"
|
|
|
|
#include "llvm/Support/SourceMgr.h"
|
|
|
|
#include "llvm/Support/Host.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/DynamicLibrary.h"
|
|
|
|
#include "llvm/Support/Memory.h"
|
|
|
|
#include "llvm/ExecutionEngine/ExecutionEngine.h"
|
|
|
|
#include "llvm/ExecutionEngine/MCJIT.h"
|
|
|
|
#include "llvm/ExecutionEngine/Interpreter.h"
|
|
|
|
#include "llvm/Target/TargetMachine.h"
|
|
|
|
#include "llvm/Target/TargetOptions.h"
|
|
|
|
#include "llvm/Transforms/Scalar.h"
|
|
|
|
#include "llvm/Transforms/IPO.h"
|
|
|
|
#include "llvm/Transforms/Instrumentation.h"
|
|
|
|
#include "llvm/Transforms/Vectorize.h"
|
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
|
|
|
#include "llvm/Bitcode/ReaderWriter.h"
|
2013-05-27 23:15:31 +00:00
|
|
|
#include "llvm-c/Core.h"
|
|
|
|
#include "llvm-c/BitReader.h"
|
2013-06-14 04:25:18 +00:00
|
|
|
#include "llvm-c/ExecutionEngine.h"
|
2013-05-27 23:15:31 +00:00
|
|
|
#include "llvm-c/Object.h"
|
|
|
|
|
2016-09-24 14:44:21 +00:00
|
|
|
#define LLVM_VERSION_GE(major, minor) \
|
|
|
|
(LLVM_VERSION_MAJOR > (major) || LLVM_VERSION_MAJOR == (major) && LLVM_VERSION_MINOR >= (minor))
|
|
|
|
|
|
|
|
#define LLVM_VERSION_EQ(major, minor) \
|
|
|
|
(LLVM_VERSION_MAJOR == (major) && LLVM_VERSION_MINOR == (minor))
|
|
|
|
|
|
|
|
#define LLVM_VERSION_LE(major, minor) \
|
|
|
|
(LLVM_VERSION_MAJOR < (major) || LLVM_VERSION_MAJOR == (major) && LLVM_VERSION_MINOR <= (minor))
|
|
|
|
|
|
|
|
#if LLVM_VERSION_GE(3, 7)
|
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/IR/LegacyPassManager.h"
|
|
|
|
#else
|
|
|
|
#include "llvm/PassManager.h"
|
|
|
|
#endif
|
|
|
|
|
2014-02-26 22:06:27 +00:00
|
|
|
#include "llvm/IR/IRPrintingPasses.h"
|
2014-03-31 21:43:19 +00:00
|
|
|
#include "llvm/IR/DebugInfo.h"
|
|
|
|
#include "llvm/IR/DIBuilder.h"
|
|
|
|
#include "llvm/Linker/Linker.h"
|
2014-02-26 22:06:27 +00:00
|
|
|
|
2014-04-15 14:25:22 +00:00
|
|
|
void LLVMRustSetLastError(const char*);
|
2014-09-10 06:12:09 +00:00
|
|
|
|
2016-08-01 21:16:16 +00:00
|
|
|
enum class LLVMRustResult {
|
|
|
|
Success,
|
|
|
|
Failure
|
|
|
|
};
|
|
|
|
|
2014-09-10 06:12:09 +00:00
|
|
|
typedef struct OpaqueRustString *RustStringRef;
|
2014-09-12 15:17:58 +00:00
|
|
|
typedef struct LLVMOpaqueTwine *LLVMTwineRef;
|
|
|
|
typedef struct LLVMOpaqueDebugLoc *LLVMDebugLocRef;
|
2014-09-27 08:33:36 +00:00
|
|
|
typedef struct LLVMOpaqueSMDiagnostic *LLVMSMDiagnosticRef;
|
2014-11-24 18:55:14 +00:00
|
|
|
typedef struct LLVMOpaqueRustJITMemoryManager *LLVMRustJITMemoryManagerRef;
|
2014-09-10 06:12:09 +00:00
|
|
|
|
|
|
|
extern "C" void
|
|
|
|
rust_llvm_string_write_impl(RustStringRef str, const char *ptr, size_t size);
|
|
|
|
|
|
|
|
class raw_rust_string_ostream : public llvm::raw_ostream {
|
|
|
|
RustStringRef str;
|
|
|
|
uint64_t pos;
|
|
|
|
|
|
|
|
void write_impl(const char *ptr, size_t size) override {
|
|
|
|
rust_llvm_string_write_impl(str, ptr, size);
|
|
|
|
pos += size;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t current_pos() const override {
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit raw_rust_string_ostream(RustStringRef str)
|
|
|
|
: str(str), pos(0) { }
|
|
|
|
|
|
|
|
~raw_rust_string_ostream() {
|
|
|
|
// LLVM requires this.
|
|
|
|
flush();
|
|
|
|
}
|
|
|
|
};
|