1995c3984SLei Zhang //===- MathToSPIRVPass.cpp - Math to SPIR-V Passes ------------------------===// 2995c3984SLei Zhang // 3995c3984SLei Zhang // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4995c3984SLei Zhang // See https://llvm.org/LICENSE.txt for license information. 5995c3984SLei Zhang // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6995c3984SLei Zhang // 7995c3984SLei Zhang //===----------------------------------------------------------------------===// 8995c3984SLei Zhang // 9995c3984SLei Zhang // This file implements a pass to convert standard dialect to SPIR-V dialect. 10995c3984SLei Zhang // 11995c3984SLei Zhang //===----------------------------------------------------------------------===// 12995c3984SLei Zhang 13995c3984SLei Zhang #include "mlir/Conversion/MathToSPIRV/MathToSPIRVPass.h" 14995c3984SLei Zhang #include "../PassDetail.h" 15995c3984SLei Zhang #include "mlir/Conversion/MathToSPIRV/MathToSPIRV.h" 16995c3984SLei Zhang #include "mlir/Dialect/SPIRV/IR/SPIRVDialect.h" 17995c3984SLei Zhang #include "mlir/Dialect/SPIRV/Transforms/SPIRVConversion.h" 18995c3984SLei Zhang 19995c3984SLei Zhang using namespace mlir; 20995c3984SLei Zhang 21995c3984SLei Zhang namespace { 22995c3984SLei Zhang /// A pass converting MLIR Math operations into the SPIR-V dialect. 23995c3984SLei Zhang class ConvertMathToSPIRVPass 24995c3984SLei Zhang : public ConvertMathToSPIRVBase<ConvertMathToSPIRVPass> { 25995c3984SLei Zhang void runOnOperation() override; 26995c3984SLei Zhang }; 27995c3984SLei Zhang } // namespace 28995c3984SLei Zhang runOnOperation()29995c3984SLei Zhangvoid ConvertMathToSPIRVPass::runOnOperation() { 30995c3984SLei Zhang MLIRContext *context = &getContext(); 31995c3984SLei Zhang ModuleOp module = getOperation(); 32995c3984SLei Zhang 33995c3984SLei Zhang auto targetAttr = spirv::lookupTargetEnvOrDefault(module); 34995c3984SLei Zhang std::unique_ptr<ConversionTarget> target = 35995c3984SLei Zhang SPIRVConversionTarget::get(targetAttr); 36995c3984SLei Zhang 37995c3984SLei Zhang SPIRVTypeConverter typeConverter(targetAttr); 38995c3984SLei Zhang 39*cc020a22SLei Zhang // Use UnrealizedConversionCast as the bridge so that we don't need to pull 40*cc020a22SLei Zhang // in patterns for other dialects. 41*cc020a22SLei Zhang auto addUnrealizedCast = [](OpBuilder &builder, Type type, ValueRange inputs, 42*cc020a22SLei Zhang Location loc) { 43*cc020a22SLei Zhang auto cast = builder.create<UnrealizedConversionCastOp>(loc, type, inputs); 44*cc020a22SLei Zhang return Optional<Value>(cast.getResult(0)); 45*cc020a22SLei Zhang }; 46*cc020a22SLei Zhang typeConverter.addSourceMaterialization(addUnrealizedCast); 47*cc020a22SLei Zhang typeConverter.addTargetMaterialization(addUnrealizedCast); 48*cc020a22SLei Zhang target->addLegalOp<UnrealizedConversionCastOp>(); 49*cc020a22SLei Zhang 50995c3984SLei Zhang RewritePatternSet patterns(context); 51995c3984SLei Zhang populateMathToSPIRVPatterns(typeConverter, patterns); 52995c3984SLei Zhang 53995c3984SLei Zhang if (failed(applyPartialConversion(module, *target, std::move(patterns)))) 54995c3984SLei Zhang return signalPassFailure(); 55995c3984SLei Zhang } 56995c3984SLei Zhang createConvertMathToSPIRVPass()57995c3984SLei Zhangstd::unique_ptr<OperationPass<ModuleOp>> mlir::createConvertMathToSPIRVPass() { 58995c3984SLei Zhang return std::make_unique<ConvertMathToSPIRVPass>(); 59995c3984SLei Zhang } 60