Skip to content

Commit

Permalink
Merge pull request #9 from TheZoq2/fix_rust_analyzer
Browse files Browse the repository at this point in the history
Use features instead of build.rs to select llvm_x_or_greater
  • Loading branch information
cdisselkoen committed Dec 18, 2020
2 parents c50b12c + 7f62ecf commit fd90527
Show file tree
Hide file tree
Showing 17 changed files with 381 additions and 389 deletions.
21 changes: 17 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,23 @@ itertools = "0.9"
[features]
# Select the LLVM version to be compatible with.
# You _must_ enable exactly one of the following features.
llvm-8 = ["llvm-sys-80"]
llvm-9 = ["llvm-sys-90"]
llvm-10 = ["llvm-sys-100"]
llvm-11 = ["llvm-sys-110"]
llvm-8 = ["llvm-sys-80", "llvm-8-or-lower", "llvm-8-or-greater"]
llvm-9 = ["llvm-sys-90", "llvm-9-or-lower", "llvm-9-or-greater"]
llvm-10 = ["llvm-sys-100", "llvm-10-or-lower", "llvm-10-or-greater"]
llvm-11 = ["llvm-sys-110", "llvm-11-or-lower", "llvm-11-or-greater"]

# For convenience, these automatically-enabled features allow us to avoid
# checking complex combinations of features all the time. they are not meant to
# be manually enabled, use the above llvm-x features instead
llvm-8-or-greater = []
llvm-9-or-greater = ["llvm-8-or-greater"]
llvm-10-or-greater = ["llvm-9-or-greater"]
llvm-11-or-greater = ["llvm-10-or-greater"]

llvm-8-or-lower = ["llvm-9-or-lower"]
llvm-9-or-lower = ["llvm-10-or-lower"]
llvm-10-or-lower = ["llvm-11-or-lower"]
llvm-11-or-lower = []

