Skip to content

Commit 601a958

Browse files
[Flang] Add support for assume_aligned directive (#81747)
This adds the parsing (and unparse) of the compiler drective assume_aligned. The compiler will issue a warning that the directive is ignored.
1 parent 06bd74b commit 601a958

File tree

6 files changed

+85
-1
lines changed

6 files changed

+85
-1
lines changed

flang/docs/Directives.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,9 @@ A list of non-standard directives supported by Flang
3030
end
3131
end interface
3232
```
33+
* `!dir$ assume_aligned desginator:alignment`, where designator is a variable,
34+
maybe with array indices, and alignment is what the compiler should assume the
35+
alignment to be. E.g A:64 or B(1,1,1):128. The alignment should be a power of 2,
36+
and is limited to 256.
37+
[This directive is currently recognised by the parser, but not
38+
handled by the other parts of the compiler].

flang/include/flang/Parser/dump-parse-tree.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ class ParseTreeDumper {
205205
NODE(parser, CompilerDirective)
206206
NODE(CompilerDirective, IgnoreTKR)
207207
NODE(CompilerDirective, LoopCount)
208+
NODE(CompilerDirective, AssumeAligned)
208209
NODE(CompilerDirective, NameValue)
209210
NODE(parser, ComplexLiteralConstant)
210211
NODE(parser, ComplexPart)

flang/include/flang/Parser/parse-tree.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3309,12 +3309,18 @@ struct CompilerDirective {
33093309
struct LoopCount {
33103310
WRAPPER_CLASS_BOILERPLATE(LoopCount, std::list<std::uint64_t>);
33113311
};
3312+
struct AssumeAligned {
3313+
TUPLE_CLASS_BOILERPLATE(AssumeAligned);
3314+
std::tuple<common::Indirection<Designator>, uint64_t> t;
3315+
};
33123316
struct NameValue {
33133317
TUPLE_CLASS_BOILERPLATE(NameValue);
33143318
std::tuple<Name, std::optional<std::uint64_t>> t;
33153319
};
33163320
CharBlock source;
3317-
std::variant<std::list<IgnoreTKR>, LoopCount, std::list<NameValue>> u;
3321+
std::variant<std::list<IgnoreTKR>, LoopCount, std::list<AssumeAligned>,
3322+
std::list<NameValue>>
3323+
u;
33183324
};
33193325

33203326
// (CUDA) ATTRIBUTE(attribute) [::] name-list

flang/lib/Parser/Fortran-parsers.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1268,9 +1268,13 @@ constexpr auto ignore_tkr{
12681268
constexpr auto loopCount{
12691269
"DIR$ LOOP COUNT" >> construct<CompilerDirective::LoopCount>(
12701270
parenthesized(nonemptyList(digitString64)))};
1271+
constexpr auto assumeAligned{"DIR$ ASSUME_ALIGNED" >>
1272+
optionalList(construct<CompilerDirective::AssumeAligned>(
1273+
indirect(designator), ":"_tok >> digitString64))};
12711274
TYPE_PARSER(beginDirective >>
12721275
sourced(construct<CompilerDirective>(ignore_tkr) ||
12731276
construct<CompilerDirective>(loopCount) ||
1277+
construct<CompilerDirective>(assumeAligned) ||
12741278
construct<CompilerDirective>(
12751279
"DIR$" >> many(construct<CompilerDirective::NameValue>(name,
12761280
maybe(("="_tok || ":"_tok) >> digitString64))))) /

flang/lib/Parser/unparse.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1819,6 +1819,11 @@ class UnparseVisitor {
18191819
[&](const CompilerDirective::LoopCount &lcount) {
18201820
Walk("!DIR$ LOOP COUNT (", lcount.v, ", ", ")");
18211821
},
1822+
[&](const std::list<CompilerDirective::AssumeAligned>
1823+
&assumeAligned) {
1824+
Word("!DIR$ ASSUME_ALIGNED ");
1825+
Walk(" ", assumeAligned, ", ");
1826+
},
18221827
[&](const std::list<CompilerDirective::NameValue> &names) {
18231828
Walk("!DIR$ ", names, " ");
18241829
},
@@ -1841,6 +1846,11 @@ class UnparseVisitor {
18411846
Walk(std::get<Name>(x.t));
18421847
Walk("=", std::get<std::optional<std::uint64_t>>(x.t));
18431848
}
1849+
void Unparse(const CompilerDirective::AssumeAligned &x) {
1850+
Walk(std::get<common::Indirection<Designator>>(x.t));
1851+
Put(":");
1852+
Walk(std::get<uint64_t>(x.t));
1853+
}
18441854

18451855
// OpenACC Directives & Clauses
18461856
void Unparse(const AccAtomicCapture &x) {

flang/test/Parser/assume-aligned.f90

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
! RUN: %flang_fc1 -fdebug-unparse-no-sema %s 2>&1 | FileCheck %s
2+
3+
SUBROUTINE aa(a, nn)
4+
IMPLICIT NONE
5+
INTEGER, INTENT(IN) :: nn
6+
COMPLEX(8), INTENT(INOUT), DIMENSION(1:nn) :: a
7+
INTEGER :: i
8+
!DIR$ assume_aligned a:16
9+
!CHECK: !DIR$ ASSUME_ALIGNED a:16
10+
!DIR$ assume_aligned a (1):16
11+
!CHECK: !DIR$ ASSUME_ALIGNED a(1):16
12+
!DIR$ assume_aligned a(1):16
13+
!CHECK: !DIR$ ASSUME_ALIGNED a(1):16
14+
!DIR$ assume_aligned a(nn):16
15+
!CHECK: !DIR$ ASSUME_ALIGNED a(nn):16
16+
!DIR$ assume_aligned a(44):16
17+
!CHECK: !DIR$ ASSUME_ALIGNED a(44):16
18+
DO i=1,nn
19+
a(i)=a(i)+1.5
20+
END DO
21+
END SUBROUTINE aa
22+
23+
SUBROUTINE bb(v, s, e)
24+
IMPLICIT NONE
25+
INTEGER, INTENT(IN) :: s(3), e(3)
26+
INTEGER :: y,z
27+
REAL(8), INTENT(IN) :: v(s(1):e(1),s(2):e(2),s(3):e(3))
28+
!DIR$ assume_aligned v(s(1),y,z) :64
29+
!CHECK: !DIR$ ASSUME_ALIGNED v(s(1),y,z):64
30+
END SUBROUTINE bb
31+
32+
SUBROUTINE f(n)
33+
IMPLICIT NONE
34+
TYPE node
35+
REAL(KIND=8), POINTER :: a(:,:)
36+
END TYPE NODE
37+
38+
TYPE(NODE), POINTER :: nodes
39+
INTEGER :: i
40+
INTEGER, INTENT(IN) :: n
41+
42+
ALLOCATE(nodes)
43+
ALLOCATE(nodes%a(1000,1000))
44+
45+
!DIR$ ASSUME_ALIGNED nodes%a(1,1) : 16
46+
!CHECK: !DIR$ ASSUME_ALIGNED nodes%a(1,1):16
47+
DO i=1,n
48+
nodes%a(1,i) = nodes%a(1,i)+1
49+
END DO
50+
END SUBROUTINE f
51+
52+
SUBROUTINE g(a, b)
53+
IMPLICIT NONE
54+
INTEGER, INTENT(in) :: a(128), b(128)
55+
!DIR$ ASSUME_ALIGNED a:32, b:64
56+
!CHECK: !DIR$ ASSUME_ALIGNED a:32, b:64
57+
END SUBROUTINE g

0 commit comments

Comments
 (0)