Skip to content

Commit

Permalink
[ImportVerilog] implement streaming concat operation with concat & ex…
Browse files Browse the repository at this point in the history
…tract operation
  • Loading branch information
chenbo-again committed Nov 12, 2024
1 parent af2794f commit be289f3
Showing 1 changed file with 117 additions and 16 deletions.
133 changes: 117 additions & 16 deletions lib/Conversion/ImportVerilog/Expressions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -798,19 +798,70 @@ struct RvalueExprVisitor {
}

Value visit(const slang::ast::StreamingConcatenationExpression &expr) {
SmallVector<Value> arguments;
SmallVector<Value> operands;
for (auto stream: expr.streams()) {
auto value = context.convertRvalueExpression(*stream.operand);
// mlir::emitError(value.getLoc(), "expression of type ")
// << value.getType();
if (!value)
continue;
value = context.convertToSimpleBitVector(value);
operands.push_back(value);
}
if(expr.sliceSize == 0) {
return builder.create<moore::ConcatOp>(loc, operands);
}

if(!value) {
mlir::emitError(loc) << "error occour";
SmallVector<Value> slicedOperands;
SmallVector<Value> tempOperands;
size_t currentWidth = 0;
for (auto value : operands) {
auto sliceSize = expr.sliceSize;
tempOperands.push_back(value);
currentWidth += cast<moore::IntType>(value.getType()).getWidth();

while(currentWidth >= sliceSize) {
// mlir::emitError(loc) <<currentWidth;
auto lastValue = tempOperands.back();
tempOperands.pop_back();
auto type = cast<moore::IntType>(lastValue.getType());

if(currentWidth == sliceSize) {
// do not need extract operation
if(tempOperands.empty()) {
slicedOperands.push_back(lastValue);
} else {
tempOperands.push_back(lastValue);
auto concatOp = builder.create<moore::ConcatOp>(loc, tempOperands);
slicedOperands.push_back(concatOp.getResult());
}
tempOperands.clear();
} else {
auto remainWidth = currentWidth - sliceSize;
auto extractWidth = type.getWidth() - remainWidth;
auto extractResultType = moore::IntType::get(context.getContext(),
extractWidth, type.getDomain());
auto remainExtractResultType = moore::IntType::get(context.getContext(),
remainWidth, type.getDomain());

auto extractOp = builder.create<moore::ExtractOp>(loc, extractResultType, lastValue, 0);
auto remainExtractOp = builder.create<moore::ExtractOp>(loc, remainExtractResultType, lastValue, extractWidth);

if(tempOperands.empty()) {
slicedOperands.push_back(extractOp.getResult());
} else {
tempOperands.push_back(extractOp.getResult());
auto concatOp = builder.create<moore::ConcatOp>(loc, tempOperands);
slicedOperands.push_back(concatOp.getResult());
}
tempOperands.clear();
tempOperands.push_back(remainExtractOp.getResult());
}
currentWidth -= sliceSize;
// mlir::emitError(loc) <<currentWidth;
}
arguments.push_back(value);
}
return builder.create<moore::StreamingConcatOp>(loc, arguments,
builder.getUI32IntegerAttr(expr.sliceSize));

std::reverse(slicedOperands.begin(), slicedOperands.end());
return builder.create<moore::ConcatOp>(loc, slicedOperands);
}

/// Emit an error for all other expressions.
Expand Down Expand Up @@ -952,19 +1003,69 @@ struct LvalueExprVisitor {
}

Value visit(const slang::ast::StreamingConcatenationExpression &expr) {
SmallVector<Value> arguments;
SmallVector<Value> operands;
for (auto stream: expr.streams()) {
auto value = context.convertLvalueExpression(*stream.operand);
// mlir::emitError(value.getLoc(), "expression of type ")
// << value.getType();
if (!value)
continue;
operands.push_back(value);
}
if(expr.sliceSize == 0) {
return builder.create<moore::ConcatRefOp>(loc, operands);
}

if(!value) {
mlir::emitError(loc) << "error occour";
SmallVector<Value> slicedOperands;
SmallVector<Value> tempOperands;
size_t currentWidth = 0;
for (auto value : operands) {
auto sliceSize = expr.sliceSize;
tempOperands.push_back(value);
currentWidth += cast<moore::IntType>(cast<moore::RefType>(value.getType()).getNestedType()).getWidth();

while(currentWidth >= sliceSize) {
// mlir::emitError(loc) <<currentWidth;
auto lastValue = tempOperands.back();
tempOperands.pop_back();
auto type = cast<moore::IntType>(cast<moore::RefType>(lastValue.getType()).getNestedType());

if(currentWidth == sliceSize) {
// do not need extract operation
if(tempOperands.empty()) {
slicedOperands.push_back(lastValue);
} else {
tempOperands.push_back(lastValue);
auto concatOp = builder.create<moore::ConcatRefOp>(loc, tempOperands);
slicedOperands.push_back(concatOp.getResult());
}
tempOperands.clear();
} else {
auto remainWidth = currentWidth - sliceSize;
auto extractWidth = type.getWidth() - remainWidth;
auto extractResultType = moore::RefType::get(moore::IntType::get(context.getContext(),
extractWidth, type.getDomain()));
auto remainExtractResultType = moore::RefType::get(moore::IntType::get(context.getContext(),
remainWidth, type.getDomain()));

auto extractOp = builder.create<moore::ExtractRefOp>(loc, extractResultType, lastValue, 0);
auto remainExtractOp = builder.create<moore::ExtractRefOp>(loc, remainExtractResultType, lastValue, extractWidth);

if(tempOperands.empty()) {
slicedOperands.push_back(extractOp.getResult());
} else {
tempOperands.push_back(extractOp.getResult());
auto concatOp = builder.create<moore::ConcatRefOp>(loc, tempOperands);
slicedOperands.push_back(concatOp.getResult());
}
tempOperands.clear();
tempOperands.push_back(remainExtractOp.getResult());
}
currentWidth -= sliceSize;
// mlir::emitError(loc) <<currentWidth;
}
arguments.push_back(value);
}
return builder.create<moore::StreamingConcatRefOp>(loc, arguments,
builder.getUI32IntegerAttr(expr.sliceSize));

std::reverse(slicedOperands.begin(), slicedOperands.end());
return builder.create<moore::ConcatRefOp>(loc, slicedOperands);
}

Value visit(const slang::ast::MemberAccessExpression &expr) {
Expand Down

0 comments on commit be289f3

Please sign in to comment.