aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib
diff options
context:
space:
mode:
authorDeric C. <cheung.deric@gmail.com>2025-07-17 14:51:53 -0700
committerGitHub <noreply@github.com>2025-07-17 14:51:53 -0700
commitfae8df2b82692ec8f69ba578847713f0da6e1ddc (patch)
tree75edffadcc11294506cb084701326f2a659c6e12 /llvm/lib
parent689e95817e1671b0ed6c7f2031fbcf2f81632978 (diff)
downloadllvm-fae8df2b82692ec8f69ba578847713f0da6e1ddc.zip
llvm-fae8df2b82692ec8f69ba578847713f0da6e1ddc.tar.gz
llvm-fae8df2b82692ec8f69ba578847713f0da6e1ddc.tar.bz2
[DirectX] Fix GEP flattening with 0-indexed GEPs on global variables (#149211)
Fixes #149179 The issue is that `Builder.CreateGEP` does not return a GEP Instruction or GEP ContantExpr when the pointer operand is a global variable and all indices are constant zeroes. This PR ensures that a GEP instruction is created if `Builder.CreateGEP` did not return a GEP.
Diffstat (limited to 'llvm/lib')
-rw-r--r--llvm/lib/Target/DirectX/DXILFlattenArrays.cpp10
1 files changed, 10 insertions, 0 deletions
diff --git a/llvm/lib/Target/DirectX/DXILFlattenArrays.cpp b/llvm/lib/Target/DirectX/DXILFlattenArrays.cpp
index ce43645..f0e2e78 100644
--- a/llvm/lib/Target/DirectX/DXILFlattenArrays.cpp
+++ b/llvm/lib/Target/DirectX/DXILFlattenArrays.cpp
@@ -343,6 +343,16 @@ bool DXILFlattenArraysVisitor::visitGetElementPtrInst(GetElementPtrInst &GEP) {
Info.RootFlattenedArrayType, Info.RootPointerOperand,
{ZeroIndex, FlattenedIndex}, GEP.getName(), GEP.getNoWrapFlags());
+ // If the pointer operand is a global variable and all indices are 0,
+ // IRBuilder::CreateGEP will return the global variable instead of creating
+ // a GEP instruction or GEP ConstantExpr. In this case we have to create and
+ // insert our own GEP instruction.
+ if (!isa<GEPOperator>(NewGEP))
+ NewGEP = GetElementPtrInst::Create(
+ Info.RootFlattenedArrayType, Info.RootPointerOperand,
+ {ZeroIndex, FlattenedIndex}, GEP.getNoWrapFlags(), GEP.getName(),
+ Builder.GetInsertPoint());
+
// Replace the current GEP with the new GEP. Store GEPInfo into the map
// for later use in case this GEP was not the end of the chain
GEPChainInfoMap.insert({cast<GEPOperator>(NewGEP), std::move(Info)});