[DAGCombiner] missed transfer of chain edge
| Bugzilla Link | 25310 |
| Resolution | FIXED |
| Resolved on | Nov 21, 2015 07:27 |
| Version | trunk |
| OS | Linux |
| Attachments | llc input |
| CC | @adibiagio,@jyknight,@arsenm,@mikaelholmen,@rotateright,@uweigand |
Extended Description
I think I have found a bug in DAGCombiner, where two stores and two loads get combined to wider variants in DAGCombiner::MergeConsecutiveStores(). The problem is that the there was another third store that aliased one of the loads, but this other store does not get an updated chain dependency to the new load.
(sorry for line breaks - please copy and paste into an editor if needed) before MergeConsecutiveStores(t49):
SelectionDAG has 44 nodes:
t0: ch = EntryToken
t2: i64,ch = CopyFromReg t0, Register:i64 %vreg12
t4: i64,ch = CopyFromReg t0, Register:i64 %vreg13
t6: i64 = add t2, Constant:i64<-4>
t7: i64 = Constant<0>
t9: i32,ch = load<LD4%scevgep14> t0, t6, undef:i64
t10: i32,ch = load<LD4%lsr.iv1012> t0, t2, undef:i64
t11: i64 = add t4, Constant:i64<-4>
t12: i32,ch = load<LD4%scevgep7> t0, t11, undef:i64 <<<<<<<<
t13: i32,ch = load<LD4%lsr.iv35> t0, t4, undef:i64 <<<<<<<<
t28: i32,ch = CopyFromReg t0, Register:i32 %vreg11
t30: i32 = add t28, Constant:i32<1>
t14: ch = TokenFactor t9:1, t10:1, t12:1, t13:1
t15: ch = store<ST4%scevgep6> t14, t9, t11, undef:i64
t48: ch = TokenFactor t9:1, t10:1, t13:1
t51: ch = store<ST4%lsr.iv35> t48, t10, t4, undef:i64 <<< // Other store chained after t13
t52: ch = TokenFactor t15, t51
t20: i64 = add t4, Constant:i64<8>
t22: ch = CopyToReg t0, Register:i64 %vreg14, t20
t24: i64 = add t2, Constant:i64<256>
t26: ch = CopyToReg t0, Register:i64 %vreg15, t24
t32: ch = CopyToReg t0, Register:i32 %vreg16, t30
t17: ch = store<ST4%scevgep13> t52, t12, t6, undef:i64 <<<<<<<<
t49: ch = store<ST4%lsr.iv1012> t52, t13, t2, undef:i64 <<<<<<<<
t50: ch = TokenFactor t17, t49
t41: ch = TokenFactor t22, t26, t32, t50
t34: i32,ch = CopyFromReg t0, Register:i32 %vreg10
t47: ch = br_cc t41, setne:ch, t34, t30, BasicBlock:ch<for.body.3.i 0x249ce30>
t44: ch = br t47, BasicBlock:ch<for.inc.40.i 0x249d000>
After MergeConsecutiveStores(t49), the t13 chain has been dropped but not replaced.
t48: ch = TokenFactor t9:1, t10:1, t0
t51: ch = store<ST4[%lsr.iv35](tbaa=<0x2414008>)> t48, t10, t4, undef:i64
I would have expected MergeConsecutiveStores() to transfer the sum of the chain users of the loads to the new load, but this does not appear to happen (It would also have been nice to get a debug output when replacing the loads).
The test case is projects/test-suite/SingleSource/Regression/C/Output/matrixTranspose, which shows up as a test fail on SystemZ.
llc -O3 matrixTranspose.linked.bc -mtriple=s390x-linux-gnu
// DAG: BB#5 'main:for.body.3.i'
// Source code: projects/test-suite/SingleSource/Regression/C/matrixTranspose.c
great! Commited as r253779.
So, LGTM. (you will commit, right?)
I don't really like the test case, but I couldn't come up with one I liked better myself, either.
This smaller test shows the same issue, but only if you examine the dag -- the instructions emitted are correct in either case, just by accident.
bin/llc -view-isel-dags -combiner-alias-analysis -mtriple=x86_64 <<EOF
define void @merge_stores_preserves_chain(i32* %arr1, i32* %arr2) { entry: %arr1_pre = getelementptr i32, i32* %arr1, i64 -1 %arr2_pre = getelementptr i32, i32* %arr2, i64 -1 %v10 = load i32, i32* %arr1_pre, align 4 %v11 = load i32, i32* %arr1, align 4 %v20 = load i32, i32* %arr2_pre, align 4 %v21 = load i32, i32* %arr2, align 4 store i32 %v10, i32* %arr2_pre, align 4 store i32 %v11, i32* %arr2, align 4 store i32 %v20, i32* %arr1_pre, align 4 store i32 %v21, i32* %arr1, align 4 ret void } EOF
However, when combined with my other changes (http://reviews.llvm.org/D14834), this bugfix does (somewhat accidentally) allow for improved store merging in the above example (both sets get turned into 8-byte-stores, instead of just 1).
So, I'll add that test to my other patch. But it too doesn't ACTUALLY prove the dag edges were constructed properly. Oh well.
Yes, sorry, on it.
I'm also working on another cleanup to MergeConsecutiveStores, and will check out this issue as well in the next day or two.
ping!
Does anybody wants to review this, please?
cc'ing James since he's investigating another bug in MergeConsecutiveStores.
Does this bug/patch overlap with your fix for bug 25154?
Patch with test case I have made a patch that seems to fix the problem. I think the problem was a confusion about incoming and outgoing chains to the loads. All the chain users of all the loads must be updated, otherwise a store that depends on only one of the loads will loose the dependency incorrectly.
I did not manage to get a very good test case, because it was very tricky to make the optimizers make the same decisions that would expose the bug generally. The test case supplied is taken from the original failing test case in test-suite. It is perhaps not so good, because with the patch, optimization results change also. Furhtermore, in that particular loop, there seems to be room for more optimizations that are currently not done.
Any comments, suggestions and/or better test case are welcome.
The first commit that exposed this bug was r250138 by Matt Arsenault.
assigned to @JonPsson