[Migrated] Language feature: barriers
Nessuno ha ancora preso questa issue.
- Lingua principale
- Rust
- Stelle
- 3.4k
- Fork
- 125
- Metriche di merge delle PR
- Nessuna PR unita negli ultimi 30g
Descrizione
Issue automatically imported from old repo: https://github.com/EmbarkStudios/rust-gpu/issues/8
Old labels: t: design
Originally creatd by Jasper-Bekkers on 2020-08-18T17:33:10Z
We want to have the same types of safety on the GPU as we do on the CPU. One of those areas that make it easy to introduce race conditions is with memory barriers. HLSL and GLSL require the programmer to explicitly put them in the right locations without any formal verifiction.
This is an attempt at solving that within the Rust type system (attempt still has racy bugs and is largely incomplete).
use core::ops::Deref;
use core::ops::DerefMut;
use core::ops::Index;
use core::ops::IndexMut;
struct MyFoo {
test: GroupSharedArray<u32>,
}
struct GroupSharedArray<T>
where
T: Default + Copy,
{
data: Box<[T]>,
size: usize,
}
impl<T: Default + Copy> GroupSharedArray<T> {
fn new() -> Self {
Self {
size: 100,
data: Box::new([T::default(); 100]),
}
}
}
struct GroupSharedWriter<T> {
data: T,
}
impl<T> Deref for GroupSharedWriter<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.data
}
}
impl<T> DerefMut for GroupSharedWriter<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.data
}
}
impl<T> GroupSharedWriter<T> {
fn new(data: T) -> Self {
Self { data }
}
fn barrier(self) -> GroupSharedReader<T> {
GroupSharedReader { data: self.data }
}
}
struct GroupSharedReader<T> {
data: T,
}
impl<T> Deref for GroupSharedReader<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.data
}
}
impl<T> GroupSharedReader<T> {
fn barrier(self) -> GroupSharedWriter<T> {
GroupSharedWriter { data: self.data }
}
}
impl<T: Default + Copy> Index<Uniform<u32>> for GroupSharedArray<T> {
type Output = T;
fn index(&self, index: Uniform<u32>) -> &Self::Output {
&self.data[index.data as usize]
}
}
impl<T: Default + Copy> IndexMut<Uniform<u32>> for GroupSharedArray<T> {
fn index_mut(&mut self, index: Uniform<u32>) -> &mut Self::Output {
&mut self.data[index.data as usize]
}
}
struct Uniform<T> {
data: T,
}
impl<T> Uniform<T> {
fn new(data: T) -> Self {
Self { data }
}
}
fn thread_idx() -> Uniform<u32> {
Uniform::new(0)
}
fn main() {
let mut data = GroupSharedWriter::new(MyFoo {
test: GroupSharedArray::new(),
}); // new does barrier
data.test[thread_idx()] = thread_idx().data;
data.test[thread_idx()] = 666; // should be illegal
let value = data.test[thread_idx()]; // should be illegal
//data.test[0] = thread_idx().data; // what happens in this case? it's unsafe/unsound. race cnd over contents of `test`
let other_data = data.barrier(); // `barrier` return GroupSharedReader
// some race-y cases handled correctly:
// data.test[thread_idx()] = 666; // is correctly marked illegal
// other_data.test[thread_idx()] = 666; // is correctly marked illegal
let test = other_data.data.test[thread_idx()];
}
// tl;dr
// - use move semantics to enforce barriers betwen reading and writing
// - broken right now because DerefMut and IndexMut require Deref and Index to be implemented
// - broken right now because we can write to the same location multiple times (data race)
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia esaminando GroupSharedArray, GroupSharedWriter, GroupSharedReader e l'esempio di barrier nell'issue. Analizza la limitazione dichiarata di DerefMut/IndexMut e i casi che consentono scritture o letture ripetute; il lavoro è completo quando esiste un design deciso e solido per imporre le barrier, invece dell'attuale prototipo incompleto.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- rust
- Ambito
- compilers
- Tipo di issue
- Funzionalità
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Ferma
- Chiarezza
- Da chiarire
- Idoneità per principianti
- 25/100