Skip to content

Commit 1a7398e

Browse files
committed
[BranchAlign] Add master --x86-branches-within-32B-boundaries flag
This flag was originally part of D70157, but was removed as we carved away pieces of the review. Since we have the nop support checked in, and it appears mature(*), I think it's time to add the master flag. For now, it will default to nop padding, but once the prefix padding support lands, we'll update the defaults. (*) I can now confirm that downstream testing of the changes which have landed to date - nop padding and compiler support for suppressions - is passing all of the functional testing we've thrown at it. There might still be something lurking, but we've gotten enough coverage to be confident of the basic approach. Note that the new flag can be used either when assembling an .s file, or when using the integrated assembler directly from the compiler. The later will use all of the suppression mechanism and should always generate correct code. We don't yet have assembly syntax for the suppressions, so passing this directly to the assembler w/a raw .s file may result in broken code. Use at your own risk. Also note that this isn't the wiring for the clang option. I think the most recent review for that is D72227, but I've lost track, so that might be off. Differential Revision: https://reviews.llvm.org/D72738
1 parent ff1e0fc commit 1a7398e

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

llvm/lib/Target/X86/MCTargetDesc/X86AsmBackend.cpp

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,14 @@ cl::opt<X86AlignBranchKind, true, cl::parser<std::string>> X86AlignBranch(
9494
"indirect indicates indirect jumps."),
9595
cl::___location(X86AlignBranchKindLoc));
9696

97+
cl::opt<bool> X86AlignBranchWithin32BBoundaries(
98+
"x86-branches-within-32B-boundaries", cl::init(false),
99+
cl::desc(
100+
"Align selected instructions to mitigate negative performance impact "
101+
"of Intel's micro code update for errata skx102. May break "
102+
"assumptions about labels corresponding to particular instructions, "
103+
"and should be used with caution."));
104+
97105
class X86ELFObjectWriter : public MCELFObjectTargetWriter {
98106
public:
99107
X86ELFObjectWriter(bool is64Bit, uint8_t OSABI, uint16_t EMachine,
@@ -119,8 +127,21 @@ class X86AsmBackend : public MCAsmBackend {
119127
X86AsmBackend(const Target &T, const MCSubtargetInfo &STI)
120128
: MCAsmBackend(support::little), STI(STI),
121129
MCII(T.createMCInstrInfo()) {
122-
AlignBoundary = assumeAligned(X86AlignBranchBoundary);
123-
AlignBranchType = X86AlignBranchKindLoc;
130+
if (X86AlignBranchWithin32BBoundaries) {
131+
// At the moment, this defaults to aligning fused branches, unconditional
132+
// jumps, and (unfused) conditional jumps with nops. Both the
133+
// instructions aligned and the alignment method (nop vs prefix) may
134+
// change in the future.
135+
AlignBoundary = assumeAligned(32);;
136+
AlignBranchType.addKind(X86::AlignBranchFused);
137+
AlignBranchType.addKind(X86::AlignBranchJcc);
138+
AlignBranchType.addKind(X86::AlignBranchJmp);
139+
}
140+
// Allow overriding defaults set by master flag
141+
if (X86AlignBranchBoundary.getNumOccurrences())
142+
AlignBoundary = assumeAligned(X86AlignBranchBoundary);
143+
if (X86AlignBranch.getNumOccurrences())
144+
AlignBranchType = X86AlignBranchKindLoc;
124145
}
125146

126147
bool allowAutoPadding() const override;

llvm/test/MC/X86/align-branch-64-1a.s

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
# Check only fused conditional jumps, conditional jumps and unconditional jumps are aligned with option --x86-align-branch-boundary=32 --x86-align-branch=fused+jcc+jmp
2-
# RUN: llvm-mc -filetype=obj -triple x86_64-unknown-unknown --x86-align-branch-boundary=32 --x86-align-branch=fused+jcc+jmp %p/Inputs/align-branch-64-1.s | llvm-objdump -d - > %t1
3-
# RUN: FileCheck --input-file=%t1 %s
2+
# RUN: llvm-mc -filetype=obj -triple x86_64-unknown-unknown --x86-align-branch-boundary=32 --x86-align-branch=fused+jcc+jmp %p/Inputs/align-branch-64-1.s -o %t1 && llvm-objdump -d %t1 | FileCheck %s
3+
4+
# Check that -x86-branches-within-32B-boundaries matches the above command line
5+
# RUN: llvm-mc -filetype=obj -triple x86_64-unknown-unknown --x86-branches-within-32B-boundaries %p/Inputs/align-branch-64-1.s -o %t2
6+
# RUN: cmp %t1 %t2
47

58
# Check no branches is aligned with option --x86-align-branch-boundary=0
6-
# RUN: llvm-mc -filetype=obj -triple x86_64-unknown-unknown --x86-align-branch-boundary=0 --x86-align-branch=fused+jcc+jmp %p/Inputs/align-branch-64-1.s | llvm-objdump -d - > %t2
7-
# RUN: llvm-mc -filetype=obj -triple x86_64-unknown-unknown %p/Inputs/align-branch-64-1.s | llvm-objdump -d - > %t3
8-
# RUN: cmp %t2 %t3
9+
# RUN: llvm-mc -filetype=obj -triple x86_64-unknown-unknown --x86-align-branch-boundary=0 --x86-align-branch=fused+jcc+jmp %p/Inputs/align-branch-64-1.s | llvm-objdump -d - > %t3
10+
# RUN: llvm-mc -filetype=obj -triple x86_64-unknown-unknown %p/Inputs/align-branch-64-1.s | llvm-objdump -d - > %t4
11+
# RUN: cmp %t3 %t4
912

1013
# CHECK: 0000000000000000 foo:
1114
# CHECK-COUNT-3: : 64 89 04 25 01 00 00 00 movl %eax, %fs:1

0 commit comments

Comments
 (0)