[Mlir-commits] [mlir] Fix libnvptxcompiler_static.a absolute path (PR #115015)

Zichen Lu llvmlistbot at llvm.org
Tue Nov 5 07:59:29 PST 2024


https://github.com/MikaOvO created https://github.com/llvm/llvm-project/pull/115015

Now when building llvm-solid with `-DMLIR_ENABLE_NVPTXCOMPILER=ON`, there will be an absolute path (`/path/to/libnvptxcompiler_static.a`) in MLIRNVVMTarget dependencies (in `/build/path/install/lib/cmake/mlir/MLIRTargets.cmake`).  For example,

```cmake
set_target_properties(MLIRNVVMTarget PROPERTIES
  INTERFACE_LINK_LIBRARIES "MLIRIR;MLIRExecutionEngineUtils;MLIRSupport;MLIRGPUDialect;MLIRTargetLLVM;MLIRNVVMToLLVMIRTranslation;LLVMSupport;/path/to/libnvptxcompiler_static.a"
)
```

If downstream project uses pre-built llvm-solid and depends on MLIRNVVMTarget, it may fail to build due to the absence of the `libnvptxcompiler_static.a` absolute path.

After this commit, there will no absolute path in `/build/path/install/lib/cmake/mlir/MLIRTargets.cmake`

```cmake
set_target_properties(MLIRNVVMTarget PROPERTIES
  INTERFACE_LINK_LIBRARIES "MLIRIR;MLIRExecutionEngineUtils;MLIRSupport;MLIRGPUDialect;MLIRTargetLLVM;MLIRNVVMToLLVMIRTranslation;LLVMSupport;\$<LINK_ONLY:MLIR_NVPTXCOMPILER_LIB>"
)
```

Then downstream project can modify `libnvptxcompiler_static.a` path and use cmake to build. For example,

```cmake
# find_library(...)

add_library(MLIR_NVPTXCOMPILER_LIB STATIC IMPORTED GLOBAL)
set_property(TARGET MLIR_NVPTXCOMPILER_LIB PROPERTY IMPORTED_LOCATION ${...})  
```


>From 64ab6fbe53b02294b26ae9ba0c5f3fd14df29897 Mon Sep 17 00:00:00 2001
From: Zichen Lu <mikaovo2000 at gmail.com>
Date: Tue, 5 Nov 2024 23:33:19 +0800
Subject: [PATCH] Fix libnvptxcompiler_static.a absolute path

---
 mlir/lib/Target/LLVM/CMakeLists.txt | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/mlir/lib/Target/LLVM/CMakeLists.txt b/mlir/lib/Target/LLVM/CMakeLists.txt
index bc14c568e46be2..dea7c26dd92042 100644
--- a/mlir/lib/Target/LLVM/CMakeLists.txt
+++ b/mlir/lib/Target/LLVM/CMakeLists.txt
@@ -70,17 +70,20 @@ if ("NVPTX" IN_LIST LLVM_TARGETS_TO_BUILD)
     if(MLIR_ENABLE_NVPTXCOMPILER)
       # Find the `nvptxcompiler` library.
       # TODO: Bump the MLIR CMake version to 3.25 and use `CUDA::nvptxcompiler_static`.
-      find_library(MLIR_NVPTXCOMPILER_LIB nvptxcompiler_static
+      find_library(MLIR_NVPTXCOMPILER_LIB_PATH nvptxcompiler_static
                   PATHS ${CUDAToolkit_LIBRARY_DIR} NO_DEFAULT_PATH)
 
       # Fail if `nvptxcompiler_static` couldn't be found.
-      if(MLIR_NVPTXCOMPILER_LIB STREQUAL "MLIR_NVPTXCOMPILER_LIB-NOTFOUND")
+      if(MLIR_NVPTXCOMPILER_LIB_PATH STREQUAL "MLIR_NVPTXCOMPILER_LIB_PATH-NOTFOUND")
         message(FATAL_ERROR
                 "Requested using the `nvptxcompiler` library backend but it couldn't be found.")
       endif()
 
+      add_library(MLIR_NVPTXCOMPILER_LIB STATIC IMPORTED GLOBAL)
+      # Downstream project can modify this path and use it in CMake
+      set_property(TARGET MLIR_NVPTXCOMPILER_LIB PROPERTY IMPORTED_LOCATION ${MLIR_NVPTXCOMPILER_LIB_PATH})
       # Link against `nvptxcompiler_static`. TODO: use `CUDA::nvptxcompiler_static`.
-      target_link_libraries(MLIRNVVMTarget PRIVATE ${MLIR_NVPTXCOMPILER_LIB})
+      target_link_libraries(MLIRNVVMTarget PRIVATE MLIR_NVPTXCOMPILER_LIB)
       target_include_directories(obj.MLIRNVVMTarget PUBLIC ${CUDAToolkit_INCLUDE_DIRS})
     endif()
   else()



More information about the Mlir-commits mailing list