diff --git a/flang/include/flang/Lower/ComplexExpr.h b/flang/include/flang/Lower/ComplexExpr.h deleted file mode 100644 --- a/flang/include/flang/Lower/ComplexExpr.h +++ /dev/null @@ -1,87 +0,0 @@ -//===-- Lower/ComplexExpr.h -- lowering of complex values -------*- C++ -*-===// -// -// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. -// See https://llvm.org/LICENSE.txt for license information. -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -// -//===----------------------------------------------------------------------===// - -#ifndef FORTRAN_LOWER_COMPLEXEXPR_H -#define FORTRAN_LOWER_COMPLEXEXPR_H - -#include "flang/Optimizer/Builder/FIRBuilder.h" - -namespace Fortran::lower { - -/// Helper to facilitate lowering of COMPLEX manipulations in FIR. -class ComplexExprHelper { -public: - explicit ComplexExprHelper(fir::FirOpBuilder &builder, mlir::Location loc) - : builder(builder), loc(loc) {} - ComplexExprHelper(const ComplexExprHelper &) = delete; - - // The values of part enum members are meaningful for - // InsertValueOp and ExtractValueOp so they are explicit. - enum class Part { Real = 0, Imag = 1 }; - - /// Type helper. Determine the type. Do not create MLIR operations. - mlir::Type getComplexPartType(mlir::Value cplx); - mlir::Type getComplexPartType(mlir::Type complexType); - - /// Complex operation creation helper. They create MLIR operations. - mlir::Value createComplex(fir::KindTy kind, mlir::Value real, - mlir::Value imag); - - /// Create a complex value. - mlir::Value createComplex(mlir::Type complexType, mlir::Value real, - mlir::Value imag); - - mlir::Value extractComplexPart(mlir::Value cplx, bool isImagPart) { - return isImagPart ? extract(cplx) : extract(cplx); - } - - /// Returns (Real, Imag) pair of \p cplx - std::pair extractParts(mlir::Value cplx) { - return {extract(cplx), extract(cplx)}; - } - - mlir::Value insertComplexPart(mlir::Value cplx, mlir::Value part, - bool isImagPart) { - return isImagPart ? insert(cplx, part) - : insert(cplx, part); - } - - mlir::Value createComplexCompare(mlir::Value cplx1, mlir::Value cplx2, - bool eq); - -protected: - template - mlir::Value extract(mlir::Value cplx) { - return builder.create( - loc, getComplexPartType(cplx), cplx, - builder.getArrayAttr({builder.getIntegerAttr( - builder.getIndexType(), static_cast(partId))})); - } - - template - mlir::Value insert(mlir::Value cplx, mlir::Value part) { - return builder.create( - loc, cplx.getType(), cplx, part, - builder.getArrayAttr({builder.getIntegerAttr( - builder.getIndexType(), static_cast(partId))})); - } - - template - mlir::Value createPartId() { - return builder.createIntegerConstant(loc, builder.getIndexType(), - static_cast(partId)); - } - -private: - fir::FirOpBuilder &builder; - mlir::Location loc; -}; - -} // namespace Fortran::lower - -#endif // FORTRAN_LOWER_COMPLEXEXPR_H diff --git a/flang/lib/Lower/CMakeLists.txt b/flang/lib/Lower/CMakeLists.txt --- a/flang/lib/Lower/CMakeLists.txt +++ b/flang/lib/Lower/CMakeLists.txt @@ -4,7 +4,6 @@ CharacterExpr.cpp CharacterRuntime.cpp Coarray.cpp - ComplexExpr.cpp ConvertType.cpp IntrinsicCall.cpp IO.cpp diff --git a/flang/lib/Lower/ComplexExpr.cpp b/flang/lib/Lower/ComplexExpr.cpp deleted file mode 100644 --- a/flang/lib/Lower/ComplexExpr.cpp +++ /dev/null @@ -1,60 +0,0 @@ -//===-- ComplexExpr.cpp ---------------------------------------------------===// -// -// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. -// See https://llvm.org/LICENSE.txt for license information. -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -// -//===----------------------------------------------------------------------===// - -#include "flang/Lower/ComplexExpr.h" -#include "flang/Lower/ConvertType.h" - -//===----------------------------------------------------------------------===// -// ComplexExprHelper implementation -//===----------------------------------------------------------------------===// - -mlir::Type -Fortran::lower::ComplexExprHelper::getComplexPartType(mlir::Type complexType) { - return Fortran::lower::convertReal( - builder.getContext(), complexType.cast().getFKind()); -} - -mlir::Type -Fortran::lower::ComplexExprHelper::getComplexPartType(mlir::Value cplx) { - return getComplexPartType(cplx.getType()); -} - -mlir::Value Fortran::lower::ComplexExprHelper::createComplex(fir::KindTy kind, - mlir::Value real, - mlir::Value imag) { - auto complexTy = fir::ComplexType::get(builder.getContext(), kind); - mlir::Value und = builder.create(loc, complexTy); - return insert(insert(und, real), imag); -} - -mlir::Value Fortran::lower::ComplexExprHelper::createComplex(mlir::Type cplxTy, - mlir::Value real, - mlir::Value imag) { - mlir::Value und = builder.create(loc, cplxTy); - return insert(insert(und, real), imag); -} - -mlir::Value Fortran::lower::ComplexExprHelper::createComplexCompare( - mlir::Value cplx1, mlir::Value cplx2, bool eq) { - auto real1 = extract(cplx1); - auto real2 = extract(cplx2); - auto imag1 = extract(cplx1); - auto imag2 = extract(cplx2); - - mlir::arith::CmpFPredicate predicate = - eq ? mlir::arith::CmpFPredicate::UEQ : mlir::arith::CmpFPredicate::UNE; - mlir::Value realCmp = - builder.create(loc, predicate, real1, real2); - mlir::Value imagCmp = - builder.create(loc, predicate, imag1, imag2); - - return eq ? builder.create(loc, realCmp, imagCmp) - .getResult() - : builder.create(loc, realCmp, imagCmp) - .getResult(); -} diff --git a/flang/lib/Lower/IO.cpp b/flang/lib/Lower/IO.cpp --- a/flang/lib/Lower/IO.cpp +++ b/flang/lib/Lower/IO.cpp @@ -10,10 +10,10 @@ #include "RTBuilder.h" #include "flang/Lower/Bridge.h" #include "flang/Lower/CharacterExpr.h" -#include "flang/Lower/ComplexExpr.h" #include "flang/Lower/PFTBuilder.h" #include "flang/Lower/Runtime.h" #include "flang/Lower/Utils.h" +#include "flang/Optimizer/Builder/Complex.h" #include "flang/Optimizer/Builder/FIRBuilder.h" #include "flang/Parser/parse-tree.h" #include "flang/Runtime/io-api.h" @@ -246,8 +246,7 @@ outputFuncArgs.push_back(builder.createConvert( loc, outputFunc.getType().getInput(2), dataLen.second)); } else if (fir::isa_complex(itemType)) { - auto parts = Fortran::lower::ComplexExprHelper{builder, loc}.extractParts( - itemValue); + auto parts = fir::factory::Complex{builder, loc}.extractParts(itemValue); outputFuncArgs.push_back(parts.first); outputFuncArgs.push_back(parts.second); } else { @@ -312,8 +311,7 @@ mlir::Type complexPartType; if (itemType.isa()) complexPartType = builder.getRefType( - Fortran::lower::ComplexExprHelper{builder, loc}.getComplexPartType( - itemType)); + fir::factory::Complex{builder, loc}.getComplexPartType(itemType)); auto complexPartAddr = [&](int index) { return builder.create( loc, complexPartType, originalItemAddr, 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 @@ -17,10 +17,10 @@ #include "RTBuilder.h" #include "flang/Common/static-multimap-view.h" #include "flang/Lower/CharacterExpr.h" -#include "flang/Lower/ComplexExpr.h" #include "flang/Lower/ConvertType.h" #include "flang/Lower/Mangler.h" #include "flang/Lower/Runtime.h" +#include "flang/Optimizer/Builder/Complex.h" #include "flang/Optimizer/Builder/FIRBuilder.h" #include "llvm/Support/CommandLine.h" #include "llvm/Support/ErrorHandling.h" @@ -959,8 +959,7 @@ } if (fir::isa_complex(type)) { // Use HYPOT to fulfill the no underflow/overflow requirement. - auto parts = - Fortran::lower::ComplexExprHelper{builder, loc}.extractParts(arg); + auto parts = fir::factory::Complex{builder, loc}.extractParts(arg); llvm::SmallVector args = {parts.first, parts.second}; return genRuntimeCall("hypot", resultType, args); } @@ -971,7 +970,7 @@ mlir::Value IntrinsicLibrary::genAimag(mlir::Type resultType, llvm::ArrayRef args) { assert(args.size() == 1); - return Fortran::lower::ComplexExprHelper{builder, loc}.extractComplexPart( + return fir::factory::Complex{builder, loc}.extractComplexPart( args[0], true /* isImagPart */); } @@ -1014,11 +1013,10 @@ llvm_unreachable("argument type mismatch"); mlir::Value cplx = args[0]; - auto imag = - Fortran::lower::ComplexExprHelper{builder, loc}.extractComplexPart( - cplx, /*isImagPart=*/true); + auto imag = fir::factory::Complex{builder, loc}.extractComplexPart( + cplx, /*isImagPart=*/true); auto negImag = builder.create(loc, imag); - return Fortran::lower::ComplexExprHelper{builder, loc}.insertComplexPart( + return fir::factory::Complex{builder, loc}.insertComplexPart( cplx, negImag, /*isImagPart=*/true); }