Skip to content

Commit c68d289

Browse files
committed
[flang][fir] Update flang test tool support classes.
This updates the various classes that support the compliation of Fortran. These classes are shared by the test tools. Authors: Eric Schweitz, Sameeran Joshi, et.al. Differential Revision: https://reviews.llvm.org/D97073
1 parent f8c1f3b commit c68d289

File tree

11 files changed

+467
-128
lines changed

11 files changed

+467
-128
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//===-- Optimizer/Support/FIRContext.h --------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// Coding style: https://mlir.llvm.org/getting_started/DeveloperGuide/
10+
//
11+
//===----------------------------------------------------------------------===//
12+
/// Setters and getters for associating context with an instance of a ModuleOp.
13+
/// The context is typically set by the tool and needed in later stages to
14+
/// determine how to correctly generate code.
15+
//===----------------------------------------------------------------------===//
16+
17+
#ifndef FORTRAN_OPTIMIZER_SUPPORT_FIRCONTEXT_H
18+
#define FORTRAN_OPTIMIZER_SUPPORT_FIRCONTEXT_H
19+
20+
#include "llvm/ADT/StringRef.h"
21+
#include "llvm/ADT/Triple.h"
22+
23+
namespace mlir {
24+
class ModuleOp;
25+
}
26+
27+
namespace fir {
28+
class KindMapping;
29+
struct NameUniquer;
30+
31+
/// Set the target triple for the module. `triple` must not be deallocated while
32+
/// module `mod` is still live.
33+
void setTargetTriple(mlir::ModuleOp mod, llvm::StringRef triple);
34+
35+
/// Get the Triple instance from the Module or return the default Triple.
36+
llvm::Triple getTargetTriple(mlir::ModuleOp mod);
37+
38+
/// Set the kind mapping for the module. `kindMap` must not be deallocated while
39+
/// module `mod` is still live.
40+
void setKindMapping(mlir::ModuleOp mod, KindMapping &kindMap);
41+
42+
/// Get the KindMapping instance from the Module. If none was set, returns a
43+
/// default.
44+
KindMapping getKindMapping(mlir::ModuleOp mod);
45+
46+
/// Helper for determining the target from the host, etc. Tools may use this
47+
/// function to provide a consistent interpretation of the `--target=<string>`
48+
/// command-line option.
49+
/// An empty string ("") or "default" will specify that the default triple
50+
/// should be used. "native" will specify that the host machine be used to
51+
/// construct the triple.
52+
std::string determineTargetTriple(llvm::StringRef triple);
53+
54+
} // namespace fir
55+
56+
#endif // FORTRAN_OPTIMIZER_SUPPORT_FIRCONTEXT_H

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

Lines changed: 44 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#ifndef OPTIMIZER_SUPPORT_INTERNALNAMES_H
10-
#define OPTIMIZER_SUPPORT_INTERNALNAMES_H
9+
#ifndef FORTRAN_OPTIMIZER_SUPPORT_INTERNALNAMES_H
10+
#define FORTRAN_OPTIMIZER_SUPPORT_INTERNALNAMES_H
1111

1212
#include "llvm/ADT/ArrayRef.h"
1313
#include "llvm/ADT/Optional.h"
@@ -59,58 +59,58 @@ struct NameUniquer {
5959
llvm::SmallVector<std::int64_t, 4> kinds;
6060
};
6161

62-
NameUniquer() = default;
63-
6462
/// Unique a common block name
65-
std::string doCommonBlock(llvm::StringRef name);
63+
static std::string doCommonBlock(llvm::StringRef name);
6664

6765
/// Unique a block data unit name
68-
std::string doBlockData(llvm::StringRef name);
66+
static std::string doBlockData(llvm::StringRef name);
6967

7068
/// Unique a (global) constant name
71-
std::string doConstant(llvm::ArrayRef<llvm::StringRef> modules,
72-
llvm::Optional<llvm::StringRef> host,
73-
llvm::StringRef name);
69+
static std::string doConstant(llvm::ArrayRef<llvm::StringRef> modules,
70+
llvm::Optional<llvm::StringRef> host,
71+
llvm::StringRef name);
7472

