Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for host target setting of support of features
Browse files Browse the repository at this point in the history
Allow features to be added using env variable or cmake using standard
mechanism of "+v,+zfence" etc
coldav committed Jul 16, 2024
1 parent 872ac00 commit a73ffcd
Showing 3 changed files with 76 additions and 6 deletions.
7 changes: 7 additions & 0 deletions doc/developer-guide.md
Original file line number Diff line number Diff line change
@@ -320,6 +320,13 @@ The builtin CMake options used when invoking CMake on the command line.
caveats above apply, and this may result in an illegal instruction crash if
your CPU doesn't support the generated instructions.

- `CA_HOST_TARGET_<arch>_FEATURES`: This option is used by the `host`` target to
enable features on a given CPU. `arch` should be a capitalized version of the
`host` target architecture e.g. `X86_64`, `RISCV64` or `AARCH64`. This should
be of the form of a comma separated list of features with either a '+' or '-'
preceding each feature to enable or disable e.g. "+v,-zfencei". The
environment variable `CA_HOST_TARGET_FEATURES` can also be used to enable or
disable features.
- `CA_USE_SPLIT_DWARF`: When building with gcc, enable split dwarf debuginfo.
This significantly reduces binary size (especially when static linking) and
speeds up the link step. Requires a non-ancient toolchain.
25 changes: 19 additions & 6 deletions modules/compiler/targets/host/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -188,7 +188,13 @@ if(CA_HOST_CROSS_COMPILERS)
target_compile_definitions(compiler-host PRIVATE
CA_HOST_TARGET_${HOST_ARCH_UPPER}_CPU="${CA_HOST_TARGET_${HOST_ARCH_UPPER}_CPU}")
endif()

ca_option(CA_HOST_TARGET_${HOST_ARCH_UPPER}_FEATURES STRING
"Feature list for host that host ${HOST_ARCH_UPPER} as a comma separated + or - list e.g. '+v,+zfh'" "")
if(CA_HOST_TARGET_${HOST_ARCH_UPPER}_FEATURES)
message(STATUS "Features ${HOST_ARCH_UPPER} name ${CA_HOST_TARGET_${HOST_ARCH_UPPER}_FEATURES}")
target_compile_definitions(compiler-host PRIVATE
CA_HOST_TARGET_${HOST_ARCH_UPPER}_FEATURES="${CA_HOST_TARGET_${HOST_ARCH_UPPER}_FEATURES}")
endif()
if(hostCrossCompilers)
# Validate the user specified cross compiler list.
foreach(CrossCompiler ${hostCrossCompilers})
@@ -225,11 +231,18 @@ if(CA_HOST_CROSS_COMPILERS)
HOST_CROSS_DEVICE_NAME_${CROSS_COMPILER}="${crossDeviceName}")
ca_option(CA_HOST_TARGET_${CROSS_COMPILER}_CPU STRING
"Name of the CPU that host ${CROSS_COMPILER} should optimize for, or 'native'" "")
if(CA_HOST_TARGET_${CROSS_COMPILER}_CPU)
message(STATUS "CPU ${CROSS_COMPILER} name ${CA_HOST_TARGET_${CROSS_COMPILER}_CPU}")
target_compile_definitions(compiler-host PRIVATE
CA_HOST_TARGET_${CROSS_COMPILER}_CPU="${CA_HOST_TARGET_${CROSS_COMPILER}_CPU}")
endif()
if(CA_HOST_TARGET_${CROSS_COMPILER}_CPU)
message(STATUS "CPU ${CROSS_COMPILER} name ${CA_HOST_TARGET_${CROSS_COMPILER}_CPU}")
target_compile_definitions(compiler-host PRIVATE
CA_HOST_TARGET_${CROSS_COMPILER}_CPU="${CA_HOST_TARGET_${CROSS_COMPILER}_CPU}")
endif()
ca_option(CA_HOST_TARGET_${CROSS_COMPILER}_FEATURES STRING
"Feature list for host that host ${HOST_ARCH_UPPER} as a comma separated + or - list e.g. '+v,+zfh'" "")
if(CA_HOST_TARGET_${CROSS_COMPILER}_FEATURES)
message(STATUS "Features ${CROSS_COMPILER} name ${CA_HOST_TARGET_${CROSS_COMPILER}_FEATURES}")
target_compile_definitions(compiler-host PRIVATE
CA_HOST_TARGET_${CROSS_COMPILER}_FEATURES="${CA_HOST_TARGET_${CROSS_COMPILER}_FEATURES}")
endif()
endforeach()
endif()
endif()
50 changes: 50 additions & 0 deletions modules/compiler/targets/host/source/target.cpp
Original file line number Diff line number Diff line change
@@ -102,6 +102,29 @@ static llvm::TargetMachine *createTargetMachine(llvm::Triple TT,
/*JIT=*/true);
}

void UpdateFeatureMapFromString(llvm::StringMap<bool> &FeatureMap,
llvm::StringRef FeatureString) {
(void)FeatureMap;
if (FeatureString.empty()) {
return;
}
llvm::SmallVector<llvm::StringRef, 4> split;
FeatureString.split(split, ',', /*MaxSplit*/ -1, /*KeepEmpty*/ false);
for (auto Current : split) {
llvm::StringRef trimmed = Current.ltrim().rtrim();
char FirstChar = trimmed[0];
llvm::StringRef Feature = trimmed.drop_front(1);
if (FirstChar == '+') {
FeatureMap[Feature] = true;
} else if (FirstChar == '-') {
FeatureMap[Feature] = false;
} else {
llvm::errs() << "Warning: '" << FirstChar
<< "' should be '-' or '+' to add or remove feature\n";
}
}
}

HostTarget::HostTarget(const HostInfo *compiler_info,
compiler::Context *context,
compiler::NotifyCallbackFn callback)
@@ -242,6 +265,7 @@ compiler::Result HostTarget::initWithBuiltins(
llvm::StringRef ABI = "";
llvm::StringRef CPUName = "";
llvm::StringMap<bool> FeatureMap;
std::string FeatureStr;

if (llvm::Triple::arm == triple.getArch()) {
FeatureMap["strict-align"] = true;
@@ -261,20 +285,32 @@ compiler::Result HostTarget::initWithBuiltins(
}
#if defined(CA_HOST_TARGET_ARM_CPU)
CPUName = CA_HOST_TARGET_ARM_CPU;
#endif
#if defined(CA_HOST_TARGET_ARM_FEATURES)
FeatureStr = CA_HOST_TARGET_ARM_FEATURES;
#endif
} else if (llvm::Triple::aarch64 == triple.getArch()) {
#if defined(CA_HOST_TARGET_AARCH64_CPU)
CPUName = CA_HOST_TARGET_AARCH64_CPU;
#endif
#if defined(CA_HOST_TARGET_AARCH64_FEATURES)
FeatureStr = CA_HOST_TARGET_AARCH64_FEATURES;
#endif
} else if (triple.isX86()) {
CPUName = "x86-64-v3"; // Default only, may be overridden below.
if (triple.isArch32Bit()) {
#if defined(CA_HOST_TARGET_X86_CPU)
CPUName = CA_HOST_TARGET_X86_CPU;
#endif
#if defined(CA_HOST_TARGET_X86_FEATURES)
FeatureStr = CA_HOST_TARGET_X86_FEATURES;
#endif
} else {
#if defined(CA_HOST_TARGET_X86_64_CPU)
CPUName = CA_HOST_TARGET_X86_64_CPU;
#endif
#if defined(CA_HOST_TARGET_X86_64_FEATURES)
FeatureStr = CA_HOST_TARGET_X86_64_FEATURES;
#endif
}
} else if (triple.isRISCV()) {
@@ -285,12 +321,18 @@ compiler::Result HostTarget::initWithBuiltins(
CPUName = "generic-rv32";
#if defined(CA_HOST_TARGET_RISCV32_CPU)
CPUName = CA_HOST_TARGET_RISCV32_CPU;
#endif
#if defined(CA_HOST_TARGET_RISCV32_FEATURES)
FeatureStr = CA_HOST_TARGET_RISCV32_FEATURES;
#endif
} else {
ABI = "lp64d";
CPUName = "generic-rv64";
#if defined(CA_HOST_TARGET_RISCV64_CPU)
CPUName = CA_HOST_TARGET_RISCV64_CPU;
#endif
#if defined(CA_HOST_TARGET_RISCV64_FEATURES)
FeatureStr = CA_HOST_TARGET_RISCV64_FEATURES;
#endif
}
// The following features are important for OpenCL, and generally constitute
@@ -314,6 +356,12 @@ compiler::Result HostTarget::initWithBuiltins(
}
#endif

#ifndef NDEBUG
if (const char *E = getenv("CA_HOST_TARGET_FEATURES")) {
FeatureStr = E;
}
#endif

if (CPUName == "native") {
CPUName = llvm::sys::getHostCPUName();
#if LLVM_VERSION_GREATER_EQUAL(19, 0)
@@ -324,6 +372,8 @@ compiler::Result HostTarget::initWithBuiltins(
#endif
}

UpdateFeatureMapFromString(FeatureMap, FeatureStr);

if (compiler_info->supports_deferred_compilation) {
llvm::orc::JITTargetMachineBuilder TMBuilder(triple);
TMBuilder.setCPU(CPUName.str());

0 comments on commit a73ffcd

Please sign in to comment.