Adding custom builtins

Hi all,
I’m hacking clang to parse a c-like quantum programming language and am having trouble parsing builtin functions. It seems to me that FunctionDecl::getBuiltinID() [Decl.cpp] is chiefly responsible for determining whether a function identifier is a builtin or not. However the guts of this function: IdentifierInfo::getBuiltinID() [IdentfierTable.h] but it appears to me that this function will only recognize objective c builtins.

/// getBuiltinID - Return a value indicating whether this is a builtin
/// function. 0 is not-built-in. 1 is builtin-for-some-nonprimary-target.
/// 2+ are specific builtin functions.
unsigned getBuiltinID() const {
if (ObjCOrBuiltinID >= tok::NUM_OBJC_KEYWORDS)
return ObjCOrBuiltinID - tok::NUM_OBJC_KEYWORDS;
return 0;

Am I missing a step in the builtin lookup process?

Many thanks,

Hi Dani,

I’ve just added some builtins for my target and this is what I needed to do:

LLVM was modified by:

  • Created an Intrinsics*.td in include/llvm/ and defined the intrinsic to extend GCCBuiltin. This file was included at the bottom of in the same directory.
  • The instruction was defined in the lib/Target/* file which used the intrinsic as it’s pattern.

Clang was modified by:

  • Created a Builtins*.def for the target in clang/include/clang/Basic.
  • Modified include/clang/Basic/TargetBuiltins.h to include the target by copying the other targets but referencing the newly created .def file.
  • Modified the TargetInfo class to contain BuiltinInfo in clang/lib/Basic/Targets.cpp.
  • Add a function in clang/lib/CodeGen/CGBuiltin.cpp to the handle the builtin and a case in EmitTargetBuiltinExpr to call that function.

If you just follow the examples of the existing targets, its quite simple. Hope that helps.


Research Student
Electronic Systems Design Group
Loughborough University