Skip to content

Commit acf6a32

Browse files
committed
[flang] add -flang-experimental-hlfir flag to flang-new
This flag instructs flang-new to use the new HLFIR lowering. It is marked as experimental and not included in --help. This was added to make it more convenient to test the performance of code generated by the HLFIR lowering. Extra diffs are from running clang-format on CLOptions.inc (which was being forced by CI). Differential Revision: https://reviews.llvm.org/D146278
1 parent 3a3ad9f commit acf6a32

File tree

8 files changed

+63
-4
lines changed

8 files changed

+63
-4
lines changed

clang/include/clang/Driver/Options.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5080,6 +5080,10 @@ def flang_experimental_exec : Flag<["-"], "flang-experimental-exec">,
50805080
Flags<[FlangOption, FlangOnlyOption, NoXarchOption, HelpHidden]>,
50815081
HelpText<"Enable support for generating executables (experimental)">;
50825082

5083+
def flang_experimental_hlfir : Flag<["-"], "flang-experimental-hlfir">,
5084+
Flags<[FlangOption, FC1Option, FlangOnlyOption, NoXarchOption, HelpHidden]>,
5085+
HelpText<"Use HLFIR lowering (experimental)">;
5086+
50835087
//===----------------------------------------------------------------------===//
50845088
// FLangOption + CoreOption + NoXarchOption
50855089
//===----------------------------------------------------------------------===//

clang/lib/Driver/ToolChains/Flang.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ void Flang::addOtherOptions(const ArgList &Args, ArgStringList &CmdArgs) const {
6565
if (stackArrays &&
6666
!stackArrays->getOption().matches(options::OPT_fno_stack_arrays))
6767
CmdArgs.push_back("-fstack-arrays");
68+
69+
if (Args.hasArg(options::OPT_flang_experimental_hlfir))
70+
CmdArgs.push_back("-flang-experimental-hlfir");
6871
}
6972

