You are viewing a plain text version of this content. The canonical link for it is here.
Posted to discuss-archive@tvm.apache.org by isong via Apache TVM Discuss <no...@discuss.tvm.ai> on 2020/09/27 06:46:21 UTC

[Apache TVM Discuss] [Questions] How can I generated C code from codegen.cc


Hello everyone,

I've looked few related previous post, but I found few information, but I don't exactly find the answer to my question, so here it is.

In this article, [Bring Your Own Codegen To TVM](https://tvm.apache.org/docs/dev/relay_bring_your_own_codegen.html?highlight=codegen), explains how [codegen.cc](https://github.com/apache/incubator-tvm/blob/master/src/relay/backend/contrib/codegen_c/codegen.cc) does the C code generation.

I can add this line to  `test_target_codegen_c_host.py` to see the generated code, and I can check the generated cod by this line
`print(mhost.get_source())`

However the generated code doesn't seem like it is from `codegen.cc`.

So my question is what `target` should I put in `tvm.buil` to generate the C code from `codegen.cc` .

Thanks a lot.





---
[Visit Topic](https://discuss.tvm.apache.org/t/how-can-i-generated-c-code-from-codegen-cc/8002/1) to respond.

You are receiving this because you enabled mailing list mode.

To unsubscribe from these emails, [click here](https://discuss.tvm.apache.org/email/unsubscribe/e9540fb9f4a1623d932e1d253b10783b7fed92921f62a789f6c166122f9cd3a7).

[Apache TVM Discuss] [Questions] How can I generated C code from codegen.cc

Posted by isong via Apache TVM Discuss <no...@discuss.tvm.ai>.

Hi @comaniac,

Thank you very much for the clarification.

So the generated output from [test_target_codegen_c_host.py](https://github.com/apache/incubator-tvm/blob/master/tests/python/unittest/test_target_codegen_c_host.py) using `target="c"` is from LLVM IR.

```
         mhost = tvm.build(s, [A, B, C], "c", name="fadd")
         print(mhost.get_source()) # added line
```
```
$ python `find . -name test_target_codegen_c_host.py`
#include "tvm/runtime/c_runtime_api.h"
#include "tvm/runtime/c_backend_api.h"
void* __tvm_module_ctx = NULL;
#ifdef __cplusplus
extern "C"
#endif
TVM_DLL int32_t fadd(void* args, void* arg_type_ids, int32_t num_args, void* out_ret_value, void* out_ret_tcode, void* resource_handle) {
  void* arg0 = (((TVMValue*)args)[0].v_handle);
  int32_t arg0_code = ((int32_t*)arg_type_ids)[(0)];
  void* arg1 = (((TVMValue*)args)[1].v_handle);
  int32_t arg1_code = ((int32_t*)arg_type_ids)[(1)];
  void* arg2 = (((TVMValue*)args)[2].v_handle);
  int32_t arg2_code = ((int32_t*)arg_type_ids)[(2)];
  void* A = (((DLTensor*)arg0)[0].data);
  void* arg0_shape = (((DLTensor*)arg0)[0].shape);
  void* arg0_strides = (((DLTensor*)arg0)[0].strides);
  int32_t dev_id = (((DLTensor*)arg0)[0].ctx.device_id);
  void* B = (((DLTensor*)arg1)[0].data);
  void* arg1_shape = (((DLTensor*)arg1)[0].shape);
  void* arg1_strides = (((DLTensor*)arg1)[0].strides);
  void* C = (((DLTensor*)arg2)[0].data);
  void* arg2_shape = (((DLTensor*)arg2)[0].shape);
  void* arg2_strides = (((DLTensor*)arg2)[0].strides);
  if (!(arg0_strides == NULL)) {
  }
  if (!(arg1_strides == NULL)) {
  }
  if (!(arg2_strides == NULL)) {
  }
  for (int32_t i0 = 0; i0 < 1024; ++i0) {
    ((float*)C)[(i0)] = (((float*)A)[(i0)] + ((float*)B)[(i0)]);
  }
  return 0;
}
```
 
Thank you.





---
[Visit Topic](https://discuss.tvm.apache.org/t/how-can-i-generated-c-code-from-codegen-cc/8002/3) to respond.

You are receiving this because you enabled mailing list mode.

To unsubscribe from these emails, [click here](https://discuss.tvm.apache.org/email/unsubscribe/bb015c5f03fb72eb7e3334fd44a781d10bf3ed679be9fffa53a5e29e32332699).

[Apache TVM Discuss] [Questions] How can I generated C code from codegen.cc

Posted by "Cody H. Yu via Apache TVM Discuss" <no...@discuss.tvm.ai>.

They are different stories.

The C codegen you pointed out in BYOC is only used to demonstrate how BYOC works, so TVM backend for CPU doesn't go through it.

Instead, TVM backend for CPU directly generates LLVM IR, so you won't get the generated C source code. Although TVM does have [C codgen base class](https://github.com/apache/incubator-tvm/blob/master/src/target/source/codegen_source_base.h), it is an abstract class and derived for [OpenCL code generation](https://github.com/apache/incubator-tvm/blob/master/src/target/source/codegen_opencl.h) for GPUs.





---
[Visit Topic](https://discuss.tvm.apache.org/t/how-can-i-generated-c-code-from-codegen-cc/8002/2) to respond.

You are receiving this because you enabled mailing list mode.

To unsubscribe from these emails, [click here](https://discuss.tvm.apache.org/email/unsubscribe/46fdcad8f7c9e0b7ca271479e249321a1540dc8facd144bc3a69e6f58fd0d31a).