Skip to content

Commit 1581612

Browse files
kychendevigcbot
authored andcommitted
Revise constant folding for math instruction.
When performing constant folding for math instruction, create a new instruction with correct type to prevent error in subsequent passes including rematerialization.
1 parent a57b60c commit 1581612

File tree

3 files changed

+45
-11
lines changed

3 files changed

+45
-11
lines changed

visa/BuildIR.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2381,6 +2381,7 @@ class IR_Builder {
23812381
G4_Imm *foldConstVal(G4_Imm* opnd, G4_INST *op);
23822382
G4_Imm *foldConstVal(G4_Imm *const1, G4_Imm *const2, G4_opcode op);
23832383
void doConsFolding(G4_INST *inst);
2384+
G4_INST *doMathConsFolding(INST_LIST_ITER &iter);
23842385
void doSimplification(G4_INST *inst);
23852386

23862387
static G4_Type findConstFoldCommonType(G4_Type type1, G4_Type type2);

visa/BuildIRImpl.cpp

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3248,17 +3248,7 @@ void IR_Builder::doConsFolding(G4_INST *inst) {
32483248
return op && op->isImm() && !op->isRelocImm();
32493249
};
32503250

3251-
if (inst->isMath() && inst->asMathInst()->isOneSrcMath()) {
3252-
G4_Operand* src0 = inst->getSrc(0);
3253-
if (srcIsFoldableImm(src0)) {
3254-
G4_Imm* foldedImm =
3255-
foldConstVal(src0->asImm(), inst);
3256-
if (foldedImm) {
3257-
inst->setOpcode(G4_mov);
3258-
inst->setSrc(foldedImm, 0);
3259-
}
3260-
}
3261-
} else if (inst->getNumSrc() == 2) {
3251+
if (inst->getNumSrc() == 2) {
32623252
G4_Operand *src0 = inst->getSrc(0);
32633253
G4_Operand *src1 = inst->getSrc(1);
32643254
if (srcIsFoldableImm(src0) && srcIsFoldableImm(src1)) {
@@ -3304,6 +3294,48 @@ void IR_Builder::doConsFolding(G4_INST *inst) {
33043294
} // TODO: integer mad, bfn, bfi, bfe
33053295
}
33063296
}
3297+
3298+
// Perform constant folding for math instructions
3299+
// op dst, Imm
3300+
// =>
3301+
// mov dst, Imm'
3302+
G4_INST *IR_Builder::doMathConsFolding(INST_LIST_ITER &iter) {
3303+
G4_INST *inst = *iter;
3304+
3305+
if (inst->getSaturate())
3306+
return inst;
3307+
3308+
auto srcIsFoldableImm = [](const G4_Operand *op) {
3309+
return op && op->isImm() && !op->isRelocImm();
3310+
};
3311+
3312+
if (inst->isMath() && inst->asMathInst()->isOneSrcMath()) {
3313+
G4_Operand *src0 = inst->getSrc(0);
3314+
if (srcIsFoldableImm(src0)) {
3315+
G4_Imm *foldedImm = foldConstVal(src0->asImm(), inst);
3316+
if (foldedImm) {
3317+
auto pred = duplicateOperand(inst->getPredicate());
3318+
auto condMod = duplicateOperand(inst->getCondMod());
3319+
auto dst = duplicateOperand(inst->getDst());
3320+
auto execSize = inst->getExecSize();
3321+
auto sat = inst->getSaturate();
3322+
auto options = inst->getOption();
3323+
3324+
G4_INST *newInst =
3325+
createInternalInst(pred, G4_mov, condMod, sat, execSize, dst,
3326+
foldedImm, nullptr, options);
3327+
newInst->inheritDIFrom(inst);
3328+
inst->transferUse(newInst);
3329+
*iter = newInst;
3330+
3331+
return newInst;
3332+
}
3333+
}
3334+
}
3335+
3336+
return inst;
3337+
}
3338+
33073339
// Do the following algebraic simplification:
33083340
// - mul v, src0, 0 ==> 0, commutative
33093341
// - and v, src0, 0 ==> 0, commutative

visa/Optimizer.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2453,6 +2453,7 @@ void Optimizer::localCopyPropagation() {
24532453
}
24542454

24552455
builder.doConsFolding(inst);
2456+
inst = builder.doMathConsFolding(ii);
24562457
builder.doSimplification(inst);
24572458

24582459
G4_INST::MovType MT = inst->canPropagate();

0 commit comments

Comments
 (0)