[flang] Add ExternalNameConversionPass to pass pipeline
authorDiana Picus <diana.picus@linaro.org>
Wed, 2 Mar 2022 11:02:59 +0000 (11:02 +0000)
committerDiana Picus <diana.picus@linaro.org>
Fri, 6 May 2022 07:50:43 +0000 (07:50 +0000)
This seems to be the consensus in
https://github.com/flang-compiler/f18-llvm-project/issues/1316

The patch adds ExternalNameConversion to the default FIR CodeGen pass
pipeline, right before the FIRtoLLVM pass. It also adds a flag to
optionally disable it, and sets it in `tco`. In other words, `flang-new`
and `flang-new -fc1` will both run the pass by default, whereas `tco`
will not, so none of the tests need to be updated.

Differential Revision: https://reviews.llvm.org/D121171

flang/include/flang/Tools/CLOptions.inc
flang/test/Driver/disable-ext-name-interop.f90 [new file with mode: 0644]
flang/test/Driver/mlir-pass-pipeline.f90 [new file with mode: 0644]
flang/test/Fir/basic-program.fir
flang/tools/tco/tco.cpp

index 2a5a670..66e0818 100644 (file)
@@ -66,6 +66,8 @@ DisableOption(BoxedProcedureRewrite, "boxed-procedure-rewrite",
     "rewrite boxed procedures");
 #endif
 
+DisableOption(ExternalNameConversion, "external-name-interop", "convert names with external convention");
+
 /// Generic for adding a pass to the pass manager if it is not disabled.
 template <typename F>
 void addPassConditionally(
@@ -139,6 +141,11 @@ inline void addBoxedProcedurePass(mlir::PassManager &pm) {
 }
 #endif
 
+inline void addExternalNameConversionPass(mlir::PassManager &pm) {
+  addPassConditionally(pm, disableExternalNameConversion,
+      [&]() { return fir::createExternalNameConversionPass(); });
+}
+
 /// Create a pass pipeline for running default optimization passes for
 /// incremental conversion of FIR.
 ///
@@ -174,6 +181,7 @@ inline void createDefaultFIRCodeGenPassPipeline(mlir::PassManager &pm) {
   pm.addNestedPass<mlir::func::FuncOp>(fir::createAbstractResultOptPass());
   fir::addCodeGenRewritePass(pm);
   fir::addTargetRewritePass(pm);
+  fir::addExternalNameConversionPass(pm);
   fir::addFIRToLLVMPass(pm);
 }
 
diff --git a/flang/test/Driver/disable-ext-name-interop.f90 b/flang/test/Driver/disable-ext-name-interop.f90
new file mode 100644 (file)
index 0000000..0c59a5b
--- /dev/null
@@ -0,0 +1,9 @@
+! Test that we can disable the ExternalNameConversion pass in flang-new.
+
+! RUN: %flang_fc1 -S %s -o - 2>&1 | FileCheck %s --check-prefix=EXTNAMES
+! RUN: %flang_fc1 -S -mmlir -disable-external-name-interop %s -o - 2>&1 | FileCheck %s --check-prefix=INTNAMES
+
+! EXTNAMES: test_ext_names_
+! INTNAMES: _QPtest_ext_names
+subroutine test_ext_names
+end subroutine
diff --git a/flang/test/Driver/mlir-pass-pipeline.f90 b/flang/test/Driver/mlir-pass-pipeline.f90
new file mode 100644 (file)
index 0000000..ba006f7
--- /dev/null
@@ -0,0 +1,32 @@
+! Test the MLIR pass pipeline
+
+! RUN: %flang_fc1 -S -mmlir --mlir-pass-statistics -mmlir --mlir-pass-statistics-display=pipeline %s -o - 2>&1 | FileCheck %s
+end program
+
+! CHECK: Pass statistics report
+
+! CHECK-LABEL: 'func.func' Pipeline
+! CHECK: ArrayValueCopy
+! CHECK: CharacterConversion
+! CHECK: Canonicalizer
+! CHECK: SimplifyRegionLite
+
+! CHECK-LABEL: 'func.func' Pipeline
+! CHECK: MemoryAllocationOpt
+! CHECK: Inliner
+! CHECK: CSE
+
+! CHECK-LABEL: 'func.func' Pipeline
+! CHECK: CFGConversion
+! CHECK: SCFToControlFlow
+! CHECK: Canonicalizer
+! CHECK: SimplifyRegionLite
+! CHECK: BoxedProcedurePass
+
+! CHECK-LABEL: 'func.func' Pipeline
+! CHECK: AbstractResultOpt
+! CHECK: CodeGenRewrite
+! CHECK: TargetRewrite
+! CHECK: ExternalNameConversion
+! CHECK: FIRToLLVMLowering
+! CHECK-NOT: LLVMIRLoweringPass
index 0f22629..e646ddd 100644 (file)
@@ -1,6 +1,8 @@
 // RUN: tco %s | FileCheck %s
+// RUN: tco %s --mlir-pass-statistics --mlir-pass-statistics-display=pipeline 2>&1 | FileCheck %s --check-prefix=PASSES
 
 // Check that tco is working with a basic test.
+// Also check the passes in the default pipeline.
 
 func @_QQmain() {
   return
@@ -9,3 +11,30 @@ func @_QQmain() {
 // CHECK: ; ModuleID = 'FIRModule'
 // CHECK-LABEL: define void @_QQmain()
 // CHECK:       ret void
+
+// PASSES: Pass statistics report
+
+// PASSES-LABEL: 'func.func' Pipeline
+// PASSES: ArrayValueCopy
+// PASSES: CharacterConversion
+// PASSES: Canonicalizer
+// PASSES: SimplifyRegionLite
+
+// PASSES-LABEL: 'func.func' Pipeline
+// PASSES: MemoryAllocationOpt
+// PASSES: Inliner
+// PASSES: CSE
+
+// PASSES-LABEL: 'func.func' Pipeline
+// PASSES: CFGConversion
+// PASSES: SCFToControlFlow
+// PASSES: Canonicalizer
+// PASSES: SimplifyRegionLite
+// PASSES: BoxedProcedurePass
+
+// PASSES-LABEL: 'func.func' Pipeline
+// PASSES: AbstractResultOpt
+// PASSES: CodeGenRewrite
+// PASSES: TargetRewrite
+// PASSES: FIRToLLVMLowering
+// PASSES: LLVMIRLoweringPass
index e35325c..0c9e2f5 100644 (file)
@@ -131,6 +131,10 @@ compileFIR(const mlir::PassPipelineCLParser &passPipeline) {
 }
 
 int main(int argc, char **argv) {
+  // Disable the ExternalNameConversion pass by default until all the tests have
+  // been updated to pass with it enabled.
+  disableExternalNameConversion = true;
+
   [[maybe_unused]] InitLLVM y(argc, argv);
   fir::support::registerMLIRPassesForFortranTools();
   fir::registerOptCodeGenPasses();