Generate native C code from TVM IR

Hi all, When I approach the class CodeGenC in src/target/source/codegen_c.h I notice it could not generate native C code as noted.

CodeGenC does not aim at generating C codes consumed by MSVC or GCC, Rather, it’s providing infrastructural abstraction for C variants like CUDA and OpenCL-C. You might find some odd variant features, e.g., type int3 for a vector of 3 ints. For native C code generator, see CodeGenLLVM.

I’ve already read the class CodeGenLLVM and LLVMModuleNode but failed to find any information relevant.

Question “Possible to convert a LLVM IR back to C code?” from @joyalbin got some answers. Does it mean native C code could only be generated by a LLVM backend which has been moved? But @comaniac suggests to directly generate C code from TVM IR. Does it mean CodeGenC or another way in CodeGenLLVM ?


1 Like

I think “native C-code generation” is used for more niche use cases like micro-TVM / embedded, but yes, we don’t typically emit native C code for convolution kernels etc.

1 Like

try target=“c”. dont know if the code generated would work though

There are a couple of different targets that output something so similar to C (e.g. CUDA, OpenCL) that some some of the functionality was extracted into a common superclass, CodeGenC. When you specify target="c", it uses CodeGenCHost in You might look at that for more guidance.

Note that with microTVM we need to use tir.disable_vectorize option to avoid generating such int3 types.

I think the previous warnings that this might not generate fully-optimized code in some situations is correct.

1 Like