https://github.com/halide/Halide
Revision cd95b27d6b969a1da8337ba820ad8d0c50884bff authored by Steven Johnson on 27 July 2018, 16:56:13 UTC, committed by GitHub on 27 July 2018, 16:56:13 UTC
Rework runtime to allow more than 64 Target::Features (Issue #2911)
2 parent s d91647c + d734dc0
Raw File
Tip revision: cd95b27d6b969a1da8337ba820ad8d0c50884bff authored by Steven Johnson on 27 July 2018, 16:56:13 UTC
Merge pull request #3116 from halide/srj-features
Tip revision: cd95b27
UnrollLoops.cpp
#include "UnrollLoops.h"
#include "IRMutator.h"
#include "IROperator.h"
#include "Simplify.h"
#include "Substitute.h"

using std::vector;

namespace Halide {
namespace Internal {

class UnrollLoops : public IRMutator2 {
    using IRMutator2::visit;

    Stmt visit(const For *for_loop) override {
        if (for_loop->for_type == ForType::Unrolled) {
            // Give it one last chance to simplify to an int
            Expr extent = simplify(for_loop->extent);
            const IntImm *e = extent.as<IntImm>();
            user_assert(e)
                << "Can only unroll for loops over a constant extent.\n"
                << "Loop over " << for_loop->name << " has extent " << extent << ".\n";
            Stmt body = mutate(for_loop->body);

            if (e->value == 1) {
                user_warning << "Warning: Unrolling a for loop of extent 1: " << for_loop->name << "\n";
            }

            vector<Stmt> iters;
            // Make n copies of the body, each wrapped in a let that defines the loop var for that body
            for (int i = 0; i < e->value; i++) {
                iters.push_back(substitute(for_loop->name, for_loop->min + i, body));
            }
            return Block::make(iters);

        } else {
            return IRMutator2::visit(for_loop);
        }
    }
};

Stmt unroll_loops(Stmt s) {
    return UnrollLoops().mutate(s);
}

}  // namespace Internal
}  // namespace Halide
back to top