14#include "mlir/IR/Attributes.h"
15#include "mlir/IR/BuiltinDialect.h"
16#include "mlir/IR/BuiltinTypes.h"
17#include "mlir/IR/Matchers.h"
18#include "mlir/IR/PatternMatch.h"
19#include "mlir/IR/Region.h"
20#include "mlir/IR/Types.h"
21#include "mlir/IR/Value.h"
22#include "llvm/ADT/ArrayRef.h"
23#include "llvm/ADT/SmallVector.h"
30 if (
auto sig = dyn_cast<RefType>(type))
31 type = sig.getNestedType();
32 if (
auto array = dyn_cast<hw::ArrayType>(type))
33 return array.getNumElements();
34 if (
auto tup = dyn_cast<hw::StructType>(type))
35 return tup.getElements().size();
36 return type.getIntOrFloatBitWidth();
40 if (
auto sig = dyn_cast<RefType>(type))
41 type = sig.getNestedType();
42 if (
auto array = dyn_cast<hw::ArrayType>(type))
43 return array.getElementType();
47template <
typename... OpTypes>
49 for (
auto *user : op->getUsers()) {
50 if (isa<OpTypes...>(user))
60OpFoldResult llhd::ConstantTimeOp::fold(FoldAdaptor adaptor) {
61 assert(adaptor.getOperands().empty() &&
"const has no operands");
62 return getValueAttr();
65void llhd::ConstantTimeOp::build(OpBuilder &builder, OperationState &result,
66 uint64_t time,
const StringRef &timeUnit,
67 unsigned delta,
unsigned epsilon) {
68 auto *ctx = builder.getContext();
69 auto attr = TimeAttr::get(ctx, time, timeUnit, delta, epsilon);
70 return build(builder, result, TimeType::get(ctx), attr);
78 unsigned index, Type resultType) {
79 return TypeSwitch<Type, Value>(val.getType())
80 .Case<hw::StructType>([&](hw::StructType ty) -> Value {
82 ty.getElements()[index].name);
84 .Case<hw::ArrayType>([&](hw::ArrayType ty) -> Value {
87 builder.getIntegerType(llvm::Log2_64_Ceil(ty.getNumElements())),
91 .Case<IntegerType>([&](IntegerType ty) -> Value {
93 resultType.getIntOrFloatBitWidth());
99 setNameFn(getResult(), *
getName());
107static std::optional<DenseMap<Attribute, Type>>
109 int width = type.getIntOrFloatBitWidth();
115 SmallVector<bool> startIndices(width,
false);
116 for (Operation *user : value.getUsers()) {
117 if (
auto extract = dyn_cast<SigExtractOp>(user)) {
119 if (matchPattern(extract.getLowBit(), m_ConstantInt(&lowBit))) {
120 startIndices[lowBit.getZExtValue()] =
true;
121 int64_t highBit = lowBit.getZExtValue() + extract.getResultWidth();
123 startIndices[highBit] =
true;
127 if (isa<ProbeOp, DriveOp>(user)) {
137 DenseMap<Attribute, Type> destructured;
138 for (
int start = 0; start < width;) {
140 while (end < width && !startIndices[end])
143 int runLength = end - start;
146 {IntegerAttr::get(IndexType::get(type.getContext()), start),
147 IntegerType::get(type.getContext(), runLength)});
154static std::optional<DenseMap<Attribute, Type>>
159 if (
auto intType = dyn_cast<IntegerType>(type)) {
162 auto destructurable = llvm::dyn_cast<DestructurableTypeInterface>(type);
165 return destructurable.getSubelementIndexMap();
169SmallVector<DestructurableMemorySlot> SignalOp::getDestructurableSlots() {
170 auto type = getType().getNestedType();
174 if (!
hasUserOfKind<SigExtractOp, SigArrayGetOp, SigStructExtractOp,
175 SigArraySliceOp>(*
this))
179 if (!destructuredType)
181 return {DestructurableMemorySlot{{getResult(), type}, *destructuredType}};
184DenseMap<Attribute, MemorySlot> SignalOp::destructure(
185 const DestructurableMemorySlot &slot,
186 const SmallPtrSetImpl<Attribute> &usedIndices, OpBuilder &builder,
187 SmallVectorImpl<DestructurableAllocationOpInterface> &newAllocators) {
188 assert(slot.ptr == getResult());
189 builder.setInsertionPointAfter(*
this);
191 DenseMap<Attribute, Type> subelementTypes = slot.subelementTypes;
192 DenseMap<Attribute, MemorySlot> slotMap;
193 SmallVector<std::pair<unsigned, Type>> indices;
194 for (
auto attr : usedIndices) {
195 assert(isa<IntegerAttr>(attr));
196 auto elemType = subelementTypes.at(attr);
197 assert(elemType &&
"used index must exist");
198 indices.push_back({cast<IntegerAttr>(attr).getInt(), elemType});
201 llvm::sort(indices, [](
auto a,
auto b) {
return a.first < b.first; });
203 for (
auto [index, type] : indices) {
205 auto sigOp = SignalOp::create(builder,
getLoc(), getNameAttr(), init);
206 newAllocators.push_back(sigOp);
207 slotMap.try_emplace<MemorySlot>(
208 IntegerAttr::get(IndexType::get(getContext()), index),
209 {sigOp.getResult(), type});
215std::optional<DestructurableAllocationOpInterface>
216SignalOp::handleDestructuringComplete(
const DestructurableMemorySlot &slot,
217 OpBuilder &builder) {
218 assert(slot.ptr == getResult());
234 if (op.getResultWidth() == op.getInputWidth() &&
235 cast<IntegerAttr>(operands[1]).getValue().isZero())
236 return op.getInput();
241OpFoldResult llhd::SigExtractOp::fold(FoldAdaptor adaptor) {
247 return std::max<int64_t>(0, std::min(a2, b2) - std::max(a1, b1));
251 SmallVectorImpl<std::pair<unsigned, Value>> &sorted) {
252 for (
auto [attr, mem] : subslots) {
253 assert(isa<IntegerAttr>(attr));
254 sorted.push_back({cast<IntegerAttr>(attr).getInt(), mem.ptr});
257 llvm::sort(sorted, [](
auto a,
auto b) {
return a.first < b.first; });
261 SmallVectorImpl<std::pair<unsigned, Type>> &sorted) {
262 for (
auto [attr, mem] : subslots) {
263 assert(isa<IntegerAttr>(attr));
264 sorted.push_back({cast<IntegerAttr>(attr).getInt(), mem});
267 llvm::sort(sorted, [](
auto a,
auto b) {
return a.first < b.first; });
270bool SigExtractOp::canRewire(
const DestructurableMemorySlot &slot,
271 SmallPtrSetImpl<Attribute> &usedIndices,
272 SmallVectorImpl<MemorySlot> &mustBeSafelyUsed,
273 const DataLayout &dataLayout) {
274 if (slot.ptr != getInput())
278 if (!type.isSignlessInteger())
280 if (!matchPattern(getLowBit(), m_ConstantInt(&idx)))
286 for (Operation *user : getResult().getUsers()) {
287 if (!isa<ProbeOp, DriveOp>(user))
291 SmallVector<std::pair<unsigned, Type>> elements;
294 int64_t index = idx.getZExtValue();
296 int64_t coveredBits = 0;
297 for (
auto [start, type] : elements) {
298 int64_t subslotWidth = type.getIntOrFloatBitWidth();
303 usedIndices.insert(IntegerAttr::get(IndexType::get(getContext()), start));
304 coveredBits += overlap;
307 if (coveredBits != width)
310 mustBeSafelyUsed.emplace_back<MemorySlot>({getResult(), type});
314DeletionKind SigExtractOp::rewire(
const DestructurableMemorySlot &slot,
315 DenseMap<Attribute, MemorySlot> &subslots,
317 const DataLayout &dataLayout) {
319 [[maybe_unused]]
bool result = matchPattern(getLowBit(), m_ConstantInt(&idx));
322 int64_t idxVal = idx.getZExtValue();
324 SmallVector<std::pair<unsigned, Value>> elements;
327 for (Operation *user :
llvm::make_early_inc_range(getResult().getUsers())) {
328 builder.setInsertionPoint(user);
330 if (
auto probeOp = dyn_cast<ProbeOp>(user)) {
331 SmallVector<Value> values;
332 for (
auto [start, value] : elements) {
333 int64_t subslotWidth = cast<RefType>(value.getType())
335 .getIntOrFloatBitWidth();
337 start + subslotWidth);
340 values.push_back(ProbeOp::create(builder, probeOp.getLoc(), value));
342 std::reverse(values.begin(), values.end());
343 Value value = comb::ConcatOp::create(builder, probeOp.getLoc(), values);
344 probeOp.replaceAllUsesWith(value);
350 auto driveOp = cast<DriveOp>(user);
351 for (
auto [start, sig] : elements) {
352 int64_t subslotWidth =
353 cast<RefType>(sig.getType()).getNestedType().getIntOrFloatBitWidth();
360 start - idxVal, subslotWidth);
361 DriveOp::create(builder, driveOp.getLoc(), sig, val, driveOp.getTime(),
362 driveOp.getEnable());
367 return DeletionKind::Delete;
370LogicalResult SigExtractOp::ensureOnlySafeAccesses(
371 const MemorySlot &slot, SmallVectorImpl<MemorySlot> &mustBeSafelyUsed,
372 const DataLayout &dataLayout) {
380OpFoldResult llhd::SigArraySliceOp::fold(FoldAdaptor adaptor) {
381 auto lowIndex = dyn_cast_or_null<IntegerAttr>(adaptor.getLowIndex());
386 if (getResultWidth() == getInputWidth() && lowIndex.getValue().isZero())
394 PatternRewriter &rewriter) {
395 IntegerAttr indexAttr;
396 if (!matchPattern(op.getLowIndex(), m_Constant(&indexAttr)))
402 if (matchPattern(op.getInput(),
403 m_Op<Op>(matchers::m_Any(), m_Constant(&a)))) {
404 auto sliceOp = op.getInput().template getDefiningOp<Op>();
405 rewriter.modifyOpInPlace(op, [&]() {
406 op.getInputMutable().assign(sliceOp.getInput());
408 rewriter, op->getLoc(), a.getValue() + indexAttr.getValue());
409 op.getLowIndexMutable().assign(newIndex);
418LogicalResult llhd::SigArraySliceOp::canonicalize(llhd::SigArraySliceOp op,
419 PatternRewriter &rewriter) {
427bool SigArrayGetOp::canRewire(
const DestructurableMemorySlot &slot,
428 SmallPtrSetImpl<Attribute> &usedIndices,
429 SmallVectorImpl<MemorySlot> &mustBeSafelyUsed,
430 const DataLayout &dataLayout) {
431 if (slot.ptr != getInput())
434 if (!matchPattern(getIndex(), m_ConstantInt(&idx)))
437 IntegerAttr::get(IndexType::get(getContext()), idx.getZExtValue());
438 if (!slot.subelementTypes.contains(index))
440 usedIndices.insert(index);
441 mustBeSafelyUsed.emplace_back<MemorySlot>(
442 {getResult(), cast<RefType>(getResult().getType()).getNestedType()});
446DeletionKind SigArrayGetOp::rewire(
const DestructurableMemorySlot &slot,
447 DenseMap<Attribute, MemorySlot> &subslots,
449 const DataLayout &dataLayout) {
451 bool result = matchPattern(getIndex(), m_ConstantInt(&idx));
455 IntegerAttr::get(IndexType::get(getContext()), idx.getZExtValue());
456 auto it = subslots.find(index);
457 assert(it != subslots.end());
458 replaceAllUsesWith(it->getSecond().ptr);
459 return DeletionKind::Delete;
462LogicalResult SigArrayGetOp::ensureOnlySafeAccesses(
463 const MemorySlot &slot, SmallVectorImpl<MemorySlot> &mustBeSafelyUsed,
464 const DataLayout &dataLayout) {
472LogicalResult llhd::SigStructExtractOp::inferReturnTypes(
473 MLIRContext *
context, std::optional<Location> loc, ValueRange operands,
474 DictionaryAttr attrs, mlir::OpaqueProperties properties,
475 mlir::RegionRange regions, SmallVectorImpl<Type> &results) {
476 typename SigStructExtractOp::Adaptor adaptor(operands, attrs, properties,
478 Type type = cast<hw::StructType>(
479 cast<RefType>(adaptor.getInput().getType()).getNestedType())
480 .getFieldType(adaptor.getField());
482 context->getDiagEngine().emit(loc.value_or(UnknownLoc()),
483 DiagnosticSeverity::Error)
484 <<
"invalid field name specified";
487 results.push_back(RefType::get(type));
491bool SigStructExtractOp::canRewire(
492 const DestructurableMemorySlot &slot,
493 SmallPtrSetImpl<Attribute> &usedIndices,
494 SmallVectorImpl<MemorySlot> &mustBeSafelyUsed,
495 const DataLayout &dataLayout) {
496 if (slot.ptr != getInput())
499 cast<hw::StructType>(cast<RefType>(getInput().getType()).getNestedType())
500 .getFieldIndex(getFieldAttr());
503 auto indexAttr = IntegerAttr::get(IndexType::get(getContext()), *index);
504 if (!slot.subelementTypes.contains(indexAttr))
506 usedIndices.insert(indexAttr);
507 mustBeSafelyUsed.emplace_back<MemorySlot>(
508 {getResult(), cast<RefType>(getResult().getType()).getNestedType()});
513SigStructExtractOp::rewire(
const DestructurableMemorySlot &slot,
514 DenseMap<Attribute, MemorySlot> &subslots,
515 OpBuilder &builder,
const DataLayout &dataLayout) {
517 cast<hw::StructType>(cast<RefType>(getInput().getType()).getNestedType())
518 .getFieldIndex(getFieldAttr());
519 assert(index.has_value());
520 auto indexAttr = IntegerAttr::get(IndexType::get(getContext()), *index);
521 auto it = subslots.find(indexAttr);
522 assert(it != subslots.end());
523 replaceAllUsesWith(it->getSecond().ptr);
524 return DeletionKind::Delete;
527LogicalResult SigStructExtractOp::ensureOnlySafeAccesses(
528 const MemorySlot &slot, SmallVectorImpl<MemorySlot> &mustBeSafelyUsed,
529 const DataLayout &dataLayout) {
537bool ProbeOp::canRewire(
const DestructurableMemorySlot &slot,
538 SmallPtrSetImpl<Attribute> &usedIndices,
539 SmallVectorImpl<MemorySlot> &mustBeSafelyUsed,
540 const DataLayout &dataLayout) {
541 for (
auto [key, _] : slot.subelementTypes)
542 usedIndices.insert(key);
544 return isa<hw::StructType, hw::ArrayType, IntegerType>(slot.elemType);
547DeletionKind ProbeOp::rewire(
const DestructurableMemorySlot &slot,
548 DenseMap<Attribute, MemorySlot> &subslots,
549 OpBuilder &builder,
const DataLayout &dataLayout) {
550 SmallVector<std::pair<unsigned, Value>> elements;
551 SmallVector<Value> probed;
553 for (
auto [_, val] : elements)
554 probed.push_back(ProbeOp::create(builder,
getLoc(), val));
557 TypeSwitch<Type, Value>(getType())
558 .Case<hw::StructType>([&](
auto ty) {
562 .Case<hw::ArrayType>([&](
auto ty) {
563 std::reverse(probed.begin(), probed.end());
566 .Case<IntegerType>([&](
auto ty) {
567 std::reverse(probed.begin(), probed.end());
568 return comb::ConcatOp::create(builder,
getLoc(), probed);
571 replaceAllUsesWith(repl);
572 return DeletionKind::Delete;
576ProbeOp::ensureOnlySafeAccesses(
const MemorySlot &slot,
577 SmallVectorImpl<MemorySlot> &mustBeSafelyUsed,
578 const DataLayout &dataLayout) {
582void ProbeOp::getEffects(
583 SmallVectorImpl<SideEffects::EffectInstance<MemoryEffects::Effect>>
585 if (mayHaveSSADominance(*getOperation()->getParentRegion()))
586 effects.emplace_back(MemoryEffects::Read::get(), &getSignalMutable());
593LogicalResult llhd::DriveOp::fold(FoldAdaptor adaptor,
594 SmallVectorImpl<OpFoldResult> &result) {
598 if (matchPattern(getEnable(), m_One())) {
599 getEnableMutable().clear();
606LogicalResult llhd::DriveOp::canonicalize(llhd::DriveOp op,
607 PatternRewriter &rewriter) {
611 if (matchPattern(op.getEnable(), m_Zero())) {
612 rewriter.eraseOp(op);
619bool DriveOp::canRewire(
const DestructurableMemorySlot &slot,
620 SmallPtrSetImpl<Attribute> &usedIndices,
621 SmallVectorImpl<MemorySlot> &mustBeSafelyUsed,
622 const DataLayout &dataLayout) {
623 for (
auto [key, _] : slot.subelementTypes)
624 usedIndices.insert(key);
626 return isa<hw::StructType, hw::ArrayType, IntegerType>(slot.elemType);
629DeletionKind DriveOp::rewire(
const DestructurableMemorySlot &slot,
630 DenseMap<Attribute, MemorySlot> &subslots,
631 OpBuilder &builder,
const DataLayout &dataLayout) {
632 SmallVector<std::pair<unsigned, Value>> driven;
635 for (
auto [idx, sig] : driven) {
636 Type nestedType = cast<RefType>(sig.getType()).getNestedType();
640 getTime(), getEnable());
643 return DeletionKind::Delete;
647DriveOp::ensureOnlySafeAccesses(
const MemorySlot &slot,
648 SmallVectorImpl<MemorySlot> &mustBeSafelyUsed,
649 const DataLayout &dataLayout) {
657LogicalResult ProcessOp::canonicalize(ProcessOp op, PatternRewriter &rewriter) {
658 if (!op.getBody().hasOneBlock())
661 auto &block = op.getBody().front();
662 auto haltOp = dyn_cast<HaltOp>(block.getTerminator());
666 if (op.getNumResults() == 0 && block.getOperations().size() == 1) {
667 rewriter.eraseOp(op);
672 if (!llvm::all_of(block.without_terminator(), [](
auto &bodyOp) {
673 return bodyOp.template hasTrait<OpTrait::ConstantLike>();
677 auto yieldOperands = haltOp.getYieldOperands();
680 llvm::BitVector operandsToErase(yieldOperands.size());
682 for (
auto [operandNo, operand] :
llvm::enumerate(yieldOperands)) {
683 auto *defOp = operand.getDefiningOp();
684 if (defOp && defOp->hasTrait<OpTrait::ConstantLike>()) {
687 if (!defOp->getParentRegion()->isProperAncestor(&op.getBody())) {
688 defOp->moveBefore(op);
690 rewriter.replaceAllUsesWith(op.getResult(operandNo), operand);
691 operandsToErase.set(operandNo);
697 if (!uniqueOperands.contains(operand)) {
698 const auto newPos = uniqueOperands.size();
699 uniqueOperands.insert(std::make_pair(operand, newPos));
700 origToNewPos.insert(std::make_pair(operandNo, newPos));
702 auto firstOccurrencePos = uniqueOperands.lookup(operand);
703 origToNewPos.insert(std::make_pair(operandNo, firstOccurrencePos));
704 operandsToErase.set(operandNo);
708 const auto countOperandsToErase = operandsToErase.count();
709 if (countOperandsToErase == 0)
714 if (countOperandsToErase == op.getNumResults()) {
715 rewriter.eraseOp(op);
719 rewriter.modifyOpInPlace(haltOp,
720 [&] { haltOp->eraseOperands(operandsToErase); });
722 SmallVector<Type> resultTypes = llvm::to_vector(haltOp->getOperandTypes());
723 auto newProcessOp = ProcessOp::create(rewriter, op.getLoc(), resultTypes,
724 op->getOperands(), op->getAttrs());
725 newProcessOp.getBody().takeBody(op.getBody());
728 for (
auto oldResult : op.getResults()) {
729 auto newResultPos = origToNewPos.find(oldResult.getResultNumber());
730 if (newResultPos == origToNewPos.end())
732 auto newResult = newProcessOp.getResult(newResultPos->getSecond());
733 rewriter.replaceAllUsesWith(oldResult, newResult);
736 rewriter.eraseOp(op);
744LogicalResult CombinationalOp::canonicalize(CombinationalOp op,
745 PatternRewriter &rewriter) {
748 if (op.getBody().hasOneBlock() && isMemoryEffectFree(op)) {
749 auto &block = op.getBody().front();
750 auto *terminator = block.getTerminator();
751 rewriter.inlineBlockBefore(&block, op, ValueRange{});
752 rewriter.replaceOp(op, terminator->getOperands());
753 rewriter.eraseOp(terminator);
764 ValueRange yieldOperands) {
766 auto *parentOp = op->getParentOp();
767 TypeRange resultTypes = TypeSwitch<Operation *, TypeRange>(parentOp)
768 .Case<ProcessOp, CombinationalOp>(
769 [](
auto op) {
return op.getResultTypes(); })
770 .Case<FinalOp>([](
auto) {
return TypeRange{}; });
773 if (yieldOperands.size() != resultTypes.size())
774 return op->emitOpError()
775 <<
"has " << yieldOperands.size()
776 <<
" yield operands, but enclosing '" << parentOp->getName()
777 <<
"' returns " << resultTypes.size();
780 for (
unsigned i = 0; i < yieldOperands.size(); ++i)
781 if (yieldOperands[i].getType() != resultTypes[i])
782 return op->emitError()
783 <<
"type of yield operand " << i <<
" ("
784 << yieldOperands[i].getType() <<
") does not match enclosing '"
785 << parentOp->getName() <<
"' result type (" << resultTypes[i]
791LogicalResult WaitOp::verify() {
799LogicalResult HaltOp::verify() {
807LogicalResult YieldOp::verify() {
812struct IntegerTypeInterface
813 :
public DestructurableTypeInterface::ExternalModel<IntegerTypeInterface,
815 std::optional<DenseMap<Attribute, Type>>
829 Type getTypeAtIndex(Type type, Attribute index)
const {
831 llvm_unreachable(
"Not implemented");
837 registry.addExtension(+[](MLIRContext *ctx, BuiltinDialect *dialect) {
838 IntegerType::attachInterface<IntegerTypeInterface>(*ctx);
841 ctx->loadDialect<comb::CombDialect>();
849#define GET_OP_CLASSES
850#include "circt/Dialect/LLHD/IR/LLHD.cpp.inc"
851#include "circt/Dialect/LLHD/IR/LLHDEnums.cpp.inc"
assert(baseType &&"element must be base type")
static std::unique_ptr< Context > context
static void getSortedPtrs(DenseMap< Attribute, MemorySlot > &subslots, SmallVectorImpl< std::pair< unsigned, Value > > &sorted)
static LogicalResult verifyYieldResults(Operation *op, ValueRange yieldOperands)
static bool hasUserOfKind(Operation *op)
static int64_t intervalOverlap(int64_t a1, int64_t a2, int64_t b1, int64_t b2)
static LogicalResult canonicalizeSigPtrArraySliceOp(Op op, PatternRewriter &rewriter)
static std::optional< DenseMap< Attribute, Type > > getIntegerSubelementIndexMap(Type type, Value value)
static std::optional< DenseMap< Attribute, Type > > getSubelementIndexMap(Type type, Value value)
static OpFoldResult foldSigPtrExtractOp(Op op, ArrayRef< Attribute > operands)
static Value getValueAtIndex(OpBuilder &builder, Location loc, Value val, unsigned index, Type resultType)
static Location getLoc(DefSlot slot)
static InstancePath empty
create(elements, Type result_type=None)
StringAttr getName(ArrayAttr names, size_t idx)
Return the name at the specified index of the ArrayAttr or null if it cannot be determined.
unsigned getLLHDTypeWidth(Type type)
Type getLLHDElementType(Type type)
void registerDestructableIntegerExternalModel(mlir::DialectRegistry ®istry)
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
function_ref< void(Value, StringRef)> OpAsmSetValueNameFn