Skip to content

Commit 8ac93c2

Browse files
SC llvm teamSC llvm team
authored andcommitted
Merged main:8b9a6af4504a into amd-gfx:30878b6d9320
Local branch amd-gfx 30878b6 Merged main:fc19424d1d6d into amd-gfx:a5dbf9439804 Remote branch main 8b9a6af [OpenMP] Add an stddef.h include to omp.h (llvm#73876)
2 parents 30878b6 + 8b9a6af commit 8ac93c2

File tree

39 files changed

+667
-425
lines changed

39 files changed

+667
-425
lines changed

bolt/lib/Core/BinaryFunction.cpp

Lines changed: 43 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3296,7 +3296,7 @@ void BinaryFunction::fixBranches() {
32963296
BB->eraseInstruction(BB->findInstruction(UncondBranch));
32973297

32983298
// Basic block that follows the current one in the final layout.
3299-
const BinaryBasicBlock *NextBB =
3299+
const BinaryBasicBlock *const NextBB =
33003300
Layout.getBasicBlockAfter(BB, /*IgnoreSplits=*/false);
33013301

33023302
if (BB->succ_size() == 1) {
@@ -3313,39 +3313,54 @@ void BinaryFunction::fixBranches() {
33133313
assert(CondBranch && "conditional branch expected");
33143314
const BinaryBasicBlock *TSuccessor = BB->getConditionalSuccessor(true);
33153315
const BinaryBasicBlock *FSuccessor = BB->getConditionalSuccessor(false);
3316-
// Check whether we support reversing this branch direction
3317-
const bool IsSupported = !MIB->isUnsupportedBranch(*CondBranch);
3318-
if (NextBB && NextBB == TSuccessor && IsSupported) {
3316+
3317+
// Eliminate unnecessary conditional branch.
3318+
if (TSuccessor == FSuccessor) {
3319+
BB->removeDuplicateConditionalSuccessor(CondBranch);
3320+
if (TSuccessor != NextBB)
3321+
BB->addBranchInstruction(TSuccessor);
3322+
continue;
3323+
}
3324+
3325+
// Reverse branch condition and swap successors.
3326+
auto swapSuccessors = [&]() {
3327+
if (MIB->isUnsupportedBranch(*CondBranch))
3328+
return false;
33193329
std::swap(TSuccessor, FSuccessor);
3320-
{
3321-
auto L = BC.scopeLock();
3322-
MIB->reverseBranchCondition(*CondBranch, TSuccessor->getLabel(), Ctx);
3323-
}
33243330
BB->swapConditionalSuccessors();
3325-
} else {
3331+
auto L = BC.scopeLock();
3332+
MIB->reverseBranchCondition(*CondBranch, TSuccessor->getLabel(), Ctx);
3333+
return true;
3334+
};
3335+
3336+
// Check whether the next block is a "taken" target and try to swap it
3337+
// with a "fall-through" target.
3338+
if (TSuccessor == NextBB && swapSuccessors())
3339+
continue;
3340+
3341+
// Update conditional branch destination if needed.
3342+
if (MIB->getTargetSymbol(*CondBranch) != TSuccessor->getLabel()) {
33263343
auto L = BC.scopeLock();
33273344
MIB->replaceBranchTarget(*CondBranch, TSuccessor->getLabel(), Ctx);
33283345
}
3329-
if (TSuccessor == FSuccessor)
3330-
BB->removeDuplicateConditionalSuccessor(CondBranch);
3331-
if (!NextBB ||
3332-
((NextBB != TSuccessor || !IsSupported) && NextBB != FSuccessor)) {
3333-
// If one of the branches is guaranteed to be "long" while the other
3334-
// could be "short", then prioritize short for "taken". This will
3335-
// generate a sequence 1 byte shorter on x86.
3336-
if (IsSupported && BC.isX86() &&
3337-
TSuccessor->getFragmentNum() != FSuccessor->getFragmentNum() &&
3338-
BB->getFragmentNum() != TSuccessor->getFragmentNum()) {
3339-
std::swap(TSuccessor, FSuccessor);
3340-
{
3341-
auto L = BC.scopeLock();
3342-
MIB->reverseBranchCondition(*CondBranch, TSuccessor->getLabel(),
3343-
Ctx);
3344-
}
3345-
BB->swapConditionalSuccessors();
3346-
}
3347-
BB->addBranchInstruction(FSuccessor);
3346+
3347+
// No need for the unconditional branch.
3348+
if (FSuccessor == NextBB)
3349+
continue;
3350+
3351+
if (BC.isX86()) {
3352+
// We are going to generate two branches. Check if their targets are in
3353+
// the same fragment as this block. If only one target is in the same
3354+
// fragment, make it the destination of the conditional branch. There
3355+
// is a chance it will be a short branch which takes 5 bytes fewer than
3356+
// a long conditional branch. For unconditional branch, the difference
3357+
// is 4 bytes.
3358+
if (BB->getFragmentNum() != TSuccessor->getFragmentNum() &&
3359+
BB->getFragmentNum() == FSuccessor->getFragmentNum())
3360+
swapSuccessors();
33483361
}
3362+
3363+
BB->addBranchInstruction(FSuccessor);
33493364
}
33503365
// Cases where the number of successors is 0 (block ends with a
33513366
// terminator) or more than 2 (switch table) don't require branch

clang/lib/CodeGen/BackendUtil.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ static cl::opt<bool> ClSanitizeOnOptimizerEarlyEP(
103103
cl::desc("Insert sanitizers on OptimizerEarlyEP."), cl::init(false));
104104

105105
// Re-link builtin bitcodes after optimization
106-
static cl::opt<bool> ClRelinkBuiltinBitcodePostop(
106+
cl::opt<bool> ClRelinkBuiltinBitcodePostop(
107107
"relink-builtin-bitcode-postop", cl::Optional,
108108
cl::desc("Re-link builtin bitcodes after optimization."), cl::init(false));
109109
}

clang/lib/CodeGen/CodeGenAction.cpp

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ using namespace llvm;
5757

5858
#define DEBUG_TYPE "codegenaction"
5959

60+
namespace llvm {
61+
extern cl::opt<bool> ClRelinkBuiltinBitcodePostop;
62+
}
63+
6064
namespace clang {
6165
class BackendConsumer;
6266
class ClangDiagnosticHandler final : public DiagnosticHandler {
@@ -251,32 +255,37 @@ bool BackendConsumer::LinkInModules(llvm::Module *M, bool ShouldLinkFiles) {
251255
}
252256

253257
CurLinkModule = LM.Module.get();
254-
255-
// TODO: If CloneModule() is updated to support cloning of unmaterialized
256-
// modules, we can remove this
257258
bool Err;
258-
if (Error E = CurLinkModule->materializeAll())
259-
return false;
259+
260+
auto DoLink = [&](auto &Mod) {
261+
if (LM.Internalize) {
262+
Err = Linker::linkModules(
263+
*M, std::move(Mod), LM.LinkFlags,
264+
[](llvm::Module &M, const llvm::StringSet<> &GVS) {
265+
internalizeModule(M, [&GVS](const llvm::GlobalValue &GV) {
266+
return !GV.hasName() || (GVS.count(GV.getName()) == 0);
267+
});
268+
});
269+
} else
270+
Err = Linker::linkModules(*M, std::move(Mod), LM.LinkFlags);
271+
};
260272

261273
// Create a Clone to move to the linker, which preserves the original
262274
// linking modules, allowing them to be linked again in the future
263-
// TODO: Add a ShouldCleanup option to make Cloning optional. When
264-
// set, we can pass the original modules to the linker for cleanup
265-
std::unique_ptr<llvm::Module> Clone = llvm::CloneModule(*LM.Module);
266-
267-
if (LM.Internalize) {
268-
Err = Linker::linkModules(
269-
*M, std::move(Clone), LM.LinkFlags,
270-
[](llvm::Module &M, const llvm::StringSet<> &GVS) {
271-
internalizeModule(M, [&GVS](const llvm::GlobalValue &GV) {
272-
return !GV.hasName() || (GVS.count(GV.getName()) == 0);
273-
});
274-
});
275-
} else
276-
Err = Linker::linkModules(*M, std::move(Clone), LM.LinkFlags);
275+
if (ClRelinkBuiltinBitcodePostop) {
276+
// TODO: If CloneModule() is updated to support cloning of unmaterialized
277+
// modules, we can remove this
278+
if (Error E = CurLinkModule->materializeAll())
279+
return false;
277280

278-
if (Err)
279-
return true;
281+
std::unique_ptr<llvm::Module> Clone = llvm::CloneModule(*LM.Module);
282+
283+
DoLink(Clone);
284+
}
285+
// Otherwise we can link (and clean up) the original modules
286+
else {
287+
DoLink(LM.Module);
288+
}
280289
}
281290

282291
return false; // success

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -699,6 +699,7 @@ void CodeGenModule::checkAliases() {
699699
void CodeGenModule::clear() {
700700
DeferredDeclsToEmit.clear();
701701
EmittedDeferredDecls.clear();
702+
DeferredAnnotations.clear();
702703
if (OpenMPRuntime)
703704
OpenMPRuntime->clear();
704705
}
@@ -3165,6 +3166,13 @@ void CodeGenModule::EmitVTablesOpportunistically() {
31653166
}
31663167

31673168
void CodeGenModule::EmitGlobalAnnotations() {
3169+
for (const auto& [MangledName, VD] : DeferredAnnotations) {
3170+
llvm::GlobalValue *GV = GetGlobalValue(MangledName);
3171+
if (GV)
3172+
AddGlobalAnnotations(VD, GV);
3173+
}
3174+
DeferredAnnotations.clear();
3175+
31683176
if (Annotations.empty())
31693177
return;
31703178

@@ -3678,6 +3686,14 @@ void CodeGenModule::EmitGlobal(GlobalDecl GD) {
36783686

36793687
// Ignore declarations, they will be emitted on their first use.
36803688
if (const auto *FD = dyn_cast<FunctionDecl>(Global)) {
3689+
// Update deferred annotations with the latest declaration if the function
3690+
// function was already used or defined.
3691+
if (FD->hasAttr<AnnotateAttr>()) {
3692+
StringRef MangledName = getMangledName(GD);
3693+
if (GetGlobalValue(MangledName))
3694+
DeferredAnnotations[MangledName] = FD;
3695+
}
3696+
36813697
// Forward declarations are emitted lazily on first use.
36823698
if (!FD->doesThisDeclarationHaveABody()) {
36833699
if (!FD->doesDeclarationForceExternallyVisibleDefinition())
@@ -4462,6 +4478,11 @@ llvm::Constant *CodeGenModule::GetOrCreateLLVMFunction(
44624478
llvm::Function::Create(FTy, llvm::Function::ExternalLinkage,
44634479
Entry ? StringRef() : MangledName, &getModule());
44644480

4481+
// Store the declaration associated with this function so it is potentially
4482+
// updated by further declarations or definitions and emitted at the end.
4483+
if (D && D->hasAttr<AnnotateAttr>())
4484+
DeferredAnnotations[MangledName] = cast<ValueDecl>(D);
4485+
44654486
// If we already created a function with the same mangled name (but different
44664487
// type) before, take its name and add it to the list of functions to be
44674488
// replaced with F at the end of CodeGen.
@@ -5748,8 +5769,6 @@ void CodeGenModule::EmitGlobalFunctionDefinition(GlobalDecl GD,
57485769
AddGlobalCtor(Fn, CA->getPriority());
57495770
if (const DestructorAttr *DA = D->getAttr<DestructorAttr>())
57505771
AddGlobalDtor(Fn, DA->getPriority(), true);
5751-
if (D->hasAttr<AnnotateAttr>())
5752-
AddGlobalAnnotations(D, Fn);
57535772
if (getLangOpts().OpenMP && D->hasAttr<OMPDeclareTargetDeclAttr>())
57545773
getOpenMPRuntime().emitDeclareTargetFunction(D, GV);
57555774
}

clang/lib/CodeGen/CodeGenModule.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,10 @@ class CodeGenModule : public CodeGenTypeCache {
431431
/// Global annotations.
432432
std::vector<llvm::Constant*> Annotations;
433433

434+
// Store deferred function annotations so they can be emitted at the end with
435+
// most up to date ValueDecl that will have all the inherited annotations.
436+
llvm::DenseMap<StringRef, const ValueDecl *> DeferredAnnotations;
437+
434438
/// Map used to get unique annotation strings.
435439
llvm::StringMap<llvm::Constant*> AnnotationStrings;
436440

0 commit comments

Comments
 (0)