[LLVMdev] automatically generating intrinsic declarations

Dan Gohman djg at cray.com
Mon Feb 5 10:08:04 PST 2007


LLVM knows what all the types of the intrinsic functions are; I thought,
why are users (including llvm-gcc...) required to duplicate all this
information in order to use them? I mean in order to call getOrInsertFunction
to get declarations for them.

So I wrote this patch, which allows all this code to be generated
automatically. Is this a good approach?

Dan

-- 
Dan Gohman, Cray Inc. <djg at cray.com>
-------------- next part --------------
Index: utils/TableGen/IntrinsicEmitter.cpp
===================================================================
RCS file: /var/cvs/llvm/llvm/utils/TableGen/IntrinsicEmitter.cpp,v
retrieving revision 1.22
diff -u -r1.22 IntrinsicEmitter.cpp
--- utils/TableGen/IntrinsicEmitter.cpp
+++ utils/TableGen/IntrinsicEmitter.cpp
@@ -38,6 +38,9 @@
   // Emit the intrinsic verifier.
   EmitVerifier(Ints, OS);
   
+  // Emit the intrinsic declaration generator.
+  EmitGenerator(Ints, OS);
+  
   // Emit mod/ref info for each function.
   EmitModRefInfo(Ints, OS);
   
@@ -121,6 +124,19 @@
   }
 }
 
+static void EmitTypeGenerate(std::ostream &OS, Record *ArgType) {
+  if (ArgType->isSubClassOf("LLVMIntegerType")) {
+    OS << "IntegerType::get(" << ArgType->getValueAsInt("Width") << ")";
+  } else if (ArgType->isSubClassOf("LLVMPackedType")) {
+    OS << "PackedType::get(";
+    EmitTypeGenerate(OS, ArgType->getValueAsDef("ElTy"));
+    OS << ", " << ArgType->getValueAsInt("NumElts") << ")";
+  } else {
+    OS << "Type::getPrimitiveType(";
+    OS << ArgType->getValueAsString("TypeVal") << ")";
+  }
+}
+
 /// RecordListComparator - Provide a determinstic comparator for lists of
 /// records.
 namespace {
@@ -176,6 +192,43 @@
   OS << "#endif\n\n";
 }
 
+void IntrinsicEmitter::EmitGenerator(const std::vector<CodeGenIntrinsic> &Ints, 
+                                     std::ostream &OS) {
+  OS << "// Code for generating Intrinsic function declarations.\n";
+  OS << "#ifdef GET_INTRINSIC_GENERATOR\n";
+  OS << "  switch (ID) {\n";
+  OS << "  default: assert(0 && \"Invalid intrinsic!\");\n";
+  
+  // Similar to GET_INTRINSIC_VERIFIER, batch up cases that have identical
+  // types.
+  typedef std::map<std::vector<Record*>, std::vector<unsigned>, 
+    RecordListComparator> MapTy;
+  MapTy UniqueArgInfos;
+  
+  // Compute the unique argument type info.
+  for (unsigned i = 0, e = Ints.size(); i != e; ++i)
+    UniqueArgInfos[Ints[i].ArgTypeDefs].push_back(i);
+
+  // Loop through the array, emitting one generator for each batch.
+  for (MapTy::iterator I = UniqueArgInfos.begin(),
+       E = UniqueArgInfos.end(); I != E; ++I) {
+    for (unsigned i = 0, e = I->second.size(); i != e; ++i) {
+      OS << "  case Intrinsic::" << Ints[I->second[i]].EnumName << ":\t\t// "
+         << Ints[I->second[i]].Name << "\n";
+    }
+    
+    const std::vector<Record*> &ArgTypes = I->first;
+    OS << "    return M->getOrInsertFunction(Intrinsic::getName(ID), ";
+    for (unsigned j = 0; j != ArgTypes.size(); ++j) {
+      EmitTypeGenerate(OS, ArgTypes[j]);
+      OS << ", ";
+    }
+    OS << "NULL);\n";
+  }
+  OS << "  }\n";
+  OS << "#endif\n\n";
+}
+
 void IntrinsicEmitter::EmitModRefInfo(const std::vector<CodeGenIntrinsic> &Ints,
                                       std::ostream &OS) {
   OS << "// BasicAliasAnalysis code.\n";
Index: utils/TableGen/IntrinsicEmitter.h
===================================================================
RCS file: /var/cvs/llvm/llvm/utils/TableGen/IntrinsicEmitter.h,v
retrieving revision 1.9
diff -u -r1.9 IntrinsicEmitter.h
--- utils/TableGen/IntrinsicEmitter.h
+++ utils/TableGen/IntrinsicEmitter.h
@@ -35,6 +35,8 @@
                                   std::ostream &OS);
     void EmitVerifier(const std::vector<CodeGenIntrinsic> &Ints, 
                       std::ostream &OS);
+    void EmitGenerator(const std::vector<CodeGenIntrinsic> &Ints, 
+                       std::ostream &OS);
     void EmitModRefInfo(const std::vector<CodeGenIntrinsic> &Ints, 
                         std::ostream &OS);
     void EmitNoMemoryInfo(const std::vector<CodeGenIntrinsic> &Ints, 


More information about the llvm-dev mailing list