Skip to content

Commit 60d2552

Browse files
stefan-iligcbot
authored andcommitted
Fix memory corruption in LinearScanRA
Memory for retGRFs was getting allocated in globalLinearScan which also freed it after going out of scope, but it was used afterwards.
1 parent 0c75e42 commit 60d2552

File tree

2 files changed

+19
-12
lines changed

2 files changed

+19
-12
lines changed

visa/LinearScanRA.cpp

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ SPDX-License-Identifier: MIT
1414
#include "SpillManagerGMRF.h"
1515
#include "common.h"
1616
#include <optional>
17-
#include <fstream>
18-
#include <tuple>
1917

2018
using namespace vISA;
2119

@@ -45,10 +43,15 @@ void LinearScanRA::allocForbiddenVector(LSLiveRange *lr) {
4543
}
4644

4745
void globalLinearScan::allocRetRegsVector(LSLiveRange *lr) {
48-
unsigned size = builder.kernel.getNumRegTotal();
49-
bool *forbidden = (bool *)GLSMem.alloc(sizeof(bool) * size);
50-
memset(forbidden, false, size);
51-
lr->setRegGRFs(forbidden);
46+
auto size = builder.kernel.getNumRegTotal();
47+
if (lr->getRetGRFs() == nullptr) {
48+
bool *forbidden = (bool *)GLSMem->alloc(sizeof(bool) * size);
49+
memset(forbidden, false, size);
50+
lr->setRegGRFs(forbidden);
51+
} else {
52+
// If we are vector is preallocated, simply clear it
53+
lr->clearRetGRF(size);
54+
}
5255
}
5356

5457
LSLiveRange *LinearScanRA::GetOrCreateLocalLiveRange(G4_Declare *topdcl) {
@@ -865,7 +868,7 @@ int LinearScanRA::linearScanRA() {
865868
PhyRegsManager pregManager(builder, initPregs, doBCR);
866869
globalLinearScan ra(gra, &l, globalLiveIntervals, &preAssignedLiveIntervals,
867870
inputIntervals, pregManager, numRegLRA, numRowsEOT,
868-
latestLexID, doBCR, highInternalConflict);
871+
latestLexID, doBCR, highInternalConflict, &LSMem);
869872

870873
// Run linear scan RA
871874
bool success = ra.runLinearScan(builder, globalLiveIntervals, spillLRs);
@@ -1783,8 +1786,8 @@ globalLinearScan::globalLinearScan(
17831786
std::list<LSInputLiveRange *, std_arena_based_allocator<LSInputLiveRange *>>
17841787
&inputLivelIntervals,
17851788
PhyRegsManager &pregMgr, unsigned int numReg, unsigned int numEOT,
1786-
unsigned int lastLexID, bool bankConflict, bool internalConflict)
1787-
: gra(g), builder(g.builder), GLSMem(4096), pregManager(pregMgr),
1789+
unsigned int lastLexID, bool bankConflict, bool internalConflict, Mem_Manager* GLSMem)
1790+
: gra(g), builder(g.builder), GLSMem(GLSMem), pregManager(pregMgr),
17881791
liveIntervals(lv), preAssignedIntervals(assignedLiveIntervals),
17891792
inputIntervals(inputLivelIntervals), numRowsEOT(numEOT),
17901793
lastLexicalID(lastLexID), numRegLRA(numReg), doBankConflict(bankConflict),
@@ -1943,8 +1946,9 @@ bool globalLinearScan::runLinearScan(IR_Builder &builder,
19431946
lr->getFirstRef(idx);
19441947
if (!lr->isEOT() && !lr->getAssigned()) {
19451948
// Add forbidden for preAssigned registers
1949+
auto isTopDclPseudoVCA = builder.kernel.fg.isPseudoVCADcl(lr->getTopDcl());
19461950
for (auto preAssginedLI : *preAssignedIntervals) {
1947-
if (builder.kernel.fg.isPseudoVCADcl(lr->getTopDcl()) &&
1951+
if (isTopDclPseudoVCA &&
19481952
(builder.isPreDefRet(preAssginedLI->getTopDcl()) ||
19491953
builder.isPreDefArg(preAssginedLI->getTopDcl()))) {
19501954
continue;

visa/LinearScanRA.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,9 @@ class LSLiveRange {
319319
}
320320
void addRetRegs(unsigned int f) { retGRFs[f] = true; }
321321
const bool *getRetGRFs() { return retGRFs; }
322+
void clearRetGRF(unsigned GRFSize) {
323+
memset(retGRFs, false, GRFSize);
324+
}
322325
void clearForbiddenGRF(unsigned GRFSize) {
323326
if (retGRFs) {
324327
memset(retGRFs, false, GRFSize);
@@ -359,7 +362,7 @@ class globalLinearScan {
359362
private:
360363
GlobalRA &gra;
361364
IR_Builder &builder;
362-
Mem_Manager GLSMem;
365+
Mem_Manager *GLSMem;
363366
PhyRegsManager &pregManager;
364367
std::vector<LSLiveRange *> &liveIntervals;
365368
std::vector<LSLiveRange *> *preAssignedIntervals;
@@ -404,7 +407,7 @@ class globalLinearScan {
404407
&inputLivelIntervals,
405408
PhyRegsManager &pregMgr, unsigned int numReg,
406409
unsigned int numEOT, unsigned int lastLexID,
407-
bool bankConflict, bool internalConflict);
410+
bool bankConflict, bool internalConflict, Mem_Manager* GLSMem);
408411

409412
void getCalleeSaveGRF(std::vector<unsigned int> &regNum, G4_Kernel *kernel);
410413

0 commit comments

Comments
 (0)