Skip to content

Barries are unsafe, but not clear why #382

Description

@nazar-pc

Functions like workgroup_memory_barrier_with_group_sync() are unsafe, but it is not clear why (no "Safety" section in docs).

They should either not be unsafe or "Safety" section should be added to corresponding function docs explaining how to use them safely.

Activity

  1. Firestar99 commented on Sep 13, 2025

    @Firestar99
    Member

    I think we just made everything in spirv_std::arch unsafe since those are all intrinsics. But I could see us making many of them safe, #306 already started on subgroup. (We also have quite a few unsound things that need fixing, like shared memory being &mut isn't great when it rather should be a &[Cell<T>])

  2. nazar-pc commented on Sep 13, 2025

    @nazar-pc
    ContributorAuthor

    Yes, shared memory (and global for that matter) are a little funny because they appear to provide &mut from multiple threads, which is at odds with Rust aliasing rules. It is like working with custom DSL: you see one thing, but imagine a different thing.

    Since this is an explicitly multi-threaded situation, it can't be Cell, but rather UnsafeCell (or SyncUnsafeCell, though I guess no one will spawn more threads from existing thread, so it makes no difference), which unfortunately immediately causes developer experience to crater due to inherent complexity of dealing with UnsafeCell.

    It may not be UB (or at very least not the same UB as in CPU code) to modify those from multiple threads, so might benefit from a custom container that is less painful to work with than UnsafeCell.

  3. schell commented on Sep 15, 2025

    @schell
    Contributor

    A section about the safety would be a pretty easy addition, and we could mention that these are unsafe because they're intrinsics.

  4. Firestar99 commented on Sep 15, 2025

    @Firestar99
    Member

    I'd rather not, if all intrinsics are unsafe people will stop reading the safety section as "it's just an intrinsic". I'd rather selectively mark functions as unsafe when they are actually unsafe.

  5. nazar-pc commented on Sep 15, 2025

    @nazar-pc
    ContributorAuthor

    because they're intrinsics

    This is not a correct justification. There are many intrinsics that are completely safe to use. In Rust standard library all standard atomic operations are safe, even most x86-64 SIMD intrinsics are actually safe in the latest stable (you have to use correct target_feature for it of course): https://doc.rust-lang.org/stable/core/arch/x86_64/fn._mm256_and_si256.html

    So I agree with @Firestar99 that every intrinsic needs to be evaluated separately.

    Let's take workgroup_memory_barrier_with_group_sync() intrinsic for example. When is it unsafe to call? "just an intrinsic" is a bad explanation because it doesn't really help the developer to use it correctly (and unsafe implies it can only be used correctly in certain ways). To me this particular intrinsic appears to be perfectly safe and there are probably many others like it.

  6. self-assigned this
    on Sep 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions