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 // This files implements the logic for converting multidimensional 17 // `scf.parallel` loops into 1D loops. 18 19 #include <memory> 20 #include <numeric> 21 #include <vector> 22 23 #include "mlir-hlo/Transforms/PassDetail.h" 24 #include "mlir-hlo/Transforms/passes.h" 25 #include "mlir/Dialect/Func/IR/FuncOps.h" 26 #include "mlir/Dialect/SCF/IR/SCF.h" 27 #include "mlir/Dialect/SCF/Utils/Utils.h" 28 29 using ::mlir::scf::ParallelOp; 30 31 namespace mlir { 32 namespace { 33 34 // This is the implementation of the CollapseParallelLoopsTo1D pass declared in 35 // include/mlir-hlo/Transforms/passes.td 36 struct CollapseParallelLoopsTo1D 37 : public CollapseParallelLoopsTo1DPassBase<CollapseParallelLoopsTo1D> { 38 void runOnOperation() override; 39 }; 40 41 } // namespace 42 } // namespace mlir 43 44 using namespace mlir; 45 runOnOperation()46void mlir::CollapseParallelLoopsTo1D::runOnOperation() { 47 getOperation()->walk([&](ParallelOp op) { 48 unsigned numLoops = op.getNumLoops(); 49 if (numLoops == 1) return; 50 std::vector<unsigned> combinedLoops(numLoops); 51 std::iota(combinedLoops.begin(), combinedLoops.end(), 0u); 52 mlir::collapseParallelLoops(op, {combinedLoops}); 53 }); 54 } 55 createCollapseParallelLoopsTo1DPass()56std::unique_ptr<OperationPass<>> mlir::createCollapseParallelLoopsTo1DPass() { 57 return std::make_unique<CollapseParallelLoopsTo1D>(); 58 } 59