<font face="arial" size="2"><p style="margin:0;padding:0;font-family: arial; font-size: 10pt; word-wrap: break-word;">Makes sense.</p>
<p style="margin:0;padding:0;font-family: arial; font-size: 10pt; word-wrap: break-word;"> </p>
<p style="margin:0;padding:0;font-family: arial; font-size: 10pt; word-wrap: break-word;">I'm trying to decide whether to patch anything in LLVM.</p>
<p style="margin:0;padding:0;font-family: arial; font-size: 10pt; word-wrap: break-word;"> </p>
<p style="margin:0;padding:0;font-family: arial; font-size: 10pt; word-wrap: break-word;">I think this feels like it falls into the category of:</p>
<p style="margin:0;padding:0;font-family: arial; font-size: 10pt; word-wrap: break-word;"> </p>
<p style="margin:0;padding:0;font-family: arial; font-size: 10pt; word-wrap: break-word;"> </p>
<p style="margin:0;padding:0;font-family: arial; font-size: 10pt; word-wrap: break-word;"> </p>
<p style="margin:0;padding:0;font-family: arial; font-size: 10pt; word-wrap: break-word;">"What you're trying to do is an error, but the error message you're getting is uninformative, unhelpful or misleading."</p>
<p style="margin:0;padding:0;font-family: arial; font-size: 10pt; word-wrap: break-word;"> </p>
<p style="margin:0;padding:0;font-family: arial; font-size: 10pt; word-wrap: break-word;"> </p>
<p style="margin:0;padding:0;font-family: arial; font-size: 10pt; word-wrap: break-word;"> </p>
<p style="margin:0;padding:0;font-family: arial; font-size: 10pt; word-wrap: break-word;">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.</p>
<p style="margin:0;padding:0;font-family: arial; font-size: 10pt; word-wrap: break-word;"> </p>
<p style="margin:0;padding:0;font-family: arial; font-size: 10pt; word-wrap: break-word;"> </p>
<p style="margin:0;padding:0;font-family: arial; font-size: 10pt; word-wrap: break-word;">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.</p>
<p style="margin:0;padding:0;font-family: arial; font-size: 10pt; word-wrap: break-word;"> </p>
<p style="margin:0;padding:0;font-family: arial; font-size: 10pt; word-wrap: break-word;"> </p>
<p style="margin:0;padding:0;font-family: arial; font-size: 10pt; word-wrap: break-word;"> </p>
<p style="margin:0;padding:0;font-family: arial; font-size: 10pt; word-wrap: break-word;">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).</p>
<p style="margin:0;padding:0;font-family: arial; font-size: 10pt; word-wrap: break-word;"> </p>
<p style="margin:0;padding:0;font-family: arial; font-size: 10pt; word-wrap: break-word;"> </p>
<p style="margin:0;padding:0;font-family: arial; font-size: 10pt; word-wrap: break-word;"> </p>
<p style="margin:0;padding:0;font-family: arial; font-size: 10pt; word-wrap: break-word;">Thanks for your prompt help, really useful.  And thanks for pinpointing where in the code the MC is being generated.</p>
<p style="margin:0;padding:0;font-family: arial; font-size: 10pt; word-wrap: break-word;"> </p>
<p style="margin:0;padding:0;font-family: arial; font-size: 10pt; word-wrap: break-word;"> </p>
<p style="margin:0;padding:0;font-family: arial; font-size: 10pt; word-wrap: break-word;">Have a great weekend!</p>
<p style="margin:0;padding:0;font-family: arial; font-size: 10pt; word-wrap: break-word;"> </p>
<p style="margin:0;padding:0;font-family: arial; font-size: 10pt; word-wrap: break-word;">Carl</p>
<p style="margin:0;padding:0;font-family: arial; font-size: 10pt; word-wrap: break-word;"> </p>
<p style="margin:0;padding:0;font-family: arial; font-size: 10pt; word-wrap: break-word;"> </p>
<p style="margin:0;padding:0;font-family: arial; font-size: 10pt; word-wrap: break-word;"> </p>
<p style="margin:0;padding:0;font-family: arial; font-size: 10pt; word-wrap: break-word;">-----Original Message-----<br />From: "Tim Northover" <t.p.northover@gmail.com><br />Sent: Saturday, July 14, 2018 7:47am<br />To: carl-llvm-dev@petosoft.com<br />Cc: "LLVM Developers Mailing List" <llvm-dev@lists.llvm.org><br />Subject: Re: [llvm-dev] Lowering a reasonably complex struct seems to create over complex and invalid assembly fixups on some targets<br /><br /></p>
<div id="SafeStyles1531574380">
<p style="margin:0;padding:0;font-family: arial; font-size: 10pt; word-wrap: break-word;">Hi Carl,<br /><br />On Sat, 14 Jul 2018 at 11:47, carl-llvm-dev@petosoft.com via llvm-dev<br /><llvm-dev@lists.llvm.org> wrote:<br />> .long (__unnamed_4&65535)-((_TMRfV4main10Brightness&65535)+16)<br />> .long (__unnamed_5&65535)-((_TMRfV4main10Brightness&65535)+20)<br /><br />Ouch.<br /><br />> 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?<br /><br />I think the code you're after is in<br />lib/CodeGen/AsmPrinter/AsmPrinter.cpp, around line 2124 where it's<br />converting a ptrtoint ConstantExpr to an MCExpr.<br /><br />Obviously the immediate fix is to do ptrtoint to i32 here instead of<br />truncating later. And to a certain extent this kind of problem is<br />inevitable. You can always and trivially write something in a Global<br />definition that is simply not implementable by the assembler<br />(relocations are not infinitely flexible on most platforms), so it's<br />the front-end's responsibility to emit constants that are valid.<br /><br />What LLVM actually seems to be doing is assuming that expressions may<br />be evaluated at a precision beyond pointer width, which seems pretty<br />valid from a few test-cases I've run. When casting a bare pointer it's<br />fine to omit the "and", but for more complex expressions it can change<br />the value. Something like<br /><br /> @tmp = global i8 zeroinitializer<br /> @var = global i64 ptrtoint(i8* getelementptr(i8, i8* @tmp, i32 1000) to i64)<br /><br />cannot be simply emitted as ".quad tmp + 1000" on (say) 32-bit Mips<br />because that introduces a R_MIPS_64 relocation which will do 64-bit<br />arithmetic, but the GEP should wrap around at 2^32.<br /><br />Of course, it's completely different on other targets: i386 does<br />32-bit arithmetic anyway (via R_386_32), ARM produces an error<br />message. So you could make a reasonable argument that the MC layer<br />and/or the translation in AsmPrinter.cpp is being a bit too<br />opinionated on how assemblers work and should be more generic.<br /><br />Personally, I'm undecided about the platonic best option here. But I'd<br />probably quietly fix the front-end if it was my problem.<br /><br />Cheers.<br /><br />Tim.</p>
</div></font>