[flang-commits] [flang] [flang] Implement !DIR$ VECTOR ALWAYS (PR #93830)
Kiran Chandramohan via flang-commits
flang-commits at lists.llvm.org
Sun Jun 9 09:54:38 PDT 2024
kiranchandramohan wrote:
Since there are some nontrivial changes here and it is the first time we are lowering directives, it is probably good to write a document that goes along with this patch. I am providing a sketch, you may choose to extend this or use a different one.
```
Introduction
Directives are commonly used in Fortran programs to specify additional actions to be performed by the compiler. The directives are always specified with the `!dir$` or `cdir$` prefix.
Loop Directives
Directives on loops are used to specify additional transformation to be performed by the compiler like enabling vectorisation, unrolling, interchange etc.
Vector Directive
This directive controls various aspects related to vectorisation. To begin with we will only support the most commonly used variant of the directive, vector always.
Vector Always Directive
This directive instructs the compiler to perform vectorisation without cost-model considerations.
Implementation Details
Parsing
The parser is model trivially to accept the vector always directive. In future this will be extended to handle other variants of the vector directive.
Semantics
Gives an error if the associated loop cannot be found for a loop directive.
Correct the parse tree if an executable directive falls in the specification section. Such directives are moved to the executable section.
Lowering
Evaluation is extended with a new field called dirs for representing Compiler directives associated with the Evaluation. While lowering the `CompilerDirective` the associated Do Loop's evaluation is found and the directive is added to it. This information is used during the lowering of the loop.
Representation in LLVM
Metadata (`llvm.loop.vectorize.enable`) is used in LLVM to inform the optimizer that a loop can be vectorized without considering its cost-model.
Representation in MLIR
LLVM dialect models this by an attribute called LoopAnnotation Attribute. The attribute can be added to the latch of the loop in LLVM and cf dialects.
Representation in HLFIR/FIR
Unstructured Loops
Similar to MLIR representation. The LoopAnnotation Attribute will be added during lowering.
fir.do_loop
The fir.do_loop will be extended with a LoopAnnotation Attribute. This attribute will be added to the latch during the ControlFlowConverter Transformation/Conversion pass.
Array Expressions
TBD
Testing
The patch will be tested with an integration lit test that tests the whole feature (from source to LLVM IR).
Lit tests will be separately written for parsing, semantics and lowering stages.
```
https://github.com/llvm/llvm-project/pull/93830
More information about the flang-commits
mailing list