This is an archive of the discontinued LLVM Phabricator instance.

[AVR] correctly declare __do_copy_data and __do_clear_bss
ClosedPublic

Authored by aykevl on Jan 1 2023, 5:10 PM.

Details

Summary

These two symbols are declared in object files to indicate whether .data needs to be copied from flash or .bss needs to be cleared. They are supported on avr-gcc and reduce firmware size a bit, which is especially important on very small chips.

I checked the behavior of avr-gcc and matched it as well as possible. From my investigation, it seems to work as follows:

__do_copy_data is set when the compiler finds a data symbol:

  • without a section name
  • with a section name starting with ".data" or ".gnu.linkonce.d"
  • with a section name starting with ".rodata" or ".gnu.linkonce.r" and flash and RAM are in the same address space

__do_clear_bss is set when the compiler finds a data symbol:

  • without a section name
  • with a section name that starts with .bss

Simply checking whether the calculated section name starts with ".data", ".rodata" or ".bss" should result in the same behavior.

Diff Detail

Event Timeline

aykevl created this revision.Jan 1 2023, 5:10 PM
Herald added a project: Restricted Project. · View Herald TranscriptJan 1 2023, 5:10 PM
aykevl requested review of this revision.Jan 1 2023, 5:10 PM
Herald added a project: Restricted Project. · View Herald TranscriptJan 1 2023, 5:10 PM
aykevl added inline comments.Jan 1 2023, 5:15 PM
llvm/test/CodeGen/AVR/clear-bss.ll
4–5

This was an incorrect test: constant globals are actually stored in .rodata which is allocated in RAM. Making it a global makes it a .bss value.

benshi001 accepted this revision.Jan 5 2023, 11:19 PM

You can add the lines I suggested while committing.

llvm/test/CodeGen/AVR/no-clear-bss.ll
5

add a new line CHECK: .globl __do_copy_data

llvm/test/CodeGen/AVR/no-copy-data.ll
3

add CHECK: .globl __do_clear_bss

This revision is now accepted and ready to land.Jan 5 2023, 11:19 PM

Please also add Fixes https://github.com/llvm/llvm-project/issues/58857 in your commit message.

aykevl updated this revision to Diff 486958.Jan 6 2023, 12:01 PM
  • add CHECK-NOT lines to tests
  • add zeroed global to the no-copy-data.ll test
aykevl added inline comments.Jan 6 2023, 12:02 PM
llvm/test/CodeGen/AVR/no-copy-data.ll
3

__do_clear_bss isn't set either: both globals aren't in .bss (one is in a different section, the other is not defined but declared).
I have added a new global to be able to test this. Do you think this is a good solution?

benshi001 added inline comments.Jan 6 2023, 6:31 PM
llvm/test/CodeGen/AVR/no-copy-data.ll
3

That is fine. Thanks!

benshi001 accepted this revision.Jan 6 2023, 6:31 PM
This revision was automatically updated to reflect the committed changes.
vitalybuka reopened this revision.Jan 10 2023, 5:35 PM
This revision is now accepted and ready to land.Jan 10 2023, 5:35 PM
vitalybuka closed this revision.Jan 10 2023, 5:42 PM

@vitalybuka oops, thank you for the fix!