diff --git a/flang/include/flang/Lower/Mangler.h b/flang/include/flang/Lower/Mangler.h --- a/flang/include/flang/Lower/Mangler.h +++ b/flang/include/flang/Lower/Mangler.h @@ -19,20 +19,6 @@ #include "llvm/ADT/StringRef.h" #include -namespace fir { - -/// Returns a name suitable to define mlir functions for Fortran intrinsic -/// Procedure. These names are guaranteed to not conflict with user defined -/// procedures. This is needed to implement Fortran generic intrinsics as -/// several mlir functions specialized for the argument types. -/// The result is guaranteed to be distinct for different mlir::FunctionType -/// arguments. The mangling pattern is: -/// fir...... -/// e.g ACOS(COMPLEX(4)) is mangled as fir.acos.z4.z4 -std::string mangleIntrinsicProcedure(llvm::StringRef genericName, - mlir::FunctionType); -} // namespace fir - namespace Fortran { namespace common { template 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 @@ -15,7 +15,6 @@ #include "flang/Lower/IntrinsicCall.h" #include "flang/Common/static-multimap-view.h" -#include "flang/Lower/Mangler.h" #include "flang/Optimizer/Builder/BoxValue.h" #include "flang/Optimizer/Builder/Character.h" #include "flang/Optimizer/Builder/Complex.h" @@ -1814,12 +1813,66 @@ return {}; } +//===----------------------------------------------------------------------===// +// Intrinsic Procedure Mangling +//===----------------------------------------------------------------------===// + +/// Helper to encode type into string for intrinsic procedure names. +/// Note: mlir has Type::dump(ostream) methods but it may add "!" that is not +/// suitable for function names. +static std::string typeToString(mlir::Type t) { + if (auto refT{t.dyn_cast()}) + return "ref_" + typeToString(refT.getEleTy()); + if (auto i{t.dyn_cast()}) { + return "i" + std::to_string(i.getWidth()); + } + if (auto cplx{t.dyn_cast()}) { + return "z" + std::to_string(cplx.getFKind()); + } + if (auto real{t.dyn_cast()}) { + return "r" + std::to_string(real.getFKind()); + } + if (auto f{t.dyn_cast()}) { + return "f" + std::to_string(f.getWidth()); + } + if (auto logical{t.dyn_cast()}) { + return "l" + std::to_string(logical.getFKind()); + } + if (auto character{t.dyn_cast()}) { + return "c" + std::to_string(character.getFKind()); + } + if (auto boxCharacter{t.dyn_cast()}) { + return "bc" + std::to_string(boxCharacter.getEleTy().getFKind()); + } + llvm_unreachable("no mangling for type"); +} + +/// Returns a name suitable to define mlir functions for Fortran intrinsic +/// Procedure. These names are guaranteed to not conflict with user defined +/// procedures. This is needed to implement Fortran generic intrinsics as +/// several mlir functions specialized for the argument types. +/// The result is guaranteed to be distinct for different mlir::FunctionType +/// arguments. The mangling pattern is: +/// fir...... +/// e.g ACOS(COMPLEX(4)) is mangled as fir.acos.z4.z4 +static std::string mangleIntrinsicProcedure(llvm::StringRef intrinsic, + mlir::FunctionType funTy) { + std::string name = "fir."; + name.append(intrinsic.str()).append("."); + assert(funTy.getNumResults() == 1 && "only function mangling supported"); + name.append(typeToString(funTy.getResult(0))); + unsigned e = funTy.getNumInputs(); + for (decltype(e) i = 0; i < e; ++i) + name.append(".").append(typeToString(funTy.getInput(i))); + return name; +} + template mlir::func::FuncOp IntrinsicLibrary::getWrapper(GeneratorType generator, llvm::StringRef name, mlir::FunctionType funcType, bool loadRefArguments) { - std::string wrapperName = fir::mangleIntrinsicProcedure(name, funcType); + std::string wrapperName = mangleIntrinsicProcedure(name, funcType); mlir::func::FuncOp function = builder.getNamedFunction(wrapperName); if (!function) { // First time this wrapper is needed, build it. diff --git a/flang/lib/Lower/Mangler.cpp b/flang/lib/Lower/Mangler.cpp --- a/flang/lib/Lower/Mangler.cpp +++ b/flang/lib/Lower/Mangler.cpp @@ -231,52 +231,6 @@ return name += hashString.c_str(); } -//===----------------------------------------------------------------------===// -// Intrinsic Procedure Mangling -//===----------------------------------------------------------------------===// - -/// Helper to encode type into string for intrinsic procedure names. -/// Note: mlir has Type::dump(ostream) methods but it may add "!" that is not -/// suitable for function names. -static std::string typeToString(mlir::Type t) { - if (auto refT{t.dyn_cast()}) - return "ref_" + typeToString(refT.getEleTy()); - if (auto i{t.dyn_cast()}) { - return "i" + std::to_string(i.getWidth()); - } - if (auto cplx{t.dyn_cast()}) { - return "z" + std::to_string(cplx.getFKind()); - } - if (auto real{t.dyn_cast()}) { - return "r" + std::to_string(real.getFKind()); - } - if (auto f{t.dyn_cast()}) { - return "f" + std::to_string(f.getWidth()); - } - if (auto logical{t.dyn_cast()}) { - return "l" + std::to_string(logical.getFKind()); - } - if (auto character{t.dyn_cast()}) { - return "c" + std::to_string(character.getFKind()); - } - if (auto boxCharacter{t.dyn_cast()}) { - return "bc" + std::to_string(boxCharacter.getEleTy().getFKind()); - } - llvm_unreachable("no mangling for type"); -} - -std::string fir::mangleIntrinsicProcedure(llvm::StringRef intrinsic, - mlir::FunctionType funTy) { - std::string name = "fir."; - name.append(intrinsic.str()).append("."); - assert(funTy.getNumResults() == 1 && "only function mangling supported"); - name.append(typeToString(funTy.getResult(0))); - unsigned e = funTy.getNumInputs(); - for (decltype(e) i = 0; i < e; ++i) - name.append(".").append(typeToString(funTy.getInput(i))); - return name; -} - std::string Fortran::lower::mangle::globalNamelistDescriptorName( const Fortran::semantics::Symbol &sym) { std::string name = mangleName(sym);