Skip to content

[flang] remove support for std::complex value lowering. #110643

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 3 commits into from
Oct 2, 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
71 changes: 51 additions & 20 deletions flang/include/flang/Optimizer/Builder/Runtime/RTBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -400,17 +400,19 @@ constexpr TypeBuilderFunc getModel<bool &>() {
return fir::ReferenceType::get(f(context));
};
}
template <>
constexpr TypeBuilderFunc getModel<std::complex<float>>() {
return [](mlir::MLIRContext *context) -> mlir::Type {
return mlir::ComplexType::get(mlir::FloatType::getF32(context));
};
}

// getModel<std::complex<T>> are not implemented on purpose.
// Prefer passing/returning the complex by reference in the runtime to
// avoid ABI issues.
// C++ std::complex is not an intrinsic type, and while it is storage
// compatible with C/Fortran complex type, it follows the struct value passing
// ABI rule, which may differ from how C complex are passed on some platforms.

template <>
constexpr TypeBuilderFunc getModel<std::complex<float> &>() {
return [](mlir::MLIRContext *context) -> mlir::Type {
TypeBuilderFunc f{getModel<std::complex<float>>()};
return fir::ReferenceType::get(f(context));
mlir::Type floatTy = getModel<float>()(context);
return fir::ReferenceType::get(mlir::ComplexType::get(floatTy));
};
}
template <>
Expand All @@ -422,16 +424,10 @@ constexpr TypeBuilderFunc getModel<const std::complex<float> *>() {
return getModel<std::complex<float> *>();
}
template <>
constexpr TypeBuilderFunc getModel<std::complex<double>>() {
return [](mlir::MLIRContext *context) -> mlir::Type {
return mlir::ComplexType::get(mlir::FloatType::getF64(context));
};
}
template <>
constexpr TypeBuilderFunc getModel<std::complex<double> &>() {
return [](mlir::MLIRContext *context) -> mlir::Type {
TypeBuilderFunc f{getModel<std::complex<double>>()};
return fir::ReferenceType::get(f(context));
mlir::Type floatTy = getModel<double>()(context);
return fir::ReferenceType::get(mlir::ComplexType::get(floatTy));
};
}
template <>
Expand Down Expand Up @@ -521,10 +517,45 @@ 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>)
// FIXME: the runtime is not using the correct ABIs when calling complex
// callbacks. lowering either need to create wrappers or just have an inline
// implementation for it. https://github.com/llvm/llvm-project/issues/110674
template <>
constexpr TypeBuilderFunc
getModel<Fortran::runtime::ValueReductionOperation<std::complex<float>>>() {
return [](mlir::MLIRContext *context) -> mlir::Type {
mlir::Type cplx = mlir::ComplexType::get(getModel<float>()(context));
auto refTy = fir::ReferenceType::get(cplx);
return mlir::FunctionType::get(context, {cplx, cplx}, refTy);
};
}
template <>
constexpr TypeBuilderFunc
getModel<Fortran::runtime::ValueReductionOperation<std::complex<double>>>() {
return [](mlir::MLIRContext *context) -> mlir::Type {
mlir::Type cplx = mlir::ComplexType::get(getModel<double>()(context));
auto refTy = fir::ReferenceType::get(cplx);
return mlir::FunctionType::get(context, {cplx, cplx}, refTy);
};
}
template <>
constexpr TypeBuilderFunc
getModel<Fortran::runtime::ReferenceReductionOperation<std::complex<float>>>() {
return [](mlir::MLIRContext *context) -> mlir::Type {
mlir::Type cplx = mlir::ComplexType::get(getModel<float>()(context));
auto refTy = fir::ReferenceType::get(cplx);
return mlir::FunctionType::get(context, {refTy, refTy}, refTy);
};
}
template <>
constexpr TypeBuilderFunc getModel<
Fortran::runtime::ReferenceReductionOperation<std::complex<double>>>() {
return [](mlir::MLIRContext *context) -> mlir::Type {
mlir::Type cplx = mlir::ComplexType::get(getModel<double>()(context));
auto refTy = fir::ReferenceType::get(cplx);
return mlir::FunctionType::get(context, {refTy, refTy}, refTy);
};
}

REDUCTION_CHAR_OPERATION_MODEL(char)
REDUCTION_CHAR_OPERATION_MODEL(char16_t)
Expand Down
1 change: 1 addition & 0 deletions flang/unittests/Optimizer/RTBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "flang/Optimizer/Builder/Runtime/RTBuilder.h"
#include "gtest/gtest.h"
#include "flang/Optimizer/Support/InitFIR.h"
#include <complex>

// Check that it is possible to make a difference between complex runtime
// function using C99 complex and C++ std::complex. This is important since
Expand Down
Loading