| 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 | // Generic OpenACC lowering functions not Stmt, Decl, or clause specific. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "CIRGenFunction.h" |
| 14 | #include "mlir/Dialect/Arith/IR/Arith.h" |
| 15 | #include "mlir/Dialect/OpenACC/OpenACC.h" |
| 16 | #include "clang/AST/ExprCXX.h" |
| 17 | |
| 18 | using namespace clang; |
| 19 | using namespace clang::CIRGen; |
| 20 | |
| 21 | namespace { |
| 22 | mlir::Value createBound(CIRGenFunction &cgf, CIRGen::CIRGenBuilderTy &builder, |
| 23 | mlir::Location boundLoc, mlir::Value lowerBound, |
| 24 | mlir::Value upperBound, mlir::Value extent) { |
| 25 | // Arrays always have a start-idx of 0. |
| 26 | mlir::Value startIdx = cgf.createOpenACCConstantInt(loc: boundLoc, width: 64, value: 0); |
| 27 | // Stride is always 1 in C/C++. |
| 28 | mlir::Value stride = cgf.createOpenACCConstantInt(loc: boundLoc, width: 64, value: 1); |
| 29 | |
| 30 | auto bound = mlir::acc::DataBoundsOp::create(builder, location: boundLoc, lowerbound: lowerBound, |
| 31 | upperbound: upperBound); |
| 32 | bound.getStartIdxMutable().assign(value: startIdx); |
| 33 | if (extent) |
| 34 | bound.getExtentMutable().assign(value: extent); |
| 35 | bound.getStrideMutable().assign(value: stride); |
| 36 | |
| 37 | return bound; |
| 38 | } |
| 39 | } // namespace |
| 40 | |
| 41 | mlir::Value CIRGenFunction::emitOpenACCIntExpr(const Expr *intExpr) { |
| 42 | mlir::Value expr = emitScalarExpr(e: intExpr); |
| 43 | mlir::Location exprLoc = cgm.getLoc(cLoc: intExpr->getBeginLoc()); |
| 44 | |
| 45 | mlir::IntegerType targetType = mlir::IntegerType::get( |
| 46 | context: &getMLIRContext(), width: getContext().getIntWidth(T: intExpr->getType()), |
| 47 | signedness: intExpr->getType()->isSignedIntegerOrEnumerationType() |
| 48 | ? mlir::IntegerType::SignednessSemantics::Signed |
| 49 | : mlir::IntegerType::SignednessSemantics::Unsigned); |
| 50 | |
| 51 | return builder.createBuiltinIntCast(loc: exprLoc, src: expr, newTy: targetType); |
| 52 | } |
| 53 | |
| 54 | mlir::Value CIRGenFunction::createOpenACCConstantInt(mlir::Location loc, |
| 55 | unsigned width, |
| 56 | int64_t value) { |
| 57 | mlir::IntegerType ty = |
| 58 | mlir::IntegerType::get(context: &getMLIRContext(), width, |
| 59 | signedness: mlir::IntegerType::SignednessSemantics::Signless); |
| 60 | auto constOp = mlir::arith::ConstantOp::create( |
| 61 | builder, location: loc, value: builder.getIntegerAttr(type: ty, value)); |
| 62 | |
| 63 | return constOp; |
| 64 | } |
| 65 | |
| 66 | CIRGenFunction::OpenACCDataOperandInfo |
| 67 | CIRGenFunction::getOpenACCDataOperandInfo(const Expr *e) { |
| 68 | const Expr *curVarExpr = e->IgnoreParenImpCasts(); |
| 69 | QualType origType = |
| 70 | curVarExpr->getType().getNonReferenceType().getUnqualifiedType(); |
| 71 | // Array sections are special, and we have to treat them that way. |
| 72 | if (const auto *section = |
| 73 | dyn_cast<ArraySectionExpr>(Val: curVarExpr->IgnoreParenImpCasts())) |
| 74 | origType = section->getElementType(); |
| 75 | |
| 76 | mlir::Location exprLoc = cgm.getLoc(cLoc: curVarExpr->getBeginLoc()); |
| 77 | llvm::SmallVector<mlir::Value> bounds; |
| 78 | llvm::SmallVector<QualType> boundTypes; |
| 79 | |
| 80 | std::string exprString; |
| 81 | llvm::raw_string_ostream os(exprString); |
| 82 | e->printPretty(OS&: os, Helper: nullptr, Policy: getContext().getPrintingPolicy()); |
| 83 | |
| 84 | auto addBoundType = [&](const Expr *e) { |
| 85 | if (const auto *section = dyn_cast<ArraySectionExpr>(Val: curVarExpr)) |
| 86 | boundTypes.push_back(Elt: section->getElementType()); |
| 87 | else |
| 88 | boundTypes.push_back(Elt: curVarExpr->getType()); |
| 89 | }; |
| 90 | |
| 91 | addBoundType(curVarExpr); |
| 92 | |
| 93 | while (isa<ArraySectionExpr, ArraySubscriptExpr>(Val: curVarExpr)) { |
| 94 | mlir::Location boundLoc = cgm.getLoc(cLoc: curVarExpr->getBeginLoc()); |
| 95 | mlir::Value lowerBound; |
| 96 | mlir::Value upperBound; |
| 97 | mlir::Value extent; |
| 98 | |
| 99 | if (const auto *section = dyn_cast<ArraySectionExpr>(Val: curVarExpr)) { |
| 100 | if (const Expr *lb = section->getLowerBound()) |
| 101 | lowerBound = emitOpenACCIntExpr(intExpr: lb); |
| 102 | else |
| 103 | lowerBound = createOpenACCConstantInt(loc: boundLoc, width: 64, value: 0); |
| 104 | |
| 105 | if (const Expr *len = section->getLength()) { |
| 106 | extent = emitOpenACCIntExpr(intExpr: len); |
| 107 | } else { |
| 108 | QualType baseTy = section->getBaseType(); |
| 109 | // We know this is the case as implicit lengths are only allowed for |
| 110 | // array types with a constant size, or a dependent size. AND since |
| 111 | // we are codegen we know we're not dependent. |
| 112 | auto *arrayTy = getContext().getAsConstantArrayType(T: baseTy); |
| 113 | // Rather than trying to calculate the extent based on the |
| 114 | // lower-bound, we can just emit this as an upper bound. |
| 115 | upperBound = createOpenACCConstantInt(loc: boundLoc, width: 64, |
| 116 | value: arrayTy->getLimitedSize() - 1); |
| 117 | } |
| 118 | |
| 119 | curVarExpr = section->getBase()->IgnoreParenImpCasts(); |
| 120 | } else { |
| 121 | const auto *subscript = cast<ArraySubscriptExpr>(Val: curVarExpr); |
| 122 | |
| 123 | lowerBound = emitOpenACCIntExpr(intExpr: subscript->getIdx()); |
| 124 | // Length of an array index is always 1. |
| 125 | extent = createOpenACCConstantInt(loc: boundLoc, width: 64, value: 1); |
| 126 | curVarExpr = subscript->getBase()->IgnoreParenImpCasts(); |
| 127 | } |
| 128 | |
| 129 | bounds.push_back(Elt: createBound(cgf&: *this, builder&: this->builder, boundLoc, lowerBound, |
| 130 | upperBound, extent)); |
| 131 | addBoundType(curVarExpr); |
| 132 | } |
| 133 | |
| 134 | if (const auto *memExpr = dyn_cast<MemberExpr>(Val: curVarExpr)) |
| 135 | return {.beginLoc: exprLoc, |
| 136 | .varValue: emitMemberExpr(e: memExpr).getPointer(), |
| 137 | .name: exprString, |
| 138 | .origType: origType, |
| 139 | .baseType: curVarExpr->getType().getNonReferenceType().getUnqualifiedType(), |
| 140 | .bounds: std::move(bounds), |
| 141 | .boundTypes: std::move(boundTypes)}; |
| 142 | |
| 143 | // Sema has made sure that only 4 types of things can get here, array |
| 144 | // subscript, array section, member expr, or DRE to a var decl (or the |
| 145 | // former 3 wrapping a var-decl), so we should be able to assume this is |
| 146 | // right. |
| 147 | const auto *dre = cast<DeclRefExpr>(Val: curVarExpr); |
| 148 | return {.beginLoc: exprLoc, |
| 149 | .varValue: emitDeclRefLValue(e: dre).getPointer(), |
| 150 | .name: exprString, |
| 151 | .origType: origType, |
| 152 | .baseType: curVarExpr->getType().getNonReferenceType().getUnqualifiedType(), |
| 153 | .bounds: std::move(bounds), |
| 154 | .boundTypes: std::move(boundTypes)}; |
| 155 | } |
| 156 | |