Skip to content

Commit cbac959

Browse files
jaladreipsigcbot
authored andcommitted
Purge unused instructions after value remapping in new inline raytracing
Purge unused instructions after value remapping in new inline raytracing
1 parent 21bae4b commit cbac959

File tree

1 file changed

+43
-6
lines changed

1 file changed

+43
-6
lines changed

IGC/AdaptorCommon/RayTracing/NewTraceRayInlineLoweringPass.cpp

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ bool InlineRaytracing::LowerAllocations(Function &F) {
142142
switch (II->getOpcode()) {
143143
case Instruction::Store: {
144144
auto *storeI = cast<StoreInst>(II);
145-
if (storeI->getValueOperand() == use->get()) {
145+
if (storeI->getValueOperand() == use->get() && v2vMap.count(storeI->getPointerOperand()) == 0) {
146146
SmallVector<Instruction *> origins;
147147
auto hasOrigins = Provenance::tryFindPointerOrigin(storeI->getPointerOperand(), origins);
148148

@@ -157,7 +157,8 @@ bool InlineRaytracing::LowerAllocations(Function &F) {
157157
cast<ArrayType>(array->getAllocatedType())->getNumElements());
158158

159159
IRB.SetInsertPoint(array);
160-
auto *newArray = IRB.CreateAlloca(ty, nullptr, array->getName(), array->getAddressSpace());
160+
auto *newArray = IRB.CreateAlloca(ty, nullptr, VALUE_NAME("RQObjectArrayAlloca_") + array->getName(),
161+
array->getAddressSpace());
161162
v2vMap[array] = newArray;
162163

163164
llvm::for_each(array->uses(), [&worklist](Use &U) { worklist.push_back(&U); });
@@ -180,12 +181,14 @@ bool InlineRaytracing::LowerAllocations(Function &F) {
180181
SmallVector<Value *> indices(cast<GetElementPtrInst>(II)->indices());
181182

182183
IRB.SetInsertPoint(II);
183-
v2vMap[II] = IRB.CreateInBoundsGEP(array->getAllocatedType(), array, indices, II->getName());
184+
v2vMap[II] = IRB.CreateInBoundsGEP(array->getAllocatedType(), array, indices,
185+
VALUE_NAME("RQObjectGEP_") + II->getName());
184186
llvm::for_each(II->uses(), [&worklist](Use &U) { worklist.push_back(&U); });
185187
} break;
186188
case Instruction::Load:
187189
IRB.SetInsertPoint(II);
188-
v2vMap[II] = IRB.CreateLoad(m_RQObjectType->getPointerTo(), v2vMap[II->getOperand(0)], II->getName());
190+
v2vMap[II] = IRB.CreateLoad(m_RQObjectType->getPointerTo(), v2vMap[II->getOperand(0)],
191+
VALUE_NAME("RQObjectLoad_") + II->getName());
189192
llvm::for_each(II->uses(), [&worklist](Use &U) { worklist.push_back(&U); });
190193
break;
191194
case Instruction::Select:
@@ -194,8 +197,8 @@ bool InlineRaytracing::LowerAllocations(Function &F) {
194197
continue;
195198

196199
IRB.SetInsertPoint(II);
197-
v2vMap[II] =
198-
IRB.CreateSelect(II->getOperand(0), v2vMap[II->getOperand(1)], v2vMap[II->getOperand(2)], II->getName());
200+
v2vMap[II] = IRB.CreateSelect(II->getOperand(0), v2vMap[II->getOperand(1)], v2vMap[II->getOperand(2)],
201+
VALUE_NAME("RQObjectSelect_") + II->getName());
199202
llvm::for_each(II->uses(), [&worklist](Use &U) { worklist.push_back(&U); });
200203
break;
201204
default:
@@ -208,6 +211,40 @@ bool InlineRaytracing::LowerAllocations(Function &F) {
208211

209212
RemapFunction(F, v2vMap, RF_IgnoreMissingLocals | RF_ReuseAndMutateDistinctMDs);
210213

214+
DenseSet<Instruction *> canBeDeleted;
215+
216+
// try to remove as many of unused instructions as possible
217+
for (auto [from, _] : v2vMap) {
218+
219+
if (auto *I = dyn_cast<Instruction>(const_cast<Value *>(from))) {
220+
221+
if (I->getType()->isVoidTy())
222+
continue;
223+
224+
if (isa<CallBase>(I))
225+
continue;
226+
227+
canBeDeleted.insert(I);
228+
}
229+
}
230+
231+
while (!canBeDeleted.empty()) {
232+
233+
bool changed = false;
234+
for (auto *V : canBeDeleted) {
235+
236+
if (V->use_empty()) {
237+
238+
canBeDeleted.erase(V);
239+
cast<Instruction>(V)->eraseFromParent();
240+
changed = true;
241+
break;
242+
}
243+
}
244+
if (!changed) // no progress has been done
245+
break;
246+
}
247+
211248
return true;
212249
}
213250

0 commit comments

Comments
 (0)