-
Notifications
You must be signed in to change notification settings - Fork 68
[Draft] [BACKEND] Enhance the remove layout implementation to reduce the duplicated values with different layout in scf.for. #4527
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -182,7 +182,8 @@ LogicalResult getConvertBackwardSlice( | |||||||||||||||||||||
OpOperand &root, SetVector<Value> &slice, Attribute rootEncoding, | ||||||||||||||||||||||
DenseMap<Value, Attribute> &layout, | ||||||||||||||||||||||
std::function<bool(Operation *)> stopPropagation, | ||||||||||||||||||||||
std::function<Value(OpOperand &, Attribute)> getExistingConversion) { | ||||||||||||||||||||||
std::function<Value(OpOperand &, Attribute)> getExistingConversion, | ||||||||||||||||||||||
bool includeForOp) { | ||||||||||||||||||||||
DenseSet<std::pair<OpOperand *, Attribute>> seen; | ||||||||||||||||||||||
SmallVector<std::pair<OpOperand *, Attribute>> queue; | ||||||||||||||||||||||
|
||||||||||||||||||||||
|
@@ -197,6 +198,12 @@ LogicalResult getConvertBackwardSlice( | |||||||||||||||||||||
|
||||||||||||||||||||||
auto updateLayout = [&](Value value, Attribute encoding) { | ||||||||||||||||||||||
assert(isTensorOrTensorPointerType(value.getType())); | ||||||||||||||||||||||
auto tensorType = getRankedTensorType(value.getType()); | ||||||||||||||||||||||
auto originEncoding = tensorType.getEncoding(); | ||||||||||||||||||||||
if (originEncoding == encoding) { | ||||||||||||||||||||||
return success(); | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
slice.insert(value); | ||||||||||||||||||||||
Attribute &existing = layout[value]; | ||||||||||||||||||||||
if (existing && existing != encoding) | ||||||||||||||||||||||
|
@@ -213,8 +220,38 @@ LogicalResult getConvertBackwardSlice( | |||||||||||||||||||||
continue; | ||||||||||||||||||||||
// Skip propagating through for op results for now. | ||||||||||||||||||||||
// TODO: enable this based on needs. | ||||||||||||||||||||||
if (currentValue.getDefiningOp<scf::ForOp>()) | ||||||||||||||||||||||
if (auto forOp = currentValue.getDefiningOp<scf::ForOp>()) { | ||||||||||||||||||||||
if (!includeForOp) | ||||||||||||||||||||||
return failure(); | ||||||||||||||||||||||
if (stopPropagation && stopPropagation(forOp)) | ||||||||||||||||||||||
continue; | ||||||||||||||||||||||
unsigned argIdx = mlir::cast<OpResult>(currentValue).getResultNumber(); | ||||||||||||||||||||||
int numIndVars = forOp.getNumInductionVars(); | ||||||||||||||||||||||
Block &loopBody = *forOp.getBody(); | ||||||||||||||||||||||
auto blockArg = loopBody.getArgument(argIdx + numIndVars); | ||||||||||||||||||||||
|
||||||||||||||||||||||
Value existing; | ||||||||||||||||||||||
if (getExistingConversion && | ||||||||||||||||||||||
(existing = getExistingConversion(*currentValueUse, encoding))) { | ||||||||||||||||||||||
if (failed(updateLayout(currentValue, encoding))) | ||||||||||||||||||||||
return failure(); | ||||||||||||||||||||||
|
||||||||||||||||||||||
continue; | ||||||||||||||||||||||
} | ||||||||||||||||||||||
return failure(); | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This return statement makes the code below unreachable. The logic for handling initOperand and yieldOperand (lines 243-253) will never execute, which appears to be the main implementation for scf.for support.
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||||||||||||||||||
|
||||||||||||||||||||||
OpOperand *initOperand = forOp.getTiedLoopInit(blockArg); | ||||||||||||||||||||||
OpOperand &yieldOperand = loopBody.getTerminator()->getOpOperand(argIdx); | ||||||||||||||||||||||
llvm::outs() << "johnlu getBackward slice check scf.for initOperand: " | ||||||||||||||||||||||
<< initOperand->get() << "\n"; | ||||||||||||||||||||||
llvm::outs() << "johnlu getBackward slice check scf.for yieldOperand: " | ||||||||||||||||||||||
<< yieldOperand.get() << "\n"; | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Debug output should not be committed to production code. Consider using LLVM_DEBUG macro or removing these debug statements before merging.
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Debug output should not be committed to production code. Consider using LLVM_DEBUG macro or removing these debug statements before merging.
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||||||||||||||||||
if (failed(updateLayout(blockArg, encoding))) | ||||||||||||||||||||||
return failure(); | ||||||||||||||||||||||
enqueue(*initOperand, encoding); | ||||||||||||||||||||||
enqueue(yieldOperand, encoding); | ||||||||||||||||||||||
continue; | ||||||||||||||||||||||
} | ||||||||||||||||||||||
if (failed(updateLayout(currentValue, encoding))) | ||||||||||||||||||||||
return failure(); | ||||||||||||||||||||||
|
||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line appears to map a result to itself when m.first equals m.second, which could be problematic. The mapping logic should ensure proper relationships between old and new ForOp results.
Copilot uses AI. Check for mistakes.