Skip to content

Commit

Permalink
[AArch64] Add workaround for Cortex-A53 erratum (835769)
Browse files Browse the repository at this point in the history
Some early revisions of the Cortex-A53 have an erratum (835769) whereby it is
possible for a 64-bit multiply-accumulate instruction in AArch64 state to
generate an incorrect result.  The details are quite complex and hard to
determine statically, since branches in the code may exist in some
 circumstances, but all cases end with a memory (load, store, or prefetch)
instruction followed immediately by the multiply-accumulate operation.

The safest work-around for this issue is to make the compiler avoid emitting
multiply-accumulate instructions immediately after memory instructions and the
simplest way to do this is to insert a NOP.

This patch implements such work-around in the backend, enabled via the option
-aarch64-fix-cortex-a53-835769.

The work-around code generation is not enabled by default.



git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@219603 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
brads55 committed Oct 13, 2014
1 parent eba0a96 commit 7e67a4b
Show file tree
Hide file tree
Showing 5 changed files with 763 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/Target/AArch64/AArch64.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ ModulePass *createAArch64PromoteConstantPass();
FunctionPass *createAArch64ConditionOptimizerPass();
FunctionPass *createAArch64AddressTypePromotionPass();
FunctionPass *createAArch64A57FPLoadBalancing();
FunctionPass *createAArch64A53Fix835769();
/// \brief Creates an ARM-specific Target Transformation Info pass.
ImmutablePass *
createAArch64TargetTransformInfoPass(const AArch64TargetMachine *TM);
Expand Down
229 changes: 229 additions & 0 deletions lib/Target/AArch64/AArch64A53Fix835769.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
//===-- AArch64A53Fix835769.cpp -------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// This pass changes code to work around Cortex-A53 erratum 835769.
// It works around it by inserting a nop instruction in code sequences that
// in some circumstances may trigger the erratum.
// It inserts a nop instruction between a sequence of the following 2 classes
// of instructions:
// instr 1: mem-instr (including loads, stores and prefetches).
// instr 2: non-SIMD integer multiply-accumulate writing 64-bit X registers.
//===----------------------------------------------------------------------===//

#include "AArch64.h"
#include "AArch64InstrInfo.h"
#include "AArch64Subtarget.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/CodeGen/MachineFunction.h"
#include "llvm/CodeGen/MachineFunctionPass.h"
#include "llvm/CodeGen/MachineInstr.h"
#include "llvm/CodeGen/MachineInstrBuilder.h"
#include "llvm/CodeGen/MachineRegisterInfo.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Debug.h"

using namespace llvm;

#define DEBUG_TYPE "aarch64-fix-cortex-a53-835769"

STATISTIC(NumNopsAdded, "Number of Nops added to work around erratum 835769");

//===----------------------------------------------------------------------===//
// Helper functions

// Is the instruction a match for the instruction that comes first in the
// sequence of instructions that can trigger the erratum?
static bool isFirstInstructionInSequence(MachineInstr *MI) {
// Must return true if this instruction is a load, a store or a prefetch.
switch (MI->getOpcode()) {
case AArch64::PRFMl:
case AArch64::PRFMroW:
case AArch64::PRFMroX:
case AArch64::PRFMui:
case AArch64::PRFUMi:
return true;
default:
return (MI->mayLoad() || MI->mayStore());
}
}

// Is the instruction a match for the instruction that comes second in the
// sequence that can trigger the erratum?
static bool isSecondInstructionInSequence(MachineInstr *MI) {
// Must return true for non-SIMD integer multiply-accumulates, writing
// to a 64-bit register.
switch (MI->getOpcode()) {
// Erratum cannot be triggered when the destination register is 32 bits,
// therefore only include the following.
case AArch64::MSUBXrrr:
case AArch64::MADDXrrr:
case AArch64::SMADDLrrr:
case AArch64::SMSUBLrrr:
case AArch64::UMADDLrrr:
case AArch64::UMSUBLrrr:
// Erratum can only be triggered by multiply-adds, not by regular
// non-accumulating multiplies, i.e. when Ra=XZR='11111'
return MI->getOperand(3).getReg() != AArch64::XZR;
default:
return false;
}
}


//===----------------------------------------------------------------------===//

namespace {
class AArch64A53Fix835769 : public MachineFunctionPass {
const AArch64InstrInfo *TII;

public:
static char ID;
explicit AArch64A53Fix835769() : MachineFunctionPass(ID) {}

bool runOnMachineFunction(MachineFunction &F) override;

const char *getPassName() const override {
return "Workaround A53 erratum 835769 pass";
}

void getAnalysisUsage(AnalysisUsage &AU) const override {
AU.setPreservesCFG();
MachineFunctionPass::getAnalysisUsage(AU);
}

private:
bool runOnBasicBlock(MachineBasicBlock &MBB);
};
char AArch64A53Fix835769::ID = 0;

} // end anonymous namespace

//===----------------------------------------------------------------------===//

bool
AArch64A53Fix835769::runOnMachineFunction(MachineFunction &F) {
const TargetMachine &TM = F.getTarget();

bool Changed = false;
DEBUG(dbgs() << "***** AArch64A53Fix835769 *****\n");

TII = TM.getSubtarget<AArch64Subtarget>().getInstrInfo();

for (auto &MBB : F) {
Changed |= runOnBasicBlock(MBB);
}

return Changed;
}

