github.com/johnnyeven/libtools@v0.0.0-20191126065708-61829c1adf46/third_party/mlir/test/lib/Transforms/TestLoopMapping.cpp (about) 1 //===- TestLoopMapping.cpp --- Parametric loop mapping pass ---------------===// 2 // 3 // Copyright 2019 The MLIR Authors. 4 // 5 // Licensed under the Apache License, Version 2.0 (the "License"); 6 // you may not use this file except in compliance with the License. 7 // You may obtain a copy of the License at 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, software 12 // distributed under the License is distributed on an "AS IS" BASIS, 13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 // See the License for the specific language governing permissions and 15 // limitations under the License. 16 // ============================================================================= 17 // 18 // This file implements a pass to parametrically map loop.for loops to virtual 19 // processing element dimensions. 20 // 21 //===----------------------------------------------------------------------===// 22 23 #include "mlir/Dialect/LoopOps/LoopOps.h" 24 #include "mlir/IR/Builders.h" 25 #include "mlir/Pass/Pass.h" 26 #include "mlir/Transforms/LoopUtils.h" 27 #include "mlir/Transforms/Passes.h" 28 29 #include "llvm/ADT/SetVector.h" 30 31 using namespace mlir; 32 33 namespace { 34 class TestLoopMappingPass : public FunctionPass<TestLoopMappingPass> { 35 public: 36 explicit TestLoopMappingPass() {} 37 38 void runOnFunction() override { 39 FuncOp func = getFunction(); 40 41 // SSA values for the transformation are created out of thin air by 42 // unregistered "new_processor_id_and_range" operations. This is enough to 43 // emulate mapping conditions. 44 SmallVector<Value *, 8> processorIds, numProcessors; 45 func.walk([&processorIds, &numProcessors](Operation *op) { 46 if (op->getName().getStringRef() != "new_processor_id_and_range") 47 return; 48 processorIds.push_back(op->getResult(0)); 49 numProcessors.push_back(op->getResult(1)); 50 }); 51 52 func.walk([&processorIds, &numProcessors](loop::ForOp op) { 53 // Ignore nested loops. 54 if (op.getParentRegion()->getParentOfType<loop::ForOp>()) 55 return; 56 mapLoopToProcessorIds(op, processorIds, numProcessors); 57 }); 58 } 59 }; 60 } // end namespace 61 62 static PassRegistration<TestLoopMappingPass> 63 reg("test-mapping-to-processing-elements", 64 "test mapping a single loop on a virtual processor grid", 65 [] { return std::make_unique<TestLoopMappingPass>(); });