Skip to content

Commit f8ec664

Browse files
committed
move intrinsic call mangling to the bridge for upstreaming
1 parent 0482133 commit f8ec664

File tree

5 files changed

+65
-69
lines changed

5 files changed

+65
-69
lines changed

flang/include/flang/Lower/Mangler.h

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,27 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
#ifndef FORTRAN_LOWER_MANGLER_H_
14-
#define FORTRAN_LOWER_MANGLER_H_
13+
#ifndef FORTRAN_LOWER_MANGLER_H
14+
#define FORTRAN_LOWER_MANGLER_H
1515

16+
#include "mlir/IR/StandardTypes.h"
17+
#include "llvm/ADT/StringRef.h"
1618
#include <string>
1719

1820
namespace fir {
1921
struct NameUniquer;
20-
}
2122

22-
namespace llvm {
23-
class StringRef;
24-
}
23+
/// Returns a name suitable to define mlir functions for Fortran intrinsic
24+
/// Procedure. These names are guaranteed to not conflict with user defined
25+
/// procedures. This is needed to implement Fortran generic intrinsics as
26+
/// several mlir functions specialized for the argument types.
27+
/// The result is guaranteed to be distinct for different mlir::FunctionType
28+
/// arguments. The mangling pattern is:
29+
/// fir.<generic name>.<result type>.<arg type>...
30+
/// e.g ACOS(COMPLEX(4)) is mangled as fir.acos.z4.z4
31+
std::string mangleIntrinsicProcedure(llvm::StringRef genericName,
32+
mlir::FunctionType);
33+
} // namespace fir
2534

2635
namespace Fortran {
2736
namespace common {
@@ -45,4 +54,4 @@ std::string demangleName(llvm::StringRef name);
4554
} // namespace lower
4655
} // namespace Fortran
4756

48-
#endif // FORTRAN_LOWER_MANGLER_H_
57+
#endif // FORTRAN_LOWER_MANGLER_H

flang/include/flang/Optimizer/Support/InternalNames.h

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@
1414
#include "llvm/ADT/StringRef.h"
1515
#include <cstdint>
1616

17-
namespace mlir {
18-
class FunctionType;
19-
}
20-
2117
namespace fir {
2218

2319
/// Internal name mangling of identifiers
@@ -127,17 +123,6 @@ struct NameUniquer {
127123
std::string toLower(llvm::StringRef name);
128124
};
129125

130-
/// Returns a name suitable to define mlir functions for Fortran intrinsic
131-
/// Procedure. These names are guaranteed to not conflict with user defined
132-
/// procedures. This is needed to implement Fortran generic intrinsics as
133-
/// several mlir functions specialized for the argument types.
134-
/// The result is guaranteed to be distinct for different mlir::FunctionType
135-
/// arguments. The mangling pattern is:
136-
/// fir.<generic name>.<result type>.<arg type>...
137-
/// e.g ACOS(COMPLEX(4)) is mangled as fir.acos.z4.z4
138-
std::string mangleIntrinsicProcedure(llvm::StringRef genericName,
139-
mlir::FunctionType);
140-
141126
} // namespace fir
142127

143128
#endif // OPTIMIZER_SUPPORT_INTERNALNAMES_H

flang/lib/Lower/IntrinsicCall.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
#include "flang/Lower/ComplexExpr.h"
2020
#include "flang/Lower/ConvertType.h"
2121
#include "flang/Lower/FIRBuilder.h"
22+
#include "flang/Lower/Mangler.h"
2223
#include "flang/Lower/Runtime.h"
23-
#include "flang/Optimizer/Support/InternalNames.h"
2424
#include "llvm/Support/CommandLine.h"
2525
#include "llvm/Support/ErrorHandling.h"
2626
#include <algorithm>

flang/lib/Lower/Mangler.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@
99
#include "flang/Lower/Mangler.h"
1010
#include "flang/Common/reference.h"
1111
#include "flang/Lower/Utils.h"
12+
#include "flang/Optimizer/Dialect/FIRType.h"
1213
#include "flang/Optimizer/Support/InternalNames.h"
1314
#include "flang/Semantics/tools.h"
1415
#include "llvm/ADT/ArrayRef.h"
1516
#include "llvm/ADT/Optional.h"
1617
#include "llvm/ADT/SmallVector.h"
18+
#include "llvm/ADT/StringRef.h"
1719
#include "llvm/ADT/Twine.h"
1820

