Skip to content

[flang] Lower REDUCE intrinsic for reduction op with args by value #95353

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions flang/include/flang/Optimizer/Builder/Runtime/RTBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions flang/include/flang/Optimizer/Builder/Runtime/Reduction.h
Original file line number Diff line number Diff line change
Expand Up @@ -229,23 +229,23 @@ 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
/// for REAL, INTEGER and LOGICAL TYPES.
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

Expand Down
16 changes: 12 additions & 4 deletions flang/lib/Optimizer/Builder/IntrinsicCall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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())) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does REDUCE works with dummy procedure and procedure pointers? If so it would be good to add tests for those cases to ensure the pattern matching here works with them.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does REDUCE works with dummy procedure and procedure pointers? If so it would be good to add tests for those cases to ensure the pattern matching here works with them.

I'll check if this is supported and add proper test if it is.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So it works but there is I need to check for the dummy procedure by value because the information is lost in FIR. I'll prepare a follow up patch and we can discuss on it.

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();
Expand Down Expand Up @@ -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);
Expand All @@ -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.
Expand All @@ -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");
}

Expand Down
Loading
Loading