Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions flang/docs/Directives.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ A list of non-standard directives supported by Flang
integer that specifying the unrolling factor. When `N` is `0` or `1`, the loop
should not be unrolled at all. If `N` is omitted the optimizer will
selects the number of times to unroll the loop.
* `!dir$ novector` disabling vectorization on the following loop.
* `!dir$ nounroll` disabling unrolling on the following loop.
* `!dir$ nounroll_and_jam` disabling unrolling and jamming on the following loop.

# Directive Details

Expand Down
3 changes: 3 additions & 0 deletions flang/include/flang/Parser/dump-parse-tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,9 @@ class ParseTreeDumper {
NODE(CompilerDirective, VectorAlways)
NODE(CompilerDirective, Unroll)
NODE(CompilerDirective, UnrollAndJam)
NODE(CompilerDirective, NoVector)
NODE(CompilerDirective, NoUnroll)
NODE(CompilerDirective, NoUnrollAndJam)
NODE(parser, ComplexLiteralConstant)
NODE(parser, ComplexPart)
NODE(parser, ComponentArraySpec)
Expand Down
9 changes: 8 additions & 1 deletion flang/include/flang/Parser/parse-tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -3351,6 +3351,9 @@ struct StmtFunctionStmt {
// !DIR$ name[=value] [, name[=value]]... = can be :
// !DIR$ UNROLL [N]
// !DIR$ UNROLL_AND_JAM [N]
// !DIR$ NOVECTOR
// !DIR$ NOUNROLL
// !DIR$ NOUNROLL_AND_JAM
// !DIR$ <anything else>
struct CompilerDirective {
UNION_CLASS_BOILERPLATE(CompilerDirective);
Expand All @@ -3376,10 +3379,14 @@ struct CompilerDirective {
struct UnrollAndJam {
WRAPPER_CLASS_BOILERPLATE(UnrollAndJam, std::optional<std::uint64_t>);
};
EMPTY_CLASS(NoVector);
EMPTY_CLASS(NoUnroll);
EMPTY_CLASS(NoUnrollAndJam);
EMPTY_CLASS(Unrecognized);
CharBlock source;
std::variant<std::list<IgnoreTKR>, LoopCount, std::list<AssumeAligned>,
VectorAlways, std::list<NameValue>, Unroll, UnrollAndJam, Unrecognized>
VectorAlways, std::list<NameValue>, Unroll, UnrollAndJam, Unrecognized,
NoVector, NoUnroll, NoUnrollAndJam>
u;
};

Expand Down
26 changes: 26 additions & 0 deletions flang/lib/Lower/Bridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2261,6 +2261,23 @@ class FirConverter : public Fortran::lower::AbstractConverter {
uja = genLoopUnrollAndJamAttr(u.v);
has_attrs = true;
},
[&](const Fortran::parser::CompilerDirective::NoVector &u) {
mlir::BoolAttr trueAttr =
mlir::BoolAttr::get(builder->getContext(), true);
va = mlir::LLVM::LoopVectorizeAttr::get(builder->getContext(),
/*disable=*/trueAttr,
{}, {}, {}, {}, {}, {});
has_attrs = true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not for this patch. But has_attrs should be renamed to hasAttrs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I'll keep that in mind for the next one.

},
[&](const Fortran::parser::CompilerDirective::NoUnroll &u) {
ua = genLoopUnrollAttr(/*unrollingFactor=*/0);
has_attrs = true;
},
[&](const Fortran::parser::CompilerDirective::NoUnrollAndJam &u) {
uja = genLoopUnrollAndJamAttr(/*unrollingFactor=*/0);
has_attrs = true;
},

[&](const auto &) {}},
dir->u);
}
Expand Down Expand Up @@ -2925,6 +2942,15 @@ class FirConverter : public Fortran::lower::AbstractConverter {
[&](const Fortran::parser::CompilerDirective::UnrollAndJam &) {
attachDirectiveToLoop(dir, &eval);
},
[&](const Fortran::parser::CompilerDirective::NoVector &) {
attachDirectiveToLoop(dir, &eval);
},
[&](const Fortran::parser::CompilerDirective::NoUnroll &) {
attachDirectiveToLoop(dir, &eval);
},
[&](const Fortran::parser::CompilerDirective::NoUnrollAndJam &) {
attachDirectiveToLoop(dir, &eval);
},
[&](const auto &) {}},
dir.u);
}
Expand Down
7 changes: 7 additions & 0 deletions flang/lib/Parser/Fortran-parsers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1310,13 +1310,20 @@ constexpr auto unroll{
"UNROLL" >> construct<CompilerDirective::Unroll>(maybe(digitString64))};
constexpr auto unrollAndJam{"UNROLL_AND_JAM" >>
construct<CompilerDirective::UnrollAndJam>(maybe(digitString64))};
constexpr auto novector{"NOVECTOR" >> construct<CompilerDirective::NoVector>()};
constexpr auto nounroll{"NOUNROLL" >> construct<CompilerDirective::NoUnroll>()};
constexpr auto nounrollAndJam{
"NOUNROLL_AND_JAM" >> construct<CompilerDirective::NoUnrollAndJam>()};
TYPE_PARSER(beginDirective >> "DIR$ "_tok >>
sourced((construct<CompilerDirective>(ignore_tkr) ||
construct<CompilerDirective>(loopCount) ||
construct<CompilerDirective>(assumeAligned) ||
construct<CompilerDirective>(vectorAlways) ||
construct<CompilerDirective>(unrollAndJam) ||
construct<CompilerDirective>(unroll) ||
construct<CompilerDirective>(novector) ||
construct<CompilerDirective>(nounrollAndJam) ||
construct<CompilerDirective>(nounroll) ||
construct<CompilerDirective>(
many(construct<CompilerDirective::NameValue>(
name, maybe(("="_tok || ":"_tok) >> digitString64))))) /
Expand Down
9 changes: 9 additions & 0 deletions flang/lib/Parser/unparse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1855,6 +1855,15 @@ class UnparseVisitor {
Word("!DIR$ UNROLL_AND_JAM");
Walk(" ", unrollAndJam.v);
},
[&](const CompilerDirective::NoVector &) {
Word("!DIR$ NOVECTOR");
},
[&](const CompilerDirective::NoUnroll &) {
Word("!DIR$ NOUNROLL");
},
[&](const CompilerDirective::NoUnrollAndJam &) {
Word("!DIR$ NOUNROLL_AND_JAM");
},
[&](const CompilerDirective::Unrecognized &) {
Word("!DIR$ ");
Word(x.source.ToString());
Expand Down
14 changes: 13 additions & 1 deletion flang/lib/Semantics/canonicalize-directives.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ static bool IsExecutionDirective(const parser::CompilerDirective &dir) {
return std::holds_alternative<parser::CompilerDirective::VectorAlways>(
dir.u) ||
std::holds_alternative<parser::CompilerDirective::Unroll>(dir.u) ||
std::holds_alternative<parser::CompilerDirective::UnrollAndJam>(dir.u);
std::holds_alternative<parser::CompilerDirective::UnrollAndJam>(dir.u) ||
std::holds_alternative<parser::CompilerDirective::NoVector>(dir.u) ||
std::holds_alternative<parser::CompilerDirective::NoUnroll>(dir.u) ||
std::holds_alternative<parser::CompilerDirective::NoUnrollAndJam>(dir.u);
}

void CanonicalizationOfDirectives::Post(parser::SpecificationPart &spec) {
Expand Down Expand Up @@ -119,6 +122,15 @@ void CanonicalizationOfDirectives::Post(parser::Block &block) {
[&](parser::CompilerDirective::UnrollAndJam &) {
CheckLoopDirective(*dir, block, it);
},
[&](parser::CompilerDirective::NoVector &) {
CheckLoopDirective(*dir, block, it);
},
[&](parser::CompilerDirective::NoUnroll &) {
CheckLoopDirective(*dir, block, it);
},
[&](parser::CompilerDirective::NoUnrollAndJam &) {
CheckLoopDirective(*dir, block, it);
},
[&](auto &) {}},
dir->u);
}
Expand Down
5 changes: 4 additions & 1 deletion flang/lib/Semantics/resolve-names.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9573,7 +9573,10 @@ void ResolveNamesVisitor::Post(const parser::AssignedGotoStmt &x) {
void ResolveNamesVisitor::Post(const parser::CompilerDirective &x) {
if (std::holds_alternative<parser::CompilerDirective::VectorAlways>(x.u) ||
std::holds_alternative<parser::CompilerDirective::Unroll>(x.u) ||
std::holds_alternative<parser::CompilerDirective::UnrollAndJam>(x.u)) {
std::holds_alternative<parser::CompilerDirective::UnrollAndJam>(x.u) ||
std::holds_alternative<parser::CompilerDirective::NoVector>(x.u) ||
std::holds_alternative<parser::CompilerDirective::NoUnroll>(x.u) ||
std::holds_alternative<parser::CompilerDirective::NoUnrollAndJam>(x.u)) {
return;
}
if (const auto *tkr{
Expand Down
10 changes: 10 additions & 0 deletions flang/test/Integration/unroll_and_jam.f90
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ subroutine unroll_and_jam_dir_1
end do
end subroutine unroll_and_jam_dir_1

! CHECK-LABEL: nounroll_and_jam_dir
subroutine nounroll_and_jam_dir
integer :: a(10)
!dir$ nounroll_and_jam
! CHECK: br i1 {{.*}}, label {{.*}}, label {{.*}}, !llvm.loop ![[ANNOTATION_DISABLE]]
do i=1,10
a(i)=i
end do
end subroutine nounroll_and_jam_dir

! CHECK-LABEL: unroll_and_jam_dir_no_factor
subroutine unroll_and_jam_dir_no_factor
integer :: a(10)
Expand Down
12 changes: 12 additions & 0 deletions flang/test/Integration/vector-always.f90
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,17 @@ subroutine vector_always
end do
end subroutine vector_always

! CHECK-LABEL: no_vector
subroutine no_vector
integer :: a(10)
!dir$ novector
! CHECK: br i1 {{.*}}, label {{.*}}, label {{.*}}, !llvm.loop ![[ANNOTATION2:.*]]
do i=1,10
a(i)=i
end do
end subroutine no_vector

! CHECK: ![[ANNOTATION]] = distinct !{![[ANNOTATION]], ![[VECTORIZE:.*]]}
! CHECK: ![[VECTORIZE]] = !{!"llvm.loop.vectorize.enable", i1 true}
! CHECK: ![[ANNOTATION2]] = distinct !{![[ANNOTATION2]], ![[VECTORIZE2:.*]]}
! CHECK: ![[VECTORIZE2]] = !{!"llvm.loop.vectorize.enable", i1 false}
13 changes: 13 additions & 0 deletions flang/test/Lower/unroll_and_jam.f90
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

! CHECK: #loop_unroll_and_jam = #llvm.loop_unroll_and_jam<disable = false>
! CHECK: #loop_unroll_and_jam1 = #llvm.loop_unroll_and_jam<disable = false, count = 2 : i64>
! CHECK: #loop_unroll_and_jam2 = #llvm.loop_unroll_and_jam<disable = true>
! CHECK: #loop_annotation = #llvm.loop_annotation<unrollAndJam = #loop_unroll_and_jam>
! CHECK: #loop_annotation1 = #llvm.loop_annotation<unrollAndJam = #loop_unroll_and_jam1>
! CHECK: #loop_annotation2 = #llvm.loop_annotation<unrollAndJam = #loop_unroll_and_jam2>

! CHECK-LABEL: unroll_and_jam_dir
subroutine unroll_and_jam_dir
Expand Down Expand Up @@ -32,3 +34,14 @@ subroutine intermediate_directive
a(i)=i
end do
end subroutine intermediate_directive


! CHECK-LABEL: nounroll_and_jam_dir
subroutine nounroll_and_jam_dir
integer :: a(10)
!dir$ nounroll_and_jam
!CHECK: fir.do_loop {{.*}} attributes {loopAnnotation = #loop_annotation2}
do i=1,10
a(i)=i
end do
end subroutine nounroll_and_jam_dir
13 changes: 13 additions & 0 deletions flang/test/Lower/vector-always.f90
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
! RUN: %flang_fc1 -emit-hlfir -o - %s | FileCheck %s

! CHECK: #loop_vectorize = #llvm.loop_vectorize<disable = false>
! CHECK: #loop_vectorize1 = #llvm.loop_vectorize<disable = true>
! CHECK: #loop_annotation = #llvm.loop_annotation<vectorize = #loop_vectorize>
! CHECK: #loop_annotation1 = #llvm.loop_annotation<vectorize = #loop_vectorize1>

! CHECK-LABEL: vector_always
subroutine vector_always
Expand All @@ -24,3 +26,14 @@ subroutine intermediate_directive
a(i)=i
end do
end subroutine intermediate_directive


! CHECK-LABEL: no_vector
subroutine no_vector
integer :: a(10)
!dir$ novector
!CHECK: fir.do_loop {{.*}} attributes {loopAnnotation = #loop_annotation1}
do i=1,10
a(i)=i
end do
end subroutine no_vector
15 changes: 15 additions & 0 deletions flang/test/Parser/compiler-directives.f90
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ subroutine unroll
! CHECK: !DIR$ UNROLL 2
do i=1,10
enddo
!dir$ nounroll
! CHECK: !DIR$ NOUNROLL
do i=1,10
enddo
end subroutine

subroutine unroll_and_jam
Expand All @@ -56,4 +60,15 @@ subroutine unroll_and_jam
! CHECK: !DIR$ UNROLL_AND_JAM 2
do i=1,10
enddo
!dir$ nounroll_and_jam
! CHECK: !DIR$ NOUNROLL_AND_JAM
do i=1,10
enddo
end subroutine

subroutine no_vector
!dir$ novector
! CHECK: !DIR$ NOVECTOR
do i=1,10
enddo
end subroutine
Loading