Skip to content

Commit 06ee672

Browse files
[clang] Move opt level in clang toolchain to clang::ConstructJob start (#141036)
We currently transfer the opt level from the user clang call to CC1 args at the end of the `ConstructJob` function, this might lead to bugs as `ConstructJob` is a big function and we easily could add a change that would return early from it. That would cause the opt level to not be transferred to CC1 args and lead to wrong opt level compilation and would be hard to spot. This PR moves the opt level to the beginning of the function as opt level should be a direct transfer without any problems, it also removes the redundancy where it was added 2 times through the function.
1 parent 59b7b5b commit 06ee672

File tree

5 files changed

+24
-34
lines changed

5 files changed

+24
-34
lines changed

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5192,6 +5192,16 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
51925192
}
51935193
}
51945194

5195+
// Optimization level for CodeGen.
5196+
if (const Arg *A = Args.getLastArg(options::OPT_O_Group)) {
5197+
if (A->getOption().matches(options::OPT_O4)) {
5198+
CmdArgs.push_back("-O3");
5199+
D.Diag(diag::warn_O4_is_O3);
5200+
} else {
5201+
A->render(Args, CmdArgs);
5202+
}
5203+
}
5204+
51955205
// Unconditionally claim the printf option now to avoid unused diagnostic.
51965206
if (const Arg *PF = Args.getLastArg(options::OPT_mprintf_kind_EQ))
51975207
PF->claim();
@@ -5563,16 +5573,6 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
55635573
break;
55645574
}
55655575

