diff --git a/mlir/lib/Dialect/Linalg/Transforms/ComprehensiveBufferize.cpp b/mlir/lib/Dialect/Linalg/Transforms/ComprehensiveBufferize.cpp --- a/mlir/lib/Dialect/Linalg/Transforms/ComprehensiveBufferize.cpp +++ b/mlir/lib/Dialect/Linalg/Transforms/ComprehensiveBufferize.cpp @@ -550,13 +550,11 @@ /// Determine which OpOperand* will alias with `result` if the op is bufferized /// in place. -/// Return None if the owner of `opOperand` does not have known -/// bufferization aliasing behavior, which indicates that the op must allocate -/// all of its tensor results. /// TODO: in the future this may need to evolve towards a list of OpOperand*. -static Optional getAliasingOpOperand(OpResult result) { +static OpOperand *getAliasingOpOperand(OpResult result) { + // Unknown ops are handled conservatively and never bufferize in-place. if (!hasKnownBufferizationAliasingBehavior(result.getDefiningOp())) - return None; + return nullptr; return TypeSwitch(result.getDefiningOp()) .Case([&](tensor::CastOp op) { return &op->getOpOperand(0); }) .Case([&](ConstantOp op) { return &op->getOpOperand(0); }) @@ -1062,13 +1060,13 @@ /// dominance). bool BufferizationAliasInfo::wouldCreateReadAfterWriteInterference( OpResult result, const DominanceInfo &domInfo) const { - Optional maybeAliasingOperand = getAliasingOpOperand(result); - if (!maybeAliasingOperand) + OpOperand *aliasingOperand = getAliasingOpOperand(result); + if (!aliasingOperand) return false; Operation *opToBufferize = result.getDefiningOp(); Value opResult = result; - Value opOperand = (*maybeAliasingOperand)->get(); + Value opOperand = aliasingOperand->get(); LDBG("----Start wouldCreateReadAfterWriteInterference\n"); LDBG("--------consider all aliases to root read: " @@ -1128,8 +1126,8 @@ // %2 = some_alias(%1) // read(%2) // ``` - if (bufferizesToMemoryWrite(**maybeAliasingOperand)) - usesWrite.insert(*maybeAliasingOperand); + if (bufferizesToMemoryWrite(*aliasingOperand)) + usesWrite.insert(aliasingOperand); if (wouldCreateReadAfterWriteInterference(opToBufferize, usesRead, usesWrite, domInfo)) return true; @@ -1277,9 +1275,8 @@ Operation *candidateOp = mit->v.getDefiningOp(); if (!candidateOp) continue; - auto maybeAliasingOperand = getAliasingOpOperand(mit->v.cast()); - if (!maybeAliasingOperand || !*maybeAliasingOperand || - !bufferizesToMemoryWrite(**maybeAliasingOperand)) + OpOperand *operand = getAliasingOpOperand(mit->v.cast()); + if (!operand || !bufferizesToMemoryWrite(*operand)) continue; LDBG("---->clobbering candidate: " << printOperationInfo(candidateOp) << '\n'); @@ -1565,9 +1562,9 @@ bool skipCopy = false) { OpBuilder::InsertionGuard guard(b); Operation *op = result.getOwner(); - Optional maybeOperand = getAliasingOpOperand(result); + OpOperand *maybeOperand = getAliasingOpOperand(result); assert(maybeOperand && "corresponding OpOperand not found"); - Value operand = (*maybeOperand)->get(); + Value operand = maybeOperand->get(); Value operandBuffer = lookup(bvm, operand); assert(operandBuffer && "operand buffer not found");