|
| 1 | +/* |
| 2 | + * Copyright (c) 2023 - 2026 Chair for Design Automation, TUM |
| 3 | + * Copyright (c) 2025 - 2026 Munich Quantum Software Company GmbH |
| 4 | + * All rights reserved. |
| 5 | + * |
| 6 | + * SPDX-License-Identifier: MIT |
| 7 | + * |
| 8 | + * Licensed under the MIT License |
| 9 | + */ |
| 10 | + |
| 11 | +#include "mlir/Dialect/QTensor/Utils/TensorIterator.h" |
| 12 | + |
| 13 | +#include "mlir/Dialect/QCO/IR/QCOOps.h" |
| 14 | +#include "mlir/Dialect/QTensor/IR/QTensorOps.h" |
| 15 | + |
| 16 | +#include <llvm/ADT/STLExtras.h> |
| 17 | +#include <llvm/ADT/TypeSwitch.h> |
| 18 | +#include <llvm/Support/ErrorHandling.h> |
| 19 | +#include <mlir/Dialect/SCF/IR/SCF.h> |
| 20 | +#include <mlir/IR/Builders.h> |
| 21 | +#include <mlir/IR/Value.h> |
| 22 | +#include <mlir/Support/LLVM.h> |
| 23 | + |
| 24 | +#include <cassert> |
| 25 | +#include <iterator> |
| 26 | + |
| 27 | +namespace mlir::qtensor { |
| 28 | +TypedValue<RankedTensorType> TensorIterator::tensor() const { |
| 29 | + if (op_ == nullptr) { |
| 30 | + return tensor_; |
| 31 | + } |
| 32 | + |
| 33 | + // The following operations don't have an OpResult. |
| 34 | + if (isa<DeallocOp, scf::YieldOp, qco::YieldOp>(op_)) { |
| 35 | + return nullptr; |
| 36 | + } |
| 37 | + |
| 38 | + return tensor_; |
| 39 | +} |
| 40 | + |
| 41 | +void TensorIterator::forward() { |
| 42 | + // If the iterator is a sentinel already, there is nothing to do. |
| 43 | + if (isSentinel_) { |
| 44 | + return; |
| 45 | + } |
| 46 | + |
| 47 | + // Find the user-operation of the tensor SSA value. |
| 48 | + assert(tensor_.hasOneUse() && "expected linear typing"); |
| 49 | + op_ = *(tensor_.user_begin()); |
| 50 | + |
| 51 | + // The following operations define the end of the tensor's life-chain. |
| 52 | + if (isa<DeallocOp, scf::YieldOp, qco::YieldOp>(op_)) { |
| 53 | + isSentinel_ = true; |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + // Find the output from the input tensor SSA value. |
| 58 | + if (!(isa<AllocOp, FromElementsOp>(op_))) { |
| 59 | + TypeSwitch<Operation*>(op_) |
| 60 | + .Case<ExtractOp>([&](ExtractOp op) { tensor_ = op.getOutTensor(); }) |
| 61 | + .Case<InsertOp>([&](InsertOp op) { tensor_ = op.getResult(); }) |
| 62 | + .Case<scf::ForOp>([&](scf::ForOp op) { |
| 63 | + tensor_ = cast<TypedValue<RankedTensorType>>( |
| 64 | + op.getTiedLoopResult(&*(tensor_.use_begin()))); |
| 65 | + }) |
| 66 | + .Case<qco::IfOp>([&](qco::IfOp op) { |
| 67 | + auto it = llvm::find(op.getQubits(), tensor_); |
| 68 | + assert(it != op.getQubits().end()); |
| 69 | + const auto idx = std::distance(op.getQubits().begin(), it); |
| 70 | + tensor_ = cast<TypedValue<RankedTensorType>>(op.getResults()[idx]); |
| 71 | + }) |
| 72 | + .Default([&](Operation* op) { |
| 73 | + report_fatal_error("unknown op in def-use chain: " + |
| 74 | + op->getName().getStringRef()); |
| 75 | + }); |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +void TensorIterator::backward() { |
| 80 | + // If the iterator is a sentinel, reactivate the iterator. |
| 81 | + if (isSentinel_) { |
| 82 | + isSentinel_ = false; |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + // If the op is a nullptr, the tensor value is a block argument and thus the |
| 87 | + // beginning of the tensor's life-chain. |
| 88 | + if (op_ == nullptr) { |
| 89 | + return; |
| 90 | + } |
| 91 | + |
| 92 | + // For these operations, tensor_ is an OpOperand. Hence, only get the def-op. |
| 93 | + if (isa<DeallocOp, scf::YieldOp, qco::YieldOp>(op_)) { |
| 94 | + op_ = tensor_.getDefiningOp(); |
| 95 | + return; |
| 96 | + } |
| 97 | + |
| 98 | + // Allocations and FromElements define the start of the tensor's life-chain. |
| 99 | + // Consequently, stop and early exit. |
| 100 | + if (isa<AllocOp, FromElementsOp>(op_)) { |
| 101 | + return; |
| 102 | + } |
| 103 | + |
| 104 | + // Find the input from the output tensor SSA value. |
| 105 | + TypeSwitch<Operation*>(op_) |
| 106 | + .Case<ExtractOp>([&](ExtractOp op) { tensor_ = op.getTensor(); }) |
| 107 | + .Case<InsertOp>([&](InsertOp op) { tensor_ = op.getDest(); }) |
| 108 | + .Case<scf::ForOp>([&](scf::ForOp op) { |
| 109 | + if (auto res = dyn_cast<OpResult>(tensor_)) { |
| 110 | + OpOperand* operand = op.getTiedLoopInit(res); |
| 111 | + tensor_ = cast<TypedValue<RankedTensorType>>(operand->get()); |
| 112 | + return; |
| 113 | + } |
| 114 | + |
| 115 | + llvm::reportFatalInternalError( |
| 116 | + "expected scf.for result for tied init lookup"); |
| 117 | + }) |
| 118 | + .Case<qco::IfOp>([&](qco::IfOp op) { |
| 119 | + if (auto res = dyn_cast<OpResult>(tensor_)) { |
| 120 | + auto it = llvm::find(op.getResults(), res); |
| 121 | + assert(it != op->result_end()); |
| 122 | + const auto idx = std::distance(op.result_begin(), it); |
| 123 | + tensor_ = cast<TypedValue<RankedTensorType>>(op.getQubits()[idx]); |
| 124 | + return; |
| 125 | + } |
| 126 | + |
| 127 | + llvm::reportFatalInternalError( |
| 128 | + "expected scf.for result for tied init lookup"); |
| 129 | + }) |
| 130 | + .Default([&](Operation* op) { |
| 131 | + llvm::reportFatalInternalError("unknown op in def-use chain: " + |
| 132 | + op->getName().getStringRef()); |
| 133 | + }); |
| 134 | + |
| 135 | + // Get the operation that produces the tensor value. |
| 136 | + // If the current tensor SSA value is a BlockArgument (no defining op), the |
| 137 | + // operation will be a nullptr. |
| 138 | + op_ = tensor_.getDefiningOp(); |
| 139 | +} |
| 140 | + |
| 141 | +static_assert(std::bidirectional_iterator<TensorIterator>); |
| 142 | +static_assert(std::sentinel_for<std::default_sentinel_t, TensorIterator>, |
| 143 | + "std::default_sentinel_t must be a sentinel for TensorIterator."); |
| 144 | +} // namespace mlir::qtensor |
0 commit comments