[clang] 60c642e - [TLI] Per-function fveclib for math library used for vectorization

2020-04-09 Thread Wenlei He via cfe-commits

Author: Wenlei He
Date: 2020-04-09T18:26:38-07:00
New Revision: 60c642e74be6af86906d9f3d982728be7bd4329f

URL: 
https://github.com/llvm/llvm-project/commit/60c642e74be6af86906d9f3d982728be7bd4329f
DIFF: 
https://github.com/llvm/llvm-project/commit/60c642e74be6af86906d9f3d982728be7bd4329f.diff

LOG: [TLI] Per-function fveclib for math library used for vectorization

Summary:
Encode `-fveclib` setting as per-function attribute so it can threaded through 
to LTO backends. Accordingly per-function TLI now reads
the attributes and select available vector function list based on that. Now we 
also populate function list for all supported vector
libraries for the shared per-module `TargetLibraryInfoImpl`, so each function 
can select its available vector list independently but without
duplicating the vector function lists. Inlining between incompatbile vectlib 
attributed is also prohibited now.

Subscribers: hiraditya, dexonsmith, cfe-commits

Tags: #clang

Differential Revision: https://reviews.llvm.org/D77632

Added: 
clang/test/CodeGen/libcalls-veclib.c
llvm/test/Transforms/Inline/veclib-compat.ll

Modified: 
clang/lib/CodeGen/BackendUtil.cpp
clang/lib/CodeGen/CGCall.cpp
llvm/include/llvm/Analysis/TargetLibraryInfo.h
llvm/lib/Analysis/InlineCost.cpp
llvm/lib/Analysis/TargetLibraryInfo.cpp
llvm/test/Transforms/Inline/inline-no-builtin-compatible.ll

Removed: 




diff  --git a/clang/lib/CodeGen/BackendUtil.cpp 
b/clang/lib/CodeGen/BackendUtil.cpp
index 8d2d5c1534fc..82c200ee6ae1 100644
--- a/clang/lib/CodeGen/BackendUtil.cpp
+++ b/clang/lib/CodeGen/BackendUtil.cpp
@@ -352,24 +352,8 @@ static void addMemTagOptimizationPasses(const 
PassManagerBuilder &Builder,
   PM.add(createStackSafetyGlobalInfoWrapperPass(/*SetMetadata=*/true));
 }
 
