Skip to content

Introduce a "vector.contract custom kernels" pass - #7778

Merged
bjacob merged 1 commit into
iree-org:mainfrom
bjacob:vector-contract-custom-kernels
Jan 22, 2022
Merged

bjacob merged 1 commit into
iree-org:mainfrom
bjacob:vector-contract-custom-kernels

Conversation

@bjacob

@bjacob bjacob commented Nov 30, 2021 •

Copy link
Copy Markdown
Collaborator

This is refurbishing @asaadaldien's existing
VectorContractToAArch64InlineAsmOp pass, repurposing it to implementing
matrix-times-matrix-transposed kernels, and generalizing it to create a
good insertion point for any other vector.contract custom "handwritten"
kernel, using either inline assembly or intrinsics.

@google-cla google-cla Bot added the cla: yes label Nov 30, 2021
@bjacob bjacob changed the title introduce a "vector.contract custom kernels" pass. Introduce a "vector.contract custom kernels" pass Nov 30, 2021
@bjacob bjacob added codegen/llvm LLVM code generation compiler backend performance ⚡ Performance/optimization related work across the compiler and runtime labels Nov 30, 2021
@bjacob
bjacob marked this pull request as ready for review November 30, 2021 03:40
This was referenced Nov 30, 2021
@bjacob
bjacob force-pushed the vector-contract-custom-kernels branch from d0ddc85 to 657f03d Compare November 30, 2021 15:03

@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.

I left a few comments. Most of them are minor.

My main comment though is that this is flattening to 1D too early. It'd be good to leave it non-1D at this stage and linearize it later. You can add a separate pass before lowering to LLVM for this if you want.

Comment thread CMakeLists.txt Resolved
Comment thread build_tools/cmake/build_android.sh Resolved
Comment thread iree/compiler/Codegen/Passes.h Resolved
@bjacob
bjacob marked this pull request as draft January 10, 2022 18:27
@bjacob
bjacob force-pushed the vector-contract-custom-kernels branch 3 times, most recently from c0bc750 to b535bd8 Compare January 12, 2022 02:16
@bjacob

bjacob commented Jan 12, 2022

Copy link
Copy Markdown
Collaborator Author

I left a few comments. Most of them are minor.

My main comment though is that this is flattening to 1D too early. It'd be good to leave it non-1D at this stage and linearize it later. You can add a separate pass before lowering to LLVM for this if you want.

Done.

@bjacob
bjacob force-pushed the vector-contract-custom-kernels branch 3 times, most recently from 99b39d1 to 9e4e9bc Compare January 13, 2022 03:06
@bjacob
bjacob marked this pull request as ready for review January 13, 2022 03:27
@bjacob
bjacob force-pushed the vector-contract-custom-kernels branch from 3adb49c to 4c5f54e Compare January 14, 2022 16:09

@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.

Nice! Mostly concerned about an LLVM dependency, but otherwise this looks good.

Comment thread iree/compiler/Codegen/LLVMCPU/BUILD Outdated
"//iree/compiler/Dialect/Flow/IR",
"//iree/compiler/Dialect/HAL/IR",
"//iree/compiler/Dialect/HAL/IR:HALDialect",
"//iree/compiler/Dialect/HAL/Target/LLVM:LLVMTargetOptions",

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 not sure this is needed here. Just noting this here as I review this PR. Will come back to this once I get through the review.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

removed, all i needed was llvm/ADT/Triple.h.

