llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT--> @llvm/pr-subscribers-flang-fir-hlfir Author: Valentin Clement (バレンタイン クレメン) (clementval) <details> <summary>Changes</summary> #<!-- -->95297 Updates the runtime entry points to distinguish between reduction operation with arguments passed by value or by reference. Add lowering to support the arguments passed by value. --- Patch is 62.25 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/95353.diff 5 Files Affected: - (modified) flang/include/flang/Optimizer/Builder/Runtime/RTBuilder.h (+22) - (modified) flang/include/flang/Optimizer/Builder/Runtime/Reduction.h (+4-4) - (modified) flang/lib/Optimizer/Builder/IntrinsicCall.cpp (+12-4) - (modified) flang/lib/Optimizer/Builder/Runtime/Reduction.cpp (+413-55) - (modified) flang/test/Lower/Intrinsics/reduce.f90 (+223-12) ``````````diff diff --git a/flang/include/flang/Optimizer/Builder/Runtime/RTBuilder.h b/flang/include/flang/Optimizer/Builder/Runtime/RTBuilder.h index 809d5b8d569dc..845ba385918d0 100644 --- a/flang/include/flang/Optimizer/Builder/Runtime/RTBuilder.h +++ b/flang/include/flang/Optimizer/Builder/Runtime/RTBuilder.h @@ -64,6 +64,18 @@ using FuncTypeBuilderFunc = mlir::FunctionType (*)(mlir::MLIRContext *); }; \ } +#define REDUCTION_VALUE_OPERATION_MODEL(T) \ + template <> \ + constexpr TypeBuilderFunc \ + getModel<Fortran::runtime::ValueReductionOperation<T>>() { \ + return [](mlir::MLIRContext *context) -> mlir::Type { \ + TypeBuilderFunc f{getModel<T>()}; \ + auto refTy = fir::ReferenceType::get(f(context)); \ + return mlir::FunctionType::get(context, {f(context), f(context)}, \ + refTy); \ + }; \ + } + #define REDUCTION_CHAR_OPERATION_MODEL(T) \ template <> \ constexpr TypeBuilderFunc \ @@ -481,17 +493,27 @@ constexpr TypeBuilderFunc getModel<void>() { } REDUCTION_REF_OPERATION_MODEL(std::int8_t) +REDUCTION_VALUE_OPERATION_MODEL(std::int8_t) REDUCTION_REF_OPERATION_MODEL(std::int16_t) +REDUCTION_VALUE_OPERATION_MODEL(std::int16_t) REDUCTION_REF_OPERATION_MODEL(std::int32_t) +REDUCTION_VALUE_OPERATION_MODEL(std::int32_t) REDUCTION_REF_OPERATION_MODEL(std::int64_t) +REDUCTION_VALUE_OPERATION_MODEL(std::int64_t) REDUCTION_REF_OPERATION_MODEL(Fortran::common::int128_t) +REDUCTION_VALUE_OPERATION_MODEL(Fortran::common::int128_t) REDUCTION_REF_OPERATION_MODEL(float) +REDUCTION_VALUE_OPERATION_MODEL(float) REDUCTION_REF_OPERATION_MODEL(double) +REDUCTION_VALUE_OPERATION_MODEL(double) REDUCTION_REF_OPERATION_MODEL(long double) +REDUCTION_VALUE_OPERATION_MODEL(long double) REDUCTION_REF_OPERATION_MODEL(std::complex<float>) +REDUCTION_VALUE_OPERATION_MODEL(std::complex<float>) REDUCTION_REF_OPERATION_MODEL(std::complex<double>) +REDUCTION_VALUE_OPERATION_MODEL(std::complex<double>) REDUCTION_CHAR_OPERATION_MODEL(char) REDUCTION_CHAR_OPERATION_MODEL(char16_t) diff --git a/flang/include/flang/Optimizer/Builder/Runtime/Reduction.h b/flang/include/flang/Optimizer/Builder/Runtime/Reduction.h index fedf453a6dc8d..2a40cddc0cc2c 100644 --- a/flang/include/flang/Optimizer/Builder/Runtime/Reduction.h +++ b/flang/include/flang/Optimizer/Builder/Runtime/Reduction.h @@ -229,8 +229,8 @@ void genIParityDim(fir::FirOpBuilder &builder, mlir::Location loc, /// result value. This is used for COMPLEX, CHARACTER and DERIVED TYPES. void genReduce(fir::FirOpBuilder &builder, mlir::Location loc, mlir::Value arrayBox, mlir::Value operation, mlir::Value maskBox, - mlir::Value identity, mlir::Value ordered, - mlir::Value resultBox); + mlir::Value identity, mlir::Value ordered, mlir::Value resultBox, + bool argByRef); /// Generate call to `Reduce` intrinsic runtime routine. This is the version /// that does not take a dim argument and return a scalare result. This is used @@ -238,14 +238,14 @@ void genReduce(fir::FirOpBuilder &builder, mlir::Location loc, mlir::Value genReduce(fir::FirOpBuilder &builder, mlir::Location loc, mlir::Value arrayBox, mlir::Value operation, mlir::Value maskBox, mlir::Value identity, - mlir::Value ordered); + mlir::Value ordered, bool argByRef); /// Generate call to `Reduce` intrinsic runtime routine. This is the version /// that takes arrays of any rank with a dim argument specified. void genReduceDim(fir::FirOpBuilder &builder, mlir::Location loc, mlir::Value arrayBox, mlir::Value operation, mlir::Value dim, mlir::Value maskBox, mlir::Value identity, - mlir::Value ordered, mlir::Value resultBox); + mlir::Value ordered, mlir::Value resultBox, bool argByRef); } // namespace fir::runtime diff --git a/flang/lib/Optimizer/Builder/IntrinsicCall.cpp b/flang/lib/Optimizer/Builder/IntrinsicCall.cpp index c438ae1250e45..bc8a1889da74e 100644 --- a/flang/lib/Optimizer/Builder/IntrinsicCall.cpp +++ b/flang/lib/Optimizer/Builder/IntrinsicCall.cpp @@ -5745,6 +5745,14 @@ IntrinsicLibrary::genReduce(mlir::Type resultType, int rank = arrayTmp.rank(); assert(rank >= 1); + // Arguements to the reduction operation are passed by reference or value? + bool argByRef = true; + if (auto embox = + mlir::dyn_cast_or_null<fir::EmboxProcOp>(operation.getDefiningOp())) { + auto fctTy = mlir::dyn_cast<mlir::FunctionType>(embox.getFunc().getType()); + argByRef = mlir::isa<fir::ReferenceType>(fctTy.getInput(0)); + } + mlir::Type ty = array.getType(); mlir::Type arrTy = fir::dyn_cast_ptrOrBoxEleTy(ty); mlir::Type eleTy = mlir::cast<fir::SequenceType>(arrTy).getEleTy(); @@ -5772,7 +5780,7 @@ IntrinsicLibrary::genReduce(mlir::Type resultType, if (fir::isa_complex(eleTy) || fir::isa_derived(eleTy)) { mlir::Value result = builder.createTemporary(loc, eleTy); fir::runtime::genReduce(builder, loc, array, operation, mask, identity, - ordered, result); + ordered, result, argByRef); if (fir::isa_derived(eleTy)) return result; return builder.create<fir::LoadOp>(loc, result); @@ -5789,11 +5797,11 @@ IntrinsicLibrary::genReduce(mlir::Type resultType, charTy.getLen()); fir::CharBoxValue temp = charHelper.createCharacterTemp(eleTy, len); fir::runtime::genReduce(builder, loc, array, operation, mask, identity, - ordered, temp.getBuffer()); + ordered, temp.getBuffer(), argByRef); return temp; } return fir::runtime::genReduce(builder, loc, array, operation, mask, - identity, ordered); + identity, ordered, argByRef); } // Handle cases that have an array result. // Create mutable fir.box to be passed to the runtime for the result. @@ -5804,7 +5812,7 @@ IntrinsicLibrary::genReduce(mlir::Type resultType, fir::factory::getMutableIRBox(builder, loc, resultMutableBox); mlir::Value dim = fir::getBase(args[2]); fir::runtime::genReduceDim(builder, loc, array, operation, dim, mask, - identity, ordered, resultIrBox); + identity, ordered, resultIrBox, argByRef); return readAndAddCleanUp(resultMutableBox, resultType, "REDUCE"); } diff --git a/flang/lib/Optimizer/Builder/Runtime/Reduction.cpp b/flang/lib/Optimizer/Builder/Runtime/Reduction.cpp index c306b50eb5698..18eff93727856 100644 --- a/flang/lib/Optimizer/Builder/Runtime/Reduction.cpp +++ b/flang/lib/Optimizer/Builder/Runtime/Reduction.cpp @@ -476,10 +476,30 @@ struct ForcedReduceReal10 { auto ty = mlir::FloatType::getF80(ctx); auto boxTy = fir::runtime::getModel<const Fortran::runtime::Descriptor &>()(ctx); - auto opTy = mlir::FunctionType::get(ctx, {ty, ty}, ty); + auto refTy = fir::ReferenceType::get(ty); + auto opTy = mlir::FunctionType::get(ctx, {refTy, refTy}, refTy); auto strTy = fir::ReferenceType::get(mlir::IntegerType::get(ctx, 8)); auto intTy = mlir::IntegerType::get(ctx, 8 * sizeof(int)); + auto i1Ty = mlir::IntegerType::get(ctx, 1); + return mlir::FunctionType::get( + ctx, {boxTy, opTy, strTy, intTy, intTy, boxTy, refTy, i1Ty}, {ty}); + }; + } +}; + +/// Placeholder for real*10 version of Reduce Intrinsic +struct ForcedReduceReal10Value { + static constexpr const char *name = + ExpandAndQuoteKey(RTNAME(ReduceReal10Value)); + static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() { + return [](mlir::MLIRContext *ctx) { + auto ty = mlir::FloatType::getF80(ctx); + auto boxTy = + fir::runtime::getModel<const Fortran::runtime::Descriptor &>()(ctx); auto refTy = fir::ReferenceType::get(ty); + auto opTy = mlir::FunctionType::get(ctx, {ty, ty}, refTy); + auto strTy = fir::ReferenceType::get(mlir::IntegerType::get(ctx, 8)); + auto intTy = mlir::IntegerType::get(ctx, 8 * sizeof(int)); auto i1Ty = mlir::IntegerType::get(ctx, 1); return mlir::FunctionType::get( ctx, {boxTy, opTy, strTy, intTy, intTy, boxTy, refTy, i1Ty}, {ty}); @@ -496,10 +516,30 @@ struct ForcedReduceReal16 { auto ty = mlir::FloatType::getF128(ctx); auto boxTy = fir::runtime::getModel<const Fortran::runtime::Descriptor &>()(ctx); - auto opTy = mlir::FunctionType::get(ctx, {ty, ty}, ty); + auto refTy = fir::ReferenceType::get(ty); + auto opTy = mlir::FunctionType::get(ctx, {refTy, refTy}, refTy); auto strTy = fir::ReferenceType::get(mlir::IntegerType::get(ctx, 8)); auto intTy = mlir::IntegerType::get(ctx, 8 * sizeof(int)); + auto i1Ty = mlir::IntegerType::get(ctx, 1); + return mlir::FunctionType::get( + ctx, {boxTy, opTy, strTy, intTy, intTy, boxTy, refTy, i1Ty}, {ty}); + }; + } +}; + +/// Placeholder for real*16 version of Reduce Intrinsic +struct ForcedReduceReal16Value { + static constexpr const char *name = + ExpandAndQuoteKey(RTNAME(ReduceReal16Value)); + static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() { + return [](mlir::MLIRContext *ctx) { + auto ty = mlir::FloatType::getF128(ctx); + auto boxTy = + fir::runtime::getModel<const Fortran::runtime::Descriptor &>()(ctx); auto refTy = fir::ReferenceType::get(ty); + auto opTy = mlir::FunctionType::get(ctx, {ty, ty}, refTy); + auto strTy = fir::ReferenceType::get(mlir::IntegerType::get(ctx, 8)); + auto intTy = mlir::IntegerType::get(ctx, 8 * sizeof(int)); auto i1Ty = mlir::IntegerType::get(ctx, 1); return mlir::FunctionType::get( ctx, {boxTy, opTy, strTy, intTy, intTy, boxTy, refTy, i1Ty}, {ty}); @@ -516,10 +556,32 @@ struct ForcedReduceReal10Dim { auto ty = mlir::FloatType::getF80(ctx); auto boxTy = fir::runtime::getModel<const Fortran::runtime::Descriptor &>()(ctx); - auto opTy = mlir::FunctionType::get(ctx, {ty, ty}, ty); + auto refTy = fir::ReferenceType::get(ty); + auto opTy = mlir::FunctionType::get(ctx, {refTy, refTy}, refTy); auto strTy = fir::ReferenceType::get(mlir::IntegerType::get(ctx, 8)); auto intTy = mlir::IntegerType::get(ctx, 8 * sizeof(int)); + auto refBoxTy = fir::ReferenceType::get(boxTy); + auto i1Ty = mlir::IntegerType::get(ctx, 1); + return mlir::FunctionType::get( + ctx, {refBoxTy, boxTy, opTy, strTy, intTy, intTy, boxTy, refTy, i1Ty}, + {}); + }; + } +}; + +/// Placeholder for DIM real*10 with value version of Reduce Intrinsic +struct ForcedReduceReal10DimValue { + static constexpr const char *name = + ExpandAndQuoteKey(RTNAME(ReduceReal10DimValue)); + static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() { + return [](mlir::MLIRContext *ctx) { + auto ty = mlir::FloatType::getF80(ctx); + auto boxTy = + fir::runtime::getModel<const Fortran::runtime::Descriptor &>()(ctx); auto refTy = fir::ReferenceType::get(ty); + auto opTy = mlir::FunctionType::get(ctx, {ty, ty}, refTy); + auto strTy = fir::ReferenceType::get(mlir::IntegerType::get(ctx, 8)); + auto intTy = mlir::IntegerType::get(ctx, 8 * sizeof(int)); auto refBoxTy = fir::ReferenceType::get(boxTy); auto i1Ty = mlir::IntegerType::get(ctx, 1); return mlir::FunctionType::get( @@ -538,10 +600,32 @@ struct ForcedReduceReal16Dim { auto ty = mlir::FloatType::getF128(ctx); auto boxTy = fir::runtime::getModel<const Fortran::runtime::Descriptor &>()(ctx); - auto opTy = mlir::FunctionType::get(ctx, {ty, ty}, ty); + auto refTy = fir::ReferenceType::get(ty); + auto opTy = mlir::FunctionType::get(ctx, {refTy, refTy}, refTy); auto strTy = fir::ReferenceType::get(mlir::IntegerType::get(ctx, 8)); auto intTy = mlir::IntegerType::get(ctx, 8 * sizeof(int)); + auto refBoxTy = fir::ReferenceType::get(boxTy); + auto i1Ty = mlir::IntegerType::get(ctx, 1); + return mlir::FunctionType::get( + ctx, {refBoxTy, boxTy, opTy, strTy, intTy, intTy, boxTy, refTy, i1Ty}, + {}); + }; + } +}; + +/// Placeholder for DIM real*16 with value version of Reduce Intrinsic +struct ForcedReduceReal16DimValue { + static constexpr const char *name = + ExpandAndQuoteKey(RTNAME(ReduceReal16DimValue)); + static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() { + return [](mlir::MLIRContext *ctx) { + auto ty = mlir::FloatType::getF128(ctx); + auto boxTy = + fir::runtime::getModel<const Fortran::runtime::Descriptor &>()(ctx); auto refTy = fir::ReferenceType::get(ty); + auto opTy = mlir::FunctionType::get(ctx, {ty, ty}, refTy); + auto strTy = fir::ReferenceType::get(mlir::IntegerType::get(ctx, 8)); + auto intTy = mlir::IntegerType::get(ctx, 8 * sizeof(int)); auto refBoxTy = fir::ReferenceType::get(boxTy); auto i1Ty = mlir::IntegerType::get(ctx, 1); return mlir::FunctionType::get( @@ -560,10 +644,30 @@ struct ForcedReduceInteger16 { auto ty = mlir::IntegerType::get(ctx, 128); auto boxTy = fir::runtime::getModel<const Fortran::runtime::Descriptor &>()(ctx); - auto opTy = mlir::FunctionType::get(ctx, {ty, ty}, ty); + auto refTy = fir::ReferenceType::get(ty); + auto opTy = mlir::FunctionType::get(ctx, {refTy, refTy}, refTy); auto strTy = fir::ReferenceType::get(mlir::IntegerType::get(ctx, 8)); auto intTy = mlir::IntegerType::get(ctx, 8 * sizeof(int)); + auto i1Ty = mlir::IntegerType::get(ctx, 1); + return mlir::FunctionType::get( + ctx, {boxTy, opTy, strTy, intTy, intTy, boxTy, refTy, i1Ty}, {ty}); + }; + } +}; + +/// Placeholder for integer*16 with value version of Reduce Intrinsic +struct ForcedReduceInteger16Value { + static constexpr const char *name = + ExpandAndQuoteKey(RTNAME(ReduceInteger16Value)); + static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() { + return [](mlir::MLIRContext *ctx) { + auto ty = mlir::IntegerType::get(ctx, 128); + auto boxTy = + fir::runtime::getModel<const Fortran::runtime::Descriptor &>()(ctx); auto refTy = fir::ReferenceType::get(ty); + auto opTy = mlir::FunctionType::get(ctx, {ty, ty}, refTy); + auto strTy = fir::ReferenceType::get(mlir::IntegerType::get(ctx, 8)); + auto intTy = mlir::IntegerType::get(ctx, 8 * sizeof(int)); auto i1Ty = mlir::IntegerType::get(ctx, 1); return mlir::FunctionType::get( ctx, {boxTy, opTy, strTy, intTy, intTy, boxTy, refTy, i1Ty}, {ty}); @@ -580,10 +684,32 @@ struct ForcedReduceInteger16Dim { auto ty = mlir::IntegerType::get(ctx, 128); auto boxTy = fir::runtime::getModel<const Fortran::runtime::Descriptor &>()(ctx); - auto opTy = mlir::FunctionType::get(ctx, {ty, ty}, ty); + auto refTy = fir::ReferenceType::get(ty); + auto opTy = mlir::FunctionType::get(ctx, {refTy, refTy}, refTy); auto strTy = fir::ReferenceType::get(mlir::IntegerType::get(ctx, 8)); auto intTy = mlir::IntegerType::get(ctx, 8 * sizeof(int)); + auto refBoxTy = fir::ReferenceType::get(boxTy); + auto i1Ty = mlir::IntegerType::get(ctx, 1); + return mlir::FunctionType::get( + ctx, {refBoxTy, boxTy, opTy, strTy, intTy, intTy, boxTy, refTy, i1Ty}, + {}); + }; + } +}; + +/// Placeholder for DIM integer*16 with value version of Reduce Intrinsic +struct ForcedReduceInteger16DimValue { + static constexpr const char *name = + ExpandAndQuoteKey(RTNAME(ReduceInteger16DimValue)); + static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() { + return [](mlir::MLIRContext *ctx) { + auto ty = mlir::IntegerType::get(ctx, 128); + auto boxTy = + fir::runtime::getModel<const Fortran::runtime::Descriptor &>()(ctx); auto refTy = fir::ReferenceType::get(ty); + auto opTy = mlir::FunctionType::get(ctx, {ty, ty}, refTy); + auto strTy = fir::ReferenceType::get(mlir::IntegerType::get(ctx, 8)); + auto intTy = mlir::IntegerType::get(ctx, 8 * sizeof(int)); auto refBoxTy = fir::ReferenceType::get(boxTy); auto i1Ty = mlir::IntegerType::get(ctx, 1); return mlir::FunctionType::get( @@ -602,10 +728,31 @@ struct ForcedReduceComplex10 { auto ty = mlir::ComplexType::get(mlir::FloatType::getF80(ctx)); auto boxTy = fir::runtime::getModel<const Fortran::runtime::Descriptor &>()(ctx); - auto opTy = mlir::FunctionType::get(ctx, {ty, ty}, ty); + auto refTy = fir::ReferenceType::get(ty); + auto opTy = mlir::FunctionType::get(ctx, {refTy, refTy}, refTy); auto strTy = fir::ReferenceType::get(mlir::IntegerType::get(ctx, 8)); auto intTy = mlir::IntegerType::get(ctx, 8 * sizeof(int)); + auto i1Ty = mlir::IntegerType::get(ctx, 1); + return mlir::FunctionType::get( + ctx, {refTy, boxTy, opTy, strTy, intTy, intTy, boxTy, refTy, i1Ty}, + {}); + }; + } +}; + +/// Placeholder for complex(10) with value version of Reduce Intrinsic +struct ForcedReduceComplex10Value { + static constexpr const char *name = + ExpandAndQuoteKey(RTNAME(CppReduceComplex10Value)); + static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() { + return [](mlir::MLIRContext *ctx) { + auto ty = mlir::ComplexType::get(mlir::FloatType::getF80(ctx)); + auto boxTy = + fir::runtime::getModel<const Fortran::runtime::Descriptor &>()(ctx); auto refTy = fir::ReferenceType::get(ty); + auto opTy = mlir::FunctionType::get(ctx, {ty, ty}, refTy); + auto strTy = fir::ReferenceType::get(mlir::IntegerType::get(ctx, 8)); + auto intTy = mlir::IntegerType::get(ctx, 8 * sizeof(int)); auto i1Ty = mlir::IntegerType::get(ctx, 1); return mlir::FunctionType::get( ctx, {refTy, boxTy, opTy, strTy, intTy, intTy, boxTy, refTy, i1Ty}, @@ -623,10 +770,32 @@ struct ForcedReduceComplex10Dim { auto ty = mlir::ComplexType::get(mlir::FloatType::getF80(ctx)); auto boxTy = fir::runtime::getModel<const Fortran::runtime::Descriptor &>()(ctx); - auto opTy = mlir::FunctionType::get(ctx, {ty, ty}, ty); + auto refTy = fir::ReferenceType::get(ty); + auto opTy = mlir::FunctionType::get(ctx, {refTy, refTy}, refTy); auto strTy = fir::ReferenceType::get(mlir::IntegerType::get(ctx, 8)); auto intTy = mlir::IntegerType::get(ctx, 8 * sizeof(int)); + auto refBoxTy = fir::ReferenceType::get(boxTy); + auto i1Ty = mlir::IntegerType::get(ctx, 1); + return mlir::FunctionType::get( + ctx, {refBoxTy, boxTy, opTy, strTy, intTy, intTy, boxTy, refTy, i1Ty}, + {}); + }; + } +}; + +/// Placeholder for Dim complex(10) with value version of Reduce Intrinsic +struct ForcedReduceComplex10DimValue { + static constexpr const char *name = + ExpandAndQuoteKey(RTNAME(CppReduceComplex10DimValue)); + static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() { + return [](mlir::MLIRContext *ctx) { + auto ty = mlir::ComplexType::get(mlir::FloatType::getF80(ctx)); + auto boxTy = + fir::runtime::getModel<const Fortran::runtime::Descriptor &>()(ctx); auto refTy = fir::ReferenceType::get(ty); + auto opTy = mlir::FunctionType::get(ctx, {ty, ty}, refTy); + auto strTy = fir::ReferenceType::get(mlir::IntegerType::get(ctx, 8)); + auto intTy = mlir::IntegerType::get(ctx, 8 * siz... [truncated] `````````` </details> https://github.com/llvm/llvm-project/pull/95353 _______________________________________________ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits