[llvm] [MLGO] Model selection for models lowered through EmitC (PR #212650)

Petr Hosek via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 14 11:46:57 PDT 2026


================
@@ -0,0 +1,136 @@
+# 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 MLDeps 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)
+  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)
----------------
petrhosek wrote:

Nit: For local variable, we typically use `lower_case` names which you already do for other variables above so I'd do the same consistently throughout this function.

https://github.com/llvm/llvm-project/pull/212650


More information about the llvm-commits mailing list