Skip to content

Commit b892c59

Browse files
committed
[clang][ARM] Fix warning for using VFP from interrupts.
This warning has three issues: - The interrupt attribute causes the function to return using an exception return instruction. This warning allows calls from one function with the interrupt attribute to another, and the diagnostic text suggests that not having the attribute on the callee is a problem. Actually making such a call will lead to a double exception return, which is unpredictable according to the ARM architecture manual section B9.1.1, "Restrictions on exception return instructions". Even on machines where an exception return from user/system mode is tolerated, if the callee's interrupt type is anything other than a supervisor call or secure monitor call, it will also return to a different address than a normal function would. For example, returning from an "IRQ" handler will return to lr - 4, which will generally result in calling the same function again. - The interrupt attribute currently does not cause caller-saved VFP registers to be saved and restored if they are used, so putting __attribute__((interrupt)) on a called function doesn't prevent it from clobbering VFP state. - It is part of the -Wextra diagnostic group and can't be individually disabled when using -Wextra, which also means the diagnostic text of this specific warning appears in the documentation of -Wextra. This change addresses all three issues by instead generating a warning for any interrupt handler where the vfp feature is enabled. The warning is also given its own diagnostic group. Closes llvm#34876.
1 parent 58df646 commit b892c59

File tree

5 files changed

+27
-55
lines changed

5 files changed

+27
-55
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,11 @@ Modified Compiler Flags
409409
evaluating to ``true`` and an empty body such as ``while(1);``)
410410
are considered infinite, even when the ``-ffinite-loop`` flag is set.
411411

412+
- Removed "arm interrupt calling convention" warning that was included in
413+
``-Wextra`` without its own flag.
414+
415+
- Added ``-Warm-interrupt-vfp-clobber``, with its own warning group.
416+
412417
Removed Compiler Flags
413418
-------------------------
414419

@@ -569,6 +574,12 @@ Improvements to Clang's diagnostics
569574
- Clang no longer emits a "declared here" note for a builtin function that has no declaration in source.
570575
Fixes #GH93369.
571576

577+
- For the ARM target, Clang no longer suggests adding ``__attribute__((interrupt))`` to
578+
functions that are called from interrupt handlers to prevent clobbering VFP registers
579+
as part of ``-Wextra`` (#GH34876). Following this suggestion leads to unpredictable
580+
behavior. Instead, a new warning, ``-Warm-interrupt-vfp-clobber`` will trigger for
581+
interrupt handlers with VFP enabled.
582+
572583
Improvements to Clang's time-trace
573584
----------------------------------
574585

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -336,9 +336,10 @@ def warn_anyx86_excessive_regsave : Warning<
336336
" with attribute 'no_caller_saved_registers'"
337337
" or be compiled with '-mgeneral-regs-only'">,
338338
InGroup<DiagGroup<"excessive-regsave">>;
339-
def warn_arm_interrupt_calling_convention : Warning<
340-
"call to function without interrupt attribute could clobber interruptee's VFP registers">,
341-
InGroup<Extra>;
339+
def warn_arm_interrupt_vfp_clobber : Warning<
340+
"interrupt service routine with vfp enabled may clobber the "
341+
"interruptee's vfp state">,
342+
InGroup<DiagGroup<"arm-interrupt-vfp-clobber">>;
342343
def warn_interrupt_attribute_invalid : Warning<
343344
"%select{MIPS|MSP430|RISC-V}0 'interrupt' attribute only applies to "
344345
"functions that have %select{no parameters|a 'void' return type}1">,

clang/lib/Sema/SemaARM.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1277,6 +1277,11 @@ void SemaARM::handleInterruptAttr(Decl *D, const ParsedAttr &AL) {
12771277
return;
12781278
}
12791279

1280+
const TargetInfo &TI = getASTContext().getTargetInfo();
1281+
if (TI.hasFeature("vfp")) {
1282+
Diag(D->getLocation(), diag::warn_arm_interrupt_vfp_clobber);
1283+
}
1284+
12801285
D->addAttr(::new (getASTContext())
12811286
ARMInterruptAttr(getASTContext(), AL, Kind));
12821287
}

clang/lib/Sema/SemaExpr.cpp

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6760,22 +6760,10 @@ ExprResult Sema::BuildResolvedCallExpr(Expr *Fn, NamedDecl *NDecl,
67606760
return ExprError();
67616761
}
67626762

