Skip to content

Commit

Permalink
[ImportVerilog] reduce the amount of operations
Browse files Browse the repository at this point in the history
  • Loading branch information
chenbo-again committed Nov 13, 2024
1 parent be289f3 commit 6f6d3ae
Showing 1 changed file with 47 additions and 96 deletions.
143 changes: 47 additions & 96 deletions lib/Conversion/ImportVerilog/Expressions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "ImportVerilogInternals.h"
#include "circt/Dialect/Moore/MooreOps.h"
#include "circt/Dialect/Moore/MooreTypes.h"
#include "mlir/IR/Diagnostics.h"
#include "slang/ast/Expression.h"
#include "slang/ast/SystemSubroutine.h"
#include "slang/ast/expressions/OperatorExpressions.h"
Expand Down Expand Up @@ -804,60 +805,36 @@ struct RvalueExprVisitor {
if (!value)
continue;
value = context.convertToSimpleBitVector(value);
if(!value) {
mlir::emitError(loc) << "Streaming concatentation do only support SimpleBitVector for now";
// return {};
}
operands.push_back(value);
}
auto value = builder.create<moore::ConcatOp>(loc, operands).getResult();

if(expr.sliceSize == 0) {
return builder.create<moore::ConcatOp>(loc, operands);
return value;
}

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;
}
auto iterMax = value.getType().getWidth()/expr.sliceSize;
auto remainSize = value.getType().getWidth() % expr.sliceSize;

for (size_t i=0; i<iterMax; i++) {
auto extractResultType = moore::IntType::get(context.getContext(),
expr.sliceSize, value.getType().getDomain());

auto extracted = builder.create<moore::ExtractOp>(loc, extractResultType, value, i*expr.sliceSize);
slicedOperands.push_back(extracted);
}
// handle other wire
if(remainSize) {
auto extractResultType = moore::IntType::get(context.getContext(),
remainSize, value.getType().getDomain());

auto extracted = builder.create<moore::ExtractOp>(loc, extractResultType, value, iterMax*expr.sliceSize);
slicedOperands.push_back(extracted);
}

std::reverse(slicedOperands.begin(), slicedOperands.end());
Expand Down Expand Up @@ -1010,58 +987,32 @@ struct LvalueExprVisitor {
continue;
operands.push_back(value);
}
auto value = builder.create<moore::ConcatRefOp>(loc, operands).getResult();

if(expr.sliceSize == 0) {
return builder.create<moore::ConcatRefOp>(loc, operands);
return value;
}

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;
}
auto widthSum = cast<moore::IntType>(value.getType().getNestedType()).getWidth();
auto domain = cast<moore::IntType>(value.getType().getNestedType()).getDomain();
auto iterMax = widthSum/expr.sliceSize;
auto remainSize = widthSum % expr.sliceSize;

for (size_t i=0; i<iterMax; i++) {
auto extractResultType = moore::RefType::get(moore::IntType::get(context.getContext(),
expr.sliceSize, domain));

auto extracted = builder.create<moore::ExtractRefOp>(loc, extractResultType, value, i*expr.sliceSize);
slicedOperands.push_back(extracted);
}
// handle other wire
if(remainSize) {
auto extractResultType = moore::RefType::get(moore::IntType::get(context.getContext(),
remainSize, domain));

auto extracted = builder.create<moore::ExtractRefOp>(loc, extractResultType, value, iterMax*expr.sliceSize);
slicedOperands.push_back(extracted);
}

std::reverse(slicedOperands.begin(), slicedOperands.end());
Expand Down

0 comments on commit 6f6d3ae

Please sign in to comment.