Skip to content

Commit f7560f0

Browse files
committed
[MTHREADS] pick 763599fc
1 parent 11e9e34 commit f7560f0

1 file changed

Lines changed: 45 additions & 3 deletions

File tree

third_party/mthreads/musa/include/TritonMUSACommon/MemDescUtils.h

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,44 @@ inline Value materializeReshapedMemDescForTarget(
504504
return {};
505505
}
506506

507+
inline void setInsertionPointAfterSameBlockDep(RewriterBase &rewriter,
508+
Operation *anchor, Value dep) {
509+
rewriter.setInsertionPoint(anchor);
510+
if (Operation *def = dep.getDefiningOp())
511+
if (def->getBlock() == anchor->getBlock() && anchor->isBeforeInBlock(def))
512+
rewriter.setInsertionPointAfter(def);
513+
}
514+
515+
inline void moveMemDescViewChainAfterDef(ArrayRef<Operation *> directUsers,
516+
Operation *def) {
517+
Block *block = def->getBlock();
518+
SmallVector<Operation *> chain;
519+
SmallPtrSet<Operation *, 8> seen;
520+
SmallVector<Operation *> worklist(directUsers.begin(), directUsers.end());
521+
while (!worklist.empty()) {
522+
Operation *op = worklist.pop_back_val();
523+
if (!seen.insert(op).second)
524+
continue;
525+
if (op->getBlock() != block)
526+
continue;
527+
if (!op->hasTrait<OpTrait::MemDescViewTrait>())
528+
continue;
529+
chain.push_back(op);
530+
llvm::append_range(worklist, op->getUsers());
531+
}
532+
if (chain.empty())
533+
return;
534+
llvm::sort(chain,
535+
[](Operation *a, Operation *b) { return a->isBeforeInBlock(b); });
536+
Operation *anchor = def;
537+
for (Operation *op : chain) {
538+
if (anchor->isBeforeInBlock(op))
539+
continue;
540+
op->moveAfter(anchor);
541+
anchor = op;
542+
}
543+
}
544+
507545
inline bool replaceTensorLocalAllocWithMemDesc(RewriterBase &rewriter,
508546
Operation *user,
509547
Value sourceMemDesc) {
@@ -514,13 +552,17 @@ inline bool replaceTensorLocalAllocWithMemDesc(RewriterBase &rewriter,
514552
if (!targetTy)
515553
return false;
516554
OpBuilder::InsertionGuard guard(rewriter);
517-
rewriter.setInsertionPoint(localAlloc);
555+
setInsertionPointAfterSameBlockDep(rewriter, localAlloc, sourceMemDesc);
518556
Value replacement =
519557
adaptMemDescValue(rewriter, localAlloc.getLoc(), sourceMemDesc, targetTy,
520558
localAlloc.getOperation());
521559
if (!replacement)
522560
return false;
561+
SmallVector<Operation *> users(localAlloc->getUsers().begin(),
562+
localAlloc->getUsers().end());
523563
rewriter.replaceOp(localAlloc, replacement);
564+
if (Operation *def = replacement.getDefiningOp())
565+
moveMemDescViewChainAfterDef(users, def);
524566
return true;
525567
}
526568

@@ -617,7 +659,7 @@ inline bool tryReplaceTensorUserWithMemDesc(RewriterBase &rewriter,
617659
if (!targetTy)
618660
continue;
619661
OpBuilder::InsertionGuard guard(rewriter);
620-
rewriter.setInsertionPoint(localAlloc);
662+
setInsertionPointAfterSameBlockDep(rewriter, localAlloc, sourceMemDesc);
621663
Value replacement = materializeTransformedMemDescForTarget(
622664
rewriter, transOp, sourceMemDesc, targetTy,
623665
localAlloc.getOperation());
@@ -640,7 +682,7 @@ inline bool tryReplaceTensorUserWithMemDesc(RewriterBase &rewriter,
640682
if (!targetTy)
641683
continue;
642684
OpBuilder::InsertionGuard guard(rewriter);
643-
rewriter.setInsertionPoint(localAlloc);
685+
setInsertionPointAfterSameBlockDep(rewriter, localAlloc, sourceMemDesc);
644686
Value replacement = materializeReshapedMemDescForTarget(
645687
rewriter, reshapeOp, sourceMemDesc, targetTy,
646688
localAlloc.getOperation());

0 commit comments

Comments
 (0)