Skip to content

Commit b3313f7

Browse files
mbelickiigcbot
authored andcommitted
Fixed problem with resolved values caching in JointMatrix resolution pass.
This patch fixes problem with JointMatrix value caching that caused wrong value to be fetch from the cache. This patch also optimizes PHI node resolution by removing the need to resolve first incoming value before creating a new PHI node. With this change in place the placeholding mechanism is no longer needed.
1 parent 0e966cf commit b3313f7

File tree

2 files changed

+14
-42
lines changed

2 files changed

+14
-42
lines changed

IGC/Compiler/Optimizer/OpenCLPasses/JointMatrixFuncsResolutionPass.cpp

+13-39
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ JointMatrixFuncsResolutionPass::JointMatrixFuncsResolutionPass(OpenCLProgramCont
3737

3838
bool JointMatrixFuncsResolutionPass::runOnFunction(Function& F)
3939
{
40-
PlaceholderInstructions.clear();
4140
ResolvedValues.clear();
4241
InstsToErase.clear();
4342
Changed = false;
@@ -485,62 +484,37 @@ Value *JointMatrixFuncsResolutionPass::ResolveCall(CallInst *CI) {
485484
} else if (funcName.startswith(JointMatrixSliceExtract)) {
486485
NewValue = ResolveSliceExtract(CI);
487486
}
487+
488+
CacheResolvedValue(CI, NewValue);
488489
return NewValue;
489490
}
490491

491-
Instruction *JointMatrixFuncsResolutionPass::CreatePlaceholder(Value *v) {
492-
Type *type = ResolveType(v->getType(), nullptr);
493-
Instruction *predecesor = nullptr;
494-
if (Instruction *inst = dyn_cast<Instruction>(v)) {
495-
predecesor = inst;
496-
}
497-
/* I'm using bit-casts as placeholder values. Undefs of each type are unique per
498-
* module and cannot be used as unique placeholders. */
499-
return BitCastInst::Create(Instruction::BitCast, UndefValue::get(type),
500-
type, "tmp.value", predecesor);
492+
void JointMatrixFuncsResolutionPass::CacheResolvedValue(Value *oldValue, Value *newValue) {
493+
ResolvedValues[oldValue] = newValue;
501494
}
502495

503496
Value *JointMatrixFuncsResolutionPass::Resolve(Value *v)
504497
{
505498
if (ResolvedValues.count(v) > 0) {
506-
Value *value = ResolvedValues[v];
507-
if (value == v) {
508-
/* We are still resolving 'v'. Create a dummy value that will be
509-
* replaced later when 'v' is finally resolved. */
510-
Instruction *placeholder = CreatePlaceholder(v);
511-
PlaceholderInstructions[v] = placeholder;
512-
return placeholder;
513-
}
514499
return ResolvedValues[v];
515500
}
516501

517502
if (CallInst *CI = dyn_cast<CallInst>(v)) {
518503
return ResolveCall(CI);
519504
} else if (PHINode *PN = dyn_cast<PHINode>(v)) {
520505
unsigned IncomingCount = PN->getNumIncomingValues();
521-
ResolvedValues[v] = v;
522-
Value *First = Resolve(PN->getIncomingValue(0));
523506

524-
PHINode *NewPN = PHINode::Create(First->getType(), IncomingCount, "matrix.phi.node", PN);
525-
ResolvedValues[v] = NewPN;
507+
Type *type = ResolveType(v->getType(), nullptr);
508+
PHINode *NewPN = PHINode::Create(type, IncomingCount, "matrix.phi.node", PN);
509+
CacheResolvedValue(v, NewPN);
526510

527-
if (PlaceholderInstructions.count(v) > 0) {
528-
Instruction *placeholder = PlaceholderInstructions[v];
529-
PlaceholderInstructions.erase(v);
530-
placeholder->replaceAllUsesWith(NewPN);
531-
InstsToErase.insert(placeholder);
532-
}
533-
534-
NewPN->addIncoming(First, PN->getIncomingBlock(0));
535-
for (unsigned i = 1; i < IncomingCount; i++) {
511+
for (unsigned i = 0; i < IncomingCount; i++) {
536512
Value *oldOperand = PN->getIncomingValue(i);
537513
Value *operand = Resolve(oldOperand);
538514
NewPN->addIncoming(operand, PN->getIncomingBlock(i));
539515
}
540516

541-
PN->replaceAllUsesWith(UndefValue::get(PN->getType()));
542-
PN->eraseFromParent();
543-
517+
InstsToErase.insert(PN);
544518
return NewPN;
545519
}
546520

@@ -555,11 +529,11 @@ void JointMatrixFuncsResolutionPass::visitCallInst(CallInst& CI)
555529
return;
556530

557531
StringRef funcName = func->getName();
558-
/* Look for store functions and then recursively trace values that are used
559-
* by them. In future when returning and passing matrices by argument is
560-
* supported this also basic block terminators should be used as
532+
/* Resolve calls to JointMatrix BIs that haven't been resolved yet. In
533+
* future when returning and passing matrices by argument is
534+
* supported also basic block terminators should be used as
561535
* transformation starting point */
562-
if (funcName.startswith(CommonBIPrefix)) {
536+
if (funcName.startswith(CommonBIPrefix) && ResolvedValues.count(&CI) <= 0) {
563537
ResolveCall(&CI);
564538
}
565539
}

IGC/Compiler/Optimizer/OpenCLPasses/JointMatrixFuncsResolutionPass.h

+1-3
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,11 @@ namespace IGC
5757
llvm::Value *Resolve(llvm::Value *value);
5858

5959
llvm::Type *ResolveType(const llvm::Type *opaqueType, JointMatrixTypeDescription *outDesc);
60-
61-
llvm::Instruction *CreatePlaceholder(llvm::Value *value);
60+
void CacheResolvedValue(llvm::Value *oldValue, llvm::Value *newValue);
6261

6362
std::string GetLoadStoreMatrixFuncName
6463
(bool isLoad, unsigned operationLayout, const JointMatrixTypeDescription *desc);
6564

66-
llvm::ValueMap<llvm::Value *, llvm::Instruction *> PlaceholderInstructions;
6765
llvm::ValueMap<llvm::Value *, llvm::Value *> ResolvedValues;
6866
llvm::SmallPtrSet<llvm::Instruction *, 8> InstsToErase;
6967

0 commit comments

Comments
 (0)