1 //===- MemRefToSPIRVPass.cpp - MemRef 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 standard dialect to SPIR-V dialect. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "mlir/Conversion/MemRefToSPIRV/MemRefToSPIRVPass.h" 14 #include "../PassDetail.h" 15 #include "mlir/Conversion/MemRefToSPIRV/MemRefToSPIRV.h" 16 #include "mlir/Dialect/SPIRV/IR/SPIRVDialect.h" 17 #include "mlir/Dialect/SPIRV/Transforms/SPIRVConversion.h" 18 19 using namespace mlir; 20 21 namespace { 22 /// A pass converting MLIR MemRef operations into the SPIR-V dialect. 23 class ConvertMemRefToSPIRVPass 24 : public ConvertMemRefToSPIRVBase<ConvertMemRefToSPIRVPass> { 25 void runOnOperation() override; 26 }; 27 } // namespace 28 29 void ConvertMemRefToSPIRVPass::runOnOperation() { 30 MLIRContext *context = &getContext(); 31 ModuleOp module = getOperation(); 32 33 auto targetAttr = spirv::lookupTargetEnvOrDefault(module); 34 std::unique_ptr<ConversionTarget> target = 35 SPIRVConversionTarget::get(targetAttr); 36 37 SPIRVTypeConverter::Options options; 38 options.boolNumBits = this->boolNumBits; 39 SPIRVTypeConverter typeConverter(targetAttr, options); 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 populateMemRefToSPIRVPatterns(typeConverter, patterns); 54 55 if (failed(applyPartialConversion(module, *target, std::move(patterns)))) 56 return signalPassFailure(); 57 } 58 59 std::unique_ptr<OperationPass<ModuleOp>> 60 mlir::createConvertMemRefToSPIRVPass() { 61 return std::make_unique<ConvertMemRefToSPIRVPass>(); 62 } 63