google / xls

XLS: Accelerated HW Synthesis
http://google.github.io/xls/
Apache License 2.0
1.18k stars 173 forks source link

proc-scoped type aliases cause type deduction errors #1368

Open rw1nkler opened 5 months ago

rw1nkler commented 5 months ago

Describe the bug

Using proc-scoped type aliases to instantiate parametric structs leads to type deduction errors when the struct's parameter is taken from a proc parameter.

To Reproduce

One can identify the issue when running tests with the DSLX interpreter for this code:

struct MyData<WIDTH: u32> { value: uN[WIDTH] }

proc MyProc<DATA_WIDTH: u32> {
    type Data = MyData<DATA_WIDTH>;
    out_s: chan<Data> out;

    config(out_s: chan<Data> out) { (out_s,) }

    init {  }

    next(tok: token, state: ()) {
        // option I: Using zero! macro works correctly
        // let tok = send(tok, out_s, zero!<Data>());

        // option II: Redefining MyData type and using it works correctly
        // type Data = MyData<DATA_WIDTH>;
        // let tok = send(tok, out_s, Data { value: uN[DATA_WIDTH]:0 });

        // option III: Using proc-scoped type alias fails
        let tok = send(tok, out_s, Data { value: uN[DATA_WIDTH]:0 });
    }
}

const TEST_WIDTH = u32:32;

#[test_proc]
proc MyProcTest {
    type Data = MyData<TEST_WIDTH>;
    terminator: chan<bool> out;
    out_r: chan<Data> in;

    config(terminator: chan<bool> out) {
        let (out_s, out_r) = chan<Data>;
        spawn MyProc<TEST_WIDTH>(out_s);
        (terminator, out_r)
    }

    init {  }

    next(tok: token, state: ()) {
        let (tok, _) = recv(tok, out_r);
        let (tok, _) = recv(tok, out_r);
        send(tok, terminator, true);
    }
}

Here is the error message from executing the test:

ERROR: /home/rwinkler/projects/xls/xls/modules/zstd/BUILD:829:17: Parsing and type checking DSLX source files of target toperror_dslx failed: (Exit 255): bash failed: error executing command (from target //xls/modules/zstd:toperror_dslx) /bin/bash -c ... (remaining 1 argument skipped)

Use --sandbox_debug to see verbose messages from the sandbox and retain the sandbox build root for debugging
E0403 15:13:52.279111       3 parametric_bind.cc:126] INTERNAL: XLS_RET_CHECK failure (xls/dslx/type_system/parametric_bind.cc:126) ctx.parametric_binding_types.contains(pdim_name) Cannot bind DATA_WIDTH : it has no associated type.
0x55b565e065ba: xabsl::StatusBuilder::CreateStatusAndConditionallyLog()
0x55b564014f39: xabsl::StatusBuilder::operator absl::lts_20240116::Status()
0x55b56410d7a8: xls::dslx::ParametricBindTypeDim()
0x55b56410decb: xls::dslx::ParametricBind()
0x55b564106d70: xls::dslx::internal::ParametricInstantiator::InstantiateOneArg()
0x55b56410ac71: xls::dslx::internal::StructInstantiator::Instantiate()
0x55b564105788: xls::dslx::InstantiateStruct()
0x55b5640d6ad0: xls::dslx::(anonymous namespace)::DeduceVisitor::HandleStructInstance()
0x55b56448d1bb: xls::dslx::StructInstance::Accept()
0x55b5640c4b9f: xls::dslx::Deduce()
0x55b5640943a5: std::__1::__function::__func<>::operator()()
0x55b564189248: xls::dslx::DeduceCtx::Deduce()
0x55b5640c5112: xls::dslx::DeduceAndResolve()
0x55b5640fec61: xls::dslx::AppendArgsForInstantiation()
0x55b5640ff743: xls::dslx::DeduceInvocation()
0x55b5640d061b: xls::dslx::(anonymous namespace)::DeduceVisitor::HandleInvocation()
0x55b56448ccfb: xls::dslx::Invocation::Accept()
0x55b5640c4b9f: xls::dslx::Deduce()
0x55b5640943a5: std::__1::__function::__func<>::operator()()
0x55b564189248: xls::dslx::DeduceCtx::Deduce()
0x55b5640c5112: xls::dslx::DeduceAndResolve()
0x55b5640d0735: xls::dslx::(anonymous namespace)::DeduceVisitor::HandleLet()
0x55b56448de0b: xls::dslx::Let::Accept()
0x55b5640c4b9f: xls::dslx::Deduce()
0x55b5640943a5: std::__1::__function::__func<>::operator()()
0x55b564189248: xls::dslx::DeduceCtx::Deduce()
0x55b5640c8144: xls::dslx::(anonymous namespace)::DeduceVisitor::HandleStatement()
0x55b56448dc8b: xls::dslx::Statement::Accept()
0x55b5640c4b9f: xls::dslx::Deduce()
0x55b5640943a5: std::__1::__function::__func<>::operator()()
0x55b564189248: xls::dslx::DeduceCtx::Deduce()
0x55b5640cb6b8: xls::dslx::(anonymous namespace)::DeduceVisitor::HandleBlock()
0x55b56448d49b: xls::dslx::Block::Accept()
0x55b5640c4b9f: xls::dslx::Deduce()
0x55b5640943a5: std::__1::__function::__func<>::operator()()
0x55b564189248: xls::dslx::DeduceCtx::Deduce()
0x55b56409b797: xls::dslx::TypecheckInvocation()
0x55b564094668: std::__1::__function::__func<>::operator()()
0x55b5640fe94e: xls::dslx::DeduceInstantiation()
0x55b5640ed96b: xls::dslx::(anonymous namespace)::DeduceSpawn()
0x55b5640d480b: xls::dslx::(anonymous namespace)::DeduceVisitor::HandleSpawn()
0x55b56448cedb: xls::dslx::Spawn::Accept()
0x55b5640c4b9f: xls::dslx::Deduce()
0x55b5640943a5: std::__1::__function::__func<>::operator()()
0x55b564189248: xls::dslx::DeduceCtx::Deduce()
0x55b5640c8144: xls::dslx::(anonymous namespace)::DeduceVisitor::HandleStatement()
0x55b56448dc8b: xls::dslx::Statement::Accept()
0x55b5640c4b9f: xls::dslx::Deduce()
0x55b5640943a5: std::__1::__function::__func<>::operator()()
0x55b564189248: xls::dslx::DeduceCtx::Deduce()

