Skip to content

Commit ce32f64

Browse files
lucabtamird
authored andcommitted
rustc_trans: don't hardcode llvm version for conditional intrinsics
This commit introduce a third parameter for compatible_ifn!, as new intrinsics are being added in recent LLVM releases and there is no need to hardcode a specific case. Signed-off-by: Luca Bruno <[email protected]>
1 parent 1be9e6f commit ce32f64

File tree

3 files changed

+27
-10
lines changed

3 files changed

+27
-10
lines changed

src/librustc_llvm/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1772,6 +1772,8 @@ extern {
17721772
-> ValueRef;
17731773

17741774
pub fn LLVMRustDebugMetadataVersion() -> u32;
1775+
pub fn LLVMVersionMajor() -> u32;
1776+
pub fn LLVMVersionMinor() -> u32;
17751777

17761778
pub fn LLVMRustAddModuleFlag(M: ModuleRef,
17771779
name: *const c_char,

src/librustc_trans/trans/context.rs

+17-10
Original file line numberDiff line numberDiff line change
@@ -870,6 +870,11 @@ fn declare_intrinsic(ccx: &CrateContext, key: & &'static str) -> Option<ValueRef
870870
ifn!("llvm.trunc.f32", fn(t_f32) -> t_f32);
871871
ifn!("llvm.trunc.f64", fn(t_f64) -> t_f64);
872872

873+
ifn!("llvm.copysign.f32", fn(t_f32, t_f32) -> t_f32);
874+
ifn!("llvm.copysign.f64", fn(t_f64, t_f64) -> t_f64);
875+
ifn!("llvm.round.f32", fn(t_f32) -> t_f32);
876+
ifn!("llvm.round.f64", fn(t_f64) -> t_f64);
877+
873878
ifn!("llvm.rint.f32", fn(t_f32) -> t_f32);
874879
ifn!("llvm.rint.f64", fn(t_f64) -> t_f64);
875880
ifn!("llvm.nearbyint.f32", fn(t_f32) -> t_f32);
@@ -931,20 +936,22 @@ fn declare_intrinsic(ccx: &CrateContext, key: & &'static str) -> Option<ValueRef
931936
ifn!("llvm.assume", fn(i1) -> void);
932937

933938
// Some intrinsics were introduced in later versions of LLVM, but they have
934-
// fallbacks in libc or libm and such. Currently, all of these intrinsics
935-
// were introduced in LLVM 3.4, so we case on that.
939+
// fallbacks in libc or libm and such.
936940
macro_rules! compatible_ifn {
937-
($name:expr, $cname:ident ($($arg:expr),*) -> $ret:expr) => (
938-
ifn!($name, fn($($arg),*) -> $ret);
941+
($name:expr, $cname:ident ($($arg:expr),*) -> $ret:expr, $llvm_version:expr) => (
942+
if unsafe { llvm::LLVMVersionMinor() >= $llvm_version } {
943+
// The `if key == $name` is already in ifn!
944+
ifn!($name, fn($($arg),*) -> $ret);
945+
} else if *key == $name {
946+
let f = declare::declare_cfn(ccx, stringify!($cname),
947+
Type::func(&[$($arg),*], &$ret),
948+
ty::mk_nil(ccx.tcx()));
949+
ccx.intrinsics().borrow_mut().insert($name, f.clone());
950+
return Some(f);
951+
}
939952
)
940953
}
941954

942-
compatible_ifn!("llvm.copysign.f32", copysignf(t_f32, t_f32) -> t_f32);
943-
compatible_ifn!("llvm.copysign.f64", copysign(t_f64, t_f64) -> t_f64);
944-
compatible_ifn!("llvm.round.f32", roundf(t_f32) -> t_f32);
945-
compatible_ifn!("llvm.round.f64", round(t_f64) -> t_f64);
946-
947-
948955
if ccx.sess().opts.debuginfo != NoDebugInfo {
949956
ifn!("llvm.dbg.declare", fn(Type::metadata(ccx), Type::metadata(ccx)) -> void);
950957
ifn!("llvm.dbg.value", fn(Type::metadata(ccx), t_i64, Type::metadata(ccx)) -> void);

src/rustllvm/RustWrapper.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,14 @@ extern "C" uint32_t LLVMRustDebugMetadataVersion() {
237237
return DEBUG_METADATA_VERSION;
238238
}
239239

240+
extern "C" uint32_t LLVMVersionMinor() {
241+
return LLVM_VERSION_MINOR;
242+
}
243+
244+
extern "C" uint32_t LLVMVersionMajor() {
245+
return LLVM_VERSION_MAJOR;
246+
}
247+
240248
extern "C" void LLVMRustAddModuleFlag(LLVMModuleRef M,
241249
const char *name,
242250
uint32_t value) {

0 commit comments

Comments
 (0)