Skip to content

Commit 21510cc

Browse files
committed
[LLVM] Fix JIT unknown reloc issue for case of RISCV
1 parent ae053e6 commit 21510cc

File tree

3 files changed

+31
-52
lines changed

3 files changed

+31
-52
lines changed

src/target/llvm/llvm_instance.cc

Lines changed: 16 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -53,18 +53,6 @@
5353
#include <llvm/Support/raw_ostream.h>
5454
#include <llvm/Target/TargetMachine.h>
5555
#include <llvm/Target/TargetOptions.h>
56-
#if TVM_LLVM_VERSION >= 190
57-
#include <llvm/TargetParser/RISCVISAInfo.h>
58-
#else
59-
#if TVM_LLVM_VERSION >= 140
60-
#include <llvm/Support/RISCVISAInfo.h>
61-
#endif
62-
#endif
63-
#if TVM_LLVM_VERSION >= 160
64-
#include <llvm/TargetParser/RISCVTargetParser.h>
65-
#else
66-
#include <llvm/Support/TargetParser.h>
67-
#endif
6856
#include <tvm/ffi/container/array.h>
6957
#include <tvm/ffi/container/map.h>
7058
#include <tvm/ffi/optional.h>
@@ -299,34 +287,25 @@ LLVMTargetInfo::LLVMTargetInfo(LLVMInstance& instance, const TargetJSON& target)
299287
// code model
300288
code_model_ = llvm::CodeModel::Medium;
301289
#if TVM_LLVM_VERSION >= 140
302-
// VLEN inference
303-
const auto cpu_name = GetOrCreateTargetMachine(false)->getMCSubtargetInfo()->getCPU();
304-
const auto canon_arch = llvm::RISCV::getMArchFromMcpu(cpu_name);
305-
auto ISAInfo =
306-
llvm::RISCVISAInfo::parseArchString(canon_arch, /*EnableExperimentalExtensions=*/true);
307-
// infer VLEN from LLVM RISCVInfo parser
308-
if (!llvm::errorToBool(ISAInfo.takeError()) && (vector_width_ == 0)) {
309-
vector_width_ = (*ISAInfo)->getMinVLen();
310-
}
311-
// infer VLEN from LLVM options (zvlXXXb override)
312-
for (const auto& attr : attrs_) {
313-
if (attr.find("zvl") != std::string::npos) {
314-
std::string vec;
315-
for (char c : attr) {
316-
if (std::isdigit(c)) vec += c;
290+
// get VLEN from the LLVM backend (zvlXXXb)
291+
Map<String, String> features = GetAllLLVMCpuFeatures();
292+
// check vector ISA
293+
if (features.count("v") > 0) {
294+
vector_width_ = 0;
295+
int zvlbits = 0;
296+
for (const auto& [attr, val] : features) {
297+
if (std::string(attr).find("zvl") != std::string::npos) {
298+
std::string vec;
299+
for (char c : std::string(attr)) {
300+
if (std::isdigit(c)) vec += c;
301+
}
302+
zvlbits = std::stoi(vec);
303+
// max of the multiple zvlXXXb
304+
if (vector_width_ < zvlbits) vector_width_ = zvlbits;
317305
}
318-
vector_width_ = std::stoi(vec);
319306
}
320307
}
321308
#endif
322-
if (vector_width_ > 0) {
323-
// push cl-opt to LLVM
324-
llvm_options_.push_back(
325-
ParseOptionString("-riscv-v-vector-bits-min:int=" + std::to_string(vector_width_)));
326-
} else {
327-
// fallback default (codegen will warn)
328-
llvm_options_.push_back(ParseOptionString("-riscv-v-vector-bits-min:int=256"));
329-
}
330309
}
331310

332311
// Target options
@@ -943,9 +922,7 @@ const int LLVMTargetInfo::GetVectorWidth() {
943922
} else if (arch == llvm::Triple::arm || arch == llvm::Triple::aarch64) {
944923
vector_width_ = 128;
945924
} else if (arch == llvm::Triple::riscv32 || arch == llvm::Triple::riscv64) {
946-
vector_width_ = 256;
947-
LOG(WARNING) << "LLVM RVV VLEN inference failed, "
948-
<< "using 256 bits, set -vector-width=XXX to override";
925+
vector_width_ = 128;
949926
} else {
950927
// fallback default
951928
vector_width_ = 128;

src/target/llvm/llvm_module.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@
3131
#include <llvm/ExecutionEngine/MCJIT.h>
3232
#include <llvm/ExecutionEngine/Orc/LLJIT.h>
3333
#include <llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h>
34+
#if _WIN32
3435
#include <llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h>
3536
#include <llvm/ExecutionEngine/SectionMemoryManager.h>
37+
#endif
3638
#include <llvm/IR/DataLayout.h>
3739
#include <llvm/IR/Function.h>
3840
#include <llvm/IR/Intrinsics.h>
@@ -503,9 +505,13 @@ void LLVMModuleNode::InitORCJIT() {
503505
const auto linkerBuilder =
504506
[&](llvm::orc::ExecutionSession& session,
505507
const llvm::Triple& triple) -> std::unique_ptr<llvm::orc::ObjectLayer> {
508+
#if _WIN32
506509
auto GetMemMgr = []() { return std::make_unique<llvm::SectionMemoryManager>(); };
507510
auto ObjLinkingLayer =
508511
std::make_unique<llvm::orc::RTDyldObjectLinkingLayer>(session, std::move(GetMemMgr));
512+
#else
513+
auto ObjLinkingLayer = std::make_unique<llvm::orc::ObjectLinkingLayer>(session);
514+
#endif
509515
if (triple.isOSBinFormatCOFF()) {
510516
ObjLinkingLayer->setOverrideObjectFlagsWithResponsibilityFlags(true);
511517
ObjLinkingLayer->setAutoClaimResponsibilityForObjectSymbols(true);

tests/python/target/test_riscv_features.py

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,16 @@
2424

2525
# fmt: off
2626
min_llvm_version, tvm_target, vec_width = tvm.testing.parameters(
27-
# generic, no-vec -> (default 256)
28-
(-1, "llvm -device=riscv_cpu -mtriple=riscv64-linux-gnu -mcpu=generic-rv64 -mattr=+i,+m", 256),
29-
(-1, "llvm -device=riscv_cpu -mtriple=riscv32-linux-gnu -mcpu=generic-rv32 -mattr=+64bit,+a,+c,+d,+f,+m", 256),
30-
# generic, with-vec -> (default 256)
31-
(-1, "llvm -device=riscv_cpu -mtriple=riscv32-linux-gnu -mcpu=generic-rv32 -mattr=+i,+m,+v", 256),
32-
(-1, "llvm -device=riscv_cpu -mtriple=riscv64-linux-gnu -mcpu=generic-rv64 -mattr=+64bit,+a,+c,+d,+f,+m,+v", 256),
33-
# explicit -vector-width
34-
(-1, "llvm -device=riscv_cpu -vector-width=128 -mtriple=riscv32-linux-gnu -mcpu=generic-rv32 -mattr=+i,+m,+v", 128),
35-
(-1, "llvm -device=riscv_cpu -vector-width=128 -mtriple=riscv64-linux-gnu -mcpu=generic-rv64 -mattr=+64bit,+a,+c,+d,+f,+m,+v", 128),
36-
(-1, "llvm -device=riscv_cpu -vector-width=512 -mtriple=riscv32-linux-gnu -mcpu=generic-rv32 -mattr=+i,+m,+v", 512),
37-
(-1, "llvm -device=riscv_cpu -vector-width=512 -mtriple=riscv64-linux-gnu -mcpu=generic-rv64 -mattr=+64bit,+a,+c,+d,+f,+m,+v", 512),
27+
# generic, no vector -> (default 128)
28+
(-1, "llvm -device=riscv_cpu -mtriple=riscv64-linux-gnu -mcpu=generic-rv64 -mattr=+i,+m", 128),
29+
(-1, "llvm -device=riscv_cpu -mtriple=riscv32-linux-gnu -mcpu=generic-rv32 -mattr=+64bit,+a,+c,+d,+f,+m", 128),
30+
# generic, with vector -> (default zvl128b)
31+
(-1, "llvm -device=riscv_cpu -mtriple=riscv32-linux-gnu -mcpu=generic-rv32 -mattr=+i,+m,+v", 128),
32+
(-1, "llvm -device=riscv_cpu -mtriple=riscv64-linux-gnu -mcpu=generic-rv64 -mattr=+64bit,+a,+c,+d,+f,+m,+v", 128),
3833
# explicit +zvlXXXb
39-
(14, "llvm -device=riscv_cpu -mtriple=riscv32-linux-gnu -mcpu=generic-rv32 -mattr=+i,+m,+v,+zvl64b", 64),
40-
(14, "llvm -device=riscv_cpu -mtriple=riscv64-linux-gnu -mcpu=generic-rv64 -mattr=+64bit,+a,+c,+d,+f,+m,+v,+zvl64b", 64),
34+
(14, "llvm -device=riscv_cpu -mtriple=riscv32-linux-gnu -mcpu=generic-rv32 -mattr=+i,+m,+v,+zvl64b", 128),
35+
(14, "llvm -device=riscv_cpu -mtriple=riscv64-linux-gnu -mcpu=generic-rv64 -mattr=+64bit,+a,+c,+d,+f,+m,+v,+zvl256b", 256),
36+
(14, "llvm -device=riscv_cpu -mtriple=riscv64-linux-gnu -mcpu=generic-rv64 -mattr=+64bit,+a,+c,+d,+f,+m,+v,+zvl512b", 512),
4137
# vendor CPU
4238
(17, "llvm -device=riscv_cpu -mtriple=riscv64-linux-gnu -mcpu=sifive-x280", 512),
4339
(18, "llvm -device=riscv_cpu -mtriple=riscv64-linux-gnu -mcpu=sifive-p670", 128),

0 commit comments

Comments
 (0)