17#include "llvm/ADT/EquivalenceClasses.h"
18#include "llvm/Support/DebugLog.h"
26#include "mlir/Interfaces/ControlFlowInterfaces.cpp.inc"
29 : producedOperandCount(0), forwardedOperands(std::move(forwardedOperands)) {
34 : producedOperandCount(producedOperandCount),
35 forwardedOperands(std::move(forwardedOperands)) {}
44std::optional<BlockArgument>
46 unsigned operandIndex,
Block *successor) {
47 LDBG() <<
"Getting branch successor argument for operand index "
48 << operandIndex <<
" in successor block";
52 if (forwardedOperands.empty()) {
53 LDBG() <<
"No forwarded operands, returning nullopt";
59 if (operandIndex < operandsStart ||
60 operandIndex >= (operandsStart + forwardedOperands.size())) {
61 LDBG() <<
"Operand index " << operandIndex <<
" out of range ["
62 << operandsStart <<
", "
63 << (operandsStart + forwardedOperands.size())
64 <<
"), returning nullopt";
71 LDBG() <<
"Computed argument index " << argIndex <<
" for successor block";
79 LDBG() <<
"Verifying branch successor operands for successor #" << succNo
80 <<
" in operation " << op->
getName();
83 unsigned operandCount = operands.
size();
85 LDBG() <<
"Branch has " << operandCount <<
" operands, target block has "
89 return op->
emitError() <<
"branch has " << operandCount
90 <<
" operands for successor #" << succNo
91 <<
", but target block has "
95 LDBG() <<
"Checking type compatibility for "
97 <<
" forwarded operands";
100 Type operandType = operands[i].getType();
102 LDBG() <<
"Checking type compatibility: operand type " << operandType
103 <<
" vs argument type " << argType;
105 if (!cast<BranchOpInterface>(op).areTypesCompatible(operandType, argType))
106 return op->
emitError() <<
"type mismatch for bb argument #" << i
107 <<
" of successor #" << succNo;
110 LDBG() <<
"Branch successor operand verification successful";
120 std::size_t expectedWeightsNum,
121 llvm::StringRef weightAnchorName,
122 llvm::StringRef weightRefName) {
126 if (weights.size() != expectedWeightsNum)
127 return op->
emitError() <<
"expects number of " << weightAnchorName
128 <<
" weights to match number of " << weightRefName
129 <<
": " << weights.size() <<
" vs "
130 << expectedWeightsNum;
132 if (llvm::all_of(weights, [](int32_t value) {
return value == 0; }))
133 return op->
emitError() <<
"branch weights cannot all be zero";
140 cast<WeightedBranchOpInterface>(op).getWeights();
151 cast<WeightedRegionBranchOpInterface>(op).getWeights();
161 auto regionInterface = cast<RegionBranchOpInterface>(op);
166 regionInterface.getAllRegionBranchPoints();
169 regionInterface.getSuccessorRegions(branchPoint, successors);
173 auto emitRegionEdgeError = [&]() {
175 regionInterface->emitOpError(
"along control flow edge from ");
176 if (branchPoint.isParent()) {
178 diag.attachNote(op->
getLoc()) <<
"region branch point";
181 << branchPoint.getTerminatorPredecessorOrNull()->getName();
183 branchPoint.getTerminatorPredecessorOrNull()->getLoc())
184 <<
"region branch point";
187 if (
Region *region = successor.getSuccessor()) {
188 diag <<
"Region #" << region->getRegionNumber();
197 regionInterface.getSuccessorOperands(branchPoint, successor);
198 ValueRange succInputs = regionInterface.getSuccessorInputs(successor);
199 if (succOperands.size() != succInputs.size()) {
200 return emitRegionEdgeError()
201 <<
": region branch point has " << succOperands.size()
202 <<
" operands, but region successor needs " << succInputs.size()
209 for (
const auto &typesIdx :
210 llvm::enumerate(llvm::zip(succOperandTypes, succInputTypes))) {
211 Type succOperandType = std::get<0>(typesIdx.value());
212 Type succInputType = std::get<1>(typesIdx.value());
213 if (!regionInterface.areTypesCompatible(succOperandType, succInputType))
214 return emitRegionEdgeError()
215 <<
": successor operand type #" << typesIdx.index() <<
" "
216 << succOperandType <<
" should match successor input type #"
217 << typesIdx.index() <<
" " << succInputType;
236 auto op = cast<RegionBranchOpInterface>(begin->
getParentOp());
237 LDBG() <<
"Starting region graph traversal from region #"
242 LDBG() <<
"Initialized visited array with " << op->getNumRegions()
247 auto enqueueAllSuccessors = [&](
Region *region) {
248 LDBG() <<
"Enqueuing successors for region #" << region->getRegionNumber();
250 for (
Block &block : *region) {
254 dyn_cast<RegionBranchTerminatorOpInterface>(block.back());
258 operandAttributes.resize(terminator->getNumOperands());
259 terminator.getSuccessorRegions(operandAttributes, successors);
260 LDBG() <<
"Found " << successors.size()
261 <<
" successors from terminator in block";
263 if (!successor.isParent()) {
264 worklist.push_back(successor.getSuccessor());
265 LDBG() <<
"Added region #"
266 << successor.getSuccessor()->getRegionNumber()
269 LDBG() <<
"Skipping parent successor";
274 enqueueAllSuccessors(begin);
275 LDBG() <<
"Initial worklist size: " << worklist.size();
278 while (!worklist.empty()) {
279 Region *nextRegion = worklist.pop_back_val();
281 <<
" from worklist (remaining: " << worklist.size() <<
")";
283 if (stopConditionFn(nextRegion, visited)) {
284 LDBG() <<
"Stop condition met for region #"
289 llvm::errs() <<
"Region " << *nextRegion <<
" has no parent op\n";
294 <<
" already visited, skipping";
300 enqueueAllSuccessors(nextRegion);
303 LDBG() <<
"Traversal completed, returning false";
311 "expected that both regions belong to the same op");
315 return nextRegion == r;
329 LDBG() <<
"Checking if operations are in mutually exclusive regions: "
330 << a->
getName() <<
" and " <<
b->getName();
332 assert(a &&
"expected non-empty operation");
333 assert(
b &&
"expected non-empty operation");
337 LDBG() <<
"Checking branch operation " << branchOp->getName();
340 if (!branchOp->isProperAncestor(
b)) {
341 LDBG() <<
"Operation b is not inside branchOp, checking next ancestor";
343 branchOp = branchOp->getParentOfType<RegionBranchOpInterface>();
347 LDBG() <<
"Both operations are inside branchOp, finding their regions";
351 Region *regionA =
nullptr, *regionB =
nullptr;
352 for (
Region &r : branchOp->getRegions()) {
353 if (r.findAncestorOpInRegion(*a)) {
354 assert(!regionA &&
"already found a region for a");
356 LDBG() <<
"Found region #" << r.
getRegionNumber() <<
" for operation a";
358 if (r.findAncestorOpInRegion(*
b)) {
359 assert(!regionB &&
"already found a region for b");
361 LDBG() <<
"Found region #" << r.getRegionNumber() <<
" for operation b";
364 assert(regionA && regionB &&
"could not find region of op");
366 LDBG() <<
"Region A: #" << regionA->
getRegionNumber() <<
", Region B: #"
367 << regionB->getRegionNumber();
371 bool regionsAreDistinct = (regionA != regionB);
375 LDBG() <<
"Regions distinct: " << regionsAreDistinct
376 <<
", A not reachable from B: " << aNotReachableFromB
377 <<
", B not reachable from A: " << bNotReachableFromA;
379 bool mutuallyExclusive =
380 regionsAreDistinct && aNotReachableFromB && bNotReachableFromA;
381 LDBG() <<
"Operations are mutually exclusive: " << mutuallyExclusive;
383 return mutuallyExclusive;
388 LDBG() <<
"No common RegionBranchOpInterface found, operations are not "
389 "mutually exclusive";
393bool RegionBranchOpInterface::isRepetitiveRegion(
unsigned index) {
394 LDBG() <<
"Checking if region #" <<
index <<
" is repetitive in operation "
395 << getOperation()->getName();
397 Region *region = &getOperation()->getRegion(
index);
400 LDBG() <<
"Region #" <<
index <<
" is repetitive: " << isRepetitive;
404bool RegionBranchOpInterface::hasLoop() {
405 LDBG() <<
"Checking if operation " << getOperation()->getName()
410 LDBG() <<
"Found " << entryRegions.size() <<
" entry regions";
413 if (!successor.isParent()) {
414 LDBG() <<
"Checking entry region #"
415 << successor.getSuccessor()->getRegionNumber() <<
" for loops";
422 return visited[nextRegion->getRegionNumber()];
426 LDBG() <<
"Found loop in entry region #"
427 << successor.getSuccessor()->getRegionNumber();
431 LDBG() <<
"Skipping parent successor";
435 LDBG() <<
"No loops found in operation";
443 return getEntrySuccessorOperands(dest);
448RegionBranchOpInterface::getNonSuccessorInputs(
RegionSuccessor successor) {
453 ValueRange successorInputs = getSuccessorInputs(successor);
454 if (!successorInputs.empty()) {
455 unsigned inputBegin =
457 ? cast<OpResult>(successorInputs.front()).getResultNumber()
458 : cast<BlockArgument>(successorInputs.front()).getArgNumber();
459 results.erase(results.begin() + inputBegin,
460 results.begin() + inputBegin + successorInputs.size());
474 branchOp.getSuccessorRegions(src, successors);
476 OperandRange operands = branchOp.getSuccessorOperands(src, dst);
477 assert(operands.size() == branchOp.getSuccessorInputs(dst).size() &&
478 "expected the same number of operands and inputs");
479 for (
const auto &[operand, input] : llvm::zip_equal(
481 mapping[&operand].push_back(input);
484void RegionBranchOpInterface::getSuccessorOperandInputMapping(
486 std::optional<RegionBranchPoint> src) {
487 if (src.has_value()) {
500 for (
const auto &[operand, inputs] : operandToInputs) {
501 for (
Value input : inputs)
502 inputToOperands[input].push_back(operand);
504 return inputToOperands;
507void RegionBranchOpInterface::getSuccessorInputOperandMapping(
515RegionBranchOpInterface::getAllRegionBranchPoints() {
518 for (
Region ®ion : getOperation()->getRegions()) {
519 for (
Block &block : region) {
522 if (
auto terminator =
523 dyn_cast<RegionBranchTerminatorOpInterface>(block.back()))
531 LDBG() <<
"Finding enclosing repetitive region for operation "
535 LDBG() <<
"Checking region #" << region->getRegionNumber()
536 <<
" in operation " << region->getParentOp()->getName();
539 if (
auto branchOp = dyn_cast<RegionBranchOpInterface>(op)) {
541 <<
"Found RegionBranchOpInterface, checking if region is repetitive";
542 if (branchOp.isRepetitiveRegion(region->getRegionNumber())) {
543 LDBG() <<
"Found repetitive region #" << region->getRegionNumber();
547 LDBG() <<
"Parent operation does not implement RegionBranchOpInterface";
551 LDBG() <<
"No enclosing repetitive region found";
556 LDBG() <<
"Finding enclosing repetitive region for value";
564 if (
auto branchOp = dyn_cast<RegionBranchOpInterface>(op)) {
566 <<
"Found RegionBranchOpInterface, checking if region is repetitive";
572 LDBG() <<
"Parent operation does not implement RegionBranchOpInterface";
577 LDBG() <<
"No enclosing repetitive region found for value";
586 assert((
b.getDefiningOp() == regionBranchOp ||
587 b.getParentRegion()->getParentOp() == regionBranchOp) &&
588 "b must be a region successor input");
601 if (isa<OpResult>(
b))
611 assert(isa<BlockArgument>(
b) &&
"b must be a block argument");
612 return isa<BlockArgument>(a) && cast<BlockArgument>(a).getOwner() ==
613 cast<BlockArgument>(
b).getOwner();
669 std::optional<unsigned> maxReachableValues = std::nullopt) {
670 assert(inputToOperands.contains(value) &&
"value must be a successor input");
671 llvm::SmallDenseSet<Value> visited;
673 worklist.push_back(value);
674 while (!worklist.empty()) {
675 Value next = worklist.pop_back_val();
676 auto it = inputToOperands.find(next);
677 if (it == inputToOperands.end()) {
679 if (maxReachableValues &&
result.size() > *maxReachableValues)
684 if (visited.insert(operand->
get()).second)
685 worklist.push_back(operand->
get());
718struct MakeRegionBranchOpSuccessorInputsDead :
public RewritePattern {
719 MakeRegionBranchOpSuccessorInputsDead(MLIRContext *context, StringRef name,
720 PatternBenefit benefit = 1)
721 : RewritePattern(name, benefit, context) {}
723 LogicalResult matchAndRewrite(Operation *op,
724 PatternRewriter &rewriter)
const override {
725 assert(!op->
hasTrait<OpTrait::IsIsolatedFromAbove>() &&
726 "isolated-from-above ops are not supported");
729 auto regionBranchOp = cast<RegionBranchOpInterface>(op);
731 regionBranchOp.getSuccessorInputOperandMapping(inputToOperands);
734 bool changed =
false;
735 for (Value value : inputToOperands.keys()) {
737 if (value.use_empty())
741 llvm::SmallDenseSet<Value> reachableValues;
743 reachableValues, value, inputToOperands,
745 reachableValues.empty())
747 assert(*reachableValues.begin() != value &&
748 "successor inputs are supposed to be excluded");
770template <
typename MappingTy,
typename KeyTy>
771static BitVector &lookupOrCreateBitVector(MappingTy &mapping, KeyTy key,
773 return mapping.try_emplace(key, size,
false).first->second;
786static llvm::EquivalenceClasses<Value> computeTiedSuccessorInputs(
788 llvm::EquivalenceClasses<Value> tiedSuccessorInputs;
789 for (
const auto &[operand, inputs] : operandToInputs) {
790 assert(!inputs.empty() &&
"expected non-empty inputs");
791 Value firstInput = inputs.front();
792 tiedSuccessorInputs.insert(firstInput);
793 for (
Value nextInput : llvm::drop_begin(inputs)) {
796 tiedSuccessorInputs.unionSets(firstInput, nextInput);
799 return tiedSuccessorInputs;
841struct RemoveDeadRegionBranchOpSuccessorInputs :
public RewritePattern {
842 RemoveDeadRegionBranchOpSuccessorInputs(MLIRContext *context, StringRef name,
843 PatternBenefit benefit = 1)
844 : RewritePattern(name, benefit, context) {}
846 LogicalResult matchAndRewrite(Operation *op,
847 PatternRewriter &rewriter)
const override {
848 assert(!op->
hasTrait<OpTrait::IsIsolatedFromAbove>() &&
849 "isolated-from-above ops are not supported");
854 auto regionBranchOp = cast<RegionBranchOpInterface>(op);
856 regionBranchOp.getSuccessorOperandInputMapping(operandToInputs);
857 llvm::EquivalenceClasses<Value> tiedSuccessorInputs =
858 computeTiedSuccessorInputs(operandToInputs);
861 SmallVector<Value> valuesToRemove;
863 BitVector resultsToRemove(regionBranchOp->getNumResults(),
false);
865 for (
auto it = tiedSuccessorInputs.begin(), e = tiedSuccessorInputs.end();
867 if (!(*it)->isLeader())
873 for (
auto memberIt = tiedSuccessorInputs.member_begin(**it);
874 memberIt != tiedSuccessorInputs.member_end(); ++memberIt) {
876 if (!memberIt->use_empty()) {
886 for (
auto memberIt = tiedSuccessorInputs.member_begin(**it);
887 memberIt != tiedSuccessorInputs.member_end(); ++memberIt) {
888 if (
auto arg = dyn_cast<BlockArgument>(*memberIt)) {
891 lookupOrCreateBitVector(blockArgsToRemove, arg.getOwner(),
892 arg.getOwner()->getNumArguments());
893 vector.set(arg.getArgNumber());
896 OpResult
result = cast<OpResult>(*memberIt);
897 assert(
result.getDefiningOp() == regionBranchOp &&
898 "result must be a region branch op result");
899 resultsToRemove.set(
result.getResultNumber());
901 valuesToRemove.push_back(*memberIt);
905 if (valuesToRemove.empty())
912 for (Value value : valuesToRemove) {
913 for (OpOperand *operand : inputsToOperands[value]) {
916 lookupOrCreateBitVector(operandsToRemove, operand->getOwner(),
917 operand->getOwner()->getNumOperands());
918 vector.set(operand->getOperandNumber());
923 for (
auto &pair : operandsToRemove) {
924 Operation *op = pair.first;
925 BitVector &operands = pair.second;
930 for (
auto &pair : blockArgsToRemove) {
931 Block *block = pair.first;
932 BitVector &blockArg = pair.second;
934 [&]() { block->eraseArguments(blockArg); });
938 if (resultsToRemove.any())
948 if (
auto arg = dyn_cast<BlockArgument>(value))
949 return arg.getOwner();
954static unsigned getArgOrResultNumber(
Value value) {
955 if (
auto opResult = llvm::dyn_cast<OpResult>(value))
956 return opResult.getResultNumber();
957 return llvm::cast<BlockArgument>(value).getArgNumber();
992 RemoveDuplicateSuccessorInputUses(MLIRContext *context, StringRef name,
993 PatternBenefit benefit = 1)
994 : RewritePattern(name, benefit, context) {}
996 LogicalResult matchAndRewrite(Operation *op,
997 PatternRewriter &rewriter)
const override {
998 assert(!op->
hasTrait<OpTrait::IsIsolatedFromAbove>() &&
999 "isolated-from-above ops are not supported");
1007 auto regionBranchOp = cast<RegionBranchOpInterface>(op);
1009 regionBranchOp.getSuccessorInputOperandMapping(inputsToOperands);
1010 SmallVector<Value> inputs = llvm::to_vector(inputsToOperands.keys());
1011 llvm::sort(inputs, [](Value a, Value
b) {
1012 return getArgOrResultNumber(a) < getArgOrResultNumber(
b);
1025 using SigEntry = std::pair<Operation *, Value>;
1026 using Signature = SmallVector<SigEntry>;
1027 auto sigEntryLess = [](
const SigEntry &a,
const SigEntry &
b) {
1028 if (a.first !=
b.first)
1029 return a.first <
b.first;
1035 using MapKey = std::pair<Signature, void *>;
1036 auto mapKeyLess = [&](
const MapKey &a,
const MapKey &
b) {
1037 if (a.second !=
b.second)
1038 return a.second <
b.second;
1039 return std::lexicographical_compare(a.first.begin(), a.first.end(),
1040 b.first.begin(),
b.first.end(),
1043 std::map<MapKey, Value,
decltype(mapKeyLess)> signatureToCanonical(
1045 bool changed =
false;
1048 for (Value input : inputs) {
1052 for (OpOperand *operand : inputsToOperands[input])
1053 sig.emplace_back(operand->getOwner(), operand->get());
1054 llvm::sort(sig, sigEntryLess);
1058 auto [it,
inserted] = signatureToCanonical.try_emplace(
1059 MapKey{std::move(sig), owner}, input);
1061 Value canonical = it->second;
1063 if (input.use_empty())
1077 return llvm::map_to_vector(values, [](
Value value) {
1088getSuccessorRegionsWithAttrs(RegionBranchOpInterface op,
1092 op.getEntrySuccessorRegions(extractConstants(op->getOperands()),
1096 RegionBranchTerminatorOpInterface terminator =
1098 terminator.getSuccessorRegions(extractConstants(terminator->getOperands()),
1123computeSingleAcyclicRegionBranchPath(RegionBranchOpInterface op) {
1124 llvm::SmallDenseSet<Region *> visited;
1131 getSuccessorRegionsWithAttrs(op, next);
1132 if (successors.size() != 1) {
1137 path.push_back(successors.front());
1138 if (successors.front().isParent()) {
1142 Region *region = successors.front().getSuccessor();
1148 if (!visited.insert(region).second) {
1153 dyn_cast<RegionBranchTerminatorOpInterface>(®ion->
front().
back());
1161 llvm_unreachable(
"expected to return from loop");
1199 InlineRegionBranchOp(MLIRContext *context, StringRef name,
1202 : RewritePattern(name, benefit, context), replBuilderFn(replBuilderFn),
1203 matcherFn(matcherFn) {}
1205 LogicalResult matchAndRewrite(Operation *op,
1206 PatternRewriter &rewriter)
const override {
1208 if (
failed(matcherFn(op)))
1213 if (!op->
hasTrait<OpTrait::HasRecursiveMemoryEffects>())
1215 op,
"pattern not applicable to ops without recursive memory effects");
1218 auto regionBranchOp = cast<RegionBranchOpInterface>(op);
1219 SmallVector<RegionSuccessor> path =
1220 computeSingleAcyclicRegionBranchPath(regionBranchOp);
1223 op,
"failed to find acyclic region branch path");
1227 ArrayRef remainingPath = path;
1228 SmallVector<Value> successorOperands = llvm::to_vector(
1229 regionBranchOp.getEntrySuccessorOperands(remainingPath.front()));
1230 while (!remainingPath.empty()) {
1231 RegionSuccessor nextSuccessor = remainingPath.consume_front();
1233 regionBranchOp.getSuccessorInputs(nextSuccessor);
1234 assert(successorInputs.size() == successorOperands.size() &&
1238 unsigned firstSuccessorInputIdx = 0;
1239 if (!successorInputs.empty())
1240 firstSuccessorInputIdx =
1242 ? cast<OpResult>(successorInputs.front()).getResultNumber()
1243 : cast<BlockArgument>(successorInputs.front()).getArgNumber();
1245 unsigned numValues =
1250 SmallVector<Value> replacements;
1252 auto getValue = [&](
unsigned idx) {
1255 : Value(nextSuccessor.getSuccessor()->getArgument(idx));
1259 for (
unsigned i = 0; i < firstSuccessorInputIdx; ++i)
1260 replacements.push_back(
1261 replBuilderFn(rewriter, op->
getLoc(), getValue(i)));
1264 llvm::append_range(replacements, successorOperands);
1267 for (
unsigned i = replacements.size(); i < numValues; ++i)
1268 replacements.push_back(
1269 replBuilderFn(rewriter, op->
getLoc(), getValue(i)));
1273 assert(remainingPath.empty() &&
"expected that the path ended");
1280 auto terminator = cast<RegionBranchTerminatorOpInterface>(
1285 successorOperands = llvm::to_vector(
1286 terminator.getSuccessorOperands(remainingPath.front()));
1290 llvm_unreachable(
"expected that paths ends with parent");
1300 patterns.
add<MakeRegionBranchOpSuccessorInputsDead,
1301 RemoveDuplicateSuccessorInputUses,
1302 RemoveDeadRegionBranchOpSuccessorInputs>(patterns.
getContext(),
1310 patterns.
add<InlineRegionBranchOp>(patterns.
getContext(), opName,
1311 replBuilderFn, matcherFn, benefit);
static LogicalResult verifyWeights(Operation *op, llvm::ArrayRef< int32_t > weights, std::size_t expectedWeightsNum, llvm::StringRef weightAnchorName, llvm::StringRef weightRefName)
static bool isDefinedBefore(Operation *regionBranchOp, Value a, Value b)
Return "true" if a can be used in lieu of b, where b is a region successor input and a is a "reachabl...
static void getSuccessorOperandInputMapping(RegionBranchOpInterface branchOp, RegionBranchSuccessorMapping &mapping, RegionBranchPoint src)
static bool traverseRegionGraph(Region *begin, StopConditionFn stopConditionFn)
Traverse the region graph starting at begin.
static LogicalResult computeReachableValuesFromSuccessorInput(llvm::SmallDenseSet< Value > &result, Value value, const RegionBranchInverseSuccessorMapping &inputToOperands, std::optional< unsigned > maxReachableValues=std::nullopt)
Compute all non-successor-input values that a successor input could have based on the given successor...
static RegionBranchInverseSuccessorMapping invertRegionBranchSuccessorMapping(const RegionBranchSuccessorMapping &operandToInputs)
function_ref< bool(Region *, ArrayRef< bool > visited)> StopConditionFn
Stop condition for traverseRegionGraph.
static bool isRegionReachable(Region *begin, Region *r)
Return true if region r is reachable from region begin according to the RegionBranchOpInterface (by t...
*if copies could not be generated due to yet unimplemented cases *copyInPlacementStart and copyOutPlacementStart in copyPlacementBlock *specify the insertion points where the incoming copies and outgoing should be inserted(the insertion happens right before the *insertion point). Since `begin` can itself be invalidated due to the memref *rewriting done from this method
static std::string diag(const llvm::Value &value)
static MutableArrayRef< OpOperand > operandsToOpOperands(OperandRange &operands)
static Operation * getOwnerOfValue(Value value)
Attributes are known-constant values of operations.
Block represents an ordered list of Operations.
BlockArgument getArgument(unsigned i)
unsigned getNumArguments()
Operation * getParentOp()
Returns the closest surrounding operation that contains this block.
IRValueT get() const
Return the current value being used by this operand.
This class represents a diagnostic that is inflight and set to be reported.
This class provides a mutable adaptor for a range of operands.
void setInsertionPoint(Block *block, Block::iterator insertPoint)
Set the insertion point to the specified location.
This class represents an operand of an operation.
This class implements the operand iterators for the Operation class.
unsigned getBeginOperandIndex() const
Return the operand index of the first element of this range.
type_range getTypes() const
Operation is the basic unit of execution within MLIR.
bool hasTrait()
Returns true if the operation was registered with a particular trait, e.g.
unsigned getNumSuccessors()
void eraseOperands(unsigned idx, unsigned length=1)
Erase the operands starting at position idx and ending at position 'idx'+'length'.
Block * getBlock()
Returns the operation block that contains this operation.
OpResult getResult(unsigned idx)
Get the 'idx'th result of this operation.
unsigned getNumRegions()
Returns the number of regions held by this operation.
Location getLoc()
The source location the operation was defined or derived from.
Operation * getParentOp()
Returns the closest surrounding operation that contains this operation or nullptr if this is a top-le...
InFlightDiagnostic emitError(const Twine &message={})
Emit an error about fatal conditions with this operation, reporting up to any diagnostic handlers tha...
OpTy getParentOfType()
Return the closest surrounding parent operation that is of type 'OpTy'.
OperationName getName()
The name of an operation is the key identifier for it.
Block * getSuccessor(unsigned index)
Region * getParentRegion()
Returns the region to which the instruction belongs.
unsigned getNumResults()
Return the number of results held by this operation.
This class represents the benefit of a pattern match in a unitless scheme that ranges from 0 (very li...
This class represents a point being branched from in the methods of the RegionBranchOpInterface.
bool isParent() const
Returns true if branching from the parent op.
static constexpr RegionBranchPoint parent()
Returns an instance of RegionBranchPoint representing the parent operation.
RegionBranchTerminatorOpInterface getTerminatorPredecessorOrNull() const
Returns the terminator if branching from a region.
This class represents a successor of a region.
bool isParent() const
Return true if the successor is the parent operation.
Region * getSuccessor() const
Return the given region successor.
This class contains a list of basic blocks and a link to the parent operation it is attached to.
BlockArgListType getArguments()
unsigned getRegionNumber()
Return the number of this region in the parent operation.
unsigned getNumArguments()
Operation * getParentOp()
Return the parent operation this region is attached to.
bool hasOneBlock()
Return true if this region has exactly one block.
MLIRContext * getContext() const
RewritePatternSet & add(ConstructorArg &&arg, ConstructorArgs &&...args)
Add an instance of each of the pattern types 'Ts' to the pattern list with the given arguments.
RewritePattern is the common base class for all DAG to DAG replacements.
virtual void replaceOp(Operation *op, ValueRange newValues)
Replace the results of the given (original) operation with the specified list of values (replacements...
virtual void eraseOp(Operation *op)
This method erases an operation that is known to have no uses.
Operation * eraseOpResults(Operation *op, const BitVector &eraseIndices)
Erase the specified results of the given operation.
virtual void inlineBlockBefore(Block *source, Block *dest, Block::iterator before, ValueRange argValues={})
Inline the operations of block 'source' into block 'dest' before the given position.
std::enable_if_t<!std::is_convertible< CallbackT, Twine >::value, LogicalResult > notifyMatchFailure(Location loc, CallbackT &&reasonCallback)
Used to notify the listener that the IR failed to be rewritten because of a match failure,...
void modifyOpInPlace(Operation *root, CallableT &&callable)
This method is a utility wrapper around an in-place modification of an operation.
virtual void replaceAllUsesWith(Value from, Value to)
Find uses of from and replace them with to.
This class models how operands are forwarded to block arguments in control flow.
SuccessorOperands(MutableOperandRange forwardedOperands)
Constructs a SuccessorOperands with no produced operands that simply forwards operands to the success...
unsigned getProducedOperandCount() const
Returns the amount of operands that are produced internally by the operation.
unsigned size() const
Returns the amount of operands passed to the successor.
OperandRange getForwardedOperands() const
Get the range of operands that are simply forwarded to the successor.
This class provides an abstraction over the various different ranges of value types.
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
This class provides an abstraction over the different types of ranges over Values.
type_range getTypes() const
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Type getType() const
Return the type of this value.
void * getAsOpaquePointer() const
Methods for supporting PointerLikeTypeTraits.
Operation * getDefiningOp() const
If this value is the result of an operation, return the operation that defines it.
Region * getParentRegion()
Return the Region in which this Value is defined.
std::optional< BlockArgument > getBranchSuccessorArgument(const SuccessorOperands &operands, unsigned operandIndex, Block *successor)
Return the BlockArgument corresponding to operand operandIndex in some successor if operandIndex is w...
LogicalResult verifyRegionBranchWeights(Operation *op)
Verify that the region weights attached to an operation implementing WeightedRegiobBranchOpInterface ...
LogicalResult verifyBranchSuccessorOperands(Operation *op, unsigned succNo, const SuccessorOperands &operands)
Verify that the given operands match those of the given successor block.
LogicalResult verifyRegionBranchOpInterface(Operation *op)
Verify that types match along control flow edges described the given op.
LogicalResult verifyBranchWeights(Operation *op)
Verify that the branch weights attached to an operation implementing WeightedBranchOpInterface are co...
Include the generated interface declarations.
bool matchPattern(Value value, const Pattern &pattern)
Entry point for matching a pattern over a Value.
DenseMap< OpOperand *, SmallVector< Value > > RegionBranchSuccessorMapping
A mapping from successor operands to successor inputs.
std::function< LogicalResult(Operation *)> PatternMatcherFn
Helper function for the region branch op inlining pattern that checks if the pattern is applicable to...
bool insideMutuallyExclusiveRegions(Operation *a, Operation *b)
Return true if a and b are in mutually exclusive regions as per RegionBranchOpInterface.
std::function< Value(OpBuilder &, Location, Value)> NonSuccessorInputReplacementBuilderFn
Helper function for the region branch op inlining pattern that builds replacement values for non-succ...
Region * getEnclosingRepetitiveRegion(Operation *op)
Return the first enclosing region of the given op that may be executed repetitively as per RegionBran...
DenseMap< Value, SmallVector< OpOperand * > > RegionBranchInverseSuccessorMapping
llvm::DenseMap< KeyT, ValueT, KeyInfoT, BucketT > DenseMap
void populateRegionBranchOpInterfaceInliningPattern(RewritePatternSet &patterns, StringRef opName, NonSuccessorInputReplacementBuilderFn replBuilderFn=detail::defaultReplBuilderFn, PatternMatcherFn matcherFn=detail::defaultMatcherFn, PatternBenefit benefit=1)
Populate a pattern that inlines the body of region branch ops when there is a single acyclic path thr...
void populateRegionBranchOpInterfaceCanonicalizationPatterns(RewritePatternSet &patterns, StringRef opName, PatternBenefit benefit=1)
Populate canonicalization patterns that simplify successor operands/inputs of region branch operation...
detail::constant_op_matcher m_Constant()
Matches a constant foldable operation.
llvm::function_ref< Fn > function_ref