2013-05-28 11:15:31 +12:00
|
|
|
|
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
|
2012-12-10 15:44:02 -08:00
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
2011-03-15 14:57:26 -07:00
|
|
|
|
//
|
2012-12-10 15:44:02 -08:00
|
|
|
|
// 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-28 11:15:31 +12:00
|
|
|
|
#include "rustllvm.h"
|
2017-03-16 21:10:04 +01:00
|
|
|
|
#include "llvm/IR/DebugInfoMetadata.h"
|
2014-09-12 08:17:58 -07:00
|
|
|
|
#include "llvm/IR/DiagnosticInfo.h"
|
|
|
|
|
#include "llvm/IR/DiagnosticPrinter.h"
|
2016-08-02 02:35:09 +03:00
|
|
|
|
#include "llvm/IR/Instructions.h"
|
2016-12-30 12:22:11 +01:00
|
|
|
|
#include "llvm/Object/Archive.h"
|
|
|
|
|
#include "llvm/Object/ObjectFile.h"
|
2013-05-28 11:15:31 +12:00
|
|
|
|
|
2014-05-21 15:07:48 -04:00
|
|
|
|
#include "llvm/IR/CallSite.h"
|
|
|
|
|
|
2011-10-21 17:35:52 -07:00
|
|
|
|
//===----------------------------------------------------------------------===
|
2011-03-15 14:57:26 -07:00
|
|
|
|
//
|
|
|
|
|
// This file defines alternate interfaces to core functions that are more
|
|
|
|
|
// readily callable by Rust's FFI.
|
|
|
|
|
//
|
2011-10-21 17:35:52 -07:00
|
|
|
|
//===----------------------------------------------------------------------===
|
2011-03-15 14:57:26 -07:00
|
|
|
|
|
2011-04-15 17:35:46 -04:00
|
|
|
|
using namespace llvm;
|
2012-09-11 16:05:51 +10:00
|
|
|
|
using namespace llvm::sys;
|
2013-12-16 20:58:21 -08:00
|
|
|
|
using namespace llvm::object;
|
2011-04-15 17:35:46 -04:00
|
|
|
|
|
2016-08-02 02:35:09 +03:00
|
|
|
|
// LLVMAtomicOrdering is already an enum - don't create another
|
|
|
|
|
// one.
|
2016-12-31 12:01:23 -05:00
|
|
|
|
static AtomicOrdering fromRust(LLVMAtomicOrdering Ordering) {
|
2016-08-02 02:35:09 +03:00
|
|
|
|
switch (Ordering) {
|
2016-12-30 12:22:11 +01:00
|
|
|
|
case LLVMAtomicOrderingNotAtomic:
|
|
|
|
|
return AtomicOrdering::NotAtomic;
|
|
|
|
|
case LLVMAtomicOrderingUnordered:
|
|
|
|
|
return AtomicOrdering::Unordered;
|
|
|
|
|
case LLVMAtomicOrderingMonotonic:
|
|
|
|
|
return AtomicOrdering::Monotonic;
|
|
|
|
|
case LLVMAtomicOrderingAcquire:
|
|
|
|
|
return AtomicOrdering::Acquire;
|
|
|
|
|
case LLVMAtomicOrderingRelease:
|
|
|
|
|
return AtomicOrdering::Release;
|
|
|
|
|
case LLVMAtomicOrderingAcquireRelease:
|
|
|
|
|
return AtomicOrdering::AcquireRelease;
|
|
|
|
|
case LLVMAtomicOrderingSequentiallyConsistent:
|
|
|
|
|
return AtomicOrdering::SequentiallyConsistent;
|
2016-08-02 02:35:09 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
llvm_unreachable("Invalid LLVMAtomicOrdering value!");
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-15 07:25:22 -07:00
|
|
|
|
static char *LastError;
|
2013-04-05 17:17:49 -07:00
|
|
|
|
|
2014-07-18 07:07:15 -07:00
|
|
|
|
extern "C" LLVMMemoryBufferRef
|
|
|
|
|
LLVMRustCreateMemoryBufferWithContentsOfFile(const char *Path) {
|
2016-12-31 12:01:23 -05:00
|
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>> BufOr =
|
2016-12-30 12:22:11 +01:00
|
|
|
|
MemoryBuffer::getFile(Path, -1, false);
|
2016-12-31 12:01:23 -05:00
|
|
|
|
if (!BufOr) {
|
|
|
|
|
LLVMRustSetLastError(BufOr.getError().message().c_str());
|
2016-12-30 12:22:11 +01:00
|
|
|
|
return nullptr;
|
2014-07-18 07:07:15 -07:00
|
|
|
|
}
|
2016-12-31 12:01:23 -05:00
|
|
|
|
return wrap(BufOr.get().release());
|
2014-07-18 07:07:15 -07:00
|
|
|
|
}
|
2011-03-15 14:57:26 -07:00
|
|
|
|
|
2014-04-15 07:25:22 -07:00
|
|
|
|
extern "C" char *LLVMRustGetLastError(void) {
|
2016-12-31 12:01:23 -05:00
|
|
|
|
char *Ret = LastError;
|
2016-12-30 13:21:21 +01:00
|
|
|
|
LastError = nullptr;
|
2016-12-31 12:01:23 -05:00
|
|
|
|
return Ret;
|
2014-04-15 07:25:22 -07:00
|
|
|
|
}
|
|
|
|
|
|
2016-12-31 12:01:23 -05:00
|
|
|
|
void LLVMRustSetLastError(const char *Err) {
|
2016-12-30 12:22:11 +01:00
|
|
|
|
free((void *)LastError);
|
2016-12-31 12:01:23 -05:00
|
|
|
|
LastError = strdup(Err);
|
2011-03-15 14:57:26 -07:00
|
|
|
|
}
|
|
|
|
|
|
2016-12-30 12:22:11 +01:00
|
|
|
|
extern "C" void LLVMRustSetNormalizedTarget(LLVMModuleRef M,
|
2016-12-31 12:01:23 -05:00
|
|
|
|
const char *Triple) {
|
|
|
|
|
unwrap(M)->setTargetTriple(Triple::normalize(Triple));
|
2011-05-05 11:34:15 -07:00
|
|
|
|
}
|
|
|
|
|
|
2011-05-10 16:10:08 -07:00
|
|
|
|
extern "C" void LLVMRustPrintPassTimings() {
|
2016-12-30 12:22:11 +01:00
|
|
|
|
raw_fd_ostream OS(2, false); // stderr.
|
2011-05-10 16:10:08 -07:00
|
|
|
|
TimerGroup::printAll(OS);
|
|
|
|
|
}
|
2011-10-31 14:42:17 -07:00
|
|
|
|
|
2016-08-02 02:35:09 +03:00
|
|
|
|
extern "C" LLVMValueRef LLVMRustGetNamedValue(LLVMModuleRef M,
|
2016-12-30 12:22:11 +01:00
|
|
|
|
const char *Name) {
|
|
|
|
|
return wrap(unwrap(M)->getNamedValue(Name));
|
2015-02-27 13:37:33 +02:00
|
|
|
|
}
|
|
|
|
|
|
2016-08-02 02:35:09 +03:00
|
|
|
|
extern "C" LLVMValueRef LLVMRustGetOrInsertFunction(LLVMModuleRef M,
|
2016-12-30 12:22:11 +01:00
|
|
|
|
const char *Name,
|
|
|
|
|
LLVMTypeRef FunctionTy) {
|
|
|
|
|
return wrap(
|
|
|
|
|
unwrap(M)->getOrInsertFunction(Name, unwrap<FunctionType>(FunctionTy)));
|
2011-11-15 00:32:31 +08:00
|
|
|
|
}
|
2011-11-10 00:55:09 -05:00
|
|
|
|
|
2016-12-30 12:22:11 +01:00
|
|
|
|
extern "C" LLVMValueRef
|
|
|
|
|
LLVMRustGetOrInsertGlobal(LLVMModuleRef M, const char *Name, LLVMTypeRef Ty) {
|
2015-03-04 00:57:44 +02:00
|
|
|
|
return wrap(unwrap(M)->getOrInsertGlobal(Name, unwrap(Ty)));
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-02 02:35:09 +03:00
|
|
|
|
extern "C" LLVMTypeRef LLVMRustMetadataTypeInContext(LLVMContextRef C) {
|
2011-11-10 00:55:09 -05:00
|
|
|
|
return wrap(Type::getMetadataTy(*unwrap(C)));
|
|
|
|
|
}
|
2012-06-21 15:01:32 -07:00
|
|
|
|
|
2016-12-31 12:01:23 -05:00
|
|
|
|
static Attribute::AttrKind fromRust(LLVMRustAttribute Kind) {
|
|
|
|
|
switch (Kind) {
|
2016-12-30 12:22:11 +01:00
|
|
|
|
case AlwaysInline:
|
|
|
|
|
return Attribute::AlwaysInline;
|
|
|
|
|
case ByVal:
|
|
|
|
|
return Attribute::ByVal;
|
|
|
|
|
case Cold:
|
|
|
|
|
return Attribute::Cold;
|
|
|
|
|
case InlineHint:
|
|
|
|
|
return Attribute::InlineHint;
|
|
|
|
|
case MinSize:
|
|
|
|
|
return Attribute::MinSize;
|
|
|
|
|
case Naked:
|
|
|
|
|
return Attribute::Naked;
|
|
|
|
|
case NoAlias:
|
|
|
|
|
return Attribute::NoAlias;
|
|
|
|
|
case NoCapture:
|
|
|
|
|
return Attribute::NoCapture;
|
|
|
|
|
case NoInline:
|
|
|
|
|
return Attribute::NoInline;
|
|
|
|
|
case NonNull:
|
|
|
|
|
return Attribute::NonNull;
|
|
|
|
|
case NoRedZone:
|
|
|
|
|
return Attribute::NoRedZone;
|
|
|
|
|
case NoReturn:
|
|
|
|
|
return Attribute::NoReturn;
|
|
|
|
|
case NoUnwind:
|
|
|
|
|
return Attribute::NoUnwind;
|
|
|
|
|
case OptimizeForSize:
|
|
|
|
|
return Attribute::OptimizeForSize;
|
|
|
|
|
case ReadOnly:
|
|
|
|
|
return Attribute::ReadOnly;
|
|
|
|
|
case SExt:
|
|
|
|
|
return Attribute::SExt;
|
|
|
|
|
case StructRet:
|
|
|
|
|
return Attribute::StructRet;
|
|
|
|
|
case UWTable:
|
|
|
|
|
return Attribute::UWTable;
|
|
|
|
|
case ZExt:
|
|
|
|
|
return Attribute::ZExt;
|
|
|
|
|
case InReg:
|
|
|
|
|
return Attribute::InReg;
|
2016-12-29 23:28:11 -05:00
|
|
|
|
case SanitizeThread:
|
|
|
|
|
return Attribute::SanitizeThread;
|
|
|
|
|
case SanitizeAddress:
|
|
|
|
|
return Attribute::SanitizeAddress;
|
|
|
|
|
case SanitizeMemory:
|
|
|
|
|
return Attribute::SanitizeMemory;
|
2016-11-16 23:36:08 +01:00
|
|
|
|
}
|
2017-01-14 15:38:12 +01:00
|
|
|
|
llvm_unreachable("bad AttributeKind");
|
2016-11-16 23:36:08 +01:00
|
|
|
|
}
|
|
|
|
|
|
2016-12-31 12:01:23 -05:00
|
|
|
|
extern "C" void LLVMRustAddCallSiteAttribute(LLVMValueRef Instr, unsigned Index,
|
|
|
|
|
LLVMRustAttribute RustAttr) {
|
2014-05-21 15:07:48 -04:00
|
|
|
|
CallSite Call = CallSite(unwrap<Instruction>(Instr));
|
2016-12-31 12:01:23 -05:00
|
|
|
|
Attribute Attr = Attribute::get(Call->getContext(), fromRust(RustAttr));
|
2016-11-21 20:30:05 +09:00
|
|
|
|
AttrBuilder B(Attr);
|
2016-12-30 12:22:11 +01:00
|
|
|
|
Call.setAttributes(Call.getAttributes().addAttributes(
|
2016-12-31 12:01:23 -05:00
|
|
|
|
Call->getContext(), Index,
|
|
|
|
|
AttributeSet::get(Call->getContext(), Index, B)));
|
2013-08-05 21:21:37 -07:00
|
|
|
|
}
|
|
|
|
|
|
2016-08-02 02:35:09 +03:00
|
|
|
|
extern "C" void LLVMRustAddDereferenceableCallSiteAttr(LLVMValueRef Instr,
|
2016-12-31 12:01:23 -05:00
|
|
|
|
unsigned Index,
|
|
|
|
|
uint64_t Bytes) {
|
2014-07-25 16:06:44 -07:00
|
|
|
|
CallSite Call = CallSite(unwrap<Instruction>(Instr));
|
|
|
|
|
AttrBuilder B;
|
2016-12-31 12:01:23 -05:00
|
|
|
|
B.addDereferenceableAttr(Bytes);
|
2016-12-30 12:22:11 +01:00
|
|
|
|
Call.setAttributes(Call.getAttributes().addAttributes(
|
2016-12-31 12:01:23 -05:00
|
|
|
|
Call->getContext(), Index,
|
|
|
|
|
AttributeSet::get(Call->getContext(), Index, B)));
|
2014-07-25 16:06:44 -07:00
|
|
|
|
}
|
|
|
|
|
|
2016-12-31 12:01:23 -05:00
|
|
|
|
extern "C" void LLVMRustAddFunctionAttribute(LLVMValueRef Fn, unsigned Index,
|
|
|
|
|
LLVMRustAttribute RustAttr) {
|
2014-05-21 15:07:48 -04:00
|
|
|
|
Function *A = unwrap<Function>(Fn);
|
2016-12-31 12:01:23 -05:00
|
|
|
|
Attribute Attr = Attribute::get(A->getContext(), fromRust(RustAttr));
|
2016-11-21 20:30:05 +09:00
|
|
|
|
AttrBuilder B(Attr);
|
2016-12-31 12:01:23 -05:00
|
|
|
|
A->addAttributes(Index, AttributeSet::get(A->getContext(), Index, B));
|
2014-05-21 15:07:48 -04:00
|
|
|
|
}
|
|
|
|
|
|
2016-12-31 12:01:23 -05:00
|
|
|
|
extern "C" void LLVMRustAddDereferenceableAttr(LLVMValueRef Fn, unsigned Index,
|
|
|
|
|
uint64_t Bytes) {
|
2014-07-25 16:06:44 -07:00
|
|
|
|
Function *A = unwrap<Function>(Fn);
|
|
|
|
|
AttrBuilder B;
|
2016-12-31 12:01:23 -05:00
|
|
|
|
B.addDereferenceableAttr(Bytes);
|
|
|
|
|
A->addAttributes(Index, AttributeSet::get(A->getContext(), Index, B));
|
2014-07-25 16:06:44 -07:00
|
|
|
|
}
|
|
|
|
|
|
2016-08-02 02:35:09 +03:00
|
|
|
|
extern "C" void LLVMRustAddFunctionAttrStringValue(LLVMValueRef Fn,
|
2016-12-31 12:01:23 -05:00
|
|
|
|
unsigned Index,
|
2016-12-30 12:22:11 +01:00
|
|
|
|
const char *Name,
|
|
|
|
|
const char *Value) {
|
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 12:10:43 -07:00
|
|
|
|
Function *F = unwrap<Function>(Fn);
|
|
|
|
|
AttrBuilder B;
|
|
|
|
|
B.addAttribute(Name, Value);
|
2016-12-31 12:01:23 -05:00
|
|
|
|
F->addAttributes(Index, AttributeSet::get(F->getContext(), Index, B));
|
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 12:10:43 -07:00
|
|
|
|
}
|
|
|
|
|
|
2016-08-02 02:35:09 +03:00
|
|
|
|
extern "C" void LLVMRustRemoveFunctionAttributes(LLVMValueRef Fn,
|
2016-12-31 12:01:23 -05:00
|
|
|
|
unsigned Index,
|
|
|
|
|
LLVMRustAttribute RustAttr) {
|
2016-11-16 23:36:08 +01:00
|
|
|
|
Function *F = unwrap<Function>(Fn);
|
|
|
|
|
const AttributeSet PAL = F->getAttributes();
|
2016-12-31 12:01:23 -05:00
|
|
|
|
Attribute Attr = Attribute::get(F->getContext(), fromRust(RustAttr));
|
2016-11-21 20:30:05 +09:00
|
|
|
|
AttrBuilder B(Attr);
|
2016-12-31 12:01:23 -05:00
|
|
|
|
const AttributeSet PALNew = PAL.removeAttributes(
|
|
|
|
|
F->getContext(), Index, AttributeSet::get(F->getContext(), Index, B));
|
|
|
|
|
F->setAttributes(PALNew);
|
2014-04-19 10:33:46 -07:00
|
|
|
|
}
|
2013-09-09 02:32:30 -04:00
|
|
|
|
|
2016-03-15 00:01:12 +01:00
|
|
|
|
// enable fpmath flag UnsafeAlgebra
|
2016-03-19 22:02:33 +01:00
|
|
|
|
extern "C" void LLVMRustSetHasUnsafeAlgebra(LLVMValueRef V) {
|
2016-12-30 12:22:11 +01:00
|
|
|
|
if (auto I = dyn_cast<Instruction>(unwrap<Value>(V))) {
|
|
|
|
|
I->setHasUnsafeAlgebra(true);
|
|
|
|
|
}
|
2016-03-15 00:01:12 +01:00
|
|
|
|
}
|
|
|
|
|
|
2016-12-30 12:22:11 +01:00
|
|
|
|
extern "C" LLVMValueRef
|
2016-12-31 12:01:23 -05:00
|
|
|
|
LLVMRustBuildAtomicLoad(LLVMBuilderRef B, LLVMValueRef Source, const char *Name,
|
|
|
|
|
LLVMAtomicOrdering Order, unsigned Alignment) {
|
|
|
|
|
LoadInst *LI = new LoadInst(unwrap(Source), 0);
|
|
|
|
|
LI->setAtomic(fromRust(Order));
|
|
|
|
|
LI->setAlignment(Alignment);
|
|
|
|
|
return wrap(unwrap(B)->Insert(LI, Name));
|
2013-05-12 21:22:20 +02:00
|
|
|
|
}
|
|
|
|
|
|
2016-08-02 02:35:09 +03:00
|
|
|
|
extern "C" LLVMValueRef LLVMRustBuildAtomicStore(LLVMBuilderRef B,
|
2016-12-31 12:01:23 -05:00
|
|
|
|
LLVMValueRef V,
|
|
|
|
|
LLVMValueRef Target,
|
|
|
|
|
LLVMAtomicOrdering Order,
|
|
|
|
|
unsigned Alignment) {
|
|
|
|
|
StoreInst *SI = new StoreInst(unwrap(V), unwrap(Target));
|
|
|
|
|
SI->setAtomic(fromRust(Order));
|
|
|
|
|
SI->setAlignment(Alignment);
|
|
|
|
|
return wrap(unwrap(B)->Insert(SI));
|
2016-12-30 12:22:11 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extern "C" LLVMValueRef
|
2016-12-31 12:01:23 -05:00
|
|
|
|
LLVMRustBuildAtomicCmpXchg(LLVMBuilderRef B, LLVMValueRef Target,
|
|
|
|
|
LLVMValueRef Old, LLVMValueRef Source,
|
|
|
|
|
LLVMAtomicOrdering Order,
|
|
|
|
|
LLVMAtomicOrdering FailureOrder, LLVMBool Weak) {
|
|
|
|
|
AtomicCmpXchgInst *ACXI = unwrap(B)->CreateAtomicCmpXchg(
|
|
|
|
|
unwrap(Target), unwrap(Old), unwrap(Source), fromRust(Order),
|
|
|
|
|
fromRust(FailureOrder));
|
|
|
|
|
ACXI->setWeak(Weak);
|
|
|
|
|
return wrap(ACXI);
|
2012-10-21 22:23:50 -04:00
|
|
|
|
}
|
2016-08-02 02:35:09 +03:00
|
|
|
|
|
|
|
|
|
enum class LLVMRustSynchronizationScope {
|
2016-12-30 12:22:11 +01:00
|
|
|
|
Other,
|
|
|
|
|
SingleThread,
|
|
|
|
|
CrossThread,
|
2016-08-02 02:35:09 +03:00
|
|
|
|
};
|
|
|
|
|
|
2016-12-31 12:01:23 -05:00
|
|
|
|
static SynchronizationScope fromRust(LLVMRustSynchronizationScope Scope) {
|
|
|
|
|
switch (Scope) {
|
2016-12-30 12:22:11 +01:00
|
|
|
|
case LLVMRustSynchronizationScope::SingleThread:
|
|
|
|
|
return SingleThread;
|
|
|
|
|
case LLVMRustSynchronizationScope::CrossThread:
|
|
|
|
|
return CrossThread;
|
|
|
|
|
default:
|
|
|
|
|
llvm_unreachable("bad SynchronizationScope.");
|
|
|
|
|
}
|
2016-08-02 02:35:09 +03:00
|
|
|
|
}
|
|
|
|
|
|
2016-12-30 12:22:11 +01:00
|
|
|
|
extern "C" LLVMValueRef
|
2016-12-31 12:01:23 -05:00
|
|
|
|
LLVMRustBuildAtomicFence(LLVMBuilderRef B, LLVMAtomicOrdering Order,
|
|
|
|
|
LLVMRustSynchronizationScope Scope) {
|
|
|
|
|
return wrap(unwrap(B)->CreateFence(fromRust(Order), fromRust(Scope)));
|
2013-07-28 19:48:16 +12:00
|
|
|
|
}
|
2012-07-25 12:06:03 -07:00
|
|
|
|
|
2016-08-02 02:35:09 +03:00
|
|
|
|
extern "C" void LLVMRustSetDebug(int Enabled) {
|
2012-10-08 21:40:09 +02:00
|
|
|
|
#ifndef NDEBUG
|
2012-07-25 12:06:03 -07:00
|
|
|
|
DebugFlag = Enabled;
|
2012-10-08 21:40:09 +02:00
|
|
|
|
#endif
|
2012-07-25 12:06:03 -07:00
|
|
|
|
}
|
2013-03-09 22:37:50 -08:00
|
|
|
|
|
2016-08-03 00:25:19 +03:00
|
|
|
|
enum class LLVMRustAsmDialect {
|
2016-12-30 12:22:11 +01:00
|
|
|
|
Other,
|
|
|
|
|
Att,
|
|
|
|
|
Intel,
|
2016-08-03 00:25:19 +03:00
|
|
|
|
};
|
|
|
|
|
|
2016-12-31 12:01:23 -05:00
|
|
|
|
static InlineAsm::AsmDialect fromRust(LLVMRustAsmDialect Dialect) {
|
|
|
|
|
switch (Dialect) {
|
2016-12-30 12:22:11 +01:00
|
|
|
|
case LLVMRustAsmDialect::Att:
|
|
|
|
|
return InlineAsm::AD_ATT;
|
|
|
|
|
case LLVMRustAsmDialect::Intel:
|
|
|
|
|
return InlineAsm::AD_Intel;
|
|
|
|
|
default:
|
|
|
|
|
llvm_unreachable("bad AsmDialect.");
|
|
|
|
|
}
|
2016-08-03 00:25:19 +03:00
|
|
|
|
}
|
|
|
|
|
|
2016-12-30 12:22:11 +01:00
|
|
|
|
extern "C" LLVMValueRef LLVMRustInlineAsm(LLVMTypeRef Ty, char *AsmString,
|
|
|
|
|
char *Constraints,
|
|
|
|
|
LLVMBool HasSideEffects,
|
|
|
|
|
LLVMBool IsAlignStack,
|
|
|
|
|
LLVMRustAsmDialect Dialect) {
|
|
|
|
|
return wrap(InlineAsm::get(unwrap<FunctionType>(Ty), AsmString, Constraints,
|
2016-12-31 12:01:23 -05:00
|
|
|
|
HasSideEffects, IsAlignStack, fromRust(Dialect)));
|
2013-03-09 22:37:50 -08:00
|
|
|
|
}
|
2013-06-13 21:25:12 -07:00
|
|
|
|
|
2016-12-30 12:22:11 +01:00
|
|
|
|
typedef DIBuilder *LLVMRustDIBuilderRef;
|
2013-06-14 11:38:29 -07:00
|
|
|
|
|
2016-08-02 02:35:09 +03:00
|
|
|
|
typedef struct LLVMOpaqueMetadata *LLVMRustMetadataRef;
|
2015-01-30 19:25:07 +01:00
|
|
|
|
|
|
|
|
|
namespace llvm {
|
2016-08-02 02:35:09 +03:00
|
|
|
|
DEFINE_ISA_CONVERSION_FUNCTIONS(Metadata, LLVMRustMetadataRef)
|
2015-01-30 19:25:07 +01:00
|
|
|
|
|
2016-08-02 02:35:09 +03:00
|
|
|
|
inline Metadata **unwrap(LLVMRustMetadataRef *Vals) {
|
2016-12-30 12:22:11 +01:00
|
|
|
|
return reinterpret_cast<Metadata **>(Vals);
|
2015-01-30 19:25:07 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-31 12:01:23 -05:00
|
|
|
|
template <typename DIT> DIT *unwrapDIPtr(LLVMRustMetadataRef Ref) {
|
|
|
|
|
return (DIT *)(Ref ? unwrap<MDNode>(Ref) : nullptr);
|
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 12:10:43 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#define DIDescriptor DIScope
|
|
|
|
|
#define DIArray DINodeArray
|
2016-12-31 12:01:23 -05:00
|
|
|
|
#define unwrapDI unwrapDIPtr
|
2013-06-14 11:38:29 -07:00
|
|
|
|
|
2016-11-18 17:15:14 -05:00
|
|
|
|
// These values **must** match debuginfo::DIFlags! They also *happen*
|
|
|
|
|
// to match LLVM, but that isn't required as we do giant sets of
|
|
|
|
|
// matching below. The value shouldn't be directly passed to LLVM.
|
|
|
|
|
enum class LLVMRustDIFlags : uint32_t {
|
2016-12-30 12:22:11 +01:00
|
|
|
|
FlagZero = 0,
|
|
|
|
|
FlagPrivate = 1,
|
|
|
|
|
FlagProtected = 2,
|
|
|
|
|
FlagPublic = 3,
|
|
|
|
|
FlagFwdDecl = (1 << 2),
|
|
|
|
|
FlagAppleBlock = (1 << 3),
|
|
|
|
|
FlagBlockByrefStruct = (1 << 4),
|
|
|
|
|
FlagVirtual = (1 << 5),
|
|
|
|
|
FlagArtificial = (1 << 6),
|
|
|
|
|
FlagExplicit = (1 << 7),
|
|
|
|
|
FlagPrototyped = (1 << 8),
|
|
|
|
|
FlagObjcClassComplete = (1 << 9),
|
|
|
|
|
FlagObjectPointer = (1 << 10),
|
|
|
|
|
FlagVector = (1 << 11),
|
|
|
|
|
FlagStaticMember = (1 << 12),
|
|
|
|
|
FlagLValueReference = (1 << 13),
|
|
|
|
|
FlagRValueReference = (1 << 14),
|
2016-11-30 14:59:45 -07:00
|
|
|
|
FlagMainSubprogram = (1 << 21),
|
2016-12-30 12:22:11 +01:00
|
|
|
|
// Do not add values that are not supported by the minimum LLVM
|
|
|
|
|
// version we support!
|
2016-11-18 17:15:14 -05:00
|
|
|
|
};
|
|
|
|
|
|
2016-12-31 12:01:23 -05:00
|
|
|
|
inline LLVMRustDIFlags operator&(LLVMRustDIFlags A, LLVMRustDIFlags B) {
|
|
|
|
|
return static_cast<LLVMRustDIFlags>(static_cast<uint32_t>(A) &
|
|
|
|
|
static_cast<uint32_t>(B));
|
2016-11-18 17:15:14 -05:00
|
|
|
|
}
|
|
|
|
|
|
2016-12-31 12:01:23 -05:00
|
|
|
|
inline LLVMRustDIFlags operator|(LLVMRustDIFlags A, LLVMRustDIFlags B) {
|
|
|
|
|
return static_cast<LLVMRustDIFlags>(static_cast<uint32_t>(A) |
|
|
|
|
|
static_cast<uint32_t>(B));
|
2016-11-18 17:15:14 -05:00
|
|
|
|
}
|
|
|
|
|
|
2016-12-31 12:01:23 -05:00
|
|
|
|
inline LLVMRustDIFlags &operator|=(LLVMRustDIFlags &A, LLVMRustDIFlags B) {
|
|
|
|
|
return A = A | B;
|
2016-11-18 17:15:14 -05:00
|
|
|
|
}
|
|
|
|
|
|
2016-12-31 12:01:23 -05:00
|
|
|
|
inline bool isSet(LLVMRustDIFlags F) { return F != LLVMRustDIFlags::FlagZero; }
|
2016-11-18 17:15:14 -05:00
|
|
|
|
|
2016-12-31 12:01:23 -05:00
|
|
|
|
inline LLVMRustDIFlags visibility(LLVMRustDIFlags F) {
|
|
|
|
|
return static_cast<LLVMRustDIFlags>(static_cast<uint32_t>(F) & 0x3);
|
2016-11-18 17:15:14 -05:00
|
|
|
|
}
|
|
|
|
|
|
2016-11-18 16:22:39 -05:00
|
|
|
|
#if LLVM_VERSION_GE(4, 0)
|
2016-12-31 12:01:23 -05:00
|
|
|
|
static DINode::DIFlags fromRust(LLVMRustDIFlags Flags) {
|
|
|
|
|
DINode::DIFlags Result = DINode::DIFlags::FlagZero;
|
2016-11-18 16:22:39 -05:00
|
|
|
|
#else
|
2016-12-31 12:01:23 -05:00
|
|
|
|
static unsigned fromRust(LLVMRustDIFlags Flags) {
|
|
|
|
|
unsigned Result = 0;
|
2016-11-18 16:22:39 -05:00
|
|
|
|
#endif
|
2016-11-18 17:15:14 -05:00
|
|
|
|
|
2016-12-31 12:01:23 -05:00
|
|
|
|
switch (visibility(Flags)) {
|
2016-12-30 12:22:11 +01:00
|
|
|
|
case LLVMRustDIFlags::FlagPrivate:
|
2016-12-31 12:01:23 -05:00
|
|
|
|
Result |= DINode::DIFlags::FlagPrivate;
|
2016-12-30 12:22:11 +01:00
|
|
|
|
break;
|
|
|
|
|
case LLVMRustDIFlags::FlagProtected:
|
2016-12-31 12:01:23 -05:00
|
|
|
|
Result |= DINode::DIFlags::FlagProtected;
|
2016-12-30 12:22:11 +01:00
|
|
|
|
break;
|
|
|
|
|
case LLVMRustDIFlags::FlagPublic:
|
2016-12-31 12:01:23 -05:00
|
|
|
|
Result |= DINode::DIFlags::FlagPublic;
|
2016-12-30 12:22:11 +01:00
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
// The rest are handled below
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-31 12:01:23 -05:00
|
|
|
|
if (isSet(Flags & LLVMRustDIFlags::FlagFwdDecl)) {
|
|
|
|
|
Result |= DINode::DIFlags::FlagFwdDecl;
|
2016-12-30 12:22:11 +01:00
|
|
|
|
}
|
2016-12-31 12:01:23 -05:00
|
|
|
|
if (isSet(Flags & LLVMRustDIFlags::FlagAppleBlock)) {
|
|
|
|
|
Result |= DINode::DIFlags::FlagAppleBlock;
|
2016-12-30 12:22:11 +01:00
|
|
|
|
}
|
2016-12-31 12:01:23 -05:00
|
|
|
|
if (isSet(Flags & LLVMRustDIFlags::FlagBlockByrefStruct)) {
|
|
|
|
|
Result |= DINode::DIFlags::FlagBlockByrefStruct;
|
2016-12-30 12:22:11 +01:00
|
|
|
|
}
|
2016-12-31 12:01:23 -05:00
|
|
|
|
if (isSet(Flags & LLVMRustDIFlags::FlagVirtual)) {
|
|
|
|
|
Result |= DINode::DIFlags::FlagVirtual;
|
2016-12-30 12:22:11 +01:00
|
|
|
|
}
|
2016-12-31 12:01:23 -05:00
|
|
|
|
if (isSet(Flags & LLVMRustDIFlags::FlagArtificial)) {
|
|
|
|
|
Result |= DINode::DIFlags::FlagArtificial;
|
2016-12-30 12:22:11 +01:00
|
|
|
|
}
|
2016-12-31 12:01:23 -05:00
|
|
|
|
if (isSet(Flags & LLVMRustDIFlags::FlagExplicit)) {
|
|
|
|
|
Result |= DINode::DIFlags::FlagExplicit;
|
2016-12-30 12:22:11 +01:00
|
|
|
|
}
|
2016-12-31 12:01:23 -05:00
|
|
|
|
if (isSet(Flags & LLVMRustDIFlags::FlagPrototyped)) {
|
|
|
|
|
Result |= DINode::DIFlags::FlagPrototyped;
|
2016-12-30 12:22:11 +01:00
|
|
|
|
}
|
2016-12-31 12:01:23 -05:00
|
|
|
|
if (isSet(Flags & LLVMRustDIFlags::FlagObjcClassComplete)) {
|
|
|
|
|
Result |= DINode::DIFlags::FlagObjcClassComplete;
|
2016-12-30 12:22:11 +01:00
|
|
|
|
}
|
2016-12-31 12:01:23 -05:00
|
|
|
|
if (isSet(Flags & LLVMRustDIFlags::FlagObjectPointer)) {
|
|
|
|
|
Result |= DINode::DIFlags::FlagObjectPointer;
|
2016-12-30 12:22:11 +01:00
|
|
|
|
}
|
2016-12-31 12:01:23 -05:00
|
|
|
|
if (isSet(Flags & LLVMRustDIFlags::FlagVector)) {
|
|
|
|
|
Result |= DINode::DIFlags::FlagVector;
|
2016-12-30 12:22:11 +01:00
|
|
|
|
}
|
2016-12-31 12:01:23 -05:00
|
|
|
|
if (isSet(Flags & LLVMRustDIFlags::FlagStaticMember)) {
|
|
|
|
|
Result |= DINode::DIFlags::FlagStaticMember;
|
2016-12-30 12:22:11 +01:00
|
|
|
|
}
|
2016-12-31 12:01:23 -05:00
|
|
|
|
if (isSet(Flags & LLVMRustDIFlags::FlagLValueReference)) {
|
|
|
|
|
Result |= DINode::DIFlags::FlagLValueReference;
|
2016-12-30 12:22:11 +01:00
|
|
|
|
}
|
2016-12-31 12:01:23 -05:00
|
|
|
|
if (isSet(Flags & LLVMRustDIFlags::FlagRValueReference)) {
|
|
|
|
|
Result |= DINode::DIFlags::FlagRValueReference;
|
2016-12-30 12:22:11 +01:00
|
|
|
|
}
|
2016-11-30 14:59:45 -07:00
|
|
|
|
#if LLVM_RUSTLLVM || LLVM_VERSION_GE(4, 0)
|
|
|
|
|
if (isSet(Flags & LLVMRustDIFlags::FlagMainSubprogram)) {
|
|
|
|
|
Result |= DINode::DIFlags::FlagMainSubprogram;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2016-12-30 12:22:11 +01:00
|
|
|
|
|
2016-12-31 12:01:23 -05:00
|
|
|
|
return Result;
|
2016-11-18 17:15:14 -05:00
|
|
|
|
}
|
|
|
|
|
|
2015-05-30 09:50:12 -04:00
|
|
|
|
extern "C" uint32_t LLVMRustDebugMetadataVersion() {
|
2016-12-30 12:22:11 +01:00
|
|
|
|
return DEBUG_METADATA_VERSION;
|
2015-05-11 21:07:38 -07:00
|
|
|
|
}
|
2014-01-28 00:05:33 -05:00
|
|
|
|
|
2016-12-30 12:22:11 +01:00
|
|
|
|
extern "C" uint32_t LLVMRustVersionMinor() { return LLVM_VERSION_MINOR; }
|
2015-01-24 12:00:35 +01:00
|
|
|
|
|
2016-12-30 12:22:11 +01:00
|
|
|
|
extern "C" uint32_t LLVMRustVersionMajor() { return LLVM_VERSION_MAJOR; }
|
2015-01-24 12:00:35 +01:00
|
|
|
|
|
2016-12-31 12:01:23 -05:00
|
|
|
|
extern "C" void LLVMRustAddModuleFlag(LLVMModuleRef M, const char *Name,
|
|
|
|
|
uint32_t Value) {
|
|
|
|
|
unwrap(M)->addModuleFlag(Module::Warning, Name, Value);
|
2014-01-28 00:05:33 -05:00
|
|
|
|
}
|
|
|
|
|
|
2016-08-02 02:35:09 +03:00
|
|
|
|
extern "C" LLVMRustDIBuilderRef LLVMRustDIBuilderCreate(LLVMModuleRef M) {
|
2016-12-30 12:22:11 +01:00
|
|
|
|
return new DIBuilder(*unwrap(M));
|
2013-06-14 11:38:29 -07:00
|
|
|
|
}
|
|
|
|
|
|
2016-08-02 02:35:09 +03:00
|
|
|
|
extern "C" void LLVMRustDIBuilderDispose(LLVMRustDIBuilderRef Builder) {
|
2016-12-30 12:22:11 +01:00
|
|
|
|
delete Builder;
|
2013-06-14 11:38:29 -07:00
|
|
|
|
}
|
|
|
|
|
|
2016-08-02 02:35:09 +03:00
|
|
|
|
extern "C" void LLVMRustDIBuilderFinalize(LLVMRustDIBuilderRef Builder) {
|
2016-12-30 12:22:11 +01:00
|
|
|
|
Builder->finalize();
|
2013-06-14 11:38:29 -07:00
|
|
|
|
}
|
|
|
|
|
|
2016-08-02 02:35:09 +03:00
|
|
|
|
extern "C" LLVMRustMetadataRef LLVMRustDIBuilderCreateCompileUnit(
|
2017-02-11 15:01:25 -06:00
|
|
|
|
LLVMRustDIBuilderRef Builder, unsigned Lang, LLVMRustMetadataRef FileRef,
|
|
|
|
|
const char *Producer, bool isOptimized, const char *Flags,
|
2016-12-30 12:22:11 +01:00
|
|
|
|
unsigned RuntimeVer, const char *SplitName) {
|
2017-02-11 15:01:25 -06:00
|
|
|
|
auto *File = unwrapDI<DIFile>(FileRef);
|
|
|
|
|
|
|
|
|
|
#if LLVM_VERSION_GE(4, 0)
|
|
|
|
|
return wrap(Builder->createCompileUnit(Lang, File, Producer, isOptimized,
|
2016-12-30 12:22:11 +01:00
|
|
|
|
Flags, RuntimeVer, SplitName));
|
2017-02-11 15:01:25 -06:00
|
|
|
|
#else
|
|
|
|
|
return wrap(Builder->createCompileUnit(Lang, File->getFilename(),
|
|
|
|
|
File->getDirectory(), Producer, isOptimized,
|
|
|
|
|
Flags, RuntimeVer, SplitName));
|
|
|
|
|
#endif
|
2016-12-30 12:22:11 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extern "C" LLVMRustMetadataRef
|
|
|
|
|
LLVMRustDIBuilderCreateFile(LLVMRustDIBuilderRef Builder, const char *Filename,
|
|
|
|
|
const char *Directory) {
|
|
|
|
|
return wrap(Builder->createFile(Filename, Directory));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extern "C" LLVMRustMetadataRef
|
|
|
|
|
LLVMRustDIBuilderCreateSubroutineType(LLVMRustDIBuilderRef Builder,
|
|
|
|
|
LLVMRustMetadataRef File,
|
|
|
|
|
LLVMRustMetadataRef ParameterTypes) {
|
|
|
|
|
return wrap(Builder->createSubroutineType(
|
2016-09-24 10:44:21 -04:00
|
|
|
|
#if LLVM_VERSION_EQ(3, 7)
|
2016-12-30 12:22:11 +01:00
|
|
|
|
unwrapDI<DIFile>(File),
|
2015-10-24 18:42:23 +09:00
|
|
|
|
#endif
|
2016-12-30 12:22:11 +01:00
|
|
|
|
DITypeRefArray(unwrap<MDTuple>(ParameterTypes))));
|
2013-06-14 11:38:29 -07:00
|
|
|
|
}
|
|
|
|
|
|
2016-08-02 02:35:09 +03:00
|
|
|
|
extern "C" LLVMRustMetadataRef LLVMRustDIBuilderCreateFunction(
|
2016-12-30 12:22:11 +01:00
|
|
|
|
LLVMRustDIBuilderRef Builder, LLVMRustMetadataRef Scope, const char *Name,
|
|
|
|
|
const char *LinkageName, LLVMRustMetadataRef File, unsigned LineNo,
|
2016-12-31 12:01:23 -05:00
|
|
|
|
LLVMRustMetadataRef Ty, bool IsLocalToUnit, bool IsDefinition,
|
|
|
|
|
unsigned ScopeLine, LLVMRustDIFlags Flags, bool IsOptimized,
|
2016-12-30 12:22:11 +01:00
|
|
|
|
LLVMValueRef Fn, LLVMRustMetadataRef TParam, LLVMRustMetadataRef Decl) {
|
2016-09-24 10:44:21 -04:00
|
|
|
|
#if LLVM_VERSION_GE(3, 8)
|
2016-12-30 12:22:11 +01:00
|
|
|
|
DITemplateParameterArray TParams =
|
|
|
|
|
DITemplateParameterArray(unwrap<MDTuple>(TParam));
|
|
|
|
|
DISubprogram *Sub = Builder->createFunction(
|
|
|
|
|
unwrapDI<DIScope>(Scope), Name, LinkageName, unwrapDI<DIFile>(File),
|
2016-12-31 12:01:23 -05:00
|
|
|
|
LineNo, unwrapDI<DISubroutineType>(Ty), IsLocalToUnit, IsDefinition,
|
|
|
|
|
ScopeLine, fromRust(Flags), IsOptimized, TParams,
|
|
|
|
|
unwrapDIPtr<DISubprogram>(Decl));
|
2016-12-30 12:22:11 +01:00
|
|
|
|
unwrap<Function>(Fn)->setSubprogram(Sub);
|
|
|
|
|
return wrap(Sub);
|
2015-10-22 22:07:19 -07:00
|
|
|
|
#else
|
2016-12-30 12:22:11 +01:00
|
|
|
|
return wrap(Builder->createFunction(
|
|
|
|
|
unwrapDI<DIScope>(Scope), Name, LinkageName, unwrapDI<DIFile>(File),
|
2016-12-31 12:01:23 -05:00
|
|
|
|
LineNo, unwrapDI<DISubroutineType>(Ty), IsLocalToUnit, IsDefinition,
|
|
|
|
|
ScopeLine, fromRust(Flags), IsOptimized, unwrap<Function>(Fn),
|
|
|
|
|
unwrapDIPtr<MDNode>(TParam), unwrapDIPtr<MDNode>(Decl)));
|
2015-10-22 22:07:19 -07:00
|
|
|
|
#endif
|
2013-06-14 11:38:29 -07:00
|
|
|
|
}
|
|
|
|
|
|
2016-12-30 12:22:11 +01:00
|
|
|
|
extern "C" LLVMRustMetadataRef
|
|
|
|
|
LLVMRustDIBuilderCreateBasicType(LLVMRustDIBuilderRef Builder, const char *Name,
|
2017-02-04 23:51:10 +13:00
|
|
|
|
uint64_t SizeInBits, uint32_t AlignInBits,
|
2016-12-30 12:22:11 +01:00
|
|
|
|
unsigned Encoding) {
|
|
|
|
|
return wrap(Builder->createBasicType(Name, SizeInBits,
|
2016-11-18 11:11:18 -05:00
|
|
|
|
#if LLVM_VERSION_LE(3, 9)
|
2016-12-30 12:22:11 +01:00
|
|
|
|
AlignInBits,
|
2016-11-18 11:11:18 -05:00
|
|
|
|
#endif
|
2016-12-30 12:22:11 +01:00
|
|
|
|
Encoding));
|
2013-06-14 11:38:29 -07:00
|
|
|
|
}
|
2013-07-02 18:10:24 +02:00
|
|
|
|
|
2016-08-02 02:35:09 +03:00
|
|
|
|
extern "C" LLVMRustMetadataRef LLVMRustDIBuilderCreatePointerType(
|
2016-12-30 12:22:11 +01:00
|
|
|
|
LLVMRustDIBuilderRef Builder, LLVMRustMetadataRef PointeeTy,
|
2017-02-04 23:51:10 +13:00
|
|
|
|
uint64_t SizeInBits, uint32_t AlignInBits, const char *Name) {
|
2016-12-30 12:22:11 +01:00
|
|
|
|
return wrap(Builder->createPointerType(unwrapDI<DIType>(PointeeTy),
|
|
|
|
|
SizeInBits, AlignInBits, Name));
|
2013-06-14 11:38:29 -07:00
|
|
|
|
}
|
|
|
|
|
|
2016-08-02 02:35:09 +03:00
|
|
|
|
extern "C" LLVMRustMetadataRef LLVMRustDIBuilderCreateStructType(
|
2016-12-30 12:22:11 +01:00
|
|
|
|
LLVMRustDIBuilderRef Builder, LLVMRustMetadataRef Scope, const char *Name,
|
|
|
|
|
LLVMRustMetadataRef File, unsigned LineNumber, uint64_t SizeInBits,
|
2017-02-04 23:51:10 +13:00
|
|
|
|
uint32_t AlignInBits, LLVMRustDIFlags Flags,
|
2016-12-30 12:22:11 +01:00
|
|
|
|
LLVMRustMetadataRef DerivedFrom, LLVMRustMetadataRef Elements,
|
|
|
|
|
unsigned RunTimeLang, LLVMRustMetadataRef VTableHolder,
|
2013-10-01 12:24:50 +02:00
|
|
|
|
const char *UniqueId) {
|
2016-12-30 12:22:11 +01:00
|
|
|
|
return wrap(Builder->createStructType(
|
|
|
|
|
unwrapDI<DIDescriptor>(Scope), Name, unwrapDI<DIFile>(File), LineNumber,
|
2016-12-31 12:01:23 -05:00
|
|
|
|
SizeInBits, AlignInBits, fromRust(Flags), unwrapDI<DIType>(DerivedFrom),
|
2016-12-30 12:22:11 +01:00
|
|
|
|
DINodeArray(unwrapDI<MDTuple>(Elements)), RunTimeLang,
|
|
|
|
|
unwrapDI<DIType>(VTableHolder), UniqueId));
|
2013-06-14 11:38:29 -07:00
|
|
|
|
}
|
|
|
|
|
|
2016-08-02 02:35:09 +03:00
|
|
|
|
extern "C" LLVMRustMetadataRef LLVMRustDIBuilderCreateMemberType(
|
2016-12-30 12:22:11 +01:00
|
|
|
|
LLVMRustDIBuilderRef Builder, LLVMRustMetadataRef Scope, const char *Name,
|
|
|
|
|
LLVMRustMetadataRef File, unsigned LineNo, uint64_t SizeInBits,
|
2017-02-04 23:51:10 +13:00
|
|
|
|
uint32_t AlignInBits, uint64_t OffsetInBits, LLVMRustDIFlags Flags,
|
2016-08-02 02:35:09 +03:00
|
|
|
|
LLVMRustMetadataRef Ty) {
|
2016-12-31 12:01:23 -05:00
|
|
|
|
return wrap(Builder->createMemberType(unwrapDI<DIDescriptor>(Scope), Name,
|
|
|
|
|
unwrapDI<DIFile>(File), LineNo,
|
|
|
|
|
SizeInBits, AlignInBits, OffsetInBits,
|
|
|
|
|
fromRust(Flags), unwrapDI<DIType>(Ty)));
|
2013-06-14 11:38:29 -07:00
|
|
|
|
}
|
2013-07-02 18:10:24 +02:00
|
|
|
|
|
2016-08-02 02:35:09 +03:00
|
|
|
|
extern "C" LLVMRustMetadataRef LLVMRustDIBuilderCreateLexicalBlock(
|
2016-12-30 12:22:11 +01:00
|
|
|
|
LLVMRustDIBuilderRef Builder, LLVMRustMetadataRef Scope,
|
|
|
|
|
LLVMRustMetadataRef File, unsigned Line, unsigned Col) {
|
|
|
|
|
return wrap(Builder->createLexicalBlock(unwrapDI<DIDescriptor>(Scope),
|
|
|
|
|
unwrapDI<DIFile>(File), Line, Col));
|
2013-06-14 11:38:29 -07:00
|
|
|
|
}
|
2013-07-02 18:10:24 +02:00
|
|
|
|
|
2016-12-30 12:22:11 +01:00
|
|
|
|
extern "C" LLVMRustMetadataRef
|
|
|
|
|
LLVMRustDIBuilderCreateLexicalBlockFile(LLVMRustDIBuilderRef Builder,
|
|
|
|
|
LLVMRustMetadataRef Scope,
|
|
|
|
|
LLVMRustMetadataRef File) {
|
|
|
|
|
return wrap(Builder->createLexicalBlockFile(unwrapDI<DIDescriptor>(Scope),
|
|
|
|
|
unwrapDI<DIFile>(File)));
|
2016-08-24 19:34:31 -07:00
|
|
|
|
}
|
|
|
|
|
|
2016-08-02 02:35:09 +03:00
|
|
|
|
extern "C" LLVMRustMetadataRef LLVMRustDIBuilderCreateStaticVariable(
|
2016-12-30 12:22:11 +01:00
|
|
|
|
LLVMRustDIBuilderRef Builder, LLVMRustMetadataRef Context, const char *Name,
|
|
|
|
|
const char *LinkageName, LLVMRustMetadataRef File, unsigned LineNo,
|
2016-12-31 12:01:23 -05:00
|
|
|
|
LLVMRustMetadataRef Ty, bool IsLocalToUnit, LLVMValueRef V,
|
2017-02-04 23:51:10 +13:00
|
|
|
|
LLVMRustMetadataRef Decl = nullptr, uint32_t AlignInBits = 0) {
|
2017-03-16 21:10:04 +01:00
|
|
|
|
llvm::GlobalVariable *InitVal = cast<llvm::GlobalVariable>(unwrap(V));
|
2016-12-11 22:08:20 +13:00
|
|
|
|
|
|
|
|
|
#if LLVM_VERSION_GE(4, 0)
|
2016-12-30 12:22:11 +01:00
|
|
|
|
llvm::DIExpression *InitExpr = nullptr;
|
|
|
|
|
if (llvm::ConstantInt *IntVal = llvm::dyn_cast<llvm::ConstantInt>(InitVal)) {
|
|
|
|
|
InitExpr = Builder->createConstantValueExpression(
|
|
|
|
|
IntVal->getValue().getSExtValue());
|
|
|
|
|
} else if (llvm::ConstantFP *FPVal =
|
|
|
|
|
llvm::dyn_cast<llvm::ConstantFP>(InitVal)) {
|
|
|
|
|
InitExpr = Builder->createConstantValueExpression(
|
|
|
|
|
FPVal->getValueAPF().bitcastToAPInt().getZExtValue());
|
|
|
|
|
}
|
2016-12-11 22:08:20 +13:00
|
|
|
|
|
2017-03-16 21:10:04 +01:00
|
|
|
|
llvm::DIGlobalVariableExpression *VarExpr = Builder->createGlobalVariableExpression(
|
2016-12-30 12:22:11 +01:00
|
|
|
|
unwrapDI<DIDescriptor>(Context), Name, LinkageName,
|
2016-12-31 12:01:23 -05:00
|
|
|
|
unwrapDI<DIFile>(File), LineNo, unwrapDI<DIType>(Ty), IsLocalToUnit,
|
2017-03-16 21:10:04 +01:00
|
|
|
|
InitExpr, unwrapDIPtr<MDNode>(Decl), AlignInBits);
|
|
|
|
|
|
|
|
|
|
InitVal->setMetadata("dbg", VarExpr);
|
|
|
|
|
|
|
|
|
|
return wrap(VarExpr);
|
2016-12-11 22:08:20 +13:00
|
|
|
|
#else
|
2017-03-16 21:01:05 +01:00
|
|
|
|
return wrap(Builder->createGlobalVariable(
|
|
|
|
|
unwrapDI<DIDescriptor>(Context), Name, LinkageName,
|
|
|
|
|
unwrapDI<DIFile>(File), LineNo, unwrapDI<DIType>(Ty), IsLocalToUnit,
|
2017-03-16 21:03:22 +01:00
|
|
|
|
InitVal, unwrapDIPtr<MDNode>(Decl)));
|
2016-12-11 22:08:20 +13:00
|
|
|
|
#endif
|
2014-02-20 20:44:29 -05:00
|
|
|
|
}
|
|
|
|
|
|
2016-08-02 02:35:09 +03:00
|
|
|
|
extern "C" LLVMRustMetadataRef LLVMRustDIBuilderCreateVariable(
|
2016-12-30 12:22:11 +01:00
|
|
|
|
LLVMRustDIBuilderRef Builder, unsigned Tag, LLVMRustMetadataRef Scope,
|
|
|
|
|
const char *Name, LLVMRustMetadataRef File, unsigned LineNo,
|
|
|
|
|
LLVMRustMetadataRef Ty, bool AlwaysPreserve, LLVMRustDIFlags Flags,
|
2017-02-04 23:51:10 +13:00
|
|
|
|
unsigned ArgNo, uint32_t AlignInBits) {
|
2016-09-24 10:44:21 -04:00
|
|
|
|
#if LLVM_VERSION_GE(3, 8)
|
2016-12-30 12:22:11 +01:00
|
|
|
|
if (Tag == 0x100) { // DW_TAG_auto_variable
|
|
|
|
|
return wrap(Builder->createAutoVariable(
|
|
|
|
|
unwrapDI<DIDescriptor>(Scope), Name, unwrapDI<DIFile>(File), LineNo,
|
2016-12-31 12:01:23 -05:00
|
|
|
|
unwrapDI<DIType>(Ty), AlwaysPreserve, fromRust(Flags)
|
2016-12-30 12:22:11 +01:00
|
|
|
|
#if LLVM_VERSION_GE(4, 0)
|
2016-12-31 12:01:23 -05:00
|
|
|
|
,
|
|
|
|
|
AlignInBits
|
2016-11-18 11:11:18 -05:00
|
|
|
|
#endif
|
|
|
|
|
));
|
2016-12-30 12:22:11 +01:00
|
|
|
|
} else {
|
|
|
|
|
return wrap(Builder->createParameterVariable(
|
|
|
|
|
unwrapDI<DIDescriptor>(Scope), Name, ArgNo, unwrapDI<DIFile>(File),
|
2016-12-31 12:01:23 -05:00
|
|
|
|
LineNo, unwrapDI<DIType>(Ty), AlwaysPreserve, fromRust(Flags)));
|
2016-12-30 12:22:11 +01:00
|
|
|
|
}
|
2015-10-24 18:42:23 +09:00
|
|
|
|
#else
|
2016-12-30 12:22:11 +01:00
|
|
|
|
return wrap(Builder->createLocalVariable(
|
|
|
|
|
Tag, unwrapDI<DIDescriptor>(Scope), Name, unwrapDI<DIFile>(File), LineNo,
|
2016-12-31 12:01:23 -05:00
|
|
|
|
unwrapDI<DIType>(Ty), AlwaysPreserve, fromRust(Flags), ArgNo));
|
2015-10-24 18:42:23 +09:00
|
|
|
|
#endif
|
2013-06-14 11:38:29 -07:00
|
|
|
|
}
|
|
|
|
|
|
2016-12-30 12:22:11 +01:00
|
|
|
|
extern "C" LLVMRustMetadataRef
|
|
|
|
|
LLVMRustDIBuilderCreateArrayType(LLVMRustDIBuilderRef Builder, uint64_t Size,
|
2017-02-04 23:51:10 +13:00
|
|
|
|
uint32_t AlignInBits, LLVMRustMetadataRef Ty,
|
2016-12-30 12:22:11 +01:00
|
|
|
|
LLVMRustMetadataRef Subscripts) {
|
|
|
|
|
return wrap(
|
|
|
|
|
Builder->createArrayType(Size, AlignInBits, unwrapDI<DIType>(Ty),
|
|
|
|
|
DINodeArray(unwrapDI<MDTuple>(Subscripts))));
|
2013-06-14 12:23:42 -07:00
|
|
|
|
}
|
|
|
|
|
|
2016-12-30 12:22:11 +01:00
|
|
|
|
extern "C" LLVMRustMetadataRef
|
|
|
|
|
LLVMRustDIBuilderCreateVectorType(LLVMRustDIBuilderRef Builder, uint64_t Size,
|
2017-02-04 23:51:10 +13:00
|
|
|
|
uint32_t AlignInBits, LLVMRustMetadataRef Ty,
|
2016-12-30 12:22:11 +01:00
|
|
|
|
LLVMRustMetadataRef Subscripts) {
|
|
|
|
|
return wrap(
|
|
|
|
|
Builder->createVectorType(Size, AlignInBits, unwrapDI<DIType>(Ty),
|
|
|
|
|
DINodeArray(unwrapDI<MDTuple>(Subscripts))));
|
2013-06-14 11:38:29 -07:00
|
|
|
|
}
|
|
|
|
|
|
2016-12-30 12:22:11 +01:00
|
|
|
|
extern "C" LLVMRustMetadataRef
|
|
|
|
|
LLVMRustDIBuilderGetOrCreateSubrange(LLVMRustDIBuilderRef Builder, int64_t Lo,
|
|
|
|
|
int64_t Count) {
|
|
|
|
|
return wrap(Builder->getOrCreateSubrange(Lo, Count));
|
2013-06-14 11:38:29 -07:00
|
|
|
|
}
|
|
|
|
|
|
2016-12-30 12:22:11 +01:00
|
|
|
|
extern "C" LLVMRustMetadataRef
|
|
|
|
|
LLVMRustDIBuilderGetOrCreateArray(LLVMRustDIBuilderRef Builder,
|
|
|
|
|
LLVMRustMetadataRef *Ptr, unsigned Count) {
|
|
|
|
|
Metadata **DataValue = unwrap(Ptr);
|
|
|
|
|
return wrap(
|
|
|
|
|
Builder->getOrCreateArray(ArrayRef<Metadata *>(DataValue, Count)).get());
|
2013-06-14 11:38:29 -07:00
|
|
|
|
}
|
|
|
|
|
|
2016-08-02 02:35:09 +03:00
|
|
|
|
extern "C" LLVMValueRef LLVMRustDIBuilderInsertDeclareAtEnd(
|
2016-12-31 12:01:23 -05:00
|
|
|
|
LLVMRustDIBuilderRef Builder, LLVMValueRef V, LLVMRustMetadataRef VarInfo,
|
2016-12-30 12:22:11 +01:00
|
|
|
|
int64_t *AddrOps, unsigned AddrOpsCount, LLVMValueRef DL,
|
2013-06-11 00:57:25 -07:00
|
|
|
|
LLVMBasicBlockRef InsertAtEnd) {
|
2016-12-30 12:22:11 +01:00
|
|
|
|
return wrap(Builder->insertDeclare(
|
2016-12-31 12:01:23 -05:00
|
|
|
|
unwrap(V), unwrap<DILocalVariable>(VarInfo),
|
2016-12-30 12:22:11 +01:00
|
|
|
|
Builder->createExpression(llvm::ArrayRef<int64_t>(AddrOps, AddrOpsCount)),
|
|
|
|
|
DebugLoc(cast<MDNode>(unwrap<MetadataAsValue>(DL)->getMetadata())),
|
|
|
|
|
unwrap(InsertAtEnd)));
|
2013-06-11 00:57:25 -07:00
|
|
|
|
}
|
|
|
|
|
|
2016-12-30 12:22:11 +01:00
|
|
|
|
extern "C" LLVMRustMetadataRef
|
|
|
|
|
LLVMRustDIBuilderCreateEnumerator(LLVMRustDIBuilderRef Builder,
|
|
|
|
|
const char *Name, uint64_t Val) {
|
|
|
|
|
return wrap(Builder->createEnumerator(Name, Val));
|
2013-07-02 10:33:51 +02:00
|
|
|
|
}
|
|
|
|
|
|
2016-08-02 02:35:09 +03:00
|
|
|
|
extern "C" LLVMRustMetadataRef LLVMRustDIBuilderCreateEnumerationType(
|
2016-12-30 12:22:11 +01:00
|
|
|
|
LLVMRustDIBuilderRef Builder, LLVMRustMetadataRef Scope, const char *Name,
|
|
|
|
|
LLVMRustMetadataRef File, unsigned LineNumber, uint64_t SizeInBits,
|
2017-02-04 23:51:10 +13:00
|
|
|
|
uint32_t AlignInBits, LLVMRustMetadataRef Elements,
|
2016-12-31 12:01:23 -05:00
|
|
|
|
LLVMRustMetadataRef ClassTy) {
|
2016-12-30 12:22:11 +01:00
|
|
|
|
return wrap(Builder->createEnumerationType(
|
|
|
|
|
unwrapDI<DIDescriptor>(Scope), Name, unwrapDI<DIFile>(File), LineNumber,
|
|
|
|
|
SizeInBits, AlignInBits, DINodeArray(unwrapDI<MDTuple>(Elements)),
|
2016-12-31 12:01:23 -05:00
|
|
|
|
unwrapDI<DIType>(ClassTy)));
|
2013-07-02 18:10:24 +02:00
|
|
|
|
}
|
|
|
|
|
|
2016-08-02 02:35:09 +03:00
|
|
|
|
extern "C" LLVMRustMetadataRef LLVMRustDIBuilderCreateUnionType(
|
2016-12-30 12:22:11 +01:00
|
|
|
|
LLVMRustDIBuilderRef Builder, LLVMRustMetadataRef Scope, const char *Name,
|
|
|
|
|
LLVMRustMetadataRef File, unsigned LineNumber, uint64_t SizeInBits,
|
2017-02-04 23:51:10 +13:00
|
|
|
|
uint32_t AlignInBits, LLVMRustDIFlags Flags, LLVMRustMetadataRef Elements,
|
2016-12-30 12:22:11 +01:00
|
|
|
|
unsigned RunTimeLang, const char *UniqueId) {
|
|
|
|
|
return wrap(Builder->createUnionType(
|
|
|
|
|
unwrapDI<DIDescriptor>(Scope), Name, unwrapDI<DIFile>(File), LineNumber,
|
2016-12-31 12:01:23 -05:00
|
|
|
|
SizeInBits, AlignInBits, fromRust(Flags),
|
2016-12-30 12:22:11 +01:00
|
|
|
|
DINodeArray(unwrapDI<MDTuple>(Elements)), RunTimeLang, UniqueId));
|
2013-07-28 19:48:16 +12:00
|
|
|
|
}
|
2013-08-09 13:47:00 -07:00
|
|
|
|
|
2016-08-02 02:35:09 +03:00
|
|
|
|
extern "C" LLVMRustMetadataRef LLVMRustDIBuilderCreateTemplateTypeParameter(
|
2016-12-30 12:22:11 +01:00
|
|
|
|
LLVMRustDIBuilderRef Builder, LLVMRustMetadataRef Scope, const char *Name,
|
|
|
|
|
LLVMRustMetadataRef Ty, LLVMRustMetadataRef File, unsigned LineNo,
|
|
|
|
|
unsigned ColumnNo) {
|
|
|
|
|
return wrap(Builder->createTemplateTypeParameter(
|
|
|
|
|
unwrapDI<DIDescriptor>(Scope), Name, unwrapDI<DIType>(Ty)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extern "C" LLVMRustMetadataRef
|
|
|
|
|
LLVMRustDIBuilderCreateNameSpace(LLVMRustDIBuilderRef Builder,
|
|
|
|
|
LLVMRustMetadataRef Scope, const char *Name,
|
|
|
|
|
LLVMRustMetadataRef File, unsigned LineNo) {
|
|
|
|
|
return wrap(Builder->createNameSpace(
|
|
|
|
|
unwrapDI<DIDescriptor>(Scope), Name, unwrapDI<DIFile>(File), LineNo
|
2016-11-25 17:23:25 +01:00
|
|
|
|
#if LLVM_VERSION_GE(4, 0)
|
2016-12-30 12:22:11 +01:00
|
|
|
|
,
|
|
|
|
|
false // ExportSymbols (only relevant for C++ anonymous namespaces)
|
2016-11-25 17:23:25 +01:00
|
|
|
|
#endif
|
2016-12-30 12:22:11 +01:00
|
|
|
|
));
|
2013-09-05 13:29:30 +02:00
|
|
|
|
}
|
2013-09-11 16:37:43 +02:00
|
|
|
|
|
2016-12-30 12:22:11 +01:00
|
|
|
|
extern "C" void
|
|
|
|
|
LLVMRustDICompositeTypeSetTypeArray(LLVMRustDIBuilderRef Builder,
|
2016-12-31 12:01:23 -05:00
|
|
|
|
LLVMRustMetadataRef CompositeTy,
|
|
|
|
|
LLVMRustMetadataRef TyArray) {
|
|
|
|
|
DICompositeType *Tmp = unwrapDI<DICompositeType>(CompositeTy);
|
|
|
|
|
Builder->replaceArrays(Tmp, DINodeArray(unwrap<MDTuple>(TyArray)));
|
2013-09-11 16:37:43 +02:00
|
|
|
|
}
|
2013-10-11 19:56:11 -04:00
|
|
|
|
|
2016-12-30 12:22:11 +01:00
|
|
|
|
extern "C" LLVMValueRef
|
2016-12-31 12:01:23 -05:00
|
|
|
|
LLVMRustDIBuilderCreateDebugLocation(LLVMContextRef ContextRef, unsigned Line,
|
2016-12-30 12:22:11 +01:00
|
|
|
|
unsigned Column, LLVMRustMetadataRef Scope,
|
|
|
|
|
LLVMRustMetadataRef InlinedAt) {
|
2016-12-31 12:01:23 -05:00
|
|
|
|
LLVMContext &Context = *unwrap(ContextRef);
|
2015-01-30 19:25:07 +01:00
|
|
|
|
|
2016-12-31 12:01:23 -05:00
|
|
|
|
DebugLoc debug_loc = DebugLoc::get(Line, Column, unwrapDIPtr<MDNode>(Scope),
|
|
|
|
|
unwrapDIPtr<MDNode>(InlinedAt));
|
2015-01-30 19:25:07 +01:00
|
|
|
|
|
2016-12-31 12:01:23 -05:00
|
|
|
|
return wrap(MetadataAsValue::get(Context, debug_loc.getAsMDNode()));
|
2015-01-30 19:25:07 +01:00
|
|
|
|
}
|
|
|
|
|
|
2016-12-30 12:22:11 +01:00
|
|
|
|
extern "C" int64_t LLVMRustDIBuilderCreateOpDeref() {
|
|
|
|
|
return dwarf::DW_OP_deref;
|
2016-08-02 02:35:09 +03:00
|
|
|
|
}
|
|
|
|
|
|
2016-12-30 12:22:11 +01:00
|
|
|
|
extern "C" int64_t LLVMRustDIBuilderCreateOpPlus() { return dwarf::DW_OP_plus; }
|
2016-08-02 02:35:09 +03:00
|
|
|
|
|
2016-12-31 12:01:23 -05:00
|
|
|
|
extern "C" void LLVMRustWriteTypeToString(LLVMTypeRef Ty, RustStringRef Str) {
|
|
|
|
|
RawRustStringOstream OS(Str);
|
|
|
|
|
unwrap<llvm::Type>(Ty)->print(OS);
|
2013-10-11 19:56:11 -04: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-02 23:19:29 -08:00
|
|
|
|
|
2016-12-31 12:01:23 -05:00
|
|
|
|
extern "C" void LLVMRustWriteValueToString(LLVMValueRef V,
|
|
|
|
|
RustStringRef Str) {
|
|
|
|
|
RawRustStringOstream OS(Str);
|
|
|
|
|
OS << "(";
|
|
|
|
|
unwrap<llvm::Value>(V)->getType()->print(OS);
|
|
|
|
|
OS << ":";
|
|
|
|
|
unwrap<llvm::Value>(V)->print(OS);
|
|
|
|
|
OS << ")";
|
2014-01-15 14:39:08 -05:00
|
|
|
|
}
|
|
|
|
|
|
2016-12-31 12:01:23 -05:00
|
|
|
|
extern "C" bool LLVMRustLinkInExternalBitcode(LLVMModuleRef DstRef, char *BC,
|
|
|
|
|
size_t Len) {
|
|
|
|
|
Module *Dst = unwrap(DstRef);
|
2016-11-17 09:10:19 -05:00
|
|
|
|
|
2016-12-31 12:01:23 -05:00
|
|
|
|
std::unique_ptr<MemoryBuffer> Buf =
|
|
|
|
|
MemoryBuffer::getMemBufferCopy(StringRef(BC, Len));
|
2016-11-17 09:10:19 -05:00
|
|
|
|
|
|
|
|
|
#if LLVM_VERSION_GE(4, 0)
|
2016-12-30 12:22:11 +01:00
|
|
|
|
Expected<std::unique_ptr<Module>> SrcOrError =
|
2016-12-31 12:01:23 -05:00
|
|
|
|
llvm::getLazyBitcodeModule(Buf->getMemBufferRef(), Dst->getContext());
|
2016-12-30 12:22:11 +01:00
|
|
|
|
if (!SrcOrError) {
|
|
|
|
|
LLVMRustSetLastError(toString(SrcOrError.takeError()).c_str());
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto Src = std::move(*SrcOrError);
|
2016-11-17 09:10:19 -05:00
|
|
|
|
#else
|
2016-12-30 12:22:11 +01:00
|
|
|
|
ErrorOr<std::unique_ptr<Module>> Src =
|
2016-12-31 12:01:23 -05:00
|
|
|
|
llvm::getLazyBitcodeModule(std::move(Buf), Dst->getContext());
|
2016-12-30 12:22:11 +01:00
|
|
|
|
if (!Src) {
|
|
|
|
|
LLVMRustSetLastError(Src.getError().message().c_str());
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2016-11-17 09:10:19 -05:00
|
|
|
|
#endif
|
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-02 23:19:29 -08:00
|
|
|
|
|
2016-12-30 12:22:11 +01:00
|
|
|
|
std::string Err;
|
2015-01-30 19:25:07 +01:00
|
|
|
|
|
2016-12-30 12:22:11 +01:00
|
|
|
|
raw_string_ostream Stream(Err);
|
|
|
|
|
DiagnosticPrinterRawOStream DP(Stream);
|
2016-11-17 09:10:19 -05:00
|
|
|
|
#if LLVM_VERSION_GE(4, 0)
|
2016-12-30 12:22:11 +01:00
|
|
|
|
if (Linker::linkModules(*Dst, std::move(Src))) {
|
2016-11-17 09:10:19 -05:00
|
|
|
|
#elif LLVM_VERSION_GE(3, 8)
|
2016-12-30 12:22:11 +01:00
|
|
|
|
if (Linker::linkModules(*Dst, std::move(Src.get()))) {
|
2015-01-30 19:25:07 +01:00
|
|
|
|
#else
|
2016-12-30 12:22:11 +01:00
|
|
|
|
if (Linker::LinkModules(Dst, Src->get(),
|
|
|
|
|
[&](const DiagnosticInfo &DI) { DI.print(DP); })) {
|
2015-01-30 19:25:07 +01:00
|
|
|
|
#endif
|
2016-12-30 12:22:11 +01:00
|
|
|
|
LLVMRustSetLastError(Err.c_str());
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
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-02 23:19:29 -08:00
|
|
|
|
}
|
2013-12-16 20:58:21 -08:00
|
|
|
|
|
2014-04-03 10:45:36 -07:00
|
|
|
|
// Note that the two following functions look quite similar to the
|
|
|
|
|
// LLVMGetSectionName function. Sadly, it appears that this function only
|
|
|
|
|
// returns a char* pointer, which isn't guaranteed to be null-terminated. The
|
|
|
|
|
// function provided by LLVM doesn't return the length, so we've created our own
|
|
|
|
|
// function which returns the length as well as the data pointer.
|
|
|
|
|
//
|
|
|
|
|
// For an example of this not returning a null terminated string, see
|
|
|
|
|
// lib/Object/COFFObjectFile.cpp in the getSectionName function. One of the
|
|
|
|
|
// branches explicitly creates a StringRef without a null terminator, and then
|
|
|
|
|
// that's returned.
|
|
|
|
|
|
|
|
|
|
inline section_iterator *unwrap(LLVMSectionIteratorRef SI) {
|
2016-12-30 12:22:11 +01:00
|
|
|
|
return reinterpret_cast<section_iterator *>(SI);
|
2014-04-03 10:45:36 -07:00
|
|
|
|
}
|
|
|
|
|
|
2016-12-30 12:22:11 +01:00
|
|
|
|
extern "C" size_t LLVMRustGetSectionName(LLVMSectionIteratorRef SI,
|
2016-12-31 12:01:23 -05:00
|
|
|
|
const char **Ptr) {
|
|
|
|
|
StringRef Ret;
|
|
|
|
|
if (std::error_code EC = (*unwrap(SI))->getName(Ret))
|
|
|
|
|
report_fatal_error(EC.message());
|
|
|
|
|
*Ptr = Ret.data();
|
|
|
|
|
return Ret.size();
|
2014-04-03 10:45:36 -07:00
|
|
|
|
}
|
2014-05-10 17:30:55 +09:00
|
|
|
|
|
|
|
|
|
// LLVMArrayType function does not support 64-bit ElementCount
|
2016-12-31 12:01:23 -05:00
|
|
|
|
extern "C" LLVMTypeRef LLVMRustArrayType(LLVMTypeRef ElementTy,
|
2016-12-30 12:22:11 +01:00
|
|
|
|
uint64_t ElementCount) {
|
2016-12-31 12:01:23 -05:00
|
|
|
|
return wrap(ArrayType::get(unwrap(ElementTy), ElementCount));
|
2014-05-10 17:30:55 +09:00
|
|
|
|
}
|
2014-09-12 08:17:58 -07:00
|
|
|
|
|
|
|
|
|
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(Twine, LLVMTwineRef)
|
|
|
|
|
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(DebugLoc, LLVMDebugLocRef)
|
|
|
|
|
|
2016-12-31 12:01:23 -05:00
|
|
|
|
extern "C" void LLVMRustWriteTwineToString(LLVMTwineRef T, RustStringRef Str) {
|
|
|
|
|
RawRustStringOstream OS(Str);
|
|
|
|
|
unwrap(T)->print(OS);
|
2014-09-12 08:17:58 -07:00
|
|
|
|
}
|
|
|
|
|
|
2016-12-30 12:22:11 +01:00
|
|
|
|
extern "C" void LLVMRustUnpackOptimizationDiagnostic(
|
2016-12-31 12:01:23 -05:00
|
|
|
|
LLVMDiagnosticInfoRef DI, RustStringRef PassNameOut,
|
|
|
|
|
LLVMValueRef *FunctionOut, LLVMDebugLocRef *DebugLocOut,
|
|
|
|
|
RustStringRef MessageOut) {
|
2016-12-30 12:22:11 +01:00
|
|
|
|
// Undefined to call this not on an optimization diagnostic!
|
2016-12-31 12:01:23 -05:00
|
|
|
|
llvm::DiagnosticInfoOptimizationBase *Opt =
|
|
|
|
|
static_cast<llvm::DiagnosticInfoOptimizationBase *>(unwrap(DI));
|
2016-12-30 12:22:11 +01:00
|
|
|
|
|
2016-12-31 12:01:23 -05:00
|
|
|
|
RawRustStringOstream PassNameOS(PassNameOut);
|
|
|
|
|
PassNameOS << Opt->getPassName();
|
|
|
|
|
*FunctionOut = wrap(&Opt->getFunction());
|
|
|
|
|
*DebugLocOut = wrap(&Opt->getDebugLoc());
|
|
|
|
|
RawRustStringOstream MessageOS(MessageOut);
|
|
|
|
|
MessageOS << Opt->getMsg();
|
2014-09-12 08:17:58 -07:00
|
|
|
|
}
|
|
|
|
|
|
2016-12-31 12:01:23 -05:00
|
|
|
|
extern "C" void
|
|
|
|
|
LLVMRustUnpackInlineAsmDiagnostic(LLVMDiagnosticInfoRef DI, unsigned *CookieOut,
|
|
|
|
|
LLVMTwineRef *MessageOut,
|
|
|
|
|
LLVMValueRef *InstructionOut) {
|
2016-12-30 12:22:11 +01:00
|
|
|
|
// Undefined to call this not on an inline assembly diagnostic!
|
2016-12-31 12:01:23 -05:00
|
|
|
|
llvm::DiagnosticInfoInlineAsm *IA =
|
|
|
|
|
static_cast<llvm::DiagnosticInfoInlineAsm *>(unwrap(DI));
|
2015-01-22 19:43:39 +01:00
|
|
|
|
|
2016-12-31 12:01:23 -05:00
|
|
|
|
*CookieOut = IA->getLocCookie();
|
|
|
|
|
*MessageOut = wrap(&IA->getMsgStr());
|
|
|
|
|
*InstructionOut = wrap(IA->getInstruction());
|
2015-01-22 19:43:39 +01:00
|
|
|
|
}
|
|
|
|
|
|
2016-12-31 12:01:23 -05:00
|
|
|
|
extern "C" void LLVMRustWriteDiagnosticInfoToString(LLVMDiagnosticInfoRef DI,
|
|
|
|
|
RustStringRef Str) {
|
|
|
|
|
RawRustStringOstream OS(Str);
|
|
|
|
|
DiagnosticPrinterRawOStream DP(OS);
|
|
|
|
|
unwrap(DI)->print(DP);
|
2014-09-12 08:17:58 -07:00
|
|
|
|
}
|
|
|
|
|
|
2016-08-02 02:35:09 +03:00
|
|
|
|
enum class LLVMRustDiagnosticKind {
|
2016-12-30 12:22:11 +01:00
|
|
|
|
Other,
|
|
|
|
|
InlineAsm,
|
|
|
|
|
StackSize,
|
|
|
|
|
DebugMetadataVersion,
|
|
|
|
|
SampleProfile,
|
|
|
|
|
OptimizationRemark,
|
|
|
|
|
OptimizationRemarkMissed,
|
|
|
|
|
OptimizationRemarkAnalysis,
|
|
|
|
|
OptimizationRemarkAnalysisFPCommute,
|
|
|
|
|
OptimizationRemarkAnalysisAliasing,
|
|
|
|
|
OptimizationRemarkOther,
|
|
|
|
|
OptimizationFailure,
|
2016-08-02 02:35:09 +03:00
|
|
|
|
};
|
|
|
|
|
|
2016-12-31 12:01:23 -05:00
|
|
|
|
static LLVMRustDiagnosticKind toRust(DiagnosticKind Kind) {
|
|
|
|
|
switch (Kind) {
|
2016-12-30 12:22:11 +01:00
|
|
|
|
case DK_InlineAsm:
|
|
|
|
|
return LLVMRustDiagnosticKind::InlineAsm;
|
|
|
|
|
case DK_StackSize:
|
|
|
|
|
return LLVMRustDiagnosticKind::StackSize;
|
|
|
|
|
case DK_DebugMetadataVersion:
|
|
|
|
|
return LLVMRustDiagnosticKind::DebugMetadataVersion;
|
|
|
|
|
case DK_SampleProfile:
|
|
|
|
|
return LLVMRustDiagnosticKind::SampleProfile;
|
|
|
|
|
case DK_OptimizationRemark:
|
|
|
|
|
return LLVMRustDiagnosticKind::OptimizationRemark;
|
|
|
|
|
case DK_OptimizationRemarkMissed:
|
|
|
|
|
return LLVMRustDiagnosticKind::OptimizationRemarkMissed;
|
|
|
|
|
case DK_OptimizationRemarkAnalysis:
|
|
|
|
|
return LLVMRustDiagnosticKind::OptimizationRemarkAnalysis;
|
2016-09-24 10:44:21 -04:00
|
|
|
|
#if LLVM_VERSION_GE(3, 8)
|
2016-12-30 12:22:11 +01:00
|
|
|
|
case DK_OptimizationRemarkAnalysisFPCommute:
|
|
|
|
|
return LLVMRustDiagnosticKind::OptimizationRemarkAnalysisFPCommute;
|
|
|
|
|
case DK_OptimizationRemarkAnalysisAliasing:
|
|
|
|
|
return LLVMRustDiagnosticKind::OptimizationRemarkAnalysisAliasing;
|
2016-08-02 02:35:09 +03:00
|
|
|
|
#endif
|
2016-12-30 12:22:11 +01:00
|
|
|
|
default:
|
2016-09-24 10:44:21 -04:00
|
|
|
|
#if LLVM_VERSION_GE(3, 9)
|
2016-12-31 12:01:23 -05:00
|
|
|
|
return (Kind >= DK_FirstRemark && Kind <= DK_LastRemark)
|
2016-12-30 12:22:11 +01:00
|
|
|
|
? LLVMRustDiagnosticKind::OptimizationRemarkOther
|
|
|
|
|
: LLVMRustDiagnosticKind::Other;
|
2016-08-02 02:35:09 +03:00
|
|
|
|
#else
|
2016-12-30 12:22:11 +01:00
|
|
|
|
return LLVMRustDiagnosticKind::Other;
|
2016-08-02 02:35:09 +03:00
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-30 12:22:11 +01:00
|
|
|
|
extern "C" LLVMRustDiagnosticKind
|
2016-12-31 12:01:23 -05:00
|
|
|
|
LLVMRustGetDiagInfoKind(LLVMDiagnosticInfoRef DI) {
|
|
|
|
|
return toRust((DiagnosticKind)unwrap(DI)->getKind());
|
2014-09-12 08:17:58 -07:00
|
|
|
|
}
|
2016-08-03 00:25:19 +03:00
|
|
|
|
// This is kept distinct from LLVMGetTypeKind, because when
|
|
|
|
|
// a new type kind is added, the Rust-side enum must be
|
|
|
|
|
// updated or UB will result.
|
|
|
|
|
extern "C" LLVMTypeKind LLVMRustGetTypeKind(LLVMTypeRef Ty) {
|
|
|
|
|
switch (unwrap(Ty)->getTypeID()) {
|
|
|
|
|
case Type::VoidTyID:
|
|
|
|
|
return LLVMVoidTypeKind;
|
|
|
|
|
case Type::HalfTyID:
|
|
|
|
|
return LLVMHalfTypeKind;
|
|
|
|
|
case Type::FloatTyID:
|
|
|
|
|
return LLVMFloatTypeKind;
|
|
|
|
|
case Type::DoubleTyID:
|
|
|
|
|
return LLVMDoubleTypeKind;
|
|
|
|
|
case Type::X86_FP80TyID:
|
|
|
|
|
return LLVMX86_FP80TypeKind;
|
|
|
|
|
case Type::FP128TyID:
|
|
|
|
|
return LLVMFP128TypeKind;
|
|
|
|
|
case Type::PPC_FP128TyID:
|
|
|
|
|
return LLVMPPC_FP128TypeKind;
|
|
|
|
|
case Type::LabelTyID:
|
|
|
|
|
return LLVMLabelTypeKind;
|
|
|
|
|
case Type::MetadataTyID:
|
|
|
|
|
return LLVMMetadataTypeKind;
|
|
|
|
|
case Type::IntegerTyID:
|
|
|
|
|
return LLVMIntegerTypeKind;
|
|
|
|
|
case Type::FunctionTyID:
|
|
|
|
|
return LLVMFunctionTypeKind;
|
|
|
|
|
case Type::StructTyID:
|
|
|
|
|
return LLVMStructTypeKind;
|
|
|
|
|
case Type::ArrayTyID:
|
|
|
|
|
return LLVMArrayTypeKind;
|
|
|
|
|
case Type::PointerTyID:
|
|
|
|
|
return LLVMPointerTypeKind;
|
|
|
|
|
case Type::VectorTyID:
|
|
|
|
|
return LLVMVectorTypeKind;
|
|
|
|
|
case Type::X86_MMXTyID:
|
|
|
|
|
return LLVMX86_MMXTypeKind;
|
2016-09-24 10:44:21 -04:00
|
|
|
|
#if LLVM_VERSION_GE(3, 8)
|
2016-08-03 00:25:19 +03:00
|
|
|
|
case Type::TokenTyID:
|
|
|
|
|
return LLVMTokenTypeKind;
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
llvm_unreachable("Unhandled TypeID.");
|
|
|
|
|
}
|
2014-09-12 08:17:58 -07:00
|
|
|
|
|
2016-12-30 12:22:11 +01:00
|
|
|
|
extern "C" void LLVMRustWriteDebugLocToString(LLVMContextRef C,
|
2016-12-31 12:01:23 -05:00
|
|
|
|
LLVMDebugLocRef DL,
|
|
|
|
|
RustStringRef Str) {
|
|
|
|
|
RawRustStringOstream OS(Str);
|
|
|
|
|
unwrap(DL)->print(OS);
|
2014-09-12 08:17:58 -07:00
|
|
|
|
}
|
2014-09-27 01:33:36 -07:00
|
|
|
|
|
|
|
|
|
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(SMDiagnostic, LLVMSMDiagnosticRef)
|
|
|
|
|
|
2016-08-02 02:35:09 +03:00
|
|
|
|
extern "C" void LLVMRustSetInlineAsmDiagnosticHandler(
|
2016-12-30 12:22:11 +01:00
|
|
|
|
LLVMContextRef C, LLVMContext::InlineAsmDiagHandlerTy H, void *CX) {
|
|
|
|
|
unwrap(C)->setInlineAsmDiagnosticHandler(H, CX);
|
2014-09-27 01:33:36 -07:00
|
|
|
|
}
|
|
|
|
|
|
2016-12-31 12:01:23 -05:00
|
|
|
|
extern "C" void LLVMRustWriteSMDiagnosticToString(LLVMSMDiagnosticRef D,
|
|
|
|
|
RustStringRef Str) {
|
|
|
|
|
RawRustStringOstream OS(Str);
|
|
|
|
|
unwrap(D)->print("", OS);
|
2014-09-27 01:33:36 -07:00
|
|
|
|
}
|
2015-06-30 08:56:56 -07:00
|
|
|
|
|
|
|
|
|
extern "C" LLVMValueRef
|
2016-12-31 12:01:23 -05:00
|
|
|
|
LLVMRustBuildLandingPad(LLVMBuilderRef B, LLVMTypeRef Ty,
|
2016-12-30 12:22:11 +01:00
|
|
|
|
LLVMValueRef PersFn, unsigned NumClauses,
|
|
|
|
|
const char *Name, LLVMValueRef F) {
|
2016-12-31 12:01:23 -05:00
|
|
|
|
return LLVMBuildLandingPad(B, Ty, PersFn, NumClauses, Name);
|
2015-06-30 08:56:56 -07:00
|
|
|
|
}
|
2015-10-23 18:18:44 -07:00
|
|
|
|
|
2016-12-31 12:01:23 -05:00
|
|
|
|
extern "C" LLVMValueRef LLVMRustBuildCleanupPad(LLVMBuilderRef B,
|
2016-12-30 12:22:11 +01:00
|
|
|
|
LLVMValueRef ParentPad,
|
2016-12-31 12:01:23 -05:00
|
|
|
|
unsigned ArgCount,
|
2016-12-30 12:22:11 +01:00
|
|
|
|
LLVMValueRef *LLArgs,
|
|
|
|
|
const char *Name) {
|
2016-09-24 10:44:21 -04:00
|
|
|
|
#if LLVM_VERSION_GE(3, 8)
|
2016-12-30 12:22:11 +01:00
|
|
|
|
Value **Args = unwrap(LLArgs);
|
2016-12-30 13:21:21 +01:00
|
|
|
|
if (ParentPad == nullptr) {
|
2016-12-31 12:01:23 -05:00
|
|
|
|
Type *Ty = Type::getTokenTy(unwrap(B)->getContext());
|
2016-12-30 12:22:11 +01:00
|
|
|
|
ParentPad = wrap(Constant::getNullValue(Ty));
|
|
|
|
|
}
|
2016-12-31 12:01:23 -05:00
|
|
|
|
return wrap(unwrap(B)->CreateCleanupPad(
|
|
|
|
|
unwrap(ParentPad), ArrayRef<Value *>(Args, ArgCount), Name));
|
2015-10-23 18:18:44 -07:00
|
|
|
|
#else
|
2016-12-30 13:21:21 +01:00
|
|
|
|
return nullptr;
|
2015-10-23 18:18:44 -07:00
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-31 12:01:23 -05:00
|
|
|
|
extern "C" LLVMValueRef LLVMRustBuildCleanupRet(LLVMBuilderRef B,
|
2016-12-30 12:22:11 +01:00
|
|
|
|
LLVMValueRef CleanupPad,
|
|
|
|
|
LLVMBasicBlockRef UnwindBB) {
|
2016-09-24 10:44:21 -04:00
|
|
|
|
#if LLVM_VERSION_GE(3, 8)
|
2016-12-30 12:22:11 +01:00
|
|
|
|
CleanupPadInst *Inst = cast<CleanupPadInst>(unwrap(CleanupPad));
|
2016-12-31 12:01:23 -05:00
|
|
|
|
return wrap(unwrap(B)->CreateCleanupRet(Inst, unwrap(UnwindBB)));
|
2015-10-23 18:18:44 -07:00
|
|
|
|
#else
|
2016-12-30 13:21:21 +01:00
|
|
|
|
return nullptr;
|
2015-10-23 18:18:44 -07:00
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extern "C" LLVMValueRef
|
2016-12-31 12:01:23 -05:00
|
|
|
|
LLVMRustBuildCatchPad(LLVMBuilderRef B, LLVMValueRef ParentPad,
|
|
|
|
|
unsigned ArgCount, LLVMValueRef *LLArgs, const char *Name) {
|
2016-09-24 10:44:21 -04:00
|
|
|
|
#if LLVM_VERSION_GE(3, 8)
|
2016-12-30 12:22:11 +01:00
|
|
|
|
Value **Args = unwrap(LLArgs);
|
2016-12-31 12:01:23 -05:00
|
|
|
|
return wrap(unwrap(B)->CreateCatchPad(
|
|
|
|
|
unwrap(ParentPad), ArrayRef<Value *>(Args, ArgCount), Name));
|
2015-10-23 18:18:44 -07:00
|
|
|
|
#else
|
2016-12-30 13:21:21 +01:00
|
|
|
|
return nullptr;
|
2015-10-23 18:18:44 -07:00
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-31 12:01:23 -05:00
|
|
|
|
extern "C" LLVMValueRef LLVMRustBuildCatchRet(LLVMBuilderRef B,
|
2016-12-30 12:22:11 +01:00
|
|
|
|
LLVMValueRef Pad,
|
|
|
|
|
LLVMBasicBlockRef BB) {
|
2016-09-24 10:44:21 -04:00
|
|
|
|
#if LLVM_VERSION_GE(3, 8)
|
2016-12-31 12:01:23 -05:00
|
|
|
|
return wrap(unwrap(B)->CreateCatchRet(cast<CatchPadInst>(unwrap(Pad)),
|
2016-12-30 12:22:11 +01:00
|
|
|
|
unwrap(BB)));
|
2015-10-23 18:18:44 -07:00
|
|
|
|
#else
|
2016-12-30 13:21:21 +01:00
|
|
|
|
return nullptr;
|
2015-10-23 18:18:44 -07:00
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-31 12:01:23 -05:00
|
|
|
|
extern "C" LLVMValueRef LLVMRustBuildCatchSwitch(LLVMBuilderRef B,
|
2016-12-30 12:22:11 +01:00
|
|
|
|
LLVMValueRef ParentPad,
|
|
|
|
|
LLVMBasicBlockRef BB,
|
|
|
|
|
unsigned NumHandlers,
|
|
|
|
|
const char *Name) {
|
2016-09-24 10:44:21 -04:00
|
|
|
|
#if LLVM_VERSION_GE(3, 8)
|
2016-12-30 13:21:21 +01:00
|
|
|
|
if (ParentPad == nullptr) {
|
2016-12-31 12:01:23 -05:00
|
|
|
|
Type *Ty = Type::getTokenTy(unwrap(B)->getContext());
|
2016-12-30 12:22:11 +01:00
|
|
|
|
ParentPad = wrap(Constant::getNullValue(Ty));
|
|
|
|
|
}
|
2016-12-31 12:01:23 -05:00
|
|
|
|
return wrap(unwrap(B)->CreateCatchSwitch(unwrap(ParentPad), unwrap(BB),
|
2016-12-30 12:22:11 +01:00
|
|
|
|
NumHandlers, Name));
|
2015-10-23 18:18:44 -07:00
|
|
|
|
#else
|
2016-12-30 13:21:21 +01:00
|
|
|
|
return nullptr;
|
2015-10-23 18:18:44 -07:00
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-30 12:22:11 +01:00
|
|
|
|
extern "C" void LLVMRustAddHandler(LLVMValueRef CatchSwitchRef,
|
|
|
|
|
LLVMBasicBlockRef Handler) {
|
2016-09-24 10:44:21 -04:00
|
|
|
|
#if LLVM_VERSION_GE(3, 8)
|
2016-12-30 12:22:11 +01:00
|
|
|
|
Value *CatchSwitch = unwrap(CatchSwitchRef);
|
|
|
|
|
cast<CatchSwitchInst>(CatchSwitch)->addHandler(unwrap(Handler));
|
2015-10-23 18:18:44 -07:00
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-24 10:44:21 -04:00
|
|
|
|
#if LLVM_VERSION_GE(3, 8)
|
2016-12-30 12:22:11 +01:00
|
|
|
|
extern "C" OperandBundleDef *LLVMRustBuildOperandBundleDef(const char *Name,
|
|
|
|
|
LLVMValueRef *Inputs,
|
|
|
|
|
unsigned NumInputs) {
|
2015-10-23 18:18:44 -07:00
|
|
|
|
return new OperandBundleDef(Name, makeArrayRef(unwrap(Inputs), NumInputs));
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-30 12:22:11 +01:00
|
|
|
|
extern "C" void LLVMRustFreeOperandBundleDef(OperandBundleDef *Bundle) {
|
2015-10-23 18:18:44 -07:00
|
|
|
|
delete Bundle;
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-30 12:22:11 +01:00
|
|
|
|
extern "C" LLVMValueRef LLVMRustBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
|
|
|
|
|
LLVMValueRef *Args, unsigned NumArgs,
|
|
|
|
|
OperandBundleDef *Bundle,
|
|
|
|
|
const char *Name) {
|
2016-12-31 12:01:23 -05:00
|
|
|
|
unsigned Len = Bundle ? 1 : 0;
|
|
|
|
|
ArrayRef<OperandBundleDef> Bundles = makeArrayRef(Bundle, Len);
|
2016-12-30 12:22:11 +01:00
|
|
|
|
return wrap(unwrap(B)->CreateCall(
|
|
|
|
|
unwrap(Fn), makeArrayRef(unwrap(Args), NumArgs), Bundles, Name));
|
2015-10-23 18:18:44 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extern "C" LLVMValueRef
|
2016-12-30 12:22:11 +01:00
|
|
|
|
LLVMRustBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn, LLVMValueRef *Args,
|
|
|
|
|
unsigned NumArgs, LLVMBasicBlockRef Then,
|
|
|
|
|
LLVMBasicBlockRef Catch, OperandBundleDef *Bundle,
|
2015-10-23 18:18:44 -07:00
|
|
|
|
const char *Name) {
|
2016-12-31 12:01:23 -05:00
|
|
|
|
unsigned Len = Bundle ? 1 : 0;
|
|
|
|
|
ArrayRef<OperandBundleDef> Bundles = makeArrayRef(Bundle, Len);
|
2016-12-30 12:22:11 +01:00
|
|
|
|
return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
|
|
|
|
|
makeArrayRef(unwrap(Args), NumArgs),
|
|
|
|
|
Bundles, Name));
|
2015-10-23 18:18:44 -07:00
|
|
|
|
}
|
|
|
|
|
#else
|
2016-12-30 12:22:11 +01:00
|
|
|
|
extern "C" void *LLVMRustBuildOperandBundleDef(const char *Name,
|
|
|
|
|
LLVMValueRef *Inputs,
|
|
|
|
|
unsigned NumInputs) {
|
2016-12-30 13:21:21 +01:00
|
|
|
|
return nullptr;
|
2015-10-23 18:18:44 -07:00
|
|
|
|
}
|
|
|
|
|
|
2016-12-30 12:22:11 +01:00
|
|
|
|
extern "C" void LLVMRustFreeOperandBundleDef(void *Bundle) {}
|
2015-10-23 18:18:44 -07:00
|
|
|
|
|
2016-12-30 12:22:11 +01:00
|
|
|
|
extern "C" LLVMValueRef LLVMRustBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
|
|
|
|
|
LLVMValueRef *Args, unsigned NumArgs,
|
|
|
|
|
void *Bundle, const char *Name) {
|
|
|
|
|
return LLVMBuildCall(B, Fn, Args, NumArgs, Name);
|
2015-10-23 18:18:44 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extern "C" LLVMValueRef
|
2016-12-30 12:22:11 +01:00
|
|
|
|
LLVMRustBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn, LLVMValueRef *Args,
|
|
|
|
|
unsigned NumArgs, LLVMBasicBlockRef Then,
|
|
|
|
|
LLVMBasicBlockRef Catch, void *Bundle, const char *Name) {
|
|
|
|
|
return LLVMBuildInvoke(B, Fn, Args, NumArgs, Then, Catch, Name);
|
2015-10-23 18:18:44 -07:00
|
|
|
|
}
|
|
|
|
|
#endif
|
2016-02-04 19:40:28 +02:00
|
|
|
|
|
2016-12-30 12:22:11 +01:00
|
|
|
|
extern "C" void LLVMRustPositionBuilderAtStart(LLVMBuilderRef B,
|
|
|
|
|
LLVMBasicBlockRef BB) {
|
2016-12-31 12:01:23 -05:00
|
|
|
|
auto Point = unwrap(BB)->getFirstInsertionPt();
|
|
|
|
|
unwrap(B)->SetInsertPoint(unwrap(BB), Point);
|
2016-02-04 19:40:28 +02:00
|
|
|
|
}
|
2016-03-28 17:57:31 +02:00
|
|
|
|
|
2016-12-30 12:22:11 +01:00
|
|
|
|
extern "C" void LLVMRustSetComdat(LLVMModuleRef M, LLVMValueRef V,
|
|
|
|
|
const char *Name) {
|
|
|
|
|
Triple TargetTriple(unwrap(M)->getTargetTriple());
|
|
|
|
|
GlobalObject *GV = unwrap<GlobalObject>(V);
|
|
|
|
|
if (!TargetTriple.isOSBinFormatMachO()) {
|
|
|
|
|
GV->setComdat(unwrap(M)->getOrInsertComdat(Name));
|
|
|
|
|
}
|
2016-03-28 17:57:31 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extern "C" void LLVMRustUnsetComdat(LLVMValueRef V) {
|
2016-12-30 12:22:11 +01:00
|
|
|
|
GlobalObject *GV = unwrap<GlobalObject>(V);
|
|
|
|
|
GV->setComdat(nullptr);
|
2016-03-28 17:57:31 +02:00
|
|
|
|
}
|
2016-09-01 13:52:33 -05:00
|
|
|
|
|
|
|
|
|
enum class LLVMRustLinkage {
|
2016-12-30 12:22:11 +01:00
|
|
|
|
ExternalLinkage = 0,
|
|
|
|
|
AvailableExternallyLinkage = 1,
|
|
|
|
|
LinkOnceAnyLinkage = 2,
|
|
|
|
|
LinkOnceODRLinkage = 3,
|
|
|
|
|
WeakAnyLinkage = 4,
|
|
|
|
|
WeakODRLinkage = 5,
|
|
|
|
|
AppendingLinkage = 6,
|
|
|
|
|
InternalLinkage = 7,
|
|
|
|
|
PrivateLinkage = 8,
|
|
|
|
|
ExternalWeakLinkage = 9,
|
|
|
|
|
CommonLinkage = 10,
|
2016-09-01 13:52:33 -05:00
|
|
|
|
};
|
|
|
|
|
|
2016-12-31 12:01:23 -05:00
|
|
|
|
static LLVMRustLinkage toRust(LLVMLinkage Linkage) {
|
|
|
|
|
switch (Linkage) {
|
2016-12-30 12:22:11 +01:00
|
|
|
|
case LLVMExternalLinkage:
|
|
|
|
|
return LLVMRustLinkage::ExternalLinkage;
|
|
|
|
|
case LLVMAvailableExternallyLinkage:
|
|
|
|
|
return LLVMRustLinkage::AvailableExternallyLinkage;
|
|
|
|
|
case LLVMLinkOnceAnyLinkage:
|
|
|
|
|
return LLVMRustLinkage::LinkOnceAnyLinkage;
|
|
|
|
|
case LLVMLinkOnceODRLinkage:
|
|
|
|
|
return LLVMRustLinkage::LinkOnceODRLinkage;
|
|
|
|
|
case LLVMWeakAnyLinkage:
|
|
|
|
|
return LLVMRustLinkage::WeakAnyLinkage;
|
|
|
|
|
case LLVMWeakODRLinkage:
|
|
|
|
|
return LLVMRustLinkage::WeakODRLinkage;
|
|
|
|
|
case LLVMAppendingLinkage:
|
|
|
|
|
return LLVMRustLinkage::AppendingLinkage;
|
|
|
|
|
case LLVMInternalLinkage:
|
|
|
|
|
return LLVMRustLinkage::InternalLinkage;
|
|
|
|
|
case LLVMPrivateLinkage:
|
|
|
|
|
return LLVMRustLinkage::PrivateLinkage;
|
|
|
|
|
case LLVMExternalWeakLinkage:
|
|
|
|
|
return LLVMRustLinkage::ExternalWeakLinkage;
|
|
|
|
|
case LLVMCommonLinkage:
|
|
|
|
|
return LLVMRustLinkage::CommonLinkage;
|
|
|
|
|
default:
|
|
|
|
|
llvm_unreachable("Invalid LLVMRustLinkage value!");
|
|
|
|
|
}
|
2016-09-01 13:52:33 -05:00
|
|
|
|
}
|
|
|
|
|
|
2016-12-31 12:01:23 -05:00
|
|
|
|
static LLVMLinkage fromRust(LLVMRustLinkage Linkage) {
|
|
|
|
|
switch (Linkage) {
|
2016-12-30 12:22:11 +01:00
|
|
|
|
case LLVMRustLinkage::ExternalLinkage:
|
|
|
|
|
return LLVMExternalLinkage;
|
|
|
|
|
case LLVMRustLinkage::AvailableExternallyLinkage:
|
|
|
|
|
return LLVMAvailableExternallyLinkage;
|
|
|
|
|
case LLVMRustLinkage::LinkOnceAnyLinkage:
|
|
|
|
|
return LLVMLinkOnceAnyLinkage;
|
|
|
|
|
case LLVMRustLinkage::LinkOnceODRLinkage:
|
|
|
|
|
return LLVMLinkOnceODRLinkage;
|
|
|
|
|
case LLVMRustLinkage::WeakAnyLinkage:
|
|
|
|
|
return LLVMWeakAnyLinkage;
|
|
|
|
|
case LLVMRustLinkage::WeakODRLinkage:
|
|
|
|
|
return LLVMWeakODRLinkage;
|
|
|
|
|
case LLVMRustLinkage::AppendingLinkage:
|
|
|
|
|
return LLVMAppendingLinkage;
|
|
|
|
|
case LLVMRustLinkage::InternalLinkage:
|
|
|
|
|
return LLVMInternalLinkage;
|
|
|
|
|
case LLVMRustLinkage::PrivateLinkage:
|
|
|
|
|
return LLVMPrivateLinkage;
|
|
|
|
|
case LLVMRustLinkage::ExternalWeakLinkage:
|
|
|
|
|
return LLVMExternalWeakLinkage;
|
|
|
|
|
case LLVMRustLinkage::CommonLinkage:
|
|
|
|
|
return LLVMCommonLinkage;
|
|
|
|
|
}
|
2017-01-14 15:38:12 +01:00
|
|
|
|
llvm_unreachable("Invalid LLVMRustLinkage value!");
|
2016-09-01 13:52:33 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extern "C" LLVMRustLinkage LLVMRustGetLinkage(LLVMValueRef V) {
|
2016-12-31 12:01:23 -05:00
|
|
|
|
return toRust(LLVMGetLinkage(V));
|
2016-09-01 13:52:33 -05:00
|
|
|
|
}
|
|
|
|
|
|
2016-12-30 12:22:11 +01:00
|
|
|
|
extern "C" void LLVMRustSetLinkage(LLVMValueRef V,
|
|
|
|
|
LLVMRustLinkage RustLinkage) {
|
2016-12-31 12:01:23 -05:00
|
|
|
|
LLVMSetLinkage(V, fromRust(RustLinkage));
|
2016-09-01 13:52:33 -05:00
|
|
|
|
}
|
2016-11-16 23:36:08 +01:00
|
|
|
|
|
2016-08-26 01:32:46 +03:00
|
|
|
|
// Returns true if both high and low were successfully set. Fails in case constant wasn’t any of
|
|
|
|
|
// the common sizes (1, 8, 16, 32, 64, 128 bits)
|
|
|
|
|
extern "C" bool LLVMRustConstInt128Get(LLVMValueRef CV, bool sext, uint64_t *high, uint64_t *low)
|
|
|
|
|
{
|
|
|
|
|
auto C = unwrap<llvm::ConstantInt>(CV);
|
|
|
|
|
if (C->getBitWidth() > 128) { return false; }
|
|
|
|
|
APInt AP;
|
|
|
|
|
if (sext) {
|
|
|
|
|
AP = C->getValue().sextOrSelf(128);
|
|
|
|
|
} else {
|
|
|
|
|
AP = C->getValue().zextOrSelf(128);
|
|
|
|
|
}
|
|
|
|
|
*low = AP.getLoBits(64).getZExtValue();
|
|
|
|
|
*high = AP.getHiBits(64).getZExtValue();
|
|
|
|
|
return true;
|
2016-09-01 13:52:33 -05:00
|
|
|
|
}
|
2016-11-16 23:36:08 +01:00
|
|
|
|
|
|
|
|
|
extern "C" LLVMContextRef LLVMRustGetValueContext(LLVMValueRef V) {
|
2016-12-30 12:22:11 +01:00
|
|
|
|
return wrap(&unwrap(V)->getContext());
|
2016-11-16 23:36:08 +01:00
|
|
|
|
}
|
2016-11-28 17:44:51 -05:00
|
|
|
|
|
|
|
|
|
enum class LLVMRustVisibility {
|
2016-12-30 12:22:11 +01:00
|
|
|
|
Default = 0,
|
|
|
|
|
Hidden = 1,
|
|
|
|
|
Protected = 2,
|
2016-11-28 17:44:51 -05:00
|
|
|
|
};
|
|
|
|
|
|
2016-12-31 12:01:23 -05:00
|
|
|
|
static LLVMRustVisibility toRust(LLVMVisibility Vis) {
|
|
|
|
|
switch (Vis) {
|
2016-12-30 12:22:11 +01:00
|
|
|
|
case LLVMDefaultVisibility:
|
|
|
|
|
return LLVMRustVisibility::Default;
|
|
|
|
|
case LLVMHiddenVisibility:
|
|
|
|
|
return LLVMRustVisibility::Hidden;
|
|
|
|
|
case LLVMProtectedVisibility:
|
|
|
|
|
return LLVMRustVisibility::Protected;
|
|
|
|
|
}
|
2017-01-14 15:38:12 +01:00
|
|
|
|
llvm_unreachable("Invalid LLVMRustVisibility value!");
|
2016-11-28 17:44:51 -05:00
|
|
|
|
}
|
|
|
|
|
|
2016-12-31 12:01:23 -05:00
|
|
|
|
static LLVMVisibility fromRust(LLVMRustVisibility Vis) {
|
|
|
|
|
switch (Vis) {
|
2016-12-30 12:22:11 +01:00
|
|
|
|
case LLVMRustVisibility::Default:
|
|
|
|
|
return LLVMDefaultVisibility;
|
|
|
|
|
case LLVMRustVisibility::Hidden:
|
|
|
|
|
return LLVMHiddenVisibility;
|
|
|
|
|
case LLVMRustVisibility::Protected:
|
|
|
|
|
return LLVMProtectedVisibility;
|
|
|
|
|
}
|
2017-01-14 15:38:12 +01:00
|
|
|
|
llvm_unreachable("Invalid LLVMRustVisibility value!");
|
2016-11-28 17:44:51 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extern "C" LLVMRustVisibility LLVMRustGetVisibility(LLVMValueRef V) {
|
2016-12-31 12:01:23 -05:00
|
|
|
|
return toRust(LLVMGetVisibility(V));
|
2016-11-28 17:44:51 -05:00
|
|
|
|
}
|
|
|
|
|
|
2017-02-10 19:29:39 +02:00
|
|
|
|
// Oh hey, a binding that makes sense for once? (because LLVM’s own do not)
|
|
|
|
|
extern "C" LLVMValueRef LLVMRustBuildIntCast(LLVMBuilderRef B, LLVMValueRef Val,
|
|
|
|
|
LLVMTypeRef DestTy, bool isSigned) {
|
|
|
|
|
return wrap(unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy), isSigned, ""));
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-30 12:22:11 +01:00
|
|
|
|
extern "C" void LLVMRustSetVisibility(LLVMValueRef V,
|
|
|
|
|
LLVMRustVisibility RustVisibility) {
|
2016-12-31 12:01:23 -05:00
|
|
|
|
LLVMSetVisibility(V, fromRust(RustVisibility));
|
2016-11-28 17:44:51 -05:00
|
|
|
|
}
|