xref: /llvm-project/flang/include/flang/Lower/ConvertConstant.h (revision 3a47d948ba1b0ebe99ff068ddf28fe9e6043e932)
1 //===-- ConvertConstant.h -- lowering of constants --------------*- 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 ///
13 /// Implements the conversion from evaluate::Constant to FIR.
14 ///
15 //===----------------------------------------------------------------------===//
16 
17 #ifndef FORTRAN_LOWER_CONVERTCONSTANT_H
18 #define FORTRAN_LOWER_CONVERTCONSTANT_H
19 
20 #include "flang/Evaluate/constant.h"
21 #include "flang/Lower/Support/Utils.h"
22 #include "flang/Optimizer/Builder/BoxValue.h"
23 #include "flang/Optimizer/Builder/FIRBuilder.h"
24 
25 namespace Fortran::lower {
26 class AbstractConverter;
27 
28 /// Class to lower evaluate::Constant to fir::ExtendedValue.
29 template <typename T>
30 class ConstantBuilder {
31 public:
32   /// Lower \p constant into a fir::ExtendedValue.
33   /// If \p outlineBigConstantsInReadOnlyMemory is set, character, derived
34   /// type, and array constants will be lowered into read only memory
35   /// fir.global, and the resulting fir::ExtendedValue will contain the address
36   /// of the fir.global. This option should not be set if the constant is being
37   /// lowered while the builder is already in a fir.global body because
38   /// fir.global initialization body cannot contain code manipulating memory
39   /// (e.g.  fir.load/fir.store...).
40   static fir::ExtendedValue gen(Fortran::lower::AbstractConverter &converter,
41                                 mlir::Location loc,
42                                 const evaluate::Constant<T> &constant,
43                                 bool outlineBigConstantsInReadOnlyMemory);
44 };
45 using namespace evaluate;
46 FOR_EACH_SPECIFIC_TYPE(extern template class ConstantBuilder, )
47 
48 template <typename T>
convertConstant(Fortran::lower::AbstractConverter & converter,mlir::Location loc,const evaluate::Constant<T> & constant,bool outlineBigConstantsInReadOnlyMemory)49 fir::ExtendedValue convertConstant(Fortran::lower::AbstractConverter &converter,
50                                    mlir::Location loc,
51                                    const evaluate::Constant<T> &constant,
52                                    bool outlineBigConstantsInReadOnlyMemory) {
53   return ConstantBuilder<T>::gen(converter, loc, constant,
54                                  outlineBigConstantsInReadOnlyMemory);
55 }
56 
57 /// Create a fir.global array with a dense attribute containing the value of
58 /// \p initExpr.
59 /// Using a dense attribute allows faster MLIR compilation times compared to
60 /// creating an initialization body for the initial value. However, a dense
61 /// attribute can only be created if initExpr is a non-empty rank 1 numerical or
62 /// logical Constant<T>. Otherwise, the value returned will be null.
63 fir::GlobalOp tryCreatingDenseGlobal(fir::FirOpBuilder &builder,
64                                      mlir::Location loc, mlir::Type symTy,
65                                      llvm::StringRef globalName,
66                                      mlir::StringAttr linkage, bool isConst,
67                                      const Fortran::lower::SomeExpr &initExpr,
68                                      cuf::DataAttributeAttr dataAttr = {});
69 
70 /// Lower a StructureConstructor that must be lowered in read only data although
71 /// it may not be wrapped into a Constant<T> (this may be the case for derived
72 /// type descriptor compiler generated data that is not fully compliant with
73 /// Fortran constant expression but can and must still be lowered into read only
74 /// memory).
75 fir::ExtendedValue
76 genInlinedStructureCtorLit(Fortran::lower::AbstractConverter &converter,
77                            mlir::Location loc,
78                            const Fortran::evaluate::StructureConstructor &ctor);
79 
80 } // namespace Fortran::lower
81 
82 #endif // FORTRAN_LOWER_CONVERTCONSTANT_H
83