2020-05-14 20:41:35 +08:00
|
|
|
//===- SCFToGPUPass.cpp - Convert a loop nest to a GPU kernel -----------===//
|
2019-06-14 16:56:19 +08:00
|
|
|
//
|
2020-01-26 11:58:30 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
2019-12-24 01:35:36 +08:00
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2019-06-14 16:56:19 +08:00
|
|
|
//
|
2019-12-24 01:35:36 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2019-06-14 16:56:19 +08:00
|
|
|
|
2020-05-14 20:41:35 +08:00
|
|
|
#include "mlir/Conversion/SCFToGPU/SCFToGPUPass.h"
|
2020-04-08 04:58:12 +08:00
|
|
|
#include "../PassDetail.h"
|
2020-05-14 20:41:35 +08:00
|
|
|
#include "mlir/Conversion/SCFToGPU/SCFToGPU.h"
|
2020-03-21 05:18:47 +08:00
|
|
|
#include "mlir/Dialect/Affine/IR/AffineOps.h"
|
2021-01-21 04:11:49 +08:00
|
|
|
#include "mlir/Dialect/Complex/IR/Complex.h"
|
2020-02-24 23:02:50 +08:00
|
|
|
#include "mlir/Dialect/GPU/GPUDialect.h"
|
2020-05-11 21:00:48 +08:00
|
|
|
#include "mlir/Dialect/SCF/SCF.h"
|
2020-02-22 03:54:49 +08:00
|
|
|
#include "mlir/Dialect/StandardOps/IR/Ops.h"
|
2020-02-24 23:02:50 +08:00
|
|
|
#include "mlir/Transforms/DialectConversion.h"
|
2019-06-14 16:56:19 +08:00
|
|
|
|
2019-11-02 01:51:33 +08:00
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
2019-06-14 16:56:19 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
|
|
|
|
using namespace mlir;
|
2020-05-11 21:00:48 +08:00
|
|
|
using namespace mlir::scf;
|
2019-06-14 16:56:19 +08:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
// A pass that traverses top-level loops in the function and converts them to
|
|
|
|
// GPU launch operations. Nested launches are not allowed, so this does not
|
|
|
|
// walk the function recursively to avoid considering nested loops.
|
2020-06-02 13:42:33 +08:00
|
|
|
struct ForLoopMapper : public ConvertAffineForToGPUBase<ForLoopMapper> {
|
2020-03-22 06:08:49 +08:00
|
|
|
ForLoopMapper() = default;
|
|
|
|
ForLoopMapper(unsigned numBlockDims, unsigned numThreadDims) {
|
|
|
|
this->numBlockDims = numBlockDims;
|
|
|
|
this->numThreadDims = numThreadDims;
|
|
|
|
}
|
2019-07-01 15:47:58 +08:00
|
|
|
|
2019-06-14 16:56:19 +08:00
|
|
|
void runOnFunction() override {
|
2020-05-05 08:46:06 +08:00
|
|
|
for (Operation &op : llvm::make_early_inc_range(getFunction().getOps())) {
|
|
|
|
if (auto forOp = dyn_cast<AffineForOp>(&op)) {
|
|
|
|
if (failed(convertAffineLoopNestToGPULaunch(forOp, numBlockDims,
|
|
|
|
numThreadDims)))
|
|
|
|
signalPassFailure();
|
2019-11-02 01:51:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-04-08 04:56:16 +08:00
|
|
|
struct ParallelLoopToGpuPass
|
2020-04-08 04:58:12 +08:00
|
|
|
: public ConvertParallelLoopToGpuBase<ParallelLoopToGpuPass> {
|
2020-02-24 23:02:50 +08:00
|
|
|
void runOnOperation() override {
|
2021-03-23 07:58:34 +08:00
|
|
|
RewritePatternSet patterns(&getContext());
|
2021-03-21 07:29:41 +08:00
|
|
|
populateParallelLoopToGPUPatterns(patterns);
|
2020-02-24 23:02:50 +08:00
|
|
|
ConversionTarget target(getContext());
|
2021-02-11 16:41:47 +08:00
|
|
|
target.markUnknownOpDynamicallyLegal([](Operation *) { return true; });
|
2020-11-13 01:36:14 +08:00
|
|
|
configureParallelLoopToGPULegality(target);
|
2020-10-27 08:25:01 +08:00
|
|
|
if (failed(applyPartialConversion(getOperation(), target,
|
|
|
|
std::move(patterns))))
|
2020-02-24 23:02:50 +08:00
|
|
|
signalPassFailure();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-06-14 16:56:19 +08:00
|
|
|
} // namespace
|
|
|
|
|
2020-04-08 04:56:16 +08:00
|
|
|
std::unique_ptr<OperationPass<FuncOp>>
|
2020-06-02 13:42:33 +08:00
|
|
|
mlir::createAffineForToGPUPass(unsigned numBlockDims, unsigned numThreadDims) {
|
2019-08-18 02:05:35 +08:00
|
|
|
return std::make_unique<ForLoopMapper>(numBlockDims, numThreadDims);
|
2019-07-01 15:47:58 +08:00
|
|
|
}
|
2020-06-02 13:42:33 +08:00
|
|
|
std::unique_ptr<OperationPass<FuncOp>> mlir::createAffineForToGPUPass() {
|
2020-04-01 16:49:43 +08:00
|
|
|
return std::make_unique<ForLoopMapper>();
|
|
|
|
}
|
2019-07-01 15:47:58 +08:00
|
|
|
|
2020-02-24 23:02:50 +08:00
|
|
|
std::unique_ptr<Pass> mlir::createParallelLoopToGpuPass() {
|
|
|
|
return std::make_unique<ParallelLoopToGpuPass>();
|
|
|
|
}
|