Skip to content

Commit 3072fbc

Browse files
committed
new changes
1 parent 0987b64 commit 3072fbc

File tree

4 files changed

+54
-43
lines changed

4 files changed

+54
-43
lines changed

llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1602,7 +1602,8 @@ class OpenMPIRBuilder {
16021602

16031603
/// Creates the buffer needed for scan reduction.
16041604
/// \param ScanVars Scan Variables.
1605-
void emitScanBasedDirectiveDeclsIR(ArrayRef<llvm::Value *> ScanVars);
1605+
void emitScanBasedDirectiveDeclsIR(ArrayRef<llvm::Value *> ScanVars,
1606+
ArrayRef<llvm::Type *> ScanVarsType);
16061607

16071608
/// Copies the result back to the reduction variable.
16081609
/// \param ReductionInfos Array type containing the ReductionOps.
@@ -2718,6 +2719,7 @@ class OpenMPIRBuilder {
27182719
InsertPointOrErrorTy createScan(const LocationDescription &Loc,
27192720
InsertPointTy AllocaIP,
27202721
ArrayRef<llvm::Value *> ScanVars,
2722+
ArrayRef<llvm::Type *> ScanVarsType,
27212723
bool IsInclusive);
27222724
/// Generator for '#omp critical'
27232725
///

llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4002,10 +4002,11 @@ OpenMPIRBuilder::emitNoUnwindRuntimeCall(llvm::FunctionCallee Callee,
40024002
// inclusive scans now.
40034003
OpenMPIRBuilder::InsertPointOrErrorTy OpenMPIRBuilder::createScan(
40044004
const LocationDescription &Loc, InsertPointTy AllocaIP,
4005-
ArrayRef<llvm::Value *> ScanVars, bool IsInclusive) {
4005+
ArrayRef<llvm::Value *> ScanVars, ArrayRef<llvm::Type *> ScanVarsType,
4006+
bool IsInclusive) {
40064007
if (ScanInfo.OMPFirstScanLoop) {
40074008
Builder.restoreIP(AllocaIP);
4008-
emitScanBasedDirectiveDeclsIR(ScanVars);
4009+
emitScanBasedDirectiveDeclsIR(ScanVars, ScanVarsType);
40094010
}
40104011
if (!updateToLocation(Loc))
40114012
return Loc.IP;
@@ -4014,11 +4015,11 @@ OpenMPIRBuilder::InsertPointOrErrorTy OpenMPIRBuilder::createScan(
40144015

40154016
if (ScanInfo.OMPFirstScanLoop) {
40164017
// Emit buffer[i] = red; at the end of the input phase.
4017-
for (Value *ScanVar : ScanVars) {
4018-
Value *Buff = ScanInfo.ReductionVarToScanBuffs[ScanVar];
4019-
Type *DestTy = Builder.getInt32Ty(); // ScanVars[i]->getType();
4018+
for (int i = 0; i < ScanVars.size(); i++) {
4019+
Value *Buff = ScanInfo.ReductionVarToScanBuffs[ScanVars[i]];
4020+
Type *DestTy = ScanVarsType[i];
40204021
Value *Val = Builder.CreateInBoundsGEP(DestTy, Buff, IV, "arrayOffset");
4021-
Value *Src = Builder.CreateLoad(DestTy, ScanVar);
4022+
Value *Src = Builder.CreateLoad(DestTy, ScanVars[i]);
40224023
Value *Dest = Builder.CreatePointerBitCastOrAddrSpaceCast(
40234024
Val, DestTy->getPointerTo(defaultAS));
40244025

@@ -4031,25 +4032,25 @@ OpenMPIRBuilder::InsertPointOrErrorTy OpenMPIRBuilder::createScan(
40314032
// Initialize the private reduction variable to 0 in each iteration.
40324033
// It is used to copy intial values to scan buffer.
40334034
ConstantInt *Zero = ConstantInt::get(Builder.getInt32Ty(), 0);
4034-
for (Value *ScanVar : ScanVars) {
4035-
Type *DestTy = Builder.getInt32Ty(); // ScanVars[i]->getType();
4035+
for (int i = 0; i < ScanVars.size(); i++) {
4036+
Type *DestTy = ScanVarsType[i];
40364037
Value *Dest = Builder.CreatePointerBitCastOrAddrSpaceCast(
4037-
ScanVar, DestTy->getPointerTo(defaultAS));
4038+
ScanVars[i], DestTy->getPointerTo(defaultAS));
40384039
Builder.CreateStore(Zero, Dest);
40394040
}
40404041

40414042
if (!ScanInfo.OMPFirstScanLoop) {
40424043
IV = ScanInfo.IV;
40434044
// Emit red = buffer[i]; at the entrance to the scan phase.
40444045
// TODO: if exclusive scan, the red = buffer[i-1] needs to be updated.
4045-
for (Value *ScanVar : ScanVars) {
4046-
Value *Buff = ScanInfo.ReductionVarToScanBuffs[ScanVar];
4047-
Type *DestTy = Builder.getInt32Ty(); // ScanVars[i]->getType();
4046+
for (int i = 0; i < ScanVars.size(); i++) {
4047+
Value *Buff = ScanInfo.ReductionVarToScanBuffs[ScanVars[i]];
4048+
Type *DestTy = ScanVarsType[i];
40484049
Value *SrcPtr =
40494050
Builder.CreateInBoundsGEP(DestTy, Buff, IV, "arrayOffset");
40504051
Value *Src = Builder.CreateLoad(DestTy, SrcPtr);
40514052
Value *Dest = Builder.CreatePointerBitCastOrAddrSpaceCast(
4052-
ScanVar, DestTy->getPointerTo(defaultAS));
4053+
ScanVars[i], DestTy->getPointerTo(defaultAS));
40534054

40544055
Builder.CreateStore(Src, Dest);
40554056
}
@@ -4070,29 +4071,26 @@ OpenMPIRBuilder::InsertPointOrErrorTy OpenMPIRBuilder::createScan(
40704071
}
40714072

40724073
void OpenMPIRBuilder::emitScanBasedDirectiveDeclsIR(
4073-
ArrayRef<Value *> ScanVars) {
4074+
ArrayRef<Value *> ScanVars, ArrayRef<Type *> ScanVarsType) {
40744075

40754076
Value *AllocSpan = Builder.CreateAdd(ScanInfo.Span, Builder.getInt32(1));
4076-
for (Value *ScanVar : ScanVars) {
4077-
llvm::Value *Buff =
4078-
Builder.CreateAlloca(Builder.getInt32Ty(), AllocSpan, "vla");
4079-
ScanInfo.ReductionVarToScanBuffs[ScanVar] = Buff;
4077+
for (int i = 0; i < ScanVars.size(); i++) {
4078+
llvm::Value *Buff = Builder.CreateAlloca(ScanVarsType[i], AllocSpan, "vla");
4079+
ScanInfo.ReductionVarToScanBuffs[ScanVars[i]] = Buff;
40804080
}
40814081
}
40824082

40834083
void OpenMPIRBuilder::emitScanBasedDirectiveFinalsIR(
40844084
SmallVector<ReductionInfo> ReductionInfos) {
4085-
llvm::Value *OMPLast = Builder.CreateNSWAdd(
4086-
ScanInfo.Span,
4087-
llvm::ConstantInt::get(ScanInfo.Span->getType(), 1, /*isSigned=*/false));
40884085
unsigned int DefaultAS = M.getDataLayout().getProgramAddressSpace();
40894086
for (ReductionInfo RedInfo : ReductionInfos) {
40904087
Value *PrivateVar = RedInfo.PrivateVariable;
40914088
Value *OrigVar = RedInfo.Variable;
40924089
Value *Buff = ScanInfo.ReductionVarToScanBuffs[PrivateVar];
40934090

40944091
Type *SrcTy = RedInfo.ElementType;
4095-
Value *Val = Builder.CreateInBoundsGEP(SrcTy, Buff, OMPLast, "arrayOffset");
4092+
Value *Val =
4093+
Builder.CreateInBoundsGEP(SrcTy, Buff, ScanInfo.Span, "arrayOffset");
40964094
Value *Src = Builder.CreateLoad(SrcTy, Val);
40974095
Value *Dest = Builder.CreatePointerBitCastOrAddrSpaceCast(
40984096
OrigVar, SrcTy->getPointerTo(DefaultAS));
@@ -4120,7 +4118,7 @@ OpenMPIRBuilder::InsertPointOrErrorTy OpenMPIRBuilder::emitScanReduction(
41204118
(llvm::Intrinsic::ID)llvm::Intrinsic::log2, Builder.getDoubleTy());
41214119
llvm::BasicBlock *InputBB = Builder.GetInsertBlock();
41224120
ConstantInt *One = ConstantInt::get(Builder.getInt32Ty(), 1);
4123-
llvm::Value *span = Builder.CreateAdd(spanDiff, One);
4121+
llvm::Value *span = ScanInfo.Span; // Builder.CreateAdd(spanDiff, One);
41244122
llvm::Value *Arg = Builder.CreateUIToFP(span, Builder.getDoubleTy());
41254123
llvm::Value *LogVal = emitNoUnwindRuntimeCall(F, Arg, "");
41264124
F = llvm::Intrinsic::getOrInsertDeclaration(
@@ -5456,7 +5454,7 @@ OpenMPIRBuilder::tileLoops(DebugLoc DL, ArrayRef<CanonicalLoopInfo *> Loops,
54565454
// TODO: It would be sufficient to only sink them into body of the
54575455
// corresponding tile loop.
54585456
SmallVector<std::pair<BasicBlock *, BasicBlock *>, 4> InbetweenCode;
5459-
for (int i = 0; i < NumLoops - 1; ++i) {
5457+
for (size_t i = 0; i < NumLoops - 1; ++i) {
54605458
CanonicalLoopInfo *Surrounding = Loops[i];
54615459
CanonicalLoopInfo *Nested = Loops[i + 1];
54625460

@@ -5469,7 +5467,7 @@ OpenMPIRBuilder::tileLoops(DebugLoc DL, ArrayRef<CanonicalLoopInfo *> Loops,
54695467
Builder.SetCurrentDebugLocation(DL);
54705468
Builder.restoreIP(OutermostLoop->getPreheaderIP());
54715469
SmallVector<Value *, 4> FloorCount, FloorRems;
5472-
for (int i = 0; i < NumLoops; ++i) {
5470+
for (size_t i = 0; i < NumLoops; ++i) {
54735471
Value *TileSize = TileSizes[i];
54745472
Value *OrigTripCount = OrigTripCounts[i];
54755473
Type *IVType = OrigTripCount->getType();

llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1440,12 +1440,14 @@ TEST_F(OpenMPIRBuilderTest, CanonicalLoopSimple) {
14401440

14411441
EXPECT_EQ(&Loop->getAfter()->front(), RetInst);
14421442
}
1443-
void createScan(llvm::Value *scanVar, OpenMPIRBuilder &OMPBuilder,
1444-
IRBuilder<> &Builder, OpenMPIRBuilder::LocationDescription Loc,
1443+
void createScan(llvm::Value *scanVar, llvm::Type *scanType,
1444+
OpenMPIRBuilder &OMPBuilder, IRBuilder<> &Builder,
1445+
OpenMPIRBuilder::LocationDescription Loc,
14451446
OpenMPIRBuilder::InsertPointTy &allocaIP) {
14461447
using InsertPointTy = OpenMPIRBuilder::InsertPointTy;
1447-
ASSERT_EXPECTED_INIT(InsertPointTy, retIp,
1448-
OMPBuilder.createScan(Loc, allocaIP, {scanVar}, true));
1448+
ASSERT_EXPECTED_INIT(
1449+
InsertPointTy, retIp,
1450+
OMPBuilder.createScan(Loc, allocaIP, {scanVar}, {scanType}, true));
14491451
Builder.restoreIP(retIp);
14501452
}
14511453

@@ -5363,7 +5365,8 @@ TEST_F(OpenMPIRBuilderTest, ScanReduction) {
53635365
auto LoopBodyGenCB = [&](InsertPointTy CodeGenIP, llvm::Value *LC) {
53645366
NumBodiesGenerated += 1;
53655367
Builder.restoreIP(CodeGenIP);
5366-
createScan(scanVar, OMPBuilder, Builder, Loc, allocaIP);
5368+
createScan(scanVar, Builder.getFloatTy(), OMPBuilder, Builder, Loc,
5369+
allocaIP);
53675370
return Error::success();
53685371
};
53695372
SmallVector<CanonicalLoopInfo *> Loops;

mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747

4848
using namespace mlir;
4949

50+
llvm::SmallDenseMap<llvm::Value *, llvm::Type *> ReductionVarToType;
5051
namespace {
5152
static llvm::omp::ScheduleKind
5253
convertToScheduleKind(std::optional<omp::ClauseScheduleKind> schedKind) {
@@ -1140,6 +1141,11 @@ initReductionVars(OP op, ArrayRef<BlockArgument> reductionArgs,
11401141
// variables. Although this could be done after allocas, we don't want to mess
11411142
// up with the alloca insertion point.
11421143
for (unsigned i = 0; i < op.getNumReductionVars(); ++i) {
1144+
1145+
llvm::Type *reductionType =
1146+
moduleTranslation.convertType(reductionDecls[i].getType());
1147+
ReductionVarToType[privateReductionVariables[i]] = reductionType;
1148+
11431149
SmallVector<llvm::Value *, 1> phis;
11441150

11451151
// map block argument to initializer region
@@ -1213,9 +1219,11 @@ static void collectReductionInfo(
12131219
atomicGen = owningAtomicReductionGens[i];
12141220
llvm::Value *variable =
12151221
moduleTranslation.lookupValue(loop.getReductionVars()[i]);
1222+
llvm::Type *reductionType =
1223+
moduleTranslation.convertType(reductionDecls[i].getType());
1224+
ReductionVarToType[privateReductionVariables[i]] = reductionType;
12161225
reductionInfos.push_back(
1217-
{moduleTranslation.convertType(reductionDecls[i].getType()), variable,
1218-
privateReductionVariables[i],
1226+
{reductionType, variable, privateReductionVariables[i],
12191227
/*EvaluationKind=*/llvm::OpenMPIRBuilder::EvalKind::Scalar,
12201228
owningReductionGens[i],
12211229
/*ReductionGenClang=*/nullptr, atomicGen});
@@ -2603,34 +2611,34 @@ convertOmpScan(Operation &opInst, llvm::IRBuilderBase &builder,
26032611
auto scanOp = cast<omp::ScanOp>(opInst);
26042612
bool isInclusive = scanOp.hasInclusiveVars();
26052613
SmallVector<llvm::Value *> llvmScanVars;
2614+
SmallVector<llvm::Type *> llvmScanVarsType;
26062615
mlir::OperandRange mlirScanVars = scanOp.getInclusiveVars();
26072616
if (!isInclusive)
26082617
mlirScanVars = scanOp.getExclusiveVars();
26092618
for (auto val : mlirScanVars) {
26102619
llvm::Value *llvmVal = moduleTranslation.lookupValue(val);
2611-
26122620
llvmScanVars.push_back(llvmVal);
2621+
llvmScanVarsType.push_back(ReductionVarToType[llvmVal]);
26132622
}
26142623
llvm::OpenMPIRBuilder::InsertPointTy allocaIP =
26152624
findAllocaInsertPoint(builder, moduleTranslation);
26162625
llvm::OpenMPIRBuilder::LocationDescription ompLoc(builder);
26172626
llvm::OpenMPIRBuilder::InsertPointOrErrorTy afterIP =
26182627
moduleTranslation.getOpenMPBuilder()->createScan(
2619-
ompLoc, allocaIP, llvmScanVars, isInclusive);
2628+
ompLoc, allocaIP, llvmScanVars, llvmScanVarsType, isInclusive);
26202629
if (failed(handleError(afterIP, opInst)))
26212630
return failure();
26222631

26232632
builder.restoreIP(*afterIP);
26242633

26252634
// TODO: The argument of LoopnestOp is stored into the index variable and this
2626-
// variable is used
2627-
// across scan operation. However that makes the mlir
2628-
// invalid.(`Intra-iteration dependences from a statement in the structured
2629-
// block sequence that precede a scan directive to a statement in the
2630-
// structured block sequence that follows a scan directive must not exist,
2631-
// except for dependences for the list items specified in an inclusive or
2632-
// exclusive clause.`). The argument of LoopNestOp need to be loaded again
2633-
// after ScanOp again so mlir generated is valid.
2635+
// variable is used across scan operation. However that makes the mlir
2636+
// invalid.(`Intra-iteration dependences from a statement in the structured
2637+
// block sequence that precede a scan directive to a statement in the
2638+
// structured block sequence that follows a scan directive must not exist,
2639+
// except for dependences for the list items specified in an inclusive or
2640+
// exclusive clause.`). The argument of LoopNestOp need to be loaded again
2641+
// after ScanOp again so mlir generated is valid.
26342642
auto parentOp = scanOp->getParentOp();
26352643
auto loopOp = cast<omp::LoopNestOp>(parentOp);
26362644
if (loopOp) {

0 commit comments

Comments
 (0)