# We'd like to have a "strict-versioning" feature which enables the
# corresponding feature in llvm-sys. "strict-versioning" requires an exact
Expand Down
25 changes: 2 additions & 23 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,9 @@ fn main() {
if cfg!(feature = "llvm-11") {
versions.push(11);
}
let selected_version = match versions.len() {
match versions.len() {
0 => panic!("llvm-ir: Please select an LLVM version using a Cargo feature."),
1 => versions[0],
1 => {},
_ => panic!("llvm-ir: Multiple LLVM versions selected. Please activate only one LLVM version feature. (Got {:?})", versions),
};

// For convenience we set a number of configuration options to avoid
// checking complex combinations of features all the time.
if selected_version >= 9 {
println!("cargo:rustc-cfg=LLVM_VERSION_9_OR_GREATER");
}
if selected_version >= 10 {
println!("cargo:rustc-cfg=LLVM_VERSION_10_OR_GREATER");
}
if selected_version >= 11 {
println!("cargo:rustc-cfg=LLVM_VERSION_11_OR_GREATER");
}
if selected_version <= 10 {
println!("cargo:rustc-cfg=LLVM_VERSION_10_OR_LOWER");
}
if selected_version <= 9 {
println!("cargo:rustc-cfg=LLVM_VERSION_9_OR_LOWER");
}
if selected_version <= 8 {
println!("cargo:rustc-cfg=LLVM_VERSION_8_OR_LOWER");
}
}
4 changes: 2 additions & 2 deletions src/basicblock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl BasicBlock {
name,
instrs: vec![],
term: Terminator::Unreachable(Unreachable {
#[cfg(LLVM_VERSION_9_OR_GREATER)]
#[cfg(feature="llvm-9-or-greater")]
debugloc: None,
}),
}
Expand Down Expand Up @@ -113,7 +113,7 @@ fn term_needs_name(term: LLVMValueRef) -> bool {
match unsafe { LLVMGetInstructionOpcode(term) } {
LLVMOpcode::LLVMInvoke => true,
LLVMOpcode::LLVMCatchSwitch => true,
#[cfg(LLVM_VERSION_9_OR_GREATER)]
#[cfg(feature="llvm-9-or-greater")]
LLVMOpcode::LLVMCallBr => true,
_ => false, // all other terminators have no result (destination) and thus don't need names
}
Expand Down
28 changes: 14 additions & 14 deletions src/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ pub enum Constant {
#[allow(non_camel_case_types)]
pub enum Float {
Half, // TODO perhaps Half(u16)
#[cfg(LLVM_VERSION_11_OR_GREATER)]
#[cfg(feature="llvm-11-or-greater")]
BFloat, // TODO perhaps BFloat(u16)
Single(f32),
Double(f64),
Expand All @@ -136,7 +136,7 @@ impl Typed for Float {
fn get_type(&self, types: &Types) -> TypeRef {
types.fp(match self {
Float::Half => FPType::Half,
#[cfg(LLVM_VERSION_11_OR_GREATER)]
#[cfg(feature="llvm-11-or-greater")]
Float::BFloat => FPType::BFloat,
Float::Single(_) => FPType::Single,
Float::Double(_) => FPType::Double,
Expand All @@ -151,7 +151,7 @@ impl Display for Float {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Float::Half => write!(f, "half"),
#[cfg(LLVM_VERSION_11_OR_GREATER)]
#[cfg(feature="llvm-11-or-greater")]
Float::BFloat => write!(f, "bfloat"),
Float::Single(s) => write!(f, "float {}", s),
Float::Double(d) => write!(f, "double {}", d),
Expand All @@ -178,13 +178,13 @@ impl Typed for Constant {
element_type.clone(),
elements.len(),
),
#[cfg(LLVM_VERSION_11_OR_GREATER)]
#[cfg(feature="llvm-11-or-greater")]
Constant::Vector(v) => types.vector_of(
types.type_of(&v[0]),
v.len(),
false, // I don't think it's possible (at least as of LLVM 11) to have a constant of scalable vector type?
),
#[cfg(LLVM_VERSION_10_OR_LOWER)]
#[cfg(feature="llvm-10-or-lower")]
Constant::Vector(v) => types.vector_of(
types.type_of(&v[0]),
v.len(),
Expand Down Expand Up @@ -793,11 +793,11 @@ impl Typed for ShuffleVector {
debug_assert_eq!(ty, types.type_of(&self.operand1));
match ty.as_ref() {
Type::VectorType { element_type, .. } => match types.type_of(&self.mask).as_ref() {
#[cfg(LLVM_VERSION_11_OR_GREATER)]
#[cfg(feature="llvm-11-or-greater")]
Type::VectorType { num_elements, scalable, .. } => {
types.vector_of(element_type.clone(), *num_elements, *scalable)
},
#[cfg(LLVM_VERSION_10_OR_LOWER)]
#[cfg(feature="llvm-10-or-lower")]
Type::VectorType { num_elements, .. } => {
types.vector_of(element_type.clone(), *num_elements)
},
Expand Down Expand Up @@ -1095,11 +1095,11 @@ impl Typed for ICmp {
let ty = types.type_of(&self.operand0);
debug_assert_eq!(ty, types.type_of(&self.operand1));
match ty.as_ref() {
#[cfg(LLVM_VERSION_11_OR_GREATER)]
#[cfg(feature="llvm-11-or-greater")]
Type::VectorType { num_elements, scalable, .. } => {
types.vector_of(types.bool(), *num_elements, *scalable)
},
#[cfg(LLVM_VERSION_10_OR_LOWER)]
#[cfg(feature="llvm-10-or-lower")]
Type::VectorType { num_elements, .. } => {
types.vector_of(types.bool(), *num_elements)
},
Expand Down Expand Up @@ -1131,11 +1131,11 @@ impl Typed for FCmp {
let ty = types.type_of(&self.operand0);
debug_assert_eq!(ty, types.type_of(&self.operand1));
match ty.as_ref() {
#[cfg(LLVM_VERSION_11_OR_GREATER)]
#[cfg(feature="llvm-11-or-greater")]
Type::VectorType { num_elements, scalable, .. } => {
types.vector_of(types.bool(), *num_elements, *scalable)
},
#[cfg(LLVM_VERSION_10_OR_LOWER)]
#[cfg(feature="llvm-10-or-lower")]
Type::VectorType { num_elements, .. } => {
types.vector_of(types.bool(), *num_elements)
},
Expand Down Expand Up @@ -1219,7 +1219,7 @@ impl Constant {
match ctx.types.type_from_llvm_ref( unsafe { LLVMTypeOf(constant) } ).as_ref() {
Type::FPType(fptype) => Constant::Float(match fptype {
FPType::Half => Float::Half,
#[cfg(LLVM_VERSION_11_OR_GREATER)]
#[cfg(feature="llvm-11-or-greater")]
FPType::BFloat => Float::BFloat,
FPType::Single => Float::Single( unsafe {
let mut b = 0;
Expand Down Expand Up @@ -1427,7 +1427,7 @@ impl InsertElement {
}

impl ShuffleVector {
#[cfg(LLVM_VERSION_10_OR_LOWER)]
#[cfg(feature="llvm-10-or-lower")]
pub(crate) fn from_llvm_ref(expr: LLVMValueRef, ctx: &mut ModuleContext) -> Self {
assert_eq!(unsafe { LLVMGetNumOperands(expr) }, 3);
Self {
Expand All @@ -1436,7 +1436,7 @@ impl ShuffleVector {
mask: Constant::from_llvm_ref(unsafe { LLVMGetOperand(expr, 2) }, ctx),
}
}
#[cfg(LLVM_VERSION_11_OR_GREATER)]
#[cfg(feature="llvm-11-or-greater")]
pub(crate) fn from_llvm_ref(expr: LLVMValueRef, _ctx: &mut ModuleContext) -> Self {
assert_eq!(unsafe { LLVMGetNumOperands(expr) }, 2);
// We currently (as of LLVM 11) have no way to get the mask of a
Expand Down
6 changes: 3 additions & 3 deletions src/from_llvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ macro_rules! wrap_with_len {
};
}

#[cfg(LLVM_VERSION_9_OR_GREATER)]
#[cfg(feature="llvm-9-or-greater")]
macro_rules! wrap_with_len_maybe_null {
($llvmFunc:ident, $argty:ty, $wrapperFunc:ident) => {
pub unsafe fn $wrapperFunc(arg: $argty) -> Option<String> {
Expand Down Expand Up @@ -75,9 +75,9 @@ wrap!(LLVMPrintValueToString, LLVMValueRef, print_to_string);
// wrap!(LLVMPrintTypeToString, LLVMTypeRef, print_type_to_string);
wrap_with_len!(LLVMGetStringAttributeKind, LLVMAttributeRef, get_string_attribute_kind);
wrap_with_len!(LLVMGetStringAttributeValue, LLVMAttributeRef, get_string_attribute_value);
#[cfg(LLVM_VERSION_9_OR_GREATER)]
#[cfg(feature="llvm-9-or-greater")]
wrap_with_len_maybe_null!(LLVMGetDebugLocFilename, LLVMValueRef, get_debugloc_filename);
#[cfg(LLVM_VERSION_9_OR_GREATER)]
#[cfg(feature="llvm-9-or-greater")]
wrap_with_len_maybe_null!(LLVMGetDebugLocDirectory, LLVMValueRef, get_debugloc_directory);

// Panics if the LLVMValueRef is not a basic block
Expand Down

0 comments on commit fd90527

Please sign in to comment.