Skip to content
This repository has been archived by the owner on Jul 1, 2023. It is now read-only.

Commit

Permalink
[BOLT][NFC] Fix braces usage in the rest of the codebase
Browse files Browse the repository at this point in the history
Summary:
Refactor remaining bolt sources to follow the braces rule for if/else/loop from
[LLVM Coding Standards](https://llvm.org/docs/CodingStandards.html).

(cherry picked from FBD33345885)
  • Loading branch information
aaupov committed Dec 30, 2021
1 parent dca3003 commit cd526a4
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 60 deletions.
3 changes: 1 addition & 2 deletions bolt/lib/RuntimeLibs/RuntimeLibrary.cpp
Expand Up @@ -55,9 +55,8 @@ void RuntimeLibrary::loadLibrary(StringRef LibPath, RuntimeDyld &RTDyld) {
object::Archive Archive(B.get()->getMemBufferRef(), Err);
for (const object::Archive::Child &C : Archive.children(Err)) {
std::unique_ptr<object::Binary> Bin = cantFail(C.getAsBinary());
if (object::ObjectFile *Obj = dyn_cast<object::ObjectFile>(&*Bin)) {
if (object::ObjectFile *Obj = dyn_cast<object::ObjectFile>(&*Bin))
RTDyld.loadObject(*Obj);
}
}
check_error(std::move(Err), B->getBufferIdentifier());
} else if (Magic == file_magic::elf_relocatable ||
Expand Down
6 changes: 2 additions & 4 deletions bolt/lib/Utils/Utils.cpp
Expand Up @@ -51,20 +51,18 @@ void check_error(Error E, Twine Message) {

std::string getEscapedName(const StringRef &Name) {
std::string Output = Name.str();
for (size_t I = 0; I < Output.size(); ++I) {
for (size_t I = 0; I < Output.size(); ++I)
if (Output[I] == ' ' || Output[I] == '\\')
Output.insert(I++, 1, '\\');
}

return Output;
}

std::string getUnescapedName(const StringRef &Name) {
std::string Output = Name.str();
for (size_t I = 0; I < Output.size(); ++I) {
for (size_t I = 0; I < Output.size(); ++I)
if (Output[I] == '\\')
Output.erase(I++, 1);
}

return Output;
}
Expand Down
7 changes: 3 additions & 4 deletions bolt/runtime/common.h
Expand Up @@ -203,9 +203,9 @@ char *intToStr(char *OutBuf, uint64_t Num, uint32_t Base) {
*OutBuf++ = '0';
return OutBuf;
}
while (Ptr != Buf) {
while (Ptr != Buf)
*OutBuf++ = *--Ptr;
}

return OutBuf;
}

Expand Down Expand Up @@ -272,9 +272,8 @@ unsigned long hexToLong(const char *Str, char Terminator = '\0') {
Res += *Str++ - 'a' + 10;
else if ('A' <= *Str && *Str <= 'F')
Res += *Str++ - 'A' + 10;
else {
else
return 0;
}
}
return Res;
}
Expand Down
27 changes: 12 additions & 15 deletions bolt/runtime/instr.cpp
Expand Up @@ -828,14 +828,14 @@ Graph::Graph(BumpPtrAllocator &Alloc, const FunctionDescription &D,
MaxNodes = D.Edges[I].ToNode;
}

for (int I = 0; I < D.NumLeafNodes; ++I) {
for (int I = 0; I < D.NumLeafNodes; ++I)
if (static_cast<int32_t>(D.LeafNodes[I].Node) > MaxNodes)
MaxNodes = D.LeafNodes[I].Node;
}
for (int I = 0; I < D.NumCalls; ++I) {

for (int I = 0; I < D.NumCalls; ++I)
if (static_cast<int32_t>(D.Calls[I].FromNode) > MaxNodes)
MaxNodes = D.Calls[I].FromNode;
}

// No nodes? Nothing to do
if (MaxNodes < 0) {
DEBUG(report("No nodes!\n"));
Expand Down Expand Up @@ -1039,10 +1039,9 @@ struct NodeToCallsMap {
}

~NodeToCallsMap() {
for (int I = NumNodes - 1; I >= 0; --I) {
for (int I = NumNodes - 1; I >= 0; --I)
if (Entries[I].Calls)
Alloc.deallocate(Entries[I].Calls);
}
Alloc.deallocate(Entries);
}
};
Expand Down Expand Up @@ -1090,10 +1089,10 @@ void Graph::computeEdgeFrequencies(const uint64_t *Counters,
});
}
// Add all root nodes to the stack
for (int I = 0; I < NumNodes; ++I) {
for (int I = 0; I < NumNodes; ++I)
if (SpanningTreeNodes[I].NumInEdges == 0)
Stack[StackTop++] = I;
}

