[flang-commits] [flang] [clang] [flang] Enable alias tags pass by default (PR #73111)
Tom Eccles via flang-commits
flang-commits at lists.llvm.org
Wed Nov 22 13:45:26 PST 2023
https://github.com/tblah updated https://github.com/llvm/llvm-project/pull/73111
>From 6000017d6d23b2f861cd6dceb82f54a2685059b6 Mon Sep 17 00:00:00 2001
From: Tom Eccles <tom.eccles at arm.com>
Date: Thu, 14 Sep 2023 09:09:29 +0000
Subject: [PATCH 1/4] [flang] Enable alias tags pass by default
Enable by default when optimizing for speed.
For simplicity, only forward the flag to the frontend driver when it
contradicts what is implied by the optimization level.
---
clang/lib/Driver/ToolChains/Flang.cpp | 20 ++++++++++++++++++++
flang/include/flang/Tools/CLOptions.inc | 8 ++++----
flang/lib/Frontend/CompilerInvocation.cpp | 22 ++++++++++++++++++----
flang/test/Driver/falias-analysis.f90 | 4 ++++
flang/test/Driver/mlir-pass-pipeline.f90 | 2 ++
flang/test/Driver/optimization-remark.f90 | 22 +++++++++-------------
flang/test/Fir/basic-program.fir | 4 ++++
flang/tools/tco/tco.cpp | 1 +
8 files changed, 62 insertions(+), 21 deletions(-)
diff --git a/clang/lib/Driver/ToolChains/Flang.cpp b/clang/lib/Driver/ToolChains/Flang.cpp
index 8bdd920c3dcbb79..9382433b94dadfd 100644
--- a/clang/lib/Driver/ToolChains/Flang.cpp
+++ b/clang/lib/Driver/ToolChains/Flang.cpp
@@ -142,6 +142,26 @@ void Flang::addCodegenOptions(const ArgList &Args,
if (shouldLoopVersion(Args))
CmdArgs.push_back("-fversion-loops-for-stride");
+ Arg *aliasAnalysis = Args.getLastArg(options::OPT_falias_analysis,
+ options::OPT_fno_alias_analysis);
+ Arg *optLevel =
+ Args.getLastArg(options::OPT_Ofast, options::OPT_O, options::OPT_O4);
+ if (aliasAnalysis) {
+ bool falias_analysis =
+ aliasAnalysis->getOption().matches(options::OPT_falias_analysis);
+ // only pass on the argument if it does not match that implied by the
+ // optimization level
+ if (optLevel) {
+ if (!falias_analysis) {
+ CmdArgs.push_back("-fno-alias-analysis");
+ }
+ } else {
+ if (falias_analysis)
+ // requested alias analysis but no optimization enabled
+ CmdArgs.push_back("-falias-analysis");
+ }
+ }
+
Args.addAllArgs(CmdArgs, {options::OPT_flang_experimental_hlfir,
options::OPT_flang_deprecated_no_hlfir,
options::OPT_flang_experimental_polymorphism,
diff --git a/flang/include/flang/Tools/CLOptions.inc b/flang/include/flang/Tools/CLOptions.inc
index c452c023b4a80ce..5a17385fb3dae87 100644
--- a/flang/include/flang/Tools/CLOptions.inc
+++ b/flang/include/flang/Tools/CLOptions.inc
@@ -157,11 +157,11 @@ inline void addDebugFoundationPass(mlir::PassManager &pm) {
[&]() { return fir::createAddDebugFoundationPass(); });
}
-inline void addFIRToLLVMPass(
- mlir::PassManager &pm, llvm::OptimizationLevel optLevel = defaultOptLevel) {
+inline void addFIRToLLVMPass(mlir::PassManager &pm,
+ llvm::OptimizationLevel optLevel = defaultOptLevel, bool applyTbaa = true) {
fir::FIRToLLVMPassOptions options;
options.ignoreMissingTypeDescriptors = ignoreMissingTypeDescriptors;
- options.applyTBAA = optLevel.isOptimizingForSpeed();
+ options.applyTBAA = applyTbaa;
options.forceUnifiedTBAATree = useOldAliasTags;
addPassConditionally(pm, disableFirToLlvmIr,
[&]() { return fir::createFIRToLLVMPass(options); });
@@ -311,7 +311,7 @@ inline void createDefaultFIRCodeGenPassPipeline(
if (config.VScaleMin != 0)
pm.addPass(fir::createVScaleAttrPass({config.VScaleMin, config.VScaleMax}));
- fir::addFIRToLLVMPass(pm, config.OptLevel);
+ fir::addFIRToLLVMPass(pm, config.OptLevel, config.AliasAnalysis);
}
/// Create a pass pipeline for lowering from MLIR to LLVM IR
diff --git a/flang/lib/Frontend/CompilerInvocation.cpp b/flang/lib/Frontend/CompilerInvocation.cpp
index cb4f2d6a6225205..cfb1dd91ead3056 100644
--- a/flang/lib/Frontend/CompilerInvocation.cpp
+++ b/flang/lib/Frontend/CompilerInvocation.cpp
@@ -242,10 +242,24 @@ static void parseCodeGenArgs(Fortran::frontend::CodeGenOptions &opts,
clang::driver::options::OPT_fno_loop_versioning, false))
opts.LoopVersioning = 1;
- opts.AliasAnalysis =
- args.hasFlag(clang::driver::options::OPT_falias_analysis,
- clang::driver::options::OPT_fno_alias_analysis,
- /*default=*/false);
+ bool aliasAnalysis = false;
+ bool noAliasAnalysis = false;
+ if (auto *arg =
+ args.getLastArg(clang::driver::options::OPT_falias_analysis,
+ clang::driver::options::OPT_fno_alias_analysis)) {
+ if (arg->getOption().matches(clang::driver::options::OPT_falias_analysis))
+ aliasAnalysis = true;
+ else
+ noAliasAnalysis = true;
+ }
+ opts.AliasAnalysis = 0;
+ if (opts.OptimizationLevel > 0) {
+ if (!noAliasAnalysis)
+ opts.AliasAnalysis = 1;
+ } else {
+ if (aliasAnalysis)
+ opts.AliasAnalysis = 1;
+ }
for (auto *a : args.filtered(clang::driver::options::OPT_fpass_plugin_EQ))
opts.LLVMPassPlugins.push_back(a->getValue());
diff --git a/flang/test/Driver/falias-analysis.f90 b/flang/test/Driver/falias-analysis.f90
index f2c5dbde6d2c878..1c74276974d4720 100644
--- a/flang/test/Driver/falias-analysis.f90
+++ b/flang/test/Driver/falias-analysis.f90
@@ -4,10 +4,14 @@
! RUN: %flang -c -emit-llvm -falias-analysis %s -o - | llvm-dis | FileCheck %s --check-prefix=CHECK-AA --check-prefix=CHECK-ALL
! RUN: %flang -c -emit-llvm -falias-analysis -fno-alias-analysis %s -o - | llvm-dis | FileCheck %s --check-prefix=CHECK-NOAA --check-prefix=CHECK-ALL
! RUN: %flang -c -emit-llvm %s -o - | llvm-dis | FileCheck %s --check-prefix=CHECK-NOAA --check-prefix=CHECK-ALL
+! RUN: %flang -c -emit-llvm -Ofast %s -o - | llvm-dis | FileCheck %s --check-prefix=CHECK-AA --check-prefix=CHECK-ALL
+! RUN: %flang -c -emit-llvm -Ofast -fno-alias-analysis %s -o - | llvm-dis | FileCheck %s --check-prefix=CHECK-NOAA --check-prefix=CHECK-ALL
! RUN: %flang -fc1 -emit-llvm -falias-analysis %s -o - | FileCheck %s --check-prefix=CHECK-AA --check-prefix=CHECK-ALL
! RUN: %flang -fc1 -emit-llvm -falias-analysis -fno-alias-analysis %s -o - | FileCheck %s --check-prefix=CHECK-NOAA --check-prefix=CHECK-ALL
! RUN: %flang -fc1 -emit-llvm %s -o - | FileCheck %s --check-prefix=CHECK-NOAA --check-prefix=CHECK-ALL
+! RUN: %flang -fc1 -emit-llvm -O3 %s -o - | FileCheck %s --check-prefix=CHECK-AA --check-prefix=CHECK-ALL
+! RUN: %flang -fc1 -emit-llvm -O3 -fno-alias-analysis %s -o - | FileCheck %s --check-prefix=CHECK-NOAA --check-prefix=CHECK-ALL
subroutine simple(a)
integer, intent(inout) :: a(:)
diff --git a/flang/test/Driver/mlir-pass-pipeline.f90 b/flang/test/Driver/mlir-pass-pipeline.f90
index 7f92ec25bef98ec..3d8c42f123e2eb0 100644
--- a/flang/test/Driver/mlir-pass-pipeline.f90
+++ b/flang/test/Driver/mlir-pass-pipeline.f90
@@ -51,6 +51,8 @@
! ALL-NEXT: 'func.func' Pipeline
! ALL-NEXT: PolymorphicOpConversion
+! O2-NEXT: AddAliasTags
+! O2-NEXT: 'func.func' Pipeline
! ALL-NEXT: CFGConversion
! ALL-NEXT: SCFToControlFlow
diff --git a/flang/test/Driver/optimization-remark.f90 b/flang/test/Driver/optimization-remark.f90
index 13fc24346eac68b..20ff9eb59a6702d 100644
--- a/flang/test/Driver/optimization-remark.f90
+++ b/flang/test/Driver/optimization-remark.f90
@@ -41,28 +41,24 @@
! Once we start filtering, this is reduced to 1 one of the loop passes.
! PASS-REGEX-LOOP-ONLY-NOT: optimization-remark.f90:77:7: remark: hoisting load [-Rpass=licm]
-! PASS-REGEX-LOOP-ONLY: optimization-remark.f90:83:5: remark: Loop deleted because it is invariant [-Rpass=loop-delete]
+! PASS-REGEX-LOOP-ONLY: optimization-remark.f90:79:5: remark: Loop deleted because it is invariant [-Rpass=loop-delete]
! MISSED-REGEX-LOOP-ONLY-NOT: optimization-remark.f90:77:7: remark: failed to hoist load with loop-invariant address because load is conditionally executed [-Rpass-missed=licm]
-! MISSED-REGEX-LOOP-ONLY: optimization-remark.f90:76:4: remark: loop not vectorized [-Rpass-missed=loop-vectorize]
+! MISSED-REGEX-LOOP-ONLY: optimization-remark.f90:72:4: remark: loop not vectorized [-Rpass-missed=loop-vectorize]
-! ANALYSIS-REGEX-LOOP-ONLY: optimization-remark.f90:79:7: remark: loop not vectorized: unsafe dependent memory operations in loop. Use #pragma clang loop distribute(enable) to allow loop distribution to attempt to isolate the offending operations into a separate loop
-! ANALYSIS-REGEX-LOOP-ONLY: Unknown data dependence. Memory location is the same as accessed at optimization-remark.f90:78:7 [-Rpass-analysis=loop-vectorize]
+! ANALYSIS-REGEX-LOOP-ONLY: optimization-remark.f90:73:7: remark: loop not vectorized: cannot identify array bounds [-Rpass-analysis=loop-vectorize]
! ANALYSIS-REGEX-LOOP-ONLY-NOT: remark: {{.*}}: IR instruction count changed from {{[0-9]+}} to {{[0-9]+}}; Delta: {{-?[0-9]+}} [-Rpass-analysis=size-info]
-! PASS: optimization-remark.f90:77:7: remark: hoisting load [-Rpass=licm]
-! PASS: optimization-remark.f90:83:5: remark: Loop deleted because it is invariant [-Rpass=loop-delete]
+! PASS: optimization-remark.f90:79:5: remark: Loop deleted because it is invariant [-Rpass=loop-delete]
-! MISSED: optimization-remark.f90:77:7: remark: failed to hoist load with loop-invariant address because load is conditionally executed [-Rpass-missed=licm]
-! MISSED: optimization-remark.f90:76:4: remark: loop not vectorized [-Rpass-missed=loop-vectorize]
-! MISSED-NOT: optimization-remark.f90:79:7: remark: loop not vectorized: unsafe dependent memory operations in loop. Use #pragma clang loop distribute(enable) to allow loop distribution to attempt to isolate the offending operations into a separate loop
+! MISSED: optimization-remark.f90:73:7: remark: failed to move load with loop-invariant address because the loop may invalidate its value [-Rpass-missed=licm]
+! MISSED: optimization-remark.f90:72:4: remark: loop not vectorized [-Rpass-missed=loop-vectorize]
+! MISSED-NOT: optimization-remark.f90:75:7: remark: loop not vectorized: unsafe dependent memory operations in loop. Use #pragma clang loop distribute(enable) to allow loop distribution to attempt to isolate the offending operations into a separate loop
! MISSED-NOT: Unknown data dependence. Memory location is the same as accessed at optimization-remark.f90:78:7 [-Rpass-analysis=loop-vectorize]
-! ANALYSIS: optimization-remark.f90:79:7: remark: loop not vectorized: unsafe dependent memory operations in loop. Use #pragma clang loop distribute(enable) to allow loop distribution to attempt to isolate the offending operations into a separate loop
-! ANALYSIS: Unknown data dependence. Memory location is the same as accessed at optimization-remark.f90:78:7 [-Rpass-analysis=loop-vectorize]
-! ANALYSIS: remark: {{.*}}: IR instruction count changed from {{[0-9]+}} to {{[0-9]+}}; Delta: {{-?[0-9]+}} [-Rpass-analysis=size-info]
-! ANALYSIS-NOT: optimization-remark.f90:77:7: remark: failed to hoist load with loop-invariant address because load is conditionally executed [-Rpass-missed=licm]
+! ANALYSIS: optimization-remark.f90:74:7: remark: loop not vectorized: unsafe dependent memory operations in loop.
+! ANALYSIS: remark: {{.*}} instructions in function [-Rpass-analysis=asm-printer]
subroutine swap_real(a1, a2)
implicit none
diff --git a/flang/test/Fir/basic-program.fir b/flang/test/Fir/basic-program.fir
index 0e82f7dfdedb447..d8a9e74c318ce18 100644
--- a/flang/test/Fir/basic-program.fir
+++ b/flang/test/Fir/basic-program.fir
@@ -57,6 +57,10 @@ func.func @_QQmain() {
// PASSES-NEXT: 'func.func' Pipeline
// PASSES-NEXT: PolymorphicOpConversion
+
+// PASSES-NEXT: AddAliasTags
+
+// PASSES-NEXT: 'func.func' Pipeline
// PASSES-NEXT: CFGConversion
// PASSES-NEXT: SCFToControlFlow
diff --git a/flang/tools/tco/tco.cpp b/flang/tools/tco/tco.cpp
index 31d6bac142dc421..a649535a39b74b3 100644
--- a/flang/tools/tco/tco.cpp
+++ b/flang/tools/tco/tco.cpp
@@ -120,6 +120,7 @@ compileFIR(const mlir::PassPipelineCLParser &passPipeline) {
return mlir::failure();
} else {
MLIRToLLVMPassPipelineConfig config(llvm::OptimizationLevel::O2);
+ config.AliasAnalysis = true; // enabled when optimizing for speed
if (codeGenLLVM) {
// Run only CodeGen passes.
fir::createDefaultFIRCodeGenPassPipeline(pm, config);
>From b776a359cb2ef92db55ca70d168e23533e69d469 Mon Sep 17 00:00:00 2001
From: Tom Eccles <tom.eccles at arm.com>
Date: Wed, 22 Nov 2023 15:32:36 +0000
Subject: [PATCH 2/4] Pass the whole MLIRToLLVMPassPipelineConfig to
addFIRToLLVMPass()
---
flang/include/flang/Tools/CLOptions.inc | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/flang/include/flang/Tools/CLOptions.inc b/flang/include/flang/Tools/CLOptions.inc
index 5a17385fb3dae87..0e00f9c01cc939a 100644
--- a/flang/include/flang/Tools/CLOptions.inc
+++ b/flang/include/flang/Tools/CLOptions.inc
@@ -158,10 +158,10 @@ inline void addDebugFoundationPass(mlir::PassManager &pm) {
}
inline void addFIRToLLVMPass(mlir::PassManager &pm,
- llvm::OptimizationLevel optLevel = defaultOptLevel, bool applyTbaa = true) {
+ const MLIRToLLVMPassPipelineConfig &config) {
fir::FIRToLLVMPassOptions options;
options.ignoreMissingTypeDescriptors = ignoreMissingTypeDescriptors;
- options.applyTBAA = applyTbaa;
+ options.applyTBAA = config.AliasAnalysis;
options.forceUnifiedTBAATree = useOldAliasTags;
addPassConditionally(pm, disableFirToLlvmIr,
[&]() { return fir::createFIRToLLVMPass(options); });
@@ -311,7 +311,7 @@ inline void createDefaultFIRCodeGenPassPipeline(
if (config.VScaleMin != 0)
pm.addPass(fir::createVScaleAttrPass({config.VScaleMin, config.VScaleMax}));
- fir::addFIRToLLVMPass(pm, config.OptLevel, config.AliasAnalysis);
+ fir::addFIRToLLVMPass(pm, config);
}
/// Create a pass pipeline for lowering from MLIR to LLVM IR
>From b5b0aa7c97dcdd723ca53c71b77382e834ec1938 Mon Sep 17 00:00:00 2001
From: Tom Eccles <tom.eccles at arm.com>
Date: Wed, 22 Nov 2023 21:39:49 +0000
Subject: [PATCH 3/4] Rename variable and combine if condition
---
clang/lib/Driver/ToolChains/Flang.cpp | 10 ++++------
1 file changed, 4 insertions(+), 6 deletions(-)
diff --git a/clang/lib/Driver/ToolChains/Flang.cpp b/clang/lib/Driver/ToolChains/Flang.cpp
index 9382433b94dadfd..b9a97cb67c5e3dc 100644
--- a/clang/lib/Driver/ToolChains/Flang.cpp
+++ b/clang/lib/Driver/ToolChains/Flang.cpp
@@ -147,16 +147,14 @@ void Flang::addCodegenOptions(const ArgList &Args,
Arg *optLevel =
Args.getLastArg(options::OPT_Ofast, options::OPT_O, options::OPT_O4);
if (aliasAnalysis) {
- bool falias_analysis =
+ bool faliasAnalysis =
aliasAnalysis->getOption().matches(options::OPT_falias_analysis);
// only pass on the argument if it does not match that implied by the
// optimization level
- if (optLevel) {
- if (!falias_analysis) {
- CmdArgs.push_back("-fno-alias-analysis");
- }
+ if (optLevel && !faliasAnalysis) {
+ CmdArgs.push_back("-fno-alias-analysis");
} else {
- if (falias_analysis)
+ if (faliasAnalysis)
// requested alias analysis but no optimization enabled
CmdArgs.push_back("-falias-analysis");
}
>From 5dbf3c29b37d5c21c1f9d4b9e4990297b756b464 Mon Sep 17 00:00:00 2001
From: Tom Eccles <tom.eccles at arm.com>
Date: Wed, 22 Nov 2023 21:45:01 +0000
Subject: [PATCH 4/4] fix formatting
---
flang/include/flang/Tools/CLOptions.inc | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/flang/include/flang/Tools/CLOptions.inc b/flang/include/flang/Tools/CLOptions.inc
index 0e00f9c01cc939a..d3e4dc6cd4a243e 100644
--- a/flang/include/flang/Tools/CLOptions.inc
+++ b/flang/include/flang/Tools/CLOptions.inc
@@ -157,8 +157,8 @@ inline void addDebugFoundationPass(mlir::PassManager &pm) {
[&]() { return fir::createAddDebugFoundationPass(); });
}
-inline void addFIRToLLVMPass(mlir::PassManager &pm,
- const MLIRToLLVMPassPipelineConfig &config) {
+inline void addFIRToLLVMPass(
+ mlir::PassManager &pm, const MLIRToLLVMPassPipelineConfig &config) {
fir::FIRToLLVMPassOptions options;
options.ignoreMissingTypeDescriptors = ignoreMissingTypeDescriptors;
options.applyTBAA = config.AliasAnalysis;
More information about the flang-commits
mailing list