Commit b07aba07 by Olli Etuaho

Make sure type gets set consistently in folded binary operations

Add a wrapper function that handles creating the folded node and setting the right parameters on it, so that the folding function handles only calculating the folded values. This will fix the precision set to constant folded values in some cases. Previously the precision was always set to be equal to one of the operands to the binary operation, but now both operands are taken into account. Folding binary operations is now in a separate function from folding unary operations. TEST=dEQP-GLES3.functional.shaders.constant_expressions.* BUG=angleproject:817 Change-Id: Id97e765173c6110f49607e21c3fb90019b1ebac7 Reviewed-on: https://chromium-review.googlesource.com/274001Reviewed-by: 's avatarJamie Madill <jmadill@chromium.org> Tested-by: 's avatarOlli Etuaho <oetuaho@nvidia.com>
parent b50788d1
......@@ -747,68 +747,76 @@ bool TIntermBinary::promote(TInfoSink &infoSink)
return true;
}
TIntermTyped *TIntermBinary::fold(TInfoSink &infoSink)
{
TIntermConstantUnion *leftConstant = mLeft->getAsConstantUnion();
TIntermConstantUnion *rightConstant = mRight->getAsConstantUnion();
if (leftConstant == nullptr || rightConstant == nullptr)
{
return nullptr;
}
TConstantUnion *constArray = leftConstant->foldBinary(mOp, rightConstant, infoSink);
if (constArray == nullptr)
{
return nullptr;
}
TIntermTyped *folded = new TIntermConstantUnion(constArray, getType());
folded->getTypePointer()->setQualifier(EvqConst);
folded->setLine(getLine());
return folded;
}
//
// The fold functions see if an operation on a constant can be done in place,
// without generating run-time code.
//
// Returns the node to keep using, which may or may not be the node passed in.
// Returns the constant value to keep using or nullptr.
//
TIntermTyped *TIntermConstantUnion::fold(
TOperator op, TIntermConstantUnion *rightNode, TInfoSink &infoSink)
TConstantUnion *TIntermConstantUnion::foldBinary(TOperator op, TIntermConstantUnion *rightNode, TInfoSink &infoSink)
{
TConstantUnion *unionArray = getUnionArrayPointer();
TConstantUnion *leftArray = getUnionArrayPointer();
TConstantUnion *rightArray = rightNode->getUnionArrayPointer();
if (!unionArray)
if (!leftArray)
return nullptr;
if (!rightArray)
return nullptr;
size_t objectSize = getType().getObjectSize();
if (rightNode)
{
// binary operations
TConstantUnion *rightUnionArray = rightNode->getUnionArrayPointer();
TType returnType = getType();
if (!rightUnionArray)
return nullptr;
// for a case like float f = vec4(2, 3, 4, 5) + 1.2;
if (rightNode->getType().getObjectSize() == 1 && objectSize > 1)
{
rightUnionArray = Vectorize(*rightNode->getUnionArrayPointer(), objectSize);
returnType = getType();
rightArray = Vectorize(*rightNode->getUnionArrayPointer(), objectSize);
}
else if (rightNode->getType().getObjectSize() > 1 && objectSize == 1)
{
// for a case like float f = 1.2 + vec4(2, 3, 4, 5);
unionArray = Vectorize(*getUnionArrayPointer(), rightNode->getType().getObjectSize());
returnType = rightNode->getType();
leftArray = Vectorize(*getUnionArrayPointer(), rightNode->getType().getObjectSize());
objectSize = rightNode->getType().getObjectSize();
}
TConstantUnion *tempConstArray = nullptr;
TIntermConstantUnion *tempNode;
TConstantUnion *resultArray = nullptr;
bool boolNodeFlag = false;
switch(op)
{
case EOpAdd:
tempConstArray = new TConstantUnion[objectSize];
resultArray = new TConstantUnion[objectSize];
for (size_t i = 0; i < objectSize; i++)
tempConstArray[i] = unionArray[i] + rightUnionArray[i];
resultArray[i] = leftArray[i] + rightArray[i];
break;
case EOpSub:
tempConstArray = new TConstantUnion[objectSize];
resultArray = new TConstantUnion[objectSize];
for (size_t i = 0; i < objectSize; i++)
tempConstArray[i] = unionArray[i] - rightUnionArray[i];
resultArray[i] = leftArray[i] - rightArray[i];
break;
case EOpMul:
case EOpVectorTimesScalar:
case EOpMatrixTimesScalar:
tempConstArray = new TConstantUnion[objectSize];
resultArray = new TConstantUnion[objectSize];
for (size_t i = 0; i < objectSize; i++)
tempConstArray[i] = unionArray[i] * rightUnionArray[i];
resultArray[i] = leftArray[i] * rightArray[i];
break;
case EOpMatrixTimesMatrix:
......@@ -829,109 +837,90 @@ TIntermTyped *TIntermConstantUnion::fold(
const int resultCols = rightCols;
const int resultRows = leftRows;
tempConstArray = new TConstantUnion[resultCols * resultRows];
resultArray = new TConstantUnion[resultCols * resultRows];
for (int row = 0; row < resultRows; row++)
{
for (int column = 0; column < resultCols; column++)
{
tempConstArray[resultRows * column + row].setFConst(0.0f);
resultArray[resultRows * column + row].setFConst(0.0f);
for (int i = 0; i < leftCols; i++)
{
tempConstArray[resultRows * column + row].setFConst(
tempConstArray[resultRows * column + row].getFConst() +
unionArray[i * leftRows + row].getFConst() *
rightUnionArray[column * rightRows + i].getFConst());
resultArray[resultRows * column + row].setFConst(
resultArray[resultRows * column + row].getFConst() +
leftArray[i * leftRows + row].getFConst() *
rightArray[column * rightRows + i].getFConst());
}
}
}
// update return type for matrix product
returnType.setPrimarySize(static_cast<unsigned char>(resultCols));
returnType.setSecondarySize(static_cast<unsigned char>(resultRows));
}
break;
case EOpDiv:
case EOpIMod:
{
tempConstArray = new TConstantUnion[objectSize];
resultArray = new TConstantUnion[objectSize];
for (size_t i = 0; i < objectSize; i++)
{
switch (getType().getBasicType())
{
case EbtFloat:
if (rightUnionArray[i] == 0.0f)
if (rightArray[i] == 0.0f)
{
infoSink.info.message(
EPrefixWarning, getLine(),
infoSink.info.message(EPrefixWarning, getLine(),
"Divide by zero error during constant folding");
tempConstArray[i].setFConst(
unionArray[i].getFConst() < 0 ? -FLT_MAX : FLT_MAX);
resultArray[i].setFConst(leftArray[i].getFConst() < 0 ? -FLT_MAX : FLT_MAX);
}
else
{
ASSERT(op == EOpDiv);
tempConstArray[i].setFConst(
unionArray[i].getFConst() /
rightUnionArray[i].getFConst());
resultArray[i].setFConst(leftArray[i].getFConst() / rightArray[i].getFConst());
}
break;
case EbtInt:
if (rightUnionArray[i] == 0)
if (rightArray[i] == 0)
{
infoSink.info.message(
EPrefixWarning, getLine(),
infoSink.info.message(EPrefixWarning, getLine(),
"Divide by zero error during constant folding");
tempConstArray[i].setIConst(INT_MAX);
resultArray[i].setIConst(INT_MAX);
}
else
{
if (op == EOpDiv)
{
tempConstArray[i].setIConst(
unionArray[i].getIConst() /
rightUnionArray[i].getIConst());
resultArray[i].setIConst(leftArray[i].getIConst() / rightArray[i].getIConst());
}
else
{
ASSERT(op == EOpIMod);
tempConstArray[i].setIConst(
unionArray[i].getIConst() %
rightUnionArray[i].getIConst());
resultArray[i].setIConst(leftArray[i].getIConst() % rightArray[i].getIConst());
}
}
break;
case EbtUInt:
if (rightUnionArray[i] == 0)
if (rightArray[i] == 0)
{
infoSink.info.message(
EPrefixWarning, getLine(),
infoSink.info.message(EPrefixWarning, getLine(),
"Divide by zero error during constant folding");
tempConstArray[i].setUConst(UINT_MAX);
resultArray[i].setUConst(UINT_MAX);
}
else
{
if (op == EOpDiv)
{
tempConstArray[i].setUConst(
unionArray[i].getUConst() /
rightUnionArray[i].getUConst());
resultArray[i].setUConst(leftArray[i].getUConst() / rightArray[i].getUConst());
}
else
{
ASSERT(op == EOpIMod);
tempConstArray[i].setUConst(
unionArray[i].getUConst() %
rightUnionArray[i].getUConst());
resultArray[i].setUConst(leftArray[i].getUConst() % rightArray[i].getUConst());
}
}
break;
default:
infoSink.info.message(
EPrefixInternalError, getLine(),
infoSink.info.message(EPrefixInternalError, getLine(),
"Constant folding cannot be done for \"/\"");
return nullptr;
}
......@@ -943,8 +932,7 @@ TIntermTyped *TIntermConstantUnion::fold(
{
if (rightNode->getBasicType() != EbtFloat)
{
infoSink.info.message(
EPrefixInternalError, getLine(),
infoSink.info.message(EPrefixInternalError, getLine(),
"Constant Folding cannot be done for matrix times vector");
return nullptr;
}
......@@ -952,35 +940,26 @@ TIntermTyped *TIntermConstantUnion::fold(
const int matrixCols = getCols();
const int matrixRows = getRows();
tempConstArray = new TConstantUnion[matrixRows];
resultArray = new TConstantUnion[matrixRows];
for (int matrixRow = 0; matrixRow < matrixRows; matrixRow++)
{
tempConstArray[matrixRow].setFConst(0.0f);
resultArray[matrixRow].setFConst(0.0f);
for (int col = 0; col < matrixCols; col++)
{
tempConstArray[matrixRow].setFConst(
tempConstArray[matrixRow].getFConst() +
unionArray[col * matrixRows + matrixRow].getFConst() *
rightUnionArray[col].getFConst());
resultArray[matrixRow].setFConst(resultArray[matrixRow].getFConst() +
leftArray[col * matrixRows + matrixRow].getFConst() *
rightArray[col].getFConst());
}
}
returnType = rightNode->getType();
returnType.setPrimarySize(static_cast<unsigned char>(matrixRows));
tempNode = new TIntermConstantUnion(tempConstArray, returnType);
tempNode->setLine(getLine());
return tempNode;
}
break;
case EOpVectorTimesMatrix:
{
if (getType().getBasicType() != EbtFloat)
{
infoSink.info.message(
EPrefixInternalError, getLine(),
infoSink.info.message(EPrefixInternalError, getLine(),
"Constant Folding cannot be done for vector times matrix");
return nullptr;
}
......@@ -988,58 +967,50 @@ TIntermTyped *TIntermConstantUnion::fold(
const int matrixCols = rightNode->getType().getCols();
const int matrixRows = rightNode->getType().getRows();
tempConstArray = new TConstantUnion[matrixCols];
resultArray = new TConstantUnion[matrixCols];
for (int matrixCol = 0; matrixCol < matrixCols; matrixCol++)
{
tempConstArray[matrixCol].setFConst(0.0f);
resultArray[matrixCol].setFConst(0.0f);
for (int matrixRow = 0; matrixRow < matrixRows; matrixRow++)
{
tempConstArray[matrixCol].setFConst(
tempConstArray[matrixCol].getFConst() +
unionArray[matrixRow].getFConst() *
rightUnionArray[matrixCol * matrixRows + matrixRow].getFConst());
resultArray[matrixCol].setFConst(resultArray[matrixCol].getFConst() +
leftArray[matrixRow].getFConst() *
rightArray[matrixCol * matrixRows + matrixRow].getFConst());
}
}
returnType.setPrimarySize(static_cast<unsigned char>(matrixCols));
}
break;
case EOpLogicalAnd:
// this code is written for possible future use,
// will not get executed currently
{
tempConstArray = new TConstantUnion[objectSize];
resultArray = new TConstantUnion[objectSize];
for (size_t i = 0; i < objectSize; i++)
{
tempConstArray[i] = unionArray[i] && rightUnionArray[i];
resultArray[i] = leftArray[i] && rightArray[i];
}
}
break;
case EOpLogicalOr:
// this code is written for possible future use,
// will not get executed currently
{
tempConstArray = new TConstantUnion[objectSize];
resultArray = new TConstantUnion[objectSize];
for (size_t i = 0; i < objectSize; i++)
{
tempConstArray[i] = unionArray[i] || rightUnionArray[i];
resultArray[i] = leftArray[i] || rightArray[i];
}
}
break;
case EOpLogicalXor:
{
tempConstArray = new TConstantUnion[objectSize];
resultArray = new TConstantUnion[objectSize];
for (size_t i = 0; i < objectSize; i++)
{
switch (getType().getBasicType())
{
case EbtBool:
tempConstArray[i].setBConst(
unionArray[i] == rightUnionArray[i] ? false : true);
resultArray[i].setBConst(leftArray[i] != rightArray[i]);
break;
default:
UNREACHABLE();
......@@ -1050,142 +1021,85 @@ TIntermTyped *TIntermConstantUnion::fold(
break;
case EOpBitwiseAnd:
tempConstArray = new TConstantUnion[objectSize];
resultArray = new TConstantUnion[objectSize];
for (size_t i = 0; i < objectSize; i++)
tempConstArray[i] = unionArray[i] & rightUnionArray[i];
resultArray[i] = leftArray[i] & rightArray[i];
break;
case EOpBitwiseXor:
tempConstArray = new TConstantUnion[objectSize];
resultArray = new TConstantUnion[objectSize];
for (size_t i = 0; i < objectSize; i++)
tempConstArray[i] = unionArray[i] ^ rightUnionArray[i];
resultArray[i] = leftArray[i] ^ rightArray[i];
break;
case EOpBitwiseOr:
tempConstArray = new TConstantUnion[objectSize];
resultArray = new TConstantUnion[objectSize];
for (size_t i = 0; i < objectSize; i++)
tempConstArray[i] = unionArray[i] | rightUnionArray[i];
resultArray[i] = leftArray[i] | rightArray[i];
break;
case EOpBitShiftLeft:
tempConstArray = new TConstantUnion[objectSize];
resultArray = new TConstantUnion[objectSize];
for (size_t i = 0; i < objectSize; i++)
tempConstArray[i] = unionArray[i] << rightUnionArray[i];
resultArray[i] = leftArray[i] << rightArray[i];
break;
case EOpBitShiftRight:
tempConstArray = new TConstantUnion[objectSize];
resultArray = new TConstantUnion[objectSize];
for (size_t i = 0; i < objectSize; i++)
tempConstArray[i] = unionArray[i] >> rightUnionArray[i];
resultArray[i] = leftArray[i] >> rightArray[i];
break;
case EOpLessThan:
ASSERT(objectSize == 1);
tempConstArray = new TConstantUnion[1];
tempConstArray->setBConst(*unionArray < *rightUnionArray);
returnType = TType(EbtBool, EbpUndefined, EvqConst);
resultArray = new TConstantUnion[1];
resultArray->setBConst(*leftArray < *rightArray);
break;
case EOpGreaterThan:
ASSERT(objectSize == 1);
tempConstArray = new TConstantUnion[1];
tempConstArray->setBConst(*unionArray > *rightUnionArray);
returnType = TType(EbtBool, EbpUndefined, EvqConst);
resultArray = new TConstantUnion[1];
resultArray->setBConst(*leftArray > *rightArray);
break;
case EOpLessThanEqual:
{
ASSERT(objectSize == 1);
TConstantUnion constant;
constant.setBConst(*unionArray > *rightUnionArray);
tempConstArray = new TConstantUnion[1];
tempConstArray->setBConst(!constant.getBConst());
returnType = TType(EbtBool, EbpUndefined, EvqConst);
resultArray = new TConstantUnion[1];
resultArray->setBConst(!(*leftArray > *rightArray));
break;
}
case EOpGreaterThanEqual:
{
ASSERT(objectSize == 1);
TConstantUnion constant;
constant.setBConst(*unionArray < *rightUnionArray);
tempConstArray = new TConstantUnion[1];
tempConstArray->setBConst(!constant.getBConst());
returnType = TType(EbtBool, EbpUndefined, EvqConst);
resultArray = new TConstantUnion[1];
resultArray->setBConst(!(*leftArray < *rightArray));
break;
}
case EOpEqual:
if (getType().getBasicType() == EbtStruct)
{
if (!CompareStructure(rightNode->getType(),
rightNode->getUnionArrayPointer(),
unionArray))
{
boolNodeFlag = true;
}
}
else
{
for (size_t i = 0; i < objectSize; i++)
{
if (unionArray[i] != rightUnionArray[i])
{
boolNodeFlag = true;
break; // break out of for loop
}
}
}
tempConstArray = new TConstantUnion[1];
if (!boolNodeFlag)
{
tempConstArray->setBConst(true);
}
else
{
tempConstArray->setBConst(false);
}
tempNode = new TIntermConstantUnion(
tempConstArray, TType(EbtBool, EbpUndefined, EvqConst));
tempNode->setLine(getLine());
return tempNode;
case EOpNotEqual:
if (getType().getBasicType() == EbtStruct)
{
if (CompareStructure(rightNode->getType(),
rightNode->getUnionArrayPointer(),
unionArray))
resultArray = new TConstantUnion[1];
bool equal = true;
if (getType().getBasicType() == EbtStruct)
{
boolNodeFlag = true;
}
equal = CompareStructure(getType(), rightArray, leftArray);
}
else
{
for (size_t i = 0; i < objectSize; i++)
{
if (unionArray[i] == rightUnionArray[i])
if (leftArray[i] != rightArray[i])
{
boolNodeFlag = true;
equal = false;
break; // break out of for loop
}
}
}
tempConstArray = new TConstantUnion[1];
if (!boolNodeFlag)
if (op == EOpEqual)
{
tempConstArray->setBConst(true);
resultArray->setBConst(equal);
}
else
{
tempConstArray->setBConst(false);
resultArray->setBConst(!equal);
}
tempNode = new TIntermConstantUnion(
tempConstArray, TType(EbtBool, EbpUndefined, EvqConst));
tempNode->setLine(getLine());
return tempNode;
}
break;
default:
infoSink.info.message(
......@@ -1193,12 +1107,25 @@ TIntermTyped *TIntermConstantUnion::fold(
"Invalid operator for constant folding");
return nullptr;
}
tempNode = new TIntermConstantUnion(tempConstArray, returnType);
tempNode->setLine(getLine());
return resultArray;
}
return tempNode;
}
else if (op == EOpAny || op == EOpAll || op == EOpLength)
//
// The fold functions see if an operation on a constant can be done in place,
// without generating run-time code.
//
// Returns the node to keep using or nullptr.
//
TIntermTyped *TIntermConstantUnion::foldUnary(TOperator op, TInfoSink &infoSink)
{
TConstantUnion *unionArray = getUnionArrayPointer();
if (!unionArray)
return nullptr;
size_t objectSize = getType().getObjectSize();
if (op == EOpAny || op == EOpAll || op == EOpLength)
{
// Do operations where the return type is different from the operand type.
......
......@@ -299,7 +299,8 @@ class TIntermConstantUnion : public TIntermTyped
virtual void traverse(TIntermTraverser *);
virtual bool replaceChildNode(TIntermNode *, TIntermNode *) { return false; }
TIntermTyped *fold(TOperator op, TIntermConstantUnion *rightNode, TInfoSink &infoSink);
TConstantUnion *foldBinary(TOperator op, TIntermConstantUnion *rightNode, TInfoSink &infoSink);
TIntermTyped *foldUnary(TOperator op, TInfoSink &infoSink);
static TIntermTyped *FoldAggregateBuiltIn(TOperator op, TIntermAggregate *aggregate, TInfoSink &infoSink);
......@@ -362,6 +363,7 @@ class TIntermBinary : public TIntermOperator
TIntermTyped *getLeft() const { return mLeft; }
TIntermTyped *getRight() const { return mRight; }
bool promote(TInfoSink &);
TIntermTyped *fold(TInfoSink &infoSink);
void setAddIndexClamp() { mAddIndexClamp = true; }
bool getAddIndexClamp() { return mAddIndexClamp; }
......
......@@ -57,19 +57,10 @@ TIntermTyped *TIntermediate::addBinaryMath(
if (!node->promote(mInfoSink))
return NULL;
//
// See if we can fold constants.
//
TIntermConstantUnion *leftTempConstant = left->getAsConstantUnion();
TIntermConstantUnion *rightTempConstant = right->getAsConstantUnion();
if (leftTempConstant && rightTempConstant)
{
TIntermTyped *typedReturnNode =
leftTempConstant->fold(node->getOp(), rightTempConstant, mInfoSink);
if (typedReturnNode)
return typedReturnNode;
}
TIntermTyped *foldedNode = node->fold(mInfoSink);
if (foldedNode)
return foldedNode;
return node;
}
......@@ -143,7 +134,7 @@ TIntermTyped *TIntermediate::addUnaryMath(
if (childTempConstant)
{
TIntermTyped *newChild = childTempConstant->fold(op, nullptr, mInfoSink);
TIntermTyped *newChild = childTempConstant->foldUnary(op, mInfoSink);
if (newChild)
return newChild;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment