This is an archive of the discontinued LLVM Phabricator instance.

[AVR] Support address space casts
ClosedPublic

Authored by aykevl on Jan 19 2023, 5:59 AM.

Details

Summary

All address spaces on AVR can be freely cast between (they keep the same bit pattern). They just aren't dereferenceable when they're in a different address space as they really do point to a separate address space.

This is supported in avr-gcc: https://godbolt.org/z/9Gfvhnhv9
(It crashes in Clang but that's a separate bug).

Diff Detail

Event Timeline

aykevl created this revision.Jan 19 2023, 5:59 AM
Herald added a project: Restricted Project. · View Herald TranscriptJan 19 2023, 5:59 AM
aykevl requested review of this revision.Jan 19 2023, 5:59 AM
Herald added a project: Restricted Project. · View Herald TranscriptJan 19 2023, 5:59 AM
benshi001 accepted this revision.Jan 19 2023, 5:59 PM

LGTM. You can adopt my suggestion while committing.

llvm/lib/Target/AVR/AVRTargetMachine.h
57

it would be better to also check the range, something like

return SrcAs < AVR::NumAddrSpaces && DestAs < AVR::NumAddrSpaces &&
       getPointerSize(SrcAs) == getPointerSize(DestAs);
This revision is now accepted and ready to land.Jan 19 2023, 5:59 PM
aykevl added inline comments.Jan 19 2023, 6:15 PM
llvm/lib/Target/AVR/AVRTargetMachine.h
57

Please take a look at other architecture. Most of them make this a no-op without constraints, like here on ARM:

/// Returns true if a cast between SrcAS and DestAS is a noop.
bool isNoopAddrSpaceCast(unsigned SrcAS, unsigned DestAS) const override {
  // Addrspacecasts are always noops.
  return true;
}

Why would AVR be different?

arsenm added a subscriber: arsenm.Jan 19 2023, 6:37 PM

Commit message is a bit misleading, I'd expect "support address space casts" would mean also handling the not-noop cases

benshi001 added inline comments.Jan 19 2023, 6:43 PM
llvm/lib/Target/AVR/AVRTargetMachine.h
57

It is just a suggestion, since I find

bool X86TargetMachine::isNoopAddrSpaceCast(unsigned SrcAS,
                                           unsigned DestAS) const {
  assert(SrcAS != DestAS && "Expected different address spaces!");
  if (getPointerSize(SrcAS) != getPointerSize(DestAS))
    return false;
  return SrcAS < 256 && DestAS < 256;
}

Is there some possibility that AVR's addrspace will exceed AVR::NumAddrSpaces ?

Commit message is a bit misleading, I'd expect "support address space casts" would mean also handling the not-noop cases

As far as I know, all pointers are currently 2 bytes (16 bits) in size. So in that sense it's a bit of a moot point.
Do you have a suggestion for a better title? Something like [AVR] Don't crash on address space casts doesn't seem great either.

llvm/lib/Target/AVR/AVRTargetMachine.h
57

Is there some possibility that AVR's addrspace will exceed AVR::NumAddrSpaces ?

I am not sure, but considering that other architectures like ARM and RISC-V simply return true that seems like a good behavior. AFAIK frontends also use address spaces for special purposes, there is more than just the address spaces the hardware supports.

I looked in the LangRef but surprisingly there is no documentation at all what an address space really is (because it is many different things).

benshi001 added inline comments.Jan 21 2023, 9:33 PM
llvm/lib/Target/AVR/AVRTargetMachine.h
57

I see. So only checking pointer size is good enough. Thanks.

aykevl closed this revision.Jan 24 2023, 5:02 PM

This is merged but wasn't closed automatically, probably because I changed the commit message.