-
Notifications
You must be signed in to change notification settings - Fork 14.5k
[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
Icohedron
merged 6 commits into
llvm:main
from
Icohedron:explicit-load-store-type-mismatch
Jul 15, 2025
Merged
[DirectX] Add a GEP to loads and stores on array allocas #148059
Icohedron
merged 6 commits into
llvm:main
from
Icohedron:explicit-load-store-type-mismatch
Jul 15, 2025
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@llvm/pr-subscribers-backend-directx Author: Deric C. (Icohedron) ChangesFixes #147114 by inserting a GEP between any direct loads and stores on an alloca. Full diff: https://github.com/llvm/llvm-project/pull/148059.diff 2 Files Affected:
diff --git a/llvm/lib/Target/DirectX/DXILLegalizePass.cpp b/llvm/lib/Target/DirectX/DXILLegalizePass.cpp
index 76a46c7a2b760..724ee57c05a39 100644
--- a/llvm/lib/Target/DirectX/DXILLegalizePass.cpp
+++ b/llvm/lib/Target/DirectX/DXILLegalizePass.cpp
@@ -562,6 +562,48 @@ legalizeGetHighLowi64Bytes(Instruction &I,
}
}
+static void legalizeLoadStoreOnArrayAllocas(
+ Instruction &I, SmallVectorImpl<Instruction *> &ToRemove,
+ DenseMap<Value *, Value *> &) {
+
+ Value *PtrOp;
+ [[maybe_unused]] Type *LoadStoreTy;
+ if (auto *LI = dyn_cast<LoadInst>(&I)) {
+ PtrOp = LI->getPointerOperand();
+ LoadStoreTy = LI->getType();
+ } else if (auto *SI = dyn_cast<StoreInst>(&I)) {
+ PtrOp = SI->getPointerOperand();
+ LoadStoreTy = SI->getValueOperand()->getType();
+ } else
+ return;
+
+ assert(LoadStoreTy->isSingleValueType() &&
+ "Expected load/store type to be a single-valued type");
+
+ auto *AllocaPtrOp = dyn_cast<AllocaInst>(PtrOp);
+ if (!AllocaPtrOp)
+ return;
+
+ Type *Ty = AllocaPtrOp->getAllocatedType();
+ if (!isa<ArrayType>(Ty)) return;
+ assert(!isa<ArrayType>(Ty->getArrayElementType()) &&
+ "Expected allocated type of AllocaInst to be a flat ArrayType");
+
+ IRBuilder<> Builder(&I);
+ Value *Zero = Builder.getInt32(0);
+ Value *GEP = Builder.CreateInBoundsGEP(Ty, AllocaPtrOp, {Zero, Zero});
+
+ Value *NewLoadStore = nullptr;
+ if (auto *LI = dyn_cast<LoadInst>(&I))
+ NewLoadStore = Builder.CreateLoad(LI->getType(), GEP, LI->getName());
+ else if (auto *SI = dyn_cast<StoreInst>(&I))
+ NewLoadStore =
+ Builder.CreateStore(SI->getValueOperand(), GEP, SI->isVolatile());
+
+ ToRemove.push_back(&I);
+ I.replaceAllUsesWith(NewLoadStore);
+}
+
namespace {
class DXILLegalizationPipeline {
@@ -605,6 +647,7 @@ 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
diff --git a/llvm/test/CodeGen/DirectX/legalize-load-store-array-alloca.ll b/llvm/test/CodeGen/DirectX/legalize-load-store-array-alloca.ll
new file mode 100644
index 0000000000000..703f569f4dfab
--- /dev/null
+++ b/llvm/test/CodeGen/DirectX/legalize-load-store-array-alloca.ll
@@ -0,0 +1,23 @@
+; RUN: opt -S -passes='dxil-legalize' -mtriple=dxil-pc-shadermodel6.3-library %s | FileCheck %s
+
+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: [[LOAD:%.*]] = load float, ptr [[GEP]], align 4
+; CHECK-NEXT: ret float [[LOAD]]
+ %a = alloca [2 x float], align 4
+ %b = load float, ptr %a, align 4
+ ret float %b
+}
+
+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: store i32 0, ptr [[GEP]], align 4
+; CHECK-NEXT: ret void
+ %a = alloca [3 x i32], align 4
+ store i32 0, ptr %a, align 4
+ ret void
+}
|
✅ With the latest revision this PR passed the C/C++ code formatter. |
farzonl
reviewed
Jul 10, 2025
farzonl
reviewed
Jul 10, 2025
inbelic
approved these changes
Jul 14, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #147114 by inserting a GEP between any direct loads and stores on an alloca.