@@ -2205,11 +2205,39 @@ class FirConverter : public Fortran::lower::AbstractConverter {
22052205 /* full=*/ fullUnrollAttr, {}, {}, {});
22062206 }
22072207
2208+ // Enabling unroll and jamming directive without a value.
2209+ // For directives with a value, if the value is greater than 1,
2210+ // force unrolling with the given factor. Otherwise, disable unrolling and
2211+ // jamming.
2212+ mlir::LLVM::LoopUnrollAndJamAttr
2213+ genLoopUnrollAndJamAttr (std::optional<std::uint64_t > count) {
2214+ mlir::BoolAttr falseAttr =
2215+ mlir::BoolAttr::get (builder->getContext (), false );
2216+ mlir::BoolAttr trueAttr = mlir::BoolAttr::get (builder->getContext (), true );
2217+ mlir::IntegerAttr countAttr;
2218+ bool shouldUnroll = true ;
2219+ if (count.has_value ()) {
2220+ auto unrollingFactor = count.value ();
2221+ if (unrollingFactor == 0 || unrollingFactor == 1 ) {
2222+ shouldUnroll = false ;
2223+ } else {
2224+ countAttr =
2225+ builder->getIntegerAttr (builder->getI64Type (), unrollingFactor);
2226+ }
2227+ }
2228+
2229+ mlir::BoolAttr disableAttr = shouldUnroll ? falseAttr : trueAttr;
2230+ return mlir::LLVM::LoopUnrollAndJamAttr::get (
2231+ builder->getContext (), /* disable=*/ disableAttr, /* count*/ countAttr, {},
2232+ {}, {}, {}, {});
2233+ }
2234+
22082235 void addLoopAnnotationAttr (
22092236 IncrementLoopInfo &info,
22102237 llvm::SmallVectorImpl<const Fortran::parser::CompilerDirective *> &dirs) {
22112238 mlir::LLVM::LoopVectorizeAttr va;
22122239 mlir::LLVM::LoopUnrollAttr ua;
2240+ mlir::LLVM::LoopUnrollAndJamAttr uja;
22132241 bool has_attrs = false ;
22142242 for (const auto *dir : dirs) {
22152243 Fortran::common::visit (
@@ -2226,12 +2254,16 @@ class FirConverter : public Fortran::lower::AbstractConverter {
22262254 ua = genLoopUnrollAttr (u.v );
22272255 has_attrs = true ;
22282256 },
2257+ [&](const Fortran::parser::CompilerDirective::UnrollAndJam &u) {
2258+ uja = genLoopUnrollAndJamAttr (u.v );
2259+ has_attrs = true ;
2260+ },
22292261 [&](const auto &) {}},
22302262 dir->u );
22312263 }
22322264 mlir::LLVM::LoopAnnotationAttr la = mlir::LLVM::LoopAnnotationAttr::get (
2233- builder->getContext (), {}, /* vectorize=*/ va, {}, /* unroll*/ ua, {}, {},
2234- {}, {}, {}, {}, {}, {}, {}, {}, {});
2265+ builder->getContext (), {}, /* vectorize=*/ va, {}, /* unroll*/ ua,
2266+ /* unroll_and_jam */ uja, {}, {}, {}, {}, {}, {}, {}, {}, {}, {});
22352267 if (has_attrs)
22362268 info.doLoop .setLoopAnnotationAttr (la);
22372269 }
@@ -2887,6 +2919,9 @@ class FirConverter : public Fortran::lower::AbstractConverter {
28872919 [&](const Fortran::parser::CompilerDirective::Unroll &) {
28882920 attachDirectiveToLoop (dir, &eval);
28892921 },
2922+ [&](const Fortran::parser::CompilerDirective::UnrollAndJam &) {
2923+ attachDirectiveToLoop (dir, &eval);
2924+ },
28902925 [&](const auto &) {}},
28912926 dir.u );
28922927 }
0 commit comments