Source file f_omp_directive.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
(*
   Copyright 2013-2018 RIKEN
   Copyright 2018-2025 Chiba Institude of Technology

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*)

(* Author: Masatomo Hashimoto <m.hashimoto@stair.center> *)

open Label_common

type atomic_sub =
  | Read
  | Write
  | Capture
  | Update

let atomic_sub_to_string = function
  | Read    -> "Read"
  | Write   -> "Write"
  | Capture -> "Capture"
  | Update  -> "Update"

let atomic_sub_to_simple_string = function
  | Read    -> "read"
  | Write   -> "write"
  | Capture -> "capture"
  | Update  -> "update"

let simd_to_string b = if b then "Simd" else ""

let simd_to_simple_string b = if b then " simd" else ""

let seq_cst_to_string b = if b then ".SeqCst" else ""

let seq_cst_to_simple_string b = if b then " seq_cst" else ""

type construct_type =
  | C_parallel
  | C_sections
  | C_do
  | C_taskgroup

let construct_type_to_string = function
  | C_parallel  -> "C_parallel"
  | C_sections  -> "C_sections"
  | C_do        -> "C_do"
  | C_taskgroup -> "C_taskgroup"

let construct_type_to_string_ = function
  | C_parallel  -> "Parallel"
  | C_sections  -> "Sections"
  | C_do        -> "Do"
  | C_taskgroup -> "Taskgroup"

let construct_type_to_simple_string = function
  | C_parallel  -> "parallel"
  | C_sections  -> "sections"
  | C_do        -> "do"
  | C_taskgroup -> "taskgroup"

type t =
  | ERROR
  | Atomic of atomic_sub option * bool (* seq_cst *)
  | Barrier
  | Critical of name option
  | Do of bool (* simd or not *)
  | Flush
  | Master
  | Ordered
  | Parallel
  | ParallelDo of bool (* simd or not *)
  | ParallelSections
  | ParallelWorkshare
  | Section
  | Sections
  | Single
  | Task
  | Taskwait
  | Taskyield
  | Threadprivate
  | Workshare
  | EndAtomic
  | EndCritical of name option
  | EndDo of bool (* simd or not *)
  | EndMaster
  | EndOrdered
  | EndParallel
  | EndSections
  | EndSingle
  | EndTask
  | EndWorkshare
  | EndParallelDo of bool (* simd or not *)
  | EndParallelSections
  | EndParallelWorkshare
(* 4.0 *)
  | Simd         
  | EndSimd
  | DeclareSimd of name
  | Target
  | EndTarget
  | TargetData
  | EndTargetData
  | TargetUpdate
  | DeclareTarget
  | Teams
  | EndTeams
  | Distribute of bool (* simd or not *)
  | EndDistribute of bool (* simd or not *)
  | DistributeParallelDo of bool (* simd or not *)
  | EndDistributeParallelDo of bool (* simd or not *)
  | TargetTeams
  | EndTargetTeams
  | TeamsDistribute of bool (* simd or not *)
  | EndTeamsDistribute of bool (* simd or not *)
  | TargetTeamsDistribute of bool (* simd or not *)
  | EndTargetTeamsDistribute of bool (* simd or not *)
  | TeamsDistributeParallelDo of bool (* simd or not *)
  | EndTeamsDistributeParallelDo of bool (* simd or not *)
  | TargetTeamsDistributeParallelDo of bool (* simd or not *)
  | EndTargetTeamsDistributeParallelDo of bool (* simd or not *)
  | Taskgroup
  | EndTaskgroup
  | Cancel of construct_type
  | CancellationPoint of construct_type
  | DeclareReduction

let to_string = function
  | ERROR                -> "ERROR"
  | Atomic(a_opt, seq_cst) -> "Atomic"^(opt_to_string atomic_sub_to_string ~prefix:"." a_opt)^(seq_cst_to_string seq_cst)
  | Barrier              -> "Barrier"
  | Critical n_opt       -> "Critical"^(string_opt_to_string ~prefix:":" n_opt)
  | Do simd              -> "Do"^(simd_to_string simd)
  | Flush                -> "Flush"
  | Master               -> "Master"
  | Ordered              -> "Ordered"
  | Parallel             -> "Parallel"
  | ParallelDo simd      -> "ParallelDo"^(simd_to_string simd)
  | ParallelSections     -> "ParallelSections"
  | ParallelWorkshare    -> "ParallelWorkshare"
  | Section              -> "Section"
  | Sections             -> "Sections"
  | Single               -> "Single"
  | Task                 -> "Task"
  | Taskwait             -> "Taskwait"
  | Taskyield            -> "Taskyield"
  | Threadprivate        -> "Threadprivate"
  | Workshare            -> "Workshare"
  | EndAtomic            -> "EndAtomic"
  | EndCritical n_opt    -> "EndCritical"^(string_opt_to_string ~prefix:":" n_opt)
  | EndDo simd           -> "EndDo"^(simd_to_string simd)
  | EndMaster            -> "EndMaster"
  | EndOrdered           -> "EndOrdered"
  | EndParallel          -> "EndParallel"
  | EndSections          -> "EndSections"
  | EndSingle            -> "EndSingle"
  | EndTask              -> "EndTask"
  | EndWorkshare         -> "EndWorkshare"
  | EndParallelDo simd   -> "EndParallelDo"^(simd_to_string simd)
  | EndParallelSections  -> "EndParallelSections"
  | EndParallelWorkshare -> "EndParallelWordshare"
