Skip to content

Commit 9ec3a0e

Browse files
authored
Merge pull request #999 from flang-compiler/jpr-move-builder-in-optimizer-5
[NFC] Move FirOpBuilder, ExtendedValue and tools into Optimizer/Builder
2 parents 34bba44 + 96658e3 commit 9ec3a0e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+2069
-2053
lines changed

flang/docs/tutorials/addingIntrinsics.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -320,14 +320,14 @@ it can be deallocated after the statement.
320320
The implementation of the runtime call in lowering looks like:
321321

322322
```c++
323-
void Fortran::lower::genTrim(Fortran::lower::FirOpBuilder &builder,
323+
void Fortran::lower::genTrim(fir::FirOpBuilder &builder,
324324
mlir::Location loc, mlir::Value resultBox,
325325
mlir::Value stringBox) {
326326
auto trimFunc = getRuntimeFunc<mkRTKey(Trim)>(loc, builder);
327327
auto fTy = trimFunc.getType();
328-
auto sourceFile = Fortran::lower::locationToFilename(builder, loc);
328+
auto sourceFile = fir::factory::locationToFilename(builder, loc);
329329
auto sourceLine =
330-
Fortran::lower::locationToLineNo(builder, loc, fTy.getInput(3));
330+
fir::factory::locationToLineNo(builder, loc, fTy.getInput(3));
331331

332332
llvm::SmallVector<mlir::Value> args;
333333
args.emplace_back(builder.createConvert(loc, fTy.getInput(0), resultBox));

flang/include/flang/Lower/AbstractConverter.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@
1515

1616
#include "flang/Common/Fortran.h"
1717
#include "flang/Lower/PFTDefs.h"
18-
#include "flang/Lower/Support/BoxValue.h"
18+
#include "flang/Optimizer/Builder/BoxValue.h"
1919
#include "mlir/IR/BuiltinOps.h"
2020
#include "llvm/ADT/ArrayRef.h"
2121

2222
namespace fir {
2323
class KindMapping;
24-
}
24+
class FirOpBuilder;
25+
} // namespace fir
2526
namespace Fortran {
2627
namespace common {
2728
template <typename>
@@ -50,7 +51,6 @@ struct Variable;
5051

5152
using SomeExpr = Fortran::evaluate::Expr<Fortran::evaluate::SomeType>;
5253
using SymbolRef = Fortran::common::Reference<const Fortran::semantics::Symbol>;
53-
class FirOpBuilder;
5454
class StatementContext;
5555

5656
//===----------------------------------------------------------------------===//
@@ -87,9 +87,10 @@ class AbstractConverter {
8787
/// Get the code defined by a label
8888
virtual pft::Evaluation *lookupLabel(pft::Label label) = 0;
8989

90-
/// For a give symbol which is host-associated, create a clone using parameters from
91-
/// the host-associated symbol.
92-
virtual bool createHostAssociateVarClone(const Fortran::semantics::Symbol &sym) = 0;
90+
/// For a give symbol which is host-associated, create a clone using
91+
/// parameters from the host-associated symbol.
92+
virtual bool
93+
createHostAssociateVarClone(const Fortran::semantics::Symbol &sym) = 0;
9394

9495
//===--------------------------------------------------------------------===//
9596
// Expressions
@@ -178,7 +179,7 @@ class AbstractConverter {
178179
//===--------------------------------------------------------------------===//
179180

180181
/// Get the OpBuilder
181-
virtual FirOpBuilder &getFirOpBuilder() = 0;
182+
virtual fir::FirOpBuilder &getFirOpBuilder() = 0;
182183
/// Get the ModuleOp
183184
virtual mlir::ModuleOp &getModuleOp() = 0;
184185
/// Get the MLIRContext

flang/include/flang/Lower/Allocatable.h

Lines changed: 2 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,19 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
#include "flang/Optimizer/Builder/MutableBox.h"
1314
#include "llvm/ADT/StringRef.h"
1415

1516
namespace mlir {
1617
class Value;
1718
class ValueRange;
18-
class Type;
1919
class Location;
2020
} // namespace mlir
2121

2222
namespace fir {
2323
class MutableBoxValue;
2424
class ExtendedValue;
25+
class FirOpBuilder;
2526
} // namespace fir
2627

2728
namespace Fortran::parser {
@@ -38,24 +39,12 @@ struct SomeType;
3839
namespace Fortran::lower {
3940
struct SymbolBox;
4041
class AbstractConverter;
41-
class FirOpBuilder;
4242
class StatementContext;
4343

4444
namespace pft {
4545
struct Variable;
4646
}
4747

48-
/// Create a fir.box of type \p boxType that can be used to initialize an
49-
/// allocatable variable. Initialization of such variable has to be done at the
50-
/// beginning of the variable lifetime by storing the created box in the memory
51-
/// for the variable box.
52-
/// \p nonDeferredParams must provide the non deferred length parameters so that
53-
/// they can already be placed in the unallocated box (inquiries about these
54-
/// parameters are legal even in unallocated state).
55-
mlir::Value createUnallocatedBox(Fortran::lower::FirOpBuilder &builder,
56-
mlir::Location loc, mlir::Type boxType,
57-
mlir::ValueRange nonDeferredParams);
58-
5948
/// Lower an allocate statement to fir.
6049
void genAllocateStmt(Fortran::lower::AbstractConverter &,
6150
const Fortran::parser::AllocateStmt &, mlir::Location);
@@ -73,31 +62,6 @@ fir::MutableBoxValue createMutableBox(Fortran::lower::AbstractConverter &,
7362
mlir::Value boxAddr,
7463
mlir::ValueRange nonDeferredParams);
7564

76-
/// Create a MutableBoxValue for a temporary allocatable.
77-
/// The created MutableBoxValue wraps a fir.ref<fir.box<fir.heap<type>>> and is
78-
/// initialized to unallocated/diassociated status. An optional name can be
79-
/// given to the created !fir.ref<fir.box>.
80-
fir::MutableBoxValue createTempMutableBox(Fortran::lower::FirOpBuilder &,
81-
mlir::Location, mlir::Type type,
82-
llvm::StringRef name = {});
83-
84-
/// Read all mutable properties into a normal symbol box.
85-
/// It is OK to call this on unassociated/unallocated boxes but any use of the
86-
/// resulting values will be undefined (only the base address will be guaranteed
87-
/// to be null).
88-
Fortran::lower::SymbolBox genMutableBoxRead(Fortran::lower::FirOpBuilder &,
89-
mlir::Location,
90-
const fir::MutableBoxValue &,
91-
bool mayBePolymorphic = true);
92-
93-
/// Update a MutableBoxValue to describe entity \p source (that must be in
94-
/// memory). If \lbounds is not empty, it is used to defined the MutableBoxValue
95-
/// lower bounds, otherwise, the lower bounds from \p source are used.
96-
void associateMutableBox(Fortran::lower::FirOpBuilder &, mlir::Location,
97-
const fir::MutableBoxValue &,
98-
const fir::ExtendedValue &source,
99-
mlir::ValueRange lbounds);
100-
10165
/// Update a MutableBoxValue to describe the entity designated by the expression
10266
/// \p source. This version takes care of \p source lowering.
10367
/// If \lbounds is not empty, it is used to defined the MutableBoxValue
@@ -108,60 +72,4 @@ void associateMutableBox(
10872
const Fortran::evaluate::Expr<Fortran::evaluate::SomeType> &source,
10973
mlir::ValueRange lbounds, Fortran::lower::StatementContext &);
11074

111-
/// Update a MutableBoxValue to describe entity \p source (that must be in
112-
/// memory) with a new array layout given by \p lbounds and \p ubounds.
113-
/// \p source must be known to be contiguous at compile time, or it must have
114-
/// rank 1 (constraint from Fortran 2018 standard 10.2.2.3 point 9).
115-
void associateMutableBoxWithRemap(Fortran::lower::FirOpBuilder &,
116-
mlir::Location, const fir::MutableBoxValue &,
117-
const fir::ExtendedValue &source,
118-
mlir::ValueRange lbounds,
119-
mlir::ValueRange ubounds);
120-
121-
/// Set the association status of a MutableBoxValue to
122-
/// disassociated/unallocated. Nothing is done with the entity that was
123-
/// previously associated/allocated. The function generates code that sets the
124-
/// address field of the MutableBoxValue to zero.
125-
void disassociateMutableBox(Fortran::lower::FirOpBuilder &, mlir::Location,
126-
const fir::MutableBoxValue &);
127-
128-
/// Generate code to conditionally reallocate a MutableBoxValue with a new
129-
/// shape, lower bounds, and length parameters if it is unallocated or if its
130-
/// current shape or deferred length parameters do not match the provided ones.
131-
/// Lower bounds are only used if the entity needs to be allocated, otherwise,
132-
/// the MutableBoxValue will keep its current lower bounds.
133-
/// If the MutableBoxValue is an array, the provided shape can be empty, in
134-
/// which case the MutableBoxValue must already be allocated at runtime and its
135-
/// shape and lower bounds will be kept. If \p shape is empty, only a length
136-
/// parameter mismatch can trigger a reallocation. See Fortran 10.2.1.3 point 3
137-
/// that this function is implementing for more details. The polymorphic
138-
/// requirements are not yet covered by this function.
139-
void genReallocIfNeeded(Fortran::lower::FirOpBuilder &, mlir::Location,
140-
const fir::MutableBoxValue &, mlir::ValueRange lbounds,
141-
mlir::ValueRange shape, mlir::ValueRange lengthParams);
142-
143-
/// Returns the fir.ref<fir.box<T>> of a MutableBoxValue filled with the current
144-
/// association / allocation properties. If the fir.ref<fir.box> already exists
145-
/// and is-up to date, this is a no-op, otherwise, code will be generated to
146-
/// fill the it.
147-
mlir::Value getMutableIRBox(Fortran::lower::FirOpBuilder &, mlir::Location,
148-
const fir::MutableBoxValue &);
149-
150-
/// When the MutableBoxValue was passed as a fir.ref<fir.box> to a call that may
151-
/// have modified it, update the MutableBoxValue according to the
152-
/// fir.ref<fir.box> value.
153-
void syncMutableBoxFromIRBox(Fortran::lower::FirOpBuilder &, mlir::Location,
154-
const fir::MutableBoxValue &);
155-
156-
/// Generate allocation or association status test and returns the resulting
157-
/// i1. This is testing this for a valid/non-null base address value.
158-
mlir::Value genIsAllocatedOrAssociatedTest(Fortran::lower::FirOpBuilder &,
159-
mlir::Location,
160-
const fir::MutableBoxValue &);
161-
162-
/// Finalize a mutable box if it is allocated or associated. This includes both
163-
/// calling the finalizer, if any, and deallocating the storage.
164-
void genFinalization(Fortran::lower::FirOpBuilder &, mlir::Location,
165-
const fir::MutableBoxValue &);
166-
16775
} // namespace Fortran::lower

flang/include/flang/Lower/CharacterRuntime.h

Lines changed: 26 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313

1414
namespace fir {
1515
class ExtendedValue;
16-
}
16+
class FirOpBuilder;
17+
} // namespace fir
1718

1819
namespace Fortran {
1920
namespace lower {
20-
class FirOpBuilder;
2121

2222
/// Generate a call to the ADJUSTL runtime.
2323
/// This calls the simple runtime entry point that then calls into the more
@@ -27,10 +27,8 @@ class FirOpBuilder;
2727
/// result. \p StringBox must be a fir.box describing the adjustl string
2828
/// argument. Note that the \p genAdjust() helper is called to do the majority
2929
/// of the lowering work.
30-
void genAdjustL(Fortran::lower::FirOpBuilder &builder,
31-
mlir::Location loc,
32-
mlir::Value resultBox,
33-
mlir::Value stringBox);
30+
void genAdjustL(fir::FirOpBuilder &builder, mlir::Location loc,
31+
mlir::Value resultBox, mlir::Value stringBox);
3432

3533
/// Generate a call to the ADJUSTR runtime.
3634
/// This calls the simple runtime entry point that then calls into the more
@@ -40,26 +38,22 @@ void genAdjustL(Fortran::lower::FirOpBuilder &builder,
4038
/// result. \p StringBox must be a fir.box describing the adjustr string
4139
/// argument. Note that the \p genAdjust() helper is called to do the majority
4240
/// of the lowering work.
43-
void genAdjustR(Fortran::lower::FirOpBuilder &builder,
44-
mlir::Location loc,
45-
mlir::Value resultBox,
46-
mlir::Value stringBox);
41+
void genAdjustR(fir::FirOpBuilder &builder, mlir::Location loc,
42+
mlir::Value resultBox, mlir::Value stringBox);
4743

4844
/// Generate a call to the ADJUST[L|R] runtime.
4945
///
5046
/// \p resultBox must be an unallocated allocatable used for the temporary
5147
/// result. \p StringBox must be a fir.box describing the adjustr string
5248
/// argument. The \p adjustFunc should be a mlir::FuncOp for the appropriate
5349
/// runtime entry function.
54-
void genAdjust(Fortran::lower::FirOpBuilder &builder,
55-
mlir::Location loc,
56-
mlir::Value resultBox,
57-
mlir::Value stringBox,
50+
void genAdjust(fir::FirOpBuilder &builder, mlir::Location loc,
51+
mlir::Value resultBox, mlir::Value stringBox,
5852
mlir::FuncOp &adjustFunc);
5953

6054
/// Generate call to a character comparison for two ssa-values of type
6155
/// `boxchar`.
62-
mlir::Value genCharCompare(FirOpBuilder &builder, mlir::Location loc,
56+
mlir::Value genCharCompare(fir::FirOpBuilder &builder, mlir::Location loc,
6357
mlir::CmpIPredicate cmp,
6458
const fir::ExtendedValue &lhs,
6559
const fir::ExtendedValue &rhs);
@@ -68,23 +62,23 @@ mlir::Value genCharCompare(FirOpBuilder &builder, mlir::Location loc,
6862
/// are 4 arguments, 2 for the lhs and 2 for the rhs. Each CHARACTER must pass a
6963
/// reference to its buffer (`ref<char<K>>`) and its LEN type parameter (some
7064
/// integral type).
71-
mlir::Value genCharCompare(FirOpBuilder &builder, mlir::Location loc,
65+
mlir::Value genCharCompare(fir::FirOpBuilder &builder, mlir::Location loc,
7266
mlir::CmpIPredicate cmp, mlir::Value lhsBuff,
7367
mlir::Value lhsLen, mlir::Value rhsBuff,
7468
mlir::Value rhsLen);
7569

7670
/// Generate call to INDEX runtime.
7771
/// This calls the simple runtime entry points based on the KIND of the string.
7872
/// No descriptors are used.
79-
mlir::Value genIndex(FirOpBuilder &builder, mlir::Location loc, int kind,
73+
mlir::Value genIndex(fir::FirOpBuilder &builder, mlir::Location loc, int kind,
8074
mlir::Value stringBase, mlir::Value stringLen,
8175
mlir::Value substringBase, mlir::Value substringLen,
8276
mlir::Value back);
8377

8478
/// Generate call to INDEX runtime.
8579
/// This calls the descriptor based runtime call implementation for the index
8680
/// intrinsic.
87-
void genIndexDescriptor(FirOpBuilder &builder, mlir::Location loc,
81+
void genIndexDescriptor(fir::FirOpBuilder &builder, mlir::Location loc,
8882
mlir::Value resultBox, mlir::Value stringBox,
8983
mlir::Value substringBox, mlir::Value backOpt,
9084
mlir::Value kind);
@@ -94,45 +88,45 @@ void genIndexDescriptor(FirOpBuilder &builder, mlir::Location loc,
9488
/// result. \p stringBox must be a fir.box describing repeat string argument.
9589
/// \p ncopies must be a value representing the number of copies.
9690
/// The runtime will always allocate the resultBox.
97-
void genRepeat(Fortran::lower::FirOpBuilder &builder, mlir::Location loc,
91+
void genRepeat(fir::FirOpBuilder &builder, mlir::Location loc,
9892
mlir::Value resultBox, mlir::Value stringBox,
9993
mlir::Value ncopies);
10094

10195
/// Generate call to trim runtime.
10296
/// \p resultBox must be an unallocated allocatable used for the temporary
10397
/// result. \p stringBox must be a fir.box describing trim string argument.
10498
/// The runtime will always allocate the resultBox.
105-
void genTrim(Fortran::lower::FirOpBuilder &builder, mlir::Location loc,
99+
void genTrim(fir::FirOpBuilder &builder, mlir::Location loc,
106100
mlir::Value resultBox, mlir::Value stringBox);
107101

108102
/// Generate call to scan runtime.
109103
/// This calls the descriptor based runtime call implementation of the scan
110104
/// intrinsics.
111-
void genScanDescriptor(Fortran::lower::FirOpBuilder &builder,
112-
mlir::Location loc, mlir::Value resultBox,
113-
mlir::Value stringBox, mlir::Value setBox,
114-
mlir::Value backBox, mlir::Value kind);
105+
void genScanDescriptor(fir::FirOpBuilder &builder, mlir::Location loc,
106+
mlir::Value resultBox, mlir::Value stringBox,
107+
mlir::Value setBox, mlir::Value backBox,
108+
mlir::Value kind);
115109

116110
/// Generate call to the scan runtime routine that is specialized on
117111
/// \param kind.
118112
/// The \param kind represents the kind of the elements in the strings.
119-
mlir::Value genScan(Fortran::lower::FirOpBuilder &builder, mlir::Location loc,
120-
int kind, mlir::Value stringBase, mlir::Value stringLen,
113+
mlir::Value genScan(fir::FirOpBuilder &builder, mlir::Location loc, int kind,
114+
mlir::Value stringBase, mlir::Value stringLen,
121115
mlir::Value setBase, mlir::Value setLen, mlir::Value back);
122116

123117
/// Generate call to verify runtime.
124118
/// This calls the descriptor based runtime call implementation of the scan
125119
/// intrinsics.
126-
void genVerifyDescriptor(Fortran::lower::FirOpBuilder &builder,
127-
mlir::Location loc, mlir::Value resultBox,
128-
mlir::Value stringBox, mlir::Value setBox,
129-
mlir::Value backBox, mlir::Value kind);
120+
void genVerifyDescriptor(fir::FirOpBuilder &builder, mlir::Location loc,
121+
mlir::Value resultBox, mlir::Value stringBox,
122+
mlir::Value setBox, mlir::Value backBox,
123+
mlir::Value kind);
130124

131125
/// Generate call to the verify runtime routine that is specialized on
132126
/// \param kind.
133127
/// The \param kind represents the kind of the elements in the strings.
134-
mlir::Value genVerify(Fortran::lower::FirOpBuilder &builder, mlir::Location loc,
135-
int kind, mlir::Value stringBase, mlir::Value stringLen,
128+
mlir::Value genVerify(fir::FirOpBuilder &builder, mlir::Location loc, int kind,
129+
mlir::Value stringBase, mlir::Value stringLen,
136130
mlir::Value setBase, mlir::Value setLen,
137131
mlir::Value back);
138132

flang/include/flang/Lower/Coarray.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#define FORTRAN_LOWER_COARRAY_H
1111

1212
#include "flang/Lower/AbstractConverter.h"
13-
#include "flang/Lower/Support/BoxValue.h"
13+
#include "flang/Optimizer/Builder/BoxValue.h"
1414

1515
namespace Fortran {
1616

flang/include/flang/Lower/ConvertExpr.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#define FORTRAN_LOWER_CONVERTEXPR_H
1919

2020
#include "flang/Evaluate/shape.h"
21-
#include "flang/Lower/Support/BoxValue.h"
21+
#include "flang/Optimizer/Builder/BoxValue.h"
2222

2323
namespace mlir {
2424
class Location;

0 commit comments

Comments
 (0)