Skip to main content

ruda_kernel/dsl/frontend/operation/
native_memory.rs

1use crate::dsl::prelude::*;
2use crate::dsl::ir::{BinaryOperator, Instruction, ManagedVariable, Operator, UnaryOperator};
3
4/// Obtain the native byte address of a slice element. The address remains valid
5/// only while the underlying allocation is alive and in its owning address space.
6pub fn native_address<T: RudaPrimitive>(_input: &Slice<T>, _index: usize) -> u64 {
7    crate::dsl::unexpanded!()
8}
9
10pub mod native_address {
11    use super::*;
12    pub fn expand<T: RudaPrimitive>(scope: &mut Scope, input: SliceExpand<T, ReadOnly>, index: NativeExpand<usize>) -> NativeExpand<u64> {
13        let (array, offset) = input.__to_raw_parts();
14        let index = if index.expand.ty != usize::as_type(scope) {
15            let converted = scope.create_local(usize::as_type(scope));
16            cast::expand::<usize, usize>(scope, index, converted.clone().into());
17            converted.into()
18        } else { index };
19        let index = add::expand(scope, index, NativeExpand::new(ManagedVariable::Plain(offset)));
20        let ty = u64::as_type(scope);
21        let output = scope.create_local(ty);
22        scope.register(Instruction::new(Operator::NativeAddress(BinaryOperator {
23            lhs: array, rhs: index.expand.consume(),
24        }), *output));
25        output.into()
26    }
27}
28
29/// Read a value at a native byte address. The caller supplies a valid, aligned
30/// address in the current device's address space, with enough readable storage.
31pub fn native_load<T: RudaPrimitive>(_address: u64) -> T {
32    crate::dsl::unexpanded!()
33}
34
35pub mod native_load {
36    use super::*;
37    pub fn expand<T: RudaPrimitive>(scope: &mut Scope, address: NativeExpand<u64>) -> NativeExpand<T> {
38        let ty = T::as_type(scope);
39        let output = scope.create_local(ty);
40        scope.register(Instruction::new(Operator::NativeLoad(UnaryOperator { input: address.expand.consume() }), *output));
41        output.into()
42    }
43}
44
45/// Write through a native byte address. The caller supplies a valid, aligned,
46/// writable allocation and obeys the device's synchronization requirements.
47pub fn native_store<T: RudaPrimitive>(_address: u64, _value: T) {
48    crate::dsl::unexpanded!()
49}
50
51pub mod native_store {
52    use super::*;
53    pub fn expand<T: RudaPrimitive>(scope: &mut Scope, address: NativeExpand<u64>, value: NativeExpand<T>) {
54        scope.register(Instruction::no_out(Operator::NativeStore(BinaryOperator {
55            lhs: address.expand.consume(), rhs: value.expand.consume(),
56        })));
57    }
58}