Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement feature for disabling opaque pointers on LLVM 15 and 16 #570

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,20 @@ edition = "2021"
[features]
default = ["target-all"]

typed-pointers = []

# Please update internal_macros::FEATURE_VERSIONS when adding a new LLVM version
llvm4-0 = ["llvm-sys-40"]
llvm5-0 = ["llvm-sys-50"]
llvm6-0 = ["llvm-sys-60"]
llvm7-0 = ["llvm-sys-70"]
llvm8-0 = ["llvm-sys-80"]
llvm9-0 = ["llvm-sys-90"]
llvm10-0 = ["llvm-sys-100"]
llvm11-0 = ["llvm-sys-110"]
llvm12-0 = ["llvm-sys-120"]
llvm13-0 = ["llvm-sys-130"]
llvm14-0 = ["llvm-sys-140"]
llvm4-0 = ["llvm-sys-40", "typed-pointers"]
llvm5-0 = ["llvm-sys-50", "typed-pointers"]
llvm6-0 = ["llvm-sys-60", "typed-pointers"]
llvm7-0 = ["llvm-sys-70", "typed-pointers"]
llvm8-0 = ["llvm-sys-80", "typed-pointers"]
llvm9-0 = ["llvm-sys-90", "typed-pointers"]
llvm10-0 = ["llvm-sys-100", "typed-pointers"]
llvm11-0 = ["llvm-sys-110", "typed-pointers"]
llvm12-0 = ["llvm-sys-120", "typed-pointers"]
llvm13-0 = ["llvm-sys-130", "typed-pointers"]
llvm14-0 = ["llvm-sys-140", "typed-pointers"]
llvm15-0 = ["llvm-sys-150"]
llvm16-0 = ["llvm-sys-160"]
llvm17-0 = ["llvm-sys-170"]
Expand Down
6 changes: 2 additions & 4 deletions examples/kaleidoscope/implementation_typed_pointers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ use inkwell::types::BasicMetadataTypeEnum;
use inkwell::values::{BasicMetadataValueEnum, BasicValueEnum, FloatValue, FunctionValue, PointerValue};
use inkwell::FloatPredicate;

use inkwell_internals::llvm_versions;

use crate::Token::*;

const ANONYMOUS_FUNCTION_NAME: &str = "anonymous";
Expand Down Expand Up @@ -856,12 +854,12 @@ impl<'a, 'ctx> Compiler<'a, 'ctx> {
builder.build_alloca(self.context.f64_type(), name).unwrap()
}