7573
/// Unique a dispatch table name
76-
std::string doDispatchTable(llvm::ArrayRef<llvm::StringRef> modules,
77-
llvm::Optional<llvm::StringRef> host,
78-
llvm::StringRef name,
79-
llvm::ArrayRef<std::int64_t> kinds);
74+
static std::string doDispatchTable(llvm::ArrayRef<llvm::StringRef> modules,
75+
llvm::Optional<llvm::StringRef> host,
76+
llvm::StringRef name,
77+
llvm::ArrayRef<std::int64_t> kinds);
8078

8179
/// Unique a compiler generated name
82-
std::string doGenerated(llvm::StringRef name);
80+
static std::string doGenerated(llvm::StringRef name);
8381

8482
/// Unique an intrinsic type descriptor
85-
std::string doIntrinsicTypeDescriptor(llvm::ArrayRef<llvm::StringRef> modules,
86-
llvm::Optional<llvm::StringRef> host,
87-
IntrinsicType type, std::int64_t kind);
83+
static std::string
84+
doIntrinsicTypeDescriptor(llvm::ArrayRef<llvm::StringRef> modules,
85+
llvm::Optional<llvm::StringRef> host,
86+
IntrinsicType type, std::int64_t kind);
8887

8988
/// Unique a procedure name
90-
std::string doProcedure(llvm::ArrayRef<llvm::StringRef> modules,
91-
llvm::Optional<llvm::StringRef> host,
92-
llvm::StringRef name);
89+
static std::string doProcedure(llvm::ArrayRef<llvm::StringRef> modules,
90+
llvm::Optional<llvm::StringRef> host,
91+
llvm::StringRef name);
9392

9493
/// Unique a derived type name
95-
std::string doType(llvm::ArrayRef<llvm::StringRef> modules,
96-
llvm::Optional<llvm::StringRef> host, llvm::StringRef name,
97-
llvm::ArrayRef<std::int64_t> kinds);
94+
static std::string doType(llvm::ArrayRef<llvm::StringRef> modules,
95+
llvm::Optional<llvm::StringRef> host,
96+
llvm::StringRef name,
97+
llvm::ArrayRef<std::int64_t> kinds);
9898

9999
/// Unique a (derived) type descriptor name
100-
std::string doTypeDescriptor(llvm::ArrayRef<llvm::StringRef> modules,
101-
llvm::Optional<llvm::StringRef> host,
102-
llvm::StringRef name,
103-
llvm::ArrayRef<std::int64_t> kinds);
104-
std::string doTypeDescriptor(llvm::ArrayRef<std::string> modules,
105-
llvm::Optional<std::string> host,
106-
llvm::StringRef name,
107-
llvm::ArrayRef<std::int64_t> kinds);
100+
static std::string doTypeDescriptor(llvm::ArrayRef<llvm::StringRef> modules,
101+
llvm::Optional<llvm::StringRef> host,
102+
llvm::StringRef name,
103+
llvm::ArrayRef<std::int64_t> kinds);
104+
static std::string doTypeDescriptor(llvm::ArrayRef<std::string> modules,
105+
llvm::Optional<std::string> host,
106+
llvm::StringRef name,
107+
llvm::ArrayRef<std::int64_t> kinds);
108108

109109
/// Unique a (global) variable name. A variable with save attribute
110110
/// defined inside a subprogram also needs to be handled here
111-
std::string doVariable(llvm::ArrayRef<llvm::StringRef> modules,
112-
llvm::Optional<llvm::StringRef> host,
113-
llvm::StringRef name);
111+
static std::string doVariable(llvm::ArrayRef<llvm::StringRef> modules,
112+
llvm::Optional<llvm::StringRef> host,
113+
llvm::StringRef name);
114114

115115
/// Entry point for the PROGRAM (called by the runtime)
116116
/// Can be overridden with the `--main-entry-name=<name>` option.
@@ -121,12 +121,17 @@ struct NameUniquer {
121121
deconstruct(llvm::StringRef uniquedName);
122122

123123
private:
124-
std::string intAsString(std::int64_t i);
125-
std::string doKind(std::int64_t kind);
126-
std::string doKinds(llvm::ArrayRef<std::int64_t> kinds);
127-
std::string toLower(llvm::StringRef name);
124+
static std::string intAsString(std::int64_t i);
125+
static std::string doKind(std::int64_t kind);
126+
static std::string doKinds(llvm::ArrayRef<std::int64_t> kinds);
127+
static std::string toLower(llvm::StringRef name);
128+
129+
NameUniquer() = delete;
130+
NameUniquer(const NameUniquer &) = delete;
131+
NameUniquer(NameUniquer &&) = delete;
132+
NameUniquer &operator=(const NameUniquer &) = delete;
128133
};
129134

