llvm-project/mlir/test/lib/Dialect/Affine/TestLoopMapping.cpp
River Riddle 87d6bf3728 [mlir][test] Generalize a bunch of FuncOp based passes to run on any operation/interfaces
A lot of test passes are currently anchored on FuncOp, but this
dependency
is generally just historical. A majority of these test passes can run on
any operation, or can operate on a specific interface
(FunctionOpInterface/SymbolOpInterface).
This allows for greatly reducing the API dependency on FuncOp, which
is slated to be moved out of the Builtin dialect.

Differential Revision: https://reviews.llvm.org/D121191
2022-03-08 12:25:32 -08:00

67 lines
2.2 KiB
C++

//===- TestLoopMapping.cpp --- Parametric loop mapping pass ---------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file implements a pass to parametrically map scf.for loops to virtual
// processing element dimensions.
//
//===----------------------------------------------------------------------===//
#include "mlir/Dialect/Affine/IR/AffineOps.h"
#include "mlir/Dialect/Affine/LoopUtils.h"
#include "mlir/Dialect/SCF/SCF.h"
#include "mlir/IR/Builders.h"
#include "mlir/Pass/Pass.h"
#include "llvm/ADT/SetVector.h"
using namespace mlir;
namespace {
class TestLoopMappingPass
: public PassWrapper<TestLoopMappingPass, OperationPass<>> {
public:
StringRef getArgument() const final {
return "test-mapping-to-processing-elements";
}
StringRef getDescription() const final {
return "test mapping a single loop on a virtual processor grid";
}
explicit TestLoopMappingPass() = default;
void getDependentDialects(DialectRegistry &registry) const override {
registry.insert<AffineDialect, scf::SCFDialect>();
}
void runOnOperation() override {
// SSA values for the transformation are created out of thin air by
// unregistered "new_processor_id_and_range" operations. This is enough to
// emulate mapping conditions.
SmallVector<Value, 8> processorIds, numProcessors;
getOperation()->walk([&processorIds, &numProcessors](Operation *op) {
if (op->getName().getStringRef() != "new_processor_id_and_range")
return;
processorIds.push_back(op->getResult(0));
numProcessors.push_back(op->getResult(1));
});
getOperation()->walk([&processorIds, &numProcessors](scf::ForOp op) {
// Ignore nested loops.
if (op->getParentRegion()->getParentOfType<scf::ForOp>())
return;
mapLoopToProcessorIds(op, processorIds, numProcessors);
});
}
};
} // namespace
namespace mlir {
namespace test {
void registerTestLoopMappingPass() { PassRegistration<TestLoopMappingPass>(); }
} // namespace test
} // namespace mlir