#[llvm_versions(..=14)]
#[cfg(feature = "typed-pointers")]
pub fn build_load(&self, ptr: PointerValue<'ctx>, name: &str) -> BasicValueEnum<'ctx> {
self.builder.build_load(ptr, name).unwrap()
}

#[llvm_versions(15..)]
#[cfg(not(feature = "typed-pointers"))]
pub fn build_load(&self, ptr: PointerValue<'ctx>, name: &str) -> BasicValueEnum<'ctx> {
self.builder.build_load(self.context.f64_type(), ptr, name).unwrap()
}
Expand Down
4 changes: 2 additions & 2 deletions src/basic_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,9 +288,9 @@ impl<'ctx> BasicBlock<'ctx> {
///
/// let void_type = context.void_type();
/// let i32_type = context.i32_type();
/// #[cfg(not(any(feature = "llvm15-0", feature = "llvm16-0", feature = "llvm17-0", feature = "llvm18-0")))]
/// #[cfg(feature = "typed-pointers")]
/// let i32_ptr_type = i32_type.ptr_type(AddressSpace::default());
/// #[cfg(any(feature = "llvm15-0", feature = "llvm16-0", feature = "llvm17-0", feature = "llvm18-0"))]
/// #[cfg(not(feature = "typed-pointers"))]
/// let i32_ptr_type = context.ptr_type(AddressSpace::default());
///
/// let fn_type = void_type.fn_type(&[i32_ptr_type.into()], false);
Expand Down
209 changes: 121 additions & 88 deletions src/builder.rs

Large diffs are not rendered by default.

19 changes: 14 additions & 5 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use crate::InlineAsmDialect;
use libc::c_void;
#[llvm_versions(..=6)]
use llvm_sys::core::LLVMConstInlineAsm;
#[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;
#[llvm_versions(7..)]
Expand All @@ -13,7 +15,7 @@ use llvm_sys::core::LLVMGetInlineAsm;
use llvm_sys::core::LLVMGetTypeByName2;
#[llvm_versions(6..)]
use llvm_sys::core::LLVMMetadataTypeInContext;
#[llvm_versions(15..)]
#[cfg(not(feature = "typed-pointers"))]
use llvm_sys::core::LLVMPointerTypeInContext;
use llvm_sys::core::{
LLVMAppendBasicBlockInContext, LLVMConstStringInContext, LLVMConstStructInContext, LLVMContextCreate,
Expand Down Expand Up @@ -43,7 +45,9 @@ use crate::targets::TargetData;
use crate::types::AnyTypeEnum;
#[llvm_versions(6..)]
use crate::types::MetadataType;
use crate::types::{AsTypeRef, BasicTypeEnum, FloatType, FunctionType, IntType, PointerType, StructType, VoidType};
#[cfg(not(feature = "typed-pointers"))]
use crate::types::PointerType;
use crate::types::{AsTypeRef, BasicTypeEnum, FloatType, FunctionType, IntType, StructType, VoidType};
use crate::values::{
ArrayValue, AsValueRef, BasicMetadataValueEnum, BasicValueEnum, FunctionValue, MetadataValue, PointerValue,
StructValue,
Expand Down Expand Up @@ -78,6 +82,11 @@ impl ContextImpl {
pub(crate) unsafe fn new(context: LLVMContextRef) -> Self {
assert!(!context.is_null());

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

ContextImpl(context)
}

Expand Down Expand Up @@ -245,7 +254,7 @@ impl ContextImpl {
unsafe { FloatType::new(LLVMPPCFP128TypeInContext(self.0)) }
}

#[llvm_versions(15..)]
#[cfg(not(feature = "typed-pointers"))]
fn ptr_type<'ctx>(&self, address_space: AddressSpace) -> PointerType<'ctx> {
unsafe { PointerType::new(LLVMPointerTypeInContext(self.0, address_space.0)) }
}
Expand Down Expand Up @@ -956,7 +965,7 @@ impl Context {
/// assert_eq!(ptr_type.get_address_space(), AddressSpace::default());
/// assert_eq!(ptr_type.get_context(), context);
/// ```
#[llvm_versions(15..)]
#[cfg(not(feature = "typed-pointers"))]
#[inline]
pub fn ptr_type(&self, address_space: AddressSpace) -> PointerType {
self.context.ptr_type(address_space)
Expand Down Expand Up @@ -1838,7 +1847,7 @@ impl<'ctx> ContextRef<'ctx> {
/// assert_eq!(ptr_type.get_address_space(), AddressSpace::default());
/// assert_eq!(ptr_type.get_context(), context);
/// ```
#[llvm_versions(15..)]
#[cfg(not(feature = "typed-pointers"))]
#[inline]
pub fn ptr_type(&self, address_space: AddressSpace) -> PointerType<'ctx> {
self.context.ptr_type(address_space)
Expand Down
21 changes: 21 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,27 @@ assert_unique_used_features! {
"llvm18-0"
}

#[cfg(all(
any(
feature = "llvm4-0",
feature = "llvm5-0",
feature = "llvm6-0",
feature = "llvm7-0",
feature = "llvm8-0",
feature = "llvm9-0",
feature = "llvm10-0",
feature = "llvm11-0",
feature = "llvm12-0",
feature = "llvm13-0",
feature = "llvm14-0"
),
not(feature = "typed-pointers")
))]
compile_error!("Opaque pointers are not supported prior to LLVM version 15.0.");

#[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.
///
/// The default address space is zero. An address space can always be created from a `u16`:
Expand Down
18 changes: 3 additions & 15 deletions src/types/array_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,25 +77,13 @@ impl<'ctx> ArrayType<'ctx> {
/// let i8_array_type = i8_type.array_type(3);
/// let i8_array_ptr_type = i8_array_type.ptr_type(AddressSpace::default());
///
/// #[cfg(any(
/// feature = "llvm4-0",
/// feature = "llvm5-0",
/// feature = "llvm6-0",
/// feature = "llvm7-0",
/// feature = "llvm8-0",
/// feature = "llvm9-0",
/// feature = "llvm10-0",
/// feature = "llvm11-0",
/// feature = "llvm12-0",
/// feature = "llvm13-0",
/// feature = "llvm14-0"
/// ))]
/// #[cfg(feature = "typed-pointers")]
/// assert_eq!(i8_array_ptr_type.get_element_type().into_array_type(), i8_array_type);
/// ```
#[cfg_attr(
any(
feature = "llvm15-0",
feature = "llvm16-0",
all(feature = "llvm15-0", not(feature = "typed-pointers")),
all(feature = "llvm16-0", not(feature = "typed-pointers")),
feature = "llvm17-0",
feature = "llvm18-0"
),
Expand Down
18 changes: 3 additions & 15 deletions src/types/float_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,25 +234,13 @@ impl<'ctx> FloatType<'ctx> {
/// let f32_type = context.f32_type();
/// let f32_ptr_type = f32_type.ptr_type(AddressSpace::default());
///
/// #[cfg(any(
/// feature = "llvm4-0",
/// feature = "llvm5-0",
/// feature = "llvm6-0",
/// feature = "llvm7-0",
/// feature = "llvm8-0",
/// feature = "llvm9-0",
/// feature = "llvm10-0",
/// feature = "llvm11-0",
/// feature = "llvm12-0",
/// feature = "llvm13-0",
/// feature = "llvm14-0"
/// ))]
/// #[cfg(feature = "typed-pointers")]
/// assert_eq!(f32_ptr_type.get_element_type().into_float_type(), f32_type);
/// ```
#[cfg_attr(
any(
feature = "llvm15-0",
feature = "llvm16-0",
all(feature = "llvm15-0", not(feature = "typed-pointers")),
all(feature = "llvm16-0", not(feature = "typed-pointers")),
feature = "llvm17-0",
feature = "llvm18-0"
),
Expand Down
18 changes: 3 additions & 15 deletions src/types/fn_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,25 +45,13 @@ impl<'ctx> FunctionType<'ctx> {
/// let fn_type = f32_type.fn_type(&[], false);
/// let fn_ptr_type = fn_type.ptr_type(AddressSpace::default());
///
/// #[cfg(any(
/// feature = "llvm4-0",
/// feature = "llvm5-0",
/// feature = "llvm6-0",
/// feature = "llvm7-0",
/// feature = "llvm8-0",
/// feature = "llvm9-0",
/// feature = "llvm10-0",
/// feature = "llvm11-0",
/// feature = "llvm12-0",
/// feature = "llvm13-0",
/// feature = "llvm14-0"
/// ))]
/// #[cfg(feature = "typed-pointers")]
/// assert_eq!(fn_ptr_type.get_element_type().into_function_type(), fn_type);
/// ```
#[cfg_attr(
any(
feature = "llvm15-0",
feature = "llvm16-0",
all(feature = "llvm15-0", not(feature = "typed-pointers")),
all(feature = "llvm16-0", not(feature = "typed-pointers")),
feature = "llvm17-0",
feature = "llvm18-0"
),
Expand Down
18 changes: 3 additions & 15 deletions src/types/int_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,25 +321,13 @@ impl<'ctx> IntType<'ctx> {
/// let i8_type = context.i8_type();
/// let i8_ptr_type = i8_type.ptr_type(AddressSpace::default());
///
/// #[cfg(any(
/// feature = "llvm4-0",
/// feature = "llvm5-0",
/// feature = "llvm6-0",
/// feature = "llvm7-0",
/// feature = "llvm8-0",
/// feature = "llvm9-0",
/// feature = "llvm10-0",
/// feature = "llvm11-0",
/// feature = "llvm12-0",
/// feature = "llvm13-0",
/// feature = "llvm14-0"
/// ))]
/// #[cfg(feature = "typed-pointers")]
/// assert_eq!(i8_ptr_type.get_element_type().into_int_type(), i8_type);
/// ```
#[cfg_attr(
any(
feature = "llvm15-0",
feature = "llvm16-0",
all(feature = "llvm15-0", not(feature = "typed-pointers")),
all(feature = "llvm16-0", not(feature = "typed-pointers")),
feature = "llvm17-0",
feature = "llvm18-0"
),
Expand Down
Loading
Loading