| 1 | //===- TosaToSPIRVTosaCustom.cpp - TOSA to SPIR-V Graph/TOSA 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-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file implements patterns to convert TOSA IR to SPIR-V Graph/TOSA. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "mlir/Conversion/TosaToSPIRVTosa/TosaToSPIRVTosa.h" |
| 14 | #include "mlir/Dialect/SPIRV/IR/SPIRVOps.h" |
| 15 | #include "mlir/Dialect/Tosa/IR/TosaOps.h" |
| 16 | #include "mlir/Transforms/DialectConversion.h" |
| 17 | #include "llvm/ADT/STLExtras.h" |
| 18 | #include "llvm/ADT/Sequence.h" |
| 19 | |
| 20 | #define DEBUG_TYPE "tosa-to-spirv-tosa-custom-pattern" |
| 21 | |
| 22 | namespace mlir::tosa { |
| 23 | namespace { |
| 24 | |
| 25 | Value encodeStringAsI8Array(StringRef value, Location loc, |
| 26 | ConversionPatternRewriter &rewriter) { |
| 27 | Type i8Type = rewriter.getIntegerType(width: 8); |
| 28 | // Empty strings are encoded as a single NULL byte because SPIR-V array |
| 29 | // types require at least one element. |
| 30 | StringRef encodedValue = value.empty() ? StringRef("\0" , 1) : value; |
| 31 | |
| 32 | SmallVector<Attribute> bytes; |
| 33 | bytes.reserve(N: encodedValue.size()); |
| 34 | llvm::transform( |
| 35 | Range&: encodedValue, d_first: std::back_inserter(x&: bytes), |
| 36 | F: [&](unsigned char byte) { return IntegerAttr::get(type: i8Type, value: byte); }); |
| 37 | |
| 38 | auto arrayType = |
| 39 | spirv::ArrayType::get(elementType: i8Type, elementCount: static_cast<unsigned>(bytes.size())); |
| 40 | auto arrayValue = ArrayAttr::get(context: rewriter.getContext(), value: bytes); |
| 41 | return spirv::ConstantOp::create(builder&: rewriter, location: loc, constant: arrayType, value: arrayValue); |
| 42 | } |
| 43 | |
| 44 | struct TosaCustomOpConvert final : public OpConversionPattern<tosa::CustomOp> { |
| 45 | TosaCustomOpConvert(const TypeConverter &typeConverter, MLIRContext *context, |
| 46 | llvm::StringMap<int32_t> domainToOpcode) |
| 47 | : OpConversionPattern<tosa::CustomOp>(typeConverter, context), |
| 48 | domainToOpcode(std::move(domainToOpcode)) {} |
| 49 | |
| 50 | LogicalResult |
| 51 | matchAndRewrite(tosa::CustomOp op, tosa::CustomOpAdaptor adaptor, |
| 52 | ConversionPatternRewriter &rewriter) const override { |
| 53 | auto opCode = domainToOpcode.find(Key: op.getDomainName()); |
| 54 | if (opCode == domainToOpcode.end()) |
| 55 | return failure(); |
| 56 | |
| 57 | if (op->getResultTypes().empty()) |
| 58 | return op.emitOpError(message: "with mapped domain requires at least one result" ); |
| 59 | |
| 60 | SmallVector<Type> types; |
| 61 | if (failed(Result: this->getTypeConverter()->convertTypes(types: op->getResultTypes(), |
| 62 | results&: types))) |
| 63 | return rewriter.notifyMatchFailure(arg&: op, msg: "type conversion failed" ); |
| 64 | |
| 65 | Type resultType = |
| 66 | types.size() == 1 ? types.front() : spirv::StructType::get(memberTypes: types); |
| 67 | |
| 68 | Value operatorName = |
| 69 | encodeStringAsI8Array(value: op.getOperatorName(), loc: op.getLoc(), rewriter); |
| 70 | Value implementationAttrsBlob = encodeStringAsI8Array( |
| 71 | value: op.getImplementationAttrs(), loc: op.getLoc(), rewriter); |
| 72 | |
| 73 | SmallVector<Value> inputs = {operatorName, implementationAttrsBlob}; |
| 74 | inputs.append(in_start: adaptor.getInputList().begin(), in_end: adaptor.getInputList().end()); |
| 75 | |
| 76 | Value result = spirv::ExperimentalMLCallOp::create( |
| 77 | builder&: rewriter, location: op.getLoc(), output: resultType, |
| 78 | opcode: rewriter.getI32IntegerAttr(value: opCode->second), parameters: inputs); |
| 79 | |
| 80 | if (types.size() == 1) { |
| 81 | rewriter.replaceOp(op, newValues: result); |
| 82 | return success(); |
| 83 | } |
| 84 | |
| 85 | SmallVector<Value> results; |
| 86 | for (auto index : llvm::seq<int32_t>(Begin: 0, End: types.size())) { |
| 87 | results.push_back(Elt: spirv::CompositeExtractOp::create(builder&: rewriter, location: op.getLoc(), |
| 88 | composite: result, indices: {index})); |
| 89 | } |
| 90 | rewriter.replaceOp(op, newValues: results); |
| 91 | return success(); |
| 92 | } |
| 93 | |
| 94 | private: |
| 95 | llvm::StringMap<int32_t> domainToOpcode; |
| 96 | }; |
| 97 | |
| 98 | } // namespace |
| 99 | |
| 100 | void populateTosaToSPIRVTosaCustomConversionPatterns( |
| 101 | SPIRVTypeConverter &typeConverter, RewritePatternSet &patterns, |
| 102 | llvm::StringMap<int32_t> domainToOpcode) { |
| 103 | patterns.add<TosaCustomOpConvert>(arg&: typeConverter, args: patterns.getContext(), |
| 104 | args: std::move(domainToOpcode)); |
| 105 | } |
| 106 | |
| 107 | } // namespace mlir::tosa |
| 108 | |