Skip to content
This repository was archived by the owner on Feb 5, 2019. It is now read-only.

Commit bd8c8d7

Browse files
committed
[SLH] Introduce a new pass to do Speculative Load Hardening to mitigate
Spectre variant #1 for x86. There is a lengthy, detailed RFC thread on llvm-dev which discusses the high level issues. High level discussion is probably best there. I've split the design document out of this patch and will land it separately once I update it to reflect the latest edits and updates to the Google doc used in the RFC thread. This patch is really just an initial step. It isn't quite ready for prime time and is only exposed via debugging flags. It has two major limitations currently: 1) It only supports x86-64, and only certain ABIs. Many assumptions are currently hard-coded and need to be factored out of the code here. 2) It doesn't include any options for more fine-grained control, either of which control flow edges are significant or which loads are important to be hardened. 3) The code is still quite rough and the testing lighter than I'd like. However, this is enough for people to begin using. I have had numerous requests from people to be able to experiment with this patch to understand the trade-offs it presents and how to use it. We would also like to encourage work to similar effect in other toolchains. The ARM folks are actively developing a system based on this for AArch64. We hope to merge this with their efforts when both are far enough along. But we also don't want to block making this available on that effort. Many thanks to the *numerous* people who helped along the way here. For this patch in particular, both Eric and Craig did a ton of review to even have confidence in it as an early, rough cut at this functionality. Differential Revision: https://reviews.llvm.org/D44824 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@336990 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 1e086c7 commit bd8c8d7

File tree

7 files changed

+2272
-0
lines changed

7 files changed

+2272
-0
lines changed

include/llvm/CodeGen/MachineBasicBlock.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,11 @@ class MachineBasicBlock
477477
/// probabilities may need to be normalized.
478478
void copySuccessor(MachineBasicBlock *Orig, succ_iterator I);
479479

480+
/// Split the old successor into old plus new and updates the probability
481+
/// info.
482+
void splitSuccessor(MachineBasicBlock *Old, MachineBasicBlock *New,
483+
bool NormalizeSuccProbs = false);
484+
480485
/// Transfers all the successors from MBB to this machine basic block (i.e.,
481486
/// copies all the successors FromMBB and remove all the successors from
482487
/// FromMBB).

lib/CodeGen/MachineBasicBlock.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,25 @@ void MachineBasicBlock::addSuccessorWithoutProb(MachineBasicBlock *Succ) {
659659
Succ->addPredecessor(this);
660660
}
661661

662+
void MachineBasicBlock::splitSuccessor(MachineBasicBlock *Old,
663+
MachineBasicBlock *New,
664+
bool NormalizeSuccProbs) {
665+
succ_iterator OldI = llvm::find(successors(), Old);
666+
assert(OldI != succ_end() && "Old is not a successor of this block!");
667+
assert(llvm::find(successors(), New) == succ_end() &&
668+
"New is already a successor of this block!");
669+
670+
// Add a new successor with equal probability as the original one. Note
671+
// that we directly copy the probability using the iterator rather than
672+
// getting a potentially synthetic probability computed when unknown. This
673+
// preserves the probabilities as-is and then we can renormalize them and
674+
// query them effectively afterward.
675+
addSuccessor(New, Probs.empty() ? BranchProbability::getUnknown()
676+
: *getProbabilityIterator(OldI));
677+
if (NormalizeSuccProbs)
678+
normalizeSuccProbs();
679+
}
680+
662681
void MachineBasicBlock::removeSuccessor(MachineBasicBlock *Succ,
663682
bool NormalizeSuccProbs) {
664683
succ_iterator I = find(Successors, Succ);

lib/Target/X86/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ set(sources
5757
X86RetpolineThunks.cpp
5858
X86SelectionDAGInfo.cpp
5959
X86ShuffleDecodeConstantPool.cpp
60+
X86SpeculativeLoadHardening.cpp
6061
X86Subtarget.cpp
6162
X86TargetMachine.cpp
6263
X86TargetObjectFile.cpp

lib/Target/X86/X86.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ InstructionSelector *createX86InstructionSelector(const X86TargetMachine &TM,
127127

128128
void initializeEvexToVexInstPassPass(PassRegistry &);
129129

130+
FunctionPass *createX86SpeculativeLoadHardeningPass();
131+
130132
} // End llvm namespace
131133

132134
#endif

0 commit comments

Comments
 (0)