Skip to content

Commit b8556bd

Browse files
authored
Merge branch 'main' into nvvm-nanosleep-ssa
2 parents c5e9f89 + 9625cf6 commit b8556bd

File tree

8 files changed

+1289
-749
lines changed

8 files changed

+1289
-749
lines changed

bolt/include/bolt/Core/MCPlusBuilder.h

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1371,20 +1371,13 @@ class MCPlusBuilder {
13711371
/// Return true if \p Inst has RestoreState annotation.
13721372
bool hasRestoreState(const MCInst &Inst) const;
13731373

1374-
/// Stores RA Signed annotation on \p Inst.
1375-
void setRASigned(MCInst &Inst) const;
1374+
/// Sets kRASigned or kRAUnsigned annotation on \p Inst.
1375+
/// Fails if \p Inst has either annotation already set.
1376+
void setRAState(MCInst &Inst, bool State) const;
13761377

1377-
/// Return true if \p Inst has Signed RA annotation.
1378-
bool isRASigned(const MCInst &Inst) const;
1379-
1380-
/// Stores RA Unsigned annotation on \p Inst.
1381-
void setRAUnsigned(MCInst &Inst) const;
1382-
1383-
/// Return true if \p Inst has Unsigned RA annotation.
1384-
bool isRAUnsigned(const MCInst &Inst) const;
1385-
1386-
/// Return true if \p Inst doesn't have any annotation related to RA state.
1387-
bool isRAStateUnknown(const MCInst &Inst) const;
1378+
/// Return true if \p Inst has kRASigned annotation, false if it has
1379+
/// kRAUnsigned annotation, and std::nullopt if neither annotation is set.
1380+
std::optional<bool> getRAState(const MCInst &Inst) const;
13881381

13891382
/// Return true if the instruction is a call with an exception handling info.
13901383
virtual bool isInvoke(const MCInst &Inst) const {

bolt/lib/Core/MCPlusBuilder.cpp

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -186,26 +186,21 @@ bool MCPlusBuilder::hasRestoreState(const MCInst &Inst) const {
186186
return hasAnnotation(Inst, MCAnnotation::kRestoreState);
187187
}
188188

189-
void MCPlusBuilder::setRASigned(MCInst &Inst) const {
189+
void MCPlusBuilder::setRAState(MCInst &Inst, bool State) const {
190190
assert(!hasAnnotation(Inst, MCAnnotation::kRASigned));
191-
setAnnotationOpValue(Inst, MCAnnotation::kRASigned, true);
192-
}
193-
194-
bool MCPlusBuilder::isRASigned(const MCInst &Inst) const {
195-
return hasAnnotation(Inst, MCAnnotation::kRASigned);
196-
}
197-
198-
void MCPlusBuilder::setRAUnsigned(MCInst &Inst) const {
199191
assert(!hasAnnotation(Inst, MCAnnotation::kRAUnsigned));
200-
setAnnotationOpValue(Inst, MCAnnotation::kRAUnsigned, true);
192+
if (State)
193+
setAnnotationOpValue(Inst, MCAnnotation::kRASigned, true);
194+
else
195+
setAnnotationOpValue(Inst, MCAnnotation::kRAUnsigned, true);
201196
}
202197

203-
bool MCPlusBuilder::isRAUnsigned(const MCInst &Inst) const {
204-
return hasAnnotation(Inst, MCAnnotation::kRAUnsigned);
205-
}
206-
207-
bool MCPlusBuilder::isRAStateUnknown(const MCInst &Inst) const {
208-
return !(isRAUnsigned(Inst) || isRASigned(Inst));
198+
std::optional<bool> MCPlusBuilder::getRAState(const MCInst &Inst) const {
199+
if (hasAnnotation(Inst, MCAnnotation::kRASigned))
200+
return true;
201+
if (hasAnnotation(Inst, MCAnnotation::kRAUnsigned))
202+
return false;
203+
return std::nullopt;
209204
}
210205

211206
std::optional<MCLandingPad> MCPlusBuilder::getEHInfo(const MCInst &Inst) const {

bolt/lib/Passes/InsertNegateRAStatePass.cpp

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,12 @@ using namespace llvm;
2121
namespace llvm {
2222
namespace bolt {
2323

24+
static bool PassFailed = false;
25+
2426
void InsertNegateRAState::runOnFunction(BinaryFunction &BF) {
27+
if (PassFailed)
28+
return;
29+
2530
BinaryContext &BC = BF.getBinaryContext();
2631

2732
if (BF.getState() == BinaryFunction::State::Empty)
@@ -39,26 +44,31 @@ void InsertNegateRAState::runOnFunction(BinaryFunction &BF) {
3944
for (FunctionFragment &FF : BF.getLayout().fragments()) {
4045
coverFunctionFragmentStart(BF, FF);
4146
bool FirstIter = true;
42-
MCInst PrevInst;
47+
bool PrevRAState = false;
4348
// As this pass runs after function splitting, we should only check
4449
// consecutive instructions inside FunctionFragments.
4550
for (BinaryBasicBlock *BB : FF) {
4651
for (auto It = BB->begin(); It != BB->end(); ++It) {
4752
MCInst &Inst = *It;
4853
if (BC.MIB->isCFI(Inst))
4954
continue;
55+
auto RAState = BC.MIB->getRAState(Inst);
56+
if (!RAState) {
57+
BC.errs() << "BOLT-ERROR: unknown RAState after inferUnknownStates "
58+
<< " in function " << BF.getPrintName() << "\n";
59+
PassFailed = true;
60+
return;
61+
}
5062
if (!FirstIter) {
5163
// Consecutive instructions with different RAState means we need to
5264
// add a OpNegateRAState.
53-
if ((BC.MIB->isRASigned(PrevInst) && BC.MIB->isRAUnsigned(Inst)) ||
54-
(BC.MIB->isRAUnsigned(PrevInst) && BC.MIB->isRASigned(Inst))) {
65+
if (*RAState != PrevRAState)
5566
It = BF.addCFIInstruction(
5667
BB, It, MCCFIInstruction::createNegateRAState(nullptr));
57-
}
5868
} else {
5969
FirstIter = false;
6070
}
61-
PrevInst = *It;
71+
PrevRAState = *RAState;
6272
}
6373
}
6474
}
@@ -81,10 +91,17 @@ void InsertNegateRAState::coverFunctionFragmentStart(BinaryFunction &BF,
8191
});
8292
// If a function is already split in the input, the first FF can also start
8393
// with Signed state. This covers that scenario as well.
84-
if (BC.MIB->isRASigned(*((*FirstNonEmpty)->begin()))) {
85-
BF.addCFIInstruction(*FirstNonEmpty, (*FirstNonEmpty)->begin(),
86-
MCCFIInstruction::createNegateRAState(nullptr));
94+
auto II = (*FirstNonEmpty)->getFirstNonPseudo();
95+
auto RAState = BC.MIB->getRAState(*II);
96+
if (!RAState) {
97+
BC.errs() << "BOLT-ERROR: unknown RAState after inferUnknownStates "
98+
<< " in function " << BF.getPrintName() << "\n";
99+
PassFailed = true;
100+
return;
87101
}
102+
if (*RAState)
103+
BF.addCFIInstruction(*FirstNonEmpty, II,
104+
MCCFIInstruction::createNegateRAState(nullptr));
88105
}
89106

90107
void InsertNegateRAState::inferUnknownStates(BinaryFunction &BF) {
@@ -96,15 +113,21 @@ void InsertNegateRAState::inferUnknownStates(BinaryFunction &BF) {
96113
if (BC.MIB->isCFI(Inst))
97114
continue;
98115

99-
if (!FirstIter && BC.MIB->isRAStateUnknown(Inst)) {
100-
if (BC.MIB->isRASigned(PrevInst) || BC.MIB->isPSignOnLR(PrevInst)) {
101-
BC.MIB->setRASigned(Inst);
102-
} else if (BC.MIB->isRAUnsigned(PrevInst) ||
103-
BC.MIB->isPAuthOnLR(PrevInst)) {
104-
BC.MIB->setRAUnsigned(Inst);
116+
auto RAState = BC.MIB->getRAState(Inst);
117+
if (!FirstIter && !RAState) {
118+
if (BC.MIB->isPSignOnLR(PrevInst))
119+
RAState = true;
120+
else if (BC.MIB->isPAuthOnLR(PrevInst))
121+
RAState = false;
122+
else {
123+
auto PrevRAState = BC.MIB->getRAState(PrevInst);
124+
RAState = PrevRAState ? *PrevRAState : false;
105125
}
126+
BC.MIB->setRAState(Inst, *RAState);
106127
} else {
107128
FirstIter = false;
129+
if (!RAState)
130+
BC.MIB->setRAState(Inst, BF.getInitialRAState());
108131
}
109132
PrevInst = Inst;
110133
}
@@ -135,6 +158,8 @@ Error InsertNegateRAState::runOnFunctions(BinaryContext &BC) {
135158
<< " functions "
136159
<< format("(%.2lf%%).\n", (100.0 * FunctionsModified) /
137160
BC.getBinaryFunctions().size());
161+
if (PassFailed)
162+
return createFatalBOLTError("");
138163
return Error::success();
139164
}
140165

bolt/lib/Passes/MarkRAStates.cpp

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,6 @@ bool MarkRAStates::runOnFunction(BinaryFunction &BF) {
7272
BF.setIgnored();
7373
return false;
7474
}
75-
// The signing instruction itself is unsigned, the next will be
76-
// signed.
77-
BC.MIB->setRAUnsigned(Inst);
7875
} else if (BC.MIB->isPAuthOnLR(Inst)) {
7976
if (!RAState) {
8077
// RA authenticating instructions should only follow signed RA state.
@@ -86,15 +83,10 @@ bool MarkRAStates::runOnFunction(BinaryFunction &BF) {
8683
BF.setIgnored();
8784
return false;
8885
}
89-
// The authenticating instruction itself is signed, but the next will be
90-
// unsigned.
91-
BC.MIB->setRASigned(Inst);
92-
} else if (RAState) {
93-
BC.MIB->setRASigned(Inst);
94-
} else {
95-
BC.MIB->setRAUnsigned(Inst);
9686
}
9787

88+
BC.MIB->setRAState(Inst, RAState);
89+
9890
// Updating RAState. All updates are valid from the next instruction.
9991
// Because the same instruction can have remember and restore, the order
10092
// here is relevant. This is the reason to loop over Annotations instead

clang/lib/AST/ASTImporter.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -696,6 +696,10 @@ namespace clang {
696696
ExpectedStmt VisitCXXFoldExpr(CXXFoldExpr *E);
697697
ExpectedStmt VisitRequiresExpr(RequiresExpr* E);
698698
ExpectedStmt VisitConceptSpecializationExpr(ConceptSpecializationExpr* E);
699+
ExpectedStmt
700+
VisitSubstNonTypeTemplateParmPackExpr(SubstNonTypeTemplateParmPackExpr *E);
701+
ExpectedStmt VisitPseudoObjectExpr(PseudoObjectExpr *E);
702+
ExpectedStmt VisitCXXParenListInitExpr(CXXParenListInitExpr *E);
699703

700704
// Helper for chaining together multiple imports. If an error is detected,
701705
// subsequent imports will return default constructed nodes, so that failure
@@ -9273,6 +9277,50 @@ ASTNodeImporter::VisitConceptSpecializationExpr(ConceptSpecializationExpr *E) {
92739277
const_cast<ImplicitConceptSpecializationDecl *>(CSD), &Satisfaction);
92749278
}
92759279

9280+
ExpectedStmt ASTNodeImporter::VisitSubstNonTypeTemplateParmPackExpr(
9281+
SubstNonTypeTemplateParmPackExpr *E) {
9282+
Error Err = Error::success();
9283+
auto ToType = importChecked(Err, E->getType());
9284+
auto ToPackLoc = importChecked(Err, E->getParameterPackLocation());
9285+
auto ToArgPack = importChecked(Err, E->getArgumentPack());
9286+
auto ToAssociatedDecl = importChecked(Err, E->getAssociatedDecl());
9287+
if (Err)
9288+
return std::move(Err);
9289+
9290+
return new (Importer.getToContext()) SubstNonTypeTemplateParmPackExpr(
9291+
ToType, E->getValueKind(), ToPackLoc, ToArgPack, ToAssociatedDecl,
9292+
E->getIndex(), E->getFinal());
9293+
}
9294+
9295+
ExpectedStmt ASTNodeImporter::VisitPseudoObjectExpr(PseudoObjectExpr *E) {
9296+
SmallVector<Expr *, 4> ToSemantics(E->getNumSemanticExprs());
9297+
if (Error Err = ImportContainerChecked(E->semantics(), ToSemantics))
9298+
return std::move(Err);
9299+
auto ToSyntOrErr = import(E->getSyntacticForm());
9300+
if (!ToSyntOrErr)
9301+
return ToSyntOrErr.takeError();
9302+
return PseudoObjectExpr::Create(Importer.getToContext(), *ToSyntOrErr,
9303+
ToSemantics, E->getResultExprIndex());
9304+
}
9305+
9306+
ExpectedStmt
9307+
ASTNodeImporter::VisitCXXParenListInitExpr(CXXParenListInitExpr *E) {
9308+
Error Err = Error::success();
9309+
auto ToType = importChecked(Err, E->getType());
9310+
auto ToInitLoc = importChecked(Err, E->getInitLoc());
9311+
auto ToBeginLoc = importChecked(Err, E->getBeginLoc());
9312+
auto ToEndLoc = importChecked(Err, E->getEndLoc());
9313+
if (Err)
9314+
return std::move(Err);
9315+
9316+
SmallVector<Expr *, 4> ToArgs(E->getInitExprs().size());
9317+
if (Error Err = ImportContainerChecked(E->getInitExprs(), ToArgs))
9318+
return std::move(Err);
9319+
return CXXParenListInitExpr::Create(Importer.getToContext(), ToArgs, ToType,
9320+
E->getUserSpecifiedInitExprs().size(),
9321+
ToInitLoc, ToBeginLoc, ToEndLoc);
9322+
}
9323+
92769324
Error ASTNodeImporter::ImportOverriddenMethods(CXXMethodDecl *ToMethod,
92779325
CXXMethodDecl *FromMethod) {
92789326
Error ImportErrors = Error::success();

clang/unittests/AST/ASTImporterTest.cpp

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3300,6 +3300,72 @@ TEST_P(ImportExpr, ConceptNestedNonInstantiationDependentRequirement) {
33003300
conceptDecl(has(requiresExpr(has(requiresExprBodyDecl())))));
33013301
}
33023302

3303+
TEST_P(ImportExpr, ImportSubstNonTypeTemplateParmPackExpr) {
3304+
MatchVerifier<Decl> Verifier;
3305+
const char *Code = R"(
3306+
template<auto ...> struct X {};
3307+
template<typename ...> struct Z {};
3308+
3309+
template<int ...N> struct E {
3310+
template<int ...M> using B = Z<X<N, M>...>;
3311+
template<int M1, int M2> E(B<M1, M2>);
3312+
};
3313+
using declToImport = E<1, 3>;
3314+
)";
3315+
testImport(Code, Lang_CXX20, "", Lang_CXX20, Verifier,
3316+
typedefNameDecl(hasName("declToImport")));
3317+
}
3318+
3319+
TEST_P(ImportExpr, ImportCXXParenListInitExpr) {
3320+
MatchVerifier<Decl> Verifier;
3321+
const char *Code = R"(
3322+
struct Node {
3323+
int val;
3324+
double d;
3325+
};
3326+
Node* declToImport() { return new Node(2, 3.14); }
3327+
)";
3328+
testImport(Code, Lang_CXX20, "", Lang_CXX20, Verifier,
3329+
functionDecl(hasName("declToImport")));
3330+
}
3331+
3332+
TEST_P(ImportExpr, ImportPseudoObjectExpr) {
3333+
MatchVerifier<Decl> Verifier;
3334+
const char *Code = R"(
3335+
namespace std {
3336+
struct strong_ordering {
3337+
int n;
3338+
constexpr operator int() const { return n; }
3339+
static const strong_ordering less, equal, greater;
3340+
};
3341+
constexpr strong_ordering strong_ordering::less{-1},
3342+
strong_ordering::equal{0}, strong_ordering::greater{1};
3343+
}
3344+
3345+
struct A {
3346+
std::strong_ordering operator<=>(const A&) const;
3347+
};
3348+
struct B {
3349+
bool operator==(const B&) const;
3350+
bool operator<(const B&) const;
3351+
};
3352+
3353+
template<typename T> struct Cmp : T {
3354+
std::strong_ordering operator<=>(const Cmp&) const = default;
3355+
};
3356+
3357+
void use(...);
3358+
void declToImport() {
3359+
use(
3360+
Cmp<A>() <=> Cmp<A>(),
3361+
Cmp<B>() <=> Cmp<B>()
3362+
);
3363+
}
3364+
)";
3365+
testImport(Code, Lang_CXX20, "", Lang_CXX20, Verifier,
3366+
functionDecl(hasName("declToImport")));
3367+
}
3368+
33033369
class ImportImplicitMethods : public ASTImporterOptionSpecificTestBase {
33043370
public:
33053371
static constexpr auto DefaultCode = R"(

0 commit comments

Comments
 (0)