MCPcopy Create free account
hub / github.com/Rust-GPU/rust-cuda / codegen

Function codegen

crates/rustc_codegen_nvvm/src/allocator.rs:10–141  ·  view source on GitHub ↗

adapted from rustc_codegen_llvm

(
    _tcx: TyCtxt<'_>,
    mods: &mut LlvmMod,
    kind: AllocatorKind,
    has_alloc_error_handler: bool,
)

Source from the content-addressed store, hash-verified

8
9// adapted from rustc_codegen_llvm
10pub(crate) unsafe fn codegen(
11 _tcx: TyCtxt<'_>,
12 mods: &mut LlvmMod,
13 kind: AllocatorKind,
14 has_alloc_error_handler: bool,
15) {
16 let llcx = &*mods.llcx;
17 let llmod = mods.llmod.as_ref().unwrap();
18 let usize = target::usize_ty(llcx);
19 let i8 = llvm::LLVMInt8TypeInContext(llcx);
20 let i8p = llvm::LLVMPointerType(i8, 0);
21 let void = llvm::LLVMVoidTypeInContext(llcx);
22
23 let mut used = Vec::new();
24
25 for method in ALLOCATOR_METHODS {
26 let mut args = Vec::with_capacity(method.inputs.len());
27 for ty in method.inputs.iter() {
28 match *ty {
29 AllocatorTy::Layout => {
30 args.push(usize); // size
31 args.push(usize); // align
32 }
33 AllocatorTy::Ptr => args.push(i8p),
34 AllocatorTy::Usize => args.push(usize),
35
36 AllocatorTy::ResultPtr | AllocatorTy::Unit => panic!("invalid allocator arg"),
37 }
38 }
39 let output = match method.output {
40 AllocatorTy::ResultPtr => Some(i8p),
41 AllocatorTy::Unit => None,
42
43 AllocatorTy::Layout | AllocatorTy::Usize | AllocatorTy::Ptr => {
44 panic!("invalid allocator output")
45 }
46 };
47
48 let ty = llvm::LLVMFunctionType(
49 output.unwrap_or(void),
50 args.as_ptr(),
51 args.len() as c_uint,
52 False,
53 );
54 let name = format!("__rust_{}", method.name);
55 let llfn = llvm::LLVMRustGetOrInsertFunction(llmod, name.as_ptr().cast(), name.len(), ty);
56
57 used.push(llfn);
58 // nvvm doesnt support uwtable so dont try to generate it
59
60 let callee = kind.fn_name(method.name);
61 let callee =
62 llvm::LLVMRustGetOrInsertFunction(llmod, callee.as_ptr().cast(), callee.len(), ty);
63 llvm::LLVMRustSetVisibility(callee, llvm::Visibility::Hidden);
64
65 let llbb = llvm::LLVMAppendBasicBlockInContext(llcx, llfn, "entry\0".as_ptr().cast());
66
67 let llbuilder = llvm::LLVMCreateBuilderInContext(llcx);

Callers

nothing calls this directly

Calls 13

usize_tyFunction · 0.85
newFunction · 0.85
LLVMRustSetVisibilityFunction · 0.85
LLVMRustSetLinkageFunction · 0.85
iterMethod · 0.80
as_ptrMethod · 0.80
apply_llfnMethod · 0.80
LLVMRustBuildCallFunction · 0.70
as_refMethod · 0.45
lenMethod · 0.45
pushMethod · 0.45

Tested by

no test coverage detected