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 = [&]() {
55 cir::AllocaOp::create(
builder, loc, itrPtrTy, itrTy,
"itr", itrAlign);
57 builder.CIRBaseBuilderTy::createStore(loc, constZero, itr);
61 [&](mlir::OpBuilder &
b, mlir::Location loc) {
69 auto loadCur = cir::LoadOp::create(
builder, loc, {itr});
75 [&](mlir::OpBuilder &
b, mlir::Location loc) {
77 auto loadCur = cir::LoadOp::create(
builder, loc, {itr});
78 auto srcOffset =
builder.createMul(loc, offsetPerSubarray, loadCur);
80 auto ptrToOffsetIntoSrc = cir::PtrStrideOp::create(
81 builder, loc, copyType, srcAlloca, srcOffset);
83 auto offsetIntoDecayDest = cir::PtrStrideOp::create(
87 builder.CIRBaseBuilderTy::createStore(loc, ptrToOffsetIntoSrc,
92 [&](mlir::OpBuilder &
b, mlir::Location loc) {
94 auto load = cir::LoadOp::create(
builder, loc, {itr});
95 auto inc = cir::UnaryOp::create(
builder, loc, load.getType(),
96 cir::UnaryOpKind::Inc, load);
97 builder.CIRBaseBuilderTy::createStore(loc, inc, itr);
102 cir::ScopeOp::create(
builder, loc,
103 [&](mlir::OpBuilder &
b, mlir::Location loc) {
109mlir::Value OpenACCRecipeBuilderBase::makeBoundsAlloca(
110 mlir::Block *block, SourceRange exprRange, mlir::Location loc,
111 std::string_view allocaName,
size_t numBounds,
112 llvm::ArrayRef<QualType> boundTypes) {
113 mlir::OpBuilder::InsertionGuard guardCase(
builder);
116 llvm::ArrayRef<mlir::BlockArgument> boundsRange =
117 block->getArguments().drop_front(1);
122 assert(boundsRange.size() + 1 == boundTypes.size());
124 mlir::Type itrTy =
cgf.cgm.convertType(
cgf.getContext().UnsignedLongLongTy);
125 auto idxType = mlir::IndexType::get(&
cgf.getMLIRContext());
127 auto getUpperBound = [&](mlir::Value bound) {
129 mlir::acc::GetUpperboundOp::create(
builder, loc, idxType, bound);
130 return mlir::UnrealizedConversionCastOp::create(
builder, loc, itrTy,
131 upperBoundVal.getResult())
135 auto isArrayTy = [&](QualType ty) {
136 if (ty->isArrayType() && !ty->isConstantArrayType())
137 cgf.cgm.errorNYI(exprRange,
"OpenACC recipe init for VLAs");
138 return ty->isConstantArrayType();
141 mlir::Type topLevelTy =
cgf.convertType(boundTypes.back());
142 cir::PointerType topLevelTyPtr =
builder.getPointerTo(topLevelTy);
144 mlir::Value initialAlloca =
builder.createAlloca(
145 loc, topLevelTyPtr, topLevelTy, allocaName,
146 cgf.getContext().getTypeAlignInChars(boundTypes.back()));
148 bool lastBoundWasArray = isArrayTy(boundTypes.back());
152 mlir::Value lastAlloca = initialAlloca;
157 llvm::ArrayRef<QualType> boundResults = boundTypes.drop_back(1);
160 llvm::SmallVector<bool> allocasLeftArr;
161 llvm::ArrayRef<QualType> resultTypes = boundTypes.drop_front();
162 std::transform_inclusive_scan(
163 resultTypes.begin(), resultTypes.end(),
164 std::back_inserter(allocasLeftArr), std::plus<bool>{},
165 [](QualType ty) { return !ty->isConstantArrayType(); },
false);
169 mlir::Value cumulativeElts;
170 for (
auto [bound, resultType, allocasLeft] : llvm::reverse(
171 llvm::zip_equal(boundsRange, boundResults, allocasLeftArr))) {
179 mlir::Value eltsPerSubArray = getUpperBound(bound);
180 mlir::Value eltsToAlloca;
187 eltsToAlloca =
builder.createMul(loc, eltsPerSubArray, cumulativeElts);
189 eltsToAlloca = eltsPerSubArray;
191 if (!lastBoundWasArray) {
194 TypeInfoChars eltInfo =
cgf.getContext().getTypeInfoInChars(resultType);
195 cir::ConstantOp eltSize =
builder.getConstInt(
196 loc, itrTy, eltInfo.Width.alignTo(eltInfo.Align).getQuantity());
197 mlir::Value curSize =
builder.createMul(loc, eltsToAlloca, eltSize);
199 mlir::Type eltTy =
cgf.convertType(resultType);
200 cir::PointerType ptrTy =
builder.getPointerTo(eltTy);
201 mlir::Value curAlloca =
builder.createAlloca(
202 loc, ptrTy, eltTy,
"openacc.init.bounds",
203 cgf.getContext().getTypeAlignInChars(resultType), curSize);
205 makeAllocaCopy(loc, ptrTy, cumulativeElts, eltsPerSubArray, lastAlloca,
207 lastAlloca = curAlloca;
212 cir::ConstantOp constZero =
builder.getConstInt(loc, itrTy, 0);
213 lastAlloca =
builder.getArrayElement(loc, loc, lastAlloca,
214 cgf.convertType(resultType),
218 cumulativeElts = eltsToAlloca;
219 lastBoundWasArray = isArrayTy(resultType);
221 return initialAlloca;
225 mlir::Value subscriptedValue, mlir::Value subscriptedValue2,
226 mlir::Value bound, mlir::Location loc,
bool inverse) {
227 mlir::Operation *bodyInsertLoc;
229 mlir::Type itrTy =
cgf.cgm.convertType(
cgf.getContext().UnsignedLongLongTy);
230 auto itrPtrTy = cir::PointerType::get(itrTy);
231 mlir::IntegerAttr itrAlign =
232 cgf.cgm.getSize(
cgf.getContext().getTypeAlignInChars(
233 cgf.getContext().UnsignedLongLongTy));
234 auto idxType = mlir::IndexType::get(&
cgf.getMLIRContext());
236 auto doSubscriptOp = [&](mlir::Value subVal,
237 cir::LoadOp idxLoad) -> mlir::Value {
240 if (
auto arrayTy = dyn_cast<cir::ArrayType>(eltTy))
241 return builder.getArrayElement(loc, loc, subVal, arrayTy.getElementType(),
247 auto eltLoad = cir::LoadOp::create(
builder, loc, {subVal});
249 return cir::PtrStrideOp::create(
builder, loc, eltLoad.getType(), eltLoad,
253 auto forStmtBuilder = [&]() {
256 mlir::acc::GetLowerboundOp::create(
builder, loc, idxType, bound);
257 auto lbConversion = mlir::UnrealizedConversionCastOp::create(
258 builder, loc, itrTy, lowerBoundVal.getResult());
260 mlir::acc::GetUpperboundOp::create(
builder, loc, idxType, bound);
261 auto ubConversion = mlir::UnrealizedConversionCastOp::create(
262 builder, loc, itrTy, upperBoundVal.getResult());
266 cir::AllocaOp::create(
builder, loc, itrPtrTy, itrTy,
"iter", itrAlign);
270 cir::ConstantOp constOne =
builder.getConstInt(loc, itrTy, 1);
272 auto sub = cir::BinOp::create(
builder, loc, itrTy, cir::BinOpKind::Sub,
273 ubConversion.getResult(0), constOne);
276 builder.CIRBaseBuilderTy::createStore(loc, sub, itr);
279 builder.CIRBaseBuilderTy::createStore(loc, lbConversion.getResult(0),
285 auto endItr = inverse ? lbConversion : ubConversion;
290 [&](mlir::OpBuilder &
b, mlir::Location loc) {
291 auto loadCur = cir::LoadOp::create(
builder, loc, {itr});
293 auto cmp =
builder.createCompare(
294 loc, inverse ? cir::CmpOpKind::ge : cir::CmpOpKind::lt, loadCur,
295 endItr.getResult(0));
299 [&](mlir::OpBuilder &
b, mlir::Location loc) {
300 auto load = cir::LoadOp::create(
builder, loc, {itr});
302 if (subscriptedValue)
303 subscriptedValue = doSubscriptOp(subscriptedValue, load);
304 if (subscriptedValue2)
305 subscriptedValue2 = doSubscriptOp(subscriptedValue2, load);
306 bodyInsertLoc =
builder.createYield(loc);
309 [&](mlir::OpBuilder &
b, mlir::Location loc) {
310 auto load = cir::LoadOp::create(
builder, loc, {itr});
311 auto unary = cir::UnaryOp::create(
313 inverse ? cir::UnaryOpKind::Dec : cir::UnaryOpKind::Inc, load);
314 builder.CIRBaseBuilderTy::createStore(loc, unary, itr);
319 cir::ScopeOp::create(
builder, loc,
320 [&](mlir::OpBuilder &
b, mlir::Location loc) {
327 builder.setInsertionPoint(bodyInsertLoc);
328 return {subscriptedValue, subscriptedValue2};
331mlir::acc::ReductionOperator
335 return mlir::acc::ReductionOperator::AccAdd;
337 return mlir::acc::ReductionOperator::AccMul;
339 return mlir::acc::ReductionOperator::AccMax;
341 return mlir::acc::ReductionOperator::AccMin;
343 return mlir::acc::ReductionOperator::AccIand;
345 return mlir::acc::ReductionOperator::AccIor;
347 return mlir::acc::ReductionOperator::AccXor;
349 return mlir::acc::ReductionOperator::AccLand;
351 return mlir::acc::ReductionOperator::AccLor;
353 llvm_unreachable(
"invalid reduction operator");
356 llvm_unreachable(
"invalid reduction operator");
364 mlir::Location loc, mlir::Location locEnd, mlir::Value mainOp,
366 mlir::Region &destroyRegion) {
369 builder.setInsertionPointToEnd(&destroyRegion.back());
372 mlir::Type elementTy =
373 mlir::cast<cir::PointerType>(mainOp.getType()).getPointee();
374 auto emitDestroy = [&](mlir::Value var, mlir::Type ty) {
375 Address addr{var, ty, alignment};
376 cgf.emitDestroy(addr, origType,
381 mlir::OpBuilder::InsertionGuard guardCase(
builder);
385 block->getArguments().drop_front(2);
387 mlir::Value subscriptedValue = block->getArgument(1);
388 for (mlir::BlockArgument boundArg : llvm::reverse(boundsRange))
392 emitDestroy(subscriptedValue,
cgf.cgm.convertType(origType));
398 emitDestroy(block->getArgument(1), elementTy);
402 mlir::acc::YieldOp::create(
builder, locEnd);
404void OpenACCRecipeBuilderBase::makeBoundsInit(
405 mlir::Value alloca, mlir::Location loc, mlir::Block *block,
407 mlir::OpBuilder::InsertionGuard guardCase(
builder);
408 builder.setInsertionPointToEnd(block);
418 block->getArguments().drop_front(isInitSection ? 1 : 2);
420 mlir::Value subscriptedValue = alloca;
421 for (mlir::BlockArgument boundArg : llvm::reverse(boundsRange))
435 mlir::Location loc, mlir::Location locEnd,
SourceRange exprRange,
436 mlir::Value mainOp, mlir::Region &recipeInitRegion,
size_t numBounds,
438 QualType origType,
bool emitInitExpr) {
439 assert(allocaDecl &&
"Required recipe variable not set?");
443 loc, numBounds,
true);
444 builder.setInsertionPointToEnd(&recipeInitRegion.back());
447 const Type *allocaPointeeType =
451 if (
cgf.getContext().getLangOpts().CPlusPlus && !allocaDecl->
getInit() &&
460 cgf.cgm.errorNYI(exprRange,
"private/reduction default-init recipe");
467 cgf.emitAutoVarAlloca(*allocaDecl,
builder.saveInsertionPoint());
469 cgf.emitAutoVarInit(tempDeclEmission);
471 mlir::Value alloca = makeBoundsAlloca(
472 block, exprRange, loc, allocaDecl->
getName(), numBounds, boundTypes);
476 if (emitInitExpr && allocaDecl->
getInit() &&
477 (!
cgf.isTrivialInitializer(allocaDecl->
getInit()) ||
478 cgf.getContext().getLangOpts().getTrivialAutoVarInit() !=
480 makeBoundsInit(alloca, loc, block, allocaDecl, origType,
485 mlir::acc::YieldOp::create(
builder, locEnd);
489 mlir::Location loc, mlir::Location locEnd, mlir::Value mainOp,
491 mlir::Region ©Region,
size_t numBounds) {
494 builder.setInsertionPointToEnd(©Region.back());
497 mlir::Value fromArg = block->getArgument(0);
498 mlir::Value toArg = block->getArgument(1);
501 block->getArguments().drop_front(2);
503 for (mlir::BlockArgument boundArg : llvm::reverse(boundsRange))
504 std::tie(fromArg, toArg) =
508 mlir::Type elementTy =
509 mlir::cast<cir::PointerType>(toArg.getType()).getPointee();
513 Address{toArg, elementTy,
cgf.getContext().getDeclAlign(allocaDecl)});
517 cgf.setAddrOfLocalVar(
519 Address{fromArg, elementTy,
cgf.getContext().getDeclAlign(allocaDecl)});
520 cgf.emitAutoVarInit(tempDeclEmission);
522 builder.setInsertionPointToEnd(©Region.back());
524 mlir::acc::YieldOp::create(
builder, locEnd);
532 mlir::Location loc, mlir::Location locEnd, mlir::Value mainOp,
533 mlir::acc::ReductionRecipeOp recipe,
size_t numBounds,
QualType origType,
538 builder.setInsertionPointToEnd(&recipe.getCombinerRegion().back());
541 mlir::Value lhsArg = block->getArgument(0);
542 mlir::Value rhsArg = block->getArgument(1);
544 block->getArguments().drop_front(2);
546 if (llvm::any_of(combinerRecipes, [](
auto &r) {
return r.Op ==
nullptr; })) {
547 cgf.cgm.errorNYI(loc,
"OpenACC Reduction combiner not generated");
548 mlir::acc::YieldOp::create(
builder, locEnd, block->getArgument(0));
553 for (mlir::BlockArgument boundArg : llvm::reverse(boundsRange))
554 std::tie(lhsArg, rhsArg) =
560 auto emitSingleCombiner =
561 [&](mlir::Value lhsArg, mlir::Value rhsArg,
563 mlir::Type elementTy =
564 mlir::cast<cir::PointerType>(lhsArg.getType()).getPointee();
566 cgf.setAddrOfLocalVar(
567 combiner.LHS,
Address{lhsArg, elementTy,
568 cgf.getContext().getDeclAlign(combiner.LHS)});
570 cgf.setAddrOfLocalVar(
571 combiner.RHS,
Address{rhsArg, elementTy,
572 cgf.getContext().getDeclAlign(combiner.RHS)});
574 [[maybe_unused]] mlir::LogicalResult stmtRes =
575 cgf.emitStmt(combiner.Op,
true);
582 auto emitCombiner = [&](mlir::Value lhsArg, mlir::Value rhsArg,
QualType ty) {
583 assert(!ty->isArrayType() &&
"Array type shouldn't get here");
584 if (
const auto *rd = ty->getAsRecordDecl()) {
585 if (combinerRecipes.size() == 1 &&
586 cgf.getContext().hasSameType(ty, combinerRecipes[0].LHS->getType())) {
589 emitSingleCombiner(lhsArg, rhsArg, combinerRecipes[0]);
593 for (
const auto &[field, combiner] :
594 llvm::zip_equal(rd->fields(), combinerRecipes)) {
595 mlir::Type fieldType =
cgf.convertType(field->getType());
596 auto fieldPtr = cir::PointerType::get(fieldType);
598 mlir::Value lhsField =
builder.createGetMember(
599 loc, fieldPtr, lhsArg, field->getName(), field->getFieldIndex());
600 mlir::Value rhsField =
builder.createGetMember(
601 loc, fieldPtr, rhsArg, field->getName(), field->getFieldIndex());
603 emitSingleCombiner(lhsField, rhsField, combiner);
610 emitSingleCombiner(lhsArg, rhsArg, combinerRecipes[0]);
614 if (
const auto *cat =
cgf.getContext().getAsConstantArrayType(origType)) {
617 auto itrTy = mlir::cast<cir::IntType>(
cgf.PtrDiffTy);
618 auto itrPtrTy = cir::PointerType::get(itrTy);
621 builder.getConstInt(loc, mlir::cast<cir::IntType>(
cgf.PtrDiffTy), 0);
623 cir::AllocaOp::create(
builder, loc, itrPtrTy, itrTy,
"itr",
624 cgf.cgm.getSize(
cgf.getPointerAlign()));
625 builder.CIRBaseBuilderTy::createStore(loc, zero, itr);
630 [&](mlir::OpBuilder &
b, mlir::Location loc) {
631 auto loadItr = cir::LoadOp::create(builder, loc, {itr});
632 mlir::Value arraySize =
builder.getConstInt(
633 loc, mlir::cast<cir::IntType>(
cgf.PtrDiffTy), cat->getZExtSize());
634 auto cmp =
builder.createCompare(loc, cir::CmpOpKind::lt, loadItr,
639 [&](mlir::OpBuilder &
b, mlir::Location loc) {
640 auto loadItr = cir::LoadOp::create(
builder, loc, {itr});
641 auto lhsElt =
builder.getArrayElement(
642 loc, loc, lhsArg,
cgf.convertType(cat->getElementType()), loadItr,
644 auto rhsElt =
builder.getArrayElement(
645 loc, loc, rhsArg,
cgf.convertType(cat->getElementType()), loadItr,
648 emitCombiner(lhsElt, rhsElt, cat->getElementType());
652 [&](mlir::OpBuilder &
b, mlir::Location loc) {
653 auto loadItr = cir::LoadOp::create(
builder, loc, {itr});
654 auto inc = cir::UnaryOp::create(
builder, loc, loadItr.getType(),
655 cir::UnaryOpKind::Inc, loadItr);
656 builder.CIRBaseBuilderTy::createStore(loc, inc, itr);
661 cgf.cgm.errorNYI(loc,
662 "OpenACC Reduction combiner non-constant array recipe");
664 emitCombiner(lhsArg, rhsArg, origType);
667 builder.setInsertionPointToEnd(&recipe.getCombinerRegion().back());
669 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()
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)
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...