7073
void Flang::addPicOptions(const ArgList &Args, ArgStringList &CmdArgs) const {

flang/include/flang/Tools/CLOptions.inc

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "mlir/Transforms/GreedyPatternRewriteDriver.h"
1515
#include "mlir/Transforms/Passes.h"
1616
#include "flang/Optimizer/CodeGen/CodeGen.h"
17+
#include "flang/Optimizer/HLFIR/Passes.h"
1718
#include "flang/Optimizer/Transforms/Passes.h"
1819
#include "llvm/Passes/OptimizationLevel.h"
1920
#include "llvm/Support/CommandLine.h"
@@ -72,7 +73,8 @@ DisableOption(BoxedProcedureRewrite, "boxed-procedure-rewrite",
7273
"rewrite boxed procedures");
7374
#endif
7475

75-
DisableOption(ExternalNameConversion, "external-name-interop", "convert names with external convention");
76+
DisableOption(ExternalNameConversion, "external-name-interop",
77+
"convert names with external convention");
7678

7779
/// Generic for adding a pass to the pass manager if it is not disabled.
7880
template <typename F>
@@ -211,15 +213,28 @@ inline void createDefaultFIROptimizerPassPipeline(mlir::PassManager &pm,
211213
pm.addPass(mlir::createCSEPass());
212214
}
213215

216+
/// Create a pass pipeline for lowering from HLFIR to FIR
217+
///
218+
/// \param pm - MLIR pass manager that will hold the pipeline definition
219+
/// \param optLevel - optimization level used for creating FIR optimization
220+
/// passes pipeline
221+
inline void createHLFIRToFIRPassPipeline(
222+
mlir::PassManager &pm, llvm::OptimizationLevel optLevel = defaultOptLevel) {
223+
if (optLevel.isOptimizingForSpeed())
224+
pm.addPass(mlir::createCanonicalizerPass());
225+
pm.addPass(hlfir::createLowerHLFIRIntrinsicsPass());
226+
pm.addPass(hlfir::createBufferizeHLFIRPass());
227+
pm.addPass(hlfir::createConvertHLFIRtoFIRPass());
228+
}
229+
214230
#if !defined(FLANG_EXCLUDE_CODEGEN)
215231
inline void createDefaultFIRCodeGenPassPipeline(mlir::PassManager &pm,
216232
llvm::OptimizationLevel optLevel = defaultOptLevel,
217233
bool underscoring = true) {
218234
fir::addBoxedProcedurePass(pm);
219235
pm.addNestedPass<mlir::func::FuncOp>(
220236
fir::createAbstractResultOnFuncOptPass());
221-
pm.addNestedPass<fir::GlobalOp>(
222-
fir::createAbstractResultOnGlobalOptPass());
237+
pm.addNestedPass<fir::GlobalOp>(fir::createAbstractResultOnGlobalOptPass());
223238
fir::addCodeGenRewritePass(pm);
224239
fir::addTargetRewritePass(pm);
225240
fir::addExternalNameConversionPass(pm, underscoring);
@@ -234,6 +249,8 @@ inline void createDefaultFIRCodeGenPassPipeline(mlir::PassManager &pm,
234249
inline void createMLIRToLLVMPassPipeline(mlir::PassManager &pm,
235250
llvm::OptimizationLevel optLevel = defaultOptLevel,
236251
bool stackArrays = false, bool underscoring = true) {
252+
fir::createHLFIRToFIRPassPipeline(pm, optLevel);
253+
237254
// Add default optimizer pass pipeline.
238255
fir::createDefaultFIROptimizerPassPipeline(pm, optLevel, stackArrays);
239256

flang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -818,6 +818,11 @@ bool CompilerInvocation::createFromArgs(
818818
success = false;
819819
}
820820

821+
// -flang-experimental-hlfir
822+
if (args.hasArg(clang::driver::options::OPT_flang_experimental_hlfir)) {
823+
res.loweringOpts.setLowerToHighLevelFIR(true);
824+
}
825+
821826
success &= parseFrontendArgs(res.getFrontendOpts(), args, diags);
822827
parseTargetArgs(res.getTargetOpts(), args);
823828
parsePreprocessorArgs(res.getPreprocessorOpts(), args);

flang/test/Driver/driver-help-hidden.f90

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
! CHECK-NEXT: Specify where to find the compiled intrinsic modules
4242
! CHECK-NEXT: -flang-experimental-exec
4343
! CHECK-NEXT: Enable support for generating executables (experimental)
44+
! CHECK-NEXT: -flang-experimental-hlfir
45+
! CHECK-NEXT: Use HLFIR lowering (experimental)
4446
! CHECK-NEXT: -flarge-sizes Use INTEGER(KIND=8) for the result type in size-related intrinsics
4547
! CHECK-NEXT: -flogical-abbreviations Enable logical abbreviations
4648
! CHECK-NEXT: -flto=<value> Set LTO mode

flang/test/Driver/mlir-pass-pipeline.f90

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
! ALL: Pass statistics report
1313

1414
! ALL: Fortran::lower::VerifierPass
15+
! O2-NEXT: Canonicalizer
16+
! ALL-NEXT: LowerHLFIRIntrinsics
17+
! ALL-NEXT: BufferizeHLFIR
18+
! ALL-NEXT: ConvertHLFIRtoFIR
1519
! ALL-NEXT: CSE
1620
! Ideally, we need an output with only the pass names, but
1721
! there is currently no way to get that, so in order to

flang/test/Fir/basic-program.fir

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ func.func @_QQmain() {
1616

1717
// PASSES: Pass statistics report
1818

19-
// PASSES: CSE
19+
// PASSES: Canonicalizer
20+
// PASSES-NEXT: LowerHLFIRIntrinsics
21+
// PASSES-NEXT: BufferizeHLFIR
22+
// PASSES-NEXT: ConvertHLFIRtoFIR
23+
// PASSES-NEXT: CSE
2024
// PASSES-NEXT: (S) 0 num-cse'd - Number of operations CSE'd
2125
// PASSES-NEXT: (S) 0 num-dce'd - Number of operations DCE'd
2226

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
! Test -flang-experimental-hlfir flag
2+
! RUN: %flang_fc1 -flang-experimental-hlfir -emit-fir -o - %s | FileCheck %s
3+
! RUN: %flang_fc1 -emit-fir -o - %s | FileCheck %s --check-prefix NO-HLFIR
4+
5+
subroutine test(a, res)
6+
real :: a(:), res
7+
res = SUM(a)
8+
end subroutine
9+
! CHECK-LABEL: func.func @_QPtest
10+
! CHECK: %[[A:.*]]: !fir.box<!fir.array<?xf32>>
11+
! CHECK: %[[RES:.*]]: !fir.ref<f32>
12+
! CHECK-DAG: %[[A_VAR:.*]]:2 = hlfir.declare %[[A]]
13+
! CHECK-DAG: %[[RES_VAR:.*]]:2 = hlfir.declare %[[RES]]
14+
! CHECK-NEXT: %[[SUM_RES:.*]] = hlfir.sum %[[A_VAR]]#0
15+
! CHECK-NEXT: hlfir.assign %[[SUM_RES]] to %[[RES_VAR]]#0
16+
! CHECK-NEXT: hlfir.destroy %[[SUM_RES]]
17+
! CHECK-NEXT: return
18+
! CHECK-NEXT: }
19+
20+
! NO-HLFIR-NOT: hlfir.

0 commit comments

Comments
 (0)