@@ -2200,11 +2200,39 @@ class FirConverter : public Fortran::lower::AbstractConverter {
22002200 /* full=*/ fullUnrollAttr, {}, {}, {});
22012201 }
22022202
2203+ // Enabling unroll and jamming directive without a value.
2204+ // For directives with a value, if the value is greater than 1,
2205+ // force unrolling with the given factor. Otherwise, disable unrolling and
2206+ // jamming.
2207+ mlir::LLVM::LoopUnrollAndJamAttr
2208+ genLoopUnrollAndJamAttr (std::optional<std::uint64_t > count) {
2209+ mlir::BoolAttr falseAttr =
2210+ mlir::BoolAttr::get (builder->getContext (), false );
2211+ mlir::BoolAttr trueAttr = mlir::BoolAttr::get (builder->getContext (), true );
2212+ mlir::IntegerAttr countAttr;
2213+ bool shouldUnroll = true ;
2214+ if (count.has_value ()) {
2215+ auto unrollingFactor = count.value ();
2216+ if (unrollingFactor == 0 || unrollingFactor == 1 ) {
2217+ shouldUnroll = false ;
2218+ } else {
2219+ countAttr =
2220+ builder->getIntegerAttr (builder->getI64Type (), unrollingFactor);
2221+ }
2222+ }
2223+
2224+ mlir::BoolAttr disableAttr = shouldUnroll ? falseAttr : trueAttr;
2225+ return mlir::LLVM::LoopUnrollAndJamAttr::get (
2226+ builder->getContext (), /* disable=*/ disableAttr, /* count*/ countAttr, {},
2227+ {}, {}, {}, {});
2228+ }
2229+
22032230 void addLoopAnnotationAttr (
22042231 IncrementLoopInfo &info,
22052232 llvm::SmallVectorImpl<const Fortran::parser::CompilerDirective *> &dirs) {
22062233 mlir::LLVM::LoopVectorizeAttr va;
22072234 mlir::LLVM::LoopUnrollAttr ua;
2235+ mlir::LLVM::LoopUnrollAndJamAttr uja;
22082236 bool has_attrs = false ;
22092237 for (const auto *dir : dirs) {
22102238 Fortran::common::visit (
@@ -2221,12 +2249,16 @@ class FirConverter : public Fortran::lower::AbstractConverter {
22212249 ua = genLoopUnrollAttr (u.v );
22222250 has_attrs = true ;
22232251 },
2252+ [&](const Fortran::parser::CompilerDirective::UnrollAndJam &u) {
2253+ uja = genLoopUnrollAndJamAttr (u.v );
2254+ has_attrs = true ;
2255+ },
22242256 [&](const auto &) {}},
22252257 dir->u );
22262258 }
22272259 mlir::LLVM::LoopAnnotationAttr la = mlir::LLVM::LoopAnnotationAttr::get (
2228- builder->getContext (), {}, /* vectorize=*/ va, {}, /* unroll*/ ua, {}, {},
2229- {}, {}, {}, {}, {}, {}, {}, {}, {});
2260+ builder->getContext (), {}, /* vectorize=*/ va, {}, /* unroll*/ ua,
2261+ /* unroll_and_jam */ uja, {}, {}, {}, {}, {}, {}, {}, {}, {}, {});
22302262 if (has_attrs)
22312263 info.doLoop .setLoopAnnotationAttr (la);
22322264 }
@@ -2882,6 +2914,9 @@ class FirConverter : public Fortran::lower::AbstractConverter {
28822914 [&](const Fortran::parser::CompilerDirective::Unroll &) {
28832915 attachDirectiveToLoop (dir, &eval);
28842916 },
2917+ [&](const Fortran::parser::CompilerDirective::UnrollAndJam &) {
2918+ attachDirectiveToLoop (dir, &eval);
2919+ },
28852920 [&](const auto &) {}},
28862921 dir.u );
28872922 }
0 commit comments