Skip to content

Commit 187b675

Browse files
committed
[NFC] Move runtime API call generator into Optimizer/Runtime - part 2/2
In order to be able to emit runtime calls in FIR transformation passes, move the runtime call generators into Optimizer/Runtime. RTBuilder.h is moved there, as well as DerivedRuntime.h, NumericRuntime.h, ReductionRuntime.h, CharacterRuntime.h, and TransformationalRuntime.h All the methods are moved from the Fortran::lower namespace into a new fir::runtime namespace. Runtime.[h, cpp] is not moved yet because it uses parse tree nodes. There is no urgent need to move it now, so I did not attempted to split it in this PR. Allocatable.cpp and IO.cpp make direct usages of RTBuilder.h, it may be cleaner to handle the runtime call generation details in new Optimizer/xxx files, but this is not done here since this calls are not currently needed in lib/Optimizer.
1 parent 528737c commit 187b675

23 files changed

+699
-696
lines changed

flang/docs/tutorials/addingIntrinsics.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,10 @@ The categories are:
167167
See `include/flang/Support/BoxValue.h` for more details.
168168

169169
If the intrinsic has a runtime implementation, the actual binding should be done in another helper function called
170-
`gen{Runtime function name}`. For instance character runtime bindings are implemented in `lib/Lower/CharacterRuntime.cpp`
170+
`gen{Runtime function name}`. For instance character runtime bindings are implemented in `lib/Optimizer/Runtime/Character.cpp`
171171
See `genTrim` for an example of such runtime binding helper.
172-
Non character related runtime binding should be added to `lib/Lower/IntrinsicRuntime.cpp`.
172+
Non character related runtime binding should be added to `lib/Optimizer/Runtime/Xxx.cpp`
173+
(for runtime API declared in `runtime/xxx.cpp`).
173174

174175
Note: Numerical elemental intrinsics that have libm/libnvmath implementations are automatically mapped and
175176
do not follow the above patterns (this includes intrinsics like `acos`, `bessel_j`...).
@@ -312,18 +313,18 @@ It also creates a descriptor for the argument with `builder.createBox()`. Note t
312313
differently because we expect the first one to be modified, but not the second one. This difference preserves SSA
313314
semantics in the IR (An SSA value cannot change).
314315

315-
The actual runtime call is generated by `Fortran::lower::genTrim` defined in `lib/Lower/CharacterRuntime.cpp`
316+
The actual runtime call is generated by `Fortran::lower::genTrim` defined in `lib/Optimizer/Runtime/Character.cpp`
316317
After the runtime call, the result descriptor is read to build the resulting `fir::CharBoxValue`
317318
with the result address and length. Since the result address was allocated, it is added to a clean-up list so that
318319
it can be deallocated after the statement.
319320

320321
The implementation of the runtime call in lowering looks like:
321322

