@@ -2153,14 +2153,42 @@ class FirConverter : public Fortran::lower::AbstractConverter {
21532153 return builder->createIntegerConstant (loc, controlType, 1 ); // step
21542154 }
21552155
2156- void addLoopAnnotationAttr (IncrementLoopInfo &info) {
2156+ void addLoopAnnotationAttr (
2157+ IncrementLoopInfo &info,
2158+ llvm::SmallVectorImpl<const Fortran::parser::CompilerDirective *> &dirs) {
21572159 mlir::BoolAttr f = mlir::BoolAttr::get (builder->getContext (), false );
2158- mlir::LLVM::LoopVectorizeAttr va = mlir::LLVM::LoopVectorizeAttr::get (
2159- builder->getContext (), /* disable=*/ f, {}, {}, {}, {}, {}, {});
2160+ mlir::BoolAttr t = mlir::BoolAttr::get (builder->getContext (), true );
2161+ mlir::LLVM::LoopVectorizeAttr va;
2162+ mlir::LLVM::LoopUnrollAttr ua;
2163+ bool has_attrs = false ;
2164+ for (const auto *dir : dirs) {
2165+ Fortran::common::visit (
2166+ Fortran::common::visitors{
2167+ [&](const Fortran::parser::CompilerDirective::VectorAlways &) {
2168+ va = mlir::LLVM::LoopVectorizeAttr::get (builder->getContext (),
2169+ /* disable=*/ f, {}, {},
2170+ {}, {}, {}, {});
2171+ has_attrs = true ;
2172+ },
2173+ [&](const Fortran::parser::CompilerDirective::Unroll &u) {
2174+ mlir::IntegerAttr countAttr;
2175+ if (u.v .has_value ()) {
2176+ countAttr = builder->getIntegerAttr (builder->getI64Type (),
2177+ u.v .value ());
2178+ }
2179+ ua = mlir::LLVM::LoopUnrollAttr::get (
2180+ builder->getContext (), /* disable=*/ f, /* count*/ countAttr,
2181+ {}, /* full*/ u.v .has_value () ? f : t, {}, {}, {});
2182+ has_attrs = true ;
2183+ },
2184+ [&](const auto &) {}},
2185+ dir->u );
2186+ }
21602187 mlir::LLVM::LoopAnnotationAttr la = mlir::LLVM::LoopAnnotationAttr::get (
2161- builder->getContext (), {}, /* vectorize=*/ va, {}, {}, {}, {}, {}, {}, {},
2162- {}, {}, {}, {}, {}, {});
2163- info.doLoop .setLoopAnnotationAttr (la);
2188+ builder->getContext (), {}, /* vectorize=*/ va, {}, /* unroll*/ ua, {}, {},
2189+ {}, {}, {}, {}, {}, {}, {}, {}, {});
2190+ if (has_attrs)
2191+ info.doLoop .setLoopAnnotationAttr (la);
21642192 }
21652193
21662194 // / Generate FIR to begin a structured or unstructured increment loop nest.
@@ -2259,14 +2287,7 @@ class FirConverter : public Fortran::lower::AbstractConverter {
22592287 if (info.hasLocalitySpecs ())
22602288 handleLocalitySpecs (info);
22612289
2262- for (const auto *dir : dirs) {
2263- Fortran::common::visit (
2264- Fortran::common::visitors{
2265- [&](const Fortran::parser::CompilerDirective::VectorAlways
2266- &d) { addLoopAnnotationAttr (info); },
2267- [&](const auto &) {}},
2268- dir->u );
2269- }
2290+ addLoopAnnotationAttr (info, dirs);
22702291 continue ;
22712292 }
22722293
@@ -2818,6 +2839,9 @@ class FirConverter : public Fortran::lower::AbstractConverter {
28182839 [&](const Fortran::parser::CompilerDirective::VectorAlways &) {
28192840 attachDirectiveToLoop (dir, &eval);
28202841 },
2842+ [&](const Fortran::parser::CompilerDirective::Unroll &) {
2843+ attachDirectiveToLoop (dir, &eval);
2844+ },
28212845 [&](const auto &) {}},
28222846 dir.u );
28232847 }
0 commit comments