Skip to content

Commit ea5ee2e

Browse files
authored
[mlir][OpenMP] Don't allow firstprivate for simd (#146734)
This is not allowed by the openmp standard.
1 parent 3099b7e commit ea5ee2e

File tree

3 files changed

+54
-1
lines changed

3 files changed

+54
-1
lines changed

mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@
1515
#include "mlir/Dialect/Func/IR/FuncOps.h"
1616
#include "mlir/Dialect/LLVMIR/LLVMTypes.h"
1717
#include "mlir/Dialect/OpenACCMPCommon/Interfaces/AtomicInterfaces.h"
18+
#include "mlir/Dialect/OpenMP/OpenMPClauseOperands.h"
1819
#include "mlir/IR/Attributes.h"
1920
#include "mlir/IR/BuiltinAttributes.h"
2021
#include "mlir/IR/DialectImplementation.h"
2122
#include "mlir/IR/OpImplementation.h"
2223
#include "mlir/IR/OperationSupport.h"
24+
#include "mlir/IR/SymbolTable.h"
2325
#include "mlir/Interfaces/FoldInterfaces.h"
2426

2527
#include "llvm/ADT/ArrayRef.h"
@@ -2640,6 +2642,23 @@ LogicalResult SimdOp::verify() {
26402642
return emitError()
26412643
<< "'omp.composite' attribute present in non-composite wrapper";
26422644

2645+
// Firstprivate is not allowed for SIMD in the standard. Check that none of
2646+
// the private decls are for firstprivate.
2647+
std::optional<ArrayAttr> privateSyms = getPrivateSyms();
2648+
if (privateSyms) {
2649+
for (const Attribute &sym : *privateSyms) {
2650+
auto symRef = cast<SymbolRefAttr>(sym);
2651+
omp::PrivateClauseOp privatizer =
2652+
SymbolTable::lookupNearestSymbolFrom<omp::PrivateClauseOp>(
2653+
getOperation(), symRef);
2654+
if (!privatizer)
2655+
return emitError() << "Cannot find privatizer '" << symRef << "'";
2656+
if (privatizer.getDataSharingType() ==
2657+
DataSharingClauseType::FirstPrivate)
2658+
return emitError() << "FIRSTPRIVATE cannot be used with SIMD";
2659+
}
2660+
}
2661+
26432662
return success();
26442663
}
26452664

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2899,7 +2899,8 @@ convertOmpSimd(Operation &opInst, llvm::IRBuilderBase &builder,
28992899
.failed())
29002900
return failure();
29012901

2902-
// TODO: no call to copyFirstPrivateVars?
2902+
// No call to copyFirstPrivateVars because FIRSTPRIVATE is not allowed for
2903+
// SIMD.
29032904

29042905
assert(afterAllocas.get()->getSinglePredecessor());
29052906
if (failed(initReductionVars(simdOp, reductionArgs, builder,

mlir/test/Dialect/OpenMP/invalid.mlir

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,39 @@ func.func @omp_simd_pretty_simdlen_safelen(%lb : index, %ub : index, %step : ind
480480

481481
// -----
482482

483+
func.func @omp_simd_bad_privatizer(%lb : index, %ub : index, %step : index) {
484+
%0 = llvm.mlir.constant(1 : i64) : i64
485+
%1 = llvm.alloca %0 x i32 : (i64) -> !llvm.ptr
486+
// expected-error @below {{Cannot find privatizer '@not_defined'}}
487+
omp.simd private(@not_defined %1 -> %arg0 : !llvm.ptr) {
488+
omp.loop_nest (%arg2) : index = (%lb) to (%ub) inclusive step (%step) {
489+
omp.yield
490+
}
491+
}
492+
}
493+
494+
// -----
495+
496+
omp.private {type = firstprivate} @_QFEp_firstprivate_i32 : i32 copy {
497+
^bb0(%arg0: !llvm.ptr, %arg1: !llvm.ptr):
498+
%0 = llvm.load %arg0 : !llvm.ptr -> i32
499+
llvm.store %0, %arg1 : i32, !llvm.ptr
500+
omp.yield(%arg1 : !llvm.ptr)
501+
}
502+
func.func @omp_simd_firstprivate(%lb : index, %ub : index, %step : index) {
503+
%0 = llvm.mlir.constant(1 : i64) : i64
504+
%1 = llvm.alloca %0 x i32 : (i64) -> !llvm.ptr
505+
// expected-error @below {{FIRSTPRIVATE cannot be used with SIMD}}
506+
omp.simd private(@_QFEp_firstprivate_i32 %1 -> %arg0 : !llvm.ptr) {
507+
omp.loop_nest (%arg2) : index = (%lb) to (%ub) inclusive step (%step) {
508+
omp.yield
509+
}
510+
}
511+
llvm.return
512+
}
513+
514+
// -----
515+
483516
// expected-error @below {{op expects alloc region to yield a value of the reduction type}}
484517
omp.declare_reduction @add_f32 : f32
485518
alloc {

0 commit comments

Comments
 (0)