Skip to content

[mlir][ODS] Change get...Mutable to return OpOperand & for single operands #66519

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions mlir/include/mlir/IR/Value.h
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,9 @@ class OpOperand : public IROperand<OpOperand, Value> {
/// Return which operand this is in the OpOperand list of the Operation.
unsigned getOperandNumber();

/// Set the current value being used by this operand.
void assign(Value value) { set(value); }

private:
/// Keep the constructor private and accessible to the OperandStorage class
/// only to avoid hard-to-debug typo/programming mistakes.
Expand Down
3 changes: 3 additions & 0 deletions mlir/include/mlir/IR/ValueRange.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ class MutableOperandRange {
ArrayRef<OperandSegment> operandSegments = std::nullopt);
MutableOperandRange(Operation *owner);

/// Construct a new mutable range for the given OpOperand.
MutableOperandRange(OpOperand &opOperand);

/// Slice this range into a sub range, with the additional operand segment.
MutableOperandRange
slice(unsigned subStart, unsigned subLen,
Expand Down
6 changes: 3 additions & 3 deletions mlir/lib/Dialect/Bufferization/IR/BufferizationOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -537,18 +537,18 @@ LogicalResult DeallocTensorOp::bufferize(RewriterBase &rewriter,

bool MaterializeInDestinationOp::bufferizesToMemoryRead(
OpOperand &opOperand, const AnalysisState &state) {
return &opOperand == &getSourceMutable()[0];
return &opOperand == &getSourceMutable();
}

bool MaterializeInDestinationOp::bufferizesToMemoryWrite(
OpOperand &opOperand, const AnalysisState &state) {
return &opOperand == &getDestMutable()[0];
return &opOperand == &getDestMutable();
}

AliasingValueList
MaterializeInDestinationOp::getAliasingValues(OpOperand &opOperand,
const AnalysisState &state) {
if (&opOperand == &getDestMutable()[0])
if (&opOperand == &getDestMutable())
return {{getOperation()->getResult(0), BufferRelation::Equivalent}};
return {};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -949,7 +949,7 @@ struct FoldReshapeWithGenericOpByExpansion
reshapeOp, "failed preconditions of fusion with producer generic op");
}

if (!controlFoldingReshapes(&reshapeOp.getSrcMutable()[0])) {
if (!controlFoldingReshapes(&reshapeOp.getSrcMutable())) {
return rewriter.notifyMatchFailure(reshapeOp,
"fusion blocked by control function");
}
Expand Down
2 changes: 1 addition & 1 deletion mlir/lib/Dialect/SCF/Transforms/TileUsingInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ mlir::scf::tileAndFuseProducerOfSlice(RewriterBase &rewriter,
// 1. Get the producer of the source (potentially walking through
// `iter_args` of nested `scf.for`)
auto [fusableProducer, destinationInitArg] =
getUntiledProducerFromSliceSource(&candidateSliceOp.getSourceMutable()[0],
getUntiledProducerFromSliceSource(&candidateSliceOp.getSourceMutable(),
loops);
if (!fusableProducer)
return std::nullopt;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -635,11 +635,11 @@ struct InsertSliceOpInterface
RankedTensorType destType = insertSliceOp.getDestType();

// The source is always read.
if (&opOperand == &insertSliceOp.getSourceMutable()[0])
if (&opOperand == &insertSliceOp.getSourceMutable())
return true;

// For the destination, it depends...
assert(&opOperand == &insertSliceOp.getDestMutable()[0] && "expected dest");
assert(&opOperand == &insertSliceOp.getDestMutable() && "expected dest");

// Dest is not read if it is entirely overwritten. E.g.:
// tensor.insert_slice %a into %t[0][10][1] : ... into tensor<10xf32>
Expand Down Expand Up @@ -839,7 +839,7 @@ struct ReshapeOpInterface
bool bufferizesToMemoryRead(Operation *op, OpOperand &opOperand,
const AnalysisState &state) const {
auto reshapeOp = cast<tensor::ReshapeOp>(op);
return &opOperand == &reshapeOp.getShapeMutable()[0];
return &opOperand == &reshapeOp.getShapeMutable();
}

bool bufferizesToMemoryWrite(Operation *op, OpOperand &opOperand,
Expand Down Expand Up @@ -916,7 +916,7 @@ struct ParallelInsertSliceOpInterface
bool bufferizesToMemoryWrite(Operation *op, OpOperand &opOperand,
const AnalysisState &state) const {
auto parallelInsertSliceOp = cast<ParallelInsertSliceOp>(op);
return &opOperand == &parallelInsertSliceOp.getDestMutable()[0];
return &opOperand == &parallelInsertSliceOp.getDestMutable();
}

LogicalResult bufferize(Operation *op, RewriterBase &rewriter,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ struct InsertSliceOpInterface
: public SubsetInsertionOpInterface::ExternalModel<InsertSliceOpInterface,
tensor::InsertSliceOp> {
OpOperand &getSourceOperand(Operation *op) const {
return op->getOpOperand(0);
return cast<tensor::InsertSliceOp>(op).getSourceMutable();
}

bool
Expand Down Expand Up @@ -91,11 +91,11 @@ struct ParallelInsertSliceOpInterface
: public SubsetInsertionOpInterface::ExternalModel<
ParallelInsertSliceOpInterface, tensor::ParallelInsertSliceOp> {
OpOperand &getSourceOperand(Operation *op) const {
return op->getOpOperand(0);
return cast<tensor::ParallelInsertSliceOp>(op).getSourceMutable();
}

OpOperand &getDestinationOperand(Operation *op) const {
return op->getOpOperand(1);
return cast<tensor::ParallelInsertSliceOp>(op).getDestMutable();
}

bool
Expand Down
6 changes: 6 additions & 0 deletions mlir/lib/IR/OperationSupport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,12 @@ MutableOperandRange::MutableOperandRange(
MutableOperandRange::MutableOperandRange(Operation *owner)
: MutableOperandRange(owner, /*start=*/0, owner->getNumOperands()) {}

/// Construct a new mutable range for the given OpOperand.
MutableOperandRange::MutableOperandRange(OpOperand &opOperand)
: MutableOperandRange(opOperand.getOwner(),
/*start=*/opOperand.getOperandNumber(),
/*length=*/1) {}

/// Slice this range into a sub range, with the additional operand segment.
MutableOperandRange
MutableOperandRange::slice(unsigned subStart, unsigned subLen,
Expand Down
2 changes: 1 addition & 1 deletion mlir/test/lib/Dialect/Test/TestDialect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -998,7 +998,7 @@ void LoopBlockOp::getSuccessorRegions(

OperandRange LoopBlockOp::getEntrySuccessorOperands(RegionBranchPoint point) {
assert(point == getBody());
return getInitMutable();
return MutableOperandRange(getInitMutable());
}

//===----------------------------------------------------------------------===//
Expand Down
2 changes: 1 addition & 1 deletion mlir/test/mlir-tblgen/op-decl-and-defs.td
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def NS_AOp : NS_Op<"a_op", [IsolatedFromAbove, IsolatedFromAbove]> {
// CHECK: ::mlir::Operation::operand_range getODSOperands(unsigned index);
// CHECK: ::mlir::TypedValue<::mlir::IntegerType> getA();
// CHECK: ::mlir::Operation::operand_range getB();
// CHECK: ::mlir::MutableOperandRange getAMutable();
// CHECK: ::mlir::OpOperand &getAMutable();
// CHECK: ::mlir::MutableOperandRange getBMutable();
// CHECK: ::mlir::Operation::result_range getODSResults(unsigned index);
// CHECK: ::mlir::TypedValue<::mlir::IntegerType> getR();
Expand Down
24 changes: 18 additions & 6 deletions mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2071,14 +2071,26 @@ void OpEmitter::genNamedOperandSetters() {
continue;
std::string name = op.getGetterName(operand.name);

auto *m = opClass.addMethod(operand.isVariadicOfVariadic()
? "::mlir::MutableOperandRangeRange"
: "::mlir::MutableOperandRange",
name + "Mutable");
StringRef returnType;
if (operand.isVariadicOfVariadic()) {
returnType = "::mlir::MutableOperandRangeRange";
} else if (operand.isVariableLength()) {
returnType = "::mlir::MutableOperandRange";
} else {
returnType = "::mlir::OpOperand &";
}
auto *m = opClass.addMethod(returnType, name + "Mutable");
ERROR_IF_PRUNED(m, name, op);
auto &body = m->body();
body << " auto range = getODSOperandIndexAndLength(" << i << ");\n"
<< " auto mutableRange = "
body << " auto range = getODSOperandIndexAndLength(" << i << ");\n";

if (!operand.isVariadicOfVariadic() && !operand.isVariableLength()) {
// In case of a single operand, return a single OpOperand.
body << " return getOperation()->getOpOperand(range.first);\n";
continue;
}

body << " auto mutableRange = "
"::mlir::MutableOperandRange(getOperation(), "
"range.first, range.second";
if (attrSizedOperands) {
Expand Down