Skip to content

Commit

Permalink
Support LLVM 17
Browse files Browse the repository at this point in the history
This includes the following LLVM-C API changes:

* Core
  * Removed functions:
    * LLVMContextSetOpaquePointers
    * LLVMConstSelect
* Transforms
  * PassRegistry and pass initialization removed, as they were
    the part of legacy Pass Manager. This results in complete
    removal of the following modules:
    * instcombine
    * ipo
    * pass_manager_builder
    * scalar
    * util
    * vectorize
* Debug info
  * New language:
    * Mojo

Fixes: #444
  • Loading branch information
vadorovsky authored and SUPERCILEX committed Jan 10, 2024
1 parent c18e3e8 commit a34bff2
Show file tree
Hide file tree
Showing 14 changed files with 208 additions and 58 deletions.
7 changes: 7 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ llvm13-0 = ["llvm-sys-130"]
llvm14-0 = ["llvm-sys-140"]
llvm15-0 = ["llvm-sys-150"]
llvm16-0 = ["llvm-sys-160"]
llvm17-0 = ["llvm-sys-170"]
# Don't link against LLVM libraries. This is useful if another dependency is
# installing LLVM. See llvm-sys for more details. We can't enable a single
# `no-llvm-linking` feature across the board of llvm versions, as it'll cause
Expand All @@ -45,6 +46,7 @@ llvm13-0-no-llvm-linking = ["llvm13-0", "llvm-sys-130/no-llvm-linking"]
llvm14-0-no-llvm-linking = ["llvm14-0", "llvm-sys-140/no-llvm-linking"]
llvm15-0-no-llvm-linking = ["llvm15-0", "llvm-sys-150/no-llvm-linking"]
llvm16-0-no-llvm-linking = ["llvm16-0", "llvm-sys-160/no-llvm-linking"]
llvm17-0-no-llvm-linking = ["llvm17-0", "llvm-sys-170/no-llvm-linking"]

# Linking preference.
# If none of these are enabled, it defaults to force static linking.
Expand All @@ -55,27 +57,31 @@ llvm13-0-force-dynamic = ["llvm13-0", "llvm-sys-130/force-dynamic"]
llvm14-0-force-dynamic = ["llvm14-0", "llvm-sys-140/force-dynamic"]
llvm15-0-force-dynamic = ["llvm15-0", "llvm-sys-150/force-dynamic"]
llvm16-0-force-dynamic = ["llvm16-0", "llvm-sys-160/force-dynamic"]
llvm17-0-force-dynamic = ["llvm17-0", "llvm-sys-170/force-dynamic"]

# Prefer dynamic linking against LLVM libraries. See llvm-sys for more details
llvm12-0-prefer-dynamic = ["llvm12-0", "llvm-sys-120/prefer-dynamic"]
llvm13-0-prefer-dynamic = ["llvm13-0", "llvm-sys-130/prefer-dynamic"]
llvm14-0-prefer-dynamic = ["llvm14-0", "llvm-sys-140/prefer-dynamic"]
llvm15-0-prefer-dynamic = ["llvm15-0", "llvm-sys-150/prefer-dynamic"]
llvm16-0-prefer-dynamic = ["llvm16-0", "llvm-sys-160/prefer-dynamic"]
llvm17-0-prefer-dynamic = ["llvm17-0", "llvm-sys-170/prefer-dynamic"]

# Force static linking against LLVM libraries. See llvm-sys for more details
llvm12-0-force-static = ["llvm12-0", "llvm-sys-120/force-static"]
llvm13-0-force-static = ["llvm13-0", "llvm-sys-130/force-static"]
llvm14-0-force-static = ["llvm14-0", "llvm-sys-140/force-static"]
llvm15-0-force-static = ["llvm15-0", "llvm-sys-150/force-static"]
llvm16-0-force-static = ["llvm16-0", "llvm-sys-160/force-static"]
llvm17-0-force-static = ["llvm17-0", "llvm-sys-170/force-static"]

# Prefer static linking against LLVM libraries. See llvm-sys for more details
llvm12-0-prefer-static = ["llvm12-0", "llvm-sys-120/prefer-static"]
llvm13-0-prefer-static = ["llvm13-0", "llvm-sys-130/prefer-static"]
llvm14-0-prefer-static = ["llvm14-0", "llvm-sys-140/prefer-static"]
llvm15-0-prefer-static = ["llvm15-0", "llvm-sys-150/prefer-static"]
llvm16-0-prefer-static = ["llvm16-0", "llvm-sys-160/prefer-static"]
llvm17-0-prefer-static = ["llvm17-0", "llvm-sys-170/prefer-static"]

