[go: up one dir, main page]

rustc_codegen_llvm/llvm/
mod.rs

1#![allow(non_snake_case)]
2
3use std::ffi::{CStr, CString};
4use std::num::NonZero;
5use std::ptr;
6use std::str::FromStr;
7use std::string::FromUtf8Error;
8
9use libc::c_uint;
10use rustc_abi::{Align, Size, WrappingRange};
11use rustc_llvm::RustString;
12
13pub(crate) use self::CallConv::*;
14pub(crate) use self::CodeGenOptSize::*;
15pub(crate) use self::MetadataType::*;
16pub(crate) use self::ffi::*;
17use crate::common::AsCCharPtr;
18
19pub(crate) mod archive_ro;
20pub(crate) mod diagnostic;
21pub(crate) mod enzyme_ffi;
22mod ffi;
23
24pub(crate) use self::enzyme_ffi::*;
25
26impl LLVMRustResult {
27    pub(crate) fn into_result(self) -> Result<(), ()> {
28        match self {
29            LLVMRustResult::Success => Ok(()),
30            LLVMRustResult::Failure => Err(()),
31        }
32    }
33}
34
35pub(crate) fn AddFunctionAttributes<'ll>(
36    llfn: &'ll Value,
37    idx: AttributePlace,
38    attrs: &[&'ll Attribute],
39) {
40    unsafe {
41        LLVMRustAddFunctionAttributes(llfn, idx.as_uint(), attrs.as_ptr(), attrs.len());
42    }
43}
44
45pub(crate) fn HasAttributeAtIndex<'ll>(
46    llfn: &'ll Value,
47    idx: AttributePlace,
48    kind: AttributeKind,
49) -> bool {
50    unsafe { LLVMRustHasAttributeAtIndex(llfn, idx.as_uint(), kind) }
51}
52
53pub(crate) fn HasStringAttribute<'ll>(llfn: &'ll Value, name: &str) -> bool {
54    unsafe { LLVMRustHasFnAttribute(llfn, name.as_c_char_ptr(), name.len()) }
55}
56
57pub(crate) fn RemoveStringAttrFromFn<'ll>(llfn: &'ll Value, name: &str) {
58    unsafe { LLVMRustRemoveFnAttribute(llfn, name.as_c_char_ptr(), name.len()) }
59}
60
61pub(crate) fn RemoveRustEnumAttributeAtIndex(
62    llfn: &Value,
63    place: AttributePlace,
64    kind: AttributeKind,
65) {
66    unsafe {
67        LLVMRustRemoveEnumAttributeAtIndex(llfn, place.as_uint(), kind);
68    }
69}
70
71pub(crate) fn AddCallSiteAttributes<'ll>(
72    callsite: &'ll Value,
73    idx: AttributePlace,
74    attrs: &[&'ll Attribute],
75) {
76    unsafe {
77        LLVMRustAddCallSiteAttributes(callsite, idx.as_uint(), attrs.as_ptr(), attrs.len());
78    }
79}
80
81pub(crate) fn CreateAttrStringValue<'ll>(
82    llcx: &'ll Context,
83    attr: &str,
84    value: &str,
85) -> &'ll Attribute {
86    unsafe {
87        LLVMCreateStringAttribute(
88            llcx,
89            attr.as_c_char_ptr(),
90            attr.len().try_into().unwrap(),
91            value.as_c_char_ptr(),
92            value.len().try_into().unwrap(),
93        )
94    }
95}
96
97pub(crate) fn CreateAttrString<'ll>(llcx: &'ll Context, attr: &str) -> &'ll Attribute {
98    unsafe {
99        LLVMCreateStringAttribute(
100            llcx,
101            attr.as_c_char_ptr(),
102            attr.len().try_into().unwrap(),
103            std::ptr::null(),
104            0,
105        )
106    }
107}
108
109pub(crate) fn CreateAlignmentAttr(llcx: &Context, bytes: u64) -> &Attribute {
110    unsafe { LLVMRustCreateAlignmentAttr(llcx, bytes) }
111}
112
113pub(crate) fn CreateDereferenceableAttr(llcx: &Context, bytes: u64) -> &Attribute {
114    unsafe { LLVMRustCreateDereferenceableAttr(llcx, bytes) }
115}
116
117pub(crate) fn CreateDereferenceableOrNullAttr(llcx: &Context, bytes: u64) -> &Attribute {
118    unsafe { LLVMRustCreateDereferenceableOrNullAttr(llcx, bytes) }
119}
120
121pub(crate) fn CreateByValAttr<'ll>(llcx: &'ll Context, ty: &'ll Type) -> &'ll Attribute {
122    unsafe { LLVMRustCreateByValAttr(llcx, ty) }
123}
124
125pub(crate) fn CreateStructRetAttr<'ll>(llcx: &'ll Context, ty: &'ll Type) -> &'ll Attribute {
126    unsafe { LLVMRustCreateStructRetAttr(llcx, ty) }
127}
128
129pub(crate) fn CreateUWTableAttr(llcx: &Context, async_: bool) -> &Attribute {
130    unsafe { LLVMRustCreateUWTableAttr(llcx, async_) }
131}
132
133pub(crate) fn CreateAllocSizeAttr(llcx: &Context, size_arg: u32) -> &Attribute {
134    unsafe { LLVMRustCreateAllocSizeAttr(llcx, size_arg) }
135}
136
137pub(crate) fn CreateAllocKindAttr(llcx: &Context, kind_arg: AllocKindFlags) -> &Attribute {
138    unsafe { LLVMRustCreateAllocKindAttr(llcx, kind_arg.bits()) }
139}
140
141pub(crate) fn CreateRangeAttr(llcx: &Context, size: Size, range: WrappingRange) -> &Attribute {
142    let lower = range.start;
143    let upper = range.end.wrapping_add(1);
144    let lower_words = [lower as u64, (lower >> 64) as u64];
145    let upper_words = [upper as u64, (upper >> 64) as u64];
146    unsafe {
147        LLVMRustCreateRangeAttribute(
148            llcx,
149            size.bits().try_into().unwrap(),
150            lower_words.as_ptr(),
151            upper_words.as_ptr(),
152        )
153    }
154}
155
156#[derive(Copy, Clone)]
157pub(crate) enum AttributePlace {
158    ReturnValue,
159    Argument(u32),
160    Function,
161}
162
163impl AttributePlace {
164    pub(crate) fn as_uint(self) -> c_uint {
165        match self {
166            AttributePlace::ReturnValue => 0,
167            AttributePlace::Argument(i) => 1 + i,
168            AttributePlace::Function => !0,
169        }
170    }
171}
172
173#[derive(Copy, Clone, PartialEq)]
174#[repr(C)]
175pub(crate) enum CodeGenOptSize {
176    CodeGenOptSizeNone = 0,
177    CodeGenOptSizeDefault = 1,
178    CodeGenOptSizeAggressive = 2,
179}
180
181impl FromStr for ArchiveKind {
182    type Err = ();
183
184    fn from_str(s: &str) -> Result<Self, Self::Err> {
185        match s {
186            "gnu" => Ok(ArchiveKind::K_GNU),
187            "bsd" => Ok(ArchiveKind::K_BSD),
188            "darwin" => Ok(ArchiveKind::K_DARWIN),
189            "coff" => Ok(ArchiveKind::K_COFF),
190            "aix_big" => Ok(ArchiveKind::K_AIXBIG),
191            _ => Err(()),
192        }
193    }
194}
195
196pub(crate) fn SetInstructionCallConv(instr: &Value, cc: CallConv) {
197    unsafe {
198        LLVMSetInstructionCallConv(instr, cc as c_uint);
199    }
200}
201pub(crate) fn SetFunctionCallConv(fn_: &Value, cc: CallConv) {
202    unsafe {
203        LLVMSetFunctionCallConv(fn_, cc as c_uint);
204    }
205}
206
207// Externally visible symbols that might appear in multiple codegen units need to appear in
208// their own comdat section so that the duplicates can be discarded at link time. This can for
209// example happen for generics when using multiple codegen units. This function simply uses the
210// value's name as the comdat value to make sure that it is in a 1-to-1 relationship to the
211// function.
212// For more details on COMDAT sections see e.g., https://www.airs.com/blog/archives/52
213pub(crate) fn SetUniqueComdat(llmod: &Module, val: &Value) {
214    let name_buf = get_value_name(val).to_vec();
215    let name =
216        CString::from_vec_with_nul(name_buf).or_else(|buf| CString::new(buf.into_bytes())).unwrap();
217    set_comdat(llmod, val, &name);
218}
219
220pub(crate) fn SetUnnamedAddress(global: &Value, unnamed: UnnamedAddr) {
221    unsafe {
222        LLVMSetUnnamedAddress(global, unnamed);
223    }
224}
225
226pub(crate) fn set_thread_local_mode(global: &Value, mode: ThreadLocalMode) {
227    unsafe {
228        LLVMSetThreadLocalMode(global, mode);
229    }
230}
231
232impl AttributeKind {
233    /// Create an LLVM Attribute with no associated value.
234    pub(crate) fn create_attr(self, llcx: &Context) -> &Attribute {
235        unsafe { LLVMRustCreateAttrNoValue(llcx, self) }
236    }
237}
238
239impl MemoryEffects {
240    /// Create an LLVM Attribute with these memory effects.
241    pub(crate) fn create_attr(self, llcx: &Context) -> &Attribute {
242        unsafe { LLVMRustCreateMemoryEffectsAttr(llcx, self) }
243    }
244}
245
246pub(crate) fn set_section(llglobal: &Value, section_name: &CStr) {
247    unsafe {
248        LLVMSetSection(llglobal, section_name.as_ptr());
249    }
250}
251
252pub(crate) fn add_global<'a>(llmod: &'a Module, ty: &'a Type, name_cstr: &CStr) -> &'a Value {
253    unsafe { LLVMAddGlobal(llmod, ty, name_cstr.as_ptr()) }
254}
255
256pub(crate) fn set_initializer(llglobal: &Value, constant_val: &Value) {
257    unsafe {
258        LLVMSetInitializer(llglobal, constant_val);
259    }
260}
261
262pub(crate) fn set_global_constant(llglobal: &Value, is_constant: bool) {
263    unsafe {
264        LLVMSetGlobalConstant(llglobal, if is_constant { ffi::True } else { ffi::False });
265    }
266}
267
268pub(crate) fn get_linkage(llglobal: &Value) -> Linkage {
269    unsafe { LLVMGetLinkage(llglobal) }.to_rust()
270}
271
272pub(crate) fn set_linkage(llglobal: &Value, linkage: Linkage) {
273    unsafe {
274        LLVMSetLinkage(llglobal, linkage);
275    }
276}
277
278pub(crate) fn is_declaration(llglobal: &Value) -> bool {
279    unsafe { LLVMIsDeclaration(llglobal) == ffi::True }
280}
281
282pub(crate) fn get_visibility(llglobal: &Value) -> Visibility {
283    unsafe { LLVMGetVisibility(llglobal) }.to_rust()
284}
285
286pub(crate) fn set_visibility(llglobal: &Value, visibility: Visibility) {
287    unsafe {
288        LLVMSetVisibility(llglobal, visibility);
289    }
290}
291
292pub(crate) fn set_alignment(llglobal: &Value, align: Align) {
293    unsafe {
294        ffi::LLVMSetAlignment(llglobal, align.bytes() as c_uint);
295    }
296}
297
298/// Get the `name`d comdat from `llmod` and assign it to `llglobal`.
299///
300/// Inserts the comdat into `llmod` if it does not exist.
301/// It is an error to call this if the target does not support comdat.
302pub(crate) fn set_comdat(llmod: &Module, llglobal: &Value, name: &CStr) {
303    unsafe {
304        let comdat = LLVMGetOrInsertComdat(llmod, name.as_ptr());
305        LLVMSetComdat(llglobal, comdat);
306    }
307}
308
309/// Safe wrapper around `LLVMGetParam`, because segfaults are no fun.
310pub(crate) fn get_param(llfn: &Value, index: c_uint) -> &Value {
311    unsafe {
312        assert!(
313            index < LLVMCountParams(llfn),
314            "out of bounds argument access: {} out of {} arguments",
315            index,
316            LLVMCountParams(llfn)
317        );
318        LLVMGetParam(llfn, index)
319    }
320}
321
322/// Safe wrapper for `LLVMGetValueName2` into a byte slice
323pub(crate) fn get_value_name(value: &Value) -> &[u8] {
324    unsafe {
325        let mut len = 0;
326        let data = LLVMGetValueName2(value, &mut len);
327        std::slice::from_raw_parts(data.cast(), len)
328    }
329}
330
331#[derive(Debug, Copy, Clone)]
332pub(crate) struct Intrinsic {
333    id: NonZero<c_uint>,
334}
335
336impl Intrinsic {
337    pub(crate) fn lookup(name: &[u8]) -> Option<Self> {
338        let id = unsafe { LLVMLookupIntrinsicID(name.as_c_char_ptr(), name.len()) };
339        NonZero::new(id).map(|id| Self { id })
340    }
341
342    pub(crate) fn get_declaration<'ll>(
343        self,
344        llmod: &'ll Module,
345        type_params: &[&'ll Type],
346    ) -> &'ll Value {
347        unsafe {
348            LLVMGetIntrinsicDeclaration(llmod, self.id, type_params.as_ptr(), type_params.len())
349        }
350    }
351}
352
353/// Safe wrapper for `LLVMSetValueName2` from a byte slice
354pub(crate) fn set_value_name(value: &Value, name: &[u8]) {
355    unsafe {
356        let data = name.as_c_char_ptr();
357        LLVMSetValueName2(value, data, name.len());
358    }
359}
360
361pub(crate) fn build_string(f: impl FnOnce(&RustString)) -> Result<String, FromUtf8Error> {
362    String::from_utf8(RustString::build_byte_buffer(f))
363}
364
365pub(crate) fn build_byte_buffer(f: impl FnOnce(&RustString)) -> Vec<u8> {
366    RustString::build_byte_buffer(f)
367}
368
369pub(crate) fn twine_to_string(tr: &Twine) -> String {
370    unsafe {
371        build_string(|s| LLVMRustWriteTwineToString(tr, s)).expect("got a non-UTF8 Twine from LLVM")
372    }
373}
374
375pub(crate) fn last_error() -> Option<String> {
376    unsafe {
377        let cstr = LLVMRustGetLastError();
378        if cstr.is_null() {
379            None
380        } else {
381            let err = CStr::from_ptr(cstr).to_bytes();
382            let err = String::from_utf8_lossy(err).to_string();
383            libc::free(cstr as *mut _);
384            Some(err)
385        }
386    }
387}
388
389/// Owning pointer to an [`OperandBundle`] that will dispose of the bundle
390/// when dropped.
391pub(crate) struct OperandBundleBox<'a> {
392    raw: ptr::NonNull<OperandBundle<'a>>,
393}
394
395impl<'a> OperandBundleBox<'a> {
396    pub(crate) fn new(name: &str, vals: &[&'a Value]) -> Self {
397        let raw = unsafe {
398            LLVMCreateOperandBundle(
399                name.as_c_char_ptr(),
400                name.len(),
401                vals.as_ptr(),
402                vals.len() as c_uint,
403            )
404        };
405        Self { raw: ptr::NonNull::new(raw).unwrap() }
406    }
407
408    /// Dereferences to the underlying `&OperandBundle`.
409    ///
410    /// This can't be a `Deref` implementation because `OperandBundle` transitively
411    /// contains an extern type, which is incompatible with `Deref::Target: ?Sized`.
412    pub(crate) fn as_ref(&self) -> &OperandBundle<'a> {
413        // SAFETY: The returned reference is opaque and can only used for FFI.
414        // It is valid for as long as `&self` is.
415        unsafe { self.raw.as_ref() }
416    }
417}
418
419impl Drop for OperandBundleBox<'_> {
420    fn drop(&mut self) {
421        unsafe {
422            LLVMDisposeOperandBundle(self.raw);
423        }
424    }
425}
426
427pub(crate) fn add_module_flag_u32(
428    module: &Module,
429    merge_behavior: ModuleFlagMergeBehavior,
430    key: &str,
431    value: u32,
432) {
433    unsafe {
434        LLVMRustAddModuleFlagU32(module, merge_behavior, key.as_c_char_ptr(), key.len(), value);
435    }
436}
437
438pub(crate) fn add_module_flag_str(
439    module: &Module,
440    merge_behavior: ModuleFlagMergeBehavior,
441    key: &str,
442    value: &str,
443) {
444    unsafe {
445        LLVMRustAddModuleFlagString(
446            module,
447            merge_behavior,
448            key.as_c_char_ptr(),
449            key.len(),
450            value.as_c_char_ptr(),
451            value.len(),
452        );
453    }
454}
455
456pub(crate) fn set_dllimport_storage_class<'ll>(v: &'ll Value) {
457    unsafe {
458        LLVMSetDLLStorageClass(v, DLLStorageClass::DllImport);
459    }
460}
461
462pub(crate) fn set_dso_local<'ll>(v: &'ll Value) {
463    unsafe {
464        LLVMRustSetDSOLocal(v, true);
465    }
466}
467
468/// Safe wrapper for `LLVMAppendModuleInlineAsm`, which delegates to
469/// `Module::appendModuleInlineAsm`.
470pub(crate) fn append_module_inline_asm<'ll>(llmod: &'ll Module, asm: &[u8]) {
471    unsafe {
472        LLVMAppendModuleInlineAsm(llmod, asm.as_ptr(), asm.len());
473    }
474}