5566-
// Optimization level for CodeGen.
5567-
if (const Arg *A = Args.getLastArg(options::OPT_O_Group)) {
5568-
if (A->getOption().matches(options::OPT_O4)) {
5569-
CmdArgs.push_back("-O3");
5570-
D.Diag(diag::warn_O4_is_O3);
5571-
} else {
5572-
A->render(Args, CmdArgs);
5573-
}
5574-
}
5575-
55765576
// Input/Output file.
55775577
if (Output.getType() == types::TY_Dependencies) {
55785578
// Handled with other dependency code.
@@ -6453,16 +6453,6 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
64536453
// preprocessed inputs and configure concludes that -fPIC is not supported.
64546454
Args.ClaimAllArgs(options::OPT_D);
64556455

6456-
// Manually translate -O4 to -O3; let clang reject others.
6457-
if (Arg *A = Args.getLastArg(options::OPT_O_Group)) {
6458-
if (A->getOption().matches(options::OPT_O4)) {
6459-
CmdArgs.push_back("-O3");
6460-
D.Diag(diag::warn_O4_is_O3);
6461-
} else {
6462-
A->render(Args, CmdArgs);
6463-
}
6464-
}
6465-
64666456
// Warn about ignored options to clang.
64676457
for (const Arg *A :
64686458
Args.filtered(options::OPT_clang_ignored_gcc_optimization_f_Group)) {

clang/test/Driver/Ofast.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,36 +12,36 @@
1212

1313
// CHECK-OFAST: use '-O3 -ffast-math' for the same behavior, or '-O3' to enable only conforming optimizations
1414
// CHECK-OFAST: -cc1
15+
// CHECK-OFAST: -Ofast
1516
// CHECK-OFAST-NOT: -relaxed-aliasing
1617
// CHECK-OFAST: -ffast-math
17-
// CHECK-OFAST: -Ofast
1818
// CHECK-OFAST: -vectorize-loops
1919

2020
// Lack of warning about '-Ofast' deprecation is checked via -Werror
2121
// CHECK-OFAST-O2: -cc1
22+
// CHECK-OFAST-O2-NOT: -Ofast
2223
// CHECK-OFAST-O2-ALIASING-NOT: -relaxed-aliasing
2324
// CHECK-OFAST-O2-ALIASING-MSVC: -relaxed-aliasing
2425
// CHECK-OFAST-O2-NOT: -ffast-math
25-
// CHECK-OFAST-O2-NOT: -Ofast
2626
// CHECK-OFAST-O2: -vectorize-loops
2727

2828
// CHECK-OFAST-NO-FAST-MATH: use '-O3 -ffast-math' for the same behavior, or '-O3' to enable only conforming optimizations
2929
// CHECK-OFAST-NO-FAST-MATH: -cc1
30+
// CHECK-OFAST-NO-FAST-MATH: -Ofast
3031
// CHECK-OFAST-NO-FAST-MATH-NOT: -relaxed-aliasing
3132
// CHECK-OFAST-NO-FAST-MATH-NOT: -ffast-math
32-
// CHECK-OFAST-NO-FAST-MATH: -Ofast
3333
// CHECK-OFAST-NO-FAST-MATH: -vectorize-loops
3434

3535
// CHECK-OFAST-NO-STRICT-ALIASING: use '-O3 -ffast-math' for the same behavior, or '-O3' to enable only conforming optimizations
3636
// CHECK-OFAST-NO-STRICT-ALIASING: -cc1
37+
// CHECK-OFAST-NO-STRICT-ALIASING: -Ofast
3738
// CHECK-OFAST-NO-STRICT-ALIASING: -relaxed-aliasing
3839
// CHECK-OFAST-NO-STRICT-ALIASING: -ffast-math
39-
// CHECK-OFAST-NO-STRICT-ALIASING: -Ofast
4040
// CHECK-OFAST-NO-STRICT-ALIASING: -vectorize-loops
4141

4242
// CHECK-OFAST-NO-VECTORIZE: use '-O3 -ffast-math' for the same behavior, or '-O3' to enable only conforming optimizations
4343
// CHECK-OFAST-NO-VECTORIZE: -cc1
44+
// CHECK-OFAST-NO-VECTORIZE: -Ofast
4445
// CHECK-OFAST-NO-VECTORIZE-NOT: -relaxed-aliasing
4546
// CHECK-OFAST-NO-VECTORIZE: -ffast-math
46-
// CHECK-OFAST-NO-VECTORIZE: -Ofast
4747
// CHECK-OFAST-NO-VECTORIZE-NOT: -vectorize-loops

clang/test/Driver/cl-options.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -185,30 +185,30 @@
185185

186186
// RUN: %clang_cl /Os --target=i686-pc-windows-msvc -### -- %s 2>&1 | FileCheck -check-prefix=Os %s
187187
// RUN: %clang_cl /Os --target=x86_64-pc-windows-msvc -### -- %s 2>&1 | FileCheck -check-prefix=Os %s
188-
// Os: -mframe-pointer=none
189188
// Os: -Os
189+
// Os: -mframe-pointer=none
190190

191191
// RUN: %clang_cl /Ot --target=i686-pc-windows-msvc -### -- %s 2>&1 | FileCheck -check-prefix=Ot %s
192192
// RUN: %clang_cl /Ot --target=x86_64-pc-windows-msvc -### -- %s 2>&1 | FileCheck -check-prefix=Ot %s
193-
// Ot: -mframe-pointer=none
194193
// Ot: -O3
194+
// Ot: -mframe-pointer=none
195195

196196
// RUN: %clang_cl /Ox --target=i686-pc-windows-msvc -### -- %s 2>&1 | FileCheck -check-prefix=Ox %s
197197
// RUN: %clang_cl /Ox --target=x86_64-pc-windows-msvc -### -- %s 2>&1 | FileCheck -check-prefix=Ox %s
198-
// Ox: -mframe-pointer=none
199198
// Ox: -O3
199+
// Ox: -mframe-pointer=none
200200

201201
// RUN: %clang_cl --target=i686-pc-win32 /O2sy- -### -- %s 2>&1 | FileCheck -check-prefix=PR24003 %s
202-
// PR24003: -mframe-pointer=all
203202
// PR24003: -Os
203+
// PR24003: -mframe-pointer=all
204204

205205
// RUN: %clang_cl --target=i686-pc-win32 -Werror -Wno-msvc-not-found /Oy- /O2 -### -- %s 2>&1 | FileCheck -check-prefix=Oy_2 %s
206-
// Oy_2: -mframe-pointer=all
207206
// Oy_2: -O3
207+
// Oy_2: -mframe-pointer=all
208208

209209
// RUN: %clang_cl --target=aarch64-pc-windows-msvc -Werror -Wno-msvc-not-found /Oy- /O2 -### -- %s 2>&1 | FileCheck -check-prefix=Oy_aarch64 %s
210-
// Oy_aarch64: -mframe-pointer=non-leaf
211210
// Oy_aarch64: -O3
211+
// Oy_aarch64: -mframe-pointer=non-leaf
212212

213213
// RUN: %clang_cl --target=i686-pc-win32 -Werror -Wno-msvc-not-found /O2 /O2 -### -- %s 2>&1 | FileCheck -check-prefix=O2O2 %s
214214
// O2O2: "-O3"

clang/test/Driver/clang-translation.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
// RUN: %clang -target i386-unknown-unknown -### -S -O0 -Os %s -o %t.s -fverbose-asm -fvisibility=hidden 2>&1 | FileCheck -check-prefix=I386 %s
22
// I386: "-triple" "i386-unknown-unknown"
3+
// I386: "-Os"
34
// I386: "-S"
45
// I386: "-disable-free"
56
// I386: "-mrelocation-model" "static"
67
// I386: "-mframe-pointer=all"
78
// I386: "-funwind-tables=2"
8-
// I386: "-Os"
99
// I386: "-fvisibility=hidden"
1010
// I386: "-o"
1111
// I386: clang-translation

clang/test/Driver/offload-Xarch.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@
3333
// RUN: %clang -x cuda %s --offload-arch=sm_52,sm_60 -Xarch_sm_52 -O3 -Xarch_sm_60 -O0 \
3434
// RUN: --target=x86_64-unknown-linux-gnu -Xarch_host -O3 -S -nogpulib -nogpuinc -### 2>&1 \
3535
// RUN: | FileCheck -check-prefix=CUDA %s
36-
// CUDA: "-cc1" "-triple" "nvptx64-nvidia-cuda" {{.*}}"-target-cpu" "sm_52" {{.*}}"-O3"
37-
// CUDA: "-cc1" "-triple" "nvptx64-nvidia-cuda" {{.*}}"-target-cpu" "sm_60" {{.*}}"-O0"
36+
// CUDA: "-cc1" "-triple" "nvptx64-nvidia-cuda" {{.*}}"-O3" {{.*}}"-target-cpu" "sm_52" {{.*}}
37+
// CUDA: "-cc1" "-triple" "nvptx64-nvidia-cuda" {{.*}}"-O0" {{.*}}"-target-cpu" "sm_60" {{.*}}
3838
// CUDA: "-cc1" "-triple" "x86_64-unknown-linux-gnu" {{.*}}"-O3"
3939

4040
// Make sure that `-Xarch_amdgcn` forwards libraries to the device linker.

0 commit comments

Comments
 (0)