rustc_codegen_llvm/llvm/
ffi.rs

1//! Bindings to the LLVM-C API (`LLVM*`), and to our own `extern "C"` wrapper
2//! functions around the unstable LLVM C++ API (`LLVMRust*`).
3//!
4//! ## Passing pointer/length strings as `*const c_uchar` (PTR_LEN_STR)
5//!
6//! Normally it's a good idea for Rust-side bindings to match the corresponding
7//! C-side function declarations as closely as possible. But when passing `&str`
8//! or `&[u8]` data as a pointer/length pair, it's more convenient to declare
9//! the Rust-side pointer as `*const c_uchar` instead of `*const c_char`.
10//! Both pointer types have the same ABI, and using `*const c_uchar` avoids
11//! the need for an extra cast from `*const u8` on the Rust side.
12
13#![allow(non_camel_case_types)]
14#![allow(non_upper_case_globals)]
15
16use std::fmt::Debug;
17use std::marker::PhantomData;
18use std::num::NonZero;
19use std::ptr;
20
21use bitflags::bitflags;
22use libc::{c_char, c_int, c_uchar, c_uint, c_ulonglong, c_void, size_t};
23use rustc_macros::TryFromU32;
24use rustc_target::spec::SymbolVisibility;
25
26use super::RustString;
27use super::debuginfo::{
28    DIArray, DIBasicType, DIBuilder, DICompositeType, DIDerivedType, DIDescriptor, DIEnumerator,
29    DIFile, DIFlags, DIGlobalVariableExpression, DILocation, DISPFlags, DIScope, DISubprogram,
30    DISubrange, DITemplateTypeParameter, DIType, DIVariable, DebugEmissionKind, DebugNameTableKind,
31};
32use crate::llvm;
33
34/// In the LLVM-C API, boolean values are passed as `typedef int LLVMBool`,
35/// which has a different ABI from Rust or C++ `bool`.
36pub(crate) type Bool = c_int;
37
38pub(crate) const True: Bool = 1 as Bool;
39pub(crate) const False: Bool = 0 as Bool;
40
41/// Wrapper for a raw enum value returned from LLVM's C APIs.
42///
43/// For C enums returned by LLVM, it's risky to use a Rust enum as the return
44/// type, because it would be UB if a later version of LLVM adds a new enum
45/// value and returns it. Instead, return this raw wrapper, then convert to the
46/// Rust-side enum explicitly.
47#[repr(transparent)]
48pub(crate) struct RawEnum<T> {
49    value: u32,
50    /// We don't own or consume a `T`, but we can produce one.
51    _rust_side_type: PhantomData<fn() -> T>,
52}
53
54impl<T: TryFrom<u32>> RawEnum<T> {
55    #[track_caller]
56    pub(crate) fn to_rust(self) -> T
57    where
58        T::Error: Debug,
59    {
60        // If this fails, the Rust-side enum is out of sync with LLVM's enum.
61        T::try_from(self.value).expect("enum value returned by LLVM should be known")
62    }
63}
64
65#[derive(Copy, Clone, PartialEq)]
66#[repr(C)]
67#[allow(dead_code)] // Variants constructed by C++.
68pub(crate) enum LLVMRustResult {
69    Success,
70    Failure,
71}
72
73/// Must match the layout of `LLVMRustModuleFlagMergeBehavior`.
74///
75/// When merging modules (e.g. during LTO), their metadata flags are combined. Conflicts are
76/// resolved according to the merge behaviors specified here. Flags differing only in merge
77/// behavior are still considered to be in conflict.
78///
79/// In order for Rust-C LTO to work, we must specify behaviors compatible with Clang. Notably,
80/// 'Error' and 'Warning' cannot be mixed for a given flag.
81///
82/// There is a stable LLVM-C version of this enum (`LLVMModuleFlagBehavior`),
83/// but as of LLVM 19 it does not support all of the enum values in the unstable
84/// C++ API.
85#[derive(Copy, Clone, PartialEq)]
86#[repr(C)]
87pub(crate) enum ModuleFlagMergeBehavior {
88    Error = 1,
89    Warning = 2,
90    Require = 3,
91    Override = 4,
92    Append = 5,
93    AppendUnique = 6,
94    Max = 7,
95    Min = 8,
96}
97
98// Consts for the LLVM CallConv type, pre-cast to usize.
99
100/// Must match the layout of `LLVMTailCallKind`.
101#[derive(Copy, Clone, PartialEq, Debug)]
102#[repr(C)]
103#[allow(dead_code)]
104pub(crate) enum TailCallKind {
105    None = 0,
106    Tail = 1,
107    MustTail = 2,
108    NoTail = 3,
109}
110
111/// LLVM CallingConv::ID. Should we wrap this?
112///
113/// See <https://github.com/llvm/llvm-project/blob/main/llvm/include/llvm/IR/CallingConv.h>
114#[derive(Copy, Clone, PartialEq, Debug, TryFromU32)]
115#[repr(C)]
116pub(crate) enum CallConv {
117    CCallConv = 0,
118    FastCallConv = 8,
119    ColdCallConv = 9,
120    PreserveMost = 14,
121    PreserveAll = 15,
122    Tail = 18,
123    X86StdcallCallConv = 64,
124    X86FastcallCallConv = 65,
125    ArmAapcsCallConv = 67,
126    Msp430Intr = 69,
127    X86_ThisCall = 70,
128    PtxKernel = 71,
129    X86_64_SysV = 78,
130    X86_64_Win64 = 79,
131    X86_VectorCall = 80,
132    X86_Intr = 83,
133    AvrNonBlockingInterrupt = 84,
134    AvrInterrupt = 85,
135    AmdgpuKernel = 91,
136}
137
138/// Must match the layout of `LLVMLinkage`.
139#[derive(Copy, Clone, PartialEq, TryFromU32)]
140#[repr(C)]
141pub(crate) enum Linkage {
142    ExternalLinkage = 0,
143    AvailableExternallyLinkage = 1,
144    LinkOnceAnyLinkage = 2,
145    LinkOnceODRLinkage = 3,
146    #[deprecated = "marked obsolete by LLVM"]
147    LinkOnceODRAutoHideLinkage = 4,
148    WeakAnyLinkage = 5,
149    WeakODRLinkage = 6,
150    AppendingLinkage = 7,
151    InternalLinkage = 8,
152    PrivateLinkage = 9,
153    #[deprecated = "marked obsolete by LLVM"]
154    DLLImportLinkage = 10,
155    #[deprecated = "marked obsolete by LLVM"]
156    DLLExportLinkage = 11,
157    ExternalWeakLinkage = 12,
158    #[deprecated = "marked obsolete by LLVM"]
159    GhostLinkage = 13,
160    CommonLinkage = 14,
161    LinkerPrivateLinkage = 15,
162    LinkerPrivateWeakLinkage = 16,
163}
164
165/// Must match the layout of `LLVMVisibility`.
166#[repr(C)]
167#[derive(Copy, Clone, PartialEq, TryFromU32)]
168pub(crate) enum Visibility {
169    Default = 0,
170    Hidden = 1,
171    Protected = 2,
172}
173
174impl Visibility {
175    pub(crate) fn from_generic(visibility: SymbolVisibility) -> Self {
176        match visibility {
177            SymbolVisibility::Hidden => Visibility::Hidden,
178            SymbolVisibility::Protected => Visibility::Protected,
179            SymbolVisibility::Interposable => Visibility::Default,
180        }
181    }
182}
183
184/// LLVMUnnamedAddr
185#[repr(C)]
186pub(crate) enum UnnamedAddr {
187    No,
188    #[expect(dead_code)]
189    Local,
190    Global,
191}
192
193/// LLVMDLLStorageClass
194#[derive(Copy, Clone)]
195#[repr(C)]
196pub(crate) enum DLLStorageClass {
197    #[allow(dead_code)]
198    Default = 0,
199    DllImport = 1, // Function to be imported from DLL.
200    #[allow(dead_code)]
201    DllExport = 2, // Function to be accessible from DLL.
202}
203
204/// Must match the layout of `LLVMRustAttributeKind`.
205/// Semantically a subset of the C++ enum llvm::Attribute::AttrKind,
206/// though it is not ABI compatible (since it's a C++ enum)
207#[repr(C)]
208#[derive(Copy, Clone, Debug)]
209#[expect(dead_code, reason = "Some variants are unused, but are kept to match the C++")]
210pub(crate) enum AttributeKind {
211    AlwaysInline = 0,
212    ByVal = 1,
213    Cold = 2,
214    InlineHint = 3,
215    MinSize = 4,
216    Naked = 5,
217    NoAlias = 6,
218    NoCapture = 7,
219    NoInline = 8,
220    NonNull = 9,
221    NoRedZone = 10,
222    NoReturn = 11,
223    NoUnwind = 12,
224    OptimizeForSize = 13,
225    ReadOnly = 14,
226    SExt = 15,
227    StructRet = 16,
228    UWTable = 17,
229    ZExt = 18,
230    InReg = 19,
231    SanitizeThread = 20,
232    SanitizeAddress = 21,
233    SanitizeMemory = 22,
234    NonLazyBind = 23,
235    OptimizeNone = 24,
236    ReadNone = 26,
237    SanitizeHWAddress = 28,
238    WillReturn = 29,
239    StackProtectReq = 30,
240    StackProtectStrong = 31,
241    StackProtect = 32,
242    NoUndef = 33,
243    SanitizeMemTag = 34,
244    NoCfCheck = 35,
245    ShadowCallStack = 36,
246    AllocSize = 37,
247    AllocatedPointer = 38,
248    AllocAlign = 39,
249    SanitizeSafeStack = 40,
250    FnRetThunkExtern = 41,
251    Writable = 42,
252    DeadOnUnwind = 43,
253    DeadOnReturn = 44,
254}
255
256/// LLVMIntPredicate
257#[derive(Copy, Clone)]
258#[repr(C)]
259pub(crate) enum IntPredicate {
260    IntEQ = 32,
261    IntNE = 33,
262    IntUGT = 34,
263    IntUGE = 35,
264    IntULT = 36,
265    IntULE = 37,
266    IntSGT = 38,
267    IntSGE = 39,
268    IntSLT = 40,
269    IntSLE = 41,
270}
271
272impl IntPredicate {
273    pub(crate) fn from_generic(intpre: rustc_codegen_ssa::common::IntPredicate) -> Self {
274        use rustc_codegen_ssa::common::IntPredicate as Common;
275        match intpre {
276            Common::IntEQ => Self::IntEQ,
277            Common::IntNE => Self::IntNE,
278            Common::IntUGT => Self::IntUGT,
279            Common::IntUGE => Self::IntUGE,
280            Common::IntULT => Self::IntULT,
281            Common::IntULE => Self::IntULE,
282            Common::IntSGT => Self::IntSGT,
283            Common::IntSGE => Self::IntSGE,
284            Common::IntSLT => Self::IntSLT,
285            Common::IntSLE => Self::IntSLE,
286        }
287    }
288}
289
290/// LLVMRealPredicate
291#[derive(Copy, Clone)]
292#[repr(C)]
293pub(crate) enum RealPredicate {
294    RealPredicateFalse = 0,
295    RealOEQ = 1,
296    RealOGT = 2,
297    RealOGE = 3,
298    RealOLT = 4,
299    RealOLE = 5,
300    RealONE = 6,
301    RealORD = 7,
302    RealUNO = 8,
303    RealUEQ = 9,
304    RealUGT = 10,
305    RealUGE = 11,
306    RealULT = 12,
307    RealULE = 13,
308    RealUNE = 14,
309    RealPredicateTrue = 15,
310}
311
312impl RealPredicate {
313    pub(crate) fn from_generic(realp: rustc_codegen_ssa::common::RealPredicate) -> Self {
314        use rustc_codegen_ssa::common::RealPredicate as Common;
315        match realp {
316            Common::RealPredicateFalse => Self::RealPredicateFalse,
317            Common::RealOEQ => Self::RealOEQ,
318            Common::RealOGT => Self::RealOGT,
319            Common::RealOGE => Self::RealOGE,
320            Common::RealOLT => Self::RealOLT,
321            Common::RealOLE => Self::RealOLE,
322            Common::RealONE => Self::RealONE,
323            Common::RealORD => Self::RealORD,
324            Common::RealUNO => Self::RealUNO,
325            Common::RealUEQ => Self::RealUEQ,
326            Common::RealUGT => Self::RealUGT,
327            Common::RealUGE => Self::RealUGE,
328            Common::RealULT => Self::RealULT,
329            Common::RealULE => Self::RealULE,
330            Common::RealUNE => Self::RealUNE,
331            Common::RealPredicateTrue => Self::RealPredicateTrue,
332        }
333    }
334}
335
336/// Must match the layout of `LLVMTypeKind`.
337///
338/// Use [`RawEnum<TypeKind>`] for values of `LLVMTypeKind` returned from LLVM,
339/// to avoid risk of UB if LLVM adds new enum values.
340///
341/// All of LLVM's variants should be declared here, even if no Rust-side code refers
342/// to them, because unknown variants will cause [`RawEnum::to_rust`] to panic.
343#[derive(Copy, Clone, PartialEq, Debug, TryFromU32)]
344#[repr(C)]
345pub(crate) enum TypeKind {
346    Void = 0,
347    Half = 1,
348    Float = 2,
349    Double = 3,
350    X86_FP80 = 4,
351    FP128 = 5,
352    PPC_FP128 = 6,
353    Label = 7,
354    Integer = 8,
355    Function = 9,
356    Struct = 10,
357    Array = 11,
358    Pointer = 12,
359    Vector = 13,
360    Metadata = 14,
361    Token = 16,
362    ScalableVector = 17,
363    BFloat = 18,
364    X86_AMX = 19,
365}
366
367impl TypeKind {
368    pub(crate) fn to_generic(self) -> rustc_codegen_ssa::common::TypeKind {
369        use rustc_codegen_ssa::common::TypeKind as Common;
370        match self {
371            Self::Void => Common::Void,
372            Self::Half => Common::Half,
373            Self::Float => Common::Float,
374            Self::Double => Common::Double,
375            Self::X86_FP80 => Common::X86_FP80,
376            Self::FP128 => Common::FP128,
377            Self::PPC_FP128 => Common::PPC_FP128,
378            Self::Label => Common::Label,
379            Self::Integer => Common::Integer,
380            Self::Function => Common::Function,
381            Self::Struct => Common::Struct,
382            Self::Array => Common::Array,
383            Self::Pointer => Common::Pointer,
384            Self::Vector => Common::Vector,
385            Self::Metadata => Common::Metadata,
386            Self::Token => Common::Token,
387            Self::ScalableVector => Common::ScalableVector,
388            Self::BFloat => Common::BFloat,
389            Self::X86_AMX => Common::X86_AMX,
390        }
391    }
392}
393
394/// LLVMAtomicRmwBinOp
395#[derive(Copy, Clone)]
396#[repr(C)]
397pub(crate) enum AtomicRmwBinOp {
398    AtomicXchg = 0,
399    AtomicAdd = 1,
400    AtomicSub = 2,
401    AtomicAnd = 3,
402    AtomicNand = 4,
403    AtomicOr = 5,
404    AtomicXor = 6,
405    AtomicMax = 7,
406    AtomicMin = 8,
407    AtomicUMax = 9,
408    AtomicUMin = 10,
409}
410
411impl AtomicRmwBinOp {
412    pub(crate) fn from_generic(op: rustc_codegen_ssa::common::AtomicRmwBinOp) -> Self {
413        use rustc_codegen_ssa::common::AtomicRmwBinOp as Common;
414        match op {
415            Common::AtomicXchg => Self::AtomicXchg,
416            Common::AtomicAdd => Self::AtomicAdd,
417            Common::AtomicSub => Self::AtomicSub,
418            Common::AtomicAnd => Self::AtomicAnd,
419            Common::AtomicNand => Self::AtomicNand,
420            Common::AtomicOr => Self::AtomicOr,
421            Common::AtomicXor => Self::AtomicXor,
422            Common::AtomicMax => Self::AtomicMax,
423            Common::AtomicMin => Self::AtomicMin,
424            Common::AtomicUMax => Self::AtomicUMax,
425            Common::AtomicUMin => Self::AtomicUMin,
426        }
427    }
428}
429
430/// LLVMAtomicOrdering
431#[derive(Copy, Clone)]
432#[repr(C)]
433pub(crate) enum AtomicOrdering {
434    #[allow(dead_code)]
435    NotAtomic = 0,
436    #[allow(dead_code)]
437    Unordered = 1,
438    Monotonic = 2,
439    // Consume = 3,  // Not specified yet.
440    Acquire = 4,
441    Release = 5,
442    AcquireRelease = 6,
443    SequentiallyConsistent = 7,
444}
445
446impl AtomicOrdering {
447    pub(crate) fn from_generic(ao: rustc_middle::ty::AtomicOrdering) -> Self {
448        use rustc_middle::ty::AtomicOrdering as Common;
449        match ao {
450            Common::Relaxed => Self::Monotonic,
451            Common::Acquire => Self::Acquire,
452            Common::Release => Self::Release,
453            Common::AcqRel => Self::AcquireRelease,
454            Common::SeqCst => Self::SequentiallyConsistent,
455        }
456    }
457}
458
459/// LLVMRustFileType
460#[derive(Copy, Clone)]
461#[repr(C)]
462pub(crate) enum FileType {
463    AssemblyFile,
464    ObjectFile,
465}
466
467/// LLVMMetadataType
468#[derive(Copy, Clone)]
469#[repr(C)]
470#[expect(dead_code, reason = "Some variants are unused, but are kept to match LLVM-C")]
471pub(crate) enum MetadataType {
472    MD_dbg = 0,
473    MD_tbaa = 1,
474    MD_prof = 2,
475    MD_fpmath = 3,
476    MD_range = 4,
477    MD_tbaa_struct = 5,
478    MD_invariant_load = 6,
479    MD_alias_scope = 7,
480    MD_noalias = 8,
481    MD_nontemporal = 9,
482    MD_mem_parallel_loop_access = 10,
483    MD_nonnull = 11,
484    MD_unpredictable = 15,
485    MD_align = 17,
486    MD_type = 19,
487    MD_vcall_visibility = 28,
488    MD_noundef = 29,
489    MD_kcfi_type = 36,
490}
491
492/// Must match the layout of `LLVMInlineAsmDialect`.
493#[derive(Copy, Clone, PartialEq)]
494#[repr(C)]
495pub(crate) enum AsmDialect {
496    Att,
497    Intel,
498}
499
500/// LLVMRustCodeGenOptLevel
501#[derive(Copy, Clone, PartialEq)]
502#[repr(C)]
503pub(crate) enum CodeGenOptLevel {
504    None,
505    Less,
506    Default,
507    Aggressive,
508}
509
510/// LLVMRustPassBuilderOptLevel
511#[repr(C)]
512pub(crate) enum PassBuilderOptLevel {
513    O0,
514    O1,
515    O2,
516    O3,
517    Os,
518    Oz,
519}
520
521/// LLVMRustOptStage
522#[derive(PartialEq)]
523#[repr(C)]
524pub(crate) enum OptStage {
525    PreLinkNoLTO,
526    PreLinkThinLTO,
527    PreLinkFatLTO,
528    ThinLTO,
529    FatLTO,
530}
531
532/// LLVMRustSanitizerOptions
533#[repr(C)]
534pub(crate) struct SanitizerOptions {
535    pub sanitize_address: bool,
536    pub sanitize_address_recover: bool,
537    pub sanitize_cfi: bool,
538    pub sanitize_dataflow: bool,
539    pub sanitize_dataflow_abilist: *const *const c_char,
540    pub sanitize_dataflow_abilist_len: size_t,
541    pub sanitize_kcfi: bool,
542    pub sanitize_memory: bool,
543    pub sanitize_memory_recover: bool,
544    pub sanitize_memory_track_origins: c_int,
545    pub sanitize_thread: bool,
546    pub sanitize_hwaddress: bool,
547    pub sanitize_hwaddress_recover: bool,
548    pub sanitize_kernel_address: bool,
549    pub sanitize_kernel_address_recover: bool,
550}
551
552/// LLVMRustRelocModel
553#[derive(Copy, Clone, PartialEq)]
554#[repr(C)]
555pub(crate) enum RelocModel {
556    Static,
557    PIC,
558    DynamicNoPic,
559    ROPI,
560    RWPI,
561    ROPI_RWPI,
562}
563
564/// LLVMRustFloatABI
565#[derive(Copy, Clone, PartialEq)]
566#[repr(C)]
567pub(crate) enum FloatAbi {
568    Default,
569    Soft,
570    Hard,
571}
572
573/// LLVMRustCodeModel
574#[derive(Copy, Clone)]
575#[repr(C)]
576pub(crate) enum CodeModel {
577    Tiny,
578    Small,
579    Kernel,
580    Medium,
581    Large,
582    None,
583}
584
585/// LLVMRustDiagnosticKind
586#[derive(Copy, Clone)]
587#[repr(C)]
588#[allow(dead_code)] // Variants constructed by C++.
589pub(crate) enum DiagnosticKind {
590    Other,
591    InlineAsm,
592    StackSize,
593    DebugMetadataVersion,
594    SampleProfile,
595    OptimizationRemark,
596    OptimizationRemarkMissed,
597    OptimizationRemarkAnalysis,
598    OptimizationRemarkAnalysisFPCommute,
599    OptimizationRemarkAnalysisAliasing,
600    OptimizationRemarkOther,
601    OptimizationFailure,
602    PGOProfile,
603    Linker,
604    Unsupported,
605    SrcMgr,
606}
607
608/// LLVMRustDiagnosticLevel
609#[derive(Copy, Clone)]
610#[repr(C)]
611#[allow(dead_code)] // Variants constructed by C++.
612pub(crate) enum DiagnosticLevel {
613    Error,
614    Warning,
615    Note,
616    Remark,
617}
618
619/// LLVMRustArchiveKind
620#[derive(Copy, Clone)]
621#[repr(C)]
622pub(crate) enum ArchiveKind {
623    K_GNU,
624    K_BSD,
625    K_DARWIN,
626    K_COFF,
627    K_AIXBIG,
628}
629
630unsafe extern "C" {
631    // LLVMRustThinLTOData
632    pub(crate) type ThinLTOData;
633
634    // LLVMRustThinLTOBuffer
635    pub(crate) type ThinLTOBuffer;
636}
637
638/// LLVMRustThinLTOModule
639#[repr(C)]
640pub(crate) struct ThinLTOModule {
641    pub identifier: *const c_char,
642    pub data: *const u8,
643    pub len: usize,
644}
645
646/// LLVMThreadLocalMode
647#[derive(Copy, Clone)]
648#[repr(C)]
649pub(crate) enum ThreadLocalMode {
650    #[expect(dead_code)]
651    NotThreadLocal,
652    GeneralDynamic,
653    LocalDynamic,
654    InitialExec,
655    LocalExec,
656}
657
658/// LLVMRustChecksumKind
659#[derive(Copy, Clone)]
660#[repr(C)]
661pub(crate) enum ChecksumKind {
662    None,
663    MD5,
664    SHA1,
665    SHA256,
666}
667
668/// LLVMRustMemoryEffects
669#[derive(Copy, Clone)]
670#[repr(C)]
671pub(crate) enum MemoryEffects {
672    None,
673    ReadOnly,
674    InaccessibleMemOnly,
675}
676
677/// LLVMOpcode
678#[derive(Copy, Clone, PartialEq, Eq)]
679#[repr(C)]
680#[expect(dead_code, reason = "Some variants are unused, but are kept to match LLVM-C")]
681pub(crate) enum Opcode {
682    Ret = 1,
683    Br = 2,
684    Switch = 3,
685    IndirectBr = 4,
686    Invoke = 5,
687    Unreachable = 7,
688    CallBr = 67,
689    FNeg = 66,
690    Add = 8,
691    FAdd = 9,
692    Sub = 10,
693    FSub = 11,
694    Mul = 12,
695    FMul = 13,
696    UDiv = 14,
697    SDiv = 15,
698    FDiv = 16,
699    URem = 17,
700    SRem = 18,
701    FRem = 19,
702    Shl = 20,
703    LShr = 21,
704    AShr = 22,
705    And = 23,
706    Or = 24,
707    Xor = 25,
708    Alloca = 26,
709    Load = 27,
710    Store = 28,
711    GetElementPtr = 29,
712    Trunc = 30,
713    ZExt = 31,
714    SExt = 32,
715    FPToUI = 33,
716    FPToSI = 34,
717    UIToFP = 35,
718    SIToFP = 36,
719    FPTrunc = 37,
720    FPExt = 38,
721    PtrToInt = 39,
722    IntToPtr = 40,
723    BitCast = 41,
724    AddrSpaceCast = 60,
725    ICmp = 42,
726    FCmp = 43,
727    PHI = 44,
728    Call = 45,
729    Select = 46,
730    UserOp1 = 47,
731    UserOp2 = 48,
732    VAArg = 49,
733    ExtractElement = 50,
734    InsertElement = 51,
735    ShuffleVector = 52,
736    ExtractValue = 53,
737    InsertValue = 54,
738    Freeze = 68,
739    Fence = 55,
740    AtomicCmpXchg = 56,
741    AtomicRMW = 57,
742    Resume = 58,
743    LandingPad = 59,
744    CleanupRet = 61,
745    CatchRet = 62,
746    CatchPad = 63,
747    CleanupPad = 64,
748    CatchSwitch = 65,
749}
750
751unsafe extern "C" {
752    type Opaque;
753}
754#[repr(C)]
755struct InvariantOpaque<'a> {
756    _marker: PhantomData<&'a mut &'a ()>,
757    _opaque: Opaque,
758}
759
760// Opaque pointer types
761unsafe extern "C" {
762    pub(crate) type Module;
763    pub(crate) type Context;
764    pub(crate) type Type;
765    pub(crate) type Value;
766    pub(crate) type ConstantInt;
767    pub(crate) type Attribute;
768    pub(crate) type Metadata;
769    pub(crate) type BasicBlock;
770    pub(crate) type Comdat;
771}
772#[repr(C)]
773pub(crate) struct Builder<'a>(InvariantOpaque<'a>);
774#[repr(C)]
775pub(crate) struct PassManager<'a>(InvariantOpaque<'a>);
776unsafe extern "C" {
777    pub type TargetMachine;
778    pub(crate) type Archive;
779}
780#[repr(C)]
781pub(crate) struct ArchiveIterator<'a>(InvariantOpaque<'a>);
782#[repr(C)]
783pub(crate) struct ArchiveChild<'a>(InvariantOpaque<'a>);
784unsafe extern "C" {
785    pub(crate) type Twine;
786    pub(crate) type DiagnosticInfo;
787    pub(crate) type SMDiagnostic;
788}
789#[repr(C)]
790pub(crate) struct RustArchiveMember<'a>(InvariantOpaque<'a>);
791/// Opaque pointee of `LLVMOperandBundleRef`.
792#[repr(C)]
793pub(crate) struct OperandBundle<'a>(InvariantOpaque<'a>);
794#[repr(C)]
795pub(crate) struct Linker<'a>(InvariantOpaque<'a>);
796
797unsafe extern "C" {
798    pub(crate) type DiagnosticHandler;
799}
800
801pub(crate) type DiagnosticHandlerTy = unsafe extern "C" fn(&DiagnosticInfo, *mut c_void);
802
803pub(crate) mod debuginfo {
804    use std::ptr;
805
806    use bitflags::bitflags;
807
808    use super::{InvariantOpaque, Metadata};
809    use crate::llvm::{self, Module};
810
811    /// Opaque target type for references to an LLVM debuginfo builder.
812    ///
813    /// `&'_ DIBuilder<'ll>` corresponds to `LLVMDIBuilderRef`, which is the
814    /// LLVM-C wrapper for `DIBuilder *`.
815    ///
816    /// Debuginfo builders are created and destroyed during codegen, so the
817    /// builder reference typically has a shorter lifetime than the LLVM
818    /// session (`'ll`) that it participates in.
819    #[repr(C)]
820    pub(crate) struct DIBuilder<'ll>(InvariantOpaque<'ll>);
821
822    /// Owning pointer to a `DIBuilder<'ll>` that will dispose of the builder
823    /// when dropped. Use `.as_ref()` to get the underlying `&DIBuilder`
824    /// needed for debuginfo FFI calls.
825    pub(crate) struct DIBuilderBox<'ll> {
826        raw: ptr::NonNull<DIBuilder<'ll>>,
827    }
828
829    impl<'ll> DIBuilderBox<'ll> {
830        pub(crate) fn new(llmod: &'ll Module) -> Self {
831            let raw = unsafe { llvm::LLVMCreateDIBuilder(llmod) };
832            let raw = ptr::NonNull::new(raw).unwrap();
833            Self { raw }
834        }
835
836        pub(crate) fn as_ref(&self) -> &DIBuilder<'ll> {
837            // SAFETY: This is an owning pointer, so `&DIBuilder` is valid
838            // for as long as `&self` is.
839            unsafe { self.raw.as_ref() }
840        }
841    }
842
843    impl<'ll> Drop for DIBuilderBox<'ll> {
844        fn drop(&mut self) {
845            unsafe { llvm::LLVMDisposeDIBuilder(self.raw) };
846        }
847    }
848
849    pub(crate) type DIDescriptor = Metadata;
850    pub(crate) type DILocation = Metadata;
851    pub(crate) type DIScope = DIDescriptor;
852    pub(crate) type DIFile = DIScope;
853    pub(crate) type DILexicalBlock = DIScope;
854    pub(crate) type DISubprogram = DIScope;
855    pub(crate) type DIType = DIDescriptor;
856    pub(crate) type DIBasicType = DIType;
857    pub(crate) type DIDerivedType = DIType;
858    pub(crate) type DICompositeType = DIDerivedType;
859    pub(crate) type DIVariable = DIDescriptor;
860    pub(crate) type DIGlobalVariableExpression = DIDescriptor;
861    pub(crate) type DIArray = DIDescriptor;
862    pub(crate) type DISubrange = DIDescriptor;
863    pub(crate) type DIEnumerator = DIDescriptor;
864    pub(crate) type DITemplateTypeParameter = DIDescriptor;
865
866    bitflags! {
867        /// Must match the layout of `LLVMDIFlags` in the LLVM-C API.
868        ///
869        /// Each value declared here must also be covered by the static
870        /// assertions in `RustWrapper.cpp` used by `fromRust(LLVMDIFlags)`.
871        #[repr(transparent)]
872        #[derive(Clone, Copy, Default)]
873        pub(crate) struct DIFlags: u32 {
874            const FlagZero                = 0;
875            const FlagPrivate             = 1;
876            const FlagProtected           = 2;
877            const FlagPublic              = 3;
878            const FlagFwdDecl             = (1 << 2);
879            const FlagAppleBlock          = (1 << 3);
880            const FlagReservedBit4        = (1 << 4);
881            const FlagVirtual             = (1 << 5);
882            const FlagArtificial          = (1 << 6);
883            const FlagExplicit            = (1 << 7);
884            const FlagPrototyped          = (1 << 8);
885            const FlagObjcClassComplete   = (1 << 9);
886            const FlagObjectPointer       = (1 << 10);
887            const FlagVector              = (1 << 11);
888            const FlagStaticMember        = (1 << 12);
889            const FlagLValueReference     = (1 << 13);
890            const FlagRValueReference     = (1 << 14);
891            const FlagReserved            = (1 << 15);
892            const FlagSingleInheritance   = (1 << 16);
893            const FlagMultipleInheritance = (2 << 16);
894            const FlagVirtualInheritance  = (3 << 16);
895            const FlagIntroducedVirtual   = (1 << 18);
896            const FlagBitField            = (1 << 19);
897            const FlagNoReturn            = (1 << 20);
898            // The bit at (1 << 21) is unused, but was `LLVMDIFlagMainSubprogram`.
899            const FlagTypePassByValue     = (1 << 22);
900            const FlagTypePassByReference = (1 << 23);
901            const FlagEnumClass           = (1 << 24);
902            const FlagThunk               = (1 << 25);
903            const FlagNonTrivial          = (1 << 26);
904            const FlagBigEndian           = (1 << 27);
905            const FlagLittleEndian        = (1 << 28);
906        }
907    }
908
909    // These values **must** match with LLVMRustDISPFlags!!
910    bitflags! {
911        #[repr(transparent)]
912        #[derive(Clone, Copy, Default)]
913        pub(crate) struct DISPFlags: u32 {
914            const SPFlagZero              = 0;
915            const SPFlagVirtual           = 1;
916            const SPFlagPureVirtual       = 2;
917            const SPFlagLocalToUnit       = (1 << 2);
918            const SPFlagDefinition        = (1 << 3);
919            const SPFlagOptimized         = (1 << 4);
920            const SPFlagMainSubprogram    = (1 << 5);
921        }
922    }
923
924    /// LLVMRustDebugEmissionKind
925    #[derive(Copy, Clone)]
926    #[repr(C)]
927    pub(crate) enum DebugEmissionKind {
928        NoDebug,
929        FullDebug,
930        LineTablesOnly,
931        DebugDirectivesOnly,
932    }
933
934    impl DebugEmissionKind {
935        pub(crate) fn from_generic(kind: rustc_session::config::DebugInfo) -> Self {
936            // We should be setting LLVM's emission kind to `LineTablesOnly` if
937            // we are compiling with "limited" debuginfo. However, some of the
938            // existing tools relied on slightly more debuginfo being generated than
939            // would be the case with `LineTablesOnly`, and we did not want to break
940            // these tools in a "drive-by fix", without a good idea or plan about
941            // what limited debuginfo should exactly look like. So for now we are
942            // instead adding a new debuginfo option "line-tables-only" so as to
943            // not break anything and to allow users to have 'limited' debug info.
944            //
945            // See https://github.com/rust-lang/rust/issues/60020 for details.
946            use rustc_session::config::DebugInfo;
947            match kind {
948                DebugInfo::None => DebugEmissionKind::NoDebug,
949                DebugInfo::LineDirectivesOnly => DebugEmissionKind::DebugDirectivesOnly,
950                DebugInfo::LineTablesOnly => DebugEmissionKind::LineTablesOnly,
951                DebugInfo::Limited | DebugInfo::Full => DebugEmissionKind::FullDebug,
952            }
953        }
954    }
955
956    /// LLVMRustDebugNameTableKind
957    #[derive(Clone, Copy)]
958    #[repr(C)]
959    pub(crate) enum DebugNameTableKind {
960        Default,
961        #[expect(dead_code)]
962        Gnu,
963        None,
964    }
965}
966
967// These values **must** match with LLVMRustAllocKindFlags
968bitflags! {
969    #[repr(transparent)]
970    #[derive(Default)]
971    pub(crate) struct AllocKindFlags : u64 {
972        const Unknown = 0;
973        const Alloc = 1;
974        const Realloc = 1 << 1;
975        const Free = 1 << 2;
976        const Uninitialized = 1 << 3;
977        const Zeroed = 1 << 4;
978        const Aligned = 1 << 5;
979    }
980}
981
982// These values **must** match with LLVMGEPNoWrapFlags
983bitflags! {
984    #[repr(transparent)]
985    #[derive(Default)]
986    pub struct GEPNoWrapFlags : c_uint {
987        const InBounds = 1 << 0;
988        const NUSW = 1 << 1;
989        const NUW = 1 << 2;
990    }
991}
992
993unsafe extern "C" {
994    pub(crate) type ModuleBuffer;
995}
996
997pub(crate) type SelfProfileBeforePassCallback =
998    unsafe extern "C" fn(*mut c_void, *const c_char, *const c_char);
999pub(crate) type SelfProfileAfterPassCallback = unsafe extern "C" fn(*mut c_void);
1000
1001pub(crate) type GetSymbolsCallback =
1002    unsafe extern "C" fn(*mut c_void, *const c_char) -> *mut c_void;
1003pub(crate) type GetSymbolsErrorCallback = unsafe extern "C" fn(*const c_char) -> *mut c_void;
1004
1005#[derive(Copy, Clone)]
1006#[repr(transparent)]
1007pub(crate) struct MetadataKindId(c_uint);
1008
1009impl From<MetadataType> for MetadataKindId {
1010    fn from(value: MetadataType) -> Self {
1011        Self(value as c_uint)
1012    }
1013}
1014
1015unsafe extern "C" {
1016    // Create and destroy contexts.
1017    pub(crate) fn LLVMContextDispose(C: &'static mut Context);
1018    pub(crate) fn LLVMGetMDKindIDInContext(
1019        C: &Context,
1020        Name: *const c_char,
1021        SLen: c_uint,
1022    ) -> MetadataKindId;
1023
1024    // Create modules.
1025    pub(crate) fn LLVMModuleCreateWithNameInContext(
1026        ModuleID: *const c_char,
1027        C: &Context,
1028    ) -> &Module;
1029    pub(crate) safe fn LLVMCloneModule(M: &Module) -> &Module;
1030
1031    /// Data layout. See Module::getDataLayout.
1032    pub(crate) fn LLVMGetDataLayoutStr(M: &Module) -> *const c_char;
1033    pub(crate) fn LLVMSetDataLayout(M: &Module, Triple: *const c_char);
1034
1035    /// Append inline assembly to a module. See `Module::appendModuleInlineAsm`.
1036    pub(crate) fn LLVMAppendModuleInlineAsm(
1037        M: &Module,
1038        Asm: *const c_uchar, // See "PTR_LEN_STR".
1039        Len: size_t,
1040    );
1041
1042    /// Create the specified uniqued inline asm string. See `InlineAsm::get()`.
1043    pub(crate) fn LLVMGetInlineAsm<'ll>(
1044        Ty: &'ll Type,
1045        AsmString: *const c_uchar, // See "PTR_LEN_STR".
1046        AsmStringSize: size_t,
1047        Constraints: *const c_uchar, // See "PTR_LEN_STR".
1048        ConstraintsSize: size_t,
1049        HasSideEffects: llvm::Bool,
1050        IsAlignStack: llvm::Bool,
1051        Dialect: AsmDialect,
1052        CanThrow: llvm::Bool,
1053    ) -> &'ll Value;
1054
1055    pub(crate) safe fn LLVMGetTypeKind(Ty: &Type) -> RawEnum<TypeKind>;
1056
1057    // Operations on integer types
1058    pub(crate) fn LLVMInt1TypeInContext(C: &Context) -> &Type;
1059    pub(crate) fn LLVMInt8TypeInContext(C: &Context) -> &Type;
1060    pub(crate) fn LLVMInt16TypeInContext(C: &Context) -> &Type;
1061    pub(crate) fn LLVMInt32TypeInContext(C: &Context) -> &Type;
1062    pub(crate) fn LLVMInt64TypeInContext(C: &Context) -> &Type;
1063    pub(crate) fn LLVMIntTypeInContext(C: &Context, NumBits: c_uint) -> &Type;
1064
1065    pub(crate) fn LLVMGetIntTypeWidth(IntegerTy: &Type) -> c_uint;
1066
1067    // Operations on real types
1068    pub(crate) fn LLVMHalfTypeInContext(C: &Context) -> &Type;
1069    pub(crate) fn LLVMFloatTypeInContext(C: &Context) -> &Type;
1070    pub(crate) fn LLVMDoubleTypeInContext(C: &Context) -> &Type;
1071    pub(crate) fn LLVMFP128TypeInContext(C: &Context) -> &Type;
1072
1073    // Operations on function types
1074    pub(crate) fn LLVMFunctionType<'a>(
1075        ReturnType: &'a Type,
1076        ParamTypes: *const &'a Type,
1077        ParamCount: c_uint,
1078        IsVarArg: Bool,
1079    ) -> &'a Type;
1080    pub(crate) fn LLVMCountParamTypes(FunctionTy: &Type) -> c_uint;
1081    pub(crate) fn LLVMGetParamTypes<'a>(FunctionTy: &'a Type, Dest: *mut &'a Type);
1082
1083    // Operations on struct types
1084    pub(crate) fn LLVMStructTypeInContext<'a>(
1085        C: &'a Context,
1086        ElementTypes: *const &'a Type,
1087        ElementCount: c_uint,
1088        Packed: Bool,
1089    ) -> &'a Type;
1090
1091    // Operations on array, pointer, and vector types (sequence types)
1092    pub(crate) fn LLVMPointerTypeInContext(C: &Context, AddressSpace: c_uint) -> &Type;
1093    pub(crate) fn LLVMVectorType(ElementType: &Type, ElementCount: c_uint) -> &Type;
1094
1095    pub(crate) fn LLVMGetElementType(Ty: &Type) -> &Type;
1096    pub(crate) fn LLVMGetVectorSize(VectorTy: &Type) -> c_uint;
1097
1098    // Operations on other types
1099    pub(crate) fn LLVMVoidTypeInContext(C: &Context) -> &Type;
1100
1101    // Operations on all values
1102    pub(crate) fn LLVMTypeOf(Val: &Value) -> &Type;
1103    pub(crate) fn LLVMGetValueName2(Val: &Value, Length: *mut size_t) -> *const c_char;
1104    pub(crate) fn LLVMSetValueName2(Val: &Value, Name: *const c_char, NameLen: size_t);
1105    pub(crate) fn LLVMReplaceAllUsesWith<'a>(OldVal: &'a Value, NewVal: &'a Value);
1106    pub(crate) safe fn LLVMSetMetadata<'a>(Val: &'a Value, KindID: MetadataKindId, Node: &'a Value);
1107    pub(crate) fn LLVMGlobalSetMetadata<'a>(Val: &'a Value, KindID: c_uint, Metadata: &'a Metadata);
1108    pub(crate) safe fn LLVMValueAsMetadata(Node: &Value) -> &Metadata;
1109
1110    // Operations on constants of any type
1111    pub(crate) fn LLVMConstNull(Ty: &Type) -> &Value;
1112    pub(crate) fn LLVMGetUndef(Ty: &Type) -> &Value;
1113    pub(crate) fn LLVMGetPoison(Ty: &Type) -> &Value;
1114
1115    // Operations on metadata
1116    pub(crate) fn LLVMMDStringInContext2(
1117        C: &Context,
1118        Str: *const c_char,
1119        SLen: size_t,
1120    ) -> &Metadata;
1121    pub(crate) fn LLVMMDNodeInContext2<'a>(
1122        C: &'a Context,
1123        Vals: *const &'a Metadata,
1124        Count: size_t,
1125    ) -> &'a Metadata;
1126    pub(crate) fn LLVMAddNamedMetadataOperand<'a>(
1127        M: &'a Module,
1128        Name: *const c_char,
1129        Val: &'a Value,
1130    );
1131
1132    // Operations on scalar constants
1133    pub(crate) fn LLVMConstInt(IntTy: &Type, N: c_ulonglong, SignExtend: Bool) -> &Value;
1134    pub(crate) fn LLVMConstIntOfArbitraryPrecision(
1135        IntTy: &Type,
1136        Wn: c_uint,
1137        Ws: *const u64,
1138    ) -> &Value;
1139    pub(crate) fn LLVMConstReal(RealTy: &Type, N: f64) -> &Value;
1140
1141    // Operations on composite constants
1142    pub(crate) fn LLVMConstArray2<'a>(
1143        ElementTy: &'a Type,
1144        ConstantVals: *const &'a Value,
1145        Length: u64,
1146    ) -> &'a Value;
1147    pub(crate) fn LLVMArrayType2(ElementType: &Type, ElementCount: u64) -> &Type;
1148    pub(crate) fn LLVMConstStringInContext2(
1149        C: &Context,
1150        Str: *const c_char,
1151        Length: size_t,
1152        DontNullTerminate: Bool,
1153    ) -> &Value;
1154    pub(crate) fn LLVMConstStructInContext<'a>(
1155        C: &'a Context,
1156        ConstantVals: *const &'a Value,
1157        Count: c_uint,
1158        Packed: Bool,
1159    ) -> &'a Value;
1160    pub(crate) fn LLVMConstNamedStruct<'a>(
1161        StructTy: &'a Type,
1162        ConstantVals: *const &'a Value,
1163        Count: c_uint,
1164    ) -> &'a Value;
1165    pub(crate) fn LLVMConstVector(ScalarConstantVals: *const &Value, Size: c_uint) -> &Value;
1166
1167    // Constant expressions
1168    pub(crate) fn LLVMConstInBoundsGEP2<'a>(
1169        ty: &'a Type,
1170        ConstantVal: &'a Value,
1171        ConstantIndices: *const &'a Value,
1172        NumIndices: c_uint,
1173    ) -> &'a Value;
1174    pub(crate) fn LLVMConstPtrToInt<'a>(ConstantVal: &'a Value, ToType: &'a Type) -> &'a Value;
1175    pub(crate) fn LLVMConstIntToPtr<'a>(ConstantVal: &'a Value, ToType: &'a Type) -> &'a Value;
1176    pub(crate) fn LLVMConstBitCast<'a>(ConstantVal: &'a Value, ToType: &'a Type) -> &'a Value;
1177    pub(crate) fn LLVMConstPointerCast<'a>(ConstantVal: &'a Value, ToType: &'a Type) -> &'a Value;
1178    pub(crate) fn LLVMGetAggregateElement(ConstantVal: &Value, Idx: c_uint) -> Option<&Value>;
1179    pub(crate) fn LLVMGetConstOpcode(ConstantVal: &Value) -> Opcode;
1180    pub(crate) fn LLVMIsAConstantExpr(Val: &Value) -> Option<&Value>;
1181
1182    // Operations on global variables, functions, and aliases (globals)
1183    pub(crate) fn LLVMIsDeclaration(Global: &Value) -> Bool;
1184    pub(crate) fn LLVMGetLinkage(Global: &Value) -> RawEnum<Linkage>;
1185    pub(crate) fn LLVMSetLinkage(Global: &Value, RustLinkage: Linkage);
1186    pub(crate) fn LLVMSetSection(Global: &Value, Section: *const c_char);
1187    pub(crate) fn LLVMGetVisibility(Global: &Value) -> RawEnum<Visibility>;
1188    pub(crate) fn LLVMSetVisibility(Global: &Value, Viz: Visibility);
1189    pub(crate) fn LLVMGetAlignment(Global: &Value) -> c_uint;
1190    pub(crate) fn LLVMSetAlignment(Global: &Value, Bytes: c_uint);
1191    pub(crate) fn LLVMSetDLLStorageClass(V: &Value, C: DLLStorageClass);
1192    pub(crate) fn LLVMGlobalGetValueType(Global: &Value) -> &Type;
1193
1194    // Operations on global variables
1195    pub(crate) safe fn LLVMIsAGlobalVariable(GlobalVar: &Value) -> Option<&Value>;
1196    pub(crate) fn LLVMAddGlobal<'a>(M: &'a Module, Ty: &'a Type, Name: *const c_char) -> &'a Value;
1197    pub(crate) fn LLVMGetNamedGlobal(M: &Module, Name: *const c_char) -> Option<&Value>;
1198    pub(crate) fn LLVMGetFirstGlobal(M: &Module) -> Option<&Value>;
1199    pub(crate) fn LLVMGetNextGlobal(GlobalVar: &Value) -> Option<&Value>;
1200    pub(crate) fn LLVMDeleteGlobal(GlobalVar: &Value);
1201    pub(crate) safe fn LLVMGetInitializer(GlobalVar: &Value) -> Option<&Value>;
1202    pub(crate) fn LLVMSetInitializer<'a>(GlobalVar: &'a Value, ConstantVal: &'a Value);
1203    pub(crate) safe fn LLVMIsThreadLocal(GlobalVar: &Value) -> Bool;
1204    pub(crate) fn LLVMSetThreadLocalMode(GlobalVar: &Value, Mode: ThreadLocalMode);
1205    pub(crate) safe fn LLVMIsGlobalConstant(GlobalVar: &Value) -> Bool;
1206    pub(crate) safe fn LLVMSetGlobalConstant(GlobalVar: &Value, IsConstant: Bool);
1207    pub(crate) safe fn LLVMSetTailCall(CallInst: &Value, IsTailCall: Bool);
1208    pub(crate) safe fn LLVMSetTailCallKind(CallInst: &Value, kind: TailCallKind);
1209
1210    // Operations on attributes
1211    pub(crate) fn LLVMCreateStringAttribute(
1212        C: &Context,
1213        Name: *const c_char,
1214        NameLen: c_uint,
1215        Value: *const c_char,
1216        ValueLen: c_uint,
1217    ) -> &Attribute;
1218
1219    // Operations on functions
1220    pub(crate) fn LLVMSetFunctionCallConv(Fn: &Value, CC: c_uint);
1221
1222    // Operations about llvm intrinsics
1223    pub(crate) fn LLVMLookupIntrinsicID(Name: *const c_char, NameLen: size_t) -> c_uint;
1224    pub(crate) fn LLVMGetIntrinsicDeclaration<'a>(
1225        Mod: &'a Module,
1226        ID: NonZero<c_uint>,
1227        ParamTypes: *const &'a Type,
1228        ParamCount: size_t,
1229    ) -> &'a Value;
1230
1231    // Operations on parameters
1232    pub(crate) fn LLVMIsAArgument(Val: &Value) -> Option<&Value>;
1233    pub(crate) safe fn LLVMCountParams(Fn: &Value) -> c_uint;
1234    pub(crate) fn LLVMGetParam(Fn: &Value, Index: c_uint) -> &Value;
1235
1236    // Operations on basic blocks
1237    pub(crate) fn LLVMGetBasicBlockParent(BB: &BasicBlock) -> &Value;
1238    pub(crate) fn LLVMAppendBasicBlockInContext<'a>(
1239        C: &'a Context,
1240        Fn: &'a Value,
1241        Name: *const c_char,
1242    ) -> &'a BasicBlock;
1243
1244    // Operations on instructions
1245    pub(crate) fn LLVMGetInstructionParent(Inst: &Value) -> &BasicBlock;
1246    pub(crate) fn LLVMGetCalledValue(CallInst: &Value) -> Option<&Value>;
1247    pub(crate) fn LLVMIsAInstruction(Val: &Value) -> Option<&Value>;
1248    pub(crate) fn LLVMGetFirstBasicBlock(Fn: &Value) -> &BasicBlock;
1249    pub(crate) fn LLVMGetOperand(Val: &Value, Index: c_uint) -> Option<&Value>;
1250
1251    // Operations on call sites
1252    pub(crate) fn LLVMSetInstructionCallConv(Instr: &Value, CC: c_uint);
1253
1254    // Operations on load/store instructions (only)
1255    pub(crate) fn LLVMSetVolatile(MemoryAccessInst: &Value, volatile: Bool);
1256
1257    // Operations on phi nodes
1258    pub(crate) fn LLVMAddIncoming<'a>(
1259        PhiNode: &'a Value,
1260        IncomingValues: *const &'a Value,
1261        IncomingBlocks: *const &'a BasicBlock,
1262        Count: c_uint,
1263    );
1264
1265    // Instruction builders
1266    pub(crate) fn LLVMCreateBuilderInContext(C: &Context) -> &mut Builder<'_>;
1267    pub(crate) fn LLVMPositionBuilderAtEnd<'a>(Builder: &Builder<'a>, Block: &'a BasicBlock);
1268    pub(crate) fn LLVMGetInsertBlock<'a>(Builder: &Builder<'a>) -> &'a BasicBlock;
1269    pub(crate) fn LLVMDisposeBuilder<'a>(Builder: &'a mut Builder<'a>);
1270
1271    // Metadata
1272    pub(crate) fn LLVMSetCurrentDebugLocation2<'a>(Builder: &Builder<'a>, Loc: *const Metadata);
1273    pub(crate) fn LLVMGetCurrentDebugLocation2<'a>(Builder: &Builder<'a>) -> Option<&'a Metadata>;
1274
1275    // Terminators
1276    pub(crate) safe fn LLVMBuildRetVoid<'a>(B: &Builder<'a>) -> &'a Value;
1277    pub(crate) fn LLVMBuildRet<'a>(B: &Builder<'a>, V: &'a Value) -> &'a Value;
1278    pub(crate) fn LLVMBuildBr<'a>(B: &Builder<'a>, Dest: &'a BasicBlock) -> &'a Value;
1279    pub(crate) fn LLVMBuildCondBr<'a>(
1280        B: &Builder<'a>,
1281        If: &'a Value,
1282        Then: &'a BasicBlock,
1283        Else: &'a BasicBlock,
1284    ) -> &'a Value;
1285    pub(crate) fn LLVMBuildSwitch<'a>(
1286        B: &Builder<'a>,
1287        V: &'a Value,
1288        Else: &'a BasicBlock,
1289        NumCases: c_uint,
1290    ) -> &'a Value;
1291    pub(crate) fn LLVMBuildLandingPad<'a>(
1292        B: &Builder<'a>,
1293        Ty: &'a Type,
1294        PersFn: Option<&'a Value>,
1295        NumClauses: c_uint,
1296        Name: *const c_char,
1297    ) -> &'a Value;
1298    pub(crate) fn LLVMBuildResume<'a>(B: &Builder<'a>, Exn: &'a Value) -> &'a Value;
1299    pub(crate) fn LLVMBuildUnreachable<'a>(B: &Builder<'a>) -> &'a Value;
1300
1301    pub(crate) fn LLVMBuildCleanupPad<'a>(
1302        B: &Builder<'a>,
1303        ParentPad: Option<&'a Value>,
1304        Args: *const &'a Value,
1305        NumArgs: c_uint,
1306        Name: *const c_char,
1307    ) -> Option<&'a Value>;
1308    pub(crate) fn LLVMBuildCleanupRet<'a>(
1309        B: &Builder<'a>,
1310        CleanupPad: &'a Value,
1311        BB: Option<&'a BasicBlock>,
1312    ) -> Option<&'a Value>;
1313    pub(crate) fn LLVMBuildCatchPad<'a>(
1314        B: &Builder<'a>,
1315        ParentPad: &'a Value,
1316        Args: *const &'a Value,
1317        NumArgs: c_uint,
1318        Name: *const c_char,
1319    ) -> Option<&'a Value>;
1320    pub(crate) fn LLVMBuildCatchRet<'a>(
1321        B: &Builder<'a>,
1322        CatchPad: &'a Value,
1323        BB: &'a BasicBlock,
1324    ) -> Option<&'a Value>;
1325    pub(crate) fn LLVMBuildCatchSwitch<'a>(
1326        Builder: &Builder<'a>,
1327        ParentPad: Option<&'a Value>,
1328        UnwindBB: Option<&'a BasicBlock>,
1329        NumHandlers: c_uint,
1330        Name: *const c_char,
1331    ) -> Option<&'a Value>;
1332    pub(crate) fn LLVMAddHandler<'a>(CatchSwitch: &'a Value, Dest: &'a BasicBlock);
1333    pub(crate) fn LLVMSetPersonalityFn<'a>(Func: &'a Value, Pers: &'a Value);
1334
1335    // Add a case to the switch instruction
1336    pub(crate) fn LLVMAddCase<'a>(Switch: &'a Value, OnVal: &'a Value, Dest: &'a BasicBlock);
1337
1338    // Add a clause to the landing pad instruction
1339    pub(crate) fn LLVMAddClause<'a>(LandingPad: &'a Value, ClauseVal: &'a Value);
1340
1341    // Set the cleanup on a landing pad instruction
1342    pub(crate) fn LLVMSetCleanup(LandingPad: &Value, Val: Bool);
1343
1344    // Arithmetic
1345    pub(crate) fn LLVMBuildAdd<'a>(
1346        B: &Builder<'a>,
1347        LHS: &'a Value,
1348        RHS: &'a Value,
1349        Name: *const c_char,
1350    ) -> &'a Value;
1351    pub(crate) fn LLVMBuildFAdd<'a>(
1352        B: &Builder<'a>,
1353        LHS: &'a Value,
1354        RHS: &'a Value,
1355        Name: *const c_char,
1356    ) -> &'a Value;
1357    pub(crate) fn LLVMBuildSub<'a>(
1358        B: &Builder<'a>,
1359        LHS: &'a Value,
1360        RHS: &'a Value,
1361        Name: *const c_char,
1362    ) -> &'a Value;
1363    pub(crate) fn LLVMBuildFSub<'a>(
1364        B: &Builder<'a>,
1365        LHS: &'a Value,
1366        RHS: &'a Value,
1367        Name: *const c_char,
1368    ) -> &'a Value;
1369    pub(crate) fn LLVMBuildMul<'a>(
1370        B: &Builder<'a>,
1371        LHS: &'a Value,
1372        RHS: &'a Value,
1373        Name: *const c_char,
1374    ) -> &'a Value;
1375    pub(crate) fn LLVMBuildFMul<'a>(
1376        B: &Builder<'a>,
1377        LHS: &'a Value,
1378        RHS: &'a Value,
1379        Name: *const c_char,
1380    ) -> &'a Value;
1381    pub(crate) fn LLVMBuildUDiv<'a>(
1382        B: &Builder<'a>,
1383        LHS: &'a Value,
1384        RHS: &'a Value,
1385        Name: *const c_char,
1386    ) -> &'a Value;
1387    pub(crate) fn LLVMBuildExactUDiv<'a>(
1388        B: &Builder<'a>,
1389        LHS: &'a Value,
1390        RHS: &'a Value,
1391        Name: *const c_char,
1392    ) -> &'a Value;
1393    pub(crate) fn LLVMBuildSDiv<'a>(
1394        B: &Builder<'a>,
1395        LHS: &'a Value,
1396        RHS: &'a Value,
1397        Name: *const c_char,
1398    ) -> &'a Value;
1399    pub(crate) fn LLVMBuildExactSDiv<'a>(
1400        B: &Builder<'a>,
1401        LHS: &'a Value,
1402        RHS: &'a Value,
1403        Name: *const c_char,
1404    ) -> &'a Value;
1405    pub(crate) fn LLVMBuildFDiv<'a>(
1406        B: &Builder<'a>,
1407        LHS: &'a Value,
1408        RHS: &'a Value,
1409        Name: *const c_char,
1410    ) -> &'a Value;
1411    pub(crate) fn LLVMBuildURem<'a>(
1412        B: &Builder<'a>,
1413        LHS: &'a Value,
1414        RHS: &'a Value,
1415        Name: *const c_char,
1416    ) -> &'a Value;
1417    pub(crate) fn LLVMBuildSRem<'a>(
1418        B: &Builder<'a>,
1419        LHS: &'a Value,
1420        RHS: &'a Value,
1421        Name: *const c_char,
1422    ) -> &'a Value;
1423    pub(crate) fn LLVMBuildFRem<'a>(
1424        B: &Builder<'a>,
1425        LHS: &'a Value,
1426        RHS: &'a Value,
1427        Name: *const c_char,
1428    ) -> &'a Value;
1429    pub(crate) fn LLVMBuildShl<'a>(
1430        B: &Builder<'a>,
1431        LHS: &'a Value,
1432        RHS: &'a Value,
1433        Name: *const c_char,
1434    ) -> &'a Value;
1435    pub(crate) fn LLVMBuildLShr<'a>(
1436        B: &Builder<'a>,
1437        LHS: &'a Value,
1438        RHS: &'a Value,
1439        Name: *const c_char,
1440    ) -> &'a Value;
1441    pub(crate) fn LLVMBuildAShr<'a>(
1442        B: &Builder<'a>,
1443        LHS: &'a Value,
1444        RHS: &'a Value,
1445        Name: *const c_char,
1446    ) -> &'a Value;
1447    pub(crate) fn LLVMBuildNSWAdd<'a>(
1448        B: &Builder<'a>,
1449        LHS: &'a Value,
1450        RHS: &'a Value,
1451        Name: *const c_char,
1452    ) -> &'a Value;
1453    pub(crate) fn LLVMBuildNUWAdd<'a>(
1454        B: &Builder<'a>,
1455        LHS: &'a Value,
1456        RHS: &'a Value,
1457        Name: *const c_char,
1458    ) -> &'a Value;
1459    pub(crate) fn LLVMBuildNSWSub<'a>(
1460        B: &Builder<'a>,
1461        LHS: &'a Value,
1462        RHS: &'a Value,
1463        Name: *const c_char,
1464    ) -> &'a Value;
1465    pub(crate) fn LLVMBuildNUWSub<'a>(
1466        B: &Builder<'a>,
1467        LHS: &'a Value,
1468        RHS: &'a Value,
1469        Name: *const c_char,
1470    ) -> &'a Value;
1471    pub(crate) fn LLVMBuildNSWMul<'a>(
1472        B: &Builder<'a>,
1473        LHS: &'a Value,
1474        RHS: &'a Value,
1475        Name: *const c_char,
1476    ) -> &'a Value;
1477    pub(crate) fn LLVMBuildNUWMul<'a>(
1478        B: &Builder<'a>,
1479        LHS: &'a Value,
1480        RHS: &'a Value,
1481        Name: *const c_char,
1482    ) -> &'a Value;
1483    pub(crate) fn LLVMBuildAnd<'a>(
1484        B: &Builder<'a>,
1485        LHS: &'a Value,
1486        RHS: &'a Value,
1487        Name: *const c_char,
1488    ) -> &'a Value;
1489    pub(crate) fn LLVMBuildOr<'a>(
1490        B: &Builder<'a>,
1491        LHS: &'a Value,
1492        RHS: &'a Value,
1493        Name: *const c_char,
1494    ) -> &'a Value;
1495    pub(crate) fn LLVMBuildXor<'a>(
1496        B: &Builder<'a>,
1497        LHS: &'a Value,
1498        RHS: &'a Value,
1499        Name: *const c_char,
1500    ) -> &'a Value;
1501    pub(crate) fn LLVMBuildNeg<'a>(B: &Builder<'a>, V: &'a Value, Name: *const c_char)
1502    -> &'a Value;
1503    pub(crate) fn LLVMBuildFNeg<'a>(
1504        B: &Builder<'a>,
1505        V: &'a Value,
1506        Name: *const c_char,
1507    ) -> &'a Value;
1508    pub(crate) fn LLVMBuildNot<'a>(B: &Builder<'a>, V: &'a Value, Name: *const c_char)
1509    -> &'a Value;
1510
1511    // Extra flags on arithmetic
1512    pub(crate) fn LLVMSetIsDisjoint(Instr: &Value, IsDisjoint: Bool);
1513    pub(crate) fn LLVMSetNUW(ArithInst: &Value, HasNUW: Bool);
1514    pub(crate) fn LLVMSetNSW(ArithInst: &Value, HasNSW: Bool);
1515
1516    // Memory
1517    pub(crate) fn LLVMBuildAlloca<'a>(
1518        B: &Builder<'a>,
1519        Ty: &'a Type,
1520        Name: *const c_char,
1521    ) -> &'a Value;
1522    pub(crate) fn LLVMBuildLoad2<'a>(
1523        B: &Builder<'a>,
1524        Ty: &'a Type,
1525        PointerVal: &'a Value,
1526        Name: *const c_char,
1527    ) -> &'a Value;
1528
1529    pub(crate) fn LLVMBuildStore<'a>(B: &Builder<'a>, Val: &'a Value, Ptr: &'a Value) -> &'a Value;
1530
1531    pub(crate) fn LLVMBuildGEPWithNoWrapFlags<'a>(
1532        B: &Builder<'a>,
1533        Ty: &'a Type,
1534        Pointer: &'a Value,
1535        Indices: *const &'a Value,
1536        NumIndices: c_uint,
1537        Name: *const c_char,
1538        Flags: GEPNoWrapFlags,
1539    ) -> &'a Value;
1540
1541    // Casts
1542    pub(crate) fn LLVMBuildTrunc<'a>(
1543        B: &Builder<'a>,
1544        Val: &'a Value,
1545        DestTy: &'a Type,
1546        Name: *const c_char,
1547    ) -> &'a Value;
1548    pub(crate) fn LLVMBuildZExt<'a>(
1549        B: &Builder<'a>,
1550        Val: &'a Value,
1551        DestTy: &'a Type,
1552        Name: *const c_char,
1553    ) -> &'a Value;
1554    pub(crate) fn LLVMBuildSExt<'a>(
1555        B: &Builder<'a>,
1556        Val: &'a Value,
1557        DestTy: &'a Type,
1558        Name: *const c_char,
1559    ) -> &'a Value;
1560    pub(crate) fn LLVMBuildFPToUI<'a>(
1561        B: &Builder<'a>,
1562        Val: &'a Value,
1563        DestTy: &'a Type,
1564        Name: *const c_char,
1565    ) -> &'a Value;
1566    pub(crate) fn LLVMBuildFPToSI<'a>(
1567        B: &Builder<'a>,
1568        Val: &'a Value,
1569        DestTy: &'a Type,
1570        Name: *const c_char,
1571    ) -> &'a Value;
1572    pub(crate) fn LLVMBuildUIToFP<'a>(
1573        B: &Builder<'a>,
1574        Val: &'a Value,
1575        DestTy: &'a Type,
1576        Name: *const c_char,
1577    ) -> &'a Value;
1578    pub(crate) fn LLVMBuildSIToFP<'a>(
1579        B: &Builder<'a>,
1580        Val: &'a Value,
1581        DestTy: &'a Type,
1582        Name: *const c_char,
1583    ) -> &'a Value;
1584    pub(crate) fn LLVMBuildFPTrunc<'a>(
1585        B: &Builder<'a>,
1586        Val: &'a Value,
1587        DestTy: &'a Type,
1588        Name: *const c_char,
1589    ) -> &'a Value;
1590    pub(crate) fn LLVMBuildFPExt<'a>(
1591        B: &Builder<'a>,
1592        Val: &'a Value,
1593        DestTy: &'a Type,
1594        Name: *const c_char,
1595    ) -> &'a Value;
1596    pub(crate) fn LLVMBuildPtrToInt<'a>(
1597        B: &Builder<'a>,
1598        Val: &'a Value,
1599        DestTy: &'a Type,
1600        Name: *const c_char,
1601    ) -> &'a Value;
1602    pub(crate) fn LLVMBuildIntToPtr<'a>(
1603        B: &Builder<'a>,
1604        Val: &'a Value,
1605        DestTy: &'a Type,
1606        Name: *const c_char,
1607    ) -> &'a Value;
1608    pub(crate) fn LLVMBuildBitCast<'a>(
1609        B: &Builder<'a>,
1610        Val: &'a Value,
1611        DestTy: &'a Type,
1612        Name: *const c_char,
1613    ) -> &'a Value;
1614    pub(crate) fn LLVMBuildPointerCast<'a>(
1615        B: &Builder<'a>,
1616        Val: &'a Value,
1617        DestTy: &'a Type,
1618        Name: *const c_char,
1619    ) -> &'a Value;
1620    pub(crate) fn LLVMBuildIntCast2<'a>(
1621        B: &Builder<'a>,
1622        Val: &'a Value,
1623        DestTy: &'a Type,
1624        IsSigned: Bool,
1625        Name: *const c_char,
1626    ) -> &'a Value;
1627
1628    // Comparisons
1629    pub(crate) fn LLVMBuildICmp<'a>(
1630        B: &Builder<'a>,
1631        Op: c_uint,
1632        LHS: &'a Value,
1633        RHS: &'a Value,
1634        Name: *const c_char,
1635    ) -> &'a Value;
1636    pub(crate) fn LLVMBuildFCmp<'a>(
1637        B: &Builder<'a>,
1638        Op: c_uint,
1639        LHS: &'a Value,
1640        RHS: &'a Value,
1641        Name: *const c_char,
1642    ) -> &'a Value;
1643
1644    // Miscellaneous instructions
1645    pub(crate) fn LLVMBuildPhi<'a>(B: &Builder<'a>, Ty: &'a Type, Name: *const c_char)
1646    -> &'a Value;
1647    pub(crate) fn LLVMBuildSelect<'a>(
1648        B: &Builder<'a>,
1649        If: &'a Value,
1650        Then: &'a Value,
1651        Else: &'a Value,
1652        Name: *const c_char,
1653    ) -> &'a Value;
1654    pub(crate) fn LLVMBuildVAArg<'a>(
1655        B: &Builder<'a>,
1656        list: &'a Value,
1657        Ty: &'a Type,
1658        Name: *const c_char,
1659    ) -> &'a Value;
1660    pub(crate) fn LLVMBuildExtractElement<'a>(
1661        B: &Builder<'a>,
1662        VecVal: &'a Value,
1663        Index: &'a Value,
1664        Name: *const c_char,
1665    ) -> &'a Value;
1666    pub(crate) fn LLVMBuildInsertElement<'a>(
1667        B: &Builder<'a>,
1668        VecVal: &'a Value,
1669        EltVal: &'a Value,
1670        Index: &'a Value,
1671        Name: *const c_char,
1672    ) -> &'a Value;
1673    pub(crate) fn LLVMBuildShuffleVector<'a>(
1674        B: &Builder<'a>,
1675        V1: &'a Value,
1676        V2: &'a Value,
1677        Mask: &'a Value,
1678        Name: *const c_char,
1679    ) -> &'a Value;
1680    pub(crate) fn LLVMBuildExtractValue<'a>(
1681        B: &Builder<'a>,
1682        AggVal: &'a Value,
1683        Index: c_uint,
1684        Name: *const c_char,
1685    ) -> &'a Value;
1686    pub(crate) fn LLVMBuildInsertValue<'a>(
1687        B: &Builder<'a>,
1688        AggVal: &'a Value,
1689        EltVal: &'a Value,
1690        Index: c_uint,
1691        Name: *const c_char,
1692    ) -> &'a Value;
1693
1694    // Atomic Operations
1695    pub(crate) fn LLVMBuildAtomicCmpXchg<'a>(
1696        B: &Builder<'a>,
1697        LHS: &'a Value,
1698        CMP: &'a Value,
1699        RHS: &'a Value,
1700        Order: AtomicOrdering,
1701        FailureOrder: AtomicOrdering,
1702        SingleThreaded: Bool,
1703    ) -> &'a Value;
1704
1705    pub(crate) fn LLVMSetWeak(CmpXchgInst: &Value, IsWeak: Bool);
1706
1707    pub(crate) fn LLVMBuildAtomicRMW<'a>(
1708        B: &Builder<'a>,
1709        Op: AtomicRmwBinOp,
1710        LHS: &'a Value,
1711        RHS: &'a Value,
1712        Order: AtomicOrdering,
1713        SingleThreaded: Bool,
1714    ) -> &'a Value;
1715
1716    pub(crate) fn LLVMBuildFence<'a>(
1717        B: &Builder<'a>,
1718        Order: AtomicOrdering,
1719        SingleThreaded: Bool,
1720        Name: *const c_char,
1721    ) -> &'a Value;
1722
1723    /// Writes a module to the specified path. Returns 0 on success.
1724    pub(crate) fn LLVMWriteBitcodeToFile(M: &Module, Path: *const c_char) -> c_int;
1725
1726    /// Creates a legacy pass manager -- only used for final codegen.
1727    pub(crate) fn LLVMCreatePassManager<'a>() -> &'a mut PassManager<'a>;
1728
1729    pub(crate) fn LLVMAddAnalysisPasses<'a>(T: &'a TargetMachine, PM: &PassManager<'a>);
1730
1731    pub(crate) fn LLVMGetHostCPUFeatures() -> *mut c_char;
1732
1733    pub(crate) fn LLVMDisposeMessage(message: *mut c_char);
1734
1735    pub(crate) fn LLVMIsMultithreaded() -> Bool;
1736
1737    pub(crate) fn LLVMStructCreateNamed(C: &Context, Name: *const c_char) -> &Type;
1738
1739    pub(crate) fn LLVMStructSetBody<'a>(
1740        StructTy: &'a Type,
1741        ElementTypes: *const &'a Type,
1742        ElementCount: c_uint,
1743        Packed: Bool,
1744    );
1745
1746    pub(crate) safe fn LLVMMetadataAsValue<'a>(C: &'a Context, MD: &'a Metadata) -> &'a Value;
1747
1748    pub(crate) safe fn LLVMSetUnnamedAddress(Global: &Value, UnnamedAddr: UnnamedAddr);
1749
1750    pub(crate) fn LLVMIsAConstantInt(value_ref: &Value) -> Option<&ConstantInt>;
1751
1752    pub(crate) fn LLVMGetOrInsertComdat(M: &Module, Name: *const c_char) -> &Comdat;
1753    pub(crate) fn LLVMSetComdat(V: &Value, C: &Comdat);
1754
1755    pub(crate) fn LLVMCreateOperandBundle(
1756        Tag: *const c_char,
1757        TagLen: size_t,
1758        Args: *const &'_ Value,
1759        NumArgs: c_uint,
1760    ) -> *mut OperandBundle<'_>;
1761    pub(crate) fn LLVMDisposeOperandBundle(Bundle: ptr::NonNull<OperandBundle<'_>>);
1762
1763    pub(crate) fn LLVMBuildCallWithOperandBundles<'a>(
1764        B: &Builder<'a>,
1765        Ty: &'a Type,
1766        Fn: &'a Value,
1767        Args: *const &'a Value,
1768        NumArgs: c_uint,
1769        Bundles: *const &OperandBundle<'a>,
1770        NumBundles: c_uint,
1771        Name: *const c_char,
1772    ) -> &'a Value;
1773    pub(crate) fn LLVMBuildInvokeWithOperandBundles<'a>(
1774        B: &Builder<'a>,
1775        Ty: &'a Type,
1776        Fn: &'a Value,
1777        Args: *const &'a Value,
1778        NumArgs: c_uint,
1779        Then: &'a BasicBlock,
1780        Catch: &'a BasicBlock,
1781        Bundles: *const &OperandBundle<'a>,
1782        NumBundles: c_uint,
1783        Name: *const c_char,
1784    ) -> &'a Value;
1785    pub(crate) fn LLVMBuildCallBr<'a>(
1786        B: &Builder<'a>,
1787        Ty: &'a Type,
1788        Fn: &'a Value,
1789        DefaultDest: &'a BasicBlock,
1790        IndirectDests: *const &'a BasicBlock,
1791        NumIndirectDests: c_uint,
1792        Args: *const &'a Value,
1793        NumArgs: c_uint,
1794        Bundles: *const &OperandBundle<'a>,
1795        NumBundles: c_uint,
1796        Name: *const c_char,
1797    ) -> &'a Value;
1798}
1799
1800// FFI bindings for `DIBuilder` functions in the LLVM-C API.
1801// Try to keep these in the same order as in `llvm/include/llvm-c/DebugInfo.h`.
1802//
1803// FIXME(#134001): Audit all `Option` parameters, especially in lists, to check
1804// that they really are nullable on the C/C++ side. LLVM doesn't appear to
1805// actually document which ones are nullable.
1806unsafe extern "C" {
1807    pub(crate) fn LLVMCreateDIBuilder<'ll>(M: &'ll Module) -> *mut DIBuilder<'ll>;
1808    pub(crate) fn LLVMDisposeDIBuilder<'ll>(Builder: ptr::NonNull<DIBuilder<'ll>>);
1809
1810    pub(crate) fn LLVMDIBuilderFinalize<'ll>(Builder: &DIBuilder<'ll>);
1811
1812    pub(crate) fn LLVMDIBuilderCreateNameSpace<'ll>(
1813        Builder: &DIBuilder<'ll>,
1814        ParentScope: Option<&'ll Metadata>,
1815        Name: *const c_uchar, // See "PTR_LEN_STR".
1816        NameLen: size_t,
1817        ExportSymbols: llvm::Bool,
1818    ) -> &'ll Metadata;
1819
1820    pub(crate) fn LLVMDIBuilderCreateLexicalBlock<'ll>(
1821        Builder: &DIBuilder<'ll>,
1822        Scope: &'ll Metadata,
1823        File: &'ll Metadata,
1824        Line: c_uint,
1825        Column: c_uint,
1826    ) -> &'ll Metadata;
1827
1828    pub(crate) fn LLVMDIBuilderCreateLexicalBlockFile<'ll>(
1829        Builder: &DIBuilder<'ll>,
1830        Scope: &'ll Metadata,
1831        File: &'ll Metadata,
1832        Discriminator: c_uint, // (optional "DWARF path discriminator"; default is 0)
1833    ) -> &'ll Metadata;
1834
1835    pub(crate) fn LLVMDIBuilderCreateDebugLocation<'ll>(
1836        Ctx: &'ll Context,
1837        Line: c_uint,
1838        Column: c_uint,
1839        Scope: &'ll Metadata,
1840        InlinedAt: Option<&'ll Metadata>,
1841    ) -> &'ll Metadata;
1842}
1843
1844#[link(name = "llvm-wrapper", kind = "static")]
1845unsafe extern "C" {
1846    pub(crate) fn LLVMRustInstallErrorHandlers();
1847    pub(crate) fn LLVMRustDisableSystemDialogsOnCrash();
1848
1849    // Create and destroy contexts.
1850    pub(crate) fn LLVMRustContextCreate(shouldDiscardNames: bool) -> &'static mut Context;
1851
1852    // Operations on all values
1853    pub(crate) fn LLVMRustGlobalAddMetadata<'a>(
1854        Val: &'a Value,
1855        KindID: c_uint,
1856        Metadata: &'a Metadata,
1857    );
1858    pub(crate) fn LLVMRustIsNonGVFunctionPointerTy(Val: &Value) -> bool;
1859
1860    // Operations on scalar constants
1861    pub(crate) fn LLVMRustConstIntGetZExtValue(ConstantVal: &ConstantInt, Value: &mut u64) -> bool;
1862    pub(crate) fn LLVMRustConstInt128Get(
1863        ConstantVal: &ConstantInt,
1864        SExt: bool,
1865        high: &mut u64,
1866        low: &mut u64,
1867    ) -> bool;
1868
1869    // Operations on global variables, functions, and aliases (globals)
1870    pub(crate) fn LLVMRustSetDSOLocal(Global: &Value, is_dso_local: bool);
1871
1872    // Operations on global variables
1873    pub(crate) fn LLVMRustGetOrInsertGlobal<'a>(
1874        M: &'a Module,
1875        Name: *const c_char,
1876        NameLen: size_t,
1877        T: &'a Type,
1878    ) -> &'a Value;
1879    pub(crate) fn LLVMRustInsertPrivateGlobal<'a>(M: &'a Module, T: &'a Type) -> &'a Value;
1880    pub(crate) fn LLVMRustGetNamedValue(
1881        M: &Module,
1882        Name: *const c_char,
1883        NameLen: size_t,
1884    ) -> Option<&Value>;
1885
1886    // Operations on attributes
1887    pub(crate) fn LLVMRustCreateAttrNoValue(C: &Context, attr: AttributeKind) -> &Attribute;
1888    pub(crate) fn LLVMRustCreateAlignmentAttr(C: &Context, bytes: u64) -> &Attribute;
1889    pub(crate) fn LLVMRustCreateDereferenceableAttr(C: &Context, bytes: u64) -> &Attribute;
1890    pub(crate) fn LLVMRustCreateDereferenceableOrNullAttr(C: &Context, bytes: u64) -> &Attribute;
1891    pub(crate) fn LLVMRustCreateByValAttr<'a>(C: &'a Context, ty: &'a Type) -> &'a Attribute;
1892    pub(crate) fn LLVMRustCreateStructRetAttr<'a>(C: &'a Context, ty: &'a Type) -> &'a Attribute;
1893    pub(crate) fn LLVMRustCreateElementTypeAttr<'a>(C: &'a Context, ty: &'a Type) -> &'a Attribute;
1894    pub(crate) fn LLVMRustCreateUWTableAttr(C: &Context, async_: bool) -> &Attribute;
1895    pub(crate) fn LLVMRustCreateAllocSizeAttr(C: &Context, size_arg: u32) -> &Attribute;
1896    pub(crate) fn LLVMRustCreateAllocKindAttr(C: &Context, size_arg: u64) -> &Attribute;
1897    pub(crate) fn LLVMRustCreateMemoryEffectsAttr(
1898        C: &Context,
1899        effects: MemoryEffects,
1900    ) -> &Attribute;
1901    pub(crate) fn LLVMRustCreateRangeAttribute(
1902        C: &Context,
1903        num_bits: c_uint,
1904        lower_words: *const u64,
1905        upper_words: *const u64,
1906    ) -> &Attribute;
1907
1908    // Operations on functions
1909    pub(crate) fn LLVMRustGetOrInsertFunction<'a>(
1910        M: &'a Module,
1911        Name: *const c_char,
1912        NameLen: size_t,
1913        FunctionTy: &'a Type,
1914    ) -> &'a Value;
1915    pub(crate) fn LLVMRustAddFunctionAttributes<'a>(
1916        Fn: &'a Value,
1917        index: c_uint,
1918        Attrs: *const &'a Attribute,
1919        AttrsLen: size_t,
1920    );
1921
1922    // Operations on call sites
1923    pub(crate) fn LLVMRustAddCallSiteAttributes<'a>(
1924        Instr: &'a Value,
1925        index: c_uint,
1926        Attrs: *const &'a Attribute,
1927        AttrsLen: size_t,
1928    );
1929
1930    pub(crate) fn LLVMRustSetFastMath(Instr: &Value);
1931    pub(crate) fn LLVMRustSetAlgebraicMath(Instr: &Value);
1932    pub(crate) fn LLVMRustSetAllowReassoc(Instr: &Value);
1933
1934    // Miscellaneous instructions
1935    pub(crate) fn LLVMRustBuildMemCpy<'a>(
1936        B: &Builder<'a>,
1937        Dst: &'a Value,
1938        DstAlign: c_uint,
1939        Src: &'a Value,
1940        SrcAlign: c_uint,
1941        Size: &'a Value,
1942        IsVolatile: bool,
1943    ) -> &'a Value;
1944    pub(crate) fn LLVMRustBuildMemMove<'a>(
1945        B: &Builder<'a>,
1946        Dst: &'a Value,
1947        DstAlign: c_uint,
1948        Src: &'a Value,
1949        SrcAlign: c_uint,
1950        Size: &'a Value,
1951        IsVolatile: bool,
1952    ) -> &'a Value;
1953    pub(crate) fn LLVMRustBuildMemSet<'a>(
1954        B: &Builder<'a>,
1955        Dst: &'a Value,
1956        DstAlign: c_uint,
1957        Val: &'a Value,
1958        Size: &'a Value,
1959        IsVolatile: bool,
1960    ) -> &'a Value;
1961
1962    pub(crate) fn LLVMRustBuildVectorReduceFAdd<'a>(
1963        B: &Builder<'a>,
1964        Acc: &'a Value,
1965        Src: &'a Value,
1966    ) -> &'a Value;
1967    pub(crate) fn LLVMRustBuildVectorReduceFMul<'a>(
1968        B: &Builder<'a>,
1969        Acc: &'a Value,
1970        Src: &'a Value,
1971    ) -> &'a Value;
1972    pub(crate) fn LLVMRustBuildVectorReduceAdd<'a>(B: &Builder<'a>, Src: &'a Value) -> &'a Value;
1973    pub(crate) fn LLVMRustBuildVectorReduceMul<'a>(B: &Builder<'a>, Src: &'a Value) -> &'a Value;
1974    pub(crate) fn LLVMRustBuildVectorReduceAnd<'a>(B: &Builder<'a>, Src: &'a Value) -> &'a Value;
1975    pub(crate) fn LLVMRustBuildVectorReduceOr<'a>(B: &Builder<'a>, Src: &'a Value) -> &'a Value;
1976    pub(crate) fn LLVMRustBuildVectorReduceXor<'a>(B: &Builder<'a>, Src: &'a Value) -> &'a Value;
1977    pub(crate) fn LLVMRustBuildVectorReduceMin<'a>(
1978        B: &Builder<'a>,
1979        Src: &'a Value,
1980        IsSigned: bool,
1981    ) -> &'a Value;
1982    pub(crate) fn LLVMRustBuildVectorReduceMax<'a>(
1983        B: &Builder<'a>,
1984        Src: &'a Value,
1985        IsSigned: bool,
1986    ) -> &'a Value;
1987    pub(crate) fn LLVMRustBuildVectorReduceFMin<'a>(
1988        B: &Builder<'a>,
1989        Src: &'a Value,
1990        IsNaN: bool,
1991    ) -> &'a Value;
1992    pub(crate) fn LLVMRustBuildVectorReduceFMax<'a>(
1993        B: &Builder<'a>,
1994        Src: &'a Value,
1995        IsNaN: bool,
1996    ) -> &'a Value;
1997
1998    pub(crate) fn LLVMRustBuildMinNum<'a>(
1999        B: &Builder<'a>,
2000        LHS: &'a Value,
2001        RHS: &'a Value,
2002    ) -> &'a Value;
2003    pub(crate) fn LLVMRustBuildMaxNum<'a>(
2004        B: &Builder<'a>,
2005        LHS: &'a Value,
2006        RHS: &'a Value,
2007    ) -> &'a Value;
2008
2009    // Atomic Operations
2010    pub(crate) fn LLVMRustBuildAtomicLoad<'a>(
2011        B: &Builder<'a>,
2012        ElementType: &'a Type,
2013        PointerVal: &'a Value,
2014        Name: *const c_char,
2015        Order: AtomicOrdering,
2016    ) -> &'a Value;
2017
2018    pub(crate) fn LLVMRustBuildAtomicStore<'a>(
2019        B: &Builder<'a>,
2020        Val: &'a Value,
2021        Ptr: &'a Value,
2022        Order: AtomicOrdering,
2023    ) -> &'a Value;
2024
2025    pub(crate) fn LLVMRustTimeTraceProfilerInitialize();
2026
2027    pub(crate) fn LLVMRustTimeTraceProfilerFinishThread();
2028
2029    pub(crate) fn LLVMRustTimeTraceProfilerFinish(FileName: *const c_char);
2030
2031    /// Returns a string describing the last error caused by an LLVMRust* call.
2032    pub(crate) fn LLVMRustGetLastError() -> *const c_char;
2033
2034    /// Prints the timing information collected by `-Ztime-llvm-passes`.
2035    pub(crate) fn LLVMRustPrintPassTimings(OutStr: &RustString);
2036
2037    /// Prints the statistics collected by `-Zprint-codegen-stats`.
2038    pub(crate) fn LLVMRustPrintStatistics(OutStr: &RustString);
2039
2040    pub(crate) fn LLVMRustInlineAsmVerify(
2041        Ty: &Type,
2042        Constraints: *const c_uchar, // See "PTR_LEN_STR".
2043        ConstraintsLen: size_t,
2044    ) -> bool;
2045
2046    pub(crate) fn LLVMRustCoverageWriteFilenamesToBuffer(
2047        Filenames: *const *const c_char,
2048        FilenamesLen: size_t,
2049        Lengths: *const size_t,
2050        LengthsLen: size_t,
2051        BufferOut: &RustString,
2052    );
2053
2054    pub(crate) fn LLVMRustCoverageWriteFunctionMappingsToBuffer(
2055        VirtualFileMappingIDs: *const c_uint,
2056        NumVirtualFileMappingIDs: size_t,
2057        Expressions: *const crate::coverageinfo::ffi::CounterExpression,
2058        NumExpressions: size_t,
2059        CodeRegions: *const crate::coverageinfo::ffi::CodeRegion,
2060        NumCodeRegions: size_t,
2061        ExpansionRegions: *const crate::coverageinfo::ffi::ExpansionRegion,
2062        NumExpansionRegions: size_t,
2063        BranchRegions: *const crate::coverageinfo::ffi::BranchRegion,
2064        NumBranchRegions: size_t,
2065        BufferOut: &RustString,
2066    );
2067
2068    pub(crate) fn LLVMRustCoverageCreatePGOFuncNameVar(
2069        F: &Value,
2070        FuncName: *const c_char,
2071        FuncNameLen: size_t,
2072    ) -> &Value;
2073    pub(crate) fn LLVMRustCoverageHashBytes(Bytes: *const c_char, NumBytes: size_t) -> u64;
2074
2075    pub(crate) fn LLVMRustCoverageWriteCovmapSectionNameToString(M: &Module, OutStr: &RustString);
2076
2077    pub(crate) fn LLVMRustCoverageWriteCovfunSectionNameToString(M: &Module, OutStr: &RustString);
2078
2079    pub(crate) fn LLVMRustCoverageWriteCovmapVarNameToString(OutStr: &RustString);
2080
2081    pub(crate) fn LLVMRustCoverageMappingVersion() -> u32;
2082    pub(crate) fn LLVMRustDebugMetadataVersion() -> u32;
2083    pub(crate) fn LLVMRustVersionMajor() -> u32;
2084    pub(crate) fn LLVMRustVersionMinor() -> u32;
2085    pub(crate) fn LLVMRustVersionPatch() -> u32;
2086
2087    /// Add LLVM module flags.
2088    ///
2089    /// In order for Rust-C LTO to work, module flags must be compatible with Clang. What
2090    /// "compatible" means depends on the merge behaviors involved.
2091    pub(crate) fn LLVMRustAddModuleFlagU32(
2092        M: &Module,
2093        MergeBehavior: ModuleFlagMergeBehavior,
2094        Name: *const c_char,
2095        NameLen: size_t,
2096        Value: u32,
2097    );
2098
2099    pub(crate) fn LLVMRustAddModuleFlagString(
2100        M: &Module,
2101        MergeBehavior: ModuleFlagMergeBehavior,
2102        Name: *const c_char,
2103        NameLen: size_t,
2104        Value: *const c_char,
2105        ValueLen: size_t,
2106    );
2107
2108    pub(crate) fn LLVMRustDIBuilderCreateCompileUnit<'a>(
2109        Builder: &DIBuilder<'a>,
2110        Lang: c_uint,
2111        File: &'a DIFile,
2112        Producer: *const c_char,
2113        ProducerLen: size_t,
2114        isOptimized: bool,
2115        Flags: *const c_char,
2116        RuntimeVer: c_uint,
2117        SplitName: *const c_char,
2118        SplitNameLen: size_t,
2119        kind: DebugEmissionKind,
2120        DWOId: u64,
2121        SplitDebugInlining: bool,
2122        DebugNameTableKind: DebugNameTableKind,
2123    ) -> &'a DIDescriptor;
2124
2125    pub(crate) fn LLVMRustDIBuilderCreateFile<'a>(
2126        Builder: &DIBuilder<'a>,
2127        Filename: *const c_char,
2128        FilenameLen: size_t,
2129        Directory: *const c_char,
2130        DirectoryLen: size_t,
2131        CSKind: ChecksumKind,
2132        Checksum: *const c_char,
2133        ChecksumLen: size_t,
2134        Source: *const c_char,
2135        SourceLen: size_t,
2136    ) -> &'a DIFile;
2137
2138    pub(crate) fn LLVMRustDIBuilderCreateSubroutineType<'a>(
2139        Builder: &DIBuilder<'a>,
2140        ParameterTypes: &'a DIArray,
2141    ) -> &'a DICompositeType;
2142
2143    pub(crate) fn LLVMRustDIBuilderCreateFunction<'a>(
2144        Builder: &DIBuilder<'a>,
2145        Scope: &'a DIDescriptor,
2146        Name: *const c_char,
2147        NameLen: size_t,
2148        LinkageName: *const c_char,
2149        LinkageNameLen: size_t,
2150        File: &'a DIFile,
2151        LineNo: c_uint,
2152        Ty: &'a DIType,
2153        ScopeLine: c_uint,
2154        Flags: DIFlags,
2155        SPFlags: DISPFlags,
2156        MaybeFn: Option<&'a Value>,
2157        TParam: &'a DIArray,
2158        Decl: Option<&'a DIDescriptor>,
2159    ) -> &'a DISubprogram;
2160
2161    pub(crate) fn LLVMRustDIBuilderCreateMethod<'a>(
2162        Builder: &DIBuilder<'a>,
2163        Scope: &'a DIDescriptor,
2164        Name: *const c_char,
2165        NameLen: size_t,
2166        LinkageName: *const c_char,
2167        LinkageNameLen: size_t,
2168        File: &'a DIFile,
2169        LineNo: c_uint,
2170        Ty: &'a DIType,
2171        Flags: DIFlags,
2172        SPFlags: DISPFlags,
2173        TParam: &'a DIArray,
2174    ) -> &'a DISubprogram;
2175
2176    pub(crate) fn LLVMRustDIBuilderCreateBasicType<'a>(
2177        Builder: &DIBuilder<'a>,
2178        Name: *const c_char,
2179        NameLen: size_t,
2180        SizeInBits: u64,
2181        Encoding: c_uint,
2182    ) -> &'a DIBasicType;
2183
2184    pub(crate) fn LLVMRustDIBuilderCreateTypedef<'a>(
2185        Builder: &DIBuilder<'a>,
2186        Type: &'a DIBasicType,
2187        Name: *const c_char,
2188        NameLen: size_t,
2189        File: &'a DIFile,
2190        LineNo: c_uint,
2191        Scope: Option<&'a DIScope>,
2192    ) -> &'a DIDerivedType;
2193
2194    pub(crate) fn LLVMRustDIBuilderCreatePointerType<'a>(
2195        Builder: &DIBuilder<'a>,
2196        PointeeTy: &'a DIType,
2197        SizeInBits: u64,
2198        AlignInBits: u32,
2199        AddressSpace: c_uint,
2200        Name: *const c_char,
2201        NameLen: size_t,
2202    ) -> &'a DIDerivedType;
2203
2204    pub(crate) fn LLVMRustDIBuilderCreateStructType<'a>(
2205        Builder: &DIBuilder<'a>,
2206        Scope: Option<&'a DIDescriptor>,
2207        Name: *const c_char,
2208        NameLen: size_t,
2209        File: &'a DIFile,
2210        LineNumber: c_uint,
2211        SizeInBits: u64,
2212        AlignInBits: u32,
2213        Flags: DIFlags,
2214        DerivedFrom: Option<&'a DIType>,
2215        Elements: &'a DIArray,
2216        RunTimeLang: c_uint,
2217        VTableHolder: Option<&'a DIType>,
2218        UniqueId: *const c_char,
2219        UniqueIdLen: size_t,
2220    ) -> &'a DICompositeType;
2221
2222    pub(crate) fn LLVMRustDIBuilderCreateMemberType<'a>(
2223        Builder: &DIBuilder<'a>,
2224        Scope: &'a DIDescriptor,
2225        Name: *const c_char,
2226        NameLen: size_t,
2227        File: &'a DIFile,
2228        LineNo: c_uint,
2229        SizeInBits: u64,
2230        AlignInBits: u32,
2231        OffsetInBits: u64,
2232        Flags: DIFlags,
2233        Ty: &'a DIType,
2234    ) -> &'a DIDerivedType;
2235
2236    pub(crate) fn LLVMRustDIBuilderCreateVariantMemberType<'a>(
2237        Builder: &DIBuilder<'a>,
2238        Scope: &'a DIScope,
2239        Name: *const c_char,
2240        NameLen: size_t,
2241        File: &'a DIFile,
2242        LineNumber: c_uint,
2243        SizeInBits: u64,
2244        AlignInBits: u32,
2245        OffsetInBits: u64,
2246        Discriminant: Option<&'a Value>,
2247        Flags: DIFlags,
2248        Ty: &'a DIType,
2249    ) -> &'a DIType;
2250
2251    pub(crate) fn LLVMRustDIBuilderCreateStaticMemberType<'a>(
2252        Builder: &DIBuilder<'a>,
2253        Scope: &'a DIDescriptor,
2254        Name: *const c_char,
2255        NameLen: size_t,
2256        File: &'a DIFile,
2257        LineNo: c_uint,
2258        Ty: &'a DIType,
2259        Flags: DIFlags,
2260        val: Option<&'a Value>,
2261        AlignInBits: u32,
2262    ) -> &'a DIDerivedType;
2263
2264    pub(crate) fn LLVMRustDIBuilderCreateQualifiedType<'a>(
2265        Builder: &DIBuilder<'a>,
2266        Tag: c_uint,
2267        Type: &'a DIType,
2268    ) -> &'a DIDerivedType;
2269
2270    pub(crate) fn LLVMRustDIBuilderCreateStaticVariable<'a>(
2271        Builder: &DIBuilder<'a>,
2272        Context: Option<&'a DIScope>,
2273        Name: *const c_char,
2274        NameLen: size_t,
2275        LinkageName: *const c_char,
2276        LinkageNameLen: size_t,
2277        File: &'a DIFile,
2278        LineNo: c_uint,
2279        Ty: &'a DIType,
2280        isLocalToUnit: bool,
2281        Val: &'a Value,
2282        Decl: Option<&'a DIDescriptor>,
2283        AlignInBits: u32,
2284    ) -> &'a DIGlobalVariableExpression;
2285
2286    pub(crate) fn LLVMRustDIBuilderCreateVariable<'a>(
2287        Builder: &DIBuilder<'a>,
2288        Tag: c_uint,
2289        Scope: &'a DIDescriptor,
2290        Name: *const c_char,
2291        NameLen: size_t,
2292        File: &'a DIFile,
2293        LineNo: c_uint,
2294        Ty: &'a DIType,
2295        AlwaysPreserve: bool,
2296        Flags: DIFlags,
2297        ArgNo: c_uint,
2298        AlignInBits: u32,
2299    ) -> &'a DIVariable;
2300
2301    pub(crate) fn LLVMRustDIBuilderCreateArrayType<'a>(
2302        Builder: &DIBuilder<'a>,
2303        Size: u64,
2304        AlignInBits: u32,
2305        Ty: &'a DIType,
2306        Subscripts: &'a DIArray,
2307    ) -> &'a DIType;
2308
2309    pub(crate) fn LLVMRustDIBuilderGetOrCreateSubrange<'a>(
2310        Builder: &DIBuilder<'a>,
2311        Lo: i64,
2312        Count: i64,
2313    ) -> &'a DISubrange;
2314
2315    pub(crate) fn LLVMRustDIBuilderGetOrCreateArray<'a>(
2316        Builder: &DIBuilder<'a>,
2317        Ptr: *const Option<&'a DIDescriptor>,
2318        Count: c_uint,
2319    ) -> &'a DIArray;
2320
2321    pub(crate) fn LLVMRustDIBuilderInsertDeclareAtEnd<'a>(
2322        Builder: &DIBuilder<'a>,
2323        Val: &'a Value,
2324        VarInfo: &'a DIVariable,
2325        AddrOps: *const u64,
2326        AddrOpsCount: c_uint,
2327        DL: &'a DILocation,
2328        InsertAtEnd: &'a BasicBlock,
2329    );
2330
2331    pub(crate) fn LLVMRustDIBuilderCreateEnumerator<'a>(
2332        Builder: &DIBuilder<'a>,
2333        Name: *const c_char,
2334        NameLen: size_t,
2335        Value: *const u64,
2336        SizeInBits: c_uint,
2337        IsUnsigned: bool,
2338    ) -> &'a DIEnumerator;
2339
2340    pub(crate) fn LLVMRustDIBuilderCreateEnumerationType<'a>(
2341        Builder: &DIBuilder<'a>,
2342        Scope: &'a DIScope,
2343        Name: *const c_char,
2344        NameLen: size_t,
2345        File: &'a DIFile,
2346        LineNumber: c_uint,
2347        SizeInBits: u64,
2348        AlignInBits: u32,
2349        Elements: &'a DIArray,
2350        ClassType: &'a DIType,
2351        IsScoped: bool,
2352    ) -> &'a DIType;
2353
2354    pub(crate) fn LLVMRustDIBuilderCreateUnionType<'a>(
2355        Builder: &DIBuilder<'a>,
2356        Scope: Option<&'a DIScope>,
2357        Name: *const c_char,
2358        NameLen: size_t,
2359        File: &'a DIFile,
2360        LineNumber: c_uint,
2361        SizeInBits: u64,
2362        AlignInBits: u32,
2363        Flags: DIFlags,
2364        Elements: Option<&'a DIArray>,
2365        RunTimeLang: c_uint,
2366        UniqueId: *const c_char,
2367        UniqueIdLen: size_t,
2368    ) -> &'a DIType;
2369
2370    pub(crate) fn LLVMRustDIBuilderCreateVariantPart<'a>(
2371        Builder: &DIBuilder<'a>,
2372        Scope: &'a DIScope,
2373        Name: *const c_char,
2374        NameLen: size_t,
2375        File: &'a DIFile,
2376        LineNo: c_uint,
2377        SizeInBits: u64,
2378        AlignInBits: u32,
2379        Flags: DIFlags,
2380        Discriminator: Option<&'a DIDerivedType>,
2381        Elements: &'a DIArray,
2382        UniqueId: *const c_char,
2383        UniqueIdLen: size_t,
2384    ) -> &'a DIDerivedType;
2385
2386    pub(crate) fn LLVMRustDIBuilderCreateTemplateTypeParameter<'a>(
2387        Builder: &DIBuilder<'a>,
2388        Scope: Option<&'a DIScope>,
2389        Name: *const c_char,
2390        NameLen: size_t,
2391        Ty: &'a DIType,
2392    ) -> &'a DITemplateTypeParameter;
2393
2394    pub(crate) fn LLVMRustDICompositeTypeReplaceArrays<'a>(
2395        Builder: &DIBuilder<'a>,
2396        CompositeType: &'a DIType,
2397        Elements: Option<&'a DIArray>,
2398        Params: Option<&'a DIArray>,
2399    );
2400
2401    pub(crate) fn LLVMRustDILocationCloneWithBaseDiscriminator<'a>(
2402        Location: &'a DILocation,
2403        BD: c_uint,
2404    ) -> Option<&'a DILocation>;
2405
2406    pub(crate) fn LLVMRustWriteTypeToString(Type: &Type, s: &RustString);
2407    pub(crate) fn LLVMRustWriteValueToString(value_ref: &Value, s: &RustString);
2408
2409    pub(crate) fn LLVMRustHasFeature(T: &TargetMachine, s: *const c_char) -> bool;
2410
2411    pub(crate) fn LLVMRustPrintTargetCPUs(TM: &TargetMachine, OutStr: &RustString);
2412    pub(crate) fn LLVMRustGetTargetFeaturesCount(T: &TargetMachine) -> size_t;
2413    pub(crate) fn LLVMRustGetTargetFeature(
2414        T: &TargetMachine,
2415        Index: size_t,
2416        Feature: &mut *const c_char,
2417        Desc: &mut *const c_char,
2418    );
2419
2420    pub(crate) fn LLVMRustGetHostCPUName(LenOut: &mut size_t) -> *const u8;
2421
2422    // This function makes copies of pointed to data, so the data's lifetime may end after this
2423    // function returns.
2424    pub(crate) fn LLVMRustCreateTargetMachine(
2425        Triple: *const c_char,
2426        CPU: *const c_char,
2427        Features: *const c_char,
2428        Abi: *const c_char,
2429        Model: CodeModel,
2430        Reloc: RelocModel,
2431        Level: CodeGenOptLevel,
2432        FloatABIType: FloatAbi,
2433        FunctionSections: bool,
2434        DataSections: bool,
2435        UniqueSectionNames: bool,
2436        TrapUnreachable: bool,
2437        Singlethread: bool,
2438        VerboseAsm: bool,
2439        EmitStackSizeSection: bool,
2440        RelaxELFRelocations: bool,
2441        UseInitArray: bool,
2442        SplitDwarfFile: *const c_char,
2443        OutputObjFile: *const c_char,
2444        DebugInfoCompression: *const c_char,
2445        UseEmulatedTls: bool,
2446        ArgsCstrBuff: *const c_char,
2447        ArgsCstrBuffLen: usize,
2448        UseWasmEH: bool,
2449    ) -> *mut TargetMachine;
2450
2451    pub(crate) fn LLVMRustDisposeTargetMachine(T: *mut TargetMachine);
2452    pub(crate) fn LLVMRustAddLibraryInfo<'a>(
2453        PM: &PassManager<'a>,
2454        M: &'a Module,
2455        DisableSimplifyLibCalls: bool,
2456    );
2457    pub(crate) fn LLVMRustWriteOutputFile<'a>(
2458        T: &'a TargetMachine,
2459        PM: *mut PassManager<'a>,
2460        M: &'a Module,
2461        Output: *const c_char,
2462        DwoOutput: *const c_char,
2463        FileType: FileType,
2464        VerifyIR: bool,
2465    ) -> LLVMRustResult;
2466    pub(crate) fn LLVMRustOptimize<'a>(
2467        M: &'a Module,
2468        TM: &'a TargetMachine,
2469        OptLevel: PassBuilderOptLevel,
2470        OptStage: OptStage,
2471        IsLinkerPluginLTO: bool,
2472        NoPrepopulatePasses: bool,
2473        VerifyIR: bool,
2474        LintIR: bool,
2475        ThinLTOBuffer: Option<&mut *mut ThinLTOBuffer>,
2476        EmitThinLTO: bool,
2477        EmitThinLTOSummary: bool,
2478        MergeFunctions: bool,
2479        UnrollLoops: bool,
2480        SLPVectorize: bool,
2481        LoopVectorize: bool,
2482        DisableSimplifyLibCalls: bool,
2483        EmitLifetimeMarkers: bool,
2484        RunEnzyme: bool,
2485        PrintBeforeEnzyme: bool,
2486        PrintAfterEnzyme: bool,
2487        PrintPasses: bool,
2488        SanitizerOptions: Option<&SanitizerOptions>,
2489        PGOGenPath: *const c_char,
2490        PGOUsePath: *const c_char,
2491        InstrumentCoverage: bool,
2492        InstrProfileOutput: *const c_char,
2493        PGOSampleUsePath: *const c_char,
2494        DebugInfoForProfiling: bool,
2495        llvm_selfprofiler: *mut c_void,
2496        begin_callback: SelfProfileBeforePassCallback,
2497        end_callback: SelfProfileAfterPassCallback,
2498        ExtraPasses: *const c_char,
2499        ExtraPassesLen: size_t,
2500        LLVMPlugins: *const c_char,
2501        LLVMPluginsLen: size_t,
2502    ) -> LLVMRustResult;
2503    pub(crate) fn LLVMRustPrintModule(
2504        M: &Module,
2505        Output: *const c_char,
2506        Demangle: extern "C" fn(*const c_char, size_t, *mut c_char, size_t) -> size_t,
2507    ) -> LLVMRustResult;
2508    pub(crate) fn LLVMRustSetLLVMOptions(Argc: c_int, Argv: *const *const c_char);
2509    pub(crate) fn LLVMRustPrintPasses();
2510    pub(crate) fn LLVMRustSetNormalizedTarget(M: &Module, triple: *const c_char);
2511    pub(crate) fn LLVMRustRunRestrictionPass(M: &Module, syms: *const *const c_char, len: size_t);
2512
2513    pub(crate) fn LLVMRustOpenArchive(path: *const c_char) -> Option<&'static mut Archive>;
2514    pub(crate) fn LLVMRustArchiveIteratorNew(AR: &Archive) -> &mut ArchiveIterator<'_>;
2515    pub(crate) fn LLVMRustArchiveIteratorNext<'a>(
2516        AIR: &ArchiveIterator<'a>,
2517    ) -> Option<&'a mut ArchiveChild<'a>>;
2518    pub(crate) fn LLVMRustArchiveChildName(
2519        ACR: &ArchiveChild<'_>,
2520        size: &mut size_t,
2521    ) -> *const c_char;
2522    pub(crate) fn LLVMRustArchiveChildFree<'a>(ACR: &'a mut ArchiveChild<'a>);
2523    pub(crate) fn LLVMRustArchiveIteratorFree<'a>(AIR: &'a mut ArchiveIterator<'a>);
2524    pub(crate) fn LLVMRustDestroyArchive(AR: &'static mut Archive);
2525
2526    pub(crate) fn LLVMRustWriteTwineToString(T: &Twine, s: &RustString);
2527
2528    pub(crate) fn LLVMRustUnpackOptimizationDiagnostic<'a>(
2529        DI: &'a DiagnosticInfo,
2530        pass_name_out: &RustString,
2531        function_out: &mut Option<&'a Value>,
2532        loc_line_out: &mut c_uint,
2533        loc_column_out: &mut c_uint,
2534        loc_filename_out: &RustString,
2535        message_out: &RustString,
2536    );
2537
2538    pub(crate) fn LLVMRustUnpackInlineAsmDiagnostic<'a>(
2539        DI: &'a DiagnosticInfo,
2540        level_out: &mut DiagnosticLevel,
2541        cookie_out: &mut u64,
2542        message_out: &mut Option<&'a Twine>,
2543    );
2544
2545    pub(crate) fn LLVMRustWriteDiagnosticInfoToString(DI: &DiagnosticInfo, s: &RustString);
2546    pub(crate) fn LLVMRustGetDiagInfoKind(DI: &DiagnosticInfo) -> DiagnosticKind;
2547
2548    pub(crate) fn LLVMRustGetSMDiagnostic<'a>(
2549        DI: &'a DiagnosticInfo,
2550        cookie_out: &mut u64,
2551    ) -> &'a SMDiagnostic;
2552
2553    pub(crate) fn LLVMRustUnpackSMDiagnostic(
2554        d: &SMDiagnostic,
2555        message_out: &RustString,
2556        buffer_out: &RustString,
2557        level_out: &mut DiagnosticLevel,
2558        loc_out: &mut c_uint,
2559        ranges_out: *mut c_uint,
2560        num_ranges: &mut usize,
2561    ) -> bool;
2562
2563    pub(crate) fn LLVMRustWriteArchive(
2564        Dst: *const c_char,
2565        NumMembers: size_t,
2566        Members: *const &RustArchiveMember<'_>,
2567        WriteSymbtab: bool,
2568        Kind: ArchiveKind,
2569        isEC: bool,
2570    ) -> LLVMRustResult;
2571    pub(crate) fn LLVMRustArchiveMemberNew<'a>(
2572        Filename: *const c_char,
2573        Name: *const c_char,
2574        Child: Option<&ArchiveChild<'a>>,
2575    ) -> &'a mut RustArchiveMember<'a>;
2576    pub(crate) fn LLVMRustArchiveMemberFree<'a>(Member: &'a mut RustArchiveMember<'a>);
2577
2578    pub(crate) fn LLVMRustSetDataLayoutFromTargetMachine<'a>(M: &'a Module, TM: &'a TargetMachine);
2579
2580    pub(crate) fn LLVMRustPositionBuilderPastAllocas<'a>(B: &Builder<'a>, Fn: &'a Value);
2581    pub(crate) fn LLVMRustPositionBuilderAtStart<'a>(B: &Builder<'a>, BB: &'a BasicBlock);
2582
2583    pub(crate) fn LLVMRustSetModulePICLevel(M: &Module);
2584    pub(crate) fn LLVMRustSetModulePIELevel(M: &Module);
2585    pub(crate) fn LLVMRustSetModuleCodeModel(M: &Module, Model: CodeModel);
2586    pub(crate) fn LLVMRustModuleBufferCreate(M: &Module) -> &'static mut ModuleBuffer;
2587    pub(crate) fn LLVMRustModuleBufferPtr(p: &ModuleBuffer) -> *const u8;
2588    pub(crate) fn LLVMRustModuleBufferLen(p: &ModuleBuffer) -> usize;
2589    pub(crate) fn LLVMRustModuleBufferFree(p: &'static mut ModuleBuffer);
2590    pub(crate) fn LLVMRustModuleCost(M: &Module) -> u64;
2591    pub(crate) fn LLVMRustModuleInstructionStats(M: &Module, Str: &RustString);
2592
2593    pub(crate) fn LLVMRustThinLTOBufferCreate(
2594        M: &Module,
2595        is_thin: bool,
2596        emit_summary: bool,
2597    ) -> &'static mut ThinLTOBuffer;
2598    pub(crate) fn LLVMRustThinLTOBufferFree(M: &'static mut ThinLTOBuffer);
2599    pub(crate) fn LLVMRustThinLTOBufferPtr(M: &ThinLTOBuffer) -> *const c_char;
2600    pub(crate) fn LLVMRustThinLTOBufferLen(M: &ThinLTOBuffer) -> size_t;
2601    pub(crate) fn LLVMRustThinLTOBufferThinLinkDataPtr(M: &ThinLTOBuffer) -> *const c_char;
2602    pub(crate) fn LLVMRustThinLTOBufferThinLinkDataLen(M: &ThinLTOBuffer) -> size_t;
2603    pub(crate) fn LLVMRustCreateThinLTOData(
2604        Modules: *const ThinLTOModule,
2605        NumModules: size_t,
2606        PreservedSymbols: *const *const c_char,
2607        PreservedSymbolsLen: size_t,
2608    ) -> Option<&'static mut ThinLTOData>;
2609    pub(crate) fn LLVMRustPrepareThinLTORename(
2610        Data: &ThinLTOData,
2611        Module: &Module,
2612        Target: &TargetMachine,
2613    );
2614    pub(crate) fn LLVMRustPrepareThinLTOResolveWeak(Data: &ThinLTOData, Module: &Module) -> bool;
2615    pub(crate) fn LLVMRustPrepareThinLTOInternalize(Data: &ThinLTOData, Module: &Module) -> bool;
2616    pub(crate) fn LLVMRustPrepareThinLTOImport(
2617        Data: &ThinLTOData,
2618        Module: &Module,
2619        Target: &TargetMachine,
2620    ) -> bool;
2621    pub(crate) fn LLVMRustFreeThinLTOData(Data: &'static mut ThinLTOData);
2622    pub(crate) fn LLVMRustParseBitcodeForLTO(
2623        Context: &Context,
2624        Data: *const u8,
2625        len: usize,
2626        Identifier: *const c_char,
2627    ) -> Option<&Module>;
2628
2629    pub(crate) fn LLVMRustLinkerNew(M: &Module) -> &mut Linker<'_>;
2630    pub(crate) fn LLVMRustLinkerAdd(
2631        linker: &Linker<'_>,
2632        bytecode: *const c_char,
2633        bytecode_len: usize,
2634    ) -> bool;
2635    pub(crate) fn LLVMRustLinkerFree<'a>(linker: &'a mut Linker<'a>);
2636    pub(crate) fn LLVMRustComputeLTOCacheKey(
2637        key_out: &RustString,
2638        mod_id: *const c_char,
2639        data: &ThinLTOData,
2640    );
2641
2642    pub(crate) fn LLVMRustContextGetDiagnosticHandler(
2643        Context: &Context,
2644    ) -> Option<&DiagnosticHandler>;
2645    pub(crate) fn LLVMRustContextSetDiagnosticHandler(
2646        context: &Context,
2647        diagnostic_handler: Option<&DiagnosticHandler>,
2648    );
2649    pub(crate) fn LLVMRustContextConfigureDiagnosticHandler(
2650        context: &Context,
2651        diagnostic_handler_callback: DiagnosticHandlerTy,
2652        diagnostic_handler_context: *mut c_void,
2653        remark_all_passes: bool,
2654        remark_passes: *const *const c_char,
2655        remark_passes_len: usize,
2656        remark_file: *const c_char,
2657        pgo_available: bool,
2658    );
2659
2660    pub(crate) fn LLVMRustGetMangledName(V: &Value, out: &RustString);
2661
2662    pub(crate) fn LLVMRustGetElementTypeArgIndex(CallSite: &Value) -> i32;
2663
2664    pub(crate) fn LLVMRustLLVMHasZlibCompressionForDebugSymbols() -> bool;
2665
2666    pub(crate) fn LLVMRustLLVMHasZstdCompressionForDebugSymbols() -> bool;
2667
2668    pub(crate) fn LLVMRustGetSymbols(
2669        buf_ptr: *const u8,
2670        buf_len: usize,
2671        state: *mut c_void,
2672        callback: GetSymbolsCallback,
2673        error_callback: GetSymbolsErrorCallback,
2674    ) -> *mut c_void;
2675
2676    pub(crate) fn LLVMRustIs64BitSymbolicFile(buf_ptr: *const u8, buf_len: usize) -> bool;
2677
2678    pub(crate) fn LLVMRustIsECObject(buf_ptr: *const u8, buf_len: usize) -> bool;
2679
2680    pub(crate) fn LLVMRustSetNoSanitizeAddress(Global: &Value);
2681    pub(crate) fn LLVMRustSetNoSanitizeHWAddress(Global: &Value);
2682}