| 1 | //===- TestRecipePopulate.cpp - Test Recipe Population -------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file contains test passes for testing the createAndPopulate methods |
| 10 | // of the recipe operations. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "mlir/Dialect/Arith/IR/Arith.h" |
| 15 | #include "mlir/Dialect/Func/IR/FuncOps.h" |
| 16 | #include "mlir/Dialect/MemRef/IR/MemRefDialect.h" |
| 17 | #include "mlir/Dialect/OpenACC/OpenACC.h" |
| 18 | #include "mlir/IR/Builders.h" |
| 19 | #include "mlir/Pass/Pass.h" |
| 20 | #include "llvm/Support/CommandLine.h" |
| 21 | |
| 22 | using namespace mlir; |
| 23 | using namespace mlir::acc; |
| 24 | |
| 25 | namespace { |
| 26 | |
| 27 | struct TestRecipePopulatePass |
| 28 | : public PassWrapper<TestRecipePopulatePass, OperationPass<ModuleOp>> { |
| 29 | MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestRecipePopulatePass) |
| 30 | |
| 31 | TestRecipePopulatePass() = default; |
| 32 | TestRecipePopulatePass(const TestRecipePopulatePass &pass) |
| 33 | : PassWrapper(pass) { |
| 34 | recipeType = pass.recipeType; |
| 35 | } |
| 36 | |
| 37 | Pass::Option<std::string> recipeType{ |
| 38 | *this, "recipe-type" , |
| 39 | llvm::cl::desc("Recipe type: private or firstprivate" ), |
| 40 | llvm::cl::init(Val: "private" )}; |
| 41 | |
| 42 | StringRef getArgument() const override { return "test-acc-recipe-populate" ; } |
| 43 | |
| 44 | StringRef getDescription() const override { |
| 45 | return "Test OpenACC recipe population" ; |
| 46 | } |
| 47 | |
| 48 | void runOnOperation() override; |
| 49 | |
| 50 | void getDependentDialects(DialectRegistry ®istry) const override { |
| 51 | registry.insert<acc::OpenACCDialect>(); |
| 52 | registry.insert<arith::ArithDialect>(); |
| 53 | registry.insert<memref::MemRefDialect>(); |
| 54 | } |
| 55 | }; |
| 56 | |
| 57 | void TestRecipePopulatePass::runOnOperation() { |
| 58 | auto module = getOperation(); |
| 59 | OpBuilder builder(&getContext()); |
| 60 | |
| 61 | // Collect all test variables |
| 62 | SmallVector<std::tuple<Operation *, Value, std::string>> testVars; |
| 63 | |
| 64 | module.walk(callback: [&](Operation *op) { |
| 65 | if (auto varName = op->getDiscardableAttrOfType<StringAttr>(name: "test.var" )) { |
| 66 | for (auto result : op->getResults()) { |
| 67 | testVars.push_back(Elt: {op, result, varName.str()}); |
| 68 | } |
| 69 | } |
| 70 | }); |
| 71 | |
| 72 | // Generate recipes at module level |
| 73 | builder.setInsertionPoint(block: &module.getBodyRegion().front(), |
| 74 | insertPoint: module.getBodyRegion().front().begin()); |
| 75 | |
| 76 | for (auto [op, var, varName] : testVars) { |
| 77 | Location loc = op->getLoc(); |
| 78 | |
| 79 | std::string recipeName = recipeType.getValue() + "_" + varName; |
| 80 | ValueRange bounds; // No bounds for memref tests |
| 81 | |
| 82 | if (recipeType == "private" ) { |
| 83 | auto recipe = PrivateRecipeOp::createAndPopulate(builder, loc, recipeName, |
| 84 | hostVar: var, varName, bounds); |
| 85 | |
| 86 | if (!recipe) { |
| 87 | op->emitError(message: "Failed to create private recipe for " ) << varName; |
| 88 | } |
| 89 | } else if (recipeType == "firstprivate" ) { |
| 90 | auto recipe = FirstprivateRecipeOp::createAndPopulate( |
| 91 | builder, loc, recipeName, hostVar: var, varName, bounds); |
| 92 | |
| 93 | if (!recipe) { |
| 94 | op->emitError(message: "Failed to create firstprivate recipe for " ) << varName; |
| 95 | } |
| 96 | } else if (recipeType == "private_from_firstprivate" ) { |
| 97 | // First create a firstprivate recipe, then use it to drive creation of a |
| 98 | // matching private recipe via the convenience overload. Give each recipe |
| 99 | // a stable, predictable name so tests can check both. |
| 100 | std::string firstprivName = "first_firstprivate_" + varName; |
| 101 | std::string privName = "private_from_firstprivate_" + varName; |
| 102 | |
| 103 | auto firstpriv = FirstprivateRecipeOp::createAndPopulate( |
| 104 | builder, loc, recipeName: firstprivName, hostVar: var, varName, bounds); |
| 105 | |
| 106 | if (!firstpriv) { |
| 107 | op->emitError(message: "Failed to create firstprivate recipe for " ) << varName; |
| 108 | return; |
| 109 | } |
| 110 | |
| 111 | auto priv = PrivateRecipeOp::createAndPopulate(builder, loc, recipeName: privName, |
| 112 | firstprivRecipe: *firstpriv); |
| 113 | |
| 114 | if (!priv) { |
| 115 | op->emitError( |
| 116 | message: "Failed to create private recipe (from firstprivate) for " ) |
| 117 | << varName; |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | } // namespace |
| 124 | |
| 125 | namespace mlir { |
| 126 | namespace test { |
| 127 | |
| 128 | void registerTestRecipePopulatePass() { |
| 129 | PassRegistration<TestRecipePopulatePass>(); |
| 130 | } |
| 131 | |
| 132 | } // namespace test |
| 133 | } // namespace mlir |
| 134 | |