Skip to content

Commit e08c19c

Browse files
Fix for changed code at the end of AllocaIP.
Some of the OpenMP code can change the instruction pointed at by the insertion point. This leads to an assert in the compiler about BB->getParent() and IP->getParent() not matching or something like that. The fix is to rebuild the insertionpoint from the block, rather than use builder.restoreIP. Also, move some of the alloca generation, rather than skipping back and forth between insert points (and ensure all the allocas are done before their users are created). A simple test, mainly to ensure the minimal reproducer doesn't fail to compile in the future is also added.
1 parent c28566c commit e08c19c

File tree

3 files changed

+45
-11
lines changed

3 files changed

+45
-11
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
!! The main point of this test is to check that the code compiles at all, so the
2+
!! checking is not very detailed. Not hitting an assert, crashing or otherwise failing
3+
!! to compile is the key point. Also, emitting llvm is required for this to happen.
4+
! RUN: %flang_fc1 -emit-llvm -fopenmp -o - %s 2>&1 | FileCheck %s
5+
subroutine proc
6+
implicit none
7+
real(8),allocatable :: F(:)
8+
real(8),allocatable :: A(:)
9+
10+
!$omp parallel private(A) reduction(+:F)
11+
allocate(A(10))
12+
!$omp end parallel
13+
end subroutine proc
14+
15+
!CHECK-LABEL: define void @proc_()
16+
!CHECK: call void
17+
!CHECK-SAME: @__kmpc_fork_call(ptr {{.*}}, i32 1, ptr @[[OMP_PAR:.*]], {{.*}})
18+
19+
!CHECK: define internal void @[[OMP_PAR]]
20+
!CHECK: omp.par.region8:
21+
!CHECK-NEXT: call ptr @malloc
22+
!CHECK-SAME: i64 10
23+

llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1391,7 +1391,8 @@ IRBuilder<>::InsertPoint OpenMPIRBuilder::createParallel(
13911391

13921392
// Change the location to the outer alloca insertion point to create and
13931393
// initialize the allocas we pass into the parallel region.
1394-
Builder.restoreIP(OuterAllocaIP);
1394+
InsertPointTy NewOuter(OuterAllocaBlock, OuterAllocaBlock->begin());
1395+
Builder.restoreIP(NewOuter);
13951396
AllocaInst *TIDAddrAlloca = Builder.CreateAlloca(Int32, nullptr, "tid.addr");
13961397
AllocaInst *ZeroAddrAlloca =
13971398
Builder.CreateAlloca(Int32, nullptr, "zero.addr");
@@ -2151,7 +2152,8 @@ OpenMPIRBuilder::createReductions(const LocationDescription &Loc,
21512152
// values.
21522153
unsigned NumReductions = ReductionInfos.size();
21532154
Type *RedArrayTy = ArrayType::get(Builder.getPtrTy(), NumReductions);
2154-
Builder.restoreIP(AllocaIP);
2155+
Builder.SetInsertPoint(AllocaIP.getBlock()->getTerminator());
2156+
// Builder.restoreIP(AllocaIP);
21552157
Value *RedArray = Builder.CreateAlloca(RedArrayTy, nullptr, "red.array");
21562158

21572159
Builder.SetInsertPoint(InsertBlock, InsertBlock->end());
@@ -2552,7 +2554,10 @@ OpenMPIRBuilder::applyStaticWorkshareLoop(DebugLoc DL, CanonicalLoopInfo *CLI,
25522554
getOrCreateRuntimeFunction(M, omp::OMPRTL___kmpc_for_static_fini);
25532555

25542556
// Allocate space for computed loop bounds as expected by the "init" function.
2555-
Builder.restoreIP(AllocaIP);
2557+
2558+
// Builder.restoreIP(AllocaIP);
2559+
Builder.SetInsertPoint(AllocaIP.getBlock()->getTerminator());
2560+
25562561
Type *I32Type = Type::getInt32Ty(M.getContext());
25572562
Value *PLastIter = Builder.CreateAlloca(I32Type, nullptr, "p.lastiter");
25582563
Value *PLowerBound = Builder.CreateAlloca(IVTy, nullptr, "p.lowerbound");

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

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1154,6 +1154,17 @@ convertOmpParallel(omp::ParallelOp opInst, llvm::IRBuilderBase &builder,
11541154
MutableArrayRef<BlockArgument> reductionArgs =
11551155
opInst.getRegion().getArguments().take_back(
11561156
opInst.getNumReductionVars());
1157+
1158+
SmallVector<llvm::Value *> byRefVars;
1159+
if (isByRef) {
1160+
for (unsigned i = 0; i < opInst.getNumReductionVars(); ++i) {
1161+
// Allocate reduction variable (which is a pointer to the real reduciton
1162+
// variable allocated in the inlined region)
1163+
byRefVars.push_back(builder.CreateAlloca(
1164+
moduleTranslation.convertType(reductionDecls[i].getType())));
1165+
}
1166+
}
1167+
11571168
for (unsigned i = 0; i < opInst.getNumReductionVars(); ++i) {
11581169
SmallVector<llvm::Value *> phis;
11591170

@@ -1166,18 +1177,13 @@ convertOmpParallel(omp::ParallelOp opInst, llvm::IRBuilderBase &builder,
11661177
assert(phis.size() == 1 &&
11671178
"expected one value to be yielded from the "
11681179
"reduction neutral element declaration region");
1169-
builder.restoreIP(allocaIP);
11701180

1171-
if (isByRef[i]) {
1172-
// Allocate reduction variable (which is a pointer to the real reduciton
1173-
// variable allocated in the inlined region)
1174-
llvm::Value *var = builder.CreateAlloca(
1175-
moduleTranslation.convertType(reductionDecls[i].getType()));
1181+
if (isByRef) {
11761182
// Store the result of the inlined region to the allocated reduction var
11771183
// ptr
1178-
builder.CreateStore(phis[0], var);
1184+
builder.CreateStore(phis[0], byRefVars[i]);
11791185

1180-
privateReductionVariables.push_back(var);
1186+
privateReductionVariables.push_back(byRefVars[i]);
11811187
moduleTranslation.mapValue(reductionArgs[i], phis[0]);
11821188
reductionVariableMap.try_emplace(opInst.getReductionVars()[i], phis[0]);
11831189
} else {

0 commit comments

Comments
 (0)