Skip to content

[AMDGPU] Fix AMDGPUPromoteAlloca handling certain loads incorrectly #123173

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions llvm/lib/Target/AMDGPU/AMDGPUPromoteAlloca.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,14 @@ calculateVectorIndex(Value *Ptr,
return I->second;
}

static void updateVectorIndex(Value *OldIdx, Value *NewIdx,
std::map<GetElementPtrInst *, Value *> &GEPIdx) {
for (auto &[GEP, Idx] : GEPIdx) {
if (Idx == OldIdx)
GEPIdx[GEP] = NewIdx;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't you just replace the value in the map? Why do you need the loop?

Also, will this be fixed by using WeakVH in #122342 (review)

Copy link
Contributor Author

@chinmaydd chinmaydd Jan 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't you just replace the value in the map? Why do you need the loop?

We need to update all occurrences (all such GEPs) of the value being used as a GEP index.

Yes, this looks like the same issue. I suppose I'll wait for that to be merged and check if it resolves the three tickets I have linked.

}
}

static Value *GEPToVectorIndex(GetElementPtrInst *GEP, AllocaInst *Alloca,
Type *VecElemTy, const DataLayout &DL) {
// TODO: Extracting a "multiple of X" from a GEP might be a useful generic
Expand Down Expand Up @@ -544,6 +552,9 @@ static Value *promoteAllocaUserToVector(
ExtractElement = Builder.CreateBitOrPointerCast(ExtractElement, AccessTy);

Inst->replaceAllUsesWith(ExtractElement);
// If the loaded value is used as an index into a GEP, update all its uses
// in the GEPVectorIdx map.
updateVectorIndex(Inst, ExtractElement, GEPVectorIdx);
return nullptr;
}
case Instruction::Store: {
Expand Down
16 changes: 16 additions & 0 deletions llvm/test/CodeGen/AMDGPU/promote-alloca-update-vector-idx.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
; RUN: opt -S -mtriple=amdgcn-amd-amdhsa -passes=amdgpu-promote-alloca < %s | FileCheck %s

define void @vector_alloca_with_loaded_value_as_index(<2 x i64> %arg) {
; CHECK-LABEL: define void @vector_alloca_with_loaded_value_as_index(
; CHECK-SAME: <2 x i64> [[ARG:%.*]]) {
; CHECK-NEXT: [[TMP1:%.*]] = extractelement <2 x i64> [[ARG]], i64 0
; CHECK-NEXT: [[TMP2:%.*]] = extractelement <2 x i64> [[ARG]], i64 1
; CHECK-NEXT: ret void
;
%alloca = alloca <2 x i64>, align 16
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is using the wrong address space

%idx = load i64, ptr %alloca, align 4
%gep = getelementptr <1 x double>, ptr %alloca, i64 %idx
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't use the weird 1 x vector type

store <2 x i64> %arg, ptr %gep, align 16
ret void
}
Loading