populateLinalgToVectorVectorizeMMT4dPatterns(context, vectorizeOpsPattenrs);
if (failed(applyPatternsAndFoldGreedily(funcOp,
std::move(vectorizeOpsPattenrs)))) {
RewritePatternSet patterns(context);

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 see this pattern used in two places. We need to decide which path we are going to take for MMT4D. Lot of this is kept around based on decision made here. Ideally we need this along only a single pipeline.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Right, I updated this PR to only use the newer LLVMCPUTileFustAndVectorizeLinalgTensorOps.cpp:

--- a/iree/compiler/Codegen/LLVMCPU/KernelDispatch.cpp
+++ b/iree/compiler/Codegen/LLVMCPU/KernelDispatch.cpp
@@ -87,12 +87,9 @@ static Optional<llvm::Triple> getTargetTriple(FuncOp entryPointFn) {
 static DispatchLoweringPassPipeline getDispatchLoweringPassPipeline(
     FuncOp entryPointFn, Operation *op) {
   return TypeSwitch<Operation *, DispatchLoweringPassPipeline>(op)
-      .Case<linalg::ContractionOpInterface>([&](auto op) {
+      .Case<linalg::ContractionOpInterface, linalg::Mmt4DOp>([&](auto op) {
         return DispatchLoweringPassPipeline::CPUTileFuseAndVectorize;
       })
-      .Case<linalg::Mmt4DOp>([&](auto op) {
-        return DispatchLoweringPassPipeline::CPUTensorToVectors;
-      })

and I dropped the changes to the old pipeline.

bjacob added a commit to bjacob/iree that referenced this pull request Jan 14, 2022
This fixes the codegen for the 2d vector transfers that we need to do
in matmul kernels targeting 2D (matrix) simd operations, for instance
allowing PR iree-org#7778 to achieve nice codegen such as:

```
   11a14: ad 05 00 f1      subs    x13, x13, #1
   11a18: f8 66 c1 ac      ldp    q24, q25, [x23], iree-org#32
   11a1c: 7a 6f c1 ac      ldp    q26, q27, [x27], iree-org#32
   11a20: 41 e3 98 4f      sdot    v1.4s, v26.16b, v24.4b[0]
   11a24: 60 e3 98 4f      sdot    v0.4s, v27.16b, v24.4b[0]
   11a28: 43 e3 b8 4f      sdot    v3.4s, v26.16b, v24.4b[1]
   11a2c: 62 e3 b8 4f      sdot    v2.4s, v27.16b, v24.4b[1]
   11a30: 46 eb 98 4f      sdot    v6.4s, v26.16b, v24.4b[2]
   11a34: 64 eb 98 4f      sdot    v4.4s, v27.16b, v24.4b[2]
   11a38: 53 eb b8 4f      sdot    v19.4s, v26.16b, v24.4b[3]
   11a3c: 70 eb b8 4f      sdot    v16.4s, v27.16b, v24.4b[3]
   11a40: 54 e3 99 4f      sdot    v20.4s, v26.16b, v25.4b[0]
   11a44: 71 e3 99 4f      sdot    v17.4s, v27.16b, v25.4b[0]
   11a48: 57 e3 b9 4f      sdot    v23.4s, v26.16b, v25.4b[1]
   11a4c: 76 e3 b9 4f      sdot    v22.4s, v27.16b, v25.4b[1]
   11a50: 55 eb 99 4f      sdot    v21.4s, v26.16b, v25.4b[2]
   11a54: 72 eb 99 4f      sdot    v18.4s, v27.16b, v25.4b[2]
   11a58: 47 eb b9 4f      sdot    v7.4s, v26.16b, v25.4b[3]
   11a5c: 65 eb b9 4f      sdot    v5.4s, v27.16b, v25.4b[3]
   11a60: a1 fd ff 54      b.ne    0x11a14 <.text+0x11a8>
```

@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.

Ok, I dont think I have more to review on this. Thanks for pushing on this and working through the whole host of first user issues here!

Comment thread build_tools/bazel/iree_check_test.bzl Resolved
Comment thread build_tools/cmake/iree_check_test.cmake Resolved
Comment thread build_tools/cmake/iree_check_test.cmake Resolved
Comment thread build_tools/cmake/iree_check_test.cmake Resolved
Comment thread build_tools/cmake/iree_check_test.cmake Resolved
Comment thread build_tools/cmake/iree_check_test.cmake Resolved
Comment thread iree/compiler/Codegen/Passes.td Resolved
bjacob added a commit to bjacob/iree that referenced this pull request Jan 21, 2022
This fixes the codegen for the 2d vector transfers that we need to do
in matmul kernels targeting 2D (matrix) simd operations, for instance
allowing PR iree-org#7778 to achieve nice codegen such as:

```
   11a14: ad 05 00 f1      subs    x13, x13, #1
   11a18: f8 66 c1 ac      ldp    q24, q25, [x23], iree-org#32
   11a1c: 7a 6f c1 ac      ldp    q26, q27, [x27], iree-org#32
   11a20: 41 e3 98 4f      sdot    v1.4s, v26.16b, v24.4b[0]
   11a24: 60 e3 98 4f      sdot    v0.4s, v27.16b, v24.4b[0]
   11a28: 43 e3 b8 4f      sdot    v3.4s, v26.16b, v24.4b[1]
   11a2c: 62 e3 b8 4f      sdot    v2.4s, v27.16b, v24.4b[1]
   11a30: 46 eb 98 4f      sdot    v6.4s, v26.16b, v24.4b[2]
   11a34: 64 eb 98 4f      sdot    v4.4s, v27.16b, v24.4b[2]
   11a38: 53 eb b8 4f      sdot    v19.4s, v26.16b, v24.4b[3]
   11a3c: 70 eb b8 4f      sdot    v16.4s, v27.16b, v24.4b[3]
   11a40: 54 e3 99 4f      sdot    v20.4s, v26.16b, v25.4b[0]
   11a44: 71 e3 99 4f      sdot    v17.4s, v27.16b, v25.4b[0]
   11a48: 57 e3 b9 4f      sdot    v23.4s, v26.16b, v25.4b[1]
   11a4c: 76 e3 b9 4f      sdot    v22.4s, v27.16b, v25.4b[1]
   11a50: 55 eb 99 4f      sdot    v21.4s, v26.16b, v25.4b[2]
   11a54: 72 eb 99 4f      sdot    v18.4s, v27.16b, v25.4b[2]
   11a58: 47 eb b9 4f      sdot    v7.4s, v26.16b, v25.4b[3]
   11a5c: 65 eb b9 4f      sdot    v5.4s, v27.16b, v25.4b[3]
   11a60: a1 fd ff 54      b.ne    0x11a14 <.text+0x11a8>
```
@bjacob
bjacob force-pushed the vector-contract-custom-kernels branch from cc3e9bc to 9c147eb Compare January 21, 2022 22:05
bjacob added a commit that referenced this pull request Jan 21, 2022
This fixes the codegen for the 2d vector transfers that we need to do
in matmul kernels targeting 2D (matrix) simd operations, for instance
allowing PR #7778 to achieve nice codegen such as:

```
   11a14: ad 05 00 f1      subs    x13, x13, #1
   11a18: f8 66 c1 ac      ldp    q24, q25, [x23], #32
   11a1c: 7a 6f c1 ac      ldp    q26, q27, [x27], #32
   11a20: 41 e3 98 4f      sdot    v1.4s, v26.16b, v24.4b[0]
   11a24: 60 e3 98 4f      sdot    v0.4s, v27.16b, v24.4b[0]
   11a28: 43 e3 b8 4f      sdot    v3.4s, v26.16b, v24.4b[1]
   11a2c: 62 e3 b8 4f      sdot    v2.4s, v27.16b, v24.4b[1]
   11a30: 46 eb 98 4f      sdot    v6.4s, v26.16b, v24.4b[2]
   11a34: 64 eb 98 4f      sdot    v4.4s, v27.16b, v24.4b[2]
   11a38: 53 eb b8 4f      sdot    v19.4s, v26.16b, v24.4b[3]
   11a3c: 70 eb b8 4f      sdot    v16.4s, v27.16b, v24.4b[3]
   11a40: 54 e3 99 4f      sdot    v20.4s, v26.16b, v25.4b[0]
   11a44: 71 e3 99 4f      sdot    v17.4s, v27.16b, v25.4b[0]
   11a48: 57 e3 b9 4f      sdot    v23.4s, v26.16b, v25.4b[1]
   11a4c: 76 e3 b9 4f      sdot    v22.4s, v27.16b, v25.4b[1]
   11a50: 55 eb 99 4f      sdot    v21.4s, v26.16b, v25.4b[2]
   11a54: 72 eb 99 4f      sdot    v18.4s, v27.16b, v25.4b[2]
   11a58: 47 eb b9 4f      sdot    v7.4s, v26.16b, v25.4b[3]
   11a5c: 65 eb b9 4f      sdot    v5.4s, v27.16b, v25.4b[3]
   11a60: a1 fd ff 54      b.ne    0x11a14 <.text+0x11a8>
```
This is refurbishing Ahmed Taei's existing
VectorContractToAArch64InlineAsmOp pass, repurposing it to implementing
matrix-times-matrix-transposed kernels, and generalizing it to create a
good insertion point for any other vector.contract custom "handwritten"
kernel, using either inline assembly or intrinsics.
@bjacob
bjacob force-pushed the vector-contract-custom-kernels branch from 9c147eb to f5a33cc Compare January 22, 2022 02:45
@bjacob
bjacob merged commit 949eb89 into iree-org:main Jan 22, 2022
@GMNGeoffrey

Copy link
Copy Markdown
Member

Thanks for powering through and getting this landed Benoit ❤️

@bjacob

bjacob commented Jan 22, 2022

Copy link
Copy Markdown
Collaborator Author

Thanks for the tireless help and attention to detail! It's going to be a lot more exciting from here on.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

codegen/llvm LLVM code generation compiler backend performance ⚡ Performance/optimization related work across the compiler and runtime

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants