2019-11-14 12:31:32 -08:00
|
|
|
//===- ConvertGPUToSPIRVPass.cpp - GPU to SPIR-V dialect lowering passes --===//
|
|
|
|
|
//
|
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-11-14 12:31:32 -08:00
|
|
|
//
|
2019-12-23 09:35:36 -08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2019-11-14 12:31:32 -08:00
|
|
|
//
|
|
|
|
|
// This file implements a pass to convert a kernel function in the GPU Dialect
|
|
|
|
|
// into a spv.module operation
|
|
|
|
|
//
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
2020-04-01 01:49:43 -07:00
|
|
|
|
2019-11-25 13:19:19 -08:00
|
|
|
#include "mlir/Conversion/GPUToSPIRV/ConvertGPUToSPIRVPass.h"
|
2020-04-07 13:58:12 -07:00
|
|
|
#include "../PassDetail.h"
|
2019-11-14 12:31:32 -08:00
|
|
|
#include "mlir/Conversion/GPUToSPIRV/ConvertGPUToSPIRV.h"
|
2020-07-01 16:54:26 -07:00
|
|
|
#include "mlir/Conversion/SCFToSPIRV/SCFToSPIRV.h"
|
2019-11-14 12:31:32 -08:00
|
|
|
#include "mlir/Conversion/StandardToSPIRV/ConvertStandardToSPIRV.h"
|
|
|
|
|
#include "mlir/Dialect/GPU/GPUDialect.h"
|
2020-05-11 15:00:48 +02:00
|
|
|
#include "mlir/Dialect/SCF/SCF.h"
|
2019-11-14 12:31:32 -08:00
|
|
|
#include "mlir/Dialect/SPIRV/SPIRVDialect.h"
|
|
|
|
|
#include "mlir/Dialect/SPIRV/SPIRVLowering.h"
|
|
|
|
|
#include "mlir/Dialect/SPIRV/SPIRVOps.h"
|
|
|
|
|
|
|
|
|
|
using namespace mlir;
|
|
|
|
|
|
|
|
|
|
namespace {
|
2020-02-04 20:58:10 -05:00
|
|
|
/// Pass to lower GPU Dialect to SPIR-V. The pass only converts the gpu.func ops
|
|
|
|
|
/// inside gpu.module ops. i.e., the function that are referenced in
|
|
|
|
|
/// gpu.launch_func ops. For each such function
|
2019-11-14 12:31:32 -08:00
|
|
|
///
|
|
|
|
|
/// 1) Create a spirv::ModuleOp, and clone the function into spirv::ModuleOp
|
|
|
|
|
/// (the original function is still needed by the gpu::LaunchKernelOp, so cannot
|
|
|
|
|
/// replace it).
|
|
|
|
|
///
|
|
|
|
|
/// 2) Lower the body of the spirv::ModuleOp.
|
2020-04-07 13:58:12 -07:00
|
|
|
struct GPUToSPIRVPass : public ConvertGPUToSPIRVBase<GPUToSPIRVPass> {
|
2020-04-07 13:55:34 -07:00
|
|
|
void runOnOperation() override;
|
2019-11-14 12:31:32 -08:00
|
|
|
};
|
|
|
|
|
} // namespace
|
|
|
|
|
|
2020-04-07 13:55:34 -07:00
|
|
|
void GPUToSPIRVPass::runOnOperation() {
|
2020-01-14 18:23:25 -05:00
|
|
|
MLIRContext *context = &getContext();
|
2020-04-07 13:55:34 -07:00
|
|
|
ModuleOp module = getOperation();
|
2019-11-14 12:31:32 -08:00
|
|
|
|
2019-12-09 09:51:25 -08:00
|
|
|
SmallVector<Operation *, 1> kernelModules;
|
|
|
|
|
OpBuilder builder(context);
|
2020-01-17 15:18:23 +01:00
|
|
|
module.walk([&builder, &kernelModules](gpu::GPUModuleOp moduleOp) {
|
|
|
|
|
// For each kernel module (should be only 1 for now, but that is not a
|
|
|
|
|
// requirement here), clone the module for conversion because the
|
|
|
|
|
// gpu.launch function still needs the kernel module.
|
|
|
|
|
builder.setInsertionPoint(moduleOp.getOperation());
|
|
|
|
|
kernelModules.push_back(builder.clone(*moduleOp.getOperation()));
|
2019-11-14 12:31:32 -08:00
|
|
|
});
|
|
|
|
|
|
2020-03-18 09:55:40 -04:00
|
|
|
auto targetAttr = spirv::lookupTargetEnvOrDefault(module);
|
|
|
|
|
std::unique_ptr<ConversionTarget> target =
|
|
|
|
|
spirv::SPIRVConversionTarget::get(targetAttr);
|
|
|
|
|
|
|
|
|
|
SPIRVTypeConverter typeConverter(targetAttr);
|
2020-07-01 17:08:08 -07:00
|
|
|
ScfToSPIRVContext scfContext;
|
2019-11-14 12:31:32 -08:00
|
|
|
OwningRewritePatternList patterns;
|
2020-02-04 20:58:10 -05:00
|
|
|
populateGPUToSPIRVPatterns(context, typeConverter, patterns);
|
2020-07-01 17:08:08 -07:00
|
|
|
populateSCFToSPIRVPatterns(context, typeConverter,scfContext, patterns);
|
2019-11-14 12:31:32 -08:00
|
|
|
populateStandardToSPIRVPatterns(context, typeConverter, patterns);
|
|
|
|
|
|
2020-06-18 15:45:43 -07:00
|
|
|
if (failed(applyFullConversion(kernelModules, *target, patterns)))
|
2019-11-14 12:31:32 -08:00
|
|
|
return signalPassFailure();
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-07 13:56:16 -07:00
|
|
|
std::unique_ptr<OperationPass<ModuleOp>> mlir::createConvertGPUToSPIRVPass() {
|
2020-02-04 20:58:10 -05:00
|
|
|
return std::make_unique<GPUToSPIRVPass>();
|
2019-11-14 12:31:32 -08:00
|
|
|
}
|