Skip to content

Commit

Permalink
Support atomic fence intrinsic
Browse files Browse the repository at this point in the history
  • Loading branch information
HKalbasi committed Jul 27, 2023
1 parent b7d91ca commit 17cc813
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 6 deletions.
8 changes: 6 additions & 2 deletions crates/hir-ty/src/consteval/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2521,12 +2521,16 @@ fn const_trait_assoc() {
);
check_number(
r#"
//- minicore: size_of
//- minicore: size_of, fn
//- /a/lib.rs crate:a
use core::mem::size_of;
pub struct S<T>(T);
impl<T> S<T> {
pub const X: usize = core::mem::size_of::<T>();
pub const X: usize = {
let k: T;
let f = || core::mem::size_of::<T>();
f()
};
}
//- /main.rs crate:main deps:a
use a::{S};
Expand Down
4 changes: 4 additions & 0 deletions crates/hir-ty/src/consteval/tests/intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,8 @@ fn atomic() {
pub fn atomic_nand_seqcst<T: Copy>(dst: *mut T, src: T) -> T;
pub fn atomic_or_release<T: Copy>(dst: *mut T, src: T) -> T;
pub fn atomic_xor_seqcst<T: Copy>(dst: *mut T, src: T) -> T;
pub fn atomic_fence_seqcst();
pub fn atomic_singlethreadfence_acqrel();
}
fn should_not_reach() {
Expand All @@ -452,13 +454,15 @@ fn atomic() {
if (30, true) != atomic_cxchg_release_seqcst(&mut y, 30, 40) {
should_not_reach();
}
atomic_fence_seqcst();
if (40, false) != atomic_cxchg_release_seqcst(&mut y, 30, 50) {
should_not_reach();
}
if (40, true) != atomic_cxchgweak_acquire_acquire(&mut y, 40, 30) {
should_not_reach();
}
let mut z = atomic_xsub_seqcst(&mut x, -200);
atomic_singlethreadfence_acqrel();
atomic_xor_seqcst(&mut x, 1024);
atomic_load_seqcst(&x) + z * 3 + atomic_load_seqcst(&y) * 2
};
Expand Down
4 changes: 1 addition & 3 deletions crates/hir-ty/src/mir/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -702,9 +702,7 @@ impl Evaluator<'_> {
}

fn layout_adt(&self, adt: AdtId, subst: Substitution) -> Result<Arc<Layout>> {
self.db.layout_of_adt(adt, subst.clone(), self.trait_env.clone()).map_err(|e| {
MirEvalError::LayoutError(e, TyKind::Adt(chalk_ir::AdtId(adt), subst).intern(Interner))
})
self.layout(&TyKind::Adt(chalk_ir::AdtId(adt), subst).intern(Interner))
}

fn place_ty<'a>(&'a self, p: &Place, locals: &'a Locals) -> Result<Ty> {
Expand Down
9 changes: 8 additions & 1 deletion crates/hir-ty/src/mir/eval/shim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1057,7 +1057,14 @@ impl Evaluator<'_> {
_span: MirSpan,
) -> Result<()> {
// We are a single threaded runtime with no UB checking and no optimization, so
// we can implement these as normal functions.
// we can implement atomic intrinsics as normal functions.

if name.starts_with("singlethreadfence_") || name.starts_with("fence_") {
return Ok(());
}

// The rest of atomic intrinsics have exactly one generic arg

let Some(ty) = generic_args.as_slice(Interner).get(0).and_then(|it| it.ty(Interner)) else {
return Err(MirEvalError::TypeError("atomic intrinsic generic arg is not provided"));
};
Expand Down
5 changes: 5 additions & 0 deletions crates/hir-ty/src/mir/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,11 @@ impl<'ctx> MirLowerCtx<'ctx> {
expr_id.into(),
)
}
TyKind::Closure(_, _) => {
not_supported!(
"method resolution not emitted for closure (Are Fn traits available?)"
);
}
TyKind::Error => {
return Err(MirLowerError::MissingFunctionDefinition(self.owner, expr_id))
}
Expand Down

0 comments on commit 17cc813

Please sign in to comment.