Skip to content

Commit 3ebf17e

Browse files
committed
[DirectX] This change fixes a circular reference discovered in flatten arrays
1 parent e0fd57c commit 3ebf17e

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

llvm/lib/Target/DirectX/DXILFlattenArrays.cpp

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class DXILFlattenArraysLegacy : public ModulePass {
4242

4343
struct GEPData {
4444
ArrayType *ParentArrayType;
45-
Value *ParendOperand;
45+
Value *ParentOperand;
4646
SmallVector<Value *> Indices;
4747
SmallVector<uint64_t> Dims;
4848
bool AllIndicesAreConstInt;
@@ -211,7 +211,7 @@ bool DXILFlattenArraysVisitor::visitAllocaInst(AllocaInst &AI) {
211211

212212
ArrayType *FattenedArrayType = ArrayType::get(BaseType, TotalElements);
213213
AllocaInst *FlatAlloca =
214-
Builder.CreateAlloca(FattenedArrayType, nullptr, AI.getName() + ".flat");
214+
Builder.CreateAlloca(FattenedArrayType, nullptr, AI.getName() + ".1dim");
215215
FlatAlloca->setAlignment(AI.getAlign());
216216
AI.replaceAllUsesWith(FlatAlloca);
217217
AI.eraseFromParent();
@@ -222,6 +222,10 @@ void DXILFlattenArraysVisitor::recursivelyCollectGEPs(
222222
GetElementPtrInst &CurrGEP, ArrayType *FlattenedArrayType,
223223
Value *PtrOperand, unsigned &GEPChainUseCount, SmallVector<Value *> Indices,
224224
SmallVector<uint64_t> Dims, bool AllIndicesAreConstInt) {
225+
// Check if this GEP is already in the map to avoid circular references
226+
if (GEPChainMap.count(&CurrGEP) > 0)
227+
return;
228+
225229
Value *LastIndex = CurrGEP.getOperand(CurrGEP.getNumOperands() - 1);
226230
AllIndicesAreConstInt &= isa<ConstantInt>(LastIndex);
227231
Indices.push_back(LastIndex);
@@ -271,10 +275,18 @@ bool DXILFlattenArraysVisitor::visitGetElementPtrInstInGEPChainBase(
271275
genInstructionFlattenIndices(GEPInfo.Indices, GEPInfo.Dims, Builder);
272276

273277
ArrayType *FlattenedArrayType = GEPInfo.ParentArrayType;
274-
Value *FlatGEP =
275-
Builder.CreateGEP(FlattenedArrayType, GEPInfo.ParendOperand,
276-
{Builder.getInt32(0), FlatIndex},
277-
GEP.getName() + ".flat", GEP.getNoWrapFlags());
278+
279+
// Don't append '.flat' to an empty string. If the SSA name isn't available
280+
// it could conflict with the ParentOperand's name.
281+
std::string FlatName = GEP.hasName() ? GEP.getName().str() + ".flat" : "";
282+
283+
Value *FlatGEP = Builder.CreateGEP(FlattenedArrayType, GEPInfo.ParentOperand,
284+
{Builder.getInt32(0), FlatIndex}, FlatName,
285+
GEP.getNoWrapFlags());
286+
287+
// Store the new GEP in the map before replacing uses to avoid circular
288+
// references
289+
GEPChainMap.erase(&GEP);
278290

279291
GEP.replaceAllUsesWith(FlatGEP);
280292
GEP.eraseFromParent();

llvm/test/CodeGen/DirectX/flatten-array.ll

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,8 @@ define void @gep_4d_test () {
123123
@b = internal global [2 x [3 x [4 x i32]]] zeroinitializer, align 16
124124

125125
define void @global_gep_load() {
126-
; CHECK: load i32, ptr getelementptr inbounds ([24 x i32], ptr @a.1dim, i32 0, i32 6), align 4
126+
; CHECK: [[GEP_PTR:%.*]] = getelementptr inbounds [24 x i32], ptr @a.1dim, i32 0, i32 6
127+
; CHECK-NEXT: load i32, ptr [[GEP_PTR]], align 4
127128
; CHECK-NEXT: ret void
128129
%1 = getelementptr inbounds [2 x [3 x [4 x i32]]], [2 x [3 x [4 x i32]]]* @a, i32 0, i32 0
129130
%2 = getelementptr inbounds [3 x [4 x i32]], [3 x [4 x i32]]* %1, i32 0, i32 1
@@ -176,7 +177,8 @@ define void @global_incomplete_gep_chain(i32 %row, i32 %col) {
176177
}
177178

178179
define void @global_gep_store() {
179-
; CHECK: store i32 1, ptr getelementptr inbounds ([24 x i32], ptr @b.1dim, i32 0, i32 13), align 4
180+
; CHECK: [[GEP_PTR:%.*]] = getelementptr inbounds [24 x i32], ptr @b.1dim, i32 0, i32 13
181+
; CHECK-NEXT: store i32 1, ptr [[GEP_PTR]], align 4
180182
; CHECK-NEXT: ret void
181183
%1 = getelementptr inbounds [2 x [3 x [4 x i32]]], [2 x [3 x [4 x i32]]]* @b, i32 0, i32 1
182184
%2 = getelementptr inbounds [3 x [4 x i32]], [3 x [4 x i32]]* %1, i32 0, i32 0

0 commit comments

Comments
 (0)