Skip to content

Commit

Permalink
[AArch64] Use Feature for A53 Erratum 835769 Fix
Browse files Browse the repository at this point in the history
When this pass was originally implemented, the fix pass was enabled
using a llvm command-line flag. This works fine, except in the case of
LTO, where the flag is not passed into the linker plugin in order to
enable the function pass in the LTO backend.

Now LTO exists, the expectation now is to use target features rather
than command-line arguments to control code generation, as this ensures
that different command-line arguments in different files are correctly
represented, and target-features always get to the LTO plugin as they
are encoded into LLVM IR.

The fall-out of this change is that the fix pass has to always be added
to the backend pass pipeline, so now it makes no changes if the function
does not have the right target feature to enable it. This should make a
minimal difference to compile time.

One advantage is it's now much easier to enable when compiling for a
Cortex-A53, as CPUs imply their own individual sets of target-features,
in a more fine-grained way. I haven't done this yet, but it is an
option, if the fix should be enabled in more places.

Existing tests of the user interface are unaffected, the changes are to
reflect that the argument is now turned into a target feature.

Reviewed By: tmatheson

Differential Revision: https://reviews.llvm.org/D114703
  • Loading branch information
lenary committed Dec 10, 2021
1 parent 28d3976 commit 52faad8
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 26 deletions.
11 changes: 11 additions & 0 deletions clang/lib/Driver/ToolChains/Arch/AArch64.cpp
Expand Up @@ -568,4 +568,15 @@ void aarch64::getAArch64TargetFeatures(const Driver &D,

if (Args.hasArg(options::OPT_mno_neg_immediates))
Features.push_back("+no-neg-immediates");

if (Arg *A = Args.getLastArg(options::OPT_mfix_cortex_a53_835769,
options::OPT_mno_fix_cortex_a53_835769)) {
if (A->getOption().matches(options::OPT_mfix_cortex_a53_835769))
Features.push_back("+fix-cortex-a53-835769");
else
Features.push_back("-fix-cortex-a53-835769");
} else if (Triple.isAndroid()) {
// Enabled A53 errata (835769) workaround by default on android
Features.push_back("+fix-cortex-a53-835769");
}
}
13 changes: 0 additions & 13 deletions clang/lib/Driver/ToolChains/Clang.cpp
Expand Up @@ -1806,19 +1806,6 @@ void Clang::AddAArch64TargetArgs(const ArgList &Args,

RenderAArch64ABI(Triple, Args, CmdArgs);

if (Arg *A = Args.getLastArg(options::OPT_mfix_cortex_a53_835769,
options::OPT_mno_fix_cortex_a53_835769)) {
CmdArgs.push_back("-mllvm");
if (A->getOption().matches(options::OPT_mfix_cortex_a53_835769))
CmdArgs.push_back("-aarch64-fix-cortex-a53-835769=1");
else
CmdArgs.push_back("-aarch64-fix-cortex-a53-835769=0");
} else if (Triple.isAndroid()) {
// Enabled A53 errata (835769) workaround by default on android
CmdArgs.push_back("-mllvm");
CmdArgs.push_back("-aarch64-fix-cortex-a53-835769=1");
}

// Forward the -mglobal-merge option for explicit control over the pass.
if (Arg *A = Args.getLastArg(options::OPT_mglobal_merge,
options::OPT_mno_global_merge)) {
Expand Down
6 changes: 3 additions & 3 deletions clang/test/Driver/aarch64-fix-cortex-a53-835769.c
Expand Up @@ -8,6 +8,6 @@
// RUN: %clang -target aarch64-android-eabi %s -### 2>&1 \
// RUN: | FileCheck --check-prefix=CHECK-YES %s

// CHECK-DEF-NOT: "-mllvm" "-aarch64-fix-cortex-a53-835769"
// CHECK-YES: "-mllvm" "-aarch64-fix-cortex-a53-835769=1"
// CHECK-NO: "-mllvm" "-aarch64-fix-cortex-a53-835769=0"
// CHECK-DEF-NOT: "{[+-]}fix-cortex-a53-835769"
// CHECK-YES: "+fix-cortex-a53-835769"
// CHECK-NO: "-fix-cortex-a53-835769"
3 changes: 3 additions & 0 deletions llvm/lib/Target/AArch64/AArch64.td
Expand Up @@ -455,6 +455,9 @@ def FeatureEL2VMSA : SubtargetFeature<"el2vmsa", "HasEL2VMSA", "true",
def FeatureEL3 : SubtargetFeature<"el3", "HasEL3", "true",
"Enable Exception Level 3">;

def FeatureFixCortexA53_835769 : SubtargetFeature<"fix-cortex-a53-835769",
"FixCortexA53_835769", "true", "Mitigate Cortex-A53 Erratum 835769">;

//===----------------------------------------------------------------------===//
// Architectures.
//
Expand Down
8 changes: 7 additions & 1 deletion llvm/lib/Target/AArch64/AArch64A53Fix835769.cpp
Expand Up @@ -15,6 +15,7 @@
//===----------------------------------------------------------------------===//

#include "AArch64.h"
#include "AArch64Subtarget.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/CodeGen/MachineFunction.h"
#include "llvm/CodeGen/MachineFunctionPass.h"
Expand Down Expand Up @@ -116,8 +117,13 @@ INITIALIZE_PASS(AArch64A53Fix835769, "aarch64-fix-cortex-a53-835769-pass",
bool
AArch64A53Fix835769::runOnMachineFunction(MachineFunction &F) {
LLVM_DEBUG(dbgs() << "***** AArch64A53Fix835769 *****\n");
auto &STI = F.getSubtarget<AArch64Subtarget>();
// Fix not requested, skip pass.
if (!STI.fixCortexA53_835769())
return false;

bool Changed = false;
TII = F.getSubtarget().getInstrInfo();
TII = STI.getInstrInfo();

for (auto &MBB : F) {
Changed |= runOnBasicBlock(MBB);
Expand Down
4 changes: 4 additions & 0 deletions llvm/lib/Target/AArch64/AArch64Subtarget.h
Expand Up @@ -116,6 +116,8 @@ class AArch64Subtarget final : public AArch64GenSubtargetInfo {
bool HasFP16FML = false;
bool HasSPE = false;

bool FixCortexA53_835769 = false;

// ARMv8.1 extensions
bool HasVH = false;
bool HasPAN = false;
Expand Down Expand Up @@ -571,6 +573,8 @@ class AArch64Subtarget final : public AArch64GenSubtargetInfo {
bool hasEL2VMSA() const { return HasEL2VMSA; }
bool hasEL3() const { return HasEL3; }

bool fixCortexA53_835769() const { return FixCortexA53_835769; }

bool addrSinkUsingGEPs() const override {
// Keeping GEPs inbounds is important for exploiting AArch64
// addressing-modes in ILP32 mode.
Expand Down
8 changes: 1 addition & 7 deletions llvm/lib/Target/AArch64/AArch64TargetMachine.cpp
Expand Up @@ -116,11 +116,6 @@ static cl::opt<bool>
cl::desc("Enable the condition optimizer pass"),
cl::init(true), cl::Hidden);

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

static cl::opt<bool>
EnableGEPOpt("aarch64-enable-gep-opt", cl::Hidden,
cl::desc("Enable optimizations on complex GEPs"),
Expand Down Expand Up @@ -764,8 +759,7 @@ void AArch64PassConfig::addPreEmitPass() {
if (TM->getOptLevel() >= CodeGenOpt::Aggressive && EnableLoadStoreOpt)
addPass(createAArch64LoadStoreOptimizationPass());

if (EnableA53Fix835769)
addPass(createAArch64A53Fix835769());
addPass(createAArch64A53Fix835769());

if (EnableBranchTargets)
addPass(createAArch64BranchTargetsPass());
Expand Down
1 change: 1 addition & 0 deletions llvm/test/CodeGen/AArch64/O0-pipeline.ll
Expand Up @@ -60,6 +60,7 @@
; CHECK-NEXT: Insert fentry calls
; CHECK-NEXT: Insert XRay ops
; CHECK-NEXT: Implement the 'patchable-function' attribute
; CHECK-NEXT: Workaround A53 erratum 835769 pass
; CHECK-NEXT: AArch64 Branch Targets
; CHECK-NEXT: Branch relaxation pass
; CHECK-NEXT: Contiguously Lay Out Funclets
Expand Down
1 change: 1 addition & 0 deletions llvm/test/CodeGen/AArch64/O3-pipeline.ll
Expand Up @@ -195,6 +195,7 @@
; CHECK-NEXT: Insert XRay ops
; CHECK-NEXT: Implement the 'patchable-function' attribute
; CHECK-NEXT: AArch64 load / store optimization pass
; CHECK-NEXT: Workaround A53 erratum 835769 pass
; CHECK-NEXT: AArch64 Branch Targets
; CHECK-NEXT: Branch relaxation pass
; CHECK-NEXT: AArch64 Compress Jump Tables
Expand Down
4 changes: 2 additions & 2 deletions llvm/test/CodeGen/AArch64/aarch64-fix-cortex-a53-835769.ll
Expand Up @@ -3,9 +3,9 @@
; therefore, the tests are a bit fragile/reliant on instruction scheduling. The
; test cases have been minimized as much as possible, but still most of the test
; cases could break if instruction scheduling heuristics for cortex-a53 change
; RUN: llc < %s -mcpu=cortex-a53 -aarch64-fix-cortex-a53-835769=1 -frame-pointer=non-leaf -stats 2>&1 \
; RUN: llc < %s -mcpu=cortex-a53 -mattr=+fix-cortex-a53-835769 -frame-pointer=non-leaf -stats 2>&1 \
; RUN: | FileCheck %s
; RUN: llc < %s -mcpu=cortex-a53 -aarch64-fix-cortex-a53-835769=0 -frame-pointer=non-leaf -stats 2>&1 \
; RUN: llc < %s -mcpu=cortex-a53 -mattr=-fix-cortex-a53-835769 -frame-pointer=non-leaf -stats 2>&1 \
; RUN: | FileCheck %s --check-prefix CHECK-NOWORKAROUND
; The following run lines are just to verify whether or not this pass runs by
; default for given CPUs. Given the fragility of the tests, this is only run on
Expand Down

0 comments on commit 52faad8

Please sign in to comment.