(* 4.0 *)
  | Simd                 -> "Simd"
  | EndSimd              -> "EndSimd"
  | DeclareSimd n        -> "DeclareSimd:"^n
  | Target               -> "Target"
  | EndTarget            -> "EndTarget"
  | TargetData           -> "TargetData"
  | EndTargetData        -> "EndTargetData"
  | TargetUpdate         -> "TargetUpdate"
  | DeclareTarget        -> "DeclareTarget"
  | Teams                -> "Teams"
  | EndTeams             -> "EndTeams"
  | Distribute simd      -> "Distribute"^(simd_to_string simd)
  | EndDistribute simd   -> "EndDistribute"^(simd_to_string simd)
  | DistributeParallelDo simd               -> "DistributeParallelDo"^(simd_to_string simd)
  | EndDistributeParallelDo simd            -> "EndDistributeParallelDo"^(simd_to_string simd)
  | TargetTeams                             -> "TargetTeams"
  | EndTargetTeams                          -> "EndTargetTeams"
  | TeamsDistribute simd                    -> "TeamsDistribute"^(simd_to_string simd)
  | EndTeamsDistribute simd                 -> "EndTeamsDistribute"^(simd_to_string simd)
  | TargetTeamsDistribute simd              -> "TargetTeamsDistribute"^(simd_to_string simd)
  | EndTargetTeamsDistribute simd           -> "EndTargetTeamsDistribute"^(simd_to_string simd)
  | TeamsDistributeParallelDo simd          -> "TeamsDistributeParallelDo"^(simd_to_string simd)
  | EndTeamsDistributeParallelDo simd       -> "EndTeamsDistributeParallelDo"^(simd_to_string simd)
  | TargetTeamsDistributeParallelDo simd    -> "TargetTeamsDistributeParallelDo"^(simd_to_string simd)
  | EndTargetTeamsDistributeParallelDo simd -> "EndTargetTeamsDistributeParallelDo"^(simd_to_string simd)
  | Taskgroup           -> "Taskgroup"
  | EndTaskgroup        -> "EndTaskgroup"
  | Cancel c            -> "Cancel:"^(construct_type_to_string c)
  | CancellationPoint c -> "CancellationPoint:"^(construct_type_to_string c)
  | DeclareReduction    -> "DeclareReduction"


let to_simple_string = function
  | ERROR                -> "<error>"
  | Atomic(a_opt, seq_cst) -> "atomic"^(opt_to_string atomic_sub_to_simple_string ~prefix:" " a_opt)^(seq_cst_to_simple_string seq_cst)
  | Barrier              -> "barrier"
  | Critical n_opt       -> "critical"^(string_opt_to_string ~prefix:" (" ~suffix:")" n_opt)
  | Do simd              -> "do"^(simd_to_simple_string simd)
  | Flush                -> "flush"
  | Master               -> "master"
  | Ordered              -> "ordered"
  | Parallel             -> "parallel"
  | ParallelDo simd      -> "parallel do"^(simd_to_simple_string simd)
  | ParallelSections     -> "parallel sections"
  | ParallelWorkshare    -> "parallel workshare"
  | Section              -> "section"
  | Sections             -> "sections"
  | Single               -> "single"
  | Task                 -> "task"
  | Taskwait             -> "taskwait"
  | Taskyield            -> "taskyield"
  | Threadprivate        -> "threadprivate"
  | Workshare            -> "workshare"
  | EndAtomic            -> "end atomic"
  | EndCritical n_opt    -> "end critical"^(string_opt_to_string ~prefix:" (" ~suffix:")" n_opt)
  | EndDo simd           -> "end do"^(simd_to_simple_string simd)
  | EndMaster            -> "end master"
  | EndOrdered           -> "end ordered"
  | EndParallel          -> "end parallel"
  | EndSections          -> "end sections"
  | EndSingle            -> "end single"
  | EndTask              -> "end task"
  | EndWorkshare         -> "end workshare"
  | EndParallelDo simd   -> "end parallel do"^(simd_to_simple_string simd)
  | EndParallelSections  -> "end parallel sections"
  | EndParallelWorkshare -> "end parallel wordshare"
