| 1 | //===----------------------------------------------------------------------===// |
| 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 | // Emit OpenACC clause recipes as CIR code. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "CIRGenCXXABI.h" |
| 14 | #include "CIRGenFunction.h" |
| 15 | |
| 16 | #include "clang/AST/ASTContext.h" |
| 17 | #include "clang/AST/DeclBase.h" |
| 18 | #include "clang/AST/Expr.h" |
| 19 | #include "clang/AST/ExprCXX.h" |
| 20 | #include "clang/AST/OpenACCClause.h" |
| 21 | #include "clang/AST/TypeBase.h" |
| 22 | #include "clang/Basic/OpenACCKinds.h" |
| 23 | |
| 24 | #include "mlir/Dialect/OpenACC/OpenACC.h" |
| 25 | |
| 26 | namespace clang::CIRGen { |
| 27 | class OpenACCRecipeBuilderBase { |
| 28 | // makes the copy of the addresses of an alloca to the previous allocation. |
| 29 | void makeAllocaCopy(mlir::Location loc, mlir::Type copyType, |
| 30 | mlir::Value numEltsToCopy, mlir::Value offsetPerSubarray, |
| 31 | mlir::Value destAlloca, mlir::Value srcAlloca); |
| 32 | // This function generates the required alloca, similar to |
| 33 | // 'emitAutoVarAlloca', except for the OpenACC array/pointer types. |
| 34 | mlir::Value makeBoundsAlloca(mlir::Block *block, SourceRange exprRange, |
| 35 | mlir::Location loc, std::string_view allocaName, |
| 36 | size_t numBounds, |
| 37 | llvm::ArrayRef<QualType> boundTypes); |
| 38 | |
| 39 | void makeBoundsInit(mlir::Value alloca, mlir::Location loc, |
| 40 | mlir::Block *block, const VarDecl *allocaDecl, |
| 41 | QualType origType, bool isInitSection); |
| 42 | |
| 43 | protected: |
| 44 | CIRGen::CIRGenFunction &cgf; |
| 45 | CIRGen::CIRGenBuilderTy &builder; |
| 46 | |
| 47 | mlir::Block *createRecipeBlock(mlir::Region ®ion, mlir::Type opTy, |
| 48 | mlir::Location loc, size_t numBounds, |
| 49 | bool isInit); |
| 50 | // Creates a loop through an 'acc.bounds', leaving the 'insertion' point to be |
| 51 | // the inside of the loop body. Traverses LB->UB UNLESS `inverse` is set. |
| 52 | // Returns the 'subscriptedValue' changed with the new bounds subscript. |
| 53 | std::pair<mlir::Value, mlir::Value> |
| 54 | createBoundsLoop(mlir::Value subscriptedValue, mlir::Value subscriptedValue2, |
| 55 | mlir::Value bound, mlir::Location loc, bool inverse); |
| 56 | |
| 57 | mlir::Value createBoundsLoop(mlir::Value subscriptedValue, mlir::Value bound, |
| 58 | mlir::Location loc, bool inverse) { |
| 59 | return createBoundsLoop(subscriptedValue, subscriptedValue2: {}, bound, loc, inverse).first; |
| 60 | } |
| 61 | |
| 62 | mlir::acc::ReductionOperator convertReductionOp(OpenACCReductionOperator op); |
| 63 | |
| 64 | // This function generates the 'combiner' section for a reduction recipe. Note |
| 65 | // that this function is not 'insertion point' clean, in that it alters the |
| 66 | // insertion point to be inside of the 'combiner' section of the recipe, but |
| 67 | // doesn't restore it aftewards. |
| 68 | void createReductionRecipeCombiner( |
| 69 | mlir::Location loc, mlir::Location locEnd, mlir::Value mainOp, |
| 70 | mlir::acc::ReductionRecipeOp recipe, size_t numBounds, QualType origType, |
| 71 | llvm::ArrayRef<OpenACCReductionRecipe::CombinerRecipe> combinerRecipes); |
| 72 | |
| 73 | void createInitRecipe(mlir::Location loc, mlir::Location locEnd, |
| 74 | SourceRange exprRange, mlir::Value mainOp, |
| 75 | mlir::Region &recipeInitRegion, size_t numBounds, |
| 76 | llvm::ArrayRef<QualType> boundTypes, |
| 77 | const VarDecl *allocaDecl, QualType origType, |
| 78 | bool emitInitExpr); |
| 79 | |
| 80 | void createFirstprivateRecipeCopy(mlir::Location loc, mlir::Location locEnd, |
| 81 | mlir::Value mainOp, |
| 82 | const VarDecl *allocaDecl, |
| 83 | const VarDecl *temporary, |
| 84 | mlir::Region ©Region, size_t numBounds); |
| 85 | |
| 86 | void createRecipeDestroySection(mlir::Location loc, mlir::Location locEnd, |
| 87 | mlir::Value mainOp, CharUnits alignment, |
| 88 | QualType origType, size_t numBounds, |
| 89 | QualType baseType, |
| 90 | mlir::Region &destroyRegion); |
| 91 | |
| 92 | OpenACCRecipeBuilderBase(CIRGen::CIRGenFunction &cgf, |
| 93 | CIRGen::CIRGenBuilderTy &builder) |
| 94 | : cgf(cgf), builder(builder) {} |
| 95 | }; |
| 96 | |
| 97 | template <typename RecipeTy> |
| 98 | class OpenACCRecipeBuilder : OpenACCRecipeBuilderBase { |
| 99 | std::string getRecipeName(SourceRange loc, QualType baseType, |
| 100 | unsigned numBounds, |
| 101 | OpenACCReductionOperator reductionOp) { |
| 102 | std::string recipeName; |
| 103 | { |
| 104 | llvm::raw_string_ostream stream(recipeName); |
| 105 | |
| 106 | if constexpr (std::is_same_v<RecipeTy, mlir::acc::PrivateRecipeOp>) { |
| 107 | stream << "privatization_" ; |
| 108 | } else if constexpr (std::is_same_v<RecipeTy, |
| 109 | mlir::acc::FirstprivateRecipeOp>) { |
| 110 | stream << "firstprivatization_" ; |
| 111 | |
| 112 | } else if constexpr (std::is_same_v<RecipeTy, |
| 113 | mlir::acc::ReductionRecipeOp>) { |
| 114 | stream << "reduction_" ; |
| 115 | // Values here are a little weird (for bitwise and/or is 'i' prefix, and |
| 116 | // logical ops with 'l'), but are chosen to be the same as the MLIR |
| 117 | // dialect names as well as to match the Flang versions of these. |
| 118 | switch (reductionOp) { |
| 119 | case OpenACCReductionOperator::Addition: |
| 120 | stream << "add_" ; |
| 121 | break; |
| 122 | case OpenACCReductionOperator::Multiplication: |
| 123 | stream << "mul_" ; |
| 124 | break; |
| 125 | case OpenACCReductionOperator::Max: |
| 126 | stream << "max_" ; |
| 127 | break; |
| 128 | case OpenACCReductionOperator::Min: |
| 129 | stream << "min_" ; |
| 130 | break; |
| 131 | case OpenACCReductionOperator::BitwiseAnd: |
| 132 | stream << "iand_" ; |
| 133 | break; |
| 134 | case OpenACCReductionOperator::BitwiseOr: |
| 135 | stream << "ior_" ; |
| 136 | break; |
| 137 | case OpenACCReductionOperator::BitwiseXOr: |
| 138 | stream << "xor_" ; |
| 139 | break; |
| 140 | case OpenACCReductionOperator::And: |
| 141 | stream << "land_" ; |
| 142 | break; |
| 143 | case OpenACCReductionOperator::Or: |
| 144 | stream << "lor_" ; |
| 145 | break; |
| 146 | case OpenACCReductionOperator::Invalid: |
| 147 | llvm_unreachable("invalid reduction operator" ); |
| 148 | } |
| 149 | } else { |
| 150 | static_assert(!sizeof(RecipeTy), "Unknown Recipe op kind" ); |
| 151 | } |
| 152 | |
| 153 | // The naming convention from Flang with bounds doesn't map to C++ types |
| 154 | // very well, so we're just going to choose our own here. |
| 155 | if (numBounds) |
| 156 | stream << "_Bcnt" << numBounds << '_'; |
| 157 | |
| 158 | MangleContext &mc = cgf.cgm.getCXXABI().getMangleContext(); |
| 159 | mc.mangleCanonicalTypeName(T: baseType, stream); |
| 160 | } |
| 161 | return recipeName; |
| 162 | } |
| 163 | |
| 164 | public: |
| 165 | OpenACCRecipeBuilder(CIRGen::CIRGenFunction &cgf, |
| 166 | CIRGen::CIRGenBuilderTy &builder) |
| 167 | : OpenACCRecipeBuilderBase(cgf, builder) {} |
| 168 | RecipeTy getOrCreateRecipe( |
| 169 | ASTContext &astCtx, mlir::OpBuilder::InsertPoint &insertLocation, |
| 170 | const Expr *varRef, const VarDecl *varRecipe, const VarDecl *temporary, |
| 171 | OpenACCReductionOperator reductionOp, DeclContext *dc, QualType origType, |
| 172 | size_t numBounds, llvm::ArrayRef<QualType> boundTypes, QualType baseType, |
| 173 | mlir::Value mainOp, |
| 174 | llvm::ArrayRef<OpenACCReductionRecipe::CombinerRecipe> |
| 175 | reductionCombinerRecipes) { |
| 176 | assert(!varRecipe->getType()->isSpecificBuiltinType( |
| 177 | BuiltinType::ArraySection) && |
| 178 | "array section shouldn't make it to recipe creation" ); |
| 179 | |
| 180 | mlir::ModuleOp mod = cgf.cgm.getModule(); |
| 181 | |
| 182 | std::string recipeName = getRecipeName(loc: varRef->getSourceRange(), baseType, |
| 183 | numBounds, reductionOp); |
| 184 | if (auto recipe = mod.lookupSymbol<RecipeTy>(recipeName)) |
| 185 | return recipe; |
| 186 | |
| 187 | mlir::Location loc = cgf.cgm.getLoc(varRef->getBeginLoc()); |
| 188 | mlir::Location locEnd = cgf.cgm.getLoc(varRef->getEndLoc()); |
| 189 | |
| 190 | mlir::OpBuilder modBuilder(mod.getBodyRegion()); |
| 191 | if (insertLocation.isSet()) |
| 192 | modBuilder.restoreInsertionPoint(ip: insertLocation); |
| 193 | RecipeTy recipe; |
| 194 | |
| 195 | if constexpr (std::is_same_v<RecipeTy, mlir::acc::ReductionRecipeOp>) { |
| 196 | recipe = RecipeTy::create(modBuilder, loc, recipeName, |
| 197 | /*sym_visibility=*/nullptr, mainOp.getType(), |
| 198 | convertReductionOp(op: reductionOp)); |
| 199 | } else { |
| 200 | recipe = RecipeTy::create(modBuilder, loc, recipeName, |
| 201 | /*sym_visibility=*/nullptr, mainOp.getType()); |
| 202 | } |
| 203 | insertLocation = modBuilder.saveInsertionPoint(); |
| 204 | |
| 205 | if constexpr (std::is_same_v<RecipeTy, mlir::acc::PrivateRecipeOp>) { |
| 206 | createInitRecipe(loc, locEnd, exprRange: varRef->getSourceRange(), mainOp, |
| 207 | recipeInitRegion&: recipe.getInitRegion(), numBounds, boundTypes, allocaDecl: varRecipe, |
| 208 | origType, /*emitInitExpr=*/emitInitExpr: true); |
| 209 | } else if constexpr (std::is_same_v<RecipeTy, |
| 210 | mlir::acc::ReductionRecipeOp>) { |
| 211 | createInitRecipe(loc, locEnd, exprRange: varRef->getSourceRange(), mainOp, |
| 212 | recipeInitRegion&: recipe.getInitRegion(), numBounds, boundTypes, allocaDecl: varRecipe, |
| 213 | origType, /*emitInitExpr=*/emitInitExpr: true); |
| 214 | createReductionRecipeCombiner(loc, locEnd, mainOp, recipe, numBounds, |
| 215 | origType, combinerRecipes: reductionCombinerRecipes); |
| 216 | } else { |
| 217 | static_assert(std::is_same_v<RecipeTy, mlir::acc::FirstprivateRecipeOp>); |
| 218 | createInitRecipe(loc, locEnd, exprRange: varRef->getSourceRange(), mainOp, |
| 219 | recipeInitRegion&: recipe.getInitRegion(), numBounds, boundTypes, allocaDecl: varRecipe, |
| 220 | origType, /*emitInitExpr=*/emitInitExpr: false); |
| 221 | createFirstprivateRecipeCopy(loc, locEnd, mainOp, allocaDecl: varRecipe, temporary, |
| 222 | copyRegion&: recipe.getCopyRegion(), numBounds); |
| 223 | } |
| 224 | |
| 225 | if (origType.isDestructedType()) |
| 226 | createRecipeDestroySection( |
| 227 | loc, locEnd, mainOp, alignment: cgf.getContext().getDeclAlign(varRecipe), |
| 228 | origType, numBounds, baseType, destroyRegion&: recipe.getDestroyRegion()); |
| 229 | return recipe; |
| 230 | } |
| 231 | }; |
| 232 | } // namespace clang::CIRGen |
| 233 | |