From d6ee6a031063cb99ca9828f1698a60536ffbb38a Mon Sep 17 00:00:00 2001 From: River Riddle Date: Sat, 7 Dec 2019 10:35:01 -0800 Subject: Update the builder API to take ValueRange instead of ArrayRef This allows for users to provide operand_range and result_range in builder.create<> calls, instead of requiring an explicit copy into a separate data structure like SmallVector/std::vector. PiperOrigin-RevId: 284360710 --- mlir/lib/IR/Operation.cpp | 32 ++++++++++++++++++++++++++++++++ mlir/lib/IR/OperationSupport.cpp | 14 +++++++++++++- 2 files changed, 45 insertions(+), 1 deletion(-) (limited to 'mlir/lib/IR') diff --git a/mlir/lib/IR/Operation.cpp b/mlir/lib/IR/Operation.cpp index 3ebc7129305..26f20d324f0 100644 --- a/mlir/lib/IR/Operation.cpp +++ b/mlir/lib/IR/Operation.cpp @@ -323,6 +323,13 @@ void Operation::replaceUsesOfWith(Value *from, Value *to) { operand.set(to); } +/// Replace the current operands of this operation with the ones provided in +/// 'operands'. If the operands list is not resizable, the size of 'operands' +/// must be less than or equal to the current number of operands. +void Operation::setOperands(ValueRange operands) { + getOperandStorage().setOperands(this, operands); +} + //===----------------------------------------------------------------------===// // Diagnostics //===----------------------------------------------------------------------===// @@ -760,6 +767,31 @@ ValueRange::ValueRange(llvm::iterator_range values) } } +/// Drop the first N elements, and keep M elements. +ValueRange ValueRange::slice(unsigned n, unsigned m) const { + assert(n + m <= size() && "Invalid specifier"); + OwnerT newOwner; + if (OpOperand *operand = owner.dyn_cast()) + newOwner = operand + n; + else if (OpResult *result = owner.dyn_cast()) + newOwner = result + n; + else + newOwner = owner.get() + n; + return ValueRange(newOwner, m); +} + +/// Drop the first n elements. +ValueRange ValueRange::drop_front(unsigned n) const { + assert(size() >= n && "Dropping more elements than exist"); + return slice(n, size() - n); +} + +/// Drop the last n elements. +ValueRange ValueRange::drop_back(unsigned n) const { + assert(size() >= n && "Dropping more elements than exist"); + return ValueRange(owner, size() - n); +} + ValueRange::Iterator::Iterator(OwnerT owner, unsigned curIndex) : indexed_accessor_iterator( owner, curIndex) {} diff --git a/mlir/lib/IR/OperationSupport.cpp b/mlir/lib/IR/OperationSupport.cpp index ab665f50c67..2c9a9cce86b 100644 --- a/mlir/lib/IR/OperationSupport.cpp +++ b/mlir/lib/IR/OperationSupport.cpp @@ -50,6 +50,18 @@ OperationState::OperationState(Location location, StringRef name, this->regions.push_back(std::move(r)); } +void OperationState::addOperands(ValueRange newOperands) { + assert(successors.empty() && "Non successor operands should be added first."); + operands.append(newOperands.begin(), newOperands.end()); +} + +void OperationState::addSuccessor(Block *successor, ValueRange succOperands) { + successors.push_back(successor); + // Insert a sentinel operand to mark a barrier between successor operands. + operands.push_back(nullptr); + operands.append(succOperands.begin(), succOperands.end()); +} + Region *OperationState::addRegion() { regions.emplace_back(new Region); return regions.back().get(); @@ -66,7 +78,7 @@ void OperationState::addRegion(std::unique_ptr &®ion) { /// Replace the operands contained in the storage with the ones provided in /// 'operands'. void detail::OperandStorage::setOperands(Operation *owner, - ArrayRef operands) { + ValueRange operands) { // If the number of operands is less than or equal to the current amount, we // can just update in place. if (operands.size() <= numOperands) { -- cgit v1.2.3