// Return the block that was fallen through to get to MBB, if any,
// otherwise nullptr.
static MachineBasicBlock *getBBFallenThrough(MachineBasicBlock &MBB,
const TargetInstrInfo *TII) {
// Get the previous machine basic block in the function.
MachineFunction::iterator MBBI = MBB;

// Can't go off top of function.
if (MBBI == MBB.getParent()->begin())
return nullptr;

MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
SmallVector<MachineOperand, 2> Cond;

MachineBasicBlock *PrevBB = std::prev(MBBI);
for (MachineBasicBlock *S : MBB.predecessors())
if (S == PrevBB && !TII->AnalyzeBranch(*PrevBB, TBB, FBB, Cond) &&
!TBB && !FBB)
return S;

return nullptr;
}

static MachineInstr *getLastNonPseudo(MachineBasicBlock *MBB) {
for (auto I = MBB->rbegin(), E = MBB->rend(); I != E; ++I) {
if (!I->isPseudo())
return &*I;
}

llvm_unreachable("Expected to find a non-pseudo instruction");
}

static void insertNopBeforeInstruction(MachineBasicBlock &MBB, MachineInstr* MI,
const TargetInstrInfo *TII) {
// If we are the first instruction of the block, put the NOP at the end of
// the previous fallthrough block
if (MI == &MBB.front()) {
MachineBasicBlock *PMBB = getBBFallenThrough(MBB, TII);
assert(PMBB && "Expected basic block");
MachineInstr *I = getLastNonPseudo(PMBB);
assert(I && "Expected instruction");
DebugLoc DL = I->getDebugLoc();
BuildMI(PMBB, DL, TII->get(AArch64::HINT)).addImm(0);
}
else {
DebugLoc DL = MI->getDebugLoc();
BuildMI(MBB, MI, DL, TII->get(AArch64::HINT)).addImm(0);
}

++NumNopsAdded;
}

bool
AArch64A53Fix835769::runOnBasicBlock(MachineBasicBlock &MBB) {
bool Changed = false;
DEBUG(dbgs() << "Running on MBB: " << MBB << " - scanning instructions...\n");

// First, scan the basic block, looking for a sequence of 2 instructions
// that match the conditions under which the erratum may trigger.

// List of terminating instructions in matching sequences
std::vector<MachineInstr*> Sequences;
unsigned Idx = 0;
MachineInstr *PrevInstr = nullptr;

if (MachineBasicBlock *PMBB = getBBFallenThrough(MBB, TII))
PrevInstr = getLastNonPseudo(PMBB);

for (auto &MI : MBB) {
MachineInstr *CurrInstr = &MI;
DEBUG(dbgs() << " Examining: " << MI);
if (PrevInstr) {
DEBUG(dbgs() << " PrevInstr: " << *PrevInstr
<< " CurrInstr: " << *CurrInstr
<< " isFirstInstructionInSequence(PrevInstr): "
<< isFirstInstructionInSequence(PrevInstr) << "\n"
<< " isSecondInstructionInSequence(CurrInstr): "
<< isSecondInstructionInSequence(CurrInstr) << "\n");
if (isFirstInstructionInSequence(PrevInstr) &&
isSecondInstructionInSequence(CurrInstr)) {
DEBUG(dbgs() << " ** pattern found at Idx " << Idx << "!\n");
Sequences.push_back(CurrInstr);
}
}
if (!CurrInstr->isPseudo())
PrevInstr = CurrInstr;
++Idx;
}

DEBUG(dbgs() << "Scan complete, "<< Sequences.size()
<< " occurences of pattern found.\n");

// Then update the basic block, inserting nops between the detected sequences.
for (auto &MI : Sequences) {
Changed = true;
insertNopBeforeInstruction(MBB, MI, TII);
}

return Changed;
}

// Factory function used by AArch64TargetMachine to add the pass to
// the passmanager.
FunctionPass *llvm::createAArch64A53Fix835769() {
return new AArch64A53Fix835769();
}
7 changes: 7 additions & 0 deletions lib/Target/AArch64/AArch64TargetMachine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ EnablePBQP("aarch64-pbqp", cl::Hidden,
cl::desc("Use PBQP register allocator (experimental)"),
cl::init(false));

static cl::opt<bool>
EnableA53Fix835769("aarch64-fix-cortex-a53-835769", cl::Hidden,
cl::desc("Work around Cortex-A53 erratum 835769"),
cl::init(false));

extern "C" void LLVMInitializeAArch64Target() {
// Register the target.
RegisterTargetMachine<AArch64leTargetMachine> X(TheAArch64leTarget);
Expand Down Expand Up @@ -274,6 +279,8 @@ bool AArch64PassConfig::addPreSched2() {
}

bool AArch64PassConfig::addPreEmitPass() {
if (EnableA53Fix835769)
addPass(createAArch64A53Fix835769());
// Relax conditional branch instructions if they're otherwise out of
// range of their destination.
addPass(createAArch64BranchRelaxation());
Expand Down
1 change: 1 addition & 0 deletions lib/Target/AArch64/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ add_llvm_target(AArch64CodeGen
AArch64DeadRegisterDefinitionsPass.cpp
AArch64ExpandPseudoInsts.cpp
AArch64FastISel.cpp
AArch64A53Fix835769.cpp
AArch64FrameLowering.cpp
AArch64ConditionOptimizer.cpp
AArch64ISelDAGToDAG.cpp
Expand Down
Loading

0 comments on commit 7e67a4b

Please sign in to comment.