Skip to content

Commit 7dfedf0

Browse files
skachkov-inteligcbot
authored andcommitted
Use return value post-copy for inserting copies with source
from calls Copy from original call value is incorrect because it is allocated on unified return value and can be overwritten by subsequent calls, so we should use post-copy instead
1 parent 6218054 commit 7dfedf0

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

IGC/VectorCompiler/lib/GenXCodeGen/GenXCoalescing.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ SPDX-License-Identifier: MIT
197197
#include "llvm/Support/Debug.h"
198198

199199
#include <algorithm>
200+
#include <map>
200201
#include <vector>
201202

202203
using namespace llvm;
@@ -317,6 +318,7 @@ namespace {
317318
std::vector<Candidate> NormalCandidates;
318319
std::vector<CallInst*> Callables;
319320
std::vector<CopyData> ToCopy;
321+
std::map<SimpleValue, Value*> CallToRetVal;
320322
std::unordered_map<Instruction *, Value *> CopyCoalesced;
321323

322324
public:
@@ -508,6 +510,7 @@ bool GenXCoalescing::runOnFunctionGroup(FunctionGroup &FG)
508510
CopyCandidates.clear();
509511
NormalCandidates.clear();
510512
Callables.clear();
513+
CallToRetVal.clear();
511514
ToCopy.clear();
512515
CopyCoalesced.clear();
513516
return true;
@@ -1400,9 +1403,10 @@ void GenXCoalescing::processCalls(FunctionGroup *FG)
14001403
showCoalesceFail(SimpleValue(CI, StructIdx), CI->getDebugLoc(),
14011404
"ret postcopy", DestLR, SourceLR);
14021405
unsigned Num = Numbering->getRetPostCopyNumber(CI, StructIdx);
1406+
SimpleValue Source(CI, StructIdx);
14031407
Instruction *NewCopy =
1404-
insertCopy(SimpleValue(CI, StructIdx), DestLR, InsertBefore,
1405-
"retval.postcopy", Num);
1408+
insertCopy(Source, DestLR, InsertBefore, "retval.postcopy", Num);
1409+
CallToRetVal[Source] = NewCopy;
14061410
IGC_ASSERT(NewCopy);
14071411
if (AllUsesAreExtract) {
14081412
// For a struct ret value where all the uses are non-struct
@@ -2039,6 +2043,9 @@ SortedCopies GenXCoalescing::getSortedCopyData() {
20392043
Instruction *GenXCoalescing::createCopy(const CopyData &CD) {
20402044
LiveRange *DestLR = Liveness->getLiveRange(CD.Dest);
20412045
LiveRange *SourceLR = Liveness->getLiveRange(CD.Source);
2046+
SimpleValue Source = CD.Source;
2047+
if (auto It = CallToRetVal.find(Source); It != CallToRetVal.end())
2048+
Source = SimpleValue{It->second};
20422049
Instruction *NewCopy = nullptr;
20432050
switch (CD.CopyT) {
20442051
case PHICOPY:
@@ -2052,7 +2059,7 @@ Instruction *GenXCoalescing::createCopy(const CopyData &CD) {
20522059
: Numbering->getNumber(CD.InsertPoint);
20532060
showCoalesceFail(CD.Dest, CD.InsertPoint->getDebugLoc(), "phi", DestLR,
20542061
SourceLR);
2055-
NewCopy = insertCopy(CD.Source, DestLR, CD.InsertPoint, "phicopy", Num);
2062+
NewCopy = insertCopy(Source, DestLR, CD.InsertPoint, "phicopy", Num);
20562063
Phi->setIncomingValue(CD.UseInDest->getOperandNo(), NewCopy);
20572064
break;
20582065
}
@@ -2063,7 +2070,7 @@ Instruction *GenXCoalescing::createCopy(const CopyData &CD) {
20632070
Instruction *DestInst = cast<Instruction>(CD.Dest.getValue());
20642071
showCoalesceFail(CD.Dest, DestInst->getDebugLoc(), "two address", DestLR,
20652072
SourceLR);
2066-
NewCopy = insertCopy(CD.Source, DestLR, DestInst, "twoaddr",
2073+
NewCopy = insertCopy(Source, DestLR, DestInst, "twoaddr",
20672074
Numbering->getNumber(DestInst) - 1);
20682075
NewCopy =
20692076
insertIntoStruct(CD.Dest.getValue()->getType(), CD.Dest.getIndex(),

0 commit comments

Comments
 (0)