| 1 | //===- FuncToSPIRVPass.cpp - Func to SPIR-V Passes ----------------===// |
|---|---|
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file implements a pass to convert Func dialect to SPIR-V dialect. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "mlir/Conversion/FuncToSPIRV/FuncToSPIRVPass.h" |
| 14 | |
| 15 | #include "mlir/Conversion/FuncToSPIRV/FuncToSPIRV.h" |
| 16 | #include "mlir/Dialect/Func/IR/FuncOps.h" |
| 17 | #include "mlir/Dialect/SPIRV/IR/SPIRVDialect.h" |
| 18 | #include "mlir/Dialect/SPIRV/Transforms/SPIRVConversion.h" |
| 19 | |
| 20 | namespace mlir { |
| 21 | #define GEN_PASS_DEF_CONVERTFUNCTOSPIRVPASS |
| 22 | #include "mlir/Conversion/Passes.h.inc" |
| 23 | } // namespace mlir |
| 24 | |
| 25 | using namespace mlir; |
| 26 | |
| 27 | namespace { |
| 28 | /// A pass converting MLIR Func operations into the SPIR-V dialect. |
| 29 | class ConvertFuncToSPIRVPass |
| 30 | : public impl::ConvertFuncToSPIRVPassBase<ConvertFuncToSPIRVPass> { |
| 31 | using Base::Base; |
| 32 | void runOnOperation() override; |
| 33 | }; |
| 34 | } // namespace |
| 35 | |
| 36 | void ConvertFuncToSPIRVPass::runOnOperation() { |
| 37 | MLIRContext *context = &getContext(); |
| 38 | Operation *op = getOperation(); |
| 39 | |
| 40 | // This pass requires the target function to be nested inside a block so |
| 41 | // that the dialect conversion framework can properly replace or move it. |
| 42 | // Running it on a detached top-level op (e.g., via --no-implicit-module) is |
| 43 | // unsupported; wrap the input in a module op first. |
| 44 | if (!op->getBlock() && isa<func::FuncOp>(Val: op)) { |
| 45 | op->emitError(message: "'") << getArgument() |
| 46 | << "' pass requires the target operation to be nested " |
| 47 | "in a block; consider wrapping the input in a module"; |
| 48 | return signalPassFailure(); |
| 49 | } |
| 50 | |
| 51 | auto targetAttr = spirv::lookupTargetEnvOrDefault(op); |
| 52 | std::unique_ptr<ConversionTarget> target = |
| 53 | SPIRVConversionTarget::get(targetAttr); |
| 54 | |
| 55 | SPIRVConversionOptions options; |
| 56 | options.emulateLT32BitScalarTypes = this->emulateLT32BitScalarTypes; |
| 57 | options.emulateUnsupportedFloatTypes = this->emulateUnsupportedFloatTypes; |
| 58 | SPIRVTypeConverter typeConverter(targetAttr, options); |
| 59 | |
| 60 | RewritePatternSet patterns(context); |
| 61 | populateFuncToSPIRVPatterns(typeConverter, patterns); |
| 62 | populateBuiltinFuncToSPIRVPatterns(typeConverter, patterns); |
| 63 | |
| 64 | if (failed(Result: applyPartialConversion(op, target: *target, patterns: std::move(patterns)))) |
| 65 | return signalPassFailure(); |
| 66 | } |
| 67 |