Skip to content

[Offload] Correctly regenerate API files if modified #141679

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

jhuber6
Copy link
Contributor

@jhuber6 jhuber6 commented May 27, 2025

Summary:
Currently these are done POST_BUILD, but they should be dependencies on
generating the library since it's a direct dependency.

@llvmbot
Copy link
Member

llvmbot commented May 27, 2025

@llvm/pr-subscribers-offload

Author: Joseph Huber (jhuber6)

Changes

Summary:
Currently these are done POST_BUILD, but they should be dependencies on
generating the library since it's a direct dependency.


Full diff: https://github.com/llvm/llvm-project/pull/141679.diff

2 Files Affected:

  • (modified) offload/liboffload/API/CMakeLists.txt (+17-10)
  • (modified) offload/liboffload/CMakeLists.txt (+10-5)
diff --git a/offload/liboffload/API/CMakeLists.txt b/offload/liboffload/API/CMakeLists.txt
index 5f8d1435d141f..b3b499568fe84 100644
--- a/offload/liboffload/API/CMakeLists.txt
+++ b/offload/liboffload/API/CMakeLists.txt
@@ -2,8 +2,8 @@
 # include directory. These files are checked in with the rest of the source,
 # therefore it is only needed when making changes to the API.
 
-find_program(CLANG_FORMAT clang-format PATHS ${LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH)
-if (CLANG_FORMAT)
+find_program(clang_format clang-format PATHS ${LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH)
+if (clang_format)
     set(LLVM_TARGET_DEFINITIONS ${CMAKE_CURRENT_SOURCE_DIR}/OffloadAPI.td)
 
     tablegen(OFFLOAD OffloadAPI.h -gen-api)
@@ -13,15 +13,22 @@ if (CLANG_FORMAT)
     tablegen(OFFLOAD OffloadPrint.hpp -gen-print-header)
     tablegen(OFFLOAD OffloadErrcodes.inc -gen-errcodes)
 
-    set(FILES_TO_COPY "OffloadAPI.h;OffloadEntryPoints.inc;OffloadFuncs.inc;OffloadImplFuncDecls.inc;OffloadPrint.hpp")
-    set(GEN_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../include/generated)
+    set(files_to_copy "OffloadAPI.h;OffloadEntryPoints.inc;OffloadFuncs.inc;OffloadImplFuncDecls.inc;OffloadPrint.hpp")
+    set(generated_dir ${CMAKE_CURRENT_SOURCE_DIR}/../include/generated)
     add_public_tablegen_target(OffloadGenerate)
-    add_custom_command(TARGET OffloadGenerate POST_BUILD COMMAND ${CLANG_FORMAT}
-        -i ${TABLEGEN_OUTPUT})
-    add_custom_command(TARGET OffloadGenerate POST_BUILD COMMAND ${CMAKE_COMMAND}
-        -E copy_if_different ${FILES_TO_COPY} ${GEN_DIR})
-    add_custom_command(TARGET OffloadGenerate POST_BUILD COMMAND ${CMAKE_COMMAND}
-        -E copy_if_different OffloadErrcodes.inc "${LIBOMPTARGET_INCLUDE_DIR}/Shared/OffloadErrcodes.inc")
+
+    add_custom_target(OffloadAPI DEPENDS OffloadGenerate)
+    foreach(file IN LISTS files_to_copy)
+      add_custom_command(
+          OUTPUT ${generated_dir}/${file}
+          COMMAND ${clang_format} -i ${file}
+          COMMAND ${CMAKE_COMMAND} -E make_directory ${generated_dir}
+          COMMAND ${CMAKE_COMMAND} -E copy_if_different ${file} ${generated_dir}
+          DEPENDS ${file}
+      )
+      add_custom_target(OffloadAPI.${file} DEPENDS ${generated_dir}/${file})
+      add_dependencies(OffloadAPI OffloadAPI.${file})
+    endforeach()
 else()
     message(WARNING "clang-format was not found, so the OffloadGenerate target\
         will not be available. Offload will still build, but you will not be\
diff --git a/offload/liboffload/CMakeLists.txt b/offload/liboffload/CMakeLists.txt
index 1b098bc01e218..6dcc5b430a0b6 100644
--- a/offload/liboffload/CMakeLists.txt
+++ b/offload/liboffload/CMakeLists.txt
@@ -19,11 +19,11 @@ if(LLVM_HAVE_LINK_VERSION_SCRIPT)
 endif()
 
 target_include_directories(LLVMOffload PUBLIC
-                            ${CMAKE_CURRENT_BINARY_DIR}/../include
-                            ${CMAKE_CURRENT_SOURCE_DIR}/include
-                            ${CMAKE_CURRENT_SOURCE_DIR}/include/generated
-                            ${CMAKE_CURRENT_SOURCE_DIR}/../include
-                            ${CMAKE_CURRENT_SOURCE_DIR}/../plugins-nextgen/common/include)
+                           ${CMAKE_CURRENT_BINARY_DIR}/../include
+                           ${CMAKE_CURRENT_SOURCE_DIR}/include
+                           ${CMAKE_CURRENT_SOURCE_DIR}/include/generated
+                           ${CMAKE_CURRENT_SOURCE_DIR}/../include
+                           ${CMAKE_CURRENT_SOURCE_DIR}/../plugins-nextgen/common/include)
 
 target_compile_options(LLVMOffload PRIVATE ${offload_compile_flags})
 target_link_options(LLVMOffload PRIVATE ${offload_link_flags})
@@ -33,6 +33,11 @@ target_compile_definitions(LLVMOffload PRIVATE
   DEBUG_PREFIX="Liboffload"
 )
 
+# Make sure these are up-to-date if modified.
+if(TARGET OffloadAPI)
+  add_dependencies(LLVMOffload OffloadAPI)
+endif()
+
 set_target_properties(LLVMOffload PROPERTIES
                       POSITION_INDEPENDENT_CODE ON
                       INSTALL_RPATH "$ORIGIN"

@jhuber6
Copy link
Contributor Author

jhuber6 commented May 27, 2025

@callumfare

Copy link
Contributor

@callumfare callumfare left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, one comment but otherwise it looks good.

At this point I'm not against removing the checked-in generated files to conform with the way Tablegen is used elsewhere, if that's something we'd prefer. The clang-format step could be optional in that case.

add_custom_command(TARGET OffloadGenerate POST_BUILD COMMAND ${CMAKE_COMMAND}
-E copy_if_different ${FILES_TO_COPY} ${GEN_DIR})
add_custom_command(TARGET OffloadGenerate POST_BUILD COMMAND ${CMAKE_COMMAND}
-E copy_if_different OffloadErrcodes.inc "${LIBOMPTARGET_INCLUDE_DIR}/Shared/OffloadErrcodes.inc")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to be missing after the change. @RossBrunton I'm not sure why it's separate from the others, is it because the output directory is different?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this confused me because there's no such file in this directory and there's already a .inc file there. So I'm pretty sure this does nothing so I removed it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@RossBrunton it's safe to delete this, right?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is here because Shared/OffloadErrcodes.inc is in a different directory, yes. I still think it's required though.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, so this is generated like the others but installed there. We really should just tablegen these on demand.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I very nearly have a PR ready to do that, should be up later today

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated.

Summary:
Currently these are done POST_BUILD, but they should be dependencies on
generating the library since it's a direct dependency.
@@ -2,8 +2,8 @@
# include directory. These files are checked in with the rest of the source,
# therefore it is only needed when making changes to the API.

find_program(CLANG_FORMAT clang-format PATHS ${LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH)
if (CLANG_FORMAT)
find_program(clang_format clang-format PATHS ${LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

find_program creates an entry into the CMakeCache.txt. By convention, those are upper case.

add_custom_command(TARGET OffloadGenerate POST_BUILD COMMAND ${CMAKE_COMMAND}
-E copy_if_different OffloadErrcodes.inc "${LIBOMPTARGET_INCLUDE_DIR}/Shared/OffloadErrcodes.inc")

add_custom_target(OffloadAPI DEPENDS OffloadGenerate)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
add_custom_target(OffloadAPI DEPENDS OffloadGenerate)
add_custom_target(OffloadAPI)
add_dependencies(OffloadAPI OffloadGenerate)

DEPENDS is for file-level dependencies

Comment on lines +22 to +26
${CMAKE_CURRENT_BINARY_DIR}/../include
${CMAKE_CURRENT_SOURCE_DIR}/include
${CMAKE_CURRENT_SOURCE_DIR}/include/generated
${CMAKE_CURRENT_SOURCE_DIR}/../include
${CMAKE_CURRENT_SOURCE_DIR}/../plugins-nextgen/common/include)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nit] unrelated noise

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants