Skip to content

Commit 2095655

Browse files
authored
[clang][sema] Fix -Wunused-function on target_version'd file-scope Fn's (llvm#81167)
We should only warn if the default version is the one that is unused. Fixes: llvm#80227
1 parent 99446df commit 2095655

File tree

4 files changed

+27
-1
lines changed

4 files changed

+27
-1
lines changed

clang/include/clang/AST/Decl.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2615,6 +2615,10 @@ class FunctionDecl : public DeclaratorDecl,
26152615
/// the target functionality.
26162616
bool isTargetMultiVersion() const;
26172617

2618+
/// True if this function is the default version of a multiversioned dispatch
2619+
/// function as a part of the target functionality.
2620+
bool isTargetMultiVersionDefault() const;
2621+
26182622
/// True if this function is a multiversioned dispatch function as a part of
26192623
/// the target-clones functionality.
26202624
bool isTargetClonesMultiVersion() const;

clang/lib/AST/Decl.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3537,6 +3537,11 @@ bool FunctionDecl::isTargetMultiVersion() const {
35373537
(hasAttr<TargetAttr>() || hasAttr<TargetVersionAttr>());
35383538
}
35393539

3540+
bool FunctionDecl::isTargetMultiVersionDefault() const {
3541+
return isMultiVersion() && hasAttr<TargetVersionAttr>() &&
3542+
getAttr<TargetVersionAttr>()->isDefaultVersion();
3543+
}
3544+
35403545
bool FunctionDecl::isTargetClonesMultiVersion() const {
35413546
return isMultiVersion() && hasAttr<TargetClonesAttr>();
35423547
}

clang/lib/Sema/Sema.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1393,7 +1393,8 @@ void Sema::ActOnEndOfTranslationUnit() {
13931393
Diag(DiagD->getLocation(), diag::warn_unneeded_internal_decl)
13941394
<< /*function=*/0 << DiagD << DiagRange;
13951395
}
1396-
} else {
1396+
} else if (!FD->isTargetMultiVersion() ||
1397+
FD->isTargetMultiVersionDefault()) {
13971398
if (FD->getDescribedFunctionTemplate())
13981399
Diag(DiagD->getLocation(), diag::warn_unused_template)
13991400
<< /*function=*/0 << DiagD << DiagRange;

clang/test/SemaCXX/warn-unused-filescoped.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,4 +236,20 @@ constexpr int constexpr4() { return 2; }
236236
#endif
237237
}
238238

239+
__attribute__((target_version("fp16")))
240+
static int not_used_fmv(void) { return 1; }
241+
__attribute__((target_version("fp16fml")))
242+
static int not_used_fmv(void) { return 2; }
243+
__attribute__((target_version("default")))
244+
static int not_used_fmv(void) { return 0; } // expected-warning {{unused function 'not_used_fmv'}}
245+
246+
247+
__attribute__((target_version("fp16")))
248+
static int definitely_used_fmv(void) { return 1; }
249+
__attribute__((target_version("fp16fml")))
250+
static int definitely_used_fmv(void) { return 2; }
251+
__attribute__((target_version("default")))
252+
static int definitely_used_fmv(void) { return 0; }
253+
int definite_user(void) { return definitely_used_fmv(); }
254+
239255
#endif

0 commit comments

Comments
 (0)