You are viewing a plain text version of this content. The canonical link for it is here.
Posted to discuss-archive@tvm.apache.org by "Ga1axy0 (via GitHub)" <gi...@apache.org> on 2026/06/23 19:05:06 UTC
[PR] [LLVM][RISCV] Detect local CPU features with hwprobe [tvm]
Ga1axy0 opened a new pull request, #19877:
URL: https://github.com/apache/tvm/pull/19877
## Description
This PR adds local RISC-V CPU feature detection for LLVM targets when TVM is running on RISC-V Linux.
When the target does not explicitly provide `mcpu` or `mattr`, TVM now uses `riscv_hwprobe` to populate the default LLVM target attributes from the local machine. This includes the base IMA extensions, F/D, C, and V.
When V is reported by `riscv_hwprobe`, TVM also reads the `vlenb` CSR to derive the local vector length and adds the matching `zvl*b` attribute, such as `+zvl256b`.
Explicit `mcpu` and `mattr` still take precedence, so cross-compilation behavior remains unchanged.
## Testing
Built TVM with LLVM enabled on a RISC-V Linux board and verified that `llvm_get_cpu_features` detects local features without explicit `mattr`.
```python
from tvm.target import Target
from tvm.target.codegen import llvm_get_cpu_features
target = Target({
"kind": "llvm",
"mtriple": "riscv64-linux-gnu",
"mabi": "lp64d",
"num-cores": 8,
})
print(sorted(llvm_get_cpu_features(target)))
```
On SpacemiT K3 X100, the detected feature set is:
```text
64bit
a
c
d
f
i
m
optimized-nf2-segment-load-store
v
zaamo
zalrsc
zca
zicsr
zmmul
zve32f
zve32x
zve64d
zve64f
zve64x
zvl128b
zvl256b
zvl32b
zvl64b
```
This matches the expected local CPU capabilities. In particular, d is detected for the lp64d ABI, v is detected for RVV support, and zvl256b reflects the board's 256-bit vector length as reported through vlenb.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org
Re: [PR] [LLVM][RISCV] Detect local CPU features with hwprobe [tvm]
Posted by "Ga1axy0 (via GitHub)" <gi...@apache.org>.
Ga1axy0 commented on code in PR #19877:
URL: https://github.com/apache/tvm/pull/19877#discussion_r3462287219
##########
src/target/llvm/llvm_instance.cc:
##########
@@ -199,13 +277,26 @@ LLVMTargetInfo::LLVMTargetInfo(LLVMInstance& instance,
if (triple_.empty() || triple_ == "default") {
triple_ = llvm::sys::getDefaultTargetTriple();
}
+ bool has_explicit_mcpu = target.Get("mcpu").has_value();
cpu_ = Downcast<ffi::String>(target.Get("mcpu").value_or(ffi::String(defaults::cpu)));
+ bool has_explicit_mattr = false;
if (const auto& v = Downcast<ffi::Optional<ffi::Array<ffi::String>>>(target.Get("mattr"))) {
+ has_explicit_mattr = true;
for (const ffi::String& s : v.value()) {
attrs_.push_back(s);
}
}
+
+ auto arch = llvm::Triple(triple_).getArch();
+ if (!has_explicit_mattr && !has_explicit_mcpu &&
+ (arch == llvm::Triple::riscv32 || arch == llvm::Triple::riscv64)) {
+ std::vector<std::string> detected_attrs = DetectLocalRISCVAttrs();
+ for (const std::string& attr : detected_attrs) {
+ attrs_.push_back(attr);
+ }
+ }
Review Comment:
I agree that applying host-detected features should only happen when the target is the host machine.
I updated the logic to compare the target triple against LLVM's default host triple before using `riscv_hwprobe`. The detected RISC-V attributes are now only applied when `mcpu` and `mattr` are both unspecified, the target architecture matches the host architecture, and the target OS matches the host OS.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org
Re: [PR] [LLVM][RISCV] Detect local CPU features with hwprobe [tvm]
Posted by "Ga1axy0 (via GitHub)" <gi...@apache.org>.
Ga1axy0 commented on code in PR #19877:
URL: https://github.com/apache/tvm/pull/19877#discussion_r3462287219
##########
src/target/llvm/llvm_instance.cc:
##########
@@ -199,13 +277,26 @@ LLVMTargetInfo::LLVMTargetInfo(LLVMInstance& instance,
if (triple_.empty() || triple_ == "default") {
triple_ = llvm::sys::getDefaultTargetTriple();
}
+ bool has_explicit_mcpu = target.Get("mcpu").has_value();
cpu_ = Downcast<ffi::String>(target.Get("mcpu").value_or(ffi::String(defaults::cpu)));
+ bool has_explicit_mattr = false;
if (const auto& v = Downcast<ffi::Optional<ffi::Array<ffi::String>>>(target.Get("mattr"))) {
+ has_explicit_mattr = true;
for (const ffi::String& s : v.value()) {
attrs_.push_back(s);
}
}
+
+ auto arch = llvm::Triple(triple_).getArch();
+ if (!has_explicit_mattr && !has_explicit_mcpu &&
+ (arch == llvm::Triple::riscv32 || arch == llvm::Triple::riscv64)) {
+ std::vector<std::string> detected_attrs = DetectLocalRISCVAttrs();
+ for (const std::string& attr : detected_attrs) {
+ attrs_.push_back(attr);
+ }
+ }
Review Comment:
I agree that applying host-detected features should only happen when the target is the host machine. And I updated the logic to compare the target triple against LLVM's default host triple before using `riscv_hwprobe`. The detected RISC-V attributes are now only applied when `mcpu` and `mattr` are both unspecified, the target architecture matches the host architecture, and the target OS matches the host OS.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org
Re: [PR] [LLVM][RISCV] Detect local CPU features with hwprobe [tvm]
Posted by "gemini-code-assist[bot] (via GitHub)" <gi...@apache.org>.
gemini-code-assist[bot] commented on code in PR #19877:
URL: https://github.com/apache/tvm/pull/19877#discussion_r3462254280
##########
src/target/llvm/llvm_instance.cc:
##########
@@ -199,13 +277,26 @@ LLVMTargetInfo::LLVMTargetInfo(LLVMInstance& instance,
if (triple_.empty() || triple_ == "default") {
triple_ = llvm::sys::getDefaultTargetTriple();
}
+ bool has_explicit_mcpu = target.Get("mcpu").has_value();
cpu_ = Downcast<ffi::String>(target.Get("mcpu").value_or(ffi::String(defaults::cpu)));
+ bool has_explicit_mattr = false;
if (const auto& v = Downcast<ffi::Optional<ffi::Array<ffi::String>>>(target.Get("mattr"))) {
+ has_explicit_mattr = true;
for (const ffi::String& s : v.value()) {
attrs_.push_back(s);
}
}
+
+ auto arch = llvm::Triple(triple_).getArch();
+ if (!has_explicit_mattr && !has_explicit_mcpu &&
+ (arch == llvm::Triple::riscv32 || arch == llvm::Triple::riscv64)) {
+ std::vector<std::string> detected_attrs = DetectLocalRISCVAttrs();
+ for (const std::string& attr : detected_attrs) {
+ attrs_.push_back(attr);
+ }
+ }
Review Comment:

Automatically detecting and applying local CPU features when `mcpu` and `mattr` are not specified is a great feature, but it should only be done when targeting the host machine. If a user is cross-compiling on a RISC-V host for a different RISC-V target (for example, compiling for a 32-bit bare-metal target `riscv32-unknown-elf` from a 64-bit RISC-V Linux host), detecting and applying the host's 64-bit features (like double-precision float `+d` or vector extensions `+v`) will result in generating unsupported instructions for the target device.
To prevent this, we should verify that the target triple's architecture and OS match the host's architecture and OS before applying local CPU features.
```c
llvm::Triple target_triple(triple_);
auto arch = target_triple.getArch();
llvm::Triple host_triple(llvm::sys::getDefaultTargetTriple());
if (!has_explicit_mattr && !has_explicit_mcpu &&
arch == host_triple.getArch() &&
target_triple.getOS() == host_triple.getOS() &&
(arch == llvm::Triple::riscv32 || arch == llvm::Triple::riscv64)) {
std::vector<std::string> detected_attrs = DetectLocalRISCVAttrs();
for (const std::string& attr : detected_attrs) {
attrs_.push_back(attr);
}
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org
Re: [PR] [LLVM][RISCV] Detect local CPU features with hwprobe [tvm]
Posted by "Ga1axy0 (via GitHub)" <gi...@apache.org>.
Ga1axy0 commented on code in PR #19877:
URL: https://github.com/apache/tvm/pull/19877#discussion_r3464248447
##########
src/target/llvm/llvm_instance.cc:
##########
@@ -199,15 +277,31 @@ LLVMTargetInfo::LLVMTargetInfo(LLVMInstance& instance,
if (triple_.empty() || triple_ == "default") {
triple_ = llvm::sys::getDefaultTargetTriple();
}
+ bool has_explicit_mcpu = target.Get("mcpu").has_value();
cpu_ = target.Get("mcpu").value_or(ffi::String(defaults::cpu)).as_or_throw<ffi::String>();
+ bool has_explicit_mattr = false;
if (const auto& v = (target.Get("mattr"))
.value_or(nullptr)
.as_or_throw<ffi::Optional<ffi::Array<ffi::String>>>()) {
+ has_explicit_mattr = true;
for (const ffi::String& s : v.value()) {
attrs_.push_back(s);
}
}
+
+ llvm::Triple target_triple(triple_);
+ auto arch = target_triple.getArch();
+ llvm::Triple host_triple(llvm::sys::getDefaultTargetTriple());
+ if (!has_explicit_mattr && !has_explicit_mcpu && arch == host_triple.getArch() &&
+ target_triple.getOS() == host_triple.getOS() &&
Review Comment:
Thanks for the clarification. I agree that adding RISC-V-specific host probing logic in this generic LLVM path is not the right direction.
The original motivation is still valid, but it should probably be handled through LLVM or a more explicit TVM target mechanism instead. I will close this PR for now and reconsider the design.
Thanks for the review.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org
Re: [PR] [LLVM][RISCV] Detect local CPU features with hwprobe [tvm]
Posted by "tqchen (via GitHub)" <gi...@apache.org>.
tqchen commented on code in PR #19877:
URL: https://github.com/apache/tvm/pull/19877#discussion_r3462438165
##########
src/target/llvm/llvm_instance.cc:
##########
@@ -199,15 +277,31 @@ LLVMTargetInfo::LLVMTargetInfo(LLVMInstance& instance,
if (triple_.empty() || triple_ == "default") {
triple_ = llvm::sys::getDefaultTargetTriple();
}
+ bool has_explicit_mcpu = target.Get("mcpu").has_value();
cpu_ = target.Get("mcpu").value_or(ffi::String(defaults::cpu)).as_or_throw<ffi::String>();
+ bool has_explicit_mattr = false;
if (const auto& v = (target.Get("mattr"))
.value_or(nullptr)
.as_or_throw<ffi::Optional<ffi::Array<ffi::String>>>()) {
+ has_explicit_mattr = true;
for (const ffi::String& s : v.value()) {
attrs_.push_back(s);
}
}
+
+ llvm::Triple target_triple(triple_);
+ auto arch = target_triple.getArch();
+ llvm::Triple host_triple(llvm::sys::getDefaultTargetTriple());
+ if (!has_explicit_mattr && !has_explicit_mcpu && arch == host_triple.getArch() &&
+ target_triple.getOS() == host_triple.getOS() &&
Review Comment:
ideally we don't specialize for riscv here, would it be something that llvm upstream can do? or can we put some flags in target. and register target tag which helps cross compile
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org
Re: [PR] [LLVM][RISCV] Detect local CPU features with hwprobe [tvm]
Posted by "tqchen (via GitHub)" <gi...@apache.org>.
tqchen commented on code in PR #19877:
URL: https://github.com/apache/tvm/pull/19877#discussion_r3462438165
##########
src/target/llvm/llvm_instance.cc:
##########
@@ -199,15 +277,31 @@ LLVMTargetInfo::LLVMTargetInfo(LLVMInstance& instance,
if (triple_.empty() || triple_ == "default") {
triple_ = llvm::sys::getDefaultTargetTriple();
}
+ bool has_explicit_mcpu = target.Get("mcpu").has_value();
cpu_ = target.Get("mcpu").value_or(ffi::String(defaults::cpu)).as_or_throw<ffi::String>();
+ bool has_explicit_mattr = false;
if (const auto& v = (target.Get("mattr"))
.value_or(nullptr)
.as_or_throw<ffi::Optional<ffi::Array<ffi::String>>>()) {
+ has_explicit_mattr = true;
for (const ffi::String& s : v.value()) {
attrs_.push_back(s);
}
}
+
+ llvm::Triple target_triple(triple_);
+ auto arch = target_triple.getArch();
+ llvm::Triple host_triple(llvm::sys::getDefaultTargetTriple());
+ if (!has_explicit_mattr && !has_explicit_mcpu && arch == host_triple.getArch() &&
+ target_triple.getOS() == host_triple.getOS() &&
Review Comment:
ideally we don't specialize for riscv here, would it be something that llvm upstream can do? or can we put some flags in target. and register target tag which helps cross compile. e.g. register features via something like https://github.com/apache/tvm/blob/main/python/tvm/target/tag_registry/riscv_cpu.py#L30
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org
Re: [PR] [LLVM][RISCV] Detect local CPU features with hwprobe [tvm]
Posted by "Ga1axy0 (via GitHub)" <gi...@apache.org>.
Ga1axy0 closed pull request #19877: [LLVM][RISCV] Detect local CPU features with hwprobe
URL: https://github.com/apache/tvm/pull/19877
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org