Skip to content

Commit d71af25

Browse files
committed
Handle variable DIM properly.
1 parent 3e54458 commit d71af25

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

flang/lib/Lower/HlfirIntrinsics.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -405,9 +405,16 @@ mlir::Value HlfirCShiftLowering::lowerImpl(
405405
mlir::Type stmtResultType) {
406406
auto operands = getOperandVector(loweredActuals, argLowering);
407407
assert(operands.size() == 3);
408-
// If DIM is not present, drop the last element which is a null Value.
409-
if (!operands[2])
408+
mlir::Value dim = operands[2];
409+
if (!dim) {
410+
// If DIM is not present, drop the last element which is a null Value.
410411
operands.truncate(2);
412+
} else {
413+
// If DIM is present, then dereference it if it is a ref.
414+
dim = hlfir::loadTrivialScalar(loc, builder, hlfir::Entity{dim});
415+
operands[2] = dim;
416+
}
417+
411418
mlir::Type resultType = computeResultType(operands[0], stmtResultType);
412419
return createOp<hlfir::CShiftOp>(resultType, operands);
413420
}

flang/test/Lower/HLFIR/cshift.f90

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,3 +195,24 @@ subroutine cshift10(a, s)
195195
! CHECK: hlfir.destroy %[[VAL_7]] : !hlfir.expr<?x!fir.type<_QMtypesTt>?>
196196
! CHECK: return
197197
! CHECK: }
198+
199+
! 1d shift by scalar with variable dim
200+
subroutine cshift11(a, s, d)
201+
integer :: a(:), s, d
202+
a = CSHIFT(a, 2, d)
203+
end subroutine
204+
! CHECK-LABEL: func.func @_QPcshift11(
205+
! CHECK-SAME: %[[VAL_0:.*]]: !fir.box<!fir.array<?xi32>> {fir.bindc_name = "a"},
206+
! CHECK-SAME: %[[VAL_1:.*]]: !fir.ref<i32> {fir.bindc_name = "s"},
207+
! CHECK-SAME: %[[VAL_2:.*]]: !fir.ref<i32> {fir.bindc_name = "d"}) {
208+
! CHECK: %[[VAL_3:.*]] = fir.dummy_scope : !fir.dscope
209+
! CHECK: %[[VAL_4:.*]]:2 = hlfir.declare %[[VAL_0]] dummy_scope %[[VAL_3]] {uniq_name = "_QFcshift11Ea"} : (!fir.box<!fir.array<?xi32>>, !fir.dscope) -> (!fir.box<!fir.array<?xi32>>, !fir.box<!fir.array<?xi32>>)
210+
! CHECK: %[[VAL_5:.*]]:2 = hlfir.declare %[[VAL_2]] dummy_scope %[[VAL_3]] {uniq_name = "_QFcshift11Ed"} : (!fir.ref<i32>, !fir.dscope) -> (!fir.ref<i32>, !fir.ref<i32>)
211+
! CHECK: %[[VAL_6:.*]]:2 = hlfir.declare %[[VAL_1]] dummy_scope %[[VAL_3]] {uniq_name = "_QFcshift11Es"} : (!fir.ref<i32>, !fir.dscope) -> (!fir.ref<i32>, !fir.ref<i32>)
212+
! CHECK: %[[VAL_7:.*]] = arith.constant 2 : i32
213+
! CHECK: %[[VAL_8:.*]] = fir.load %[[VAL_5]]#0 : !fir.ref<i32>
214+
! CHECK: %[[VAL_9:.*]] = hlfir.cshift %[[VAL_4]]#0 %[[VAL_7]] dim %[[VAL_8]] : (!fir.box<!fir.array<?xi32>>, i32, i32) -> !hlfir.expr<?xi32>
215+
! CHECK: hlfir.assign %[[VAL_9]] to %[[VAL_4]]#0 : !hlfir.expr<?xi32>, !fir.box<!fir.array<?xi32>>
216+
! CHECK: hlfir.destroy %[[VAL_9]] : !hlfir.expr<?xi32>
217+
! CHECK: return
218+
! CHECK: }

0 commit comments

Comments
 (0)