Skip to content

Commit 6f6e42c

Browse files
committed
fix iterator stability issue
1 parent 2e21e3d commit 6f6e42c

File tree

4 files changed

+25
-36
lines changed

4 files changed

+25
-36
lines changed

llvm/lib/Target/DirectX/DXILDataScalarization.cpp

Lines changed: 20 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
//===- DXILDataScalarization.cpp - Prepare LLVM Module for DXIL Data
2-
// Legalization----===//
1+
//===- DXILDataScalarization.cpp - Perform DXIL Data Legalization----===//
32
//
43
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
54
// See https://llvm.org/LICENSE.txt for license information.
65
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
76
//
8-
//===--------------------------------------------------------------------------------===//
7+
//===----------------------------------------------------------------===//
98

109
#include "DXILDataScalarization.h"
1110
#include "DirectX.h"
1211
#include "llvm/ADT/PostOrderIterator.h"
12+
#include "llvm/ADT/STLExtras.h"
1313
#include "llvm/IR/GlobalVariable.h"
1414
#include "llvm/IR/IRBuilder.h"
1515
#include "llvm/IR/InstVisitor.h"
@@ -20,7 +20,6 @@
2020
#include "llvm/IR/Type.h"
2121
#include "llvm/Transforms/Utils/Cloning.h"
2222
#include "llvm/Transforms/Utils/Local.h"
23-
#include <utility>
2423

