Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 4 additions & 9 deletions crates/cuda_builder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -699,10 +699,12 @@ fn invoke_rustc(builder: &CudaBuilder) -> Result<PathBuf, CudaBuilderError> {

let mut rustflags = vec![
format!("-Zcodegen-backend={}", rustc_codegen_nvvm.display()),
"-Zunstable-options".into(),
"-Zcrate-attr=feature(register_tool)".into(),
"-Zcrate-attr=register_tool(nvvm_internal)".into(),
"-Zcrate-attr=no_std".into(),
"-Zsaturating_float_casts=false".into(),
"-Cpanic=immediate-abort".into(),
];

if let Some(emit) = &builder.emit {
Expand Down Expand Up @@ -775,12 +777,7 @@ fn invoke_rustc(builder: &CudaBuilder) -> Result<PathBuf, CudaBuilderError> {
cargo.arg("--release");
}

// TODO(RDambrosio016): Remove this once we can get meaningful error messages in panic to work.
// for now we enable it to remove some useless indirect calls in the ptx.
cargo.arg("-Zbuild-std-features=panic_immediate_abort");

if builder.optix {
cargo.arg("-Zbuild-std-features=panic_immediate_abort");
cargo.arg("-Zunstable-options");
cargo.arg("--config");
cargo.arg("optix=\"1\"");
Expand Down Expand Up @@ -838,7 +835,7 @@ struct RustcOutput {
}

fn get_last_artifact(out: &str) -> Option<PathBuf> {
let artifacts =
let mut artifacts =
out.lines()
.filter_map(|line| match serde_json::from_str::<RustcOutput>(line) {
Ok(line) => Some(line),
Expand All @@ -849,9 +846,7 @@ fn get_last_artifact(out: &str) -> Option<PathBuf> {
}
});

let last = artifacts
.filter(|line| line.reason == "compiler-artifact")
.next_back()?;
let last = artifacts.rfind(|line| line.reason == "compiler-artifact")?;

let mut filenames = last
.filenames
Expand Down
2 changes: 1 addition & 1 deletion crates/cuda_std/src/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ pub fn index() -> u32 {

#[inline(always)]
pub fn index_1d() -> u32 {
thread_idx_x() as u32 + block_idx_x() as u32 * block_dim_x() as u32
thread_idx_x() + block_idx_x() * block_dim_x()
}

#[inline(always)]
Expand Down
2 changes: 0 additions & 2 deletions crates/gpu_rand/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@
#![deny(missing_docs)]
#![deny(missing_debug_implementations)]
#![allow(clippy::unreadable_literal)]
#![feature(doc_cfg)]

pub mod xoroshiro;

mod default;
Expand Down
14 changes: 13 additions & 1 deletion crates/rustc_codegen_nvvm/rustc_llvm_wrapper/RustWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1500,6 +1500,18 @@ extern "C" LLVMValueRef LLVMRustBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
unwrap(Fn), makeArrayRef(unwrap(Args), NumArgs), Bundles, Name));
}

extern "C" LLVMValueRef LLVMRustBuildCall2(LLVMBuilderRef B, LLVMTypeRef FnTy,
LLVMValueRef Fn, LLVMValueRef *Args,
unsigned NumArgs,
OperandBundleDef *Bundle,
const char *Name)
{
assert(Bundle == nullptr && "LLVM 7 lacks CreateCall(FunctionType, ..., Bundles)");
return wrap(unwrap(B)->CreateCall(
unwrap<FunctionType>(FnTy), unwrap(Fn),
makeArrayRef(unwrap(Args), NumArgs), Name));
}

extern "C" LLVMValueRef
LLVMRustBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn, LLVMValueRef *Args,
unsigned NumArgs, LLVMBasicBlockRef Then,
Expand Down Expand Up @@ -1925,4 +1937,4 @@ extern "C" LLVMValueRef LLVMBuildInBoundsGEP2(LLVMBuilderRef B, LLVMTypeRef Ty,
unwrap(B)->CreateInBoundsGEP(unwrap(Ty), unwrap(Pointer), IdxList, Name));
}

#endif
#endif
6 changes: 5 additions & 1 deletion crates/rustc_codegen_nvvm/src/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,10 @@ impl ArgAttributeExt for ArgAttribute {
where
F: FnMut(llvm::Attribute),
{
for_each_kind!(self, f, NoAlias, NoCapture, NonNull, ReadOnly, InReg)
for_each_kind!(self, f, NoAlias, NonNull, ReadOnly, InReg);
if self.contains(ArgAttribute::CapturesNone) {
f(llvm::Attribute::NoCapture);
}
}
}

Expand Down Expand Up @@ -672,6 +675,7 @@ impl<'ll, 'tcx> ArgAbiExt<'ll, 'tcx> for ArgAbi<'tcx, Ty<'tcx>> {
scratch_align,
bx.const_usize(copy_bytes),
MemFlags::empty(),
None,
);

bx.lifetime_end(llscratch, scratch_size);
Expand Down
201 changes: 87 additions & 114 deletions crates/rustc_codegen_nvvm/src/allocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,155 +3,128 @@ use crate::llvm::{self, False, True};
use crate::target;
use libc::c_uint;
use rustc_ast::expand::allocator::{
ALLOCATOR_METHODS, AllocatorKind, AllocatorTy, alloc_error_handler_name, default_fn_name,
AllocatorMethod, AllocatorTy, NO_ALLOC_SHIM_IS_UNSTABLE, default_fn_name, global_fn_name,
};
use rustc_middle::ty::TyCtxt;
use rustc_symbol_mangling::mangle_internal_symbol;

// adapted from rustc_codegen_llvm
pub(crate) unsafe fn codegen(
_tcx: TyCtxt<'_>,
pub(crate) fn codegen(
tcx: TyCtxt<'_>,
mods: &mut LlvmMod,
_module_name: &str,
kind: AllocatorKind,
alloc_error_handler_kind: AllocatorKind,
methods: &[AllocatorMethod],
) {
let llcx = &*mods.llcx;
let llmod = unsafe { mods.llmod.as_ref().unwrap() };
let usize = unsafe { target::usize_ty(llcx) };
let i8 = unsafe { llvm::LLVMInt8TypeInContext(llcx) };
let i8p = unsafe { llvm::LLVMPointerType(i8, 0) };
let void = unsafe { llvm::LLVMVoidTypeInContext(llcx) };
unsafe {
let llcx = &*mods.llcx;
let llmod = mods.llmod.as_ref().unwrap();
let usize = target::usize_ty(llcx);
let i8 = llvm::LLVMInt8TypeInContext(llcx);
let i8p = llvm::LLVMPointerType(i8, 0);
let void = llvm::LLVMVoidTypeInContext(llcx);

let mut used = Vec::new();
let mut used = Vec::new();

if kind == AllocatorKind::Default {
for method in ALLOCATOR_METHODS {
for method in methods {
let mut args = Vec::with_capacity(method.inputs.len());
for ty in method.inputs.iter() {
match ty.ty {
for input in method.inputs {
match input.ty {
AllocatorTy::Layout => {
args.push(usize); // size
args.push(usize); // align
args.push(usize);
args.push(usize);
}
AllocatorTy::Ptr => args.push(i8p),
AllocatorTy::Usize => args.push(usize),

AllocatorTy::ResultPtr | AllocatorTy::Unit => panic!("invalid allocator arg"),
AllocatorTy::Never | AllocatorTy::ResultPtr | AllocatorTy::Unit => {
panic!("invalid allocator arg")
}
}
}

let no_return = matches!(method.output, AllocatorTy::Never);
let output = match method.output {
AllocatorTy::ResultPtr => Some(i8p),
AllocatorTy::Unit => None,

AllocatorTy::Never | AllocatorTy::Unit => None,
AllocatorTy::Layout | AllocatorTy::Usize | AllocatorTy::Ptr => {
panic!("invalid allocator output")
}
};

let ty = unsafe {
llvm::LLVMFunctionType(
output.unwrap_or(void),
args.as_ptr(),
args.len() as c_uint,
False,
)
};
let name = format!("__rust_{}", method.name);
let llfn = unsafe {
llvm::LLVMRustGetOrInsertFunction(llmod, name.as_ptr().cast(), name.len(), ty)
};

let ty = llvm::LLVMFunctionType(
output.unwrap_or(void),
args.as_ptr(),
args.len() as c_uint,
False,
);
let from_name = mangle_internal_symbol(tcx, &global_fn_name(method.name));
let llfn = llvm::LLVMRustGetOrInsertFunction(
llmod,
from_name.as_ptr().cast(),
from_name.len(),
ty,
);
used.push(llfn);
// nvvm doesnt support uwtable so dont try to generate it

let callee = default_fn_name(method.name);
let callee = unsafe {
llvm::LLVMRustGetOrInsertFunction(llmod, callee.as_ptr().cast(), callee.len(), ty)
};
unsafe { llvm::LLVMRustSetVisibility(callee, llvm::Visibility::Hidden) };
if no_return {
llvm::Attribute::NoReturn.apply_llfn(llvm::AttributePlace::Function, llfn);
}

let llbb = unsafe {
llvm::LLVMAppendBasicBlockInContext(llcx, llfn, c"entry".as_ptr().cast())
};
let to_name = mangle_internal_symbol(tcx, &default_fn_name(method.name));
let callee = llvm::LLVMRustGetOrInsertFunction(
llmod,
to_name.as_ptr().cast(),
to_name.len(),
ty,
);
used.push(callee);
if no_return {
llvm::Attribute::NoReturn.apply_llfn(llvm::AttributePlace::Function, callee);
}
llvm::LLVMRustSetVisibility(callee, llvm::Visibility::Hidden);

let llbuilder = unsafe { llvm::LLVMCreateBuilderInContext(llcx) };
unsafe { llvm::LLVMPositionBuilderAtEnd(llbuilder, llbb) };
let llbb = llvm::LLVMAppendBasicBlockInContext(llcx, llfn, c"entry".as_ptr().cast());
let llbuilder = llvm::LLVMCreateBuilderInContext(llcx);
llvm::LLVMPositionBuilderAtEnd(llbuilder, llbb);
let args = args
.iter()
.enumerate()
.map(|(i, _)| unsafe { llvm::LLVMGetParam(llfn, i as c_uint) })
.map(|(i, _)| llvm::LLVMGetParam(llfn, i as c_uint))
.collect::<Vec<_>>();
let ret = unsafe {
llvm::LLVMRustBuildCall(
llbuilder,
callee,
args.as_ptr(),
args.len() as c_uint,
None,
)
};
unsafe { llvm::LLVMSetTailCall(ret, True) };
let ret = llvm::LLVMRustBuildCall(
llbuilder,
callee,
args.as_ptr(),
args.len() as c_uint,
None,
);
llvm::LLVMSetTailCall(ret, True);
if output.is_some() {
unsafe { llvm::LLVMBuildRet(llbuilder, ret) };
llvm::LLVMBuildRet(llbuilder, ret);
} else {
unsafe { llvm::LLVMBuildRetVoid(llbuilder) };
llvm::LLVMBuildRetVoid(llbuilder);
}
unsafe { llvm::LLVMDisposeBuilder(llbuilder) };
llvm::LLVMDisposeBuilder(llbuilder);
}
}

// rust alloc error handler
let args = [usize, usize]; // size, align

let ty = unsafe { llvm::LLVMFunctionType(void, args.as_ptr(), args.len() as c_uint, False) };
let name = "__rust_alloc_error_handler".to_string();
let llfn =
unsafe { llvm::LLVMRustGetOrInsertFunction(llmod, name.as_ptr().cast(), name.len(), ty) };

used.push(llfn);

// -> ! DIFlagNoReturn
llvm::Attribute::NoReturn.apply_llfn(llvm::AttributePlace::Function, llfn);

let callee = alloc_error_handler_name(alloc_error_handler_kind);
let callee = unsafe {
llvm::LLVMRustGetOrInsertFunction(llmod, callee.as_ptr().cast(), callee.len(), ty)
};

used.push(callee);

// -> ! DIFlagNoReturn
llvm::Attribute::NoReturn.apply_llfn(llvm::AttributePlace::Function, callee);
unsafe { llvm::LLVMRustSetVisibility(callee, llvm::Visibility::Hidden) };

let llbb = unsafe { llvm::LLVMAppendBasicBlockInContext(llcx, llfn, c"entry".as_ptr().cast()) };

let llbuilder = unsafe { llvm::LLVMCreateBuilderInContext(llcx) };
unsafe { llvm::LLVMPositionBuilderAtEnd(llbuilder, llbb) };
let args = args
.iter()
.enumerate()
.map(|(i, _)| unsafe { llvm::LLVMGetParam(llfn, i as c_uint) })
.collect::<Vec<_>>();
let ret = unsafe {
llvm::LLVMRustBuildCall(llbuilder, callee, args.as_ptr(), args.len() as c_uint, None)
};
unsafe { llvm::LLVMSetTailCall(ret, True) };
unsafe { llvm::LLVMBuildRetVoid(llbuilder) };
unsafe { llvm::LLVMDisposeBuilder(llbuilder) };

let ptr_ty = unsafe { llvm::LLVMPointerType(llvm::LLVMInt8TypeInContext(llcx), 0) };
let shim_ty = llvm::LLVMFunctionType(void, std::ptr::null(), 0, False);
let shim_name = mangle_internal_symbol(tcx, NO_ALLOC_SHIM_IS_UNSTABLE);
let shim = llvm::LLVMRustGetOrInsertFunction(
llmod,
shim_name.as_ptr().cast(),
shim_name.len(),
shim_ty,
);
used.push(shim);

let ptr_ty = llvm::LLVMPointerType(llvm::LLVMInt8TypeInContext(llcx), 0);
for used in &mut used {
*used = llvm::LLVMConstBitCast(used, ptr_ty);
}

for used in &mut used {
*used = unsafe { llvm::LLVMConstBitCast(used, ptr_ty) };
let section = c"llvm.metadata";
let array = llvm::LLVMConstArray(ptr_ty, used.as_ptr(), used.len() as u32);
let g = llvm::LLVMAddGlobal(llmod, llvm::LLVMTypeOf(array), c"llvm.used".as_ptr().cast());
llvm::LLVMSetInitializer(g, array);
llvm::LLVMRustSetLinkage(g, llvm::Linkage::AppendingLinkage);
llvm::LLVMSetSection(g, section.as_ptr());
}

let section = c"llvm.metadata";
let array = unsafe { llvm::LLVMConstArray(ptr_ty, used.as_ptr(), used.len() as u32) };
let g = unsafe {
llvm::LLVMAddGlobal(llmod, llvm::LLVMTypeOf(array), c"llvm.used".as_ptr().cast())
};
unsafe { llvm::LLVMSetInitializer(g, array) };
unsafe { llvm::LLVMRustSetLinkage(g, llvm::Linkage::AppendingLinkage) };
unsafe { llvm::LLVMSetSection(g, section.as_ptr()) };
}
2 changes: 1 addition & 1 deletion crates/rustc_codegen_nvvm/src/asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use rustc_codegen_ssa::{
ConstCodegenMethods, GlobalAsmOperandRef, InlineAsmOperandRef,
},
};
use rustc_hash::FxHashMap;
use rustc_data_structures::fx::FxHashMap;
use rustc_middle::{span_bug, ty::Instance};
use rustc_span::{Pos, Span};
use rustc_target::asm::{InlineAsmRegClass, InlineAsmRegOrRegClass, NvptxInlineAsmRegClass};
Expand Down
Loading
Loading