1+ use bitflags:: bitflags;
2+
13use crate :: ty:: { self , PseudoCanonicalInput , Ty , TyCtxt , TypingEnv } ;
24
3- // TODO(Sa4dUs): it doesn't feel correct for me to place this on `rustc_ast::expand`, will look for a proper location
45pub struct OffloadMetadata {
56 pub payload_size : u64 ,
6- pub mode : TransferKind ,
7+ pub mode : MappingFlags ,
78}
89
9- // TODO(Sa4dUs): add `OMP_MAP_TARGET_PARAM = 0x20` flag only when needed
10- #[ repr( u64 ) ]
11- #[ derive( Debug , Copy , Clone ) ]
12- pub enum TransferKind {
13- FromGpu = 1 ,
14- ToGpu = 2 ,
15- Both = 1 + 2 ,
10+ bitflags ! {
11+ /// Mirrors `OpenMPOffloadMappingFlags` from Clang/OpenMP.
12+ #[ derive( Debug , Copy , Clone ) ]
13+ #[ repr( transparent) ]
14+ pub struct MappingFlags : u64 {
15+ /// No flags.
16+ const NONE = 0x0 ;
17+ /// Allocate memory on the device and move data from host to device.
18+ const TO = 0x01 ;
19+ /// Allocate memory on the device and move data from device to host.
20+ const FROM = 0x02 ;
21+ /// Always perform the requested mapping action, even if already mapped.
22+ const ALWAYS = 0x04 ;
23+ /// Delete the element from the device environment, ignoring ref count.
24+ const DELETE = 0x08 ;
25+ /// The element being mapped is a pointer-pointee pair.
26+ const PTR_AND_OBJ = 0x10 ;
27+ /// The base address should be passed to the target kernel as argument.
28+ const TARGET_PARAM = 0x20 ;
29+ /// The runtime must return the device pointer.
30+ const RETURN_PARAM = 0x40 ;
31+ /// The reference being passed is a pointer to private data.
32+ const PRIVATE = 0x80 ;
33+ /// Pass the element by value.
34+ const LITERAL = 0x100 ;
35+ /// Implicit map (generated by compiler, not explicit in code).
36+ const IMPLICIT = 0x200 ;
37+ /// Hint to allocate memory close to the target device.
38+ const CLOSE = 0x400 ;
39+ /// Reserved (0x800 in OpenMP for XLC compatibility).
40+ const RESERVED = 0x800 ;
41+ /// Require that the data is already allocated on the device.
42+ const PRESENT = 0x1000 ;
43+ /// Increment/decrement a separate ref counter (OpenACC compatibility).
44+ const OMPX_HOLD = 0x2000 ;
45+ /// Used for non-contiguous list items in target update.
46+ const NON_CONTIG = 0x100000000000 ;
47+ /// 16 MSBs indicate membership in a struct.
48+ const MEMBER_OF = 0xffff000000000000 ;
49+ }
1650}
1751
1852impl OffloadMetadata {
19- pub fn new ( payload_size : u64 , mode : TransferKind ) -> Self {
20- OffloadMetadata { payload_size, mode }
21- }
22-
2353 pub fn from_ty < ' tcx > ( tcx : TyCtxt < ' tcx > , ty : Ty < ' tcx > ) -> Self {
2454 OffloadMetadata {
2555 payload_size : get_payload_size ( tcx, ty) ,
26- mode : TransferKind :: from_ty ( tcx, ty) ,
56+ mode : MappingFlags :: from_ty ( tcx, ty) ,
2757 }
2858 }
2959}
3060
31- // TODO (Sa4dUs): WIP, rn we just have a naive logic for references
61+ // FIXME (Sa4dUs): implement a solid logic to determine the payload size
3262fn get_payload_size < ' tcx > ( tcx : TyCtxt < ' tcx > , ty : Ty < ' tcx > ) -> u64 {
3363 match ty. kind ( ) {
3464 ty:: RawPtr ( inner, _) | ty:: Ref ( _, inner, _) => get_payload_size ( tcx, * inner) ,
@@ -43,48 +73,42 @@ fn get_payload_size<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> u64 {
4373 }
4474}
4575
46- impl TransferKind {
47- pub fn from_ty < ' tcx > ( _tcx : TyCtxt < ' tcx > , ty : Ty < ' tcx > ) -> Self {
48- // TODO(Sa4dUs): this logic is probs not fully correct, but it works for now
76+ impl MappingFlags {
77+ fn from_ty < ' tcx > ( _tcx : TyCtxt < ' tcx > , ty : Ty < ' tcx > ) -> Self {
78+ use rustc_ast:: Mutability :: * ;
79+
4980 match ty. kind ( ) {
5081 ty:: Bool
5182 | ty:: Char
5283 | ty:: Int ( _)
5384 | ty:: Uint ( _)
54- | ty:: Float ( _) => TransferKind :: ToGpu ,
55-
56- ty:: Adt ( _, _)
85+ | ty:: Float ( _)
86+ | ty:: Adt ( _, _)
5787 | ty:: Tuple ( _)
58- | ty:: Array ( _, _) => TransferKind :: ToGpu ,
59-
60- ty:: RawPtr ( _, rustc_ast:: Mutability :: Not )
61- | ty:: Ref ( _, _, rustc_ast:: Mutability :: Not ) => TransferKind :: ToGpu ,
62-
63- ty:: RawPtr ( _, rustc_ast:: Mutability :: Mut )
64- | ty:: Ref ( _, _, rustc_ast:: Mutability :: Mut ) => TransferKind :: Both ,
65-
66- ty:: Slice ( _)
67- | ty:: Str
68- | ty:: Dynamic ( _, _) => TransferKind :: Both ,
69-
70- ty:: FnDef ( _, _)
88+ | ty:: Array ( _, _)
89+ | ty:: FnDef ( _, _)
7190 | ty:: FnPtr ( _, _)
7291 | ty:: Closure ( _, _)
7392 | ty:: CoroutineClosure ( _, _)
7493 | ty:: Coroutine ( _, _)
75- | ty:: CoroutineWitness ( _, _) => TransferKind :: ToGpu ,
76-
77- ty:: Alias ( _, _)
94+ | ty:: CoroutineWitness ( _, _)
95+ | ty :: Never
96+ | ty:: Alias ( _, _)
7897 | ty:: Param ( _)
7998 | ty:: Bound ( _, _)
8099 | ty:: Placeholder ( _)
81100 | ty:: Infer ( _)
82- | ty:: Error ( _) => TransferKind :: ToGpu ,
101+ | ty:: Error ( _) => MappingFlags :: TO ,
102+
103+ ty:: RawPtr ( _, Not ) | ty:: Ref ( _, _, Not ) => MappingFlags :: TO ,
104+
105+ ty:: RawPtr ( _, Mut ) | ty:: Ref ( _, _, Mut ) => MappingFlags :: TO | MappingFlags :: FROM ,
106+
107+ ty:: Slice ( _) | ty:: Str | ty:: Dynamic ( _, _) => MappingFlags :: TO | MappingFlags :: FROM ,
83108
84- ty:: Never => TransferKind :: ToGpu ,
85- ty:: Foreign ( _) => TransferKind :: Both ,
86- ty:: Pat ( _, _) => TransferKind :: Both ,
87- ty:: UnsafeBinder ( _) => TransferKind :: Both ,
109+ ty:: Foreign ( _) | ty:: Pat ( _, _) | ty:: UnsafeBinder ( _) => {
110+ MappingFlags :: TO | MappingFlags :: FROM
111+ }
88112 }
89113 }
90114}
0 commit comments