Skip to content

Commit 4236431

Browse files
author
badumbatish
committed
Merge branch 'main' of https://github.com/llvm/llvm-project into issue50142
2 parents 8cafed8 + 42578e8 commit 4236431

File tree

465 files changed

+18311
-6327
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

465 files changed

+18311
-6327
lines changed

bolt/include/bolt/Rewrite/RewriteInstance.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,9 @@ class RewriteInstance {
202202
/// Map code sections generated by BOLT.
203203
void mapCodeSections(BOLTLinker::SectionMapper MapSection);
204204

205+
/// Map code without relocating sections.
206+
void mapCodeSectionsInPlace(BOLTLinker::SectionMapper MapSection);
207+
205208
/// Map the rest of allocatable sections.
206209
void mapAllocatableSections(BOLTLinker::SectionMapper MapSection);
207210

bolt/lib/Rewrite/RewriteInstance.cpp

Lines changed: 116 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -3856,136 +3856,138 @@ std::vector<BinarySection *> RewriteInstance::getCodeSections() {
38563856
}
38573857

38583858
void RewriteInstance::mapCodeSections(BOLTLinker::SectionMapper MapSection) {
3859-
if (BC->HasRelocations) {
3860-
// Map sections for functions with pre-assigned addresses.
3861-
for (BinaryFunction *InjectedFunction : BC->getInjectedBinaryFunctions()) {
3862-
const uint64_t OutputAddress = InjectedFunction->getOutputAddress();
3863-
if (!OutputAddress)
3864-
continue;
3865-
3866-
ErrorOr<BinarySection &> FunctionSection =
3867-
InjectedFunction->getCodeSection();
3868-
assert(FunctionSection && "function should have section");
3869-
FunctionSection->setOutputAddress(OutputAddress);
3870-
MapSection(*FunctionSection, OutputAddress);
3871-
InjectedFunction->setImageAddress(FunctionSection->getAllocAddress());
3872-
InjectedFunction->setImageSize(FunctionSection->getOutputSize());
3873-
}
3874-
3875-
// Populate the list of sections to be allocated.
3876-
std::vector<BinarySection *> CodeSections = getCodeSections();
3859+
if (!BC->HasRelocations) {
3860+
mapCodeSectionsInPlace(MapSection);
3861+
return;
3862+
}
38773863

3878-
// Remove sections that were pre-allocated (patch sections).
3879-
llvm::erase_if(CodeSections, [](BinarySection *Section) {
3880-
return Section->getOutputAddress();
3881-
});
3882-
LLVM_DEBUG(dbgs() << "Code sections in the order of output:\n";
3883-
for (const BinarySection *Section : CodeSections)
3884-
dbgs() << Section->getName() << '\n';
3885-
);
3864+
// Map sections for functions with pre-assigned addresses.
3865+
for (BinaryFunction *InjectedFunction : BC->getInjectedBinaryFunctions()) {
3866+
const uint64_t OutputAddress = InjectedFunction->getOutputAddress();
3867+
if (!OutputAddress)
3868+
continue;
38863869

3887-
uint64_t PaddingSize = 0; // size of padding required at the end
3870+
ErrorOr<BinarySection &> FunctionSection =
3871+
InjectedFunction->getCodeSection();
3872+
assert(FunctionSection && "function should have section");
3873+
FunctionSection->setOutputAddress(OutputAddress);
3874+
MapSection(*FunctionSection, OutputAddress);
3875+
InjectedFunction->setImageAddress(FunctionSection->getAllocAddress());
3876+
InjectedFunction->setImageSize(FunctionSection->getOutputSize());
3877+
}
38883878

3889-
// Allocate sections starting at a given Address.
3890-
auto allocateAt = [&](uint64_t Address) {
3891-
const char *LastNonColdSectionName = BC->HasWarmSection
3892-
? BC->getWarmCodeSectionName()
3893-
: BC->getMainCodeSectionName();
3894-
for (BinarySection *Section : CodeSections) {
3895-
Address = alignTo(Address, Section->getAlignment());
3896-
Section->setOutputAddress(Address);
3897-
Address += Section->getOutputSize();
3898-
3899-
// Hugify: Additional huge page from right side due to
3900-
// weird ASLR mapping addresses (4KB aligned)
3901-
if (opts::Hugify && !BC->HasFixedLoadAddress &&
3902-
Section->getName() == LastNonColdSectionName)
3903-
Address = alignTo(Address, Section->getAlignment());
3904-
}
3879+
// Populate the list of sections to be allocated.
3880+
std::vector<BinarySection *> CodeSections = getCodeSections();
39053881

3906-
// Make sure we allocate enough space for huge pages.
3907-
ErrorOr<BinarySection &> TextSection =
3908-
BC->getUniqueSectionByName(LastNonColdSectionName);
3909-
if (opts::HotText && TextSection && TextSection->hasValidSectionID()) {
3910-
uint64_t HotTextEnd =
3911-
TextSection->getOutputAddress() + TextSection->getOutputSize();
3912-
HotTextEnd = alignTo(HotTextEnd, BC->PageAlign);
3913-
if (HotTextEnd > Address) {
3914-
PaddingSize = HotTextEnd - Address;
3915-
Address = HotTextEnd;
3916-
}
3917-
}
3918-
return Address;
3919-
};
3882+
// Remove sections that were pre-allocated (patch sections).
3883+
llvm::erase_if(CodeSections, [](BinarySection *Section) {
3884+
return Section->getOutputAddress();
3885+
});
3886+
LLVM_DEBUG(dbgs() << "Code sections in the order of output:\n";
3887+
for (const BinarySection *Section : CodeSections) dbgs()
3888+
<< Section->getName() << '\n';);
39203889

3921-
// Try to allocate sections before the \p Address and return an address for
3922-
// the allocation of the first section, or 0 if [0, Address) range is not
3923-
// big enough to fit all sections.
3924-
auto allocateBefore = [&](uint64_t Address) -> uint64_t {
3925-
for (BinarySection *Section : llvm::reverse(CodeSections)) {
3926-
if (Section->getOutputSize() > Address)
3927-
return 0;
3928-
Address -= Section->getOutputSize();
3929-
Address = alignDown(Address, Section->getAlignment());
3930-
Section->setOutputAddress(Address);
3931-
}
3932-
return Address;
3933-
};
3890+
uint64_t PaddingSize = 0; // size of padding required at the end
39343891

3935-
// Check if we can fit code in the original .text
3936-
bool AllocationDone = false;
3937-
if (opts::UseOldText) {
3938-
uint64_t StartAddress;
3939-
uint64_t EndAddress;
3940-
if (opts::HotFunctionsAtEnd) {
3941-
EndAddress = BC->OldTextSectionAddress + BC->OldTextSectionSize;
3942-
StartAddress = allocateBefore(EndAddress);
3943-
} else {
3944-
StartAddress = BC->OldTextSectionAddress;
3945-
EndAddress = allocateAt(BC->OldTextSectionAddress);
3946-
}
3892+
// Allocate sections starting at a given Address.
3893+
auto allocateAt = [&](uint64_t Address) {
3894+
const char *LastNonColdSectionName = BC->HasWarmSection
3895+
? BC->getWarmCodeSectionName()
3896+
: BC->getMainCodeSectionName();
3897+
for (BinarySection *Section : CodeSections) {
3898+
Address = alignTo(Address, Section->getAlignment());
3899+
Section->setOutputAddress(Address);
3900+
Address += Section->getOutputSize();
3901+
3902+
// Hugify: Additional huge page from right side due to
3903+
// weird ASLR mapping addresses (4KB aligned)
3904+
if (opts::Hugify && !BC->HasFixedLoadAddress &&
3905+
Section->getName() == LastNonColdSectionName)
3906+
Address = alignTo(Address, Section->getAlignment());
3907+
}
39473908

3948-
const uint64_t CodeSize = EndAddress - StartAddress;
3949-
if (CodeSize <= BC->OldTextSectionSize) {
3950-
BC->outs() << "BOLT-INFO: using original .text for new code with 0x"
3951-
<< Twine::utohexstr(opts::AlignText) << " alignment";
3952-
if (StartAddress != BC->OldTextSectionAddress)
3953-
BC->outs() << " at 0x" << Twine::utohexstr(StartAddress);
3954-
BC->outs() << '\n';
3955-
AllocationDone = true;
3956-
} else {
3957-
BC->errs()
3958-
<< "BOLT-WARNING: original .text too small to fit the new code"
3959-
<< " using 0x" << Twine::utohexstr(opts::AlignText)
3960-
<< " alignment. " << CodeSize << " bytes needed, have "
3961-
<< BC->OldTextSectionSize << " bytes available.\n";
3962-
opts::UseOldText = false;
3909+
// Make sure we allocate enough space for huge pages.
3910+
ErrorOr<BinarySection &> TextSection =
3911+
BC->getUniqueSectionByName(LastNonColdSectionName);
3912+
if (opts::HotText && TextSection && TextSection->hasValidSectionID()) {
3913+
uint64_t HotTextEnd =
3914+
TextSection->getOutputAddress() + TextSection->getOutputSize();
3915+
HotTextEnd = alignTo(HotTextEnd, BC->PageAlign);
3916+
if (HotTextEnd > Address) {
3917+
PaddingSize = HotTextEnd - Address;
3918+
Address = HotTextEnd;
39633919
}
39643920
}
3921+
return Address;
3922+
};
39653923

3966-
if (!AllocationDone)
3967-
NextAvailableAddress = allocateAt(NextAvailableAddress);
3924+
// Try to allocate sections before the \p Address and return an address for
3925+
// the allocation of the first section, or 0 if [0, Address) range is not
3926+
// big enough to fit all sections.
3927+
auto allocateBefore = [&](uint64_t Address) -> uint64_t {
3928+
for (BinarySection *Section : llvm::reverse(CodeSections)) {
3929+
if (Section->getOutputSize() > Address)
3930+
return 0;
3931+
Address -= Section->getOutputSize();
3932+
Address = alignDown(Address, Section->getAlignment());
3933+
Section->setOutputAddress(Address);
3934+
}
3935+
return Address;
3936+
};
39683937

3969-
// Do the mapping for ORC layer based on the allocation.
3970-
for (BinarySection *Section : CodeSections) {
3971-
LLVM_DEBUG(
3972-
dbgs() << "BOLT: mapping " << Section->getName() << " at 0x"
3973-
<< Twine::utohexstr(Section->getAllocAddress()) << " to 0x"
3974-
<< Twine::utohexstr(Section->getOutputAddress()) << '\n');
3975-
MapSection(*Section, Section->getOutputAddress());
3976-
Section->setOutputFileOffset(
3977-
getFileOffsetForAddress(Section->getOutputAddress()));
3938+
// Check if we can fit code in the original .text
3939+
bool AllocationDone = false;
3940+
if (opts::UseOldText) {
3941+
uint64_t StartAddress;
3942+
uint64_t EndAddress;
3943+
if (opts::HotFunctionsAtEnd) {
3944+
EndAddress = BC->OldTextSectionAddress + BC->OldTextSectionSize;
3945+
StartAddress = allocateBefore(EndAddress);
3946+
} else {
3947+
StartAddress = BC->OldTextSectionAddress;
3948+
EndAddress = allocateAt(BC->OldTextSectionAddress);
3949+
}
3950+
3951+
const uint64_t CodeSize = EndAddress - StartAddress;
3952+
if (CodeSize <= BC->OldTextSectionSize) {
3953+
BC->outs() << "BOLT-INFO: using original .text for new code with 0x"
3954+
<< Twine::utohexstr(opts::AlignText) << " alignment";
3955+
if (StartAddress != BC->OldTextSectionAddress)
3956+
BC->outs() << " at 0x" << Twine::utohexstr(StartAddress);
3957+
BC->outs() << '\n';
3958+
AllocationDone = true;
3959+
} else {
3960+
BC->errs() << "BOLT-WARNING: original .text too small to fit the new code"
3961+
<< " using 0x" << Twine::utohexstr(opts::AlignText)
3962+
<< " alignment. " << CodeSize << " bytes needed, have "
3963+
<< BC->OldTextSectionSize << " bytes available.\n";
3964+
opts::UseOldText = false;
39783965
}
3966+
}
39793967

3980-
// Check if we need to insert a padding section for hot text.
3981-
if (PaddingSize && !opts::UseOldText)
3982-
BC->outs() << "BOLT-INFO: padding code to 0x"
3983-
<< Twine::utohexstr(NextAvailableAddress)
3984-
<< " to accommodate hot text\n";
3968+
if (!AllocationDone)
3969+
NextAvailableAddress = allocateAt(NextAvailableAddress);
39853970

3986-
return;
3971+
// Do the mapping for ORC layer based on the allocation.
3972+
for (BinarySection *Section : CodeSections) {
3973+
LLVM_DEBUG(dbgs() << "BOLT: mapping " << Section->getName() << " at 0x"
3974+
<< Twine::utohexstr(Section->getAllocAddress())
3975+
<< " to 0x"
3976+
<< Twine::utohexstr(Section->getOutputAddress()) << '\n');
3977+
MapSection(*Section, Section->getOutputAddress());
3978+
Section->setOutputFileOffset(
3979+
getFileOffsetForAddress(Section->getOutputAddress()));
39873980
}
39883981

3982+
// Check if we need to insert a padding section for hot text.
3983+
if (PaddingSize && !opts::UseOldText)
3984+
BC->outs() << "BOLT-INFO: padding code to 0x"
3985+
<< Twine::utohexstr(NextAvailableAddress)
3986+
<< " to accommodate hot text\n";
3987+
}
3988+
3989+
void RewriteInstance::mapCodeSectionsInPlace(
3990+
BOLTLinker::SectionMapper MapSection) {
39893991
// Processing in non-relocation mode.
39903992
uint64_t NewTextSectionStartAddress = NextAvailableAddress;
39913993

clang-tools-extra/clang-doc/BitcodeReader.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ static llvm::Error decodeRecord(const Record &R, InfoType &Field,
9494
case InfoType::IT_typedef:
9595
case InfoType::IT_concept:
9696
case InfoType::IT_variable:
97+
case InfoType::IT_friend:
9798
Field = IT;
9899
return llvm::Error::success();
99100
}
@@ -111,6 +112,7 @@ static llvm::Error decodeRecord(const Record &R, FieldId &Field,
111112
case FieldId::F_child_namespace:
112113
case FieldId::F_child_record:
113114
case FieldId::F_concept:
115+
case FieldId::F_friend:
114116
case FieldId::F_default:
115117
Field = F;
116118
return llvm::Error::success();
@@ -450,6 +452,15 @@ static llvm::Error parseRecord(const Record &R, unsigned ID,
450452
}
451453
}
452454

455+
static llvm::Error parseRecord(const Record &R, unsigned ID, StringRef Blob,
456+
FriendInfo *F) {
457+
if (ID == FRIEND_IS_CLASS) {
458+
return decodeRecord(R, F->IsClass, Blob);
459+
}
460+
return llvm::createStringError(llvm::inconvertibleErrorCode(),
461+
"invalid field for Friend");
462+
}
463+
453464
template <typename T> static llvm::Expected<CommentInfo *> getCommentInfo(T I) {
454465
return llvm::createStringError(llvm::inconvertibleErrorCode(),
455466
"invalid type cannot contain CommentInfo");
@@ -525,6 +536,18 @@ template <> llvm::Error addTypeInfo(FunctionInfo *I, FieldTypeInfo &&T) {
525536
return llvm::Error::success();
526537
}
527538

539+
template <> llvm::Error addTypeInfo(FriendInfo *I, FieldTypeInfo &&T) {
540+
if (!I->Params)
541+
I->Params.emplace();
542+
I->Params->emplace_back(std::move(T));
543+
return llvm::Error::success();
544+
}
545+
546+
template <> llvm::Error addTypeInfo(FriendInfo *I, TypeInfo &&T) {
547+
I->ReturnType.emplace(std::move(T));
548+
return llvm::Error::success();
549+
}
550+
528551
template <> llvm::Error addTypeInfo(EnumInfo *I, TypeInfo &&T) {
529552
I->BaseType = std::move(T);
530553
return llvm::Error::success();
@@ -667,6 +690,16 @@ llvm::Error addReference(ConstraintInfo *I, Reference &&R, FieldId F) {
667690
"ConstraintInfo cannot contain this Reference");
668691
}
669692

693+
template <>
694+
llvm::Error addReference(FriendInfo *Friend, Reference &&R, FieldId F) {
695+
if (F == FieldId::F_friend) {
696+
Friend->Ref = std::move(R);
697+
return llvm::Error::success();
698+
}
699+
return llvm::createStringError(llvm::inconvertibleErrorCode(),
700+
"Friend cannot contain this Reference");
701+
}
702+
670703
template <typename T, typename ChildInfoType>
671704
static void addChild(T I, ChildInfoType &&R) {
672705
llvm::errs() << "invalid child type for info";
@@ -700,6 +733,9 @@ template <> void addChild(RecordInfo *I, EnumInfo &&R) {
700733
template <> void addChild(RecordInfo *I, TypedefInfo &&R) {
701734
I->Children.Typedefs.emplace_back(std::move(R));
702735
}
736+
template <> void addChild(RecordInfo *I, FriendInfo &&R) {
737+
I->Friends.emplace_back(std::move(R));
738+
}
703739

704740
// Other types of children:
705741
template <> void addChild(EnumInfo *I, EnumValueInfo &&R) {
@@ -741,6 +777,9 @@ template <> void addTemplate(FunctionInfo *I, TemplateInfo &&P) {
741777
template <> void addTemplate(ConceptInfo *I, TemplateInfo &&P) {
742778
I->Template = std::move(P);
743779
}
780+
template <> void addTemplate(FriendInfo *I, TemplateInfo &&P) {
781+
I->Template.emplace(std::move(P));
782+
}
744783

745784
// Template specializations go only into template records.
746785
template <typename T>
@@ -921,6 +960,10 @@ llvm::Error ClangDocBitcodeReader::readSubBlock(unsigned ID, T I) {
921960
case BI_VAR_BLOCK_ID: {
922961
return handleSubBlock<VarInfo>(ID, I, CreateAddFunc(addChild<T, VarInfo>));
923962
}
963+
case BI_FRIEND_BLOCK_ID: {
964+
return handleSubBlock<FriendInfo>(ID, I,
965+
CreateAddFunc(addChild<T, FriendInfo>));
966+
}
924967
default:
925968
return llvm::createStringError(llvm::inconvertibleErrorCode(),
926969
"invalid subblock type");
@@ -1032,6 +1075,8 @@ ClangDocBitcodeReader::readBlockToInfo(unsigned ID) {
10321075
return createInfo<FunctionInfo>(ID);
10331076
case BI_VAR_BLOCK_ID:
10341077
return createInfo<VarInfo>(ID);
1078+
case BI_FRIEND_BLOCK_ID:
1079+
return createInfo<FriendInfo>(ID);
10351080
default:
10361081
return llvm::createStringError(llvm::inconvertibleErrorCode(),
10371082
"cannot create info");
@@ -1072,6 +1117,7 @@ ClangDocBitcodeReader::readBitcode() {
10721117
case BI_TYPEDEF_BLOCK_ID:
10731118
case BI_CONCEPT_BLOCK_ID:
10741119
case BI_VAR_BLOCK_ID:
1120+
case BI_FRIEND_BLOCK_ID:
10751121
case BI_FUNCTION_BLOCK_ID: {
10761122
auto InfoOrErr = readBlockToInfo(ID);
10771123
if (!InfoOrErr)

0 commit comments

Comments
 (0)