Skip to content

Commit d637647

Browse files
authored
[clang][Driver] Fix enabling strict alising by default when the environment is MSVC (#91689)
From looking at the rest of code and from my own understanding, the driver mode is supposed to be independent of MSVC compatibility when the target triple is `*-windows-msvc`. Therefore strict aliasing should be disabled by default when the target triple is `*-windows-msvc` so code assuming MSVC behaves as expected when compiled with Clang.
1 parent c460e45 commit d637647

File tree

4 files changed

+17
-5
lines changed

4 files changed

+17
-5
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -878,6 +878,10 @@ Windows Support
878878
including STL headers will no longer slow down compile times since ``intrin.h``
879879
is not included from MSVC STL.
880880

881+
- When the target triple is `*-windows-msvc` strict aliasing is now disabled by default
882+
to ensure compatibility with msvc. Previously strict aliasing was only disabled if the
883+
driver mode was cl.
884+
881885
LoongArch Support
882886
^^^^^^^^^^^^^^^^^
883887

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5681,11 +5681,10 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
56815681
// enabled. This alias option is being used to simplify the hasFlag logic.
56825682
OptSpecifier StrictAliasingAliasOption =
56835683
OFastEnabled ? options::OPT_Ofast : options::OPT_fstrict_aliasing;
5684-
// We turn strict aliasing off by default if we're in CL mode, since MSVC
5684+
// We turn strict aliasing off by default if we're Windows MSVC since MSVC
56855685
// doesn't do any TBAA.
5686-
bool TBAAOnByDefault = !D.IsCLMode();
56875686
if (!Args.hasFlag(options::OPT_fstrict_aliasing, StrictAliasingAliasOption,
5688-
options::OPT_fno_strict_aliasing, TBAAOnByDefault))
5687+
options::OPT_fno_strict_aliasing, !IsWindowsMSVC))
56895688
CmdArgs.push_back("-relaxed-aliasing");
56905689
if (!Args.hasFlag(options::OPT_fstruct_path_tbaa,
56915690
options::OPT_fno_struct_path_tbaa, true))

clang/test/Driver/Ofast.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
// RUN: %clang -fno-fast-math -Ofast -### %s 2>&1 | FileCheck -check-prefix=CHECK-OFAST %s
44
// RUN: %clang -fno-strict-aliasing -Ofast -### %s 2>&1 | FileCheck -check-prefix=CHECK-OFAST %s
55
// RUN: %clang -fno-vectorize -Ofast -### %s 2>&1 | FileCheck -check-prefix=CHECK-OFAST %s
6-
// RUN: %clang -Ofast -O2 -### %s 2>&1 | FileCheck -check-prefix=CHECK-OFAST-O2 %s
6+
// RUN: %clang -Ofast -O2 -### %s 2>&1 | FileCheck -check-prefix=CHECK-OFAST-O2 \
7+
// RUN: %if target={{.*-windows-msvc.*}} %{ --check-prefix=CHECK-OFAST-O2-ALIASING-MSVC %} \
8+
// RUN: %else %{ --check-prefix=CHECK-OFAST-O2-ALIASING %} %s
79
// RUN: %clang -Ofast -fno-fast-math -### %s 2>&1 | FileCheck -check-prefix=CHECK-OFAST-NO-FAST-MATH %s
810
// RUN: %clang -Ofast -fno-strict-aliasing -### %s 2>&1 | FileCheck -check-prefix=CHECK-OFAST-NO-STRICT-ALIASING %s
911
// RUN: %clang -Ofast -fno-vectorize -### %s 2>&1 | FileCheck -check-prefix=CHECK-OFAST-NO-VECTORIZE %s
@@ -15,7 +17,8 @@
1517
// CHECK-OFAST: -vectorize-loops
1618

1719
// CHECK-OFAST-O2: -cc1
18-
// CHECK-OFAST-O2-NOT: -relaxed-aliasing
20+
// CHECK-OFAST-O2-ALIASING-NOT: -relaxed-aliasing
21+
// CHECK-OFAST-O2-ALIASING-MSVC: -relaxed-aliasing
1922
// CHECK-OFAST-O2-NOT: -ffast-math
2023
// CHECK-OFAST-O2-NOT: -Ofast
2124
// CHECK-OFAST-O2: -vectorize-loops

clang/test/Driver/clang_f_opts.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -623,3 +623,9 @@
623623
// RUN: %clang -### --target=aarch64-windows-msvc -fno-ms-volatile %s 2>&1 | FileCheck -check-prefix=CHECK-NO-MS-VOLATILE %s
624624
// CHECK-MS-VOLATILE: -fms-volatile
625625
// CHECK-NO-MS-VOLATILE-NOT: -fms-volatile
626+
627+
// RUN: %clang -### --target=x86_64-pc-windows-msvc %s 2>&1 | FileCheck -check-prefix=CHECK-NO-STRICT-ALIASING %s
628+
// RUN: %clang -### --target=x86_64-pc-windows-msvc -fstrict-aliasing %s 2>&1 | FileCheck -check-prefix=CHECK-STRICT-ALIASING %s
629+
// RUN: %clang -### --target=x86_64-pc-windows-msvc -fno-strict-aliasing %s 2>&1 | FileCheck -check-prefix=CHECK-NO-STRICT-ALIASING %s
630+
// CHECK-STRICT-ALIASING-NOT: -relaxed-aliasing
631+
// CHECK-NO-STRICT-ALIASING: -relaxed-aliasing

0 commit comments

Comments
 (0)