Skip to content

[DirectX] Add a GEP to loads and stores on array allocas #148059

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

Merged
merged 6 commits into from
Jul 15, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
9 changes: 5 additions & 4 deletions llvm/lib/Target/DirectX/DXILLegalizePass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@ static void fixI8UseChain(Instruction &I,
ElementType = AI->getAllocatedType();
if (auto *GEP = dyn_cast<GetElementPtrInst>(NewOperands[0])) {
ElementType = GEP->getSourceElementType();
if (ElementType->isArrayTy())
ElementType = ElementType->getArrayElementType();
}
if (ElementType->isArrayTy())
ElementType = ElementType->getArrayElementType();
LoadInst *NewLoad = Builder.CreateLoad(ElementType, NewOperands[0]);
ReplacedValues[Load] = NewLoad;
ToRemove.push_back(Load);
Expand Down Expand Up @@ -593,7 +593,8 @@ legalizeLoadStoreOnArrayAllocas(Instruction &I,

IRBuilder<> Builder(&I);
Value *Zero = Builder.getInt32(0);
Value *GEP = Builder.CreateInBoundsGEP(Ty, AllocaPtrOp, {Zero, Zero});
Value *GEP = Builder.CreateGEP(Ty, AllocaPtrOp, {Zero, Zero}, "",
GEPNoWrapFlags::all());

Value *NewLoadStore = nullptr;
if (auto *LI = dyn_cast<LoadInst>(&I))
Expand Down Expand Up @@ -649,14 +650,14 @@ class DXILLegalizationPipeline {
LegalizationPipeline[Stage1].push_back(legalizeMemCpy);
LegalizationPipeline[Stage1].push_back(removeMemSet);
LegalizationPipeline[Stage1].push_back(updateFnegToFsub);
LegalizationPipeline[Stage1].push_back(legalizeLoadStoreOnArrayAllocas);
// Note: legalizeGetHighLowi64Bytes and
// downcastI64toI32InsertExtractElements both modify extractelement, so they
// must run staggered stages. legalizeGetHighLowi64Bytes runs first b\c it
// removes extractelements, reducing the number that
// downcastI64toI32InsertExtractElements needs to handle.
LegalizationPipeline[Stage2].push_back(
downcastI64toI32InsertExtractElements);
LegalizationPipeline[Stage2].push_back(legalizeLoadStoreOnArrayAllocas);
}
};

Expand Down
10 changes: 6 additions & 4 deletions llvm/test/CodeGen/DirectX/legalize-i8.ll
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,9 @@ define i32 @i8_geps_index0() {
; CHECK: [[LOAD:%.*]] = load i32, ptr [[GEP]], align 4
; CHECK-NEXT: ret i32 [[LOAD]]
%1 = alloca [2 x i32], align 8
%2 = getelementptr inbounds nuw i8, ptr %1, i32 0
%3 = load i8, ptr %2
%4 = sext i8 %3 to i32
ret i32 %4
%2 = load i8, ptr %1
%3 = sext i8 %2 to i32
ret i32 %3
}

define i32 @i8_geps_index1() {
Expand All @@ -149,11 +148,14 @@ define i32 @i8_geps_index1() {
define i32 @i8_gep_store() {
; CHECK-LABEL: define i32 @i8_gep_store(
; CHECK-NEXT: [[ALLOCA:%.*]] = alloca [2 x i32], align 8
; CHECK-NEXT: [[GEP:%.*]] = getelementptr inbounds nuw [2 x i32], ptr [[ALLOCA]], i32 0, i32 0
; CHECK-NEXT: store i32 0, ptr [[GEP]], align 4
; CHECK-NEXT: [[GEP:%.*]] = getelementptr inbounds nuw [2 x i32], ptr [[ALLOCA]], i32 0, i32 1
; CHECK-NEXT: store i32 1, ptr [[GEP]], align 4
; CHECK: [[LOAD:%.*]] = load i32, ptr [[GEP]], align 4
; CHECK-NEXT: ret i32 [[LOAD]]
%1 = alloca [2 x i32], align 8
store i8 0, ptr %1
%2 = getelementptr inbounds nuw i8, ptr %1, i32 4
store i8 1, ptr %2
%3 = load i8, ptr %2
Expand Down
4 changes: 2 additions & 2 deletions llvm/test/CodeGen/DirectX/legalize-load-store-array-alloca.ll
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
define float @load() {
; CHECK-LABEL: define float @load
; CHECK-NEXT: [[ALLOCA:%.*]] = alloca [2 x float], align 4
; CHECK-NEXT: [[GEP:%.*]] = getelementptr inbounds [2 x float], ptr [[ALLOCA]], i32 0, i32 0
; CHECK-NEXT: [[GEP:%.*]] = getelementptr inbounds nuw [2 x float], ptr [[ALLOCA]], i32 0, i32 0
; CHECK-NEXT: [[LOAD:%.*]] = load float, ptr [[GEP]], align 4
; CHECK-NEXT: ret float [[LOAD]]
%a = alloca [2 x float], align 4
Expand All @@ -14,7 +14,7 @@ define float @load() {
define void @store() {
; CHECK-LABEL: define void @store
; CHECK-NEXT: [[ALLOCA:%.*]] = alloca [3 x i32], align 4
; CHECK-NEXT: [[GEP:%.*]] = getelementptr inbounds [3 x i32], ptr [[ALLOCA]], i32 0, i32 0
; CHECK-NEXT: [[GEP:%.*]] = getelementptr inbounds nuw [3 x i32], ptr [[ALLOCA]], i32 0, i32 0
; CHECK-NEXT: store i32 0, ptr [[GEP]], align 4
; CHECK-NEXT: ret void
%a = alloca [3 x i32], align 4
Expand Down
Loading