11#include "mlir/IR/ImplicitLocOpBuilder.h"
12#include "mlir/Pass/Pass.h"
13#include "llvm/Support/Debug.h"
15#define DEBUG_TYPE "arc-allocate-state"
23#define GEN_PASS_DEF_ALLOCATESTATE
24#include "circt/Dialect/Arc/ArcPasses.h.inc"
32using llvm::SmallMapVector;
40struct AllocateStatePass
41 :
public arc::impl::AllocateStateBase<AllocateStatePass> {
43 using AllocateStateBase::AllocateStateBase;
45 void runOnOperation()
override;
46 void allocateBlock(
Block *block, Value rootStorage);
47 void allocateOps(Value storage,
Block *block, ArrayRef<Operation *> ops,
49 void tapNamedState(AllocStateOp allocOp, IntegerAttr offset);
51 SmallVector<Attribute> traceTaps;
55void AllocateStatePass::runOnOperation() {
58 ModelOp modelOp = getOperation();
59 LLVM_DEBUG(llvm::dbgs() <<
"Allocating state in `" << modelOp.getName()
64 Value rootStorage = modelOp.getBodyBlock().getArgument(0);
65 modelOp.walk([&](Block *block) { allocateBlock(block, rootStorage); });
67 if (!traceTaps.empty())
68 modelOp.setTraceTapsAttr(ArrayAttr::get(modelOp.getContext(), traceTaps));
74void AllocateStatePass::tapNamedState(AllocStateOp allocOp,
77 auto valueType = cast<StateType>(allocOp.getType()).getType();
78 auto typeAttr = TypeAttr::get(valueType);
80 if (
auto namesAttr = allocOp->getAttrOfType<ArrayAttr>(
"names")) {
81 if (!namesAttr.empty()) {
82 tapAttr = TraceTapAttr::get(allocOp.getContext(), typeAttr,
83 offset.getValue().getZExtValue(), namesAttr);
85 }
else if (
auto nameAttr = allocOp->getAttrOfType<StringAttr>(
"name")) {
86 auto arrayAttr = ArrayAttr::get(nameAttr.getContext(), {nameAttr});
87 tapAttr = TraceTapAttr::get(allocOp.getContext(), typeAttr,
88 offset.getValue().getZExtValue(), arrayAttr);
92 traceTaps.push_back(tapAttr);
95 auto indexAttr = IntegerAttr::get(
96 IntegerType::get(allocOp.getContext(), 64,
97 IntegerType::SignednessSemantics::Unsigned),
98 traceTaps.size() - 1);
99 auto modelSymAttr = FlatSymbolRefAttr::get(getOperation().getSymNameAttr());
101 for (
auto *user : allocOp.getResult().getUsers()) {
102 if (
auto writeUser = dyn_cast<StateWriteOp>(user)) {
104 assert(!(writeUser.getTraceTapIndex() || writeUser.getTraceTapModel()));
105 writeUser.setTraceTapIndexAttr(indexAttr);
106 writeUser.setTraceTapModelAttr(modelSymAttr);
111void AllocateStatePass::allocateBlock(Block *block, Value rootStorage) {
112 SmallMapVector<Value, std::vector<Operation *>, 1> opsByStorage;
116 for (
auto &op : *block) {
117 if (isa<AllocStateOp, RootInputOp, RootOutputOp, AllocMemoryOp,
118 AllocStorageOp>(&op))
119 opsByStorage[op.getOperand(0)].push_back(&op);
121 LLVM_DEBUG(llvm::dbgs() <<
"- Visiting block in "
122 << block->getParentOp()->getName() <<
"\n");
126 bool isRootBlock = block == rootStorage.getParentBlock();
127 for (
auto &[storage, ops] : opsByStorage) {
128 unsigned padding = isRootBlock && storage == rootStorage ?
kStateOffset : 0;
129 allocateOps(storage, block, ops, padding);
133void AllocateStatePass::allocateOps(Value storage, Block *block,
134 ArrayRef<Operation *> ops,
136 SmallVector<std::tuple<Value, Value, IntegerAttr>> gettersToCreate;
140 unsigned currentByte = padding;
141 auto allocBytes = [&](
unsigned numBytes) {
142 currentByte = llvm::alignToPowerOf2(
143 currentByte, llvm::bit_ceil(std::min(numBytes, 16U)));
144 unsigned offset = currentByte;
145 currentByte += numBytes;
150 OpBuilder builder(block->getParentOp());
151 for (
auto *op : ops) {
152 if (isa<AllocStateOp, RootInputOp, RootOutputOp>(op)) {
153 auto result = op->getResult(0);
154 auto storage = op->getOperand(0);
155 unsigned numBytes = cast<StateType>(result.getType()).getByteWidth();
156 auto offset = builder.getI32IntegerAttr(allocBytes(numBytes));
157 op->setAttr(
"offset", offset);
158 gettersToCreate.emplace_back(result, storage, offset);
160 if (
auto allocStateOp = dyn_cast<AllocStateOp>(op))
161 tapNamedState(allocStateOp, offset);
165 if (
auto memOp = dyn_cast<AllocMemoryOp>(op)) {
166 auto memType = memOp.getType();
167 unsigned stride = memType.getStride();
168 unsigned numBytes = memType.getNumWords() * stride;
169 auto offset = builder.getI32IntegerAttr(allocBytes(numBytes));
170 op->setAttr(
"offset", offset);
171 op->setAttr(
"stride", builder.getI32IntegerAttr(stride));
172 gettersToCreate.emplace_back(memOp, memOp.getStorage(), offset);
176 if (
auto allocStorageOp = dyn_cast<AllocStorageOp>(op)) {
177 auto offset = builder.getI32IntegerAttr(
178 allocBytes(allocStorageOp.getType().getSize()));
179 allocStorageOp.setOffsetAttr(offset);
180 gettersToCreate.emplace_back(allocStorageOp, allocStorageOp.getInput(),
185 assert(
"unsupported op for allocation" &&
false);
191 DenseMap<Operation *, unsigned> opOrder;
192 block->walk([&](Operation *op) { opOrder.insert({op, opOrder.size()}); });
193 SmallVector<StorageGetOp> getters;
194 for (
auto [result, storage, offset] : gettersToCreate) {
196 for (
auto *user :
llvm::make_early_inc_range(result.getUsers())) {
197 auto &getter = getterForBlock[user->getBlock()];
200 auto userOrder = opOrder.lookup(user);
201 if (!getter || !result.getDefiningOp<AllocStorageOp>()) {
202 ImplicitLocOpBuilder builder(result.getLoc(), user);
204 StorageGetOp::create(builder, result.getType(), storage, offset);
205 getters.push_back(getter);
206 opOrder[getter] = userOrder;
207 }
else if (userOrder < opOrder.lookup(getter)) {
208 getter->moveBefore(user);
209 opOrder[getter] = userOrder;
211 user->replaceUsesOfWith(result, getter);
219 storage.setType(StorageType::get(&getContext(), currentByte));
221 auto substorage = AllocStorageOp::create(
222 builder, block->getParentOp()->getLoc(),
223 StorageType::get(&getContext(), currentByte), storage);
225 op->replaceUsesOfWith(storage, substorage);
226 for (
auto op : getters)
227 op->replaceUsesOfWith(storage, substorage);
static constexpr unsigned kStateOffset
assert(baseType &&"element must be base type")
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.