Skip to content

Commit

Permalink
Add support for experimental OpenASIP RISC-V features
Browse files Browse the repository at this point in the history
  • Loading branch information
karihepola committed Oct 1, 2024
1 parent 3df0f48 commit 64c4c73
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 4 deletions.
10 changes: 10 additions & 0 deletions llvm/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1228,6 +1228,16 @@ if (LLVM_INCLUDE_UTILS)
add_subdirectory(utils/llvm-lit)
endif()

# Copy the RISCV Target directory to
install(DIRECTORY ${LLVM_MAIN_SRC_DIR}/lib/Target/RISCV
DESTINATION ${CMAKE_INSTALL_PREFIX}/riscv/LLVMRISCVTarget
FILES_MATCHING
PATTERN "*"
PATTERN "cmake/*" EXCLUDE
PATTERN "*.inc" EXCLUDE
PATTERN "*.def" EXCLUDE
PATTERN "CMakeLists.txt" EXCLUDE)

if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
install(DIRECTORY include/llvm include/llvm-c
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
Expand Down
43 changes: 40 additions & 3 deletions llvm/lib/Target/RISCV/RISCVISelLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#include "llvm/Support/MathExtras.h"
#include "llvm/Support/raw_ostream.h"
#include <optional>
#include "OpenASIPDefines.h"

using namespace llvm;

Expand Down Expand Up @@ -297,7 +298,13 @@ RISCVTargetLowering::RISCVTargetLowering(const TargetMachine &TM,
setOperationAction({ISD::ROTL, ISD::ROTR}, MVT::i32, Custom);
setOperationAction({ISD::ROTL, ISD::ROTR}, XLenVT, Custom);
} else {
setOperationAction({ISD::ROTL, ISD::ROTR}, XLenVT, Expand);
#ifndef OPENASIP_ROTL
setOperationAction(ISD::ROTL, XLenVT, Expand);
#endif
#ifndef OPENASIP_ROTR
setOperationAction(ISD::ROTR, XLenVT, Expand);
#endif

}

// With Zbb we have an XLen rev8 instruction, but not GREVI. So we'll
Expand All @@ -323,6 +330,20 @@ RISCVTargetLowering::RISCVTargetLowering(const TargetMachine &TM,
setOperationAction({ISD::CTTZ, ISD::CTLZ, ISD::CTPOP}, XLenVT, Expand);
}

#ifdef OPENASIP_MIN
setOperationAction(ISD::SMIN, XLenVT, Legal);
#endif
#ifdef OPENASIP_MAX
setOperationAction(ISD::SMAX, XLenVT, Legal);
#endif
#ifdef OPENASIP_MINU
setOperationAction(ISD::UMIN, XLenVT, Legal);
#endif
#ifdef OPENASIP_MAXU
setOperationAction(ISD::UMAX, XLenVT, Legal);
#endif


if (Subtarget.hasVendorXTHeadBb()) {
setOperationAction(ISD::CTLZ, XLenVT, Legal);

Expand All @@ -335,8 +356,10 @@ RISCVTargetLowering::RISCVTargetLowering(const TargetMachine &TM,
if (Subtarget.is64Bit())
setOperationAction(ISD::ABS, MVT::i32, Custom);

#ifndef OPENASIP_SELECT
if (!Subtarget.hasVendorXTHeadCondMov())
setOperationAction(ISD::SELECT, XLenVT, Custom);
#endif

static const unsigned FPLegalNodeTypes[] = {
ISD::FMINNUM, ISD::FMAXNUM, ISD::LRINT,
Expand Down Expand Up @@ -1013,8 +1036,22 @@ RISCVTargetLowering::RISCVTargetLowering(const TargetMachine &TM,
ISD::UREM, ISD::SHL, ISD::SRA, ISD::SRL},
VT, Custom);

setOperationAction(
{ISD::SMIN, ISD::SMAX, ISD::UMIN, ISD::UMAX, ISD::ABS}, VT, Custom);
#ifndef OPENASIP_MIN
setOperationAction(ISD::SMIN, VT, Custom);
#endif
#ifndef OPENASIP_MAX
setOperationAction(ISD::SMAX, VT, Custom);
#endif
#ifndef OPENASIP_MINU
setOperationAction(ISD::UMIN, VT, Custom);
#endif
#ifndef OPENASIP_MAXU
setOperationAction(ISD::UMAX, VT, Custom);
#endif
#ifndef OPENASIP_ABS
setOperationAction(ISD::ABS, VT, Custom);
#endif


// vXi64 MULHS/MULHU requires the V extension instead of Zve64*.
if (VT.getVectorElementType() != MVT::i64 || Subtarget.hasStdExtV())
Expand Down
6 changes: 6 additions & 0 deletions llvm/lib/Target/RISCV/RISCVInstrInfo.td
Original file line number Diff line number Diff line change
Expand Up @@ -1970,3 +1970,9 @@ include "RISCVInstrInfoXVentana.td"
include "RISCVInstrInfoXTHead.td"
include "RISCVInstrInfoXSf.td"
include "RISCVInstrInfoXCV.td"

//===----------------------------------------------------------------------===//
// Generated OpenASIP extensions
//===----------------------------------------------------------------------===//

include "RISCVInstrInfoOpenASIP.td"
53 changes: 52 additions & 1 deletion llvm/tools/llc/llc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@
#include "llvm/Transforms/Utils/Cloning.h"
#include <memory>
#include <optional>

#include "llvm/Support/DynamicLibrary.h"


#include <iostream>

typedef int (*FuncPtr)();


using namespace llvm;

static codegen::RegisterCodeGenFlags CGF;
Expand Down Expand Up @@ -334,6 +343,40 @@ struct LLCDiagnosticHandler : public DiagnosticHandler {
}
};

void loadDynamicTargetPlugins() {
// Load any given target plugins
for (unsigned i = 0; i < LoadOpt.getNumPlugins(); i++) {
const char* libPath = LoadOpt.getPlugin(i).c_str();
// Load the shared library
if (!sys::DynamicLibrary::LoadLibraryPermanently(libPath)) {
// Define initialization function names
const std::vector<std::string> initFunctionNames = {
"LLVMInitializeRISCVTarget",
"LLVMInitializeRISCVTargetInfo",
"LLVMInitializeRISCVTargetMC",
"LLVMInitializeRISCVTargetMCA",
"LLVMInitializeRISCVAsmParser",
"LLVMInitializeRISCVAsmPrinter"
};

// Get function pointers and call initialization functions
for (const auto& functionName : initFunctionNames) {
auto func = reinterpret_cast<void (*)()>(
sys::DynamicLibrary::SearchForAddressOfSymbol(
functionName.c_str()));
if (!func) {
std::cerr << "Error getting function pointer for "
<< functionName << std::endl;
} else {
func(); // Call initialization function
}
}
} else {
std::cerr << "Error in loading plugin" << std::endl;
}
}
}

// main - Entry point for the llc compiler.
//
int main(int argc, char **argv) {
Expand All @@ -348,6 +391,15 @@ int main(int argc, char **argv) {
InitializeAllAsmPrinters();
InitializeAllAsmParsers();

/*
* TODO: It is necessary to parse here to register --load
If we parse the cmdline here, --version does not work correctly.
*
*/
cl::ParseCommandLineOptions(argc, argv, "llvm system compiler\n");
loadDynamicTargetPlugins();


// Initialize codegen and IR passes used by llc so that the -print-after,
// -print-before, and -stop-after options work.
PassRegistry *Registry = PassRegistry::getPassRegistry();
Expand Down Expand Up @@ -375,7 +427,6 @@ int main(int argc, char **argv) {
// Register the target printer for --version.
cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);

cl::ParseCommandLineOptions(argc, argv, "llvm system compiler\n");

if (TimeTrace)
timeTraceProfilerInitialize(TimeTraceGranularity, argv[0]);
Expand Down
29 changes: 29 additions & 0 deletions llvm/tools/llvm-mc/llvm-mc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
#include "llvm/Support/ToolOutputFile.h"
#include "llvm/Support/WithColor.h"
#include "llvm/TargetParser/Host.h"
#include "llvm/Support/PluginLoader.h"
#include "llvm/Support/DynamicLibrary.h"


using namespace llvm;

Expand Down Expand Up @@ -346,6 +349,29 @@ static int AssembleInput(const char *ProgName, const Target *TheTarget,
return Res;
}


void loadDynamicTargetPlugins() {
const std::vector<std::string> initFunctionNames = {
"LLVMInitializeRISCVTargetInfo",
"LLVMInitializeRISCVTargetMC",
"LLVMInitializeRISCVAsmParser",
"LLVMInitializeRISCVDisassembler"
};

// Get function pointers and call initialization functions
for (const auto& functionName : initFunctionNames) {
auto func = reinterpret_cast<void (*)()>(
sys::DynamicLibrary::SearchForAddressOfSymbol(
functionName.c_str()));
if (!func) {
llvm::outs() << "Error getting function pointer for "
<< functionName << "\n";
} else {
func(); // Call initialization function
}
}
}

int main(int argc, char **argv) {
InitLLVM X(argc, argv);

Expand All @@ -360,6 +386,9 @@ int main(int argc, char **argv) {

cl::HideUnrelatedOptions({&MCCategory, &getColorCategory()});
cl::ParseCommandLineOptions(argc, argv, "llvm machine code playground\n");

loadDynamicTargetPlugins();

const MCTargetOptions MCOptions = mc::InitMCTargetOptionsFromFlags();
setDwarfDebugFlags(argc, argv);

Expand Down

0 comments on commit 64c4c73

Please sign in to comment.