16#ifndef FORTRAN_OPTIMIZER_BUILDER_FIRBUILDER_H
17#define FORTRAN_OPTIMIZER_BUILDER_FIRBUILDER_H
20#include "flang/Optimizer/Dialect/FIROps.h"
21#include "flang/Optimizer/Dialect/FIROpsSupport.h"
22#include "flang/Optimizer/Dialect/FIRType.h"
23#include "flang/Optimizer/Dialect/Support/FIRContext.h"
24#include "flang/Optimizer/Dialect/Support/KindMapping.h"
25#include "mlir/IR/Builders.h"
26#include "mlir/IR/BuiltinOps.h"
27#include "llvm/ADT/DenseMap.h"
37class AbstractArrayBox;
46 return builder.getI64Type();
55class FirOpBuilder :
public mlir::OpBuilder,
public mlir::OpBuilder::Listener {
58 mlir::SymbolTable *symbolTable =
nullptr)
59 : OpBuilder{op,
this}, kindMap{std::move(kindMap)},
60 symbolTable{symbolTable} {}
62 mlir::SymbolTable *symbolTable =
nullptr)
63 : OpBuilder(builder), OpBuilder::Listener(), kindMap{std::move(kindMap)},
64 symbolTable{symbolTable} {
67 explicit FirOpBuilder(mlir::OpBuilder &builder, mlir::ModuleOp mod)
68 : OpBuilder(builder), OpBuilder::Listener(),
74 : OpBuilder(builder), OpBuilder::Listener(), kindMap{std::move(kindMap)} {
76 auto fmi = mlir::dyn_cast<mlir::arith::ArithFastMathInterface>(*op);
82 FirOpBuilder(mlir::OpBuilder &builder, mlir::Operation *op)
87 : OpBuilder(other), OpBuilder::Listener(), kindMap{other.kindMap},
88 fastMathFlags{other.fastMathFlags},
89 integerOverflowFlags{other.integerOverflowFlags},
90 symbolTable{other.symbolTable} {
95 : OpBuilder(other), OpBuilder::Listener(),
96 kindMap{std::move(other.kindMap)}, fastMathFlags{other.fastMathFlags},
97 integerOverflowFlags{other.integerOverflowFlags},
98 symbolTable{other.symbolTable} {
103 mlir::Region &
getRegion() {
return *getBlock()->getParent(); }
107 return getRegion().getParentOfType<mlir::ModuleOp>();
112 return getRegion().getParentOfType<mlir::func::FuncOp>();
123 return getIntegerType(
135 bool allowCharacterConversion =
false,
136 bool allowRebox =
false);
161 return mlir::SymbolRefAttr::get(getContext(), str);
167 fir::BoxProcType getBoxProcType(mlir::FunctionType funcTy) {
168 return fir::BoxProcType::get(getContext(), funcTy);
188 mlir::Type integerType) {
194 llvm::APFloat::integerPart val);
198 const llvm::APFloat &val);
208 llvm::StringRef uniqName, llvm::StringRef name,
211 bool asTarget =
false);
213 llvm::StringRef uniqName, llvm::StringRef name,
216 bool asTarget =
false);
227 mlir::Location loc, mlir::Type type, llvm::StringRef name,
228 mlir::ValueRange lenParams = {}, mlir::ValueRange shape = {},
230 std::optional<Fortran::common::CUDADataAttr> cudaAttr = std::nullopt);
236 mlir::Location loc, mlir::Type type, llvm::StringRef name = {},
237 mlir::ValueRange shape = {}, mlir::ValueRange lenParams = {},
239 std::optional<Fortran::common::CUDADataAttr> cudaAttr = std::nullopt);
243 mlir::ValueRange shape) {
253 llvm::StringRef name,
261 llvm::StringRef name = {}, mlir::ValueRange shape = {},
262 mlir::ValueRange lenParams = {},
275 fir::GlobalOp
createGlobal(mlir::Location loc, mlir::Type type,
276 llvm::StringRef name,
277 mlir::StringAttr linkage = {},
278 mlir::Attribute value = {},
bool isConst =
false,
279 bool isTarget =
false,
280 cuf::DataAttributeAttr dataAttr = {});
282 fir::GlobalOp
createGlobal(mlir::Location loc, mlir::Type type,
283 llvm::StringRef name,
bool isConst,
bool isTarget,
284 std::function<
void(FirOpBuilder &)> bodyBuilder,
285 mlir::StringAttr linkage = {},
286 cuf::DataAttributeAttr dataAttr = {});
290 llvm::StringRef name,
291 mlir::StringAttr linkage = {},
292 mlir::Attribute value = {}) {
293 return createGlobal(loc, type, name, linkage, value,
true,
299 llvm::StringRef name,
300 std::function<
void(FirOpBuilder &)> bodyBuilder,
301 mlir::StringAttr linkage = {}) {
303 bodyBuilder, linkage);
308 llvm::StringRef
string);
310 std::pair<fir::TypeInfoOp, mlir::OpBuilder::InsertPoint>
311 createTypeInfoOp(mlir::Location loc, fir::RecordType recordType,
312 fir::RecordType parentType);
318 mlir::StringAttr createCommonLinkage() {
return getStringAttr(
"common"); }
320 mlir::StringAttr createInternalLinkage() {
return getStringAttr(
"internal"); }
322 mlir::StringAttr createLinkOnceLinkage() {
return getStringAttr(
"linkonce"); }
324 mlir::StringAttr createLinkOnceODRLinkage() {
325 return getStringAttr(
"linkonce_odr");
328 mlir::StringAttr createWeakLinkage() {
return getStringAttr(
"weak"); }
335 static mlir::func::FuncOp
336 getNamedFunction(mlir::ModuleOp module,
const mlir::SymbolTable *symbolTable,
337 llvm::StringRef name);
344 static mlir::func::FuncOp
345 getNamedFunction(mlir::ModuleOp module,
const mlir::SymbolTable *symbolTable,
346 mlir::SymbolRefAttr symbol);
348 fir::GlobalOp getNamedGlobal(llvm::StringRef name) {
352 static fir::GlobalOp getNamedGlobal(mlir::ModuleOp module,
353 const mlir::SymbolTable *symbolTable,
354 llvm::StringRef name);
357 mlir::Value
createConvert(mlir::Location loc, mlir::Type toTy,
367 mlir::Value
loadIfRef(mlir::Location loc, mlir::Value val);
372 mlir::FunctionType ty) {
377 mlir::ModuleOp module,
378 llvm::StringRef name,
379 mlir::FunctionType ty,
380 mlir::SymbolTable *);
401 mlir::ValueRange triples, mlir::ValueRange path);
412 mlir::Value
createBox(mlir::Location loc, mlir::Type boxType,
413 mlir::Value addr, mlir::Value shape, mlir::Value slice,
432 : ifOp{ifOp}, builder{builder} {}
433 template <
typename CC>
435 builder.setInsertionPointToStart(&ifOp.getThenRegion().front());
439 template <
typename CC>
441 assert(!ifOp.getElseRegion().empty() &&
"must have else region");
442 builder.setInsertionPointToStart(&ifOp.getElseRegion().front());
446 void end() { builder.setInsertionPointAfter(ifOp); }
451 return ifOp.getResults();
454 fir::IfOp &getIfOp() {
return ifOp; };
464 mlir::Value cdt,
bool withElseRegion) {
465 auto op = create<fir::IfOp>(loc, results, cdt, withElseRegion);
472 auto op = create<fir::IfOp>(loc, std::nullopt, cdt,
false);
479 auto op = create<fir::IfOp>(loc, std::nullopt, cdt,
true);
483 mlir::Value genNot(mlir::Location loc, mlir::Value
boolean) {
484 return create<mlir::arith::CmpIOp>(loc, mlir::arith::CmpIPredicate::eq,
492 mlir::Value
genIsNullAddr(mlir::Location loc, mlir::Value addr);
497 mlir::Value ub, mlir::Value step,
502 mlir::Value
genAbsentOp(mlir::Location loc, mlir::Type argTy);
508 fastMathFlags = flags;
524 if (flags == mlir::arith::FastMathFlags::none)
527 std::string fmfString{mlir::arith::stringifyFastMathFlags(flags)};
528 std::replace(fmfString.begin(), fmfString.end(),
',',
'_');
536 integerOverflowFlags = flags;
541 return integerOverflowFlags;
549 mlir::OpBuilder::InsertPoint previous)
override {
551 if (previous.isSet())
553 setCommonAttributes(op);
561 template <
typename OpTy>
563 mlir::Value left, mlir::Value right) {
564 if (!resultType.isIntOrFloat())
565 return create<OpTy>(loc, resultType, left, right);
566 mlir::Type signlessType = mlir::IntegerType::get(
567 getContext(), resultType.getIntOrFloatBitWidth(),
568 mlir::IntegerType::SignednessSemantics::Signless);
569 mlir::Type opResType = resultType;
570 if (left.getType().isUnsignedInteger()) {
572 opResType = signlessType;
574 if (right.getType().isUnsignedInteger()) {
576 opResType = signlessType;
578 mlir::Value result = create<OpTy>(loc, opResType, left, right);
579 if (resultType.isUnsignedInteger())
587 void setCommonAttributes(mlir::Operation *op)
const;
593 mlir::arith::FastMathFlags fastMathFlags{};
597 mlir::arith::IntegerOverflowFlags integerOverflowFlags{};
601 mlir::SymbolTable *symbolTable =
nullptr;
606 std::unique_ptr<mlir::DataLayout> dataLayout =
nullptr;
611namespace fir::factory {
633 mlir::Value defaultValue);
667 llvm::StringRef
string);
671std::string uniqueCGIdent(llvm::StringRef prefix, llvm::StringRef name);
677 fir::SequenceType seqTy);
696 mlir::Value component);
707 mlir::Value element);
722 bool needFinalization =
false,
723 bool isTemporaryLHS =
false);
731 bool needFinalization =
false,
732 bool isTemporaryLHS =
false);
744 fir::ArrayLoadOp arrLoad,
748 fir::SequenceType seqTy, mlir::Value memref,
759std::optional<std::int64_t> getExtentFromTriplet(mlir::Value lb, mlir::Value ub,
770 mlir::Value cPtr, mlir::Type ty);
775 mlir::Value cDevPtr, mlir::Type ty);
779 mlir::Location loc, mlir::Value cPtr);
792mlir::Value createConvert(mlir::OpBuilder &, mlir::Location, mlir::Type,
796void setInternalLinkage(mlir::func::FuncOp);
799elideExtentsAlreadyInType(mlir::Type type, mlir::ValueRange shape);
802elideLengthsAlreadyInType(mlir::Type type, mlir::ValueRange lenParams);
805uint64_t getAllocaAddressSpace(mlir::DataLayout *dataLayout);
814 mlir::ValueRange extents2);
Definition: MathOptionsBase.h:22
Definition: BoxValue.h:125
Definition: BoxValue.h:291
Definition: BoxValue.h:478
Definition: FIRBuilder.h:429
mlir::Operation::result_range getResults()
End the IfOp and return the results if any.
Definition: FIRBuilder.h:449
Definition: FIRBuilder.h:55
IfBuilder genIfThenElse(mlir::Location loc, mlir::Value cdt)
Definition: FIRBuilder.h:478
fir::StringLitOp createStringLitOp(mlir::Location loc, llvm::StringRef string)
Convert a StringRef string into a fir::StringLitOp.
Definition: FIRBuilder.cpp:530
fir::GlobalOp createGlobal(mlir::Location loc, mlir::Type type, llvm::StringRef name, mlir::StringAttr linkage={}, mlir::Attribute value={}, bool isConst=false, bool isTarget=false, cuf::DataAttributeAttr dataAttr={})
Create a global value.
Definition: FIRBuilder.cpp:363
mlir::Type getRefType(mlir::Type eleTy)
Safely create a reference type to the type eleTy.
Definition: FIRBuilder.cpp:95
void genStackRestore(mlir::Location loc, mlir::Value stackPointer)
Definition: FIRBuilder.cpp:356
mlir::SymbolRefAttr getSymbolRefAttr(llvm::StringRef str)
Wrap str to a SymbolRefAttr.
Definition: FIRBuilder.h:160
mlir::Value createRealZeroConstant(mlir::Location loc, mlir::Type realType)
Create a real constant of type realType with a value zero.
Definition: FIRBuilder.h:201
mlir::Value createSlice(mlir::Location loc, const fir::ExtendedValue &exv, mlir::ValueRange triples, mlir::ValueRange path)
Definition: FIRBuilder.cpp:596
mlir::Value createConvert(mlir::Location loc, mlir::Type toTy, mlir::Value val)
Lazy creation of fir.convert op.
Definition: FIRBuilder.cpp:511
IfBuilder genIfOp(mlir::Location loc, mlir::TypeRange results, mlir::Value cdt, bool withElseRegion)
Definition: FIRBuilder.h:463
mlir::Value genIsNullAddr(mlir::Location loc, mlir::Value addr)
Generate code testing addr is a null address.
Definition: FIRBuilder.cpp:752
mlir::func::FuncOp getNamedFunction(mlir::SymbolRefAttr symbol)
Definition: FIRBuilder.h:341
mlir::Value createRealConstant(mlir::Location loc, mlir::Type realType, llvm::APFloat::integerPart val)
Create a real constant from an integer value.
Definition: FIRBuilder.cpp:150
mlir::Type getCharacterLengthType()
Get character length type.
Definition: FIRBuilder.h:153
mlir::Value createTemporaryAlloc(mlir::Location loc, mlir::Type type, llvm::StringRef name, mlir::ValueRange lenParams={}, mlir::ValueRange shape={}, llvm::ArrayRef< mlir::NamedAttribute > attrs={}, std::optional< Fortran::common::CUDADataAttr > cudaAttr=std::nullopt)
Definition: FIRBuilder.cpp:290
IfBuilder genIfThen(mlir::Location loc, mlir::Value cdt)
Definition: FIRBuilder.h:471
mlir::Value genStackSave(mlir::Location loc)
Definition: FIRBuilder.cpp:350
mlir::Value genShape(mlir::Location loc, const fir::AbstractArrayBox &arr)
Construct one of the two forms of shape op from an array box.
Definition: FIRBuilder.cpp:562
const fir::KindMapping & getKindMap()
Get a reference to the kind map.
Definition: FIRBuilder.h:116
mlir::Value createAllOnesInteger(mlir::Location loc, mlir::Type integerType)
Definition: FIRBuilder.cpp:140
mlir::ModuleOp getModule()
Get the current Module.
Definition: FIRBuilder.h:106
fir::GlobalOp createGlobalConstant(mlir::Location loc, mlir::Type type, llvm::StringRef name, mlir::StringAttr linkage={}, mlir::Attribute value={})
Create a global constant (read-only) value.
Definition: FIRBuilder.h:289
mlir::Value createMinusOneInteger(mlir::Location loc, mlir::Type integerType)
Definition: FIRBuilder.h:187
mlir::IntegerType getDefaultIntegerType()
Get the default integer type.
Definition: FIRBuilder.h:122
mlir::Value createNullConstant(mlir::Location loc, mlir::Type ptrType={})
Definition: FIRBuilder.cpp:125
void setFastMathFlags(mlir::arith::FastMathFlags flags)
Definition: FIRBuilder.h:507
LLVM_DUMP_METHOD void dumpFunc()
Dump the current function. (debug)
Definition: FIRBuilder.cpp:734
mlir::ArrayAttr create2DI64ArrayAttr(llvm::SmallVectorImpl< llvm::SmallVector< int64_t > > &intData)
Definition: FIRBuilder.cpp:280
mlir::Value convertWithSemantics(mlir::Location loc, mlir::Type toTy, mlir::Value val, bool allowCharacterConversion=false, bool allowRebox=false)
Definition: FIRBuilder.cpp:425
mlir::SymbolTable * getMLIRSymbolTable()
Get func.func/fir.global symbol table attached to this builder if any.
Definition: FIRBuilder.h:119
mlir::Value genIsNotNullAddr(mlir::Location loc, mlir::Value addr)
Generate code testing addr is not a null address.
Definition: FIRBuilder.cpp:746
mlir::Value convertToIndexType(mlir::Location loc, mlir::Value val)
Cast the input value to IndexType.
Definition: FIRBuilder.h:383
void createStoreWithConvert(mlir::Location loc, mlir::Value val, mlir::Value addr)
Definition: FIRBuilder.cpp:516
void setIntegerOverflowFlags(mlir::arith::IntegerOverflowFlags flags)
Definition: FIRBuilder.h:535
mlir::Region & getRegion()
Get the current Region of the insertion point.
Definition: FIRBuilder.h:103
void notifyOperationInserted(mlir::Operation *op, mlir::OpBuilder::InsertPoint previous) override
FirOpBuilder hook for creating new operation.
Definition: FIRBuilder.h:548
mlir::func::FuncOp getNamedFunction(llvm::StringRef name)
Definition: FIRBuilder.h:332
mlir::Value genExtentFromTriplet(mlir::Location loc, mlir::Value lb, mlir::Value ub, mlir::Value step, mlir::Type type)
Definition: FIRBuilder.cpp:758
std::string getFastMathFlagsString()
Definition: FIRBuilder.h:522
mlir::Value createUnsigned(mlir::Location loc, mlir::Type resultType, mlir::Value left, mlir::Value right)
Definition: FIRBuilder.h:562
mlir::Value createBool(mlir::Location loc, bool b)
Create constant i1 with value 1. if b is true or 0. otherwise.
Definition: FIRBuilder.h:417
mlir::arith::IntegerOverflowFlags getIntegerOverflowFlags() const
Get current IntegerOverflowFlags value.
Definition: FIRBuilder.h:540
mlir::func::FuncOp getFunction()
Get the current Function.
Definition: FIRBuilder.h:111
mlir::Value createTemporary(mlir::Location loc, mlir::Type type, llvm::StringRef name={}, mlir::ValueRange shape={}, mlir::ValueRange lenParams={}, llvm::ArrayRef< mlir::NamedAttribute > attrs={}, std::optional< Fortran::common::CUDADataAttr > cudaAttr=std::nullopt)
Definition: FIRBuilder.cpp:312
mlir::Type getRealType(int kind)
Get the mlir float type that implements Fortran REAL(kind).
Definition: FIRBuilder.cpp:105
mlir::Block * getAllocaBlock()
Get the block for adding Allocas.
Definition: FIRBuilder.cpp:250
mlir::Type getVarLenSeqTy(mlir::Type eleTy, unsigned rank=1)
Create a sequence of eleTy with rank dimensions of unknown size.
Definition: FIRBuilder.cpp:100
mlir::Value loadIfRef(mlir::Location loc, mlir::Value val)
Definition: FIRBuilder.cpp:524
mlir::Value createHeapTemporary(mlir::Location loc, mlir::Type type, llvm::StringRef name={}, mlir::ValueRange shape={}, mlir::ValueRange lenParams={}, llvm::ArrayRef< mlir::NamedAttribute > attrs={})
Create a temporary on the heap.
Definition: FIRBuilder.cpp:336
mlir::Value createShape(mlir::Location loc, const fir::ExtendedValue &exv)
Definition: FIRBuilder.cpp:575
mlir::Value allocateLocal(mlir::Location loc, mlir::Type ty, llvm::StringRef uniqName, llvm::StringRef name, bool pinned, llvm::ArrayRef< mlir::Value > shape, llvm::ArrayRef< mlir::Value > lenParams, bool asTarget=false)
Definition: FIRBuilder.cpp:210
mlir::Value genAbsentOp(mlir::Location loc, mlir::Type argTy)
Definition: FIRBuilder.cpp:775
mlir::arith::FastMathFlags getFastMathFlags() const
Get current FastMathFlags value.
Definition: FIRBuilder.h:516
mlir::Value createIntegerConstant(mlir::Location loc, mlir::Type integerType, std::int64_t i)
Definition: FIRBuilder.cpp:131
mlir::Value createBox(mlir::Location loc, const fir::ExtendedValue &exv, bool isPolymorphic=false, bool isAssumedType=false)
Definition: FIRBuilder.cpp:647
mlir::Block * getEntryBlock()
Get the entry block of the current Function.
Definition: FIRBuilder.h:139
mlir::func::FuncOp createFunction(mlir::Location loc, llvm::StringRef name, mlir::FunctionType ty)
Definition: FIRBuilder.h:371
mlir::DataLayout & getDataLayout()
Construct a data layout on demand and return it.
Definition: FIRBuilder.cpp:837
mlir::Value createTemporary(mlir::Location loc, mlir::Type type, mlir::ValueRange shape)
Create an unnamed and untracked temporary on the stack.
Definition: FIRBuilder.h:242
mlir::Type getIntPtrType()
Definition: FIRBuilder.h:157
Definition: KindMapping.h:48
fir::ExtendedValue createBoxValue(mlir::Location loc, AbstractConverter &converter, const SomeExpr &expr, SymMap &symMap, StatementContext &stmtCtx)
Definition: ConvertExpr.cpp:7562
Definition: AbstractConverter.h:31
KindMapping getKindMapping(mlir::ModuleOp mod)
Definition: FIRContext.cpp:43
mlir::Type getIntPtrType(mlir::OpBuilder &builder)
Get the integer type with a pointer size.
Definition: FIRBuilder.h:43
bool isAssumedType(mlir::Type ty)
Definition: FIRType.cpp:320
Definition: AbstractConverter.h:27