Skip to content

Commit

Permalink
[Op][Backend][Binding] Add ReverseOp (Fix #42)
Browse files Browse the repository at this point in the history
  • Loading branch information
chhzh123 committed Mar 17, 2022
1 parent 031e3e9 commit 0c2f9ee
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
10 changes: 10 additions & 0 deletions include/hcl/Bindings/Python/hcl/build_ir.py
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,11 @@ def __setitem__(self, indices, expr):
indices = indices[0]
return SetBitOp(self, indices, expr)

def reverse(self):
if not is_integer_type(self.dtype):
raise RuntimeError("Only integers can reverse the bits")
return BitReverseOp(self)

def __nonzero__(self):
raise RuntimeError(
"1) Cannot use and / or / not operator to Expr, "
Expand Down Expand Up @@ -1057,6 +1062,11 @@ def __init__(self, val):
) # use the same op


class BitReverseOp(UnaryOp):
def __init__(self, val):
super().__init__(hcl_d.BitReverseOp, val.dtype, val)


class BitCastOp(UnaryOp):
def __init__(self, dtype, val):
super().__init__(arith.BitcastOp, dtype, val)
Expand Down
9 changes: 9 additions & 0 deletions include/hcl/Dialect/HeteroCLOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -441,4 +441,13 @@ def SetIntSliceOp : HeteroCL_Op<"set_slice"> {
}];
}

def BitReverseOp : HeteroCL_Op<"bit_reverse", [SameOperandsAndResultType]> {
let summary = "reverse bits of an integer";
let arguments = (ins SignlessIntegerLike:$num);
let results = (outs SignlessIntegerLike:$result);
let assemblyFormat = [{
`(` $num `:` type($num) `)` attr-dict
}];
}

#endif // HCL_OPS
3 changes: 2 additions & 1 deletion include/hcl/Dialect/Visitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class HLSCppVisitorBase {
// Logical expressions.
arith::XOrIOp, arith::AndIOp, arith::OrIOp, arith::ShLIOp,
arith::ShRSIOp, arith::ShRUIOp, hcl::GetIntBitOp, hcl::SetIntBitOp,
hcl::GetIntSliceOp, hcl::SetIntSliceOp,
hcl::GetIntSliceOp, hcl::SetIntSliceOp, hcl::BitReverseOp,
// Special operations.
CallOp, ReturnOp, SelectOp, ConstantOp, arith::ConstantOp,
arith::TruncIOp, arith::TruncFOp, arith::ExtUIOp, arith::ExtSIOp,
Expand Down Expand Up @@ -186,6 +186,7 @@ class HLSCppVisitorBase {
HANDLE(hcl::SetIntBitOp);
HANDLE(hcl::GetIntSliceOp);
HANDLE(hcl::SetIntSliceOp);
HANDLE(hcl::BitReverseOp);

// Special operations.
HANDLE(CallOp);
Expand Down
15 changes: 15 additions & 0 deletions lib/Translation/EmitHLSCpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ class ModuleEmitter : public HCLEmitterBase {
void emitSetBit(hcl::SetIntBitOp op);
void emitGetSlice(hcl::GetIntSliceOp op);
void emitSetSlice(hcl::SetIntSliceOp op);
void emitBitReverse(hcl::BitReverseOp op);
void emitBitcast(arith::BitcastOp op);

/// Top-level MLIR module emitter.
Expand Down Expand Up @@ -503,6 +504,9 @@ class ExprVisitor : public HLSCppVisitorBase<ExprVisitor, bool> {
bool visitOp(hcl::SetIntBitOp op) { return emitter.emitSetBit(op), true; }
bool visitOp(hcl::GetIntSliceOp op) { return emitter.emitGetSlice(op), true; }
bool visitOp(hcl::SetIntSliceOp op) { return emitter.emitSetSlice(op), true; }
bool visitOp(hcl::BitReverseOp op) {
return emitter.emitBitReverse(op), true;
}

/// Unary expressions.
bool visitOp(math::AbsOp op) { return emitter.emitUnary(op, "abs"), true; }
Expand Down Expand Up @@ -1407,6 +1411,17 @@ void ModuleEmitter::emitSetSlice(hcl::SetIntSliceOp op) {
emitInfoAndNewLine(op);
}

void ModuleEmitter::emitBitReverse(hcl::BitReverseOp op) {
indent();
Value result = op.getResult();
fixUnsignedType(result, op->hasAttr("unsigned"));
emitValue(result);
os << " = ";
emitValue(op.num());
os << ".reverse();";
emitInfoAndNewLine(op);
}

void ModuleEmitter::emitSelect(SelectOp op) {
unsigned rank = emitNestedLoopHead(op.getResult());
unsigned conditionRank = rank;
Expand Down

0 comments on commit 0c2f9ee

Please sign in to comment.