1 <!--===- docs/OpenMP-semantics.md
3 Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 See https://llvm.org/LICENSE.txt for license information.
5 SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
9 # OpenMP Semantic Analysis
18 1. Define and document the parse tree representation for
19 * Directives (listed below)
20 * Clauses (listed below)
22 1. All the directives and clauses need source provenance for messages
23 1. Define and document how an OpenMP directive in the parse tree
24 will be represented as the parent of the statement(s)
25 to which the directive applies.
26 The parser itself will not be able to construct this representation;
27 there will be subsequent passes that do so
28 just like for example _do-stmt_ and _do-construct_.
29 1. Define and document the symbol table extensions
30 1. Define and document the module file extensions
35 OpenMP divides directives into three categories as follows.
36 The directives that are in the same categories share some characteristics.
40 #### Declarative directives
42 An OpenMP directive may only be placed in a declarative context.
43 A declarative directive results in one or more declarations only;
44 it is not associated with the immediate execution of any user code.
46 List of existing ones:
53 There is a parser node for each of these directives and
54 the parser node saves information associated with the directive,
56 the name of the procedure-name in the `declare simd` directive.
58 Each parse tree node keeps source provenance,
59 one for the directive name itself and
60 one for the entire directive starting from the directive name.
62 A top-level class, `OpenMPDeclarativeConstruct`,
63 holds all four of the node types as discriminated unions
64 along with the source provenance for the entire directive
65 starting from `!$OMP`.
68 `OpenMPDeclarativeConstruct` is part
69 of the `SpecificationConstruct` and `SpecificationPart`
71 a declarative directive can only be placed in the specification part
74 All the `Names` or `Designators` associated
75 with the declarative directive will be resolved in later phases.
77 #### Executable directives
79 An OpenMP directive that is **not** declarative.
80 That is, it may only be placed in an executable context.
81 It contains stand-alone directives and constructs
82 that are associated with code blocks.
83 The stand-alone directive is described in the next section.
85 The constructs associated with code blocks listed below
86 share a similar structure:
87 _Begin Directive_, _Clause List_, _Code Block_, _End Directive_.
88 The _End Directive_ is optional for constructs
89 like Loop-associated constructs.
91 * Block-associated constructs (`OpenMPBlockConstruct`)
92 * Loop-associated constructs (`OpenMPLoopConstruct`)
93 * Atomic construct (`OpenMPAtomicConstruct`)
94 * Sections Construct (`OpenMPSectionsConstruct`,
95 contains Sections/Parallel Sections constructs)
96 * Critical Construct (`OpenMPCriticalConstruct`)
98 A top-level class, `OpenMPConstruct`,
99 includes stand-alone directive and constructs
100 listed above as discriminated unions.
102 In the `parse-tree.h`, `OpenMPConstruct` is an element
103 of the `ExecutableConstruct`.
105 All the `Names` or `Designators` associated
106 with the executable directive will be resolved in Semantic Analysis.
108 When the backtracking parser can not identify the associated code blocks,
109 the parse tree will be rewritten later in the Semantics Analysis.
111 #### Stand-alone Directives
113 An OpenMP executable directive that has no associated user code
114 except for that which appears in clauses in the directive.
116 List of existing ones:
128 A higher-level class is created for each category
129 which contains directives listed above that share a similar structure:
130 * OpenMPSimpleStandaloneConstruct
131 (taskyield, barrier, taskwait,
132 target enter/exit data, target update, ordered)
133 * OpenMPFlushConstruct
134 * OpenMPCancelConstruct
135 * OpenMPCancellationPointConstruct
137 A top-level class, `OpenMPStandaloneConstruct`,
138 holds all four of the node types as discriminated unions
139 along with the source provenance for the entire directive.
140 Also, each parser node for the stand-alone directive saves
141 the source provenance for the directive name itself.
145 Each clause represented as a distinct class in `parse-tree.h`.
146 A top-level class, `OmpClause`,
147 includes all the clauses as discriminated unions.
148 The parser node for `OmpClause` saves the source provenance
149 for the entire clause.
151 All the `Names` or `Designators` associated
152 with the clauses will be resolved in Semantic Analysis.
154 Note that the backtracking parser will not validate
155 that the list of clauses associated
156 with a directive is valid other than to make sure they are well-formed.
158 the parser does not check that
159 the association between directive and clauses is correct
160 nor check that the values in the directives or clauses are correct.
161 These checks are deferred to later phases of semantics to simplify the parser.
163 ## Symbol Table Extensions for OpenMP
165 Name resolution can be impacted by the OpenMP code.
166 In addition to the regular steps to do the name resolution,
167 new scopes and symbols may need to be created
168 when encountering certain OpenMP constructs.
169 This section describes the extensions
170 for OpenMP during Symbol Table construction.
172 OpenMP uses the fork-join model of parallel execution and
173 all OpenMP threads have access to
174 a _shared_ memory place to store and retrieve variables
175 but each thread can also have access to
176 its _threadprivate_ memory that must not be accessed by other threads.
178 For the directives and clauses that can control the data environments,
179 compiler needs to determine two kinds of _access_
180 to variables used in the directive’s associated structured block:
181 **shared** and **private**.
182 Each variable referenced in the structured block
183 has an original variable immediately outside of the OpenMP constructs.
184 Reference to a shared variable in the structured block
185 becomes a reference to the original variable.
186 However, each private variable referenced in the structured block,
187 a new version of the original variable (of the same type and size)
188 will be created in the threadprivate memory.
190 There are exceptions that directives/clauses
191 need to create a new `Symbol` without creating a new `Scope`,
193 when encountering each of the data environment controlling directives
194 (discussed in the following sections),
195 a new `Scope` will be created.
196 For each private variable referenced in the structured block,
197 a new `Symbol` is created out of the original variable
198 and the new `Symbol` is associated
199 with original variable’s `Symbol` via `HostAssocDetails`.
200 A new set of OpenMP specific flags are added
201 into `Flag` class in `symbol.h` to indicate the types of
203 data-sharing attributes,
204 and data-mapping attributes
205 in the OpenMP data environments.
207 ### New Symbol without new Scope
209 OpenMP directives that require new `Symbol` to be created
210 but not new `Scope` are listed in the following table
211 in terms of the Symbol Table extensions for OpenMP:
215 <td rowspan="2" colspan="2" >Directives/Clauses
217 <td rowspan="2" >Create New
223 <td colspan="2" >Add Flag
233 <td rowspan="4" >Declarative Directives
235 <td>declare simd [(proc-name)]
239 <td>The name of the enclosing function, subroutine, or interface body
240 to which it applies, or proc-name
250 <td>The name of the enclosing function, subroutine, or interface body
257 <td>threadprivate(list)
261 <td>named variables and named common blocks
267 <td>declare reduction
271 <td>reduction-identifier
273 <td>OmpDeclareReduction
277 <td>Stand-alone directives
283 <td>variable, array section or common block name
289 <td colspan="2" >critical [(name)]
293 <td>name (user-defined identifier)
299 <td colspan="2" >if ([ directive-name-modifier :] scalar-logical-expr)
303 <td>directive-name-modifier
313 * Discussed in “Module File Extensions for OpenMP” section
316 ### New Symbol with new Scope
318 For the following OpenMP regions:
321 * `target data` regions
325 * task generating regions (created by `task` or `taskloop` constructs)
326 * worksharing regions
327 (created by `do`, `sections`, `single`, or `workshare` constructs)
329 A new `Scope` will be created
330 when encountering the above OpenMP constructs
331 to ensure the correct data environment during the Code Generation.
332 To determine whether a variable referenced in these regions
333 needs the creation of a new `Symbol`,
334 all the data-sharing attribute rules
335 described in OpenMP Spec [2.15.1] apply during the Name Resolution.
336 The available data-sharing attributes are:
341 and **_lastprivate_**.
342 The attribute is represented as `Flag` in the `Symbol` object.
344 More details are listed in the following table:
348 <td rowspan="2" >Attribute
350 <td rowspan="2" >Create New Symbol
352 <td colspan="2" >Add Flag
366 <td>Original variable
433 To determine the right data-sharing attribute,
434 OpenMP defines that the data-sharing attributes
435 of variables that are referenced in a construct can be
436 _predetermined_, _explicitly determined_, or _implicitly determined_.
438 #### Predetermined data-sharing attributes
440 * Assumed-size arrays are **shared**
441 * The loop iteration variable(s)
442 in the associated _do-loop(s)_ of a
446 or _distributeconstruct_
448 * A loop iteration variable
449 for a sequential loop in a _parallel_ or task generating construct
450 is **private** in the innermost such construct that encloses the loop
451 * Implied-do indices and _forall_ indices are **private**
452 * The loop iteration variable in the associated _do-loop_
453 of a _simd_ construct with just one associated _do-loop_
454 is **linear** with a linear-step
455 that is the increment of the associated _do-loop_
456 * The loop iteration variables in the associated _do-loop(s)_ of a _simd_
457 construct with multiple associated _do-loop(s)_ are **lastprivate**
459 #### Explicitly determined data-sharing attributes
461 Variables with _explicitly determined_ data-sharing attributes are:
463 * Variables are referenced in a given construct
464 * Variables are listed in a data-sharing attribute clause on the construct.
466 The data-sharing attribute clauses are:
468 (discussed in “Implicitly determined data-sharing attributes”)
472 * _firstprivate_ clause
473 * _lastprivate_ clause
475 (new `Symbol` created with the flag `OmpReduction` set)
477 Note that variables with _predetermined_ data-sharing attributes
478 may not be listed (with exceptions) in data-sharing attribute clauses.
480 #### Implicitly determined data-sharing attributes
482 Variables with implicitly determined data-sharing attributes are:
484 * Variables are referenced in a given construct
485 * Variables do not have _predetermined_ data-sharing attributes
486 * Variables are not listed in a data-sharing attribute clause
489 Rules for variables with _implicitly determined_ data-sharing attributes:
491 * In a _parallel_ construct, if no _default_ clause is present,
492 these variables are **shared**
493 * In a task generating construct,
494 if no _default_ clause is present,
495 a variable for which the data-sharing attribute
496 is not determined by the rules above
497 and that in the enclosing context is determined
498 to be shared by all implicit tasks
499 bound to the current team is **shared**
500 * In a _target_ construct,
501 variables that are not mapped after applying data-mapping attribute rules
502 (discussed later) are **firstprivate**
503 * In an orphaned task generating construct,
504 if no _default_ clause is present, dummy arguments are **firstprivate**
505 * In a task generating construct, if no _default_ clause is present,
506 a variable for which the data-sharing attribute is not determined
507 by the rules above is **firstprivate**
508 * For constructs other than task generating constructs or _target_ constructs,
509 if no _default_ clause is present,
510 these variables reference the variables with the same names
511 that exist in the enclosing context
512 * In a _parallel_, _teams_, or task generating construct,
513 the data-sharing attributes of these variables are determined
514 by the _default_ clause, if present:
516 clause causes all variables referenced in the construct
517 that have _implicitly determined_ data-sharing attributes
520 clause causes all variables referenced in the construct
521 that have _implicitly determined_ data-sharing attributes
523 * _default(firstprivate)_
524 clause causes all variables referenced in the construct
525 that have _implicitly determined_ data-sharing attributes
526 to be **firstprivate**
528 clause requires that each variable
529 that is referenced in the construct,
530 and that does not have a _predetermined_ data-sharing attribute,
531 must have its data-sharing attribute _explicitly determined_
532 by being listed in a data-sharing attribute clause
535 ### Data-mapping Attribute
537 When encountering the _target data_ and _target_ directives,
538 the data-mapping attributes of any variable referenced in a target region
539 will be determined and represented as `Flag` in the `Symbol` object
541 No `Symbol` or `Scope` will be created.
543 However, there are some exceptions for this, Pointers that appear in a
544 use_device_ptr clause are privatized and the device pointers to the
545 corresponding list items in the device data environment are assigned into the
546 private versions so it is best to follow the representation for privatised
547 variables i.e represent them with a new Symbol and `OmpUseDevicePtr` flag.
548 If a list item that appears in a use_device_addr clause has corresponding
549 storage in the device data environment, references to the list item in the
550 associated structured block are converted into references to the corresponding
551 list item so following the same i.e. represent them with a new Symbol and
552 `OmpUseDeviceAddr` flag.
554 The basic steps to determine the data-mapping attribute are:
556 1. If _map_ clause is present,
557 the data-mapping attribute is determined by the _map-type_
558 on the clause and its corresponding `Flag` are listed below:
563 data-mapping attribute
582 (default if map-type is not present)
584 <td>OmpMapTo & OmpMapFrom
607 2. Otherwise, the following data-mapping rules apply
608 for variables referenced in a _target_ construct
609 that are _not_ declared in the construct and
610 do not appear in data-sharing attribute or map clauses:
611 * If a variable appears in a _to_ or _link_ clause
612 on a _declare target_ directive then it is treated
613 as if it had appeared in a _map_ clause with a _map-type_ of **tofrom**
614 3. Otherwise, the following implicit data-mapping attribute rules apply:
615 * If a _defaultmap(tofrom:scalar)_ clause is _not_ present
616 then a scalar variable is not mapped,
617 but instead has an implicit data-sharing attribute of **firstprivate**
618 * If a _defaultmap(tofrom:scalar)_ clause is present
619 then a scalar variable is treated as if it had appeared
620 in a map clause with a map-type of **tofrom**
621 * If a variable is not a scalar
622 then it is treated as if it had appeared in a map clause
623 with a _map-type_ of **tofrom**
625 After the completion of the Name Resolution phase,
626 all the data-sharing or data-mapping attributes marked for the `Symbols`
627 may be used later in the Semantics Analysis and in the Code Generation.
629 ## Module File Extensions for OpenMP
631 After the successful compilation of modules and submodules
632 that may contain the following Declarative Directives,
633 the entire directive starting from `!$OMP` needs to be written out
634 into `.mod` files in their corresponding Specification Part:
636 * _declare simd_ or _declare target_
638 In the “New Symbol without new Scope” section,
639 we described that when encountering these two declarative directives,
640 new `Flag` will be applied to the Symbol of the name of
641 the enclosing function, subroutine, or interface body to
642 which it applies, or proc-name.
643 This `Flag` should be part of the API information
644 for the given subroutine or function
646 * _declare reduction_
648 The _reduction-identifier_ in this directive
649 can be use-associated or host-associated.
650 However, it will not act like other Symbols
651 because user may have a reduction name
652 that is the same as a Fortran entity name in the same scope.
653 Therefore a specific data structure needs to be created
654 to save the _reduction-identifier_ information
655 in the Scope and this directive needs to be written into `.mod` files
657 ## Phases of OpenMP Analysis
659 1. Create the parse tree for OpenMP
660 1. Add types for directives and clauses
661 1. Add type(s) that will be used for directives
662 2. Add type(s) that will be used for clauses
663 3. Add other types, e.g. wrappers or other containers
664 4. Use std::variant to encapsulate meaningful types
665 2. Implemented in the parser for OpenMP (openmp-grammar.h)
666 2. Create canonical nesting
667 1. Restructure parse tree to reflect the association
668 of directives and stmts
669 1. Associate `OpenMPLoopConstruct`
670 with `DoConstruct` and `OpenMPEndLoopDirective`
671 1. Investigate, and perhaps reuse,
672 the algorithm used to restructure do-loops
673 2. Add a pass near the code that restructures do-loops;
674 but do not extend the code that handles do-loop for OpenMP;
675 keep this code separate.
676 3. Report errors that prevent restructuring
677 (e.g. loop directive not followed by loop)
678 We should abort in case of errors
679 because there is no point to perform further checks
680 if it is not a legal OpenMP construct
681 3. Validate the structured-block
682 1. Structured-block is a block of executable statements
683 1. Single entry and single exit
684 1. Access to the structured block must not be the result of a branch
685 1. The point of exit cannot be a branch out of the structured block
686 4. Check that directive and clause combinations are legal
687 1. Begin and End directive should match
688 1. Simply check that the clauses are allowed by the directives
689 1. Write as a separate pass for simplicity and correctness of the parse tree
690 5. Write parse tree tests
691 1. At this point, the parse tree should be perfectly formed
692 1. Write tests that check for correct form and provenance information
693 1. Write tests for errors that can occur during the restructuring
694 6. Scope, symbol tables, and name resolution
695 1. Update the existing code to handle names and scopes introduced by OpenMP
696 1. Write tests to make sure names are properly implemented
697 7. Check semantics that is specific to each directive
698 1. Validate the directive and its clauses
699 1. Some clause checks require the result of name resolution,
700 i.e. “A list item may appear in a _linear_ or _firstprivate_ clause
703 Validate the nested statement for legality in the scope of the directive
704 1. Check the nesting of regions [OpenMP 4.5 spec 2.17]
705 8. Module file utilities
706 1. Write necessary OpenMP declarative directives to `.mod` files
707 2. Update the existing code
708 to read available OpenMP directives from the `.mod` files