Skip to content

Commit f5f572f

Browse files
bjopetstellar
authored andcommitted
[SelectionDAG] Mark frame index as "aliased" at argument copy elison (#89712)
This is a fix for miscompiles reported in #89060 After argument copy elison the IR value for the eliminated alloca is aliasing with the fixed stack object. This patch is making sure that we mark the fixed stack object as being aliased with IR values to avoid that for example schedulers are reordering accesses to the fixed stack object. This could otherwise happen when there is a mix of MemOperands refering the shared fixed stack slow via both the IR value for the elided alloca, and via a fixed stack pseudo source value (as would be the case when lowering the arguments). (cherry picked from commit d8b253b)
1 parent dfc89f8 commit f5f572f

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

llvm/include/llvm/CodeGen/MachineFrameInfo.h

+7
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,13 @@ class MachineFrameInfo {
697697
return Objects[ObjectIdx+NumFixedObjects].isAliased;
698698
}
699699

700+
/// Set "maybe pointed to by an LLVM IR value" for an object.
701+
void setIsAliasedObjectIndex(int ObjectIdx, bool IsAliased) {
702+
assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
703+
"Invalid Object Idx!");
704+
Objects[ObjectIdx+NumFixedObjects].isAliased = IsAliased;
705+
}
706+
700707
/// Returns true if the specified index corresponds to an immutable object.
701708
bool isImmutableObjectIndex(int ObjectIdx) const {
702709
// Tail calling functions can clobber their function arguments.

llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -10888,14 +10888,15 @@ static void tryToElideArgumentCopy(
1088810888
}
1088910889

1089010890
// Perform the elision. Delete the old stack object and replace its only use
10891-
// in the variable info map. Mark the stack object as mutable.
10891+
// in the variable info map. Mark the stack object as mutable and aliased.
1089210892
LLVM_DEBUG({
1089310893
dbgs() << "Eliding argument copy from " << Arg << " to " << *AI << '\n'
1089410894
<< " Replacing frame index " << OldIndex << " with " << FixedIndex
1089510895
<< '\n';
1089610896
});
1089710897
MFI.RemoveStackObject(OldIndex);
1089810898
MFI.setIsImmutableObjectIndex(FixedIndex, false);
10899+
MFI.setIsAliasedObjectIndex(FixedIndex, true);
1089910900
AllocaIndex = FixedIndex;
1090010901
ArgCopyElisionFrameIndexMap.insert({OldIndex, FixedIndex});
1090110902
for (SDValue ArgVal : ArgVals)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 4
2+
; RUN: llc -mtriple hexagon-- -o - %s | FileCheck %s
3+
4+
; Reproducer for https://github.com/llvm/llvm-project/issues/89060
5+
;
6+
; Problem was a bug in argument copy elison. Given that the %alloca is
7+
; eliminated, the same frame index will be used for accessing %alloca and %a
8+
; on the fixed stack. Care must be taken when setting up
9+
; MachinePointerInfo/MemOperands for those accesses to either make sure that
10+
; we always refer to the fixed stack slot the same way (not using the
11+
; ir.alloca name), or make sure that we still detect that they alias each
12+
; other if using different kinds of MemOperands to identify the same fixed
13+
; stack entry.
14+
;
15+
define i32 @f(i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32 %q1, i32 %a, i32 %q2) {
16+
; CHECK-LABEL: f:
17+
; CHECK: .cfi_startproc
18+
; CHECK-NEXT: // %bb.0:
19+
; CHECK-NEXT: {
20+
; CHECK-NEXT: r0 = memw(r29+#36)
21+
; CHECK-NEXT: r1 = memw(r29+#28)
22+
; CHECK-NEXT: }
23+
; CHECK-NEXT: {
24+
; CHECK-NEXT: r0 = sub(r1,r0)
25+
; CHECK-NEXT: r2 = memw(r29+#32)
26+
; CHECK-NEXT: memw(r29+#32) = ##666
27+
; CHECK-NEXT: }
28+
; CHECK-NEXT: {
29+
; CHECK-NEXT: r0 = xor(r0,r2)
30+
; CHECK-NEXT: jumpr r31
31+
; CHECK-NEXT: }
32+
%alloca = alloca i32
33+
store i32 %a, ptr %alloca ; Should be elided.
34+
store i32 666, ptr %alloca
35+
%x = sub i32 %q1, %q2
36+
%y = xor i32 %x, %a ; Results in a load of %a from fixed stack.
37+
; Using same frame index as elided %alloca.
38+
ret i32 %y
39+
}

0 commit comments

Comments
 (0)