[LLVMdev] How to add a trivial LLVM intrinsic

759 views
Skip to first unread message

Matt Renzelmann

unread,
Jun 23, 2009, 5:13:39 PM6/23/09
to llv...@cs.uiuc.edu
Hello,

As an experiment, I'm simply trying to add a trivial intrinsic to LLVM, but
I'm having trouble, which suggests that I'm overlooking the obvious ;)
Platform is GCC-4.2.4/LLVM from SVN/x86.

First, I've read the page here about 10 times, so I'll speculate that my
reading comprehension is lacking:
http://www.llvm.org/docs/ExtendingLLVM.html

Code I've added to the bottom of the llvm/include/llvm/Intrinsics.td file:
-----------------
def int_test_intrinsic :
Intrinsic<[llvm_i32_ty],
[llvm_i32_ty],
[],
"llvm.test.intrinsic">;
-----------------

Code I've added to visitBuiltinCall in
llvm/lib/Target/CBackend/CBackend.cpp:
-----------------
case Intrinsic::test_intrinsic:
return true;
-----------------

I've not added any code to llvm/lib/Target/X86 because when I did, I
received a tablegen error. I think I'm not understanding the difference
between an intrinsic that maps directly to a single machine instruction, and
a more complex intrinsic that invokes a C function.

In particular, I tried adding:
def int_test_and_clear_bit : X86Inst;

to the end of X86.td, but that resulted in a tablegen compilation error.

Code I've added to the end of LowerIntrinsicCall in
/lib/CodeGen/IntrinsicLowering.cpp:
-----------------
case Intrinsic::test_intrinsic:
break;
-----------------

Note that the intent is to make this intrinsic a NOOP, just as an
experiment.

But alas, the following trivial test program does not work:

-----------------
#include <stdio.h>

int llvm_test_intrinsic(int) asm ("llvm.test.intrinsic");

int bar (int blah) {
return llvm_test_intrinsic (blah);
}

int main() {
int x = 3;
bar (x);
return 0;
}
-----------------
After invoking:
llvm-gcc -g test.c -o test.native
-----------------
The compiler returns:
cc1: Function.cpp:323: unsigned int llvm::Function::getIntrinsicID(bool)
const: Assertion `noAssert && "Invalid LLVM intrinsic name"' failed.
test.c:24: internal compiler error: Aborted
Please submit a full bug report,
with preprocessed source if appropriate.
See <URL:http://developer.apple.com/bugreporter> for instructions.
-----------------

So, it's clear I'm missing something, or lots of things. Any help would be
appreciated :) I'm sure this is easy.

Thanks and regards,
Matt

_______________________________________________
LLVM Developers mailing list
LLV...@cs.uiuc.edu http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev

Duncan Sands

unread,
Jun 24, 2009, 3:27:44 AM6/24/09
to LLVM Developers Mailing List
Hi,

> After invoking:
> llvm-gcc -g test.c -o test.native
> -----------------
> The compiler returns:
> cc1: Function.cpp:323: unsigned int llvm::Function::getIntrinsicID(bool)
> const: Assertion `noAssert && "Invalid LLVM intrinsic name"' failed.
> test.c:24: internal compiler error: Aborted

did you rebuild llvm-gcc after building llvm with your new intrinsic
defined?

Ciao,

Duncan.

Matt Renzelmann

unread,
Jun 24, 2009, 2:06:08 PM6/24/09
to LLVM Developers Mailing List
Thank you for your reply--I've now done that. Unfortunately, there's a new
error now:

------------
Cannot yet select: intrinsic %llvm.test_intrinsic
test.c:25: internal compiler error: Aborted


Please submit a full bug report,
with preprocessed source if appropriate.
See <URL:http://developer.apple.com/bugreporter> for instructions.
------------

I've switched to using LLVM-2.5 instead of the latest SVN version, but I
made all the same changes described in the previous email. After making the
changes, I built LLVM, and then built the GCC front end.

I traced the "Cannot yet select" print statement to DAGISelEmitter.cpp, but
am unsure how to proceed.

Thanks again for your help,
Matt

Duncan Sands

unread,
Jun 24, 2009, 3:37:49 PM6/24/09
to LLVM Developers Mailing List
Hi Matt,

> Thank you for your reply--I've now done that. Unfortunately, there's a new
> error now:
>
> ------------
> Cannot yet select: intrinsic %llvm.test_intrinsic
> test.c:25: internal compiler error: Aborted

that's because you didn't tell the code generators what
they should do with your intrinsic.

Matt Renzelmann

unread,
Jun 24, 2009, 3:46:42 PM6/24/09
to LLVM Developers Mailing List
Thank you again for your help. Assuming my experimental "noop" intrinsic is
intended to be target independent, what file should I modify to provide the
code generator with the necessary information? I've already modified:

./lib/CodeGen/IntrinsicLowering.cpp
./lib/Target/CBackend/CBackend.cpp
./include/llvm/Intrinsics.td

Presumably it's not one of those? Or, if it is, I imagine I've simply
screwed something up?

Thanks again,
Matt

-----Original Message-----
From: llvmdev...@cs.uiuc.edu [mailto:llvmdev...@cs.uiuc.edu] On
Behalf Of Duncan Sands
Sent: Wednesday, June 24, 2009 2:38 PM
To: LLVM Developers Mailing List
Subject: Re: [LLVMdev] How to add a trivial LLVM intrinsic

Reply all
Reply to author
Forward
0 new messages