1 /* Copyright 2022 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 #include <algorithm>
17 #include <iterator>
18 #include <memory>
19 
20 #include "mlir/Dialect/Func/IR/FuncOps.h"
21 #include "mlir/Dialect/Linalg/Transforms/CodegenStrategy.h"
22 #include "mlir/Dialect/Utils/StructuredOpsUtils.h"  // from @llvm-project
23 #include "tensorflow/compiler/mlir/tfrt/jit/transforms/tf_jitrt_passes.h"
24 
25 namespace tensorflow {
26 namespace {
27 
28 #define GEN_PASS_CLASSES
29 #include "tensorflow/compiler/mlir/tfrt/jit/transforms/tf_jitrt_passes.h.inc"
30 
31 using mlir::linalg::CodegenStrategy;
32 
33 struct LowerTransposePass : public LowerTransposeBase<LowerTransposePass> {
runOnOperationtensorflow::__anon2a275d0d0111::LowerTransposePass34   void runOnOperation() override {
35     mlir::OpPassManager dynamic_pm("func.func");
36     auto avx_lowering_options =
37         mlir::x86vector::avx2::LoweringOptions().setTransposeOptions(
38             mlir::x86vector::avx2::TransposeLoweringOptions()
39                 .lower4x8xf32()
40                 .lower8x8xf32());
41 
42     CodegenStrategy strategy;
43     strategy.vectorLowering(mlir::linalg::LinalgVectorLoweringOptions()
44                                 .enableShapeCastLowering(false)
45                                 .enableVectorTransposeLowering()
46                                 .enableAVX2Lowering()
47                                 .setAVX2LoweringOptions(avx_lowering_options));
48 
49     strategy.configurePassPipeline(dynamic_pm, &getContext());
50     if (failed(runPipeline(dynamic_pm, getOperation()))) {
51       return signalPassFailure();
52     }
53   }
54 };
55 
56 }  // namespace
57 
58 std::unique_ptr<mlir::OperationPass<mlir::func::FuncOp>>
CreateLowerVectorTransposePass()59 CreateLowerVectorTransposePass() {
60   return std::make_unique<LowerTransposePass>();
61 }
62 
63 }  // namespace tensorflow
64