Skip to content

Commit

Permalink
[Driver] Allow for -O3 on Windows using clang-cl (#12504)
Browse files Browse the repository at this point in the history
We currently support -O3 for Linux compilations, expand this to also be
available on Windows. This also better aligns with our existing product
offerings.
  • Loading branch information
mdtoguchi authored Feb 1, 2024
1 parent f7a360d commit 0af4ac7
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 8 deletions.
3 changes: 3 additions & 0 deletions clang/include/clang/Driver/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -8404,6 +8404,9 @@ def : CLFlag<"O1">, Alias<_SLASH_O>, AliasArgs<["1"]>,
HelpText<"Optimize for size (like /Og /Os /Oy /Ob2 /GF /Gy)">;
def : CLFlag<"O2">, Alias<_SLASH_O>, AliasArgs<["2"]>,
HelpText<"Optimize for speed (like /Og /Oi /Ot /Oy /Ob2 /GF /Gy)">;
def : CLFlag<"O3">, Alias<_SLASH_O>, AliasArgs<["3"]>,
HelpText<"Optimize for maximum speed and enable more aggressive optimizations"
" that may not improve performance on some programs">;
def : CLFlag<"Ob0">, Alias<_SLASH_O>, AliasArgs<["b0"]>,
HelpText<"Disable function inlining">;
def : CLFlag<"Ob1">, Alias<_SLASH_O>, AliasArgs<["b1"]>,
Expand Down
9 changes: 7 additions & 2 deletions clang/lib/Driver/ToolChains/MSVC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -930,6 +930,7 @@ static void TranslateOptArg(Arg *A, llvm::opt::DerivedArgList &DAL,
break;
case '1':
case '2':
case '3':
case 'x':
case 'd':
// Ignore /O[12xd] flags that aren't the last one on the command line.
Expand All @@ -946,11 +947,14 @@ static void TranslateOptArg(Arg *A, llvm::opt::DerivedArgList &DAL,
} else if (OptChar == '2' || OptChar == 'x') {
DAL.AddFlagArg(A, Opts.getOption(options::OPT_fbuiltin));
DAL.AddJoinedArg(A, Opts.getOption(options::OPT_O), "2");
} else if (OptChar == '3') {
DAL.AddFlagArg(A, Opts.getOption(options::OPT_fbuiltin));
DAL.AddJoinedArg(A, Opts.getOption(options::OPT_O), "3");
}
if (SupportsForcingFramePointer &&
!DAL.hasArgNoClaim(options::OPT_fno_omit_frame_pointer))
DAL.AddFlagArg(A, Opts.getOption(options::OPT_fomit_frame_pointer));
if (OptChar == '1' || OptChar == '2')
if (OptChar == '1' || OptChar == '2' || OptChar == '3')
DAL.AddFlagArg(A, Opts.getOption(options::OPT_ffunction_sections));
}
break;
Expand Down Expand Up @@ -1070,7 +1074,8 @@ MSVCToolChain::TranslateArgs(const llvm::opt::DerivedArgList &Args,
// OptChar does not expand; it's an argument to the previous char.
continue;
}
if (OptChar == '1' || OptChar == '2' || OptChar == 'x' || OptChar == 'd')
if (OptChar == '1' || OptChar == '2' || OptChar == 'x' ||
OptChar == 'd' || OptChar == '3')
ExpandChar = OptStr.data() + I;
}
}
Expand Down
12 changes: 6 additions & 6 deletions clang/test/Driver/Xarch.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// RUN: %clang -target i386-apple-darwin11 -m32 -Xarch_i386 -O3 %s -S -### 2>&1 | FileCheck -check-prefix=O3ONCE %s
// O3ONCE: "-O3"
// O3ONCE-NOT: "-O3"
// RUN: %clang -target i386-apple-darwin11 -m32 -Xarch_i386 -O5 %s -S -### 2>&1 | FileCheck -check-prefix=O5ONCE %s
// O5ONCE: "-O5"
// O5ONCE-NOT: "-O5"

// RUN: %clang -target i386-apple-darwin11 -m64 -Xarch_i386 -O3 %s -S -### 2>&1 | FileCheck -check-prefix=O3NONE %s
// O3NONE-NOT: "-O3"
// O3NONE: argument unused during compilation: '-Xarch_i386 -O3'
// RUN: %clang -target i386-apple-darwin11 -m64 -Xarch_i386 -O5 %s -S -### 2>&1 | FileCheck -check-prefix=O5NONE %s
// O5NONE-NOT: "-O5"
// O5NONE: argument unused during compilation: '-Xarch_i386 -O5'

// RUN: not %clang -target i386-apple-darwin11 -m32 -Xarch_i386 -o -Xarch_i386 -S %s -S -Xarch_i386 -o 2>&1 | FileCheck -check-prefix=INVALID %s
// INVALID: error: invalid Xarch argument: '-Xarch_i386 -o'
Expand Down
4 changes: 4 additions & 0 deletions clang/test/Driver/cl-options.c
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,10 @@
// RUN: %clang_cl --target=i686-pc-win32 -Werror -Wno-msvc-not-found /O2 /O2 -### -- %s 2>&1 | FileCheck -check-prefix=O2O2 %s
// O2O2: "-O2"

// RUN: %clang_cl --target=i686-pc-win32 -Werror -Wno-msvc-not-found /O3 -### -- %s 2>&1 | FileCheck -check-prefix=O3 %s
// O3: -mframe-pointer=none
// O3: -O3

// RUN: %clang_cl /Zs -Werror /Oy -- %s 2>&1

// RUN: %clang_cl --target=i686-pc-win32 -Werror -Wno-msvc-not-found /Oy- -### -- %s 2>&1 | FileCheck -check-prefix=Oy_ %s
Expand Down

0 comments on commit 0af4ac7

Please sign in to comment.