322323
```c++
323-
void Fortran::lower::genTrim(fir::FirOpBuilder &builder,
324+
void fir::runtime::genTrim(fir::FirOpBuilder &builder,
324325
mlir::Location loc, mlir::Value resultBox,
325326
mlir::Value stringBox) {
326-
auto trimFunc = getRuntimeFunc<mkRTKey(Trim)>(loc, builder);
327+
auto trimFunc = fir::runtime::getRuntimeFunc<mkRTKey(Trim)>(loc, builder);
327328
auto fTy = trimFunc.getType();
328329
auto sourceFile = fir::factory::locationToFilename(builder, loc);
329330
auto sourceLine =

flang/include/flang/Lower/Allocatable.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
#ifndef FORTRAN_LOWER_ALLOCATABLE_H
14+
#define FORTRAN_LOWER_ALLOCATABLE_H
15+
1316
#include "flang/Optimizer/Builder/MutableBox.h"
1417
#include "llvm/ADT/StringRef.h"
1518

@@ -73,3 +76,5 @@ void associateMutableBox(
7376
mlir::ValueRange lbounds, Fortran::lower::StatementContext &);
7477

7578
} // namespace Fortran::lower
79+
80+
#endif // FORTRAN_LOWER_ALLOCATABLE_H

flang/include/flang/Optimizer/Runtime/Character.h

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
//===-- Lower/CharacterRuntime.h -- lower CHARACTER operations --*- C++ -*-===//
1+
//===-- Character.h -- generate calls to character runtime API --*- C++ -*-===//
22
//
33
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.
55
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#ifndef FORTRAN_LOWER_CHARACTERRUNTIME_H
10-
#define FORTRAN_LOWER_CHARACTERRUNTIME_H
9+
#ifndef FORTRAN_OPTIMIZER_RUNTIME_CHARACTER_H
10+
#define FORTRAN_OPTIMIZER_RUNTIME_CHARACTER_H
1111

1212
#include "mlir/Dialect/StandardOps/IR/Ops.h"
1313

@@ -16,8 +16,7 @@ class ExtendedValue;
1616
class FirOpBuilder;
1717
} // namespace fir
1818

19-
namespace Fortran {
20-
namespace lower {
19+
namespace fir::runtime {
2120

2221
/// Generate a call to the ADJUSTL runtime.
2322
/// This calls the simple runtime entry point that then calls into the more
@@ -130,7 +129,6 @@ mlir::Value genVerify(fir::FirOpBuilder &builder, mlir::Location loc, int kind,
130129
mlir::Value setBase, mlir::Value setLen,
131130
mlir::Value back);
132131

133-
} // namespace lower
134-
} // namespace Fortran
132+
} // namespace fir::runtime
135133

136-
#endif // FORTRAN_LOWER_CHARACTERRUNTIME_H
134+
#endif // FORTRAN_OPTIMIZER_RUNTIME_CHARACTER_H

flang/include/flang/Optimizer/Runtime/Derived.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
//===-- Lower/DerivedRuntime.h - lower derived type runtime API -*- C++ -*-===//
1+
//===-- Derived.h - generate derived type runtime API calls -*- C++ -----*-===//
22
//
33
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.
55
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#ifndef FORTRAN_LOWER_DERIVEDRUNTIME_H
10-
#define FORTRAN_LOWER_DERIVEDRUNTIME_H
9+
#ifndef FORTRAN_OPTIMIZER_RUNTIME_DERIVED_H
10+
#define FORTRAN_OPTIMIZER_RUNTIME_DERIVED_H
1111

1212
namespace mlir {
1313
class Value;
@@ -18,7 +18,7 @@ namespace fir {
1818
class FirOpBuilder;
1919
}
2020

21-
namespace Fortran::lower {
21+
namespace fir::runtime {
2222

2323
/// Generate call to derived type initialization runtime routine to
2424
/// default initialize \p box.
@@ -35,5 +35,5 @@ void genDerivedTypeDestroy(fir::FirOpBuilder &builder, mlir::Location loc,
3535
void genDerivedTypeAssign(fir::FirOpBuilder &builder, mlir::Location loc,
3636
mlir::Value destinationBox, mlir::Value sourceBox);
3737

38-
} // namespace Fortran::lower
39-
#endif
38+
} // namespace fir::runtime
39+
#endif // FORTRAN_OPTIMIZER_RUNTIME_DERIVED_H

flang/include/flang/Optimizer/Runtime/Numeric.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
//===-- Lower/NumericRuntime.h -- lower numeric intrinsics --*- C++ -*-===//
1+
//===-- Numeric.h -- generate numeric intrinsics runtime calls --*- C++ -*-===//
22
//
33
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.
55
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#ifndef FORTRAN_LOWER_NUMERICRUNTIME_H
10-
#define FORTRAN_LOWER_NUMERICRUNTIME_H
9+
#ifndef FORTRAN_OPTIMIZER_RUNTIME_NUMERIC_H
10+
#define FORTRAN_OPTIMIZER_RUNTIME_NUMERIC_H
1111

1212
#include "mlir/Dialect/StandardOps/IR/Ops.h"
1313

@@ -16,7 +16,7 @@ class ExtendedValue;
1616
class FirOpBuilder;
1717
} // namespace fir
1818

19-
namespace Fortran::lower {
19+
namespace fir::runtime {
2020

2121
/// Generate call to Exponent intrinsic runtime routine.
2222
mlir::Value genExponent(fir::FirOpBuilder &builder, mlir::Location loc,
@@ -46,5 +46,5 @@ mlir::Value genSetExponent(fir::FirOpBuilder &builder, mlir::Location loc,
4646
mlir::Value genSpacing(fir::FirOpBuilder &builder, mlir::Location loc,
4747
mlir::Value x);
4848

49-
} // namespace Fortran::lower
50-
#endif
49+
} // namespace fir::runtime
50+
#endif // FORTRAN_OPTIMIZER_RUNTIME_NUMERIC_H

flang/include/flang/Optimizer/Runtime/RTBuilder.h

Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,29 @@
1414
///
1515
//===----------------------------------------------------------------------===//
1616

17-
#ifndef FORTRAN_LOWER_RTBUILDER_H
18-
#define FORTRAN_LOWER_RTBUILDER_H
17+
#ifndef FORTRAN_OPTIMIZER_RUNTIME_RTBUILDER_H
18+
#define FORTRAN_OPTIMIZER_RUNTIME_RTBUILDER_H
1919

20-
#include "flang/Lower/ConvertType.h"
20+
#include "flang/Common/Fortran.h"
2121
#include "flang/Optimizer/Builder/FIRBuilder.h"
2222
#include "flang/Optimizer/Dialect/FIRType.h"
2323
#include "mlir/IR/BuiltinTypes.h"
2424
#include "mlir/IR/MLIRContext.h"
2525
#include "llvm/ADT/SmallVector.h"
2626
#include <functional>
2727

28-
// List the runtime headers we want to be able to dissect
29-
#include "../../runtime/io-api.h"
30-
3128
// Incomplete type indicating C99 complex ABI in interfaces. Beware, _Complex
3229
// and std::complex are layout compatible, but not compatible in all ABI call
3330
// interface (e.g. X86 32 bits). _Complex is not standard C++, so do not use
3431
// it here.
3532
struct c_float_complex_t;
3633
struct c_double_complex_t;
3734

38-
namespace Fortran::lower {
35+
namespace Fortran::runtime {
36+
class Descriptor;
37+
}
38+
39+
namespace fir::runtime {
3940

4041
using TypeBuilderFunc = mlir::Type (*)(mlir::MLIRContext *);
4142
using FuncTypeBuilderFunc = mlir::FunctionType (*)(mlir::MLIRContext *);
@@ -76,13 +77,6 @@ constexpr TypeBuilderFunc getModel<int &>() {
7677
};
7778
}
7879
template <>
79-
constexpr TypeBuilderFunc getModel<Fortran::runtime::io::Iostat>() {
80-
return [](mlir::MLIRContext *context) -> mlir::Type {
81-
return mlir::IntegerType::get(context,
82-
8 * sizeof(Fortran::runtime::io::Iostat));
83-
};
84-
}
85-
template <>
8680
constexpr TypeBuilderFunc getModel<char *>() {
8781
return [](mlir::MLIRContext *context) -> mlir::Type {
8882
return fir::ReferenceType::get(mlir::IntegerType::get(context, 8));
@@ -156,10 +150,6 @@ constexpr TypeBuilderFunc getModel<unsigned long long>() {
156150
};
157151
}
158152
template <>
159-
constexpr TypeBuilderFunc getModel<Fortran::runtime::io::IoStatementState *>() {
160-
return getModel<char *>();
161-
}
162-
template <>
163153
constexpr TypeBuilderFunc getModel<double>() {
164154
return [](mlir::MLIRContext *context) -> mlir::Type {
165155
return mlir::FloatType::getF64(context);
@@ -261,13 +251,6 @@ constexpr TypeBuilderFunc getModel<Fortran::common::TypeCategory>() {
261251
};
262252
}
263253
template <>
264-
constexpr TypeBuilderFunc
265-
getModel<const Fortran::runtime::io::NamelistGroup &>() {
266-
return [](mlir::MLIRContext *context) -> mlir::Type {
267-
return fir::ReferenceType::get(mlir::TupleType::get(context));
268-
};
269-
}
270-
template <>
271254
constexpr TypeBuilderFunc getModel<void>() {
272255
return [](mlir::MLIRContext *context) -> mlir::Type {
273256
return mlir::NoneType::get(context);
@@ -355,10 +338,10 @@ struct RuntimeTableEntry<RuntimeTableKey<KT>, RuntimeIdentifier<Cs...>> {
355338
E(X, 43), E(X, 44), E(X, 45), E(X, 46), E(X, 47), E(X, 48), E(X, 49)
356339
#define ExpandKey(X) MacroExpandKey(QuoteKey(X))
357340
#define FullSeq(X) std::integer_sequence<char, ExpandKey(X)>
358-
#define AsSequence(X) decltype(Fortran::lower::details::filter(FullSeq(X){}))
341+
#define AsSequence(X) decltype(fir::runtime::details::filter(FullSeq(X){}))
359342
#define mkKey(X) \
360-
Fortran::lower::RuntimeTableEntry< \
361-
Fortran::lower::RuntimeTableKey<decltype(X)>, AsSequence(X)>
343+
fir::runtime::RuntimeTableEntry<fir::runtime::RuntimeTableKey<decltype(X)>, \
344+
AsSequence(X)>
362345
#define mkRTKey(X) mkKey(RTNAME(X))
363346

364347
/// Get (or generate) the MLIR FuncOp for a given runtime function. Its template
@@ -405,6 +388,6 @@ createArguments(fir::FirOpBuilder &builder, mlir::Location loc,
405388
return result;
406389
}
407390

408-
} // namespace Fortran::lower
391+
} // namespace fir::runtime
409392

410-
#endif // FORTRAN_LOWER_RTBUILDER_H
393+
#endif // FORTRAN_OPTIMIZER_RUNTIME_RTBUILDER_H

flang/include/flang/Optimizer/Runtime/Reduction.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
//===-- Lower/ReductionRuntime.h -- lower reduction intrinsics --*- C++ -*-===//
1+
//===-- Reduction.h -- generate calls to reduction runtime API --*- C++ -*-===//
22
//
33
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.
55
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#ifndef FORTRAN_LOWER_REDUCTIONRUNTIME_H
10-
#define FORTRAN_LOWER_REDUCTIONRUNTIME_H
9+
#ifndef FORTRAN_OPTIMIZER_RUNTIME_REDUCTION_H
10+
#define FORTRAN_OPTIMIZER_RUNTIME_REDUCTION_H
1111

1212
#include "mlir/Dialect/StandardOps/IR/Ops.h"
1313

@@ -16,7 +16,7 @@ class ExtendedValue;
1616
class FirOpBuilder;
1717
} // namespace fir
1818

19-
namespace Fortran::lower {
19+
namespace fir::runtime {
2020

2121
/// Generate call to all runtime routine.
2222
/// This calls the descriptor based runtime call implementation of the all
@@ -143,6 +143,6 @@ void genSumDim(fir::FirOpBuilder &builder, mlir::Location loc,
143143
mlir::Value resultBox, mlir::Value arrayBox, mlir::Value dim,
144144
mlir::Value maskBox);
145145

146-
} // namespace Fortran::lower
146+
} // namespace fir::runtime
147147

148-
#endif // FORTRAN_LOWER_REDUCTIONRUNTIME_H
148+
#endif // FORTRAN_OPTIMIZER_RUNTIME_REDUCTION_H

flang/include/flang/Optimizer/Runtime/Transformational.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
//===-- Lower/TransformationalRuntime.h --*- C++ -*-===//
2-
// lower transformational intrinsics
1+
//===-- Transformational.h --------------------------------------*- C++ -*-===//
2+
// Generate transformational intrinsic runtime API calls.
33
//
44
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
55
// See https://llvm.org/LICENSE.txt for license information.
66
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
77
//
88
//===----------------------------------------------------------------------===//
99

10-
#ifndef FORTRAN_LOWER_TRANSFORMATIONALRUNTIME_H
11-
#define FORTRAN_LOWER_TRANSFORMATIONALRUNTIME_H
10+
#ifndef FORTRAN_OPTIMIZER_RUNTIME_TRANSFORMATIONAL_H
11+
#define FORTRAN_OPTIMIZER_RUNTIME_TRANSFORMATIONAL_H
1212

1313
#include "mlir/Dialect/StandardOps/IR/Ops.h"
1414

@@ -17,7 +17,7 @@ class ExtendedValue;
1717
class FirOpBuilder;
1818
} // namespace fir
1919

20-
namespace Fortran::lower {
20+
namespace fir::runtime {
2121

2222
void genCshift(fir::FirOpBuilder &builder, mlir::Location loc,
2323
mlir::Value resultBox, mlir::Value arrayBox,
@@ -50,6 +50,6 @@ void genUnpack(fir::FirOpBuilder &builder, mlir::Location loc,
5050
mlir::Value resultBox, mlir::Value vectorBox,
5151
mlir::Value maskBox, mlir::Value fieldBox);
5252

53-
} // namespace Fortran::lower
53+
} // namespace fir::runtime
5454

55-
#endif // FORTRAN_LOWER_TRANSFORMATIONALRUNTIME_H
55+
#endif // FORTRAN_OPTIMIZER_RUNTIME_TRANSFORMATIONAL_H

flang/lib/Lower/Allocatable.cpp

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#include "flang/Lower/Allocatable.h"
1414
#include "../runtime/allocatable.h"
1515
#include "../runtime/pointer.h"
16-
#include "RTBuilder.h"
1716
#include "StatementContext.h"
1817
#include "flang/Evaluate/tools.h"
1918
#include "flang/Lower/AbstractConverter.h"
@@ -23,6 +22,7 @@
2322
#include "flang/Optimizer/Builder/FIRBuilder.h"
2423
#include "flang/Optimizer/Dialect/FIROps.h"
2524
#include "flang/Optimizer/Dialect/FIROpsSupport.h"
25+
#include "flang/Optimizer/Runtime/RTBuilder.h"
2626
#include "flang/Optimizer/Support/FatalError.h"
2727
#include "flang/Parser/parse-tree.h"
2828
#include "flang/Semantics/tools.h"
@@ -119,9 +119,9 @@ static void genRuntimeSetBounds(fir::FirOpBuilder &builder, mlir::Location loc,
119119
mlir::Value upperBound) {
120120
auto callee =
121121
box.isPointer()
122-
? Fortran::lower::getRuntimeFunc<mkRTKey(PointerSetBounds)>(loc,
123-
builder)
124-
: Fortran::lower::getRuntimeFunc<mkRTKey(AllocatableSetBounds)>(
122+
? fir::runtime::getRuntimeFunc<mkRTKey(PointerSetBounds)>(loc,
123+
builder)
124+
: fir::runtime::getRuntimeFunc<mkRTKey(AllocatableSetBounds)>(
125125
loc, builder);
126126
llvm::SmallVector<mlir::Value> args{box.getAddr(), dimIndex, lowerBound,
127127
upperBound};
@@ -139,9 +139,9 @@ static void genRuntimeInitCharacter(fir::FirOpBuilder &builder,
139139
mlir::Value len) {
140140
auto callee =
141141
box.isPointer()
142-
? Fortran::lower::getRuntimeFunc<mkRTKey(PointerNullifyCharacter)>(
142+
? fir::runtime::getRuntimeFunc<mkRTKey(PointerNullifyCharacter)>(
143143
loc, builder)
144-
: Fortran::lower::getRuntimeFunc<mkRTKey(AllocatableInitCharacter)>(
144+
: fir::runtime::getRuntimeFunc<mkRTKey(AllocatableInitCharacter)>(
145145
loc, builder);
146146
auto inputTypes = callee.getType().getInputs();
147147
if (inputTypes.size() != 5)
@@ -167,10 +167,9 @@ static mlir::Value genRuntimeAllocate(fir::FirOpBuilder &builder,
167167
ErrorManager &errorManager) {
168168
auto callee =
169169
box.isPointer()
170-
? Fortran::lower::getRuntimeFunc<mkRTKey(PointerAllocate)>(loc,
171-
builder)
172-
: Fortran::lower::getRuntimeFunc<mkRTKey(AllocatableAllocate)>(
173-
loc, builder);
170+
? fir::runtime::getRuntimeFunc<mkRTKey(PointerAllocate)>(loc, builder)
171+
: fir::runtime::getRuntimeFunc<mkRTKey(AllocatableAllocate)>(loc,
172+
builder);
174173
llvm::SmallVector<mlir::Value> args{
175174
box.getAddr(), errorManager.hasStat, errorManager.errMsgAddr,
176175
errorManager.sourceFile, errorManager.sourceLine};
@@ -189,9 +188,9 @@ static mlir::Value genRuntimeDeallocate(fir::FirOpBuilder &builder,
189188
auto boxAddress = fir::factory::getMutableIRBox(builder, loc, box);
190189
auto callee =
191190
box.isPointer()
192-
? Fortran::lower::getRuntimeFunc<mkRTKey(PointerDeallocate)>(loc,
193-
builder)
194-
: Fortran::lower::getRuntimeFunc<mkRTKey(AllocatableDeallocate)>(
191+
? fir::runtime::getRuntimeFunc<mkRTKey(PointerDeallocate)>(loc,
192+
builder)
193+
: fir::runtime::getRuntimeFunc<mkRTKey(AllocatableDeallocate)>(
195194
loc, builder);
196195
llvm::SmallVector<mlir::Value> args{
197196
boxAddress, errorManager.hasStat, errorManager.errMsgAddr,

0 commit comments

Comments
 (0)