brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.2 KiB · 7ceab39 Raw
93 lines · cpp
1//===- FuncToSPIRV.cpp - Func to SPIR-V Patterns ------------------===//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 patterns to convert Func dialect to SPIR-V dialect.10//11//===----------------------------------------------------------------------===//12 13#include "mlir/Conversion/FuncToSPIRV/FuncToSPIRV.h"14#include "mlir/Dialect/Func/IR/FuncOps.h"15#include "mlir/Dialect/SPIRV/IR/SPIRVOps.h"16#include "mlir/Dialect/SPIRV/Transforms/SPIRVConversion.h"17#include "mlir/Dialect/SPIRV/Utils/LayoutUtils.h"18#include "mlir/IR/AffineMap.h"19 20#define DEBUG_TYPE "func-to-spirv-pattern"21 22using namespace mlir;23 24//===----------------------------------------------------------------------===//25// Operation conversion26//===----------------------------------------------------------------------===//27 28// Note that DRR cannot be used for the patterns in this file: we may need to29// convert type along the way, which requires ConversionPattern. DRR generates30// normal RewritePattern.31 32namespace {33 34/// Converts func.return to spirv.Return.35class ReturnOpPattern final : public OpConversionPattern<func::ReturnOp> {36public:37  using Base::Base;38 39  LogicalResult40  matchAndRewrite(func::ReturnOp returnOp, OpAdaptor adaptor,41                  ConversionPatternRewriter &rewriter) const override {42    if (returnOp.getNumOperands() > 1)43      return failure();44 45    if (returnOp.getNumOperands() == 1) {46      rewriter.replaceOpWithNewOp<spirv::ReturnValueOp>(47          returnOp, adaptor.getOperands()[0]);48    } else {49      rewriter.replaceOpWithNewOp<spirv::ReturnOp>(returnOp);50    }51    return success();52  }53};54 55/// Converts func.call to spirv.FunctionCall.56class CallOpPattern final : public OpConversionPattern<func::CallOp> {57public:58  using Base::Base;59 60  LogicalResult61  matchAndRewrite(func::CallOp callOp, OpAdaptor adaptor,62                  ConversionPatternRewriter &rewriter) const override {63    // multiple results func was not converted to spirv.func64    if (callOp.getNumResults() > 1)65      return failure();66    if (callOp.getNumResults() == 1) {67      auto resultType =68          getTypeConverter()->convertType(callOp.getResult(0).getType());69      if (!resultType)70        return failure();71      rewriter.replaceOpWithNewOp<spirv::FunctionCallOp>(72          callOp, resultType, adaptor.getOperands(), callOp->getAttrs());73    } else {74      rewriter.replaceOpWithNewOp<spirv::FunctionCallOp>(75          callOp, TypeRange(), adaptor.getOperands(), callOp->getAttrs());76    }77    return success();78  }79};80 81} // namespace82 83//===----------------------------------------------------------------------===//84// Pattern population85//===----------------------------------------------------------------------===//86 87void mlir::populateFuncToSPIRVPatterns(const SPIRVTypeConverter &typeConverter,88                                       RewritePatternSet &patterns) {89  MLIRContext *context = patterns.getContext();90 91  patterns.add<ReturnOpPattern, CallOpPattern>(typeConverter, context);92}93