Skip to content

Commit 946e7aa

Browse files
committed
[llvm-jitlink] Pass object features when creating MCSubtargetInfo
The reason for this patch is to allow the MCDisassembler used in tests to disassemble instructions that are only available when a specific feature is enabled. For example, on RISC-V it's currently not possible to use decode_operand() on a compressed instruction. This patch fixes this. Reviewed By: lhames Differential Revision: https://reviews.llvm.org/D149523
1 parent 6285863 commit 946e7aa

File tree

2 files changed

+23
-12
lines changed

2 files changed

+23
-12
lines changed

llvm/tools/llvm-jitlink/llvm-jitlink.cpp

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -893,7 +893,8 @@ class PhonyExternalsGenerator : public DefinitionGenerator {
893893
}
894894
};
895895

896-
Expected<std::unique_ptr<Session>> Session::Create(Triple TT) {
896+
Expected<std::unique_ptr<Session>> Session::Create(Triple TT,
897+
SubtargetFeatures Features) {
897898

898899
std::unique_ptr<ExecutorProcessControl> EPC;
899900
if (OutOfProcessExecutor.getNumOccurrences()) {
@@ -923,6 +924,7 @@ Expected<std::unique_ptr<Session>> Session::Create(Triple TT) {
923924
std::unique_ptr<Session> S(new Session(std::move(EPC), Err));
924925
if (Err)
925926
return std::move(Err);
927+
S->Features = std::move(Features);
926928
return std::move(S);
927929
}
928930

@@ -1223,8 +1225,8 @@ Session::findSymbolInfo(StringRef SymbolName, Twine ErrorMsgStem) {
12231225

12241226
} // end namespace llvm
12251227

1226-
static Triple getFirstFileTriple() {
1227-
static Triple FirstTT = []() {
1228+
static std::pair<Triple, SubtargetFeatures> getFirstFileTripleAndFeatures() {
1229+
static std::pair<Triple, SubtargetFeatures> FirstTTAndFeatures = []() {
12281230
assert(!InputFiles.empty() && "InputFiles can not be empty");
12291231
for (auto InputFile : InputFiles) {
12301232
auto ObjBuffer = ExitOnErr(getFile(InputFile));
@@ -1241,16 +1243,19 @@ static Triple getFirstFileTriple() {
12411243
TT.setObjectFormat(Triple::COFF);
12421244
TT.setOS(Triple::OSType::Win32);
12431245
}
1244-
return TT;
1246+
SubtargetFeatures Features;
1247+
if (auto ObjFeatures = Obj->getFeatures())
1248+
Features = std::move(*ObjFeatures);
1249+
return std::make_pair(TT, Features);
12451250
}
12461251
default:
12471252
break;
12481253
}
12491254
}
1250-
return Triple();
1255+
return std::make_pair(Triple(), SubtargetFeatures());
12511256
}();
12521257

1253-
return FirstTT;
1258+
return FirstTTAndFeatures;
12541259
}
12551260

12561261
static Error sanitizeArguments(const Triple &TT, const char *ArgV0) {
@@ -1808,7 +1813,9 @@ struct TargetInfo {
18081813
};
18091814
} // anonymous namespace
18101815

1811-
static TargetInfo getTargetInfo(const Triple &TT) {
1816+
static TargetInfo
1817+
getTargetInfo(const Triple &TT,
1818+
const SubtargetFeatures &TF = SubtargetFeatures()) {
18121819
auto TripleName = TT.str();
18131820
std::string ErrorStr;
18141821
const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, ErrorStr);
@@ -1818,7 +1825,7 @@ static TargetInfo getTargetInfo(const Triple &TT) {
18181825
inconvertibleErrorCode()));
18191826

18201827
std::unique_ptr<MCSubtargetInfo> STI(
1821-
TheTarget->createMCSubtargetInfo(TripleName, "", ""));
1828+
TheTarget->createMCSubtargetInfo(TripleName, "", TF.getString()));
18221829
if (!STI)
18231830
ExitOnErr(
18241831
make_error<StringError>("Unable to create subtarget for " + TripleName,
@@ -1879,7 +1886,7 @@ static Error runChecks(Session &S) {
18791886

18801887
LLVM_DEBUG(dbgs() << "Running checks...\n");
18811888

1882-
auto TI = getTargetInfo(S.ES.getTargetTriple());
1889+
auto TI = getTargetInfo(S.ES.getTargetTriple(), S.Features);
18831890

18841891
auto IsSymbolValid = [&S](StringRef Symbol) {
18851892
return S.isSymbolRegistered(Symbol);
@@ -2026,9 +2033,10 @@ int main(int argc, char *argv[]) {
20262033
std::unique_ptr<JITLinkTimers> Timers =
20272034
ShowTimes ? std::make_unique<JITLinkTimers>() : nullptr;
20282035

2029-
ExitOnErr(sanitizeArguments(getFirstFileTriple(), argv[0]));
2036+
auto [TT, Features] = getFirstFileTripleAndFeatures();
2037+
ExitOnErr(sanitizeArguments(TT, argv[0]));
20302038

2031-
auto S = ExitOnErr(Session::Create(getFirstFileTriple()));
2039+
auto S = ExitOnErr(Session::Create(std::move(TT), std::move(Features)));
20322040

20332041
{
20342042
TimeRegion TR(Timers ? &Timers->LoadObjectsTimer : nullptr);

llvm/tools/llvm-jitlink/llvm-jitlink.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h"
2020
#include "llvm/ExecutionEngine/Orc/SimpleRemoteEPC.h"
2121
#include "llvm/ExecutionEngine/RuntimeDyldChecker.h"
22+
#include "llvm/MC/SubtargetFeature.h"
2223
#include "llvm/Support/Error.h"
2324
#include "llvm/Support/Regex.h"
2425
#include "llvm/Support/raw_ostream.h"
@@ -36,10 +37,12 @@ struct Session {
3637
orc::JITDylib *MainJD = nullptr;
3738
orc::ObjectLinkingLayer ObjLayer;
3839
orc::JITDylibSearchOrder JDSearchOrder;
40+
SubtargetFeatures Features;
3941

4042
~Session();
4143

42-
static Expected<std::unique_ptr<Session>> Create(Triple TT);
44+
static Expected<std::unique_ptr<Session>> Create(Triple TT,
45+
SubtargetFeatures Features);
4346
void dumpSessionInfo(raw_ostream &OS);
4447
void modifyPassConfig(const Triple &FTT,
4548
jitlink::PassConfiguration &PassConfig);

0 commit comments

Comments
 (0)