Skip to content

Commit 05d10a6

Browse files
committed
[mlir][Target] Support Fatbin target for static nvptxcompiler
1 parent 79f59af commit 05d10a6

File tree

2 files changed

+55
-2
lines changed

2 files changed

+55
-2
lines changed

mlir/lib/Target/LLVM/CMakeLists.txt

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,27 +66,39 @@ if ("NVPTX" IN_LIST LLVM_TARGETS_TO_BUILD)
6666
set(MLIR_CUDAToolkit_ROOT ${CUDAToolkit_LIBRARY_ROOT})
6767
endif()
6868

69-
# Add the `nvptxcompiler` library.
69+
# Add the `nvptxcompiler` library and the `nvfatbin` library.
7070
if(MLIR_ENABLE_NVPTXCOMPILER)
7171
# Find the `nvptxcompiler` library.
7272
# TODO: Bump the MLIR CMake version to 3.25 and use `CUDA::nvptxcompiler_static`.
7373
find_library(MLIR_NVPTXCOMPILER_LIB_PATH nvptxcompiler_static
7474
PATHS ${CUDAToolkit_LIBRARY_DIR} NO_DEFAULT_PATH)
75+
find_library(MLIR_NVFATBIN_LIB_PATH nvfatbin_static
76+
PATHS ${CUDAToolkit_LIBRARY_DIR} NO_DEFAULT_PATH)
7577

7678
# Fail if `nvptxcompiler_static` couldn't be found.
7779
if(MLIR_NVPTXCOMPILER_LIB_PATH STREQUAL "MLIR_NVPTXCOMPILER_LIB_PATH-NOTFOUND")
7880
message(FATAL_ERROR
7981
"Requested using the `nvptxcompiler` library backend but it couldn't be found.")
8082
endif()
8183

84+
# Fail if `nvfatbin_static` couldn't be found.
85+
if(MLIR_NVFATBIN_LIB_PATH STREQUAL "MLIR_NVFATBIN_LIB_PATH-NOTFOUND")
86+
message(FATAL_ERROR
87+
"Requested using the `nvfatbin` library backend but it couldn't be found.")
88+
endif()
89+
8290
add_library(MLIR_NVPTXCOMPILER_LIB STATIC IMPORTED GLOBAL)
91+
add_library(MLIR_NVFATBIN_LIB STATIC IMPORTED GLOBAL)
8392
# Downstream projects can modify this path and use it in CMake. For example:
8493
# add_library(MLIR_NVPTXCOMPILER_LIB STATIC IMPORTED GLOBAL)
8594
# set_property(TARGET MLIR_NVPTXCOMPILER_LIB PROPERTY IMPORTED_LOCATION ${...})
8695
# where `...` is to be replaced with the path to the library.
8796
set_property(TARGET MLIR_NVPTXCOMPILER_LIB PROPERTY IMPORTED_LOCATION ${MLIR_NVPTXCOMPILER_LIB_PATH})
88-
# Link against `nvptxcompiler_static`. TODO: use `CUDA::nvptxcompiler_static`.
97+
set_property(TARGET MLIR_NVFATBIN_LIB PROPERTY IMPORTED_LOCATION ${MLIR_NVFATBIN_LIB_PATH})
98+
# Link against `nvptxcompiler_static` and `nvfatbin_static`.
99+
# TODO: use `CUDA::nvptxcompiler_static` and `CUDA::nvfatbin_static`.
89100
target_link_libraries(MLIRNVVMTarget PRIVATE MLIR_NVPTXCOMPILER_LIB)
101+
target_link_libraries(MLIRNVVMTarget PRIVATE MLIR_NVFATBIN_LIB)
90102
target_include_directories(obj.MLIRNVVMTarget PUBLIC ${CUDAToolkit_INCLUDE_DIRS})
91103
endif()
92104
else()

mlir/lib/Target/LLVM/NVVM/Target.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,18 @@ NVPTXSerializer::compileToBinary(const std::string &ptxCode) {
473473
} \
474474
} while (false)
475475

476+
#include <nvFatbin.h>
477+
478+
#define RETURN_ON_NVFATBIN_ERROR(expr) \
479+
do { \
480+
auto result = (expr); \
481+
if (result != nvFatbinResult::NVFATBIN_SUCCESS) { \
482+
emitError(loc) << llvm::Twine(#expr).concat(" failed with error: ") \
483+
<< nvFatbinGetErrorString(result); \
484+
return std::nullopt; \
485+
} \
486+
} while (false)
487+
476488
std::optional<SmallVector<char, 0>>
477489
NVPTXSerializer::compileToBinaryNVPTX(const std::string &ptxCode) {
478490
Location loc = getOperation().getLoc();
@@ -486,6 +498,11 @@ NVPTXSerializer::compileToBinaryNVPTX(const std::string &ptxCode) {
486498
targetOptions.tokenizeCmdOptions();
487499
cmdOpts.second.append(
488500
{"-arch", getTarget().getChip().data(), "--opt-level", optLevel.c_str()});
501+
bool useFatbin32 = false;
502+
for (const char *option : cmdOpts.second) {
503+
if (StringRef(option) == "-32")
504+
useFatbin32 = true;
505+
}
489506

490507
// Create the compiler handle.
491508
RETURN_ON_NVPTXCOMPILER_ERROR(
@@ -538,6 +555,30 @@ NVPTXSerializer::compileToBinaryNVPTX(const std::string &ptxCode) {
538555
});
539556
#undef DEBUG_TYPE
540557
RETURN_ON_NVPTXCOMPILER_ERROR(nvPTXCompilerDestroy(&compiler));
558+
559+
if (targetOptions.getCompilationTarget() == gpu::CompilationTarget::Fatbin) {
560+
const char *cubinOpts[1] = {"-64"};
561+
if (useFatbin32) {
562+
cubinOpts[0] = {"-32"};
563+
}
564+
nvFatbinHandle handle;
565+
566+
auto chip = getTarget().getChip();
567+
chip.consume_front("sm_");
568+
569+
RETURN_ON_NVFATBIN_ERROR(nvFatbinCreate(&handle, cubinOpts, 1));
570+
RETURN_ON_NVFATBIN_ERROR(nvFatbinAddCubin(
571+
handle, binary.data(), binary.size(), chip.data(), nullptr));
572+
RETURN_ON_NVFATBIN_ERROR(nvFatbinAddPTX(
573+
handle, ptxCode.data(), ptxCode.size(), chip.data(), nullptr, nullptr));
574+
575+
size_t fatbinSize;
576+
RETURN_ON_NVFATBIN_ERROR(nvFatbinSize(handle, &fatbinSize));
577+
SmallVector<char, 0> fatbin(fatbinSize, 0);
578+
RETURN_ON_NVFATBIN_ERROR(nvFatbinGet(handle, (void *)fatbin.data()));
579+
RETURN_ON_NVFATBIN_ERROR(nvFatbinDestroy(&handle));
580+
return fatbin;
581+
}
541582
return binary;
542583
}
543584
#endif // MLIR_ENABLE_NVPTXCOMPILER

0 commit comments

Comments
 (0)