Skip to content

Commit

Permalink
[llvm-jitlink] Pass object features when creating MCSubtargetInfo
Browse files Browse the repository at this point in the history
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
  • Loading branch information
mtvec committed May 13, 2023
1 parent 6285863 commit 946e7aa
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
30 changes: 19 additions & 11 deletions llvm/tools/llvm-jitlink/llvm-jitlink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -893,7 +893,8 @@ class PhonyExternalsGenerator : public DefinitionGenerator {
}
};

Expected<std::unique_ptr<Session>> Session::Create(Triple TT) {
Expected<std::unique_ptr<Session>> Session::Create(Triple TT,
SubtargetFeatures Features) {

std::unique_ptr<ExecutorProcessControl> EPC;
if (OutOfProcessExecutor.getNumOccurrences()) {
Expand Down Expand Up @@ -923,6 +924,7 @@ Expected<std::unique_ptr<Session>> Session::Create(Triple TT) {
std::unique_ptr<Session> S(new Session(std::move(EPC), Err));
if (Err)
return std::move(Err);
S->Features = std::move(Features);
return std::move(S);
}

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

} // end namespace llvm

static Triple getFirstFileTriple() {
static Triple FirstTT = []() {
static std::pair<Triple, SubtargetFeatures> getFirstFileTripleAndFeatures() {
static std::pair<Triple, SubtargetFeatures> FirstTTAndFeatures = []() {
assert(!InputFiles.empty() && "InputFiles can not be empty");
for (auto InputFile : InputFiles) {
auto ObjBuffer = ExitOnErr(getFile(InputFile));
Expand All @@ -1241,16 +1243,19 @@ static Triple getFirstFileTriple() {
TT.setObjectFormat(Triple::COFF);
TT.setOS(Triple::OSType::Win32);
}
return TT;
SubtargetFeatures Features;
if (auto ObjFeatures = Obj->getFeatures())
Features = std::move(*ObjFeatures);
return std::make_pair(TT, Features);
}
default:
break;
}
}
return Triple();
return std::make_pair(Triple(), SubtargetFeatures());
}();

return FirstTT;
return FirstTTAndFeatures;
}

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

static TargetInfo getTargetInfo(const Triple &TT) {
static TargetInfo
getTargetInfo(const Triple &TT,
const SubtargetFeatures &TF = SubtargetFeatures()) {
auto TripleName = TT.str();
std::string ErrorStr;
const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, ErrorStr);
Expand All @@ -1818,7 +1825,7 @@ static TargetInfo getTargetInfo(const Triple &TT) {
inconvertibleErrorCode()));

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

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

auto TI = getTargetInfo(S.ES.getTargetTriple());
auto TI = getTargetInfo(S.ES.getTargetTriple(), S.Features);

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

ExitOnErr(sanitizeArguments(getFirstFileTriple(), argv[0]));
auto [TT, Features] = getFirstFileTripleAndFeatures();
ExitOnErr(sanitizeArguments(TT, argv[0]));

auto S = ExitOnErr(Session::Create(getFirstFileTriple()));
auto S = ExitOnErr(Session::Create(std::move(TT), std::move(Features)));

{
TimeRegion TR(Timers ? &Timers->LoadObjectsTimer : nullptr);
Expand Down
5 changes: 4 additions & 1 deletion llvm/tools/llvm-jitlink/llvm-jitlink.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h"
#include "llvm/ExecutionEngine/Orc/SimpleRemoteEPC.h"
#include "llvm/ExecutionEngine/RuntimeDyldChecker.h"
#include "llvm/MC/SubtargetFeature.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/Regex.h"
#include "llvm/Support/raw_ostream.h"
Expand All @@ -36,10 +37,12 @@ struct Session {
orc::JITDylib *MainJD = nullptr;
orc::ObjectLinkingLayer ObjLayer;
orc::JITDylibSearchOrder JDSearchOrder;
SubtargetFeatures Features;

~Session();

static Expected<std::unique_ptr<Session>> Create(Triple TT);
static Expected<std::unique_ptr<Session>> Create(Triple TT,
SubtargetFeatures Features);
void dumpSessionInfo(raw_ostream &OS);
void modifyPassConfig(const Triple &FTT,
jitlink::PassConfiguration &PassConfig);
Expand Down

0 comments on commit 946e7aa

Please sign in to comment.