Skip to content

Commit

Permalink
WIP: Support LLVM 18 (#483)
Browse files Browse the repository at this point in the history
* Add serde derives for some config datastructures

Signed-off-by: xermicus <[email protected]>

* llvm18-0 feature

Signed-off-by: xermicus <[email protected]>

* add some missing guards

Signed-off-by: Cyrill Leutwiler <[email protected]>

* support tail call kinds

Signed-off-by: xermicus <[email protected]>

* re-export LLVMTailCallKind enum

Signed-off-by: xermicus <[email protected]>

* add get_tail_call_kind getter on InstructionValue

Signed-off-by: xermicus <[email protected]>

* test get_tail_call_kind

Signed-off-by: xermicus <[email protected]>

* support for fast math flags

Signed-off-by: xermicus <[email protected]>

* non negative flag for zext instructions

Signed-off-by: xermicus <[email protected]>

* disjoint flag for or instructions

Signed-off-by: xermicus <[email protected]>

* target machine options builder

Signed-off-by: xermicus <[email protected]>

* implement safe wrapper for LLVM target machine options

Signed-off-by: xermicus <[email protected]>

* llvm-sys 180 has been released

Signed-off-by: xermicus <[email protected]>

---------

Signed-off-by: xermicus <[email protected]>
Signed-off-by: Cyrill Leutwiler <[email protected]>
  • Loading branch information
xermicus authored Mar 31, 2024
1 parent 69c5a3f commit b74d272
Show file tree
Hide file tree
Showing 23 changed files with 791 additions and 124 deletions.
7 changes: 7 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ llvm14-0 = ["llvm-sys-140"]
llvm15-0 = ["llvm-sys-150"]
llvm16-0 = ["llvm-sys-160"]
llvm17-0 = ["llvm-sys-170"]
llvm18-0 = ["llvm-sys-180"]
# 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 @@ -47,6 +48,7 @@ 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"]
llvm18-0-no-llvm-linking = ["llvm18-0", "llvm-sys-180/no-llvm-linking"]

# Linking preference.
# If none of these are enabled, it defaults to force static linking.
Expand All @@ -58,6 +60,7 @@ 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"]
llvm18-0-force-dynamic = ["llvm18-0", "llvm-sys-180/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"]
Expand All @@ -66,6 +69,7 @@ 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"]
llvm18-0-prefer-dynamic = ["llvm18-0", "llvm-sys-180/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"]
Expand All @@ -74,6 +78,7 @@ 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"]
llvm18-0-force-static = ["llvm18-0", "llvm-sys-180/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"]
Expand All @@ -82,6 +87,7 @@ 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"]
llvm18-0-prefer-static = ["llvm18-0", "llvm-sys-180/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 @@ -144,6 +150,7 @@ 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.1", optional = true }
llvm-sys-180 = { package = "llvm-sys", version = "180.0.0", optional = true }
once_cell = "1.16"
static-alloc = { version = "0.2", optional = true }
thiserror = "1.0.48"
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@ Inkwell aims to help you pen your own programming languages by safely wrapping l
## Requirements

* Rust 1.56+ (Stable, Beta, or Nightly)
* One of LLVM 4-17
* One of LLVM 4-18

## Usage

You'll need to point your Cargo.toml to use a single LLVM version feature flag corresponding to your LLVM version as such:

```toml
[dependencies]
inkwell = { version = "0.4.0", features = ["llvm17-0"] }
inkwell = { version = "0.4.0", features = ["llvm18-0"] }
```

Supported versions:
LLVM 4-17 mapping to a cargo feature flag `llvmM-0` where `M` corresponds to the LLVM major version.
LLVM 4-18 mapping to a cargo feature flag `llvmM-0` where `M` corresponds to the LLVM major version.

Please be aware that we may make breaking changes on master from time to time since we are
pre-v1.0.0, in compliance with semver. Please prefer a crates.io release whenever possible!
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; 14] = [
const FEATURE_VERSIONS: [&str; 15] = [
"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",
"llvm14-0", "llvm15-0", "llvm16-0", "llvm17-0", "llvm18-0",
];

/// Gets the index of the feature version that represents `latest`
Expand Down
18 changes: 14 additions & 4 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2738,7 +2738,7 @@ impl<'ctx> Builder<'ctx> {
/// feature = "llvm14-0"
/// ))]
/// let array = builder.build_load(array_alloca, "array_load").unwrap().into_array_value();
/// #[cfg(any(feature = "llvm15-0", feature = "llvm16-0", feature = "llvm17-0"))]
/// #[cfg(any(feature = "llvm15-0", feature = "llvm16-0", feature = "llvm17-0", feature = "llvm18-0"))]
/// let array = builder.build_load(i32_type, array_alloca, "array_load").unwrap().into_array_value();
///
/// let const_int1 = i32_type.const_int(2, false);
Expand Down Expand Up @@ -2821,7 +2821,7 @@ impl<'ctx> Builder<'ctx> {
/// feature = "llvm14-0"
/// ))]
/// let array = builder.build_load(array_alloca, "array_load").unwrap().into_array_value();
/// #[cfg(any(feature = "llvm15-0", feature = "llvm16-0", feature = "llvm17-0"))]
/// #[cfg(any(feature = "llvm15-0", feature = "llvm16-0", feature = "llvm17-0", feature = "llvm18-0"))]
/// let array = builder.build_load(i32_type, array_alloca, "array_load").unwrap().into_array_value();
///
/// let const_int1 = i32_type.const_int(2, false);
Expand Down Expand Up @@ -3256,7 +3256,12 @@ impl<'ctx> Builder<'ctx> {
));
}

#[cfg(not(any(feature = "llvm15-0", feature = "llvm16-0", feature = "llvm17-0")))]
#[cfg(not(any(
feature = "llvm15-0",
feature = "llvm16-0",
feature = "llvm17-0",
feature = "llvm18-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 Down Expand Up @@ -3334,7 +3339,12 @@ impl<'ctx> Builder<'ctx> {
));
}

