24 types.reserve(numBounds + 2);
25 types.push_back(opTy);
30 types.push_back(opTy);
32 auto boundsTy = mlir::acc::DataBoundsType::get(&
cgf.getMLIRContext());
33 for (
size_t i = 0; i < numBounds; ++i)
34 types.push_back(boundsTy);
37 return builder.createBlock(®ion, region.end(), types, locs);
39void OpenACCRecipeBuilderBase::makeAllocaCopy(mlir::Location loc,
41 mlir::Value numEltsToCopy,
42 mlir::Value offsetPerSubarray,
43 mlir::Value destAlloca,
44 mlir::Value srcAlloca) {
45 mlir::OpBuilder::InsertionGuard guardCase(
builder);
48 auto itrPtrTy = cir::PointerType::get(itrTy);
49 mlir::IntegerAttr itrAlign =
53 auto loopBuilder = [&]() {
54 auto itr = cir::AllocaOp::create(
builder, loc, itrPtrTy,
"itr", itrAlign);
56 builder.CIRBaseBuilderTy::createStore(loc, constZero, itr);
60 [&](mlir::OpBuilder &b, mlir::Location loc) {
68 auto loadCur = cir::LoadOp::create(
builder, loc, {itr});
74 [&](mlir::OpBuilder &b, mlir::Location loc) {
76 auto loadCur = cir::LoadOp::create(
builder, loc, {itr});
77 auto srcOffset =
builder.createMul(loc, offsetPerSubarray, loadCur);
79 auto ptrToOffsetIntoSrc = cir::PtrStrideOp::create(
80 builder, loc, copyType, srcAlloca, srcOffset);
82 auto offsetIntoDecayDest = cir::PtrStrideOp::create(
86 builder.CIRBaseBuilderTy::createStore(loc, ptrToOffsetIntoSrc,
91 [&](mlir::OpBuilder &b, mlir::Location loc) {
93 auto load = cir::LoadOp::create(
builder, loc, {itr});
94 auto inc =
builder.createInc(loc, load);
95 builder.CIRBaseBuilderTy::createStore(loc, inc, itr);
100 cir::ScopeOp::create(
builder, loc,
101 [&](mlir::OpBuilder &b, mlir::Location loc) {
107mlir::Value OpenACCRecipeBuilderBase::makeBoundsAlloca(
108 mlir::Block *block, SourceRange exprRange, mlir::Location loc,
109 std::string_view allocaName,
size_t numBounds,
110 llvm::ArrayRef<QualType> boundTypes) {
111 mlir::OpBuilder::InsertionGuard guardCase(
builder);
114 llvm::ArrayRef<mlir::BlockArgument> boundsRange =
115 block->getArguments().drop_front(1);
120 assert(boundsRange.size() + 1 == boundTypes.size());
122 mlir::Type itrTy =
cgf.cgm.convertType(
cgf.getContext().UnsignedLongLongTy);
123 auto idxType = mlir::IndexType::get(&
cgf.getMLIRContext());
125 auto getUpperBound = [&](mlir::Value bound) {
127 mlir::acc::GetUpperboundOp::create(
builder, loc, idxType, bound);
128 return mlir::UnrealizedConversionCastOp::create(
builder, loc, itrTy,
129 upperBoundVal.getResult())
133 auto isArrayTy = [&](QualType ty) {
134 if (ty->isArrayType() && !ty->isConstantArrayType())
135 cgf.cgm.errorNYI(exprRange,
"OpenACC recipe init for VLAs");
136 return ty->isConstantArrayType();
139 mlir::Type topLevelTy =
cgf.convertType(boundTypes.back());
140 cir::PointerType topLevelTyPtr =
builder.getPointerTo(topLevelTy);
142 mlir::Value initialAlloca =
builder.createAlloca(
143 loc, topLevelTyPtr, allocaName,
144 cgf.getContext().getTypeAlignInChars(boundTypes.back()));
146 bool lastBoundWasArray = isArrayTy(boundTypes.back());
150 mlir::Value lastAlloca = initialAlloca;
155 llvm::ArrayRef<QualType> boundResults = boundTypes.drop_back(1);
158 llvm::SmallVector<bool> allocasLeftArr;
159 llvm::ArrayRef<QualType> resultTypes = boundTypes.drop_front();
160 std::transform_inclusive_scan(
161 resultTypes.begin(), resultTypes.end(),
162 std::back_inserter(allocasLeftArr), std::plus<bool>{},
163 [](QualType ty) { return !ty->isConstantArrayType(); },
false);
167 mlir::Value cumulativeElts;
168 for (
auto [bound, resultType, allocasLeft] : llvm::reverse(
169 llvm::zip_equal(boundsRange, boundResults, allocasLeftArr))) {
177 mlir::Value eltsPerSubArray = getUpperBound(bound);
178 mlir::Value eltsToAlloca;
185 eltsToAlloca =
builder.createMul(loc, eltsPerSubArray, cumulativeElts);
187 eltsToAlloca = eltsPerSubArray;
189 if (!lastBoundWasArray) {
192 TypeInfoChars eltInfo =
cgf.getContext().getTypeInfoInChars(resultType);
193 cir::ConstantOp eltSize =
builder.getConstInt(
194 loc, itrTy, eltInfo.Width.alignTo(eltInfo.Align).getQuantity());
195 mlir::Value curSize =
builder.createMul(loc, eltsToAlloca, eltSize);
197 mlir::Type eltTy =
cgf.convertType(resultType);
198 cir::PointerType ptrTy =
builder.getPointerTo(eltTy);
199 mlir::Value curAlloca =
builder.createAlloca(
200 loc, ptrTy, eltTy,
"openacc.init.bounds",
201 cgf.getContext().getTypeAlignInChars(resultType), curSize);
203 makeAllocaCopy(loc, ptrTy, cumulativeElts, eltsPerSubArray, lastAlloca,
205 lastAlloca = curAlloca;
210 cir::ConstantOp constZero =
builder.getConstInt(loc, itrTy, 0);
211 lastAlloca =
builder.getArrayElement(loc, loc, lastAlloca,
212 cgf.convertType(resultType),
216 cumulativeElts = eltsToAlloca;
217 lastBoundWasArray = isArrayTy(resultType);
219 return initialAlloca;
223 mlir::Value subscriptedValue, mlir::Value subscriptedValue2,
224 mlir::Value bound, mlir::Location loc,
bool inverse) {
225 mlir::Operation *bodyInsertLoc;
227 mlir::Type itrTy =
cgf.cgm.convertType(
cgf.getContext().UnsignedLongLongTy);
228 auto itrPtrTy = cir::PointerType::get(itrTy);
229 mlir::IntegerAttr itrAlign =
230 cgf.cgm.getSize(
cgf.getContext().getTypeAlignInChars(
231 cgf.getContext().UnsignedLongLongTy));
232 auto idxType = mlir::IndexType::get(&
cgf.getMLIRContext());
234 auto doSubscriptOp = [&](mlir::Value subVal,
235 cir::LoadOp idxLoad) -> mlir::Value {
238 if (
auto arrayTy = dyn_cast<cir::ArrayType>(eltTy))
239 return builder.getArrayElement(loc, loc, subVal, arrayTy.getElementType(),
245 auto eltLoad = cir::LoadOp::create(
builder, loc, {subVal});
247 return cir::PtrStrideOp::create(
builder, loc, eltLoad.getType(), eltLoad,
251 auto forStmtBuilder = [&]() {
254 mlir::acc::GetLowerboundOp::create(
builder, loc, idxType, bound);
255 auto lbConversion = mlir::UnrealizedConversionCastOp::create(
256 builder, loc, itrTy, lowerBoundVal.getResult());
258 mlir::acc::GetUpperboundOp::create(
builder, loc, idxType, bound);
259 auto ubConversion = mlir::UnrealizedConversionCastOp::create(
260 builder, loc, itrTy, upperBoundVal.getResult());
263 auto itr = cir::AllocaOp::create(
builder, loc, itrPtrTy,
"iter", itrAlign);
267 cir::ConstantOp constOne =
builder.getConstInt(loc, itrTy, 1);
270 cir::SubOp::create(
builder, loc, ubConversion.getResult(0), constOne);
273 builder.CIRBaseBuilderTy::createStore(loc, sub, itr);
276 builder.CIRBaseBuilderTy::createStore(loc, lbConversion.getResult(0),
282 auto endItr = inverse ? lbConversion : ubConversion;
287 [&](mlir::OpBuilder &b, mlir::Location loc) {
288 auto loadCur = cir::LoadOp::create(
builder, loc, {itr});
290 auto cmp =
builder.createCompare(
291 loc, inverse ? cir::CmpOpKind::ge : cir::CmpOpKind::lt, loadCur,
292 endItr.getResult(0));
296 [&](mlir::OpBuilder &b, mlir::Location loc) {
297 auto load = cir::LoadOp::create(
builder, loc, {itr});
299 if (subscriptedValue)
300 subscriptedValue = doSubscriptOp(subscriptedValue, load);
301 if (subscriptedValue2)
302 subscriptedValue2 = doSubscriptOp(subscriptedValue2, load);
303 bodyInsertLoc =
builder.createYield(loc);
306 [&](mlir::OpBuilder &b, mlir::Location loc) {
307 auto load = cir::LoadOp::create(
builder, loc, {itr});
308 auto unary = inverse ?
builder.createDec(loc, load)
309 :
builder.createInc(loc, load);
310 builder.CIRBaseBuilderTy::createStore(loc, unary, itr);
315 cir::ScopeOp::create(
builder, loc,
316 [&](mlir::OpBuilder &b, mlir::Location loc) {
323 builder.setInsertionPoint(bodyInsertLoc);
324 return {subscriptedValue, subscriptedValue2};
327mlir::acc::ReductionOperator
331 return mlir::acc::ReductionOperator::AccAdd;
333 return mlir::acc::ReductionOperator::AccMul;
335 return mlir::acc::ReductionOperator::AccMax;
337 return mlir::acc::ReductionOperator::AccMin;
339 return mlir::acc::ReductionOperator::AccIand;
341 return mlir::acc::ReductionOperator::AccIor;
343 return mlir::acc::ReductionOperator::AccXor;
345 return mlir::acc::ReductionOperator::AccLand;
347 return mlir::acc::ReductionOperator::AccLor;
349 llvm_unreachable(
"invalid reduction operator");
352 llvm_unreachable(
"invalid reduction operator");
360 mlir::Location loc, mlir::Location locEnd, mlir::Value mainOp,
362 mlir::Region &destroyRegion) {
365 builder.setInsertionPointToEnd(&destroyRegion.back());
368 mlir::Type elementTy =
369 mlir::cast<cir::PointerType>(mainOp.getType()).getPointee();
370 auto emitDestroy = [&](mlir::Value var, mlir::Type ty) {
371 Address addr{var, ty, alignment};
372 cgf.emitDestroy(addr, origType,
377 mlir::OpBuilder::InsertionGuard guardCase(
builder);
381 block->getArguments().drop_front(2);
383 mlir::Value subscriptedValue = block->getArgument(1);
384 for (mlir::BlockArgument boundArg : llvm::reverse(boundsRange))
388 emitDestroy(subscriptedValue,
cgf.cgm.convertType(origType));
394 emitDestroy(block->getArgument(1), elementTy);
398 mlir::acc::YieldOp::create(
builder, locEnd);
400void OpenACCRecipeBuilderBase::makeBoundsInit(
401 mlir::Value alloca, mlir::Location loc, mlir::Block *block,
403 mlir::OpBuilder::InsertionGuard guardCase(
builder);
404 builder.setInsertionPointToEnd(block);
414 block->getArguments().drop_front(isInitSection ? 1 : 2);
416 mlir::Value subscriptedValue = alloca;
417 for (mlir::BlockArgument boundArg : llvm::reverse(boundsRange))
431 mlir::Location loc, mlir::Location locEnd,
SourceRange exprRange,
432 mlir::Value mainOp, mlir::Region &recipeInitRegion,
size_t numBounds,
434 QualType origType,
bool emitInitExpr) {
435 assert(allocaDecl &&
"Required recipe variable not set?");
439 loc, numBounds,
true);
440 builder.setInsertionPointToEnd(&recipeInitRegion.back());
443 const Type *allocaPointeeType =
447 if (
cgf.getContext().getLangOpts().CPlusPlus && !allocaDecl->
getInit() &&
456 cgf.cgm.errorNYI(exprRange,
"private/reduction default-init recipe");
463 cgf.emitAutoVarAlloca(*allocaDecl,
builder.saveInsertionPoint());
465 cgf.emitAutoVarInit(tempDeclEmission);
467 mlir::Value alloca = makeBoundsAlloca(
468 block, exprRange, loc, allocaDecl->
getName(), numBounds, boundTypes);
472 if (emitInitExpr && allocaDecl->
getInit() &&
473 (!
cgf.isTrivialInitializer(allocaDecl->
getInit()) ||
474 cgf.getContext().getLangOpts().getTrivialAutoVarInit() !=
476 makeBoundsInit(alloca, loc, block, allocaDecl, origType,
481 mlir::acc::YieldOp::create(
builder, locEnd);
485 mlir::Location loc, mlir::Location locEnd, mlir::Value mainOp,
487 mlir::Region ©Region,
size_t numBounds) {
490 builder.setInsertionPointToEnd(©Region.back());
493 mlir::Value fromArg = block->getArgument(0);
494 mlir::Value toArg = block->getArgument(1);
497 block->getArguments().drop_front(2);
499 for (mlir::BlockArgument boundArg : llvm::reverse(boundsRange))
500 std::tie(fromArg, toArg) =
504 mlir::Type elementTy =
505 mlir::cast<cir::PointerType>(toArg.getType()).getPointee();
509 Address{toArg, elementTy,
cgf.getContext().getDeclAlign(allocaDecl)});
513 cgf.setAddrOfLocalVar(
515 Address{fromArg, elementTy,
cgf.getContext().getDeclAlign(allocaDecl)});
516 cgf.emitAutoVarInit(tempDeclEmission);
518 builder.setInsertionPointToEnd(©Region.back());
520 mlir::acc::YieldOp::create(
builder, locEnd);
528 mlir::Location loc, mlir::Location locEnd, mlir::Value mainOp,
529 mlir::acc::ReductionRecipeOp recipe,
size_t numBounds,
QualType origType,
534 builder.setInsertionPointToEnd(&recipe.getCombinerRegion().back());
537 mlir::Value lhsArg = block->getArgument(0);
538 mlir::Value rhsArg = block->getArgument(1);
540 block->getArguments().drop_front(2);
542 if (llvm::any_of(combinerRecipes, [](
auto &r) {
return r.Op ==
nullptr; })) {
543 cgf.cgm.errorNYI(loc,
"OpenACC Reduction combiner not generated");
544 mlir::acc::YieldOp::create(
builder, locEnd, block->getArgument(0));
549 for (mlir::BlockArgument boundArg : llvm::reverse(boundsRange))
550 std::tie(lhsArg, rhsArg) =
556 auto emitSingleCombiner =
557 [&](mlir::Value lhsArg, mlir::Value rhsArg,
559 mlir::Type elementTy =
560 mlir::cast<cir::PointerType>(lhsArg.getType()).getPointee();
562 cgf.setAddrOfLocalVar(
563 combiner.LHS,
Address{lhsArg, elementTy,
564 cgf.getContext().getDeclAlign(combiner.LHS)});
566 cgf.setAddrOfLocalVar(
567 combiner.RHS,
Address{rhsArg, elementTy,
568 cgf.getContext().getDeclAlign(combiner.RHS)});
570 [[maybe_unused]] mlir::LogicalResult stmtRes =
571 cgf.emitStmt(combiner.Op,
true);
578 auto emitCombiner = [&](mlir::Value lhsArg, mlir::Value rhsArg,
QualType ty) {
579 assert(!ty->isArrayType() &&
"Array type shouldn't get here");
580 if (
const auto *rd = ty->getAsRecordDecl()) {
581 if (combinerRecipes.size() == 1 &&
582 cgf.getContext().hasSameType(ty, combinerRecipes[0].LHS->getType())) {
585 emitSingleCombiner(lhsArg, rhsArg, combinerRecipes[0]);
590 cgf.cgm.getTypes().getCIRGenRecordLayout(rd);
591 for (
const auto &[field, combiner] :
592 llvm::zip_equal(rd->fields(), combinerRecipes)) {
593 mlir::Type fieldType =
cgf.convertType(field->getType());
594 auto fieldPtr = cir::PointerType::get(fieldType);
597 mlir::Value lhsField =
builder.createGetMember(
598 loc, fieldPtr, lhsArg, field->getName(), fieldIndex);
599 mlir::Value rhsField =
builder.createGetMember(
600 loc, fieldPtr, rhsArg, field->getName(), fieldIndex);
602 emitSingleCombiner(lhsField, rhsField, combiner);
609 emitSingleCombiner(lhsArg, rhsArg, combinerRecipes[0]);
613 if (
const auto *cat =
cgf.getContext().getAsConstantArrayType(origType)) {
616 auto itrTy = mlir::cast<cir::IntType>(
cgf.ptrDiffTy);
617 auto itrPtrTy = cir::PointerType::get(itrTy);
620 builder.getConstInt(loc, mlir::cast<cir::IntType>(
cgf.ptrDiffTy), 0);
621 mlir::Value itr = cir::AllocaOp::create(
622 builder, loc, itrPtrTy,
"itr",
cgf.cgm.getSize(
cgf.getPointerAlign()));
623 builder.CIRBaseBuilderTy::createStore(loc, zero, itr);
628 [&](mlir::OpBuilder &b, mlir::Location loc) {
629 auto loadItr = cir::LoadOp::create(builder, loc, {itr});
630 mlir::Value arraySize =
builder.getConstInt(
631 loc, mlir::cast<cir::IntType>(
cgf.ptrDiffTy), cat->getZExtSize());
632 auto cmp =
builder.createCompare(loc, cir::CmpOpKind::lt, loadItr,
637 [&](mlir::OpBuilder &b, mlir::Location loc) {
638 auto loadItr = cir::LoadOp::create(
builder, loc, {itr});
639 auto lhsElt =
builder.getArrayElement(
640 loc, loc, lhsArg,
cgf.convertType(cat->getElementType()), loadItr,
642 auto rhsElt =
builder.getArrayElement(
643 loc, loc, rhsArg,
cgf.convertType(cat->getElementType()), loadItr,
646 emitCombiner(lhsElt, rhsElt, cat->getElementType());
650 [&](mlir::OpBuilder &b, mlir::Location loc) {
651 auto loadItr = cir::LoadOp::create(
builder, loc, {itr});
652 auto inc =
builder.createInc(loc, loadItr);
653 builder.CIRBaseBuilderTy::createStore(loc, inc, itr);
658 cgf.cgm.errorNYI(loc,
659 "OpenACC Reduction combiner non-constant array recipe");
661 emitCombiner(lhsArg, rhsArg, origType);
664 builder.setInsertionPointToEnd(&recipe.getCombinerRegion().back());
666 mlir::acc::YieldOp::create(builder, locEnd, block->getArgument(0));
cir::ConditionOp createCondition(mlir::Value condition)
Create a loop condition.
cir::ForOp createFor(mlir::Location loc, llvm::function_ref< void(mlir::OpBuilder &, mlir::Location)> condBuilder, llvm::function_ref< void(mlir::OpBuilder &, mlir::Location)> bodyBuilder, llvm::function_ref< void(mlir::OpBuilder &, mlir::Location)> stepBuilder)
Create a for operation.
cir::CmpOp createCompare(mlir::Location loc, cir::CmpOpKind kind, mlir::Value lhs, mlir::Value rhs)
CharUnits getTypeAlignInChars(QualType T) const
Return the ABI-specified alignment of a (complete) type T, in characters.
CharUnits getDeclAlign(const Decl *D, bool ForAlignof=false) const
Return a conservative estimate of the alignment of the specified decl D.
CanQualType UnsignedLongLongTy
cir::ConstantOp getConstInt(mlir::Location loc, llvm::APSInt intVal)
void forceCleanup(ArrayRef< mlir::Value * > valuesToReload={})
Force the emission of cleanups now, instead of waiting until this object is destroyed.
mlir::Type convertType(clang::QualType t)
void emitAutoVarInit(const AutoVarEmission &emission)
Emit the initializer for an allocated variable.
clang::ASTContext & getContext() const
mlir::Type convertType(clang::QualType type)
mlir::IntegerAttr getSize(CharUnits size)
This class handles record and union layout info while lowering AST types to CIR types.
unsigned getCIRFieldNo(const clang::FieldDecl *fd) const
Return cir::RecordType element number that corresponds to the field FD.
void createReductionRecipeCombiner(mlir::Location loc, mlir::Location locEnd, mlir::Value mainOp, mlir::acc::ReductionRecipeOp recipe, size_t numBounds, QualType origType, llvm::ArrayRef< OpenACCReductionRecipe::CombinerRecipe > combinerRecipes)
void createInitRecipe(mlir::Location loc, mlir::Location locEnd, SourceRange exprRange, mlir::Value mainOp, mlir::Region &recipeInitRegion, size_t numBounds, llvm::ArrayRef< QualType > boundTypes, const VarDecl *allocaDecl, QualType origType, bool emitInitExpr)
CIRGen::CIRGenBuilderTy & builder
void createFirstprivateRecipeCopy(mlir::Location loc, mlir::Location locEnd, mlir::Value mainOp, const VarDecl *allocaDecl, const VarDecl *temporary, mlir::Region ©Region, size_t numBounds)
mlir::acc::ReductionOperator convertReductionOp(OpenACCReductionOperator op)
CIRGen::CIRGenFunction & cgf
std::pair< mlir::Value, mlir::Value > createBoundsLoop(mlir::Value subscriptedValue, mlir::Value subscriptedValue2, mlir::Value bound, mlir::Location loc, bool inverse)
void createRecipeDestroySection(mlir::Location loc, mlir::Location locEnd, mlir::Value mainOp, CharUnits alignment, QualType origType, size_t numBounds, QualType baseType, mlir::Region &destroyRegion)
mlir::Block * createRecipeBlock(mlir::Region ®ion, mlir::Type opTy, mlir::Location loc, size_t numBounds, bool isInit)
CharUnits - This is an opaque type for sizes expressed in character units.
StringRef getName() const
Get the name of identifier for this declaration as a StringRef.
A (possibly-)qualified type.
A trivial tuple used to represent a source range.
const Type * getPointeeOrArrayElementType() const
If this is a pointer type, return the pointee type.
bool isPointerType() const
bool isBuiltinType() const
Helper methods to distinguish type categories.
Represents a variable declaration or definition.
const Expr * getInit() const
@ Type
The l-value was considered opaque, so the alignment was determined from a type.
@ Invalid
Invalid Reduction Clause Kind.
bool isa(CodeGen::Address addr)
U cast(CodeGen::Address addr)
bool emittedAsOffload
True if the variable was emitted as an offload recipe, and thus doesn't have the same sort of alloca ...
void setAllocatedAddress(Address a)
Represents a scope, including function bodies, compound statements, and the substatements of if/while...