1921
// recursively build the vector of module scopes
@@ -118,3 +120,49 @@ std::string Fortran::lower::mangle::demangleName(llvm::StringRef name) {
118120
auto result = fir::NameUniquer::deconstruct(name);
119121
return result.second.name;
120122
}
123+
124+
//===----------------------------------------------------------------------===//
125+
// Intrinsic Procedure Mangling
126+
//===----------------------------------------------------------------------===//
127+
128+
/// Helper to encode type into string for intrinsic procedure names.
129+
/// Note: mlir has Type::dump(ostream) methods but it may add "!" that is not
130+
/// suitable for function names.
131+
static std::string typeToString(mlir::Type t) {
132+
if (auto refT{t.dyn_cast<fir::ReferenceType>()})
133+
return "ref_" + typeToString(refT.getEleTy());
134+
if (auto i{t.dyn_cast<mlir::IntegerType>()}) {
135+
return "i" + std::to_string(i.getWidth());
136+
}
137+
if (auto cplx{t.dyn_cast<fir::CplxType>()}) {
138+
return "z" + std::to_string(cplx.getFKind());
139+
}
140+
if (auto real{t.dyn_cast<fir::RealType>()}) {
141+
return "r" + std::to_string(real.getFKind());
142+
}
143+
if (auto f{t.dyn_cast<mlir::FloatType>()}) {
144+
return "f" + std::to_string(f.getWidth());
145+
}
146+
if (auto logical{t.dyn_cast<fir::LogicalType>()}) {
147+
return "l" + std::to_string(logical.getFKind());
148+
}
149+
if (auto character{t.dyn_cast<fir::CharacterType>()}) {
150+
return "c" + std::to_string(character.getFKind());
151+
}
152+
if (auto boxCharacter{t.dyn_cast<fir::BoxCharType>()}) {
153+
return "bc" + std::to_string(boxCharacter.getEleTy().getFKind());
154+
}
155+
llvm_unreachable("no mangling for type");
156+
}
157+
158+
std::string fir::mangleIntrinsicProcedure(llvm::StringRef intrinsic,
159+
mlir::FunctionType funTy) {
160+
std::string name = "fir.";
161+
name.append(intrinsic.str()).append(".");
162+
assert(funTy.getNumResults() == 1 && "only function mangling supported");
163+
name.append(typeToString(funTy.getResult(0)));
164+
auto e = funTy.getNumInputs();
165+
for (decltype(e) i = 0; i < e; ++i)
166+
name.append(".").append(typeToString(funTy.getInput(i)));
167+
return name;
168+
}

flang/lib/Optimizer/Support/InternalNames.cpp

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -276,49 +276,3 @@ fir::NameUniquer::deconstruct(llvm::StringRef uniq) {
276276
}
277277
return {NameKind::NOT_UNIQUED, DeconstructedName(uniq)};
278278
}
279-
280-
//===----------------------------------------------------------------------===//
281-
// Intrinsic Procedure Mangling
282-
//===----------------------------------------------------------------------===//
283-
284-
/// Helper to encode type into string for intrinsic procedure names.
285-
/// Note: mlir has Type::dump(ostream) methods but it may add "!" that is not
286-
/// suitable for function names.
287-
static std::string typeToString(mlir::Type t) {
288-
if (auto refT{t.dyn_cast<fir::ReferenceType>()})
289-
return "ref_" + typeToString(refT.getEleTy());
290-
if (auto i{t.dyn_cast<mlir::IntegerType>()}) {
291-
return "i" + std::to_string(i.getWidth());
292-
}
293-
if (auto cplx{t.dyn_cast<fir::CplxType>()}) {
294-
return "z" + std::to_string(cplx.getFKind());
295-
}
296-
if (auto real{t.dyn_cast<fir::RealType>()}) {
297-
return "r" + std::to_string(real.getFKind());
298-
}
299-
if (auto f{t.dyn_cast<mlir::FloatType>()}) {
300-
return "f" + std::to_string(f.getWidth());
301-
}
302-
if (auto logical{t.dyn_cast<fir::LogicalType>()}) {
303-
return "l" + std::to_string(logical.getFKind());
304-
}
305-
if (auto character{t.dyn_cast<fir::CharacterType>()}) {
306-
return "c" + std::to_string(character.getFKind());
307-
}
308-
if (auto boxCharacter{t.dyn_cast<fir::BoxCharType>()}) {
309-
return "bc" + std::to_string(boxCharacter.getEleTy().getFKind());
310-
}
311-
llvm_unreachable("no mangling for type");
312-
}
313-
314-
std::string fir::mangleIntrinsicProcedure(llvm::StringRef intrinsic,
315-
mlir::FunctionType funTy) {
316-
std::string name{"fir." + intrinsic.str() + "."};
317-
assert(funTy.getNumResults() == 1 && "only function mangling supported");
318-
name += typeToString(funTy.getResult(0));
319-
auto e = funTy.getNumInputs();
320-
for (decltype(e) i = 0; i < e; ++i) {
321-
name += "." + typeToString(funTy.getInput(i));
322-
}
323-
return name;
324-
}

0 commit comments

Comments
 (0)