Skip to content

Commit 88654f8

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 567b2c6 commit 88654f8

File tree

5 files changed

+25
-55
lines changed

5 files changed

+25
-55
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,9 @@ New Compiler Flags
420420
Matches MSVC behaviour by defining ``__STDC__`` to ``1`` when
421421
MSVC compatibility mode is used. It has no effect for C++ code.
422422

423+
- For the ARM target, added ``-Warm-interrupt-vfp-clobber`` that will emit a
424+
diagnostic when an interrupt handler is declared and VFP is enabled.
425+
423426
Deprecated Compiler Flags
424427
-------------------------
425428

@@ -458,6 +461,12 @@ Modified Compiler Flags
458461
evaluating to ``true`` and an empty body such as ``while(1);``)
459462
are considered infinite, even when the ``-ffinite-loop`` flag is set.
460463

464+
- Removed the "arm interrupt calling convention" warning that was included in
465+
``-Wextra`` without its own flag (#GH34876). This warning suggested adding
466+
``__attribute__((interrupt))`` to functions that are called from interrupt handlers
467+
to prevent clobbering VFP registers. Following this suggestion leads to unpredictable
468+
behavior by causing multiple exception returns from one exception.
469+
461470
Removed Compiler Flags
462471
-------------------------
463472

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
@@ -1326,6 +1326,11 @@ void SemaARM::handleInterruptAttr(Decl *D, const ParsedAttr &AL) {
13261326
return;
13271327
}
13281328

1329+
const TargetInfo &TI = getASTContext().getTargetInfo();
1330+
if (TI.hasFeature("vfp")) {
1331+
Diag(D->getLocation(), diag::warn_arm_interrupt_vfp_clobber);
1332+
}
1333+
13291334
D->addAttr(::new (getASTContext())
13301335
ARMInterruptAttr(getASTContext(), AL, Kind));
13311336
}

clang/lib/Sema/SemaExpr.cpp

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

6773-
// Interrupt handlers don't save off the VFP regs automatically on ARM,
6774-
// so there's some risk when calling out to non-interrupt handler functions
6775-
// that the callee might not preserve them. This is easy to diagnose here,
6776-
// but can be very challenging to debug.
6777-
// Likewise, X86 interrupt handlers may only call routines with attribute
6773+
// X86 interrupt handlers may only call routines with attribute
67786774
// no_caller_saved_registers since there is no efficient way to
67796775
// save and restore the non-GPR state.
67806776
if (auto *Caller = getCurFunctionDecl()) {
6781-
if (Caller->hasAttr<ARMInterruptAttr>()) {
6782-
bool VFP = Context.getTargetInfo().hasFeature("vfp");
6783-
if (VFP && (!FDecl || !FDecl->hasAttr<ARMInterruptAttr>())) {
6784-
Diag(Fn->getExprLoc(), diag::warn_arm_interrupt_calling_convention);
6785-
if (FDecl)
6786-
Diag(FDecl->getLocation(), diag::note_callee_decl) << FDecl;
6787-
}
6788-
}
67896777
if (Caller->hasAttr<AnyX86InterruptAttr>() ||
67906778
Caller->hasAttr<AnyX86NoCallerSavedRegistersAttr>()) {
67916779
const TargetInfo &TI = Context.getTargetInfo();

clang/test/Sema/arm-interrupt-attr.c

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)