From e2866c0a673ebe073ebfdd4b48b5f09508d143f6 Mon Sep 17 00:00:00 2001 From: Caleb Zulawski Date: Fri, 22 Jul 2022 01:48:30 +0000 Subject: Add simd_cast_ptr, simd_expose_addr, and simd_from_exposed_addr intrinsics --- compiler/rustc_codegen_llvm/src/intrinsic.rs | 79 ++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) (limited to 'compiler/rustc_codegen_llvm/src') diff --git a/compiler/rustc_codegen_llvm/src/intrinsic.rs b/compiler/rustc_codegen_llvm/src/intrinsic.rs index a18f5b9dd7f..78afbdfc521 100644 --- a/compiler/rustc_codegen_llvm/src/intrinsic.rs +++ b/compiler/rustc_codegen_llvm/src/intrinsic.rs @@ -1704,6 +1704,85 @@ unsupported {} from `{}` with element `{}` of size `{}` to `{}`"#, bitwise_red!(simd_reduce_all: vector_reduce_and, true); bitwise_red!(simd_reduce_any: vector_reduce_or, true); + if name == sym::simd_cast_ptr { + require_simd!(ret_ty, "return"); + let (out_len, out_elem) = ret_ty.simd_size_and_type(bx.tcx()); + require!( + in_len == out_len, + "expected return type with length {} (same as input type `{}`), \ + found `{}` with length {}", + in_len, + in_ty, + ret_ty, + out_len + ); + + match in_elem.kind() { + ty::RawPtr(_) => {} + _ => return_error!("expected pointer, got `{}`", in_elem), + } + match out_elem.kind() { + ty::RawPtr(_) => {} + _ => return_error!("expected pointer, got `{}`", out_elem), + } + + if in_elem == out_elem { + return Ok(args[0].immediate()); + } else { + return Ok(bx.pointercast(args[0].immediate(), llret_ty)); + } + } + + if name == sym::simd_expose_addr { + require_simd!(ret_ty, "return"); + let (out_len, out_elem) = ret_ty.simd_size_and_type(bx.tcx()); + require!( + in_len == out_len, + "expected return type with length {} (same as input type `{}`), \ + found `{}` with length {}", + in_len, + in_ty, + ret_ty, + out_len + ); + + match in_elem.kind() { + ty::RawPtr(_) => {} + _ => return_error!("expected pointer, got `{}`", in_elem), + } + match out_elem.kind() { + ty::Uint(ty::UintTy::Usize) => {} + _ => return_error!("expected `usize`, got `{}`", out_elem), + } + + return Ok(bx.ptrtoint(args[0].immediate(), llret_ty)); + } + + if name == sym::simd_from_exposed_addr { + require_simd!(ret_ty, "return"); + let (out_len, out_elem) = ret_ty.simd_size_and_type(bx.tcx()); + require!( + in_len == out_len, + "expected return type with length {} (same as input type `{}`), \ + found `{}` with length {}", + in_len, + in_ty, + ret_ty, + out_len + ); + + match in_elem.kind() { + ty::Uint(ty::UintTy::Usize) => {} + _ => return_error!("expected `usize`, got `{}`", in_elem), + } + match out_elem.kind() { + ty::RawPtr(_) => {} + _ => return_error!("expected pointer, got `{}`", out_elem), + } + + return Ok(bx.inttoptr(args[0].immediate(), llret_ty)); + } + if name == sym::simd_cast || name == sym::simd_as { require_simd!(ret_ty, "return"); let (out_len, out_elem) = ret_ty.simd_size_and_type(bx.tcx()); -- cgit 1.4.1-3-g733a5 From d00928aa69512c02490fcb228a70ad5c3f865613 Mon Sep 17 00:00:00 2001 From: Caleb Zulawski Date: Thu, 4 Aug 2022 03:49:16 +0000 Subject: Require pointers to be sized --- compiler/rustc_codegen_llvm/src/intrinsic.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'compiler/rustc_codegen_llvm/src') diff --git a/compiler/rustc_codegen_llvm/src/intrinsic.rs b/compiler/rustc_codegen_llvm/src/intrinsic.rs index 78afbdfc521..45463f96b8b 100644 --- a/compiler/rustc_codegen_llvm/src/intrinsic.rs +++ b/compiler/rustc_codegen_llvm/src/intrinsic.rs @@ -1718,11 +1718,19 @@ unsupported {} from `{}` with element `{}` of size `{}` to `{}`"#, ); match in_elem.kind() { - ty::RawPtr(_) => {} + ty::RawPtr(p) => require!( + p.ty.is_sized(bx.tcx.at(span), ty::ParamEnv::reveal_all()), + "cannot cast pointer to unsized type `{}`", + in_elem + ), _ => return_error!("expected pointer, got `{}`", in_elem), } match out_elem.kind() { - ty::RawPtr(_) => {} + ty::RawPtr(p) => require!( + p.ty.is_sized(bx.tcx.at(span), ty::ParamEnv::reveal_all()), + "cannot cast to pointer to unsized type `{}`", + out_elem + ), _ => return_error!("expected pointer, got `{}`", out_elem), } -- cgit 1.4.1-3-g733a5 From 3f2ce0624dfed866f758521b225e26c00b3250d8 Mon Sep 17 00:00:00 2001 From: Caleb Zulawski Date: Sat, 27 Aug 2022 13:35:18 +0000 Subject: Check pointer metadata rather than pointee size --- compiler/rustc_codegen_llvm/src/intrinsic.rs | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) (limited to 'compiler/rustc_codegen_llvm/src') diff --git a/compiler/rustc_codegen_llvm/src/intrinsic.rs b/compiler/rustc_codegen_llvm/src/intrinsic.rs index 45463f96b8b..82d34ce9d14 100644 --- a/compiler/rustc_codegen_llvm/src/intrinsic.rs +++ b/compiler/rustc_codegen_llvm/src/intrinsic.rs @@ -1718,19 +1718,23 @@ unsupported {} from `{}` with element `{}` of size `{}` to `{}`"#, ); match in_elem.kind() { - ty::RawPtr(p) => require!( - p.ty.is_sized(bx.tcx.at(span), ty::ParamEnv::reveal_all()), - "cannot cast pointer to unsized type `{}`", - in_elem - ), + ty::RawPtr(p) => { + let (metadata, check_sized) = p.ty.ptr_metadata_ty(bx.tcx, |ty| { + bx.tcx.normalize_erasing_regions(ty::ParamEnv::reveal_all(), ty) + }); + assert!(!check_sized); // we are in codegen, so we shouldn't see these types + require!(metadata.is_unit(), "cannot cast fat pointer `{}`", in_elem) + } _ => return_error!("expected pointer, got `{}`", in_elem), } match out_elem.kind() { - ty::RawPtr(p) => require!( - p.ty.is_sized(bx.tcx.at(span), ty::ParamEnv::reveal_all()), - "cannot cast to pointer to unsized type `{}`", - out_elem - ), + ty::RawPtr(p) => { + let (metadata, check_sized) = p.ty.ptr_metadata_ty(bx.tcx, |ty| { + bx.tcx.normalize_erasing_regions(ty::ParamEnv::reveal_all(), ty) + }); + assert!(!check_sized); // we are in codegen, so we shouldn't see these types + require!(metadata.is_unit(), "cannot cast to fat pointer `{}`", out_elem) + } _ => return_error!("expected pointer, got `{}`", out_elem), } -- cgit 1.4.1-3-g733a5