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