Warning: This file is not a C or C++ file. It does not have highlighting.
1 | //===-- Tools/CrossToolHelpers.h --------------------------------- *-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 | // A header file for containing functionallity that is used across Flang tools, |
9 | // such as helper functions which apply or generate information needed accross |
10 | // tools like bbc and flang-new. |
11 | //===----------------------------------------------------------------------===// |
12 | |
13 | #ifndef FORTRAN_TOOLS_CROSS_TOOL_HELPERS_H |
14 | #define FORTRAN_TOOLS_CROSS_TOOL_HELPERS_H |
15 | |
16 | #include "flang/Common/MathOptionsBase.h" |
17 | #include "flang/Frontend/CodeGenOptions.h" |
18 | #include "flang/Frontend/LangOptions.h" |
19 | #include <cstdint> |
20 | |
21 | #include "mlir/Dialect/OpenMP/OpenMPDialect.h" |
22 | #include "mlir/IR/BuiltinOps.h" |
23 | #include "llvm/Frontend/Debug/Options.h" |
24 | #include "llvm/Passes/OptimizationLevel.h" |
25 | |
26 | /// Configuriation for the MLIR to LLVM pass pipeline. |
27 | struct MLIRToLLVMPassPipelineConfig { |
28 | explicit MLIRToLLVMPassPipelineConfig(llvm::OptimizationLevel level) { |
29 | OptLevel = level; |
30 | } |
31 | explicit MLIRToLLVMPassPipelineConfig(llvm::OptimizationLevel level, |
32 | const Fortran::frontend::CodeGenOptions &opts, |
33 | const Fortran::common::MathOptionsBase &mathOpts) { |
34 | OptLevel = level; |
35 | StackArrays = opts.StackArrays; |
36 | Underscoring = opts.Underscoring; |
37 | LoopVersioning = opts.LoopVersioning; |
38 | DebugInfo = opts.getDebugInfo(); |
39 | AliasAnalysis = opts.AliasAnalysis; |
40 | FramePointerKind = opts.getFramePointer(); |
41 | // The logic for setting these attributes is intended to match the logic |
42 | // used in Clang. |
43 | NoInfsFPMath = mathOpts.getNoHonorInfs(); |
44 | NoNaNsFPMath = mathOpts.getNoHonorNaNs(); |
45 | ApproxFuncFPMath = mathOpts.getApproxFunc(); |
46 | NoSignedZerosFPMath = mathOpts.getNoSignedZeros(); |
47 | UnsafeFPMath = mathOpts.getAssociativeMath() && |
48 | mathOpts.getReciprocalMath() && NoSignedZerosFPMath && |
49 | ApproxFuncFPMath && mathOpts.getFPContractEnabled(); |
50 | } |
51 | |
52 | llvm::OptimizationLevel OptLevel; ///< optimisation level |
53 | bool StackArrays = false; ///< convert memory allocations to alloca. |
54 | bool Underscoring = true; ///< add underscores to function names. |
55 | bool LoopVersioning = false; ///< Run the version loop pass. |
56 | bool AliasAnalysis = false; ///< Add TBAA tags to generated LLVMIR |
57 | llvm::codegenoptions::DebugInfoKind DebugInfo = |
58 | llvm::codegenoptions::NoDebugInfo; ///< Debug info generation. |
59 | llvm::FramePointerKind FramePointerKind = |
60 | llvm::FramePointerKind::None; ///< Add frame pointer to functions. |
61 | unsigned VScaleMin = 0; ///< SVE vector range minimum. |
62 | unsigned VScaleMax = 0; ///< SVE vector range maximum. |
63 | bool NoInfsFPMath = false; ///< Set no-infs-fp-math attribute for functions. |
64 | bool NoNaNsFPMath = false; ///< Set no-nans-fp-math attribute for functions. |
65 | bool ApproxFuncFPMath = |
66 | false; ///< Set approx-func-fp-math attribute for functions. |
67 | bool NoSignedZerosFPMath = |
68 | false; ///< Set no-signed-zeros-fp-math attribute for functions. |
69 | bool UnsafeFPMath = false; ///< Set unsafe-fp-math attribute for functions. |
70 | }; |
71 | |
72 | struct OffloadModuleOpts { |
73 | OffloadModuleOpts() {} |
74 | OffloadModuleOpts(uint32_t OpenMPTargetDebug, bool OpenMPTeamSubscription, |
75 | bool OpenMPThreadSubscription, bool OpenMPNoThreadState, |
76 | bool OpenMPNoNestedParallelism, bool OpenMPIsTargetDevice, |
77 | bool OpenMPIsGPU, uint32_t OpenMPVersion, std::string OMPHostIRFile = {}, |
78 | bool NoGPULib = false) |
79 | : OpenMPTargetDebug(OpenMPTargetDebug), |
80 | OpenMPTeamSubscription(OpenMPTeamSubscription), |
81 | OpenMPThreadSubscription(OpenMPThreadSubscription), |
82 | OpenMPNoThreadState(OpenMPNoThreadState), |
83 | OpenMPNoNestedParallelism(OpenMPNoNestedParallelism), |
84 | OpenMPIsTargetDevice(OpenMPIsTargetDevice), OpenMPIsGPU(OpenMPIsGPU), |
85 | OpenMPVersion(OpenMPVersion), OMPHostIRFile(OMPHostIRFile), |
86 | NoGPULib(NoGPULib) {} |
87 | |
88 | OffloadModuleOpts(Fortran::frontend::LangOptions &Opts) |
89 | : OpenMPTargetDebug(Opts.OpenMPTargetDebug), |
90 | OpenMPTeamSubscription(Opts.OpenMPTeamSubscription), |
91 | OpenMPThreadSubscription(Opts.OpenMPThreadSubscription), |
92 | OpenMPNoThreadState(Opts.OpenMPNoThreadState), |
93 | OpenMPNoNestedParallelism(Opts.OpenMPNoNestedParallelism), |
94 | OpenMPIsTargetDevice(Opts.OpenMPIsTargetDevice), |
95 | OpenMPIsGPU(Opts.OpenMPIsGPU), OpenMPVersion(Opts.OpenMPVersion), |
96 | OMPHostIRFile(Opts.OMPHostIRFile), NoGPULib(Opts.NoGPULib) {} |
97 | |
98 | uint32_t OpenMPTargetDebug = 0; |
99 | bool OpenMPTeamSubscription = false; |
100 | bool OpenMPThreadSubscription = false; |
101 | bool OpenMPNoThreadState = false; |
102 | bool OpenMPNoNestedParallelism = false; |
103 | bool OpenMPIsTargetDevice = false; |
104 | bool OpenMPIsGPU = false; |
105 | uint32_t OpenMPVersion = 11; |
106 | std::string OMPHostIRFile = {}; |
107 | bool NoGPULib = false; |
108 | }; |
109 | |
110 | // Shares assinging of the OpenMP OffloadModuleInterface and its assorted |
111 | // attributes accross Flang tools (bbc/flang) |
112 | void setOffloadModuleInterfaceAttributes( |
113 | mlir::ModuleOp &module, OffloadModuleOpts Opts) { |
114 | // Should be registered by the OpenMPDialect |
115 | if (auto offloadMod = llvm::dyn_cast<mlir::omp::OffloadModuleInterface>( |
116 | module.getOperation())) { |
117 | offloadMod.setIsTargetDevice(Opts.OpenMPIsTargetDevice); |
118 | offloadMod.setIsGPU(Opts.OpenMPIsGPU); |
119 | if (Opts.OpenMPIsTargetDevice) { |
120 | offloadMod.setFlags(Opts.OpenMPTargetDebug, Opts.OpenMPTeamSubscription, |
121 | Opts.OpenMPThreadSubscription, Opts.OpenMPNoThreadState, |
122 | Opts.OpenMPNoNestedParallelism, Opts.OpenMPVersion, Opts.NoGPULib); |
123 | |
124 | if (!Opts.OMPHostIRFile.empty()) |
125 | offloadMod.setHostIRFilePath(Opts.OMPHostIRFile); |
126 | } |
127 | } |
128 | } |
129 | |
130 | void setOpenMPVersionAttribute(mlir::ModuleOp &module, int64_t version) { |
131 | module.getOperation()->setAttr( |
132 | mlir::StringAttr::get(module.getContext(), llvm::Twine{"omp.version"}), |
133 | mlir::omp::VersionAttr::get(module.getContext(), version)); |
134 | } |
135 | |
136 | #endif // FORTRAN_TOOLS_CROSS_TOOL_HELPERS_H |
137 |
Warning: This file is not a C or C++ file. It does not have highlighting.