2020-05-14 14:41:35 +02:00
|
|
|
//===- SCFToGPUPass.cpp - Convert a loop nest to a GPU kernel -----------===//
|
2019-06-14 01:56:19 -07:00
|
|
|
//
|
2020-01-26 03:58:30 +00:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
2019-12-23 09: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 01:56:19 -07:00
|
|
|
//
|
2019-12-23 09:35:36 -08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2019-06-14 01:56:19 -07:00
|
|
|
|
2020-05-14 14:41:35 +02:00
|
|
|
#include "mlir/Conversion/SCFToGPU/SCFToGPUPass.h"
|
2020-04-07 13:58:12 -07:00
|
|
|
#include "../PassDetail.h"
|
2020-05-14 14:41:35 +02:00
|
|
|
#include "mlir/Conversion/SCFToGPU/SCFToGPU.h"
|
2020-03-20 14:18:47 -07:00
|
|
|
#include "mlir/Dialect/Affine/IR/AffineOps.h"
|
2020-02-24 16:02:50 +01:00
|
|
|
#include "mlir/Dialect/GPU/GPUDialect.h"
|
2020-05-11 15:00:48 +02:00
|
|
|
#include "mlir/Dialect/SCF/SCF.h"
|
2020-02-21 11:54:49 -08:00
|
|
|
#include "mlir/Dialect/StandardOps/IR/Ops.h"
|
2020-02-24 16:02:50 +01:00
|
|
|
#include "mlir/Transforms/DialectConversion.h"
|
2019-06-14 01:56:19 -07:00
|
|
|
|
2019-11-01 10:51:33 -07:00
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
2019-06-14 01:56:19 -07:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
|
|
|
|
|
|
using namespace mlir;
|
2020-05-11 15:00:48 +02:00
|
|
|
using namespace mlir::scf;
|
2019-06-14 01:56:19 -07: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-01 22:42:33 -07:00
|
|
|
struct ForLoopMapper : public ConvertAffineForToGPUBase<ForLoopMapper> {
|
2020-03-21 15:08:49 -07:00
|
|
|
ForLoopMapper() = default;
|
|
|
|
|
ForLoopMapper(unsigned numBlockDims, unsigned numThreadDims) {
|
|
|
|
|
this->numBlockDims = numBlockDims;
|
|
|
|
|
this->numThreadDims = numThreadDims;
|
|
|
|
|
}
|
2019-07-01 00:47:58 -07:00
|
|
|
|
2019-06-14 01:56:19 -07:00
|
|
|
void runOnFunction() override {
|
2020-05-04 17:46:06 -07: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-01 10:51:33 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2020-04-07 13:56:16 -07:00
|
|
|
struct ParallelLoopToGpuPass
|
2020-04-07 13:58:12 -07:00
|
|
|
: public ConvertParallelLoopToGpuBase<ParallelLoopToGpuPass> {
|
2020-02-24 16:02:50 +01:00
|
|
|
void runOnOperation() override {
|
|
|
|
|
OwningRewritePatternList patterns;
|
|
|
|
|
populateParallelLoopToGPUPatterns(patterns, &getContext());
|
|
|
|
|
ConversionTarget target(getContext());
|
|
|
|
|
target.addLegalDialect<StandardOpsDialect>();
|
2020-03-20 14:18:47 -07:00
|
|
|
target.addLegalDialect<AffineDialect>();
|
2020-02-24 16:02:50 +01:00
|
|
|
target.addLegalDialect<gpu::GPUDialect>();
|
2020-05-11 15:00:48 +02:00
|
|
|
target.addLegalDialect<scf::SCFDialect>();
|
|
|
|
|
target.addIllegalOp<scf::ParallelOp>();
|
2020-02-24 16:02:50 +01:00
|
|
|
if (failed(applyPartialConversion(getOperation(), target, patterns)))
|
|
|
|
|
signalPassFailure();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2019-06-14 01:56:19 -07:00
|
|
|
} // namespace
|
|
|
|
|
|
2020-04-07 13:56:16 -07:00
|
|
|
std::unique_ptr<OperationPass<FuncOp>>
|
2020-06-01 22:42:33 -07:00
|
|
|
mlir::createAffineForToGPUPass(unsigned numBlockDims, unsigned numThreadDims) {
|
2019-08-17 11:05:35 -07:00
|
|
|
return std::make_unique<ForLoopMapper>(numBlockDims, numThreadDims);
|
2019-07-01 00:47:58 -07:00
|
|
|
}
|
2020-06-01 22:42:33 -07:00
|
|
|
std::unique_ptr<OperationPass<FuncOp>> mlir::createAffineForToGPUPass() {
|
2020-04-01 01:49:43 -07:00
|
|
|
return std::make_unique<ForLoopMapper>();
|
|
|
|
|
}
|
2019-07-01 00:47:58 -07:00
|
|
|
|
2020-02-24 16:02:50 +01:00
|
|
|
std::unique_ptr<Pass> mlir::createParallelLoopToGpuPass() {
|
|
|
|
|
return std::make_unique<ParallelLoopToGpuPass>();
|
|
|
|
|
}
|