Skip to content

Commit

Permalink
[Target] Don't register AArch64 target tags without LLVM compiler sup…
Browse files Browse the repository at this point in the history
…port (#16897)

This commit aims to fix the issue described here:
#16425 (comment) by
conditionally registering the target tags based on the availability of
the LLVM AArch64 backend. It's possible to extract the targets LLVM
has been compiled for using `llvm-config --targets-built`.

Change-Id: I20b608aea9ea554b0c0388ee884621305d2d59b9
  • Loading branch information
lhutton1 authored Apr 17, 2024
1 parent d030ce2 commit 857fe61
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
1 change: 1 addition & 0 deletions cmake/modules/LLVM.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ if(NOT ${USE_LLVM} MATCHES ${IS_FALSE_PATTERN})
if (${TVM_MLIR_VERSION})
add_definitions(-DTVM_MLIR_VERSION=${TVM_MLIR_VERSION})
endif()
add_definitions(-DTVM_LLVM_HAS_AARCH64_TARGET=${TVM_LLVM_HAS_AARCH64_TARGET})
tvm_file_glob(GLOB COMPILER_LLVM_SRCS src/target/llvm/*.cc)
list(APPEND TVM_LINKER_LIBS ${LLVM_LIBS})
list(APPEND COMPILER_SRCS ${COMPILER_LLVM_SRCS})
Expand Down
18 changes: 18 additions & 0 deletions cmake/utils/FindLLVM.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ macro(find_llvm use_llvm)
endif()
set(TVM_LLVM_VERSION ${LLVM_VERSION_MAJOR}${LLVM_VERSION_MINOR})
set(TVM_INFO_LLVM_VERSION "${LLVM_VERSION_MAJOR}.${LLVM_VERSION_MINOR}.${LLVM_VERSION_PATCH}")
set(TVM_LLVM_HAS_AARCH64_TARGET 0)
if(DEFINED LLVM_TARGETS_TO_BUILD AND "AArch64" IN_LIST LLVM_TARGETS_TO_BUILD)
set(TVM_LLVM_HAS_AARCH64_TARGET 1)
endif()
else()
# use llvm config
message(STATUS "Use llvm-config=" ${LLVM_CONFIG})
Expand Down Expand Up @@ -118,6 +122,13 @@ macro(find_llvm use_llvm)
if(NOT "${__llvm_exit_code}" STREQUAL "0")
message(FATAL_ERROR "Fatal error executing: ${LLVM_CONFIG} --cmakedir")
endif()
execute_process(COMMAND ${LLVM_CONFIG} --targets-built
RESULT_VARIABLE __llvm_exit_code
OUTPUT_VARIABLE __llvm_targets_built
OUTPUT_STRIP_TRAILING_WHITESPACE)
if(NOT "${__llvm_exit_code}" STREQUAL "0")
message(FATAL_ERROR "Fatal error executing: ${LLVM_CONFIG} --targets-built")
endif()
cmake_path(SET "__llvm_cmakedir" "${__llvm_cmakedir}")
message(STATUS "LLVM cmakedir: ${__llvm_cmakedir}")
# map prefix => $
Expand Down Expand Up @@ -152,6 +163,12 @@ macro(find_llvm use_llvm)
string(REPLACE "$" ${__llvm_prefix} __lib_with_prefix "${__flag}")
list(APPEND LLVM_LIBS "${__lib_with_prefix}")
endforeach()
# targets built
set(TVM_LLVM_HAS_AARCH64_TARGET 0)
separate_arguments(BUILT_TARGET_LIST NATIVE_COMMAND ${__llvm_targets_built})
if("AArch64" IN_LIST BUILT_TARGET_LIST)
set(TVM_LLVM_HAS_AARCH64_TARGET 1)
endif()
if (${USE_MLIR})
if (EXISTS "${__llvm_libdir}/libMLIRPresburger.a")
if (EXISTS "${__llvm_libdir}/libMLIRSupport.a")
Expand Down Expand Up @@ -203,4 +220,5 @@ macro(find_llvm use_llvm)
if (${TVM_LLVM_VERSION} LESS 40)
message(FATAL_ERROR "TVM requires LLVM 4.0 or higher.")
endif()
message(STATUS "Found TVM_LLVM_HAS_AARCH64_TARGET=" ${TVM_LLVM_HAS_AARCH64_TARGET})
endmacro(find_llvm)
7 changes: 4 additions & 3 deletions src/target/parsers/aprofile.cc
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ static TargetFeatures GetFeatures(TargetJSON target) {
Array<String> targets = llvm_backend.GetAllLLVMTargets();
if ((IsAArch64(mtriple) && !CheckContains(targets, "aarch64")) ||
(IsAArch32(mtriple, mcpu) && !CheckContains(targets, "arm"))) {
LOG(WARNING) << "Cannot parse target features. LLVM was not compiled with support for "
"Arm(R)-based targets.";
LOG(WARNING) << "Cannot parse target features for target: " << target
<< ". LLVM was not compiled with support for Arm(R)-based targets.";
return {};
}

Expand All @@ -115,7 +115,8 @@ static TargetFeatures GetFeatures(TargetJSON target) {
{"has_sme", Bool(has_feature("sme"))}};
#endif

LOG(WARNING) << "Cannot parse Arm(R)-based target features without LLVM support.";
LOG(WARNING) << "Cannot parse Arm(R)-based target features for target " << target
<< " without LLVM support.";
return {};
}

Expand Down
6 changes: 5 additions & 1 deletion src/target/tag.cc
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ Target TargetTag::AddTag(String name, Map<String, ObjectRef> config, bool overri

/********** Register Target tags **********/

#if TVM_LLVM_HAS_AARCH64_TARGET
TVM_REGISTER_TARGET_TAG("raspberry-pi/4b-aarch64")
.set_config({{"kind", String("llvm")},
{"mtriple", String("aarch64-linux-gnu")},
Expand Down Expand Up @@ -130,7 +131,8 @@ TVM_REGISTER_TARGET_TAG("nvidia/jetson-agx-orin-64gb")
{"mtriple", String("aarch64-linux-gnu")},
{"mcpu", String("cortex-a78")},
{"num-cores", Integer(12)}}}});
#endif
#endif // TVM_LLVM_VERSION >= 110
#endif // TVM_LLVM_HAS_AARCH64_TARGET

#define TVM_REGISTER_CUDA_TAG(Name, Arch, SharedMem, RegPerBlock) \
TVM_REGISTER_TARGET_TAG(Name).set_config({ \
Expand Down Expand Up @@ -437,9 +439,11 @@ TVM_REGISTER_TAG_AWS_C5("aws/cpu/c5.24xlarge", 48, "cascadelake");
{"mtriple", String("arm64-apple-macos")}, \
{"mcpu", String("apple-latest")}}}});

#if TVM_LLVM_HAS_AARCH64_TARGET
TVM_REGISTER_METAL_GPU_TAG("apple/m1-gpu", 1024, 32768, 32);
TVM_REGISTER_METAL_GPU_TAG("apple/m1-gpu-restricted", 256, 32768, 32);
TVM_REGISTER_METAL_GPU_TAG("apple/m2-gpu", 1024, 32768, 32);
#endif // TVM_LLVM_HAS_AARCH64_TARGET

#undef TVM_REGISTER_METAL_TAG

Expand Down

0 comments on commit 857fe61

Please sign in to comment.