[llvm] [MLGO] Model selection for models lowered through EmitC (PR #212650)
ioana ghiban via llvm-commits
llvm-commits at lists.llvm.org
Mon Aug 17 09:26:31 PDT 2026
================
@@ -0,0 +1,141 @@
+# Lower MLGO models to C++ headers via the MLIR-based pipeline.
+#
+# Each entry in ${models} has the form "cli-flag,path/to/model.mlir,type".
+# For entries whose type matches ${target_type}, this function:
+# 1. Lowers model MLIR with ${mlir_opt} and translates it with ${mlir_translate}
+# to a C++ header defining Model<N> in ${LLVM_INCLUDE_DIR}/${include_subdir}.
+# 2. Generates ${generated_file_basename}.def in ${LLVM_INCLUDE_DIR}/${include_subdir}
+# with one MLGO_MODEL(ClassName, "cli-flag") entry per model.
+# 3. Generates ${generated_file_basename}.h in ${LLVM_INCLUDE_DIR}/${include_subdir}
+# including all generated model headers.
+# 4. Appends the target driving generation to ${out_var} in the caller's scope.
+# 5. Defines LLVM_HAVE_MLIR_LOWERING_<TARGET_TYPE>.
+function(mlgo_lower_models models mlir_opt mlir_translate target_type
+ include_subdir generated_file_basename out_var)
+ set(def_file "${generated_file_basename}.def")
+ set(umbrella_header "${generated_file_basename}.h")
+
+ if(NOT IS_ABSOLUTE "${mlir_opt}")
+ cmake_path(ABSOLUTE_PATH mlir_opt BASE_DIRECTORY "${CMAKE_BINARY_DIR}")
+ endif()
+
+ if(NOT IS_ABSOLUTE "${mlir_translate}")
+ cmake_path(ABSOLUTE_PATH mlir_translate BASE_DIRECTORY "${CMAKE_BINARY_DIR}")
+ endif()
+
+ string(APPEND def_content
+ "/* Auto-generated by CMake */\n"
+ "#ifndef MLGO_MODEL\n"
+ "# define MLGO_MODEL(CLASS_NAME, CLI_FLAG)\n"
+ "#endif\n\n"
+ )
+
+ set(headers_content "/* Auto-generated by CMake */\nnamespace llvm {\n")
+ set(mlgo_gen_targets "")
+ set(model_index 1)
+
+ foreach(model_info IN LISTS models)
+ # Parse comma-separated fields (from the LLVM_MLGO_MODELS flag)
+ # cli-flag,path/to/model.mlir,type
+ string(REPLACE "," ";" model_fields "${model_info}")
+ list(GET model_fields 0 cli_flag)
+ list(GET model_fields 1 model_path)
+ list(GET model_fields 2 model_type)
+
+ # Only process models matching target_type
+ if (NOT model_type STREQUAL target_type)
+ continue()
+ endif()
+
+ set(class_name "${target_type}_Model${model_index}")
+ math(EXPR model_index "${model_index} + 1")
+
+ if (NOT IS_ABSOLUTE "${model_path}")
+ get_filename_component(model_path "${model_path}" ABSOLUTE BASE_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
+ endif()
+
+ set(emitc_mlir "${CMAKE_CURRENT_BINARY_DIR}/${class_name}_emitc.mlir")
+ set(header_file "${LLVM_INCLUDE_DIR}/${include_subdir}/${class_name}.h")
+
+ # Pass pipeline to lower MLIR models to EmitC dialect
+ # TODO: Simplify with builtin pipeline for translation.
+ # TODO: It isn't ideal to hardcode this pass pipeline here. It would be better to
+ # use the transform dialect.
+ set(mlir_passes
+ "func.func(tosa-to-linalg-named,tosa-to-linalg,tosa-to-arith,tosa-to-tensor)"
+ "symbol-privatize"
+ "scalarize-single-element-tensor-return"
+ "one-shot-bufferize{bufferize-function-boundaries=true function-boundary-type-conversion=identity-layout-map buffer-alignment=0}"
+ "buffer-results-to-out-params{hoist-static-allocs=true}"
+ "func.func(promote-buffers-to-stack)"
+ "buffer-deallocation-pipeline"
+ "func.func(convert-linalg-to-loops)"
+ "expand-strided-metadata"
+ "canonicalize"
+ "memref-elide-reinterpret-cast"
+ "convert-to-emitc"
+ "wrap-emitc-func-in-class{class-name-format=${class_name}}"
----------------
ioghiban wrote:
[nit]: Could you please keep the EmitC rewrite passes separate? Now they are interleaved with the conversion passes. Perhaps place them afterwards, after the full EmitC conversion?
https://github.com/llvm/llvm-project/pull/212650
More information about the llvm-commits
mailing list