2524
#define DEBUG_TYPE "dxil-data-scalarization"
2625
#define Max_VEC_SIZE 4
@@ -164,14 +163,16 @@ Constant *transformInitializer(Constant *Init, Type *OrigType, Type *NewType,
164163
if (isa<VectorType>(OrigType) && isa<ArrayType>(NewType)) {
165164
// Convert vector initializer to array initializer
166165
SmallVector<Constant *, Max_VEC_SIZE> ArrayElements;
167-
if( ConstantVector *ConstVecInit = dyn_cast<ConstantVector>(Init)) {
168-
for (unsigned I = 0; I < ConstVecInit->getNumOperands(); ++I)
169-
ArrayElements.push_back(ConstVecInit->getOperand(I));
170-
} else if (ConstantDataVector *ConstDataVecInit = llvm::dyn_cast<llvm::ConstantDataVector>(Init)) {
171-
for (unsigned I = 0; I < ConstDataVecInit->getNumElements(); ++I)
172-
ArrayElements.push_back(ConstDataVecInit->getElementAsConstant(I));
173-
} else {
174-
llvm_unreachable("Expected a ConstantVector or ConstantDataVector for vector initializer!");
166+
if (ConstantVector *ConstVecInit = dyn_cast<ConstantVector>(Init)) {
167+
for (unsigned I = 0; I < ConstVecInit->getNumOperands(); ++I)
168+
ArrayElements.push_back(ConstVecInit->getOperand(I));
169+
} else if (ConstantDataVector *ConstDataVecInit =
170+
llvm::dyn_cast<llvm::ConstantDataVector>(Init)) {
171+
for (unsigned I = 0; I < ConstDataVecInit->getNumElements(); ++I)
172+
ArrayElements.push_back(ConstDataVecInit->getElementAsConstant(I));
173+
} else {
174+
llvm_unreachable("Expected a ConstantVector or ConstantDataVector for "
175+
"vector initializer!");
175176
}
176177

177178
return ConstantArray::get(cast<ArrayType>(NewType), ArrayElements);
@@ -213,9 +214,10 @@ static void findAndReplaceVectors(Module &M) {
213214
// Create a new global variable with the updated type
214215
GlobalVariable *NewGlobal = new GlobalVariable(
215216
M, NewType, G.isConstant(), G.getLinkage(),
216-
// This is set via: transformInitializer
217-
nullptr, G.getName() + ".scalarized", &G, G.getThreadLocalMode(),
218-
G.getAddressSpace(), G.isExternallyInitialized());
217+
// Initializer is set via transformInitializer
218+
/*Initializer=*/nullptr, G.getName() + ".scalarized", &G,
219+
G.getThreadLocalMode(), G.getAddressSpace(),
220+
G.isExternallyInitialized());
219221

220222
// Copy relevant attributes
221223
NewGlobal->setUnnamedAddr(G.getUnnamedAddr());
@@ -230,31 +232,16 @@ static void findAndReplaceVectors(Module &M) {
230232
}
231233

232234
// Note: we want to do G.replaceAllUsesWith(NewGlobal);, but it assumes
233-
// type equality
234-
// So instead we will use the visitor pattern
235+
// type equality. Instead we will use the visitor pattern.
235236
Impl.GlobalMap[&G] = NewGlobal;
236-
for (User *U : G.users()) {
237-
// Note: The GEPS are stored as constExprs
238-
// This step flattens them out to instructions
237+
for (User *U : make_early_inc_range(G.users())) {
239238
if (isa<ConstantExpr>(U) && isa<Operator>(U)) {
240239
ConstantExpr *CE = cast<ConstantExpr>(U);
241240
convertUsersOfConstantsToInstructions(CE,
242241
/*RestrictToFunc=*/nullptr,
243242
/*RemoveDeadConstants=*/false,
244243
/*IncludeSelf=*/true);
245244
}
246-
}
247-
// Uses should have grown
248-
std::vector<User *> UsersToProcess;
249-
// Collect all users first
250-
// work around so I can delete
251-
// in a loop body
252-
for (User *U : G.users()) {
253-
UsersToProcess.push_back(U);
254-
}
255-
256-
// Now process each user
257-
for (User *U : UsersToProcess) {
258245
if (isa<Instruction>(U)) {
259246
Instruction *Inst = cast<Instruction>(U);
260247
Function *F = Inst->getFunction();
@@ -294,4 +281,4 @@ INITIALIZE_PASS_END(DXILDataScalarizationLegacy, DEBUG_TYPE,
294281

295282
ModulePass *llvm::createDXILDataScalarizationLegacyPass() {
296283
return new DXILDataScalarizationLegacy();
297-
}
284+
}

llvm/test/CodeGen/DirectX/llc-pipeline.ll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
; CHECK-NEXT: Target Transform Information
99
; CHECK-NEXT: ModulePass Manager
1010
; CHECK-NEXT: DXIL Intrinsic Expansion
11+
; CHECK-NEXT: DXIL Data Scalarization
1112
; CHECK-NEXT: FunctionPass Manager
1213
; CHECK-NEXT: Dominator Tree Construction
1314
; CHECK-NEXT: Scalarize vector operations

llvm/test/CodeGen/DirectX/scalar-load.ll

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,18 @@
22
; RUN: llc %s -mtriple=dxil-pc-shadermodel6.3-library --filetype=asm -o - | FileCheck %s
33
@"arrayofVecData" = local_unnamed_addr addrspace(3) global [2 x <3 x float>] zeroinitializer, align 16
44
@"vecData" = external addrspace(3) global <4 x i32>, align 4
5-
;@staticArrayOfVecData = internal global [3 x <4 x i32>] zeroinitializer, align 4
65
@staticArrayOfVecData = internal global [3 x <4 x i32>] [<4 x i32> <i32 1, i32 2, i32 3, i32 4>, <4 x i32> <i32 5, i32 6, i32 7, i32 8>, <4 x i32> <i32 9, i32 10, i32 11, i32 12>], align 4
7-
6+
@staticArray = internal global [4 x i32] [i32 1, i32 2, i32 3, i32 4], align 4
87

98
; CHECK: @arrayofVecData.scalarized = local_unnamed_addr addrspace(3) global [2 x [3 x float]] zeroinitializer, align 16
109
; CHECK: @vecData.scalarized = external addrspace(3) global [4 x i32], align 4
1110
; CHECK: @staticArrayOfVecData.scalarized = internal global [3 x [4 x i32]] {{\[}}[4 x i32] [i32 1, i32 2, i32 3, i32 4], [4 x i32] [i32 5, i32 6, i32 7, i32 8], [4 x i32] [i32 9, i32 10, i32 11, i32 12]], align 4
11+
; Check @staticArray
1212

1313
; CHECK-NOT: @arrayofVecData
1414
; CHECK-NOT: @vecData
1515
; CHECK-NOT: @staticArrayOfVecData
16+
; CHECK-NOT: @staticArray.scalarized
1617

1718
; CHECK-LABEL: load_array_vec_test
1819
define <4 x i32> @load_array_vec_test() {

llvm/test/CodeGen/DirectX/scalar-store.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@ define void @store_vec_test(<4 x i32> %inputVec) {
2424
; CHECK-NOT: store i32 %inputVec.{{.*}}, ptr addrspace(3)
2525
store <4 x i32> %inputVec, <4 x i32> addrspace(3)* @"vecData", align 4
2626
ret void
27-
}
27+
}

0 commit comments

Comments
 (0)