Skip to content

Commit 84b41bd

Browse files
committed
Add LLVM 16 support
1 parent 6d414fa commit 84b41bd

File tree

22 files changed

+84
-44
lines changed

22 files changed

+84
-44
lines changed

.github/workflows/linux.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ jobs:
2121
- ["13", "13-0", "v13.0.0-rust-1.59/llvm-lld-13.0.0-rust-1.59-linux.tar.gz"]
2222
- ["14", "14-0", "v14.0.6-rust-1.64/llvm-lld-14.0.6-rust-1.64-linux.tar.gz"]
2323
- ["15", "15-0", "v15.0.0-rust-1.65/llvm-lld-15.0.0-rust-1.65-linux.tar.gz"]
24+
- ["16", "16-0", "v16.0.2-rust-1.71/llvm-lld-16.0.2-rust-1.71-linux.tar.gz"]
2425
steps:
2526
- name: Checkout Repo
2627
uses: actions/checkout@v2
@@ -108,6 +109,7 @@ jobs:
108109
- ["13", "13-0", "1.59", "v13.0.0-rust-1.59/llvm-lld-13.0.0-rust-1.59-linux.tar.gz"]
109110
- ["14", "14-0", "1.64", "v14.0.6-rust-1.64/llvm-lld-14.0.6-rust-1.64-linux.tar.gz"]
110111
- ["15", "15-0", "1.65", "v15.0.0-rust-1.65/llvm-lld-15.0.0-rust-1.65-linux.tar.gz"]
112+
- ["16", "16-0", "nightly-2023-04-29", "v16.0.2-rust-1.71/llvm-lld-16.0.2-rust-1.71-linux.tar.gz"]
111113
steps:
112114
- name: Checkout Repo
113115
uses: actions/checkout@v2

.github/workflows/macos.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ jobs:
2121
- ["13", "13-0", "v13.0.0-rust-1.59/llvm-lld-13.0.0-rust-1.59-macos.tar.gz"]
2222
- ["14", "14-0", "v14.0.6-rust-1.64/llvm-lld-14.0.6-rust-1.64-macos.tar.gz"]
2323
- ["15", "15-0", "v15.0.0-rust-1.65/llvm-lld-15.0.0-rust-1.65-macos.tar.gz"]
24+
- ["16", "16-0", "v16.0.2-rust-1.71/llvm-lld-16.0.2-rust-1.71-macos.tar.gz"]
2425
steps:
2526
- name: Checkout Repo
2627
uses: actions/checkout@v2

.github/workflows/windows.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ jobs:
2020
- ["13", "13-0", "v13.0.0-rust-1.59/llvm-lld-13.0.0-rust-1.59-windows.7z"]
2121
- ["14", "14-0", "v14.0.6-rust-1.64/llvm-lld-14.0.6-rust-1.64-windows.7z"]
2222
- ["15", "15-0", "v15.0.0-rust-1.65/llvm-lld-15.0.0-rust-1.65-windows.7z"]
23+
- ["16", "16-0", "v16.0.2-rust-1.71/llvm-lld-16.0.2-rust-1.71-windows.7z"]
2324
steps:
2425
- name: Checkout Repo
2526
uses: actions/checkout@v2
@@ -106,6 +107,7 @@ jobs:
106107
- ["13", "13-0", "1.59", "v13.0.0-rust-1.59/llvm-lld-13.0.0-rust-1.59-windows.7z"]
107108
- ["14", "14-0", "1.64", "v14.0.6-rust-1.64/llvm-lld-14.0.6-rust-1.64-windows.7z"]
108109
- ["15", "15-0", "1.65", "v15.0.0-rust-1.65/llvm-lld-15.0.0-rust-1.65-windows.7z"]
110+
- ["16", "16-0", "nightly-2023-04-29", "v16.0.2-rust-1.71/llvm-lld-16.0.2-rust-1.71-windows.7z"]
109111
steps:
110112
- name: Checkout Repo
111113
uses: actions/checkout@v2

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ Supported versions:
4040
| 13.0.x | llvm13-0 | **✓** | **✓** | **✓** |
4141
| 14.0.x | llvm14-0 | **✓** | **✓** | **✓** |
4242
| 15.0.x | llvm15-0 | **✓** | **✓** | **✓** |
43+
| 16.0.x | llvm16-0 | **✓** | **✓** | **✓** |
4344

4445
## Getting Started
4546

examples/strings.rs

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,13 @@ fn encode_global_strings<'a>(module: &mut Module<'a>) -> Vec<GlobalString<'a>> {
6767

