<div><div dir="auto">I see another issue that you’ll hit later. Your intrinsic doesn’t have IntrNoMem and has no results. So it will be in SelectionDAG as INTRINSIC_VOID rather that INTRINSIC_WO_CHAIN. Your getNode call will need to copy the chain input and output. And riscv_foo needs the SDNPHasChain property.</div></div><div><div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Jan 26, 2022 at 5:58 AM Jessica Clarke via cfe-dev <<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;padding-left:1ex;border-left-color:rgb(204,204,204)">On 26 Jan 2022, at 12:19, David Mallasén Quintana via cfe-dev <<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a>> wrote:<br>
> <br>
> Hello,<br>
> <br>
> Following up on this, and trying to do something similar to<br>
> <a href="https://github.com/llvm/llvm-project/commit/16877c5" rel="noreferrer" target="_blank">https://github.com/llvm/llvm-project/commit/16877c5</a>, I am trying to<br>
> add a simple Clang builtin: void __builtin_riscv_foo() that should<br>
> output an intrinsic @llvm.riscv.foo() which will outpu, with a pattern<br>
> in the backend, a simple custom instruction FOO without parameters.<br>
> However, when compiling I receive the following error:<br>
> <br>
> clang: llvm/lib/IR/Function.cpp:844: std::string<br>
> llvm::Intrinsic::getName(llvm::Intrinsic::ID,<br>
> llvm::ArrayRef<llvm::Type*>, llvm::Module*, llvm::FunctionType*):<br>
> Assertion `(Tys.empty() || Intrinsic::isOverloaded(Id)) && "This<br>
> version of getName is for overloaded intrinsics only"' failed.<br>
<br>
As you can see, it’s telling you that you’re treating your intrinsic as<br>
an overloaded intrinsic, but your intrinsic isn’t polymorphic (it<br>
doesn’t even have any arguments or return types, let alone overloaded).<br>
You can see towards the bottom of EmitRISCVBuiltinExpr the<br>
IntrinsicTypes = {ResultType}, which is the list of types used to<br>
instantiate the overloads. Since you have no overloads, that list<br>
should be empty for your intrinsic, so you should not be reusing that<br>
bit of code and instead have a separate top-level case statement.<br>
<br>
Jess<br>
<br>
> PLEASE submit a bug report to <a href="https://bugs.llvm.org/" rel="noreferrer" target="_blank">https://bugs.llvm.org/</a> and include the<br>
> crash backtrace, preprocessed source, and associated run script.<br>
> Stack dump:<br>
> 0.    Program arguments: clang --target=riscv64 -march=rv64gcxbar<br>
> intrinsics_test.c -c -o intrinsics_test.o<br>
> 1.    <eof> parser at end of file<br>
> 2.    intrinsics_test.c:21:6: LLVM IR generation of declaration 'test'<br>
> 3.    intrinsics_test.c:21:6: Generating code for declaration 'test'<br>
> 4.    intrinsics_test.c:22:33: LLVM IR generation of compound statement ('{}')<br>
> 5.    intrinsics_test.c:23:37: LLVM IR generation of compound statement ('{}')<br>
> <br>
> To get to this point I have added the following code:<br>
> In clang/include/clang/Basic/BuiltinsRISCV.def:<br>
>    TARGET_BUILTIN(__builtin_riscv_foo, "v", "n", "xbar")<br>
> <br>
> In clang/lib/CodeGen/CGBuiltin.cpp : EmitRISCVBuiltinExpr:<br>
>    case RISCV::BI__builtin_riscv_foo: {<br>
> and later in that same function<br>
>    case RISCV::BI__builtin_riscv_foo:<br>
>        ID = Intrinsic::riscv_foo;<br>
>        break;<br>
> <br>
> In llvm/include/llvm/IR/IntrinsicsRISCV.td:<br>
>    let TargetPrefix = "riscv" in {<br>
>        def int_riscv_foo : Intrinsic<[], [], []>;<br>
>    }<br>
> <br>
> In llvm/lib/Target/RISCV/RISCVISelLowering.h:<br>
>    FOO, // Before ISD::FIRST_TARGET_MEMORY_OPCODE<br>
> <br>
> In llvm/lib/Target/RISCV/RISCVISelLowering.cpp : LowerINTRINSIC_WO_CHAIN:<br>
>    case Intrinsic::riscv_foo: {<br>
>        return DAG.getNode(RISCVISD::FOO, DL, XLenVT);<br>
>    }<br>
> and in RISCVTargetLowering::getTargetNodeName:<br>
>   NODE_NAME_CASE(FOO)<br>
> <br>
> In llvm/lib/Target/RISCV/RISCVInstrInfoBar.td<br>
>    def riscv_foo : SDNode<"RISCVISD::FOO", SDTypeProfile<0, 0, []>>;<br>
>    def : Pat<(riscv_foo), (FOO)>;<br>
> <br>
> I think the test for the Clang builtin should be:<br>
> // RUN: %clang_cc1 -triple riscv64 -target-feature +xbar -emit-llvm %s -o - \<br>
> // RUN:     | FileCheck %s  -check-prefix=RV64Xbar<br>
> <br>
> // RV64Xbar-LABEL: @foo(<br>
> // RV64Xbar-NEXT:  entry:<br>
> // RV64Xbar-NEXT:    call void @llvm.riscv.foo()<br>
> // RV64Xbar-NEXT:    ret void<br>
> //<br>
> void foo() {<br>
>    __builtin_riscv_foo();<br>
> }<br>
> <br>
> Is there something missing that I can't find? I have tried small<br>
> variations of this code with no success.<br>
> <br>
> Best regards,<br>
> David<br>
> <br>
> El mar, 18 ene 2022 a las 12:34, David Mallasén Quintana<br>
> (<<a href="mailto:dmallase@ucm.es" target="_blank">dmallase@ucm.es</a>>) escribió:<br>
>> <br>
>> Hello,<br>
>> <br>
>> I have added new instructions to the RISC-V LLVM backend for some<br>
>> specific hardware I developed and I want to access a sequence of them<br>
>> directly from C code. As of now I do this using inline assembly but it<br>
>> can be a bit cumbersome. If I'm not mistaken the way to go would be to<br>
>> access this functionality from C with CLANG builtins and lowering this<br>
>> to machine code with LLVM intrinsics that generate my target RISC-V<br>
>> instructions.<br>
>> <br>
>> I have seen the documentation on how to add LLVM intrinsics. However,<br>
>> I can't find something similar with CLANG builtins, so any pointers to<br>
>> some documentation would be greatly appreciated. Also, if there is a<br>
>> better way of achieving this I would be grateful for some information<br>
>> on how it could be done.<br>
>> <br>
>> Best regards,<br>
>> David<br>
> _______________________________________________<br>
> cfe-dev mailing list<br>
> <a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a><br>
> <a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev</a><br>
<br>
_______________________________________________<br>
cfe-dev mailing list<br>
<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a><br>
<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev</a><br>
</blockquote></div></div>
</div>-- <br><div dir="ltr" class="gmail_signature" data-smartmail="gmail_signature">~Craig</div>