diff --git a/mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp b/mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp --- a/mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp +++ b/mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp @@ -124,18 +124,20 @@ pbKind = llvm::omp::getProcBindKind(bind.getValue()); // TODO: Is the Parallel construct cancellable? bool isCancellable = false; - // TODO: Determine the actual alloca insertion point, e.g., the function - // entry or the alloca insertion point as provided by the body callback - // above. - llvm::OpenMPIRBuilder::InsertPointTy allocaIP(builder.saveIP()); - if (failed(bodyGenStatus)) - return failure(); + + // Insert allocas at the entry block of the current function. + llvm::BasicBlock &funcEntryBlock = + builder.GetInsertBlock()->getParent()->getEntryBlock(); + llvm::OpenMPIRBuilder::InsertPointTy allocaIP( + &funcEntryBlock, funcEntryBlock.getFirstInsertionPt()); + llvm::OpenMPIRBuilder::LocationDescription ompLoc( builder.saveIP(), builder.getCurrentDebugLocation()); builder.restoreIP(moduleTranslation.getOpenMPBuilder()->createParallel( ompLoc, allocaIP, bodyGenCB, privCB, finiCB, ifCond, numThreads, pbKind, isCancellable)); - return success(); + + return bodyGenStatus; } /// Converts an OpenMP 'master' operation into LLVM IR using OpenMPIRBuilder. diff --git a/mlir/test/Target/LLVMIR/openmp-llvm.mlir b/mlir/test/Target/LLVMIR/openmp-llvm.mlir --- a/mlir/test/Target/LLVMIR/openmp-llvm.mlir +++ b/mlir/test/Target/LLVMIR/openmp-llvm.mlir @@ -151,6 +151,13 @@ // CHECK: define void @test_omp_parallel_if_1(i32 %[[IF_VAR_1:.*]]) llvm.func @test_omp_parallel_if_1(%arg0: i32) -> () { +// Check that the allocas are emitted by the OpenMPIRBuilder at the top of the +// funciton, before the condition. Allocas are only emitted by the builder when +// the `if` clause is present. We match specific SSA value names since LLVM +// actually produces those names. +// CHECK: %tid.addr{{.*}} = alloca i32 +// CHECK: %zero.addr{{.*}} = alloca i32 + // CHECK: %[[IF_COND_VAR_1:.*]] = icmp slt i32 %[[IF_VAR_1]], 0 %0 = llvm.mlir.constant(0 : index) : i32 %1 = llvm.icmp "slt" %arg0, %0 : i32