[clang] [Clang][OpenMP] Fix `inscan` reductions on a `worksharing` loop not combined with parallel (PR #242653)
Akash Manna via cfe-commits
cfe-commits at lists.llvm.org
Sat Nov 14 06:59:57 PST 2026
https://github.com/akash-manna-sky created https://github.com/llvm/llvm-project/pull/242653
Fixes #87466
For a `for` with `reduction(inscan)` that is not combined with `parallel`, the scan buffer was emitted inside the outlined function, so every thread filled a private copy and ran the prefix pass on it, reading slots it never wrote. A non-constant trip count also re-emitted the loop pre-inits into `LocalDeclMap` and tripped an assertion.
The buffer is now allocated once per team: a `single` region allocates it, `__kmpc_copyprivate` hands the pointer to the other threads, the master thread does the prefix pass and the final copy, and the allocating thread frees it after the end barrier.
Assisted-by: LLMs.
>From 053d7d07f88ec183edc4f6b2d4f5d5b4cfe68986 Mon Sep 17 00:00:00 2001
From: Akash Manna <akash.manna.mymail at gmail.com>
Date: Fri, 13 Nov 2026 23:49:30 +0530
Subject: [PATCH] [Clang][CodeGen][OpenMP] Fix inscan reductions on a
worksharing loop not combined with parallel
Fixes #87466
For a `for` with `reduction(inscan)` that is not combined with `parallel`, the scan buffer was emitted inside the outlined function, so every thread filled a private copy and ran the prefix pass on it, reading slots it never wrote. A non-constant trip count also re-emitted the loop pre-inits into `LocalDeclMap` and tripped an assertion.
The buffer is now allocated once per team: a `single` region allocates it, `__kmpc_copyprivate` hands the pointer to the other threads, the master thread does the prefix pass and the final copy, and the allocating thread frees it after the end barrier.
LLM tools were used for this contribution.
---
clang/docs/ReleaseNotes.md | 2 +
clang/lib/CodeGen/CGOpenMPRuntime.cpp | 164 ++++++++++---
clang/lib/CodeGen/CGOpenMPRuntime.h | 20 ++
clang/lib/CodeGen/CGStmtOpenMP.cpp | 244 ++++++++++++++++----
clang/test/OpenMP/GH87466.cpp | 168 ++++++++++++++
clang/test/OpenMP/for_scan_codegen.cpp | 106 ++++++++-
clang/test/OpenMP/for_simd_scan_codegen.cpp | 106 ++++++++-
7 files changed, 710 insertions(+), 100 deletions(-)
create mode 100644 clang/test/OpenMP/GH87466.cpp
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index 6d1dbbbb6eb77b..f17e444b7c697d 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -1513,6 +1513,8 @@ The `alpha.cplusplus.UseAfterLifetimeEnd` checker was renamed to `alpha.core.Use
- The `loop` construct without a `bind` clause or with `bind(thread)` is now
allowed inside a `simd` region for OpenMP 5.0 or later, as the OpenMP
specification permits.
+- Fixed wrong results and an assertion failure for `reduction(inscan, ...)` on a
+ worksharing loop that is not combined with `parallel`. (#GH87466)
- Constructs nested inside a loop-transforming construct are now checked
against the restrictions of an enclosing `simd` region or of a loop with an
`order(concurrent)` clause. Previously, such constructs (e.g. `parallel`
diff --git a/clang/lib/CodeGen/CGOpenMPRuntime.cpp b/clang/lib/CodeGen/CGOpenMPRuntime.cpp
index d3a6ffee3ea748..a7a2c7294eb407 100644
--- a/clang/lib/CodeGen/CGOpenMPRuntime.cpp
+++ b/clang/lib/CodeGen/CGOpenMPRuntime.cpp
@@ -2288,20 +2288,32 @@ void CGOpenMPRuntime::emitTaskgroupRegion(CodeGenFunction &CGF,
/// Given an array of pointers to variables, project the address of a
/// given variable.
static Address emitAddrOfVarFromArray(CodeGenFunction &CGF, Address Array,
- unsigned Index, const VarDecl *Var) {
+ unsigned Index, llvm::Type *ElemTy,
+ CharUnits Align) {
// Pull out the pointer to the variable.
Address PtrAddr = CGF.Builder.CreateConstArrayGEP(Array, Index);
llvm::Value *Ptr = CGF.Builder.CreateLoad(PtrAddr);
+ return Address(Ptr, ElemTy, Align);
+}
- llvm::Type *ElemTy = CGF.ConvertTypeForMem(Var->getType());
- return Address(Ptr, ElemTy, CGF.getContext().getDeclAlign(Var));
+static Address emitAddrOfVarFromArray(CodeGenFunction &CGF, Address Array,
+ unsigned Index, const VarDecl *Var) {
+ return emitAddrOfVarFromArray(CGF, Array, Index,
+ CGF.ConvertTypeForMem(Var->getType()),
+ CGF.getContext().getDeclAlign(Var));
}
+/// Callback that emits the copy of the \p Idx-th copyprivate variable from
+/// \p SrcAddr to \p DestAddr in the copyprivate copy function.
+using CopyprivateCopyGenTy = llvm::function_ref<void(
+ CodeGenFunction &CGF, unsigned Idx, Address DestAddr, Address SrcAddr)>;
+
+/// Emits the copy function for __kmpc_copyprivate. \p VarTypes are the LLVM
+/// types and the alignments of the copyprivate variables.
static llvm::Value *emitCopyprivateCopyFunction(
CodeGenModule &CGM, llvm::Type *ArgsElemType,
- ArrayRef<const Expr *> CopyprivateVars, ArrayRef<const Expr *> DestExprs,
- ArrayRef<const Expr *> SrcExprs, ArrayRef<const Expr *> AssignmentOps,
- SourceLocation Loc) {
+ ArrayRef<std::pair<llvm::Type *, CharUnits>> VarTypes,
+ CopyprivateCopyGenTy CopyGen, SourceLocation Loc) {
ASTContext &C = CGM.getContext();
// void copy_func(void *LHSArg, void *RHSArg);
@@ -2339,34 +2351,105 @@ static llvm::Value *emitCopyprivateCopyFunction(
// *(Type1*)Dst[1] = *(Type1*)Src[1];
// ...
// *(Typen*)Dst[n] = *(Typen*)Src[n];
- for (unsigned I = 0, E = AssignmentOps.size(); I < E; ++I) {
- const auto *DestVar =
- cast<VarDecl>(cast<DeclRefExpr>(DestExprs[I])->getDecl());
- Address DestAddr = emitAddrOfVarFromArray(CGF, LHS, I, DestVar);
-
- const auto *SrcVar =
- cast<VarDecl>(cast<DeclRefExpr>(SrcExprs[I])->getDecl());
- Address SrcAddr = emitAddrOfVarFromArray(CGF, RHS, I, SrcVar);
-
- const auto *VD = cast<DeclRefExpr>(CopyprivateVars[I])->getDecl();
- QualType Type = VD->getType();
- CGF.EmitOMPCopy(Type, DestAddr, SrcAddr, DestVar, SrcVar, AssignmentOps[I]);
+ for (unsigned I = 0, E = VarTypes.size(); I < E; ++I) {
+ Address DestAddr = emitAddrOfVarFromArray(CGF, LHS, I, VarTypes[I].first,
+ VarTypes[I].second);
+ Address SrcAddr = emitAddrOfVarFromArray(CGF, RHS, I, VarTypes[I].first,
+ VarTypes[I].second);
+ CopyGen(CGF, I, DestAddr, SrcAddr);
}
CGF.FinishFunction();
return Fn;
}
+void CGOpenMPRuntime::emitCopyprivateCall(
+ CodeGenFunction &CGF, SourceLocation Loc, Address CopyprivateList,
+ QualType CopyprivateArrayTy, llvm::Value *CpyFn, llvm::Value *DidIt) {
+ // call __kmpc_copyprivate(ident_t *, gtid, <buf_size>, <copyprivate list>,
+ // <copy_func>, did_it);
+ llvm::Value *BufSize = CGF.getTypeSize(CopyprivateArrayTy);
+ Address CL = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
+ CopyprivateList, CGF.VoidPtrTy, CGF.Int8Ty);
+ llvm::Value *Args[] = {
+ emitUpdateLocation(CGF, Loc), // ident_t *<loc>
+ getThreadID(CGF, Loc), // i32 <gtid>
+ BufSize, // size_t <buf_size>
+ CL.emitRawPointer(CGF), // void *<copyprivate list>
+ CpyFn, // void (*) (void *, void *) <copy_func>
+ DidIt // i32 did_it
+ };
+ CGF.EmitRuntimeCall(OMPBuilder.getOrCreateRuntimeFunction(
+ CGM.getModule(), OMPRTL___kmpc_copyprivate),
+ Args);
+}
+
+void CGOpenMPRuntime::emitCopyprivateBroadcast(CodeGenFunction &CGF,
+ SourceLocation Loc,
+ ArrayRef<Address> Addrs,
+ Address DidIt) {
+ if (!CGF.HaveInsertPoint())
+ return;
+ ASTContext &C = CGM.getContext();
+ llvm::APInt ArraySize(/*unsigned int numBits=*/32, Addrs.size());
+ QualType CopyprivateArrayTy = C.getConstantArrayType(
+ C.VoidPtrTy, ArraySize, nullptr, ArraySizeModifier::Normal,
+ /*IndexTypeQuals=*/0);
+ Address CopyprivateList = CGF.CreateMemTempWithoutCast(
+ CopyprivateArrayTy, ".omp.copyprivate.cpr_list");
+ SmallVector<std::pair<llvm::Type *, CharUnits>, 4> VarTypes;
+ for (unsigned I = 0, E = Addrs.size(); I < E; ++I) {
+ assert(Addrs[I].getElementType()->isSingleValueType() &&
+ "Expected a scalar copyprivate variable.");
+ Address Elem = CGF.Builder.CreateConstArrayGEP(CopyprivateList, I);
+ CGF.Builder.CreateStore(CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
+ Addrs[I].emitRawPointer(CGF), CGF.VoidPtrTy),
+ Elem);
+ VarTypes.emplace_back(Addrs[I].getElementType(), Addrs[I].getAlignment());
+ }
+ llvm::Value *CpyFn = emitCopyprivateCopyFunction(
+ CGM, CGF.ConvertTypeForMem(CopyprivateArrayTy), VarTypes,
+ [](CodeGenFunction &CGF, unsigned, Address DestAddr, Address SrcAddr) {
+ CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcAddr), DestAddr);
+ },
+ Loc);
+ emitCopyprivateCall(CGF, Loc, CopyprivateList, CopyprivateArrayTy, CpyFn,
+ CGF.Builder.CreateLoad(DidIt));
+}
+
+llvm::Value *CGOpenMPRuntime::emitAllocCall(CodeGenFunction &CGF,
+ SourceLocation Loc,
+ llvm::Value *Size,
+ CharUnits Alignment) {
+ // Use the default allocator.
+ llvm::Value *Args[] = {getThreadID(CGF, Loc), CGM.getSize(Alignment), Size,
+ llvm::ConstantPointerNull::get(CGF.VoidPtrTy)};
+ return CGF.EmitRuntimeCall(OMPBuilder.getOrCreateRuntimeFunction(
+ CGM.getModule(), OMPRTL___kmpc_aligned_alloc),
+ Args);
+}
+
+void CGOpenMPRuntime::emitFreeCall(CodeGenFunction &CGF, SourceLocation Loc,
+ llvm::Value *Ptr) {
+ llvm::Value *Args[] = {
+ getThreadID(CGF, Loc),
+ CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(Ptr, CGF.VoidPtrTy),
+ llvm::ConstantPointerNull::get(CGF.VoidPtrTy)};
+ CGF.EmitRuntimeCall(OMPBuilder.getOrCreateRuntimeFunction(CGM.getModule(),
+ OMPRTL___kmpc_free),
+ Args);
+}
+
void CGOpenMPRuntime::emitSingleRegion(CodeGenFunction &CGF,
const RegionCodeGenTy &SingleOpGen,
SourceLocation Loc,
ArrayRef<const Expr *> CopyprivateVars,
+ ArrayRef<const Expr *> DestExprs,
ArrayRef<const Expr *> SrcExprs,
- ArrayRef<const Expr *> DstExprs,
ArrayRef<const Expr *> AssignmentOps) {
if (!CGF.HaveInsertPoint())
return;
- assert(CopyprivateVars.size() == SrcExprs.size() &&
- CopyprivateVars.size() == DstExprs.size() &&
+ assert(CopyprivateVars.size() == DestExprs.size() &&
+ CopyprivateVars.size() == SrcExprs.size() &&
CopyprivateVars.size() == AssignmentOps.size());
ASTContext &C = CGM.getContext();
// int32 did_it = 0;
@@ -2412,6 +2495,7 @@ void CGOpenMPRuntime::emitSingleRegion(CodeGenFunction &CGF,
// Create a list of all private variables for copyprivate.
Address CopyprivateList = CGF.CreateMemTempWithoutCast(
CopyprivateArrayTy, ".omp.copyprivate.cpr_list");
+ SmallVector<std::pair<llvm::Type *, CharUnits>, 4> VarTypes;
for (unsigned I = 0, E = CopyprivateVars.size(); I < E; ++I) {
Address Elem = CGF.Builder.CreateConstArrayGEP(CopyprivateList, I);
CGF.Builder.CreateStore(
@@ -2419,27 +2503,31 @@ void CGOpenMPRuntime::emitSingleRegion(CodeGenFunction &CGF,
CGF.EmitLValue(CopyprivateVars[I]).getPointer(CGF),
CGF.VoidPtrTy),
Elem);
+ // The pseudo destination and source variables have the same type.
+ const auto *DestVar =
+ cast<VarDecl>(cast<DeclRefExpr>(DestExprs[I])->getDecl());
+ VarTypes.emplace_back(
+ CGM.getTypes().ConvertTypeForMem(DestVar->getType()),
+ C.getDeclAlign(DestVar));
}
// Build function that copies private values from single region to all other
// threads in the corresponding parallel region.
llvm::Value *CpyFn = emitCopyprivateCopyFunction(
- CGM, CGF.ConvertTypeForMem(CopyprivateArrayTy), CopyprivateVars,
- SrcExprs, DstExprs, AssignmentOps, Loc);
- llvm::Value *BufSize = CGF.getTypeSize(CopyprivateArrayTy);
- Address CL = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
- CopyprivateList, CGF.VoidPtrTy, CGF.Int8Ty);
- llvm::Value *DidItVal = CGF.Builder.CreateLoad(DidIt);
- llvm::Value *Args[] = {
- emitUpdateLocation(CGF, Loc), // ident_t *<loc>
- getThreadID(CGF, Loc), // i32 <gtid>
- BufSize, // size_t <buf_size>
- CL.emitRawPointer(CGF), // void *<copyprivate list>
- CpyFn, // void (*) (void *, void *) <copy_func>
- DidItVal // i32 did_it
- };
- CGF.EmitRuntimeCall(OMPBuilder.getOrCreateRuntimeFunction(
- CGM.getModule(), OMPRTL___kmpc_copyprivate),
- Args);
+ CGM, CGF.ConvertTypeForMem(CopyprivateArrayTy), VarTypes,
+ [&](CodeGenFunction &CGF, unsigned I, Address DestAddr,
+ Address SrcAddr) {
+ const auto *DestVar =
+ cast<VarDecl>(cast<DeclRefExpr>(DestExprs[I])->getDecl());
+ const auto *SrcVar =
+ cast<VarDecl>(cast<DeclRefExpr>(SrcExprs[I])->getDecl());
+ QualType Type =
+ cast<DeclRefExpr>(CopyprivateVars[I])->getDecl()->getType();
+ CGF.EmitOMPCopy(Type, DestAddr, SrcAddr, DestVar, SrcVar,
+ AssignmentOps[I]);
+ },
+ Loc);
+ emitCopyprivateCall(CGF, Loc, CopyprivateList, CopyprivateArrayTy, CpyFn,
+ CGF.Builder.CreateLoad(DidIt));
}
}
diff --git a/clang/lib/CodeGen/CGOpenMPRuntime.h b/clang/lib/CodeGen/CGOpenMPRuntime.h
index ae55295cadc14f..cf26875166aa4b 100644
--- a/clang/lib/CodeGen/CGOpenMPRuntime.h
+++ b/clang/lib/CodeGen/CGOpenMPRuntime.h
@@ -340,6 +340,12 @@ class CGOpenMPRuntime {
///
llvm::Value *getThreadID(CodeGenFunction &CGF, SourceLocation Loc);
+ /// Emits the call to __kmpc_copyprivate for the variables whose addresses are
+ /// stored in \p CopyprivateList (of type \p CopyprivateArrayTy).
+ void emitCopyprivateCall(CodeGenFunction &CGF, SourceLocation Loc,
+ Address CopyprivateList, QualType CopyprivateArrayTy,
+ llvm::Value *CpyFn, llvm::Value *DidIt);
+
/// Get the function name of an outlined region.
std::string getOutlinedHelperName(StringRef Name) const;
std::string getOutlinedHelperName(CodeGenFunction &CGF) const;
@@ -848,6 +854,20 @@ class CGOpenMPRuntime {
ArrayRef<const Expr *> SrcExprs,
ArrayRef<const Expr *> AssignmentOps);
+ /// Emits the copyprivate broadcast (with its barrier) of the scalar private
+ /// variables at \p Addrs from the thread for which the flag at \p DidIt is
+ /// non-zero to all other threads of the team.
+ void emitCopyprivateBroadcast(CodeGenFunction &CGF, SourceLocation Loc,
+ ArrayRef<Address> Addrs, Address DidIt);
+
+ /// Emits __kmpc_aligned_alloc of \p Size bytes aligned to \p Alignment in
+ /// the default allocator and returns the pointer.
+ llvm::Value *emitAllocCall(CodeGenFunction &CGF, SourceLocation Loc,
+ llvm::Value *Size, CharUnits Alignment);
+
+ /// Emits __kmpc_free of the memory \p Ptr allocated with emitAllocCall.
+ void emitFreeCall(CodeGenFunction &CGF, SourceLocation Loc, llvm::Value *Ptr);
+
/// Emit an ordered region.
/// \param OrderedOpGen Generator for the statement associated with the given
/// ordered region.
diff --git a/clang/lib/CodeGen/CGStmtOpenMP.cpp b/clang/lib/CodeGen/CGStmtOpenMP.cpp
index e63957df3e4069..b2b79dc5cf7cf7 100644
--- a/clang/lib/CodeGen/CGStmtOpenMP.cpp
+++ b/clang/lib/CodeGen/CGStmtOpenMP.cpp
@@ -4294,6 +4294,15 @@ emitDispatchForLoopBounds(CodeGenFunction &CGF, const OMPExecutableDirective &S,
return {LBVal, UBVal};
}
+/// Returns true if the directive has a 'reduction' clause with the 'inscan'
+/// modifier.
+static bool hasInscanReduction(const OMPExecutableDirective &S) {
+ return llvm::any_of(S.getClausesOfKind<OMPReductionClause>(),
+ [](const OMPReductionClause *C) {
+ return C->getModifier() == OMPC_REDUCTION_inscan;
+ });
+}
+
/// Emits internal temp array declarations for the directive with inscan
/// reductions.
/// The code is the following:
@@ -4348,6 +4357,140 @@ static void emitScanBasedDirectiveDecls(
}
}
+/// Emits internal temp array declarations for the directive with inscan
+/// reductions that is not combined with 'parallel'. All threads of the team
+/// execute the directive, so the arrays are allocated by one thread and shared
+/// with the others via copyprivate; they are mapped in \p TempsScope.
+/// The code is the following:
+/// \code
+/// size num_iters = <num_iters>;
+/// <type> *buffer;
+/// int did_it = 0;
+/// if (__kmpc_single()) {
+/// buffer = __kmpc_aligned_alloc(num_iters * sizeof(<type>));
+/// did_it = 1;
+/// }
+/// __kmpc_copyprivate(&buffer, did_it);
+/// \endcode
+/// \returns the address of the did_it flag, non-zero in the allocating thread.
+static Address emitScanBasedDirectiveSharedDecls(
+ CodeGenFunction &CGF, const OMPLoopDirective &S,
+ llvm::function_ref<llvm::Value *(CodeGenFunction &)> NumIteratorsGen,
+ CodeGenFunction::OMPPrivateScope &TempsScope) {
+ CGOpenMPRuntime &RT = CGF.CGM.getOpenMPRuntime();
+ llvm::Value *OMPScanNumIterations = CGF.Builder.CreateIntCast(
+ NumIteratorsGen(CGF), CGF.SizeTy, /*isSigned=*/false);
+ SmallVector<const Expr *, 4> Shareds;
+ SmallVector<const Expr *, 4> Privates;
+ SmallVector<const Expr *, 4> ReductionOps;
+ SmallVector<const Expr *, 4> CopyArrayTemps;
+ for (const auto *C : S.getClausesOfKind<OMPReductionClause>()) {
+ assert(C->getModifier() == OMPC_REDUCTION_inscan &&
+ "Only inscan reductions are expected.");
+ Shareds.append(C->varlist_begin(), C->varlist_end());
+ Privates.append(C->privates().begin(), C->privates().end());
+ ReductionOps.append(C->reduction_ops().begin(), C->reduction_ops().end());
+ CopyArrayTemps.append(C->copy_array_temps().begin(),
+ C->copy_array_temps().end());
+ }
+ SmallVector<const VarDecl *, 4> TempVDs;
+ SmallVector<Address, 4> TempPtrAddrs;
+ {
+ ReductionCodeGen RedCG(Shareds, Shareds, Privates, ReductionOps);
+ unsigned Count = 0;
+ auto *ITA = CopyArrayTemps.begin();
+ for (const Expr *IRef : Privates) {
+ const auto *PrivateVD = cast<VarDecl>(cast<DeclRefExpr>(IRef)->getDecl());
+ if (PrivateVD->getType()->isVariablyModifiedType()) {
+ RedCG.emitSharedOrigLValue(CGF, Count);
+ RedCG.emitAggregateType(CGF, Count);
+ }
+ const auto *TempVD = cast<VarDecl>(cast<DeclRefExpr>(*ITA)->getDecl());
+ CodeGenFunction::OpaqueValueMapping DimMapping(
+ CGF,
+ cast<OpaqueValueExpr>(
+ cast<VariableArrayType>(TempVD->getType()->getAsArrayTypeUnsafe())
+ ->getSizeExpr()),
+ RValue::get(OMPScanNumIterations));
+ CGF.EmitVariablyModifiedType(TempVD->getType());
+ TempVDs.push_back(TempVD);
+ TempPtrAddrs.push_back(
+ CGF.CreateMemTemp(CGF.getContext().getPointerType(TempVD->getType()),
+ TempVD->getName() + ".buf.addr"));
+ ++ITA;
+ ++Count;
+ }
+ }
+ Address DidIt = CGF.CreateMemTempWithoutCast(
+ CGF.getContext().getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1),
+ ".omp.scan.did_it");
+ CGF.Builder.CreateStore(CGF.Builder.getInt32(0), DidIt);
+ auto &&AllocGen = [&S, &TempVDs, &TempPtrAddrs,
+ DidIt](CodeGenFunction &CGF, PrePostActionTy &Action) {
+ Action.Enter(CGF);
+ for (unsigned I = 0, E = TempVDs.size(); I < E; ++I) {
+ const VarDecl *TempVD = TempVDs[I];
+ QualType Ty = TempVD->getType();
+ CharUnits Align = CGF.getContext().getDeclAlign(TempVD);
+ llvm::Value *Ptr = CGF.CGM.getOpenMPRuntime().emitAllocCall(
+ CGF, S.getBeginLoc(), CGF.getTypeSize(Ty), Align);
+ CGF.Builder.CreateStore(Ptr, TempPtrAddrs[I]);
+ const Expr *Init = TempVD->getInit();
+ if (Init && !CGF.isTrivialInitializer(Init))
+ CGF.EmitExprAsInit(
+ Init, TempVD,
+ CGF.MakeAddrLValue(Address(Ptr, CGF.ConvertTypeForMem(Ty), Align),
+ Ty, AlignmentSource::Decl),
+ /*capturedByInit=*/false);
+ }
+ CGF.Builder.CreateStore(CGF.Builder.getInt32(1), DidIt);
+ };
+ RT.emitSingleRegion(CGF, AllocGen, S.getBeginLoc(),
+ /*CopyprivateVars=*/{}, /*DestExprs=*/{},
+ /*SrcExprs=*/{}, /*AssignmentOps=*/{});
+ RT.emitCopyprivateBroadcast(CGF, S.getBeginLoc(), TempPtrAddrs, DidIt);
+ for (unsigned I = 0, E = TempVDs.size(); I < E; ++I) {
+ const VarDecl *TempVD = TempVDs[I];
+ llvm::Value *Ptr = CGF.Builder.CreateLoad(TempPtrAddrs[I]);
+ TempsScope.addPrivate(TempVD,
+ Address(Ptr, CGF.ConvertTypeForMem(TempVD->getType()),
+ CGF.getContext().getDeclAlign(TempVD)));
+ }
+ (void)TempsScope.Privatize();
+ return DidIt;
+}
+
+/// Releases the temp arrays allocated by emitScanBasedDirectiveSharedDecls;
+/// must follow the barrier at the end of the directive.
+/// The code is the following:
+/// \code
+/// if (did_it)
+/// __kmpc_free(buffer);
+/// \endcode
+static void emitScanBasedDirectiveSharedDeclsFree(CodeGenFunction &CGF,
+ const OMPLoopDirective &S,
+ Address DidIt) {
+ llvm::BasicBlock *ThenBB = CGF.createBasicBlock("omp.scan.free");
+ llvm::BasicBlock *ExitBB = CGF.createBasicBlock("omp.scan.free.exit");
+ llvm::Value *DidItVal =
+ CGF.Builder.CreateIsNotNull(CGF.Builder.CreateLoad(DidIt));
+ CGF.Builder.CreateCondBr(DidItVal, ThenBB, ExitBB);
+ CGF.EmitBlock(ThenBB);
+ for (const auto *C : S.getClausesOfKind<OMPReductionClause>()) {
+ for (const Expr *E : C->copy_array_temps()) {
+ const auto *TempVD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl());
+ QualType Ty = TempVD->getType();
+ Address Addr = CGF.GetAddrOfLocalVar(TempVD);
+ if (QualType::DestructionKind DtorKind = Ty.isDestructedType())
+ CGF.emitDestroy(Addr, Ty, CGF.getDestroyer(DtorKind),
+ /*useEHCleanupForArray=*/false);
+ CGF.CGM.getOpenMPRuntime().emitFreeCall(CGF, S.getBeginLoc(),
+ Addr.emitRawPointer(CGF));
+ }
+ }
+ CGF.EmitBlock(ExitBB);
+}
+
/// Copies final inscan reductions values to the original variables.
/// The code is the following:
/// \code
@@ -4406,22 +4549,29 @@ static void emitScanBasedDirectiveFinals(
/// <input phase>;
/// buffer[i] = red;
/// }
-/// #pragma omp master // in parallel region
-/// for (int k = 0; k != ceil(log2(num_iters)); ++k)
-/// for (size cnt = last_iter; cnt >= pow(2, k); --k)
-/// buffer[i] op= buffer[i-pow(2,k)];
-/// #pragma omp barrier // in parallel region
+/// #pragma omp master
+/// {
+/// for (int k = 0; k != ceil(log2(num_iters)); ++k)
+/// for (size cnt = last_iter; cnt >= pow(2, k); --k)
+/// buffer[i] op= buffer[i-pow(2,k)];
+/// <orig_var> = buffer[num_iters-1]; // not combined with 'parallel' only
+/// }
+/// #pragma omp barrier
/// #pragma omp ...
/// for (0..<num_iters>) {
/// red = InclusiveScan ? buffer[i] : buffer[i-1];
/// <scan phase>;
/// }
/// \endcode
+/// For combined directives the original list items are updated by the
+/// encountering thread after the parallel region instead.
static void emitScanBasedDirective(
CodeGenFunction &CGF, const OMPLoopDirective &S,
llvm::function_ref<llvm::Value *(CodeGenFunction &)> NumIteratorsGen,
llvm::function_ref<void(CodeGenFunction &)> FirstGen,
llvm::function_ref<void(CodeGenFunction &)> SecondGen) {
+ OpenMPDirectiveKind EKind = getEffectiveDirectiveKind(S);
+ bool IsStandalone = !isOpenMPParallelDirective(EKind);
llvm::Value *OMPScanNumIterations = CGF.Builder.CreateIntCast(
NumIteratorsGen(CGF), CGF.SizeTy, /*isSigned=*/false);
SmallVector<const Expr *, 4> Privates;
@@ -4451,13 +4601,12 @@ static void emitScanBasedDirective(
CodeGenFunction::OMPLocalDeclMapRAII Scope(CGF);
FirstGen(CGF);
}
- // #pragma omp barrier // in parallel region
auto &&CodeGen = [&S, OMPScanNumIterations, &LHSs, &RHSs, &CopyArrayElems,
- &ReductionOps,
- &Privates](CodeGenFunction &CGF, PrePostActionTy &Action) {
+ &ReductionOps, &Privates, NumIteratorsGen, IsStandalone](
+ CodeGenFunction &CGF, PrePostActionTy &Action) {
Action.Enter(CGF);
// Emit prefix reduction:
- // #pragma omp master // in parallel region
+ // #pragma omp master
// for (int k = 0; k <= ceil(log2(n)); ++k)
llvm::BasicBlock *InputBB = CGF.Builder.GetInsertBlock();
llvm::BasicBlock *LoopBB = CGF.createBasicBlock("omp.outer.log.scan.body");
@@ -4544,20 +4693,22 @@ static void emitScanBasedDirective(
CGF.Builder.CreateCondBr(Cmp, LoopBB, ExitBB);
auto DL1 = ApplyDebugLocation::CreateDefaultArtificial(CGF, S.getEndLoc());
CGF.EmitBlock(ExitBB);
+ if (IsStandalone)
+ emitScanBasedDirectiveFinals(CGF, S, NumIteratorsGen);
};
- OpenMPDirectiveKind EKind = getEffectiveDirectiveKind(S);
- if (isOpenMPParallelDirective(EKind)) {
- CGF.CGM.getOpenMPRuntime().emitMasterRegion(CGF, CodeGen, S.getBeginLoc());
- CGF.CGM.getOpenMPRuntime().emitBarrierCall(
- CGF, S.getBeginLoc(), OMPD_unknown, /*EmitChecks=*/false,
- /*ForceSimpleCall=*/true);
- } else {
- RegionCodeGenTy RCG(CodeGen);
- RCG(CGF);
- }
+ // #pragma omp master
+ CGF.CGM.getOpenMPRuntime().emitMasterRegion(CGF, CodeGen, S.getBeginLoc());
+ // #pragma omp barrier
+ CGF.CGM.getOpenMPRuntime().emitBarrierCall(CGF, S.getBeginLoc(), OMPD_unknown,
+ /*EmitChecks=*/false,
+ /*ForceSimpleCall=*/true);
CGF.OMPFirstScanLoop = false;
- SecondGen(CGF);
+ {
+ // Emit loop with scan phase.
+ CodeGenFunction::OMPLocalDeclMapRAII Scope(CGF);
+ SecondGen(CGF);
+ }
}
static bool emitWorksharingDirective(CodeGenFunction &CGF,
@@ -4565,10 +4716,12 @@ static bool emitWorksharingDirective(CodeGenFunction &CGF,
bool HasCancel) {
bool HasLastprivates;
OpenMPDirectiveKind EKind = getEffectiveDirectiveKind(S);
- if (llvm::any_of(S.getClausesOfKind<OMPReductionClause>(),
- [](const OMPReductionClause *C) {
- return C->getModifier() == OMPC_REDUCTION_inscan;
- })) {
+ if (hasInscanReduction(S)) {
+ // For combined 'parallel for' directives the temp arrays are emitted by the
+ // encountering thread around the parallel region (see
+ // EmitOMPParallelForDirective); otherwise they are allocated once per team
+ // here and released after the barrier at the end of the directive.
+ bool IsStandalone = !isOpenMPParallelDirective(EKind);
const auto &&NumIteratorsGen = [&S](CodeGenFunction &CGF) {
CodeGenFunction::OMPLocalDeclMapRAII Scope(CGF);
OMPLoopScope LoopScope(CGF, S);
@@ -4583,18 +4736,27 @@ static bool emitWorksharingDirective(CodeGenFunction &CGF,
CGF.CGM.getOpenMPRuntime().emitBarrierCall(CGF, S.getBeginLoc(),
OMPD_for);
};
- const auto &&SecondGen = [&S, HasCancel, EKind,
+ const auto &&SecondGen = [&S, HasCancel, EKind, IsStandalone,
&HasLastprivates](CodeGenFunction &CGF) {
CodeGenFunction::OMPCancelStackRAII CancelRegion(CGF, EKind, HasCancel);
HasLastprivates = CGF.EmitOMPWorksharingLoop(S, S.getEnsureUpperBound(),
emitForLoopBounds,
emitDispatchForLoopBounds);
+ // Emit an implicit barrier at the end, required even with 'nowait': the
+ // shared temp arrays are released after it.
+ if (IsStandalone)
+ CGF.CGM.getOpenMPRuntime().emitBarrierCall(CGF, S.getBeginLoc(),
+ OMPD_for);
};
- if (!isOpenMPParallelDirective(EKind))
- emitScanBasedDirectiveDecls(CGF, S, NumIteratorsGen);
- emitScanBasedDirective(CGF, S, NumIteratorsGen, FirstGen, SecondGen);
- if (!isOpenMPParallelDirective(EKind))
- emitScanBasedDirectiveFinals(CGF, S, NumIteratorsGen);
+ if (IsStandalone) {
+ CodeGenFunction::OMPPrivateScope TempsScope(CGF);
+ Address DidIt = emitScanBasedDirectiveSharedDecls(CGF, S, NumIteratorsGen,
+ TempsScope);
+ emitScanBasedDirective(CGF, S, NumIteratorsGen, FirstGen, SecondGen);
+ emitScanBasedDirectiveSharedDeclsFree(CGF, S, DidIt);
+ } else {
+ emitScanBasedDirective(CGF, S, NumIteratorsGen, FirstGen, SecondGen);
+ }
} else {
CodeGenFunction::OMPCancelStackRAII CancelRegion(CGF, EKind, HasCancel);
HasLastprivates = CGF.EmitOMPWorksharingLoop(S, S.getEnsureUpperBound(),
@@ -4706,8 +4868,10 @@ static void emitOMPForDirective(const OMPLoopDirective &S, CodeGenFunction &CGF,
}
if (!UseOMPIRBuilder) {
- // Emit an implicit barrier at the end.
- if (!S.getSingleClause<OMPNowaitClause>() || HasLastprivates)
+ // Emit an implicit barrier at the end (for inscan reductions it is emitted
+ // by emitWorksharingDirective).
+ if ((!S.getSingleClause<OMPNowaitClause>() || HasLastprivates) &&
+ !hasInscanReduction(S))
CGM.getOpenMPRuntime().emitBarrierCall(CGF, S.getBeginLoc(), OMPD_for);
}
// Check for outer lastprivate conditional update.
@@ -4731,8 +4895,10 @@ void CodeGenFunction::EmitOMPForSimdDirective(const OMPForSimdDirective &S) {
CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_simd, CodeGen);
}
- // Emit an implicit barrier at the end.
- if (!S.getSingleClause<OMPNowaitClause>() || HasLastprivates)
+ // Emit an implicit barrier at the end (for inscan reductions it is emitted
+ // by emitWorksharingDirective).
+ if ((!S.getSingleClause<OMPNowaitClause>() || HasLastprivates) &&
+ !hasInscanReduction(S))
CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getBeginLoc(), OMPD_for);
// Check for outer lastprivate conditional update.
checkForLastprivateConditionalUpdate(*this, S);
@@ -5239,10 +5405,7 @@ void CodeGenFunction::EmitOMPParallelForDirective(
OMPLoopScope LoopScope(CGF, S);
return CGF.EmitScalarExpr(S.getNumIterations());
};
- bool IsInscan = llvm::any_of(S.getClausesOfKind<OMPReductionClause>(),
- [](const OMPReductionClause *C) {
- return C->getModifier() == OMPC_REDUCTION_inscan;
- });
+ bool IsInscan = hasInscanReduction(S);
if (IsInscan)
emitScanBasedDirectiveDecls(*this, S, NumIteratorsGen);
auto LPCRegion =
@@ -5273,10 +5436,7 @@ void CodeGenFunction::EmitOMPParallelForSimdDirective(
OMPLoopScope LoopScope(CGF, S);
return CGF.EmitScalarExpr(S.getNumIterations());
};
- bool IsInscan = llvm::any_of(S.getClausesOfKind<OMPReductionClause>(),
- [](const OMPReductionClause *C) {
- return C->getModifier() == OMPC_REDUCTION_inscan;
- });
+ bool IsInscan = hasInscanReduction(S);
if (IsInscan)
emitScanBasedDirectiveDecls(*this, S, NumIteratorsGen);
auto LPCRegion =
diff --git a/clang/test/OpenMP/GH87466.cpp b/clang/test/OpenMP/GH87466.cpp
new file mode 100644
index 00000000000000..11329e57caa6c3
--- /dev/null
+++ b/clang/test/OpenMP/GH87466.cpp
@@ -0,0 +1,168 @@
+// RUN: %clang_cc1 -verify -fopenmp -x c++ -triple x86_64-unknown-linux-gnu -emit-llvm %s -o - | FileCheck %s
+// RUN: %clang_cc1 -verify -fopenmp-simd -x c++ -triple x86_64-unknown-linux-gnu -emit-llvm %s -o - | FileCheck --check-prefix SIMD-ONLY0 %s
+// SIMD-ONLY0-NOT: {{__kmpc|__tgt}}
+// expected-no-diagnostics
+
+// The temp arrays of an inscan reduction on a worksharing loop not combined
+// with 'parallel' must be shared by the team, not allocated by every thread.
+
+int omp_get_num_threads();
+int thread_data[64];
+int scan[64];
+int gsum;
+
+// CHECK-LABEL: define {{.*}}void @_Z21exclusive_in_parallelv(
+// CHECK: call void {{.*}}@__kmpc_fork_call(ptr @{{.+}}, i32 {{[0-9]+}}, ptr [[OUTLINED:@[^,]+]],
+void exclusive_in_parallel() {
+ int curr_scan = 0;
+#pragma omp parallel shared(thread_data, scan, curr_scan)
+ {
+ int n = omp_get_num_threads();
+#pragma omp for reduction(inscan, + : curr_scan)
+ for (int i = 0; i < n; ++i) {
+ scan[i] = curr_scan;
+#pragma omp scan exclusive(curr_scan)
+ curr_scan += thread_data[i];
+ }
+ }
+}
+
+// CHECK: define internal void [[OUTLINED]](
+// CHECK-NOT: alloca i32, i64
+// CHECK: call i32 @__kmpc_single(
+// CHECK: [[BUF:%.+]] = call ptr @__kmpc_aligned_alloc(i32 %{{.+}}, i64 16, i64 %{{.+}}, ptr null)
+// CHECK: store ptr [[BUF]], ptr [[BUF_ADDR:%.+]],
+// CHECK: call void @__kmpc_end_single(
+// CHECK: call void @__kmpc_copyprivate(ptr @{{.+}}, i32 %{{.+}}, i64 8, ptr %{{.+}}, ptr @.omp.copyprivate.copy_func{{.*}}, i32 %{{.+}})
+// CHECK: [[BUF_PTR:%.+]] = load ptr, ptr [[BUF_ADDR]],
+// CHECK: call void @__kmpc_for_static_init_4(
+// CHECK: getelementptr inbounds nuw i32, ptr [[BUF_PTR]], i64 %
+// CHECK: call void @__kmpc_for_static_fini(
+// CHECK: call void @__kmpc_barrier(
+// CHECK: call i32 @__kmpc_master(
+// CHECK: call double @llvm.log2.f64(
+// CHECK: [[LAST:%.+]] = sub nsw i64 %{{.+}}, 1
+// CHECK: [[LAST_REF:%.+]] = getelementptr inbounds nuw i32, ptr [[BUF_PTR]], i64 [[LAST]]
+// CHECK: [[LAST_VAL:%.+]] = load i32, ptr [[LAST_REF]],
+// CHECK: store i32 [[LAST_VAL]], ptr %
+// CHECK: call void @__kmpc_end_master(
+// CHECK: call void @__kmpc_barrier(
+// CHECK: call void @__kmpc_for_static_init_4(
+// CHECK: getelementptr inbounds nuw i32, ptr [[BUF_PTR]], i64 %
+// CHECK: call void @__kmpc_for_static_fini(
+// CHECK: call void @__kmpc_barrier(
+// CHECK: call void @__kmpc_free(i32 %{{.+}}, ptr [[BUF_PTR]], ptr null)
+// CHECK-NOT: call void @__kmpc_barrier(
+// CHECK: ret void
+
+// CHECK-LABEL: define {{.*}}void @_Z21inclusive_in_parallelv(
+// CHECK: call void {{.*}}@__kmpc_fork_call(ptr @{{.+}}, i32 {{[0-9]+}}, ptr [[OUTLINED_INCL:@[^,]+]],
+void inclusive_in_parallel() {
+ int curr_scan = 0;
+#pragma omp parallel
+ {
+ int n = omp_get_num_threads();
+#pragma omp for reduction(inscan, + : curr_scan)
+ for (int i = 0; i < n; ++i) {
+ curr_scan += thread_data[i];
+#pragma omp scan inclusive(curr_scan)
+ scan[i] = curr_scan;
+ }
+ }
+}
+
+// CHECK: define internal void [[OUTLINED_INCL]](
+// CHECK-NOT: alloca i32, i64
+// CHECK: call i32 @__kmpc_single(
+// CHECK: call ptr @__kmpc_aligned_alloc(
+// CHECK: call void @__kmpc_end_single(
+// CHECK: call void @__kmpc_copyprivate(
+// CHECK: call void @__kmpc_for_static_init_4(
+// CHECK: call void @__kmpc_for_static_fini(
+// CHECK: call void @__kmpc_barrier(
+// CHECK: call i32 @__kmpc_master(
+// CHECK: call void @__kmpc_end_master(
+// CHECK: call void @__kmpc_barrier(
+// CHECK: call void @__kmpc_for_static_init_4(
+// CHECK: call void @__kmpc_for_static_fini(
+// CHECK: call void @__kmpc_barrier(
+// CHECK: call void @__kmpc_free(
+// CHECK: ret void
+
+// CHECK-LABEL: define {{.*}}void @_Z8orphanedi(
+// CHECK: [[GTID:%.+]] = call i32 @__kmpc_global_thread_num(
+// CHECK-NOT: alloca i32, i64
+// CHECK: call i32 @__kmpc_single(ptr @{{.+}}, i32 [[GTID]])
+// CHECK: call ptr @__kmpc_aligned_alloc(i32 [[GTID]], i64 16, i64 %{{.+}}, ptr null)
+// CHECK: call void @__kmpc_end_single(ptr @{{.+}}, i32 [[GTID]])
+// CHECK: call void @__kmpc_copyprivate(ptr @{{.+}}, i32 [[GTID]], i64 8, ptr %{{.+}}, ptr @.omp.copyprivate.copy_func{{.*}}, i32 %{{.+}})
+// CHECK: call void @__kmpc_for_static_init_4(
+// CHECK: call void @__kmpc_barrier(ptr @{{.+}}, i32 [[GTID]])
+// CHECK: call i32 @__kmpc_master(ptr @{{.+}}, i32 [[GTID]])
+// CHECK: store i32 %{{.+}}, ptr @gsum,
+// CHECK: call void @__kmpc_end_master(ptr @{{.+}}, i32 [[GTID]])
+// CHECK: call void @__kmpc_barrier(ptr @{{.+}}, i32 [[GTID]])
+// CHECK: call void @__kmpc_for_static_init_4(
+// CHECK: call void @__kmpc_barrier(ptr @{{.+}}, i32 [[GTID]])
+// CHECK: call void @__kmpc_free(i32 [[GTID]], ptr %{{.+}}, ptr null)
+// CHECK: ret void
+void orphaned(int n) {
+#pragma omp for reduction(inscan, + : gsum)
+ for (int i = 0; i < n; ++i) {
+ gsum += thread_data[i];
+#pragma omp scan inclusive(gsum)
+ scan[i] = gsum;
+ }
+}
+
+// CHECK-LABEL: define {{.*}}void @_Z20for_simd_in_parallelv(
+// CHECK: call void {{.*}}@__kmpc_fork_call(ptr @{{.+}}, i32 {{[0-9]+}}, ptr [[OUTLINED_SIMD:@[^,]+]],
+void for_simd_in_parallel() {
+ int curr_scan = 0;
+#pragma omp parallel
+ {
+ int n = omp_get_num_threads();
+#pragma omp for simd reduction(inscan, + : curr_scan)
+ for (int i = 0; i < n; ++i) {
+ scan[i] = curr_scan;
+#pragma omp scan exclusive(curr_scan)
+ curr_scan += thread_data[i];
+ }
+ }
+}
+
+// CHECK: define internal void [[OUTLINED_SIMD]](
+// CHECK-NOT: alloca i32, i64
+// CHECK: call i32 @__kmpc_single(
+// CHECK: call ptr @__kmpc_aligned_alloc(
+// CHECK: call void @__kmpc_copyprivate(
+// CHECK: call i32 @__kmpc_master(
+// CHECK: call void @__kmpc_end_master(
+// CHECK: call void @__kmpc_barrier(
+// CHECK: call void @__kmpc_free(
+// CHECK: ret void
+
+// Combined directive: the buffer is allocated before the parallel region.
+// CHECK-LABEL: define {{.*}}void @_Z8combinedi(
+// CHECK: [[BUF:%.+]] = alloca i32, i64 %
+// CHECK: call void {{.*}}@__kmpc_fork_call(ptr @{{.+}}, i32 {{[0-9]+}}, ptr [[OUTLINED_COMB:@[^,]+]],
+// CHECK: getelementptr inbounds nuw i32, ptr [[BUF]], i64 %
+// CHECK: store i32 %{{.+}}, ptr %curr_scan,
+// CHECK: ret void
+void combined(int n) {
+ int curr_scan = 0;
+#pragma omp parallel for reduction(inscan, + : curr_scan)
+ for (int i = 0; i < n; ++i) {
+ scan[i] = curr_scan;
+#pragma omp scan exclusive(curr_scan)
+ curr_scan += thread_data[i];
+ }
+}
+
+// CHECK: define internal void [[OUTLINED_COMB]](
+// CHECK-NOT: call i32 @__kmpc_single(
+// CHECK-NOT: call ptr @__kmpc_aligned_alloc(
+// CHECK-NOT: call void @__kmpc_copyprivate(
+// CHECK: call i32 @__kmpc_master(
+// CHECK-NOT: call void @__kmpc_free(
+// CHECK: ret void
diff --git a/clang/test/OpenMP/for_scan_codegen.cpp b/clang/test/OpenMP/for_scan_codegen.cpp
index 61e6534db471e7..f000be1ebae83e 100644
--- a/clang/test/OpenMP/for_scan_codegen.cpp
+++ b/clang/test/OpenMP/for_scan_codegen.cpp
@@ -17,14 +17,29 @@ void bar();
void baz(int n) {
static float a[10];
static double b;
- // CHECK: call ptr @llvm.stacksave.p0()
- // CHECK: [[A_BUF_SIZE:%.+]] = mul nuw i64 10, [[NUM_ELEMS:%[^,]+]]
+ // CHECK: [[RES:%.+]] = call i32 @__kmpc_single(ptr @{{.+}}, i32 [[GTID:%.+]])
+ // CHECK: [[IS_SINGLE:%.+]] = icmp ne i32 [[RES]], 0
+ // CHECK: br i1 [[IS_SINGLE]], label %[[SINGLE_THEN:[^,]+]], label %[[SINGLE_END:[^,]+]]
+ // CHECK: [[SINGLE_THEN]]:
// float a_buffer[10][n];
- // CHECK: [[A_BUF:%.+]] = alloca float, i64 [[A_BUF_SIZE]],
+ // CHECK: [[A_BUF_SIZE:%.+]] = mul nuw i64 10, [[NUM_ELEMS:%[^,]+]]
+ // CHECK: [[A_BUF_BYTES:%.+]] = mul nuw i64 [[A_BUF_SIZE]], 4
+ // CHECK: [[A_BUF:%.+]] = call ptr @__kmpc_aligned_alloc(i32 [[GTID]], i64 16, i64 [[A_BUF_BYTES]], ptr null)
+ // CHECK: store ptr [[A_BUF]], ptr [[A_BUF_ADDR:%.+]],
// double b_buffer[10];
- // CHECK: [[B_BUF:%.+]] = alloca double, i64 10,
+ // CHECK: [[B_BUF:%.+]] = call ptr @__kmpc_aligned_alloc(i32 [[GTID]], i64 16, i64 80, ptr null)
+ // CHECK: store ptr [[B_BUF]], ptr [[B_BUF_ADDR:%.+]],
+ // CHECK: store i32 1, ptr [[DID_IT:%.+]],
+ // CHECK: call void @__kmpc_end_single(ptr @{{.+}}, i32 [[GTID]])
+ // CHECK: [[SINGLE_END]]:
+ // CHECK: store ptr [[A_BUF_ADDR]], ptr %
+ // CHECK: store ptr [[B_BUF_ADDR]], ptr %
+ // CHECK: [[DID_IT_VAL:%.+]] = load i32, ptr [[DID_IT]],
+ // CHECK: call void @__kmpc_copyprivate(ptr @{{.+}}, i32 [[GTID]], i64 16, ptr %{{.+}}, ptr @.omp.copyprivate.copy_func{{.*}}, i32 [[DID_IT_VAL]])
+ // CHECK: [[A_BUF:%.+]] = load ptr, ptr [[A_BUF_ADDR]],
+ // CHECK: [[B_BUF:%.+]] = load ptr, ptr [[B_BUF_ADDR]],
#pragma omp for reduction(inscan, +:a[:n], b)
for (int i = 0; i < 10; ++i) {
// CHECK: call void @__kmpc_for_static_init_4(
@@ -58,6 +73,10 @@ void baz(int n) {
// CHECK: call void @__kmpc_barrier(
foo();
#pragma omp scan inclusive(a[:n], b)
+ // CHECK: [[RES:%.+]] = call i32 @__kmpc_master(ptr @{{.+}}, i32 [[GTID]])
+ // CHECK: [[IS_MASTER:%.+]] = icmp ne i32 [[RES]], 0
+ // CHECK: br i1 [[IS_MASTER]], label %[[MASTER_THEN:[^,]+]], label %[[MASTER_END:[^,]+]]
+ // CHECK: [[MASTER_THEN]]:
// CHECK: [[LOG2_10:%.+]] = call double @llvm.log2.f64(double 1.000000e+01)
// CHECK: [[CEIL_LOG2_10:%.+]] = call double @llvm.ceil.f64(double [[LOG2_10]])
// CHECK: [[CEIL_LOG2_10_INT:%.+]] = fptoui double [[CEIL_LOG2_10]] to i32
@@ -114,6 +133,17 @@ void baz(int n) {
// CHECK: [[CMP:%.+]] = icmp ne i32 [[K_NEXT]], [[CEIL_LOG2_10_INT]]
// CHECK: br i1 [[CMP]], label %[[OUTER_BODY]], label %[[OUTER_EXIT:[^,]+]]
// CHECK: [[OUTER_EXIT]]:
+
+ // a = a_buffer[9]; b = b_buffer[9];
+ // CHECK: [[LAST:%.+]] = mul nsw i64 9, [[NUM_ELEMS]]
+ // CHECK: [[LAST_REF:%.+]] = getelementptr inbounds nuw float, ptr [[A_BUF]], i64 [[LAST]]
+ // CHECK: call void @llvm.memcpy.p0.p0.i64(ptr align 16 @_ZZ3baziE1a, ptr align 4 [[LAST_REF]], i64 %{{.+}}, i1 false)
+ // CHECK: [[LAST_REF_B:%.+]] = getelementptr inbounds nuw double, ptr [[B_BUF]], i64 9
+ // CHECK: [[LAST_VAL:%.+]] = load double, ptr [[LAST_REF_B]],
+ // CHECK: store double [[LAST_VAL]], ptr @_ZZ3baziE1b,
+ // CHECK: call void @__kmpc_end_master(ptr @{{.+}}, i32 [[GTID]])
+ // CHECK: [[MASTER_END]]:
+ // CHECK: call void @__kmpc_barrier(
bar();
// CHECK: call void @__kmpc_for_static_init_4(
// CHECK: call ptr @llvm.stacksave.p0()
@@ -150,18 +180,39 @@ void baz(int n) {
// CHECK: [[LOOP_CONTINUE]]:
// CHECK: call void @llvm.stackrestore.p0(ptr %
// CHECK: call void @__kmpc_for_static_fini(
- // CHECK: call void @llvm.stackrestore.p0(ptr
// CHECK: call void @__kmpc_barrier(
+ // CHECK: [[DID_IT_VAL:%.+]] = load i32, ptr [[DID_IT]],
+ // CHECK: [[FREE:%.+]] = icmp ne i32 [[DID_IT_VAL]], 0
+ // CHECK: br i1 [[FREE]], label %[[FREE_THEN:[^,]+]], label %[[FREE_END:[^,]+]]
+ // CHECK: [[FREE_THEN]]:
+ // CHECK: call void @__kmpc_free(i32 [[GTID]], ptr [[A_BUF]], ptr null)
+ // CHECK: call void @__kmpc_free(i32 [[GTID]], ptr [[B_BUF]], ptr null)
+ // CHECK: [[FREE_END]]:
}
- // CHECK: call ptr @llvm.stacksave.p0()
- // CHECK: [[A_BUF_SIZE:%.+]] = mul nuw i64 10, [[NUM_ELEMS:%[^,]+]]
+ // CHECK: [[RES:%.+]] = call i32 @__kmpc_single(ptr @{{.+}}, i32 [[GTID]])
+ // CHECK: [[IS_SINGLE:%.+]] = icmp ne i32 [[RES]], 0
+ // CHECK: br i1 [[IS_SINGLE]], label %[[SINGLE_THEN:[^,]+]], label %[[SINGLE_END:[^,]+]]
+ // CHECK: [[SINGLE_THEN]]:
// float a_buffer[10][n];
- // CHECK: [[A_BUF:%.+]] = alloca float, i64 [[A_BUF_SIZE]],
+ // CHECK: [[A_BUF_SIZE:%.+]] = mul nuw i64 10, [[NUM_ELEMS:%[^,]+]]
+ // CHECK: [[A_BUF_BYTES:%.+]] = mul nuw i64 [[A_BUF_SIZE]], 4
+ // CHECK: [[A_BUF:%.+]] = call ptr @__kmpc_aligned_alloc(i32 [[GTID]], i64 16, i64 [[A_BUF_BYTES]], ptr null)
+ // CHECK: store ptr [[A_BUF]], ptr [[A_BUF_ADDR:%.+]],
// double b_buffer[10];
- // CHECK: [[B_BUF:%.+]] = alloca double, i64 10,
+ // CHECK: [[B_BUF:%.+]] = call ptr @__kmpc_aligned_alloc(i32 [[GTID]], i64 16, i64 80, ptr null)
+ // CHECK: store ptr [[B_BUF]], ptr [[B_BUF_ADDR:%.+]],
+ // CHECK: store i32 1, ptr [[DID_IT:%.+]],
+ // CHECK: call void @__kmpc_end_single(ptr @{{.+}}, i32 [[GTID]])
+ // CHECK: [[SINGLE_END]]:
+ // CHECK: store ptr [[A_BUF_ADDR]], ptr %
+ // CHECK: store ptr [[B_BUF_ADDR]], ptr %
+ // CHECK: [[DID_IT_VAL:%.+]] = load i32, ptr [[DID_IT]],
+ // CHECK: call void @__kmpc_copyprivate(ptr @{{.+}}, i32 [[GTID]], i64 16, ptr %{{.+}}, ptr @.omp.copyprivate.copy_func{{.*}}, i32 [[DID_IT_VAL]])
+ // CHECK: [[A_BUF:%.+]] = load ptr, ptr [[A_BUF_ADDR]],
+ // CHECK: [[B_BUF:%.+]] = load ptr, ptr [[B_BUF_ADDR]],
#pragma omp for reduction(inscan, +:a[:n], b)
for (int i = 0; i < 10; ++i) {
// CHECK: call void @__kmpc_for_static_init_4(
@@ -203,6 +254,10 @@ void baz(int n) {
// CHECK: call void @__kmpc_barrier(
foo();
#pragma omp scan exclusive(a[:n], b)
+ // CHECK: [[RES:%.+]] = call i32 @__kmpc_master(ptr @{{.+}}, i32 [[GTID]])
+ // CHECK: [[IS_MASTER:%.+]] = icmp ne i32 [[RES]], 0
+ // CHECK: br i1 [[IS_MASTER]], label %[[MASTER_THEN:[^,]+]], label %[[MASTER_END:[^,]+]]
+ // CHECK: [[MASTER_THEN]]:
// CHECK: [[LOG2_10:%.+]] = call double @llvm.log2.f64(double 1.000000e+01)
// CHECK: [[CEIL_LOG2_10:%.+]] = call double @llvm.ceil.f64(double [[LOG2_10]])
// CHECK: [[CEIL_LOG2_10_INT:%.+]] = fptoui double [[CEIL_LOG2_10]] to i32
@@ -259,6 +314,17 @@ void baz(int n) {
// CHECK: [[CMP:%.+]] = icmp ne i32 [[K_NEXT]], [[CEIL_LOG2_10_INT]]
// CHECK: br i1 [[CMP]], label %[[OUTER_BODY]], label %[[OUTER_EXIT:[^,]+]]
// CHECK: [[OUTER_EXIT]]:
+
+ // a = a_buffer[9]; b = b_buffer[9];
+ // CHECK: [[LAST:%.+]] = mul nsw i64 9, [[NUM_ELEMS]]
+ // CHECK: [[LAST_REF:%.+]] = getelementptr inbounds nuw float, ptr [[A_BUF]], i64 [[LAST]]
+ // CHECK: call void @llvm.memcpy.p0.p0.i64(ptr align 16 @_ZZ3baziE1a, ptr align 4 [[LAST_REF]], i64 %{{.+}}, i1 false)
+ // CHECK: [[LAST_REF_B:%.+]] = getelementptr inbounds nuw double, ptr [[B_BUF]], i64 9
+ // CHECK: [[LAST_VAL:%.+]] = load double, ptr [[LAST_REF_B]],
+ // CHECK: store double [[LAST_VAL]], ptr @_ZZ3baziE1b,
+ // CHECK: call void @__kmpc_end_master(ptr @{{.+}}, i32 [[GTID]])
+ // CHECK: [[MASTER_END]]:
+ // CHECK: call void @__kmpc_barrier(
bar();
// CHECK: call void @__kmpc_for_static_init_4(
// CHECK: call ptr @llvm.stacksave.p0()
@@ -294,10 +360,30 @@ void baz(int n) {
// CHECK: [[LOOP_CONTINUE]]:
// CHECK: call void @llvm.stackrestore.p0(ptr %
// CHECK: call void @__kmpc_for_static_fini(
- // CHECK: call void @llvm.stackrestore.p0(ptr
// CHECK: call void @__kmpc_barrier(
+ // CHECK: [[DID_IT_VAL:%.+]] = load i32, ptr [[DID_IT]],
+ // CHECK: [[FREE:%.+]] = icmp ne i32 [[DID_IT_VAL]], 0
+ // CHECK: br i1 [[FREE]], label %[[FREE_THEN:[^,]+]], label %[[FREE_END:[^,]+]]
+ // CHECK: [[FREE_THEN]]:
+ // CHECK: call void @__kmpc_free(i32 [[GTID]], ptr [[A_BUF]], ptr null)
+ // CHECK: call void @__kmpc_free(i32 [[GTID]], ptr [[B_BUF]], ptr null)
+ // CHECK: [[FREE_END]]:
}
}
+// CHECK: define internal void @.omp.copyprivate.copy_func(ptr noundef %{{.+}}, ptr noundef %{{.+}})
+// CHECK: [[DST_ELEM:%.+]] = getelementptr inbounds [2 x ptr], ptr %{{.+}}, i64 0, i64 0
+// CHECK: [[DST:%.+]] = load ptr, ptr [[DST_ELEM]],
+// CHECK: [[SRC_ELEM:%.+]] = getelementptr inbounds [2 x ptr], ptr %{{.+}}, i64 0, i64 0
+// CHECK: [[SRC:%.+]] = load ptr, ptr [[SRC_ELEM]],
+// CHECK: [[VAL:%.+]] = load ptr, ptr [[SRC]],
+// CHECK: store ptr [[VAL]], ptr [[DST]],
+// CHECK: [[DST_ELEM:%.+]] = getelementptr inbounds [2 x ptr], ptr %{{.+}}, i64 0, i64 1
+// CHECK: [[DST:%.+]] = load ptr, ptr [[DST_ELEM]],
+// CHECK: [[SRC_ELEM:%.+]] = getelementptr inbounds [2 x ptr], ptr %{{.+}}, i64 0, i64 1
+// CHECK: [[SRC:%.+]] = load ptr, ptr [[SRC_ELEM]],
+// CHECK: [[VAL:%.+]] = load ptr, ptr [[SRC]],
+// CHECK: store ptr [[VAL]], ptr [[DST]],
+
#endif
diff --git a/clang/test/OpenMP/for_simd_scan_codegen.cpp b/clang/test/OpenMP/for_simd_scan_codegen.cpp
index 9ab62f8a682b2e..eea578375e017c 100644
--- a/clang/test/OpenMP/for_simd_scan_codegen.cpp
+++ b/clang/test/OpenMP/for_simd_scan_codegen.cpp
@@ -17,14 +17,29 @@ void bar();
void baz(int n) {
static float a[10];
static double b;
- // CHECK: call ptr @llvm.stacksave.p0()
- // CHECK: [[A_BUF_SIZE:%.+]] = mul nuw i64 10, [[NUM_ELEMS:%[^,]+]]
+ // CHECK: [[RES:%.+]] = call i32 @__kmpc_single(ptr @{{.+}}, i32 [[GTID:%.+]])
+ // CHECK: [[IS_SINGLE:%.+]] = icmp ne i32 [[RES]], 0
+ // CHECK: br i1 [[IS_SINGLE]], label %[[SINGLE_THEN:[^,]+]], label %[[SINGLE_END:[^,]+]]
+ // CHECK: [[SINGLE_THEN]]:
// float a_buffer[10][n];
- // CHECK: [[A_BUF:%.+]] = alloca float, i64 [[A_BUF_SIZE]],
+ // CHECK: [[A_BUF_SIZE:%.+]] = mul nuw i64 10, [[NUM_ELEMS:%[^,]+]]
+ // CHECK: [[A_BUF_BYTES:%.+]] = mul nuw i64 [[A_BUF_SIZE]], 4
+ // CHECK: [[A_BUF:%.+]] = call ptr @__kmpc_aligned_alloc(i32 [[GTID]], i64 16, i64 [[A_BUF_BYTES]], ptr null)
+ // CHECK: store ptr [[A_BUF]], ptr [[A_BUF_ADDR:%.+]],
// double b_buffer[10];
- // CHECK: [[B_BUF:%.+]] = alloca double, i64 10,
+ // CHECK: [[B_BUF:%.+]] = call ptr @__kmpc_aligned_alloc(i32 [[GTID]], i64 16, i64 80, ptr null)
+ // CHECK: store ptr [[B_BUF]], ptr [[B_BUF_ADDR:%.+]],
+ // CHECK: store i32 1, ptr [[DID_IT:%.+]],
+ // CHECK: call void @__kmpc_end_single(ptr @{{.+}}, i32 [[GTID]])
+ // CHECK: [[SINGLE_END]]:
+ // CHECK: store ptr [[A_BUF_ADDR]], ptr %
+ // CHECK: store ptr [[B_BUF_ADDR]], ptr %
+ // CHECK: [[DID_IT_VAL:%.+]] = load i32, ptr [[DID_IT]],
+ // CHECK: call void @__kmpc_copyprivate(ptr @{{.+}}, i32 [[GTID]], i64 16, ptr %{{.+}}, ptr @.omp.copyprivate.copy_func{{.*}}, i32 [[DID_IT_VAL]])
+ // CHECK: [[A_BUF:%.+]] = load ptr, ptr [[A_BUF_ADDR]],
+ // CHECK: [[B_BUF:%.+]] = load ptr, ptr [[B_BUF_ADDR]],
#pragma omp for simd reduction(inscan, +:a[:n], b)
for (int i = 0; i < 10; ++i) {
// CHECK: call void @__kmpc_for_static_init_4(
@@ -58,6 +73,10 @@ void baz(int n) {
// CHECK: call void @__kmpc_barrier(
foo();
#pragma omp scan inclusive(a[:n], b)
+ // CHECK: [[RES:%.+]] = call i32 @__kmpc_master(ptr @{{.+}}, i32 [[GTID]])
+ // CHECK: [[IS_MASTER:%.+]] = icmp ne i32 [[RES]], 0
+ // CHECK: br i1 [[IS_MASTER]], label %[[MASTER_THEN:[^,]+]], label %[[MASTER_END:[^,]+]]
+ // CHECK: [[MASTER_THEN]]:
// CHECK: [[LOG2_10:%.+]] = call double @llvm.log2.f64(double 1.000000e+01)
// CHECK: [[CEIL_LOG2_10:%.+]] = call double @llvm.ceil.f64(double [[LOG2_10]])
// CHECK: [[CEIL_LOG2_10_INT:%.+]] = fptoui double [[CEIL_LOG2_10]] to i32
@@ -114,6 +133,17 @@ void baz(int n) {
// CHECK: [[CMP:%.+]] = icmp ne i32 [[K_NEXT]], [[CEIL_LOG2_10_INT]]
// CHECK: br i1 [[CMP]], label %[[OUTER_BODY]], label %[[OUTER_EXIT:[^,]+]]
// CHECK: [[OUTER_EXIT]]:
+
+ // a = a_buffer[9]; b = b_buffer[9];
+ // CHECK: [[LAST:%.+]] = mul nsw i64 9, [[NUM_ELEMS]]
+ // CHECK: [[LAST_REF:%.+]] = getelementptr inbounds nuw float, ptr [[A_BUF]], i64 [[LAST]]
+ // CHECK: call void @llvm.memcpy.p0.p0.i64(ptr align 16 @_ZZ3baziE1a, ptr align 4 [[LAST_REF]], i64 %{{.+}}, i1 false)
+ // CHECK: [[LAST_REF_B:%.+]] = getelementptr inbounds nuw double, ptr [[B_BUF]], i64 9
+ // CHECK: [[LAST_VAL:%.+]] = load double, ptr [[LAST_REF_B]],
+ // CHECK: store double [[LAST_VAL]], ptr @_ZZ3baziE1b,
+ // CHECK: call void @__kmpc_end_master(ptr @{{.+}}, i32 [[GTID]])
+ // CHECK: [[MASTER_END]]:
+ // CHECK: call void @__kmpc_barrier(
bar();
// CHECK: call void @__kmpc_for_static_init_4(
// CHECK: call ptr @llvm.stacksave.p0()
@@ -150,18 +180,39 @@ void baz(int n) {
// CHECK: [[LOOP_CONTINUE]]:
// CHECK: call void @llvm.stackrestore.p0(ptr %
// CHECK: call void @__kmpc_for_static_fini(
- // CHECK: call void @llvm.stackrestore.p0(ptr
// CHECK: call void @__kmpc_barrier(
+ // CHECK: [[DID_IT_VAL:%.+]] = load i32, ptr [[DID_IT]],
+ // CHECK: [[FREE:%.+]] = icmp ne i32 [[DID_IT_VAL]], 0
+ // CHECK: br i1 [[FREE]], label %[[FREE_THEN:[^,]+]], label %[[FREE_END:[^,]+]]
+ // CHECK: [[FREE_THEN]]:
+ // CHECK: call void @__kmpc_free(i32 [[GTID]], ptr [[A_BUF]], ptr null)
+ // CHECK: call void @__kmpc_free(i32 [[GTID]], ptr [[B_BUF]], ptr null)
+ // CHECK: [[FREE_END]]:
}
- // CHECK: call ptr @llvm.stacksave.p0()
- // CHECK: [[A_BUF_SIZE:%.+]] = mul nuw i64 10, [[NUM_ELEMS:%[^,]+]]
+ // CHECK: [[RES:%.+]] = call i32 @__kmpc_single(ptr @{{.+}}, i32 [[GTID]])
+ // CHECK: [[IS_SINGLE:%.+]] = icmp ne i32 [[RES]], 0
+ // CHECK: br i1 [[IS_SINGLE]], label %[[SINGLE_THEN:[^,]+]], label %[[SINGLE_END:[^,]+]]
+ // CHECK: [[SINGLE_THEN]]:
// float a_buffer[10][n];
- // CHECK: [[A_BUF:%.+]] = alloca float, i64 [[A_BUF_SIZE]],
+ // CHECK: [[A_BUF_SIZE:%.+]] = mul nuw i64 10, [[NUM_ELEMS:%[^,]+]]
+ // CHECK: [[A_BUF_BYTES:%.+]] = mul nuw i64 [[A_BUF_SIZE]], 4
+ // CHECK: [[A_BUF:%.+]] = call ptr @__kmpc_aligned_alloc(i32 [[GTID]], i64 16, i64 [[A_BUF_BYTES]], ptr null)
+ // CHECK: store ptr [[A_BUF]], ptr [[A_BUF_ADDR:%.+]],
// double b_buffer[10];
- // CHECK: [[B_BUF:%.+]] = alloca double, i64 10,
+ // CHECK: [[B_BUF:%.+]] = call ptr @__kmpc_aligned_alloc(i32 [[GTID]], i64 16, i64 80, ptr null)
+ // CHECK: store ptr [[B_BUF]], ptr [[B_BUF_ADDR:%.+]],
+ // CHECK: store i32 1, ptr [[DID_IT:%.+]],
+ // CHECK: call void @__kmpc_end_single(ptr @{{.+}}, i32 [[GTID]])
+ // CHECK: [[SINGLE_END]]:
+ // CHECK: store ptr [[A_BUF_ADDR]], ptr %
+ // CHECK: store ptr [[B_BUF_ADDR]], ptr %
+ // CHECK: [[DID_IT_VAL:%.+]] = load i32, ptr [[DID_IT]],
+ // CHECK: call void @__kmpc_copyprivate(ptr @{{.+}}, i32 [[GTID]], i64 16, ptr %{{.+}}, ptr @.omp.copyprivate.copy_func{{.*}}, i32 [[DID_IT_VAL]])
+ // CHECK: [[A_BUF:%.+]] = load ptr, ptr [[A_BUF_ADDR]],
+ // CHECK: [[B_BUF:%.+]] = load ptr, ptr [[B_BUF_ADDR]],
#pragma omp for simd reduction(inscan, +:a[:n], b)
for (int i = 0; i < 10; ++i) {
// CHECK: call void @__kmpc_for_static_init_4(
@@ -203,6 +254,10 @@ void baz(int n) {
// CHECK: call void @__kmpc_barrier(
foo();
#pragma omp scan exclusive(a[:n], b)
+ // CHECK: [[RES:%.+]] = call i32 @__kmpc_master(ptr @{{.+}}, i32 [[GTID]])
+ // CHECK: [[IS_MASTER:%.+]] = icmp ne i32 [[RES]], 0
+ // CHECK: br i1 [[IS_MASTER]], label %[[MASTER_THEN:[^,]+]], label %[[MASTER_END:[^,]+]]
+ // CHECK: [[MASTER_THEN]]:
// CHECK: [[LOG2_10:%.+]] = call double @llvm.log2.f64(double 1.000000e+01)
// CHECK: [[CEIL_LOG2_10:%.+]] = call double @llvm.ceil.f64(double [[LOG2_10]])
// CHECK: [[CEIL_LOG2_10_INT:%.+]] = fptoui double [[CEIL_LOG2_10]] to i32
@@ -259,6 +314,17 @@ void baz(int n) {
// CHECK: [[CMP:%.+]] = icmp ne i32 [[K_NEXT]], [[CEIL_LOG2_10_INT]]
// CHECK: br i1 [[CMP]], label %[[OUTER_BODY]], label %[[OUTER_EXIT:[^,]+]]
// CHECK: [[OUTER_EXIT]]:
+
+ // a = a_buffer[9]; b = b_buffer[9];
+ // CHECK: [[LAST:%.+]] = mul nsw i64 9, [[NUM_ELEMS]]
+ // CHECK: [[LAST_REF:%.+]] = getelementptr inbounds nuw float, ptr [[A_BUF]], i64 [[LAST]]
+ // CHECK: call void @llvm.memcpy.p0.p0.i64(ptr align 16 @_ZZ3baziE1a, ptr align 4 [[LAST_REF]], i64 %{{.+}}, i1 false)
+ // CHECK: [[LAST_REF_B:%.+]] = getelementptr inbounds nuw double, ptr [[B_BUF]], i64 9
+ // CHECK: [[LAST_VAL:%.+]] = load double, ptr [[LAST_REF_B]],
+ // CHECK: store double [[LAST_VAL]], ptr @_ZZ3baziE1b,
+ // CHECK: call void @__kmpc_end_master(ptr @{{.+}}, i32 [[GTID]])
+ // CHECK: [[MASTER_END]]:
+ // CHECK: call void @__kmpc_barrier(
bar();
// CHECK: call void @__kmpc_for_static_init_4(
// CHECK: call ptr @llvm.stacksave.p0()
@@ -294,11 +360,31 @@ void baz(int n) {
// CHECK: [[LOOP_CONTINUE]]:
// CHECK: call void @llvm.stackrestore.p0(ptr %
// CHECK: call void @__kmpc_for_static_fini(
- // CHECK: call void @llvm.stackrestore.p0(ptr
// CHECK: call void @__kmpc_barrier(
+ // CHECK: [[DID_IT_VAL:%.+]] = load i32, ptr [[DID_IT]],
+ // CHECK: [[FREE:%.+]] = icmp ne i32 [[DID_IT_VAL]], 0
+ // CHECK: br i1 [[FREE]], label %[[FREE_THEN:[^,]+]], label %[[FREE_END:[^,]+]]
+ // CHECK: [[FREE_THEN]]:
+ // CHECK: call void @__kmpc_free(i32 [[GTID]], ptr [[A_BUF]], ptr null)
+ // CHECK: call void @__kmpc_free(i32 [[GTID]], ptr [[B_BUF]], ptr null)
+ // CHECK: [[FREE_END]]:
}
}
+// CHECK: define internal void @.omp.copyprivate.copy_func(ptr noundef %{{.+}}, ptr noundef %{{.+}})
+// CHECK: [[DST_ELEM:%.+]] = getelementptr inbounds [2 x ptr], ptr %{{.+}}, i64 0, i64 0
+// CHECK: [[DST:%.+]] = load ptr, ptr [[DST_ELEM]],
+// CHECK: [[SRC_ELEM:%.+]] = getelementptr inbounds [2 x ptr], ptr %{{.+}}, i64 0, i64 0
+// CHECK: [[SRC:%.+]] = load ptr, ptr [[SRC_ELEM]],
+// CHECK: [[VAL:%.+]] = load ptr, ptr [[SRC]],
+// CHECK: store ptr [[VAL]], ptr [[DST]],
+// CHECK: [[DST_ELEM:%.+]] = getelementptr inbounds [2 x ptr], ptr %{{.+}}, i64 0, i64 1
+// CHECK: [[DST:%.+]] = load ptr, ptr [[DST_ELEM]],
+// CHECK: [[SRC_ELEM:%.+]] = getelementptr inbounds [2 x ptr], ptr %{{.+}}, i64 0, i64 1
+// CHECK: [[SRC:%.+]] = load ptr, ptr [[SRC_ELEM]],
+// CHECK: [[VAL:%.+]] = load ptr, ptr [[SRC]],
+// CHECK: store ptr [[VAL]], ptr [[DST]],
+
#endif
// CHECK: !{!"llvm.loop.vectorize.enable"}
More information about the cfe-commits
mailing list