Heikki Kultala
2011-Feb-14 18:10 UTC
[LLVMdev] broken alignment in stack(caused by bug in SelectionDAGBuilder) causes invalid schedules with r125471 and newer
The following problems happens with architectures, where stack alignment is smaller than the biggest preferred alignment for any data type SP pointer may point anywhere with alignment of stack alignment (4 in our case) SelectionDAGBuilder however calls CreateStackObject with preferred alignment is given data type(8 in our problemaric case. The ABI alignment for this data type is only 4) This means, that the MachineFrameInfo thinks that the stack object is aligned by 8 bytes, even though in reality if may also be aligned only by 4 bytes, of the SP points to address which is aligned by 4, but not 8. r125471 introduced optimization which can optimize add into xor in case object is aligned so that the operation only touches the lowest bits, which were zero on the aligned pointer. But when the MachineFrameInfo contains invalid align information, this optimization breaks and it incorrectly optimizes add by 4 into xor by 4, when the object is (in reality) aligned by 4, not by 8(which MFI thinks about it's alignment) Two solutions come into my mind: 1) changiing the SelectionDAGBuilder to use std::min(StackAlignment, PreferredAlignment) instead of PreferredAlignment. This might require asserting (StackAlignment <= ABI alignment). 2) changing the SelectionAGBuilder to use ABI alignment instead of the preferred alignment for the stack objects. The affected code lines are around 4820, and 5694 of SelectionDAGBuilder.cpp
Heikki Kultala
2011-Feb-14 18:43 UTC
[LLVMdev] broken alignment in stack(caused by bug in SelectionDAGBuilder) causes invalid schedules with r125471 and newer
On 14 Feb 2011, at 20:10, Heikki Kultala wrote:> The following problems happens with architectures, where stack alignment is smaller than the biggest preferred alignment for any data type > > > SP pointer may point anywhere with alignment of stack alignment (4 in our case) > > SelectionDAGBuilder however calls CreateStackObject with preferred alignment is given data type(8 in our problemaric case. The ABI alignment for this data type is only 4) > > This means, that the MachineFrameInfo thinks that the stack object is aligned by 8 bytes, even though in reality if may also be aligned only by 4 bytes, of the SP points to address which is aligned by 4, but not 8. > > > r125471 introduced optimization which can optimize add into xor in case object is aligned so that the operation only touches the lowest bits, which were zero on the aligned pointer. > > But when the MachineFrameInfo contains invalid align information, this optimization breaks and it incorrectly optimizes add by 4 into xor by 4, when the object is (in reality) aligned by 4, not by 8(which MFI thinks about it's alignment) > > > > Two solutions come into my mind: > > 1) changiing the SelectionDAGBuilder to use std::min(StackAlignment, PreferredAlignment) instead of PreferredAlignment. This might require asserting (StackAlignment <= ABI alignment). > > 2) changing the SelectionAGBuilder to use ABI alignment instead of the preferred alignment for the stack objects. > > > The affected code lines are around 4820, and 5694 of SelectionDAGBuilder.cppHmm, it seems that CreateStackObject is called also in many other places with preferred alignment. So maybe it would be better to instead change the part which checks the alignment (InferPtrAlignment?) , so that for objects in stack , the alignment would be correctly calculated taking the stack alignment into consideration