[flang-commits] [flang] 5bbfbd9 - [flang][OpenMP] Fix parallel do with integer*1 var
Leandro Lupori via flang-commits
flang-commits at lists.llvm.org
Wed Aug 2 06:17:12 PDT 2023
Author: Leandro Lupori
Date: 2023-08-02T10:16:16-03:00
New Revision: 5bbfbd9a1cc5fbde44ac7d6a8187dba0cff298d6
URL: https://github.com/llvm/llvm-project/commit/5bbfbd9a1cc5fbde44ac7d6a8187dba0cff298d6
DIFF: https://github.com/llvm/llvm-project/commit/5bbfbd9a1cc5fbde44ac7d6a8187dba0cff298d6.diff
LOG: [flang][OpenMP] Fix parallel do with integer*1 var
Temporaries created to store worksharing loop index values were
using different types than that of the original index variables.
This caused invalid IR to be produced when an index variable was
used in binary operations which expected its original type.
Fix this by creating temporaries with the types of their original
variables and converting the loop values, that continue to use
the types that OpenMP runtime expects, to them.
Fixes https://github.com/llvm/llvm-project/issues/60870
Reviewed By: kiranchandramohan
Differential Revision: https://reviews.llvm.org/D156803
Added:
Modified:
flang/lib/Lower/OpenMP.cpp
flang/test/Lower/OpenMP/wsloop-variable.f90
Removed:
################################################################################
diff --git a/flang/lib/Lower/OpenMP.cpp b/flang/lib/Lower/OpenMP.cpp
index fa3779b2d93d74..a9cde9ab835e5f 100644
--- a/flang/lib/Lower/OpenMP.cpp
+++ b/flang/lib/Lower/OpenMP.cpp
@@ -1936,22 +1936,23 @@ static void resetBeforeTerminator(fir::FirOpBuilder &firOpBuilder,
static mlir::Operation *
createAndSetPrivatizedLoopVar(Fortran::lower::AbstractConverter &converter,
- mlir::Location loc, mlir::Type loopVarType,
- mlir::Value indexVal,
+ mlir::Location loc, mlir::Value indexVal,
const Fortran::semantics::Symbol *sym) {
fir::FirOpBuilder &firOpBuilder = converter.getFirOpBuilder();
mlir::OpBuilder::InsertPoint insPt = firOpBuilder.saveInsertionPoint();
firOpBuilder.setInsertionPointToStart(firOpBuilder.getAllocaBlock());
+ mlir::Type tempTy = converter.genType(*sym);
mlir::Value temp = firOpBuilder.create<fir::AllocaOp>(
- loc, loopVarType, /*pinned=*/true, /*lengthParams=*/mlir::ValueRange{},
+ loc, tempTy, /*pinned=*/true, /*lengthParams=*/mlir::ValueRange{},
/*shapeParams*/ mlir::ValueRange{},
llvm::ArrayRef<mlir::NamedAttribute>{
Fortran::lower::getAdaptToByRefAttr(firOpBuilder)});
converter.bindSymbol(*sym, temp);
firOpBuilder.restoreInsertionPoint(insPt);
+ mlir::Value cvtVal = firOpBuilder.createConvert(loc, tempTy, indexVal);
mlir::Operation *storeOp = firOpBuilder.create<fir::StoreOp>(
- loc, indexVal, converter.getSymbolAddress(*sym));
+ loc, cvtVal, converter.getSymbolAddress(*sym));
return storeOp;
}
@@ -1997,8 +1998,7 @@ static void createBodyOfOp(
for (const Fortran::semantics::Symbol *arg : args) {
mlir::Value indexVal =
fir::getBase(op.getRegion().front().getArgument(argIndex));
- storeOp = createAndSetPrivatizedLoopVar(converter, loc, loopVarType,
- indexVal, arg);
+ storeOp = createAndSetPrivatizedLoopVar(converter, loc, indexVal, arg);
argIndex++;
}
} else {
diff --git a/flang/test/Lower/OpenMP/wsloop-variable.f90 b/flang/test/Lower/OpenMP/wsloop-variable.f90
index bc92a87fb570b6..466055868f1cc6 100644
--- a/flang/test/Lower/OpenMP/wsloop-variable.f90
+++ b/flang/test/Lower/OpenMP/wsloop-variable.f90
@@ -23,11 +23,13 @@ program wsloop_variable
!CHECK: %[[TMP6:.*]] = fir.convert %[[TMP1]] : (i32) -> i64
!CHECK: %[[TMP7:.*]] = fir.convert %{{.*}} : (i32) -> i64
!CHECK: omp.wsloop for (%[[ARG0:.*]], %[[ARG1:.*]]) : i64 = (%[[TMP2]], %[[TMP5]]) to (%[[TMP3]], %[[TMP6]]) inclusive step (%[[TMP4]], %[[TMP7]]) {
-!CHECK: fir.store %[[ARG0]] to %[[STORE_IV0:.*]] : !fir.ref<i64>
+!CHECK: %[[ARG0_I16:.*]] = fir.convert %[[ARG0]] : (i64) -> i16
+!CHECK: fir.store %[[ARG0_I16]] to %[[STORE_IV0:.*]] : !fir.ref<i16>
!CHECK: fir.store %[[ARG1]] to %[[STORE_IV1:.*]] : !fir.ref<i64>
-!CHECK: %[[LOAD_IV0:.*]] = fir.load %[[STORE_IV0]] : !fir.ref<i64>
+!CHECK: %[[LOAD_IV0:.*]] = fir.load %[[STORE_IV0]] : !fir.ref<i16>
+!CHECK: %[[LOAD_IV0_I64:.*]] = fir.convert %[[LOAD_IV0]] : (i16) -> i64
!CHECK: %[[LOAD_IV1:.*]] = fir.load %[[STORE_IV1]] : !fir.ref<i64>
-!CHECK: %[[TMP10:.*]] = arith.addi %[[LOAD_IV0]], %[[LOAD_IV1]] : i64
+!CHECK: %[[TMP10:.*]] = arith.addi %[[LOAD_IV0_I64]], %[[LOAD_IV1]] : i64
!CHECK: %[[TMP11:.*]] = fir.convert %[[TMP10]] : (i64) -> f32
!CHECK: fir.store %[[TMP11]] to %{{.*}} : !fir.ref<f32>
!CHECK: omp.yield
@@ -45,9 +47,10 @@ program wsloop_variable
!CHECK: %[[TMP13:.*]] = fir.convert %{{.*}} : (i8) -> i32
!CHECK: %[[TMP14:.*]] = fir.convert %{{.*}} : (i64) -> i32
!CHECK: omp.wsloop for (%[[ARG0:.*]]) : i32 = (%[[TMP12]]) to (%[[TMP13]]) inclusive step (%[[TMP14]]) {
-!CHECK: fir.store %[[ARG0]] to %[[STORE3:.*]] : !fir.ref<i32>
-!CHECK: %[[LOAD3:.*]] = fir.load %[[STORE3]] : !fir.ref<i32>
-!CHECK: %[[TMP16:.*]] = fir.convert %[[LOAD3]] : (i32) -> f32
+!CHECK: %[[ARG0_I16:.*]] = fir.convert %[[ARG0]] : (i32) -> i16
+!CHECK: fir.store %[[ARG0_I16]] to %[[STORE3:.*]] : !fir.ref<i16>
+!CHECK: %[[LOAD3:.*]] = fir.load %[[STORE3]] : !fir.ref<i16>
+!CHECK: %[[TMP16:.*]] = fir.convert %[[LOAD3]] : (i16) -> f32
!CHECK: fir.store %[[TMP16]] to %{{.*}} : !fir.ref<f32>
!CHECK: omp.yield
@@ -63,9 +66,10 @@ program wsloop_variable
!CHECK: %[[TMP18:.*]] = fir.convert %{{.*}} : (i16) -> i64
!CHECK: %[[TMP19:.*]] = fir.convert %{{.*}} : (i32) -> i64
!CHECK: omp.wsloop for (%[[ARG1:.*]]) : i64 = (%[[TMP17]]) to (%[[TMP18]]) inclusive step (%[[TMP19]]) {
-!CHECK: fir.store %[[ARG1]] to %[[STORE4:.*]] : !fir.ref<i64>
-!CHECK: %[[LOAD4:.*]] = fir.load %[[STORE4]] : !fir.ref<i64>
-!CHECK: %[[TMP21:.*]] = fir.convert %[[LOAD4]] : (i64) -> f32
+!CHECK: %[[ARG1_I128:.*]] = fir.convert %[[ARG1]] : (i64) -> i128
+!CHECK: fir.store %[[ARG1_I128]] to %[[STORE4:.*]] : !fir.ref<i128>
+!CHECK: %[[LOAD4:.*]] = fir.load %[[STORE4]] : !fir.ref<i128>
+!CHECK: %[[TMP21:.*]] = fir.convert %[[LOAD4]] : (i128) -> f32
!CHECK: fir.store %[[TMP21]] to %{{.*}} : !fir.ref<f32>
!CHECK: omp.yield
!CHECK: }
@@ -79,12 +83,14 @@ program wsloop_variable
end program wsloop_variable
!CHECK-LABEL: func.func @_QPwsloop_variable_sub() {
+!CHECK: %[[IV2:.*]] = fir.alloca i8 {adapt.valuebyref, pinned}
!CHECK: %[[VAL_0:.*]] = fir.alloca i128 {bindc_name = "i16_lb", uniq_name = "_QFwsloop_variable_subEi16_lb"}
!CHECK: %[[VAL_1:.*]] = fir.alloca i8 {bindc_name = "i1_ub", uniq_name = "_QFwsloop_variable_subEi1_ub"}
!CHECK: %[[VAL_2:.*]] = fir.alloca i16 {bindc_name = "i2", uniq_name = "_QFwsloop_variable_subEi2"}
!CHECK: %[[VAL_3:.*]] = fir.alloca i16 {bindc_name = "i2_s", uniq_name = "_QFwsloop_variable_subEi2_s"}
!CHECK: %[[VAL_4:.*]] = fir.alloca i32 {bindc_name = "i4_s", uniq_name = "_QFwsloop_variable_subEi4_s"}
!CHECK: %[[VAL_5:.*]] = fir.alloca i64 {bindc_name = "i8", uniq_name = "_QFwsloop_variable_subEi8"}
+!CHECK: %[[J1:.*]] = fir.alloca i8 {bindc_name = "j1", uniq_name = "_QFwsloop_variable_subEj1"}
!CHECK: %[[VAL_6:.*]] = fir.alloca f32 {bindc_name = "x", uniq_name = "_QFwsloop_variable_subEx"}
!CHECK: %[[VAL_7:.*]] = arith.constant 1 : i32
!CHECK: %[[VAL_8:.*]] = fir.load %[[VAL_1]] : !fir.ref<i8>
@@ -92,7 +98,8 @@ end program wsloop_variable
!CHECK: %[[VAL_10:.*]] = fir.convert %[[VAL_8]] : (i8) -> i32
!CHECK: %[[VAL_11:.*]] = fir.convert %[[VAL_9]] : (i16) -> i32
!CHECK: omp.wsloop for (%[[ARG0:.*]]) : i32 = (%[[VAL_7]]) to (%[[VAL_10]]) inclusive step (%[[VAL_11]]) {
-!CHECK: fir.store %[[ARG0]] to %[[STORE_IV:.*]] : !fir.ref<i32>
+!CHECK: %[[ARG0_I16:.*]] = fir.convert %[[ARG0]] : (i32) -> i16
+!CHECK: fir.store %[[ARG0_I16]] to %[[STORE_IV:.*]] : !fir.ref<i16>
!CHECK: %[[VAL_13:.*]] = fir.load %[[VAL_0]] : !fir.ref<i128>
!CHECK: %[[VAL_14:.*]] = fir.convert %[[VAL_13]] : (i128) -> index
!CHECK: %[[VAL_15:.*]] = arith.constant 100 : i32
@@ -104,8 +111,8 @@ end program wsloop_variable
!CHECK-SAME: %[[VAL_14]] to %[[VAL_16]] step %[[VAL_18]]
!CHECK-SAME: iter_args(%[[IV:.*]] = %[[LB]]) -> (index, i64) {
!CHECK: fir.store %[[IV]] to %[[VAL_5]] : !fir.ref<i64>
-!CHECK: %[[LOAD_IV:.*]] = fir.load %[[STORE_IV]] : !fir.ref<i32>
-!CHECK: %[[VAL_22:.*]] = fir.convert %[[LOAD_IV]] : (i32) -> i64
+!CHECK: %[[LOAD_IV:.*]] = fir.load %[[STORE_IV]] : !fir.ref<i16>
+!CHECK: %[[VAL_22:.*]] = fir.convert %[[LOAD_IV]] : (i16) -> i64
!CHECK: %[[VAL_23:.*]] = fir.load %[[VAL_5]] : !fir.ref<i64>
!CHECK: %[[VAL_24:.*]] = arith.addi %[[VAL_22]], %[[VAL_23]] : i64
!CHECK: %[[VAL_25:.*]] = fir.convert %[[VAL_24]] : (i64) -> f32
@@ -119,11 +126,9 @@ end program wsloop_variable
!CHECK: fir.store %[[VAL_19]]#1 to %[[VAL_5]] : !fir.ref<i64>
!CHECK: omp.yield
!CHECK: }
-!CHECK: return
-!CHECK: }
subroutine wsloop_variable_sub
- integer(kind=1) :: i1_ub
+ integer(kind=1) :: i1, i1_ub, j1
integer(kind=2) :: i2, i2_s
integer(kind=4) :: i4_s
integer(kind=8) :: i8
@@ -138,4 +143,31 @@ subroutine wsloop_variable_sub
end do
!$omp end do
+!CHECK: %[[C1:.*]] = arith.constant 1 : i32
+!CHECK: %[[C10:.*]] = arith.constant 10 : i32
+!CHECK: %[[C1_2:.*]] = arith.constant 1 : i32
+!CHECK: omp.wsloop for (%[[ARG0:.*]]) : i32 = (%[[C1]]) to (%[[C10]]) inclusive step (%[[C1_2]]) {
+!CHECK: %[[ARG0_I8:.*]] = fir.convert %[[ARG0]] : (i32) -> i8
+!CHECK: fir.store %[[ARG0_I8]] to %[[IV2]] : !fir.ref<i8>
+!CHECK: %[[IV2LOAD:.*]] = fir.load %[[IV2]] : !fir.ref<i8>
+!CHECK: %[[J1LOAD:.*]] = fir.load %[[J1]] : !fir.ref<i8>
+!CHECK: %[[VAL_27:.*]] = arith.cmpi eq, %[[IV2LOAD]], %[[J1LOAD]] : i8
+!CHECK: fir.if %[[VAL_27]] {
+!CHECK: } else {
+!CHECK: }
+!CHECK: omp.yield
+!CHECK: }
+
+ j1 = 5
+ !$omp do
+ do i1 = 1, 10
+ if (i1 .eq. j1) then
+ print *, "EQ"
+ end if
+ end do
+ !$omp end do
+
+!CHECK: return
+!CHECK: }
+
end
More information about the flang-commits
mailing list