Skip to content

Commit 4a976a4

Browse files
committed
[BOLT][NFC] Review nits
- remove unused imports - fix clangd warnings - rename functions - improve comments - reformat lib/Core/Exceptions.cpp
1 parent 855c74d commit 4a976a4

File tree

5 files changed

+23
-30
lines changed

5 files changed

+23
-30
lines changed

bolt/include/bolt/Core/MCPlusBuilder.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class MCPlusBuilder {
7878
if (*FirstAnnotationOp > OpIndex || Inst.getNumOperands() < OpIndex)
7979
return std::nullopt;
8080

81-
auto Op = Inst.begin() + OpIndex;
81+
const auto *Op = Inst.begin() + OpIndex;
8282
const int64_t ImmValue = Op->getImm();
8383
return extractAnnotationIndex(ImmValue);
8484
}

bolt/include/bolt/Passes/InsertNegateRAStatePass.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#define BOLT_PASSES_INSERT_NEGATE_RA_STATE_PASS
1414

1515
#include "bolt/Passes/BinaryPasses.h"
16-
#include <stack>
1716

1817
namespace llvm {
1918
namespace bolt {
@@ -31,14 +30,17 @@ class InsertNegateRAState : public BinaryFunctionPass {
3130
private:
3231
/// Loops over all instructions and adds OpNegateRAState CFI
3332
/// after any pointer signing or authenticating instructions,
34-
/// which operate on the LR, except fused ptrauth + ret instructions
35-
/// (such as RETAA).
33+
/// which operate on the LR, except fused pauth + ret instructions
34+
/// (such as RETAA). Normal pauth and psign instructions are "special cases",
35+
/// meaning they always need an OpNegateRAState CFI after them.
36+
/// Fused pauth + ret instructions are not, they work as any other
37+
/// instruction.
3638
/// Returns true, if any OpNegateRAState CFIs were added.
37-
bool addNegateRAStateAfterPacOrAuth(BinaryFunction &BF);
39+
bool addNegateRAStateAfterPSignOrPAuth(BinaryFunction &BF);
3840
/// Because states are tracked as MCAnnotations on individual instructions,
3941
/// newly inserted instructions do not have a state associated with them.
4042
/// New states are "inherited" from the last known state.
41-
void fixUnknownStates(BinaryFunction &BF);
43+
void inferUnknownStates(BinaryFunction &BF);
4244
};
4345

4446
} // namespace bolt

bolt/lib/Core/Exceptions.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -648,12 +648,12 @@ bool CFIReaderWriter::fillCFIInfoFor(BinaryFunction &Function) const {
648648
// same id but mean different things. The latter is used in AArch64.
649649
if (Function.getBinaryContext().isAArch64()) {
650650
Function.setContainedNegateRAState();
651-
// The location OpNegateRAState CFIs are needed
652-
// depends on the order of BasicBlocks, which changes during
653-
// optimizations. Instead of adding OpNegateRAState CFIs, an annotation
654-
// is added to the instruction, to mark that the instruction modifies
655-
// the RA State. The actual state for instructions are worked out in
656-
// MarkRAStates based on these annotations.
651+
// The location OpNegateRAState CFIs are needed depends on the order of
652+
// BasicBlocks, which changes during optimizations. Instead of adding
653+
// OpNegateRAState CFIs, an annotation is added to the instruction, to
654+
// mark that the instruction modifies the RA State. The actual state for
655+
// instructions are worked out in MarkRAStates based on these
656+
// annotations.
657657
if (Offset != 0)
658658
Function.setInstModifiesRAState(DW_CFA_AARCH64_negate_ra_state,
659659
Offset);

bolt/lib/Passes/InsertNegateRAStatePass.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,7 @@
1414
#include "bolt/Passes/InsertNegateRAStatePass.h"
1515
#include "bolt/Core/BinaryFunction.h"
1616
#include "bolt/Core/ParallelUtilities.h"
17-
#include "bolt/Utils/CommandLineOpts.h"
1817
#include <cstdlib>
19-
#include <fstream>
20-
#include <iterator>
2118

2219
using namespace llvm;
2320

@@ -38,10 +35,10 @@ void InsertNegateRAState::runOnFunction(BinaryFunction &BF) {
3835
}
3936

4037
// If none is inserted, the function doesn't need more work.
41-
if (!addNegateRAStateAfterPacOrAuth(BF))
38+
if (!addNegateRAStateAfterPSignOrPAuth(BF))
4239
return;
4340

44-
fixUnknownStates(BF);
41+
inferUnknownStates(BF);
4542

4643
// Support for function splitting:
4744
// if two consecutive BBs with Signed state are going to end up in different
@@ -53,7 +50,7 @@ void InsertNegateRAState::runOnFunction(BinaryFunction &BF) {
5350
// BOLT can generate empty BBs at function splitting which are only used as
5451
// target labels. We should add the negate-ra-state CFI to the first
5552
// non-empty BB.
56-
auto FirstNonEmpty =
53+
auto *FirstNonEmpty =
5754
std::find_if(FF.begin(), FF.end(), [](BinaryBasicBlock *BB) {
5855
// getFirstNonPseudo returns BB.end() if it does not find any
5956
// Instructions.
@@ -92,7 +89,8 @@ void InsertNegateRAState::runOnFunction(BinaryFunction &BF) {
9289
}
9390
}
9491

95-
bool InsertNegateRAState::addNegateRAStateAfterPacOrAuth(BinaryFunction &BF) {
92+
bool InsertNegateRAState::addNegateRAStateAfterPSignOrPAuth(
93+
BinaryFunction &BF) {
9694
BinaryContext &BC = BF.getBinaryContext();
9795
bool FoundAny = false;
9896
for (BinaryBasicBlock &BB : BF) {
@@ -109,7 +107,7 @@ bool InsertNegateRAState::addNegateRAStateAfterPacOrAuth(BinaryFunction &BF) {
109107
return FoundAny;
110108
}
111109

112-
void InsertNegateRAState::fixUnknownStates(BinaryFunction &BF) {
110+
void InsertNegateRAState::inferUnknownStates(BinaryFunction &BF) {
113111
BinaryContext &BC = BF.getBinaryContext();
114112
bool FirstIter = true;
115113
MCInst PrevInst;

bolt/lib/Passes/MarkRAStates.cpp

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,7 @@
2222
#include "bolt/Passes/MarkRAStates.h"
2323
#include "bolt/Core/BinaryFunction.h"
2424
#include "bolt/Core/ParallelUtilities.h"
25-
#include "bolt/Utils/CommandLineOpts.h"
2625
#include <cstdlib>
27-
#include <fstream>
28-
#include <iterator>
29-
30-
#include <iostream>
3126
#include <optional>
3227
#include <stack>
3328

@@ -49,11 +44,9 @@ void MarkRAStates::runOnFunction(BinaryFunction &BF) {
4944
if ((BC.MIB->isPSignOnLR(Inst) ||
5045
(BC.MIB->isPAuthOnLR(Inst) && !BC.MIB->isPAuthAndRet(Inst))) &&
5146
!BC.MIB->hasNegateRAState(Inst)) {
52-
// no .cfi_negate_ra_state attached to signing or authenticating instr
53-
// means, that this is a function with handwritten assembly, which might
54-
// not respect Clang's conventions (e.g. tailcalls are always
55-
// authenticated, so functions always start with unsigned RAState when
56-
// working with compiler-generated code)
47+
// Not all functions have .cfi_negate_ra_state in them. But if one does,
48+
// we expect psign/pauth instructions to have the hasNegateRAState
49+
// annotation.
5750
BF.setIgnored();
5851
BC.outs() << "BOLT-INFO: inconsistent RAStates in function "
5952
<< BF.getPrintName() << "\n";

0 commit comments

Comments
 (0)