running intrinsics from C code

I’ve created an intrinsic from my target, but I can’t figure out how I can run it from a C code. Most of the targets have a GCCBuiltin and it looks like it is the way to execute an intrinsic from C code. However in my case there is no actual GCC built in.

Any help on this is really appreciated.

GCCBuiltin just gives it a name for clang to lookup. Generally they match up with builtins that gcc also implements, but that’s not a requirement.

If you add a builtin with the same name to the builtin file in clang’s include/clang/Basic/Builtins*.def then they will find each other.

You can also just add a builtin to clang’s builtin file and catch it in clang’s lib/CodeGen/CGBuiltins.cpp and implement whatever IR you want including calling an intrinsic.

Hope this helps.

Thanks for the tip. For some reason it didn’t work. I’m sure I’m missing something very simple. Do you mind helping out? Here is what I have so far:

I’m getting a following error when I run a simple test:
cannot compile this builtin function yet

Here is the test code:

typedef int v4si attribute ((vector_size (16)));

int foo (int a, int b, int c, int d) {
v4si vec = {a, b, c, d};
return esencia_hadd(vec);
}

My intrinsic is defined in /include/llvm/IR/IntrinsicsEsencia.td

let TargetPrefix = “esencia” in
def int_esencia_hadd : Intrinsic<[llvm_i32_ty],
[llvm_v4i32_ty],
[IntrNoMem]>;

My builtin is defined in /include/clang/Basic/BuiltinsEsencia.def

BUILTIN(esencia_hadd, “iV4Si”, “nc”)

I’ve also added code to <full path to clang/lib/Basic/Targerts.cpp

const Builtin::Info EsenciaTargetInfo::BuiltinInfo[] = {
#define BUILTIN(ID, TYPE, ATTRS) { #ID, TYPE, ATTRS, 0, ALL_LANGUAGES },
#define LIBBUILTIN(ID, TYPE, ATTRS, HEADER) { #ID, TYPE, ATTRS, HEADER,
ALL_LANGUAGES },
#include “clang/Basic/BuiltinsEsencia.def”
};

as well as to /include/clang/Basic/TargetBuiltins.h

/// \brief Esencia builtins
namespace Esencia {
enum {
LastTIBuiltin = clang::Builtin::FirstTSBuiltin-1,
#define BUILTIN(ID, TYPE, ATTRS) BI##ID,
#include “clang/Basic/BuiltinsEsencia.def”
LastTSBuiltin
};
}

What am I missing?