Skip to content
Draft
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
10 changes: 3 additions & 7 deletions xls/codegen/vast/translate_vast_to_dslx.cc
Original file line number Diff line number Diff line change
Expand Up @@ -614,19 +614,16 @@ class VastToDslxTranslator {
dslx::Import * std,
dslx_builder_->GetOrImportModule(dslx::ImportTokens({"std"})));

XLS_ASSIGN_OR_RETURN(std::unique_ptr<dslx::Type> ct,
deduce_ctx().Deduce(args[0]));

const verilog::DataType* dt = vast_type_map_.at((*vast_call->args())[0]);
dslx::Span span = CreateNodeSpan(vast_call);
dslx::BitsType* bt = down_cast<dslx::BitsType*>(ct.get());
auto* ubits_type = module().Make<dslx::BuiltinTypeAnnotation>(
span, dslx::BuiltinType::kUN,
module().GetOrCreateBuiltinNameDef(dslx::BuiltinType::kUN));

XLS_ASSIGN_OR_RETURN(int64_t bit_width, bt->size().GetAsInt64());
XLS_ASSIGN_OR_RETURN(int64_t bit_width, dt->FlatBitCountAsInt64());
auto* bits_size = module().Make<dslx::Number>(
span, absl::StrCat(bit_width), dslx::NumberKind::kOther, nullptr);
if (bt->is_signed()) {
if (dt->is_signed()) {
auto* unsigned_type = module().Make<dslx::ArrayTypeAnnotation>(
span, ubits_type, bits_size);
args[0] = module().Make<dslx::Cast>(span, args[0], unsigned_type);
Expand All @@ -638,7 +635,6 @@ class VastToDslxTranslator {

XLS_ASSIGN_OR_RETURN(
result, dslx_builder_->CastToInferredVastType(vast_call, result));
XLS_RETURN_IF_ERROR(deduce_ctx().Deduce(result).status());
return result;
}
return absl::InvalidArgumentError(
Expand Down
25 changes: 25 additions & 0 deletions xls/codegen/vast/translate_vast_to_dslx_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -793,6 +793,10 @@ TEST_F(TranslateVastToDslxTest, Clog2) {
// parameter logic[$clog2(32767):0] var_6 = 32'shbeef;
// parameter logic[$clog2(32767):0] var_7 = -32'shbeef;
//
// parameter int a = 512;
// parameter int b = 2;
// parameter int clog_div = $clog2($clog2(a / b));
//
// endpackage
VerilogFileHelper f = CreateFile();
Module* p = f->AddModule("p", f.NextLoc());
Expand Down Expand Up @@ -854,6 +858,24 @@ TEST_F(TranslateVastToDslxTest, Clog2) {
/*declared_as_signed=*/true),
f.NextLoc()),
f.NextLoc());
ParameterRef* a =
p->AddParameter(f->Make<Def>(f.NextLoc(), "a", DataKind::kInteger,
f->Make<IntegerType>(f.NextLoc())),
f.BareLiteral(512), f.NextLoc());
ParameterRef* b =
p->AddParameter(f->Make<Def>(f.NextLoc(), "b", DataKind::kInteger,
f->Make<IntegerType>(f.NextLoc())),
f.BareLiteral(2), f.NextLoc());
p->AddParameter(
f->Make<Def>(f.NextLoc(), "clog_div", DataKind::kInteger,
f->Make<IntegerType>(f.NextLoc())),

f->Make<SystemFunctionCall>(
f.NextLoc(), "clog2",
std::vector<Expression*>{f->Make<SystemFunctionCall>(
f.NextLoc(), "clog2",
std::vector<Expression*>{f->Div(a, b, f.NextLoc())})}),
f.NextLoc());

const std::string kExpected = R"(#![allow(nonstandard_constant_naming)]
#![allow(nonstandard_member_naming)]
Expand All @@ -868,6 +890,9 @@ pub const var_4 = std::clog2(s32:1025 as uN[32]) as s32; // s32:11
pub const var_5 = std::clog2(s32:2048 as uN[32]) as s32; // s32:11
pub const var_6 = u16:0xbeef;
pub const var_7 = -s32:0xbeef as u16;
pub const a = s32:512;
pub const b = s32:2;
pub const clog_div = std::clog2(std::clog2((a / b) as uN[32])) as s32; // s32:3
)";

XLS_EXPECT_VAST_TRANSLATION(f, kExpected);
Expand Down