# Don't force linking to libffi on non-windows platforms. Without this feature
# inkwell always links to libffi on non-windows platforms.
Expand Down Expand Up @@ -137,6 +143,7 @@ llvm-sys-130 = { package = "llvm-sys", version = "130.0.4", optional = true }
llvm-sys-140 = { package = "llvm-sys", version = "140.0.2", optional = true }
llvm-sys-150 = { package = "llvm-sys", version = "150.0.3", optional = true }
llvm-sys-160 = { package = "llvm-sys", version = "160.1.0", optional = true }
llvm-sys-170 = { package = "llvm-sys", version = "170.0.0", optional = true }
once_cell = "1.16"
static-alloc = { version = "0.2", optional = true }
thiserror = "1.0.48"
Expand Down
4 changes: 2 additions & 2 deletions internal_macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ use syn::{parse_macro_input, parse_quote};
use syn::{Attribute, Field, Ident, Item, LitFloat, Token, Variant};

// This array should match the LLVM features in the top level Cargo manifest
const FEATURE_VERSIONS: [&str; 13] = [
const FEATURE_VERSIONS: [&str; 14] = [
"llvm4-0", "llvm5-0", "llvm6-0", "llvm7-0", "llvm8-0", "llvm9-0", "llvm10-0", "llvm11-0", "llvm12-0", "llvm13-0",
"llvm14-0", "llvm15-0", "llvm16-0",
"llvm14-0", "llvm15-0", "llvm16-0", "llvm17-0",
];

/// Gets the index of the feature version that represents `latest`
Expand Down
24 changes: 12 additions & 12 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1461,7 +1461,7 @@ impl<'ctx> Builder<'ctx> {
/// both a power of 2 and under 2^64.
///
/// The final argument should be a pointer-sized integer.
///
///
/// Returns an `Err(BuilderError::AlignmentError)` if the source or destination alignments are not a power of 2.
///
/// [`TargetData::ptr_sized_int_type_in_context`](https://thedan64.github.io/inkwell/inkwell/targets/struct.TargetData.html#method.ptr_sized_int_type_in_context) will get you one of those.
Expand Down Expand Up @@ -1509,7 +1509,7 @@ impl<'ctx> Builder<'ctx> {
/// both a power of 2 and under 2^64.
///
/// The final argument should be a pointer-sized integer.
///
///
/// Returns an `Err(BuilderError::AlignmentError)` if the source or destination alignments are not a power of 2 under 2^64.
///
/// [`TargetData::ptr_sized_int_type_in_context`](https://thedan64.github.io/inkwell/inkwell/targets/struct.TargetData.html#method.ptr_sized_int_type_in_context) will get you one of those.
Expand Down Expand Up @@ -1557,7 +1557,7 @@ impl<'ctx> Builder<'ctx> {
/// both a power of 2 and under 2^64.
///
/// The final argument should be a pointer-sized integer.
///
///
/// Returns an `Err(BuilderError::AlignmentError)` if the source alignment is not a power of 2 under 2^64.
///
/// [`TargetData::ptr_sized_int_type_in_context`](https://thedan64.github.io/inkwell/inkwell/targets/struct.TargetData.html#method.ptr_sized_int_type_in_context) will get you one of those.
Expand Down Expand Up @@ -2669,7 +2669,7 @@ impl<'ctx> Builder<'ctx> {
// REVIEW: What if instruction and basic_block are completely unrelated?
// It'd be great if we could get the BB from the instruction behind the scenes
/// Set the position of the builder to after an instruction.
///
///
/// Be sure to call one of the `position_*` methods or all `build_*` methods will return `Err(BuilderError::UnsetPosition)`.
pub fn position_at(&self, basic_block: BasicBlock<'ctx>, instruction: &InstructionValue<'ctx>) {
self.positioned.set(PositionState::Set);
Expand All @@ -2678,7 +2678,7 @@ impl<'ctx> Builder<'ctx> {
}

/// Set the position of the builder to before an instruction.
///
///
/// Be sure to call one of the `position_*` methods or all `build_*` methods will return `Err(BuilderError::UnsetPosition)`.
pub fn position_before(&self, instruction: &InstructionValue<'ctx>) {
self.positioned.set(PositionState::Set);
Expand All @@ -2687,7 +2687,7 @@ impl<'ctx> Builder<'ctx> {
}

/// Set the position of the builder to the end of a basic block.
///
///
/// Be sure to call one of the `position_*` methods or all `build_*` methods will return `Err(BuilderError::UnsetPosition)`.
pub fn position_at_end(&self, basic_block: BasicBlock<'ctx>) {
self.positioned.set(PositionState::Set);
Expand All @@ -2699,7 +2699,7 @@ impl<'ctx> Builder<'ctx> {

/// Builds an extract value instruction which extracts a `BasicValueEnum`
/// from a struct or array.
///
///
/// Returns `Err(BuilderError::ExtractOutOfRange)` if the provided index is out of bounds of the aggregate value length.
///
/// # Example
Expand Down Expand Up @@ -2782,7 +2782,7 @@ impl<'ctx> Builder<'ctx> {

/// Builds an insert value instruction which inserts a `BasicValue` into a struct
/// or array and returns the resulting aggregate value.
///
///
/// Returns `Err(BuilderError::ExtractOutOfRange)` if the provided index is out of bounds of the aggregate value length.
///
/// # Example
Expand Down Expand Up @@ -3215,7 +3215,7 @@ impl<'ctx> Builder<'ctx> {
/// May return of the following errors:
/// - `Err(BuilderError::BitwidthError)` if the bitwidth of the value is not a power of 2 and less than 8
/// - `Err(BuilderError:PointeeTypeMismatch)` if the pointee type does not match the value's type
///
///
/// # Example
///
/// ```
Expand Down Expand Up @@ -3256,7 +3256,7 @@ impl<'ctx> Builder<'ctx> {
));
}

#[cfg(not(any(feature = "llvm15-0", feature = "llvm16-0")))]
#[cfg(not(any(feature = "llvm15-0", feature = "llvm16-0", feature = "llvm17-0")))]
if ptr.get_type().get_element_type() != value.get_type().into() {
return Err(BuilderError::PointeeTypeMismatch(
"Pointer's pointee type must match the value's type.",
Expand All @@ -3278,7 +3278,7 @@ impl<'ctx> Builder<'ctx> {
}

/// Builds a cmpxchg instruction. It allows you to atomically compare and replace memory.
///
///
/// May return one of the following errors:
/// - `Err(BuilderError::PointeeTypeMismatch)` if the pointer does not point to an element of the value type
/// - `Err(BuilderError::ValueTypeMismatch)` if the value to compare and the new values are not of the same type, or if
Expand Down Expand Up @@ -3334,7 +3334,7 @@ impl<'ctx> Builder<'ctx> {
));
}

#[cfg(not(any(feature = "llvm15-0", feature = "llvm16-0")))]
#[cfg(not(any(feature = "llvm15-0", feature = "llvm16-0", feature = "llvm17-0")))]
if ptr.get_type().get_element_type().to_basic_type_enum() != cmp.get_type() {
return Err(BuilderError::PointeeTypeMismatch(
"The pointer does not point to an element of the value type.",
Expand Down
41 changes: 38 additions & 3 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@ use llvm_sys::core::{
LLVMCreateStringAttribute, LLVMDoubleTypeInContext, LLVMFP128TypeInContext, LLVMFloatTypeInContext,
LLVMGetGlobalContext, LLVMGetMDKindIDInContext, LLVMHalfTypeInContext, LLVMInsertBasicBlockInContext,
LLVMInt16TypeInContext, LLVMInt1TypeInContext, LLVMInt32TypeInContext, LLVMInt64TypeInContext,
LLVMInt8TypeInContext, LLVMIntTypeInContext, LLVMMDNodeInContext, LLVMMDStringInContext,
LLVMModuleCreateWithNameInContext, LLVMPPCFP128TypeInContext, LLVMStructCreateNamed, LLVMStructTypeInContext,
LLVMVoidTypeInContext, LLVMX86FP80TypeInContext,
LLVMInt8TypeInContext, LLVMIntTypeInContext, LLVMModuleCreateWithNameInContext, LLVMPPCFP128TypeInContext,
LLVMStructCreateNamed, LLVMStructTypeInContext, LLVMVoidTypeInContext, LLVMX86FP80TypeInContext,
};
#[llvm_versions(4.0..=16.0)]
use llvm_sys::core::{LLVMMDNodeInContext, LLVMMDStringInContext};
#[llvm_versions(16.0..=latest)]
use llvm_sys::core::{LLVMMDNodeInContext2, LLVMMDStringInContext2};
use llvm_sys::ir_reader::LLVMParseIRInContext;
use llvm_sys::prelude::{LLVMContextRef, LLVMDiagnosticInfoRef, LLVMTypeRef, LLVMValueRef};
use llvm_sys::target::{LLVMIntPtrTypeForASInContext, LLVMIntPtrTypeInContext};
Expand Down Expand Up @@ -321,6 +324,7 @@ impl ContextImpl {
}
}

#[llvm_versions(4.0..=16.0)]
fn metadata_node<'ctx>(&self, values: &[BasicMetadataValueEnum<'ctx>]) -> MetadataValue<'ctx> {
let mut tuple_values: Vec<LLVMValueRef> = values.iter().map(|val| val.as_value_ref()).collect();
unsafe {
Expand All @@ -332,12 +336,43 @@ impl ContextImpl {
}
}

#[llvm_versions(17.0..=latest)]
fn metadata_node<'ctx>(&self, values: &[BasicMetadataValueEnum<'ctx>]) -> MetadataValue<'ctx> {
use llvm_sys::{core::LLVMMetadataAsValue, prelude::LLVMMetadataRef};

let mut tuple_values: Vec<LLVMMetadataRef> = values
.iter()
.map(|val| val.into_metadata_value().as_metadata_ref())
.collect();
unsafe {
MetadataValue::new(LLVMMetadataAsValue(
self.0,
LLVMMDNodeInContext2(self.0, tuple_values.as_mut_ptr(), tuple_values.len()),
))
}
}

#[llvm_versions(4.0..=16.0)]
fn metadata_string<'ctx>(&self, string: &str) -> MetadataValue<'ctx> {
let c_string = to_c_str(string);

unsafe { MetadataValue::new(LLVMMDStringInContext(self.0, c_string.as_ptr(), string.len() as u32)) }
}

#[llvm_versions(17.0..=latest)]
fn metadata_string<'ctx>(&self, string: &str) -> MetadataValue<'ctx> {
use llvm_sys::core::LLVMMetadataAsValue;

let c_string = to_c_str(string);

unsafe {
MetadataValue::new(LLVMMetadataAsValue(
self.0,
LLVMMDStringInContext2(self.0, c_string.as_ptr(), string.len()),
))
}
}

fn get_kind_id(&self, key: &str) -> u32 {
unsafe { LLVMGetMDKindIDInContext(self.0, key.as_ptr() as *const ::libc::c_char, key.len() as u32) }
}
Expand Down
24 changes: 17 additions & 7 deletions src/debug_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,8 @@ impl<'ctx> DebugInfoBuilder<'ctx> {
feature = "llvm13-0",
feature = "llvm14-0",
feature = "llvm15-0",
feature = "llvm16-0"
feature = "llvm16-0",
feature = "llvm17-0"
))]
sysroot: &str,
#[cfg(any(
Expand All @@ -201,7 +202,8 @@ impl<'ctx> DebugInfoBuilder<'ctx> {
feature = "llvm13-0",
feature = "llvm14-0",
feature = "llvm15-0",
feature = "llvm16-0"
feature = "llvm16-0",
feature = "llvm17-0"
))]
sdk: &str,
) -> (Self, DICompileUnit<'ctx>) {
Expand Down Expand Up @@ -238,7 +240,8 @@ impl<'ctx> DebugInfoBuilder<'ctx> {
feature = "llvm13-0",
feature = "llvm14-0",
feature = "llvm15-0",
feature = "llvm16-0"
feature = "llvm16-0",
feature = "llvm17-0"
))]
sysroot,
#[cfg(any(
Expand All @@ -247,7 +250,8 @@ impl<'ctx> DebugInfoBuilder<'ctx> {
feature = "llvm13-0",
feature = "llvm14-0",
feature = "llvm15-0",
feature = "llvm16-0"
feature = "llvm16-0",
feature = "llvm17-0"
))]
sdk,
);
Expand Down Expand Up @@ -292,7 +296,8 @@ impl<'ctx> DebugInfoBuilder<'ctx> {
feature = "llvm13-0",
feature = "llvm14-0",
feature = "llvm15-0",
feature = "llvm16-0"
feature = "llvm16-0",
feature = "llvm17-0"
))]
sysroot: &str,
#[cfg(any(
Expand All @@ -301,7 +306,8 @@ impl<'ctx> DebugInfoBuilder<'ctx> {
feature = "llvm13-0",
feature = "llvm14-0",
feature = "llvm15-0",
feature = "llvm16-0"
feature = "llvm16-0",
feature = "llvm17-0"
))]
sdk: &str,
) -> DICompileUnit<'ctx> {
Expand Down Expand Up @@ -341,7 +347,8 @@ impl<'ctx> DebugInfoBuilder<'ctx> {
feature = "llvm13-0",
feature = "llvm14-0",
feature = "llvm15-0",
feature = "llvm16-0"
feature = "llvm16-0",
feature = "llvm17-0"
))]
{
LLVMDIBuilderCreateCompileUnit(
Expand Down Expand Up @@ -1585,5 +1592,8 @@ mod flags {
#[llvm_versions(16.0..=latest)]
#[llvm_variant(LLVMDWARFSourceLanguageAda2012)]
Ada2012,
#[llvm_versions(17.0..=latest)]
#[llvm_variant(LLVMDWARFSourceLanguageMojo)]
Mojo,
}
}
19 changes: 18 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ extern crate llvm_sys_140 as llvm_sys;
extern crate llvm_sys_150 as llvm_sys;
#[cfg(feature = "llvm16-0")]
extern crate llvm_sys_160 as llvm_sys;
#[cfg(feature = "llvm17-0")]
extern crate llvm_sys_170 as llvm_sys;
#[cfg(feature = "llvm4-0")]
extern crate llvm_sys_40 as llvm_sys;
#[cfg(feature = "llvm5-0")]
Expand Down Expand Up @@ -107,7 +109,22 @@ macro_rules! assert_unique_used_features {
}
}

