22 types.reserve(numBounds + 2);
23 types.push_back(opTy);
28 types.push_back(opTy);
30 auto boundsTy = mlir::acc::DataBoundsType::get(&
cgf.getMLIRContext());
31 for (
size_t i = 0; i < numBounds; ++i)
32 types.push_back(boundsTy);
35 return builder.createBlock(®ion, region.end(), types, locs);
37void OpenACCRecipeBuilderBase::makeAllocaCopy(mlir::Location loc,
39 mlir::Value numEltsToCopy,
40 mlir::Value offsetPerSubarray,
41 mlir::Value destAlloca,
42 mlir::Value srcAlloca) {
43 mlir::OpBuilder::InsertionGuard guardCase(
builder);
46 auto itrPtrTy = cir::PointerType::get(itrTy);
47 mlir::IntegerAttr itrAlign =
51 auto loopBuilder = [&]() {
52 auto itr = cir::AllocaOp::create(
builder, loc, itrPtrTy,
"itr", itrAlign);
54 builder.CIRBaseBuilderTy::createStore(loc, constZero, itr);
58 [&](mlir::OpBuilder &b, mlir::Location loc) {
66 auto loadCur = cir::LoadOp::create(
builder, loc, itr.getResult());
72 [&](mlir::OpBuilder &b, mlir::Location loc) {
74 auto loadCur = cir::LoadOp::create(
builder, loc, itr.getResult());
77 auto ptrToOffsetIntoSrc = cir::PtrStrideOp::create(
78 builder, loc, copyType, srcAlloca, srcOffset);
80 auto offsetIntoDecayDest = cir::PtrStrideOp::create(
84 builder.CIRBaseBuilderTy::createStore(loc, ptrToOffsetIntoSrc,
89 [&](mlir::OpBuilder &b, mlir::Location loc) {
91 auto load = cir::LoadOp::create(
builder, loc, itr.getResult());
93 builder.CIRBaseBuilderTy::createStore(loc, inc, itr);
98 cir::ScopeOp::create(
builder, loc,
99 [&](mlir::OpBuilder &b, mlir::Location loc) {
105mlir::Value OpenACCRecipeBuilderBase::makeBoundsAlloca(
106 mlir::Block *block, SourceRange exprRange, mlir::Location loc,
107 std::string_view allocaName,
size_t numBounds,
108 llvm::ArrayRef<QualType> boundTypes) {
109 mlir::OpBuilder::InsertionGuard guardCase(
builder);
112 llvm::ArrayRef<mlir::BlockArgument> boundsRange =
113 block->getArguments().drop_front(1);
118 assert(boundsRange.size() + 1 == boundTypes.size());
120 mlir::Type itrTy =
cgf.cgm.convertType(
cgf.getContext().UnsignedLongLongTy);
121 auto idxType = mlir::IndexType::get(&
cgf.getMLIRContext());
123 auto getUpperBound = [&](mlir::Value bound) {
125 mlir::acc::GetUpperboundOp::create(
builder, loc, idxType, bound);
126 return builder.createBuiltinIntCast(loc, upperBoundVal.getResult(), itrTy);
129 auto isArrayTy = [&](QualType ty) {
130 if (ty->isArrayType() && !ty->isConstantArrayType())
131 cgf.cgm.errorNYI(exprRange,
"OpenACC recipe init for VLAs");
132 return ty->isConstantArrayType();
135 mlir::Type topLevelTy =
cgf.convertType(boundTypes.back());
136 cir::PointerType topLevelTyPtr =
builder.getPointerTo(topLevelTy);
138 mlir::Value initialAlloca =
builder.createAlloca(
139 loc, topLevelTyPtr, allocaName,
140 cgf.getContext().getTypeAlignInChars(boundTypes.back()));
142 bool lastBoundWasArray = isArrayTy(boundTypes.back());
146 mlir::Value lastAlloca = initialAlloca;
151 llvm::ArrayRef<QualType> boundResults = boundTypes.drop_back(1);
154 llvm::SmallVector<bool> allocasLeftArr;
155 llvm::ArrayRef<QualType> resultTypes = boundTypes.drop_front();
156 bool accumulator =
false;
157 for (QualType ty : resultTypes) {
158 accumulator = accumulator || !ty->isConstantArrayType();
159 allocasLeftArr.push_back(accumulator);
164 mlir::Value cumulativeElts;
165 for (
auto [bound, resultType, allocasLeft] : llvm::reverse(
166 llvm::zip_equal(boundsRange, boundResults, allocasLeftArr))) {
174 mlir::Value eltsPerSubArray = getUpperBound(bound);
175 mlir::Value eltsToAlloca;
182 eltsToAlloca =
builder.createMul(loc, eltsPerSubArray, cumulativeElts);
184 eltsToAlloca = eltsPerSubArray;
186 if (!lastBoundWasArray) {
189 TypeInfoChars eltInfo =
cgf.getContext().getTypeInfoInChars(resultType);
190 cir::ConstantOp eltSize =
builder.getConstInt(
191 loc, itrTy, eltInfo.Width.alignTo(eltInfo.Align).getQuantity());
192 mlir::Value curSize =
builder.createMul(loc, eltsToAlloca, eltSize);
194 mlir::Type eltTy =
cgf.convertType(resultType);
195 cir::PointerType ptrTy =
builder.getPointerTo(eltTy);
196 mlir::Value curAlloca =
builder.createAlloca(
197 loc, ptrTy, eltTy,
"openacc.init.bounds",
198 cgf.getContext().getTypeAlignInChars(resultType), curSize);
200 makeAllocaCopy(loc, ptrTy, cumulativeElts, eltsPerSubArray, lastAlloca,
202 lastAlloca = curAlloca;
207 cir::ConstantOp constZero =
builder.getConstInt(loc, itrTy, 0);
208 lastAlloca =
builder.getArrayElement(loc, loc, lastAlloca,
209 cgf.convertType(resultType),
213 cumulativeElts = eltsToAlloca;
214 lastBoundWasArray = isArrayTy(resultType);
216 return initialAlloca;
220 mlir::Value subscriptedValue, mlir::Value subscriptedValue2,
221 mlir::Value bound, mlir::Location loc,
bool inverse) {
222 mlir::Operation *bodyInsertLoc;
224 mlir::Type itrTy =
cgf.cgm.convertType(
cgf.getContext().UnsignedLongLongTy);
225 auto itrPtrTy = cir::PointerType::get(itrTy);
226 mlir::IntegerAttr itrAlign =
227 cgf.cgm.getSize(
cgf.getContext().getTypeAlignInChars(
228 cgf.getContext().UnsignedLongLongTy));
229 auto idxType = mlir::IndexType::get(&
cgf.getMLIRContext());
231 auto doSubscriptOp = [&](mlir::Value subVal,
232 cir::LoadOp idxLoad) -> mlir::Value {
235 if (
auto arrayTy = dyn_cast<cir::ArrayType>(eltTy))
236 return builder.getArrayElement(loc, loc, subVal, arrayTy.getElementType(),
242 auto eltLoad = cir::LoadOp::create(
builder, loc, subVal);
244 return cir::PtrStrideOp::create(
builder, loc, eltLoad.getType(), eltLoad,
248 auto forStmtBuilder = [&]() {
251 mlir::acc::GetLowerboundOp::create(
builder, loc, idxType, bound);
252 mlir::Value lbConversion =
253 builder.createBuiltinIntCast(loc, lowerBoundVal.getResult(), itrTy);
255 mlir::acc::GetUpperboundOp::create(
builder, loc, idxType, bound);
256 mlir::Value ubConversion =
257 builder.createBuiltinIntCast(loc, upperBoundVal.getResult(), itrTy);
260 auto itr = cir::AllocaOp::create(
builder, loc, itrPtrTy,
"iter", itrAlign);
264 cir::ConstantOp constOne =
builder.getConstInt(loc, itrTy, 1);
266 auto sub = cir::SubOp::create(
builder, loc, ubConversion, constOne);
269 builder.CIRBaseBuilderTy::createStore(loc, sub, itr);
272 builder.CIRBaseBuilderTy::createStore(loc, lbConversion, itr);
277 mlir::Value endItr = inverse ? lbConversion : ubConversion;
282 [&](mlir::OpBuilder &b, mlir::Location loc) {
283 auto loadCur = cir::LoadOp::create(
builder, loc, itr.getResult());
285 auto cmp =
builder.createCompare(
286 loc, inverse ? cir::CmpOpKind::ge : cir::CmpOpKind::lt, loadCur,
291 [&](mlir::OpBuilder &b, mlir::Location loc) {
292 auto load = cir::LoadOp::create(
builder, loc, itr.getResult());
294 if (subscriptedValue)
295 subscriptedValue = doSubscriptOp(subscriptedValue, load);
296 if (subscriptedValue2)
297 subscriptedValue2 = doSubscriptOp(subscriptedValue2, load);
298 bodyInsertLoc =
builder.createYield(loc);
301 [&](mlir::OpBuilder &b, mlir::Location loc) {
302 auto load = cir::LoadOp::create(
builder, loc, itr.getResult());
303 auto unary = inverse ?
builder.createDec(loc, load)
304 :
builder.createInc(loc, load);
305 builder.CIRBaseBuilderTy::createStore(loc, unary, itr);
310 cir::ScopeOp::create(
builder, loc,
311 [&](mlir::OpBuilder &b, mlir::Location loc) {
318 builder.setInsertionPoint(bodyInsertLoc);
319 return {subscriptedValue, subscriptedValue2};
322mlir::acc::ReductionOperator
326 return mlir::acc::ReductionOperator::AccAdd;
328 return mlir::acc::ReductionOperator::AccMul;
330 return mlir::acc::ReductionOperator::AccMax;
332 return mlir::acc::ReductionOperator::AccMin;
334 return mlir::acc::ReductionOperator::AccIand;
336 return mlir::acc::ReductionOperator::AccIor;
338 return mlir::acc::ReductionOperator::AccXor;
340 return mlir::acc::ReductionOperator::AccLand;
342 return mlir::acc::ReductionOperator::AccLor;
344 llvm_unreachable(
"invalid reduction operator");
347 llvm_unreachable(
"invalid reduction operator");
355 mlir::Location loc, mlir::Location locEnd, mlir::Value mainOp,
357 mlir::Region &destroyRegion) {
360 builder.setInsertionPointToEnd(&destroyRegion.back());
363 mlir::Type elementTy =
364 mlir::cast<cir::PointerType>(mainOp.getType()).getPointee();
365 auto emitDestroy = [&](mlir::Value var, mlir::Type ty) {
366 Address addr{var, ty, alignment};
367 cgf.emitDestroy(addr, origType,
372 mlir::OpBuilder::InsertionGuard guardCase(
builder);
376 block->getArguments().drop_front(2);
378 mlir::Value subscriptedValue = block->getArgument(1);
379 for (mlir::BlockArgument boundArg : llvm::reverse(boundsRange))
383 emitDestroy(subscriptedValue,
cgf.cgm.convertType(origType));
389 emitDestroy(block->getArgument(1), elementTy);
393 mlir::acc::YieldOp::create(
builder, locEnd);
395void OpenACCRecipeBuilderBase::makeBoundsInit(
396 mlir::Value alloca, mlir::Location loc, mlir::Block *block,
398 mlir::OpBuilder::InsertionGuard guardCase(
builder);
399 builder.setInsertionPointToEnd(block);
409 block->getArguments().drop_front(isInitSection ? 1 : 2);
411 mlir::Value subscriptedValue = alloca;
412 for (mlir::BlockArgument boundArg : llvm::reverse(boundsRange))
426 mlir::Location loc, mlir::Location locEnd,
SourceRange exprRange,
427 mlir::Value mainOp, mlir::Region &recipeInitRegion,
size_t numBounds,
429 QualType origType,
bool emitInitExpr) {
430 assert(allocaDecl &&
"Required recipe variable not set?");
434 loc, numBounds,
true);
435 builder.setInsertionPointToEnd(&recipeInitRegion.back());
438 const Type *allocaPointeeType =
442 if (
cgf.getContext().getLangOpts().CPlusPlus && !allocaDecl->
getInit() &&
451 cgf.cgm.errorNYI(exprRange,
"private/reduction default-init recipe");
458 cgf.emitAutoVarAlloca(*allocaDecl,
builder.saveInsertionPoint());
460 cgf.emitAutoVarInit(tempDeclEmission);
462 mlir::Value alloca = makeBoundsAlloca(
463 block, exprRange, loc, allocaDecl->
getName(), numBounds, boundTypes);
467 if (emitInitExpr && allocaDecl->
getInit() &&
468 (!
cgf.isTrivialInitializer(allocaDecl->
getInit()) ||
469 cgf.getContext().getLangOpts().getTrivialAutoVarInit() !=
471 makeBoundsInit(alloca, loc, block, allocaDecl, origType,
476 mlir::acc::YieldOp::create(
builder, locEnd);
480 mlir::Location loc, mlir::Location locEnd, mlir::Value mainOp,
482 mlir::Region ©Region,
size_t numBounds) {
485 builder.setInsertionPointToEnd(©Region.back());
488 mlir::Value fromArg = block->getArgument(0);
489 mlir::Value toArg = block->getArgument(1);
492 block->getArguments().drop_front(2);
494 for (mlir::BlockArgument boundArg : llvm::reverse(boundsRange))
495 std::tie(fromArg, toArg) =
499 mlir::Type elementTy =
500 mlir::cast<cir::PointerType>(toArg.getType()).getPointee();
504 Address{toArg, elementTy,
cgf.getContext().getDeclAlign(allocaDecl)});
508 cgf.setAddrOfLocalVar(
510 Address{fromArg, elementTy,
cgf.getContext().getDeclAlign(allocaDecl)});
511 cgf.emitAutoVarInit(tempDeclEmission);
513 builder.setInsertionPointToEnd(©Region.back());
515 mlir::acc::YieldOp::create(
builder, locEnd);
523 mlir::Location loc, mlir::Location locEnd, mlir::Value mainOp,
524 mlir::acc::ReductionRecipeOp recipe,
size_t numBounds,
QualType origType,
529 builder.setInsertionPointToEnd(&recipe.getCombinerRegion().back());
532 mlir::Value lhsArg = block->getArgument(0);
533 mlir::Value rhsArg = block->getArgument(1);
535 block->getArguments().drop_front(2);
537 if (llvm::any_of(combinerRecipes, [](
auto &r) {
return r.Op ==
nullptr; })) {
538 cgf.cgm.errorNYI(loc,
"OpenACC Reduction combiner not generated");
539 mlir::acc::YieldOp::create(
builder, locEnd, block->getArgument(0));
544 for (mlir::BlockArgument boundArg : llvm::reverse(boundsRange))
545 std::tie(lhsArg, rhsArg) =
551 auto emitSingleCombiner =
552 [&](mlir::Value lhsArg, mlir::Value rhsArg,
554 mlir::Type elementTy =
555 mlir::cast<cir::PointerType>(lhsArg.getType()).getPointee();
557 cgf.setAddrOfLocalVar(
558 combiner.LHS,
Address{lhsArg, elementTy,
559 cgf.getContext().getDeclAlign(combiner.LHS)});
561 cgf.setAddrOfLocalVar(
562 combiner.RHS,
Address{rhsArg, elementTy,
563 cgf.getContext().getDeclAlign(combiner.RHS)});
565 [[maybe_unused]] mlir::LogicalResult stmtRes =
566 cgf.emitStmt(combiner.Op,
true);
573 auto emitCombiner = [&](mlir::Value lhsArg, mlir::Value rhsArg,
QualType ty) {
574 assert(!ty->isArrayType() &&
"Array type shouldn't get here");
575 if (
const auto *rd = ty->getAsRecordDecl()) {
576 if (combinerRecipes.size() == 1 &&
577 cgf.getContext().hasSameType(ty, combinerRecipes[0].LHS->getType())) {
580 emitSingleCombiner(lhsArg, rhsArg, combinerRecipes[0]);
585 cgf.cgm.getTypes().getCIRGenRecordLayout(rd);
586 for (
const auto &[field, combiner] :
587 llvm::zip_equal(rd->fields(), combinerRecipes)) {
588 mlir::Type fieldType =
cgf.convertType(field->getType());
589 auto fieldPtr = cir::PointerType::get(fieldType);
592 mlir::Value lhsField =
builder.createGetMember(
593 loc, fieldPtr, lhsArg, field->getName(), fieldIndex);
594 mlir::Value rhsField =
builder.createGetMember(
595 loc, fieldPtr, rhsArg, field->getName(), fieldIndex);
597 emitSingleCombiner(lhsField, rhsField, combiner);
604 emitSingleCombiner(lhsArg, rhsArg, combinerRecipes[0]);
608 if (
const auto *cat =
cgf.getContext().getAsConstantArrayType(origType)) {
611 auto itrTy = mlir::cast<cir::IntType>(
cgf.ptrDiffTy);
612 auto itrPtrTy = cir::PointerType::get(itrTy);
615 builder.getConstInt(loc, mlir::cast<cir::IntType>(
cgf.ptrDiffTy), 0);
616 mlir::Value itr = cir::AllocaOp::create(
617 builder, loc, itrPtrTy,
"itr",
cgf.cgm.getSize(
cgf.getPointerAlign()));
618 builder.CIRBaseBuilderTy::createStore(loc, zero, itr);
623 [&](mlir::OpBuilder &b, mlir::Location loc) {
624 auto loadItr = cir::LoadOp::create(builder, loc, itr);
625 mlir::Value arraySize = builder.getConstInt(
626 loc, mlir::cast<cir::IntType>(cgf.ptrDiffTy), cat->getZExtSize());
627 auto cmp = builder.createCompare(loc, cir::CmpOpKind::lt, loadItr,
629 builder.createCondition(cmp);
632 [&](mlir::OpBuilder &b, mlir::Location loc) {
633 auto loadItr = cir::LoadOp::create(builder, loc, itr);
634 auto lhsElt = builder.getArrayElement(
635 loc, loc, lhsArg, cgf.convertType(cat->getElementType()), loadItr,
637 auto rhsElt = builder.getArrayElement(
638 loc, loc, rhsArg, cgf.convertType(cat->getElementType()), loadItr,
641 emitCombiner(lhsElt, rhsElt, cat->getElementType());
642 builder.createYield(loc);
645 [&](mlir::OpBuilder &b, mlir::Location loc) {
646 auto loadItr = cir::LoadOp::create(builder, loc, itr);
647 auto inc = builder.createInc(loc, loadItr);
648 builder.CIRBaseBuilderTy::createStore(loc, inc, itr);
649 builder.createYield(loc);
653 cgf.cgm.errorNYI(loc,
654 "OpenACC Reduction combiner non-constant array recipe");
656 emitCombiner(lhsArg, rhsArg, origType);
659 builder.setInsertionPointToEnd(&recipe.getCombinerRegion().back());
661 mlir::acc::YieldOp::create(
builder, locEnd, block->getArgument(0));
cir::ConditionOp createCondition(mlir::Value condition)
Create a loop condition.
mlir::Value createInc(mlir::Location loc, mlir::Value input, bool nsw=false)
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::PointerType getPointerTo(mlir::Type ty)
mlir::Value createMul(mlir::Location loc, mlir::Value lhs, mlir::Value rhs, OverflowFlags flags=OverflowFlags::None)
cir::CmpOp createCompare(mlir::Location loc, cir::CmpOpKind kind, mlir::Value lhs, mlir::Value rhs)
cir::YieldOp createYield(mlir::Location loc, mlir::ValueRange value={})
Create a yield operation.
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)
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...