55 lines · cpp
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-exception6//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/SPIRV/IR/SPIRVDialect.h"17#include "mlir/Dialect/SPIRV/Transforms/SPIRVConversion.h"18 19namespace mlir {20#define GEN_PASS_DEF_CONVERTFUNCTOSPIRVPASS21#include "mlir/Conversion/Passes.h.inc"22} // namespace mlir23 24using namespace mlir;25 26namespace {27/// A pass converting MLIR Func operations into the SPIR-V dialect.28class ConvertFuncToSPIRVPass29 : public impl::ConvertFuncToSPIRVPassBase<ConvertFuncToSPIRVPass> {30 using Base::Base;31 void runOnOperation() override;32};33} // namespace34 35void ConvertFuncToSPIRVPass::runOnOperation() {36 MLIRContext *context = &getContext();37 Operation *op = getOperation();38 39 auto targetAttr = spirv::lookupTargetEnvOrDefault(op);40 std::unique_ptr<ConversionTarget> target =41 SPIRVConversionTarget::get(targetAttr);42 43 SPIRVConversionOptions options;44 options.emulateLT32BitScalarTypes = this->emulateLT32BitScalarTypes;45 options.emulateUnsupportedFloatTypes = this->emulateUnsupportedFloatTypes;46 SPIRVTypeConverter typeConverter(targetAttr, options);47 48 RewritePatternSet patterns(context);49 populateFuncToSPIRVPatterns(typeConverter, patterns);50 populateBuiltinFuncToSPIRVPatterns(typeConverter, patterns);51 52 if (failed(applyPartialConversion(op, *target, std::move(patterns))))53 return signalPassFailure();54}55