Skip to content

Commit

Permalink
Extend ability to disable opaque pointers to LLVM 16
Browse files Browse the repository at this point in the history
  • Loading branch information
Derppening committed Feb 17, 2025
1 parent 20153a6 commit 4a3d0b8
Show file tree
Hide file tree
Showing 13 changed files with 100 additions and 21 deletions.
64 changes: 62 additions & 2 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ use llvm_sys::core::{
use llvm_sys::core::{LLVMBuildCall, LLVMBuildInvoke};
#[llvm_versions(15..)]
use llvm_sys::core::{LLVMBuildCall2, LLVMBuildInvoke2};
#[cfg(feature = "typed-pointers")]
#[cfg(all(feature = "typed-pointers", not(feature = "llvm16-0")))]
#[cfg_attr(feature = "llvm15-0", allow(deprecated))]
use llvm_sys::core::{LLVMBuildGEP, LLVMBuildInBoundsGEP, LLVMBuildLoad, LLVMBuildPtrDiff, LLVMBuildStructGEP};
#[cfg(not(feature = "typed-pointers"))]
#[cfg(any(not(feature = "typed-pointers"), feature = "llvm16-0"))]
use llvm_sys::core::{LLVMBuildGEP2, LLVMBuildInBoundsGEP2, LLVMBuildLoad2, LLVMBuildPtrDiff2, LLVMBuildStructGEP2};
#[llvm_versions(8..)]
use llvm_sys::core::{LLVMBuildIntCast2, LLVMBuildMemCpy, LLVMBuildMemMove, LLVMBuildMemSet};
Expand Down Expand Up @@ -1097,6 +1097,7 @@ impl<'ctx> Builder<'ctx> {

let mut index_values: Vec<LLVMValueRef> = ordered_indexes.iter().map(|val| val.as_value_ref()).collect();

#[cfg(not(feature = "llvm16-0"))]
#[cfg_attr(feature = "llvm15-0", allow(deprecated))]
let value = LLVMBuildGEP(
self.builder,
Expand All @@ -1105,6 +1106,15 @@ impl<'ctx> Builder<'ctx> {
index_values.len() as u32,
c_string.as_ptr(),
);
#[cfg(feature = "llvm16-0")]
let value = LLVMBuildGEP2(
self.builder,
ptr.get_type().get_element_type().as_type_ref(),
ptr.as_value_ref(),
index_values.as_mut_ptr(),
index_values.len() as u32,
c_string.as_ptr(),
);

Ok(PointerValue::new(value))
}
Expand Down Expand Up @@ -1155,6 +1165,7 @@ impl<'ctx> Builder<'ctx> {

let mut index_values: Vec<LLVMValueRef> = ordered_indexes.iter().map(|val| val.as_value_ref()).collect();

#[cfg(not(feature = "llvm16-0"))]
#[cfg_attr(feature = "llvm15-0", allow(deprecated))]
let value = LLVMBuildInBoundsGEP(
self.builder,
Expand All @@ -1163,6 +1174,15 @@ impl<'ctx> Builder<'ctx> {
index_values.len() as u32,
c_string.as_ptr(),
);
#[cfg(feature = "llvm16-0")]
let value = LLVMBuildInBoundsGEP2(
self.builder,
ptr.get_type().get_element_type().as_type_ref(),
ptr.as_value_ref(),
index_values.as_mut_ptr(),
index_values.len() as u32,
c_string.as_ptr(),
);

Ok(PointerValue::new(value))
}
Expand Down Expand Up @@ -1258,8 +1278,19 @@ impl<'ctx> Builder<'ctx> {

let c_string = to_c_str(name);

#[cfg(not(feature = "llvm16-0"))]
#[cfg_attr(feature = "llvm15-0", allow(deprecated))]
let value = unsafe { LLVMBuildStructGEP(self.builder, ptr.as_value_ref(), index, c_string.as_ptr()) };
#[cfg(feature = "llvm16-0")]
let value = unsafe {
LLVMBuildStructGEP2(
self.builder,
ptr.get_type().get_element_type().as_type_ref(),
ptr.as_value_ref(),
index,
c_string.as_ptr(),
)
};

unsafe { Ok(PointerValue::new(value)) }
}
Expand Down Expand Up @@ -1377,6 +1408,7 @@ impl<'ctx> Builder<'ctx> {
return Err(BuilderError::UnsetPosition);
}
let c_string = to_c_str(name);
#[cfg(not(feature = "llvm16-0"))]
#[cfg_attr(feature = "llvm15-0", allow(deprecated))]
let value = unsafe {
LLVMBuildPtrDiff(
Expand All @@ -1386,6 +1418,24 @@ impl<'ctx> Builder<'ctx> {
c_string.as_ptr(),
)
};
#[cfg(feature = "llvm16-0")]
let value = {
if lhs_ptr.get_type().as_basic_type_enum() != rhs_ptr.get_type().as_basic_type_enum() {
return Err(BuilderError::ValueTypeMismatch(
"The values must have the same pointer type.",
));
}

unsafe {
LLVMBuildPtrDiff2(
self.builder,
lhs_ptr.get_type().get_element_type().as_type_ref(),
lhs_ptr.as_value_ref(),
rhs_ptr.as_value_ref(),
c_string.as_ptr(),
)
}
};

unsafe { Ok(IntValue::new(value)) }
}
Expand Down Expand Up @@ -1535,8 +1585,18 @@ impl<'ctx> Builder<'ctx> {
}
let c_string = to_c_str(name);

#[cfg(not(feature = "llvm16-0"))]
#[cfg_attr(feature = "llvm15-0", allow(deprecated))]
let value = unsafe { LLVMBuildLoad(self.builder, ptr.as_value_ref(), c_string.as_ptr()) };
#[cfg(feature = "llvm16-0")]
let value = unsafe {
LLVMBuildLoad2(
self.builder,
ptr.get_type().get_element_type().as_type_ref(),
ptr.as_value_ref(),
c_string.as_ptr(),
)
};

unsafe { Ok(BasicValueEnum::new(value)) }
}
Expand Down
8 changes: 5 additions & 3 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::InlineAsmDialect;
use libc::c_void;
#[llvm_versions(..=6)]
use llvm_sys::core::LLVMConstInlineAsm;
#[cfg(all(feature = "llvm15-0", feature = "typed-pointers"))]
#[cfg(all(any(feature = "llvm15-0", feature = "llvm16-0"), feature = "typed-pointers"))]
use llvm_sys::core::LLVMContextSetOpaquePointers;
#[llvm_versions(12..)]
use llvm_sys::core::LLVMCreateTypeAttribute;
Expand Down Expand Up @@ -82,8 +82,10 @@ impl ContextImpl {
pub(crate) unsafe fn new(context: LLVMContextRef) -> Self {
assert!(!context.is_null());

#[cfg(all(feature = "llvm15-0", feature = "typed-pointers"))]
unsafe { LLVMContextSetOpaquePointers(context, 0) };
#[cfg(all(any(feature = "llvm15-0", feature = "llvm16-0"), feature = "typed-pointers"))]
unsafe {
LLVMContextSetOpaquePointers(context, 0)
};

ContextImpl(context)
}
Expand Down
5 changes: 1 addition & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,7 @@ assert_unique_used_features! {
))]
compile_error!("Opaque pointers are not supported prior to LLVM version 15.0.");

#[cfg(all(
any(feature = "llvm16-0", feature = "llvm17-0", feature = "llvm18-0"),
feature = "typed-pointers"
))]
#[cfg(all(any(feature = "llvm17-0", feature = "llvm18-0"), feature = "typed-pointers"))]
compile_error!("Typed pointers are not supported starting from LLVM version 17.0.");

