<div dir="ltr"><div dir="ltr">On Thu, 4 Feb 2021 at 12:18, Keane, Erich <<a href="mailto:erich.keane@intel.com">erich.keane@intel.com</a>> wrote:<br></div><div class="gmail_quote"><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-US" style="overflow-wrap: break-word;">
<div class="gmail-m_9045172571673120284WordSection1">
<p class="MsoNormal">>Hmm, but will this actually be better than making SourceLocations 64 bits wide? Assuming we use the side table unconditionally, and assuming that most SourceLocations are stored in exactly one place (not true with templates, but often
the case otherwise), this means that each time we <u></u><u></u></p>
<p class="MsoNormal">>store a SourceLocation, we'll use up 96 bits of storage (32 bits for the SourceLocation and 64 bits for the table entry). That would be 50% worse than the simpler approach of making SourceLocations always be 64 bits wide. If we could use
regular 32 bit source locations for smaller values, and <u></u><u></u></p>
<p class="MsoNormal">>then switch to the side table once we get (say) half way through them, maybe that would be acceptable, but it would mean that we take a constant-factor memory usage (and, due to loss of locality, probably performance) hit for all translation
units larger than some threshold. Also, we'd still <u></u><u></u></p>
<p class="MsoNormal">>need full-width source locations for the spelling and expansion locations of the macro expansion SLocEntrys, so those are going to grow significantly larger -- and if we have a large number of macro expansions (such as in the boost case),
that'll be a big cost.<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">Hmm… yes, that is true. I would expect the SLocEntrys to just be the 64 bit larger version (and the macro expensions would use those instead), so those are equal to the 64 bit size. Spending a bit (the ½ thing above) to switch between
the two I think makes this a size regression for anything >1GB (I think we currently handle a 2GB TU). We’d end up taking a size-hit vs just using 64-bit source-locs somewhere between 1GB and 2GB, but I haven’t done the math yet.<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">>I don't think we're at the point where we can answer this question yet -- I don't think we sufficiently understand the problem. From what I've seen so far, it still seems likely to me that the problem is that we're allocating too much
of the SourceLocation address space for macro expansions ><u></u><u></u></p>
<p class="MsoNormal">>that don't need locations to be allocated for them, either due to an outright bug or a pathological case in the boost code.<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">I’m not sure how we omit those, from my look at the SourceManager it seems that all of those would be necessary (I don’t have a sufficiently good idea of how that works perhaps).</p></div></div></blockquote><div><br></div><div>I don't have any particular mechanism in mind. Theoretically, we could imagine (recursively) garbage-collecting SLocEntrys that are not referenced by any token, but actually doing it that way doesn't seem feasible.</div><div><br></div><div>One possible approach: set a checkpoint in the SLocEntry space when we begin processing a macro expansion or macro argument expansion. If we get to the end of the expansion, see if we actually created any tokens. If not, roll back to that checkpoint and recover all the SLocEntry space we allocated in the mean time. That actually seems like it would be pretty quick and straightforward to implement, now I come to think of it... hm. Worth a shot? (Of course, we don't know if that will help at all in the boost case, because we don't know what pattern of macro expansions they're using.)</div><div> </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-US" style="overflow-wrap: break-word;"><div class="gmail-m_9045172571673120284WordSection1">
<p class="MsoNormal">>I think it would be worth looking at a SourceManager dump from around the crash and analyzing what we're spending the source location space on -- is this a death by a million cuts situation, or are there some surprisingly large allocations
in there? If the former, are there patterns? Are we <u></u><u></u></p>
<p class="MsoNormal">>generating duplicate locations (eg, repeatedly expanding the same macros with the same arguments in a way we can reuse)? Are there lots of SLocEntries that are unreferenced by file locations (eg, macro expansions that ended up producing
no tokens)? There may be some smart way we can <u></u><u></u></p>
<p class="MsoNormal">>conditionally disable updateLocForMacroArgTokens (<a href="https://github.com/llvm/llvm-project/blob/master/clang/lib/Lex/TokenLexer.cpp#L1054" target="_blank">https://github.com/llvm/llvm-project/blob/master/clang/lib/Lex/TokenLexer.cpp#L1054</a>) or
make it lazy, so we don't assign full location information to preprocessor-tokens that are formed during macro expansion but don't result in any
<u></u><u></u></p>
<p class="MsoNormal">>real tokens -- or if there isn't a smart way to do that, maybe we can give boost a way of disabling location tracking for some of its macros.<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">I should be getting a reproducer that doesn’t have a lot of other stuff happening in the next day or so, so I’ll share that dumpwhen I can.<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">>I think it would also be worth pursuing the 64-bit source locations option (making SourceLocation larger, with no side table) as a configure-time flag, and fixing up all the places where we make the 32-bit assumption. If nothing else,
that'll let us measure the memory and compile time <u></u><u></u></p>
<p class="MsoNormal">>overhead of doubling our source location storage. I expect that the numbers we get back from that will make that option an immediate non-starter, but perhaps not -- in particular, if we could store a SourceLocation as a (FileID, offset)
pair, that would save us from doing binary searches to <u></u><u></u></p>
<p class="MsoNormal">>ocate the FileID for a given SourceLocation, which we do sometimes spend a non-trivial amount of our time doing, and it'd be worth measuring whether we can improve compile time at the cost of memory usage by doing that.<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">That sounds like a really good starting point. Configure-time I think is the right place to do this. FileID + Offset would be nice (as you say, that search looks really expensive), though would be likely still 64 bits (32 for fileid +
32).<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">>I'd also be interested in seeing whether we can add a flag to turn off tracking macro expansion locations entirely (so we'd only ever track spelling locations). That would result in some QoI loss, but it might also allow cases such as
this boost example to work without too much effort from us. <u></u><u></u></p>
<p class="MsoNormal">>It'd be interesting to see how much maintenance burden we think such a flag would add.<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">Hmm… I will have to think on that.<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<div style="border-right:none;border-bottom:none;border-left:none;border-top:1pt solid rgb(225,225,225);padding:3pt 0in 0in">
<p class="MsoNormal"><b>From:</b> Richard Smith <<a href="mailto:richard@metafoo.co.uk" target="_blank">richard@metafoo.co.uk</a>> <br>
<b>Sent:</b> Thursday, February 4, 2021 11:49 AM<br>
<b>To:</b> David Rector <<a href="mailto:davrecthreads@gmail.com" target="_blank">davrecthreads@gmail.com</a>><br>
<b>Cc:</b> Keane, Erich <<a href="mailto:erich.keane@intel.com" target="_blank">erich.keane@intel.com</a>>; clang developer list <<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a>><br>
<b>Subject:</b> Re: [cfe-dev] [RFC] Clang SourceLocation overflow<u></u><u></u></p>
</div>
<p class="MsoNormal"><u></u> <u></u></p>
<div>
<div>
<p class="MsoNormal">On Thu, 4 Feb 2021 at 10:50, David Rector <<a href="mailto:davrecthreads@gmail.com" target="_blank">davrecthreads@gmail.com</a>> wrote:<u></u><u></u></p>
</div>
<div>
<blockquote style="border-top:none;border-right:none;border-bottom:none;border-left:1pt solid rgb(204,204,204);padding:0in 0in 0in 6pt;margin-left:4.8pt;margin-right:0in">
<div>
<div>
<blockquote style="margin-top:5pt;margin-bottom:5pt">
<div>
<p class="MsoNormal">On Feb 4, 2021, at 1:24 PM, Keane, Erich <<a href="mailto:erich.keane@intel.com" target="_blank">erich.keane@intel.com</a>> wrote:<u></u><u></u></p>
</div>
<p class="MsoNormal"><u></u> <u></u></p>
<div>
<div>
<div>
<p class="MsoNormal">So 1 more thing:<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">Richard suggested one way about this was to switch from ‘negative numbers are loaded, positive are from files’ to something with a split.<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><br>
I believe based on my reading that we already DO that for SourceLocation. They are represented by an unsigned, and we have a “CurrentLoadedOffset” that is the split between the two. SO, we are already using as much room as we can for that.<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">The negative/positive discriminator Richard was mentioning seems to be the FileID, which is an index into 1 of 2 vectors (which contain the SLocEntrys, which store a list of macro expansions AND file + offset). I don’t believe we are running
into that limit however. SO, I don’t think that buys us much.<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">I’ve also been looking into replacing the bit for IsMacro vs IsFile. I believe that wouldn’t be too horrible, other than the need to access SourceManager. It WOULD be somewhat expensive (since we end up binary searching the files list
to get the FileID), but would buy us 2x the data we have now.<u></u><u></u></p>
</div>
</div>
</div>
</blockquote>
<blockquote style="margin-top:5pt;margin-bottom:5pt">
<div>
<div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
<div>
<div style="border-right:none;border-bottom:none;border-left:none;border-top:1pt solid rgb(225,225,225);padding:3pt 0in 0in">
<div>
<p class="MsoNormal"><b>From:</b> Keane, Erich <br>
<b>Sent:</b> Thursday, February 4, 2021 8:18 AM<br>
<b>To:</b> David Rector <<a href="mailto:davrecthreads@gmail.com" target="_blank">davrecthreads@gmail.com</a>><br>
<b>Cc:</b> Richard Smith <<a href="mailto:richard@metafoo.co.uk" target="_blank">richard@metafoo.co.uk</a>>; clang developer list <<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a>><br>
<b>Subject:</b> RE: [cfe-dev] [RFC] Clang SourceLocation overflow<u></u><u></u></p>
</div>
</div>
</div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">> So if our solution allowed passed around 64-bit SourceLocations in the preprocessing stage, and only converting them to 32-bit (interpreted as an index to 64-bit data where necessary, or all the time) when constructing an AST node, I
am pretty sure that would solve the problem, at least in this case.<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">Right… So what I envision is at the moment is:<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">PPSourceLocation: Does everything the current one does, so it is represented as a 63 bit location + 1 bit for macro/file discriminator.<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">ASTSourceLocation: This is ONLY a 32 bit unsigned index and contains no info nor functionality itself (besides conversions). However, ASTSourceLocation and a SourceManager (which contains the lookup vector) gets you the PPSourceLocation,
where we do all the work. <u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">Lex/Parse deal with PPSourceLocation. When it comes to store in the AST, we store the ASTSourceLocation and create an index in the SourceManager’s vector<uint64_t>. When the AST needs to do something with the location besides copying/storing
it, it does the conversion. The conversion from PPSourceLocation->ASTSourceLocation might be expensive (since we would presumably want to de-dupe the vector), but ASTSourceLocation->PPSourceLocation is just a vector-lookup.<u></u><u></u></p>
</div>
</div>
</div>
</blockquote>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">Agree. Maybe leave "ASTSourceLocation" as "SourceLocation" since the change will otherwise be invisible to the vast majority of users, who only interact with SourceLocations via AST nodes (I think).<u></u><u></u></p>
</div>
</div>
</div>
</blockquote>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">Yes, if we make this change, I agree we should call the small representation SourceLocation in order to minimize churn.<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">Hmm, but will this actually be better than making SourceLocations 64 bits wide? Assuming we use the side table unconditionally, and assuming that most SourceLocations are stored in exactly one place (not true with templates, but often the
case otherwise), this means that each time we store a SourceLocation, we'll use up 96 bits of storage (32 bits for the SourceLocation and 64 bits for the table entry). That would be 50% worse than the simpler approach of making SourceLocations always be 64
bits wide. If we could use regular 32 bit source locations for smaller values, and then switch to the side table once we get (say) half way through them, maybe that would be acceptable, but it would mean that we take a constant-factor memory usage (and, due
to loss of locality, probably performance) hit for all translation units larger than some threshold. Also, we'd still need full-width source locations for the spelling and expansion locations of the macro expansion SLocEntrys, so those are going to grow significantly
larger -- and if we have a large number of macro expansions (such as in the boost case), that'll be a big cost.<u></u><u></u></p>
</div>
<blockquote style="border-top:none;border-right:none;border-bottom:none;border-left:1pt solid rgb(204,204,204);padding:0in 0in 0in 6pt;margin-left:4.8pt;margin-right:0in">
<div>
<div>
<blockquote style="margin-top:5pt;margin-bottom:5pt">
<div>
<div>
<p class="MsoNormal">> Agree, but if the AST can get to be above that size (disregarding template instantiation nodes) that is the only answer I think. But somehow I doubt that is happening, or that any case in which that is happening is reasonable code.<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">At the moment, we aren’t getting to the 31^2-1 ast-source-locations, since obviously we would run out of offsets way before that would happen (assuming the de-dupe step above). But the above system buys us at least 2x*, but likely much
more. At the moment, this is likely enough to make me happy.<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">> I know preciously little about the modules implementation, but I would imagine we could store the vector<uint64_t> in the module as well - or store the big locations, either way.<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">I Hadn’t considered storing the array itself in the module header. I know about as much as you do (besides the ASTReader/ASTWriter interface), but my initial thought was to just convert to big-locations first, then store those. Hopefully
Richard can comment.<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">> Also, I wonder if that, when "Ran out of SourceLocations" is encountered with modules, the problem might be that template instantiation data within the modules are occupying large chunks of bytes, such that the problem could again be
resolved with an indexing solution that only ever refers to the SourceLocations of their patterns (i.e. reducing the number of unique indices to keep it under 2^31).<u></u><u></u></p>
</div>
</div>
</blockquote>
</div>
</div>
</blockquote>
<div>
<p class="MsoNormal">We don't have different SourceLocations for templates versus instantiations; it's not that. The problem on the modules side of things is that if you import N different modules, all of which textually include some large non-modular header,
then they all contain SourceLocations for the contents of that header. So the consumer of those modules ends up paying N x the cost in source location address space. And also N x the cost in memory usage for the declarations in that header and so on -- there,
running out of SourceLocations is not the problem, it's a symptom of the problem, and the problem is that too many copies of a large textual header got brought into the same compilation.<u></u><u></u></p>
</div>
<blockquote style="border-top:none;border-right:none;border-bottom:none;border-left:1pt solid rgb(204,204,204);padding:0in 0in 0in 6pt;margin-left:4.8pt;margin-right:0in">
<div>
<div>
<blockquote style="margin-top:5pt;margin-bottom:5pt">
<div>
<div>
<p class="MsoNormal">Agreed… I think the above/below indexing solution buys us quite a bit of headroom. It isn’t as good as true 64 bit source-locations, but the work to do this would make the eventual 64-bit switch quite a bit easier I would expect (since
it would just be removing the vector and making the conversion be a simple copy).<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
</div>
</blockquote>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">If I understood the Boost VMD example correctly (i.e. almost no AST nodes actually generated in that file, notwithstanding the huge buffers generated by macro expansions), the indexing approach will free up plenty of bits in all but the
most unusual cases, such that I would venture it will keep us from ever having to go 64 bit.<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">The only case that would not be solved by this, I think, is if a huge, non-templated AST were to be built, almost certainly by doing massive template-like stuff with the preprocessor, in which case the indexing would only buy us what you
say below: a multiple equal to the average token length in bytes (though even more to the extent there are also unused-in-the-AST macro expansions in the concatenation of buffers).<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">If such cases really need to be supported, then at that point I would think it reasonable to add an option to build clang with 64-bit SourceLocations used in the AST — i.e. the user would have to use a different clang binary to compile
that sort of stuff (is that the option that was proposed before?). But it might be worthwhile to put that off until we see actual cases in which that really is necessary, where we need more than 2^32 indices to uniquely identify source locations for all the
non-template-instantiated nodes in the AST.<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<p style="margin:0in;font-stretch:normal;min-height:17px"><span style="color:rgb(69,60,204)"> <u></u><u></u></span></p>
<div>
<p class="MsoNormal"><span style="color:rgb(69,60,204)">So I guess the open questions are:<u></u><u></u></span></p>
</div>
<ol style="margin-top:0in" start="1" type="1">
<li class="MsoNormal" style="color:rgb(69,60,204);font-stretch:normal">
Is this acceptable to **Richard**/etc?<u></u><u></u></li></ol>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">Bump.<u></u><u></u></p>
</div>
</div>
</div>
</blockquote>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">I don't think we're at the point where we can answer this question yet -- I don't think we sufficiently understand the problem. From what I've seen so far, it still seems likely to me that the problem is that we're allocating too much of
the SourceLocation address space for macro expansions that don't need locations to be allocated for them, either due to an outright bug or a pathological case in the boost code.<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">I think it would be worth looking at a SourceManager dump from around the crash and analyzing what we're spending the source location space on -- is this a death by a million cuts situation, or are there some surprisingly large allocations
in there? If the former, are there patterns? Are we generating duplicate locations (eg, repeatedly expanding the same macros with the same arguments in a way we can reuse)? Are there lots of SLocEntries that are unreferenced by file locations (eg, macro expansions
that ended up producing no tokens)? There may be some smart way we can conditionally disable updateLocForMacroArgTokens (<a href="https://github.com/llvm/llvm-project/blob/master/clang/lib/Lex/TokenLexer.cpp#L1054" target="_blank">https://github.com/llvm/llvm-project/blob/master/clang/lib/Lex/TokenLexer.cpp#L1054</a>)
or make it lazy, so we don't assign full location information to preprocessor-tokens that are formed during macro expansion but don't result in any real tokens -- or if there isn't a smart way to do that, maybe we can give boost a way of disabling location
tracking for some of its macros.<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">I think it would also be worth pursuing the 64-bit source locations option (making SourceLocation larger, with no side table) as a configure-time flag, and fixing up all the places where we make the 32-bit assumption. If nothing else, that'll
let us measure the memory and compile time overhead of doubling our source location storage. I expect that the numbers we get back from that will make that option an immediate non-starter, but perhaps not -- in particular, if we could store a SourceLocation
as a (FileID, offset) pair, that would save us from doing binary searches to locate the FileID for a given SourceLocation, which we do sometimes spend a non-trivial amount of our time doing, and it'd be worth measuring whether we can improve compile time at
the cost of memory usage by doing that.<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">I'd also be interested in seeing whether we can add a flag to turn off tracking macro expansion locations entirely (so we'd only ever track spelling locations). That would result in some QoI loss, but it might also allow cases such as this
boost example to work without too much effort from us. It'd be interesting to see how much maintenance burden we think such a flag would add.<u></u><u></u></p>
</div>
<blockquote style="border-top:none;border-right:none;border-bottom:none;border-left:1pt solid rgb(204,204,204);padding:0in 0in 0in 6pt;margin-left:4.8pt;margin-right:0in">
<div>
<div>
<ol style="margin-top:0in" start="1" type="1">
<li class="MsoNormal" style="color:rgb(69,60,204)">Does anyone want to help
<span style="font-family:"Segoe UI Emoji",sans-serif">😊</span><u></u><u></u></li></ol>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">While I’m not super set up for clang development at the moment, if, pending Richard’s thoughts, you wanted to start and when the task came into full focus it looked not-so-trivial, I would be happy to lend a hand, since I think we are on
the same page about what it seems needs to be done.<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<blockquote style="margin-top:5pt;margin-bottom:5pt">
<div>
<div>
<p class="MsoNormal">*My assumption that I think holds is that ASTSourceLocations would end up representing significantly less than half of the actual locations. In reality, I’m guessing this system buys us closer to 5x than it does 2x, simply because I’m
guessing the average identifier/token/etc is closer to 5 bytes than 2. Even a trivial example which is a ‘bad’ case (int foo(int i, int j, int k) { return i + j + k;}) Has 50 characters, but I believe ~18 source locations in the AST.<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
<div>
<div style="border-right:none;border-bottom:none;border-left:none;border-top:1pt solid rgb(225,225,225);padding:3pt 0in 0in">
<div>
<p class="MsoNormal"><b>From:</b> David Rector <<a href="mailto:davrecthreads@gmail.com" target="_blank">davrecthreads@gmail.com</a>> <br>
<b>Sent:</b> Thursday, February 4, 2021 7:57 AM<br>
<b>To:</b> Keane, Erich <<a href="mailto:erich.keane@intel.com" target="_blank">erich.keane@intel.com</a>><br>
<b>Cc:</b> Richard Smith <<a href="mailto:richard@metafoo.co.uk" target="_blank">richard@metafoo.co.uk</a>>; clang developer list <<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a>><br>
<b>Subject:</b> Re: [cfe-dev] [RFC] Clang SourceLocation overflow<u></u><u></u></p>
</div>
</div>
</div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
<div>
<p class="MsoNormal" style="margin-bottom:12pt"> <u></u><u></u></p>
<blockquote style="margin-top:5pt;margin-bottom:5pt">
<div>
<div>
<p class="MsoNormal">On Feb 4, 2021, at 10:23 AM, Keane, Erich <<a href="mailto:erich.keane@intel.com" target="_blank">erich.keane@intel.com</a>> wrote:<u></u><u></u></p>
</div>
</div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
<div>
<div>
<div>
<p class="MsoNormal">> but which don’t actually contain any SourceLocations *which end up in the AST* (since those are the only SourceLocations that really need to be 32 bit). <u></u><u></u></p>
</div>
</div>
<div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
</div>
<div>
<div>
<p class="MsoNormal">I think we still need to keep non-ast source locations around, since they are necessary to print the ‘expanded from macro’ notes. So even ones that aren’t in the AST get referenced anyway. So we would need some way of differentiating
the two. I wonder if we would be able to create a 64-bit “large source location” that converted to a “small source location” when added to the AST that used the indexing feature.<u></u><u></u></p>
</div>
</div>
<div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
</div>
<div>
<div>
<p class="MsoNormal">> how many unique SourceLocations are in the AST, in each of the various described cases in which a user runs out of SourceLocations? <u></u><u></u></p>
</div>
</div>
<div>
<div>
<p class="MsoNormal"><br>
This I do not have a good feel of unfortunately.<u></u><u></u></p>
</div>
</div>
</div>
</blockquote>
<div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
</div>
<div>
<div>
<p class="MsoNormal">I think in this case, the macros probably create very few if any AST nodes at all. Here’s a link to the problematic code, I think it is pure macro wizardry:<u></u><u></u></p>
</div>
</div>
<div>
<div>
<p class="MsoNormal"><a href="https://github.com/boostorg/vmd/blob/develop/test/test_doc_modifiers_return_type.cxx" target="_blank">https://github.com/boostorg/vmd/blob/develop/test/test_doc_modifiers_return_type.cxx</a><u></u><u></u></p>
</div>
</div>
<div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
</div>
<div>
<div>
<p class="MsoNormal">So if our solution allowed passed around 64-bit SourceLocations in the preprocessing stage, and only converting them to 32-bit (interpreted as an index to 64-bit data where necessary, or all the time) when constructing an AST node, I am
pretty sure that would solve the problem, at least in this case.<u></u><u></u></p>
</div>
</div>
<p class="MsoNormal" style="margin-bottom:12pt"> <u></u><u></u></p>
<blockquote style="margin-top:5pt;margin-bottom:5pt">
<div>
<div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
</div>
<div>
<div>
<p class="MsoNormal">>, the only long term solution is to allow the user to select 64-bit SourceLocations<u></u><u></u></p>
</div>
</div>
<div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
</div>
<div>
<div>
<p class="MsoNormal">I can’t think of any way to do this that doesn’t mean we have to have 2x of a number of AST nodes in the compiler. It feels like that would be a pretty massive inflation of our executable size, so I’m not sure what we’re willing to allow
for that. <u></u><u></u></p>
</div>
</div>
</div>
</blockquote>
<div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
</div>
<div>
<div>
<p class="MsoNormal">Agree, but if the AST can get to be above that size (disregarding template instantiation nodes) that is the only answer I think. But somehow I doubt that is happening, or that any case in which that is happening is reasonable code.<u></u><u></u></p>
</div>
</div>
<div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
</div>
<blockquote style="margin-top:5pt;margin-bottom:5pt">
<div>
<div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
</div>
<div>
<div>
<p class="MsoNormal">Another side-note: If we start using some sort of ‘index into a vector of real source locations’ thing, we would likely need to change how modules are emitted/retrieved, though presumably we could just have them contain the ‘large source
location’ listed above.<u></u><u></u></p>
</div>
</div>
</div>
</blockquote>
<div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
</div>
<div>
<div>
<p class="MsoNormal">I know preciously little about the modules implementation, but I would imagine we could store the vector<uint64_t> in the module as well - or store the big locations, either way.<u></u><u></u></p>
</div>
</div>
<div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
</div>
<div>
<div>
<p class="MsoNormal">Also, I wonder if that, when "Ran out of SourceLocations" is encountered with modules, the problem might be that template instantiation data within the modules are occupying large chunks of bytes, such that the problem could again be resolved
with an indexing solution that only ever refers to the SourceLocations of their patterns (i.e. reducing the number of unique indices to keep it under 2^31).<u></u><u></u></p>
</div>
</div>
<p class="MsoNormal" style="margin-bottom:12pt"> <u></u><u></u></p>
<blockquote style="margin-top:5pt;margin-bottom:5pt">
<div>
<div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
</div>
<div>
<div style="border-right:none;border-bottom:none;border-left:none;border-top:1pt solid rgb(225,225,225);padding:3pt 0in 0in">
<div>
<div>
<p class="MsoNormal"><b>From:</b> David Rector <<a href="mailto:davrecthreads@gmail.com" target="_blank">davrecthreads@gmail.com</a>> <br>
<b>Sent:</b> Thursday, February 4, 2021 7:12 AM<br>
<b>To:</b> Keane, Erich <<a href="mailto:erich.keane@intel.com" target="_blank">erich.keane@intel.com</a>><br>
<b>Cc:</b> Richard Smith <<a href="mailto:richard@metafoo.co.uk" target="_blank">richard@metafoo.co.uk</a>>; clang developer list <<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a>><br>
<b>Subject:</b> Re: [cfe-dev] [RFC] Clang SourceLocation overflow<u></u><u></u></p>
</div>
</div>
</div>
</div>
<div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
</div>
<div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
</div>
<div>
<div>
<p class="MsoNormal" style="margin-bottom:12pt"> <u></u><u></u></p>
</div>
<blockquote style="margin-top:5pt;margin-bottom:5pt">
<div>
<div>
<div>
<p class="MsoNormal">On Feb 4, 2021, at 9:34 AM, Keane, Erich <<a href="mailto:erich.keane@intel.com" target="_blank">erich.keane@intel.com</a>> wrote:<u></u><u></u></p>
</div>
</div>
</div>
<div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
</div>
<div>
<div>
<div>
<div>
<p class="MsoNormal">I don’t know if it is related to that, but I had the reporter run -dD and it crashed with these two at the end:<u></u><u></u></p>
</div>
</div>
</div>
<div>
<div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
</div>
</div>
<div>
<div>
<div>
<p class="MsoNormal"><span style="font-size:10pt;font-family:"Courier New"">#define TUPLE_IS_VALID_ARRAY_E (2,(3,4))</span><u></u><u></u></p>
</div>
</div>
</div>
<div>
<div>
<div>
<p class="MsoNormal"><span style="font-size:10pt;font-family:"Courier New"">#define TUPLE_IS_VALID_LIST_E (anydata,BOOST_PP</span><u></u><u></u></p>
</div>
</div>
</div>
<div>
<div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
</div>
</div>
<div>
<div>
<div>
<p class="MsoNormal">So presumably it is related to the boost-preprocessor that you mentioned, however from a quick googling of the source code, that macro is:<u></u><u></u></p>
</div>
</div>
</div>
<div>
<div>
<div>
<p class="MsoNormal"><span style="font-size:10pt;font-family:"Courier New"">#define TUPLE_IS_VALID_LIST_E (anydata,BOOST_PP_NIL)</span><u></u><u></u></p>
</div>
</div>
</div>
<div>
<div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
</div>
</div>
<div>
<div>
<div>
<p class="MsoNormal">So there isn’t any huge expansion here. That said, since this is just a header include FROM boost, I’d expect us to be able to compile it.<u></u><u></u></p>
</div>
</div>
</div>
</div>
</blockquote>
<div>
<div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
</div>
</div>
<div>
<div>
<div>
<p class="MsoNormal">Agreed, Boost probably isn’t doing unnecessary expansions.<u></u><u></u></p>
</div>
</div>
</div>
<div>
<div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
</div>
</div>
<div>
<div>
<div>
<p class="MsoNormal">However, it is possible it is doing a lot of nested expansions that are necessary for it to implement its logic, but which don’t actually contain any SourceLocations *which end up in the AST* (since those are the only SourceLocations that
really need to be 32 bit). <u></u><u></u></p>
</div>
</div>
</div>
<div>
<div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
</div>
</div>
<div>
<div>
<div>
<p class="MsoNormal">If these ultimately-unused expansions (e.g. expansions into arguments of other macros cannot be used in the AST) are added to the concatenation of buffers through which a SourceLocation must specify an offset, then there may be many more
bytes in the buffers than SourceLocations in the AST when dealing with extremely heavy macro usage such as with the Boost VMD/PP stuff.<u></u><u></u></p>
</div>
</div>
</div>
<div>
<div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
</div>
</div>
<div>
<div>
<div>
<p class="MsoNormal">Hard to wade through the Preprocessor/Lexer details to know if this is the case, so an easier question is just: how many unique SourceLocations are in the AST, in each of the various described cases in which a user runs out of SourceLocations?
If the number of AST-used SourceLocations exceeds 2^31, the only answer is 64-bit SourceLocations. If it significantly under that, an indexing solution would work, and might well give the best bang for the buck.<u></u><u></u></p>
</div>
</div>
</div>
<div>
<div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
</div>
</div>
<div>
<div>
<div>
<p class="MsoNormal">Tthe other cases mentioned in the thread you linked to are also worth considering. In the current Boost VMD case, the problem results from macro expansions, somehow. In the previous thread, the cause was extremely frequent use of large
unguarded #includes (but which might involve macro expansions too, or somehow otherwise result in large chunks of bytes never referenced via SourceLocations in the AST). <u></u><u></u></p>
</div>
</div>
</div>
<div>
<div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
</div>
</div>
<div>
<div>
<div>
<p class="MsoNormal">However someone else in that thread encountered problems while working on modules, which seems not to involve macros/preprocessor buffers at all, and so might be the most troubling case, and might indicate that even if macros are the problem
here, the only long term solution is to allow the user to select 64-bit SourceLocations — unless there are *also* large chunks of bytes in the imported-from-AST buffers that do not contain SourceLocations. So again would be interesting to know the number
of AST-used unique SourceLocations in those other cases, when we run out of them.<u></u><u></u></p>
</div>
</div>
</div>
<div>
<div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
</div>
</div>
<div>
<p class="MsoNormal" style="margin-bottom:12pt"> <u></u><u></u></p>
</div>
<blockquote style="margin-top:5pt;margin-bottom:5pt">
<div>
<div>
<div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
</div>
</div>
<div>
<div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
</div>
</div>
<div>
<div style="border-right:none;border-bottom:none;border-left:none;border-top:1pt solid rgb(225,225,225);padding:3pt 0in 0in">
<div>
<div>
<div>
<p class="MsoNormal"><b>From:</b> David Rector <<a href="mailto:davrecthreads@gmail.com" target="_blank">davrecthreads@gmail.com</a>> <br>
<b>Sent:</b> Wednesday, February 3, 2021 12:33 PM<br>
<b>To:</b> Keane, Erich <<a href="mailto:erich.keane@intel.com" target="_blank">erich.keane@intel.com</a>><br>
<b>Cc:</b> Richard Smith <<a href="mailto:richard@metafoo.co.uk" target="_blank">richard@metafoo.co.uk</a>>; clang developer list <<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a>><br>
<b>Subject:</b> Re: [cfe-dev] [RFC] Clang SourceLocation overflow<u></u><u></u></p>
</div>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
</div>
</div>
<div>
<div>
<div>
<p class="MsoNormal">Not sure if this helps but: I didn’t think about the boost file you were trying to compile at first, but now that I see it involves the boost preprocessor stuff, I bet the issue involves code somewhere that is written like <u></u><u></u></p>
</div>
</div>
</div>
<div>
<div>
<div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
</div>
</div>
<div>
<div>
<div>
<div>
<p class="MsoNormal">`<span style="font-family:Menlo,serif">BOOST_PP_IF(condition, HUGEMACROEXPANSION_A, HUGEMACROEXPANSION_B)</span>`<u></u><u></u></p>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<div>
<p class="MsoNormal">which should instead be written<u></u><u></u></p>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<div>
<p class="MsoNormal">`<span style="font-family:Menlo,serif">BOOST_PP_CAT(HUGEMACROEXPANSION, BOOST_PP_IF(0, _A, _B))</span>`<u></u><u></u></p>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<div>
<p class="MsoNormal">The former results in two huge expansions, one of which is thrown away, whereas the latter only results in one expansion.<u></u><u></u></p>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<div>
<p class="MsoNormal">I recall writing reasonably small but complex boost pp code that would take like 15 minutes just to preprocess, and now that I think about it, it might have resulted in the "ran out of source locations" a few times, before I finally figured
out to always write those conditionals the second way. <u></u><u></u></p>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<div>
<p class="MsoNormal">Not sure if that’s the issue, but it’s a place to look.<u></u><u></u></p>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<div>
<p class="MsoNormal">Dave<u></u><u></u></p>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
</div>
</div>
<div>
<div>
<div>
<p class="MsoNormal" style="margin-bottom:12pt"><u></u> <u></u></p>
</div>
</div>
<blockquote style="margin-top:5pt;margin-bottom:5pt">
<div>
<div>
<div>
<div>
<p class="MsoNormal">On Feb 3, 2021, at 3:10 PM, Keane, Erich <<a href="mailto:erich.keane@intel.com" target="_blank">erich.keane@intel.com</a>> wrote:<u></u><u></u></p>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
</div>
</div>
<div>
<div>
<div>
<div>
<div>
<p class="MsoNormal">Thanks for the response Richard! I’m working with my QA team to get a better reproducer to see if we can figure out what is the root-cause. It DOES include a couple of other files so I’m not sure all of what is entailed.<u></u><u></u></p>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<div>
<p class="MsoNormal">>There are also some relatively cheap things we could do to expand our capacity: we could remove the 'is macro location' bit without much effort (though this would slow down the current places that check the flag), which would double our
available source location capacity. <u></u><u></u></p>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<div>
<p class="MsoNormal">This seems like a useful and cheap thing to do, if find this is a legitimate issue, I’ll see if I can put a patch together to do this one.<u></u><u></u></p>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<div>
<p class="MsoNormal">>And we could allow the division between local and imported locations to be determined dynamically rather than fixing a 50/50 split, which would in practice be likely to double the available capacity again. But those would only be useful
if we're just a little over the limit; they wouldn't help if there's an asymptotic problem in our source location usage.<u></u><u></u></p>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<div>
<p class="MsoNormal">I don’t have a great idea on how to do this, or how we use this, but based on your description it seems worth-while. At least in non-module TUs I’d assume that the “imported-from-ast-file” is relatively rare. Also, I would think these
imports happen ‘first’, right? So we could figure out the split as soon as we’re done with imports and optimize our space.<u></u><u></u></p>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
</div>
</div>
</div>
<div style="border-right:none;border-bottom:none;border-left:none;border-top:1pt solid rgb(225,225,225);padding:3pt 0in 0in">
<div>
<div>
<div>
<div>
<p class="MsoNormal"><b>From:</b> Richard Smith <<a href="mailto:richard@metafoo.co.uk" target="_blank">richard@metafoo.co.uk</a>> <br>
<b>Sent:</b> Wednesday, February 3, 2021 11:54 AM<br>
<b>To:</b> Keane, Erich <<a href="mailto:erich.keane@intel.com" target="_blank">erich.keane@intel.com</a>><br>
<b>Cc:</b> David Rector <<a href="mailto:davrecthreads@gmail.com" target="_blank">davrecthreads@gmail.com</a>>; clang developer list <<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a>><br>
<b>Subject:</b> Re: [cfe-dev] [RFC] Clang SourceLocation overflow<u></u><u></u></p>
</div>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<div>
<div>
<div>
<p class="MsoNormal">On Tue, 2 Feb 2021 at 10:41, Keane, Erich 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>
</div>
</div>
</div>
<div>
<blockquote style="border-top:none;border-right:none;border-bottom:none;border-left:1pt solid rgb(204,204,204);padding:0in 0in 0in 6pt;margin:5pt 0in 5pt 4.8pt">
<div>
<div>
<div>
<div>
<div>
<div>
<p class="MsoNormal">Presumably, yes, we could hit that limit, and that limit is closer than we’d think.<u></u><u></u></p>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<div>
<p class="MsoNormal">Based on my understanding though, we currently stores the ‘offset’ into the buffer. If we were to go with your plan, we’d reduce the ‘SourceLocation’ space significantly, since we’d only have 1 value taken up by “ALongFunctionName” instead
of ~15? Or, at least, only 1 instead of 3 in ‘int’.<u></u><u></u></p>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<div>
<p class="MsoNormal">I presume that anything short of expanding the size of SourceLocation is going to be a temporary measure at best. <u></u><u></u></p>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<div>
<p class="MsoNormal">One consideration: What do we think about making SourceLocation have an underlying type of ‘bitfield’ that is a smaller size than 64 bits? Then at least we could make SourceRange be a packed value? So make SourceLocation be 40 bits (obviously
would still be 64 if stored directly), but at least we could pack 2 of them into 80 bits for any type that does some sort of bit-packing? <u></u><u></u></p>
</div>
</div>
</div>
</div>
</div>
</div>
</blockquote>
<div>
<div>
<div>
<div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<div>
<div>
<p class="MsoNormal">I doubt this would help in practice -- most of the storage in AST nodes tends to be SourceLocations and pointers.<u></u><u></u></p>
</div>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
</div>
</div>
</div>
</div>
<blockquote style="border-top:none;border-right:none;border-bottom:none;border-left:1pt solid rgb(204,204,204);padding:0in 0in 0in 6pt;margin:5pt 0in 5pt 4.8pt">
<div>
<div>
<div>
<div>
<div>
<div>
<p class="MsoNormal">Alternatively, what about making SourceRange (instead of 2 SourceLocation objects) be a SourceLocation + unsigned? It would make it a little smaller? Then we could do similar optimizations over time to the ASTNodes. That is, all of the
TypeLoc types could just store things like open/close parens as offsets-from-the-original rather than SourceLocations, then calculate them when needed? I would assume anything that required re-calculation would be acceptable, since SourceLocations are seemingly
quite rarely used (except for the few cases that Richard mentioned below).<u></u><u></u></p>
</div>
</div>
</div>
</div>
</div>
</div>
</blockquote>
<div>
<div>
<div>
<div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<div>
<div>
<p class="MsoNormal">That doesn't seem likely to work: we can't assume the beginning and end of a source range are close together in source location space (they could be in different SLocEntrys, which could be a long way apart from each other).<u></u><u></u></p>
</div>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
</div>
</div>
</div>
</div>
<blockquote style="border-top:none;border-right:none;border-bottom:none;border-left:1pt solid rgb(204,204,204);padding:0in 0in 0in 6pt;margin:5pt 0in 5pt 4.8pt">
<div>
<div>
<div>
<div>
<div>
<div>
<p class="MsoNormal">My preference is obviously to just make SourceLocation be uint64_t instead, but the impact on AST size is going to be significant. So I guess I’m hoping that Richard Smith can comment and help us figure out how much pain we are willing
to go through for this?<u></u><u></u></p>
</div>
</div>
</div>
</div>
</div>
</div>
</blockquote>
<div>
<div>
<div>
<div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<div>
<div>
<p class="MsoNormal">We could do that, and having a build-time selector for 32/64-bit SourceLocations seems like it might not impose a huge cost. But I think we should first try to gain some confidence that we're addressing the right problem -- running out
of source locations seems likely to indicate that there's something more fundamental wrong with the compilation. Currently we reserve one bit for an 'is macro location' flag, and divide the remaining addressable 2GB into a 1GB local region and a 1GB imported-from-AST-file
region. The resulting "1GB of preprocessed source (including all intermediate stages of macro expansion)" implementation limit does not seem especially restrictive to me, so the first thing I think we should find out is how we're actually hitting that limit
in the boost example. We can currently handle huge compilations without hitting the limit, so if a single header file can hit it, that seems indicative of a bug that's not just caused by the limit being too low.<u></u><u></u></p>
</div>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<div>
<div>
<p class="MsoNormal">One possible cause would be that a large header is included a *lot* and doesn't have a proper include guard. If that's the case, that seems like a problem that we should get fixed in boost -- or in Clang if it's a bug in include guard detection
-- because that will contribute to long compile times too. I expect there are other cases where source location address space gets wasted that we could drill down into.<u></u><u></u></p>
</div>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<div>
<div>
<div>
<p class="MsoNormal">If we find the problem is our location tracking for macro expansions (eg, pushing a large volume of tokens through deeply nested macro expansions), we could probably find a way to turn that off; there may even be ways we can intelligently
turn it off selectively in cases where we think the intermediary information is unlikely to be useful. We could also look into throwing away source location address space for macro expansions that ended up producing no tokens. But we need to understand the
nature of the problem first.<u></u><u></u></p>
</div>
</div>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<div>
<div>
<p class="MsoNormal">There are also some relatively cheap things we could do to expand our capacity: we could remove the 'is macro location' bit without much effort (though this would slow down the current places that check the flag), which would double our
available source location capacity. And we could allow the division between local and imported locations to be determined dynamically rather than fixing a 50/50 split, which would in practice be likely to double the available capacity again. But those would
only be useful if we're just a little over the limit; they wouldn't help if there's an asymptotic problem in our source location usage.<u></u><u></u></p>
</div>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
</div>
</div>
</div>
</div>
<blockquote style="border-top:none;border-right:none;border-bottom:none;border-left:1pt solid rgb(204,204,204);padding:0in 0in 0in 6pt;margin:5pt 0in 5pt 4.8pt">
<div>
<div>
<div>
<div style="border-right:none;border-bottom:none;border-left:none;border-top:1pt solid rgb(225,225,225);padding:3pt 0in 0in">
<div>
<div>
<div>
<div>
<p class="MsoNormal"><b>From:</b> David Rector <<a href="mailto:davrecthreads@gmail.com" target="_blank">davrecthreads@gmail.com</a>> <br>
<b>Sent:</b> Tuesday, February 2, 2021 10:28 AM<br>
<b>To:</b> Keane, Erich <<a href="mailto:erich.keane@intel.com" target="_blank">erich.keane@intel.com</a>><br>
<b>Cc:</b> clang developer list <<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a>><br>
<b>Subject:</b> Re: [cfe-dev] [RFC] Clang SourceLocation overflow<u></u><u></u></p>
</div>
</div>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<div>
<p class="MsoNormal">An additional consideration: if the files/buffers are so large that each SourceLocation cannot fit in 30 bits, does that imply that so many SourceLocations will be generated that the indices to them won’t be able to fit in 30 bits? That
may be the case now that I think of it, in which case the only solution really would be 64 bit SourceLocations, or something still more creative/costly.<u></u><u></u></p>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
</div>
</div>
</div>
</div>
<div>
<blockquote style="margin-top:5pt;margin-bottom:5pt">
<div>
<div>
<div>
<div>
<div>
<p class="MsoNormal">On Feb 2, 2021, at 11:53 AM, Keane, Erich <<a href="mailto:erich.keane@intel.com" target="_blank">erich.keane@intel.com</a>> wrote:<u></u><u></u></p>
</div>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<div>
<div>
<div>
<p class="MsoNormal">Huh, that is an interesting idea! The issue ends up being the callers of getOffset though, since it is a previate method.<u></u><u></u></p>
</div>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<div>
<div>
<p class="MsoNormal">That said, I wonder if the extra bit-use is just putting off the inevitable, and we should just use the vector in SourceManager for everything. My initial thought is that we could do away with the ‘IsMacro’ bit as well, and just encode
that in the vector too. It makes isFileID and isMacroID require the SourceManager as well, but I wonder if that is OK?<u></u><u></u></p>
</div>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
</div>
</div>
</div>
</div>
<div>
<div style="border-right:none;border-bottom:none;border-left:none;border-top:1pt solid rgb(225,225,225);padding:3pt 0in 0in">
<div>
<div>
<div>
<div>
<div>
<p class="MsoNormal"><b>From:</b> David Rector <<a href="mailto:davrecthreads@gmail.com" target="_blank">davrecthreads@gmail.com</a>> <br>
<b>Sent:</b> Tuesday, February 2, 2021 8:46 AM<br>
<b>To:</b> Keane, Erich <<a href="mailto:erich.keane@intel.com" target="_blank">erich.keane@intel.com</a>><br>
<b>Cc:</b> clang developer list <<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a>><br>
<b>Subject:</b> Re: [cfe-dev] [RFC] Clang SourceLocation overflow<u></u><u></u></p>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<div>
<div>
<div>
<p class="MsoNormal">A possible solution not mentioned: reserve a bit in SourceLocation for "IsBigLoc". When IsBigLoc = true, the remaining 30 bits of the "ID" field is not an offset but an index into a vector of uintptr_ts in SourceManager, each holding 64-bit
offset data.<u></u><u></u></p>
</div>
</div>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<div>
<div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
</div>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<div>
<div>
<div>
<p class="MsoNormal">Only a few changes to private methods and isolated details would be needed I think: e.g. method `unsigned SourceLocation::getOffset()` would become `uintptr_t SourceLocation::getOffset(const SourceManager &SM)`, and would dereference and
return the larger data when IsBigLoc. And more generally `unsigned` types should be changed to `uintptr_t` everywhere 32-bit encodings are currently assumed (mostly in SourceManager).<u></u><u></u></p>
</div>
</div>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<div>
<div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
</div>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<div>
<div>
<div>
<p class="MsoNormal">This might be easier and less intrusive than allowing 64-bit SourceLocations depending on a build flag, if I understand that proposal correctly, since that would require templating virtually everything with the SourceLocation type.<u></u><u></u></p>
</div>
</div>
</div>
</div>
</div>
</div>
<div>
<div>
<p class="MsoNormal" style="margin-bottom:12pt"><br>
<br>
<u></u><u></u></p>
</div>
<blockquote style="margin-top:5pt;margin-bottom:5pt">
<div>
<div>
<div>
<div>
<div>
<div>
<p class="MsoNormal">On Feb 2, 2021, at 9:21 AM, Keane, Erich 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>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<div>
<div>
<div>
<div>
<p class="MsoNormal">Original thread here: <a href="https://lists.llvm.org/pipermail/cfe-dev/2019-October/063459.html" target="_blank"><span style="color:rgb(5,99,193)">https://lists.llvm.org/pipermail/cfe-dev/2019-October/063459.html</span></a><u></u><u></u></p>
</div>
</div>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<div>
<div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
</div>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<div>
<div>
<div>
<p class="MsoNormal">I’m bringing this back up since we have a reproduction of this in Boost now. We haven’t finished analyzing what boost is doing, but simply doing an include of:<u></u><u></u></p>
</div>
</div>
</div>
</div>
</div>
</div>
<pre>#include <libs/vmd/test/test_doc_modifiers_return_type.cxx><u></u><u></u></pre>
<div>
<div>
<div>
<div>
<div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
</div>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<div>
<div>
<div>
<p class="MsoNormal">Now causes us to run out of source locations, hitting:<u></u><u></u></p>
</div>
</div>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<div>
<div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
</div>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<div>
<div>
<div>
<p class="MsoNormal">/llvm/clang/lib/Basic/SourceManager.cpp:680: clang::SourceLocation clang::SourceManager::createExpansionLocImpl(const clang::SrcMgr::ExpansionInfo &, unsigned int, int, unsigned int): Assertion `NextLocalOffset + TokLength + 1 > NextLocalOffset
&& NextLocalOffset + TokLength + 1 <= CurrentLoadedOffset && "Ran out of source locations!"' failed.<u></u><u></u></p>
</div>
</div>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<div>
<div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
</div>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<div>
<div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
</div>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<div>
<div>
<div>
<p class="MsoNormal">From the last discussion, it seems that increasing our source-location size isn’t really acceptable due to how much it is stored in the AST( Multiple times per node), and giving up on location isn’t viable either. Additionally, the source
location/source manager layout is quite complex and I don’t quite understand it yet, so I don’t have a good way of suggesting an alternative.<u></u><u></u></p>
</div>
</div>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<div>
<div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
</div>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<div>
<div>
<div>
<p class="MsoNormal">SO, I’d like to re-start the discussion into this, we need to find a way to make our compiler able to support more source-locations, as I can’t imagine this is going to be the only time we run into this.<u></u><u></u></p>
</div>
</div>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<div>
<div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
</div>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<div>
<div>
<div>
<p class="MsoNormal">-Erihc <u></u><u></u></p>
</div>
</div>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<div>
<div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
</div>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<div>
<div>
<div>
<p class="MsoNormal">>>>>>>>>>>>>>>>>>>>> <u></u><u></u></p>
</div>
</div>
</div>
</div>
</div>
</div>
<pre>Hi Matt.<u></u><u></u></pre>
<pre> <u></u><u></u></pre>
<pre>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></pre>
<pre> <u></u><u></u></pre>
<pre>Thanks,<u></u><u></u></pre>
<pre>Christof<u></u><u></u></pre>
<pre> <u></u><u></u></pre>
<pre>From: Matt Asplund <<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" target="_blank"><span style="color:rgb(5,99,193)">mwasplund at gmail.com</span></a>><u></u><u></u></pre>
<pre>Sent: 10 October 2019 14:09<u></u><u></u></pre>
<pre>To: Christof Douma <<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" target="_blank"><span style="color:rgb(5,99,193)">Christof.Douma at arm.com</span></a>><u></u><u></u></pre>
<pre>Cc: Richard Smith <<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" target="_blank"><span style="color:rgb(5,99,193)">richard at metafoo.co.uk</span></a>>; Mikhail Maltsev <<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" target="_blank"><span style="color:rgb(5,99,193)">Mikhail.Maltsev at arm.com</span></a>>; Clang Dev <<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" target="_blank"><span style="color:rgb(5,99,193)">cfe-dev at lists.llvm.org</span></a>>; nd <<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" target="_blank"><span style="color:rgb(5,99,193)">nd at arm.com</span></a>><u></u><u></u></pre>
<pre>Subject: Re: [cfe-dev] [RFC] Clang SourceLocation overflow<u></u><u></u></pre>
<pre> <u></u><u></u></pre>
<pre>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></pre>
<pre> <u></u><u></u></pre>
<pre>-Matt<u></u><u></u></pre>
<pre> <u></u><u></u></pre>
<pre>On Thu, Oct 10, 2019, 2:13 AM Christof Douma <<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" target="_blank"><span style="color:rgb(5,99,193)">Christof.Douma at arm.com</span></a><mailto:<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" target="_blank"><span style="color:rgb(5,99,193)">Christof.Douma at arm.com</span></a>>> wrote:<u></u><u></u></pre>
<pre>Hi Richard.<u></u><u></u></pre>
<pre> <u></u><u></u></pre>
<pre>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></pre>
<pre> <u></u><u></u></pre>
<pre>If there are people that have already done some experiments on the size impact of 64-bits location pointers, we’re welcome your insights. @Matt Asplund<mailto:<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" target="_blank"><span style="color:rgb(5,99,193)">mwasplund at gmail.com</span></a>> I believe you said that you’ve done some prototyping, is there something you can share?<u></u><u></u></pre>
<pre> <u></u><u></u></pre>
<pre>Thanks,<u></u><u></u></pre>
<pre>Christof<u></u><u></u></pre>
<pre> <u></u><u></u></pre>
<pre>From: Richard Smith <<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" target="_blank"><span style="color:rgb(5,99,193)">richard at metafoo.co.uk</span></a><mailto:<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" target="_blank"><span style="color:rgb(5,99,193)">richard at metafoo.co.uk</span></a>>><u></u><u></u></pre>
<pre>Sent: 08 October 2019 19:53<u></u><u></u></pre>
<pre>To: Christof Douma <<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" target="_blank"><span style="color:rgb(5,99,193)">Christof.Douma at arm.com</span></a><mailto:<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" target="_blank"><span style="color:rgb(5,99,193)">Christof.Douma at arm.com</span></a>>><u></u><u></u></pre>
<pre>Cc: Mikhail Maltsev <<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" target="_blank"><span style="color:rgb(5,99,193)">Mikhail.Maltsev at arm.com</span></a><mailto:<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" target="_blank"><span style="color:rgb(5,99,193)">Mikhail.Maltsev at arm.com</span></a>>>; Clang Dev <<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" target="_blank"><span style="color:rgb(5,99,193)">cfe-dev at lists.llvm.org</span></a><mailto:<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" target="_blank"><span style="color:rgb(5,99,193)">cfe-dev at lists.llvm.org</span></a>>>; nd <<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" target="_blank"><span style="color:rgb(5,99,193)">nd at arm.com</span></a><mailto:<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" target="_blank"><span style="color:rgb(5,99,193)">nd at arm.com</span></a>>><u></u><u></u></pre>
<pre>Subject: Re: [cfe-dev] [RFC] Clang SourceLocation overflow<u></u><u></u></pre>
<pre> <u></u><u></u></pre>
<pre>On Tue, 8 Oct 2019, 10:42 Christof Douma via cfe-dev, <<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" target="_blank"><span style="color:rgb(5,99,193)">cfe-dev at lists.llvm.org</span></a><mailto:<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" target="_blank"><span style="color:rgb(5,99,193)">cfe-dev at lists.llvm.org</span></a>>> wrote:<u></u><u></u></pre>
<pre>Hi Richard, Paul and other.<u></u><u></u></pre>
<pre> <u></u><u></u></pre>
<pre>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></pre>
<pre> <u></u><u></u></pre>
<pre>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></pre>
<pre> <u></u><u></u></pre>
<pre>Hope that gave some more background of where this question comes from.<u></u><u></u></pre>
<pre> <u></u><u></u></pre>
<pre>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></pre>
<pre>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></pre>
<pre>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></pre>
<pre> <u></u><u></u></pre>
<pre>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></pre>
<pre>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></pre>
<pre>Thanks,<u></u><u></u></pre>
<pre>Christof<u></u><u></u></pre>
<pre> <u></u><u></u></pre>
<pre>From: cfe-dev <<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" target="_blank"><span style="color:rgb(5,99,193)">cfe-dev-bounces at lists.llvm.org</span></a><mailto:<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" target="_blank"><span style="color:rgb(5,99,193)">cfe-dev-bounces at lists.llvm.org</span></a>>> On Behalf Of Richard Smith via cfe-dev<u></u><u></u></pre>
<pre>Sent: 07 October 2019 20:36<u></u><u></u></pre>
<pre>To: Mikhail Maltsev <<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" target="_blank"><span style="color:rgb(5,99,193)">Mikhail.Maltsev at arm.com</span></a><mailto:<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" target="_blank"><span style="color:rgb(5,99,193)">Mikhail.Maltsev at arm.com</span></a>>><u></u><u></u></pre>
<pre>Cc: nd <<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" target="_blank"><span style="color:rgb(5,99,193)">nd at arm.com</span></a><mailto:<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" target="_blank"><span style="color:rgb(5,99,193)">nd at arm.com</span></a>>>; <a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" target="_blank"><span style="color:rgb(5,99,193)">cfe-dev at lists.llvm.org</span></a><mailto:<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" target="_blank"><span style="color:rgb(5,99,193)">cfe-dev at lists.llvm.org</span></a>><u></u><u></u></pre>
<pre>Subject: Re: [cfe-dev] [RFC] Clang SourceLocation overflow<u></u><u></u></pre>
<pre> <u></u><u></u></pre>
<pre>On Wed, 2 Oct 2019 at 09:26, Mikhail Maltsev via cfe-dev <<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" target="_blank"><span style="color:rgb(5,99,193)">cfe-dev at lists.llvm.org</span></a><mailto:<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" target="_blank"><span style="color:rgb(5,99,193)">cfe-dev at lists.llvm.org</span></a>>> wrote:<u></u><u></u></pre>
<pre>Hi all,<u></u><u></u></pre>
<pre> <u></u><u></u></pre>
<pre>We are experiencing a problem with Clang SourceLocation overflow.<u></u><u></u></pre>
<pre>Currently source locations are 32-bit values, one bit is a flag, which gives<u></u><u></u></pre>
<pre>a source location space of 2^31 characters.<u></u><u></u></pre>
<pre> <u></u><u></u></pre>
<pre>When the Clang lexer processes an #include directive it reserves the total size<u></u><u></u></pre>
<pre>of the file being included in the source location space. An overflow can occur<u></u><u></u></pre>
<pre>if a large file (which does not have include guards by design) is included many<u></u><u></u></pre>
<pre>times into a single TU.<u></u><u></u></pre>
<pre> <u></u><u></u></pre>
<pre>The pattern of including a file multiple times is for example required by<u></u><u></u></pre>
<pre>the AUTOSAR standard [1], which is widely used in the automotive industry.<u></u><u></u></pre>
<pre>Specifically the pattern is described in the Specification of Memory Mapping [2]:<u></u><u></u></pre>
<pre> <u></u><u></u></pre>
<pre>Section 8.2.1, MEMMAP003:<u></u><u></u></pre>
<pre>"The start and stop symbols for section control are configured with section<u></u><u></u></pre>
<pre>identifiers defined in MemMap.h [...] For instance:<u></u><u></u></pre>
<pre> <u></u><u></u></pre>
<pre>#define EEP_START_SEC_VAR_16BIT<u></u><u></u></pre>
<pre>#include "MemMap.h"<u></u><u></u></pre>
<pre>static uint16 EepTimer;<u></u><u></u></pre>
<pre>static uint16 EepRemainingBytes;<u></u><u></u></pre>
<pre>#define EEP_STOP_SEC_VAR_16BIT<u></u><u></u></pre>
<pre>#include "MemMap.h""<u></u><u></u></pre>
<pre> <u></u><u></u></pre>
<pre>Section 8.2.2, MEMMAP005:<u></u><u></u></pre>
<pre>"The file MemMap.h shall provide a mechanism to select different code, variable<u></u><u></u></pre>
<pre>or constant sections by checking the definition of the module specific memory<u></u><u></u></pre>
<pre>allocation key words for starting a section [...]"<u></u><u></u></pre>
<pre> <u></u><u></u></pre>
<pre>In practice MemMap.h can reach several MBs and can be included several thousand<u></u><u></u></pre>
<pre>times causing an overflow in the source location space.<u></u><u></u></pre>
<pre> <u></u><u></u></pre>
<pre>The problem does not occur with GCC because it tracks line numbers rather than<u></u><u></u></pre>
<pre>file offsets. Column numbers are tracked separately and are optional. I.e., in<u></u><u></u></pre>
<pre>GCC a source location can be either a (line+column) tuple packed into 32 bits or<u></u><u></u></pre>
<pre>(when the line number exceeds a certain threshold) a 32-bit line number.<u></u><u></u></pre>
<pre> <u></u><u></u></pre>
<pre>We are looking for an acceptable way of resolving the problem and propose the<u></u><u></u></pre>
<pre>following approaches for discussion:<u></u><u></u></pre>
<pre>1. Use 64 bits for source location tracking.<u></u><u></u></pre>
<pre>2. Track until an overflow occurs after that make the lexer output<u></u><u></u></pre>
<pre> the <invalid location> special value for all subsequent tokens.<u></u><u></u></pre>
<pre>3. Implement an approach similar to the one used by GCC and start tracking line<u></u><u></u></pre>
<pre> numbers instead of file offsets after a certain threshold. Resort to (2)<u></u><u></u></pre>
<pre> when even line numbers overflow.<u></u><u></u></pre>
<pre>4. (?) Detect the multiple inclusion pattern and track it differently (for now<u></u><u></u></pre>
<pre> we don't have specific ideas on how to implement this)<u></u><u></u></pre>
<pre> <u></u><u></u></pre>
<pre>Is any of these approaches viable? What caveats should we expect? (we already<u></u><u></u></pre>
<pre>know about static_asserts guarding the sizes of certain class fields which start<u></u><u></u></pre>
<pre>failing in the first approach).<u></u><u></u></pre>
<pre> <u></u><u></u></pre>
<pre>Other suggestions are welcome.<u></u><u></u></pre>
<pre> <u></u><u></u></pre>
<pre>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></pre>
<pre> <u></u><u></u></pre>
<pre>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></pre>
<pre>_______________________________________________<u></u><u></u></pre>
<pre>cfe-dev mailing list<u></u><u></u></pre>
<pre><a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" target="_blank"><span style="color:rgb(5,99,193)">cfe-dev at lists.llvm.org</span></a><mailto:<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" target="_blank"><span style="color:rgb(5,99,193)">cfe-dev at lists.llvm.org</span></a>><u></u><u></u></pre>
<pre><a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" target="_blank"><span style="color:rgb(5,99,193)">https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev</span></a><u></u><u></u></pre>
<pre>-------------- next part --------------<u></u><u></u></pre>
<pre>An HTML attachment was scrubbed...<u></u><u></u></pre>
<pre>URL: <<a href="http://lists.llvm.org/pipermail/cfe-dev/attachments/20191010/bc5ff8cd/attachment.html" target="_blank"><span style="color:rgb(5,99,193)">http://lists.llvm.org/pipermail/cfe-dev/attachments/20191010/bc5ff8cd/attachment.html</span></a>><u></u><u></u></pre>
<div>
<div>
<div>
<div>
<div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
</div>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<div>
<div>
<p class="MsoNormal"><span style="font-size:9pt;font-family:Helvetica,sans-serif">_______________________________________________<br>
cfe-dev mailing list<br>
</span><a href="mailto:cfe-dev@lists.llvm.org" target="_blank"><span style="font-size:9pt;font-family:Helvetica,sans-serif;color:rgb(5,99,193)">cfe-dev@lists.llvm.org</span></a><span style="font-size:9pt;font-family:Helvetica,sans-serif"><br>
</span><a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" target="_blank"><span style="font-size:9pt;font-family:Helvetica,sans-serif;color:rgb(5,99,193)">https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev</span></a><u></u><u></u></p>
</div>
</div>
</div>
</div>
</div>
</div>
</blockquote>
</div>
</div>
</blockquote>
</div>
<div>
<div>
<div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
</div>
</div>
</div>
</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>
</div>
</div>
</div>
</div>
</blockquote>
</div>
</div>
</div>
</blockquote>
</div>
</div>
</div>
</div>
</blockquote>
</div>
</div>
</blockquote>
</div>
</div>
</blockquote>
</div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
</blockquote>
</div>
</div>
</div>
</div>
</blockquote></div></div>