Hi all,
we are currently in the work of supporting custom RISCV instructions through builtin and intrinsic functions in.
A patch for LLVM 17 could look something like that:
Addition in clang/include/clang/Basic/BuiltinsRISCV.def
TARGET_BUILTIN(__builtin_riscv_sbox_sbox, "ii", "nc", "experimental-xsbox")
Addition in llvm/include/llvm/IR/IntrinsicsRISCV.td
let TargetPrefix = "riscv" in {
multiclass SBOXIntrinsic<list<LLVMType> ret_types,
list<LLVMType> param_types,
list<IntrinsicProperty> intr_properties> {
def "int_riscv_sbox_" # NAME
: ClangBuiltin<"__builtin_riscv_sbox_" # NAME>,
Intrinsic<ret_types,param_types,intr_properties>;
}
defm sbox : SBOXIntrinsic<[llvm_i32_ty],
[llvm_i32_ty],
[IntrNoMem]>;
} // TargetPrefix = "riscv"
Addition in llvm/lib/Target/RISCV/RISCVInstrInfo.td
// let Predicates = [HasNonStdExtSBOX] in {
def RV_SBOX_sbox_Inst
: RVInst<(outs GPR:$rd), (ins GPR:$rs1),
"SBOX.sbox", "$rd, $rs1",
[(set GPR:$rd, (int_riscv_sbox_sbox GPR:$rs1))],
InstFormatOther>,
Sched<[]> {
let hasSideEffects = 0;
let mayLoad = 0;
let mayStore = 0;
let Constraints = "";
bits<5> rd;
bits<5> rs1;
let Inst{6-0} = 0b0101011;
let Inst{11-7} = rd;
let Inst{14-12} = 0b000;
let Inst{19-15} = rs1;
let Inst{24-20} = 0b00000;
let Inst{31-25} = 0b0000100;
}
} // Predicates = [HasNonStdExtSBOX]
This works all fine when using resulting LLVM/Clang compiler with the riscv32 target.
Recently, we startet supporting riscv64 with our tooling and are now hitting an llvm_unreachable
in llvm/lib/Target/RISCV/RISCVISelLowering.cpp
:
“Don’t know how to custom type legalize this intrinsic!”
This line moves between LLVM 17 and 19, but the string is very unique. Yes, we tested it in LLVM 19, too. There you have to use TableGen instead of this Clang definition file for the builtin function and some things on how the subtarget registration works has changed.
But our problem here is more that we are interessted in supporting this instruction both in riscv32 and riscv64. And I am not sure, how I have to generate the intrinsic so that it is transparent for C/C++ user which uses the builtin function with an integer as well legal for the riscv64 backend which does not allow operations including a 32 bit values. It would be nice if the could include this in a single compiler
I tried a few, but so far I just moved the error to another assertions.
Has anyone done something like that? Thanks in advance!