MLIR 24.0.0git
TosaToSPIRVTosaConstants.cpp
Go to the documentation of this file.
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
18#include <optional>
19
20namespace mlir {
21#define GEN_PASS_DEF_TOSATOSPIRVTOSAMARKGRAPHCONSTANTS
22#include "mlir/Conversion/Passes.h.inc"
23
24namespace tosa {
25namespace {
26
27constexpr uint32_t maxInlineConstElements = 16;
28constexpr uint32_t maxInlineConstShapeElements = 32;
29
30std::optional<ElementsAttr> getConstantValues(Operation *op) {
31 if (auto constOp = dyn_cast<tosa::ConstOp>(op))
32 return constOp.getValuesAttr();
33 if (auto constShapeOp = dyn_cast<tosa::ConstShapeOp>(op))
34 return constShapeOp.getValuesAttr();
35 return std::nullopt;
36}
37
38bool 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>(op)
47 ? maxInlineConstElements
48 : maxInlineConstShapeElements;
49 return values->size() > maxInlineElements;
50}
51
52void setGraphConstantId(Operation *op, uint32_t id) {
53 auto i32Type = IntegerType::get(op->getContext(), 32);
54 op->setDiscardableAttr(graphARMGraphConstantIdAttrName,
55 IntegerAttr::get(i32Type, id));
56}
57
58struct TosaToSPIRVTosaMarkGraphConstants final
59 : impl::TosaToSPIRVTosaMarkGraphConstantsBase<
60 TosaToSPIRVTosaMarkGraphConstants> {
61 void runOnOperation() override {
62 uint32_t nextConstantId = 0;
63 WalkResult result =
64 getOperation().walk([&](Operation *op) {
65 if (!isa<tosa::ConstOp, tosa::ConstShapeOp>(op))
66 return WalkResult::advance();
67
68 if (op->hasDiscardableAttr(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, nextConstantId++);
78 return WalkResult::advance();
79 });
80
81 if (result.wasInterrupted())
82 signalPassFailure();
83 }
84};
85
86} // namespace
87
89 return std::make_unique<TosaToSPIRVTosaMarkGraphConstants>();
90}
91
92} // namespace tosa
93} // namespace mlir
static WalkResult advance()
Definition WalkResult.h:47
static WalkResult interrupt()
Definition WalkResult.h:46
std::unique_ptr< Pass > createTosaToSPIRVTosaMarkGraphConstants()
constexpr llvm::StringLiteral graphARMGraphConstantIdAttrName
Include the generated interface declarations.