#[cfg(not(any(feature = "llvm15-0", feature = "llvm16-0", feature = "llvm17-0")))]
#[cfg(not(any(
feature = "llvm15-0",
feature = "llvm16-0",
feature = "llvm17-0",
feature = "llvm18-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
4 changes: 2 additions & 2 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,7 @@ impl Context {
/// builder.build_call(callable_value, params, "exit").unwrap();
/// }
///
/// #[cfg(any(feature = "llvm15-0", feature = "llvm16-0", feature = "llvm17-0"))]
/// #[cfg(any(feature = "llvm15-0", feature = "llvm16-0", feature = "llvm17-0", feature = "llvm18-0"))]
/// builder.build_indirect_call(asm_fn, asm, params, "exit").unwrap();
///
/// builder.build_return(None).unwrap();
Expand Down Expand Up @@ -1417,7 +1417,7 @@ impl<'ctx> ContextRef<'ctx> {
/// builder.build_call(callable_value, params, "exit").unwrap();
/// }
///
/// #[cfg(any(feature = "llvm15-0", feature = "llvm16-0", feature = "llvm17-0"))]
/// #[cfg(any(feature = "llvm15-0", feature = "llvm16-0", feature = "llvm17-0", feature = "llvm18-0"))]
/// builder.build_indirect_call(asm_fn, asm, params, "exit").unwrap();
///
/// builder.build_return(None).unwrap();
Expand Down
21 changes: 14 additions & 7 deletions src/debug_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,8 @@ impl<'ctx> DebugInfoBuilder<'ctx> {
feature = "llvm14-0",
feature = "llvm15-0",
feature = "llvm16-0",
feature = "llvm17-0"
feature = "llvm17-0",
feature = "llvm18-0"
))]
sysroot: &str,
#[cfg(any(
Expand All @@ -203,7 +204,8 @@ impl<'ctx> DebugInfoBuilder<'ctx> {
feature = "llvm14-0",
feature = "llvm15-0",
feature = "llvm16-0",
feature = "llvm17-0"
feature = "llvm17-0",
feature = "llvm18-0"
))]
sdk: &str,
) -> (Self, DICompileUnit<'ctx>) {
Expand Down Expand Up @@ -241,7 +243,8 @@ impl<'ctx> DebugInfoBuilder<'ctx> {
feature = "llvm14-0",
feature = "llvm15-0",
feature = "llvm16-0",
feature = "llvm17-0"
feature = "llvm17-0",
feature = "llvm18-0"
))]
sysroot,
#[cfg(any(
Expand All @@ -251,7 +254,8 @@ impl<'ctx> DebugInfoBuilder<'ctx> {
feature = "llvm14-0",
feature = "llvm15-0",
feature = "llvm16-0",
feature = "llvm17-0"
feature = "llvm17-0",
feature = "llvm18-0"
))]
sdk,
);
Expand Down Expand Up @@ -297,7 +301,8 @@ impl<'ctx> DebugInfoBuilder<'ctx> {
feature = "llvm14-0",
feature = "llvm15-0",
feature = "llvm16-0",
feature = "llvm17-0"
feature = "llvm17-0",
feature = "llvm18-0"
))]
sysroot: &str,
#[cfg(any(
Expand All @@ -307,7 +312,8 @@ impl<'ctx> DebugInfoBuilder<'ctx> {
feature = "llvm14-0",
feature = "llvm15-0",
feature = "llvm16-0",
feature = "llvm17-0"
feature = "llvm17-0",
feature = "llvm18-0"
))]
sdk: &str,
) -> DICompileUnit<'ctx> {
Expand Down Expand Up @@ -348,7 +354,8 @@ impl<'ctx> DebugInfoBuilder<'ctx> {
feature = "llvm14-0",
feature = "llvm15-0",
feature = "llvm16-0",
feature = "llvm17-0"
feature = "llvm17-0",
feature = "llvm18-0"
))]
{
LLVMDIBuilderCreateCompileUnit(
Expand Down
17 changes: 16 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ pub extern crate llvm_sys_150 as llvm_sys;
pub extern crate llvm_sys_160 as llvm_sys;
#[cfg(feature = "llvm17-0")]
pub extern crate llvm_sys_170 as llvm_sys;
#[cfg(feature = "llvm18-0")]
pub extern crate llvm_sys_180 as llvm_sys;
#[cfg(feature = "llvm4-0")]
pub extern crate llvm_sys_40 as llvm_sys;
#[cfg(feature = "llvm5-0")]
Expand All @@ -70,6 +72,7 @@ pub extern crate llvm_sys_80 as llvm_sys;
#[cfg(feature = "llvm9-0")]
pub extern crate llvm_sys_90 as llvm_sys;

use llvm_sys::target_machine::LLVMCodeGenOptLevel;
use llvm_sys::{
LLVMAtomicOrdering, LLVMAtomicRMWBinOp, LLVMDLLStorageClass, LLVMIntPredicate, LLVMRealPredicate,
LLVMThreadLocalMode, LLVMVisibility,
Expand Down Expand Up @@ -125,7 +128,8 @@ assert_unique_used_features! {
"llvm14-0",
"llvm15-0",
"llvm16-0",
"llvm17-0"
"llvm17-0",
"llvm18-0"
}

/// Defines the address space in which a global will be inserted.
Expand Down Expand Up @@ -400,6 +404,17 @@ impl Default for OptimizationLevel {
}
}

impl From<OptimizationLevel> for LLVMCodeGenOptLevel {
fn from(value: OptimizationLevel) -> Self {
match value {
OptimizationLevel::None => LLVMCodeGenOptLevel::LLVMCodeGenLevelNone,
OptimizationLevel::Less => LLVMCodeGenOptLevel::LLVMCodeGenLevelLess,
OptimizationLevel::Default => LLVMCodeGenOptLevel::LLVMCodeGenLevelDefault,
OptimizationLevel::Aggressive => LLVMCodeGenOptLevel::LLVMCodeGenLevelAggressive,
}
}
}

#[llvm_enum(LLVMVisibility)]
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
Expand Down
12 changes: 8 additions & 4 deletions src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1431,7 +1431,8 @@ impl<'ctx> Module<'ctx> {
feature = "llvm14-0",
feature = "llvm15-0",
feature = "llvm16-0",
feature = "llvm17-0"
feature = "llvm17-0",
feature = "llvm18-0"
))]
sysroot: &str,
#[cfg(any(
Expand All @@ -1441,7 +1442,8 @@ impl<'ctx> Module<'ctx> {
feature = "llvm14-0",
feature = "llvm15-0",
feature = "llvm16-0",
feature = "llvm17-0"
feature = "llvm17-0",
feature = "llvm18-0"
))]
sdk: &str,
) -> (DebugInfoBuilder<'ctx>, DICompileUnit<'ctx>) {
Expand All @@ -1467,7 +1469,8 @@ impl<'ctx> Module<'ctx> {
feature = "llvm14-0",
feature = "llvm15-0",
feature = "llvm16-0",
feature = "llvm17-0"
feature = "llvm17-0",
feature = "llvm18-0"
))]
sysroot,
#[cfg(any(
Expand All @@ -1477,7 +1480,8 @@ impl<'ctx> Module<'ctx> {
feature = "llvm14-0",
feature = "llvm15-0",
feature = "llvm16-0",
feature = "llvm17-0"
feature = "llvm17-0",
feature = "llvm18-0"
))]
sdk,
)
Expand Down
Loading

0 comments on commit b74d272

Please sign in to comment.