diff --git a/flang/include/flang/Lower/Bridge.h b/flang/include/flang/Lower/Bridge.h --- a/flang/include/flang/Lower/Bridge.h +++ b/flang/include/flang/Lower/Bridge.h @@ -19,7 +19,7 @@ #include "flang/Lower/LoweringOptions.h" #include "flang/Lower/StatementContext.h" #include "flang/Optimizer/Builder/FIRBuilder.h" -#include "flang/Optimizer/Support/KindMapping.h" +#include "flang/Optimizer/Dialect/Support/KindMapping.h" #include "mlir/IR/BuiltinOps.h" namespace Fortran { diff --git a/flang/include/flang/Optimizer/Builder/FIRBuilder.h b/flang/include/flang/Optimizer/Builder/FIRBuilder.h --- a/flang/include/flang/Optimizer/Builder/FIRBuilder.h +++ b/flang/include/flang/Optimizer/Builder/FIRBuilder.h @@ -20,7 +20,7 @@ #include "flang/Optimizer/Dialect/FIROps.h" #include "flang/Optimizer/Dialect/FIROpsSupport.h" #include "flang/Optimizer/Dialect/FIRType.h" -#include "flang/Optimizer/Support/KindMapping.h" +#include "flang/Optimizer/Dialect/Support/KindMapping.h" #include "mlir/IR/Builders.h" #include "mlir/IR/BuiltinOps.h" #include "llvm/ADT/DenseMap.h" diff --git a/flang/include/flang/Optimizer/Dialect/Support/FIRContext.h b/flang/include/flang/Optimizer/Dialect/Support/FIRContext.h new file mode 100644 --- /dev/null +++ b/flang/include/flang/Optimizer/Dialect/Support/FIRContext.h @@ -0,0 +1,56 @@ +//===-- Optimizer/Support/FIRContext.h --------------------------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// Coding style: https://mlir.llvm.org/getting_started/DeveloperGuide/ +// +//===----------------------------------------------------------------------===// +/// Setters and getters for associating context with an instance of a ModuleOp. +/// The context is typically set by the tool and needed in later stages to +/// determine how to correctly generate code. +//===----------------------------------------------------------------------===// + +#ifndef FORTRAN_OPTIMIZER_SUPPORT_FIRCONTEXT_H +#define FORTRAN_OPTIMIZER_SUPPORT_FIRCONTEXT_H + +#include "llvm/ADT/StringRef.h" +#include "llvm/TargetParser/Triple.h" + +namespace mlir { +class ModuleOp; +} // namespace mlir + +namespace fir { +class KindMapping; +struct NameUniquer; + +/// Set the target triple for the module. `triple` must not be deallocated while +/// module `mod` is still live. +void setTargetTriple(mlir::ModuleOp mod, llvm::StringRef triple); + +/// Get the Triple instance from the Module or return the default Triple. +llvm::Triple getTargetTriple(mlir::ModuleOp mod); + +/// Set the kind mapping for the module. `kindMap` must not be deallocated while +/// module `mod` is still live. +void setKindMapping(mlir::ModuleOp mod, KindMapping &kindMap); + +/// Get the KindMapping instance from the Module. If none was set, returns a +/// default. +KindMapping getKindMapping(mlir::ModuleOp mod); + +/// Helper for determining the target from the host, etc. Tools may use this +/// function to provide a consistent interpretation of the `--target=` +/// command-line option. +/// An empty string ("") or "default" will specify that the default triple +/// should be used. "native" will specify that the host machine be used to +/// construct the triple. +std::string determineTargetTriple(llvm::StringRef triple); + +} // namespace fir + +#endif // FORTRAN_OPTIMIZER_SUPPORT_FIRCONTEXT_H diff --git a/flang/include/flang/Optimizer/Dialect/Support/KindMapping.h b/flang/include/flang/Optimizer/Dialect/Support/KindMapping.h new file mode 100644 --- /dev/null +++ b/flang/include/flang/Optimizer/Dialect/Support/KindMapping.h @@ -0,0 +1,137 @@ +//===-- Optimizer/Support/KindMapping.h -- support kind mapping -*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// Coding style: https://mlir.llvm.org/getting_started/DeveloperGuide/ +// +//===----------------------------------------------------------------------===// + +#ifndef FORTRAN_OPTIMIZER_SUPPORT_KINDMAPPING_H +#define FORTRAN_OPTIMIZER_SUPPORT_KINDMAPPING_H + +#include "mlir/IR/OpDefinition.h" +#include "llvm/ADT/DenseMap.h" +#include "llvm/IR/Type.h" + +namespace llvm { +struct fltSemantics; +} // namespace llvm + +namespace fir { + +/// The kind mapping is an encoded string that informs FIR how the Fortran KIND +/// values from the front-end should be converted to LLVM IR types. This +/// encoding allows the mapping from front-end KIND values to backend LLVM IR +/// types to be customized by the front-end. +/// +/// The provided string uses the following syntax. +/// +/// intrinsic-key `:` kind-value (`,` intrinsic-key `:` kind-value)* +/// +/// intrinsic-key is a single character for the intrinsic type. +/// 'i' : INTEGER (size in bits) +/// 'l' : LOGICAL (size in bits) +/// 'a' : CHARACTER (size in bits) +/// 'r' : REAL (encoding value) +/// 'c' : COMPLEX (encoding value) +/// +/// kind-value is either an unsigned integer (for 'i', 'l', and 'a') or one of +/// 'Half', 'BFloat', 'Float', 'Double', 'X86_FP80', or 'FP128' (for 'r' and +/// 'c'). +/// +/// If LLVM adds support for new floating-point types, the final list should be +/// extended. +class KindMapping { +public: + using KindTy = unsigned; + using Bitsize = unsigned; + using LLVMTypeID = llvm::Type::TypeID; + using MatchResult = mlir::ParseResult; + + /// KindMapping constructor with both the kind map and default kinds read from + /// command-line options. + explicit KindMapping(mlir::MLIRContext *context); + /// KindMapping constructor taking a `defs` argument to specify the default + /// kinds for intrinsic types. To set the default kinds, an ArrayRef of 6 + /// KindTy must be passed. The kinds must be the given in the following order: + /// CHARACTER, COMPLEX, DOUBLE PRECISION, INTEGER, LOGICAL, and REAL. The + /// kind map is read from command-line options, if given. + explicit KindMapping(mlir::MLIRContext *context, llvm::ArrayRef defs); + /// KindMapping constructor taking an optional `defs` argument to specify the + /// default kinds for intrinsic types. To set the default kinds, an ArrayRef + /// of 6 KindTy must be passed. The kinds must be the given in the following + /// order: CHARACTER, COMPLEX, DOUBLE PRECISION, INTEGER, LOGICAL, and REAL. + explicit KindMapping(mlir::MLIRContext *context, llvm::StringRef map, + llvm::ArrayRef defs = std::nullopt); + explicit KindMapping(mlir::MLIRContext *context, llvm::StringRef map, + llvm::StringRef defs) + : KindMapping{context, map, toDefaultKinds(defs)} {} + + /// Get the size in bits of !fir.char + Bitsize getCharacterBitsize(KindTy kind) const; + + /// Get the size in bits of !fir.int + Bitsize getIntegerBitsize(KindTy kind) const; + + /// Get the size in bits of !fir.logical + Bitsize getLogicalBitsize(KindTy kind) const; + + /// Get the size in bits of !fir.real + Bitsize getRealBitsize(KindTy kind) const; + + /// Get the LLVM Type::TypeID of !fir.real + LLVMTypeID getRealTypeID(KindTy kind) const; + + /// Get the LLVM Type::TypeID of !fir.complex + LLVMTypeID getComplexTypeID(KindTy kind) const; + + mlir::MLIRContext *getContext() const { return context; } + + /// Get the float semantics of !fir.real + const llvm::fltSemantics &getFloatSemantics(KindTy kind) const; + + /// Get the default kind map as a string. + static constexpr const char *getDefaultMap() { return ""; } + + /// Convert the current kind map to a string. + std::string mapToString() const; + + //===--------------------------------------------------------------------===// + // Default kinds of intrinsic types + //===--------------------------------------------------------------------===// + + KindTy defaultCharacterKind() const; + KindTy defaultComplexKind() const; + KindTy defaultDoubleKind() const; + KindTy defaultIntegerKind() const; + KindTy defaultLogicalKind() const; + KindTy defaultRealKind() const; + + /// Get the default kinds as a string. + static constexpr const char *getDefaultKinds() { return "a1c4d8i4l4r4"; } + + /// Convert the current default kinds to a string. + std::string defaultsToString() const; + + /// Translate a default kinds string into a default kind vector. This vector + /// can be passed to the KindMapping ctor. + static std::vector toDefaultKinds(llvm::StringRef defs); + +private: + MatchResult badMapString(const llvm::Twine &ptr); + MatchResult parse(llvm::StringRef kindMap); + mlir::LogicalResult setDefaultKinds(llvm::ArrayRef defs); + + mlir::MLIRContext *context; + llvm::DenseMap, Bitsize> intMap; + llvm::DenseMap, LLVMTypeID> floatMap; + llvm::DenseMap defaultMap; +}; + +} // namespace fir + +#endif // FORTRAN_OPTIMIZER_SUPPORT_KINDMAPPING_H diff --git a/flang/include/flang/Optimizer/Support/FIRContext.h b/flang/include/flang/Optimizer/Support/FIRContext.h deleted file mode 100644 --- a/flang/include/flang/Optimizer/Support/FIRContext.h +++ /dev/null @@ -1,56 +0,0 @@ -//===-- Optimizer/Support/FIRContext.h --------------------------*- C++ -*-===// -// -// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. -// See https://llvm.org/LICENSE.txt for license information. -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -// -//===----------------------------------------------------------------------===// -// -// Coding style: https://mlir.llvm.org/getting_started/DeveloperGuide/ -// -//===----------------------------------------------------------------------===// -/// Setters and getters for associating context with an instance of a ModuleOp. -/// The context is typically set by the tool and needed in later stages to -/// determine how to correctly generate code. -//===----------------------------------------------------------------------===// - -#ifndef FORTRAN_OPTIMIZER_SUPPORT_FIRCONTEXT_H -#define FORTRAN_OPTIMIZER_SUPPORT_FIRCONTEXT_H - -#include "llvm/ADT/StringRef.h" -#include "llvm/TargetParser/Triple.h" - -namespace mlir { -class ModuleOp; -} // namespace mlir - -namespace fir { -class KindMapping; -struct NameUniquer; - -/// Set the target triple for the module. `triple` must not be deallocated while -/// module `mod` is still live. -void setTargetTriple(mlir::ModuleOp mod, llvm::StringRef triple); - -/// Get the Triple instance from the Module or return the default Triple. -llvm::Triple getTargetTriple(mlir::ModuleOp mod); - -/// Set the kind mapping for the module. `kindMap` must not be deallocated while -/// module `mod` is still live. -void setKindMapping(mlir::ModuleOp mod, KindMapping &kindMap); - -/// Get the KindMapping instance from the Module. If none was set, returns a -/// default. -KindMapping getKindMapping(mlir::ModuleOp mod); - -/// Helper for determining the target from the host, etc. Tools may use this -/// function to provide a consistent interpretation of the `--target=` -/// command-line option. -/// An empty string ("") or "default" will specify that the default triple -/// should be used. "native" will specify that the host machine be used to -/// construct the triple. -std::string determineTargetTriple(llvm::StringRef triple); - -} // namespace fir - -#endif // FORTRAN_OPTIMIZER_SUPPORT_FIRCONTEXT_H diff --git a/flang/include/flang/Optimizer/Support/KindMapping.h b/flang/include/flang/Optimizer/Support/KindMapping.h deleted file mode 100644 --- a/flang/include/flang/Optimizer/Support/KindMapping.h +++ /dev/null @@ -1,137 +0,0 @@ -//===-- Optimizer/Support/KindMapping.h -- support kind mapping -*- C++ -*-===// -// -// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. -// See https://llvm.org/LICENSE.txt for license information. -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -// -//===----------------------------------------------------------------------===// -// -// Coding style: https://mlir.llvm.org/getting_started/DeveloperGuide/ -// -//===----------------------------------------------------------------------===// - -#ifndef FORTRAN_OPTIMIZER_SUPPORT_KINDMAPPING_H -#define FORTRAN_OPTIMIZER_SUPPORT_KINDMAPPING_H - -#include "mlir/IR/OpDefinition.h" -#include "llvm/ADT/DenseMap.h" -#include "llvm/IR/Type.h" - -namespace llvm { -struct fltSemantics; -} // namespace llvm - -namespace fir { - -/// The kind mapping is an encoded string that informs FIR how the Fortran KIND -/// values from the front-end should be converted to LLVM IR types. This -/// encoding allows the mapping from front-end KIND values to backend LLVM IR -/// types to be customized by the front-end. -/// -/// The provided string uses the following syntax. -/// -/// intrinsic-key `:` kind-value (`,` intrinsic-key `:` kind-value)* -/// -/// intrinsic-key is a single character for the intrinsic type. -/// 'i' : INTEGER (size in bits) -/// 'l' : LOGICAL (size in bits) -/// 'a' : CHARACTER (size in bits) -/// 'r' : REAL (encoding value) -/// 'c' : COMPLEX (encoding value) -/// -/// kind-value is either an unsigned integer (for 'i', 'l', and 'a') or one of -/// 'Half', 'BFloat', 'Float', 'Double', 'X86_FP80', or 'FP128' (for 'r' and -/// 'c'). -/// -/// If LLVM adds support for new floating-point types, the final list should be -/// extended. -class KindMapping { -public: - using KindTy = unsigned; - using Bitsize = unsigned; - using LLVMTypeID = llvm::Type::TypeID; - using MatchResult = mlir::ParseResult; - - /// KindMapping constructor with both the kind map and default kinds read from - /// command-line options. - explicit KindMapping(mlir::MLIRContext *context); - /// KindMapping constructor taking a `defs` argument to specify the default - /// kinds for intrinsic types. To set the default kinds, an ArrayRef of 6 - /// KindTy must be passed. The kinds must be the given in the following order: - /// CHARACTER, COMPLEX, DOUBLE PRECISION, INTEGER, LOGICAL, and REAL. The - /// kind map is read from command-line options, if given. - explicit KindMapping(mlir::MLIRContext *context, llvm::ArrayRef defs); - /// KindMapping constructor taking an optional `defs` argument to specify the - /// default kinds for intrinsic types. To set the default kinds, an ArrayRef - /// of 6 KindTy must be passed. The kinds must be the given in the following - /// order: CHARACTER, COMPLEX, DOUBLE PRECISION, INTEGER, LOGICAL, and REAL. - explicit KindMapping(mlir::MLIRContext *context, llvm::StringRef map, - llvm::ArrayRef defs = std::nullopt); - explicit KindMapping(mlir::MLIRContext *context, llvm::StringRef map, - llvm::StringRef defs) - : KindMapping{context, map, toDefaultKinds(defs)} {} - - /// Get the size in bits of !fir.char - Bitsize getCharacterBitsize(KindTy kind) const; - - /// Get the size in bits of !fir.int - Bitsize getIntegerBitsize(KindTy kind) const; - - /// Get the size in bits of !fir.logical - Bitsize getLogicalBitsize(KindTy kind) const; - - /// Get the size in bits of !fir.real - Bitsize getRealBitsize(KindTy kind) const; - - /// Get the LLVM Type::TypeID of !fir.real - LLVMTypeID getRealTypeID(KindTy kind) const; - - /// Get the LLVM Type::TypeID of !fir.complex - LLVMTypeID getComplexTypeID(KindTy kind) const; - - mlir::MLIRContext *getContext() const { return context; } - - /// Get the float semantics of !fir.real - const llvm::fltSemantics &getFloatSemantics(KindTy kind) const; - - /// Get the default kind map as a string. - static constexpr const char *getDefaultMap() { return ""; } - - /// Convert the current kind map to a string. - std::string mapToString() const; - - //===--------------------------------------------------------------------===// - // Default kinds of intrinsic types - //===--------------------------------------------------------------------===// - - KindTy defaultCharacterKind() const; - KindTy defaultComplexKind() const; - KindTy defaultDoubleKind() const; - KindTy defaultIntegerKind() const; - KindTy defaultLogicalKind() const; - KindTy defaultRealKind() const; - - /// Get the default kinds as a string. - static constexpr const char *getDefaultKinds() { return "a1c4d8i4l4r4"; } - - /// Convert the current default kinds to a string. - std::string defaultsToString() const; - - /// Translate a default kinds string into a default kind vector. This vector - /// can be passed to the KindMapping ctor. - static std::vector toDefaultKinds(llvm::StringRef defs); - -private: - MatchResult badMapString(const llvm::Twine &ptr); - MatchResult parse(llvm::StringRef kindMap); - mlir::LogicalResult setDefaultKinds(llvm::ArrayRef defs); - - mlir::MLIRContext *context; - llvm::DenseMap, Bitsize> intMap; - llvm::DenseMap, LLVMTypeID> floatMap; - llvm::DenseMap defaultMap; -}; - -} // namespace fir - -#endif // FORTRAN_OPTIMIZER_SUPPORT_KINDMAPPING_H diff --git a/flang/lib/Frontend/CMakeLists.txt b/flang/lib/Frontend/CMakeLists.txt --- a/flang/lib/Frontend/CMakeLists.txt +++ b/flang/lib/Frontend/CMakeLists.txt @@ -13,9 +13,7 @@ TextDiagnostic.cpp DEPENDS - FIRBuilder FIRDialect - FIRSupport FIROptTransformsPassIncGen HLFIRDialect MLIRIR @@ -28,6 +26,7 @@ FortranCommon FortranLower FIRDialect + FIRDialectSupport FIRSupport FIRBuilder FIRCodeGen diff --git a/flang/lib/Frontend/FrontendActions.cpp b/flang/lib/Frontend/FrontendActions.cpp --- a/flang/lib/Frontend/FrontendActions.cpp +++ b/flang/lib/Frontend/FrontendActions.cpp @@ -18,9 +18,9 @@ #include "flang/Lower/Bridge.h" #include "flang/Lower/PFTBuilder.h" #include "flang/Lower/Support/Verifier.h" -#include "flang/Optimizer/Support/FIRContext.h" +#include "flang/Optimizer/Dialect/Support/FIRContext.h" +#include "flang/Optimizer/Dialect/Support/KindMapping.h" #include "flang/Optimizer/Support/InitFIR.h" -#include "flang/Optimizer/Support/KindMapping.h" #include "flang/Optimizer/Support/Utils.h" #include "flang/Parser/dump-parse-tree.h" #include "flang/Parser/parsing.h" diff --git a/flang/lib/Lower/Bridge.cpp b/flang/lib/Lower/Bridge.cpp --- a/flang/lib/Lower/Bridge.cpp +++ b/flang/lib/Lower/Bridge.cpp @@ -41,8 +41,8 @@ #include "flang/Optimizer/Dialect/FIRAttr.h" #include "flang/Optimizer/Dialect/FIRDialect.h" #include "flang/Optimizer/Dialect/FIROps.h" +#include "flang/Optimizer/Dialect/Support/FIRContext.h" #include "flang/Optimizer/HLFIR/HLFIROps.h" -#include "flang/Optimizer/Support/FIRContext.h" #include "flang/Optimizer/Support/FatalError.h" #include "flang/Optimizer/Support/InternalNames.h" #include "flang/Optimizer/Transforms/Passes.h" diff --git a/flang/lib/Lower/CMakeLists.txt b/flang/lib/Lower/CMakeLists.txt --- a/flang/lib/Lower/CMakeLists.txt +++ b/flang/lib/Lower/CMakeLists.txt @@ -27,17 +27,16 @@ Runtime.cpp SymbolMap.cpp VectorSubscripts.cpp - + DEPENDS FIRDialect - FIRBuilder - FIRSupport FIRTransforms HLFIRDialect ${dialect_libs} LINK_LIBS FIRDialect + FIRDialectSupport FIRBuilder FIRSupport FIRTransforms diff --git a/flang/lib/Lower/ConvertVariable.cpp b/flang/lib/Lower/ConvertVariable.cpp --- a/flang/lib/Lower/ConvertVariable.cpp +++ b/flang/lib/Lower/ConvertVariable.cpp @@ -32,8 +32,8 @@ #include "flang/Optimizer/Dialect/FIRAttr.h" #include "flang/Optimizer/Dialect/FIRDialect.h" #include "flang/Optimizer/Dialect/FIROps.h" +#include "flang/Optimizer/Dialect/Support/FIRContext.h" #include "flang/Optimizer/HLFIR/HLFIROps.h" -#include "flang/Optimizer/Support/FIRContext.h" #include "flang/Optimizer/Support/FatalError.h" #include "flang/Optimizer/Support/InternalNames.h" #include "flang/Semantics/runtime-type-info.h" diff --git a/flang/lib/Lower/IO.cpp b/flang/lib/Lower/IO.cpp --- a/flang/lib/Lower/IO.cpp +++ b/flang/lib/Lower/IO.cpp @@ -29,7 +29,7 @@ #include "flang/Optimizer/Builder/Runtime/RTBuilder.h" #include "flang/Optimizer/Builder/Todo.h" #include "flang/Optimizer/Dialect/FIRDialect.h" -#include "flang/Optimizer/Support/FIRContext.h" +#include "flang/Optimizer/Dialect/Support/FIRContext.h" #include "flang/Parser/parse-tree.h" #include "flang/Runtime/io-api.h" #include "flang/Semantics/tools.h" diff --git a/flang/lib/Optimizer/Analysis/CMakeLists.txt b/flang/lib/Optimizer/Analysis/CMakeLists.txt --- a/flang/lib/Optimizer/Analysis/CMakeLists.txt +++ b/flang/lib/Optimizer/Analysis/CMakeLists.txt @@ -2,9 +2,8 @@ AliasAnalysis.cpp DEPENDS - FIRBuilder FIRDialect - FIRSupport + MLIRIR LINK_LIBS FIRBuilder diff --git a/flang/lib/Optimizer/Builder/CMakeLists.txt b/flang/lib/Optimizer/Builder/CMakeLists.txt --- a/flang/lib/Optimizer/Builder/CMakeLists.txt +++ b/flang/lib/Optimizer/Builder/CMakeLists.txt @@ -27,12 +27,12 @@ DEPENDS FIRDialect - FIRSupport HLFIRDialect ${dialect_libs} LINK_LIBS FIRDialect + FIRDialectSupport FIRSupport HLFIRDialect ${dialect_libs} diff --git a/flang/lib/Optimizer/Builder/IntrinsicCall.cpp b/flang/lib/Optimizer/Builder/IntrinsicCall.cpp --- a/flang/lib/Optimizer/Builder/IntrinsicCall.cpp +++ b/flang/lib/Optimizer/Builder/IntrinsicCall.cpp @@ -33,7 +33,7 @@ #include "flang/Optimizer/Builder/Runtime/Transformational.h" #include "flang/Optimizer/Builder/Todo.h" #include "flang/Optimizer/Dialect/FIROpsSupport.h" -#include "flang/Optimizer/Support/FIRContext.h" +#include "flang/Optimizer/Dialect/Support/FIRContext.h" #include "flang/Optimizer/Support/FatalError.h" #include "flang/Optimizer/Support/Utils.h" #include "flang/Runtime/entry-names.h" diff --git a/flang/lib/Optimizer/CodeGen/BoxedProcedure.cpp b/flang/lib/Optimizer/CodeGen/BoxedProcedure.cpp --- a/flang/lib/Optimizer/CodeGen/BoxedProcedure.cpp +++ b/flang/lib/Optimizer/CodeGen/BoxedProcedure.cpp @@ -13,7 +13,7 @@ #include "flang/Optimizer/Dialect/FIRDialect.h" #include "flang/Optimizer/Dialect/FIROps.h" #include "flang/Optimizer/Dialect/FIRType.h" -#include "flang/Optimizer/Support/FIRContext.h" +#include "flang/Optimizer/Dialect/Support/FIRContext.h" #include "flang/Optimizer/Support/FatalError.h" #include "flang/Optimizer/Support/InternalNames.h" #include "mlir/IR/PatternMatch.h" diff --git a/flang/lib/Optimizer/CodeGen/CMakeLists.txt b/flang/lib/Optimizer/CodeGen/CMakeLists.txt --- a/flang/lib/Optimizer/CodeGen/CMakeLists.txt +++ b/flang/lib/Optimizer/CodeGen/CMakeLists.txt @@ -8,15 +8,14 @@ TargetRewrite.cpp DEPENDS - FIRBuilder FIRDialect - FIRSupport FIROptCodeGenPassIncGen CGOpsIncGen LINK_LIBS FIRBuilder FIRDialect + FIRDialectSupport FIRSupport MLIRComplexToLLVM MLIRComplexToStandard diff --git a/flang/lib/Optimizer/CodeGen/PreCGRewrite.cpp b/flang/lib/Optimizer/CodeGen/PreCGRewrite.cpp --- a/flang/lib/Optimizer/CodeGen/PreCGRewrite.cpp +++ b/flang/lib/Optimizer/CodeGen/PreCGRewrite.cpp @@ -17,7 +17,7 @@ #include "flang/Optimizer/Dialect/FIRDialect.h" #include "flang/Optimizer/Dialect/FIROps.h" #include "flang/Optimizer/Dialect/FIRType.h" -#include "flang/Optimizer/Support/FIRContext.h" +#include "flang/Optimizer/Dialect/Support/FIRContext.h" #include "mlir/Transforms/DialectConversion.h" #include "mlir/Transforms/RegionUtils.h" #include "llvm/ADT/STLExtras.h" diff --git a/flang/lib/Optimizer/CodeGen/Target.h b/flang/lib/Optimizer/CodeGen/Target.h --- a/flang/lib/Optimizer/CodeGen/Target.h +++ b/flang/lib/Optimizer/CodeGen/Target.h @@ -13,7 +13,7 @@ #ifndef FORTRAN_OPTMIZER_CODEGEN_TARGET_H #define FORTRAN_OPTMIZER_CODEGEN_TARGET_H -#include "flang/Optimizer/Support/KindMapping.h" +#include "flang/Optimizer/Dialect/Support/KindMapping.h" #include "mlir/IR/BuiltinTypes.h" #include "llvm/TargetParser/Triple.h" #include diff --git a/flang/lib/Optimizer/CodeGen/Target.cpp b/flang/lib/Optimizer/CodeGen/Target.cpp --- a/flang/lib/Optimizer/CodeGen/Target.cpp +++ b/flang/lib/Optimizer/CodeGen/Target.cpp @@ -13,8 +13,8 @@ #include "Target.h" #include "flang/Optimizer/Builder/Todo.h" #include "flang/Optimizer/Dialect/FIRType.h" +#include "flang/Optimizer/Dialect/Support/KindMapping.h" #include "flang/Optimizer/Support/FatalError.h" -#include "flang/Optimizer/Support/KindMapping.h" #include "mlir/IR/BuiltinTypes.h" #include "mlir/IR/TypeRange.h" diff --git a/flang/lib/Optimizer/CodeGen/TargetRewrite.cpp b/flang/lib/Optimizer/CodeGen/TargetRewrite.cpp --- a/flang/lib/Optimizer/CodeGen/TargetRewrite.cpp +++ b/flang/lib/Optimizer/CodeGen/TargetRewrite.cpp @@ -24,7 +24,7 @@ #include "flang/Optimizer/Dialect/FIROps.h" #include "flang/Optimizer/Dialect/FIROpsSupport.h" #include "flang/Optimizer/Dialect/FIRType.h" -#include "flang/Optimizer/Support/FIRContext.h" +#include "flang/Optimizer/Dialect/Support/FIRContext.h" #include "mlir/Transforms/DialectConversion.h" #include "llvm/ADT/STLExtras.h" #include "llvm/ADT/TypeSwitch.h" diff --git a/flang/lib/Optimizer/CodeGen/TypeConverter.h b/flang/lib/Optimizer/CodeGen/TypeConverter.h --- a/flang/lib/Optimizer/CodeGen/TypeConverter.h +++ b/flang/lib/Optimizer/CodeGen/TypeConverter.h @@ -18,8 +18,8 @@ #include "Target.h" #include "flang/Optimizer/Builder/Todo.h" // remove when TODO's are done #include "flang/Optimizer/Dialect/FIRType.h" -#include "flang/Optimizer/Support/FIRContext.h" -#include "flang/Optimizer/Support/KindMapping.h" +#include "flang/Optimizer/Dialect/Support/FIRContext.h" +#include "flang/Optimizer/Dialect/Support/KindMapping.h" #include "mlir/Conversion/LLVMCommon/TypeConverter.h" #include "llvm/Support/Debug.h" diff --git a/flang/lib/Optimizer/Dialect/CMakeLists.txt b/flang/lib/Optimizer/Dialect/CMakeLists.txt --- a/flang/lib/Optimizer/Dialect/CMakeLists.txt +++ b/flang/lib/Optimizer/Dialect/CMakeLists.txt @@ -1,3 +1,5 @@ +add_subdirectory(Support) + add_flang_library(FIRDialect FIRAttr.cpp FIRDialect.cpp @@ -8,11 +10,11 @@ DEPENDS CanonicalizationPatternsIncGen - FIRSupport + MLIRIR FIROpsIncGen LINK_LIBS - FIRSupport + FIRDialectSupport MLIRArithDialect MLIROpenMPToLLVM MLIRLLVMToLLVMIRTranslation diff --git a/flang/lib/Optimizer/Dialect/FIRAttr.cpp b/flang/lib/Optimizer/Dialect/FIRAttr.cpp --- a/flang/lib/Optimizer/Dialect/FIRAttr.cpp +++ b/flang/lib/Optimizer/Dialect/FIRAttr.cpp @@ -12,7 +12,7 @@ #include "flang/Optimizer/Dialect/FIRAttr.h" #include "flang/Optimizer/Dialect/FIRDialect.h" -#include "flang/Optimizer/Support/KindMapping.h" +#include "flang/Optimizer/Dialect/Support/KindMapping.h" #include "mlir/IR/AttributeSupport.h" #include "mlir/IR/BuiltinTypes.h" #include "mlir/IR/DialectImplementation.h" diff --git a/flang/lib/Optimizer/Dialect/FIROps.cpp b/flang/lib/Optimizer/Dialect/FIROps.cpp --- a/flang/lib/Optimizer/Dialect/FIROps.cpp +++ b/flang/lib/Optimizer/Dialect/FIROps.cpp @@ -14,8 +14,8 @@ #include "flang/Optimizer/Dialect/FIRAttr.h" #include "flang/Optimizer/Dialect/FIROpsSupport.h" #include "flang/Optimizer/Dialect/FIRType.h" -#include "flang/Optimizer/Support/FIRContext.h" -#include "flang/Optimizer/Support/KindMapping.h" +#include "flang/Optimizer/Dialect/Support/FIRContext.h" +#include "flang/Optimizer/Dialect/Support/KindMapping.h" #include "flang/Optimizer/Support/Utils.h" #include "mlir/Dialect/CommonFolders.h" #include "mlir/Dialect/Func/IR/FuncOps.h" diff --git a/flang/lib/Optimizer/Dialect/FIRType.cpp b/flang/lib/Optimizer/Dialect/FIRType.cpp --- a/flang/lib/Optimizer/Dialect/FIRType.cpp +++ b/flang/lib/Optimizer/Dialect/FIRType.cpp @@ -12,7 +12,7 @@ #include "flang/Optimizer/Dialect/FIRType.h" #include "flang/Optimizer/Dialect/FIRDialect.h" -#include "flang/Optimizer/Support/KindMapping.h" +#include "flang/Optimizer/Dialect/Support/KindMapping.h" #include "flang/Tools/PointerModels.h" #include "mlir/IR/Builders.h" #include "mlir/IR/BuiltinDialect.h" diff --git a/flang/lib/Optimizer/Dialect/Support/CMakeLists.txt b/flang/lib/Optimizer/Dialect/Support/CMakeLists.txt new file mode 100644 --- /dev/null +++ b/flang/lib/Optimizer/Dialect/Support/CMakeLists.txt @@ -0,0 +1,12 @@ +get_property(dialect_libs GLOBAL PROPERTY MLIR_DIALECT_LIBS) + +add_flang_library(FIRDialectSupport + KindMapping.cpp + FIRContext.cpp + + DEPENDS + MLIRIR + + LINK_LIBS + ${dialect_libs} +) diff --git a/flang/lib/Optimizer/Dialect/Support/FIRContext.cpp b/flang/lib/Optimizer/Dialect/Support/FIRContext.cpp new file mode 100644 --- /dev/null +++ b/flang/lib/Optimizer/Dialect/Support/FIRContext.cpp @@ -0,0 +1,63 @@ +//===-- FIRContext.cpp ----------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// Coding style: https://mlir.llvm.org/getting_started/DeveloperGuide/ +// +//===----------------------------------------------------------------------===// + +#include "flang/Optimizer/Dialect/Support/FIRContext.h" +#include "flang/Optimizer/Dialect/Support/KindMapping.h" +#include "mlir/Dialect/LLVMIR/LLVMDialect.h" +#include "mlir/IR/BuiltinAttributes.h" +#include "mlir/IR/BuiltinOps.h" +#include "llvm/TargetParser/Host.h" + +void fir::setTargetTriple(mlir::ModuleOp mod, llvm::StringRef triple) { + auto target = fir::determineTargetTriple(triple); + mod->setAttr(mlir::LLVM::LLVMDialect::getTargetTripleAttrName(), + mlir::StringAttr::get(mod.getContext(), target)); +} + +llvm::Triple fir::getTargetTriple(mlir::ModuleOp mod) { + if (auto target = mod->getAttrOfType( + mlir::LLVM::LLVMDialect::getTargetTripleAttrName())) + return llvm::Triple(target.getValue()); + return llvm::Triple(llvm::sys::getDefaultTargetTriple()); +} + +static constexpr const char *kindMapName = "fir.kindmap"; +static constexpr const char *defKindName = "fir.defaultkind"; + +void fir::setKindMapping(mlir::ModuleOp mod, fir::KindMapping &kindMap) { + auto *ctx = mod.getContext(); + mod->setAttr(kindMapName, mlir::StringAttr::get(ctx, kindMap.mapToString())); + auto defs = kindMap.defaultsToString(); + mod->setAttr(defKindName, mlir::StringAttr::get(ctx, defs)); +} + +fir::KindMapping fir::getKindMapping(mlir::ModuleOp mod) { + auto *ctx = mod.getContext(); + if (auto defs = mod->getAttrOfType(defKindName)) { + auto defVals = fir::KindMapping::toDefaultKinds(defs.getValue()); + if (auto maps = mod->getAttrOfType(kindMapName)) + return fir::KindMapping(ctx, maps.getValue(), defVals); + return fir::KindMapping(ctx, defVals); + } + return fir::KindMapping(ctx); +} + +std::string fir::determineTargetTriple(llvm::StringRef triple) { + // Treat "" or "default" as stand-ins for the default machine. + if (triple.empty() || triple == "default") + return llvm::sys::getDefaultTargetTriple(); + // Treat "native" as stand-in for the host machine. + if (triple == "native") + return llvm::sys::getProcessTriple(); + // TODO: normalize the triple? + return triple.str(); +} diff --git a/flang/lib/Optimizer/Dialect/Support/KindMapping.cpp b/flang/lib/Optimizer/Dialect/Support/KindMapping.cpp new file mode 100644 --- /dev/null +++ b/flang/lib/Optimizer/Dialect/Support/KindMapping.cpp @@ -0,0 +1,460 @@ +//===-- KindMapping.cpp ---------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// Coding style: https://mlir.llvm.org/getting_started/DeveloperGuide/ +// +//===----------------------------------------------------------------------===// + +#include "flang/Optimizer/Dialect/Support/KindMapping.h" +#include "mlir/Dialect/LLVMIR/LLVMDialect.h" +#include "llvm/Support/CommandLine.h" + +/// Allow the user to set the FIR intrinsic type kind value to LLVM type +/// mappings. Note that these are not mappings from kind values to any +/// other MLIR dialect, only to LLVM IR. The default values follow the f18 +/// front-end kind mappings. + +using Bitsize = fir::KindMapping::Bitsize; +using KindTy = fir::KindMapping::KindTy; +using LLVMTypeID = fir::KindMapping::LLVMTypeID; +using MatchResult = fir::KindMapping::MatchResult; + +static llvm::cl::opt + clKindMapping("kind-mapping", + llvm::cl::desc("kind mapping string to set kind precision"), + llvm::cl::value_desc("kind-mapping-string"), + llvm::cl::init(fir::KindMapping::getDefaultMap())); + +static llvm::cl::opt + clDefaultKinds("default-kinds", + llvm::cl::desc("string to set default kind values"), + llvm::cl::value_desc("default-kind-string"), + llvm::cl::init(fir::KindMapping::getDefaultKinds())); + +// Keywords for the floating point types. + +static constexpr const char *kwHalf = "Half"; +static constexpr const char *kwBFloat = "BFloat"; +static constexpr const char *kwFloat = "Float"; +static constexpr const char *kwDouble = "Double"; +static constexpr const char *kwX86FP80 = "X86_FP80"; +static constexpr const char *kwFP128 = "FP128"; +static constexpr const char *kwPPCFP128 = "PPC_FP128"; + +/// Integral types default to the kind value being the size of the value in +/// bytes. The default is to scale from bytes to bits. +static Bitsize defaultScalingKind(KindTy kind) { + const unsigned bitsInByte = 8; + return kind * bitsInByte; +} + +/// Floating-point types default to the kind value being the size of the value +/// in bytes. The default is to translate kinds of 2, 3, 4, 8, 10, and 16 to a +/// valid llvm::Type::TypeID value. Otherwise, the default is FloatTyID. +static LLVMTypeID defaultRealKind(KindTy kind) { + switch (kind) { + case 2: + return LLVMTypeID::HalfTyID; + case 3: + return LLVMTypeID::BFloatTyID; + case 4: + return LLVMTypeID::FloatTyID; + case 8: + return LLVMTypeID::DoubleTyID; + case 10: + return LLVMTypeID::X86_FP80TyID; + case 16: + return LLVMTypeID::FP128TyID; + default: + return LLVMTypeID::FloatTyID; + } +} + +// lookup the kind-value given the defaults, the mappings, and a KIND key +template +static RT doLookup(std::function def, + const llvm::DenseMap, RT> &map, + KindTy kind) { + std::pair key{KEY, kind}; + auto iter = map.find(key); + if (iter != map.end()) + return iter->second; + return def(kind); +} + +// do a lookup for INTEGER, LOGICAL, or CHARACTER +template +static Bitsize getIntegerLikeBitsize(KindTy kind, const MAP &map) { + return doLookup(defaultScalingKind, map, kind); +} + +// do a lookup for REAL or COMPLEX +template +static LLVMTypeID getFloatLikeTypeID(KindTy kind, const MAP &map) { + return doLookup(defaultRealKind, map, kind); +} + +template +static const llvm::fltSemantics &getFloatSemanticsOfKind(KindTy kind, + const MAP &map) { + switch (doLookup(defaultRealKind, map, kind)) { + case LLVMTypeID::HalfTyID: + return llvm::APFloat::IEEEhalf(); + case LLVMTypeID::BFloatTyID: + return llvm::APFloat::BFloat(); + case LLVMTypeID::FloatTyID: + return llvm::APFloat::IEEEsingle(); + case LLVMTypeID::DoubleTyID: + return llvm::APFloat::IEEEdouble(); + case LLVMTypeID::X86_FP80TyID: + return llvm::APFloat::x87DoubleExtended(); + case LLVMTypeID::FP128TyID: + return llvm::APFloat::IEEEquad(); + case LLVMTypeID::PPC_FP128TyID: + return llvm::APFloat::PPCDoubleDouble(); + default: + llvm_unreachable("Invalid floating type"); + } +} + +/// Parse an intrinsic type code. The codes are ('a', CHARACTER), ('c', +/// COMPLEX), ('i', INTEGER), ('l', LOGICAL), and ('r', REAL). +static MatchResult parseCode(char &code, const char *&ptr, const char *endPtr) { + if (ptr >= endPtr) + return mlir::failure(); + if (*ptr != 'a' && *ptr != 'c' && *ptr != 'i' && *ptr != 'l' && *ptr != 'r') + return mlir::failure(); + code = *ptr++; + return mlir::success(); +} + +/// Same as `parseCode` but adds the ('d', DOUBLE PRECISION) code. +static MatchResult parseDefCode(char &code, const char *&ptr, + const char *endPtr) { + if (ptr >= endPtr) + return mlir::failure(); + if (*ptr == 'd') { + code = *ptr++; + return mlir::success(); + } + return parseCode(code, ptr, endPtr); +} + +template +static MatchResult parseSingleChar(const char *&ptr, const char *endPtr) { + if (ptr >= endPtr || *ptr != ch) + return mlir::failure(); + ++ptr; + return mlir::success(); +} + +static MatchResult parseColon(const char *&ptr, const char *endPtr) { + return parseSingleChar<':'>(ptr, endPtr); +} + +static MatchResult parseComma(const char *&ptr, const char *endPtr) { + return parseSingleChar<','>(ptr, endPtr); +} + +/// Recognize and parse an unsigned integer. +static MatchResult parseInt(unsigned &result, const char *&ptr, + const char *endPtr) { + const char *beg = ptr; + while (ptr < endPtr && *ptr >= '0' && *ptr <= '9') + ptr++; + if (beg == ptr) + return mlir::failure(); + llvm::StringRef ref(beg, ptr - beg); + int temp; + if (ref.consumeInteger(10, temp)) + return mlir::failure(); + result = temp; + return mlir::success(); +} + +static mlir::LogicalResult matchString(const char *&ptr, const char *endPtr, + llvm::StringRef literal) { + llvm::StringRef s(ptr, endPtr - ptr); + if (s.startswith(literal)) { + ptr += literal.size(); + return mlir::success(); + } + return mlir::failure(); +} + +/// Recognize and parse the various floating-point keywords. These follow the +/// LLVM naming convention. +static MatchResult parseTypeID(LLVMTypeID &result, const char *&ptr, + const char *endPtr) { + if (mlir::succeeded(matchString(ptr, endPtr, kwHalf))) { + result = LLVMTypeID::HalfTyID; + return mlir::success(); + } + if (mlir::succeeded(matchString(ptr, endPtr, kwBFloat))) { + result = LLVMTypeID::BFloatTyID; + return mlir::success(); + } + if (mlir::succeeded(matchString(ptr, endPtr, kwFloat))) { + result = LLVMTypeID::FloatTyID; + return mlir::success(); + } + if (mlir::succeeded(matchString(ptr, endPtr, kwDouble))) { + result = LLVMTypeID::DoubleTyID; + return mlir::success(); + } + if (mlir::succeeded(matchString(ptr, endPtr, kwX86FP80))) { + result = LLVMTypeID::X86_FP80TyID; + return mlir::success(); + } + if (mlir::succeeded(matchString(ptr, endPtr, kwFP128))) { + result = LLVMTypeID::FP128TyID; + return mlir::success(); + } + if (mlir::succeeded(matchString(ptr, endPtr, kwPPCFP128))) { + result = LLVMTypeID::PPC_FP128TyID; + return mlir::success(); + } + return mlir::failure(); +} + +fir::KindMapping::KindMapping(mlir::MLIRContext *context, llvm::StringRef map, + llvm::ArrayRef defs) + : context{context} { + if (mlir::failed(setDefaultKinds(defs))) + llvm::report_fatal_error("bad default kinds"); + if (mlir::failed(parse(map))) + llvm::report_fatal_error("could not parse kind map"); +} + +fir::KindMapping::KindMapping(mlir::MLIRContext *context, + llvm::ArrayRef defs) + : KindMapping{context, clKindMapping, defs} {} + +fir::KindMapping::KindMapping(mlir::MLIRContext *context) + : KindMapping{context, clKindMapping, clDefaultKinds} {} + +MatchResult fir::KindMapping::badMapString(const llvm::Twine &ptr) { + auto unknown = mlir::UnknownLoc::get(context); + mlir::emitError(unknown, ptr); + return mlir::failure(); +} + +MatchResult fir::KindMapping::parse(llvm::StringRef kindMap) { + if (kindMap.empty()) + return mlir::success(); + const char *srcPtr = kindMap.begin(); + const char *endPtr = kindMap.end(); + while (true) { + char code = '\0'; + KindTy kind = 0; + if (parseCode(code, srcPtr, endPtr) || parseInt(kind, srcPtr, endPtr)) + return badMapString(srcPtr); + if (code == 'a' || code == 'i' || code == 'l') { + Bitsize bits = 0; + if (parseColon(srcPtr, endPtr) || parseInt(bits, srcPtr, endPtr)) + return badMapString(srcPtr); + intMap[std::pair{code, kind}] = bits; + } else if (code == 'r' || code == 'c') { + LLVMTypeID id{}; + if (parseColon(srcPtr, endPtr) || parseTypeID(id, srcPtr, endPtr)) + return badMapString(srcPtr); + floatMap[std::pair{code, kind}] = id; + } else { + return badMapString(srcPtr); + } + if (parseComma(srcPtr, endPtr)) + break; + } + if (srcPtr > endPtr) + return badMapString(srcPtr); + return mlir::success(); +} + +Bitsize fir::KindMapping::getCharacterBitsize(KindTy kind) const { + return getIntegerLikeBitsize<'a'>(kind, intMap); +} + +Bitsize fir::KindMapping::getIntegerBitsize(KindTy kind) const { + return getIntegerLikeBitsize<'i'>(kind, intMap); +} + +Bitsize fir::KindMapping::getLogicalBitsize(KindTy kind) const { + return getIntegerLikeBitsize<'l'>(kind, intMap); +} + +LLVMTypeID fir::KindMapping::getRealTypeID(KindTy kind) const { + return getFloatLikeTypeID<'r'>(kind, floatMap); +} + +LLVMTypeID fir::KindMapping::getComplexTypeID(KindTy kind) const { + return getFloatLikeTypeID<'c'>(kind, floatMap); +} + +Bitsize fir::KindMapping::getRealBitsize(KindTy kind) const { + auto typeId = getFloatLikeTypeID<'r'>(kind, floatMap); + llvm::LLVMContext llCtxt; // FIXME + return llvm::Type::getPrimitiveType(llCtxt, typeId)->getPrimitiveSizeInBits(); +} + +const llvm::fltSemantics & +fir::KindMapping::getFloatSemantics(KindTy kind) const { + return getFloatSemanticsOfKind<'r'>(kind, floatMap); +} + +std::string fir::KindMapping::mapToString() const { + std::string result; + bool addComma = false; + for (auto [k, v] : intMap) { + if (addComma) + result.append(","); + else + addComma = true; + result += k.first + std::to_string(k.second) + ":" + std::to_string(v); + } + for (auto [k, v] : floatMap) { + if (addComma) + result.append(","); + else + addComma = true; + result.append(k.first + std::to_string(k.second) + ":"); + switch (v) { + default: + llvm_unreachable("unhandled type-id"); + case LLVMTypeID::HalfTyID: + result.append(kwHalf); + break; + case LLVMTypeID::BFloatTyID: + result.append(kwBFloat); + break; + case LLVMTypeID::FloatTyID: + result.append(kwFloat); + break; + case LLVMTypeID::DoubleTyID: + result.append(kwDouble); + break; + case LLVMTypeID::X86_FP80TyID: + result.append(kwX86FP80); + break; + case LLVMTypeID::FP128TyID: + result.append(kwFP128); + break; + case LLVMTypeID::PPC_FP128TyID: + result.append(kwPPCFP128); + break; + } + } + return result; +} + +mlir::LogicalResult +fir::KindMapping::setDefaultKinds(llvm::ArrayRef defs) { + if (defs.empty()) { + // generic front-end defaults + const KindTy genericKind = 4; + defaultMap.insert({'a', 1}); + defaultMap.insert({'c', genericKind}); + defaultMap.insert({'d', 2 * genericKind}); + defaultMap.insert({'i', genericKind}); + defaultMap.insert({'l', genericKind}); + defaultMap.insert({'r', genericKind}); + return mlir::success(); + } + if (defs.size() != 6) + return mlir::failure(); + + // defaults determined after command-line processing + defaultMap.insert({'a', defs[0]}); + defaultMap.insert({'c', defs[1]}); + defaultMap.insert({'d', defs[2]}); + defaultMap.insert({'i', defs[3]}); + defaultMap.insert({'l', defs[4]}); + defaultMap.insert({'r', defs[5]}); + return mlir::success(); +} + +std::string fir::KindMapping::defaultsToString() const { + return std::string("a") + std::to_string(defaultMap.find('a')->second) + + std::string("c") + std::to_string(defaultMap.find('c')->second) + + std::string("d") + std::to_string(defaultMap.find('d')->second) + + std::string("i") + std::to_string(defaultMap.find('i')->second) + + std::string("l") + std::to_string(defaultMap.find('l')->second) + + std::string("r") + std::to_string(defaultMap.find('r')->second); +} + +/// Convert a default intrinsic code into the proper position in the array. The +/// default kinds have a precise ordering. +static int codeToIndex(char code) { + switch (code) { + case 'a': + return 0; + case 'c': + return 1; + case 'd': + return 2; + case 'i': + return 3; + case 'l': + return 4; + case 'r': + return 5; + } + llvm_unreachable("invalid default kind intrinsic code"); +} + +std::vector fir::KindMapping::toDefaultKinds(llvm::StringRef defs) { + std::vector result(6); + char code; + KindTy kind; + if (defs.empty()) + defs = clDefaultKinds; + const char *srcPtr = defs.begin(); + const char *endPtr = defs.end(); + while (srcPtr < endPtr) { + if (parseDefCode(code, srcPtr, endPtr) || parseInt(kind, srcPtr, endPtr)) + llvm::report_fatal_error("invalid default kind code"); + result[codeToIndex(code)] = kind; + } + assert(srcPtr == endPtr); + return result; +} + +KindTy fir::KindMapping::defaultCharacterKind() const { + auto iter = defaultMap.find('a'); + assert(iter != defaultMap.end()); + return iter->second; +} + +KindTy fir::KindMapping::defaultComplexKind() const { + auto iter = defaultMap.find('c'); + assert(iter != defaultMap.end()); + return iter->second; +} + +KindTy fir::KindMapping::defaultDoubleKind() const { + auto iter = defaultMap.find('d'); + assert(iter != defaultMap.end()); + return iter->second; +} + +KindTy fir::KindMapping::defaultIntegerKind() const { + auto iter = defaultMap.find('i'); + assert(iter != defaultMap.end()); + return iter->second; +} + +KindTy fir::KindMapping::defaultLogicalKind() const { + auto iter = defaultMap.find('l'); + assert(iter != defaultMap.end()); + return iter->second; +} + +KindTy fir::KindMapping::defaultRealKind() const { + auto iter = defaultMap.find('r'); + assert(iter != defaultMap.end()); + return iter->second; +} diff --git a/flang/lib/Optimizer/HLFIR/Transforms/BufferizeHLFIR.cpp b/flang/lib/Optimizer/HLFIR/Transforms/BufferizeHLFIR.cpp --- a/flang/lib/Optimizer/HLFIR/Transforms/BufferizeHLFIR.cpp +++ b/flang/lib/Optimizer/HLFIR/Transforms/BufferizeHLFIR.cpp @@ -22,10 +22,10 @@ #include "flang/Optimizer/Dialect/FIRDialect.h" #include "flang/Optimizer/Dialect/FIROps.h" #include "flang/Optimizer/Dialect/FIRType.h" +#include "flang/Optimizer/Dialect/Support/FIRContext.h" #include "flang/Optimizer/HLFIR/HLFIRDialect.h" #include "flang/Optimizer/HLFIR/HLFIROps.h" #include "flang/Optimizer/HLFIR/Passes.h" -#include "flang/Optimizer/Support/FIRContext.h" #include "mlir/IR/PatternMatch.h" #include "mlir/Pass/Pass.h" #include "mlir/Pass/PassManager.h" diff --git a/flang/lib/Optimizer/HLFIR/Transforms/CMakeLists.txt b/flang/lib/Optimizer/HLFIR/Transforms/CMakeLists.txt --- a/flang/lib/Optimizer/HLFIR/Transforms/CMakeLists.txt +++ b/flang/lib/Optimizer/HLFIR/Transforms/CMakeLists.txt @@ -12,6 +12,7 @@ LINK_LIBS FIRDialect FIRBuilder + FIRDialectSupport FIRSupport HLFIRDialect MLIRIR diff --git a/flang/lib/Optimizer/HLFIR/Transforms/ConvertToFIR.cpp b/flang/lib/Optimizer/HLFIR/Transforms/ConvertToFIR.cpp --- a/flang/lib/Optimizer/HLFIR/Transforms/ConvertToFIR.cpp +++ b/flang/lib/Optimizer/HLFIR/Transforms/ConvertToFIR.cpp @@ -18,9 +18,9 @@ #include "flang/Optimizer/Dialect/FIRDialect.h" #include "flang/Optimizer/Dialect/FIROps.h" #include "flang/Optimizer/Dialect/FIRType.h" +#include "flang/Optimizer/Dialect/Support/FIRContext.h" #include "flang/Optimizer/HLFIR/HLFIROps.h" #include "flang/Optimizer/HLFIR/Passes.h" -#include "flang/Optimizer/Support/FIRContext.h" #include "mlir/Transforms/DialectConversion.h" namespace hlfir { diff --git a/flang/lib/Optimizer/Support/CMakeLists.txt b/flang/lib/Optimizer/Support/CMakeLists.txt --- a/flang/lib/Optimizer/Support/CMakeLists.txt +++ b/flang/lib/Optimizer/Support/CMakeLists.txt @@ -1,10 +1,8 @@ get_property(dialect_libs GLOBAL PROPERTY MLIR_DIALECT_LIBS) add_flang_library(FIRSupport - FIRContext.cpp InitFIR.cpp InternalNames.cpp - KindMapping.cpp DEPENDS FIROpsIncGen diff --git a/flang/lib/Optimizer/Support/FIRContext.cpp b/flang/lib/Optimizer/Support/FIRContext.cpp deleted file mode 100644 --- a/flang/lib/Optimizer/Support/FIRContext.cpp +++ /dev/null @@ -1,63 +0,0 @@ -//===-- FIRContext.cpp ----------------------------------------------------===// -// -// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. -// See https://llvm.org/LICENSE.txt for license information. -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -// -//===----------------------------------------------------------------------===// -// -// Coding style: https://mlir.llvm.org/getting_started/DeveloperGuide/ -// -//===----------------------------------------------------------------------===// - -#include "flang/Optimizer/Support/FIRContext.h" -#include "flang/Optimizer/Support/KindMapping.h" -#include "mlir/Dialect/LLVMIR/LLVMDialect.h" -#include "mlir/IR/BuiltinAttributes.h" -#include "mlir/IR/BuiltinOps.h" -#include "llvm/TargetParser/Host.h" - -void fir::setTargetTriple(mlir::ModuleOp mod, llvm::StringRef triple) { - auto target = fir::determineTargetTriple(triple); - mod->setAttr(mlir::LLVM::LLVMDialect::getTargetTripleAttrName(), - mlir::StringAttr::get(mod.getContext(), target)); -} - -llvm::Triple fir::getTargetTriple(mlir::ModuleOp mod) { - if (auto target = mod->getAttrOfType( - mlir::LLVM::LLVMDialect::getTargetTripleAttrName())) - return llvm::Triple(target.getValue()); - return llvm::Triple(llvm::sys::getDefaultTargetTriple()); -} - -static constexpr const char *kindMapName = "fir.kindmap"; -static constexpr const char *defKindName = "fir.defaultkind"; - -void fir::setKindMapping(mlir::ModuleOp mod, fir::KindMapping &kindMap) { - auto *ctx = mod.getContext(); - mod->setAttr(kindMapName, mlir::StringAttr::get(ctx, kindMap.mapToString())); - auto defs = kindMap.defaultsToString(); - mod->setAttr(defKindName, mlir::StringAttr::get(ctx, defs)); -} - -fir::KindMapping fir::getKindMapping(mlir::ModuleOp mod) { - auto *ctx = mod.getContext(); - if (auto defs = mod->getAttrOfType(defKindName)) { - auto defVals = fir::KindMapping::toDefaultKinds(defs.getValue()); - if (auto maps = mod->getAttrOfType(kindMapName)) - return fir::KindMapping(ctx, maps.getValue(), defVals); - return fir::KindMapping(ctx, defVals); - } - return fir::KindMapping(ctx); -} - -std::string fir::determineTargetTriple(llvm::StringRef triple) { - // Treat "" or "default" as stand-ins for the default machine. - if (triple.empty() || triple == "default") - return llvm::sys::getDefaultTargetTriple(); - // Treat "native" as stand-in for the host machine. - if (triple == "native") - return llvm::sys::getProcessTriple(); - // TODO: normalize the triple? - return triple.str(); -} diff --git a/flang/lib/Optimizer/Support/KindMapping.cpp b/flang/lib/Optimizer/Support/KindMapping.cpp deleted file mode 100644 --- a/flang/lib/Optimizer/Support/KindMapping.cpp +++ /dev/null @@ -1,460 +0,0 @@ -//===-- KindMapping.cpp ---------------------------------------------------===// -// -// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. -// See https://llvm.org/LICENSE.txt for license information. -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -// -//===----------------------------------------------------------------------===// -// -// Coding style: https://mlir.llvm.org/getting_started/DeveloperGuide/ -// -//===----------------------------------------------------------------------===// - -#include "flang/Optimizer/Support/KindMapping.h" -#include "mlir/Dialect/LLVMIR/LLVMDialect.h" -#include "llvm/Support/CommandLine.h" - -/// Allow the user to set the FIR intrinsic type kind value to LLVM type -/// mappings. Note that these are not mappings from kind values to any -/// other MLIR dialect, only to LLVM IR. The default values follow the f18 -/// front-end kind mappings. - -using Bitsize = fir::KindMapping::Bitsize; -using KindTy = fir::KindMapping::KindTy; -using LLVMTypeID = fir::KindMapping::LLVMTypeID; -using MatchResult = fir::KindMapping::MatchResult; - -static llvm::cl::opt - clKindMapping("kind-mapping", - llvm::cl::desc("kind mapping string to set kind precision"), - llvm::cl::value_desc("kind-mapping-string"), - llvm::cl::init(fir::KindMapping::getDefaultMap())); - -static llvm::cl::opt - clDefaultKinds("default-kinds", - llvm::cl::desc("string to set default kind values"), - llvm::cl::value_desc("default-kind-string"), - llvm::cl::init(fir::KindMapping::getDefaultKinds())); - -// Keywords for the floating point types. - -static constexpr const char *kwHalf = "Half"; -static constexpr const char *kwBFloat = "BFloat"; -static constexpr const char *kwFloat = "Float"; -static constexpr const char *kwDouble = "Double"; -static constexpr const char *kwX86FP80 = "X86_FP80"; -static constexpr const char *kwFP128 = "FP128"; -static constexpr const char *kwPPCFP128 = "PPC_FP128"; - -/// Integral types default to the kind value being the size of the value in -/// bytes. The default is to scale from bytes to bits. -static Bitsize defaultScalingKind(KindTy kind) { - const unsigned bitsInByte = 8; - return kind * bitsInByte; -} - -/// Floating-point types default to the kind value being the size of the value -/// in bytes. The default is to translate kinds of 2, 3, 4, 8, 10, and 16 to a -/// valid llvm::Type::TypeID value. Otherwise, the default is FloatTyID. -static LLVMTypeID defaultRealKind(KindTy kind) { - switch (kind) { - case 2: - return LLVMTypeID::HalfTyID; - case 3: - return LLVMTypeID::BFloatTyID; - case 4: - return LLVMTypeID::FloatTyID; - case 8: - return LLVMTypeID::DoubleTyID; - case 10: - return LLVMTypeID::X86_FP80TyID; - case 16: - return LLVMTypeID::FP128TyID; - default: - return LLVMTypeID::FloatTyID; - } -} - -// lookup the kind-value given the defaults, the mappings, and a KIND key -template -static RT doLookup(std::function def, - const llvm::DenseMap, RT> &map, - KindTy kind) { - std::pair key{KEY, kind}; - auto iter = map.find(key); - if (iter != map.end()) - return iter->second; - return def(kind); -} - -// do a lookup for INTEGER, LOGICAL, or CHARACTER -template -static Bitsize getIntegerLikeBitsize(KindTy kind, const MAP &map) { - return doLookup(defaultScalingKind, map, kind); -} - -// do a lookup for REAL or COMPLEX -template -static LLVMTypeID getFloatLikeTypeID(KindTy kind, const MAP &map) { - return doLookup(defaultRealKind, map, kind); -} - -template -static const llvm::fltSemantics &getFloatSemanticsOfKind(KindTy kind, - const MAP &map) { - switch (doLookup(defaultRealKind, map, kind)) { - case LLVMTypeID::HalfTyID: - return llvm::APFloat::IEEEhalf(); - case LLVMTypeID::BFloatTyID: - return llvm::APFloat::BFloat(); - case LLVMTypeID::FloatTyID: - return llvm::APFloat::IEEEsingle(); - case LLVMTypeID::DoubleTyID: - return llvm::APFloat::IEEEdouble(); - case LLVMTypeID::X86_FP80TyID: - return llvm::APFloat::x87DoubleExtended(); - case LLVMTypeID::FP128TyID: - return llvm::APFloat::IEEEquad(); - case LLVMTypeID::PPC_FP128TyID: - return llvm::APFloat::PPCDoubleDouble(); - default: - llvm_unreachable("Invalid floating type"); - } -} - -/// Parse an intrinsic type code. The codes are ('a', CHARACTER), ('c', -/// COMPLEX), ('i', INTEGER), ('l', LOGICAL), and ('r', REAL). -static MatchResult parseCode(char &code, const char *&ptr, const char *endPtr) { - if (ptr >= endPtr) - return mlir::failure(); - if (*ptr != 'a' && *ptr != 'c' && *ptr != 'i' && *ptr != 'l' && *ptr != 'r') - return mlir::failure(); - code = *ptr++; - return mlir::success(); -} - -/// Same as `parseCode` but adds the ('d', DOUBLE PRECISION) code. -static MatchResult parseDefCode(char &code, const char *&ptr, - const char *endPtr) { - if (ptr >= endPtr) - return mlir::failure(); - if (*ptr == 'd') { - code = *ptr++; - return mlir::success(); - } - return parseCode(code, ptr, endPtr); -} - -template -static MatchResult parseSingleChar(const char *&ptr, const char *endPtr) { - if (ptr >= endPtr || *ptr != ch) - return mlir::failure(); - ++ptr; - return mlir::success(); -} - -static MatchResult parseColon(const char *&ptr, const char *endPtr) { - return parseSingleChar<':'>(ptr, endPtr); -} - -static MatchResult parseComma(const char *&ptr, const char *endPtr) { - return parseSingleChar<','>(ptr, endPtr); -} - -/// Recognize and parse an unsigned integer. -static MatchResult parseInt(unsigned &result, const char *&ptr, - const char *endPtr) { - const char *beg = ptr; - while (ptr < endPtr && *ptr >= '0' && *ptr <= '9') - ptr++; - if (beg == ptr) - return mlir::failure(); - llvm::StringRef ref(beg, ptr - beg); - int temp; - if (ref.consumeInteger(10, temp)) - return mlir::failure(); - result = temp; - return mlir::success(); -} - -static mlir::LogicalResult matchString(const char *&ptr, const char *endPtr, - llvm::StringRef literal) { - llvm::StringRef s(ptr, endPtr - ptr); - if (s.startswith(literal)) { - ptr += literal.size(); - return mlir::success(); - } - return mlir::failure(); -} - -/// Recognize and parse the various floating-point keywords. These follow the -/// LLVM naming convention. -static MatchResult parseTypeID(LLVMTypeID &result, const char *&ptr, - const char *endPtr) { - if (mlir::succeeded(matchString(ptr, endPtr, kwHalf))) { - result = LLVMTypeID::HalfTyID; - return mlir::success(); - } - if (mlir::succeeded(matchString(ptr, endPtr, kwBFloat))) { - result = LLVMTypeID::BFloatTyID; - return mlir::success(); - } - if (mlir::succeeded(matchString(ptr, endPtr, kwFloat))) { - result = LLVMTypeID::FloatTyID; - return mlir::success(); - } - if (mlir::succeeded(matchString(ptr, endPtr, kwDouble))) { - result = LLVMTypeID::DoubleTyID; - return mlir::success(); - } - if (mlir::succeeded(matchString(ptr, endPtr, kwX86FP80))) { - result = LLVMTypeID::X86_FP80TyID; - return mlir::success(); - } - if (mlir::succeeded(matchString(ptr, endPtr, kwFP128))) { - result = LLVMTypeID::FP128TyID; - return mlir::success(); - } - if (mlir::succeeded(matchString(ptr, endPtr, kwPPCFP128))) { - result = LLVMTypeID::PPC_FP128TyID; - return mlir::success(); - } - return mlir::failure(); -} - -fir::KindMapping::KindMapping(mlir::MLIRContext *context, llvm::StringRef map, - llvm::ArrayRef defs) - : context{context} { - if (mlir::failed(setDefaultKinds(defs))) - llvm::report_fatal_error("bad default kinds"); - if (mlir::failed(parse(map))) - llvm::report_fatal_error("could not parse kind map"); -} - -fir::KindMapping::KindMapping(mlir::MLIRContext *context, - llvm::ArrayRef defs) - : KindMapping{context, clKindMapping, defs} {} - -fir::KindMapping::KindMapping(mlir::MLIRContext *context) - : KindMapping{context, clKindMapping, clDefaultKinds} {} - -MatchResult fir::KindMapping::badMapString(const llvm::Twine &ptr) { - auto unknown = mlir::UnknownLoc::get(context); - mlir::emitError(unknown, ptr); - return mlir::failure(); -} - -MatchResult fir::KindMapping::parse(llvm::StringRef kindMap) { - if (kindMap.empty()) - return mlir::success(); - const char *srcPtr = kindMap.begin(); - const char *endPtr = kindMap.end(); - while (true) { - char code = '\0'; - KindTy kind = 0; - if (parseCode(code, srcPtr, endPtr) || parseInt(kind, srcPtr, endPtr)) - return badMapString(srcPtr); - if (code == 'a' || code == 'i' || code == 'l') { - Bitsize bits = 0; - if (parseColon(srcPtr, endPtr) || parseInt(bits, srcPtr, endPtr)) - return badMapString(srcPtr); - intMap[std::pair{code, kind}] = bits; - } else if (code == 'r' || code == 'c') { - LLVMTypeID id{}; - if (parseColon(srcPtr, endPtr) || parseTypeID(id, srcPtr, endPtr)) - return badMapString(srcPtr); - floatMap[std::pair{code, kind}] = id; - } else { - return badMapString(srcPtr); - } - if (parseComma(srcPtr, endPtr)) - break; - } - if (srcPtr > endPtr) - return badMapString(srcPtr); - return mlir::success(); -} - -Bitsize fir::KindMapping::getCharacterBitsize(KindTy kind) const { - return getIntegerLikeBitsize<'a'>(kind, intMap); -} - -Bitsize fir::KindMapping::getIntegerBitsize(KindTy kind) const { - return getIntegerLikeBitsize<'i'>(kind, intMap); -} - -Bitsize fir::KindMapping::getLogicalBitsize(KindTy kind) const { - return getIntegerLikeBitsize<'l'>(kind, intMap); -} - -LLVMTypeID fir::KindMapping::getRealTypeID(KindTy kind) const { - return getFloatLikeTypeID<'r'>(kind, floatMap); -} - -LLVMTypeID fir::KindMapping::getComplexTypeID(KindTy kind) const { - return getFloatLikeTypeID<'c'>(kind, floatMap); -} - -Bitsize fir::KindMapping::getRealBitsize(KindTy kind) const { - auto typeId = getFloatLikeTypeID<'r'>(kind, floatMap); - llvm::LLVMContext llCtxt; // FIXME - return llvm::Type::getPrimitiveType(llCtxt, typeId)->getPrimitiveSizeInBits(); -} - -const llvm::fltSemantics & -fir::KindMapping::getFloatSemantics(KindTy kind) const { - return getFloatSemanticsOfKind<'r'>(kind, floatMap); -} - -std::string fir::KindMapping::mapToString() const { - std::string result; - bool addComma = false; - for (auto [k, v] : intMap) { - if (addComma) - result.append(","); - else - addComma = true; - result += k.first + std::to_string(k.second) + ":" + std::to_string(v); - } - for (auto [k, v] : floatMap) { - if (addComma) - result.append(","); - else - addComma = true; - result.append(k.first + std::to_string(k.second) + ":"); - switch (v) { - default: - llvm_unreachable("unhandled type-id"); - case LLVMTypeID::HalfTyID: - result.append(kwHalf); - break; - case LLVMTypeID::BFloatTyID: - result.append(kwBFloat); - break; - case LLVMTypeID::FloatTyID: - result.append(kwFloat); - break; - case LLVMTypeID::DoubleTyID: - result.append(kwDouble); - break; - case LLVMTypeID::X86_FP80TyID: - result.append(kwX86FP80); - break; - case LLVMTypeID::FP128TyID: - result.append(kwFP128); - break; - case LLVMTypeID::PPC_FP128TyID: - result.append(kwPPCFP128); - break; - } - } - return result; -} - -mlir::LogicalResult -fir::KindMapping::setDefaultKinds(llvm::ArrayRef defs) { - if (defs.empty()) { - // generic front-end defaults - const KindTy genericKind = 4; - defaultMap.insert({'a', 1}); - defaultMap.insert({'c', genericKind}); - defaultMap.insert({'d', 2 * genericKind}); - defaultMap.insert({'i', genericKind}); - defaultMap.insert({'l', genericKind}); - defaultMap.insert({'r', genericKind}); - return mlir::success(); - } - if (defs.size() != 6) - return mlir::failure(); - - // defaults determined after command-line processing - defaultMap.insert({'a', defs[0]}); - defaultMap.insert({'c', defs[1]}); - defaultMap.insert({'d', defs[2]}); - defaultMap.insert({'i', defs[3]}); - defaultMap.insert({'l', defs[4]}); - defaultMap.insert({'r', defs[5]}); - return mlir::success(); -} - -std::string fir::KindMapping::defaultsToString() const { - return std::string("a") + std::to_string(defaultMap.find('a')->second) + - std::string("c") + std::to_string(defaultMap.find('c')->second) + - std::string("d") + std::to_string(defaultMap.find('d')->second) + - std::string("i") + std::to_string(defaultMap.find('i')->second) + - std::string("l") + std::to_string(defaultMap.find('l')->second) + - std::string("r") + std::to_string(defaultMap.find('r')->second); -} - -/// Convert a default intrinsic code into the proper position in the array. The -/// default kinds have a precise ordering. -static int codeToIndex(char code) { - switch (code) { - case 'a': - return 0; - case 'c': - return 1; - case 'd': - return 2; - case 'i': - return 3; - case 'l': - return 4; - case 'r': - return 5; - } - llvm_unreachable("invalid default kind intrinsic code"); -} - -std::vector fir::KindMapping::toDefaultKinds(llvm::StringRef defs) { - std::vector result(6); - char code; - KindTy kind; - if (defs.empty()) - defs = clDefaultKinds; - const char *srcPtr = defs.begin(); - const char *endPtr = defs.end(); - while (srcPtr < endPtr) { - if (parseDefCode(code, srcPtr, endPtr) || parseInt(kind, srcPtr, endPtr)) - llvm::report_fatal_error("invalid default kind code"); - result[codeToIndex(code)] = kind; - } - assert(srcPtr == endPtr); - return result; -} - -KindTy fir::KindMapping::defaultCharacterKind() const { - auto iter = defaultMap.find('a'); - assert(iter != defaultMap.end()); - return iter->second; -} - -KindTy fir::KindMapping::defaultComplexKind() const { - auto iter = defaultMap.find('c'); - assert(iter != defaultMap.end()); - return iter->second; -} - -KindTy fir::KindMapping::defaultDoubleKind() const { - auto iter = defaultMap.find('d'); - assert(iter != defaultMap.end()); - return iter->second; -} - -KindTy fir::KindMapping::defaultIntegerKind() const { - auto iter = defaultMap.find('i'); - assert(iter != defaultMap.end()); - return iter->second; -} - -KindTy fir::KindMapping::defaultLogicalKind() const { - auto iter = defaultMap.find('l'); - assert(iter != defaultMap.end()); - return iter->second; -} - -KindTy fir::KindMapping::defaultRealKind() const { - auto iter = defaultMap.find('r'); - assert(iter != defaultMap.end()); - return iter->second; -} diff --git a/flang/lib/Optimizer/Transforms/AbstractResult.cpp b/flang/lib/Optimizer/Transforms/AbstractResult.cpp --- a/flang/lib/Optimizer/Transforms/AbstractResult.cpp +++ b/flang/lib/Optimizer/Transforms/AbstractResult.cpp @@ -11,7 +11,7 @@ #include "flang/Optimizer/Dialect/FIRDialect.h" #include "flang/Optimizer/Dialect/FIROps.h" #include "flang/Optimizer/Dialect/FIRType.h" -#include "flang/Optimizer/Support/FIRContext.h" +#include "flang/Optimizer/Dialect/Support/FIRContext.h" #include "flang/Optimizer/Transforms/Passes.h" #include "mlir/Dialect/Func/IR/FuncOps.h" #include "mlir/IR/Diagnostics.h" diff --git a/flang/lib/Optimizer/Transforms/AddDebugFoundation.cpp b/flang/lib/Optimizer/Transforms/AddDebugFoundation.cpp --- a/flang/lib/Optimizer/Transforms/AddDebugFoundation.cpp +++ b/flang/lib/Optimizer/Transforms/AddDebugFoundation.cpp @@ -16,7 +16,7 @@ #include "flang/Optimizer/Dialect/FIRDialect.h" #include "flang/Optimizer/Dialect/FIROps.h" #include "flang/Optimizer/Dialect/FIRType.h" -#include "flang/Optimizer/Support/FIRContext.h" +#include "flang/Optimizer/Dialect/Support/FIRContext.h" #include "flang/Optimizer/Transforms/Passes.h" #include "mlir/Dialect/Func/IR/FuncOps.h" #include "mlir/Dialect/LLVMIR/LLVMDialect.h" diff --git a/flang/lib/Optimizer/Transforms/ArrayValueCopy.cpp b/flang/lib/Optimizer/Transforms/ArrayValueCopy.cpp --- a/flang/lib/Optimizer/Transforms/ArrayValueCopy.cpp +++ b/flang/lib/Optimizer/Transforms/ArrayValueCopy.cpp @@ -14,7 +14,7 @@ #include "flang/Optimizer/Builder/Todo.h" #include "flang/Optimizer/Dialect/FIRDialect.h" #include "flang/Optimizer/Dialect/FIROpsSupport.h" -#include "flang/Optimizer/Support/FIRContext.h" +#include "flang/Optimizer/Dialect/Support/FIRContext.h" #include "flang/Optimizer/Transforms/Passes.h" #include "mlir/Dialect/ControlFlow/IR/ControlFlowOps.h" #include "mlir/Dialect/SCF/IR/SCF.h" diff --git a/flang/lib/Optimizer/Transforms/CMakeLists.txt b/flang/lib/Optimizer/Transforms/CMakeLists.txt --- a/flang/lib/Optimizer/Transforms/CMakeLists.txt +++ b/flang/lib/Optimizer/Transforms/CMakeLists.txt @@ -17,14 +17,13 @@ PolymorphicOpConversion.cpp DEPENDS - FIRBuilder FIRDialect - FIRSupport FIROptTransformsPassIncGen LINK_LIBS FIRBuilder FIRDialect + FIRDialectSupport FIRSupport HLFIRDialect MLIRAffineUtils diff --git a/flang/lib/Optimizer/Transforms/CharacterConversion.cpp b/flang/lib/Optimizer/Transforms/CharacterConversion.cpp --- a/flang/lib/Optimizer/Transforms/CharacterConversion.cpp +++ b/flang/lib/Optimizer/Transforms/CharacterConversion.cpp @@ -9,8 +9,8 @@ #include "flang/Optimizer/Dialect/FIRDialect.h" #include "flang/Optimizer/Dialect/FIROps.h" #include "flang/Optimizer/Dialect/FIRType.h" -#include "flang/Optimizer/Support/FIRContext.h" -#include "flang/Optimizer/Support/KindMapping.h" +#include "flang/Optimizer/Dialect/Support/FIRContext.h" +#include "flang/Optimizer/Dialect/Support/KindMapping.h" #include "flang/Optimizer/Transforms/Passes.h" #include "mlir/Dialect/Affine/IR/AffineOps.h" #include "mlir/Dialect/Func/IR/FuncOps.h" diff --git a/flang/lib/Optimizer/Transforms/ControlFlowConverter.cpp b/flang/lib/Optimizer/Transforms/ControlFlowConverter.cpp --- a/flang/lib/Optimizer/Transforms/ControlFlowConverter.cpp +++ b/flang/lib/Optimizer/Transforms/ControlFlowConverter.cpp @@ -9,9 +9,9 @@ #include "flang/Optimizer/Dialect/FIRDialect.h" #include "flang/Optimizer/Dialect/FIROps.h" #include "flang/Optimizer/Dialect/FIROpsSupport.h" -#include "flang/Optimizer/Support/FIRContext.h" +#include "flang/Optimizer/Dialect/Support/FIRContext.h" +#include "flang/Optimizer/Dialect/Support/KindMapping.h" #include "flang/Optimizer/Support/InternalNames.h" -#include "flang/Optimizer/Support/KindMapping.h" #include "flang/Optimizer/Support/TypeCode.h" #include "flang/Optimizer/Transforms/Passes.h" #include "flang/Runtime/derived-api.h" diff --git a/flang/lib/Optimizer/Transforms/PolymorphicOpConversion.cpp b/flang/lib/Optimizer/Transforms/PolymorphicOpConversion.cpp --- a/flang/lib/Optimizer/Transforms/PolymorphicOpConversion.cpp +++ b/flang/lib/Optimizer/Transforms/PolymorphicOpConversion.cpp @@ -9,9 +9,9 @@ #include "flang/Optimizer/Dialect/FIRDialect.h" #include "flang/Optimizer/Dialect/FIROps.h" #include "flang/Optimizer/Dialect/FIROpsSupport.h" -#include "flang/Optimizer/Support/FIRContext.h" +#include "flang/Optimizer/Dialect/Support/FIRContext.h" +#include "flang/Optimizer/Dialect/Support/KindMapping.h" #include "flang/Optimizer/Support/InternalNames.h" -#include "flang/Optimizer/Support/KindMapping.h" #include "flang/Optimizer/Support/TypeCode.h" #include "flang/Optimizer/Transforms/Passes.h" #include "flang/Runtime/derived-api.h" diff --git a/flang/lib/Optimizer/Transforms/SimplifyIntrinsics.cpp b/flang/lib/Optimizer/Transforms/SimplifyIntrinsics.cpp --- a/flang/lib/Optimizer/Transforms/SimplifyIntrinsics.cpp +++ b/flang/lib/Optimizer/Transforms/SimplifyIntrinsics.cpp @@ -29,8 +29,8 @@ #include "flang/Optimizer/Builder/Todo.h" #include "flang/Optimizer/Dialect/FIROps.h" #include "flang/Optimizer/Dialect/FIRType.h" +#include "flang/Optimizer/Dialect/Support/FIRContext.h" #include "flang/Optimizer/HLFIR/HLFIRDialect.h" -#include "flang/Optimizer/Support/FIRContext.h" #include "flang/Optimizer/Transforms/Passes.h" #include "flang/Runtime/entry-names.h" #include "mlir/Dialect/LLVMIR/LLVMDialect.h" diff --git a/flang/lib/Optimizer/Transforms/StackArrays.cpp b/flang/lib/Optimizer/Transforms/StackArrays.cpp --- a/flang/lib/Optimizer/Transforms/StackArrays.cpp +++ b/flang/lib/Optimizer/Transforms/StackArrays.cpp @@ -12,7 +12,7 @@ #include "flang/Optimizer/Dialect/FIRDialect.h" #include "flang/Optimizer/Dialect/FIROps.h" #include "flang/Optimizer/Dialect/FIRType.h" -#include "flang/Optimizer/Support/FIRContext.h" +#include "flang/Optimizer/Dialect/Support/FIRContext.h" #include "flang/Optimizer/Transforms/Passes.h" #include "mlir/Analysis/DataFlow/ConstantPropagationAnalysis.h" #include "mlir/Analysis/DataFlow/DeadCodeAnalysis.h" diff --git a/flang/tools/bbc/CMakeLists.txt b/flang/tools/bbc/CMakeLists.txt --- a/flang/tools/bbc/CMakeLists.txt +++ b/flang/tools/bbc/CMakeLists.txt @@ -12,6 +12,7 @@ get_property(dialect_libs GLOBAL PROPERTY MLIR_DIALECT_LIBS) target_link_libraries(bbc PRIVATE FIRDialect +FIRDialectSupport FIRSupport FIRTransforms FIRBuilder diff --git a/flang/tools/bbc/bbc.cpp b/flang/tools/bbc/bbc.cpp --- a/flang/tools/bbc/bbc.cpp +++ b/flang/tools/bbc/bbc.cpp @@ -19,10 +19,10 @@ #include "flang/Lower/Bridge.h" #include "flang/Lower/PFTBuilder.h" #include "flang/Lower/Support/Verifier.h" -#include "flang/Optimizer/Support/FIRContext.h" +#include "flang/Optimizer/Dialect/Support/FIRContext.h" +#include "flang/Optimizer/Dialect/Support/KindMapping.h" #include "flang/Optimizer/Support/InitFIR.h" #include "flang/Optimizer/Support/InternalNames.h" -#include "flang/Optimizer/Support/KindMapping.h" #include "flang/Optimizer/Support/Utils.h" #include "flang/Optimizer/Transforms/Passes.h" #include "flang/Parser/characters.h" diff --git a/flang/tools/tco/CMakeLists.txt b/flang/tools/tco/CMakeLists.txt --- a/flang/tools/tco/CMakeLists.txt +++ b/flang/tools/tco/CMakeLists.txt @@ -8,6 +8,7 @@ target_link_libraries(tco PRIVATE FIRCodeGen FIRDialect + FIRDialectSupport FIRSupport FIRTransforms FIRBuilder diff --git a/flang/tools/tco/tco.cpp b/flang/tools/tco/tco.cpp --- a/flang/tools/tco/tco.cpp +++ b/flang/tools/tco/tco.cpp @@ -12,10 +12,10 @@ //===----------------------------------------------------------------------===// #include "flang/Optimizer/CodeGen/CodeGen.h" -#include "flang/Optimizer/Support/FIRContext.h" +#include "flang/Optimizer/Dialect/Support/FIRContext.h" +#include "flang/Optimizer/Dialect/Support/KindMapping.h" #include "flang/Optimizer/Support/InitFIR.h" #include "flang/Optimizer/Support/InternalNames.h" -#include "flang/Optimizer/Support/KindMapping.h" #include "flang/Optimizer/Transforms/Passes.h" #include "mlir/IR/AsmState.h" #include "mlir/IR/BuiltinOps.h" diff --git a/flang/unittests/Optimizer/Builder/CharacterTest.cpp b/flang/unittests/Optimizer/Builder/CharacterTest.cpp --- a/flang/unittests/Optimizer/Builder/CharacterTest.cpp +++ b/flang/unittests/Optimizer/Builder/CharacterTest.cpp @@ -10,8 +10,8 @@ #include "gtest/gtest.h" #include "flang/Optimizer/Builder/BoxValue.h" #include "flang/Optimizer/Builder/FIRBuilder.h" +#include "flang/Optimizer/Dialect/Support/KindMapping.h" #include "flang/Optimizer/Support/InitFIR.h" -#include "flang/Optimizer/Support/KindMapping.h" struct CharacterTest : public testing::Test { public: diff --git a/flang/unittests/Optimizer/Builder/ComplexTest.cpp b/flang/unittests/Optimizer/Builder/ComplexTest.cpp --- a/flang/unittests/Optimizer/Builder/ComplexTest.cpp +++ b/flang/unittests/Optimizer/Builder/ComplexTest.cpp @@ -9,8 +9,8 @@ #include "flang/Optimizer/Builder/Complex.h" #include "gtest/gtest.h" #include "flang/Optimizer/Builder/FIRBuilder.h" +#include "flang/Optimizer/Dialect/Support/KindMapping.h" #include "flang/Optimizer/Support/InitFIR.h" -#include "flang/Optimizer/Support/KindMapping.h" struct ComplexTest : public testing::Test { public: diff --git a/flang/unittests/Optimizer/Builder/DoLoopHelperTest.cpp b/flang/unittests/Optimizer/Builder/DoLoopHelperTest.cpp --- a/flang/unittests/Optimizer/Builder/DoLoopHelperTest.cpp +++ b/flang/unittests/Optimizer/Builder/DoLoopHelperTest.cpp @@ -8,8 +8,8 @@ #include "flang/Optimizer/Builder/DoLoopHelper.h" #include "gtest/gtest.h" +#include "flang/Optimizer/Dialect/Support/KindMapping.h" #include "flang/Optimizer/Support/InitFIR.h" -#include "flang/Optimizer/Support/KindMapping.h" #include using namespace mlir; diff --git a/flang/unittests/Optimizer/Builder/FIRBuilderTest.cpp b/flang/unittests/Optimizer/Builder/FIRBuilderTest.cpp --- a/flang/unittests/Optimizer/Builder/FIRBuilderTest.cpp +++ b/flang/unittests/Optimizer/Builder/FIRBuilderTest.cpp @@ -9,8 +9,8 @@ #include "flang/Optimizer/Builder/FIRBuilder.h" #include "gtest/gtest.h" #include "flang/Optimizer/Builder/BoxValue.h" +#include "flang/Optimizer/Dialect/Support/KindMapping.h" #include "flang/Optimizer/Support/InitFIR.h" -#include "flang/Optimizer/Support/KindMapping.h" using namespace mlir; diff --git a/flang/unittests/Optimizer/Builder/HLFIRToolsTest.cpp b/flang/unittests/Optimizer/Builder/HLFIRToolsTest.cpp --- a/flang/unittests/Optimizer/Builder/HLFIRToolsTest.cpp +++ b/flang/unittests/Optimizer/Builder/HLFIRToolsTest.cpp @@ -10,8 +10,8 @@ #include "gtest/gtest.h" #include "flang/Optimizer/Builder/BoxValue.h" #include "flang/Optimizer/Builder/FIRBuilder.h" +#include "flang/Optimizer/Dialect/Support/KindMapping.h" #include "flang/Optimizer/Support/InitFIR.h" -#include "flang/Optimizer/Support/KindMapping.h" struct HLFIRToolsTest : public testing::Test { public: diff --git a/flang/unittests/Optimizer/Builder/Runtime/RuntimeCallTestBase.h b/flang/unittests/Optimizer/Builder/Runtime/RuntimeCallTestBase.h --- a/flang/unittests/Optimizer/Builder/Runtime/RuntimeCallTestBase.h +++ b/flang/unittests/Optimizer/Builder/Runtime/RuntimeCallTestBase.h @@ -11,8 +11,8 @@ #include "gtest/gtest.h" #include "flang/Optimizer/Builder/FIRBuilder.h" +#include "flang/Optimizer/Dialect/Support/KindMapping.h" #include "flang/Optimizer/Support/InitFIR.h" -#include "flang/Optimizer/Support/KindMapping.h" struct RuntimeCallTest : public testing::Test { public: diff --git a/flang/unittests/Optimizer/CMakeLists.txt b/flang/unittests/Optimizer/CMakeLists.txt --- a/flang/unittests/Optimizer/CMakeLists.txt +++ b/flang/unittests/Optimizer/CMakeLists.txt @@ -4,6 +4,7 @@ FIRBuilder FIRCodeGen FIRDialect + FIRDialectSupport FIRSupport HLFIRDialect ${dialect_libs} diff --git a/flang/unittests/Optimizer/FIRContextTest.cpp b/flang/unittests/Optimizer/FIRContextTest.cpp --- a/flang/unittests/Optimizer/FIRContextTest.cpp +++ b/flang/unittests/Optimizer/FIRContextTest.cpp @@ -6,11 +6,11 @@ // //===----------------------------------------------------------------------===// -#include "flang/Optimizer/Support/FIRContext.h" +#include "flang/Optimizer/Dialect/Support/FIRContext.h" #include "gtest/gtest.h" #include "mlir/IR/BuiltinAttributes.h" #include "mlir/IR/BuiltinOps.h" -#include "flang/Optimizer/Support/KindMapping.h" +#include "flang/Optimizer/Dialect/Support/KindMapping.h" #include "llvm/TargetParser/Host.h" #include diff --git a/flang/unittests/Optimizer/KindMappingTest.cpp b/flang/unittests/Optimizer/KindMappingTest.cpp --- a/flang/unittests/Optimizer/KindMappingTest.cpp +++ b/flang/unittests/Optimizer/KindMappingTest.cpp @@ -6,7 +6,7 @@ // //===----------------------------------------------------------------------===// -#include "flang/Optimizer/Support/KindMapping.h" +#include "flang/Optimizer/Dialect/Support/KindMapping.h" #include "gtest/gtest.h" #include