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 @@ -485,10 +485,12 @@ void genRandomInit(llvm::ArrayRef); void genRandomNumber(llvm::ArrayRef); void genRandomSeed(llvm::ArrayRef); + fir::ExtendedValue genReshape(mlir::Type, llvm::ArrayRef); mlir::Value genSetExponent(mlir::Type resultType, llvm::ArrayRef args); fir::ExtendedValue genSize(mlir::Type, llvm::ArrayRef); fir::ExtendedValue genSum(mlir::Type, llvm::ArrayRef); + fir::ExtendedValue genSpread(mlir::Type, llvm::ArrayRef); void genSystemClock(llvm::ArrayRef); fir::ExtendedValue genTransfer(mlir::Type, llvm::ArrayRef); @@ -719,6 +721,13 @@ &I::genRandomSeed, {{{"size", asBox}, {"put", asBox}, {"get", asBox}}}, /*isElemental=*/false}, + {"reshape", + &I::genReshape, + {{{"source", asBox}, + {"shape", asBox}, + {"pad", asBox, handleDynamicOptional}, + {"order", asBox, handleDynamicOptional}}}, + /*isElemental=*/false}, {"set_exponent", &I::genSetExponent}, {"size", &I::genSize, @@ -726,6 +735,10 @@ {"dim", asAddr, handleDynamicOptional}, {"kind", asValue}}}, /*isElemental=*/false}, + {"spread", + &I::genSpread, + {{{"source", asAddr}, {"dim", asValue}, {"ncopies", asValue}}}, + /*isElemental=*/false}, {"sum", &I::genSum, {{{"array", asBox}, @@ -2441,6 +2454,52 @@ Fortran::lower::genRandomSeed(builder, loc, -1, mlir::Value{}); } +// RESHAPE +fir::ExtendedValue +IntrinsicLibrary::genReshape(mlir::Type resultType, + llvm::ArrayRef args) { + assert(args.size() == 4); + + // Handle source argument + mlir::Value source = builder.createBox(loc, args[0]); + + // Handle shape argument + mlir::Value shape = builder.createBox(loc, args[1]); + assert(fir::BoxValue(shape).rank() == 1); + mlir::Type shapeTy = shape.getType(); + mlir::Type shapeArrTy = fir::dyn_cast_ptrOrBoxEleTy(shapeTy); + auto resultRank = shapeArrTy.cast().getShape(); + + assert(resultRank[0] != fir::SequenceType::getUnknownExtent() && + "shape arg must have constant size"); + + // Handle optional pad argument + mlir::Value pad = isAbsent(args[2]) + ? builder.create( + loc, fir::BoxType::get(builder.getI1Type())) + : builder.createBox(loc, args[2]); + + // Handle optional order argument + mlir::Value order = isAbsent(args[3]) + ? builder.create( + loc, fir::BoxType::get(builder.getI1Type())) + : builder.createBox(loc, args[3]); + + // Create mutable fir.box to be passed to the runtime for the result. + mlir::Type type = builder.getVarLenSeqTy(resultType, resultRank[0]); + fir::MutableBoxValue resultMutableBox = + fir::factory::createTempMutableBox(builder, loc, type); + + mlir::Value resultIrBox = + fir::factory::getMutableIRBox(builder, loc, resultMutableBox); + + fir::runtime::genReshape(builder, loc, resultIrBox, source, shape, pad, + order); + + return readAndAddCleanUp(resultMutableBox, resultType, + "unexpected result for RESHAPE"); +} + // SET_EXPONENT mlir::Value IntrinsicLibrary::genSetExponent(mlir::Type resultType, llvm::ArrayRef args) { @@ -2452,6 +2511,38 @@ fir::getBase(args[1]))); } +// SPREAD +fir::ExtendedValue +IntrinsicLibrary::genSpread(mlir::Type resultType, + llvm::ArrayRef args) { + + assert(args.size() == 3); + + // Handle source argument + mlir::Value source = builder.createBox(loc, args[0]); + fir::BoxValue sourceTmp = source; + unsigned sourceRank = sourceTmp.rank(); + + // Handle Dim argument + mlir::Value dim = fir::getBase(args[1]); + + // Handle ncopies argument + mlir::Value ncopies = fir::getBase(args[2]); + + // Generate result descriptor + mlir::Type resultArrayType = + builder.getVarLenSeqTy(resultType, sourceRank + 1); + fir::MutableBoxValue resultMutableBox = + fir::factory::createTempMutableBox(builder, loc, resultArrayType); + mlir::Value resultIrBox = + fir::factory::getMutableIRBox(builder, loc, resultMutableBox); + + fir::runtime::genSpread(builder, loc, resultIrBox, source, dim, ncopies); + + return readAndAddCleanUp(resultMutableBox, resultType, + "unexpected result for SPREAD"); +} + // SUM fir::ExtendedValue IntrinsicLibrary::genSum(mlir::Type resultType, diff --git a/flang/test/Lower/Intrinsics/reshape.f90 b/flang/test/Lower/Intrinsics/reshape.f90 new file mode 100644 --- /dev/null +++ b/flang/test/Lower/Intrinsics/reshape.f90 @@ -0,0 +1,56 @@ +! RUN: bbc -emit-fir %s -o - | FileCheck %s + +! CHECK-LABEL: func @_QPreshape_test( +! CHECK-SAME: %[[arg0:.*]]: !fir.box>{{.*}}, %[[arg1:[^:]+]]: !fir.box>{{.*}}, %[[arg2:[^:]+]]: !fir.box>{{.*}}, %[[arg3:.*]]: !fir.ref>{{.*}}, %[[arg4:.*]]: !fir.ref>{{.*}}) { +subroutine reshape_test(x, source, pd, sh, ord) + integer :: x(:,:) + integer :: source(:,:,:) + integer :: pd(:,:,:) + integer :: sh(2) + integer :: ord(2) + ! CHECK-DAG: %[[c2:.*]] = arith.constant 2 : index + ! CHECK-DAG: %[[a0:.*]] = fir.alloca !fir.box>> + ! CHECK-DAG: %[[a1:.*]] = fir.shape %[[c2]] : (index) -> !fir.shape<1> + ! CHECK-DAG: %[[a2:.*]] = fir.embox %[[arg3]](%{{.*}}) : (!fir.ref>, !fir.shape<1>) -> !fir.box> + ! CHECK-DAG: %[[a3:.*]] = fir.embox %[[arg4]](%{{.*}}) : (!fir.ref>, !fir.shape<1>) -> !fir.box> + ! CHECK-DAG: %[[a8:.*]] = fir.convert %[[a0]] : (!fir.ref>>>) -> !fir.ref> + ! CHECK-DAG: %[[a9:.*]] = fir.convert %[[arg1]] : (!fir.box>) -> !fir.box + ! CHECK-DAG: %[[a10:.*]] = fir.convert %[[a2]] : (!fir.box>) -> !fir.box + ! CHECK-DAG: %[[a11:.*]] = fir.convert %[[arg2]] : (!fir.box>) -> !fir.box + ! CHECK-DAG: %[[a12:.*]] = fir.convert %[[a3]] : (!fir.box>) -> !fir.box + x = reshape(source, sh, pd, ord) + ! CHECK: %{{.*}} = fir.call @_FortranAReshape(%[[a8]], %[[a9]], %[[a10]], %[[a11]], %[[a12]], %{{.*}}, %{{.*}}) : (!fir.ref>, !fir.box, !fir.box, !fir.box, !fir.box, !fir.ref, i32) -> none + ! CHECK-DAG: %[[a15:.*]] = fir.load %[[a0]] : !fir.ref>>> + ! CHECK-DAG: %[[a18:.*]] = fir.box_addr %[[a15]] : (!fir.box>>) -> !fir.heap> + ! CHECK-DAG: fir.freemem %[[a18]] + end subroutine + + ! CHECK-LABEL: func @_QPtest_reshape_optional( + ! CHECK-SAME: %[[VAL_0:.*]]: !fir.ref>>> + ! CHECK-SAME: %[[VAL_1:.*]]: !fir.ref>>> + subroutine test_reshape_optional(pad, order, source, shape) + real, pointer :: pad(:, :) + integer, pointer :: order(:) + real :: source(:, :, :) + integer :: shape(4) + print *, reshape(source=source, shape=shape, pad=pad, order=order) + ! CHECK: %[[VAL_13:.*]] = fir.load %[[VAL_0]] : !fir.ref>>> + ! CHECK: %[[VAL_14:.*]] = fir.box_addr %[[VAL_13]] : (!fir.box>>) -> !fir.ptr> + ! CHECK: %[[VAL_15:.*]] = fir.convert %[[VAL_14]] : (!fir.ptr>) -> i64 + ! CHECK: %[[VAL_16:.*]] = arith.constant 0 : i64 + ! CHECK: %[[VAL_17:.*]] = arith.cmpi ne, %[[VAL_15]], %[[VAL_16]] : i64 + ! CHECK: %[[VAL_18:.*]] = fir.load %[[VAL_0]] : !fir.ref>>> + ! CHECK: %[[VAL_19:.*]] = fir.absent !fir.box>> + ! CHECK: %[[VAL_20:.*]] = arith.select %[[VAL_17]], %[[VAL_18]], %[[VAL_19]] : !fir.box>> + ! CHECK: %[[VAL_21:.*]] = fir.load %[[VAL_1]] : !fir.ref>>> + ! CHECK: %[[VAL_22:.*]] = fir.box_addr %[[VAL_21]] : (!fir.box>>) -> !fir.ptr> + ! CHECK: %[[VAL_23:.*]] = fir.convert %[[VAL_22]] : (!fir.ptr>) -> i64 + ! CHECK: %[[VAL_24:.*]] = arith.constant 0 : i64 + ! CHECK: %[[VAL_25:.*]] = arith.cmpi ne, %[[VAL_23]], %[[VAL_24]] : i64 + ! CHECK: %[[VAL_26:.*]] = fir.load %[[VAL_1]] : !fir.ref>>> + ! CHECK: %[[VAL_27:.*]] = fir.absent !fir.box>> + ! CHECK: %[[VAL_28:.*]] = arith.select %[[VAL_25]], %[[VAL_26]], %[[VAL_27]] : !fir.box>> + ! CHECK: %[[VAL_38:.*]] = fir.convert %[[VAL_20]] : (!fir.box>>) -> !fir.box + ! CHECK: %[[VAL_39:.*]] = fir.convert %[[VAL_28]] : (!fir.box>>) -> !fir.box + ! CHECK: %[[VAL_41:.*]] = fir.call @_FortranAReshape({{.*}}, {{.*}}, %{{.*}}, %[[VAL_38]], %[[VAL_39]], %{{.*}}, %{{.*}}) : (!fir.ref>, !fir.box, !fir.box, !fir.box, !fir.box, !fir.ref, i32) -> none + end subroutine diff --git a/flang/test/Lower/Intrinsics/spread.f90 b/flang/test/Lower/Intrinsics/spread.f90 new file mode 100644 --- /dev/null +++ b/flang/test/Lower/Intrinsics/spread.f90 @@ -0,0 +1,39 @@ +! RUN: bbc -emit-fir %s -o - | FileCheck %s + +! CHECK-LABEL: func @_QPspread_test( +! CHECK-SAME: %[[arg0:[^:]+]]: !fir.ref{{.*}}, %[[arg1:[^:]+]]: !fir.ref{{.*}}, %[[arg2:[^:]+]]: !fir.ref{{.*}}, %[[arg3:.*]]: !fir.box>{{.*}}) { +subroutine spread_test(s,d,n,r) + integer :: s,d,n + integer :: r(:) + ! CHECK-DAG: %[[a0:.*]] = fir.alloca !fir.box>> + ! CHECK-DAG: %[[a1:.*]] = fir.load %[[arg1]] : !fir.ref + ! CHECK-DAG: %[[a2:.*]] = fir.load %[[arg2]] : !fir.ref + ! CHECK-DAG: %[[a3:.*]] = fir.embox %[[arg0]] : (!fir.ref) -> !fir.box + ! CHECK-DAG: %[[a8:.*]] = fir.convert %[[a0]] : (!fir.ref>>>) -> !fir.ref> + ! CHECK-DAG: %[[a9:.*]] = fir.convert %[[a3]] : (!fir.box) -> !fir.box + ! CHECK-DAG: %[[a10:.*]] = fir.convert %[[a2]] : (i32) -> i64 + r = spread(s,d,n) + ! CHECK: %{{.*}} = fir.call @_FortranASpread(%[[a8]], %[[a9]], %[[a1]], %[[a10]], %{{.*}}, %{{.*}}) : (!fir.ref>, !fir.box, i32, i64, !fir.ref, i32) -> none + ! CHECK-DAG: %[[a13:.*]] = fir.load %[[a0]] : !fir.ref>>> + ! CHECK-DAG: %[[a15:.*]] = fir.box_addr %[[a13]] : (!fir.box>>) -> !fir.heap> + ! CHECK: fir.freemem %[[a15]] + end subroutine + + ! CHECK-LABEL: func @_QPspread_test2( + ! CHECK-SAME: %[[arg0:.*]]: !fir.box>{{.*}}, %[[arg1:[^:]+]]: !fir.ref{{.*}}, %[[arg2:[^:]+]]: !fir.ref{{.*}}, %[[arg3:.*]]: !fir.box>{{.*}}) { + subroutine spread_test2(s,d,n,r) + integer :: s(:),d,n + integer :: r(:,:) + ! CHECK-DAG: %[[a0:.*]] = fir.alloca !fir.box>> + ! CHECK-DAG: %[[a1:.*]] = fir.load %[[arg1]] : !fir.ref + ! CHECK-DAG: %[[a2:.*]] = fir.load %[[arg2]] : !fir.ref + ! CHECK-DAG: %[[a7:.*]] = fir.convert %[[a0]] : (!fir.ref>>>) -> !fir.ref> + ! CHECK-DAG: %[[a8:.*]] = fir.convert %[[arg0]] : (!fir.box>) -> !fir.box + ! CHECK-DAG: %[[a9:.*]] = fir.convert %[[a2]] : (i32) -> i64 + r = spread(s,d,n) + ! CHECK: %{{.*}} = fir.call @_FortranASpread(%[[a7]], %[[a8]], %[[a1]], %[[a9]], %{{.*}}, %{{.*}}) : (!fir.ref>, !fir.box, i32, i64, !fir.ref, i32) -> none + ! CHECK-DAG: %[[a12:.*]] = fir.load %[[a0]] : !fir.ref>>> + ! CHECK-DAG: %[[a15:.*]] = fir.box_addr %[[a12]] : (!fir.box>>) -> !fir.heap> + ! CHECK: fir.freemem %[[a15:.*]] + end subroutine + \ No newline at end of file