From d5985bc9ecbdc792d514cf531107d33f85644fdc Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Thu, 4 Apr 2019 13:05:41 -0700 Subject: rustc: Start implementing compat with LLVM 9 This commit doesn't actually migrate to LLVM 9, but it brings our own C++ bindings in line with LLVM 9 and able to compile against tip of tree. The changes made were: * The `MainSubprogram` flag for debuginfo moved between flag types. * Iteration of archive members was tweaked slightly and we have to construct the two iterators before constructing the returned `RustArchiveIterator` value. * The `getOrInsertFunction` binding now returns a wrapper which we use `getCallee()` on to get the value we're interested in. --- src/rustllvm/RustWrapper.cpp | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) (limited to 'src/rustllvm/RustWrapper.cpp') diff --git a/src/rustllvm/RustWrapper.cpp b/src/rustllvm/RustWrapper.cpp index a00417a3629..437e2d482ef 100644 --- a/src/rustllvm/RustWrapper.cpp +++ b/src/rustllvm/RustWrapper.cpp @@ -117,7 +117,11 @@ extern "C" LLVMValueRef LLVMRustGetOrInsertFunction(LLVMModuleRef M, const char *Name, LLVMTypeRef FunctionTy) { return wrap( - unwrap(M)->getOrInsertFunction(Name, unwrap(FunctionTy))); + unwrap(M)->getOrInsertFunction(Name, unwrap(FunctionTy)) +#if LLVM_VERSION_GE(9, 0) + .getCallee() +#endif + ); } extern "C" LLVMValueRef @@ -417,7 +421,6 @@ enum class LLVMRustDIFlags : uint32_t { FlagIntroducedVirtual = (1 << 18), FlagBitField = (1 << 19), FlagNoReturn = (1 << 20), - FlagMainSubprogram = (1 << 21), // Do not add values that are not supported by the minimum LLVM // version we support! see llvm/include/llvm/IR/DebugInfoFlags.def }; @@ -508,9 +511,6 @@ static DINode::DIFlags fromRust(LLVMRustDIFlags Flags) { if (isSet(Flags & LLVMRustDIFlags::FlagNoReturn)) { Result |= DINode::DIFlags::FlagNoReturn; } - if (isSet(Flags & LLVMRustDIFlags::FlagMainSubprogram)) { - Result |= DINode::DIFlags::FlagMainSubprogram; - } return Result; } @@ -525,6 +525,7 @@ enum class LLVMRustDISPFlags : uint32_t { SPFlagLocalToUnit = (1 << 2), SPFlagDefinition = (1 << 3), SPFlagOptimized = (1 << 4), + SPFlagMainSubprogram = (1 << 5), // Do not add values that are not supported by the minimum LLVM // version we support! see llvm/include/llvm/IR/DebugInfoFlags.def // (In LLVM < 8, createFunction supported these as separate bool arguments.) @@ -575,6 +576,11 @@ static DISubprogram::DISPFlags fromRust(LLVMRustDISPFlags SPFlags) { if (isSet(SPFlags & LLVMRustDISPFlags::SPFlagOptimized)) { Result |= DISubprogram::DISPFlags::SPFlagOptimized; } +#if LLVM_VERSION_GE(9, 0) + if (isSet(SPFlags & LLVMRustDISPFlags::SPFlagMainSubprogram)) { + Result |= DISubprogram::DISPFlags::SPFlagMainSubprogram; + } +#endif return Result; } @@ -671,18 +677,27 @@ extern "C" LLVMMetadataRef LLVMRustDIBuilderCreateFunction( DITemplateParameterArray TParams = DITemplateParameterArray(unwrap(TParam)); #if LLVM_VERSION_GE(8, 0) + DISubprogram::DISPFlags llvmSPFlags = fromRust(SPFlags); + DINode::DIFlags llvmFlags = fromRust(Flags); +#if LLVM_VERSION_LT(9, 0) + if (isSet(SPFlags & LLVMRustDISPFlags::SPFlagMainSubprogram)) + llvmFlags |= DINode::DIFlags::FlagMainSubprogram; +#endif DISubprogram *Sub = Builder->createFunction( unwrapDI(Scope), Name, LinkageName, unwrapDI(File), - LineNo, unwrapDI(Ty), ScopeLine, fromRust(Flags), - fromRust(SPFlags), TParams, unwrapDIPtr(Decl)); + LineNo, unwrapDI(Ty), ScopeLine, llvmFlags, + llvmSPFlags, TParams, unwrapDIPtr(Decl)); #else bool IsLocalToUnit = isSet(SPFlags & LLVMRustDISPFlags::SPFlagLocalToUnit); bool IsDefinition = isSet(SPFlags & LLVMRustDISPFlags::SPFlagDefinition); bool IsOptimized = isSet(SPFlags & LLVMRustDISPFlags::SPFlagOptimized); + DINode::DIFlags llvmFlags = fromRust(Flags); + if (isSet(SPFlags & LLVMRustDISPFlags::SPFlagMainSubprogram)) + llvmFlags |= DINode::DIFlags::FlagMainSubprogram; DISubprogram *Sub = Builder->createFunction( unwrapDI(Scope), Name, LinkageName, unwrapDI(File), LineNo, unwrapDI(Ty), IsLocalToUnit, IsDefinition, - ScopeLine, fromRust(Flags), IsOptimized, TParams, + ScopeLine, llvmFlags, IsOptimized, TParams, unwrapDIPtr(Decl)); #endif unwrap(Fn)->setSubprogram(Sub); -- cgit 1.4.1-3-g733a5 From a1099ae73ea0fdd9b82329aaf64a977c44a7595e Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Thu, 18 Apr 2019 15:28:18 -0700 Subject: Remove the unused LLVMRustIsRustLLVM --- src/librustc_codegen_llvm/llvm/ffi.rs | 1 - src/rustllvm/RustWrapper.cpp | 8 -------- 2 files changed, 9 deletions(-) (limited to 'src/rustllvm/RustWrapper.cpp') diff --git a/src/librustc_codegen_llvm/llvm/ffi.rs b/src/librustc_codegen_llvm/llvm/ffi.rs index f6ee8bec16a..f88923fc9f1 100644 --- a/src/librustc_codegen_llvm/llvm/ffi.rs +++ b/src/librustc_codegen_llvm/llvm/ffi.rs @@ -1382,7 +1382,6 @@ extern "C" { pub fn LLVMRustDebugMetadataVersion() -> u32; pub fn LLVMRustVersionMajor() -> u32; pub fn LLVMRustVersionMinor() -> u32; - pub fn LLVMRustIsRustLLVM() -> bool; pub fn LLVMRustAddModuleFlag(M: &Module, name: *const c_char, value: u32); diff --git a/src/rustllvm/RustWrapper.cpp b/src/rustllvm/RustWrapper.cpp index 437e2d482ef..66453c08a66 100644 --- a/src/rustllvm/RustWrapper.cpp +++ b/src/rustllvm/RustWrapper.cpp @@ -613,14 +613,6 @@ extern "C" uint32_t LLVMRustVersionMinor() { return LLVM_VERSION_MINOR; } extern "C" uint32_t LLVMRustVersionMajor() { return LLVM_VERSION_MAJOR; } -extern "C" bool LLVMRustIsRustLLVM() { -#ifdef LLVM_RUSTLLVM - return 1; -#else - return 0; -#endif -} - extern "C" void LLVMRustAddModuleFlag(LLVMModuleRef M, const char *Name, uint32_t Value) { unwrap(M)->addModuleFlag(Module::Warning, Name, Value); -- cgit 1.4.1-3-g733a5 From eb338220911fbcf0661e4f8802b23d0bd4e5d077 Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Sat, 6 Jul 2019 21:52:25 +0200 Subject: Pass type to byval attributes --- src/librustc_codegen_llvm/abi.rs | 64 +++++++++++++++++++---------------- src/librustc_codegen_llvm/declare.rs | 2 +- src/librustc_codegen_llvm/llvm/ffi.rs | 2 ++ src/rustllvm/RustWrapper.cpp | 22 ++++++++++++ 4 files changed, 60 insertions(+), 30 deletions(-) (limited to 'src/rustllvm/RustWrapper.cpp') diff --git a/src/librustc_codegen_llvm/abi.rs b/src/librustc_codegen_llvm/abi.rs index 38d4b7e3f9d..ff87afe0c44 100644 --- a/src/librustc_codegen_llvm/abi.rs +++ b/src/librustc_codegen_llvm/abi.rs @@ -34,17 +34,17 @@ trait ArgAttributeExt { impl ArgAttributeExt for ArgAttribute { fn for_each_kind(&self, mut f: F) where F: FnMut(llvm::Attribute) { for_each_kind!(self, f, - ByVal, NoAlias, NoCapture, NonNull, ReadOnly, SExt, StructRet, ZExt, InReg) + NoAlias, NoCapture, NonNull, ReadOnly, SExt, StructRet, ZExt, InReg) } } pub trait ArgAttributesExt { - fn apply_llfn(&self, idx: AttributePlace, llfn: &Value); - fn apply_callsite(&self, idx: AttributePlace, callsite: &Value); + fn apply_llfn(&self, idx: AttributePlace, llfn: &Value, ty: Option<&Type>); + fn apply_callsite(&self, idx: AttributePlace, callsite: &Value, ty: Option<&Type>); } impl ArgAttributesExt for ArgAttributes { - fn apply_llfn(&self, idx: AttributePlace, llfn: &Value) { + fn apply_llfn(&self, idx: AttributePlace, llfn: &Value, ty: Option<&Type>) { let mut regular = self.regular; unsafe { let deref = self.pointee_size.bytes(); @@ -65,11 +65,14 @@ impl ArgAttributesExt for ArgAttributes { idx.as_uint(), align.bytes() as u32); } + if regular.contains(ArgAttribute::ByVal) { + llvm::LLVMRustAddByValAttr(llfn, idx.as_uint(), ty.unwrap()); + } regular.for_each_kind(|attr| attr.apply_llfn(idx, llfn)); } } - fn apply_callsite(&self, idx: AttributePlace, callsite: &Value) { + fn apply_callsite(&self, idx: AttributePlace, callsite: &Value, ty: Option<&Type>) { let mut regular = self.regular; unsafe { let deref = self.pointee_size.bytes(); @@ -90,6 +93,9 @@ impl ArgAttributesExt for ArgAttributes { idx.as_uint(), align.bytes() as u32); } + if regular.contains(ArgAttribute::ByVal) { + llvm::LLVMRustAddByValCallSiteAttr(callsite, idx.as_uint(), ty.unwrap()); + } regular.for_each_kind(|attr| attr.apply_callsite(idx, callsite)); } } @@ -298,7 +304,7 @@ pub trait FnTypeLlvmExt<'tcx> { fn llvm_type(&self, cx: &CodegenCx<'ll, 'tcx>) -> &'ll Type; fn ptr_to_llvm_type(&self, cx: &CodegenCx<'ll, 'tcx>) -> &'ll Type; fn llvm_cconv(&self) -> llvm::CallConv; - fn apply_attrs_llfn(&self, llfn: &'ll Value); + fn apply_attrs_llfn(&self, cx: &CodegenCx<'ll, 'tcx>, llfn: &'ll Value); fn apply_attrs_callsite(&self, bx: &mut Builder<'a, 'll, 'tcx>, callsite: &'ll Value); } @@ -384,51 +390,51 @@ impl<'tcx> FnTypeLlvmExt<'tcx> for FnType<'tcx, Ty<'tcx>> { } } - fn apply_attrs_llfn(&self, llfn: &'ll Value) { + fn apply_attrs_llfn(&self, cx: &CodegenCx<'ll, 'tcx>, llfn: &'ll Value) { let mut i = 0; - let mut apply = |attrs: &ArgAttributes| { - attrs.apply_llfn(llvm::AttributePlace::Argument(i), llfn); + let mut apply = |attrs: &ArgAttributes, ty: Option<&Type>| { + attrs.apply_llfn(llvm::AttributePlace::Argument(i), llfn, ty); i += 1; }; match self.ret.mode { PassMode::Direct(ref attrs) => { - attrs.apply_llfn(llvm::AttributePlace::ReturnValue, llfn); + attrs.apply_llfn(llvm::AttributePlace::ReturnValue, llfn, None); } - PassMode::Indirect(ref attrs, _) => apply(attrs), + PassMode::Indirect(ref attrs, _) => apply(attrs, Some(self.ret.layout.llvm_type(cx))), _ => {} } for arg in &self.args { if arg.pad.is_some() { - apply(&ArgAttributes::new()); + apply(&ArgAttributes::new(), None); } match arg.mode { PassMode::Ignore(_) => {} PassMode::Direct(ref attrs) | - PassMode::Indirect(ref attrs, None) => apply(attrs), + PassMode::Indirect(ref attrs, None) => apply(attrs, Some(arg.layout.llvm_type(cx))), PassMode::Indirect(ref attrs, Some(ref extra_attrs)) => { - apply(attrs); - apply(extra_attrs); + apply(attrs, None); + apply(extra_attrs, None); } PassMode::Pair(ref a, ref b) => { - apply(a); - apply(b); + apply(a, None); + apply(b, None); } - PassMode::Cast(_) => apply(&ArgAttributes::new()), + PassMode::Cast(_) => apply(&ArgAttributes::new(), None), } } } fn apply_attrs_callsite(&self, bx: &mut Builder<'a, 'll, 'tcx>, callsite: &'ll Value) { let mut i = 0; - let mut apply = |attrs: &ArgAttributes| { - attrs.apply_callsite(llvm::AttributePlace::Argument(i), callsite); + let mut apply = |attrs: &ArgAttributes, ty: Option<&Type>| { + attrs.apply_callsite(llvm::AttributePlace::Argument(i), callsite, ty); i += 1; }; match self.ret.mode { PassMode::Direct(ref attrs) => { - attrs.apply_callsite(llvm::AttributePlace::ReturnValue, callsite); + attrs.apply_callsite(llvm::AttributePlace::ReturnValue, callsite, None); } - PassMode::Indirect(ref attrs, _) => apply(attrs), + PassMode::Indirect(ref attrs, _) => apply(attrs, Some(self.ret.layout.llvm_type(bx))), _ => {} } if let layout::Abi::Scalar(ref scalar) = self.ret.layout.abi { @@ -446,21 +452,21 @@ impl<'tcx> FnTypeLlvmExt<'tcx> for FnType<'tcx, Ty<'tcx>> { } for arg in &self.args { if arg.pad.is_some() { - apply(&ArgAttributes::new()); + apply(&ArgAttributes::new(), None); } match arg.mode { PassMode::Ignore(_) => {} PassMode::Direct(ref attrs) | - PassMode::Indirect(ref attrs, None) => apply(attrs), + PassMode::Indirect(ref attrs, None) => apply(attrs, Some(arg.layout.llvm_type(bx))), PassMode::Indirect(ref attrs, Some(ref extra_attrs)) => { - apply(attrs); - apply(extra_attrs); + apply(attrs, None); + apply(extra_attrs, None); } PassMode::Pair(ref a, ref b) => { - apply(a); - apply(b); + apply(a, None); + apply(b, None); } - PassMode::Cast(_) => apply(&ArgAttributes::new()), + PassMode::Cast(_) => apply(&ArgAttributes::new(), None), } } diff --git a/src/librustc_codegen_llvm/declare.rs b/src/librustc_codegen_llvm/declare.rs index bcb14b8899e..62eab0f3d4e 100644 --- a/src/librustc_codegen_llvm/declare.rs +++ b/src/librustc_codegen_llvm/declare.rs @@ -107,7 +107,7 @@ impl DeclareMethods<'tcx> for CodegenCx<'ll, 'tcx> { llvm::Attribute::NoReturn.apply_llfn(Function, llfn); } - fty.apply_attrs_llfn(llfn); + fty.apply_attrs_llfn(self, llfn); llfn } diff --git a/src/librustc_codegen_llvm/llvm/ffi.rs b/src/librustc_codegen_llvm/llvm/ffi.rs index a5c295cd452..75f639ec3ec 100644 --- a/src/librustc_codegen_llvm/llvm/ffi.rs +++ b/src/librustc_codegen_llvm/llvm/ffi.rs @@ -794,6 +794,7 @@ extern "C" { pub fn LLVMRustAddAlignmentAttr(Fn: &Value, index: c_uint, bytes: u32); pub fn LLVMRustAddDereferenceableAttr(Fn: &Value, index: c_uint, bytes: u64); pub fn LLVMRustAddDereferenceableOrNullAttr(Fn: &Value, index: c_uint, bytes: u64); + pub fn LLVMRustAddByValAttr(Fn: &Value, index: c_uint, ty: &Type); pub fn LLVMRustAddFunctionAttribute(Fn: &Value, index: c_uint, attr: Attribute); pub fn LLVMRustAddFunctionAttrStringValue(Fn: &Value, index: c_uint, @@ -824,6 +825,7 @@ extern "C" { pub fn LLVMRustAddDereferenceableOrNullCallSiteAttr(Instr: &Value, index: c_uint, bytes: u64); + pub fn LLVMRustAddByValCallSiteAttr(Instr: &Value, index: c_uint, ty: &Type); // Operations on load/store instructions (only) pub fn LLVMSetVolatile(MemoryAccessInst: &Value, volatile: Bool); diff --git a/src/rustllvm/RustWrapper.cpp b/src/rustllvm/RustWrapper.cpp index 66453c08a66..16d08ee534e 100644 --- a/src/rustllvm/RustWrapper.cpp +++ b/src/rustllvm/RustWrapper.cpp @@ -237,6 +237,17 @@ extern "C" void LLVMRustAddDereferenceableOrNullCallSiteAttr(LLVMValueRef Instr, Call->getContext(), Index, B)); } +extern "C" void LLVMRustAddByValCallSiteAttr(LLVMValueRef Instr, unsigned Index, + LLVMTypeRef Ty) { + CallSite Call = CallSite(unwrap(Instr)); +#if LLVM_VERSION_GE(9, 0) + Attribute Attr = Attribute::getWithByValType(Call->getContext(), unwrap(Ty)); +#else + Attribute Attr = Attribute::get(Call->getContext(), Attribute::ByVal); +#endif + Call.addAttribute(Index, Attr); +} + extern "C" void LLVMRustAddFunctionAttribute(LLVMValueRef Fn, unsigned Index, LLVMRustAttribute RustAttr) { Function *A = unwrap(Fn); @@ -271,6 +282,17 @@ extern "C" void LLVMRustAddDereferenceableOrNullAttr(LLVMValueRef Fn, A->addAttributes(Index, B); } +extern "C" void LLVMRustAddByValAttr(LLVMValueRef Fn, unsigned Index, + LLVMTypeRef Ty) { + Function *F = unwrap(Fn); +#if LLVM_VERSION_GE(9, 0) + Attribute Attr = Attribute::getWithByValType(F->getContext(), unwrap(Ty)); +#else + Attribute Attr = Attribute::get(F->getContext(), Attribute::ByVal); +#endif + F->addAttribute(Index, Attr); +} + extern "C" void LLVMRustAddFunctionAttrStringValue(LLVMValueRef Fn, unsigned Index, const char *Name, -- cgit 1.4.1-3-g733a5