130135
} // namespace fir
131136

132-
#endif // OPTIMIZER_SUPPORT_INTERNALNAMES_H
137+
#endif // FORTRAN_OPTIMIZER_SUPPORT_INTERNALNAMES_H

flang/include/flang/Optimizer/Support/KindMapping.h

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

13-
#ifndef OPTIMIZER_SUPPORT_KINDMAPPING_H
14-
#define OPTIMIZER_SUPPORT_KINDMAPPING_H
13+
#ifndef FORTRAN_OPTIMIZER_SUPPORT_KINDMAPPING_H
14+
#define FORTRAN_OPTIMIZER_SUPPORT_KINDMAPPING_H
1515

1616
#include "mlir/IR/OpDefinition.h"
1717
#include "llvm/ADT/DenseMap.h"
@@ -52,15 +52,24 @@ class KindMapping {
5252
using LLVMTypeID = llvm::Type::TypeID;
5353
using MatchResult = mlir::ParseResult;
5454

55-
/// KindMapping constructors take an optional `defs` argument to specify the
55+
/// KindMapping constructor with both the kind map and default kinds read from
56+
/// command-line options.
57+
explicit KindMapping(mlir::MLIRContext *context);
58+
/// KindMapping constructor taking a `defs` argument to specify the default
59+
/// kinds for intrinsic types. To set the default kinds, an ArrayRef of 6
60+
/// KindTy must be passed. The kinds must be the given in the following order:
61+
/// CHARACTER, COMPLEX, DOUBLE PRECISION, INTEGER, LOGICAL, and REAL. The
62+
/// kind map is read from command-line options, if given.
63+
explicit KindMapping(mlir::MLIRContext *context, llvm::ArrayRef<KindTy> defs);
64+
/// KindMapping constructor taking an optional `defs` argument to specify the
5665
/// default kinds for intrinsic types. To set the default kinds, an ArrayRef
5766
/// of 6 KindTy must be passed. The kinds must be the given in the following
5867
/// order: CHARACTER, COMPLEX, DOUBLE PRECISION, INTEGER, LOGICAL, and REAL.
59-
/// If `defs` is not specified, default default kinds will be used.
60-
explicit KindMapping(mlir::MLIRContext *context,
61-
llvm::ArrayRef<KindTy> defs = llvm::None);
6268
explicit KindMapping(mlir::MLIRContext *context, llvm::StringRef map,
6369
llvm::ArrayRef<KindTy> defs = llvm::None);
70+
explicit KindMapping(mlir::MLIRContext *context, llvm::StringRef map,
71+
llvm::StringRef defs)
72+
: KindMapping{context, map, toDefaultKinds(defs)} {}
6473

6574
/// Get the size in bits of !fir.char<kind>
6675
Bitsize getCharacterBitsize(KindTy kind) const;
@@ -85,6 +94,12 @@ class KindMapping {
8594
/// Get the float semantics of !fir.real<kind>
8695
const llvm::fltSemantics &getFloatSemantics(KindTy kind) const;
8796

97+
/// Get the default kind map as a string.
98+
static constexpr const char *getDefaultMap() { return ""; }
99+
100+
/// Convert the current kind map to a string.
101+
std::string mapToString() const;
102+
88103
//===--------------------------------------------------------------------===//
89104
// Default kinds of intrinsic types
90105
//===--------------------------------------------------------------------===//
@@ -96,6 +111,16 @@ class KindMapping {
96111
KindTy defaultLogicalKind() const;
97112
KindTy defaultRealKind() const;
98113

114+
/// Get the default kinds as a string.
115+
static constexpr const char *getDefaultKinds() { return "a1c4d8i4l4r4"; }
116+
117+
/// Convert the current default kinds to a string.
118+
std::string defaultsToString() const;
119+
120+
/// Translate a default kinds string into a default kind vector. This vector
121+
/// can be passed to the KindMapping ctor.
122+
static std::vector<KindTy> toDefaultKinds(llvm::StringRef defs);
123+
99124
private:
100125
MatchResult badMapString(const llvm::Twine &ptr);
101126
MatchResult parse(llvm::StringRef kindMap);
@@ -109,4 +134,4 @@ class KindMapping {
109134

110135
} // namespace fir
111136

112-
#endif // OPTIMIZER_SUPPORT_KINDMAPPING_H
137+
#endif // FORTRAN_OPTIMIZER_SUPPORT_KINDMAPPING_H

flang/lib/Optimizer/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ add_flang_library(FIROptimizer
66
Dialect/FIROps.cpp
77
Dialect/FIRType.cpp
88

9+
Support/FIRContext.cpp
910
Support/InternalNames.cpp
1011
Support/KindMapping.cpp
1112

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
//===-- FIRContext.cpp ----------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// Coding style: https://mlir.llvm.org/getting_started/DeveloperGuide/
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "flang/Optimizer/Support/FIRContext.h"
14+
#include "flang/Optimizer/Support/KindMapping.h"
15+
#include "mlir/IR/BuiltinAttributes.h"
16+
#include "mlir/IR/BuiltinOps.h"
17+
#include "llvm/Support/Host.h"
18+
19+
static constexpr const char *tripleName = "fir.triple";
20+
21+
void fir::setTargetTriple(mlir::ModuleOp mod, llvm::StringRef triple) {
22+
auto target = fir::determineTargetTriple(triple);
23+
mod->setAttr(tripleName, mlir::StringAttr::get(mod.getContext(), target));
24+
}
25+
26+
llvm::Triple fir::getTargetTriple(mlir::ModuleOp mod) {
27+
if (auto target = mod->getAttrOfType<mlir::StringAttr>(tripleName))
28+
return llvm::Triple(target.getValue());
29+
return llvm::Triple(llvm::sys::getDefaultTargetTriple());
30+
}
31+
32+
static constexpr const char *kindMapName = "fir.kindmap";
33+
static constexpr const char *defKindName = "fir.defaultkind";
34+
35+
void fir::setKindMapping(mlir::ModuleOp mod, fir::KindMapping &kindMap) {
36+
auto ctx = mod.getContext();
37+
mod->setAttr(kindMapName, mlir::StringAttr::get(ctx, kindMap.mapToString()));
38+
auto defs = kindMap.defaultsToString();
39+
mod->setAttr(defKindName, mlir::StringAttr::get(ctx, defs));
40+
}
41+
42+
fir::KindMapping fir::getKindMapping(mlir::ModuleOp mod) {
43+
auto ctx = mod.getContext();
44+
if (auto defs = mod->getAttrOfType<mlir::StringAttr>(defKindName)) {
45+
auto defVals = fir::KindMapping::toDefaultKinds(defs.getValue());
46+
if (auto maps = mod->getAttrOfType<mlir::StringAttr>(kindMapName))
47+
return fir::KindMapping(ctx, maps.getValue(), defVals);
48+
return fir::KindMapping(ctx, defVals);
49+
}
50+
return fir::KindMapping(ctx);
51+
}
52+
53+
std::string fir::determineTargetTriple(llvm::StringRef triple) {
54+
// Treat "" or "default" as stand-ins for the default machine.
55+
if (triple.empty() || triple == "default")
56+
return llvm::sys::getDefaultTargetTriple();
57+
// Treat "native" as stand-in for the host machine.
58+
if (triple == "native")
59+
return llvm::sys::getProcessTriple();
60+
// TODO: normalize the triple?
61+
return triple.str();
62+
}

flang/lib/Optimizer/Support/InternalNames.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66
//
77
//===----------------------------------------------------------------------===//
8+
//
9+
// Coding style: https://mlir.llvm.org/getting_started/DeveloperGuide/
10+
//
11+
//===----------------------------------------------------------------------===//
812

913
#include "flang/Optimizer/Support/InternalNames.h"
1014
#include "flang/Optimizer/Dialect/FIRType.h"

0 commit comments

Comments
 (0)