[llvm-dev] Lowering a reasonably complex struct seems to create over complex and invalid assembly fixups on some targets

carl-llvm-dev@petosoft.com via llvm-dev llvm-dev at lists.llvm.org
Sat Jul 14 06:28:30 PDT 2018


Makes sense.
 
I'm trying to decide whether to patch anything in LLVM.
 
I think this feels like it falls into the category of:
 
 
 
"What you're trying to do is an error, but the error message you're getting is uninformative, unhelpful or misleading."
 
 
 
The version of LLVM I've compiled doesn't have ARM target support (for no particular reason, I just configured it for AVR and x86 only, by chance) so I can't test the IR.  I feel like the behaviour of AsmPrinter isn't quite right on some platforms, it sounds like ARM has the right approach, just throw an error if you're trying to lower LLVM IR that has 64 bit pointers onto platforms that could never support that, then throw an informative error, rather than creating MC Expressions that cannot be assembled by the lower layers.
 
 
I may take a bit of time to look at it one day and see if I can see an obvious fix. But as you've probably guessed, I'm very new to LLVM (I'm a swift developer by day and IoT/compiler hacker by night), so I'd probably get out of my depth too quickly.
 
 
 
The best/right fix for me is (as you suggested) compiling the swift front end from scratch to try and support a 16 bit pointer target triple (difficult according to Apple engineers I chatted to but possible and they might help a bit).
 
 
 
Thanks for your prompt help, really useful.  And thanks for pinpointing where in the code the MC is being generated.
 
 
Have a great weekend!
 
Carl
 
 
 
-----Original Message-----
From: "Tim Northover" <t.p.northover at gmail.com>
Sent: Saturday, July 14, 2018 7:47am
To: carl-llvm-dev at petosoft.com
Cc: "LLVM Developers Mailing List" <llvm-dev at lists.llvm.org>
Subject: Re: [llvm-dev] Lowering a reasonably complex struct seems to create over complex and invalid assembly fixups on some targets



Hi Carl,

On Sat, 14 Jul 2018 at 11:47, carl-llvm-dev at petosoft.com via llvm-dev
<llvm-dev at lists.llvm.org> wrote:
> .long (__unnamed_4&65535)-((_TMRfV4main10Brightness&65535)+16)
> .long (__unnamed_5&65535)-((_TMRfV4main10Brightness&65535)+20)

Ouch.

> AVR is an experimental target but I think MIPS is mature? So I’m trying to get to the bottom of how LLVM is lowering to these expressions in fixups, when they cannot be evaluated by the MC layer?

I think the code you're after is in
lib/CodeGen/AsmPrinter/AsmPrinter.cpp, around line 2124 where it's
converting a ptrtoint ConstantExpr to an MCExpr.

Obviously the immediate fix is to do ptrtoint to i32 here instead of
truncating later. And to a certain extent this kind of problem is
inevitable. You can always and trivially write something in a Global
definition that is simply not implementable by the assembler
(relocations are not infinitely flexible on most platforms), so it's
the front-end's responsibility to emit constants that are valid.

What LLVM actually seems to be doing is assuming that expressions may
be evaluated at a precision beyond pointer width, which seems pretty
valid from a few test-cases I've run. When casting a bare pointer it's
fine to omit the "and", but for more complex expressions it can change
the value. Something like

 @tmp = global i8 zeroinitializer
 @var = global i64 ptrtoint(i8* getelementptr(i8, i8* @tmp, i32 1000) to i64)

cannot be simply emitted as ".quad tmp + 1000" on (say) 32-bit Mips
because that introduces a R_MIPS_64 relocation which will do 64-bit
arithmetic, but the GEP should wrap around at 2^32.

Of course, it's completely different on other targets: i386 does
32-bit arithmetic anyway (via R_386_32), ARM produces an error
message. So you could make a reasonable argument that the MC layer
and/or the translation in AsmPrinter.cpp is being a bit too
opinionated on how assemblers work and should be more generic.

Personally, I'm undecided about the platonic best option here. But I'd
probably quietly fix the front-end if it was my problem.

Cheers.

Tim.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20180714/5a2dc10d/attachment.html>


More information about the llvm-dev mailing list