Skip to content

Commit a0d29ab

Browse files
author
Chandra Ghale
committed
address comments,support all types
1 parent 262a861 commit a0d29ab

File tree

2 files changed

+40
-25
lines changed

2 files changed

+40
-25
lines changed

clang/lib/CodeGen/CGOpenMPRuntime.cpp

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4935,13 +4935,34 @@ void CGOpenMPRuntime::emitPrivateReduction(
49354935
if (const auto *DRE = dyn_cast<DeclRefExpr>(Privates)) {
49364936
if (const auto *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
49374937
const Expr *InitExpr = VD->getInit();
4938-
if (InitExpr && !PrivateType->isAggregateType() &&
4939-
!PrivateType->isAnyComplexType()) {
4938+
if (InitExpr) {
49404939
Expr::EvalResult Result;
49414940
if (InitExpr->EvaluateAsRValue(Result, CGF.getContext())) {
49424941
APValue &InitValue = Result.Val;
49434942
if (InitValue.isInt())
49444943
InitVal = llvm::ConstantInt::get(LLVMType, InitValue.getInt());
4944+
else if (InitValue.isFloat())
4945+
InitVal = llvm::ConstantFP::get(LLVMType, InitValue.getFloat());
4946+
else if (InitValue.isComplexInt()) {
4947+
// For complex int: create struct { real, imag }
4948+
llvm::Constant *Real = llvm::ConstantInt::get(
4949+
cast<llvm::StructType>(LLVMType)->getElementType(0),
4950+
InitValue.getComplexIntReal());
4951+
llvm::Constant *Imag = llvm::ConstantInt::get(
4952+
cast<llvm::StructType>(LLVMType)->getElementType(1),
4953+
InitValue.getComplexIntImag());
4954+
InitVal = llvm::ConstantStruct::get(
4955+
cast<llvm::StructType>(LLVMType), {Real, Imag});
4956+
} else if (InitValue.isComplexFloat()) {
4957+
llvm::Constant *Real = llvm::ConstantFP::get(
4958+
cast<llvm::StructType>(LLVMType)->getElementType(0),
4959+
InitValue.getComplexFloatReal());
4960+
llvm::Constant *Imag = llvm::ConstantFP::get(
4961+
cast<llvm::StructType>(LLVMType)->getElementType(1),
4962+
InitValue.getComplexFloatImag());
4963+
InitVal = llvm::ConstantStruct::get(
4964+
cast<llvm::StructType>(LLVMType), {Real, Imag});
4965+
}
49454966
}
49464967
}
49474968
}
@@ -4950,11 +4971,10 @@ void CGOpenMPRuntime::emitPrivateReduction(
49504971
InitVal = llvm::Constant::getNullValue(LLVMType);
49514972
}
49524973
std::string ReductionVarNameStr;
4953-
if (const auto *DRE = dyn_cast<DeclRefExpr>(Privates->IgnoreParenCasts())) {
4974+
if (const auto *DRE = dyn_cast<DeclRefExpr>(Privates->IgnoreParenCasts()))
49544975
ReductionVarNameStr = DRE->getDecl()->getNameAsString();
4955-
} else {
4976+
else
49564977
ReductionVarNameStr = "unnamed_priv_var";
4957-
}
49584978

49594979
// Create an internal shared variable
49604980
std::string SharedName =
@@ -5027,18 +5047,11 @@ void CGOpenMPRuntime::emitPrivateReduction(
50275047
}
50285048
if (const auto *DRE = dyn_cast<DeclRefExpr>(Privates)) {
50295049
if (const auto *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
5030-
const Expr *InitExpr = VD->getInit();
5031-
if (InitExpr && (PrivateType->isAggregateType() ||
5032-
PrivateType->isAnyComplexType())) {
5050+
if (const Expr *InitExpr = VD->getInit()) {
50335051
CGF.EmitAnyExprToMem(InitExpr, SharedResult,
50345052
PrivateType.getQualifiers(), true);
50355053
return;
50365054
}
5037-
if (!InitVal->isNullValue()) {
5038-
CGF.EmitStoreOfScalar(InitVal,
5039-
CGF.MakeAddrLValue(SharedResult, PrivateType));
5040-
return;
5041-
}
50425055
}
50435056
}
50445057
CGF.EmitNullInitialization(SharedResult, PrivateType);
@@ -5086,9 +5099,8 @@ void CGOpenMPRuntime::emitPrivateReduction(
50865099
}
50875100
};
50885101
EmitCriticalReduction(ReductionGen);
5089-
}
5090-
// Handle built-in reduction operations.
5091-
else {
5102+
} else {
5103+
// Handle built-in reduction operations.
50925104
const Expr *ReductionClauseExpr = ReductionOp->IgnoreParenCasts();
50935105
if (const auto *Cleanup = dyn_cast<ExprWithCleanups>(ReductionClauseExpr))
50945106
ReductionClauseExpr = Cleanup->getSubExpr()->IgnoreParenCasts();
@@ -5106,10 +5118,6 @@ void CGOpenMPRuntime::emitPrivateReduction(
51065118
if (!AssignRHS)
51075119
return;
51085120

5109-
const Expr *CombinerExpr = AssignRHS->IgnoreParenImpCasts();
5110-
if (const auto *MTE = dyn_cast<MaterializeTemporaryExpr>(CombinerExpr))
5111-
CombinerExpr = MTE->getSubExpr()->IgnoreParenImpCasts();
5112-
51135121
auto ReductionGen = [&](CodeGenFunction &CGF, PrePostActionTy &Action) {
51145122
Action.Enter(CGF);
51155123
const auto *OmpOutDRE =
@@ -5414,7 +5422,7 @@ void CGOpenMPRuntime::emitReduction(CodeGenFunction &CGF, SourceLocation Loc,
54145422
} else {
54155423
// Emit as a critical region.
54165424
auto &&CritRedGen = [E, Loc](CodeGenFunction &CGF, const Expr *,
5417-
const Expr *, const Expr *) {
5425+
const Expr *, const Expr *) {
54185426
CGOpenMPRuntime &RT = CGF.CGM.getOpenMPRuntime();
54195427
std::string Name = RT.getName({"atomic_reduction"});
54205428
RT.emitCriticalRegion(
@@ -5467,6 +5475,14 @@ void CGOpenMPRuntime::emitReduction(CodeGenFunction &CGF, SourceLocation Loc,
54675475
if (LHSExprs.size() != Privates.size() ||
54685476
LHSExprs.size() != ReductionOps.size())
54695477
return;
5478+
assert(!LHSExprs.empty() && "PrivateVarReduction: LHSExprs is empty");
5479+
assert(!Privates.empty() && "PrivateVarReduction: Privates is empty");
5480+
assert(!ReductionOps.empty() &&
5481+
"PrivateVarReduction: ReductionOps is empty");
5482+
assert(LHSExprs.size() == Privates.size() &&
5483+
"PrivateVarReduction: Privates size mismatch");
5484+
assert(LHSExprs.size() == ReductionOps.size() &&
5485+
"PrivateVarReduction: ReductionOps size mismatch");
54705486
for (unsigned I :
54715487
llvm::seq<unsigned>(std::min(ReductionOps.size(), LHSExprs.size()))) {
54725488
emitPrivateReduction(CGF, Loc, Privates[I], LHSExprs[I], RHSExprs[I],

clang/test/OpenMP/for_private_reduction_codegen.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ int main(void) {
464464
// CHECK-NEXT: [[TMP28:%.*]] = icmp eq i32 [[TMP0]], 0
465465
// CHECK-NEXT: br i1 [[TMP28]], label %[[INIT:.*]], label %[[INIT_END:.*]]
466466
// CHECK: [[INIT]]:
467-
// CHECK-NEXT: call void @llvm.memset.p0.i64(ptr align 4 @.omp.reduction..internal_pivate_.sum_v, i8 0, i64 4, i1 false)
467+
// CHECK-NEXT: store i32 0, ptr @.omp.reduction..internal_pivate_.sum_v, align 4
468468
// CHECK-NEXT: br label %[[INIT_END]]
469469
// CHECK: [[INIT_END]]:
470470
// CHECK-NEXT: call void @__kmpc_barrier(ptr @[[GLOB2]], i32 [[TMP0]])
@@ -668,7 +668,7 @@ int main(void) {
668668
// CHECK-NEXT: [[TMP47:%.*]] = icmp eq i32 [[TMP0]], 0
669669
// CHECK-NEXT: br i1 [[TMP47]], label %[[INIT:.*]], label %[[INIT_END:.*]]
670670
// CHECK: [[INIT]]:
671-
// CHECK-NEXT: call void @llvm.memset.p0.i64(ptr align 4 @.omp.reduction..internal_pivate_.sum_v.1, i8 0, i64 4, i1 false)
671+
// CHECK-NEXT: store i32 0, ptr @.omp.reduction..internal_pivate_.sum_v.1, align 4
672672
// CHECK-NEXT: br label %[[INIT_END]]
673673
// CHECK: [[INIT_END]]:
674674
// CHECK-NEXT: call void @__kmpc_barrier(ptr @[[GLOB2]], i32 [[TMP0]])
@@ -734,7 +734,7 @@ int main(void) {
734734
//
735735
//
736736
// CHECK-LABEL: define dso_local noundef i32 @main(
737-
// CHECK-SAME: ) #[[ATTR8:[0-9]+]] {
737+
// CHECK-SAME: ) #[[ATTR7:[0-9]+]] {
738738
// CHECK-NEXT: [[ENTRY:.*:]]
739739
// CHECK-NEXT: [[RETVAL:%.*]] = alloca i32, align 4
740740
// CHECK-NEXT: [[V:%.*]] = alloca [10 x i32], align 16
@@ -785,4 +785,3 @@ int main(void) {
785785
// CHECK-NEXT: [[ARRAYDECAY:%.*]] = getelementptr inbounds [10 x i32], ptr [[TMP0]], i64 0, i64 0
786786
// CHECK-NEXT: call void @_Z6do_rediPiRi(i32 noundef 10, ptr noundef [[ARRAYDECAY]], ptr noundef nonnull align 4 dereferenceable(4) [[S_V]])
787787
// CHECK-NEXT: ret void
788-

0 commit comments

Comments
 (0)