diff --git a/src/rustllvm/RustWrapper.cpp b/src/rustllvm/RustWrapper.cpp index 85749d883d2f3..f5fa66f1b0e5a 100644 --- a/src/rustllvm/RustWrapper.cpp +++ b/src/rustllvm/RustWrapper.cpp @@ -651,17 +651,32 @@ extern "C" LLVMRustMetadataRef LLVMRustDIBuilderCreateStaticVariable( bool isLocalToUnit, LLVMValueRef Val, LLVMRustMetadataRef Decl = NULL, - uint64_t AlignInBits = 0) -{ - return wrap(Builder->createGlobalVariable( - unwrapDI(Context), + uint64_t AlignInBits = 0) { + Constant *InitVal = cast(unwrap(Val)); + +#if LLVM_VERSION_GE(4, 0) + llvm::DIExpression *InitExpr = nullptr; + if (llvm::ConstantInt *IntVal = llvm::dyn_cast(InitVal)) { + InitExpr = Builder->createConstantValueExpression( + IntVal->getValue().getSExtValue()); + } else if (llvm::ConstantFP *FPVal = llvm::dyn_cast(InitVal)) { + InitExpr = Builder->createConstantValueExpression( + FPVal->getValueAPF().bitcastToAPInt().getZExtValue()); + } +#endif + + return wrap(Builder->createGlobalVariable(unwrapDI(Context), Name, LinkageName, unwrapDI(File), LineNo, unwrapDI(Ty), isLocalToUnit, - cast(unwrap(Val)), +#if LLVM_VERSION_GE(4, 0) + InitExpr, +#else + InitVal, +#endif unwrapDIptr(Decl) #if LLVM_VERSION_GE(4, 0) , AlignInBits