Skip to content

Commit 9fc5095

Browse files
committed
- thread default kinds to code gen
- fix some bugs - some work on furthering support of descriptors for "F77" I/O
1 parent 30308c0 commit 9fc5095

File tree

9 files changed

+262
-64
lines changed

9 files changed

+262
-64
lines changed

flang/include/flang/Lower/Bridge.h

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
namespace fir {
2222
struct NameUniquer;
2323
}
24+
namespace llvm {
25+
class Triple;
26+
}
2427

2528
namespace Fortran {
2629
namespace common {
@@ -52,8 +55,11 @@ class LoweringBridge {
5255
create(mlir::MLIRContext &ctx,
5356
const Fortran::common::IntrinsicTypeDefaultKinds &defaultKinds,
5457
const Fortran::evaluate::IntrinsicProcTable &intrinsics,
55-
const Fortran::parser::AllCookedSources &allCooked) {
56-
return LoweringBridge{defaultKinds, intrinsics, allCooked};
58+
const Fortran::parser::AllCookedSource &allCooked,
59+
llvm::Triple &triple, fir::NameUniquer &uniquer,
60+
fir::KindMapping &kindMap) {
61+
return LoweringBridge(ctx, defaultKinds, intrinsics, allCooked, triple,
62+
uniquer, kindMap);
5763
}
5864

5965
//===--------------------------------------------------------------------===//
@@ -89,15 +95,16 @@ class LoweringBridge {
8995
void parseSourceFile(llvm::SourceMgr &);
9096

9197
/// Cross the bridge from the Fortran parse-tree, etc. to MLIR dialects
92-
void lower(const Fortran::parser::Program &program, fir::NameUniquer &uniquer,
98+
void lower(const Fortran::parser::Program &program,
9399
const Fortran::semantics::SemanticsContext &semanticsContext);
94100

95101
private:
96102
explicit LoweringBridge(
97103
mlir::MLIRContext &ctx,
98104
const Fortran::common::IntrinsicTypeDefaultKinds &defaultKinds,
99105
const Fortran::evaluate::IntrinsicProcTable &intrinsics,
100-
const Fortran::parser::AllCookedSources &);
106+
const Fortran::parser::AllCookedSource &cooked, llvm::Triple &triple,
107+
fir::NameUniquer &uniquer, fir::KindMapping &kindMap);
101108
LoweringBridge() = delete;
102109
LoweringBridge(const LoweringBridge &) = delete;
103110

@@ -106,7 +113,7 @@ class LoweringBridge {
106113
const Fortran::parser::AllCookedSources *cooked;
107114
std::unique_ptr<mlir::MLIRContext> context;
108115
std::unique_ptr<mlir::ModuleOp> module;
109-
fir::KindMapping kindMap;
116+
fir::KindMapping &kindMap;
110117
};
111118

112119
} // namespace lower

flang/include/flang/Lower/CharacterExpr.h

Lines changed: 16 additions & 1 deletion
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
#ifndef FORTRAN_LOWER_CHARACTEREXPR_H
1014
#define FORTRAN_LOWER_CHARACTEREXPR_H
@@ -107,7 +111,10 @@ class CharacterExprHelper {
107111
static bool isCharacter(mlir::Type type);
108112

109113
/// Extract the kind of a character type
110-
static int getCharacterKind(mlir::Type type);
114+
static fir::KindTy getCharacterKind(mlir::Type type);
115+
116+
/// Extract the kind of a character or array of character type.
117+
static fir::KindTy getCharacterOrSequenceKind(mlir::Type type);
111118

112119
/// Determine the base character type
113120
static fir::CharacterType getCharacterType(mlir::Type type);
@@ -130,6 +137,14 @@ class CharacterExprHelper {
130137
fir::ExtendedValue toExtendedValue(mlir::Value character,
131138
mlir::Value len = {});
132139

140+
/// Is `type` a sequence (array) of CHARACTER type? Return true for any of the
141+
/// following cases:
142+
/// - !fir.array<len x dim x ... x !fir.char<kind>>
143+
/// - !fir.array<dim x !fir.char<kind, len>>
144+
/// - !fir.ref<T> where T is either of the first two cases
145+
/// - !fir.box<T> where T is either of the first two cases
146+
static bool isArray(mlir::Type type);
147+
133148
private:
134149
fir::CharBoxValue materializeValue(const fir::CharBoxValue &str);
135150
fir::CharBoxValue toDataLengthPair(mlir::Value character);

flang/include/flang/Optimizer/Dialect/FIRType.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ namespace fir {
3636

3737
class FIROpsDialect;
3838

39-
using KindTy = int;
39+
using KindTy = unsigned;
4040

4141
namespace detail {
4242
struct BoxTypeStorage;

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

Lines changed: 21 additions & 2 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
#ifndef OPTIMIZER_SUPPORT_KINDMAPPING_H
1014
#define OPTIMIZER_SUPPORT_KINDMAPPING_H
@@ -48,8 +52,10 @@ class KindMapping {
4852
using LLVMTypeID = llvm::Type::TypeID;
4953
using MatchResult = mlir::ParseResult;
5054

51-
explicit KindMapping(mlir::MLIRContext *context);
52-
explicit KindMapping(mlir::MLIRContext *context, llvm::StringRef map);
55+
explicit KindMapping(mlir::MLIRContext *context,
56+
llvm::ArrayRef<KindTy> defs = llvm::None);
57+
explicit KindMapping(mlir::MLIRContext *context, llvm::StringRef map,
58+
llvm::ArrayRef<KindTy> defs = llvm::None);
5359

5460
/// Get the size in bits of !fir.char<kind>
5561
Bitsize getCharacterBitsize(KindTy kind) const;
@@ -74,13 +80,26 @@ class KindMapping {
7480
/// Get the float semantics of !fir.real<kind>
7581
const llvm::fltSemantics &getFloatSemantics(KindTy kind) const;
7682

83+
//===--------------------------------------------------------------------===//
84+
// Default kinds of intrinsic types
85+
//===--------------------------------------------------------------------===//
86+
87+
KindTy defaultCharacterKind() const;
88+
KindTy defaultComplexKind() const;
89+
KindTy defaultDoubleKind() const;
90+
KindTy defaultIntegerKind() const;
91+
KindTy defaultLogicalKind() const;
92+
KindTy defaultRealKind() const;
93+
7794
private:
7895
MatchResult badMapString(const llvm::Twine &ptr);
7996
MatchResult parse(llvm::StringRef kindMap);
97+
mlir::LogicalResult setDefaultKinds(llvm::ArrayRef<KindTy> defs);
8098

8199
mlir::MLIRContext *context;
82100
llvm::DenseMap<std::pair<char, KindTy>, Bitsize> intMap;
83101
llvm::DenseMap<std::pair<char, KindTy>, LLVMTypeID> floatMap;
102+
llvm::DenseMap<char, KindTy> defaultMap;
84103
};
85104

86105
} // namespace fir

flang/lib/Lower/Bridge.cpp

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "flang/Optimizer/Dialect/FIRAttr.h"
3030
#include "flang/Optimizer/Dialect/FIRDialect.h"
3131
#include "flang/Optimizer/Dialect/FIROps.h"
32+
#include "flang/Optimizer/Support/FIRContext.h"
3233
#include "flang/Optimizer/Support/InternalNames.h"
3334
#include "flang/Optimizer/Transforms/Passes.h"
3435
#include "flang/Parser/parse-tree.h"
@@ -41,6 +42,7 @@
4142
#include "llvm/Support/CommandLine.h"
4243
#include "llvm/Support/ErrorHandling.h"
4344
#include "llvm/Support/MD5.h"
45+
4446
#define DEBUG_TYPE "flang-lower-bridge"
4547

4648
#undef TODO
@@ -2435,12 +2437,12 @@ Fortran::lower::LoweringBridge::createFoldingContext() const {
24352437
}
24362438

24372439
void Fortran::lower::LoweringBridge::lower(
2438-
const Fortran::parser::Program &prg, fir::NameUniquer &uniquer,
2440+
const Fortran::parser::Program &prg,
24392441
const Fortran::semantics::SemanticsContext &semanticsContext) {
24402442
auto pft = Fortran::lower::createPFT(prg, semanticsContext);
24412443
if (dumpBeforeFir)
24422444
Fortran::lower::dumpPFT(llvm::errs(), *pft);
2443-
FirConverter converter{*this, uniquer};
2445+
FirConverter converter{*this, *fir::getNameUniquer(getModule())};
24442446
converter.run(*pft);
24452447
}
24462448

@@ -2451,12 +2453,14 @@ void Fortran::lower::LoweringBridge::parseSourceFile(llvm::SourceMgr &srcMgr) {
24512453
}
24522454

24532455
Fortran::lower::LoweringBridge::LoweringBridge(
2454-
mlir::MLIRContext &ctx,
2456+
mlir::MLIRContext &context,
24552457
const Fortran::common::IntrinsicTypeDefaultKinds &defaultKinds,
24562458
const Fortran::evaluate::IntrinsicProcTable &intrinsics,
2457-
const Fortran::parser::CookedSource &cooked)
2458-
: defaultKinds{defaultKinds},
2459-
intrinsics{intrinsics}, cooked{&cooked}, context{ctx}, kindMap{&ctx} {
2459+
const Fortran::parser::CookedSource &cooked, llvm::Triple &triple,
2460+
fir::NameUniquer &uniquer, fir::KindMapping &kindMap)
2461+
: defaultKinds{defaultKinds}, intrinsics{intrinsics}, cooked{&cooked},
2462+
context{context}, kindMap{kindMap} {
2463+
// Register the diagnostic handler.
24602464
context.getDiagEngine().registerHandler([](mlir::Diagnostic &diag) {
24612465
auto &os = llvm::errs();
24622466
switch (diag.getSeverity()) {
@@ -2478,6 +2482,12 @@ Fortran::lower::LoweringBridge::LoweringBridge(
24782482
os.flush();
24792483
return mlir::success();
24802484
});
2485+
2486+
// Create the module and attach the attributes.
24812487
module = std::make_unique<mlir::ModuleOp>(
24822488
mlir::ModuleOp::create(mlir::UnknownLoc::get(&context)));
2489+
assert(module.get() && "module was not created");
2490+
fir::setTargetTriple(*module.get(), triple);
2491+
fir::setNameUniquer(*module.get(), uniquer);
2492+
fir::setKindMapping(*module.get(), kindMap);
24832493
}

flang/lib/Lower/CharacterExpr.cpp

Lines changed: 37 additions & 6 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/Lower/CharacterExpr.h"
1014
#include "flang/Lower/ConvertType.h"
@@ -17,22 +21,30 @@
1721
// CharacterExprHelper implementation
1822
//===----------------------------------------------------------------------===//
1923

20-
/// Get fir.char<kind> type with the same kind as inside str.
21-
fir::CharacterType
22-
Fortran::lower::CharacterExprHelper::getCharacterType(mlir::Type type) {
24+
template <bool checkForScalar>
25+
static fir::CharacterType recoverCharacterType(mlir::Type type) {
2326
if (auto boxType = type.dyn_cast<fir::BoxCharType>())
2427
return boxType.getEleTy();
2528
if (auto refType = type.dyn_cast<fir::ReferenceType>())
2629
type = refType.getEleTy();
2730
if (auto seqType = type.dyn_cast<fir::SequenceType>()) {
28-
assert(seqType.getShape().size() == 1 && "rank must be 1");
31+
// In a context where `type` may be a sequence, we want to opt out of this
32+
// assertion by setting `checkForScalar` to `false`.
33+
assert((!checkForScalar || seqType.getShape().size() == 1) &&
34+
"rank must be 1 for a scalar CHARACTER");
2935
type = seqType.getEleTy();
3036
}
3137
if (auto charType = type.dyn_cast<fir::CharacterType>())
3238
return charType;
3339
llvm_unreachable("Invalid character value type");
3440
}
3541

42+
/// Get fir.char<kind> type with the same kind as inside str.
43+
fir::CharacterType
44+
Fortran::lower::CharacterExprHelper::getCharacterType(mlir::Type type) {
45+
return recoverCharacterType<true>(type);
46+
}
47+
3648
fir::CharacterType Fortran::lower::CharacterExprHelper::getCharacterType(
3749
const fir::CharBoxValue &box) {
3850
return getCharacterType(box.getBuffer().getType());
@@ -528,6 +540,25 @@ bool Fortran::lower::CharacterExprHelper::isCharacter(mlir::Type type) {
528540
return type.isa<fir::CharacterType>();
529541
}
530542

531-
int Fortran::lower::CharacterExprHelper::getCharacterKind(mlir::Type type) {
532-
return getCharacterType(type).getFKind();
543+
fir::KindTy
544+
Fortran::lower::CharacterExprHelper::getCharacterKind(mlir::Type type) {
545+
return recoverCharacterType<true>(type).getFKind();
546+
}
547+
548+
fir::KindTy Fortran::lower::CharacterExprHelper::getCharacterOrSequenceKind(
549+
mlir::Type type) {
550+
return recoverCharacterType<false>(type).getFKind();
551+
}
552+
553+
bool Fortran::lower::CharacterExprHelper::isArray(mlir::Type type) {
554+
if (auto boxTy = type.dyn_cast<fir::BoxType>())
555+
type = boxTy.getEleTy();
556+
if (auto eleTy = fir::dyn_cast_ptrEleTy(type))
557+
type = eleTy;
558+
if (auto seqTy = type.dyn_cast<fir::SequenceType>()) {
559+
auto charTy = seqTy.getEleTy().dyn_cast<fir::CharacterType>();
560+
assert(charTy);
561+
return (!charTy.singleton()) || (seqTy.getDimension() > 1);
562+
}
563+
return false;
533564
}

0 commit comments

Comments
 (0)