diff --git a/flang/lib/Lower/IntrinsicCall.cpp b/flang/lib/Lower/IntrinsicCall.cpp --- a/flang/lib/Lower/IntrinsicCall.cpp +++ b/flang/lib/Lower/IntrinsicCall.cpp @@ -463,6 +463,7 @@ mlir::Value genIshftc(mlir::Type, llvm::ArrayRef); fir::ExtendedValue genLbound(mlir::Type, llvm::ArrayRef); fir::ExtendedValue genNull(mlir::Type, llvm::ArrayRef); + fir::ExtendedValue genPack(mlir::Type, llvm::ArrayRef); fir::ExtendedValue genLen(mlir::Type, llvm::ArrayRef); fir::ExtendedValue genLenTrim(mlir::Type, llvm::ArrayRef); fir::ExtendedValue genMaxloc(mlir::Type, llvm::ArrayRef); @@ -480,6 +481,7 @@ fir::ExtendedValue genTransfer(mlir::Type, llvm::ArrayRef); fir::ExtendedValue genUbound(mlir::Type, llvm::ArrayRef); + fir::ExtendedValue genUnpack(mlir::Type, llvm::ArrayRef); /// Define the different FIR generators that can be mapped to intrinsic to /// generate the related code. @@ -677,6 +679,12 @@ {"mask", asBox, handleDynamicOptional}}}, /*isElemental=*/false}, {"null", &I::genNull, {{{"mold", asInquired}}}, /*isElemental=*/false}, + {"pack", + &I::genPack, + {{{"array", asBox}, + {"mask", asBox}, + {"vector", asBox, handleDynamicOptional}}}, + /*isElemental=*/false}, {"random_init", &I::genRandomInit, {{{"repeatable", asValue}, {"image_distinct", asValue}}}, @@ -708,6 +716,10 @@ &I::genUbound, {{{"array", asBox}, {"dim", asValue}, {"kind", asValue}}}, /*isElemental=*/false}, + {"unpack", + &I::genUnpack, + {{{"vector", asBox}, {"mask", asBox}, {"field", asBox}}}, + /*isElemental=*/false}, }; static const IntrinsicHandler *findIntrinsicHandler(llvm::StringRef name) { @@ -2182,6 +2194,38 @@ return fir::MutableBoxValue(boxStorage, mold->nonDeferredLenParams(), {}); } +// PACK +fir::ExtendedValue +IntrinsicLibrary::genPack(mlir::Type resultType, + llvm::ArrayRef args) { + [[maybe_unused]] auto numArgs = args.size(); + assert(numArgs == 2 || numArgs == 3); + + // Handle required array argument + mlir::Value array = builder.createBox(loc, args[0]); + + // Handle required mask argument + mlir::Value mask = builder.createBox(loc, args[1]); + + // Handle optional vector argument + mlir::Value vector = isAbsent(args, 2) + ? builder.create( + loc, fir::BoxType::get(builder.getI1Type())) + : builder.createBox(loc, args[2]); + + // Create mutable fir.box to be passed to the runtime for the result. + mlir::Type resultArrayType = builder.getVarLenSeqTy(resultType, 1); + fir::MutableBoxValue resultMutableBox = + fir::factory::createTempMutableBox(builder, loc, resultArrayType); + mlir::Value resultIrBox = + fir::factory::getMutableIRBox(builder, loc, resultMutableBox); + + fir::runtime::genPack(builder, loc, resultIrBox, array, mask, vector); + + return readAndAddCleanUp(resultMutableBox, resultType, + "unexpected result for PACK"); +} + // RANDOM_INIT void IntrinsicLibrary::genRandomInit(llvm::ArrayRef args) { assert(args.size() == 2); @@ -2402,6 +2446,36 @@ return mlir::Value(); } +// UNPACK +fir::ExtendedValue +IntrinsicLibrary::genUnpack(mlir::Type resultType, + llvm::ArrayRef args) { + assert(args.size() == 3); + + // Handle required vector argument + mlir::Value vector = builder.createBox(loc, args[0]); + + // Handle required mask argument + fir::BoxValue maskBox = builder.createBox(loc, args[1]); + mlir::Value mask = fir::getBase(maskBox); + unsigned maskRank = maskBox.rank(); + + // Handle required field argument + mlir::Value field = builder.createBox(loc, args[2]); + + // Create mutable fir.box to be passed to the runtime for the result. + mlir::Type resultArrayType = builder.getVarLenSeqTy(resultType, maskRank); + fir::MutableBoxValue resultMutableBox = + fir::factory::createTempMutableBox(builder, loc, resultArrayType); + mlir::Value resultIrBox = + fir::factory::getMutableIRBox(builder, loc, resultMutableBox); + + fir::runtime::genUnpack(builder, loc, resultIrBox, vector, mask, field); + + return readAndAddCleanUp(resultMutableBox, resultType, + "unexpected result for UNPACK"); +} + //===----------------------------------------------------------------------===// // Argument lowering rules interface //===----------------------------------------------------------------------===// diff --git a/flang/test/Lower/Intrinsics/pack.f90 b/flang/test/Lower/Intrinsics/pack.f90 new file mode 100644 --- /dev/null +++ b/flang/test/Lower/Intrinsics/pack.f90 @@ -0,0 +1,42 @@ +! RUN: bbc -emit-fir %s -o - | FileCheck %s + +! CHECK-LABEL: pack_test +! CHECK-SAME: %[[arg0:[^:]+]]: !fir.box> +! CHECK-SAME: %[[arg1:.*]]: !fir.box>> +! CHECK-SAME: %[[arg2:[^:]+]]: !fir.box> +! CHECK-SAME: %[[arg3:[^:]+]]: !fir.box> +subroutine pack_test(a,m,v,r) + integer :: a(:) + logical :: m(:) + integer :: v(:) + integer :: r(:) + ! CHECK: %[[a0:.*]] = fir.alloca !fir.box>> + ! CHECK: %[[a5:.*]] = fir.convert %[[a0]] : (!fir.ref>>>) -> !fir.ref> + ! CHECK: %[[a6:.*]] = fir.convert %[[arg0]] : (!fir.box>) -> !fir.box + ! CHECK: %[[a7:.*]] = fir.convert %[[arg1]] : (!fir.box>>) -> !fir.box + ! CHECK: %[[a8:.*]] = fir.convert %[[arg2]] : (!fir.box>) -> !fir.box + r = pack(a,m,v) + ! CHECK: %{{.*}} = fir.call @_FortranAPack(%[[a5]], %[[a6]], %[[a7]], %[[a8]], %{{.*}}, %{{.*}}) : (!fir.ref>, !fir.box, !fir.box, !fir.box, !fir.ref, i32) -> none + ! CHECK: %[[a11:.*]] = fir.load %[[a0]] : !fir.ref>>> + ! CHECK: %[[a13:.*]] = fir.box_addr %[[a11]] : (!fir.box>>) -> !fir.heap> + ! CHECK: fir.freemem %[[a13]] + end subroutine + + ! CHECK-LABEL: func @_QPtest_pack_optional( + ! CHECK-SAME: %[[VAL_0:.*]]: !fir.ref>>> + subroutine test_pack_optional(vector, array, mask) + integer, pointer :: vector(:) + integer :: array(:, :) + logical :: mask(:, :) + print *, pack(array, mask, vector) + ! CHECK: %[[VAL_9:.*]] = fir.load %[[VAL_0]] : !fir.ref>>> + ! CHECK: %[[VAL_10:.*]] = fir.box_addr %[[VAL_9]] : (!fir.box>>) -> !fir.ptr> + ! CHECK: %[[VAL_11:.*]] = fir.convert %[[VAL_10]] : (!fir.ptr>) -> i64 + ! CHECK: %[[VAL_12:.*]] = arith.constant 0 : i64 + ! CHECK: %[[VAL_13:.*]] = arith.cmpi ne, %[[VAL_11]], %[[VAL_12]] : i64 + ! CHECK: %[[VAL_14:.*]] = fir.load %[[VAL_0]] : !fir.ref>>> + ! CHECK: %[[VAL_15:.*]] = fir.absent !fir.box>> + ! CHECK: %[[VAL_16:.*]] = arith.select %[[VAL_13]], %[[VAL_14]], %[[VAL_15]] : !fir.box>> + ! CHECK: %[[VAL_26:.*]] = fir.convert %[[VAL_16]] : (!fir.box>>) -> !fir.box + ! CHECK: fir.call @_FortranAPack(%{{.*}}, %{{.*}}, %{{.*}}, %[[VAL_26]], %{{.*}}, %{{.*}}) : (!fir.ref>, !fir.box, !fir.box, !fir.box, !fir.ref, i32) -> none + end subroutine diff --git a/flang/test/Lower/transformational-intrinsics.f90 b/flang/test/Lower/transformational-intrinsics.f90 new file mode 100644 --- /dev/null +++ b/flang/test/Lower/transformational-intrinsics.f90 @@ -0,0 +1,289 @@ +! Test how transformational intrinsic function references are lowered + +! RUN: bbc -emit-fir %s -o - | FileCheck %s + +! The exact intrinsic being tested does not really matter, what is +! tested here is that transformational intrinsics are lowered correctly +! regardless of the context they appear into. + + + +module test2 + interface + subroutine takes_array_desc(l) + logical(1) :: l(:) + end subroutine + end interface + + contains + + ! CHECK-LABEL: func @_QMtest2Pin_io( + ! CHECK-SAME: %[[arg0:.*]]: !fir.box>>{{.*}}) { + subroutine in_io(x) + logical(1) :: x(:, :) + ! CHECK: %[[res_desc:.]] = fir.alloca !fir.box>>> + ! CHECK-DAG: %[[res_arg:.*]] = fir.convert %[[res_desc]] : (!fir.ref>>>>) -> !fir.ref> + ! CHECK-DAG: %[[x_arg:.*]] = fir.convert %[[arg0]] : (!fir.box>>) -> !fir.box + ! CHECK: fir.call @_Fortran{{.*}}AllDim(%[[res_arg]], %[[x_arg]], {{.*}}) : (!fir.ref>, !fir.box, i32, !fir.ref, i32) -> none + ! CHECK: %[[res_desc_load:.*]] = fir.load %[[res_desc]] : !fir.ref>>>> + ! CHECK-DAG: %[[dims:.*]]:3 = fir.box_dims %[[res_desc_load]], %c0{{.*}} : (!fir.box>>>, index) -> (index, index, index) + ! CHECK-DAG: %[[res_addr:.*]] = fir.box_addr %[[res_desc_load]] : (!fir.box>>>) -> !fir.heap>> + ! CHECK-DAG: %[[res_shape:.*]] = fir.shape_shift %[[dims]]#0, %[[dims]]#1 : (index, index) -> !fir.shapeshift<1> + ! CHECK: %[[io_embox:.*]] = fir.embox %[[res_addr]](%[[res_shape]]) : (!fir.heap>>, !fir.shapeshift<1>) -> !fir.box>> + ! CHECK: %[[io_embox_cast:.*]] = fir.convert %[[io_embox]] : (!fir.box>>) -> !fir.box + ! CHECK: fir.call @_Fortran{{.*}}ioOutputDescriptor({{.*}}, %[[io_embox_cast]]) : (!fir.ref, !fir.box) -> i1 + print *, all(x, 1) + ! CHECK: fir.freemem %[[res_addr]] + end subroutine + + ! CHECK-LABEL: func @_QMtest2Pin_call( + ! CHECK-SAME: %[[arg0:.*]]: !fir.box>>{{.*}}) { + subroutine in_call(x) + implicit none + logical(1) :: x(:, :) + ! CHECK: %[[res_desc:.]] = fir.alloca !fir.box>>> + ! CHECK-DAG: %[[res_arg:.*]] = fir.convert %[[res_desc]] : (!fir.ref>>>>) -> !fir.ref> + ! CHECK-DAG: %[[x_arg:.*]] = fir.convert %[[arg0]] : (!fir.box>>) -> !fir.box + ! CHECK: fir.call @_Fortran{{.*}}AllDim(%[[res_arg]], %[[x_arg]], {{.*}}) : (!fir.ref>, !fir.box, i32, !fir.ref, i32) -> none + ! CHECK: %[[res_desc_load:.*]] = fir.load %[[res_desc]] : !fir.ref>>>> + ! CHECK-DAG: %[[dims:.*]]:3 = fir.box_dims %[[res_desc_load]], %c0{{.*}} : (!fir.box>>>, index) -> (index, index, index) + ! CHECK-DAG: %[[res_addr:.*]] = fir.box_addr %[[res_desc_load]] : (!fir.box>>>) -> !fir.heap>> + ! CHECK-DAG: %[[res_shape:.*]] = fir.shape_shift %[[dims]]#0, %[[dims]]#1 : (index, index) -> !fir.shapeshift<1> + ! CHECK: %[[call_embox:.*]] = fir.embox %[[res_addr]](%[[res_shape]]) : (!fir.heap>>, !fir.shapeshift<1>) -> !fir.box>> + ! CHECK: fir.call @_QPtakes_array_desc(%[[call_embox]]) : (!fir.box>>) -> () + call takes_array_desc(all(x, 1)) + ! CHECK: fir.freemem %[[res_addr]] + end subroutine + + ! CHECK-LABEL: func @_QMtest2Pin_implicit_call( + ! CHECK-SAME: %[[arg0:.*]]: !fir.box>>{{.*}}) { + subroutine in_implicit_call(x) + logical(1) :: x(:, :) + ! CHECK: %[[res_desc:.]] = fir.alloca !fir.box>>> + ! CHECK: fir.call @_Fortran{{.*}}AllDim + ! CHECK: %[[res_desc_load:.*]] = fir.load %[[res_desc]] : !fir.ref>>>> + ! CHECK: %[[res_addr:.*]] = fir.box_addr %[[res_desc_load]] : (!fir.box>>>) -> !fir.heap>> + ! CHECK: %[[res_addr_cast:.*]] = fir.convert %[[res_addr]] : (!fir.heap>>) -> !fir.ref>> + ! CHECK: fir.call @_QPtakes_implicit_array(%[[res_addr_cast]]) : (!fir.ref>>) -> () + call takes_implicit_array(all(x, 1)) + ! CHECK: fir.freemem %[[res_addr]] + end subroutine + + ! CHECK-LABEL: func @_QMtest2Pin_assignment( + ! CHECK-SAME: %[[arg0:.*]]: !fir.box>>{{.*}}, %[[arg1:.*]]: !fir.box>>{{.*}}) + subroutine in_assignment(x, y) + logical(1) :: x(:, :), y(:) + ! CHECK: %[[res_desc:.]] = fir.alloca !fir.box>>> + ! CHECK: %[[y_load:.*]] = fir.array_load %[[arg1]] : (!fir.box>>) -> !fir.array> + ! CHECK: fir.call @_Fortran{{.*}}AllDim + + ! CHECK: %[[res_desc_load:.*]] = fir.load %[[res_desc]] : !fir.ref>>>> + ! CHECK-DAG: %[[dims:.*]]:3 = fir.box_dims %[[res_desc_load]], %c0{{.*}} : (!fir.box>>>, index) -> (index, index, index) + ! CHECK-DAG: %[[res_addr:.*]] = fir.box_addr %[[res_desc_load]] : (!fir.box>>>) -> !fir.heap>> + ! CHECK-DAG: %[[res_shape:.*]] = fir.shape_shift %[[dims]]#0, %[[dims]]#1 : (index, index) -> !fir.shapeshift<1> + ! CHECK: %[[res_load:.*]] = fir.array_load %[[res_addr]](%[[res_shape]]) : (!fir.heap>>, !fir.shapeshift<1>) -> !fir.array> + + ! CHECK: %[[assign:.*]] = fir.do_loop %[[idx:.*]] = %{{.*}} to {{.*}} { + ! CHECK: %[[res_elt:.*]] = fir.array_fetch %[[res_load]], %[[idx]] : (!fir.array>, index) -> !fir.logical<1> + ! CHECK: fir.array_update %{{.*}} %[[res_elt]], %[[idx]] : (!fir.array>, !fir.logical<1>, index) -> !fir.array> + ! CHECK: } + ! CHECK: fir.array_merge_store %[[y_load]], %[[assign]] to %[[arg1]] : !fir.array>, !fir.array>, !fir.box>> + y = all(x, 1) + ! CHECK: fir.freemem %[[res_addr]] + end subroutine + + ! CHECK-LABEL: func @_QMtest2Pin_elem_expr( + ! CHECK-SAME: %[[arg0:.*]]: !fir.box>>{{.*}}, %[[arg1:.*]]: !fir.box>>{{.*}}, %[[arg2:.*]]: !fir.box>>{{.*}}) + subroutine in_elem_expr(x, y, z) + logical(1) :: x(:, :), y(:), z(:) + ! CHECK: %[[res_desc:.]] = fir.alloca !fir.box>>> + ! CHECK-DAG: %[[y_load:.*]] = fir.array_load %[[arg1]] : (!fir.box>>) -> !fir.array> + ! CHECK-DAG: %[[z_load:.*]] = fir.array_load %[[arg2]] : (!fir.box>>) -> !fir.array> + ! CHECK: fir.call @_Fortran{{.*}}AllDim + + ! CHECK: %[[res_desc_load:.*]] = fir.load %[[res_desc]] : !fir.ref>>>> + ! CHECK-DAG: %[[dims:.*]]:3 = fir.box_dims %[[res_desc_load]], %c0{{.*}} : (!fir.box>>>, index) -> (index, index, index) + ! CHECK-DAG: %[[res_addr:.*]] = fir.box_addr %[[res_desc_load]] : (!fir.box>>>) -> !fir.heap>> + ! CHECK-DAG: %[[res_shape:.*]] = fir.shape_shift %[[dims]]#0, %[[dims]]#1 : (index, index) -> !fir.shapeshift<1> + ! CHECK: %[[res_load:.*]] = fir.array_load %[[res_addr]](%[[res_shape]]) : (!fir.heap>>, !fir.shapeshift<1>) -> !fir.array> + + ! CHECK: %[[elem_expr:.*]] = fir.do_loop %[[idx:.*]] = %{{.*}} to {{.*}} { + ! CHECK-DAG: %[[y_elt:.*]] = fir.array_fetch %[[y_load]], %[[idx]] : (!fir.array>, index) -> !fir.logical<1> + ! CHECK-DAG: %[[res_elt:.*]] = fir.array_fetch %[[res_load]], %[[idx]] : (!fir.array>, index) -> !fir.logical<1> + ! CHECK-DAG: %[[y_elt_i1:.*]] = fir.convert %[[y_elt]] : (!fir.logical<1>) -> i1 + ! CHECK-DAG: %[[res_elt_i1:.*]] = fir.convert %[[res_elt]] : (!fir.logical<1>) -> i1 + ! CHECK: %[[neqv_i1:.*]] = arith.cmpi ne, %[[y_elt_i1]], %[[res_elt_i1]] : i1 + ! CHECK: %[[neqv:.*]] = fir.convert %[[neqv_i1]] : (i1) -> !fir.logical<1> + ! CHECK: fir.array_update %{{.*}} %[[neqv]], %[[idx]] : (!fir.array>, !fir.logical<1>, index) -> !fir.array> + ! CHECK: } + ! CHECK: fir.array_merge_store %[[z_load]], %[[elem_expr]] to %[[arg2]] : !fir.array>, !fir.array>, !fir.box>> + z = y .neqv. all(x, 1) + ! CHECK: fir.freemem %[[res_addr]] + end subroutine + + ! CSHIFT + + ! CHECK-LABEL: func @_QMtest2Pcshift_test() { + ! CHECK: %[[VAL_0:.*]] = fir.alloca !fir.box>> + ! CHECK: %[[VAL_1:.*]] = fir.alloca i32 + ! CHECK: %[[VAL_2:.*]] = fir.alloca !fir.box>> + ! CHECK: %[[VAL_3:.*]] = arith.constant 3 : index + ! CHECK: %[[VAL_4:.*]] = arith.constant 3 : index + ! CHECK: %[[VAL_5:.*]] = fir.alloca !fir.array<3x3xi32> {bindc_name = "array", uniq_name = "_QMtest2Fcshift_testEarray"} + ! CHECK: %[[VAL_6:.*]] = arith.constant 3 : index + ! CHECK: %[[VAL_7:.*]] = arith.constant 3 : index + ! CHECK: %[[VAL_8:.*]] = fir.alloca !fir.array<3x3xi32> {bindc_name = "result", uniq_name = "_QMtest2Fcshift_testEresult"} + ! CHECK: %[[VAL_9:.*]] = arith.constant 3 : index + ! CHECK: %[[VAL_10:.*]] = fir.alloca !fir.array<3xi32> {bindc_name = "shift", uniq_name = "_QMtest2Fcshift_testEshift"} + ! CHECK: %[[VAL_11:.*]] = arith.constant 6 : index + ! CHECK: %[[VAL_12:.*]] = fir.alloca !fir.array<6xi32> {bindc_name = "vector", uniq_name = "_QMtest2Fcshift_testEvector"} + ! CHECK: %[[VAL_13:.*]] = arith.constant 6 : index + ! CHECK: %[[VAL_14:.*]] = fir.alloca !fir.array<6xi32> {bindc_name = "vectorresult", uniq_name = "_QMtest2Fcshift_testEvectorresult"} + ! CHECK: %[[VAL_15:.*]] = fir.shape %[[VAL_6]], %[[VAL_7]] : (index, index) -> !fir.shape<2> + ! CHECK: %[[VAL_16:.*]] = fir.array_load %[[VAL_8]](%[[VAL_15]]) : (!fir.ref>, !fir.shape<2>) -> !fir.array<3x3xi32> + ! CHECK: %[[VAL_17:.*]] = arith.constant -2 : i32 + ! CHECK: %[[VAL_18:.*]] = fir.shape %[[VAL_3]], %[[VAL_4]] : (index, index) -> !fir.shape<2> + ! CHECK: %[[VAL_19:.*]] = fir.embox %[[VAL_5]](%[[VAL_18]]) : (!fir.ref>, !fir.shape<2>) -> !fir.box> + ! CHECK: %[[VAL_20:.*]] = fir.zero_bits !fir.heap> + ! CHECK: %[[VAL_21:.*]] = arith.constant 0 : index + ! CHECK: %[[VAL_22:.*]] = fir.shape %[[VAL_21]], %[[VAL_21]] : (index, index) -> !fir.shape<2> + ! CHECK: %[[VAL_23:.*]] = fir.embox %[[VAL_20]](%[[VAL_22]]) : (!fir.heap>, !fir.shape<2>) -> !fir.box>> + ! CHECK: fir.store %[[VAL_23]] to %[[VAL_2]] : !fir.ref>>> + ! CHECK: %[[VAL_24:.*]] = fir.shape %[[VAL_9]] : (index) -> !fir.shape<1> + ! CHECK: %[[VAL_25:.*]] = fir.embox %[[VAL_10]](%[[VAL_24]]) : (!fir.ref>, !fir.shape<1>) -> !fir.box> + ! CHECK: %[[VAL_26:.*]] = fir.address_of(@_QQcl{{.*}}) : !fir.ref>>>) -> !fir.ref> + ! CHECK: %[[VAL_29:.*]] = fir.convert %[[VAL_19]] : (!fir.box>) -> !fir.box + ! CHECK: %[[VAL_30:.*]] = fir.convert %[[VAL_25]] : (!fir.box>) -> !fir.box + ! CHECK: %[[VAL_31:.*]] = fir.convert %[[VAL_26]] : (!fir.ref>) -> !fir.ref + ! CHECK: %[[VAL_32:.*]] = fir.call @_FortranACshift(%[[VAL_28]], %[[VAL_29]], %[[VAL_30]], %[[VAL_17]], %[[VAL_31]], %[[VAL_27]]) : (!fir.ref>, !fir.box, !fir.box, i32, !fir.ref, i32) -> none + ! CHECK: %[[VAL_33:.*]] = fir.load %[[VAL_2]] : !fir.ref>>> + ! CHECK: %[[VAL_34:.*]] = arith.constant 0 : index + ! CHECK: %[[VAL_35:.*]]:3 = fir.box_dims %[[VAL_33]], %[[VAL_34]] : (!fir.box>>, index) -> (index, index, index) + ! CHECK: %[[VAL_36:.*]] = arith.constant 1 : index + ! CHECK: %[[VAL_37:.*]]:3 = fir.box_dims %[[VAL_33]], %[[VAL_36]] : (!fir.box>>, index) -> (index, index, index) + ! CHECK: %[[VAL_38:.*]] = fir.box_addr %[[VAL_33]] : (!fir.box>>) -> !fir.heap> + ! CHECK: %[[VAL_39:.*]] = fir.shape_shift %[[VAL_35]]#0, %[[VAL_35]]#1, %[[VAL_37]]#0, %[[VAL_37]]#1 : (index, index, index, index) -> !fir.shapeshift<2> + ! CHECK: %[[VAL_40:.*]] = fir.array_load %[[VAL_38]](%[[VAL_39]]) : (!fir.heap>, !fir.shapeshift<2>) -> !fir.array + ! CHECK: %[[VAL_41:.*]] = arith.constant 1 : index + ! CHECK: %[[VAL_42:.*]] = arith.constant 0 : index + ! CHECK: %[[VAL_43:.*]] = arith.subi %[[VAL_6]], %[[VAL_41]] : index + ! CHECK: %[[VAL_44:.*]] = arith.subi %[[VAL_7]], %[[VAL_41]] : index + ! CHECK: %[[VAL_45:.*]] = fir.do_loop %[[VAL_46:.*]] = %[[VAL_42]] to %[[VAL_44]] step %[[VAL_41]] unordered iter_args(%[[VAL_47:.*]] = %[[VAL_16]]) -> (!fir.array<3x3xi32>) { + ! CHECK: %[[VAL_48:.*]] = fir.do_loop %[[VAL_49:.*]] = %[[VAL_42]] to %[[VAL_43]] step %[[VAL_41]] unordered iter_args(%[[VAL_50:.*]] = %[[VAL_47]]) -> (!fir.array<3x3xi32>) { + ! CHECK: %[[VAL_51:.*]] = fir.array_fetch %[[VAL_40]], %[[VAL_49]], %[[VAL_46]] : (!fir.array, index, index) -> i32 + ! CHECK: %[[VAL_52:.*]] = fir.array_update %[[VAL_50]], %[[VAL_51]], %[[VAL_49]], %[[VAL_46]] : (!fir.array<3x3xi32>, i32, index, index) -> !fir.array<3x3xi32> + ! CHECK: fir.result %[[VAL_52]] : !fir.array<3x3xi32> + ! CHECK: } + ! CHECK: fir.result %[[VAL_53:.*]] : !fir.array<3x3xi32> + ! CHECK: } + ! CHECK: fir.array_merge_store %[[VAL_16]], %[[VAL_54:.*]] to %[[VAL_8]] : !fir.array<3x3xi32>, !fir.array<3x3xi32>, !fir.ref> + ! CHECK: fir.freemem %[[VAL_38]] + ! CHECK: %[[VAL_55:.*]] = fir.shape %[[VAL_13]] : (index) -> !fir.shape<1> + ! CHECK: %[[VAL_56:.*]] = fir.array_load %[[VAL_14]](%[[VAL_55]]) : (!fir.ref>, !fir.shape<1>) -> !fir.array<6xi32> + ! CHECK: %[[VAL_57:.*]] = arith.constant 3 : i32 + ! CHECK: fir.store %[[VAL_57]] to %[[VAL_1]] : !fir.ref + ! CHECK: %[[VAL_58:.*]] = fir.shape %[[VAL_11]] : (index) -> !fir.shape<1> + ! CHECK: %[[VAL_59:.*]] = fir.embox %[[VAL_12]](%[[VAL_58]]) : (!fir.ref>, !fir.shape<1>) -> !fir.box> + ! CHECK: %[[VAL_60:.*]] = fir.zero_bits !fir.heap> + ! CHECK: %[[VAL_61:.*]] = arith.constant 0 : index + ! CHECK: %[[VAL_62:.*]] = fir.shape %[[VAL_61]] : (index) -> !fir.shape<1> + ! CHECK: %[[VAL_63:.*]] = fir.embox %[[VAL_60]](%[[VAL_62]]) : (!fir.heap>, !fir.shape<1>) -> !fir.box>> + ! CHECK: fir.store %[[VAL_63]] to %[[VAL_0]] : !fir.ref>>> + ! CHECK: %[[VAL_64:.*]] = fir.load %[[VAL_1]] : !fir.ref + ! CHECK: %[[VAL_65:.*]] = fir.address_of(@_QQcl.{{.*}}) : !fir.ref>>>) -> !fir.ref> + ! CHECK: %[[VAL_68:.*]] = fir.convert %[[VAL_59]] : (!fir.box>) -> !fir.box + ! CHECK: %[[VAL_69:.*]] = fir.convert %[[VAL_64]] : (i32) -> i64 + ! CHECK: %[[VAL_70:.*]] = fir.convert %[[VAL_65]] : (!fir.ref>) -> !fir.ref + ! CHECK: %[[VAL_71:.*]] = fir.call @_FortranACshiftVector(%[[VAL_67]], %[[VAL_68]], %[[VAL_69]], %[[VAL_70]], %[[VAL_66]]) : (!fir.ref>, !fir.box, i64, !fir.ref, i32) -> none + ! CHECK: %[[VAL_72:.*]] = fir.load %[[VAL_0]] : !fir.ref>>> + ! CHECK: %[[VAL_73:.*]] = arith.constant 0 : index + ! CHECK: %[[VAL_74:.*]]:3 = fir.box_dims %[[VAL_72]], %[[VAL_73]] : (!fir.box>>, index) -> (index, index, index) + ! CHECK: %[[VAL_75:.*]] = fir.box_addr %[[VAL_72]] : (!fir.box>>) -> !fir.heap> + ! CHECK: %[[VAL_76:.*]] = fir.shape_shift %[[VAL_74]]#0, %[[VAL_74]]#1 : (index, index) -> !fir.shapeshift<1> + ! CHECK: %[[VAL_77:.*]] = fir.array_load %[[VAL_75]](%[[VAL_76]]) : (!fir.heap>, !fir.shapeshift<1>) -> !fir.array + ! CHECK: %[[VAL_78:.*]] = arith.constant 1 : index + ! CHECK: %[[VAL_79:.*]] = arith.constant 0 : index + ! CHECK: %[[VAL_80:.*]] = arith.subi %[[VAL_13]], %[[VAL_78]] : index + ! CHECK: %[[VAL_81:.*]] = fir.do_loop %[[VAL_82:.*]] = %[[VAL_79]] to %[[VAL_80]] step %[[VAL_78]] unordered iter_args(%[[VAL_83:.*]] = %[[VAL_56]]) -> (!fir.array<6xi32>) { + ! CHECK: %[[VAL_84:.*]] = fir.array_fetch %[[VAL_77]], %[[VAL_82]] : (!fir.array, index) -> i32 + ! CHECK: %[[VAL_85:.*]] = fir.array_update %[[VAL_83]], %[[VAL_84]], %[[VAL_82]] : (!fir.array<6xi32>, i32, index) -> !fir.array<6xi32> + ! CHECK: fir.result %[[VAL_85]] : !fir.array<6xi32> + ! CHECK: } + ! CHECK: fir.array_merge_store %[[VAL_56]], %[[VAL_86:.*]] to %[[VAL_14]] : !fir.array<6xi32>, !fir.array<6xi32>, !fir.ref> + ! CHECK: fir.freemem %[[VAL_75]] + ! CHECK: return + ! CHECK: } + + subroutine cshift_test() + integer, dimension (3, 3) :: array + integer, dimension(3) :: shift + integer, dimension(3, 3) :: result + integer, dimension(6) :: vectorResult + integer, dimension (6) :: vector + result = cshift(array, shift, -2) ! non-vector case + vectorResult = cshift(vector, 3) ! vector case + end subroutine cshift_test + + ! UNPACK + ! CHECK-LABEL: func @_QMtest2Punpack_test + subroutine unpack_test() + integer, dimension(3) :: vector + integer, dimension (3,3) :: field + + logical, dimension(3,3) :: mask + integer, dimension(3,3) :: result + result = unpack(vector, mask, field) + ! CHECK-DAG: %[[a0:.*]] = fir.alloca !fir.box>> + ! CHECK-DAG: %[[a1:.*]] = fir.alloca i32 + ! CHECK-DAG: %[[a2:.*]] = fir.alloca !fir.box>> + ! CHECK-DAG: %[[a3:.*]] = fir.alloca !fir.array<3x3xi32> {bindc_name = "field", uniq_name = "_QMtest2Funpack_testEfield"} + ! CHECK-DAG: %[[a4:.*]] = fir.alloca !fir.array<3x3x!fir.logical<4>> {bindc_name = "mask", uniq_name = "_QMtest2Funpack_testEmask"} + ! CHECK-DAG: %[[a5:.*]] = fir.alloca !fir.array<3x3xi32> {bindc_name = "result", uniq_name = "_QMtest2Funpack_testEresult"} + ! CHECK-DAG: %[[a6:.*]] = fir.alloca !fir.array<3xi32> {bindc_name = "vector", uniq_name = "_QMtest2Funpack_testEvector"} + ! CHECK: %[[a7:.*]] = fir.shape %{{.*}}, %{{.*}} : (index, index) -> !fir.shape<2> + ! CHECK-NEXT: %[[a8:.*]] = fir.array_load %[[a5]](%[[a7]]) : (!fir.ref>, !fir.shape<2>) -> !fir.array<3x3xi32> + ! CHECK: %[[a9:.*]] = fir.shape %{{.*}} : (index) -> !fir.shape<1> + ! CHECK: %[[a10:.*]] = fir.embox %[[a6]](%[[a9]]) : (!fir.ref>, !fir.shape<1>) -> !fir.box> + ! CHECK: %[[a11:.*]] = fir.shape %{{.*}}, %{{.*}} : (index, index) -> !fir.shape<2> + ! CHECK: %[[a12:.*]] = fir.embox %[[a4]](%[[a11]]) : (!fir.ref>>, !fir.shape<2>) -> !fir.box>> + ! CHECK: %[[a13:.*]] = fir.shape %{{.*}}, %{{.*}} : (index, index) -> !fir.shape<2> + ! CHECK-NEXT: %[[a14:.*]] = fir.embox %[[a3]](%[[a13]]) : (!fir.ref>, !fir.shape<2>) -> !fir.box> + ! CHECK: %[[a15:.*]] = fir.zero_bits !fir.heap> + ! CHECK: %[[a16:.*]] = fir.shape %{{.*}}, %{{.*}} : (index, index) -> !fir.shape<2> + ! CHECK: %[[a17:.*]] = fir.embox %[[a15]](%[[a16]]) : (!fir.heap>, !fir.shape<2>) -> !fir.box>> + ! CHECK-NEXT: fir.store %[[a17]] to %[[a2]] : !fir.ref>>> + ! CHECK-DAG: %[[a19:.*]] = fir.convert %[[a2]] : (!fir.ref>>>) -> !fir.ref> + ! CHECK-DAG: %[[a20:.*]] = fir.convert %[[a10]] : (!fir.box>) -> !fir.box + ! CHECK-DAG: %[[a21:.*]] = fir.convert %[[a12]] : (!fir.box>>) -> !fir.box + ! CHECK-DAG: %[[a22:.*]] = fir.convert %[[a14]] : (!fir.box>) -> !fir.box + ! CHECK: fir.call @_FortranAUnpack(%[[a19]], %[[a20]], %[[a21]], %[[a22]], %{{.*}}, %{{.*}}) : (!fir.ref>, !fir.box, !fir.box, !fir.box, !fir.ref, i32) -> none + ! CHECK-NEXT: %[[a22:.*]] = fir.load %{{.*}} : !fir.ref>>> + ! CHECK: %[[a25:.*]] = fir.box_addr %[[a22]] : (!fir.box>>) -> !fir.heap> + ! CHECK: fir.freemem %[[a25]] + ! CHECK: %[[a36:.*]] = fir.shape %{{.*}}, %{{.*}} : (index, index) -> !fir.shape<2> + ! CHECK: %[[a38:.*]] = fir.shape %{{.*}} : (index) -> !fir.shape<1> + ! CHECK-NEXT: %[[a39:.*]] = fir.embox %[[a6]](%[[a38]]) : (!fir.ref>, !fir.shape<1>) -> !fir.box> + ! CHECK: %[[a40:.*]] = fir.shape %{{.*}}, %{{.*}} : (index, index) -> !fir.shape<2> + ! CHECK-NEXT: %[[a41:.*]] = fir.embox %[[a4]](%[[a40]]) : (!fir.ref>>, !fir.shape<2>) -> !fir.box>> + ! CHECK: %[[a42:.*]] = fir.embox %[[a1]] : (!fir.ref) -> !fir.box + ! CHECK: %[[a43:.*]] = fir.zero_bits !fir.heap> + ! CHECK: %[[a44:.*]] = fir.shape %{{.*}}, %{{.*}} : (index, index) -> !fir.shape<2> + ! CHECK: %[[a45:.*]] = fir.embox %[[a43]](%[[a44]]) : (!fir.heap>, !fir.shape<2>) -> !fir.box>> + ! CHECK-NEXT: fir.store %[[a45]] to %[[a0]] : !fir.ref>>> + ! CHECK: %[[a47:.*]] = fir.convert %[[a0]] : (!fir.ref>>>) -> !fir.ref> + ! CHECK: %[[a48:.*]] = fir.convert %[[a39]] : (!fir.box>) -> !fir.box + ! CHECK: %[[a49:.*]] = fir.convert %[[a41]] : (!fir.box>>) -> !fir.box + ! CHECK: %[[a50:.*]] = fir.convert %[[a42]] : (!fir.box) -> !fir.box + result = unpack(vector, mask, 343) + ! CHECK: fir.call @_FortranAUnpack(%[[a47]], %[[a48]], %[[a49]], %[[a50]], %{{.*}}, %{{.*}}) : (!fir.ref>, !fir.box, !fir.box, !fir.box, !fir.ref, i32) -> none + ! CHECK: %[[a53:.*]] = fir.load %[[a0]] : !fir.ref>>> + ! CHECK: %[[a56:.*]] = fir.box_addr %[[a53]] : (!fir.box>>) -> !fir.heap> + ! CHECK: fir.freemem %[[a56]] + ! CHECK-NEXT: return + end subroutine unpack_test + + end module