| 1 | //===- VectorToSPIRVPass.cpp - Vector 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 Vector dialect to SPIRV dialect. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "mlir/Conversion/VectorToSPIRV/VectorToSPIRVPass.h" |
| 14 | |
| 15 | #include "mlir/Conversion/UBToSPIRV/UBToSPIRV.h" |
| 16 | #include "mlir/Conversion/VectorToSPIRV/VectorToSPIRV.h" |
| 17 | #include "mlir/Dialect/SPIRV/IR/SPIRVDialect.h" |
| 18 | #include "mlir/Dialect/SPIRV/Transforms/SPIRVConversion.h" |
| 19 | #include "mlir/Dialect/UB/IR/UBOps.h" |
| 20 | #include "mlir/Pass/Pass.h" |
| 21 | #include "mlir/Transforms/DialectConversion.h" |
| 22 | |
| 23 | namespace mlir { |
| 24 | #define GEN_PASS_DEF_CONVERTVECTORTOSPIRVPASS |
| 25 | #include "mlir/Conversion/Passes.h.inc" |
| 26 | } // namespace mlir |
| 27 | |
| 28 | using namespace mlir; |
| 29 | |
| 30 | namespace { |
| 31 | struct ConvertVectorToSPIRVPass |
| 32 | : public impl::ConvertVectorToSPIRVPassBase<ConvertVectorToSPIRVPass> { |
| 33 | void runOnOperation() override; |
| 34 | }; |
| 35 | } // namespace |
| 36 | |
| 37 | void ConvertVectorToSPIRVPass::runOnOperation() { |
| 38 | MLIRContext *context = &getContext(); |
| 39 | Operation *op = getOperation(); |
| 40 | |
| 41 | auto targetAttr = spirv::lookupTargetEnvOrDefault(op); |
| 42 | std::unique_ptr<ConversionTarget> target = |
| 43 | SPIRVConversionTarget::get(targetAttr: targetAttr); |
| 44 | |
| 45 | SPIRVTypeConverter typeConverter(targetAttr); |
| 46 | |
| 47 | // Use UnrealizedConversionCast as the bridge so that we don't need to pull in |
| 48 | // patterns for other dialects. |
| 49 | target->addLegalOp<UnrealizedConversionCastOp>(); |
| 50 | |
| 51 | RewritePatternSet patterns(context); |
| 52 | populateVectorToSPIRVPatterns(typeConverter, patterns); |
| 53 | // Used for folds, e.g. vector.extract[-1] -> ub.poison -> spirv.Undef. |
| 54 | ub::populateUBToSPIRVConversionPatterns(converter: typeConverter, patterns); |
| 55 | |
| 56 | if (failed(applyPartialConversion(op, *target, std::move(patterns)))) |
| 57 | return signalPassFailure(); |
| 58 | } |
| 59 |
