[flang-commits] [flang] [flang] Implement !DIR$ UNROLL [N] (PR #123331)
Jean-Didier PAILLEUX via flang-commits
flang-commits at lists.llvm.org
Tue Jan 21 00:40:06 PST 2025
================
@@ -2153,14 +2153,42 @@ class FirConverter : public Fortran::lower::AbstractConverter {
return builder->createIntegerConstant(loc, controlType, 1); // step
}
- void addLoopAnnotationAttr(IncrementLoopInfo &info) {
+ void addLoopAnnotationAttr(
+ IncrementLoopInfo &info,
+ llvm::SmallVectorImpl<const Fortran::parser::CompilerDirective *> &dirs) {
mlir::BoolAttr f = mlir::BoolAttr::get(builder->getContext(), false);
- mlir::LLVM::LoopVectorizeAttr va = mlir::LLVM::LoopVectorizeAttr::get(
- builder->getContext(), /*disable=*/f, {}, {}, {}, {}, {}, {});
+ mlir::BoolAttr t = mlir::BoolAttr::get(builder->getContext(), true);
+ mlir::LLVM::LoopVectorizeAttr va;
+ mlir::LLVM::LoopUnrollAttr ua;
+ bool has_attrs = false;
+ for (const auto *dir : dirs) {
+ Fortran::common::visit(
+ Fortran::common::visitors{
+ [&](const Fortran::parser::CompilerDirective::VectorAlways &) {
+ va = mlir::LLVM::LoopVectorizeAttr::get(builder->getContext(),
+ /*disable=*/f, {}, {},
+ {}, {}, {}, {});
+ has_attrs = true;
+ },
+ [&](const Fortran::parser::CompilerDirective::Unroll &u) {
+ mlir::IntegerAttr countAttr;
+ if (u.v.has_value()) {
+ countAttr = builder->getIntegerAttr(builder->getI64Type(),
+ u.v.value());
+ }
+ ua = mlir::LLVM::LoopUnrollAttr::get(
+ builder->getContext(), /*disable=*/f, /*count*/ countAttr,
+ {}, /*full*/ u.v.has_value() ? f : t, {}, {}, {});
+ has_attrs = true;
+ },
+ [&](const auto &) {}},
+ dir->u);
+ }
mlir::LLVM::LoopAnnotationAttr la = mlir::LLVM::LoopAnnotationAttr::get(
- builder->getContext(), {}, /*vectorize=*/va, {}, {}, {}, {}, {}, {}, {},
- {}, {}, {}, {}, {}, {});
- info.doLoop.setLoopAnnotationAttr(la);
+ builder->getContext(), {}, /*vectorize=*/va, {}, /*unroll*/ ua, {}, {},
+ {}, {}, {}, {}, {}, {}, {}, {}, {});
+ if (has_attrs)
+ info.doLoop.setLoopAnnotationAttr(la);
----------------
JDPailleux wrote:
Hi @DavidTruby , thanks for your feedback! About your question, I added the `has_attrs` because having moved your loop into this function, if `dirs` is empty, it will return an empty `LoopAnnotationAttr` and the attribute ` {loopAnnotation = #llvm.loop_annotation<>} ` will be put in the IR on all loops having no attached directives and many Flang tests will fail.
https://github.com/llvm/llvm-project/pull/123331
More information about the flang-commits
mailing list