assert_unique_used_features! {"llvm4-0", "llvm5-0", "llvm6-0", "llvm7-0", "llvm8-0", "llvm9-0", "llvm10-0", "llvm11-0", "llvm12-0", "llvm13-0", "llvm14-0", "llvm15-0", "llvm16-0"}
assert_unique_used_features! {
"llvm4-0",
"llvm5-0",
"llvm6-0",
"llvm7-0",
"llvm8-0",
"llvm9-0",
"llvm10-0",
"llvm11-0",
"llvm12-0",
"llvm13-0",
"llvm14-0",
"llvm15-0",
"llvm16-0",
"llvm17-0"
}

/// Defines the address space in which a global will be inserted.
///
Expand Down
12 changes: 8 additions & 4 deletions src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1425,7 +1425,8 @@ impl<'ctx> Module<'ctx> {
feature = "llvm13-0",
feature = "llvm14-0",
feature = "llvm15-0",
feature = "llvm16-0"
feature = "llvm16-0",
feature = "llvm17-0"
))]
sysroot: &str,
#[cfg(any(
Expand All @@ -1434,7 +1435,8 @@ impl<'ctx> Module<'ctx> {
feature = "llvm13-0",
feature = "llvm14-0",
feature = "llvm15-0",
feature = "llvm16-0"
feature = "llvm16-0",
feature = "llvm17-0"
))]
sdk: &str,
) -> (DebugInfoBuilder<'ctx>, DICompileUnit<'ctx>) {
Expand All @@ -1459,7 +1461,8 @@ impl<'ctx> Module<'ctx> {
feature = "llvm13-0",
feature = "llvm14-0",
feature = "llvm15-0",
feature = "llvm16-0"
feature = "llvm16-0",
feature = "llvm17-0"
))]
sysroot,
#[cfg(any(
Expand All @@ -1468,7 +1471,8 @@ impl<'ctx> Module<'ctx> {
feature = "llvm13-0",
feature = "llvm14-0",
feature = "llvm15-0",
feature = "llvm16-0"
feature = "llvm16-0",
feature = "llvm17-0"
))]
sdk,
)
Expand Down
Loading

0 comments on commit a34bff2

Please sign in to comment.