Skip to content

Commit 895ebce

Browse files
Make constant argument globalisation disabled by default.
This pass can convert local constant arguments into global values, but it can have negative effects on code that is borderline to non-conforming, where a constant argument is modified inside the callee - many compilers accept such code. So, to use this pass, it needs to be enabled explicitly. To enable in flang-new, use -mmlir --enable-constant-argument-globalisation
1 parent a537883 commit 895ebce

File tree

10 files changed

+24
-16
lines changed

10 files changed

+24
-16
lines changed

flang/include/flang/Tools/CLOptions.inc

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@
2525
static llvm::cl::opt<bool> disable##DOName("disable-" DOOption, \
2626
llvm::cl::desc("disable " DODescription " pass"), llvm::cl::init(false), \
2727
llvm::cl::Hidden)
28+
#define EnableOption(EOName, EOOption, EODescription) \
29+
static llvm::cl::opt<bool> enable##EOName("enable-" EOOption, \
30+
llvm::cl::desc("enable " EODescription " pass"), llvm::cl::init(false), \
31+
llvm::cl::Hidden)
2832

2933
/// Shared option in tools to control whether dynamically sized array
3034
/// allocations should always be on the heap.
@@ -86,8 +90,8 @@ DisableOption(BoxedProcedureRewrite, "boxed-procedure-rewrite",
8690

8791
DisableOption(ExternalNameConversion, "external-name-interop",
8892
"convert names with external convention");
89-
DisableOption(ConstantArgumentGlobalisation, "constant-argument-globalisation",
90-
"disable the local constants to global constant conversion");
93+
EnableOption(ConstantArgumentGlobalisation, "constant-argument-globalisation",
94+
"enable the local constants to global constant conversion");
9195

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

@@ -272,7 +276,7 @@ inline void createDefaultFIROptimizerPassPipeline(
272276
// These passes may increase code size.
273277
pm.addPass(fir::createSimplifyIntrinsics());
274278
pm.addPass(fir::createAlgebraicSimplificationPass(config));
275-
if (!disableConstantArgumentGlobalisation)
279+
if (enableConstantArgumentGlobalisation)
276280
pm.addPass(fir::createConstantArgumentGlobalisationOpt());
277281
}
278282

flang/lib/Optimizer/Transforms/ConstantArgumentGlobalisation.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ class CallOpRewriter : public mlir::OpRewritePattern<fir::CallOp> {
134134
for (auto a : allocas) {
135135
unsigned count = 0;
136136

137-
for (auto i : a.first->getUsers())
137+
for ([[maybe_unused]]auto i : a.first->getUsers())
138138
++count;
139139

140140
// If the alloca is only used for a store and the call operand, the

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
! CHECK-NEXT: SimplifyRegionLite
3333
! CHECK-NEXT: SimplifyIntrinsics
3434
! CHECK-NEXT: AlgebraicSimplification
35-
! CHECK-NEXT: ConstantArgumentGlobalisationOpt
3635
! CHECK-NEXT: CSE
3736
! CHECK-NEXT: (S) 0 num-cse'd - Number of operations CSE'd
3837
! CHECK-NEXT: (S) 0 num-dce'd - Number of operations DCE'd

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@
6666
! ALL-NEXT: SimplifyRegionLite
6767
! O2-NEXT: SimplifyIntrinsics
6868
! O2-NEXT: AlgebraicSimplification
69-
! O2-NEXT: ConstantArgumentGlobalisationOpt
7069
! ALL-NEXT: CSE
7170
! ALL-NEXT: (S) 0 num-cse'd - Number of operations CSE'd
7271
! ALL-NEXT: (S) 0 num-dce'd - Number of operations DCE'd

flang/test/Fir/basic-program.fir

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ func.func @_QQmain() {
6666
// PASSES-NEXT: SimplifyRegionLite
6767
// PASSES-NEXT: SimplifyIntrinsics
6868
// PASSES-NEXT: AlgebraicSimplification
69-
// PASSES-NEXT: ConstantArgumentGlobalisationOpt
7069
// PASSES-NEXT: CSE
7170
// PASSES-NEXT: (S) 0 num-cse'd - Number of operations CSE'd
7271
// PASSES-NEXT: (S) 0 num-dce'd - Number of operations DCE'd

flang/test/Fir/boxproc.fir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
// CHECK-LABEL: define void @_QPtest_proc_dummy_other(ptr
1818
// CHECK-SAME: %[[VAL_0:.*]])
19-
// CHECK: call void %[[VAL_0]](ptr @{{.*}})
19+
// CHECK: call void %[[VAL_0]](ptr %{{.*}})
2020

2121
func.func @_QPtest_proc_dummy() {
2222
%c0_i32 = arith.constant 0 : i32

flang/test/Lower/character-local-variables.f90

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
! RUN: bbc -hlfir=false %s -o - | FileCheck %s
2+
! RUN: bbc -hlfir=false --enable-constant-argument-globalisation %s -o - \
3+
! RUN: | FileCheck %s --check-prefix=CHECK-CONST
24

35
! Test lowering of local character variables
46

@@ -116,8 +118,10 @@ subroutine dyn_array_dyn_len_lb(l, n)
116118
subroutine assumed_length_param(n)
117119
character(*), parameter :: c(1)=(/"abcd"/)
118120
integer :: n
119-
! CHECK: %[[tmp:.*]] = fir.address_of(@_global_const_.{{.*}}) : !fir.ref<i64>
120-
! CHECK: fir.call @_QPtake_int(%[[tmp]]) {{.*}}: (!fir.ref<i64>) -> ()
121+
! CHECK: %[[c4:.*]] = arith.constant 4 : i64
122+
! CHECK: fir.store %[[c4]] to %[[tmp:.*]] : !fir.ref<i64>
123+
! CHECK-CONST: %[[tmp:.*]] = fir.address_of(@_global_const_.{{.*}}) : !fir.ref<i64>
124+
! CHECK-CONST: fir.call @_QPtake_int(%[[tmp]]) {{.*}}: (!fir.ref<i64>) -> ()
121125
call take_int(len(c(n), kind=8))
122126
end
123127

flang/test/Lower/dummy-arguments.f90

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
! CHECK-LABEL: _QQmain
44
program test1
5-
! CHECK-DAG: %[[TEN:.*]] = fir.address_of(@_global_const_.{{.*}}) : !fir.ref<i32>
5+
! CHECK-DAG: %[[TMP:.*]] = fir.alloca
6+
! CHECK-DAG: %[[TEN:.*]] = arith.constant
7+
! CHECK: fir.store %[[TEN]] to %[[TMP]]
68
! CHECK-NEXT: fir.call @_QFPfoo
79
call foo(10)
810
contains

flang/test/Lower/host-associated.f90

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -448,10 +448,11 @@ subroutine bar()
448448

449449
! CHECK-LABEL: func @_QPtest_proc_dummy_other(
450450
! CHECK-SAME: %[[VAL_0:.*]]: !fir.boxproc<() -> ()>) {
451+
! CHECK: %[[VAL_1:.*]] = arith.constant 4 : i32
452+
! CHECK: %[[VAL_2:.*]] = fir.alloca i32 {adapt.valuebyref}
453+
! CHECK: fir.store %[[VAL_1]] to %[[VAL_2]] : !fir.ref<i32>
451454
! CHECK: %[[VAL_3:.*]] = fir.box_addr %[[VAL_0]] : (!fir.boxproc<() -> ()>) -> ((!fir.ref<i32>) -> ())
452-
! CHECK: %[[VAL_1:.*]] = fir.address_of(@_global_const_.{{.*}}) : !fir.ref<i32>
453-
! CHECK: fir.call %[[VAL_3]](%[[VAL_1]]) {{.*}}: (!fir.ref<i32>) -> ()
454-
455+
! CHECK: fir.call %[[VAL_3]](%[[VAL_2]]) {{.*}}: (!fir.ref<i32>) -> ()
455456
! CHECK: return
456457
! CHECK: }
457458

flang/test/Transforms/constant-argument-globalisation.fir

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
// RUN: fir-opt --constant-argument-globalisation-opt < %s | FileCheck %s
2-
// RUN: %flang_fc1 -emit-llvm -flang-deprecated-no-hlfir -O2 -mllvm --disable-constant-argument-globalisation -o - %s | FileCheck --check-prefix=DISABLE %s
1+
// RUN: fir-opt --constant-argument-globalisation-opt < %s | FileCheck %s
2+
// RUN: %flang_fc1 -emit-llvm -flang-deprecated-no-hlfir -O2 -o - %s | FileCheck --check-prefix=DISABLE %s
33
module {
44
func.func @sub1(%arg0: !fir.ref<i32> {fir.bindc_name = "x"}, %arg1: !fir.ref<i32> {fir.bindc_name = "y"}) {
55
%0 = fir.alloca i32 {adapt.valuebyref}

0 commit comments

Comments
 (0)