E0403 15:13:52.279187       3 command_line_utils.cc:45] Could not extract a textual position from error message: INTERNAL: XLS_RET_CHECK failure (xls/dslx/type_system/parametric_bind.cc:126) ctx.parametric_binding_types.contains(pdim_name) Cannot bind DATA_WIDTH : it has no associated type.: INVALID_ARGUMENT: Provided status is not in recognized error form: INTERNAL: XLS_RET_CHECK failure (xls/dslx/type_system/parametric_bind.cc:126) ctx.parametric_binding_types.contains(pdim_name) Cannot bind DATA_WIDTH : it has no associated type.
Error parsing and type checking DSLX source file: xls/modules/zstd/toperror.x
Error: INTERNAL: XLS_RET_CHECK failure (xls/dslx/type_system/parametric_bind.cc:126) ctx.parametric_binding_types.contains(pdim_name) Cannot bind DATA_WIDTH : it has no associated type.Target //xls/modules/zstd:toperror_dslx_test failed to build
Use --verbose_failures to see the command lines of failed build steps.

Source file: myproc.x

Expected behavior The type deduction for the proc-scoped alias should work correctly because it works for the same alias declared in next function

Additional context Related issues:

FYI @cdleary

dplassgit commented 1 week ago

Updated syntax:

struct MyData<WIDTH : u32>{value : uN[WIDTH]}

proc MyProc<DATA_WIDTH: u32> {
  type Data = MyData<DATA_WIDTH>;
  out_s: chan<Data> out;

  config(out_s: chan<Data> out) {(out_s, )}
  init {}

  next(_: ()) {
    // option I: Redefining MyData type and using it works correctly
    //type Data = MyData<DATA_WIDTH>;
    //let tok = send(join(), out_s, Data { value: uN[DATA_WIDTH]:0 });

    // option II: Using proc-scoped type alias fails
    send(join(), out_s, Data{value : uN[DATA_WIDTH] : 0});
  }
}

const TEST_WIDTH = u32:32;

#[test_proc]
proc MyProcTest {
  type Data = MyData<TEST_WIDTH>;
  terminator: chan<bool> out;
  out_r: chan<Data> in;

  config(terminator: chan<bool> out) {
    let(out_s, out_r) = chan<Data>("out");
    spawn MyProc<TEST_WIDTH>(out_s);
    (terminator, out_r)
  }

  init {}

  next(_: ()) {
    let(tok, _) = recv(join(), out_r);
    send(tok, terminator, true);
  }
}