| 1 | //===- TosaToSPIRVTosaConstants.cpp - TOSA graph constants ---------------===// |
|---|---|
| 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 preprocessing that marks TOSA constants that should be |
| 10 | // lowered to SPIR-V Graph constants. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "mlir/Conversion/TosaToSPIRVTosa/TosaToSPIRVTosa.h" |
| 15 | #include "mlir/Dialect/Func/IR/FuncOps.h" |
| 16 | #include "mlir/Dialect/Tosa/IR/TosaOps.h" |
| 17 | #include "mlir/IR/BuiltinAttributes.h" |
| 18 | #include <optional> |
| 19 | |
| 20 | namespace mlir { |
| 21 | #define GEN_PASS_DEF_TOSATOSPIRVTOSAMARKGRAPHCONSTANTS |
| 22 | #include "mlir/Conversion/Passes.h.inc" |
| 23 | |
| 24 | namespace tosa { |
| 25 | namespace { |
| 26 | |
| 27 | constexpr uint32_t maxInlineConstElements = 16; |
| 28 | constexpr uint32_t maxInlineConstShapeElements = 32; |
| 29 | |
| 30 | std::optional<ElementsAttr> getConstantValues(Operation *op) { |
| 31 | if (auto constOp = dyn_cast<tosa::ConstOp>(Val: op)) |
| 32 | return constOp.getValuesAttr(); |
| 33 | if (auto constShapeOp = dyn_cast<tosa::ConstShapeOp>(Val: op)) |
| 34 | return constShapeOp.getValuesAttr(); |
| 35 | return std::nullopt; |
| 36 | } |
| 37 | |
| 38 | bool shouldMarkGraphConstant(Operation *op) { |
| 39 | if (op->use_empty()) |
| 40 | return false; |
| 41 | |
| 42 | std::optional<ElementsAttr> values = getConstantValues(op); |
| 43 | if (!values) |
| 44 | return false; |
| 45 | |
| 46 | uint32_t maxInlineElements = isa<tosa::ConstOp>(Val: op) |
| 47 | ? maxInlineConstElements |
| 48 | : maxInlineConstShapeElements; |
| 49 | return values->size() > maxInlineElements; |
| 50 | } |
| 51 | |
| 52 | void setGraphConstantId(Operation *op, uint32_t id) { |
| 53 | auto i32Type = IntegerType::get(context: op->getContext(), width: 32); |
| 54 | op->setDiscardableAttr(name: graphARMGraphConstantIdAttrName, |
| 55 | value: IntegerAttr::get(type: i32Type, value: id)); |
| 56 | } |
| 57 | |
| 58 | struct TosaToSPIRVTosaMarkGraphConstants final |
| 59 | : impl::TosaToSPIRVTosaMarkGraphConstantsBase< |
| 60 | TosaToSPIRVTosaMarkGraphConstants> { |
| 61 | void runOnOperation() override { |
| 62 | uint32_t nextConstantId = 0; |
| 63 | WalkResult result = |
| 64 | getOperation().walk(callback: [&](Operation *op) { |
| 65 | if (!isa<tosa::ConstOp, tosa::ConstShapeOp>(Val: op)) |
| 66 | return WalkResult::advance(); |
| 67 | |
| 68 | if (op->hasDiscardableAttr(name: graphARMGraphConstantIdAttrName)) { |
| 69 | op->emitOpError() |
| 70 | << "already has `"<< graphARMGraphConstantIdAttrName |
| 71 | << "`; this pass assigns graph constant IDs automatically and " |
| 72 | "does not support pre-marked constants"; |
| 73 | return WalkResult::interrupt(); |
| 74 | } |
| 75 | |
| 76 | if (shouldMarkGraphConstant(op)) |
| 77 | setGraphConstantId(op, id: nextConstantId++); |
| 78 | return WalkResult::advance(); |
| 79 | }); |
| 80 | |
| 81 | if (result.wasInterrupted()) |
| 82 | signalPassFailure(); |
| 83 | } |
| 84 | }; |
| 85 | |
| 86 | } // namespace |
| 87 | |
| 88 | std::unique_ptr<Pass> createTosaToSPIRVTosaMarkGraphConstants() { |
| 89 | return std::make_unique<TosaToSPIRVTosaMarkGraphConstants>(); |
| 90 | } |
| 91 | |
| 92 | } // namespace tosa |
| 93 | } // namespace mlir |
| 94 |