[Polly] [DependenceInfo] change WAR, WAW generation to correct semantics
commit490659bdcf7557513f29542c59ce01af72cdc25b
authorSiddharth Bhat <siddu.druid@gmail.com>
Tue, 4 Apr 2017 13:08:23 +0000 (4 13:08 +0000)
committerSiddharth Bhat <siddu.druid@gmail.com>
Tue, 4 Apr 2017 13:08:23 +0000 (4 13:08 +0000)
treeec9c26611e1ca197e3b4de4846ebaf8abd9af337
parent32524af12231f56481e4bb70aac8aa1bbe4cf5e1
[Polly] [DependenceInfo] change WAR, WAW generation to correct semantics

= Change of WAR, WAW generation: =

- `buildFlow(Sink, MustSource, MaySource, Sink)` treates any flow of the form
    `sink <- may source <- must source` as a *may* dependence.

- we used to call:
```lang=cpp, name=old-flow-call.cpp
Flow = buildFlow(MustWrite, MustWrite, Read, Schedule);
WAW = isl_union_flow_get_must_dependence(Flow);
WAR = isl_union_flow_get_may_dependence(Flow);
```

- This caused some WAW dependences to be treated as WAR dependences.
- Incorrect semantics.

- Now, we call WAR and WAW correctly.

== Correct WAW: ==
```lang=cpp, name=new-waw-call.cpp
   Flow = buildFlow(Write, MustWrite, MayWrite, Schedule);
   WAW = isl_union_flow_get_may_dependence(Flow);
   isl_union_flow_free(Flow);
```

== Correct WAR: ==
```lang=cpp, name=new-war-call.cpp
    Flow = buildFlow(Write, Read, MustaWrite, Schedule);
    WAR = isl_union_flow_get_must_dependence(Flow);
    isl_union_flow_free(Flow);
```

- We want the "shortest" WAR possible (exact dependences).
- We mark all the *must-writes* as may-source, reads as must-souce.
- Then, we ask for *must* dependence.
- This removes all the reads that flow through a *must-write*
  before reaching a sink.
- Note that we only block ealier writes with *must-writes*. This is
  intuitively correct, as we do not want may-writes to block
  must-writes.
- Leaves us with direct (R -> W).

- This affects reduction generation since RED is built using WAW and WAR.

= New StrictWAW for Reductions: =

- We used to call:
```lang=cpp,name=old-waw-war-call.cpp
      Flow = buildFlow(MustWrite, MustWrite, Read, Schedule);
      WAW = isl_union_flow_get_must_dependence(Flow);
      WAR = isl_union_flow_get_may_dependence(Flow);
```

- This *is* the right model of WAW we need for reductions, just not in general.
- Reductions need to track only *strict* WAW, without any interfering reductions.

= Explanation: Why the new WAR dependences in tests are correct: =

- We no longer set WAR = WAR - WAW
- Hence, we will have WAR dependences that were originally removed.
- These may look incorrect, but in fact make sense.

== Code: ==
```lang=llvm, name=new-war-dependence.ll
  ;    void manyreductions(long *A) {
  ;      for (long i = 0; i < 1024; i++)
  ;        for (long j = 0; j < 1024; j++)
  ; S0:          *A += 42;
  ;
  ;      for (long i = 0; i < 1024; i++)
  ;        for (long j = 0; j < 1024; j++)
  ; S1:          *A += 42;
  ;
```
=== WAR dependence: ===
  {  S0[1023, 1023] -> S1[0, 0] }

- Between `S0[1023, 1023]` and `S1[0, 0]`, we will have the dependences:

```lang=cpp, name=dependence-incorrect, counterexample
        S0[1023, 1023]:
    *-- tmp = *A (load0)--*
WAR 2   add = tmp + 42    |
    *-> *A = add (store0) |
                         WAR 1
        S1[0, 0]:         |
        tmp = *A (load1)  |
        add = tmp + 42    |
        A = add (store1)<-*
```

- One may assume that WAR2 *hides* WAR1 (since store0 happens before
  store1). However, within a statement, Polly has no idea about the
  ordering of loads and stores.

- Hence, according to Polly, the code may have looked like this:
```lang=cpp, name=dependence-correct
    S0[1023, 1023]:
    A = add (store0)
    tmp = A (load0) ---*
    add = A + 42       |
                     WAR 1
    S1[0, 0]:          |
    tmp = A (load1)    |
    add = A + 42       |
    A = add (store1) <-*
```

- So, Polly  generates (correct) WAR dependences. It does not make sense
  to remove these dependences, since they are correct with respect to
  Polly's model.

    Reviewers: grosser, Meinersbur

    tags: #polly

    Differential revision: https://reviews.llvm.org/D31386

git-svn-id: https://llvm.org/svn/llvm-project/polly/trunk@299429 91177308-0d34-0410-b5e6-96231b3b80d8
17 files changed:
lib/Analysis/DependenceInfo.cpp
lib/Transform/ScheduleOptimizer.cpp
test/DependenceInfo/different_schedule_dimensions.ll
test/DependenceInfo/do_pluto_matmult.ll
test/DependenceInfo/generate_may_write_dependence_info.ll
test/DependenceInfo/may_writes_do_not_block_must_writes_for_war.ll [new file with mode: 0644]
test/DependenceInfo/reduction_dependences_equal_non_reduction_dependences.ll
test/DependenceInfo/reduction_multiple_reductions_2.ll
test/DependenceInfo/reduction_privatization_deps.ll
test/DependenceInfo/reduction_privatization_deps_2.ll
test/DependenceInfo/reduction_privatization_deps_3.ll
test/DependenceInfo/reduction_privatization_deps_4.ll
test/DependenceInfo/reduction_privatization_deps_5.ll
test/DependenceInfo/reduction_sequence.ll
test/DependenceInfo/reduction_simple_iv_debug_wrapped_dependences.ll
test/DependenceInfo/reduction_simple_privatization_deps_2.ll
test/DependenceInfo/reduction_simple_privatization_deps_w_parameter.ll