(* 4.0 *)
  | Simd                 -> "simd"
  | EndSimd              -> "end simd"
  | DeclareSimd n        -> "declare simd ("^n^")"
  | Target               -> "target"
  | EndTarget            -> "end target"
  | TargetData           -> "target data"
  | EndTargetData        -> "end target data"
  | TargetUpdate         -> "target update"
  | DeclareTarget        -> "declare target"
  | Teams                -> "teams"
  | EndTeams             -> "end teams"
  | Distribute simd      -> "distribute"^(simd_to_simple_string simd)
  | EndDistribute simd   -> "end distribute"^(simd_to_simple_string simd)
  | DistributeParallelDo simd               -> "distribute parallel do"^(simd_to_simple_string simd)
  | EndDistributeParallelDo simd            -> "end distribute parallel do"^(simd_to_simple_string simd)
  | TargetTeams                             -> "target teams"
  | EndTargetTeams                          -> "end target teams"
  | TeamsDistribute simd                    -> "teams distribute"^(simd_to_simple_string simd)
  | EndTeamsDistribute simd                 -> "end teams distribute"^(simd_to_simple_string simd)
  | TargetTeamsDistribute simd              -> "target teams distribute"^(simd_to_simple_string simd)
  | EndTargetTeamsDistribute simd           -> "end target teams distribute"^(simd_to_simple_string simd)
  | TeamsDistributeParallelDo simd          -> "teams distribute parallel do"^(simd_to_simple_string simd)
  | EndTeamsDistributeParallelDo simd       -> "end teams distribute parallel do"^(simd_to_simple_string simd)
  | TargetTeamsDistributeParallelDo simd    -> "target teams distribute parallel do"^(simd_to_simple_string simd)
  | EndTargetTeamsDistributeParallelDo simd -> "end target teams distribute parallel do"^(simd_to_simple_string simd)
  | Taskgroup           -> "taskgroup"
  | EndTaskgroup        -> "end taskgroup"
  | Cancel c            -> "cancel "^(construct_type_to_simple_string c)
  | CancellationPoint c -> "cancellation point "^(construct_type_to_simple_string c)
  | DeclareReduction    -> "declare reduction"


let to_tag = function
  | ERROR                -> "ERROR", []
  | Atomic(a_opt, seq_cst) -> "OmpAtomic"^(opt_to_string atomic_sub_to_string a_opt)^(seq_cst_to_string seq_cst), []
  | Barrier              -> "OmpBarrier", []
  | Critical n_opt       -> "OmpCritical", (string_opt_to_attr name_attr_name n_opt)
  | Do simd              -> "OmpDo"^(simd_to_string simd), []
  | Flush                -> "OmpFlush", []
  | Master               -> "OmpMaster", []
  | Ordered              -> "OmpOrdered", []
  | Parallel             -> "OmpParallel", []
  | ParallelDo simd      -> "OmpParallelDo"^(simd_to_string simd), []
  | ParallelSections     -> "OmpParallelSections", []
  | ParallelWorkshare    -> "OmpParallelWorkshare", []
  | Section              -> "OmpSection", []
  | Sections             -> "OmpSections", []
  | Single               -> "OmpSingle", []
  | Task                 -> "OmpTask", []
  | Taskwait             -> "OmpTaskwait", []
  | Taskyield            -> "OmpTaskyield", []
  | Threadprivate        -> "OmpThreadprivate", []
  | Workshare            -> "OmpWorkshare", []
  | EndAtomic            -> "OmpEndAtomic", []
  | EndCritical n_opt    -> "OmpEndCritical", (string_opt_to_attr name_attr_name n_opt)
  | EndDo simd           -> "OmpEndDo"^(simd_to_string simd), []
  | EndMaster            -> "OmpEndMaster", []
  | EndOrdered           -> "OmpEndOrdered", []
  | EndParallel          -> "OmpEndParallel", []
  | EndSections          -> "OmpEndSections", []
  | EndSingle            -> "OmpEndSingle", []
  | EndTask              -> "OmpEndTask", []
  | EndWorkshare         -> "OmpEndWorkshare", []
  | EndParallelDo simd   -> "OmpEndParallelDo"^(simd_to_string simd), []
  | EndParallelSections  -> "OmpEndParallelSections", []
  | EndParallelWorkshare -> "OmpEndParallelWordshare", []
