[llvm] r236677 - Let llc and opt override "-target-cpu" and "-target-features" via command line

Akira Hatanaka ahatanaka at apple.com
Wed May 6 16:54:14 PDT 2015


Author: ahatanak
Date: Wed May  6 18:54:14 2015
New Revision: 236677

URL: http://llvm.org/viewvc/llvm-project?rev=236677&view=rev
Log:
Let llc and opt override "-target-cpu" and "-target-features" via command line
options.

This commit fixes a bug in llc and opt where "-mcpu" and "-mattr" wouldn't
override function attributes "-target-cpu" and "-target-features" in the IR.

Differential Revision: http://reviews.llvm.org/D9537

Added:
    llvm/trunk/test/CodeGen/X86/llc-override-mcpu-mattr.ll
    llvm/trunk/test/Other/opt-override-mcpu-mattr.ll
Modified:
    llvm/trunk/include/llvm/CodeGen/CommandFlags.h
    llvm/trunk/include/llvm/IR/Function.h
    llvm/trunk/lib/IR/Function.cpp
    llvm/trunk/test/Transforms/SLPVectorizer/X86/call.ll
    llvm/trunk/tools/llc/llc.cpp
    llvm/trunk/tools/opt/opt.cpp

Modified: llvm/trunk/include/llvm/CodeGen/CommandFlags.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/CodeGen/CommandFlags.h?rev=236677&r1=236676&r2=236677&view=diff
==============================================================================
--- llvm/trunk/include/llvm/CodeGen/CommandFlags.h (original)
+++ llvm/trunk/include/llvm/CodeGen/CommandFlags.h Wed May  6 18:54:14 2015
@@ -16,6 +16,7 @@
 #ifndef LLVM_CODEGEN_COMMANDFLAGS_H
 #define LLVM_CODEGEN_COMMANDFLAGS_H
 
+#include "llvm/IR/Module.h"
 #include "llvm/MC/MCTargetOptionsCommandFlags.h"
 #include "llvm//MC/SubtargetFeature.h"
 #include "llvm/Support/CodeGen.h"
@@ -291,4 +292,15 @@ static inline std::string getFeaturesStr
   return Features.getString();
 }
 
+static inline void overrideFunctionAttributes(StringRef CPU, StringRef Features,
+                                              Module &M) {
+  for (auto &F : M) {
+    if (!CPU.empty())
+      llvm::overrideFunctionAttribute("target-cpu", CPU, F);
+
+    if (!Features.empty())
+      llvm::overrideFunctionAttribute("target-features", Features, F);
+  }
+}
+
 #endif

Modified: llvm/trunk/include/llvm/IR/Function.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/IR/Function.h?rev=236677&r1=236676&r2=236677&view=diff
==============================================================================
--- llvm/trunk/include/llvm/IR/Function.h (original)
+++ llvm/trunk/include/llvm/IR/Function.h Wed May  6 18:54:14 2015
@@ -584,6 +584,9 @@ ilist_traits<Argument>::getSymTab(Functi
   return F ? &F->getValueSymbolTable() : nullptr;
 }
 
+/// \brief Overwrite attribute Kind in function F.
+void overrideFunctionAttribute(StringRef Kind, StringRef Value, Function &F);
+
 } // End llvm namespace
 
 #endif

Modified: llvm/trunk/lib/IR/Function.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/IR/Function.cpp?rev=236677&r1=236676&r2=236677&view=diff
==============================================================================
--- llvm/trunk/lib/IR/Function.cpp (original)
+++ llvm/trunk/lib/IR/Function.cpp Wed May  6 18:54:14 2015
@@ -975,3 +975,16 @@ void Function::setPrologueData(Constant
   }
   setValueSubclassData(PDData);
 }
+
+void llvm::overrideFunctionAttribute(StringRef Kind, StringRef Value,
+                                     Function &F) {
+  auto &Ctx = F.getContext();
+  AttributeSet Attrs = F.getAttributes(), AttrsToRemove;
+
+  AttrsToRemove =
+      AttrsToRemove.addAttribute(Ctx, AttributeSet::FunctionIndex, Kind);
+  Attrs = Attrs.removeAttributes(Ctx, AttributeSet::FunctionIndex,
+                                 AttrsToRemove);
+  Attrs = Attrs.addAttribute(Ctx, AttributeSet::FunctionIndex, Kind, Value);
+  F.setAttributes(Attrs);
+}

