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 bool accumulator =
false;
159 for (QualType ty : resultTypes) {
160 accumulator = accumulator || !ty->isConstantArrayType();
161 allocasLeftArr.push_back(accumulator);
166 mlir::Value cumulativeElts;
167 for (
auto [bound, resultType, allocasLeft] : llvm::reverse(
168 llvm::zip_equal(boundsRange, boundResults, allocasLeftArr))) {
176 mlir::Value eltsPerSubArray = getUpperBound(bound);
177 mlir::Value eltsToAlloca;
184 eltsToAlloca =
builder.createMul(loc, eltsPerSubArray, cumulativeElts);
186 eltsToAlloca = eltsPerSubArray;
188 if (!lastBoundWasArray) {
191 TypeInfoChars eltInfo =
cgf.getContext().getTypeInfoInChars(resultType);
192 cir::ConstantOp eltSize =
builder.getConstInt(
193 loc, itrTy, eltInfo.Width.alignTo(eltInfo.Align).getQuantity());
194 mlir::Value curSize =
builder.createMul(loc, eltsToAlloca, eltSize);
196 mlir::Type eltTy =
cgf.convertType(resultType);
197 cir::PointerType ptrTy =
builder.getPointerTo(eltTy);
198 mlir::Value curAlloca =
builder.createAlloca(
199 loc, ptrTy, eltTy,
"openacc.init.bounds",
200 cgf.getContext().getTypeAlignInChars(resultType), curSize);
202 makeAllocaCopy(loc, ptrTy, cumulativeElts, eltsPerSubArray, lastAlloca,
204 lastAlloca = curAlloca;
209 cir::ConstantOp constZero =
builder.getConstInt(loc, itrTy, 0);
210 lastAlloca =
builder.getArrayElement(loc, loc, lastAlloca,
211 cgf.convertType(resultType),
215 cumulativeElts = eltsToAlloca;
216 lastBoundWasArray = isArrayTy(resultType);
218 return initialAlloca;
222 mlir::Value subscriptedValue, mlir::Value subscriptedValue2,
223 mlir::Value bound, mlir::Location loc,
bool inverse) {
224 mlir::Operation *bodyInsertLoc;
226 mlir::Type itrTy =
cgf.cgm.convertType(
cgf.getContext().UnsignedLongLongTy);
227 auto itrPtrTy = cir::PointerType::get(itrTy);
228 mlir::IntegerAttr itrAlign =
229 cgf.cgm.getSize(
cgf.getContext().getTypeAlignInChars(
230 cgf.getContext().UnsignedLongLongTy));
231 auto idxType = mlir::IndexType::get(&
cgf.getMLIRContext());
233 auto doSubscriptOp = [&](mlir::Value subVal,
234 cir::LoadOp idxLoad) -> mlir::Value {
237 if (
auto arrayTy = dyn_cast<cir::ArrayType>(eltTy))
238 return builder.getArrayElement(loc, loc, subVal, arrayTy.getElementType(),
244 auto eltLoad = cir::LoadOp::create(
builder, loc, {subVal});
246 return cir::PtrStrideOp::create(
builder, loc, eltLoad.getType(), eltLoad,
250 auto forStmtBuilder = [&]() {
253 mlir::acc::GetLowerboundOp::create(
builder, loc, idxType, bound);
254 mlir::Value lbConversion =
255 builder.createBuiltinIntCast(loc, lowerBoundVal.getResult(), itrTy);
257 mlir::acc::GetUpperboundOp::create(
builder, loc, idxType, bound);
258 mlir::Value ubConversion =
259 builder.createBuiltinIntCast(loc, upperBoundVal.getResult(), itrTy);
262 auto itr = cir::AllocaOp::create(
builder, loc, itrPtrTy,
"iter", itrAlign);
266 cir::ConstantOp constOne =
builder.getConstInt(loc, itrTy, 1);
268 auto sub = cir::SubOp::create(
builder, loc, ubConversion, constOne);
271 builder.CIRBaseBuilderTy::createStore(loc, sub, itr);
274 builder.CIRBaseBuilderTy::createStore(loc, lbConversion, itr);
279 mlir::Value endItr = inverse ? lbConversion : ubConversion;
284 [&](mlir::OpBuilder &b, mlir::Location loc) {
285 auto loadCur = cir::LoadOp::create(
builder, loc, {itr});
287 auto cmp =
builder.createCompare(
288 loc, inverse ? cir::CmpOpKind::ge : cir::CmpOpKind::lt, loadCur,
293 [&](mlir::OpBuilder &b, mlir::Location loc) {
294 auto load = cir::LoadOp::create(
builder, loc, {itr});
296 if (subscriptedValue)
297 subscriptedValue = doSubscriptOp(subscriptedValue, load);
298 if (subscriptedValue2)
299 subscriptedValue2 = doSubscriptOp(subscriptedValue2, load);
300 bodyInsertLoc =
builder.createYield(loc);
303 [&](mlir::OpBuilder &b, mlir::Location loc) {
304 auto load = cir::LoadOp::create(
builder, loc, {itr});
305 auto unary = inverse ?
builder.createDec(loc, load)
306 :
builder.createInc(loc, load);
307 builder.CIRBaseBuilderTy::createStore(loc, unary, itr);
312 cir::ScopeOp::create(
builder, loc,
313 [&](mlir::OpBuilder &b, mlir::Location loc) {
320 builder.setInsertionPoint(bodyInsertLoc);
321 return {subscriptedValue, subscriptedValue2};
324mlir::acc::ReductionOperator
328 return mlir::acc::ReductionOperator::AccAdd;
330 return mlir::acc::ReductionOperator::AccMul;
332 return mlir::acc::ReductionOperator::AccMax;
334 return mlir::acc::ReductionOperator::AccMin;
336 return mlir::acc::ReductionOperator::AccIand;
338 return mlir::acc::ReductionOperator::AccIor;
340 return mlir::acc::ReductionOperator::AccXor;
342 return mlir::acc::ReductionOperator::AccLand;
344 return mlir::acc::ReductionOperator::AccLor;
346 llvm_unreachable(
"invalid reduction operator");
349 llvm_unreachable(
"invalid reduction operator");
357 mlir::Location loc, mlir::Location locEnd, mlir::Value mainOp,
359 mlir::Region &destroyRegion) {
362 builder.setInsertionPointToEnd(&destroyRegion.back());
365 mlir::Type elementTy =
366 mlir::cast<cir::PointerType>(mainOp.getType()).getPointee();
367 auto emitDestroy = [&](mlir::Value var, mlir::Type ty) {
368 Address addr{var, ty, alignment};
369 cgf.emitDestroy(addr, origType,
374 mlir::OpBuilder::InsertionGuard guardCase(
builder);
378 block->getArguments().drop_front(2);
380 mlir::Value subscriptedValue = block->getArgument(1);
381 for (mlir::BlockArgument boundArg : llvm::reverse(boundsRange))
385 emitDestroy(subscriptedValue,
cgf.cgm.convertType(origType));
391 emitDestroy(block->getArgument(1), elementTy);
395 mlir::acc::YieldOp::create(
builder, locEnd);
397void OpenACCRecipeBuilderBase::makeBoundsInit(
398 mlir::Value alloca, mlir::Location loc, mlir::Block *block,
400 mlir::OpBuilder::InsertionGuard guardCase(
builder);
401 builder.setInsertionPointToEnd(block);
411 block->getArguments().drop_front(isInitSection ? 1 : 2);
413 mlir::Value subscriptedValue = alloca;
414 for (mlir::BlockArgument boundArg : llvm::reverse(boundsRange))
428 mlir::Location loc, mlir::Location locEnd,
SourceRange exprRange,
429 mlir::Value mainOp, mlir::Region &recipeInitRegion,
size_t numBounds,
431 QualType origType,
bool emitInitExpr) {
432 assert(allocaDecl &&
"Required recipe variable not set?");
436 loc, numBounds,
true);
437 builder.setInsertionPointToEnd(&recipeInitRegion.back());
440 const Type *allocaPointeeType =
444 if (
cgf.getContext().getLangOpts().CPlusPlus && !allocaDecl->
getInit() &&
453 cgf.cgm.errorNYI(exprRange,
"private/reduction default-init recipe");
460 cgf.emitAutoVarAlloca(*allocaDecl,
builder.saveInsertionPoint());
462 cgf.emitAutoVarInit(tempDeclEmission);
464 mlir::Value alloca = makeBoundsAlloca(
465 block, exprRange, loc, allocaDecl->
getName(), numBounds, boundTypes);
469 if (emitInitExpr && allocaDecl->
getInit() &&
470 (!
cgf.isTrivialInitializer(allocaDecl->
getInit()) ||
471 cgf.getContext().getLangOpts().getTrivialAutoVarInit() !=
473 makeBoundsInit(alloca, loc, block, allocaDecl, origType,
478 mlir::acc::YieldOp::create(
builder, locEnd);
482 mlir::Location loc, mlir::Location locEnd, mlir::Value mainOp,
484 mlir::Region ©Region,
size_t numBounds) {
487 builder.setInsertionPointToEnd(©Region.back());
490 mlir::Value fromArg = block->getArgument(0);
491 mlir::Value toArg = block->getArgument(1);
494 block->getArguments().drop_front(2);
496 for (mlir::BlockArgument boundArg : llvm::reverse(boundsRange))
497 std::tie(fromArg, toArg) =
501 mlir::Type elementTy =
502 mlir::cast<cir::PointerType>(toArg.getType()).getPointee();
506 Address{toArg, elementTy,
cgf.getContext().getDeclAlign(allocaDecl)});
510 cgf.setAddrOfLocalVar(
512 Address{fromArg, elementTy,
cgf.getContext().getDeclAlign(allocaDecl)});
513 cgf.emitAutoVarInit(tempDeclEmission);
515 builder.setInsertionPointToEnd(©Region.back());
517 mlir::acc::YieldOp::create(
builder, locEnd);
525 mlir::Location loc, mlir::Location locEnd, mlir::Value mainOp,
526 mlir::acc::ReductionRecipeOp recipe,
size_t numBounds,
QualType origType,
531 builder.setInsertionPointToEnd(&recipe.getCombinerRegion().back());
534 mlir::Value lhsArg = block->getArgument(0);
535 mlir::Value rhsArg = block->getArgument(1);
537 block->getArguments().drop_front(2);
539 if (llvm::any_of(combinerRecipes, [](
auto &r) {
return r.Op ==
nullptr; })) {
540 cgf.cgm.errorNYI(loc,
"OpenACC Reduction combiner not generated");
541 mlir::acc::YieldOp::create(
builder, locEnd, block->getArgument(0));
546 for (mlir::BlockArgument boundArg : llvm::reverse(boundsRange))
547 std::tie(lhsArg, rhsArg) =
553 auto emitSingleCombiner =
554 [&](mlir::Value lhsArg, mlir::Value rhsArg,
556 mlir::Type elementTy =
557 mlir::cast<cir::PointerType>(lhsArg.getType()).getPointee();
559 cgf.setAddrOfLocalVar(
560 combiner.LHS,
Address{lhsArg, elementTy,
561 cgf.getContext().getDeclAlign(combiner.LHS)});
563 cgf.setAddrOfLocalVar(
564 combiner.RHS,
Address{rhsArg, elementTy,
565 cgf.getContext().getDeclAlign(combiner.RHS)});
567 [[maybe_unused]] mlir::LogicalResult stmtRes =
568 cgf.emitStmt(combiner.Op,
true);
575 auto emitCombiner = [&](mlir::Value lhsArg, mlir::Value rhsArg,
QualType ty) {
576 assert(!ty->isArrayType() &&
"Array type shouldn't get here");
577 if (
const auto *rd = ty->getAsRecordDecl()) {
578 if (combinerRecipes.size() == 1 &&
579 cgf.getContext().hasSameType(ty, combinerRecipes[0].LHS->getType())) {
582 emitSingleCombiner(lhsArg, rhsArg, combinerRecipes[0]);
587 cgf.cgm.getTypes().getCIRGenRecordLayout(rd);
588 for (
const auto &[field, combiner] :
589 llvm::zip_equal(rd->fields(), combinerRecipes)) {
590 mlir::Type fieldType =
cgf.convertType(field->getType());
591 auto fieldPtr = cir::PointerType::get(fieldType);
594 mlir::Value lhsField =
builder.createGetMember(
595 loc, fieldPtr, lhsArg, field->getName(), fieldIndex);
596 mlir::Value rhsField =
builder.createGetMember(
597 loc, fieldPtr, rhsArg, field->getName(), fieldIndex);
599 emitSingleCombiner(lhsField, rhsField, combiner);
606 emitSingleCombiner(lhsArg, rhsArg, combinerRecipes[0]);
610 if (
const auto *cat =
cgf.getContext().getAsConstantArrayType(origType)) {
613 auto itrTy = mlir::cast<cir::IntType>(
cgf.ptrDiffTy);
614 auto itrPtrTy = cir::PointerType::get(itrTy);
617 builder.getConstInt(loc, mlir::cast<cir::IntType>(
cgf.ptrDiffTy), 0);
618 mlir::Value itr = cir::AllocaOp::create(
619 builder, loc, itrPtrTy,
"itr",
cgf.cgm.getSize(
cgf.getPointerAlign()));
620 builder.CIRBaseBuilderTy::createStore(loc, zero, itr);
625 [&](mlir::OpBuilder &b, mlir::Location loc) {
626 auto loadItr = cir::LoadOp::create(builder, loc, {itr});
627 mlir::Value arraySize =
builder.getConstInt(
628 loc, mlir::cast<cir::IntType>(
cgf.ptrDiffTy), cat->getZExtSize());
629 auto cmp =
builder.createCompare(loc, cir::CmpOpKind::lt, loadItr,
634 [&](mlir::OpBuilder &b, mlir::Location loc) {
635 auto loadItr = cir::LoadOp::create(
builder, loc, {itr});
636 auto lhsElt =
builder.getArrayElement(
637 loc, loc, lhsArg,
cgf.convertType(cat->getElementType()), loadItr,
639 auto rhsElt =
builder.getArrayElement(
640 loc, loc, rhsArg,
cgf.convertType(cat->getElementType()), loadItr,
643 emitCombiner(lhsElt, rhsElt, cat->getElementType());
647 [&](mlir::OpBuilder &b, mlir::Location loc) {
648 auto loadItr = cir::LoadOp::create(
builder, loc, {itr});
649 auto inc =
builder.createInc(loc, loadItr);
650 builder.CIRBaseBuilderTy::createStore(loc, inc, itr);
655 cgf.cgm.errorNYI(loc,
656 "OpenACC Reduction combiner non-constant array recipe");
658 emitCombiner(lhsArg, rhsArg, origType);
661 builder.setInsertionPointToEnd(&recipe.getCombinerRegion().back());
663 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...