Skip to content

Improve memref vectorization to not use larger type if not needed - #3915

Merged
ThomasRaoux merged 2 commits into
iree-org:mainfrom
ThomasRaoux:memref_vectorization_f16
Nov 19, 2020
Merged

ThomasRaoux merged 2 commits into
iree-org:mainfrom
ThomasRaoux:memref_vectorization_f16

Conversation

@ThomasRaoux

Copy link
Copy Markdown
Contributor

This allows better code generation for f16 cases as it can allow skipping costly back and forth casting.

(kMaxVectorizationSizeInBits % memrefType.getElementTypeBitWidth() ==
0) &&
!ShapedType::isDynamic(memrefType.getShape().back()) &&
getUsesIfAllTransferOp(v, uses)) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is this function from?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same file, line 35. Note that this part of the code is just moved.

Type elemType = type.getElementType();
// If the vector we need to generate is bigger than the the max vector size
// allowed for loads use a larger element type.
if (numElements > kMaxVectorNumElements) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am actually not sure what is happening here. Based on the number of elements, the type is changing. That is counter-intuitive to me.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the idea is that before we were trying to convert everything to memref<..vec<4xi32>>, this change is keeping the original type if the vectorization size we want is smaller. For instance if we are loading vec<4xf16>, the memref would get converted to memref<..vec<4xf16>> but if we are trying load vec<8xf16> this will create a memref<...vec<4xf32>> and cast the result of the load to vec<4xf16>. This can useful because sometimes we a type smaller than 4bytes and we still want to load it as a larger type and cast it to get better memory access patterns.
I can add more comments if you think that is what is missing or we can chat more about the logic.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why cant we convert to memref<...<vec<8xf16>>?

It might be easier to this in the analysis itself. Figure out the memref type to use for the allocation, and adjust the loads appropriately?

Let me know if you want to chat offline?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why cant we convert to memref<...<vec<8xf16>>?

It might be easier to this in the analysis itself. Figure out the memref type to use for the allocation, and adjust the loads appropriately?

Let me know if you want to chat offline?

I added a comment to explain the logic. Hopefully that makes it more clear.

Type elemType = type.getElementType();
// If the vector we need to generate is bigger than the the max vector size
// allowed for loads use a larger element type.
if (numElements > kMaxVectorNumElements) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why cant we convert to memref<...<vec<8xf16>>?

It might be easier to this in the analysis itself. Figure out the memref type to use for the allocation, and adjust the loads appropriately?

Let me know if you want to chat offline?

@MaheshRavishankar MaheshRavishankar left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the clarification. Looks good.

@ThomasRaoux
ThomasRaoux merged commit e9ff756 into iree-org:main Nov 19, 2020
@ThomasRaoux
ThomasRaoux deleted the memref_vectorization_f16 branch February 16, 2021 23:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants