Skip to content

[clang][FMV] Fix crash with cpu_specific attribute. #115762

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions clang/lib/CodeGen/CodeGenModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4562,15 +4562,17 @@ llvm::Constant *CodeGenModule::GetOrCreateMultiVersionResolver(GlobalDecl GD) {
ResolverName += ".resolver";
}

bool ShouldReturnIFunc =
getTarget().supportsIFunc() && !FD->isCPUSpecificMultiVersion();

// If the resolver has already been created, just return it. This lookup may
// yield a function declaration instead of a resolver on AArch64. That is
// because we didn't know whether a resolver will be generated when we first
// encountered a use of the symbol named after this resolver. Therefore,
// targets which support ifuncs should not return here unless we actually
// found an ifunc.
llvm::GlobalValue *ResolverGV = GetGlobalValue(ResolverName);
if (ResolverGV &&
(isa<llvm::GlobalIFunc>(ResolverGV) || !getTarget().supportsIFunc()))
if (ResolverGV && (isa<llvm::GlobalIFunc>(ResolverGV) || !ShouldReturnIFunc))
return ResolverGV;

const CGFunctionInfo &FI = getTypes().arrangeGlobalDeclaration(GD);
Expand All @@ -4583,7 +4585,7 @@ llvm::Constant *CodeGenModule::GetOrCreateMultiVersionResolver(GlobalDecl GD) {

// For cpu_specific, don't create an ifunc yet because we don't know if the
// cpu_dispatch will be emitted in this translation unit.
if (getTarget().supportsIFunc() && !FD->isCPUSpecificMultiVersion()) {
if (ShouldReturnIFunc) {
unsigned AS = getTypes().getTargetAddressSpace(FD->getType());
llvm::Type *ResolverType =
llvm::FunctionType::get(llvm::PointerType::get(DeclTy, AS), false);
Expand All @@ -4602,11 +4604,9 @@ llvm::Constant *CodeGenModule::GetOrCreateMultiVersionResolver(GlobalDecl GD) {

llvm::Constant *Resolver = GetOrCreateLLVMFunction(
ResolverName, DeclTy, GlobalDecl{}, /*ForVTable=*/false);
assert(isa<llvm::GlobalValue>(Resolver) &&
assert(isa<llvm::GlobalValue>(Resolver) && !ResolverGV &&
"Resolver should be created for the first time");
SetCommonAttributes(FD, cast<llvm::GlobalValue>(Resolver));
if (ResolverGV)
replaceDeclarationWith(ResolverGV, Resolver);
return Resolver;
}

Expand Down
6 changes: 6 additions & 0 deletions clang/test/CodeGen/attr-cpuspecific.c
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,12 @@ void usages(void) {
CpuSpecificNoDispatch();
// LINUX: @CpuSpecificNoDispatch.ifunc()
// WINDOWS: @CpuSpecificNoDispatch()
//
// Adding another use of CpuSpecificNoDispatch reproduces the
// crash in https://github.com/llvm/llvm-project/issues/115299
CpuSpecificNoDispatch();
// LINUX: @CpuSpecificNoDispatch.ifunc()
// WINDOWS: @CpuSpecificNoDispatch()
OrderDispatchUsageSpecific();
// LINUX: @OrderDispatchUsageSpecific.ifunc()
// WINDOWS: @OrderDispatchUsageSpecific()
Expand Down
Loading