Thank you @banach-space, @Jimmy2027 and @kraiskil for attending. We discussed the following issues and potential directions for improvement:
Translating only primitive C types
While EmitC does provide its own primitive types where needed (e.g. emitc::PointerType, emitc::SizeTType), it still relies on MLIR’s builtin numeric types for its ops and leaves it to the translator to map these types to C’s primitive types. This doesn’t align with MLIR’s best practice of taking care of type conversion at dialect level and keeping the translator as simple as possible.
Note that convert-to-EmitC passes already handle sign issues involved in lowering from signless dialects to the signed based EmitC.
Proposal
Add C’s primitive types as EmitC types, replacing emission of the builtin numeric types numeric types in the translator with type conversion in lowering passes.
Hardening C/C++ variants support
EmitC supports emitting multiple C/C++ variants, but which variant is needed or desired for the program is not specified in MLIR. The variant to actually be emitted is determined partly by CL options used in lowering passes, and partly by the translator based on the ops and types used in the module (see complete list of rules/constraints here).
Not having the target variant explicitly stated in the program has several drawbacks:
-
Understanding which variant was assumed by lowering passes requires going over the entire program. Understanding which variant will be coerced by the translator requires running it.
-
As we add more features to the dialect, EmitC programs may end up being incompatible with any variant despite each operation being valid: OpenCL-C, for example, supports (certain) vector types, but lacks function pointers. Such inconsistencies should be detected as early and simply as possible. As another example consider GCC’s vector extension vs. Clang’s attribute-based memory spaces.
-
The
emitc.fileoperation represents a single C/C++ file, facilitating packing multiple C/C++ compilation units and emitting them selectively. Targetting a different variant for eachemitc.file, e.g. for heterogenous architectures, should ideally be doable atemitc.filelevel, which is less convenient with pass-level CL options.
Proposal: Encode the C/C++ variant in emitc.file
The emitc.file operation provides a natural scope for designating the desired C/C++ variant for compilation units. The variant can be encoded in a new enumerated emitc.variant property of this op.
Note that emitc.file is currently optional, but will become mandatory as part of this change. This may be a positive change in and of itself, as it will improve its integration and testing w.r.t other EmitC ops.
TBD: How to best verify adherence to the variants constraints? Alternatives:
- Within
emitc.file::verify() - Using a validating utility shared between a validating pass and the translator
Extending C/C++ struct support
EmitC currently provides the following ops w.r.t. structs/classes:
-
Member access via its
emitc.member(.) andemitc.member_of_ptr(->) ops, which can be applied to values ofemitc.opaquetypes representing named structures/classes. -
C++ class definition, including support for fields and methods, via its
emitc.classoperation for defining C++ classes.
Supporting struct definion for C could be useful for various purposes, e.g. lowering MemRef types into memref descriptors, and Tuple types which currently require std::tuple. With minor changes, the emitc.class should become usable for defining C structs in addition to classes:
- Must not contain methods.
- Must not specify
final. - Emitted as
structinstead ofclasswith apublic:access specifier (worth doing for better readability in C++ too, especially withemitc.classnot supporting other access specifiers at the moment).
The existing solution for struct types is the emitc.opaque type. It should work just the same for structs defined by emitc.class, or we could introduce a new emitc.udt type for user-defined types which are not opaque. Besides being more clear, this might save some compilation time if verifying valid use and access is desired for structs defined in the module.
Fully supporting the MemRef dialect
Current support for the memref dialect and type is based on emitc.array. While arrays are a native C/C++ construct, they fall short of representing subviews which (a) may have non-unit strides and (b) may carry distinct allocation and aligned pointers. Furthermore, even consecutive multi-dimensional arrays are limited in C/C++ and sometimes force careful lowering to prevent generating usage of arrays where C doesn’t allow them and resorting to pointers instead.
Proposal: Lower into a memref descriptor as in memref-to-llvm lowering
The descriptor implementation of the memref type used for lowering the MemRef type is a proven representation capable of supporting the full range of strided MemRef capabilies. The LLVM descriptor is implemented using LLVM structs. The same descriptor can be implemented as a C struct, allowing EmitC to fully support the memref dialect.
Two alternatives for the existing memref-to-array lowering are:
- Maintaining it as an alternative lowering pass.
- Converting it to an EmitC-to-EmitC pass that replaces descriptors with arrays where possible.
Relocating EmitC’s integration tests
EmitC, being an egress dialect, raises similar needs to LLVM for end-to-end tests that verify entire pipelines. It in fact already provides such test cases for tosa and math, but does so as part of its dialect-level LIT testing, which has a few disadvantages:
-
Such tests impose a dependence of dialect-level testing on lowering/transformations of other dialects.
-
As these tests run longer pipelines than usual dialect-level tests, they take longer to run. Furthermore, such tests would ideally at least compile (if not execute) the generated C code, but the runtime of such tests may prove to be unacceptable under the dialects tests suite. It’s also unclear which C compiler(s) would be available for such testing, except maybe the project’s own clang binary.
Potential alternatives suggested (pending further investigation):
-
MLIR tests include a dedicated integration suite which verifies and prevents regression at an E2E pipeline level. These tests are subject to the
MLIR_INCLUDE_INTEGRATION_TESTSCMake flag, so they don’t burden uninterested users. See @banach-space’s comment for more details on how such integration tests might be implemented. -
The build bot framework provides early detection of post-commit regressions on various test setups. @rengolin suggests in his summary for the Lighthouse’s round table to set up such a build bot once an EmitC setup is available there.