6868
match global.get_initializer() {
6969
// C-like strings
70-
Some(BasicValueEnum::ArrayValue(arr)) => {
70+
Some(BasicValueEnum::ArrayValue(arr)) if arr.is_const_string() => {
7171
let encoded_str = match arr.get_string_constant() {
72-
Some(s) => s.iter().map(|c| *c + 1).collect::<Vec<_>>(),
72+
Some(s) => s
73+
.to_bytes_with_nul()
74+
.iter()
75+
.map(|c| *c + 1)
76+
.collect::<Vec<_>>(),
7377
None => continue,
7478
};
7579
let new_const = cx.const_string(&encoded_str, false);
@@ -80,11 +84,19 @@ fn encode_global_strings<'a>(module: &mut Module<'a>) -> Vec<GlobalString<'a>> {
8084
// Rust-like strings
8185
Some(BasicValueEnum::StructValue(stru)) if stru.get_num_operands() <= 1 => {
8286
let arr = match stru.get_operand(0) {
83-
Some(Either::Left(BasicValueEnum::ArrayValue(arr))) => arr,
87+
Some(Either::Left(BasicValueEnum::ArrayValue(arr)))
88+
if arr.is_const_string() =>
89+
{
90+
arr
91+
}
8492
_ => continue,
8593
};
8694
let encoded_str = match arr.get_string_constant() {
87-
Some(s) => s.iter().map(|c| *c + 1).collect::<Vec<_>>(),
95+
Some(s) => s
96+
.to_bytes_with_nul()
97+
.iter()
98+
.map(|c| *c + 1)
99+
.collect::<Vec<_>>(),
88100
None => continue,
89101
};
90102
let new_const = cx.const_string(&encoded_str, false);
@@ -139,15 +151,15 @@ fn create_decode_fn<'a>(module: &mut Module<'a>) -> FunctionValue<'a> {
139151
cx.i32_type().const_all_ones(),
140152
"",
141153
);
142-
#[cfg(not(feature = "llvm15-0"))]
154+
#[cfg(not(any(feature = "llvm15-0", feature = "llvm16-0")))]
143155
let var10 = unsafe {
144156
builder.build_gep(
145157
phi1.as_basic_value().into_pointer_value(),
146158
&[cx.i64_type().const_int(1, false)],
147159
"",
148160
)
149161
};
150-
#[cfg(feature = "llvm15-0")]
162+
#[cfg(any(feature = "llvm15-0", feature = "llvm16-0"))]
151163
let var10 = unsafe {
152164
builder.build_gep(
153165
cx.i8_type(),
@@ -156,9 +168,9 @@ fn create_decode_fn<'a>(module: &mut Module<'a>) -> FunctionValue<'a> {
156168
"",
157169
)
158170
};
159-
#[cfg(not(feature = "llvm15-0"))]
171+
#[cfg(not(any(feature = "llvm15-0", feature = "llvm16-0")))]
160172
let var11 = builder.build_load(phi1.as_basic_value().into_pointer_value(), "");
161-
#[cfg(feature = "llvm15-0")]
173+
#[cfg(any(feature = "llvm15-0", feature = "llvm16-0"))]
162174
let var11 = builder.build_load(cx.i8_type(), phi1.as_basic_value().into_pointer_value(), "");
163175
let var12 = builder.build_int_add(var11.into_int_value(), cx.i8_type().const_all_ones(), "");
164176
builder.build_store(phi1.as_basic_value().into_pointer_value(), var12);
@@ -203,11 +215,11 @@ fn create_decode_stub<'a>(
203215
(s, len)
204216
}
205217
GlobalString::Struct(gs, id, len) => {
206-
#[cfg(not(feature = "llvm15-0"))]
218+
#[cfg(not(any(feature = "llvm15-0", feature = "llvm16-0")))]
207219
let s = builder
208220
.build_struct_gep(gs.as_pointer_value(), id, "")
209221
.unwrap();
210-
#[cfg(feature = "llvm15-0")]
222+
#[cfg(any(feature = "llvm15-0", feature = "llvm16-0"))]
211223
let s = {
212224
let i8_ty_ptr = cx.i8_type().ptr_type(AddressSpace::default());
213225
let struct_ty = cx.struct_type(&[i8_ty_ptr.into()], false);

llvm-plugin/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ llvm12-0 = ["inkwell/llvm12-0-no-llvm-linking"]
3030
llvm13-0 = ["inkwell/llvm13-0-no-llvm-linking"]
3131
llvm14-0 = ["inkwell/llvm14-0-no-llvm-linking"]
3232
llvm15-0 = ["inkwell/llvm15-0-no-llvm-linking"]
33+
llvm16-0 = ["inkwell/llvm16-0-no-llvm-linking"]
3334

3435
target-x86 = ["inkwell/target-x86"]
3536
target-arm = ["inkwell/target-arm"]
@@ -50,7 +51,7 @@ target-riscv = ["inkwell/target-riscv"]
5051
target-all = ["inkwell/target-all"]
5152

5253
[dependencies]
53-
inkwell = { package = "llvm-plugin-inkwell", version = "0.2", features = ["internal-getters"] }
54+
inkwell = { package = "llvm-plugin-inkwell", version = "0.3" }
5455
llvm-plugin-macros = { path = "../llvm-plugin-macros", version = "0.2", optional = true }
5556

5657
[build-dependencies]

llvm-plugin/build.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ fn main() {
4040
build.define("LLVM14_0", None);
4141
#[cfg(feature = "llvm15-0")]
4242
build.define("LLVM15_0", None);
43+
#[cfg(feature = "llvm16-0")]
44+
build.define("LLVM16_0", None);
4345

4446
build.warnings(false);
4547
build.compile("llvm-plugin-cpp");
@@ -222,6 +224,8 @@ mod llvm_sys {
222224
(14, 0)
223225
} else if cfg!(feature = "llvm15-0") {
224226
(15, 0)
227+
} else if cfg!(feature = "llvm16-0") {
228+
(16, 0)
225229
} else {
226230
panic!("Missing llvm* feature")
227231
}

llvm-plugin/cpp/ffi.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#include "common.hh"
1010
#include "pass.hh"
1111

12-
#if defined(LLVM14_0) || defined(LLVM15_0)
12+
#if defined(LLVM14_0) || defined(LLVM15_0) || defined(LLVM16_0)
1313
#include <llvm/Passes/OptimizationLevel.h>
1414
using LlvmOptLevel = llvm::OptimizationLevel;
1515
#else
@@ -87,7 +87,7 @@ auto functionAnalysisManagerRegisterPass(
8787
});
8888
}
8989

90-
#ifdef LLVM15_0
90+
#if defined(LLVM15_0) || defined(LLVM16_0)
9191
auto passBuilderAddFullLinkTimeOptimizationLastEPCallback(
9292
llvm::PassBuilder &Builder, const void *DataPtr,
9393
void (*Deleter)(const void *),
@@ -104,7 +104,7 @@ auto passBuilderAddFullLinkTimeOptimizationLastEPCallback(
104104
}
105105
#endif
106106

107-
#ifdef LLVM15_0
107+
#if defined(LLVM15_0) || defined(LLVM16_0)
108108
auto passBuilderAddFullLinkTimeOptimizationEarlyEPCallback(
109109
llvm::PassBuilder &Builder, const void *DataPtr,
110110
void (*Deleter)(const void *),
@@ -139,7 +139,7 @@ auto passBuilderAddOptimizerLastEPCallback(
139139
}
140140
#endif
141141

142-
#ifdef LLVM15_0
142+
#if defined(LLVM15_0) || defined(LLVM16_0)
143143
auto passBuilderAddOptimizerEarlyEPCallback(
144144
llvm::PassBuilder &Builder, const void *DataPtr,
145145
void (*Deleter)(const void *),

llvm-plugin/src/analysis.rs

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use std::ffi::c_void;
22

33
use inkwell::module::Module;
44
use inkwell::values::{AsValueRef, FunctionValue};
5-
use inkwell::LLVMReference;
65

76
use crate::{LlvmFunctionAnalysis, LlvmModuleAnalysis};
87

@@ -182,11 +181,10 @@ impl ModuleAnalysisManager {
182181
"Analysis cannot request its own result"
183182
);
184183

185-
unsafe {
186-
let res =
187-
crate::get_module_analysis_result(self.inner, A::id(), module.get_ref().cast());
188-
Box::leak(Box::from_raw(res.cast()))
189-
}
184+
let res =
185+
crate::get_module_analysis_result(self.inner, A::id(), module.as_mut_ptr().cast());
186+
187+
unsafe { Box::leak(Box::from_raw(res.cast())) }
190188
}
191189

192190
/// Returns the result of the analysis on a given module IR.
@@ -210,14 +208,13 @@ impl ModuleAnalysisManager {
210208
"Analysis cannot request its own result"
211209
);
212210

213-
unsafe {
214-
let res = crate::get_module_analysis_cached_result(
215-
self.inner,
216-
A::id(),
217-
module.get_ref().cast(),
218-
);
219-
(!res.is_null()).then_some(Box::leak(Box::from_raw(res.cast())))
220-
}
211+
let res = crate::get_module_analysis_cached_result(
212+
self.inner,
213+
A::id(),
214+
module.as_mut_ptr().cast(),
215+
);
216+
217+
unsafe { (!res.is_null()).then_some(Box::leak(Box::from_raw(res.cast()))) }
221218
}
222219

223220
/// Returns a [FunctionAnalysisManagerProxy], which is essentially an interface
@@ -226,9 +223,10 @@ impl ModuleAnalysisManager {
226223
&self,
227224
module: &Module<'a>,
228225
) -> FunctionAnalysisManagerProxy {
229-
let proxy = crate::get_function_analysis_manager_module_proxy(self.inner, unsafe {
230-
module.get_ref().cast()
231-
});
226+
let proxy = crate::get_function_analysis_manager_module_proxy(
227+
self.inner,
228+
module.as_mut_ptr().cast(),
229+
);
232230
FunctionAnalysisManagerProxy { inner: proxy }
233231
}
234232

llvm-plugin/src/ffi.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,23 @@ pub type AnalysisKey = *const u8;
44

55
#[link(name = "llvm-plugin-cpp")]
66
extern "C" {
7-
#[cfg(feature = "llvm15-0")]
7+
#[cfg(any(feature = "llvm15-0", feature = "llvm16-0"))]
88
pub(crate) fn passBuilderAddFullLinkTimeOptimizationLastEPCallback(
99
builder: *mut c_void,
1010
cb: *const c_void,
1111
cb_deleter: extern "C" fn(*const c_void),
1212
cb_sys: extern "C" fn(*const c_void, *mut c_void, crate::OptimizationLevel),
1313
);
1414

15-
#[cfg(feature = "llvm15-0")]
15+
#[cfg(any(feature = "llvm15-0", feature = "llvm16-0"))]
1616
pub(crate) fn passBuilderAddFullLinkTimeOptimizationEarlyEPCallback(
1717
builder: *mut c_void,
1818
cb: *const c_void,
1919
cb_deleter: extern "C" fn(*const c_void),
2020
cb_sys: extern "C" fn(*const c_void, *mut c_void, crate::OptimizationLevel),
2121
);
2222

23-
#[cfg(feature = "llvm15-0")]
23+
#[cfg(any(feature = "llvm15-0", feature = "llvm16-0"))]
2424
pub(crate) fn passBuilderAddOptimizerEarlyEPCallback(
2525
builder: *mut c_void,
2626
cb: *const c_void,
@@ -34,6 +34,7 @@ extern "C" {
3434
feature = "llvm13-0",
3535
feature = "llvm14-0",
3636
feature = "llvm15-0",
37+
feature = "llvm16-0",
3738
))]
3839
pub(crate) fn passBuilderAddOptimizerLastEPCallback(
3940
builder: *mut c_void,
@@ -47,6 +48,7 @@ extern "C" {
4748
feature = "llvm13-0",
4849
feature = "llvm14-0",
4950
feature = "llvm15-0",
51+
feature = "llvm16-0",
5052
))]
5153
pub(crate) fn passBuilderAddPipelineEarlySimplificationEPCallback(
5254
builder: *mut c_void,
@@ -60,6 +62,7 @@ extern "C" {
6062
feature = "llvm13-0",
6163
feature = "llvm14-0",
6264
feature = "llvm15-0",
65+
feature = "llvm16-0",
6366
))]
6467
pub(crate) fn passBuilderAddPipelineStartEPCallback(
6568
builder: *mut c_void,
@@ -129,6 +132,7 @@ extern "C" {
129132
feature = "llvm13-0",
130133
feature = "llvm14-0",
131134
feature = "llvm15-0",
135+
feature = "llvm16-0",
132136
))]
133137
pub(crate) fn modulePassManagerIsEmpty(manager: *mut c_void) -> bool;
134138

@@ -144,6 +148,7 @@ extern "C" {
144148
feature = "llvm13-0",
145149
feature = "llvm14-0",
146150
feature = "llvm15-0",
151+
feature = "llvm16-0",
147152
))]
148153
pub(crate) fn functionPassManagerIsEmpty(manager: *mut c_void) -> bool;
149154

0 commit comments

Comments
 (0)