6763-
// Interrupt handlers don't save off the VFP regs automatically on ARM,
6764-
// so there's some risk when calling out to non-interrupt handler functions
6765-
// that the callee might not preserve them. This is easy to diagnose here,
6766-
// but can be very challenging to debug.
6767-
// Likewise, X86 interrupt handlers may only call routines with attribute
6763+
// X86 interrupt handlers may only call routines with attribute
67686764
// no_caller_saved_registers since there is no efficient way to
67696765
// save and restore the non-GPR state.
67706766
if (auto *Caller = getCurFunctionDecl()) {
6771-
if (Caller->hasAttr<ARMInterruptAttr>()) {
6772-
bool VFP = Context.getTargetInfo().hasFeature("vfp");
6773-
if (VFP && (!FDecl || !FDecl->hasAttr<ARMInterruptAttr>())) {
6774-
Diag(Fn->getExprLoc(), diag::warn_arm_interrupt_calling_convention);
6775-
if (FDecl)
6776-
Diag(FDecl->getLocation(), diag::note_callee_decl) << FDecl;
6777-
}
6778-
}
67796767
if (Caller->hasAttr<AnyX86InterruptAttr>() ||
67806768
Caller->hasAttr<AnyX86NoCallerSavedRegistersAttr>()) {
67816769
const TargetInfo &TI = Context.getTargetInfo();
Lines changed: 6 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,19 @@
1-
// RUN: %clang_cc1 %s -triple arm-apple-darwin -target-feature +vfp2 -verify -fsyntax-only
2-
// RUN: %clang_cc1 %s -triple thumb-apple-darwin -target-feature +vfp3 -verify -fsyntax-only
3-
// RUN: %clang_cc1 %s -triple armeb-none-eabi -target-feature +vfp4 -verify -fsyntax-only
4-
// RUN: %clang_cc1 %s -triple thumbeb-none-eabi -target-feature +neon -verify -fsyntax-only
5-
// RUN: %clang_cc1 %s -triple thumbeb-none-eabi -target-feature +neon -target-feature +soft-float -DSOFT -verify -fsyntax-only
1+
// RUN: %clang_cc1 %s -triple arm-none-eabi -DSOFT -verify -fsyntax-only
2+
// RUN: %clang_cc1 %s -triple arm-none-eabi -target-feature +vfp2 -verify -fsyntax-only
63

7-
__attribute__((interrupt(IRQ))) void foo(void) {} // expected-error {{'interrupt' attribute requires a string}}
8-
__attribute__((interrupt("irq"))) void foo1(void) {} // expected-warning {{'interrupt' attribute argument not supported: irq}}
94

5+
#ifdef SOFT
6+
__attribute__((interrupt("irq"))) void foo1(void) {} // expected-warning {{'interrupt' attribute argument not supported: irq}}
7+
__attribute__((interrupt(IRQ))) void foo(void) {} // expected-error {{'interrupt' attribute requires a string}}
108
__attribute__((interrupt("IRQ", 1))) void foo2(void) {} // expected-error {{'interrupt' attribute takes no more than 1 argument}}
11-
129
__attribute__((interrupt("IRQ"))) void foo3(void) {}
1310
__attribute__((interrupt("FIQ"))) void foo4(void) {}
1411
__attribute__((interrupt("SWI"))) void foo5(void) {}
1512
__attribute__((interrupt("ABORT"))) void foo6(void) {}
1613
__attribute__((interrupt("UNDEF"))) void foo7(void) {}
17-
1814
__attribute__((interrupt)) void foo8(void) {}
1915
__attribute__((interrupt())) void foo9(void) {}
2016
__attribute__((interrupt(""))) void foo10(void) {}
21-
22-
#ifndef SOFT
23-
// expected-note@+2 {{'callee1' declared here}}
24-
#endif
25-
void callee1(void);
26-
__attribute__((interrupt("IRQ"))) void callee2(void);
27-
void caller1(void) {
28-
callee1();
29-
callee2();
30-
}
31-
32-
#ifndef SOFT
33-
__attribute__((interrupt("IRQ"))) void caller2(void) {
34-
callee1(); // expected-warning {{call to function without interrupt attribute could clobber interruptee's VFP registers}}
35-
callee2();
36-
}
37-
38-
void (*callee3)(void);
39-
__attribute__((interrupt("IRQ"))) void caller3(void) {
40-
callee3(); // expected-warning {{call to function without interrupt attribute could clobber interruptee's VFP registers}}
41-
}
4217
#else
43-
__attribute__((interrupt("IRQ"))) void caller2(void) {
44-
callee1();
45-
callee2();
46-
}
47-
48-
void (*callee3)(void);
49-
__attribute__((interrupt("IRQ"))) void caller3(void) {
50-
callee3();
51-
}
18+
__attribute__((interrupt("IRQ"))) void float_irq(void); // expected-warning {{interrupt service routine with vfp enabled may clobber the interruptee's vfp state}}
5219
#endif

0 commit comments

Comments
 (0)