(* 4.0 *)
  | Simd                 -> "OmpSimd", []
  | EndSimd              -> "OmpEndSimd", []
  | DeclareSimd n        -> "OmpDeclareSimd", [name_attr_name,n]
  | Target               -> "OmpTarget", []
  | EndTarget            -> "OmpEndTarget", []
  | TargetData           -> "OmpTargetData", []
  | EndTargetData        -> "OmpEndTargetData", []
  | TargetUpdate         -> "OmpTargetUpdate", []
  | DeclareTarget        -> "OmpDeclareTarget", []
  | Teams                -> "OmpTeams", []
  | EndTeams             -> "OmpEndTeams", []
  | Distribute simd      -> "OmpDistribute"^(simd_to_string simd), []
  | EndDistribute simd   -> "OmpEndDistribute"^(simd_to_string simd), []
  | DistributeParallelDo simd               -> "OmpDistributeParallelDo"^(simd_to_string simd), []
  | EndDistributeParallelDo simd            -> "OmpEndDistributeParallelDo"^(simd_to_string simd), []
  | TargetTeams                             -> "OmpTargetTeams", []
  | EndTargetTeams                          -> "OmpEndTargetTeams", []
  | TeamsDistribute simd                    -> "OmpTeamsDistribute"^(simd_to_string simd), []
  | EndTeamsDistribute simd                 -> "OmpEndTeamsDistribute"^(simd_to_string simd), []
  | TargetTeamsDistribute simd              -> "OmpTargetTeamsDistribute"^(simd_to_string simd), []
  | EndTargetTeamsDistribute simd           -> "OmpEndTargetTeamsDistribute"^(simd_to_string simd), []
  | TeamsDistributeParallelDo simd          -> "OmpTeamsDistributeParallelDo"^(simd_to_string simd), []
  | EndTeamsDistributeParallelDo simd       -> "OmpEndTeamsDistributeParallelDo"^(simd_to_string simd), []
  | TargetTeamsDistributeParallelDo simd    -> "OmpTargetTeamsDistributeParallelDo"^(simd_to_string simd), []
  | EndTargetTeamsDistributeParallelDo simd -> "OmpEndTargetTeamsDistributeParallelDo"^(simd_to_string simd), []
  | Taskgroup           -> "OmpTaskgroup", []
  | EndTaskgroup        -> "OmpEndTaskgroup", []
  | Cancel c            -> "OmpCancel", ["type",construct_type_to_string_ c]
  | CancellationPoint c -> "OmpCancellationPoint", ["type",construct_type_to_string_ c]
  | DeclareReduction    -> "OmpDeclareReduction", []


let get_name = function
  | Critical n_opt
  | EndCritical n_opt -> 
      begin
        match n_opt with
        | Some n -> n
        | _ -> raise Not_found
      end
  | _ -> raise Not_found

let get_name_opt = function
  | Critical n_opt 
  | EndCritical n_opt
    -> n_opt
  | _ -> None

let is_specification_part = function
  | ERROR
  | DeclareSimd _
  | DeclareTarget
  | Threadprivate
  | DeclareReduction
    -> true

  | _ -> false

let is_execution_part = function
  | ERROR
  | Atomic _
  | EndAtomic
  | Critical _
  | EndCritical _
  | Do _
  | EndDo _
  | Master
  | EndMaster
  | Ordered
  | EndOrdered
  | Parallel
  | EndParallel
  | Sections
  | Section
  | EndSections
  | Single
  | EndSingle
  | Task
  | EndTask
  | Workshare
  | EndWorkshare
  | ParallelDo _
  | EndParallelDo _
  | ParallelSections
  | EndParallelSections
  | ParallelWorkshare
  | EndParallelWorkshare
  | Simd
  | EndSimd
  | Target
  | EndTarget
  | Teams
  | EndTeams
  | Distribute _
  | EndDistribute _
  | DistributeParallelDo _
  | EndDistributeParallelDo _
  | TargetTeams
  | EndTargetTeams
  | TeamsDistribute _
  | EndTeamsDistribute _
  | TargetTeamsDistribute _
  | EndTargetTeamsDistribute _
  | TeamsDistributeParallelDo _
  | EndTeamsDistributeParallelDo _
  | TargetTeamsDistributeParallelDo _
  | EndTargetTeamsDistributeParallelDo _
  | Taskgroup
  | EndTaskgroup

  | Barrier
  | Flush
  | Taskwait
  | Taskyield
  | TargetData
  | EndTargetData
  | TargetUpdate
  | Cancel _
  | CancellationPoint _
    -> true

  | _ -> false

let anonymize = function
  | Atomic(_, _)  -> Atomic(None, false)
  | Critical _    -> Critical None
  | EndCritical _ -> EndCritical None
  | l -> l