This is an archive of the discontinued LLVM Phabricator instance.

[AVR] Add support for the 'interrupt' and 'naked' attributes
ClosedPublic

Authored by dylanmckay on Jan 7 2017, 7:08 PM.

Details

Summary

This teaches clang how to parse and lower the 'interrupt' and 'naked'
attributes.

This allows interrupt signal handlers to be written.

Diff Detail

Repository
rL LLVM

Event Timeline

dylanmckay updated this revision to Diff 83543.Jan 7 2017, 7:08 PM
dylanmckay retitled this revision from to [AVR] Add support for the 'interrupt' and 'naked' attributes.
dylanmckay updated this object.
dylanmckay added a reviewer: aaron.ballman.
dylanmckay added a subscriber: cfe-commits.
dylanmckay updated this revision to Diff 83544.Jan 7 2017, 7:16 PM

Lower the 'signal' attribute correctly

I had forgotten to lower this attribute and so it would not have been lowered to IR at all.

Now it works fine.

aaron.ballman requested changes to this revision.Jan 10 2017, 8:34 AM
aaron.ballman edited edge metadata.

The patch is also missing Sema tests that ensure the attributes are properly diagnosed when applied to something other than a function, a target other than AVR, arguments are present, etc.

include/clang/Basic/Attr.td
485 ↗(On Diff #83544)

No new undocumented attributes, please.

488 ↗(On Diff #83544)

Does this attribute appertain to any specific subjects, or can you apply it to any declaration?

490 ↗(On Diff #83544)

No new undocumented attributes, please.

test/CodeGen/avr/attributes/naked.c
4 ↗(On Diff #83544)

This test seems unrelated as you didn't modify anything about the naked attribute?

This revision now requires changes to proceed.Jan 10 2017, 8:34 AM
dylanmckay edited edge metadata.
dylanmckay marked 2 inline comments as done.

[AVR] Document the 'interrupt' and 'naked' attributes

include/clang/Basic/Attr.td
488 ↗(On Diff #83544)

It can be applied to function definitions only.

test/CodeGen/avr/attributes/naked.c
4 ↗(On Diff #83544)

I didn't modify the naked attribute, but I did want to include a test for AVR.

aaron.ballman added inline comments.Jan 12 2017, 6:53 PM
include/clang/Basic/Attr.td
488 ↗(On Diff #83544)

Then it should have a Subjects line like AVRInterrupt does.

lib/CodeGen/TargetInfo.cpp
6898 ↗(On Diff #83913)

You can use const auto * here.

lib/Sema/SemaDeclAttr.cpp
5129 ↗(On Diff #83913)

You can remove this function.

5136 ↗(On Diff #83913)

...and this one.

5158 ↗(On Diff #83913)

Just call handleSimpleAttribute() instead.

5721 ↗(On Diff #83913)

...same here.

test/CodeGen/avr/attributes/naked.c
4 ↗(On Diff #83544)

This should probably be a separate commit then (and doesn't really need code review, since it's just adding a test and is otherwise NFC); just make sure the commit message explains why the test is beneficial to add.

dylanmckay updated this revision to Diff 85051.Jan 19 2017, 3:44 PM
dylanmckay marked 5 inline comments as done.

Code review from Aaron

  • Use 'handleSimpleAttribute' rather than duplicating it
  • Use 'auto' where a type is explicitly casted
  • Add warnings for when the attribute is not on a function
  • Add sema tests for when the attr is not on a function
dylanmckay updated this revision to Diff 85052.Jan 19 2017, 3:47 PM

Remove a test for the 'naked' attribute

dylanmckay updated this revision to Diff 85053.Jan 19 2017, 3:48 PM

Add 'Subjects' field to 'AVRSignal'

dylanmckay marked 3 inline comments as done.Jan 19 2017, 3:49 PM
dylanmckay added inline comments.
lib/Sema/SemaDeclAttr.cpp
5145 ↗(On Diff #85053)

I'm pretty sure that this check shouldn't be necessary, because we define Subjects = [Function] in TableGen.

Without it though, the warning doesn't appear. Do you know why that is @aaron.ballman?

malcolm.parsons added inline comments.
lib/CodeGen/TargetInfo.cpp
6916 ↗(On Diff #85053)

You can use auto * here too.

aaron.ballman added inline comments.Jan 23 2017, 7:31 AM
lib/Sema/SemaDeclAttr.cpp
5145 ↗(On Diff #85053)

It's because it requires custom parsing.

5146 ↗(On Diff #85053)

The diagnostic and the predicate for it don't match your Subjects line. Should this appertain to ObjC methods? If not, you want to use ExpectedFunction instead. If so, you want to add ObjCMethod to the Subjects line.

5158 ↗(On Diff #83913)

Since this is no longer truly a simple attribute (it requires some custom checking), I think my earlier advice was wrong -- you should split this into a handleAVRInterruptAttr() method. I forgot that you need the custom checking because the attribute has custom parsing.

5721 ↗(On Diff #83913)

... same here.

dylanmckay updated this revision to Diff 87054.Feb 3 2017, 5:16 PM
dylanmckay marked 5 inline comments as done.

Code review comments

  • Add 'Subjects = [ObjCMethod]' to attributes
  • Use 'auto' keyword in one place
  • Move complex attr parsing logic into static function

Just one more issue due to the nature of needing a custom parsed attribute.

lib/Sema/SemaDeclAttr.cpp
5130 ↗(On Diff #87054)

Because you have to do custom parsing, you also need to manually check that the attribute was not given any arguments and diagnose if they are present.

test/Sema/avr-interrupt-attr.c
5 ↗(On Diff #87054)

Please add a test for __attribute__((interrupt(12))), and a test with an Objective-C method. Similar for signal. You should add an ObjC method to the codegen tests as well.

dylanmckay updated this revision to Diff 87164.Feb 5 2017, 1:25 PM
dylanmckay marked 2 inline comments as done.

Verify that no arguments are given to the attributes

Also adds a bunch of tests.

dylanmckay marked an inline comment as done.Feb 5 2017, 1:26 PM

The new tests aren't really what I had in mind. The codegen tests that should be sema tests can just be rolled into your existing sema tests, by the way.

lib/Sema/SemaDeclAttr.cpp
5137 ↗(On Diff #87164)

This should simply return rather than attempt to attach an invalid attribute to the declaration (same below).

test/CodeGen/avr/attributes/interrupt-no-args.c
3 ↗(On Diff #87164)

This should be a test in Sema, not in CodeGen.

test/CodeGen/avr/attributes/interrupt.c
3 ↗(On Diff #87164)

As should this.

test/CodeGen/avr/attributes/interrupt.m
1 ↗(On Diff #87164)

Why objective-c++?

4 ↗(On Diff #87164)

This is not an Objective-C method decl, so it doesn't really test anything new. The test I was envisioning was something like:

@interface F // There's probably an expected warning here about a missing base class
-(void) foo __attribute__((interrupt));
@end

@implementation F
-(void) foo __attribute__((interrupt)) {}
@end
test/CodeGen/avr/attributes/signal-no-args.c
4 ↗(On Diff #87164)

This should also be a Sema test.

test/CodeGen/avr/attributes/signal.m
4 ↗(On Diff #87164)

Same concerns about this not testing anything new as above.

dylanmckay updated this revision to Diff 87174.Feb 5 2017, 4:41 PM
dylanmckay marked 2 inline comments as done.

Code review

  • Remove support for ObjC methods. We shouldn't do this as it doesn't really make sense
  • Move some tests to Sema
  • Don't attach invalid attributes when it isn't invalid
lib/Sema/SemaDeclAttr.cpp
5137 ↗(On Diff #87164)

Nice catch

test/CodeGen/avr/attributes/interrupt.c
3 ↗(On Diff #87164)

It seems like this sort of test _does_ sit in CodeGen - see test/CodeGen/{arm-interrupt-attr.c|mips-interrupt-attr.c}.

dylanmckay marked 4 inline comments as done.Feb 5 2017, 4:42 PM
dylanmckay added inline comments.
test/CodeGen/avr/attributes/interrupt.m
4 ↗(On Diff #87164)

Removed Obj-C method support.

dylanmckay marked 2 inline comments as done.Feb 5 2017, 4:43 PM
aaron.ballman added inline comments.Feb 6 2017, 10:20 AM
lib/Sema/SemaDeclAttr.cpp
5137 ↗(On Diff #87174)

No need to call Attr.setInvalid() here or below.

test/CodeGen/avr/attributes/interrupt.c
3 ↗(On Diff #87164)

You're correct, this test does belong here. I think I attached my comment to the wrong thing (sorry about that).

dylanmckay updated this revision to Diff 87365.Feb 6 2017, 11:08 PM
dylanmckay marked 4 inline comments as done.

Remove 'Attr.setInvalid()'

test/CodeGen/avr/attributes/interrupt.c
3 ↗(On Diff #87164)

No problems, thanks for the review!

This revision is now accepted and ready to land.Feb 7 2017, 5:10 AM
This revision was automatically updated to reflect the committed changes.