/// Defines the address space in which a global will be inserted.
Expand Down
2 changes: 1 addition & 1 deletion src/types/array_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ impl<'ctx> ArrayType<'ctx> {
#[cfg_attr(
any(
all(feature = "llvm15-0", not(feature = "typed-pointers")),
feature = "llvm16-0",
all(feature = "llvm16-0", not(feature = "typed-pointers")),
feature = "llvm17-0",
feature = "llvm18-0"
),
Expand Down
2 changes: 1 addition & 1 deletion src/types/float_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ impl<'ctx> FloatType<'ctx> {
#[cfg_attr(
any(
all(feature = "llvm15-0", not(feature = "typed-pointers")),
feature = "llvm16-0",
all(feature = "llvm16-0", not(feature = "typed-pointers")),
feature = "llvm17-0",
feature = "llvm18-0"
),
Expand Down
2 changes: 1 addition & 1 deletion src/types/fn_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl<'ctx> FunctionType<'ctx> {
#[cfg_attr(
any(
all(feature = "llvm15-0", not(feature = "typed-pointers")),
feature = "llvm16-0",
all(feature = "llvm16-0", not(feature = "typed-pointers")),
feature = "llvm17-0",
feature = "llvm18-0"
),
Expand Down
2 changes: 1 addition & 1 deletion src/types/int_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ impl<'ctx> IntType<'ctx> {
#[cfg_attr(
any(
all(feature = "llvm15-0", not(feature = "typed-pointers")),
feature = "llvm16-0",
all(feature = "llvm16-0", not(feature = "typed-pointers")),
feature = "llvm17-0",
feature = "llvm18-0"
),
Expand Down
4 changes: 2 additions & 2 deletions src/types/ptr_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ impl<'ctx> PointerType<'ctx> {
#[cfg_attr(
any(
all(feature = "llvm15-0", not(feature = "typed-pointers")),
feature = "llvm16-0",
all(feature = "llvm16-0", not(feature = "typed-pointers")),
feature = "llvm17-0",
feature = "llvm18-0"
),
Expand Down Expand Up @@ -356,7 +356,7 @@ impl<'ctx> PointerType<'ctx> {
///
/// assert_eq!(f32_ptr_type.get_element_type().into_float_type(), f32_type);
/// ```
#[llvm_versions(..=15)]
#[llvm_versions(..=16)]
#[cfg(feature = "typed-pointers")]
pub fn get_element_type(self) -> AnyTypeEnum<'ctx> {
self.ptr_type.get_element_type()
Expand Down
2 changes: 1 addition & 1 deletion src/types/scalable_vec_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ impl<'ctx> ScalableVectorType<'ctx> {
#[cfg_attr(
any(
all(feature = "llvm15-0", not(feature = "typed-pointers")),
feature = "llvm16-0",
all(feature = "llvm16-0", not(feature = "typed-pointers")),
feature = "llvm17-0",
feature = "llvm18-0"
),
Expand Down
2 changes: 1 addition & 1 deletion src/types/struct_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ impl<'ctx> StructType<'ctx> {
#[cfg_attr(
any(
all(feature = "llvm15-0", not(feature = "typed-pointers")),
feature = "llvm16-0",
all(feature = "llvm16-0", not(feature = "typed-pointers")),
feature = "llvm17-0",
feature = "llvm18-0"
),
Expand Down
2 changes: 1 addition & 1 deletion src/types/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ pub unsafe trait BasicType<'ctx>: AnyType<'ctx> {
#[cfg_attr(
any(
all(feature = "llvm15-0", not(feature = "typed-pointers")),
feature = "llvm16-0",
all(feature = "llvm16-0", not(feature = "typed-pointers")),
feature = "llvm17-0",
feature = "llvm18-0"
),
Expand Down
2 changes: 1 addition & 1 deletion src/types/vec_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ impl<'ctx> VectorType<'ctx> {
#[cfg_attr(
any(
all(feature = "llvm15-0", not(feature = "typed-pointers")),
feature = "llvm16-0",
all(feature = "llvm16-0", not(feature = "typed-pointers")),
feature = "llvm17-0",
feature = "llvm18-0"
),
Expand Down
24 changes: 22 additions & 2 deletions src/values/ptr_value.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#[cfg(feature = "typed-pointers")]
#[cfg(all(feature = "typed-pointers", not(feature = "llvm16-0")))]
#[cfg_attr(feature = "llvm15-0", allow(deprecated))]
use llvm_sys::core::{LLVMConstGEP, LLVMConstInBoundsGEP};
#[cfg(not(feature = "typed-pointers"))]
#[cfg(any(not(feature = "typed-pointers"), feature = "llvm16-0"))]
use llvm_sys::core::{LLVMConstGEP2, LLVMConstInBoundsGEP2};

use llvm_sys::core::{LLVMConstAddrSpaceCast, LLVMConstPointerCast, LLVMConstPtrToInt};
Expand Down Expand Up @@ -89,6 +89,7 @@ impl<'ctx> PointerValue<'ctx> {
pub unsafe fn const_gep(self, ordered_indexes: &[IntValue<'ctx>]) -> PointerValue<'ctx> {
let mut index_values: Vec<LLVMValueRef> = ordered_indexes.iter().map(|val| val.as_value_ref()).collect();

#[cfg(not(feature = "llvm16-0"))]
#[cfg_attr(feature = "llvm15-0", allow(deprecated))]
let value = {
LLVMConstGEP(
Expand All @@ -97,6 +98,15 @@ impl<'ctx> PointerValue<'ctx> {
index_values.len() as u32,
)
};
#[cfg(feature = "llvm16-0")]
let value = {
LLVMConstGEP2(
self.get_type().get_element_type().as_type_ref(),
self.as_value_ref(),
index_values.as_mut_ptr(),
index_values.len() as u32,
)
};

PointerValue::new(value)
}
Expand Down Expand Up @@ -124,6 +134,7 @@ impl<'ctx> PointerValue<'ctx> {
pub unsafe fn const_in_bounds_gep(self, ordered_indexes: &[IntValue<'ctx>]) -> PointerValue<'ctx> {
let mut index_values: Vec<LLVMValueRef> = ordered_indexes.iter().map(|val| val.as_value_ref()).collect();

#[cfg(not(feature = "llvm16-0"))]
#[cfg_attr(feature = "llvm15-0", allow(deprecated))]
let value = {
LLVMConstInBoundsGEP(
Expand All @@ -132,6 +143,15 @@ impl<'ctx> PointerValue<'ctx> {
index_values.len() as u32,
)
};
#[cfg(feature = "llvm16-0")]
let value = {
LLVMConstInBoundsGEP2(
self.get_type().get_element_type().as_type_ref(),
self.as_value_ref(),
index_values.as_mut_ptr(),
index_values.len() as u32,
)
};

PointerValue::new(value)
}
Expand Down

0 comments on commit 4a3d0b8

Please sign in to comment.