// Empty stack?
if (StackTop == 0) {
DEBUG(report("Empty stack!\n"));
Expand Down Expand Up @@ -1139,7 +1138,7 @@ void Graph::computeEdgeFrequencies(const uint64_t *Counters,
const uint32_t Succ = SpanningTreeNodes[Cur].OutEdges[I].Node;
Stack[StackTop++] = Succ;
assert(StackTop <= NumNodes, "stack grew too large");
}
}
continue;
}
Visited[Cur] = S_VISITED;
Expand Down Expand Up @@ -1177,9 +1176,8 @@ void Graph::computeEdgeFrequencies(const uint64_t *Counters,
}

// No parent? Reached a tree root, limit to call frequency updating.
if (SpanningTreeNodes[Cur].NumInEdges == 0) {
if (SpanningTreeNodes[Cur].NumInEdges == 0)
continue;
}

assert(SpanningTreeNodes[Cur].NumInEdges == 1, "must have 1 parent");
const uint32_t Parent = SpanningTreeNodes[Cur].InEdges[0].Node;
Expand Down Expand Up @@ -1234,9 +1232,9 @@ const uint8_t *writeFunctionProfile(int FD, ProfileWriterContext &Ctx,
// Skip funcs we know are cold
#ifndef ENABLE_DEBUG
uint64_t CountersFreq = 0;
for (int I = 0; I < F.NumLeafNodes; ++I) {
for (int I = 0; I < F.NumLeafNodes; ++I)
CountersFreq += bolt_instr_locations[F.LeafNodes[I].Counter];
}

if (CountersFreq == 0) {
for (int I = 0; I < F.NumEdges; ++I) {
const uint32_t C = F.Edges[I].Counter;
Expand Down Expand Up @@ -1442,9 +1440,8 @@ int openProfile() {
extern "C" void __bolt_instr_clear_counters() {
memSet(reinterpret_cast<char *>(__bolt_instr_locations), 0,
__bolt_num_counters * 8);
for (int I = 0; I < __bolt_instr_num_ind_calls; ++I) {
for (int I = 0; I < __bolt_instr_num_ind_calls; ++I)
GlobalIndCallCounters[I].resetCounters();
}
}

/// This is the entry point for profile writing.
Expand Down
58 changes: 23 additions & 35 deletions bolt/tools/merge-fdata/merge-fdata.cpp
Expand Up @@ -82,25 +82,24 @@ static void report_error(Twine Message, StringRef CustomError) {

void mergeProfileHeaders(BinaryProfileHeader &MergedHeader,
const BinaryProfileHeader &Header) {
if (MergedHeader.FileName.empty()) {
if (MergedHeader.FileName.empty())
MergedHeader.FileName = Header.FileName;
}

if (!MergedHeader.FileName.empty() &&
MergedHeader.FileName != Header.FileName) {
MergedHeader.FileName != Header.FileName)
errs() << "WARNING: merging profile from a binary for " << Header.FileName
<< " into a profile for binary " << MergedHeader.FileName << '\n';
}
if (MergedHeader.Id.empty()) {

if (MergedHeader.Id.empty())
MergedHeader.Id = Header.Id;
}
if (!MergedHeader.Id.empty() && (MergedHeader.Id != Header.Id)) {

if (!MergedHeader.Id.empty() && (MergedHeader.Id != Header.Id))
errs() << "WARNING: build-ids in merged profiles do not match\n";
}

// Cannot merge samples profile with LBR profile.
if (!MergedHeader.Flags) {
if (!MergedHeader.Flags)
MergedHeader.Flags = Header.Flags;
}

constexpr auto Mask = llvm::bolt::BinaryFunction::PF_LBR |
llvm::bolt::BinaryFunction::PF_SAMPLE;
if ((MergedHeader.Flags & Mask) != (Header.Flags & Mask)) {
Expand All @@ -110,16 +109,15 @@ void mergeProfileHeaders(BinaryProfileHeader &MergedHeader,
MergedHeader.Flags = MergedHeader.Flags | Header.Flags;

if (!Header.Origin.empty()) {
if (MergedHeader.Origin.empty()) {
if (MergedHeader.Origin.empty())
MergedHeader.Origin = Header.Origin;
} else if (MergedHeader.Origin != Header.Origin) {
else if (MergedHeader.Origin != Header.Origin)
MergedHeader.Origin += "; " + Header.Origin;
}
}

if (MergedHeader.EventNames.empty()) {
if (MergedHeader.EventNames.empty())
MergedHeader.EventNames = Header.EventNames;
}

if (MergedHeader.EventNames != Header.EventNames) {
errs() << "WARNING: merging profiles with different sampling events\n";
MergedHeader.EventNames += "," + Header.EventNames;
Expand Down Expand Up @@ -163,9 +161,8 @@ void mergeBasicBlockProfile(BinaryBasicBlockProfile &MergedBB,
}

// Append the rest of call sites.
for (std::pair<const uint32_t, CallSiteInfo *> CSI : CSByOffset) {
for (std::pair<const uint32_t, CallSiteInfo *> CSI : CSByOffset)
MergedBB.CallSites.emplace_back(std::move(*CSI.second));
}

// Merge successor info.
std::vector<SuccessorInfo *> SIByIndex(BF.NumBasicBlocks);
Expand All @@ -184,11 +181,9 @@ void mergeBasicBlockProfile(BinaryBasicBlockProfile &MergedBB,

SIByIndex[MergedSI.Index] = nullptr;
}
for (SuccessorInfo *SI : SIByIndex) {
if (SI) {
for (SuccessorInfo *SI : SIByIndex)
if (SI)
MergedBB.Successors.emplace_back(std::move(*SI));
}
}
}

void mergeFunctionProfile(BinaryFunctionProfile &MergedBF,
Expand Down Expand Up @@ -224,11 +219,9 @@ void mergeFunctionProfile(BinaryFunctionProfile &MergedBF,
}

// Append blocks unique to BF (i.e. those that are not in MergedBF).
for (BinaryBasicBlockProfile *BB : BlockByIndex) {
if (BB) {
for (BinaryBasicBlockProfile *BB : BlockByIndex)
if (BB)
MergedBF.Blocks.emplace_back(std::move(*BB));
}
}
}

bool isYAML(const StringRef Filename) {
Expand Down Expand Up @@ -258,20 +251,18 @@ void mergeLegacyProfiles(const cl::list<std::string> &Filenames) {
StringRef Buf = MB.get()->getBuffer();
// Check if the string "boltedcollection" is in the first line
if (Buf.startswith("boltedcollection\n")) {
if (!First && !BoltedCollection) {
if (!First && !BoltedCollection)
report_error(
Filename,
"cannot mix profile collected in BOLT and non-BOLT deployments");
}
BoltedCollection = true;
if (!First)
Buf = Buf.drop_front(17);
} else {
if (BoltedCollection) {
if (BoltedCollection)
report_error(
Filename,
"cannot mix profile collected in BOLT and non-BOLT deployments");
}
}

outs() << Buf;
Expand Down Expand Up @@ -378,11 +369,9 @@ int main(int argc, char **argv) {
[](const StringMapEntry<BinaryFunctionProfile> &V) {
// Return total branch count.
uint64_t BranchCount = 0;
for (const BinaryBasicBlockProfile &BI : V.second.Blocks) {
for (const SuccessorInfo &SI : BI.Successors) {
for (const BinaryBasicBlockProfile &BI : V.second.Blocks)
for (const SuccessorInfo &SI : BI.Successors)
BranchCount += SI.Count;
}
}
return std::make_pair(BranchCount, StringRef(V.second.Name));
};

Expand All @@ -396,9 +385,8 @@ int main(int argc, char **argv) {
<< (opts::PrintFunctionList == opts::ST_EXEC_COUNT ? "execution"
: "total branch")
<< " count:\n";
for (std::pair<uint64_t, StringRef> &FI : FunctionList) {
for (std::pair<uint64_t, StringRef> &FI : FunctionList)
errs() << FI.second << " : " << FI.first << '\n';
}
}

return 0;
Expand Down

0 comments on commit cd526a4

Please sign in to comment.