Skip to content

[clang][bytecode] Implement arithmetic, bitwise and compound assignment operator #108949

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 75 additions & 7 deletions clang/lib/AST/ByteCode/Compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1267,12 +1267,8 @@ bool Compiler<Emitter>::VisitVectorBinOp(const BinaryOperator *E) {
assert(E->getLHS()->getType()->isVectorType());
assert(E->getRHS()->getType()->isVectorType());

// FIXME: Current only support comparison binary operator, add support for
// other binary operator.
if (!E->isComparisonOp() && !E->isLogicalOp())
return this->emitInvalid(E);
// Prepare storage for result.
if (!Initializing) {
if (!Initializing && !E->isCompoundAssignmentOp()) {
unsigned LocalIndex = allocateTemporary(E);
if (!this->emitGetPtrLocal(LocalIndex, E))
return false;
Expand All @@ -1281,6 +1277,9 @@ bool Compiler<Emitter>::VisitVectorBinOp(const BinaryOperator *E) {
const Expr *LHS = E->getLHS();
const Expr *RHS = E->getRHS();
const auto *VecTy = E->getType()->getAs<VectorType>();
auto Op = E->isCompoundAssignmentOp()
? BinaryOperator::getOpForCompoundAssignment(E->getOpcode())
: E->getOpcode();

// The LHS and RHS of a comparison operator must have the same type. So we
// just use LHS vector element type here.
Expand All @@ -1301,6 +1300,17 @@ bool Compiler<Emitter>::VisitVectorBinOp(const BinaryOperator *E) {
if (!this->emitSetLocal(PT_Ptr, RHSOffset, E))
return false;

if (E->isCompoundAssignmentOp() && !this->emitGetLocal(PT_Ptr, LHSOffset, E))
return false;

// BitAdd/BitOr/BitXor/Shl/Shr doesn't support bool type, we need perform the
// integer promotion.
bool NeedIntPromot = ElemT == PT_Bool && (E->isBitwiseOp() || E->isShiftOp());
QualType PromotTy =
Ctx.getASTContext().getPromotedIntegerType(Ctx.getASTContext().BoolTy);
PrimType PromotT = classifyPrim(PromotTy);
PrimType OpT = NeedIntPromot ? PromotT : ElemT;

auto getElem = [=](unsigned Offset, unsigned Index) {
if (!this->emitGetLocal(PT_Ptr, Offset, E))
return false;
Expand All @@ -1311,16 +1321,63 @@ bool Compiler<Emitter>::VisitVectorBinOp(const BinaryOperator *E) {
return false;
if (!this->emitPrimCast(PT_Bool, ResultElemT, VecTy->getElementType(), E))
return false;
} else if (NeedIntPromot) {
if (!this->emitPrimCast(ElemT, PromotT, PromotTy, E))
return false;
}
return true;
};

#define EMIT_ARITH_OP(OP) \
{ \
if (ElemT == PT_Float) { \
if (!this->emit##OP##f(getFPOptions(E), E)) \
return false; \
} else { \
if (!this->emit##OP(ElemT, E)) \
return false; \
} \
break; \
}

for (unsigned I = 0; I != VecTy->getNumElements(); ++I) {
if (!getElem(LHSOffset, I))
return false;
if (!getElem(RHSOffset, I))
return false;
switch (E->getOpcode()) {
switch (Op) {
case BO_Add:
EMIT_ARITH_OP(Add)
case BO_Sub:
EMIT_ARITH_OP(Sub)
case BO_Mul:
EMIT_ARITH_OP(Mul)
case BO_Div:
EMIT_ARITH_OP(Div)
case BO_Rem:
if (!this->emitRem(ElemT, E))
return false;
break;
case BO_And:
if (!this->emitBitAnd(OpT, E))
return false;
break;
case BO_Or:
if (!this->emitBitOr(OpT, E))
return false;
break;
case BO_Xor:
if (!this->emitBitXor(OpT, E))
return false;
break;
case BO_Shl:
if (!this->emitShl(OpT, ElemT, E))
return false;
break;
case BO_Shr:
if (!this->emitShr(OpT, ElemT, E))
return false;
break;
case BO_EQ:
if (!this->emitEQ(ElemT, E))
return false;
Expand Down Expand Up @@ -1356,7 +1413,7 @@ bool Compiler<Emitter>::VisitVectorBinOp(const BinaryOperator *E) {
return false;
break;
default:
llvm_unreachable("Unsupported binary operator");
return this->emitInvalid(E);
}

// The result of the comparison is a vector of the same width and number
Expand All @@ -1371,10 +1428,19 @@ bool Compiler<Emitter>::VisitVectorBinOp(const BinaryOperator *E) {
return false;
}

// If we performed an integer promotion, we need to cast the compute result
// into result vector element type.
if (NeedIntPromot &&
!this->emitPrimCast(PromotT, ResultElemT, VecTy->getElementType(), E))
return false;

// Initialize array element with the value we just computed.
if (!this->emitInitElem(ResultElemT, I, E))
return false;
}

if (DiscardResult && E->isCompoundAssignmentOp() && !this->emitPopPtr(E))
return false;
return true;
}

Expand Down Expand Up @@ -2292,6 +2358,8 @@ bool Compiler<Emitter>::VisitPointerCompoundAssignOperator(
template <class Emitter>
bool Compiler<Emitter>::VisitCompoundAssignOperator(
const CompoundAssignOperator *E) {
if (E->getType()->isVectorType())
return VisitVectorBinOp(E);

const Expr *LHS = E->getLHS();
const Expr *RHS = E->getRHS();
Expand Down
Loading
Loading