Skip to content

Commit

Permalink
[flang][NFC] Refactor to remove .inc file containing shared code (llv…
Browse files Browse the repository at this point in the history
…m#109874)

Remove flang/include/flang/Tools/CLOptions.inc - which was included as
is in - several places. Move the code in it to header and source files
which are used used in the "standard" way. Some minor cleanup such as
removing trailing whitespace and excessive newlines and reordering
entries alphabetically for files that were modified along the way.
Update the documentation that referenced CLOptions.inc.
  • Loading branch information
tarunprabhu authored Sep 25, 2024
1 parent eb48aac commit c3201dd
Show file tree
Hide file tree
Showing 15 changed files with 671 additions and 473 deletions.
2 changes: 1 addition & 1 deletion flang/docs/FlangDriver.md
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ e.g. during the semantic checks.
## FIR Optimizer Pass Pipeline Extension Points

The default FIR optimizer pass pipeline `createDefaultFIROptimizerPassPipeline`
in `flang/include/flang/Tools/CLOptions.inc` contains extension point callback
in `flang/lib/Optimizer/Passes/Pipelines.cpp` contains extension point callback
invocations `invokeFIROptEarlyEPCallbacks`, `invokeFIRInlinerCallback`, and
`invokeFIROptLastEPCallbacks` for Flang drivers to be able to insert additonal
passes at different points of the default pass pipeline. An example use of these
Expand Down
60 changes: 60 additions & 0 deletions flang/include/flang/Optimizer/Passes/CommandLineOpts.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
//===-- CommandLineOpts.h -- shared command line options --------*- 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
//
//===----------------------------------------------------------------------===//

/// This file declares some shared command-line options that can be used when
/// debugging the test tools.

#ifndef FORTRAN_OPTIMIZER_PASSES_COMMANDLINEOPTS_H
#define FORTRAN_OPTIMIZER_PASSES_COMMANDLINEOPTS_H

#include "llvm/Frontend/Debug/Options.h"
#include "llvm/Passes/OptimizationLevel.h"
#include "llvm/Support/CommandLine.h"

/// Shared option in tools to control whether dynamically sized array
/// allocations should always be on the heap.
extern llvm::cl::opt<bool> dynamicArrayStackToHeapAllocation;

/// Shared option in tools to set a maximum value for the number of elements in
/// a compile-time sized array that can be allocated on the stack.
extern llvm::cl::opt<std::size_t> arrayStackAllocationThreshold;

/// Shared option in tools to ignore missing runtime type descriptor objects
/// when translating FIR to LLVM. The resulting program will crash if the
/// runtime needs the derived type descriptors, this is only a debug option to
/// allow compiling manually written FIR programs involving derived types
/// without having to write the derived type descriptors which are normally
/// generated by the frontend.
extern llvm::cl::opt<bool> ignoreMissingTypeDescriptors;

/// Default optimization level used to create Flang pass pipeline is O0.
extern llvm::OptimizationLevel defaultOptLevel;

extern llvm::codegenoptions::DebugInfoKind noDebugInfo;

/// Optimizer Passes
extern llvm::cl::opt<bool> disableCfgConversion;
extern llvm::cl::opt<bool> disableFirAvc;
extern llvm::cl::opt<bool> disableFirMao;

extern llvm::cl::opt<bool> disableFirAliasTags;
extern llvm::cl::opt<bool> useOldAliasTags;

/// CodeGen Passes
extern llvm::cl::opt<bool> disableCodeGenRewrite;
extern llvm::cl::opt<bool> disableTargetRewrite;
extern llvm::cl::opt<bool> disableDebugInfo;
extern llvm::cl::opt<bool> disableFirToLlvmIr;
extern llvm::cl::opt<bool> disableLlvmIrToLlvm;
extern llvm::cl::opt<bool> disableBoxedProcedureRewrite;

extern llvm::cl::opt<bool> disableExternalNameConversion;
extern llvm::cl::opt<bool> enableConstantArgumentGlobalisation;
extern llvm::cl::opt<bool> disableCompilerGeneratedNamesConversion;

#endif // FORTRAN_OPTIMIZER_PASSES_COMMANDLINE_OPTS_H
162 changes: 162 additions & 0 deletions flang/include/flang/Optimizer/Passes/Pipelines.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
//===-- Pipelines.h -- FIR pass pipelines -----------------------*- 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
//
//===----------------------------------------------------------------------===//

/// This file declares some utilties to setup FIR pass pipelines. These are
/// common to flang and the test tools.

#ifndef FORTRAN_OPTIMIZER_PASSES_PIPELINES_H
#define FORTRAN_OPTIMIZER_PASSES_PIPELINES_H

#include "flang/Optimizer/CodeGen/CodeGen.h"
#include "flang/Optimizer/HLFIR/Passes.h"
#include "flang/Optimizer/OpenMP/Passes.h"
#include "flang/Optimizer/Passes/CommandLineOpts.h"
#include "flang/Optimizer/Transforms/Passes.h"
#include "flang/Tools/CrossToolHelpers.h"
#include "mlir/Conversion/ReconcileUnrealizedCasts/ReconcileUnrealizedCasts.h"
#include "mlir/Conversion/SCFToControlFlow/SCFToControlFlow.h"
#include "mlir/Dialect/LLVMIR/LLVMAttrs.h"
#include "mlir/Pass/PassManager.h"
#include "mlir/Transforms/GreedyPatternRewriteDriver.h"
#include "mlir/Transforms/Passes.h"
#include "llvm/Frontend/Debug/Options.h"
#include "llvm/Passes/OptimizationLevel.h"
#include "llvm/Support/CommandLine.h"

namespace fir {

using PassConstructor = std::unique_ptr<mlir::Pass>();

template <typename OP>
void addNestedPassToOps(mlir::PassManager &pm, PassConstructor ctor) {
pm.addNestedPass<OP>(ctor());
}

template <typename OP, typename... OPS,
typename = std::enable_if_t<sizeof...(OPS) != 0>>
void addNestedPassToOps(mlir::PassManager &pm, PassConstructor ctor) {
addNestedPassToOps<OP>(pm, ctor);
addNestedPassToOps<OPS...>(pm, ctor);
}

/// Generic for adding a pass to the pass manager if it is not disabled.
template <typename F>
void addPassConditionally(mlir::PassManager &pm, llvm::cl::opt<bool> &disabled,
F ctor) {
if (!disabled)
pm.addPass(ctor());
}

template <typename OP, typename F>
void addNestedPassConditionally(mlir::PassManager &pm,
llvm::cl::opt<bool> &disabled, F ctor) {
if (!disabled)
pm.addNestedPass<OP>(ctor());
}

void addNestedPassToAllTopLevelOperations(mlir::PassManager &pm,
PassConstructor ctor);

void addNestedPassToAllTopLevelOperationsConditionally(
mlir::PassManager &pm, llvm::cl::opt<bool> &disabled, PassConstructor ctor);

/// Add MLIR Canonicalizer pass with region simplification disabled.
/// FIR does not support the promotion of some SSA value to block arguments (or
/// into arith.select operands) that may be done by mlir block merging in the
/// region simplification (e.g., !fir.shape<> SSA values are not supported as
/// block arguments).
/// Aside from the fir.shape issue, moving some abstract SSA value into block
/// arguments may have a heavy cost since it forces their code generation that
/// may be expensive (array temporary). The MLIR pass does not take these
/// extra costs into account when doing block merging.
void addCanonicalizerPassWithoutRegionSimplification(mlir::OpPassManager &pm);

void addCfgConversionPass(mlir::PassManager &pm,
const MLIRToLLVMPassPipelineConfig &config);

void addAVC(mlir::PassManager &pm, const llvm::OptimizationLevel &optLevel);

void addMemoryAllocationOpt(mlir::PassManager &pm);

void addCodeGenRewritePass(mlir::PassManager &pm, bool preserveDeclare);

void addTargetRewritePass(mlir::PassManager &pm);

mlir::LLVM::DIEmissionKind
getEmissionKind(llvm::codegenoptions::DebugInfoKind kind);

void addBoxedProcedurePass(mlir::PassManager &pm);

void addExternalNameConversionPass(mlir::PassManager &pm,
bool appendUnderscore = true);

void addCompilerGeneratedNamesConversionPass(mlir::PassManager &pm);

void addDebugInfoPass(mlir::PassManager &pm,
llvm::codegenoptions::DebugInfoKind debugLevel,
llvm::OptimizationLevel optLevel,
llvm::StringRef inputFilename);

void addFIRToLLVMPass(mlir::PassManager &pm,
const MLIRToLLVMPassPipelineConfig &config);

void addLLVMDialectToLLVMPass(mlir::PassManager &pm, llvm::raw_ostream &output);

/// Use inliner extension point callback to register the default inliner pass.
void registerDefaultInlinerPass(MLIRToLLVMPassPipelineConfig &config);

/// Create a pass pipeline for running default optimization passes for
/// incremental conversion of FIR.
///
/// \param pm - MLIR pass manager that will hold the pipeline definition
void createDefaultFIROptimizerPassPipeline(mlir::PassManager &pm,
MLIRToLLVMPassPipelineConfig &pc);

/// Create a pass pipeline for lowering from HLFIR to FIR
///
/// \param pm - MLIR pass manager that will hold the pipeline definition
/// \param optLevel - optimization level used for creating FIR optimization
/// passes pipeline
void createHLFIRToFIRPassPipeline(
mlir::PassManager &pm, llvm::OptimizationLevel optLevel = defaultOptLevel);

/// Create a pass pipeline for handling certain OpenMP transformations needed
/// prior to FIR lowering.
///
/// WARNING: These passes must be run immediately after the lowering to ensure
/// that the FIR is correct with respect to OpenMP operations/attributes.
///
/// \param pm - MLIR pass manager that will hold the pipeline definition.
/// \param isTargetDevice - Whether code is being generated for a target device
/// rather than the host device.
void createOpenMPFIRPassPipeline(mlir::PassManager &pm, bool isTargetDevice);

#if !defined(FLANG_EXCLUDE_CODEGEN)
void createDebugPasses(mlir::PassManager &pm,
llvm::codegenoptions::DebugInfoKind debugLevel,
llvm::OptimizationLevel OptLevel,
llvm::StringRef inputFilename);

void createDefaultFIRCodeGenPassPipeline(mlir::PassManager &pm,
MLIRToLLVMPassPipelineConfig config,
llvm::StringRef inputFilename = {});

/// Create a pass pipeline for lowering from MLIR to LLVM IR
///
/// \param pm - MLIR pass manager that will hold the pipeline definition
/// \param optLevel - optimization level used for creating FIR optimization
/// passes pipeline
void createMLIRToLLVMPassPipeline(mlir::PassManager &pm,
MLIRToLLVMPassPipelineConfig &config,
llvm::StringRef inputFilename = {});
#undef FLANG_EXCLUDE_CODEGEN
#endif

} // namespace fir

#endif // FORTRAN_OPTIMIZER_PASSES_PIPELINES_H
Loading

0 comments on commit c3201dd

Please sign in to comment.