Skip to content

Commit

Permalink
[snippy] update model interface
Browse files Browse the repository at this point in the history
  • Loading branch information
arrv-sc committed Dec 12, 2024
1 parent bca94b5 commit 9af48db
Show file tree
Hide file tree
Showing 2 changed files with 268 additions and 69 deletions.
161 changes: 104 additions & 57 deletions llvm/tools/llvm-snippy/Model/RISCVModel/RVM.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,72 +16,121 @@ extern "C" {

#define RVMAPI_ENTRY_POINT_SYMBOL RVMVTable
#define RVMAPI_VERSION_SYMBOL RVMInterfaceVersion
#define RVMAPI_CURRENT_INTERFACE_VERSION 16u
#define RVMAPI_CURRENT_INTERFACE_VERSION 20u

typedef uint64_t RVMRegT;

typedef struct RVMState RVMState;
typedef struct RVMCallbackHandler RVMCallbackHandler;

#define RVM_API_INTERNAL_STDEXT_FLAG(EXT_LITERAL) \
(1ull << ((EXT_LITERAL) - 'A'))
#ifdef RVM_FOR_EACH_MISA_EXT
#error RVM_FOR_EACH_MISA_EXT should not be defined at this point
#else
// Order is important here. See "36.11 Subset Naming Convention"
#define RVM_FOR_EACH_MISA_EXT(MACRO) \
MACRO(RVM_MISA_M, M) \
MACRO(RVM_MISA_A, A) \
MACRO(RVM_MISA_F, F) \
MACRO(RVM_MISA_D, D) \
MACRO(RVM_MISA_Q, Q) \
MACRO(RVM_MISA_L, L) \
MACRO(RVM_MISA_C, C) \
MACRO(RVM_MISA_B, B) \
MACRO(RVM_MISA_T, T) \
MACRO(RVM_MISA_P, P) \
MACRO(RVM_MISA_V, V) \
MACRO(RVM_MISA_H, H) \
MACRO(RVM_MISA_E, E) \
MACRO(RVM_MISA_G, G) \
MACRO(RVM_MISA_I, I) \
MACRO(RVM_MISA_J, J) \
MACRO(RVM_MISA_K, K) \
MACRO(RVM_MISA_N, N) \
MACRO(RVM_MISA_O, O) \
MACRO(RVM_MISA_R, R) \
MACRO(RVM_MISA_S, S) \
MACRO(RVM_MISA_U, U) \
MACRO(RVM_MISA_W, W) \
MACRO(RVM_MISA_X, X) \
MACRO(RVM_MISA_Y, Y) \
MACRO(RVM_MISA_Z, Z)
#endif
#ifdef RVM_DEFINE_ENUM_CASE
#error RVM_DEFINE_ENUM_CASE should not be defined at this point
#else
#define RVM_DEFINE_ENUM_CASE(Name, name) Name,
#endif
typedef enum {
RVM_MISA_A = RVM_API_INTERNAL_STDEXT_FLAG('A'),
RVM_MISA_B = RVM_API_INTERNAL_STDEXT_FLAG('B'),
RVM_MISA_C = RVM_API_INTERNAL_STDEXT_FLAG('C'),
RVM_MISA_D = RVM_API_INTERNAL_STDEXT_FLAG('D'),
RVM_MISA_E = RVM_API_INTERNAL_STDEXT_FLAG('E'),
RVM_MISA_F = RVM_API_INTERNAL_STDEXT_FLAG('F'),
RVM_MISA_G = RVM_API_INTERNAL_STDEXT_FLAG('G'),
RVM_MISA_H = RVM_API_INTERNAL_STDEXT_FLAG('H'),
RVM_MISA_I = RVM_API_INTERNAL_STDEXT_FLAG('I'),
RVM_MISA_J = RVM_API_INTERNAL_STDEXT_FLAG('J'),
RVM_MISA_K = RVM_API_INTERNAL_STDEXT_FLAG('K'),
RVM_MISA_L = RVM_API_INTERNAL_STDEXT_FLAG('L'),
RVM_MISA_M = RVM_API_INTERNAL_STDEXT_FLAG('M'),
RVM_MISA_N = RVM_API_INTERNAL_STDEXT_FLAG('N'),
RVM_MISA_O = RVM_API_INTERNAL_STDEXT_FLAG('O'),
RVM_MISA_P = RVM_API_INTERNAL_STDEXT_FLAG('P'),
RVM_MISA_Q = RVM_API_INTERNAL_STDEXT_FLAG('Q'),
RVM_MISA_R = RVM_API_INTERNAL_STDEXT_FLAG('R'),
RVM_MISA_S = RVM_API_INTERNAL_STDEXT_FLAG('S'),
RVM_MISA_T = RVM_API_INTERNAL_STDEXT_FLAG('T'),
RVM_MISA_U = RVM_API_INTERNAL_STDEXT_FLAG('U'),
RVM_MISA_V = RVM_API_INTERNAL_STDEXT_FLAG('V'),
RVM_MISA_W = RVM_API_INTERNAL_STDEXT_FLAG('W'),
RVM_MISA_X = RVM_API_INTERNAL_STDEXT_FLAG('X'),
RVM_MISA_Y = RVM_API_INTERNAL_STDEXT_FLAG('Y'),
RVM_MISA_Z = RVM_API_INTERNAL_STDEXT_FLAG('Z'),
RVM_FOR_EACH_MISA_EXT(RVM_DEFINE_ENUM_CASE) RVM_MISA_NUMBER
} RVMMisaExt;
#undef RVM_API_INTERNAL_STDEXT_FLAG

#ifdef RVM_FOR_EACH_ZEXT
#error RVM_FOR_EACH_ZEXT should not be defined at this point
#else
#define RVM_FOR_EACH_ZEXT(MACRO) \
MACRO(RVM_ZEXT_ICSR, icsr) \
MACRO(RVM_ZEXT_IFENCEI, ifencei) \
MACRO(RVM_ZEXT_FH, fh) \
MACRO(RVM_ZEXT_FHMIN, fhmin) \
MACRO(RVM_ZEXT_BA, ba) \
MACRO(RVM_ZEXT_BB, bb) \
MACRO(RVM_ZEXT_BC, bc) \
MACRO(RVM_ZEXT_BS, bs) \
MACRO(RVM_ZEXT_BITMANIP, bitmanip) \
MACRO(RVM_ZEXT_BKB, bkb) \
MACRO(RVM_ZEXT_BKC, bkc) \
MACRO(RVM_ZEXT_BKX, bkx) \
MACRO(RVM_ZEXT_VKB, vkb) \
MACRO(RVM_ZEXT_VBB, vbb) \
MACRO(RVM_ZEXT_VBC, vbc) \
MACRO(RVM_ZEXT_VKNED, vkned) \
MACRO(RVM_ZEXT_VKNHA, vknha) \
MACRO(RVM_ZEXT_VKNHB, vknhb) \
MACRO(RVM_ZEXT_VKG, vkg) \
MACRO(RVM_ZEXT_VKSED, vksed) \
MACRO(RVM_ZEXT_VKSH, vksh) \
MACRO(RVM_ZEXT_VKT, vkt) \
MACRO(RVM_ZEXT_VKN, vkn) \
MACRO(RVM_ZEXT_VKNC, vknc) \
MACRO(RVM_ZEXT_VKNG, vkng) \
MACRO(RVM_ZEXT_VKS, vks) \
MACRO(RVM_ZEXT_VKSC, vksc) \
MACRO(RVM_ZEXT_VKSG, vksg) \
MACRO(RVM_ZEXT_KND, knd) \
MACRO(RVM_ZEXT_KNE, kne) \
MACRO(RVM_ZEXT_KNH, knh) \
MACRO(RVM_ZEXT_KSED, ksed) \
MACRO(RVM_ZEXT_KSH, ksh) \
MACRO(RVM_ZEXT_KR, kr) \
MACRO(RVM_ZEXT_KN, kn) \
MACRO(RVM_ZEXT_KS, ks) \
MACRO(RVM_ZEXT_K, k) \
MACRO(RVM_ZEXT_KT, kt)
#endif

typedef enum {
RVM_ZEXT_FH = 1ull << 0,
RVM_ZEXT_BA = 1ull << 1,
RVM_ZEXT_BB = 1ull << 2,
RVM_ZEXT_BC = 1ull << 3,
RVM_ZEXT_BS = 1ull << 4,
RVM_ZEXT_BITMANIP = RVM_ZEXT_BA | RVM_ZEXT_BB | RVM_ZEXT_BC | RVM_ZEXT_BS,
RVM_ZEXT_BKB = 1ull << 5,
RVM_ZEXT_BKC = 1ull << 6,
RVM_ZEXT_BKX = 1ull << 7,
RVM_ZEXT_KND = 1ull << 8,
RVM_ZEXT_KNE = 1ull << 9,
RVM_ZEXT_KNH = 1ull << 10,
RVM_ZEXT_KSED = 1ull << 11,
RVM_ZEXT_KSH = 1ull << 12,
RVM_ZEXT_KR = 1ull << 13,
RVM_ZEXT_KN = 1ull << 14,
RVM_ZEXT_KS = 1ull << 15,
RVM_ZEXT_K = 1ull << 16,
RVM_ZEXT_KT = 1ull << 17,
RVM_ZEXT_FHMIN = 1ull << 21,
// Vector crypto
RVM_ZEXT_VKB = 1ull << 22,
RVM_ZEXT_VBB = 1ull << 23,
RVM_ZEXT_VBC = 1ull << 24,
RVM_FOR_EACH_ZEXT(RVM_DEFINE_ENUM_CASE) RVM_ZEXT_NUMBER
} RVMZExt;

#ifdef RVM_FOR_EACH_XEXT
#error RVM_FOR_EACH_XEXT should not be defined at this point
#else
#define RVM_FOR_EACH_XEXT(MACRO)
#endif

typedef enum {
RVM_FOR_EACH_XEXT(RVM_DEFINE_ENUM_CASE) RVM_XEXT_NUMBER
} RVMXExt;
#undef RVM_DEFINE_ENUM_CASE

typedef struct RVMExtDescriptior {
size_t ZExtSize; // Set this to sizeof(ZExt)
size_t XExtSize; // Set this to sizeof(XExt)
char ZExt[RVM_ZEXT_NUMBER];
char XExt[RVM_XEXT_NUMBER];
char MisaExt[RVM_MISA_NUMBER];
} RVMExtDescriptor;

typedef enum {
RVM_MSTATUS_VS_FIELD_OFFSET = 9,
RVM_MSTATUS_FS_FIELD_OFFSET = 13,
Expand Down Expand Up @@ -287,9 +336,6 @@ struct RVMConfig {
uint64_t RamStart;
uint64_t RamSize;
int RV64;
uint64_t MisaExt;
uint64_t ZExt;
uint64_t XExt;
unsigned VLEN;
int EnableMisalignedAccess;
RVMStopMode Mode;
Expand All @@ -304,6 +350,7 @@ struct RVMConfig {
PCUpdateCallbackTy PCUpdateCallback;
int ChangeMaskAgnosticElems;
int ChangeTailAgnosticElems;
RVMExtDescriptor Extensions;
};

typedef RVMState *(*rvm_modelCreate_t)(const RVMConfig *);
Expand Down
176 changes: 164 additions & 12 deletions llvm/tools/llvm-snippy/Model/RISCVModel/RVM.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,167 @@
#pragma once
#include "VTable.h"

#include <algorithm>
#include <cassert>
#include <cctype>
#include <cstdint>
#include <memory>
#include <sstream>
#include <string>

namespace rvm {
namespace detail {
// replace composite extensions with their components
inline RVMExtDescriptor normalize_extensions(const RVMExtDescriptor &Ext) {
RVMExtDescriptor Norm;
std::copy(std::begin(Ext.ZExt), std::end(Ext.ZExt), Norm.ZExt);
std::copy(std::begin(Ext.XExt), std::end(Ext.XExt), Norm.XExt);
std::copy(std::begin(Ext.MisaExt), std::end(Ext.MisaExt), Norm.MisaExt);
auto &ZExt = Norm.ZExt;
// MISA extensions
auto &MisaExt = Norm.MisaExt;
if (MisaExt[RVM_MISA_G]) {
MisaExt[RVM_MISA_M] = true;
MisaExt[RVM_MISA_A] = true;
MisaExt[RVM_MISA_F] = true;
MisaExt[RVM_MISA_D] = true;
ZExt[RVM_ZEXT_IFENCEI] = true;
ZExt[RVM_ZEXT_ICSR] = true;
MisaExt[RVM_MISA_G] = false;
}
// standard extensions
if (ZExt[RVM_ZEXT_KN]) {
ZExt[RVM_ZEXT_BKB] = true;
ZExt[RVM_ZEXT_BKC] = true;
ZExt[RVM_ZEXT_BKX] = true;
ZExt[RVM_ZEXT_KNE] = true;
ZExt[RVM_ZEXT_KND] = true;
ZExt[RVM_ZEXT_KNH] = true;
ZExt[RVM_ZEXT_KN] = false;
}
if (ZExt[RVM_ZEXT_KS]) {
ZExt[RVM_ZEXT_BKB] = true;
ZExt[RVM_ZEXT_BKC] = true;
ZExt[RVM_ZEXT_BKX] = true;
ZExt[RVM_ZEXT_KSED] = true;
ZExt[RVM_ZEXT_KSH] = true;
ZExt[RVM_ZEXT_KS] = false;
}
if (ZExt[RVM_ZEXT_K]) {
ZExt[RVM_ZEXT_KN] = true;
ZExt[RVM_ZEXT_KR] = true;
ZExt[RVM_ZEXT_KT] = true;
ZExt[RVM_ZEXT_K] = false;
}
if (ZExt[RVM_ZEXT_BITMANIP]) {
ZExt[RVM_ZEXT_BA] = true;
ZExt[RVM_ZEXT_BB] = true;
ZExt[RVM_ZEXT_BC] = true;
ZExt[RVM_ZEXT_BS] = true;
ZExt[RVM_ZEXT_BITMANIP] = false;
}
if (ZExt[RVM_ZEXT_VKN]) {
ZExt[RVM_ZEXT_VKNED] = true;
ZExt[RVM_ZEXT_VKNHB] = true;
ZExt[RVM_ZEXT_VKB] = true;
ZExt[RVM_ZEXT_VKT] = true;
ZExt[RVM_ZEXT_VKN] = false;
}
if (ZExt[RVM_ZEXT_VKNC]) {
ZExt[RVM_ZEXT_VKN] = true;
ZExt[RVM_ZEXT_VBC] = true;
ZExt[RVM_ZEXT_VKNC] = false;
}
if (ZExt[RVM_ZEXT_VKNG]) {
ZExt[RVM_ZEXT_VKN] = true;
ZExt[RVM_ZEXT_VKG] = true;
ZExt[RVM_ZEXT_VKNG] = false;
}
if (ZExt[RVM_ZEXT_VKS]) {
ZExt[RVM_ZEXT_VKSED] = true;
ZExt[RVM_ZEXT_VKSH] = true;
ZExt[RVM_ZEXT_VKB] = true;
ZExt[RVM_ZEXT_VKT] = true;
ZExt[RVM_ZEXT_VKS] = false;
}
if (ZExt[RVM_ZEXT_VKSC]) {
ZExt[RVM_ZEXT_VKS] = true;
ZExt[RVM_ZEXT_VBC] = true;
ZExt[RVM_ZEXT_VKSC] = false;
}
if (ZExt[RVM_ZEXT_VKSG]) {
ZExt[RVM_ZEXT_VKS] = true;
ZExt[RVM_ZEXT_VKG] = true;
ZExt[RVM_ZEXT_VKSG] = false;
}
return Norm;
}
#ifdef ADD_MISA_CASE
#error ADD_MISA_CASE should not be defined at this point
#else
#define ADD_MISA_CASE(Name, name) \
if (MisaExt[Name]) \
OS << #name;
#endif
inline void add_misa(std::ostream &OS, const RVMExtDescriptor &Ext) {
auto &MisaExt = Ext.MisaExt;
RVM_FOR_EACH_MISA_EXT(ADD_MISA_CASE)
}
#undef ADD_MISA_CASE

#ifdef ADD_ZEXT_CASE
#error ADD_ZEXT_CASE should not be defined at this point
#else
#define ADD_ZEXT_CASE(NAME, name) \
if (ZExt[NAME]) \
OS << "_Z" << #name;
#endif
inline void add_standard_extensions(std::ostream &OS,
const RVMExtDescriptor &Ext) {
auto &ZExt = Ext.ZExt;
RVM_FOR_EACH_ZEXT(ADD_ZEXT_CASE);
}
#undef ADD_ZEXT_CASE

#ifdef ADD_XEXT_CASE
#error ADD_XEXT_CASE should not be defined at this point
#else
#define ADD_XEXT_CASE(NAME, name) \
if (XExt[NAME]) \
OS << "_X" << #name;
#endif
inline void add_custom_extensions(std::ostream &OS,
const RVMExtDescriptor &Ext) {
auto &XExt = Ext.XExt;
RVM_FOR_EACH_XEXT(ADD_XEXT_CASE);
}
#undef ADD_XEXT_CASE

} // namespace detail

inline std::string create_isa_string(const RVMExtDescriptor &Ext, bool RV64,
bool Lowercase = false) {
assert(sizeof(Ext.ZExt) == Ext.ZExtSize);
assert(sizeof(Ext.XExt) == Ext.XExtSize);
auto Norm = detail::normalize_extensions(Ext);
std::stringstream SS;
SS << (RV64 ? "RV64I" : "RV32I");
detail::add_misa(SS, Norm);
detail::add_standard_extensions(SS, Norm);
detail::add_custom_extensions(SS, Norm);
auto Isa = SS.str();
auto FirstUnderscore = std::find(Isa.begin(), Isa.end(), '_');
if (FirstUnderscore != Isa.end())
Isa.erase(FirstUnderscore);
if (Lowercase) {
std::string LoweredIsa;
std::transform(Isa.begin(), Isa.end(), std::back_inserter(LoweredIsa),
[](auto C) { return std::tolower(C); });
return LoweredIsa;
}

return Isa;
}

class State {
struct StateDeleter {
Expand Down Expand Up @@ -79,11 +234,6 @@ class State {
return *this;
}

Builder &setMisa(uint64_t Misa) {
Config.MisaExt = Misa;
return *this;
}

Builder &registerCallbackHandler(RVMCallbackHandler *Handler) {
Config.CallbackHandler = Handler;
return *this;
Expand Down Expand Up @@ -114,13 +264,15 @@ class State {
return *this;
}

Builder &setZext(uint64_t Zext) {
Config.ZExt = Zext;
return *this;
}

Builder &setXext(uint64_t Xext) {
Config.XExt = Xext;
Builder &setExtensions(const RVMExtDescriptor &Ext) {
std::copy(std::begin(Ext.ZExt), std::end(Ext.ZExt),
Config.Extensions.ZExt);
std::copy(std::begin(Ext.XExt), std::end(Ext.XExt),
Config.Extensions.XExt);
std::copy(std::begin(Ext.MisaExt), std::end(Ext.MisaExt),
Config.Extensions.MisaExt);
Config.Extensions.ZExtSize = Ext.ZExtSize;
Config.Extensions.XExtSize = Ext.XExtSize;
return *this;
}

Expand Down

0 comments on commit 9af48db

Please sign in to comment.