[PATCH] Add an -mattr option to the gold plugin to support subtarget features in LTO

Tom Roeder tmroeder at google.com
Mon Mar 24 16:27:47 PDT 2014


Hi,

The small patch attached here adds support for an -mattr option to the
gold plugin and to llvm-lto. This allows the caller to specify details
of the subtarget architecture, like +aes, or +ssse3 on x86.  Note that
this required a change to the include/llvm-c/lto.h interface: it adds
a function lto_codegen_set_attr and it increments the version of the
interface.

Please let me know what you think.

Tom
-------------- next part --------------
diff --git a/include/llvm-c/lto.h b/include/llvm-c/lto.h
index 87904c2..42014e5 100644
--- a/include/llvm-c/lto.h
+++ b/include/llvm-c/lto.h
@@ -40,7 +40,7 @@ typedef bool lto_bool_t;
  * @{
  */
 
-#define LTO_API_VERSION 10
+#define LTO_API_VERSION 11
 
 /**
  * \since prior to LTO_API_VERSION=3
@@ -383,6 +383,14 @@ lto_codegen_set_pic_model(lto_code_gen_t cg, lto_codegen_model);
 extern void
 lto_codegen_set_cpu(lto_code_gen_t cg, const char *cpu);
 
+/**
+ * Sets attributes for the cpu to generate code for.
+ *
+ * \since LTO_API_VERSION=11
+ */
+extern void
+lto_codegen_set_attr(lto_code_gen_t cg, const char *attr);
+
 
 /**
  * Sets the location of the assembler tool to run. If not set, libLTO
diff --git a/include/llvm/LTO/LTOCodeGenerator.h b/include/llvm/LTO/LTOCodeGenerator.h
index ee2b208..db79f6e 100644
--- a/include/llvm/LTO/LTOCodeGenerator.h
+++ b/include/llvm/LTO/LTOCodeGenerator.h
@@ -74,6 +74,7 @@ struct LTOCodeGenerator {
   void setInternalizeStrategy(lto_internalize_strategy);
 
   void setCpu(const char *mCpu) { MCpu = mCpu; }
+  void setAttr(const char *mAttr) { MAttr = mAttr; }
 
   void addMustPreserveSymbol(const char *sym) { MustPreserveSymbols[sym] = 1; }
 
@@ -160,6 +161,7 @@ private:
   llvm::MemoryBuffer *NativeObjectFile;
   std::vector<char *> CodegenOptions;
   std::string MCpu;
+  std::string MAttr;
   std::string NativeObjectPath;
   llvm::TargetOptions Options;
   lto_diagnostic_handler_t DiagHandler;
diff --git a/lib/LTO/LTOCodeGenerator.cpp b/lib/LTO/LTOCodeGenerator.cpp
index cdb4f95..2908739 100644
--- a/lib/LTO/LTOCodeGenerator.cpp
+++ b/lib/LTO/LTOCodeGenerator.cpp
@@ -311,8 +311,9 @@ bool LTOCodeGenerator::determineTarget(std::string &errMsg) {
     break;
   }
 
-  // construct LTOModule, hand over ownership of module and target
-  SubtargetFeatures Features;
+  // Construct LTOModule, hand over ownership of module and target. Use MAttr as
+  // the default set of features.
+  SubtargetFeatures Features(MAttr);
   Features.getDefaultSubtargetFeatures(Triple);
   std::string FeatureStr = Features.getString();
   // Set a default CPU for Darwin triples.
diff --git a/test/LTO/attrs.ll b/test/LTO/attrs.ll
new file mode 100644
index 0000000..a2c5368
--- /dev/null
+++ b/test/LTO/attrs.ll
@@ -0,0 +1,14 @@
+; RUN: llvm-as < %s >%t1
+; RUN: llvm-lto -exported-symbol=test_x86_aesni_aeskeygenassist -mattr=+aes -o %t2 %t1
+; RUN: llvm-objdump -d %t2 | FileCheck -check-prefix=WITH_AES %s
+; RUN: not llvm-lto -exported-symbol=test_x86_aesni_aeskeygenassist -mattr=-aes -o %t3 %t1 2>&1 | FileCheck -check-prefix=WITHOUT_AES %s
+
+target triple = "x86_64-unknown-linux-gnu"
+declare <2 x i64> @llvm.x86.aesni.aeskeygenassist(<2 x i64>, i8)
+define <2 x i64> @test_x86_aesni_aeskeygenassist(<2 x i64> %a0) {
+  ; WITH_AES: vaeskeygenassist
+  %res = call <2 x i64> @llvm.x86.aesni.aeskeygenassist(<2 x i64> %a0, i8 7)
+  ret <2 x i64> %res
+}
+
+; WITHOUT_AES: LLVM ERROR: Cannot select: intrinsic %llvm.x86.aesni.aeskeygenassist
diff --git a/tools/llvm-lto/llvm-lto.cpp b/tools/llvm-lto/llvm-lto.cpp
index ec3f0fa..f944be3 100644
--- a/tools/llvm-lto/llvm-lto.cpp
+++ b/tools/llvm-lto/llvm-lto.cpp
@@ -130,6 +130,16 @@ int main(int argc, char **argv) {
   for (unsigned i = 0; i < KeptDSOSyms.size(); ++i)
     CodeGen.addMustPreserveSymbol(KeptDSOSyms[i].c_str());
 
+  std::string attrs;
+  for (unsigned i = 0; i < MAttrs.size(); ++i) {
+    if (i > 0)
+      attrs.append(",");
+    attrs.append(MAttrs[i]);
+  }
+
+  if (!attrs.empty())
+    CodeGen.setAttr(attrs.c_str());
+
   if (!OutputFilename.empty()) {
     size_t len = 0;
     std::string ErrorInfo;
diff --git a/tools/lto/lto.cpp b/tools/lto/lto.cpp
index 1b1e462..f7326aa 100644
--- a/tools/lto/lto.cpp
+++ b/tools/lto/lto.cpp
@@ -56,6 +56,20 @@ static void lto_initialize() {
   }
 }
 
+// Convert the subtarget features into a string to pass to LTOCodeGenerator.
+static void lto_add_attrs(lto_code_gen_t cg) {
+  if (MAttrs.size()) {
+    std::string attrs;
+    for (unsigned i = 0; i < MAttrs.size(); ++i) {
+      if (i > 0)
+        attrs.append(",");
+      attrs.append(MAttrs[i]);
+    }
+
+    cg->setAttr(attrs.c_str());
+  }
+}
+
 /// lto_get_version - Returns a printable string.
 extern const char* lto_get_version() {
   return LTOCodeGenerator::getVersionString();
@@ -252,6 +266,11 @@ void lto_codegen_set_cpu(lto_code_gen_t cg, const char *cpu) {
   return cg->setCpu(cpu);
 }
 
+/// lto_codegen_set_attr - Sets the attr to generate code for.
+void lto_codegen_set_attr(lto_code_gen_t cg, const char *attr) {
+  return cg->setAttr(attr);
+}
+
 /// lto_codegen_set_assembler_path - Sets the path to the assembler tool.
 void lto_codegen_set_assembler_path(lto_code_gen_t cg, const char *path) {
   // In here only for backwards compatibility. We use MC now.
@@ -285,6 +304,7 @@ void lto_codegen_add_must_preserve_symbol(lto_code_gen_t cg,
 bool lto_codegen_write_merged_modules(lto_code_gen_t cg, const char *path) {
   if (!parsedOptions) {
     cg->parseCodeGenDebugOptions();
+    lto_add_attrs(cg);
     parsedOptions = true;
   }
   return !cg->writeMergedModules(path, sLastErrorString);
@@ -299,6 +319,7 @@ bool lto_codegen_write_merged_modules(lto_code_gen_t cg, const char *path) {
 const void *lto_codegen_compile(lto_code_gen_t cg, size_t *length) {
   if (!parsedOptions) {
     cg->parseCodeGenDebugOptions();
+    lto_add_attrs(cg);
     parsedOptions = true;
   }
   return cg->compile(length, DisableOpt, DisableInline, DisableGVNLoadPRE,
@@ -311,6 +332,7 @@ const void *lto_codegen_compile(lto_code_gen_t cg, size_t *length) {
 bool lto_codegen_compile_to_file(lto_code_gen_t cg, const char **name) {
   if (!parsedOptions) {
     cg->parseCodeGenDebugOptions();
+    lto_add_attrs(cg);
     parsedOptions = true;
   }
   return !cg->compile_to_file(name, DisableOpt, DisableInline, DisableGVNLoadPRE,


More information about the llvm-commits mailing list