FLANG
CrossToolHelpers.h
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.
11//===----------------------------------------------------------------------===//
12
13#ifndef FORTRAN_TOOLS_CROSS_TOOL_HELPERS_H
14#define FORTRAN_TOOLS_CROSS_TOOL_HELPERS_H
15
16#include "flang/Frontend/CodeGenOptions.h"
18#include "flang/Support/LangOptions.h"
20#include <cstdint>
21
22#include "mlir/Dialect/OpenMP/OpenMPDialect.h"
23#include "mlir/Pass/PassRegistry.h"
24#include "llvm/ADT/SmallVector.h"
25#include "llvm/Frontend/Debug/Options.h"
26#include "llvm/Passes/OptimizationLevel.h"
27
28// Flang Extension Point Callbacks
30public:
31 void registerFIROptEarlyEPCallbacks(
32 const std::function<void(mlir::PassManager &, llvm::OptimizationLevel)>
33 &C) {
34 FIROptEarlyEPCallbacks.push_back(C);
35 }
36
37 void registerFIRInlinerCallback(
38 const std::function<void(mlir::PassManager &, llvm::OptimizationLevel)>
39 &C) {
40 FIRInlinerCallback.push_back(C);
41 }
42
43 void registerFIROptLastEPCallbacks(
44 const std::function<void(mlir::PassManager &, llvm::OptimizationLevel)>
45 &C) {
46 FIROptLastEPCallbacks.push_back(C);
47 }
48
49 void invokeFIROptEarlyEPCallbacks(
50 mlir::PassManager &pm, llvm::OptimizationLevel optLevel) {
51 for (auto &C : FIROptEarlyEPCallbacks)
52 C(pm, optLevel);
53 };
54
55 void invokeFIRInlinerCallback(
56 mlir::PassManager &pm, llvm::OptimizationLevel optLevel) {
57 for (auto &C : FIRInlinerCallback)
58 C(pm, optLevel);
59 };
60
61 void invokeFIROptLastEPCallbacks(
62 mlir::PassManager &pm, llvm::OptimizationLevel optLevel) {
63 for (auto &C : FIROptLastEPCallbacks)
64 C(pm, optLevel);
65 };
66
67private:
69 std::function<void(mlir::PassManager &, llvm::OptimizationLevel)>, 1>
70 FIROptEarlyEPCallbacks;
71
73 std::function<void(mlir::PassManager &, llvm::OptimizationLevel)>, 1>
74 FIRInlinerCallback;
75
77 std::function<void(mlir::PassManager &, llvm::OptimizationLevel)>, 1>
78 FIROptLastEPCallbacks;
79};
80
82struct MLIRToLLVMPassPipelineConfig : public FlangEPCallBacks {
83 explicit MLIRToLLVMPassPipelineConfig(llvm::OptimizationLevel level) {
84 OptLevel = level;
85 }
86 explicit MLIRToLLVMPassPipelineConfig(llvm::OptimizationLevel level,
88 const Fortran::common::MathOptionsBase &mathOpts) {
89 OptLevel = level;
90 StackArrays = opts.StackArrays;
91 EnableSafeTrampoline = opts.EnableSafeTrampoline;
92 Underscoring = opts.Underscoring;
93 LoopVersioning = opts.LoopVersioning;
94 DebugInfo = opts.getDebugInfo();
95 AliasAnalysis = opts.AliasAnalysis;
96 FramePointerKind = opts.getFramePointer();
97 // The logic for setting these attributes is intended to match the logic
98 // used in Clang.
99 NoInfsFPMath = mathOpts.getNoHonorInfs();
100 NoNaNsFPMath = mathOpts.getNoHonorNaNs();
101 ApproxFuncFPMath = mathOpts.getApproxFunc();
102 NoSignedZerosFPMath = mathOpts.getNoSignedZeros();
103 UnsafeFPMath = mathOpts.getAssociativeMath() &&
104 mathOpts.getReciprocalMath() && NoSignedZerosFPMath &&
105 ApproxFuncFPMath && mathOpts.getFPContractEnabled();
108 UseSampleProfile = !opts.SampleProfileFile.empty();
109 DebugInfoForProfiling = opts.DebugInfoForProfiling;
110 if (opts.InstrumentFunctions) {
111 InstrumentFunctionEntry = "__cyg_profile_func_enter";
112 InstrumentFunctionExit = "__cyg_profile_func_exit";
113 }
114 DwarfVersion = opts.DwarfVersion;
117 }
118
119 llvm::OptimizationLevel OptLevel;
120 bool StackArrays = false;
122 bool Underscoring = true;
123 bool LoopVersioning = false;
124 bool AliasAnalysis = false;
125 llvm::codegenoptions::DebugInfoKind DebugInfo =
126 llvm::codegenoptions::NoDebugInfo;
127 llvm::FramePointerKind FramePointerKind =
128 llvm::FramePointerKind::None;
129 unsigned VScaleMin = 0;
130 unsigned VScaleMax = 0;
131 bool NoInfsFPMath = false;
132 bool NoNaNsFPMath = false;
133 bool ApproxFuncFPMath = false;
135 false;
136 bool UnsafeFPMath = false;
137 std::string Reciprocals = "";
139 std::string PreferVectorWidth = "";
141 bool NSWOnLoopVarInc = true;
142 bool EnableOpenMP = false;
144 false;
145 bool UseSampleProfile = false;
147 bool EnableOpenMPSimd = false;
150 "";
153 "";
156 Fortran::frontend::CodeGenOptions::ComplexRangeKind::
157 CX_Full;
158 int32_t DwarfVersion = 0;
159 std::string SplitDwarfFile = "";
160 std::string DwarfDebugFlags = "";
161 Fortran::common::FPMaxminBehavior fpMaxminBehavior =
162 Fortran::common::FPMaxminBehavior::Legacy;
163};
164
166[[maybe_unused]] static mlir::omp::OffloadModuleOpts makeOffloadModuleOpts(
168 return mlir::omp::OffloadModuleOpts(Opts.OpenMPTargetDebug,
169 Opts.OpenMPTeamSubscription, Opts.OpenMPThreadSubscription,
170 Opts.OpenMPNoThreadState, Opts.OpenMPNoNestedParallelism,
171 Opts.OpenMPIsTargetDevice, Opts.OpenMPIsGPU, Opts.OpenMPForceUSM,
172 Opts.OpenMPVersion, Opts.OMPHostIRFile, Opts.OMPTargetTriples,
173 Opts.NoGPULib);
174}
175
176#endif // FORTRAN_TOOLS_CROSS_TOOL_HELPERS_H
Definition CrossToolHelpers.h:29
Definition LangOptions.h:58
std::vector< llvm::Triple > OMPTargetTriples
List of triples passed in using -fopenmp-targets.
Definition LangOptions.h:76
std::string OMPHostIRFile
signed integer overflow handling
Definition LangOptions.h:73
Definition MathOptionsBase.h:22
Definition CodeGenOptions.h:49
std::string PreferVectorWidth
The prefered vector width, if requested by -mprefer-vector-width.
Definition CodeGenOptions.h:56
std::string SplitDwarfFile
Definition CodeGenOptions.h:177
std::string Reciprocals
List of reciprocal estimate sub-options.
Definition CodeGenOptions.h:59
std::string SampleProfileFile
Name of the profile file to use with -fprofile-sample-use.
Definition CodeGenOptions.h:183
ComplexRangeKind
Controls the various implementations for complex division.
Definition CodeGenOptions.h:210
std::string DwarfDebugFlags
Definition CodeGenOptions.h:78
Definition OpenACC.h:20
FPMaxminBehavior
Definition FPMaxminBehavior.h:29
llvm::codegenoptions::DebugInfoKind DebugInfo
Debug info generation.
Definition CrossToolHelpers.h:125
bool EnableSafeTrampoline
Use runtime trampoline pool (W^X).
Definition CrossToolHelpers.h:121
bool NoSignedZerosFPMath
Set no-signed-zeros-fp-math attribute for functions.
Definition CrossToolHelpers.h:134
std::string DwarfDebugFlags
Debug flags to append to DWARF producer.
Definition CrossToolHelpers.h:160
bool LoopVersioning
Run the version loop pass.
Definition CrossToolHelpers.h:123
unsigned VScaleMin
SVE vector range minimum.
Definition CrossToolHelpers.h:129
std::string Reciprocals
Definition CrossToolHelpers.h:137
llvm::FramePointerKind FramePointerKind
Add frame pointer to functions.
Definition CrossToolHelpers.h:127
bool NoNaNsFPMath
Set no-nans-fp-math attribute for functions.
Definition CrossToolHelpers.h:132
std::string SplitDwarfFile
File name for the split debug info.
Definition CrossToolHelpers.h:159
bool Underscoring
add underscores to function names.
Definition CrossToolHelpers.h:122
std::string PreferVectorWidth
Definition CrossToolHelpers.h:139
bool ApproxFuncFPMath
Set afn flag for instructions.
Definition CrossToolHelpers.h:133
unsigned VScaleMax
SVE vector range maximum.
Definition CrossToolHelpers.h:130
bool NoInfsFPMath
Set ninf flag for instructions.
Definition CrossToolHelpers.h:131
llvm::OptimizationLevel OptLevel
optimisation level
Definition CrossToolHelpers.h:119
bool EnableOpenMPSimd
Enable OpenMP simd-only mode.
Definition CrossToolHelpers.h:147
bool DebugInfoForProfiling
Enable extra debugging info.
Definition CrossToolHelpers.h:146
bool NSWOnLoopVarInc
Add nsw flag to loop variable increments.
Definition CrossToolHelpers.h:141
bool EnableOpenMPIsTargetDevice
Compiling for an OpenMP target device.
Definition CrossToolHelpers.h:143
bool AliasAnalysis
Add TBAA tags to generated LLVMIR.
Definition CrossToolHelpers.h:124
Fortran::frontend::CodeGenOptions::ComplexRangeKind ComplexRange
Method for calculating complex number division.
Definition CrossToolHelpers.h:155
bool SkipConvertComplexPow
Do not run complex pow conversion.
Definition CrossToolHelpers.h:148
bool UnsafeFPMath
Set all fast-math flags for instructions.
Definition CrossToolHelpers.h:136
std::string InstrumentFunctionEntry
Definition CrossToolHelpers.h:149
bool StackArrays
convert memory allocations to alloca.
Definition CrossToolHelpers.h:120
bool UseSampleProfile
Enable sample based profiling.
Definition CrossToolHelpers.h:145
int32_t DwarfVersion
Version of DWARF debug info to generate.
Definition CrossToolHelpers.h:158
bool EnableOpenMP
Enable OpenMP lowering.
Definition CrossToolHelpers.h:142
std::string InstrumentFunctionExit
Definition CrossToolHelpers.h:152