-
Notifications
You must be signed in to change notification settings - Fork 86
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
[aievec] Make transpose lowering more progressive #1574
Open
jsetoain
wants to merge
1
commit into
Xilinx:main
Choose a base branch
from
jsetoain:lower-transpose-through-flat-transpose
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2979,6 +2979,67 @@ struct LowerVectorTransposeOpToAIEVecShuffleOpPattern | |
} | ||
}; | ||
|
||
// Convert a `vector.flat_transpose` op to an `aievec.shuffle` op for AIEml. | ||
struct LowerVectorFlatTransposeOpToAIEVecShuffleOpPattern | ||
: OpConversionPattern<vector::FlatTransposeOp> { | ||
using OpConversionPattern::OpConversionPattern; | ||
LogicalResult | ||
matchAndRewrite(vector::FlatTransposeOp transpOp, OpAdaptor adaptor, | ||
ConversionPatternRewriter &rewriter) const override { | ||
auto rows = transpOp.getRows(); | ||
auto cols = transpOp.getColumns(); | ||
auto resVecTy = cast<VectorType>(transpOp.getResult().getType()); | ||
auto elemTyBitWidth = resVecTy.getElementTypeBitWidth(); | ||
auto vBitWidth = elemTyBitWidth * rows * cols; | ||
|
||
if (vBitWidth != 512) | ||
return failure(); | ||
|
||
if (elemTyBitWidth != 8 && elemTyBitWidth != 16 && elemTyBitWidth != 32) | ||
return failure(); | ||
|
||
auto shuffleMode = aievec::ShuffleMode::T32_4X4; | ||
if (elemTyBitWidth == 8) { | ||
switch (rows) { | ||
case 4: | ||
shuffleMode = aievec::ShuffleMode::T8_4X16; | ||
break; | ||
case 8: | ||
shuffleMode = aievec::ShuffleMode::T8_8X8; | ||
break; | ||
case 16: | ||
shuffleMode = aievec::ShuffleMode::T8_16X4; | ||
break; | ||
default: | ||
return failure(); | ||
} | ||
} else if (elemTyBitWidth == 16) { | ||
switch (rows) { | ||
case 2: | ||
shuffleMode = aievec::ShuffleMode::T16_2X16; | ||
break; | ||
case 4: | ||
shuffleMode = aievec::ShuffleMode::T16_4X8; | ||
break; | ||
case 8: | ||
shuffleMode = aievec::ShuffleMode::T16_8X4; | ||
break; | ||
case 16: | ||
shuffleMode = aievec::ShuffleMode::T16_16X2; | ||
break; | ||
default: | ||
return failure(); | ||
} | ||
} else if (cols != 4) | ||
return failure(); | ||
|
||
rewriter.replaceOpWithNewOp<aievec::ShuffleOp>( | ||
transpOp, resVecTy, adaptor.getMatrix(), nullptr, shuffleMode); | ||
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. operand name is matrix, interesting. |
||
|
||
return success(); | ||
} | ||
}; | ||
|
||
//===----------------------------------------------------------------------===// | ||
// Pattern collection | ||
//===----------------------------------------------------------------------===// | ||
|
@@ -3061,7 +3122,8 @@ static void populateAIEVecV2ConversionPatterns(RewritePatternSet &patterns, | |
ConvertMulAddToAIEVecFMAElemOpPattern, | ||
ConvertVectorFMAOpToAIEVecFMAElemOpPattern, | ||
LowerVectorExtractStridedSliceOpAIE2Pattern, | ||
LowerVectorTransposeOpToAIEVecShuffleOpPattern | ||
LowerVectorTransposeOpToAIEVecShuffleOpPattern, | ||
LowerVectorFlatTransposeOpToAIEVecShuffleOpPattern | ||
>(patterns.getContext()); | ||
patterns.add<LowerVectorContractionOpToAIEVecMatMulPattern | ||
>(patterns.getContext(), backend == TargetBackend::CPP); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -627,6 +627,54 @@ struct ExtractTransposeFromContractionOp | |
} | ||
}; | ||
|
||
// This pattern flattens a `vector.transpose` operation for shapes that can be | ||
// handled by basic AIE shuffle ops. | ||
struct FlattenVectorTransposeOpPattern | ||
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. Can we add a separate lit test for this pattern? Thanks! |
||
: public OpConversionPattern<vector::TransposeOp> { | ||
using OpConversionPattern<vector::TransposeOp>::OpConversionPattern; | ||
|
||
LogicalResult | ||
matchAndRewrite(vector::TransposeOp transpOp, OpAdaptor adaptor, | ||
ConversionPatternRewriter &rewriter) const override { | ||
auto resTy = transpOp.getResultVectorType(); | ||
auto resShape = resTy.getShape(); | ||
auto elemTyBitWidth = resTy.getElementTypeBitWidth(); | ||
auto vBitWidth = std::accumulate(resShape.begin(), resShape.end(), | ||
elemTyBitWidth, std::multiplies<>()); | ||
if (vBitWidth != 512) | ||
return failure(); | ||
|
||
if (elemTyBitWidth != 8 && elemTyBitWidth != 16 && elemTyBitWidth != 32) | ||
return failure(); | ||
|
||
// Verify leading dimensions are all 1. | ||
for (int64_t i = 0; i < static_cast<int64_t>(resShape.size() - 2); ++i) | ||
if (resShape[i] != 1) | ||
return failure(); | ||
|
||
// Only permutation of the 2 innermost dimensions are supported. | ||
ArrayRef<int64_t> perm = transpOp.getPermutation(); | ||
for (int64_t i = 0; i < static_cast<int64_t>(perm.size() - 2); ++i) | ||
if (perm[i] != i) | ||
return failure(); | ||
if (perm.back() != static_cast<int64_t>(perm.size() - 2)) | ||
return failure(); | ||
|
||
auto flatVecTy = | ||
VectorType::get({512 / elemTyBitWidth}, resTy.getElementType()); | ||
auto loc = transpOp.getLoc(); | ||
auto flatInput = rewriter.create<vector::ShapeCastOp>(loc, flatVecTy, | ||
adaptor.getVector()); | ||
auto flatTranspOp = rewriter.create<vector::FlatTransposeOp>( | ||
loc, flatVecTy, flatInput, static_cast<int32_t>(resShape.back()), | ||
static_cast<int32_t>(resShape[resShape.size() - 2])); | ||
rewriter.replaceOpWithNewOp<vector::ShapeCastOp>(transpOp, resTy, | ||
flatTranspOp); | ||
|
||
return success(); | ||
} | ||
}; | ||
|
||
//============================================================================// | ||
//============ AIE2 canonicalization conversion patterns ===============// | ||
//============================================================================// | ||
|
@@ -690,6 +738,32 @@ static void configureAIE2CanonicalizeLegalizations(ConversionTarget &target, | |
[](vector::ContractionOp op) { | ||
return !isGemmBTransposedContractionOp(op); | ||
}); | ||
target.addDynamicallyLegalOp<vector::TransposeOp>([](vector::TransposeOp op) { | ||
auto resTy = op.getResultVectorType(); | ||
auto resShape = resTy.getShape(); | ||
auto elemTyBitWidth = resTy.getElementTypeBitWidth(); | ||
auto vBitWidth = std::accumulate(resShape.begin(), resShape.end(), | ||
elemTyBitWidth, std::multiplies<>()); | ||
if (vBitWidth != 512) | ||
return true; | ||
|
||
if (elemTyBitWidth != 8 && elemTyBitWidth != 16 && elemTyBitWidth != 32) | ||
return true; | ||
|
||
// Verify leading dimensions are all 1. | ||
for (int64_t i = 0; i < static_cast<int64_t>(resShape.size() - 2); ++i) | ||
if (resShape[i] != 1) | ||
return true; | ||
|
||
// Only permutation of the 2 innermost dimensions are supported. | ||
ArrayRef<int64_t> perm = op.getPermutation(); | ||
for (int64_t i = 0; i < static_cast<int64_t>(perm.size() - 2); ++i) | ||
if (perm[i] != i) | ||
return true; | ||
if (perm.back() != static_cast<int64_t>(perm.size() - 2)) | ||
return true; | ||
return false; | ||
}); | ||
} | ||
|
||
static void | ||
|
@@ -699,7 +773,8 @@ populateAIE2CanonicalizeConversionPatterns(RewritePatternSet &patterns, | |
256); | ||
patterns | ||
.add<ExtractTransposeFromContractionOp, FlattenMultDimTransferReadPattern, | ||
FlattenMultDimTransferWritePattern>(patterns.getContext()); | ||
FlattenMultDimTransferWritePattern, FlattenVectorTransposeOpPattern>( | ||
patterns.getContext()); | ||
} | ||
|
||
//============================================================================// | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
We can probably remove the old pattern
LowerVectorTransposeOpToAIEVecShuffleOpPattern
in the future, to reduce the maintenance cost.