-static TargetLibraryInfoImpl *createTLII(llvm::Triple &TargetTriple,
- const CodeGenOptions &CodeGenOpts) {
-  TargetLibraryInfoImpl *TLII = new TargetLibraryInfoImpl(TargetTriple);
-
-  switch (CodeGenOpts.getVecLib()) {
-  case CodeGenOptions::Accelerate:
-
TLII->addVectorizableFunctionsFromVecLib(TargetLibraryInfoImpl::Accelerate);
-break;
-  case CodeGenOptions::MASSV:
-TLII->addVectorizableFunctionsFromVecLib(TargetLibraryInfoImpl::MASSV);
-break;
-  case CodeGenOptions::SVML:
-TLII->addVectorizableFunctionsFromVecLib(TargetLibraryInfoImpl::SVML);
-break;
-  default:
-break;
-  }
-  return TLII;
+static TargetLibraryInfoImpl *createTLII(llvm::Triple &TargetTriple) {
+  return new TargetLibraryInfoImpl(TargetTriple);
 }
 
 static void addSymbolRewriterPass(const CodeGenOptions &Opts,
@@ -562,8 +546,7 @@ void EmitAssemblyHelper::CreatePasses(legacy::PassManager 
&MPM,
   // are inserted before PMBuilder ones - they'd get the default-constructed
   // TLI with an unknown target otherwise.
   Triple TargetTriple(TheModule->getTargetTriple());
-  std::unique_ptr TLII(
-  createTLII(TargetTriple, CodeGenOpts));
+  std::unique_ptr TLII(createTLII(TargetTriple));
 
   // If we reached here with a non-empty index file name, then the index file
   // was empty and we are not performing ThinLTO backend compilation (used in
@@ -805,8 +788,7 @@ bool EmitAssemblyHelper::AddEmitPasses(legacy::PassManager 
&CodeGenPasses,
raw_pwrite_stream *DwoOS) {
   // Add LibraryInfo.
   llvm::Triple TargetTriple(TheModule->getTargetTriple());
-  std::unique_ptr TLII(
-  createTLII(TargetTriple, CodeGenOpts));
+  std::unique_ptr TLII(createTLII(TargetTriple));
   CodeGenPasses.add(new TargetLibraryInfoWrapperPass(*TLII));
 
   // Normal mode, emit a .s or .o file by running the code generator. Note,
@@ -1142,8 +1124,7 @@ void EmitAssemblyHelper::EmitAssemblyWithNewPassManager(
   // Register the target library analysis directly and give it a customized
   // preset TLI.
   Triple TargetTriple(TheModule->getTargetTriple());
-  std::unique_ptr TLII(
-  createTLII(TargetTriple, CodeGenOpts));
+  std::unique_ptr TLII(createTLII(TargetTriple));
   FAM.registerPass([&] { return TargetLibraryAnalysis(*TLII); });
 
   // Register all the basic analyses with the managers.

diff  --git a/clang/lib/CodeGen/CGCall.cpp b/clang/lib/CodeGen/CGCall.cpp
index 3c44632dfd60..73869fa3c9b0 100644
--- a/clang/lib/CodeGen/CGCall.cpp
+++ b/clang/lib/CodeGen/CGCall.cpp
@@ -1868,6 +1868,24 @@ static void addNoBuiltinAttributes(llvm::AttrBuilder 
&FuncAttrs,
   llvm::for_each(NBA->builtinNames(), AddNoBuiltinAttr);
 }
 
+static void addVectLibAttributes(llvm::AttrBuilder &FuncAttrs,
+ const CodeGenOptions &CodeGenOpts) {
+  StringRef AttributeName = "veclib";
+  switch (CodeGenOpts.getVecLib()) {
+  case CodeGenOptions::Accelerate:
+FuncAttrs.addAttribute(AttributeName, "Accelerate");
+break;
+  case CodeGenOptions::MASSV:
+FuncAttrs.addAttribute(Attrib

[clang] 7c8a693 - [Remarks] Add callsite locations to inline remarks

2020-06-20 Thread Wenlei He via cfe-commits

Author: Wenlei He
Date: 2020-06-20T23:32:10-07:00
New Revision: 7c8a6936bf6b578518673f442c6c292d62cdd465

URL: 
https://github.com/llvm/llvm-project/commit/7c8a6936bf6b578518673f442c6c292d62cdd465
DIFF: 
https://github.com/llvm/llvm-project/commit/7c8a6936bf6b578518673f442c6c292d62cdd465.diff

LOG: [Remarks] Add callsite locations to inline remarks

Summary:
Add call site location info into inline remarks so we can differentiate inline 
sites.
This can be useful for inliner tuning. We can also reconstruct full 
hierarchical inline
tree from parsing such remarks. The messege of inline remark is also tweaked so 
we can
differentiate SampleProfileLoader inline from CGSCC inline.

Reviewers: wmi, davidxl, hoy

Subscribers: hiraditya, cfe-commits, llvm-commits

Tags: #clang, #llvm

Differential Revision: https://reviews.llvm.org/D82213

Added: 


Modified: 
clang/test/Frontend/optimization-remark-with-hotness-new-pm.c
clang/test/Frontend/optimization-remark-with-hotness.c
llvm/include/llvm/Analysis/InlineAdvisor.h
llvm/lib/Analysis/InlineAdvisor.cpp
llvm/lib/Transforms/IPO/SampleProfile.cpp
llvm/test/Transforms/Inline/optimization-remarks-passed-yaml.ll
llvm/test/Transforms/SampleProfile/Inputs/remarks.prof
llvm/test/Transforms/SampleProfile/remarks.ll

Removed: 




diff  --git a/clang/test/Frontend/optimization-remark-with-hotness-new-pm.c 
b/clang/test/Frontend/optimization-remark-with-hotness-new-pm.c
index 24da0925abdc..2a03bcb9390f 100644
--- a/clang/test/Frontend/optimization-remark-with-hotness-new-pm.c
+++ b/clang/test/Frontend/optimization-remark-with-hotness-new-pm.c
@@ -73,7 +73,7 @@ void bar(int x) {
   // THRESHOLD-NOT: hotness
   // NO_PGO: '-fdiagnostics-show-hotness' requires profile-guided optimization 
information
   // NO_PGO: '-fdiagnostics-hotness-threshold=' requires profile-guided 
optimization information
-  // expected-remark@+1 {{foo inlined into bar with (cost=always): always 
inline attribute (hotness:}}
+  // expected-remark@+1 {{foo inlined into bar with (cost=always): always 
inline attribute at callsite bar:8 (hotness:}}
   sum += foo(x, x - 2);
 }
 

diff  --git a/clang/test/Frontend/optimization-remark-with-hotness.c 
b/clang/test/Frontend/optimization-remark-with-hotness.c
index 0f3a4e9d1d6b..96be3524db16 100644
--- a/clang/test/Frontend/optimization-remark-with-hotness.c
+++ b/clang/test/Frontend/optimization-remark-with-hotness.c
@@ -66,7 +66,7 @@ void bar(int x) {
   // THRESHOLD-NOT: hotness
   // NO_PGO: '-fdiagnostics-show-hotness' requires profile-guided optimization 
information
   // NO_PGO: '-fdiagnostics-hotness-threshold=' requires profile-guided 
optimization information
-  // expected-remark@+1 {{foo inlined into bar with (cost=always): always 
inliner (hotness:}}
+  // expected-remark@+1 {{foo inlined into bar with (cost=always): always 
inliner at callsite bar:8 (hotness:}}
   sum += foo(x, x - 2);
 }
 

diff  --git a/llvm/include/llvm/Analysis/InlineAdvisor.h 
b/llvm/include/llvm/Analysis/InlineAdvisor.h
index 7268c11fdd79..66a848e865e1 100644
--- a/llvm/include/llvm/Analysis/InlineAdvisor.h
+++ b/llvm/include/llvm/Analysis/InlineAdvisor.h
@@ -217,7 +217,12 @@ shouldInline(CallBase &CB, 
function_ref GetInlineCost,
 /// Emit ORE message.
 void emitInlinedInto(OptimizationRemarkEmitter &ORE, DebugLoc DLoc,
  const BasicBlock *Block, const Function &Callee,
- const Function &Caller, const InlineCost &IC);
+ const Function &Caller, const InlineCost &IC,
+ bool ForProfileContext = false,
+ const char *PassName = nullptr);
+
+/// Add location info to ORE message.
+void addLocationToRemarks(OptimizationRemark &Remark, DebugLoc DLoc);
 
 /// Set the inline-remark attribute.
 void setInlineRemark(CallBase &CB, StringRef Message);

diff  --git a/llvm/lib/Analysis/InlineAdvisor.cpp 
b/llvm/lib/Analysis/InlineAdvisor.cpp
index d3cd703385f4..2d7b4cabebb1 100644
--- a/llvm/lib/Analysis/InlineAdvisor.cpp
+++ b/llvm/lib/Analysis/InlineAdvisor.cpp
@@ -18,6 +18,7 @@
 #include "llvm/Analysis/ProfileSummaryInfo.h"
 #include "llvm/Analysis/TargetLibraryInfo.h"
 #include "llvm/Analysis/TargetTransformInfo.h"
+#include "llvm/IR/DebugInfoMetadata.h"
 #include "llvm/IR/Instructions.h"
 #include "llvm/Support/raw_ostream.h"
 
@@ -354,14 +355,43 @@ llvm::shouldInline(CallBase &CB,
   return IC;
 }
 
+void llvm::addLocationToRemarks(OptimizationRemark &Remark, DebugLoc DLoc) {
+  if (!DLoc.get())
+return;
+
+  bool First = true;
+  Remark << " at callsite ";
+  for (DILocation *DIL = DLoc.get(); DIL; DIL = DIL->getInlinedAt()) {
+if (!First)
+  Remark << " @ ";
+unsigned int Offset = DIL->getLine();
+Offset -= DIL->getScope()->getSubprogram()->getLine();
+unsigned int Discriminator = DIL->getBaseDiscriminator();
+StringRef Name = DIL->getSco