Skip to content

Commit 1837681

Browse files
authored
[Driver] Don't default to -mrelax-all for non-RISCV -O0
Some assembly mnemonics may assemble to instructions of different lengths. The longer form is to support instructions like a long branch. On X86, -mrelax-all enables `MCRelaxAll`, which expands instructions to the long form regardless of whether a short form suffices, while -mno-relax-all only expands instructions when needed. ``` // x86 example void foo(int a) { // -mno-relax-all or gas: short jump (2 bytes) // -mrelax-all: near jump (6 bytes) if (a) bar(); } ``` The -mrelax-all default for non-RISCV -O0 appears to only affect x86 and increases code size without any compile time difference for a stage-2 x86-64 build of lld. ``` -mrelax-all: file size: 60.9MiB VM size: 52.4MiB -mno-relax-all: file size: 58.2MiB VM size: 49.7MiB ``` There is no compile time difference (other than noise) GNU assembler doesn't expand instructions by default. Let's remove the -mrelax-all default. Pull Request: #90013
1 parent ef2ca97 commit 1837681

File tree

2 files changed

+11
-43
lines changed

2 files changed

+11
-43
lines changed

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 10 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -835,46 +835,6 @@ static void addPGOAndCoverageFlags(const ToolChain &TC, Compilation &C,
835835
}
836836
}
837837

838-
/// Check whether the given input tree contains any compilation actions.
839-
static bool ContainsCompileAction(const Action *A) {
840-
if (isa<CompileJobAction>(A) || isa<BackendJobAction>(A))
841-
return true;
842-
843-
return llvm::any_of(A->inputs(), ContainsCompileAction);
844-
}
845-
846-
/// Check if -relax-all should be passed to the internal assembler.
847-
/// This is done by default when compiling non-assembler source with -O0.
848-
static bool UseRelaxAll(Compilation &C, const ArgList &Args) {
849-
bool RelaxDefault = true;
850-
851-
if (Arg *A = Args.getLastArg(options::OPT_O_Group))
852-
RelaxDefault = A->getOption().matches(options::OPT_O0);
853-
854-
// RISC-V requires an indirect jump for offsets larger than 1MiB. This cannot
855-
// be done by assembler branch relaxation as it needs a free temporary
856-
// register. Because of this, branch relaxation is handled by a MachineIR
857-
// pass before the assembler. Forcing assembler branch relaxation for -O0
858-
// makes the MachineIR branch relaxation inaccurate and it will miss cases
859-
// where an indirect branch is necessary. To avoid this issue we are
860-
// sacrificing the compile time improvement of using -mrelax-all for -O0.
861-
if (C.getDefaultToolChain().getTriple().isRISCV())
862-
RelaxDefault = false;
863-
864-
if (RelaxDefault) {
865-
RelaxDefault = false;
866-
for (const auto &Act : C.getActions()) {
867-
if (ContainsCompileAction(Act)) {
868-
RelaxDefault = true;
869-
break;
870-
}
871-
}
872-
}
873-
874-
return Args.hasFlag(options::OPT_mrelax_all, options::OPT_mno_relax_all,
875-
RelaxDefault);
876-
}
877-
878838
static void
879839
RenderDebugEnablingArgs(const ArgList &Args, ArgStringList &CmdArgs,
880840
llvm::codegenoptions::DebugInfoKind DebugInfoKind,
@@ -2472,8 +2432,16 @@ static void CollectArgsForIntegratedAssembler(Compilation &C,
24722432
const ArgList &Args,
24732433
ArgStringList &CmdArgs,
24742434
const Driver &D) {
2475-
if (UseRelaxAll(C, Args))
2476-
CmdArgs.push_back("-mrelax-all");
2435+
// Default to -mno-relax-all.
2436+
//
2437+
// Note: RISC-V requires an indirect jump for offsets larger than 1MiB. This
2438+
// cannot be done by assembler branch relaxation as it needs a free temporary
2439+
// register. Because of this, branch relaxation is handled by a MachineIR pass
2440+
// before the assembler. Forcing assembler branch relaxation for -O0 makes the
2441+
// MachineIR branch relaxation inaccurate and it will miss cases where an
2442+
// indirect branch is necessary.
2443+
Args.addOptInFlag(CmdArgs, options::OPT_mrelax_all,
2444+
options::OPT_mno_relax_all);
24772445

24782446
// Only default to -mincremental-linker-compatible if we think we are
24792447
// targeting the MSVC linker.

clang/test/Driver/integrated-as.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// RUN: %clang -### -c -save-temps -integrated-as --target=x86_64 %s 2>&1 | FileCheck %s
44

55
// CHECK: cc1as
6-
// CHECK: -mrelax-all
6+
// CHECK-NOT: -mrelax-all
77

88
// RISC-V does not enable -mrelax-all
99
// RUN: %clang -### -c -save-temps -integrated-as --target=riscv64 %s 2>&1 | FileCheck %s -check-prefix=RISCV-RELAX

0 commit comments

Comments
 (0)