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 "../PassDetail.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/Pass/Pass.h" 20 #include "mlir/Transforms/DialectConversion.h" 21 22 using namespace mlir; 23 24 namespace { 25 struct ConvertVectorToSPIRVPass 26 : public ConvertVectorToSPIRVBase<ConvertVectorToSPIRVPass> { 27 void runOnOperation() override; 28 }; 29 } // namespace 30 runOnOperation()31void ConvertVectorToSPIRVPass::runOnOperation() { 32 MLIRContext *context = &getContext(); 33 ModuleOp module = getOperation(); 34 35 auto targetAttr = spirv::lookupTargetEnvOrDefault(module); 36 std::unique_ptr<ConversionTarget> target = 37 SPIRVConversionTarget::get(targetAttr); 38 39 SPIRVTypeConverter typeConverter(targetAttr); 40 41 // Use UnrealizedConversionCast as the bridge so that we don't need to pull in 42 // patterns for other dialects. 43 auto addUnrealizedCast = [](OpBuilder &builder, Type type, ValueRange inputs, 44 Location loc) { 45 auto cast = builder.create<UnrealizedConversionCastOp>(loc, type, inputs); 46 return Optional<Value>(cast.getResult(0)); 47 }; 48 typeConverter.addSourceMaterialization(addUnrealizedCast); 49 typeConverter.addTargetMaterialization(addUnrealizedCast); 50 target->addLegalOp<UnrealizedConversionCastOp>(); 51 52 RewritePatternSet patterns(context); 53 populateVectorToSPIRVPatterns(typeConverter, patterns); 54 55 if (failed(applyPartialConversion(module, *target, std::move(patterns)))) 56 return signalPassFailure(); 57 } 58 59 std::unique_ptr<OperationPass<ModuleOp>> createConvertVectorToSPIRVPass()60mlir::createConvertVectorToSPIRVPass() { 61 return std::make_unique<ConvertVectorToSPIRVPass>(); 62 } 63