<html><body><p><font size="2">Thanks David and Michael for the clarification. </font><br><br><font size="2">I think I understand the rational behind those checks in delinearization now.</font><br><br><tt><font size="2">> Some other languages have stronger guarantees about their array dimensions accesses being in range. But this being a flat C array, there is nothing out-of-bounds going on.</font></tt><br><br><font size="2">Hmm...perhaps some information about these guarantees can come from the IR that the front-ends produce. I'm wondering if the 'inrange' keyword (or maybe a new keyword) can be used for this purpose? </font><br><br><tt><font size="2">> Think of an array A[2][2] and two accesses:<br>> A[1][0] = value;<br>> use(A[0][2]);<br>> The subscript of the second is out-of-range and will be evaluated to<br>> the same address as A[1][0]. A dependence-analysis that assumes that<br>> different subscripts means different addresses will not catch the<br>> dependence between the two statements.</font></tt><br><br><font size="2">I had a discussion about this syntax with some C standard experts and there seems to be disagreements on whether an out-of-bound access with respect to an individual dimension is defined behaviour or not. I do not mean to start that discussion here, especially because there may be code in the field relying on certain interpretation of the standard, but just want to mention that option-control maybe a good way to deal with complications like this.</font><br><br><font size="2">The debug option I had in mind would leave the checks in place by default. I do not intend to decrease the test coverage either. Hopefully the option can actually help us increase test coverage for cases that are currently failing to produce accurate dependence vectors due to the validity conditions being unknown at compile-time. I'll post a patch for review soon.</font><br><br><font size="2">Bardia Mahjour<br>Compiler Optimizations<br>Hydra Squad Lead<br>IBM Toronto Software Lab<br>bmahjour@ca.ibm.com (905) 413-2336<br><br></font><br><br><img width="16" height="16" src="cid:1__=8FBB0E90DFE665378f9e8a93df938690918c8FB@" border="0" alt="Inactive hide details for David Green ---2019/05/22 06:01:31 PM---Hello Yes, I agree that the SCEV cannot be simplified. Is my"><font size="2" color="#424282">David Green ---2019/05/22 06:01:31 PM---Hello Yes, I agree that the SCEV cannot be simplified. Is my understanding correct that it is passe</font><br><br><font size="2" color="#5F5F5F">From: </font><font size="2">David Green <David.Green@arm.com></font><br><font size="2" color="#5F5F5F">To: </font><font size="2">Bardia Mahjour <bmahjour@ca.ibm.com></font><br><font size="2" color="#5F5F5F">Cc: </font><font size="2">"Doerfert, Johannes" <jdoerfert@anl.gov>, "llvm-dev@lists.llvm.org" <llvm-dev@lists.llvm.org>, nd <nd@arm.com></font><br><font size="2" color="#5F5F5F">Date: </font><font size="2">2019/05/22 06:01 PM</font><br><font size="2" color="#5F5F5F">Subject: </font><font size="2">[EXTERNAL] Re: Re: [llvm-dev] Delinearization validity checks in DependenceAnalysis</font><br><hr width="100%" size="2" align="left" noshade style="color:#8091A5; "><br><br><br><tt><font size="2">Hello<br><br>Yes, I agree that the SCEV cannot be simplified. Is my understanding correct that it is passed to a function like "isKnownNegative"? Which could still be able to prove is always true.<br><br>The delinearisation may be valid, depending on exactly how you define delinearisation (under what conditions it should be giving results). It would be invalid for DA to return a dependency of [0 >] though. The code is perfectly valid from the C or llvm-ir specifications and we can't change the meaning of it just because we don't like it :) One of the observable effects of the program is the last values written into A, and unroll-and-jam will reorder the stores if it does not understand it is unsafe, leading to different output. i.e a mis-compile. C isn't very friendly to us here, and llvm is linearising the access. Some other languages have stronger guarantees about their array dimensions accesses being in range. But this being a flat C array, there is nothing out-of-bounds going on.<br><br>I'm not sure about just disabling the checks, at least without some other way of keeping the results correct. Having a debug option probably wouldn't hurt, but I'm not sure about turning it on for tests if it reduces the test coverage. Other people may have different opinions though.<br><br>Any improvements to DA in general would be excellent, and it has a lot of room for improvement.<br>Dave<br><br><br><br><br><br>From: Bardia Mahjour<br>Sent: 22 May 2019 18:09<br>To: David Green<br>Cc: Doerfert, Johannes; llvm-dev@lists.llvm.org; nd<br>Subject: Re: Re: [llvm-dev] Delinearization validity checks in DependenceAnalysis<br> <br><br>Hi David,<br><br>Sorry for my delayed response.<br><br>> There are already some function like isKnownPredicate that attempt to deal with some of this is DA. Can we extend the other for these cases? Perhaps with some extra information that we have that SCEV would in general not know, or do something extra that it would not compute. Or ideally can we just improve SCEV to get this right?<br><br>It looks like SCEV already tries to simplify identically sign and zero extended terms in an expression. However expressions that contain mix of sex and zex like this: `(-3 + (sext i32 %m to i64) + (-1 * (zext i32 %m to<br>> i64))<nsw>)` cannot be simplified unless we can assume that %m is never negative. Although in practice %m will not be negative in most cases, the compiler cannot prove that at compile-time so I don't think we can "improve" SCEV to catch these cases.<br><br><br>> So this loop:<br>> void test(int * A, signed N, int M)<br>> {<br>> for(int i = 1; i < N; i+=1) {<br>> for(int j = 0; j < M; j+=1) {<br>> A[i*M + j] = 2;<br>> A[i*M + j - 1] = 4;<br>> }<br>> }<br>> }<br>> Needs to not get the dependency vector [0 >], otherwise unroll and jam might think it was safe to reorder (unroll and jam) and miscompile the code.<br><br>Without the delinearization validity checks, the access functions A[i*M + j] and A[i*M + j - 1] would get delinearized as follows:<br><br>SrcSCEV = {{((4 * %M) + %A)<nsw>,+,(4 * %M)}<%for.body>,+,4}<%for.body4><br>DstSCEV = {{(-4 + (4 * %M) + %A),+,(4 * %M)}<%for.body>,+,4}<%for.body4><br><br>SrcSubscripts: {1,+,1}<%for.body>{0,+,1}<%for.body4><br>DstSubscripts: {1,+,1}<%for.body>{-1,+,1}<%for.body4> delinearized<br>subscript 0<br>src = {1,+,1}<%for.body><br>dst = {1,+,1}<%for.body><br>class = 1<br>loops = {1}<br>subscript 1<br>src = {0,+,1}<%for.body4><br>dst = {-1,+,1}<%for.body4><br>class = 1<br>loops = {2}<br>Separable = {0 1}<br>Coupled = {}<br><br>Why is this not a valid delinearization? The fact that the {-1,+,1} subscript goes out of bounds with respect to the second dimension is a problem that originates from the user code (and can be fixed by initializing j to 1 instead of 0). It is not a problem introduced by delinearization. Nevertheless delinearization validity checks fail because they find that the subscript in the second dimension could be negative and think that we didn't delinearize correctly.<br><br>Do you have any objections to adding an option to disable these checks, at least for the test cases where we know the delinearization would be correct, but cannot be guaranteed by the compiler?<br><br>Do we still need the validity step in the delinearization algorithm, if we could guarantee that the array size parameters collected (guessed) in the first step of the algorithm are in fact the correct array dimensions?<br><br><br><br>David Green ---2019/05/16 03:50:55 PM---Hello Under the proviso that it's been a while since I looked into any of these things...<br><br>From: David Green <David.Green@arm.com><br>To: "Doerfert, Johannes" <jdoerfert@anl.gov>, Bardia Mahjour <bmahjour@ca.ibm.com><br>Cc: "llvm-dev@lists.llvm.org" <llvm-dev@lists.llvm.org>, nd <nd@arm.com><br>Date: 2019/05/16 03:50 PM<br>Subject: [EXTERNAL] Re: [llvm-dev] Delinearization validity checks in DependenceAnalysis<br><br><br><br><br><br>Hello<br><br>Under the proviso that it's been a while since I looked into any of these things...<br><br>On 05/15, Bardia Mahjour via llvm-dev wrote:<br>> I also get correct results for my example (for a 64-bit target) if the upper<br>> bounds are changed to unsigned. The reason is simply because clang zero-extends<br>> `m` for address calculations but sign-extends it for the loop upper bound. This<br>> prevents SCEV from canceling out the 'm' term from the difference expression<br>> that looks like `(-3 + (sext i32 %m to i64) + (-1 * (zext i32 %m to<br>> i64))<nsw>)`. While we cannot reduce expressions of this form in general, it<br>> does pose a sever limitation for the vast majority of cases where the loop<br>> upper bound is non-negative.<br>><br><br>There are already some function like isKnownPredicate that attempt to deal with some of this is DA. Can we extend the other for these cases? Perhaps with some extra information that we have that SCEV would in general not know, or do something extra that it would not compute. Or ideally can we just improve SCEV to get this right?<br><br>> Regarding your example, I'm not sure I fully understand the concern and I<br>> would appreciate it if you could clarify that for me. My understanding is<br>> that if the intended shape of 'a' is in fact 'a[n][m]' then the example, as<br>> provided, has an out-of-bound access to start with. To avoid this out-bound<br>> access one would need to change the upper bound of the j-loop to be 'm-2'.<br>> Interestingly, if we do that, the current validity checks start to pass and<br>> we get [0 2] as the dependence vector. Here's a slightly modified version<br>> of your example that I tried:<br>><br>> ```<br>> typedef unsigned long long TT;<br>> void foo(TT n, TT m, int *a) {<br>> for (TT i = 0; i < n; ++i)<br>> for (TT j = 0; j < m-2; ++j) {<br>> a[i*m + j] = a[i*m + j+2];<br>> }<br>> }<br>> ```<br><br>Sure, but is wouldn't be invalid from C to use the "for (TT j = 0; j < m; ++j)" bound, so long as the size of the object passed through to "a" was large enough that it didn't overflow. We don't have enough information to prove this won't happen, so unfortunately we need to be a conservative.<br><br>So this loop:<br><br>void test(int * A, signed N, int M)<br>{<br> for(int i = 1; i < N; i+=1) {<br> for(int j = 0; j < M; j+=1) {<br> A[i*M + j] = 2;<br> A[i*M + j - 1] = 4;<br> }<br> }<br>}<br><br>Needs to not get the dependency vector [0 >], otherwise unroll and jam might think it was safe to reorder (unroll and jam) and miscompile the code.<br><br>><br>> If the concern is that the actual intended shape of the array may have been<br>> something other than a[n][m], and that we are indexing it such that the<br>> accesses are in-bound with respect to the memory of the whole array but not<br>> with respect to individual dimensions, then I'm not sure we can reason<br>> about *any* delinearization statically (except for the limited cases where<br>> the bounds are compile-time known).<br>><br>> Am I misunderstanding the issue?<br>><br>> Bardia Mahjour<br>> Compiler Optimizations<br>> Hydra Squad Lead<br>> IBM Toronto Software Lab<br>> bmahjour@ca.ibm.com (905) 413-2336<br>><br>><br>><br>><br>><br>> From: David Green <David.Green@arm.com><br>> To: "llvm-dev@lists.llvm.org" <llvm-dev@lists.llvm.org>, Bardia<br>> Mahjour <bmahjour@ca.ibm.com><br>> Cc: nd <nd@arm.com><br>> Date: 2019/05/14 02:50 PM<br>> Subject: [EXTERNAL] Re: [llvm-dev] Delinearization validity checks in<br>> DependenceAnalysis<br>><br>><br>><br>> Hello<br>><br>> Interestingly, the example you provide works for me so long as either it's<br>> a 32bit target, or the array bounds (n and m) are changed to unsigned.<br>><br>> For a bit of history, DA used to have a different delinearisation method<br>> based on geps, but it was known to be invalid in same cases and was<br>> eventually removed. There was no delinearisation for a while until these<br>> checks were fixed, enough to be correct, but not as powerful as they could<br>> be. I believe Pollys delinearisation is much more powerful, and can version<br>> loops with runtime conditions.<br>><br>> IIRC, the checks are there for cases like this:<br>> void foo(unsigned n, unsigned m, int *a) {<br>> for (int i = 0; i < n; ++i)<br>> for (int j = 0; j < m; ++j) {<br>> a[i*m + j] = a[i*m + j+2];<br>> }<br>> }<br>><br>> The "j-2" can underflow into the previous i iterations space. So the<br>> distance vector isn't [0 2] (and isn't even [= <]). Unfortunately this is<br>> perfectly valid in C (I think for the int a[][m] case too).<br>><br>> See this comment for why they were needed and perhaps a better way to fix<br>> it:<br>> </font></tt><tt><font size="2"><a href="https://github.com/llvm/llvm-project/commit/d143c65de3c884d09197da279d2f04f094efaf15#diff-57473b376037dd3637516348b9b02556L3274">https://github.com/llvm/llvm-project/commit/d143c65de3c884d09197da279d2f04f094efaf15#diff-57473b376037dd3637516348b9b02556L3274</a></font></tt><tt><font size="2"><br>><br>><br>> Any improvements to the delinearisation code would be most welcome.<br>> Dave<br>><br>><br>><br>><br>> From: llvm-dev <llvm-dev-bounces@lists.llvm.org> on behalf of Bardia<br>> Mahjour via llvm-dev <llvm-dev@lists.llvm.org><br>> Sent: 13 May 2019 14:48<br>> To: llvm-dev@lists.llvm.org<br>> Subject: [llvm-dev] Delinearization validity checks in DependenceAnalysis<br>><br>> Hi all,<br>><br>> I have been looking at the `DependenceAnalysis` pass in<br>> `llvm/include/llvm/Analysis/DependenceAnalysis.h`.<br>> In order for this analysis to produce accurate dependence vectors for<br>> multi-dimensional arrays in nested loops, it needs to "delinearize" array<br>> element accesses to recover the subscripts in each dimension of the array.<br>> I believe that the current implementation of delinearization is based on<br>> this paper:<br>> </font></tt><tt><font size="2"><a href="http://web.cse.ohio-state.edu/~pouchet.2/doc/ics-article.15a.pdf">http://web.cse.ohio-state.edu/~pouchet.2/doc/ics-article.15a.pdf</a></font></tt><tt><font size="2"><br>> .<br>><br>> This paper describes how to delinearize the subscripts, and as a last step<br>> it requires certain conditions to be met in order to validate that the<br>> delinearized indexes are correct. The `tryDelinearize` function in<br>> `DependenceAnalysis.cpp` appears to be checking for cases where these<br>> conditions can be validated *at compile time*:<br>><br>> ```<br>> // Statically check that the array bounds are in-range. The first subscript<br>> we<br>> // don't have a size for and it cannot overflow into another subscript, so<br>> is<br>> // always safe. The others need to be 0 <= subscript[i] < bound, for both<br>> src<br>> // and dst.<br>> // FIXME: It may be better to record these sizes and add them as<br>> constraints<br>> // to the dependency checks.<br>> for (int i = 1; i < size; ++i) {<br>> if (!isKnownNonNegative(SrcSubscripts[i], SrcPtr))<br>> return false;<br>><br>> if (!isKnownLessThan(SrcSubscripts[i], Sizes[i - 1]))<br>> return false;<br>><br>> if (!isKnownNonNegative(DstSubscripts[i], DstPtr))<br>> return false;<br>><br>> if (!isKnownLessThan(DstSubscripts[i], Sizes[i - 1]))<br>> return false;<br>> }<br>> ```<br>><br>> The problem is that in a lot of cases these conditions cannot be proven<br>> statically, even though the delinearized indexes are in fact correct. For<br>> example consider this simple loop:<br>><br>> ```<br>> void foo(int n, int m, int a[][m]) {<br>> for (int i = 0; i < n-1; ++i)<br>> for (int j = 2; j < m; ++j) {<br>> a[i][j] = a[i+1][j-2];<br>> }<br>> }<br>><br>> clang test.c -c -O3 -S -Xclang -disable-llvm-passes -emit-llvm<br>> opt -mem2reg -instcombine -indvars -loop-simplify -loop-rotate -inline<br>> -simplifycfg test.ll -S -o test.simp.ll<br>> opt test.simp.ll -analyze -da<br>> ```<br>><br>> will produce:<br>><br>> ```<br>> da analyze - none!<br>> da analyze - anti [* *|<]!<br>> da analyze - output [* *]!<br>> ```<br>><br>> If the validity checks were not present, the result would be much more<br>> accurate dependence vector with accurate dependence distances:<br>><br>> ```<br>> da analyze - none!<br>> da analyze - consistent anti [1 -2]!<br>> da analyze - none!<br>> ```<br>><br>> In my experience the static validity checks tend to fail in many common<br>> cases (involving loops with symbolic bounds). Sometimes this happens<br>> because SCEV is not able to simplify the expressions due to presence of<br>> type casts and sign/zero extensions, but other times the conditions are<br>> just not computable at compile-time.<br>><br>> So far I haven't been able to come up with an example where the validity<br>> checks in the current implementation legitimately catch a case of invalid<br>> delinearization. I've also disabled these checks and run some tests without<br>> finding any failures (other than improvements in the dependence analysis<br>> LIT tests).<br>><br>> I also had a quick look at Polly which benefits from delinearization. From<br>> what I saw, a much more optimistic approach is taken where the validity<br>> checks seem to be avoided.<br>><br>> My questions are:<br>> 1. Does anyone have more information about why these validity checks are<br>> necessary in the current implementation, perhaps with some examples showing<br>> an incorrect delinearization that's possible without those checks?<br>> 2. Are there any concerns with putting these validity checks under an<br>> option so that we can more easily disable them and experiment? This could<br>> also help us improve LIT tests, since some of them have been pessimised to<br>> compensate for DA's inability to delinearize, and could fail to catch<br>> regressions as a result of bad changes to the data dependence analysis.<br>><br>> Looking forward to your help on this.<br>><br>> Thank you,<br>><br>> Bardia Mahjour<br>> Compiler Optimizations<br>> Hydra Squad Lead<br>> IBM Toronto Software Lab<br>> bmahjour@ca.ibm.com (905) 413-2336<br>><br>><br>><br>><br>><br><br><br><br>> _______________________________________________<br>> LLVM Developers mailing list<br>> llvm-dev@lists.llvm.org<br>> </font></tt><tt><font size="2"><a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev">https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a></font></tt><tt><font size="2"><br><br><br>--<br><br>Johannes Doerfert<br>Researcher<br><br>Argonne National Laboratory<br>Lemont, IL 60439, USA<br><br>jdoerfert@anl.gov<br> [attachment "da3.c" deleted by Bardia Mahjour/Toronto/IBM]<br><br><br>[attachment "graycol.gif" deleted by Bardia Mahjour/Toronto/IBM] </font></tt><br><br><BR>
</body></html>