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 builder.createBuiltinIntCast(loc, upperBoundVal.getResult(), itrTy);
131 auto isArrayTy = [&](QualType ty) {
132 if (ty->isArrayType() && !ty->isConstantArrayType())
133 cgf.cgm.errorNYI(exprRange,
"OpenACC recipe init for VLAs");
134 return ty->isConstantArrayType();
137 mlir::Type topLevelTy =
cgf.convertType(boundTypes.back());
138 cir::PointerType topLevelTyPtr =
builder.getPointerTo(topLevelTy);
140 mlir::Value initialAlloca =
builder.createAlloca(
141 loc, topLevelTyPtr, allocaName,
142 cgf.getContext().getTypeAlignInChars(boundTypes.back()));
144 bool lastBoundWasArray = isArrayTy(boundTypes.back());
148 mlir::Value lastAlloca = initialAlloca;
153 llvm::ArrayRef<QualType> boundResults = boundTypes.drop_back(1);
156 llvm::SmallVector<bool> allocasLeftArr;
157 llvm::ArrayRef<QualType> resultTypes = boundTypes.drop_front();
158 std::transform_inclusive_scan(
159 resultTypes.begin(), resultTypes.end(),
160 std::back_inserter(allocasLeftArr), std::plus<bool>{},
161 [](QualType ty) { return !ty->isConstantArrayType(); },
false);
165 mlir::Value cumulativeElts;
166 for (
auto [bound, resultType, allocasLeft] : llvm::reverse(
167 llvm::zip_equal(boundsRange, boundResults, allocasLeftArr))) {
175 mlir::Value eltsPerSubArray = getUpperBound(bound);
176 mlir::Value eltsToAlloca;
183 eltsToAlloca =
builder.createMul(loc, eltsPerSubArray, cumulativeElts);
185 eltsToAlloca = eltsPerSubArray;
187 if (!lastBoundWasArray) {
190 TypeInfoChars eltInfo =
cgf.getContext().getTypeInfoInChars(resultType);
191 cir::ConstantOp eltSize =
builder.getConstInt(
192 loc, itrTy, eltInfo.Width.alignTo(eltInfo.Align).getQuantity());
193 mlir::Value curSize =
builder.createMul(loc, eltsToAlloca, eltSize);
195 mlir::Type eltTy =
cgf.convertType(resultType);
196 cir::PointerType ptrTy =
builder.getPointerTo(eltTy);
197 mlir::Value curAlloca =
builder.createAlloca(
198 loc, ptrTy, eltTy,
"openacc.init.bounds",
199 cgf.getContext().getTypeAlignInChars(resultType), curSize);
201 makeAllocaCopy(loc, ptrTy, cumulativeElts, eltsPerSubArray, lastAlloca,
203 lastAlloca = curAlloca;
208 cir::ConstantOp constZero =
builder.getConstInt(loc, itrTy, 0);
209 lastAlloca =
builder.getArrayElement(loc, loc, lastAlloca,
210 cgf.convertType(resultType),
214 cumulativeElts = eltsToAlloca;
215 lastBoundWasArray = isArrayTy(resultType);
217 return initialAlloca;
221 mlir::Value subscriptedValue, mlir::Value subscriptedValue2,
222 mlir::Value bound, mlir::Location loc,
bool inverse) {
223 mlir::Operation *bodyInsertLoc;
225 mlir::Type itrTy =
cgf.cgm.convertType(
cgf.getContext().UnsignedLongLongTy);
226 auto itrPtrTy = cir::PointerType::get(itrTy);
227 mlir::IntegerAttr itrAlign =
228 cgf.cgm.getSize(
cgf.getContext().getTypeAlignInChars(
229 cgf.getContext().UnsignedLongLongTy));
230 auto idxType = mlir::IndexType::get(&
cgf.getMLIRContext());
232 auto doSubscriptOp = [&](mlir::Value subVal,
233 cir::LoadOp idxLoad) -> mlir::Value {
236 if (
auto arrayTy = dyn_cast<cir::ArrayType>(eltTy))
237 return builder.getArrayElement(loc, loc, subVal, arrayTy.getElementType(),
243 auto eltLoad = cir::LoadOp::create(
builder, loc, {subVal});
245 return cir::PtrStrideOp::create(
builder, loc, eltLoad.getType(), eltLoad,
249 auto forStmtBuilder = [&]() {
252 mlir::acc::GetLowerboundOp::create(
builder, loc, idxType, bound);
253 mlir::Value lbConversion =
254 builder.createBuiltinIntCast(loc, lowerBoundVal.getResult(), itrTy);
256 mlir::acc::GetUpperboundOp::create(
builder, loc, idxType, bound);
257 mlir::Value ubConversion =
258 builder.createBuiltinIntCast(loc, upperBoundVal.getResult(), itrTy);
261 auto itr = cir::AllocaOp::create(
builder, loc, itrPtrTy,
"iter", itrAlign);
265 cir::ConstantOp constOne =
builder.getConstInt(loc, itrTy, 1);
267 auto sub = cir::SubOp::create(
builder, loc, ubConversion, constOne);
270 builder.CIRBaseBuilderTy::createStore(loc, sub, itr);
273 builder.CIRBaseBuilderTy::createStore(loc, lbConversion, itr);
278 mlir::Value endItr = inverse ? lbConversion : ubConversion;
283 [&](mlir::OpBuilder &b, mlir::Location loc) {
284 auto loadCur = cir::LoadOp::create(
builder, loc, {itr});
286 auto cmp =
builder.createCompare(
287 loc, inverse ? cir::CmpOpKind::ge : cir::CmpOpKind::lt, loadCur,
292 [&](mlir::OpBuilder &b, mlir::Location loc) {
293 auto load = cir::LoadOp::create(
builder, loc, {itr});
295 if (subscriptedValue)
296 subscriptedValue = doSubscriptOp(subscriptedValue, load);
297 if (subscriptedValue2)
298 subscriptedValue2 = doSubscriptOp(subscriptedValue2, load);
299 bodyInsertLoc =
builder.createYield(loc);
302 [&](mlir::OpBuilder &b, mlir::Location loc) {
303 auto load = cir::LoadOp::create(
builder, loc, {itr});
304 auto unary = inverse ?
builder.createDec(loc, load)
305 :
builder.createInc(loc, load);
306 builder.CIRBaseBuilderTy::createStore(loc, unary, itr);
311 cir::ScopeOp::create(
builder, loc,
312 [&](mlir::OpBuilder &b, mlir::Location loc) {
319 builder.setInsertionPoint(bodyInsertLoc);
320 return {subscriptedValue, subscriptedValue2};
323mlir::acc::ReductionOperator
327 return mlir::acc::ReductionOperator::AccAdd;
329 return mlir::acc::ReductionOperator::AccMul;
331 return mlir::acc::ReductionOperator::AccMax;
333 return mlir::acc::ReductionOperator::AccMin;
335 return mlir::acc::ReductionOperator::AccIand;
337 return mlir::acc::ReductionOperator::AccIor;
339 return mlir::acc::ReductionOperator::AccXor;
341 return mlir::acc::ReductionOperator::AccLand;
343 return mlir::acc::ReductionOperator::AccLor;
345 llvm_unreachable(
"invalid reduction operator");
348 llvm_unreachable(
"invalid reduction operator");
356 mlir::Location loc, mlir::Location locEnd, mlir::Value mainOp,
358 mlir::Region &destroyRegion) {
361 builder.setInsertionPointToEnd(&destroyRegion.back());
364 mlir::Type elementTy =
365 mlir::cast<cir::PointerType>(mainOp.getType()).getPointee();
366 auto emitDestroy = [&](mlir::Value var, mlir::Type ty) {
367 Address addr{var, ty, alignment};
368 cgf.emitDestroy(addr, origType,
373 mlir::OpBuilder::InsertionGuard guardCase(
builder);
377 block->getArguments().drop_front(2);
379 mlir::Value subscriptedValue = block->getArgument(1);
380 for (mlir::BlockArgument boundArg : llvm::reverse(boundsRange))
384 emitDestroy(subscriptedValue,
cgf.cgm.convertType(origType));
390 emitDestroy(block->getArgument(1), elementTy);
394 mlir::acc::YieldOp::create(
builder, locEnd);
396void OpenACCRecipeBuilderBase::makeBoundsInit(
397 mlir::Value alloca, mlir::Location loc, mlir::Block *block,
399 mlir::OpBuilder::InsertionGuard guardCase(
builder);
400 builder.setInsertionPointToEnd(block);
410 block->getArguments().drop_front(isInitSection ? 1 : 2);
412 mlir::Value subscriptedValue = alloca;
413 for (mlir::BlockArgument boundArg : llvm::reverse(boundsRange))
427 mlir::Location loc, mlir::Location locEnd,
SourceRange exprRange,
428 mlir::Value mainOp, mlir::Region &recipeInitRegion,
size_t numBounds,
430 QualType origType,
bool emitInitExpr) {
431 assert(allocaDecl &&
"Required recipe variable not set?");
435 loc, numBounds,
true);
436 builder.setInsertionPointToEnd(&recipeInitRegion.back());
439 const Type *allocaPointeeType =
443 if (
cgf.getContext().getLangOpts().CPlusPlus && !allocaDecl->
getInit() &&
452 cgf.cgm.errorNYI(exprRange,
"private/reduction default-init recipe");
459 cgf.emitAutoVarAlloca(*allocaDecl,
builder.saveInsertionPoint());
461 cgf.emitAutoVarInit(tempDeclEmission);
463 mlir::Value alloca = makeBoundsAlloca(
464 block, exprRange, loc, allocaDecl->
getName(), numBounds, boundTypes);
468 if (emitInitExpr && allocaDecl->
getInit() &&
469 (!
cgf.isTrivialInitializer(allocaDecl->
getInit()) ||
470 cgf.getContext().getLangOpts().getTrivialAutoVarInit() !=
472 makeBoundsInit(alloca, loc, block, allocaDecl, origType,
477 mlir::acc::YieldOp::create(
builder, locEnd);
481 mlir::Location loc, mlir::Location locEnd, mlir::Value mainOp,
483 mlir::Region ©Region,
size_t numBounds) {
486 builder.setInsertionPointToEnd(©Region.back());
489 mlir::Value fromArg = block->getArgument(0);
490 mlir::Value toArg = block->getArgument(1);
493 block->getArguments().drop_front(2);
495 for (mlir::BlockArgument boundArg : llvm::reverse(boundsRange))
496 std::tie(fromArg, toArg) =
500 mlir::Type elementTy =
501 mlir::cast<cir::PointerType>(toArg.getType()).getPointee();
505 Address{toArg, elementTy,
cgf.getContext().getDeclAlign(allocaDecl)});
509 cgf.setAddrOfLocalVar(
511 Address{fromArg, elementTy,
cgf.getContext().getDeclAlign(allocaDecl)});
512 cgf.emitAutoVarInit(tempDeclEmission);
514 builder.setInsertionPointToEnd(©Region.back());
516 mlir::acc::YieldOp::create(
builder, locEnd);
524 mlir::Location loc, mlir::Location locEnd, mlir::Value mainOp,
525 mlir::acc::ReductionRecipeOp recipe,
size_t numBounds,
QualType origType,
530 builder.setInsertionPointToEnd(&recipe.getCombinerRegion().back());
533 mlir::Value lhsArg = block->getArgument(0);
534 mlir::Value rhsArg = block->getArgument(1);
536 block->getArguments().drop_front(2);
538 if (llvm::any_of(combinerRecipes, [](
auto &r) {
return r.Op ==
nullptr; })) {
539 cgf.cgm.errorNYI(loc,
"OpenACC Reduction combiner not generated");
540 mlir::acc::YieldOp::create(
builder, locEnd, block->getArgument(0));
545 for (mlir::BlockArgument boundArg : llvm::reverse(boundsRange))
546 std::tie(lhsArg, rhsArg) =
552 auto emitSingleCombiner =
553 [&](mlir::Value lhsArg, mlir::Value rhsArg,
555 mlir::Type elementTy =
556 mlir::cast<cir::PointerType>(lhsArg.getType()).getPointee();
558 cgf.setAddrOfLocalVar(
559 combiner.LHS,
Address{lhsArg, elementTy,
560 cgf.getContext().getDeclAlign(combiner.LHS)});
562 cgf.setAddrOfLocalVar(
563 combiner.RHS,
Address{rhsArg, elementTy,
564 cgf.getContext().getDeclAlign(combiner.RHS)});
566 [[maybe_unused]] mlir::LogicalResult stmtRes =
567 cgf.emitStmt(combiner.Op,
true);
574 auto emitCombiner = [&](mlir::Value lhsArg, mlir::Value rhsArg,
QualType ty) {
575 assert(!ty->isArrayType() &&
"Array type shouldn't get here");
576 if (
const auto *rd = ty->getAsRecordDecl()) {
577 if (combinerRecipes.size() == 1 &&
578 cgf.getContext().hasSameType(ty, combinerRecipes[0].LHS->getType())) {
581 emitSingleCombiner(lhsArg, rhsArg, combinerRecipes[0]);
586 cgf.cgm.getTypes().getCIRGenRecordLayout(rd);
587 for (
const auto &[field, combiner] :
588 llvm::zip_equal(rd->fields(), combinerRecipes)) {
589 mlir::Type fieldType =
cgf.convertType(field->getType());
590 auto fieldPtr = cir::PointerType::get(fieldType);
593 mlir::Value lhsField =
builder.createGetMember(
594 loc, fieldPtr, lhsArg, field->getName(), fieldIndex);
595 mlir::Value rhsField =
builder.createGetMember(
596 loc, fieldPtr, rhsArg, field->getName(), fieldIndex);
598 emitSingleCombiner(lhsField, rhsField, combiner);
605 emitSingleCombiner(lhsArg, rhsArg, combinerRecipes[0]);
609 if (
const auto *cat =
cgf.getContext().getAsConstantArrayType(origType)) {
612 auto itrTy = mlir::cast<cir::IntType>(
cgf.ptrDiffTy);
613 auto itrPtrTy = cir::PointerType::get(itrTy);
616 builder.getConstInt(loc, mlir::cast<cir::IntType>(
cgf.ptrDiffTy), 0);
617 mlir::Value itr = cir::AllocaOp::create(
618 builder, loc, itrPtrTy,
"itr",
cgf.cgm.getSize(
cgf.getPointerAlign()));
619 builder.CIRBaseBuilderTy::createStore(loc, zero, itr);
624 [&](mlir::OpBuilder &b, mlir::Location loc) {
625 auto loadItr = cir::LoadOp::create(builder, loc, {itr});
626 mlir::Value arraySize =
builder.getConstInt(
627 loc, mlir::cast<cir::IntType>(
cgf.ptrDiffTy), cat->getZExtSize());
628 auto cmp =
builder.createCompare(loc, cir::CmpOpKind::lt, loadItr,
633 [&](mlir::OpBuilder &b, mlir::Location loc) {
634 auto loadItr = cir::LoadOp::create(
builder, loc, {itr});
635 auto lhsElt =
builder.getArrayElement(
636 loc, loc, lhsArg,
cgf.convertType(cat->getElementType()), loadItr,
638 auto rhsElt =
builder.getArrayElement(
639 loc, loc, rhsArg,
cgf.convertType(cat->getElementType()), loadItr,
642 emitCombiner(lhsElt, rhsElt, cat->getElementType());
646 [&](mlir::OpBuilder &b, mlir::Location loc) {
647 auto loadItr = cir::LoadOp::create(
builder, loc, {itr});
648 auto inc =
builder.createInc(loc, loadItr);
649 builder.CIRBaseBuilderTy::createStore(loc, inc, itr);
654 cgf.cgm.errorNYI(loc,
655 "OpenACC Reduction combiner non-constant array recipe");
657 emitCombiner(lhsArg, rhsArg, origType);
660 builder.setInsertionPointToEnd(&recipe.getCombinerRegion().back());
662 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...