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