<div dir="ltr">(Looks like the patch converts a few files to dos-style line endings, which makes it larger than it needs to be.)</div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sun, Nov 3, 2019 at 3:51 PM Matt Asplund via cfe-dev <<a href="mailto:cfe-dev@lists.llvm.org">cfe-dev@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div>I have attached a patch for the changes to update SourceLocation to internally use an unsigned long long (I tried to put it on phabricator, but it was not having my changes). The change is as isolated as I could make it to allow a majority of clang to continue to use the old 32 bit ids whenever possible and only convert locations that I was personally hitting overflows (when trying to convert down) during large pcm lazy loading. Note: There may be a few random C++20 Modules fixes and/or hacks sprinkled in, this was entirely to unblock my own investigations into a C++ Modules build system.</div><div><br></div><div>-Matt<br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, Oct 10, 2019 at 7:59 AM Christof Douma <<a href="mailto:Christof.Douma@arm.com" target="_blank">Christof.Douma@arm.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<div lang="EN-GB">
<div>
<p class="MsoNormal"><span>Hi Matt.<u></u><u></u></span></p>
<p class="MsoNormal"><span><u></u> <u></u></span></p>
<p class="MsoNormal"><span>Thanks for the offer. Whenever you’re back and have a moment is fine by me, I don’t think we’re in a massive hurry.<u></u><u></u></span></p>
<p class="MsoNormal"><span><u></u> <u></u></span></p>
<p class="MsoNormal"><span>Thanks,<u></u><u></u></span></p>
<p class="MsoNormal"><span>Christof<u></u><u></u></span></p>
<p class="MsoNormal"><span><u></u> <u></u></span></p>
<div style="border-color:currentcolor currentcolor currentcolor blue;border-style:none none none solid;border-width:medium medium medium 1.5pt;padding:0cm 0cm 0cm 4pt">
<div>
<div style="border-color:rgb(225,225,225) currentcolor currentcolor;border-style:solid none none;border-width:1pt medium medium;padding:3pt 0cm 0cm">
<p class="MsoNormal"><b><span lang="EN-US">From:</span></b><span lang="EN-US"> Matt Asplund <<a href="mailto:mwasplund@gmail.com" target="_blank">mwasplund@gmail.com</a>>
<br>
<b>Sent:</b> 10 October 2019 14:09<br>
<b>To:</b> Christof Douma <<a href="mailto:Christof.Douma@arm.com" target="_blank">Christof.Douma@arm.com</a>><br>
<b>Cc:</b> Richard Smith <<a href="mailto:richard@metafoo.co.uk" target="_blank">richard@metafoo.co.uk</a>>; Mikhail Maltsev <<a href="mailto:Mikhail.Maltsev@arm.com" target="_blank">Mikhail.Maltsev@arm.com</a>>; Clang Dev <<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a>>; nd <<a href="mailto:nd@arm.com" target="_blank">nd@arm.com</a>><br>
<b>Subject:</b> Re: [cfe-dev] [RFC] Clang SourceLocation overflow<u></u><u></u></span></p>
</div>
</div>
<p class="MsoNormal"><u></u> <u></u></p>
<div>
<p class="MsoNormal">Sure, I can share my changes. In general my changes are very localized to the code paths I was hitting oveflow issues and tried to keep as much as possible using 32bit encodings. Is there an ETA for when you will start investigating, I
am out of town for the next week but if needed can get access to my desktop.<u></u><u></u></p>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">-Matt<u></u><u></u></p>
</div>
</div>
<p class="MsoNormal"><u></u> <u></u></p>
<div>
<div>
<p class="MsoNormal">On Thu, Oct 10, 2019, 2:13 AM Christof Douma <<a href="mailto:Christof.Douma@arm.com" target="_blank">Christof.Douma@arm.com</a>> wrote:<u></u><u></u></p>
</div>
<blockquote style="border-color:currentcolor currentcolor currentcolor rgb(204,204,204);border-style:none none none solid;border-width:medium medium medium 1pt;padding:0cm 0cm 0cm 6pt;margin:5pt 0cm 5pt 4.8pt">
<div>
<div>
<p class="MsoNormal">Hi Richard.<u></u><u></u></p>
<p class="MsoNormal"> <u></u><u></u></p>
<p class="MsoNormal">Thanks for the clarification, I certainly had not realized that location tracking was needed for correctness of clang. Decoupling this sounds like a painful processes, and the only
thing we get is errors without any location information. Sounds like not a great trade-off. We’ll go experiment a bit with the size impact of using 64-bits and will attempt to take the route of a cmake configuration option.
<u></u><u></u></p>
<p class="MsoNormal"> <u></u><u></u></p>
<p class="MsoNormal">If there are people that have already done some experiments on the size impact of 64-bits location pointers, we’re welcome your insights.
<a href="mailto:mwasplund@gmail.com" target="_blank"><span style="text-decoration:none">@Matt Asplund</span></a> I believe you said that you’ve done some prototyping, is there something you can share?<u></u><u></u></p>
<p class="MsoNormal"> <u></u><u></u></p>
<p class="MsoNormal">Thanks,<u></u><u></u></p>
<p class="MsoNormal">Christof<u></u><u></u></p>
<p class="MsoNormal"> <u></u><u></u></p>
<div style="border-color:currentcolor currentcolor currentcolor blue;border-style:none none none solid;border-width:medium medium medium 1.5pt;padding:0cm 0cm 0cm 4pt">
<div>
<div style="border-color:rgb(225,225,225) currentcolor currentcolor;border-style:solid none none;border-width:1pt medium medium;padding:3pt 0cm 0cm">
<p class="MsoNormal"><b><span lang="EN-US">From:</span></b><span lang="EN-US"> Richard Smith <</span><a href="mailto:richard@metafoo.co.uk" target="_blank"><span lang="EN-US">richard@metafoo.co.uk</span></a><span lang="EN-US">>
<br>
<b>Sent:</b> 08 October 2019 19:53<br>
<b>To:</b> Christof Douma <</span><a href="mailto:Christof.Douma@arm.com" target="_blank"><span lang="EN-US">Christof.Douma@arm.com</span></a><span lang="EN-US">><br>
<b>Cc:</b> Mikhail Maltsev <</span><a href="mailto:Mikhail.Maltsev@arm.com" target="_blank"><span lang="EN-US">Mikhail.Maltsev@arm.com</span></a><span lang="EN-US">>; Clang Dev <</span><a href="mailto:cfe-dev@lists.llvm.org" target="_blank"><span lang="EN-US">cfe-dev@lists.llvm.org</span></a><span lang="EN-US">>;
nd <</span><a href="mailto:nd@arm.com" target="_blank"><span lang="EN-US">nd@arm.com</span></a><span lang="EN-US">><br>
<b>Subject:</b> Re: [cfe-dev] [RFC] Clang SourceLocation overflow</span><u></u><u></u></p>
</div>
</div>
<p class="MsoNormal"> <u></u><u></u></p>
<div>
<div>
<div>
<div>
<p class="MsoNormal">On Tue, 8 Oct 2019, 10:42 Christof Douma via cfe-dev, <<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a>> wrote:<u></u><u></u></p>
</div>
<blockquote style="border-color:currentcolor currentcolor currentcolor rgb(204,204,204);border-style:none none none solid;border-width:medium medium medium 1pt;padding:0cm 0cm 0cm 6pt;margin:5pt 0cm 5pt 4.8pt">
<div>
<div>
<p class="MsoNormal">Hi Richard, Paul and other.<u></u><u></u></p>
<p class="MsoNormal"> <u></u><u></u></p>
<p class="MsoNormal">Thanks for the input so far. I wanted to point out that it’s not our code-base. Rather, we’re seeing more use of the LLVM technology in the automotive market and as usual we’re
faced with existing code bases that are tried and tested with other toolchains (gcc or others) and when LLVM comes along things don’t always work directly.<u></u><u></u></p>
<p class="MsoNormal"> <u></u><u></u></p>
<p class="MsoNormal">We’ve suggested better ways of structuring their code and your suggestions are certainly good input. However, legacy code is especially sticky in any market that has to handle ‘safety’
concerns, like automotive, aerospace and medical markets. Code changes are pretty expensive in those fields. So while I hope that over time we see more sensible coding structures, I don’t expect that to happen any time soon. In the mean time, we’re searching
for a solution for this coding pattern that doesn’t play well with clang.<u></u><u></u></p>
<p class="MsoNormal"> <u></u><u></u></p>
<p class="MsoNormal">Hope that gave some more background of where this question comes from.<u></u><u></u></p>
<p class="MsoNormal"> <u></u><u></u></p>
<p class="MsoNormal">Do all options that were suggested by Mikhail really require fundamental restructuring of major parts of clang? This surprised me, I had expected that the option 2 to be possible
without a complete overhaul. (2 is “Track until an overflow occurs after that make the lexer output the <invalid location> special value for all subsequent tokens.”)<u></u><u></u></p>
</div>
</div>
</blockquote>
</div>
</div>
<div>
<p class="MsoNormal">Clang uses source locations as part of the semantic representation of the AST in some cases (simple example: some forms of initialization might use parens or not, with different
semantics, and we distinguish between them based on whether we have paren locations; there are also some checks that look at which of two source locations came first when determining which warnings or errors to produce, and so on). Maybe we could go through
and fix all of those, but that's still a very large task and it'd be hard to check we got them all.<u></u><u></u></p>
</div>
<div>
<div>
<blockquote style="border-color:currentcolor currentcolor currentcolor rgb(204,204,204);border-style:none none none solid;border-width:medium medium medium 1pt;padding:0cm 0cm 0cm 6pt;margin:5pt 0cm 5pt 4.8pt">
<div>
<div>
<p class="MsoNormal">Not nice user experience but maybe doable? I was hoping there was something slightly better that still works without a major restructuring (maybe something that at least gives a
rough location or something that only gives the location of the error and not the include stack under an option or using some kind of heuristic to detect that things go haywire).<u></u><u></u></p>
<p class="MsoNormal"> <u></u><u></u></p>
<p class="MsoNormal">As an alternative, I was curious if it would be possible and acceptable to make the switch between 32-bit and 64-bit location tracking a build-time/cmake decision? I’ve not done
any estimation on the memory size growth, so maybe this is a dead end.<u></u><u></u></p>
</div>
</div>
</blockquote>
</div>
</div>
<div>
<p class="MsoNormal">If someone is prepared to do the work to add and maintain this build mode, I think this might be a feasible option.<u></u><u></u></p>
</div>
<div>
<div>
<blockquote style="border-color:currentcolor currentcolor currentcolor rgb(204,204,204);border-style:none none none solid;border-width:medium medium medium 1pt;padding:0cm 0cm 0cm 6pt;margin:5pt 0cm 5pt 4.8pt">
<div>
<div>
<p class="MsoNormal">Thanks,<u></u><u></u></p>
<p class="MsoNormal">Christof<u></u><u></u></p>
<p class="MsoNormal"> <u></u><u></u></p>
<div style="border-color:currentcolor currentcolor currentcolor blue;border-style:none none none solid;border-width:medium medium medium 1.5pt;padding:0cm 0cm 0cm 4pt">
<div>
<div style="border-color:rgb(225,225,225) currentcolor currentcolor;border-style:solid none none;border-width:1pt medium medium;padding:3pt 0cm 0cm">
<p class="MsoNormal"><b><span lang="EN-US">From:</span></b><span lang="EN-US"> cfe-dev <</span><a href="mailto:cfe-dev-bounces@lists.llvm.org" target="_blank"><span lang="EN-US">cfe-dev-bounces@lists.llvm.org</span></a><span lang="EN-US">>
<b>On Behalf Of </b>Richard Smith via cfe-dev<br>
<b>Sent:</b> 07 October 2019 20:36<br>
<b>To:</b> Mikhail Maltsev <</span><a href="mailto:Mikhail.Maltsev@arm.com" target="_blank"><span lang="EN-US">Mikhail.Maltsev@arm.com</span></a><span lang="EN-US">><br>
<b>Cc:</b> nd <</span><a href="mailto:nd@arm.com" target="_blank"><span lang="EN-US">nd@arm.com</span></a><span lang="EN-US">>;
</span><a href="mailto:cfe-dev@lists.llvm.org" target="_blank"><span lang="EN-US">cfe-dev@lists.llvm.org</span></a><span lang="EN-US"><br>
<b>Subject:</b> Re: [cfe-dev] [RFC] Clang SourceLocation overflow</span><u></u><u></u></p>
</div>
</div>
<p class="MsoNormal"> <u></u><u></u></p>
<div>
<div>
<p class="MsoNormal">On Wed, 2 Oct 2019 at 09:26, Mikhail Maltsev via cfe-dev <<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a>> wrote:<u></u><u></u></p>
</div>
<div>
<blockquote style="border-color:currentcolor currentcolor currentcolor rgb(204,204,204);border-style:none none none solid;border-width:medium medium medium 1pt;padding:0cm 0cm 0cm 6pt;margin:5pt 0cm 5pt 4.8pt">
<p class="MsoNormal">Hi all,<br>
<br>
We are experiencing a problem with Clang SourceLocation overflow.<br>
Currently source locations are 32-bit values, one bit is a flag, which gives<br>
a source location space of 2^31 characters.<br>
<br>
When the Clang lexer processes an #include directive it reserves the total size<br>
of the file being included in the source location space. An overflow can occur<br>
if a large file (which does not have include guards by design) is included many<br>
times into a single TU.<br>
<br>
The pattern of including a file multiple times is for example required by<br>
the AUTOSAR standard [1], which is widely used in the automotive industry.<br>
Specifically the pattern is described in the Specification of Memory Mapping [2]:<br>
<br>
Section 8.2.1, MEMMAP003:<br>
"The start and stop symbols for section control are configured with section<br>
identifiers defined in MemMap.h [...] For instance:<br>
<br>
#define EEP_START_SEC_VAR_16BIT<br>
#include "MemMap.h"<br>
static uint16 EepTimer;<br>
static uint16 EepRemainingBytes;<br>
#define EEP_STOP_SEC_VAR_16BIT<br>
#include "MemMap.h""<br>
<br>
Section 8.2.2, MEMMAP005:<br>
"The file MemMap.h shall provide a mechanism to select different code, variable<br>
or constant sections by checking the definition of the module specific memory<br>
allocation key words for starting a section [...]"<br>
<br>
In practice MemMap.h can reach several MBs and can be included several thousand<br>
times causing an overflow in the source location space.<br>
<br>
The problem does not occur with GCC because it tracks line numbers rather than<br>
file offsets. Column numbers are tracked separately and are optional. I.e., in<br>
GCC a source location can be either a (line+column) tuple packed into 32 bits or<br>
(when the line number exceeds a certain threshold) a 32-bit line number.<br>
<br>
We are looking for an acceptable way of resolving the problem and propose the<br>
following approaches for discussion:<br>
1. Use 64 bits for source location tracking.<br>
2. Track until an overflow occurs after that make the lexer output<br>
the <invalid location> special value for all subsequent tokens.<br>
3. Implement an approach similar to the one used by GCC and start tracking line<br>
numbers instead of file offsets after a certain threshold. Resort to (2)<br>
when even line numbers overflow.<br>
4. (?) Detect the multiple inclusion pattern and track it differently (for now<br>
we don't have specific ideas on how to implement this)<br>
<br>
Is any of these approaches viable? What caveats should we expect? (we already<br>
know about static_asserts guarding the sizes of certain class fields which start<br>
failing in the first approach).<br>
<br>
Other suggestions are welcome.<u></u><u></u></p>
</blockquote>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">I don't think any of the above approaches are reasonable; they would all require fundamental restructuring of major parts of Clang, an efficiency or memory size hit for all other
users of Clang, or some combination of those.<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">Your code pattern seems unreasonable; including a multi-megabyte file thousands of times is not a good idea. Can you split out parts of MemMap.h into a separate header that is only
included once, and keep only the parts that actually change on repeated inclusion in MemMap.h itself?<u></u><u></u></p>
</div>
</div>
</div>
</div>
</div>
</div>
<p class="MsoNormal">_______________________________________________<br>
cfe-dev mailing list<br>
<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a><br>
<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev</a><u></u><u></u></p>
</blockquote>
</div>
</div>
</div>
</div>
</div>
</div>
</blockquote>
</div>
</div>
</div>
</div>
</blockquote></div>
_______________________________________________<br>
cfe-dev mailing list<br>
<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a><br>
<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev</a><br>
</blockquote></div>