Added: llvm/trunk/test/CodeGen/X86/llc-override-mcpu-mattr.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/X86/llc-override-mcpu-mattr.ll?rev=236677&view=auto
==============================================================================
--- llvm/trunk/test/CodeGen/X86/llc-override-mcpu-mattr.ll (added)
+++ llvm/trunk/test/CodeGen/X86/llc-override-mcpu-mattr.ll Wed May  6 18:54:14 2015
@@ -0,0 +1,19 @@
+; RUN: llc < %s -march x86-64 -mcpu=broadwell | FileCheck %s
+; RUN: llc < %s -march x86-64 -mattr=+avx2 | FileCheck %s
+
+; Check that llc can overide function attributes target-cpu and target-features
+; using command line options -mcpu and -mattr.
+
+; CHECK: vpsadbw %ymm{{[0-9]+}}, %ymm{{[0-9]+}}, %ymm{{[0-9]+}}
+
+define <4 x i64> @foo1(<4 x i64> %s1, <4 x i64> %s2) #0 {
+entry:
+  %0 = bitcast <4 x i64> %s1 to <32 x i8>
+  %1 = bitcast <4 x i64> %s2 to <32 x i8>
+  %2 = tail call <4 x i64> @llvm.x86.avx2.psad.bw(<32 x i8> %0, <32 x i8> %1)
+  ret <4 x i64> %2
+}
+
+declare <4 x i64> @llvm.x86.avx2.psad.bw(<32 x i8>, <32 x i8>)
+
+attributes #0 = { "target-cpu"="core2" "target-features"="+ssse3,+cx16,+sse4.2,+sse4.1,+sse,+sse2,+sse3,+avx,+popcnt" }

Added: llvm/trunk/test/Other/opt-override-mcpu-mattr.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Other/opt-override-mcpu-mattr.ll?rev=236677&view=auto
==============================================================================
--- llvm/trunk/test/Other/opt-override-mcpu-mattr.ll (added)
+++ llvm/trunk/test/Other/opt-override-mcpu-mattr.ll Wed May  6 18:54:14 2015
@@ -0,0 +1,13 @@
+; RUN: opt < %s -mtriple=x86_64-apple-darwin -mcpu=broadwell -mattr=+avx2 -S | FileCheck %s
+
+; Check that opt can rewrite function attributes target-cpu and target-features
+; using command line options -mcpu and -mattr.
+
+; CHECK: attributes #0 = { nounwind readnone ssp uwtable "target-cpu"="broadwell" "target-features"="+avx2" "use-soft-float"="false" }
+
+define i32 @foo1() #0 {
+entry:
+  ret i32 0
+}
+
+attributes #0 = { nounwind readnone ssp uwtable "target-cpu"="core2" "target-features"="+ssse3,+cx16,+sse,+sse2,+sse3" "use-soft-float"="false" }

Modified: llvm/trunk/test/Transforms/SLPVectorizer/X86/call.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/SLPVectorizer/X86/call.ll?rev=236677&r1=236676&r2=236677&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/SLPVectorizer/X86/call.ll (original)
+++ llvm/trunk/test/Transforms/SLPVectorizer/X86/call.ll Wed May  6 18:54:14 2015
@@ -119,10 +119,10 @@ entry:
 }
 
 
-; CHECK: declare <2 x double> @llvm.sin.v2f64(<2 x double>) #0
-; CHECK: declare <2 x double> @llvm.cos.v2f64(<2 x double>) #0
-; CHECK: declare <2 x double> @llvm.pow.v2f64(<2 x double>, <2 x double>) #0
-; CHECK: declare <2 x double> @llvm.exp2.v2f64(<2 x double>) #0
+; CHECK: declare <2 x double> @llvm.sin.v2f64(<2 x double>) [[ATTR0:#[0-9]+]]
+; CHECK: declare <2 x double> @llvm.cos.v2f64(<2 x double>) [[ATTR0]]
+; CHECK: declare <2 x double> @llvm.pow.v2f64(<2 x double>, <2 x double>) [[ATTR0]]
+; CHECK: declare <2 x double> @llvm.exp2.v2f64(<2 x double>) [[ATTR0]]
 
-; CHECK: attributes #0 = { nounwind readnone }
+; CHECK: attributes [[ATTR0]] = { nounwind readnone }
 

Modified: llvm/trunk/tools/llc/llc.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llc/llc.cpp?rev=236677&r1=236676&r2=236677&view=diff
==============================================================================
--- llvm/trunk/tools/llc/llc.cpp (original)
+++ llvm/trunk/tools/llc/llc.cpp Wed May  6 18:54:14 2015
@@ -305,6 +305,9 @@ static int compileModule(char **argv, LL
   if (const DataLayout *DL = Target->getDataLayout())
     M->setDataLayout(*DL);
 
+  // Override function attributes.
+  overrideFunctionAttributes(CPUStr, FeaturesStr, *M);
+
   if (RelaxAll.getNumOccurrences() > 0 &&
       FileType != TargetMachine::CGFT_ObjectFile)
     errs() << argv[0]

Modified: llvm/trunk/tools/opt/opt.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/opt/opt.cpp?rev=236677&r1=236676&r2=236677&view=diff
==============================================================================
--- llvm/trunk/tools/opt/opt.cpp (original)
+++ llvm/trunk/tools/opt/opt.cpp Wed May  6 18:54:14 2015
@@ -394,6 +394,9 @@ int main(int argc, char **argv) {
 
   std::unique_ptr<TargetMachine> TM(Machine);
 
+  // Override function attributes.
+  overrideFunctionAttributes(CPUStr, FeaturesStr, *M);
+
   // If the output is set to be emitted to standard out, and standard out is a
   // console, print out a warning message and refuse to do it.  We don't
   // impress anyone by spewing tons of binary goo to a terminal.





More information about the llvm-commits mailing list