[PATCH] D118627: [clang-format] Don't break block comments when sorting includes.

2022-01-31 Thread Marek Kurdej via Phabricator via cfe-commits
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rG95bf0a9ebdb4: [clang-format] Don't break block comments 
when sorting includes. (authored by curdeius).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D118627/new/

https://reviews.llvm.org/D118627

Files:
  clang/lib/Format/Format.cpp
  clang/unittests/Format/SortIncludesTest.cpp


Index: clang/unittests/Format/SortIncludesTest.cpp
===
--- clang/unittests/Format/SortIncludesTest.cpp
+++ clang/unittests/Format/SortIncludesTest.cpp
@@ -70,6 +70,21 @@
  {tooling::Range(25, 1)}));
 }
 
+TEST_F(SortIncludesTest, TrailingComments) {
+  EXPECT_EQ("#include \"a.h\"\n"
+"#include \"b.h\" /* long\n"
+"  * long\n"
+"  * comment*/\n"
+"#include \"c.h\"\n"
+"#include \"d.h\"\n",
+sort("#include \"a.h\"\n"
+ "#include \"c.h\"\n"
+ "#include \"b.h\" /* long\n"
+ "  * long\n"
+ "  * comment*/\n"
+ "#include \"d.h\"\n"));
+}
+
 TEST_F(SortIncludesTest, SortedIncludesUsingSortPriorityAttribute) {
   FmtStyle.IncludeStyle.IncludeBlocks = tooling::IncludeStyle::IBS_Regroup;
   FmtStyle.IncludeStyle.IncludeCategories = {
Index: clang/lib/Format/Format.cpp
===
--- clang/lib/Format/Format.cpp
+++ clang/lib/Format/Format.cpp
@@ -2678,6 +2678,15 @@
 if (!FormattingOff && !MergeWithNextLine) {
   if (IncludeRegex.match(Line, &Matches)) {
 StringRef IncludeName = Matches[2];
+if (Line.contains("/*") && !Line.contains("*/")) {
+  // #include with a start of a block comment, but without the end.
+  // Need to keep all the lines until the end of the comment together.
+  // FIXME: This is somehow simplified check that probably does not 
work
+  // correctly if there are multiple comments on a line.
+  Pos = Code.find("*/", SearchFrom);
+  Line = Code.substr(
+  Prev, (Pos != StringRef::npos ? Pos + 2 : Code.size()) - Prev);
+}
 int Category = Categories.getIncludePriority(
 IncludeName,
 /*CheckMainHeader=*/!MainIncludeFound && FirstIncludeBlock);


Index: clang/unittests/Format/SortIncludesTest.cpp
===
--- clang/unittests/Format/SortIncludesTest.cpp
+++ clang/unittests/Format/SortIncludesTest.cpp
@@ -70,6 +70,21 @@
  {tooling::Range(25, 1)}));
 }
 
+TEST_F(SortIncludesTest, TrailingComments) {
+  EXPECT_EQ("#include \"a.h\"\n"
+"#include \"b.h\" /* long\n"
+"  * long\n"
+"  * comment*/\n"
+"#include \"c.h\"\n"
+"#include \"d.h\"\n",
+sort("#include \"a.h\"\n"
+ "#include \"c.h\"\n"
+ "#include \"b.h\" /* long\n"
+ "  * long\n"
+ "  * comment*/\n"
+ "#include \"d.h\"\n"));
+}
+
 TEST_F(SortIncludesTest, SortedIncludesUsingSortPriorityAttribute) {
   FmtStyle.IncludeStyle.IncludeBlocks = tooling::IncludeStyle::IBS_Regroup;
   FmtStyle.IncludeStyle.IncludeCategories = {
Index: clang/lib/Format/Format.cpp
===
--- clang/lib/Format/Format.cpp
+++ clang/lib/Format/Format.cpp
@@ -2678,6 +2678,15 @@
 if (!FormattingOff && !MergeWithNextLine) {
   if (IncludeRegex.match(Line, &Matches)) {
 StringRef IncludeName = Matches[2];
+if (Line.contains("/*") && !Line.contains("*/")) {
+  // #include with a start of a block comment, but without the end.
+  // Need to keep all the lines until the end of the comment together.
+  // FIXME: This is somehow simplified check that probably does not work
+  // correctly if there are multiple comments on a line.
+  Pos = Code.find("*/", SearchFrom);
+  Line = Code.substr(
+  Prev, (Pos != StringRef::npos ? Pos + 2 : Code.size()) - Prev);
+}
 int Category = Categories.getIncludePriority(
 IncludeName,
 /*CheckMainHeader=*/!MainIncludeFound && FirstIncludeBlock);
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] 95bf0a9 - [clang-format] Don't break block comments when sorting includes.

2022-01-31 Thread Marek Kurdej via cfe-commits

Author: Marek Kurdej
Date: 2022-02-01T08:51:10+01:00
New Revision: 95bf0a9ebdb4ee6f78699e20751602c8c3c5733f

URL: 
https://github.com/llvm/llvm-project/commit/95bf0a9ebdb4ee6f78699e20751602c8c3c5733f
DIFF: 
https://github.com/llvm/llvm-project/commit/95bf0a9ebdb4ee6f78699e20751602c8c3c5733f.diff

LOG: [clang-format] Don't break block comments when sorting includes.

Fixes https://github.com/llvm/llvm-project/issues/34626.

Before, the include sorter would break the code:
```
#include 
#include  /* long
   comment */
```
and change it into:
```
#include  /* long
#include 
   comment */
```

This commit handles only the most basic case of a single block comment on an 
include line, but does not try to handle all the possible edge cases with 
multiple comments.

Reviewed By: HazardyKnusperkeks

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

Added: 


Modified: 
clang/lib/Format/Format.cpp
clang/unittests/Format/SortIncludesTest.cpp

Removed: 




diff  --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 04e2915e3af69..baf9c6885a86b 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -2678,6 +2678,15 @@ tooling::Replacements sortCppIncludes(const FormatStyle 
&Style, StringRef Code,
 if (!FormattingOff && !MergeWithNextLine) {
   if (IncludeRegex.match(Line, &Matches)) {
 StringRef IncludeName = Matches[2];
+if (Line.contains("/*") && !Line.contains("*/")) {
+  // #include with a start of a block comment, but without the end.
+  // Need to keep all the lines until the end of the comment together.
+  // FIXME: This is somehow simplified check that probably does not 
work
+  // correctly if there are multiple comments on a line.
+  Pos = Code.find("*/", SearchFrom);
+  Line = Code.substr(
+  Prev, (Pos != StringRef::npos ? Pos + 2 : Code.size()) - Prev);
+}
 int Category = Categories.getIncludePriority(
 IncludeName,
 /*CheckMainHeader=*/!MainIncludeFound && FirstIncludeBlock);

diff  --git a/clang/unittests/Format/SortIncludesTest.cpp 
b/clang/unittests/Format/SortIncludesTest.cpp
index 1d215af715382..b0ee6bfc35979 100644
--- a/clang/unittests/Format/SortIncludesTest.cpp
+++ b/clang/unittests/Format/SortIncludesTest.cpp
@@ -70,6 +70,21 @@ TEST_F(SortIncludesTest, BasicSorting) {
  {tooling::Range(25, 1)}));
 }
 
+TEST_F(SortIncludesTest, TrailingComments) {
+  EXPECT_EQ("#include \"a.h\"\n"
+"#include \"b.h\" /* long\n"
+"  * long\n"
+"  * comment*/\n"
+"#include \"c.h\"\n"
+"#include \"d.h\"\n",
+sort("#include \"a.h\"\n"
+ "#include \"c.h\"\n"
+ "#include \"b.h\" /* long\n"
+ "  * long\n"
+ "  * comment*/\n"
+ "#include \"d.h\"\n"));
+}
+
 TEST_F(SortIncludesTest, SortedIncludesUsingSortPriorityAttribute) {
   FmtStyle.IncludeStyle.IncludeBlocks = tooling::IncludeStyle::IBS_Regroup;
   FmtStyle.IncludeStyle.IncludeCategories = {



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D118596: [clang][dataflow] Enable comparison of distinct values in Environment

2022-01-31 Thread Gábor Horváth via Phabricator via cfe-commits
xazax.hun added inline comments.



Comment at: clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp:151
 
-if (auto *FirstVal = dyn_cast(Val)) {
-  auto *SecondVal = cast(It->second);
-  if (&FirstVal->getPointeeLoc() == &SecondVal->getPointeeLoc()) {
-LocToVal.insert({Loc, FirstVal});
+if (auto *Val1 = dyn_cast(Val)) {
+  auto *Val2 = cast(It->second);

Now we have multiple, slightly different versions of equality for values. One 
here, one in `Environment::equivalentTo`. I'd prefer to have one way to check 
value equality factored out somewhere and reused it both here and there. Also, 
`Environment::equivalentTo` works with any indirection, this snippet only works 
with pointers (and not with references).

It is also not clear to me at this point what is the role of 
`compareEquivalent`. Is it intentional that it is not used here?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D118596/new/

https://reviews.llvm.org/D118596

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D118428: [clang-cl] Support the /JMC flag

2022-01-31 Thread Yuanfang Chen via Phabricator via cfe-commits
ychen added a comment.

@hans @aganea I think all comments are addressed except that I don't know how 
to test the ARM/ARM64 JMC function (I don't have ARM hardware or is there any 
ARM-based Windows virtual machine?). PTAL.




Comment at: clang/docs/ReleaseNotes.rst:155
+- Add support for MSVC-compatible ``/JMC``/``/JMC-`` flag in clang-cl, and 
+  equivalent -cc1 flag ``-fjmc``. ``/JMC`` could only be used when ``/Zi`` or
+  ``/Z7`` is turned on. With this addition, clang-cl can be used in Visual

aganea wrote:
> hans wrote:
> > The cc1 flag is more of an implementation detail, so not sure it's worth 
> > mentioning in the release notes. Or do we want "clang -fjmc" to work?
> @ychen @hans An additional question is whether we want to use the same cc1 
> flag for an potential ELF port?
Removed. Yeah, I think "clang -fjmc" for an ELF implementation makes sense.



Comment at: clang/include/clang/Driver/Options.td:6333
+def _SLASH_JMC_ : CLFlag<"JMC-">,
+  HelpText<"Disable native C/C++ Just My Code debugging (default)">;
 def _SLASH_LD : CLFlag<"LD">, HelpText<"Create DLL">;

hans wrote:
> I'm not sure "native C/C++" adds much info in the context of clang. Maybe 
> just "Enable Just My Code debugging" is enough?
> 
> (Also, how do we want to spell this? Other features are not typically 
> capitalized in clang.. so maybe "enable just my code debugging" or "enable 
> just-my-code debugging"?)
Used "enable just-my-code debugging"



Comment at: clang/lib/Driver/ToolChains/Clang.cpp:7458
+   /*Default=*/false)) {
+if (*EmitCodeView && *DebugInfoKind >= 
codegenoptions::DebugInfoConstructor)
+  CmdArgs.push_back("-fjmc");

aganea wrote:
> hans wrote:
> > Why that level specifically and not e.g. *DebugInfoKind > NoDebugInfo?
> @hans MSVC does this check too, not sure why.
> ```
> D:\git\llvm-project>cl main.cpp /c /JMC
> Microsoft (R) C/C++ Optimizing Compiler Version 19.29.30139 for ARM64
> Copyright (C) Microsoft Corporation.  All rights reserved.
> 
> cl : Command line warning D9007 : '/JMC' requires '/Zi, /ZI or /Z7'; option 
> ignored
> main.cpp
> ```
I think it needs enough debug info to enable MSVC stepping. The ones < 
DebugInfoConstructor seems not sufficient but I'm entirely sure.



Comment at: llvm/lib/CodeGen/CommandFlags.cpp:462
+  static cl::opt EnableJMCInstrument(
+  "enable-jmc-instrument",
+  cl::desc("Instrument functions with a call to 
__CheckForDebuggerJustMyCode"),

hans wrote:
> Other "on/off" options don't seem to have "enable" in the name or flag 
> spelling, e.g. "-strict-dwarf", not "-enable-strict-dwarf". Maybe this should 
> be just "-jmc-instrument" and JMCInstrument?
The "-jmc-instrument" is already used by the pass itself (`#define DEBUG_TYPE 
"jmc-instrument"`). The `DEBUG_TYPE` one enables `opt -jmc-instrument`; this 
makes `llc -enable-jmc-instrument` to run the pass in IR codegen pipeline.

Just renamed `cl::opt EnableJMCInstrument` to `cl::opt 
JMCInstrument`.



Comment at: llvm/lib/CodeGen/JMCInstrumenter.cpp:18
+// - (TODO) calls to __CheckForDebuggerJustMyCode should be a scheduling
+// boundary
+//

hans wrote:
> Is that necessary? I assume nothing interesting would get scheduled across 
> such a call anyway?
Agreed that in practice, it is not very likely to cause trouble. But I still 
think it is nice to have. 

Removed the TODO. Make it a comment down below.



Comment at: llvm/lib/CodeGen/JMCInstrumenter.cpp:66
+  // `realpath` on Linux and `GetFullPathName()` on Windows
+  // 
(https://docs.microsoft.com/en-us/dotnet/standard/io/file-path-formats#path-normalization).
+  SmallString<256> FilePath(SP.getDirectory());

hans wrote:
> I don't think we'd want to call realpath or GetFullPathName() as some builds 
> may want to use relative paths, or paths with a specific prefix (see the 
> -fdebug-compilation-dir flag)
Yep, missed that. Comments updated.



Comment at: llvm/lib/CodeGen/JMCInstrumenter.cpp:75
+  // __D032E919_file@any@c. The hashing function or the naming convention match
+  // MSVC's behavior however the match is not required to make JMC work.
+

hans wrote:
> Should probably point out here (or below) that we're not using the same hash 
> as MSVC.
yep. Updated the comment here.



Comment at: llvm/lib/CodeGen/JMCInstrumenter.cpp:143
+  Function *Decl = M.getFunction(CheckFunctionName);
+  assert(Decl);
+  Decl->setUnnamedAddr(GlobalValue::UnnamedAddr::Global);

hans wrote:
> What if there are no function definitions in the module? In that case, I 
> think we would not have created the function declaration, and this assert 
> would fail?
> 
> I think instead of this micro-optimization, it would be safer set the 
> function attributes etc. when creating the decl, and cache i

[PATCH] D118428: [clang-cl] Support the /JMC flag

2022-01-31 Thread Yuanfang Chen via Phabricator via cfe-commits
ychen updated this revision to Diff 404805.
ychen marked 10 inline comments as done.
ychen added a comment.

- address hans's comments


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D118428/new/

https://reviews.llvm.org/D118428

Files:
  clang/docs/ReleaseNotes.rst
  clang/include/clang/Basic/CodeGenOptions.def
  clang/include/clang/Basic/DiagnosticDriverKinds.td
  clang/include/clang/Driver/Options.td
  clang/lib/CodeGen/BackendUtil.cpp
  clang/lib/Driver/ToolChains/Clang.cpp
  clang/test/Driver/cl-options.c
  llvm/include/llvm/CodeGen/CommandFlags.h
  llvm/include/llvm/CodeGen/Passes.h
  llvm/include/llvm/InitializePasses.h
  llvm/include/llvm/LinkAllPasses.h
  llvm/include/llvm/Target/TargetOptions.h
  llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp
  llvm/lib/CodeGen/CMakeLists.txt
  llvm/lib/CodeGen/CodeGen.cpp
  llvm/lib/CodeGen/CommandFlags.cpp
  llvm/lib/CodeGen/JMCInstrumenter.cpp
  llvm/lib/CodeGen/TargetPassConfig.cpp
  llvm/test/CodeGen/X86/jmc-instrument.ll
  llvm/test/Instrumentation/JustMyCode/jmc-instrument-x86.ll
  llvm/test/Instrumentation/JustMyCode/jmc-instrument.ll
  llvm/tools/opt/opt.cpp

Index: llvm/tools/opt/opt.cpp
===
--- llvm/tools/opt/opt.cpp
+++ llvm/tools/opt/opt.cpp
@@ -498,7 +498,8 @@
   "generic-to-nvvm",  "expandmemcmp",
   "loop-reduce",  "lower-amx-type",
   "pre-amx-config",   "lower-amx-intrinsics",
-  "polyhedral-info",  "replace-with-veclib"};
+  "polyhedral-info",  "replace-with-veclib",
+  "jmc-instrument"};
   for (const auto &P : PassNamePrefix)
 if (Pass.startswith(P))
   return true;
@@ -572,6 +573,7 @@
   initializeHardwareLoopsPass(Registry);
   initializeTypePromotionPass(Registry);
   initializeReplaceWithVeclibLegacyPass(Registry);
+  initializeJMCInstrumenterPass(Registry);
 
 #ifdef BUILD_EXAMPLES
   initializeExampleIRTransforms(Registry);
Index: llvm/test/Instrumentation/JustMyCode/jmc-instrument.ll
===
--- /dev/null
+++ llvm/test/Instrumentation/JustMyCode/jmc-instrument.ll
@@ -0,0 +1,120 @@
+; RUN: opt -jmc-instrument -mtriple=x86_64-pc-windows-msvc  -S < %s | FileCheck %s
+; RUN: opt -jmc-instrument -mtriple=aarch64-pc-windows-msvc -S < %s | FileCheck %s
+; RUN: opt -jmc-instrument -mtriple=arm-pc-windows-msvc -S < %s | FileCheck %s
+
+; CHECK: $__JustMyCode_Default = comdat any
+
+; CHECK: @"__7DF23CF5_x@c" = internal unnamed_addr global i8 1, section ".msvcjmc", align 1, !dbg !0
+; CHECK: @"__A85D9D03_x@c" = internal unnamed_addr global i8 1, section ".msvcjmc", align 1, !dbg !5
+; CHECK: @llvm.used = appending global [1 x i8*] [i8* bitcast (void (i8*)* @__JustMyCode_Default to i8*)], section "llvm.metadata"
+
+; CHECK: define void @l1() !dbg !13 {
+; CHECK:   call void @__CheckForDebuggerJustMyCode(i8* noundef @"__7DF23CF5_x@c")
+; CHECK:   ret void
+; CHECK: }
+
+; CHECK: define void @l2() !dbg !17 {
+; CHECK:   call void @__CheckForDebuggerJustMyCode(i8* noundef @"__7DF23CF5_x@c")
+; CHECK:   ret void
+; CHECK: }
+
+; CHECK: define void @w1() !dbg !19 {
+; CHECK:   call void @__CheckForDebuggerJustMyCode(i8* noundef @"__A85D9D03_x@c")
+; CHECK:   ret void
+; CHECK: }
+
+; CHECK: define void @w2() !dbg !20 {
+; CHECK:   call void @__CheckForDebuggerJustMyCode(i8* noundef @"__A85D9D03_x@c")
+; CHECK:   ret void
+; CHECK: }
+
+; CHECK: define void @w3() !dbg !22 {
+; CHECK:   call void @__CheckForDebuggerJustMyCode(i8* noundef @"__A85D9D03_x@c")
+; CHECK:   ret void
+; CHECK: }
+
+; CHECK: define void @w4() !dbg !24 {
+; CHECK:   call void @__CheckForDebuggerJustMyCode(i8* noundef @"__A85D9D03_x@c")
+; CHECK:   ret void
+; CHECK: }
+
+; CHECK: declare void @__CheckForDebuggerJustMyCode(i8* noundef) unnamed_addr
+
+; CHECK: define void @__JustMyCode_Default(i8* noundef %0) unnamed_addr comdat {
+; CHECK:   ret void
+; CHECK: }
+
+; CHECK: !llvm.linker.options = !{!12}
+
+; CHECK: !0 = !DIGlobalVariableExpression(var: !1, expr: !DIExpression())
+; CHECK: !1 = distinct !DIGlobalVariable(name: "__7DF23CF5_x@c", scope: !2, file: !3, type: !8, isLocal: true, isDefinition: true)
+; CHECK: !2 = distinct !DICompileUnit(language: DW_LANG_C99, file: !3, producer: "clang", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, globals: !4, splitDebugInlining: false, nameTableKind: None)
+; CHECK: !3 = !DIFile(filename: "a/x.c", directory: "/tmp")
+; CHECK: !4 = !{!0, !5}
+; CHECK: !5 = !DIGlobalVariableExpression(var: !6, expr: !DIExpression())
+; CHECK: !6 = distinct !DIGlobalVariable(name: "__A85D9D03_x@c", scope: !2, file: !7, type: !8, isLocal: true, isDefinition: true)
+; CHECK: !7 = !DIFile(filename: "./x.c", directory: "C:ab")
+; CHECK: !8 = !DIBasicType(name: "unsigned char", size: 8, encoding: DW_ATE_unsigned_char, flags: DIFlagArtificial)
+; CHECK: !9 = !{i32 2, !"CodeView", i32 1}
+; CHECK: !10 = 

[PATCH] D115283: [AMDGPU] Set "amdgpu_hostcall" module flag if an AMDGPU function has calls to device lib functions that use hostcalls.

2022-01-31 Thread Sameer Sahasrabuddhe via Phabricator via cfe-commits
sameerds added inline comments.



Comment at: clang/lib/CodeGen/TargetInfo.cpp:9434
+  if (Callee &&
+  (Callee->getName() == "__ockl_call_host_function" ||
+   Callee->getName() == "__ockl_fprintf_stderr_begin")) {

Just to confirm what others have probably disovered, the only function whose 
presence should be checked is ``__ockl_hostcall_internal``. All others are 
wrappers that are free to disappear during optimization.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D115283/new/

https://reviews.llvm.org/D115283

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D118198: [OpenMP] Remove call to 'clang-offload-wrapper' binary

2022-01-31 Thread Joseph Huber via Phabricator via cfe-commits
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rG19fac745e322: [OpenMP] Remove call to 
'clang-offload-wrapper' binary (authored by jhuber6).

Changed prior to commit:
  https://reviews.llvm.org/D118198?vs=403043&id=404797#toc

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D118198/new/

https://reviews.llvm.org/D118198

Files:
  clang/tools/clang-linker-wrapper/CMakeLists.txt
  clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
  clang/tools/clang-linker-wrapper/OffloadWrapper.cpp
  clang/tools/clang-linker-wrapper/OffloadWrapper.h

Index: clang/tools/clang-linker-wrapper/OffloadWrapper.h
===
--- /dev/null
+++ clang/tools/clang-linker-wrapper/OffloadWrapper.h
@@ -0,0 +1,20 @@
+//===- OffloadWrapper.h ---*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#ifndef LLVM_CLANG_TOOLS_CLANG_LINKER_WRAPPER_OFFLOAD_WRAPPER_H
+#define LLVM_CLANG_TOOLS_CLANG_LINKER_WRAPPER_OFFLOAD_WRAPPER_H
+
+#include "llvm/ADT/ArrayRef.h"
+#include "llvm/IR/Module.h"
+
+/// Wrap the input device images into the module \p M as global symbols and
+/// registers the images with the OpenMP Offloading runtime libomptarget.
+llvm::Error wrapBinaries(llvm::Module &M,
+ llvm::ArrayRef> Images);
+
+#endif
Index: clang/tools/clang-linker-wrapper/OffloadWrapper.cpp
===
--- /dev/null
+++ clang/tools/clang-linker-wrapper/OffloadWrapper.cpp
@@ -0,0 +1,267 @@
+//===- OffloadWrapper.cpp ---*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "OffloadWrapper.h"
+#include "llvm/ADT/ArrayRef.h"
+#include "llvm/ADT/Triple.h"
+#include "llvm/IR/Constants.h"
+#include "llvm/IR/GlobalVariable.h"
+#include "llvm/IR/IRBuilder.h"
+#include "llvm/IR/LLVMContext.h"
+#include "llvm/IR/Module.h"
+#include "llvm/Transforms/Utils/ModuleUtils.h"
+
+using namespace llvm;
+
+namespace {
+
+IntegerType *getSizeTTy(Module &M) {
+  LLVMContext &C = M.getContext();
+  switch (M.getDataLayout().getPointerTypeSize(Type::getInt8PtrTy(C))) {
+  case 4u:
+return Type::getInt32Ty(C);
+  case 8u:
+return Type::getInt64Ty(C);
+  }
+  llvm_unreachable("unsupported pointer type size");
+}
+
+// struct __tgt_offload_entry {
+//   void *addr;
+//   char *name;
+//   size_t size;
+//   int32_t flags;
+//   int32_t reserved;
+// };
+StructType *getEntryTy(Module &M) {
+  LLVMContext &C = M.getContext();
+  StructType *EntryTy = StructType::getTypeByName(C, "__tgt_offload_entry");
+  if (!EntryTy)
+EntryTy = StructType::create("__tgt_offload_entry", Type::getInt8PtrTy(C),
+ Type::getInt8PtrTy(C), getSizeTTy(M),
+ Type::getInt32Ty(C), Type::getInt32Ty(C));
+  return EntryTy;
+}
+
+PointerType *getEntryPtrTy(Module &M) {
+  return PointerType::getUnqual(getEntryTy(M));
+}
+
+// struct __tgt_device_image {
+//   void *ImageStart;
+//   void *ImageEnd;
+//   __tgt_offload_entry *EntriesBegin;
+//   __tgt_offload_entry *EntriesEnd;
+// };
+StructType *getDeviceImageTy(Module &M) {
+  LLVMContext &C = M.getContext();
+  StructType *ImageTy = StructType::getTypeByName(C, "__tgt_device_image");
+  if (!ImageTy)
+ImageTy = StructType::create("__tgt_device_image", Type::getInt8PtrTy(C),
+ Type::getInt8PtrTy(C), getEntryPtrTy(M),
+ getEntryPtrTy(M));
+  return ImageTy;
+}
+
+PointerType *getDeviceImagePtrTy(Module &M) {
+  return PointerType::getUnqual(getDeviceImageTy(M));
+}
+
+// struct __tgt_bin_desc {
+//   int32_t NumDeviceImages;
+//   __tgt_device_image *DeviceImages;
+//   __tgt_offload_entry *HostEntriesBegin;
+//   __tgt_offload_entry *HostEntriesEnd;
+// };
+StructType *getBinDescTy(Module &M) {
+  LLVMContext &C = M.getContext();
+  StructType *DescTy = StructType::getTypeByName(C, "__tgt_bin_desc");
+  if (!DescTy)
+DescTy = StructType::create("__tgt_bin_desc", Type::getInt32Ty(C),
+getDeviceImagePtrTy(M), getEntryPtrTy(M),
+getEntryPtrTy(M));
+  return DescTy;
+}
+
+PointerType *getBinDescPtrTy(Module &M) {
+  return PointerType::getUnqual(getBinDescTy(M));
+}
+
+/// Creates binar

[PATCH] D118197: [OpenMP] Replace sysmtem call to `llc` with target machine

2022-01-31 Thread Joseph Huber via Phabricator via cfe-commits
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rGeb6ddf288cd0: [OpenMP] Replace sysmtem call to `llc` with 
target machine (authored by jhuber6).

Changed prior to commit:
  https://reviews.llvm.org/D118197?vs=403042&id=404796#toc

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D118197/new/

https://reviews.llvm.org/D118197

Files:
  clang/tools/clang-linker-wrapper/CMakeLists.txt
  clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp

Index: clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
===
--- clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
+++ clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
@@ -23,6 +23,7 @@
 #include "llvm/IR/Module.h"
 #include "llvm/IRReader/IRReader.h"
 #include "llvm/LTO/LTO.h"
+#include "llvm/MC/TargetRegistry.h"
 #include "llvm/Object/Archive.h"
 #include "llvm/Object/ArchiveWriter.h"
 #include "llvm/Object/Binary.h"
@@ -42,6 +43,7 @@
 #include "llvm/Support/TargetSelect.h"
 #include "llvm/Support/WithColor.h"
 #include "llvm/Support/raw_ostream.h"
+#include "llvm/Target/TargetMachine.h"
 
 using namespace llvm;
 using namespace llvm::object;
@@ -958,6 +960,49 @@
   return Error::success();
 }
 
+// Compile the module to an object file using the appropriate target machine for
+// the host triple.
+Expected compileModule(Module &M) {
+  if (M.getTargetTriple().empty())
+M.setTargetTriple(HostTriple);
+
+  std::string Msg;
+  const Target *T = TargetRegistry::lookupTarget(M.getTargetTriple(), Msg);
+  if (!T)
+return createStringError(inconvertibleErrorCode(), Msg);
+
+  auto Options =
+  codegen::InitTargetOptionsFromCodeGenFlags(Triple(M.getTargetTriple()));
+  StringRef CPU = "";
+  StringRef Features = "";
+  std::unique_ptr TM(T->createTargetMachine(
+  HostTriple, CPU, Features, Options, Reloc::PIC_, M.getCodeModel()));
+
+  if (M.getDataLayout().isDefault())
+M.setDataLayout(TM->createDataLayout());
+
+  SmallString<128> ObjectFile;
+  int FD = -1;
+  if (Error Err = createOutputFile(sys::path::filename(ExecutableName) +
+   "offload-wrapper",
+   "o", ObjectFile))
+return std::move(Err);
+  if (std::error_code EC = sys::fs::openFileForWrite(ObjectFile, FD))
+return errorCodeToError(EC);
+
+  auto OS = std::make_unique(FD, true);
+
+  legacy::PassManager CodeGenPasses;
+  TargetLibraryInfoImpl TLII(Triple(M.getTargetTriple()));
+  CodeGenPasses.add(new TargetLibraryInfoWrapperPass(TLII));
+  if (TM->addPassesToEmitFile(CodeGenPasses, *OS, nullptr, CGFT_ObjectFile))
+return createStringError(inconvertibleErrorCode(),
+ "Failed to execute host backend");
+  CodeGenPasses.run(M);
+
+  return static_cast(ObjectFile);
+}
+
 /// Creates an object file containing the device image stored in the filename \p
 /// ImageFile that can be linked with the host.
 Expected wrapDeviceImage(StringRef ImageFile) {
@@ -987,30 +1032,11 @@
 return createStringError(inconvertibleErrorCode(),
  "'clang-offload-wrapper' failed");
 
-  ErrorOr CompilerPath = sys::findProgramByName("llc");
-  if (!WrapperPath)
-return createStringError(WrapperPath.getError(),
- "Unable to find 'llc' in path");
-
-  // Create a new file to write the wrapped bitcode file to.
-  SmallString<128> ObjectFile;
-  if (Error Err = createOutputFile(sys::path::filename(ExecutableName) +
-   "-offload-wrapper",
-   "o", ObjectFile))
-return std::move(Err);
-
-  SmallVector CompilerArgs;
-  CompilerArgs.push_back(*CompilerPath);
-  CompilerArgs.push_back("--filetype=obj");
-  CompilerArgs.push_back("--relocation-model=pic");
-  CompilerArgs.push_back("-o");
-  CompilerArgs.push_back(ObjectFile);
-  CompilerArgs.push_back(BitcodeFile);
-
-  if (sys::ExecuteAndWait(*CompilerPath, CompilerArgs))
-return createStringError(inconvertibleErrorCode(), "'llc' failed");
+  LLVMContext Context;
+  SMDiagnostic Err;
+  std::unique_ptr M = parseIRFile(BitcodeFile, Err, Context);
 
-  return static_cast(ObjectFile);
+  return compileModule(*M);
 }
 
 Optional findFile(StringRef Dir, const Twine &Name) {
Index: clang/tools/clang-linker-wrapper/CMakeLists.txt
===
--- clang/tools/clang-linker-wrapper/CMakeLists.txt
+++ clang/tools/clang-linker-wrapper/CMakeLists.txt
@@ -4,6 +4,8 @@
   Core
   BinaryFormat
   MC
+  Target
+  Analysis
   Passes
   IRReader
   Object
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D118155: [OpenMP] Improve symbol resolution for OpenMP Offloading LTO

2022-01-31 Thread Joseph Huber via Phabricator via cfe-commits
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rG46d019041cd9: [OpenMP] Improve symbol resolution for OpenMP 
Offloading LTO (authored by jhuber6).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D118155/new/

https://reviews.llvm.org/D118155

Files:
  clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp

Index: clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
===
--- clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
+++ clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
@@ -736,6 +736,7 @@
   SmallVector, 4> BitcodeFiles;
   SmallVector NewInputFiles;
   StringMap UsedInRegularObj;
+  StringMap UsedInSharedLib;
 
   // Search for bitcode files in the input and create an LTO input file. If it
   // is not a bitcode file, scan its symbol table for symbols we need to
@@ -759,7 +760,11 @@
 if (!Name)
   return Name.takeError();
 
-UsedInRegularObj[*Name] = true;
+// Record if we've seen these symbols in any object or shared libraries.
+if ((*ObjFile)->isRelocatableObject()) {
+  UsedInRegularObj[*Name] = true;
+} else
+  UsedInSharedLib[*Name] = true;
   }
 } else {
   Expected> InputFileOrErr =
@@ -767,6 +772,7 @@
   if (!InputFileOrErr)
 return InputFileOrErr.takeError();
 
+  // Save the input file and the buffer associated with its memory.
   BitcodeFiles.push_back(std::move(*InputFileOrErr));
   SavedBuffers.push_back(std::move(*BufferOrErr));
 }
@@ -797,22 +803,16 @@
 return false;
   };
 
-  // We have visibility of the whole program if every input is bitcode, all
-  // inputs are statically linked so there should be no external references.
+  // We assume visibility of the whole program if every input file was bitcode.
   bool WholeProgram = BitcodeFiles.size() == InputFiles.size();
   auto LTOBackend = (EmbedBC)
 ? createLTO(TheTriple, Arch, WholeProgram, LinkOnly)
 : createLTO(TheTriple, Arch, WholeProgram);
 
-  // TODO: Run more tests to verify that this is correct.
-  // Create the LTO instance with the necessary config and add the bitcode files
-  // to it after resolving symbols. We make a few assumptions about symbol
-  // resolution.
-  // 1. The target is going to be a stand-alone executable file.
-  // 2. We do not support relocatable object files.
-  // 3. All inputs are relocatable object files extracted from host binaries, so
-  //there is no resolution to a dynamic library.
-  StringMap PrevailingSymbols;
+  // We need to resolve the symbols so the LTO backend knows which symbols need
+  // to be kept or can be internalized. This is a simplified symbol resolution
+  // scheme to approximate the full resolution a linker would do.
+  DenseSet PrevailingSymbols;
   for (auto &BitcodeFile : BitcodeFiles) {
 const auto Symbols = BitcodeFile->symbols();
 SmallVector Resolutions(Symbols.size());
@@ -821,35 +821,43 @@
   lto::SymbolResolution &Res = Resolutions[Idx++];
 
   // We will use this as the prevailing symbol definition in LTO unless
-  // it is undefined in the module or another symbol has already been used.
-  Res.Prevailing = !Sym.isUndefined() && !PrevailingSymbols[Sym.getName()];
-
-  // We need LTO to preserve symbols referenced in other object files, or
-  // are needed by the rest of the toolchain.
+  // it is undefined or another definition has already been used.
+  Res.Prevailing =
+  !Sym.isUndefined() && PrevailingSymbols.insert(Sym.getName()).second;
+
+  // We need LTO to preseve the following global symbols:
+  // 1) Symbols used in regular objects.
+  // 2) Sections that will be given a __start/__stop symbol.
+  // 3) Prevailing symbols that are needed visibile to external libraries.
   Res.VisibleToRegularObj =
   UsedInRegularObj[Sym.getName()] ||
   isValidCIdentifier(Sym.getSectionName()) ||
-  (Res.Prevailing && Sym.getName().startswith("__omp"));
-
-  // We do not currently support shared libraries, so no symbols will be
-  // referenced externally by shared libraries.
-  Res.ExportDynamic = false;
-
-  // The result will currently always be an executable, so the only time the
-  // definition will not reside in this link unit is if it's undefined.
-  Res.FinalDefinitionInLinkageUnit = !Sym.isUndefined();
+  (Res.Prevailing &&
+   (Sym.getVisibility() != GlobalValue::HiddenVisibility &&
+!Sym.canBeOmittedFromSymbolTable()));
+
+  // Identify symbols that must be exported dynamically and can be
+  // referenced by other files.
+  Res.ExportDynamic =
+  Sym.getVisibility() != GlobalValue::HiddenVisibility &&
+  (Us

[PATCH] D117246: [OpenMP] Add support for linking AMDGPU images

2022-01-31 Thread Joseph Huber via Phabricator via cfe-commits
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rGce16ca3c7419: [OpenMP] Add support for linking AMDGPU images 
(authored by jhuber6).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D117246/new/

https://reviews.llvm.org/D117246

Files:
  clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp


Index: clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
===
--- clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
+++ clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
@@ -501,7 +501,7 @@
   // Create a new file to write the linked device image to.
   SmallString<128> TempFile;
   if (std::error_code EC = sys::fs::createTemporaryFile(
-  TheTriple.getArchName() + "-" + Arch, "cubin", TempFile))
+  "lto-" + TheTriple.getArchName() + "-" + Arch, "cubin", TempFile))
 return createFileError(TempFile, EC);
   TempFiles.push_back(static_cast(TempFile));
 
@@ -576,6 +576,50 @@
   return static_cast(TempFile);
 }
 } // namespace nvptx
+namespace amdgcn {
+Expected link(ArrayRef InputFiles,
+   ArrayRef LinkerArgs, Triple TheTriple,
+   StringRef Arch) {
+  // AMDGPU uses the lld binary to link device object files.
+  ErrorOr LLDPath =
+  sys::findProgramByName("lld", sys::path::parent_path(LinkerExecutable));
+  if (!LLDPath)
+LLDPath = sys::findProgramByName("lld");
+  if (!LLDPath)
+return createStringError(LLDPath.getError(),
+ "Unable to find 'lld' in path");
+
+  // Create a new file to write the linked device image to.
+  SmallString<128> TempFile;
+  if (std::error_code EC = sys::fs::createTemporaryFile(
+  TheTriple.getArchName() + "-" + Arch + "-image", "out", TempFile))
+return createFileError(TempFile, EC);
+  TempFiles.push_back(static_cast(TempFile));
+
+  SmallVector CmdArgs;
+  CmdArgs.push_back(*LLDPath);
+  CmdArgs.push_back("-flavor");
+  CmdArgs.push_back("gnu");
+  CmdArgs.push_back("--no-undefined");
+  CmdArgs.push_back("-shared");
+  CmdArgs.push_back("-o");
+  CmdArgs.push_back(TempFile);
+
+  // Copy system library paths used by the host linker.
+  for (StringRef Arg : LinkerArgs)
+if (Arg.startswith("-L"))
+  CmdArgs.push_back(Arg);
+
+  // Add extracted input files.
+  for (StringRef Input : InputFiles)
+CmdArgs.push_back(Input);
+
+  if (sys::ExecuteAndWait(*LLDPath, CmdArgs))
+return createStringError(inconvertibleErrorCode(), "'lld' failed");
+
+  return static_cast(TempFile);
+}
+} // namespace amdgcn
 
 Expected linkDevice(ArrayRef InputFiles,
  ArrayRef LinkerArgs,
@@ -585,7 +629,7 @@
   case Triple::nvptx64:
 return nvptx::link(InputFiles, LinkerArgs, TheTriple, Arch);
   case Triple::amdgcn:
-// TODO: AMDGCN linking support.
+return amdgcn::link(InputFiles, LinkerArgs, TheTriple, Arch);
   case Triple::x86:
   case Triple::x86_64:
 // TODO: x86 linking support.


Index: clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
===
--- clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
+++ clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
@@ -501,7 +501,7 @@
   // Create a new file to write the linked device image to.
   SmallString<128> TempFile;
   if (std::error_code EC = sys::fs::createTemporaryFile(
-  TheTriple.getArchName() + "-" + Arch, "cubin", TempFile))
+  "lto-" + TheTriple.getArchName() + "-" + Arch, "cubin", TempFile))
 return createFileError(TempFile, EC);
   TempFiles.push_back(static_cast(TempFile));
 
@@ -576,6 +576,50 @@
   return static_cast(TempFile);
 }
 } // namespace nvptx
+namespace amdgcn {
+Expected link(ArrayRef InputFiles,
+   ArrayRef LinkerArgs, Triple TheTriple,
+   StringRef Arch) {
+  // AMDGPU uses the lld binary to link device object files.
+  ErrorOr LLDPath =
+  sys::findProgramByName("lld", sys::path::parent_path(LinkerExecutable));
+  if (!LLDPath)
+LLDPath = sys::findProgramByName("lld");
+  if (!LLDPath)
+return createStringError(LLDPath.getError(),
+ "Unable to find 'lld' in path");
+
+  // Create a new file to write the linked device image to.
+  SmallString<128> TempFile;
+  if (std::error_code EC = sys::fs::createTemporaryFile(
+  TheTriple.getArchName() + "-" + Arch + "-image", "out", TempFile))
+return createFileError(TempFile, EC);
+  TempFiles.push_back(static_cast(TempFile));
+
+  SmallVector CmdArgs;
+  CmdArgs.push_back(*LLDPath);
+  CmdArgs.push_back("-flavor");
+  CmdArgs.push_back("gnu");
+  CmdArgs.push_back("--no-undefined");
+  CmdArgs.push_back("-shared");
+  CmdArgs.push_back("-o");
+  CmdArgs.push_back(TempFile);
+
+  // Copy system li

[PATCH] D117156: [OpenMP] Add extra flag handling to linker wrapper

2022-01-31 Thread Joseph Huber via Phabricator via cfe-commits
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
jhuber6 marked an inline comment as done.
Closed by commit rGcb7cfaec7185: [OpenMP] Add extra flag handling to linker 
wrapper (authored by jhuber6).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D117156/new/

https://reviews.llvm.org/D117156

Files:
  clang/lib/Driver/ToolChains/Clang.cpp
  clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp

Index: clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
===
--- clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
+++ clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
@@ -48,6 +48,12 @@
 
 static cl::opt Help("h", cl::desc("Alias for -help"), cl::Hidden);
 
+enum DebugKind {
+  NoDebugInfo,
+  DirectivesOnly,
+  FullDebugInfo,
+};
+
 // Mark all our options with this category, everything else (except for -help)
 // will be hidden.
 static cl::OptionCategory
@@ -58,29 +64,53 @@
 cl::desc("Strip offloading sections from the host object file."),
 cl::init(true), cl::cat(ClangLinkerWrapperCategory));
 
-static cl::opt LinkerUserPath("linker-path",
+static cl::opt LinkerUserPath("linker-path", cl::Required,
cl::desc("Path of linker binary"),
cl::cat(ClangLinkerWrapperCategory));
 
 static cl::opt
-TargetFeatures("target-feature", cl::desc("Target features for triple"),
+TargetFeatures("target-feature", cl::ZeroOrMore,
+   cl::desc("Target features for triple"),
cl::cat(ClangLinkerWrapperCategory));
 
-static cl::opt OptLevel("opt-level",
+static cl::opt OptLevel("opt-level", cl::ZeroOrMore,
  cl::desc("Optimization level for LTO"),
  cl::init("O2"),
  cl::cat(ClangLinkerWrapperCategory));
 
 static cl::opt
-BitcodeLibrary("target-library",
+BitcodeLibrary("target-library", cl::ZeroOrMore,
cl::desc("Path for the target bitcode library"),
cl::cat(ClangLinkerWrapperCategory));
 
 static cl::opt EmbedBC(
 "target-embed-bc", cl::ZeroOrMore,
-cl::desc("Embed linked bitcode instead of an executable device image."),
+cl::desc("Embed linked bitcode instead of an executable device image"),
 cl::init(false), cl::cat(ClangLinkerWrapperCategory));
 
+static cl::opt
+HostTriple("host-triple", cl::ZeroOrMore,
+   cl::desc("Triple to use for the host compilation"),
+   cl::init(sys::getDefaultTargetTriple()),
+   cl::cat(ClangLinkerWrapperCategory));
+
+static cl::opt
+PtxasOption("ptxas-option", cl::ZeroOrMore,
+cl::desc("Argument to pass to the ptxas invocation"),
+cl::cat(ClangLinkerWrapperCategory));
+
+static cl::opt Verbose("v", cl::ZeroOrMore,
+ cl::desc("Verbose output from tools"),
+ cl::init(false),
+ cl::cat(ClangLinkerWrapperCategory));
+
+static cl::opt DebugInfo(
+cl::desc("Choose debugging level:"), cl::init(NoDebugInfo),
+cl::values(clEnumValN(NoDebugInfo, "g0", "No debug information"),
+   clEnumValN(DirectivesOnly, "gline-directives-only",
+  "Direction information"),
+   clEnumValN(FullDebugInfo, "g", "Full debugging support")));
+
 // Do not parse linker options.
 static cl::list
 HostLinkerArgs(cl::Positional,
@@ -480,6 +510,14 @@
   std::string Opt = "-" + OptLevel;
   CmdArgs.push_back(*PtxasPath);
   CmdArgs.push_back(TheTriple.isArch64Bit() ? "-m64" : "-m32");
+  if (Verbose)
+CmdArgs.push_back("-v");
+  if (DebugInfo == DirectivesOnly && OptLevel[1] == '0')
+CmdArgs.push_back("-lineinfo");
+  else if (DebugInfo == FullDebugInfo && OptLevel[1] == '0')
+CmdArgs.push_back("-g");
+  if (!PtxasOption.empty())
+CmdArgs.push_back(PtxasOption);
   CmdArgs.push_back("-o");
   CmdArgs.push_back(TempFile);
   CmdArgs.push_back(Opt);
@@ -511,10 +549,13 @@
 return createFileError(TempFile, EC);
   TempFiles.push_back(static_cast(TempFile));
 
-  // TODO: Pass in arguments like `-g` and `-v` from the driver.
   SmallVector CmdArgs;
   CmdArgs.push_back(*NvlinkPath);
   CmdArgs.push_back(TheTriple.isArch64Bit() ? "-m64" : "-m32");
+  if (Verbose)
+CmdArgs.push_back("-v");
+  if (DebugInfo != NoDebugInfo)
+CmdArgs.push_back("-g");
   CmdArgs.push_back("-o");
   CmdArgs.push_back(TempFile);
   CmdArgs.push_back("-arch");
@@ -563,16 +604,16 @@
 
   switch (DI.getSeverity()) {
   case DS_Error:
-WithColor::error(errs(), LinkerExecutable) << ErrStorage;
+WithColor::error(errs(), LinkerExecutable) << ErrStorage << "\n";
 break;
   case DS_Warning:
-WithColor

[PATCH] D117049: [OpenMP] Add support for embedding bitcode images in wrapper tool

2022-01-31 Thread Joseph Huber via Phabricator via cfe-commits
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rGf28c3153ee6d: [OpenMP] Add support for embedding bitcode 
images in wrapper tool (authored by jhuber6).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D117049/new/

https://reviews.llvm.org/D117049

Files:
  clang/lib/Driver/ToolChains/Clang.cpp
  clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp

Index: clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
===
--- clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
+++ clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
@@ -76,12 +76,18 @@
cl::desc("Path for the target bitcode library"),
cl::cat(ClangLinkerWrapperCategory));
 
+static cl::opt EmbedBC(
+"target-embed-bc", cl::ZeroOrMore,
+cl::desc("Embed linked bitcode instead of an executable device image."),
+cl::init(false), cl::cat(ClangLinkerWrapperCategory));
+
 // Do not parse linker options.
 static cl::list
-HostLinkerArgs(cl::Sink, cl::desc("..."));
+HostLinkerArgs(cl::Positional,
+   cl::desc("..."));
 
 /// Path of the current binary.
-static std::string LinkerExecutable;
+static const char *LinkerExecutable;
 
 /// Temporary files created by the linker wrapper.
 static SmallVector TempFiles;
@@ -411,8 +417,8 @@
 
   std::unique_ptr Buffer =
   MemoryBuffer::getMemBuffer(Library.getMemoryBufferRef(), false);
-  if (Error Err = writeArchive(TempFile, Members, true, Library.kind(),
-true, Library.isThin(), std::move(Buffer)))
+  if (Error Err = writeArchive(TempFile, Members, true, Library.kind(), true,
+   Library.isThin(), std::move(Buffer)))
 return std::move(Err);
 
   return static_cast(TempFile);
@@ -489,7 +495,7 @@
   return static_cast(TempFile);
 }
 
-Expected link(ArrayRef InputFiles,
+Expected link(ArrayRef InputFiles,
ArrayRef LinkerArgs, Triple TheTriple,
StringRef Arch) {
   // NVPTX uses the nvlink binary to link device object files.
@@ -520,7 +526,7 @@
   CmdArgs.push_back(Arg);
 
   // Add extracted input files.
-  for (auto Input : InputFiles)
+  for (StringRef Input : InputFiles)
 CmdArgs.push_back(Input);
 
   if (sys::ExecuteAndWait(*NvlinkPath, CmdArgs))
@@ -530,7 +536,7 @@
 }
 } // namespace nvptx
 
-Expected linkDevice(ArrayRef InputFiles,
+Expected linkDevice(ArrayRef InputFiles,
  ArrayRef LinkerArgs,
  Triple TheTriple, StringRef Arch) {
   switch (TheTriple.getArch()) {
@@ -597,8 +603,10 @@
   llvm_unreachable("Invalid optimization level");
 }
 
-std::unique_ptr createLTO(const Triple &TheTriple, StringRef Arch,
-bool WholeProgram) {
+template >
+std::unique_ptr createLTO(
+const Triple &TheTriple, StringRef Arch, bool WholeProgram,
+ModuleHook Hook = [](size_t, const Module &) { return true; }) {
   lto::Config Conf;
   lto::ThinBackend Backend;
   // TODO: Handle index-only thin-LTO
@@ -617,7 +625,7 @@
   Conf.PTO.LoopVectorization = Conf.OptLevel > 1;
   Conf.PTO.SLPVectorization = Conf.OptLevel > 1;
 
-  // TODO: Handle outputting bitcode using a module hook.
+  Conf.PostInternalizeModuleHook = Hook;
   if (TheTriple.isNVPTX())
 Conf.CGFileType = CGFT_AssemblyFile;
   else
@@ -637,11 +645,11 @@
  [](char C) { return C == '_' || isAlnum(C); });
 }
 
-Expected> linkBitcodeFiles(ArrayRef InputFiles,
- const Triple &TheTriple,
- StringRef Arch) {
+Error linkBitcodeFiles(SmallVectorImpl &InputFiles,
+   const Triple &TheTriple, StringRef Arch) {
   SmallVector, 4> SavedBuffers;
   SmallVector, 4> BitcodeFiles;
+  SmallVector NewInputFiles;
   StringMap UsedInRegularObj;
 
   // Search for bitcode files in the input and create an LTO input file. If it
@@ -660,6 +668,7 @@
   if (!ObjFile)
 return ObjFile.takeError();
 
+  NewInputFiles.push_back(File.str());
   for (auto &Sym : (*ObjFile)->symbols()) {
 Expected Name = Sym.getName();
 if (!Name)
@@ -679,12 +688,36 @@
   }
 
   if (BitcodeFiles.empty())
-return None;
+return Error::success();
+
+  auto HandleError = [&](std::error_code EC) {
+logAllUnhandledErrors(errorCodeToError(EC),
+  WithColor::error(errs(), LinkerExecutable));
+exit(1);
+  };
+
+  // LTO Module hook to output bitcode without running the backend.
+  auto LinkOnly = [&](size_t Task, const Module &M) {
+SmallString<128> TempFile;
+if (std::error_code EC = sys::fs::createTemporaryFile(
+"jit-" + TheTriple.getTriple(), "bc", TempFile))
+   

[clang] 19fac74 - [OpenMP] Remove call to 'clang-offload-wrapper' binary

2022-01-31 Thread Joseph Huber via cfe-commits

Author: Joseph Huber
Date: 2022-01-31T23:11:43-05:00
New Revision: 19fac745e3226fbe9db4d3eab106fdaf19d9019d

URL: 
https://github.com/llvm/llvm-project/commit/19fac745e3226fbe9db4d3eab106fdaf19d9019d
DIFF: 
https://github.com/llvm/llvm-project/commit/19fac745e3226fbe9db4d3eab106fdaf19d9019d.diff

LOG: [OpenMP] Remove call to 'clang-offload-wrapper' binary

Summary:
This patch removes the system call to the `clang-offload-wrapper` tool
by replicating its functionality in a new file. This improves
performance and makes the future wrapping functionality easier to
change.

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

Added: 
clang/tools/clang-linker-wrapper/OffloadWrapper.cpp
clang/tools/clang-linker-wrapper/OffloadWrapper.h

Modified: 
clang/tools/clang-linker-wrapper/CMakeLists.txt
clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp

Removed: 




diff  --git a/clang/tools/clang-linker-wrapper/CMakeLists.txt 
b/clang/tools/clang-linker-wrapper/CMakeLists.txt
index 5f08a443b5e9..1614f40fb60e 100644
--- a/clang/tools/clang-linker-wrapper/CMakeLists.txt
+++ b/clang/tools/clang-linker-wrapper/CMakeLists.txt
@@ -5,6 +5,7 @@ set(LLVM_LINK_COMPONENTS
   BinaryFormat
   MC
   Target
+  TransformUtils
   Analysis
   Passes
   IRReader
@@ -19,6 +20,7 @@ endif()
 
 add_clang_executable(clang-linker-wrapper
   ClangLinkerWrapper.cpp
+  OffloadWrapper.cpp
 
   DEPENDS
   ${tablegen_deps}

diff  --git a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp 
b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
index 6dc29767183d..4ec4d6b13404 100644
--- a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
+++ b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
@@ -14,6 +14,7 @@
 //
 //===-===//
 
+#include "OffloadWrapper.h"
 #include "clang/Basic/Version.h"
 #include "llvm/BinaryFormat/Magic.h"
 #include "llvm/Bitcode/BitcodeWriter.h"
@@ -963,9 +964,6 @@ Error linkDeviceFiles(ArrayRef DeviceFiles,
 // Compile the module to an object file using the appropriate target machine 
for
 // the host triple.
 Expected compileModule(Module &M) {
-  if (M.getTargetTriple().empty())
-M.setTargetTriple(HostTriple);
-
   std::string Msg;
   const Target *T = TargetRegistry::lookupTarget(M.getTargetTriple(), Msg);
   if (!T)
@@ -1003,40 +1001,29 @@ Expected compileModule(Module &M) {
   return static_cast(ObjectFile);
 }
 
-/// Creates an object file containing the device image stored in the filename 
\p
-/// ImageFile that can be linked with the host.
-Expected wrapDeviceImage(StringRef ImageFile) {
-  // TODO: Call these utilities as a library intead of executing them here.
-  ErrorOr WrapperPath =
-  sys::findProgramByName("clang-offload-wrapper");
-  if (!WrapperPath)
-return createStringError(WrapperPath.getError(),
- "Unable to find 'clang-offload-wrapper' in path");
-
-  // Create a new file to write the wrapped bitcode file to.
-  SmallString<128> BitcodeFile;
-  if (Error Err = createOutputFile(sys::path::filename(ExecutableName) +
-   "-offload-wrapper",
-   "bc", BitcodeFile))
-return std::move(Err);
-
-  SmallVector WrapperArgs;
-  WrapperArgs.push_back(*WrapperPath);
-  WrapperArgs.push_back("-target");
-  WrapperArgs.push_back(HostTriple);
-  WrapperArgs.push_back("-o");
-  WrapperArgs.push_back(BitcodeFile);
-  WrapperArgs.push_back(ImageFile);
-
-  if (sys::ExecuteAndWait(*WrapperPath, WrapperArgs))
-return createStringError(inconvertibleErrorCode(),
- "'clang-offload-wrapper' failed");
+/// Creates the object file containing the device image and runtime 
registration
+/// code from the device images stored in \p Images.
+Expected wrapDeviceImages(ArrayRef Images) {
+  SmallVector, 4> SavedBuffers;
+  SmallVector, 4> ImagesToWrap;
+
+  for (StringRef ImageFilename : Images) {
+llvm::ErrorOr> ImageOrError =
+llvm::MemoryBuffer::getFileOrSTDIN(ImageFilename);
+if (std::error_code EC = ImageOrError.getError())
+  return createFileError(ImageFilename, EC);
+ImagesToWrap.emplace_back((*ImageOrError)->getBufferStart(),
+  (*ImageOrError)->getBufferSize());
+SavedBuffers.emplace_back(std::move(*ImageOrError));
+  }
 
   LLVMContext Context;
-  SMDiagnostic Err;
-  std::unique_ptr M = parseIRFile(BitcodeFile, Err, Context);
+  Module M("offload.wrapper.module", Context);
+  M.setTargetTriple(HostTriple);
+  if (Error Err = wrapBinaries(M, ImagesToWrap))
+return std::move(Err);
 
-  return compileModule(*M);
+  return compileModule(M);
 }
 
 Optional findFile(StringRef Dir, const Twine &Name) {
@@ -1162,13 +1149,10 @@ int main(int argc, const char **argv) {
 
   // Wrap each linked device image into a linkable host binary and add 

[PATCH] D117048: [OpenMP] Link the bitcode library late for device LTO

2022-01-31 Thread Joseph Huber via Phabricator via cfe-commits
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rG3762111aa960: [OpenMP] Link the bitcode library late for 
device LTO (authored by jhuber6).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D117048/new/

https://reviews.llvm.org/D117048

Files:
  clang/lib/Driver/ToolChains/AMDGPUOpenMP.cpp
  clang/lib/Driver/ToolChains/Clang.cpp
  clang/lib/Driver/ToolChains/Cuda.cpp
  clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp

Index: clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
===
--- clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
+++ clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
@@ -68,9 +68,14 @@
 
 static cl::opt OptLevel("opt-level",
  cl::desc("Optimization level for LTO"),
- cl::init("O0"),
+ cl::init("O2"),
  cl::cat(ClangLinkerWrapperCategory));
 
+static cl::opt
+BitcodeLibrary("target-library",
+   cl::desc("Path for the target bitcode library"),
+   cl::cat(ClangLinkerWrapperCategory));
+
 // Do not parse linker options.
 static cl::list
 HostLinkerArgs(cl::Sink, cl::desc("..."));
@@ -197,7 +202,7 @@
   std::unique_ptr Output = std::move(*OutputOrErr);
   std::copy(Contents->begin(), Contents->end(), Output->getBufferStart());
   if (Error E = Output->commit())
-return E;
+return std::move(E);
 
   DeviceFiles.emplace_back(DeviceTriple, Arch, TempFile);
   ToBeStripped.push_back(*Name);
@@ -225,7 +230,7 @@
 std::unique_ptr Output = std::move(*OutputOrErr);
 std::copy(Contents.begin(), Contents.end(), Output->getBufferStart());
 if (Error E = Output->commit())
-  return E;
+  return std::move(E);
 StripFile = TempFile;
   }
 
@@ -307,7 +312,7 @@
 std::unique_ptr Output = std::move(*OutputOrErr);
 std::copy(Contents.begin(), Contents.end(), Output->getBufferStart());
 if (Error E = Output->commit())
-  return E;
+  return std::move(E);
 
 DeviceFiles.emplace_back(DeviceTriple, Arch, TempFile);
 ToBeDeleted.push_back(&GV);
@@ -318,7 +323,7 @@
 
   // We need to materialize the lazy module before we make any changes.
   if (Error Err = M->materializeAll())
-return Err;
+return std::move(Err);
 
   // Remove the global from the module and write it to a new file.
   for (GlobalVariable *GV : ToBeDeleted) {
@@ -392,7 +397,7 @@
   }
 
   if (Err)
-return Err;
+return std::move(Err);
 
   if (!NewMembers)
 return None;
@@ -406,9 +411,9 @@
 
   std::unique_ptr Buffer =
   MemoryBuffer::getMemBuffer(Library.getMemoryBufferRef(), false);
-  if (Error WriteErr = writeArchive(TempFile, Members, true, Library.kind(),
+  if (Error Err = writeArchive(TempFile, Members, true, Library.kind(),
 true, Library.isThin(), std::move(Buffer)))
-return WriteErr;
+return std::move(Err);
 
   return static_cast(TempFile);
 }
@@ -726,7 +731,7 @@
 
 // Add the bitcode file with its resolved symbols to the LTO job.
 if (Error Err = LTOBackend->add(std::move(BitcodeFile), Resolutions))
-  return Err;
+  return std::move(Err);
   }
 
   // Run the LTO job to compile the bitcode.
@@ -744,7 +749,7 @@
 std::make_unique(FD, true));
   };
   if (Error Err = LTOBackend->run(AddStream))
-return Err;
+return std::move(Err);
 
   for (auto &File : Files) {
 if (!TheTriple.isNVPTX())
@@ -957,6 +962,17 @@
 }
   }
 
+  // Add the device bitcode library to the device files if it was passed in.
+  if (!BitcodeLibrary.empty()) {
+// FIXME: Hacky workaround to avoid a backend crash at O0.
+if (OptLevel[1] - '0' == 0)
+  OptLevel[1] = '1';
+auto DeviceAndPath = StringRef(BitcodeLibrary).split('=');
+auto TripleAndArch = DeviceAndPath.first.rsplit('-');
+DeviceFiles.emplace_back(TripleAndArch.first, TripleAndArch.second,
+ DeviceAndPath.second);
+  }
+
   // Link the device images extracted from the linker input.
   SmallVector LinkedImages;
   if (Error Err = linkDeviceFiles(DeviceFiles, LinkerArgs, LinkedImages))
Index: clang/lib/Driver/ToolChains/Cuda.cpp
===
--- clang/lib/Driver/ToolChains/Cuda.cpp
+++ clang/lib/Driver/ToolChains/Cuda.cpp
@@ -744,6 +744,10 @@
   return;
 }
 
+// Link the bitcode library late if we're using device LTO.
+if (getDriver().isUsingLTO(/* IsOffload */ true))
+  return;
+
 std::string BitcodeSuffix;
 if (DriverArgs.hasFlag(options::OPT_fopenmp_target_new_runtime,
options::OPT_fno_openmp_target_new_runtime, true))
Index: clang/lib/Driver/ToolC

[clang] eb6ddf2 - [OpenMP] Replace sysmtem call to `llc` with target machine

2022-01-31 Thread Joseph Huber via cfe-commits

Author: Joseph Huber
Date: 2022-01-31T23:11:42-05:00
New Revision: eb6ddf288cd0ec58be8ff6c0840c440a5f31dd0b

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

LOG: [OpenMP] Replace sysmtem call to `llc` with target machine

Summary:
This patch replaces the system call to the `llc` binary with a library
call to the target machine interface. This should be faster than
relying on an external system call to compile the final wrapper binary.

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

Added: 


Modified: 
clang/tools/clang-linker-wrapper/CMakeLists.txt
clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp

Removed: 




diff  --git a/clang/tools/clang-linker-wrapper/CMakeLists.txt 
b/clang/tools/clang-linker-wrapper/CMakeLists.txt
index 3e6d67fed4cf..5f08a443b5e9 100644
--- a/clang/tools/clang-linker-wrapper/CMakeLists.txt
+++ b/clang/tools/clang-linker-wrapper/CMakeLists.txt
@@ -4,6 +4,8 @@ set(LLVM_LINK_COMPONENTS
   Core
   BinaryFormat
   MC
+  Target
+  Analysis
   Passes
   IRReader
   Object

diff  --git a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp 
b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
index 1dd95cb51d7f..6dc29767183d 100644
--- a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
+++ b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
@@ -23,6 +23,7 @@
 #include "llvm/IR/Module.h"
 #include "llvm/IRReader/IRReader.h"
 #include "llvm/LTO/LTO.h"
+#include "llvm/MC/TargetRegistry.h"
 #include "llvm/Object/Archive.h"
 #include "llvm/Object/ArchiveWriter.h"
 #include "llvm/Object/Binary.h"
@@ -42,6 +43,7 @@
 #include "llvm/Support/TargetSelect.h"
 #include "llvm/Support/WithColor.h"
 #include "llvm/Support/raw_ostream.h"
+#include "llvm/Target/TargetMachine.h"
 
 using namespace llvm;
 using namespace llvm::object;
@@ -958,6 +960,49 @@ Error linkDeviceFiles(ArrayRef DeviceFiles,
   return Error::success();
 }
 
+// Compile the module to an object file using the appropriate target machine 
for
+// the host triple.
+Expected compileModule(Module &M) {
+  if (M.getTargetTriple().empty())
+M.setTargetTriple(HostTriple);
+
+  std::string Msg;
+  const Target *T = TargetRegistry::lookupTarget(M.getTargetTriple(), Msg);
+  if (!T)
+return createStringError(inconvertibleErrorCode(), Msg);
+
+  auto Options =
+  codegen::InitTargetOptionsFromCodeGenFlags(Triple(M.getTargetTriple()));
+  StringRef CPU = "";
+  StringRef Features = "";
+  std::unique_ptr TM(T->createTargetMachine(
+  HostTriple, CPU, Features, Options, Reloc::PIC_, M.getCodeModel()));
+
+  if (M.getDataLayout().isDefault())
+M.setDataLayout(TM->createDataLayout());
+
+  SmallString<128> ObjectFile;
+  int FD = -1;
+  if (Error Err = createOutputFile(sys::path::filename(ExecutableName) +
+   "offload-wrapper",
+   "o", ObjectFile))
+return std::move(Err);
+  if (std::error_code EC = sys::fs::openFileForWrite(ObjectFile, FD))
+return errorCodeToError(EC);
+
+  auto OS = std::make_unique(FD, true);
+
+  legacy::PassManager CodeGenPasses;
+  TargetLibraryInfoImpl TLII(Triple(M.getTargetTriple()));
+  CodeGenPasses.add(new TargetLibraryInfoWrapperPass(TLII));
+  if (TM->addPassesToEmitFile(CodeGenPasses, *OS, nullptr, CGFT_ObjectFile))
+return createStringError(inconvertibleErrorCode(),
+ "Failed to execute host backend");
+  CodeGenPasses.run(M);
+
+  return static_cast(ObjectFile);
+}
+
 /// Creates an object file containing the device image stored in the filename 
\p
 /// ImageFile that can be linked with the host.
 Expected wrapDeviceImage(StringRef ImageFile) {
@@ -987,30 +1032,11 @@ Expected wrapDeviceImage(StringRef 
ImageFile) {
 return createStringError(inconvertibleErrorCode(),
  "'clang-offload-wrapper' failed");
 
-  ErrorOr CompilerPath = sys::findProgramByName("llc");
-  if (!WrapperPath)
-return createStringError(WrapperPath.getError(),
- "Unable to find 'llc' in path");
-
-  // Create a new file to write the wrapped bitcode file to.
-  SmallString<128> ObjectFile;
-  if (Error Err = createOutputFile(sys::path::filename(ExecutableName) +
-   "-offload-wrapper",
-   "o", ObjectFile))
-return std::move(Err);
-
-  SmallVector CompilerArgs;
-  CompilerArgs.push_back(*CompilerPath);
-  CompilerArgs.push_back("--filetype=obj");
-  CompilerArgs.push_back("--relocation-model=pic");
-  CompilerArgs.push_back("-o");
-  CompilerArgs.push_back(ObjectFile);
-  CompilerArgs.push_back(BitcodeFile);
-
-  if (sys::ExecuteAndWait(*CompilerPath, CompilerArgs))
-return createStringError(inconvertibleErrorCod

[clang] 9375f15 - [OpenMP] Cleanup the Linker Wrapper

2022-01-31 Thread Joseph Huber via cfe-commits

Author: Joseph Huber
Date: 2022-01-31T23:11:42-05:00
New Revision: 9375f1563e87e115cc8f9d052760d68460889f4e

URL: 
https://github.com/llvm/llvm-project/commit/9375f1563e87e115cc8f9d052760d68460889f4e
DIFF: 
https://github.com/llvm/llvm-project/commit/9375f1563e87e115cc8f9d052760d68460889f4e.diff

LOG: [OpenMP] Cleanup the Linker Wrapper

Summary:
Various changes and cleanup for the Linker Wrapper tool.

Added: 


Modified: 
clang/lib/Driver/ToolChains/Clang.cpp
clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp

Removed: 




diff  --git a/clang/lib/Driver/ToolChains/Clang.cpp 
b/clang/lib/Driver/ToolChains/Clang.cpp
index 537b9ca829262..7aac977209eba 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -8153,9 +8153,9 @@ void LinkerWrapper::ConstructJob(Compilation &C, const 
JobAction &JA,
   if (getToolChain().getDriver().isUsingLTO(/* IsOffload */ true)) {
 // Pass in target features for each toolchain.
 auto OpenMPTCRange = C.getOffloadToolChains();
-for (auto TI = OpenMPTCRange.first, TE = OpenMPTCRange.second; TI != TE;
- ++TI) {
-  const ToolChain *TC = TI->second;
+for (auto &I :
+ llvm::make_range(OpenMPTCRange.first, OpenMPTCRange.second)) {
+  const ToolChain *TC = I.second;
   const ArgList &TCArgs = C.getArgsForToolChain(TC, "", 
Action::OFK_OpenMP);
   ArgStringList FeatureArgs;
   TC->addClangTargetOptions(TCArgs, FeatureArgs, Action::OFK_OpenMP);
@@ -8165,9 +8165,8 @@ void LinkerWrapper::ConstructJob(Compilation &C, const 
JobAction &JA,
 }
 
 // Pass in the bitcode library to be linked during LTO.
-for (auto TI = OpenMPTCRange.first, TE = OpenMPTCRange.second; TI != TE;
- ++TI) {
-  const ToolChain *TC = TI->second;
+for (auto &I : llvm::make_range(OpenMPTCRange.first, 
OpenMPTCRange.second)) {
+  const ToolChain *TC = I.second;
   const Driver &D = TC->getDriver();
   const ArgList &TCArgs = C.getArgsForToolChain(TC, "", 
Action::OFK_OpenMP);
   StringRef Arch = TCArgs.getLastArgValue(options::OPT_march_EQ);
@@ -8232,7 +8231,7 @@ void LinkerWrapper::ConstructJob(Compilation &C, const 
JobAction &JA,
   }
 
   for (const auto &A : Args.getAllArgValues(options::OPT_Xcuda_ptxas))
-CmdArgs.push_back(Args.MakeArgString("-ptxas-option=" + A));
+CmdArgs.push_back(Args.MakeArgString("-ptxas-args=" + A));
 
   // Forward remarks passes to the LLVM backend in the wrapper.
   if (const Arg *A = Args.getLastArg(options::OPT_Rpass_EQ))

diff  --git a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp 
b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
index 83393514d7634..1dd95cb51d7f0 100644
--- a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
+++ b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
@@ -78,10 +78,10 @@ static cl::opt OptLevel("opt-level", 
cl::ZeroOrMore,
  cl::init("O2"),
  cl::cat(ClangLinkerWrapperCategory));
 
-static cl::opt
-BitcodeLibrary("target-library", cl::ZeroOrMore,
-   cl::desc("Path for the target bitcode library"),
-   cl::cat(ClangLinkerWrapperCategory));
+static cl::list
+BitcodeLibraries("target-library", cl::ZeroOrMore,
+ cl::desc("Path for the target bitcode library"),
+ cl::cat(ClangLinkerWrapperCategory));
 
 static cl::opt EmbedBitcode(
 "target-embed-bc", cl::ZeroOrMore,
@@ -94,8 +94,8 @@ static cl::opt
cl::init(sys::getDefaultTargetTriple()),
cl::cat(ClangLinkerWrapperCategory));
 
-static cl::opt
-PtxasOption("ptxas-option", cl::ZeroOrMore,
+static cl::list
+PtxasArgs("ptxas-args", cl::ZeroOrMore,
 cl::desc("Argument to pass to the ptxas invocation"),
 cl::cat(ClangLinkerWrapperCategory));
 
@@ -164,6 +164,15 @@ static StringRef getDeviceFileExtension(StringRef 
DeviceTriple,
   return "o";
 }
 
+/// Extract the device file from the string '-=.bc'.
+DeviceFile getBitcodeLibrary(StringRef LibraryStr) {
+  auto DeviceAndPath = StringRef(LibraryStr).split('=');
+  auto TripleAndArch = DeviceAndPath.first.rsplit('-');
+  return DeviceFile(TripleAndArch.first, TripleAndArch.second,
+DeviceAndPath.second);
+}
+
+/// Get a temporary filename suitable for output.
 Error createOutputFile(const Twine &Prefix, StringRef Extension,
SmallString<128> &NewFilename) {
   if (!SaveTemps) {
@@ -264,7 +273,7 @@ extractFromBinary(const ObjectFile &Obj,
 }
   }
 
-  if (ToBeStripped.empty())
+  if (ToBeStripped.empty() || !StripSections)
 return None;
 
   // If the object file to strip doesn't exist we need to write it so we can
@@ -272,8 +281,9 @@ extractFromBinary(const ObjectFile &Obj,
   SmallString<128> StripFile = Obj.getFi

[PATCH] D116975: [OpenMP] Initial Implementation of LTO and bitcode linking in linker wrapper

2022-01-31 Thread Joseph Huber via Phabricator via cfe-commits
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rGc732c3df749b: [OpenMP] Initial Implementation of LTO and 
bitcode linking in linker wrapper (authored by jhuber6).

Changed prior to commit:
  https://reviews.llvm.org/D116975?vs=404528&id=404790#toc

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D116975/new/

https://reviews.llvm.org/D116975

Files:
  clang/lib/Driver/Driver.cpp
  clang/lib/Driver/ToolChains/Clang.cpp
  clang/tools/clang-linker-wrapper/CMakeLists.txt
  clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp

Index: clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
===
--- clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
+++ clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
@@ -17,9 +17,12 @@
 #include "clang/Basic/Version.h"
 #include "llvm/BinaryFormat/Magic.h"
 #include "llvm/Bitcode/BitcodeWriter.h"
+#include "llvm/CodeGen/CommandFlags.h"
 #include "llvm/IR/Constants.h"
+#include "llvm/IR/DiagnosticPrinter.h"
 #include "llvm/IR/Module.h"
 #include "llvm/IRReader/IRReader.h"
+#include "llvm/LTO/LTO.h"
 #include "llvm/Object/Archive.h"
 #include "llvm/Object/ArchiveWriter.h"
 #include "llvm/Object/Binary.h"
@@ -36,6 +39,7 @@
 #include "llvm/Support/Signals.h"
 #include "llvm/Support/SourceMgr.h"
 #include "llvm/Support/StringSaver.h"
+#include "llvm/Support/TargetSelect.h"
 #include "llvm/Support/WithColor.h"
 #include "llvm/Support/raw_ostream.h"
 
@@ -58,6 +62,15 @@
cl::desc("Path of linker binary"),
cl::cat(ClangLinkerWrapperCategory));
 
+static cl::opt
+TargetFeatures("target-feature", cl::desc("Target features for triple"),
+   cl::cat(ClangLinkerWrapperCategory));
+
+static cl::opt OptLevel("opt-level",
+ cl::desc("Optimization level for LTO"),
+ cl::init("O0"),
+ cl::cat(ClangLinkerWrapperCategory));
+
 // Do not parse linker options.
 static cl::list
 HostLinkerArgs(cl::Sink, cl::desc("..."));
@@ -68,6 +81,9 @@
 /// Temporary files created by the linker wrapper.
 static SmallVector TempFiles;
 
+/// Codegen flags for LTO backend.
+static codegen::RegisterCodeGenFlags CodeGenFlags;
+
 /// Magic section string that marks the existence of offloading data. The
 /// section string will be formatted as `.llvm.offloading..`.
 #define OFFLOAD_SECTION_MAGIC_STR ".llvm.offloading."
@@ -191,6 +207,28 @@
   if (ToBeStripped.empty())
 return None;
 
+  // If the object file to strip doesn't exist we need to write it so we can
+  // pass it to llvm-strip.
+  SmallString<128> StripFile = Obj.getFileName();
+  if (!sys::fs::exists(StripFile)) {
+SmallString<128> TempFile;
+if (std::error_code EC = sys::fs::createTemporaryFile(
+sys::path::stem(StripFile), "o", TempFile))
+  return createFileError(TempFile, EC);
+TempFiles.push_back(static_cast(TempFile));
+
+auto Contents = Obj.getMemoryBufferRef().getBuffer();
+Expected> OutputOrErr =
+FileOutputBuffer::create(TempFile, Contents.size());
+if (!OutputOrErr)
+  return OutputOrErr.takeError();
+std::unique_ptr Output = std::move(*OutputOrErr);
+std::copy(Contents.begin(), Contents.end(), Output->getBufferStart());
+if (Error E = Output->commit())
+  return E;
+StripFile = TempFile;
+  }
+
   // We will use llvm-strip to remove the now unneeded section containing the
   // offloading code.
   ErrorOr StripPath = sys::findProgramByName("llvm-strip");
@@ -207,7 +245,7 @@
   SmallVector StripArgs;
   StripArgs.push_back(*StripPath);
   StripArgs.push_back("--no-strip-all");
-  StripArgs.push_back(Obj.getFileName());
+  StripArgs.push_back(StripFile);
   for (auto &Section : ToBeStripped) {
 StripArgs.push_back("--remove-section");
 StripArgs.push_back(Section);
@@ -408,6 +446,44 @@
 
 // TODO: Move these to a separate file.
 namespace nvptx {
+Expected assemble(StringRef InputFile, Triple TheTriple,
+   StringRef Arch) {
+  // NVPTX uses the nvlink binary to link device object files.
+  ErrorOr PtxasPath =
+  sys::findProgramByName("ptxas", sys::path::parent_path(LinkerExecutable));
+  if (!PtxasPath)
+PtxasPath = sys::findProgramByName("ptxas");
+  if (!PtxasPath)
+return createStringError(PtxasPath.getError(),
+ "Unable to find 'ptxas' in path");
+
+  // Create a new file to write the linked device image to.
+  SmallString<128> TempFile;
+  if (std::error_code EC = sys::fs::createTemporaryFile(
+  TheTriple.getArchName() + "-" + Arch, "cubin", TempFile))
+return createFileError(TempFile, EC);
+  TempFiles.push_back(static_cast(TempFile));
+
+  // TO

[clang] 58dc981 - [OpenMP] Include the executable name in the temporary files

2022-01-31 Thread Joseph Huber via cfe-commits

Author: Joseph Huber
Date: 2022-01-31T23:11:42-05:00
New Revision: 58dc981e082df9db2de1cbd867fa6d73e8eea214

URL: 
https://github.com/llvm/llvm-project/commit/58dc981e082df9db2de1cbd867fa6d73e8eea214
DIFF: 
https://github.com/llvm/llvm-project/commit/58dc981e082df9db2de1cbd867fa6d73e8eea214.diff

LOG: [OpenMP] Include the executable name in the temporary files

Summary:
This parses the executable name out of the linker arguments so we can
use it to give more informative temporary file names and so we don't
accidentally use it for device linking.

Added: 


Modified: 
clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp

Removed: 




diff  --git a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp 
b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
index 56a1859c337e2..83393514d7634 100644
--- a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
+++ b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
@@ -123,6 +123,9 @@ static cl::list
 /// Path of the current binary.
 static const char *LinkerExecutable;
 
+/// Filename of the executable being created.
+static StringRef ExecutableName;
+
 /// Temporary files created by the linker wrapper.
 static SmallVector TempFiles;
 
@@ -242,8 +245,9 @@ extractFromBinary(const ObjectFile &Obj,
   SmallString<128> TempFile;
   StringRef DeviceExtension = getDeviceFileExtension(
   DeviceTriple, identify_magic(*Contents) == file_magic::bitcode);
-  if (Error Err = createOutputFile(Prefix + "-device-" + DeviceTriple,
-   DeviceExtension, TempFile))
+  if (Error Err =
+  createOutputFile(Prefix + "-device-" + DeviceTriple + "-" + Arch,
+   DeviceExtension, TempFile))
 return std::move(Err);
 
   Expected> OutputOrErr =
@@ -348,8 +352,9 @@ extractFromBitcode(std::unique_ptr Buffer,
 SmallString<128> TempFile;
 StringRef DeviceExtension = getDeviceFileExtension(
 DeviceTriple, identify_magic(Contents) == file_magic::bitcode);
-if (Error Err = createOutputFile(Prefix + "-device-" + DeviceTriple,
- DeviceExtension, TempFile))
+if (Error Err =
+createOutputFile(Prefix + "-device-" + DeviceTriple + "-" + Arch,
+ DeviceExtension, TempFile))
   return std::move(Err);
 
 Expected> OutputOrErr =
@@ -501,8 +506,10 @@ Expected assemble(StringRef InputFile, Triple 
TheTriple,
 
   // Create a new file to write the linked device image to.
   SmallString<128> TempFile;
-  if (Error Err = createOutputFile(
-  "lto-" + TheTriple.getArchName() + "-" + Arch, "cubin", TempFile))
+  if (Error Err =
+  createOutputFile(sys::path::filename(ExecutableName) + "-device-" +
+   TheTriple.getArchName() + "-" + Arch,
+   "cubin", TempFile))
 return std::move(Err);
 
   // TODO: Pass in arguments like `-g` and `-v` from the driver.
@@ -544,8 +551,10 @@ Expected link(ArrayRef 
InputFiles,
 
   // Create a new file to write the linked device image to.
   SmallString<128> TempFile;
-  if (Error Err = createOutputFile(
-  TheTriple.getArchName() + "-" + Arch + "-image", "out", TempFile))
+  if (Error Err =
+  createOutputFile(sys::path::filename(ExecutableName) + "-device-" +
+   TheTriple.getArchName() + "-" + Arch,
+   "out", TempFile))
 return std::move(Err);
 
   SmallVector CmdArgs;
@@ -590,8 +599,9 @@ Expected link(ArrayRef InputFiles,
 
   // Create a new file to write the linked device image to.
   SmallString<128> TempFile;
-  if (Error Err = createOutputFile(
-  TheTriple.getArchName() + "-" + Arch + "-image", "out", TempFile))
+  if (Error Err = createOutputFile(sys::path::filename(ExecutableName) + "-" +
+   TheTriple.getArchName() + "-" + Arch,
+   "out", TempFile))
 return std::move(Err);
 
   SmallVector CmdArgs;
@@ -716,8 +726,9 @@ std::unique_ptr createLTO(
 };
 Conf.PostInternalizeModuleHook = [&](size_t, const Module &M) {
   SmallString<128> TempFile;
-  if (Error Err =
-  createOutputFile("lto-" + TheTriple.getTriple(), "bc", TempFile))
+  if (Error Err = createOutputFile(sys::path::filename(ExecutableName) +
+   "-device-" + TheTriple.getTriple(),
+   "bc", TempFile))
 HandleError(std::move(Err));
 
   std::error_code EC;
@@ -799,8 +810,6 @@ Error linkBitcodeFiles(SmallVectorImpl 
&InputFiles,
   if (BitcodeFiles.empty())
 return Error::success();
 
-  assert(!BitcodeLibrary.empty() && "Bitcode linking without `-foffload-lto`");
-
   auto HandleError = [&](Error Err) {
 logAllUnhandledErrors(std::move(Err)

[clang] bf499c5 - [OpenMP] Implement save temps functionality in linker wrapper

2022-01-31 Thread Joseph Huber via cfe-commits

Author: Joseph Huber
Date: 2022-01-31T23:11:42-05:00
New Revision: bf499c58af3d3a96661f5c7ef81b264eac575541

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

LOG: [OpenMP] Implement save temps functionality in linker wrapper

Summary:
This patch implements the `-save-temps` flag for the linker wrapper.
This allows the user to inspect the intermeditary outpout that the
linker wrapper creates.

Added: 


Modified: 
clang/lib/Driver/ToolChains/Clang.cpp
clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp

Removed: 




diff  --git a/clang/lib/Driver/ToolChains/Clang.cpp 
b/clang/lib/Driver/ToolChains/Clang.cpp
index 1ddffcdc24f48..537b9ca829262 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -8244,6 +8244,8 @@ void LinkerWrapper::ConstructJob(Compilation &C, const 
JobAction &JA,
   if (const Arg *A = Args.getLastArg(options::OPT_Rpass_analysis_EQ))
 CmdArgs.push_back(
 Args.MakeArgString(Twine("-pass-remarks-analysis=") + A->getValue()));
+  if (Args.getLastArg(options::OPT_save_temps_EQ))
+CmdArgs.push_back("-save-temps");
 
   // Add the linker arguments to be forwarded by the wrapper.
   CmdArgs.push_back("-linker-path");

diff  --git a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp 
b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
index 8331bda62420b..56a1859c337e2 100644
--- a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
+++ b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
@@ -111,6 +111,10 @@ static cl::opt DebugInfo(
   "Direction information"),
clEnumValN(FullDebugInfo, "g", "Full debugging support")));
 
+static cl::opt SaveTemps("save-temps", cl::ZeroOrMore,
+   cl::desc("Save intermediary results."),
+   cl::cat(ClangLinkerWrapperCategory));
+
 // Do not parse linker options.
 static cl::list
 HostLinkerArgs(cl::Positional,
@@ -157,6 +161,21 @@ static StringRef getDeviceFileExtension(StringRef 
DeviceTriple,
   return "o";
 }
 
+Error createOutputFile(const Twine &Prefix, StringRef Extension,
+   SmallString<128> &NewFilename) {
+  if (!SaveTemps) {
+if (std::error_code EC =
+sys::fs::createTemporaryFile(Prefix, Extension, NewFilename))
+  return createFileError(NewFilename, EC);
+TempFiles.push_back(static_cast(NewFilename));
+  } else {
+const Twine &Filename = Prefix + "." + Extension;
+Filename.toNullTerminatedStringRef(NewFilename);
+  }
+
+  return Error::success();
+}
+
 Error runLinker(std::string &LinkerPath, SmallVectorImpl &Args) {
   std::vector LinkerArgs;
   LinkerArgs.push_back(LinkerPath);
@@ -204,11 +223,8 @@ void removeFromCompilerUsed(Module &M, GlobalValue &Value) 
{
 Expected>
 extractFromBinary(const ObjectFile &Obj,
   SmallVectorImpl &DeviceFiles) {
-
   StringRef Extension = sys::path::extension(Obj.getFileName()).drop_front();
-  StringRef Prefix = sys::path::stem(Obj.getFileName()).take_until([](char C) {
-return C == '-';
-  });
+  StringRef Prefix = sys::path::stem(Obj.getFileName());
   SmallVector ToBeStripped;
 
   // Extract data from sections of the form `.llvm.offloading..`.
@@ -226,10 +242,9 @@ extractFromBinary(const ObjectFile &Obj,
   SmallString<128> TempFile;
   StringRef DeviceExtension = getDeviceFileExtension(
   DeviceTriple, identify_magic(*Contents) == file_magic::bitcode);
-  if (std::error_code EC = sys::fs::createTemporaryFile(
-  Prefix + "-device-" + DeviceTriple, DeviceExtension, TempFile))
-return createFileError(TempFile, EC);
-  TempFiles.push_back(static_cast(TempFile));
+  if (Error Err = createOutputFile(Prefix + "-device-" + DeviceTriple,
+   DeviceExtension, TempFile))
+return std::move(Err);
 
   Expected> OutputOrErr =
   FileOutputBuffer::create(TempFile, Sec.getSize());
@@ -253,10 +268,9 @@ extractFromBinary(const ObjectFile &Obj,
   SmallString<128> StripFile = Obj.getFileName();
   if (!sys::fs::exists(StripFile)) {
 SmallString<128> TempFile;
-if (std::error_code EC = sys::fs::createTemporaryFile(
-sys::path::stem(StripFile), "o", TempFile))
-  return createFileError(TempFile, EC);
-TempFiles.push_back(static_cast(TempFile));
+if (Error Err = createOutputFile(sys::path::stem(StripFile),
+ sys::path::extension(StripFile), 
TempFile))
+  return std::move(Err);
 
 auto Contents = Obj.getMemoryBufferRef().getBuffer();
 Expected> OutputOrErr =
@@ -278,10 +292,8 @@ extractFromBinary(const ObjectFile &Obj,
  "Unable to fin

[clang] a47b1cf - [OpenMP] Embed bitcode after optimizations instead of linking

2022-01-31 Thread Joseph Huber via cfe-commits

Author: Joseph Huber
Date: 2022-01-31T23:11:42-05:00
New Revision: a47b1cf306fb7494d493784c25dafb2dd6084b99

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

LOG: [OpenMP] Embed bitcode after optimizations instead of linking

Summary:
Various changes to the linker wrapper, and the bitcode embedding is not
done after the optimizations have run rather than after linking is done.
This saves time when doing JIT.

Added: 


Modified: 
clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp

Removed: 




diff  --git a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp 
b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
index 9f1f4acdf3bae..8331bda62420b 100644
--- a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
+++ b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
@@ -83,7 +83,7 @@ static cl::opt
cl::desc("Path for the target bitcode library"),
cl::cat(ClangLinkerWrapperCategory));
 
-static cl::opt EmbedBC(
+static cl::opt EmbedBitcode(
 "target-embed-bc", cl::ZeroOrMore,
 cl::desc("Embed linked bitcode instead of an executable device image"),
 cl::init(false), cl::cat(ClangLinkerWrapperCategory));
@@ -657,7 +657,7 @@ void diagnosticHandler(const DiagnosticInfo &DI) {
 WithColor::note(errs(), LinkerExecutable) << ErrStorage << "\n";
 break;
   case DS_Remark:
-WithColor::remark(errs(), LinkerExecutable) << ErrStorage << "\n";
+WithColor::remark(errs()) << ErrStorage << "\n";
 break;
   }
 }
@@ -710,7 +710,7 @@ std::unique_ptr createLTO(
   Conf.PTO.LoopVectorization = Conf.OptLevel > 1;
   Conf.PTO.SLPVectorization = Conf.OptLevel > 1;
 
-  Conf.PostInternalizeModuleHook = Hook;
+  Conf.PostOptModuleHook = Hook;
   if (TheTriple.isNVPTX())
 Conf.CGFileType = CGFT_AssemblyFile;
   else
@@ -781,6 +781,8 @@ Error linkBitcodeFiles(SmallVectorImpl 
&InputFiles,
   if (BitcodeFiles.empty())
 return Error::success();
 
+  assert(!BitcodeLibrary.empty() && "Bitcode linking without `-foffload-lto`");
+
   auto HandleError = [&](std::error_code EC) {
 logAllUnhandledErrors(errorCodeToError(EC),
   WithColor::error(errs(), LinkerExecutable));
@@ -805,7 +807,7 @@ Error linkBitcodeFiles(SmallVectorImpl 
&InputFiles,
 
   // We assume visibility of the whole program if every input file was bitcode.
   bool WholeProgram = BitcodeFiles.size() == InputFiles.size();
-  auto LTOBackend = (EmbedBC)
+  auto LTOBackend = (EmbedBitcode)
 ? createLTO(TheTriple, Arch, WholeProgram, LinkOnly)
 : createLTO(TheTriple, Arch, WholeProgram);
 
@@ -879,14 +881,13 @@ Error linkBitcodeFiles(SmallVectorImpl 
&InputFiles,
 return Err;
 
   // Is we are compiling for NVPTX we need to run the assembler first.
-  for (auto &File : Files) {
-if (!TheTriple.isNVPTX() || EmbedBC)
-  continue;
-
-auto FileOrErr = nvptx::assemble(File, TheTriple, Arch);
-if (!FileOrErr)
-  return FileOrErr.takeError();
-File = *FileOrErr;
+  if (TheTriple.isNVPTX() && !EmbedBitcode) {
+for (auto &File : Files) {
+  auto FileOrErr = nvptx::assemble(File, TheTriple, Arch);
+  if (!FileOrErr)
+return FileOrErr.takeError();
+  File = *FileOrErr;
+}
   }
 
   // Append the new inputs to the device linker input.
@@ -918,7 +919,7 @@ Error linkDeviceFiles(ArrayRef DeviceFiles,
   return Err;
 
 // If we are embedding bitcode for JIT, skip the final device linking.
-if (EmbedBC) {
+if (EmbedBitcode) {
   assert(!LinkerInput.getValue().empty() && "No bitcode image to embed");
   LinkedImages.push_back(LinkerInput.getValue().front());
   continue;
@@ -1080,8 +1081,8 @@ int main(int argc, const char **argv) {
 if (Optional Library = searchLibrary(Arg, LibraryPaths))
   Filename = *Library;
 
-if (sys::path::extension(Filename) == ".o" ||
-sys::path::extension(Filename) == ".a") {
+if (sys::fs::exists(Filename) && (sys::path::extension(Filename) == ".o" ||
+  sys::path::extension(Filename) == ".a")) 
{
   ErrorOr> BufferOrErr =
   MemoryBuffer::getFileOrSTDIN(Filename);
   if (std::error_code EC = BufferOrErr.getError())
@@ -1100,9 +1101,6 @@ int main(int argc, const char **argv) {
 
   // Add the device bitcode library to the device files if it was passed in.
   if (!BitcodeLibrary.empty()) {
-// FIXME: Hacky workaround to avoid a backend crash at O0.
-if (OptLevel[1] - '0' == 0)
-  OptLevel[1] = '1';
 auto DeviceAndPath = StringRef(BitcodeLibrary).split('=');
 auto TripleAndArch = DeviceAndPath.first.rsplit('-');
 DeviceFiles.emplace_back(TripleAndArch.first, TripleAndArch.second,


  

[PATCH] D116675: [OpenMP] Search for static libraries in offload linker tool

2022-01-31 Thread Joseph Huber via Phabricator via cfe-commits
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rG0e82c7553be9: [OpenMP] Search for static libraries in 
offload linker tool (authored by jhuber6).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D116675/new/

https://reviews.llvm.org/D116675

Files:
  clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp


Index: clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
===
--- clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
+++ clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
@@ -65,7 +65,9 @@
 /// Path of the current binary.
 static std::string LinkerExecutable;
 
+/// Temporary files created by the linker wrapper.
 static SmallVector TempFiles;
+
 /// Magic section string that marks the existence of offloading data. The
 /// section string will be formatted as `.llvm.offloading..`.
 #define OFFLOAD_SECTION_MAGIC_STR ".llvm.offloading."
@@ -551,6 +553,44 @@
   return static_cast(ObjectFile);
 }
 
+Optional findFile(StringRef Dir, const Twine &Name) {
+  SmallString<128> Path;
+  // TODO: Parse `--sysroot` somewhere and use it here.
+  sys::path::append(Path, Dir, Name);
+  if (sys::fs::exists(Path))
+return static_cast(Path);
+  return None;
+}
+
+Optional findFromSearchPaths(StringRef Name,
+  ArrayRef SearchPaths) {
+  for (StringRef Dir : SearchPaths)
+if (Optional File = findFile(Dir, Name))
+  return File;
+  return None;
+}
+
+Optional searchLibraryBaseName(StringRef Name,
+ArrayRef SearchPaths) {
+  for (StringRef Dir : SearchPaths) {
+if (Optional File = findFile(Dir, "lib" + Name + ".a"))
+  return File;
+  }
+  return None;
+}
+
+/// Search for static libraries in the linker's library path given input like
+/// `-lfoo` or `-l:libfoo.a`.
+Optional searchLibrary(StringRef Input,
+ArrayRef SearchPaths) {
+  if (!Input.startswith("-l"))
+return None;
+  StringRef Name = Input.drop_front(2);
+  if (Name.startswith(":"))
+return findFromSearchPaths(Name.drop_front(), SearchPaths);
+  return searchLibraryBaseName(Name, SearchPaths);
+}
+
 } // namespace
 
 int main(int argc, const char **argv) {
@@ -581,16 +621,26 @@
   for (const std::string &Arg : HostLinkerArgs)
 LinkerArgs.push_back(Arg);
 
+  SmallVector LibraryPaths;
+  for (const StringRef Arg : LinkerArgs)
+if (Arg.startswith("-L"))
+  LibraryPaths.push_back(Arg.drop_front(2));
+
   // Try to extract device code from the linker input and replace the linker
   // input with a new file that has the device section stripped.
   SmallVector DeviceFiles;
   for (std::string &Arg : LinkerArgs) {
-if (sys::path::extension(Arg) == ".o" ||
-sys::path::extension(Arg) == ".a") {
+// Search for static libraries in the library link path.
+std::string Filename = Arg;
+if (Optional Library = searchLibrary(Arg, LibraryPaths))
+  Filename = *Library;
+
+if (sys::path::extension(Filename) == ".o" ||
+sys::path::extension(Filename) == ".a") {
   ErrorOr> BufferOrErr =
-  MemoryBuffer::getFileOrSTDIN(Arg);
+  MemoryBuffer::getFileOrSTDIN(Filename);
   if (std::error_code EC = BufferOrErr.getError())
-return reportError(createFileError(Arg, EC));
+return reportError(createFileError(Filename, EC));
 
   auto NewFileOrErr =
   extractFromBuffer(std::move(*BufferOrErr), DeviceFiles);


Index: clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
===
--- clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
+++ clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
@@ -65,7 +65,9 @@
 /// Path of the current binary.
 static std::string LinkerExecutable;
 
+/// Temporary files created by the linker wrapper.
 static SmallVector TempFiles;
+
 /// Magic section string that marks the existence of offloading data. The
 /// section string will be formatted as `.llvm.offloading..`.
 #define OFFLOAD_SECTION_MAGIC_STR ".llvm.offloading."
@@ -551,6 +553,44 @@
   return static_cast(ObjectFile);
 }
 
+Optional findFile(StringRef Dir, const Twine &Name) {
+  SmallString<128> Path;
+  // TODO: Parse `--sysroot` somewhere and use it here.
+  sys::path::append(Path, Dir, Name);
+  if (sys::fs::exists(Path))
+return static_cast(Path);
+  return None;
+}
+
+Optional findFromSearchPaths(StringRef Name,
+  ArrayRef SearchPaths) {
+  for (StringRef Dir : SearchPaths)
+if (Optional File = findFile(Dir, Name))
+  return File;
+  return None;
+}
+
+Optional searchLibraryBaseName(StringRef Name,
+ArrayRef SearchPaths) {
+  for (StringRef Dir : SearchPaths) {
+if 

[clang] 46d0190 - [OpenMP] Improve symbol resolution for OpenMP Offloading LTO

2022-01-31 Thread Joseph Huber via cfe-commits

Author: Joseph Huber
Date: 2022-01-31T23:11:42-05:00
New Revision: 46d019041cd924d8bdf69c818bbe13f6cd37f466

URL: 
https://github.com/llvm/llvm-project/commit/46d019041cd924d8bdf69c818bbe13f6cd37f466
DIFF: 
https://github.com/llvm/llvm-project/commit/46d019041cd924d8bdf69c818bbe13f6cd37f466.diff

LOG: [OpenMP] Improve symbol resolution for OpenMP Offloading LTO

This patch improves the symbol resolution done for LTO with offloading
applications. The symbol resolution done here allows the LTO backend to
internalize more functions. The symbol resoltion done is a simplified
view that does not take into account various options like `--wrap` or
`--dyanimic-list` and always assumes we are creating a shared object.
The actual target may be an executable, but semantically it is used as a
shared object because certain objects need to be visible outside of the
executable when they are read by the OpenMP plugin.

Depends on D117246

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

Added: 


Modified: 
clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp

Removed: 




diff  --git a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp 
b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
index 5ffe857fc9c4c..9f1f4acdf3bae 100644
--- a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
+++ b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
@@ -736,6 +736,7 @@ Error linkBitcodeFiles(SmallVectorImpl 
&InputFiles,
   SmallVector, 4> BitcodeFiles;
   SmallVector NewInputFiles;
   StringMap UsedInRegularObj;
+  StringMap UsedInSharedLib;
 
   // Search for bitcode files in the input and create an LTO input file. If it
   // is not a bitcode file, scan its symbol table for symbols we need to
@@ -759,7 +760,11 @@ Error linkBitcodeFiles(SmallVectorImpl 
&InputFiles,
 if (!Name)
   return Name.takeError();
 
-UsedInRegularObj[*Name] = true;
+// Record if we've seen these symbols in any object or shared 
libraries.
+if ((*ObjFile)->isRelocatableObject()) {
+  UsedInRegularObj[*Name] = true;
+} else
+  UsedInSharedLib[*Name] = true;
   }
 } else {
   Expected> InputFileOrErr =
@@ -767,6 +772,7 @@ Error linkBitcodeFiles(SmallVectorImpl 
&InputFiles,
   if (!InputFileOrErr)
 return InputFileOrErr.takeError();
 
+  // Save the input file and the buffer associated with its memory.
   BitcodeFiles.push_back(std::move(*InputFileOrErr));
   SavedBuffers.push_back(std::move(*BufferOrErr));
 }
@@ -797,22 +803,16 @@ Error linkBitcodeFiles(SmallVectorImpl 
&InputFiles,
 return false;
   };
 
-  // We have visibility of the whole program if every input is bitcode, all
-  // inputs are statically linked so there should be no external references.
+  // We assume visibility of the whole program if every input file was bitcode.
   bool WholeProgram = BitcodeFiles.size() == InputFiles.size();
   auto LTOBackend = (EmbedBC)
 ? createLTO(TheTriple, Arch, WholeProgram, LinkOnly)
 : createLTO(TheTriple, Arch, WholeProgram);
 
-  // TODO: Run more tests to verify that this is correct.
-  // Create the LTO instance with the necessary config and add the bitcode 
files
-  // to it after resolving symbols. We make a few assumptions about symbol
-  // resolution.
-  // 1. The target is going to be a stand-alone executable file.
-  // 2. We do not support relocatable object files.
-  // 3. All inputs are relocatable object files extracted from host binaries, 
so
-  //there is no resolution to a dynamic library.
-  StringMap PrevailingSymbols;
+  // We need to resolve the symbols so the LTO backend knows which symbols need
+  // to be kept or can be internalized. This is a simplified symbol resolution
+  // scheme to approximate the full resolution a linker would do.
+  DenseSet PrevailingSymbols;
   for (auto &BitcodeFile : BitcodeFiles) {
 const auto Symbols = BitcodeFile->symbols();
 SmallVector Resolutions(Symbols.size());
@@ -821,35 +821,43 @@ Error linkBitcodeFiles(SmallVectorImpl 
&InputFiles,
   lto::SymbolResolution &Res = Resolutions[Idx++];
 
   // We will use this as the prevailing symbol definition in LTO unless
-  // it is undefined in the module or another symbol has already been used.
-  Res.Prevailing = !Sym.isUndefined() && !PrevailingSymbols[Sym.getName()];
-
-  // We need LTO to preserve symbols referenced in other object files, or
-  // are needed by the rest of the toolchain.
+  // it is undefined or another definition has already been used.
+  Res.Prevailing =
+  !Sym.isUndefined() && PrevailingSymbols.insert(Sym.getName()).second;
+
+  // We need LTO to preseve the following global symbols:
+  // 1) Symbols used in regular objects.
+  // 2) Sections that will be given a __start/__stop symbol.
+   

[PATCH] D116627: [Clang] Initial support for linking offloading code in tool

2022-01-31 Thread Joseph Huber via Phabricator via cfe-commits
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rGd0744585f9ea: [Clang] Initial support for linking offloading 
code in tool (authored by jhuber6).

Changed prior to commit:
  https://reviews.llvm.org/D116627?vs=404598&id=404788#toc

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D116627/new/

https://reviews.llvm.org/D116627

Files:
  clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp

Index: clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
===
--- clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
+++ clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
@@ -28,6 +28,7 @@
 #include "llvm/Support/Errc.h"
 #include "llvm/Support/FileOutputBuffer.h"
 #include "llvm/Support/FileSystem.h"
+#include "llvm/Support/Host.h"
 #include "llvm/Support/InitLLVM.h"
 #include "llvm/Support/MemoryBuffer.h"
 #include "llvm/Support/Path.h"
@@ -59,22 +60,26 @@
 
 // Do not parse linker options.
 static cl::list
-LinkerArgs(cl::Sink, cl::desc("..."));
+HostLinkerArgs(cl::Sink, cl::desc("..."));
 
 /// Path of the current binary.
 static std::string LinkerExecutable;
 
+static SmallVector TempFiles;
 /// Magic section string that marks the existence of offloading data. The
 /// section string will be formatted as `.llvm.offloading..`.
-#define OFFLOAD_SECTION_MAGIC_STR ".llvm.offloading"
+#define OFFLOAD_SECTION_MAGIC_STR ".llvm.offloading."
 
+/// Information for a device offloading file extracted from the host.
 struct DeviceFile {
   DeviceFile(StringRef TheTriple, StringRef Arch, StringRef Filename)
   : TheTriple(TheTriple), Arch(Arch), Filename(Filename) {}
 
-  const Triple TheTriple;
+  const std::string TheTriple;
   const std::string Arch;
   const std::string Filename;
+
+  operator std::string() const { return TheTriple + "-" + Arch; }
 };
 
 namespace {
@@ -83,6 +88,16 @@
 extractFromBuffer(std::unique_ptr Buffer,
   SmallVectorImpl &DeviceFiles);
 
+static StringRef getDeviceFileExtension(StringRef DeviceTriple,
+bool IsBitcode = false) {
+  Triple TheTriple(DeviceTriple);
+  if (TheTriple.isAMDGPU() || IsBitcode)
+return "bc";
+  if (TheTriple.isNVPTX())
+return "cubin";
+  return "o";
+}
+
 Error runLinker(std::string &LinkerPath, SmallVectorImpl &Args) {
   std::vector LinkerArgs;
   LinkerArgs.push_back(LinkerPath);
@@ -150,9 +165,12 @@
 
 if (Expected Contents = Sec.getContents()) {
   SmallString<128> TempFile;
+  StringRef DeviceExtension = getDeviceFileExtension(
+  DeviceTriple, identify_magic(*Contents) == file_magic::bitcode);
   if (std::error_code EC = sys::fs::createTemporaryFile(
-  Prefix + "-device-" + DeviceTriple, Extension, TempFile))
+  Prefix + "-device-" + DeviceTriple, DeviceExtension, TempFile))
 return createFileError(TempFile, EC);
+  TempFiles.push_back(static_cast(TempFile));
 
   Expected> OutputOrErr =
   FileOutputBuffer::create(TempFile, Sec.getSize());
@@ -173,10 +191,7 @@
 
   // We will use llvm-strip to remove the now unneeded section containing the
   // offloading code.
-  ErrorOr StripPath = sys::findProgramByName(
-  "llvm-strip", sys::path::parent_path(LinkerExecutable));
-  if (!StripPath)
-StripPath = sys::findProgramByName("llvm-strip");
+  ErrorOr StripPath = sys::findProgramByName("llvm-strip");
   if (!StripPath)
 return createStringError(StripPath.getError(),
  "Unable to find 'llvm-strip' in path");
@@ -185,6 +200,7 @@
   if (std::error_code EC =
   sys::fs::createTemporaryFile(Prefix + "-host", Extension, TempFile))
 return createFileError(TempFile, EC);
+  TempFiles.push_back(static_cast(TempFile));
 
   SmallVector StripArgs;
   StripArgs.push_back(*StripPath);
@@ -237,9 +253,12 @@
 
 StringRef Contents = CDS->getAsString();
 SmallString<128> TempFile;
+StringRef DeviceExtension = getDeviceFileExtension(
+DeviceTriple, identify_magic(Contents) == file_magic::bitcode);
 if (std::error_code EC = sys::fs::createTemporaryFile(
-Prefix + "-device-" + DeviceTriple, Extension, TempFile))
+Prefix + "-device-" + DeviceTriple, DeviceExtension, TempFile))
   return createFileError(TempFile, EC);
+TempFiles.push_back(static_cast(TempFile));
 
 Expected> OutputOrErr =
 FileOutputBuffer::create(TempFile, Contents.size());
@@ -271,6 +290,8 @@
   if (std::error_code EC =
   sys::fs::createTemporaryFile(Prefix + "-host", Extension, TempFile))
 return createFileError(TempFile, EC);
+  TempFiles.push_back(static_cast(TempFile));
+
   std::error_code EC;
   raw_fd_ostream HostOutput(TempFile, EC, sys::fs::OF_None);
   if (EC)
@@ -341,6 +362,7 @@
   if (std::error_code EC =
 

[clang] ce16ca3 - [OpenMP] Add support for linking AMDGPU images

2022-01-31 Thread Joseph Huber via cfe-commits

Author: Joseph Huber
Date: 2022-01-31T23:11:42-05:00
New Revision: ce16ca3c7419fc6cec43d0dae5e9f62df05faf6b

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

LOG: [OpenMP] Add support for linking AMDGPU images

This patch adds support for linking AMDGPU images using the LLD binary.
AMDGPU files are always bitcode images and will always use the LTO
backend. Additionally we now pass the default architecture found with
the `amdgpu-arch` tool to the argument list.

Depends on D117156

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

Added: 


Modified: 
clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp

Removed: 




diff  --git a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp 
b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
index 88dd1eed17110..5ffe857fc9c4c 100644
--- a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
+++ b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
@@ -501,7 +501,7 @@ Expected assemble(StringRef InputFile, Triple 
TheTriple,
   // Create a new file to write the linked device image to.
   SmallString<128> TempFile;
   if (std::error_code EC = sys::fs::createTemporaryFile(
-  TheTriple.getArchName() + "-" + Arch, "cubin", TempFile))
+  "lto-" + TheTriple.getArchName() + "-" + Arch, "cubin", TempFile))
 return createFileError(TempFile, EC);
   TempFiles.push_back(static_cast(TempFile));
 
@@ -576,6 +576,50 @@ Expected link(ArrayRef 
InputFiles,
   return static_cast(TempFile);
 }
 } // namespace nvptx
+namespace amdgcn {
+Expected link(ArrayRef InputFiles,
+   ArrayRef LinkerArgs, Triple TheTriple,
+   StringRef Arch) {
+  // AMDGPU uses the lld binary to link device object files.
+  ErrorOr LLDPath =
+  sys::findProgramByName("lld", sys::path::parent_path(LinkerExecutable));
+  if (!LLDPath)
+LLDPath = sys::findProgramByName("lld");
+  if (!LLDPath)
+return createStringError(LLDPath.getError(),
+ "Unable to find 'lld' in path");
+
+  // Create a new file to write the linked device image to.
+  SmallString<128> TempFile;
+  if (std::error_code EC = sys::fs::createTemporaryFile(
+  TheTriple.getArchName() + "-" + Arch + "-image", "out", TempFile))
+return createFileError(TempFile, EC);
+  TempFiles.push_back(static_cast(TempFile));
+
+  SmallVector CmdArgs;
+  CmdArgs.push_back(*LLDPath);
+  CmdArgs.push_back("-flavor");
+  CmdArgs.push_back("gnu");
+  CmdArgs.push_back("--no-undefined");
+  CmdArgs.push_back("-shared");
+  CmdArgs.push_back("-o");
+  CmdArgs.push_back(TempFile);
+
+  // Copy system library paths used by the host linker.
+  for (StringRef Arg : LinkerArgs)
+if (Arg.startswith("-L"))
+  CmdArgs.push_back(Arg);
+
+  // Add extracted input files.
+  for (StringRef Input : InputFiles)
+CmdArgs.push_back(Input);
+
+  if (sys::ExecuteAndWait(*LLDPath, CmdArgs))
+return createStringError(inconvertibleErrorCode(), "'lld' failed");
+
+  return static_cast(TempFile);
+}
+} // namespace amdgcn
 
 Expected linkDevice(ArrayRef InputFiles,
  ArrayRef LinkerArgs,
@@ -585,7 +629,7 @@ Expected linkDevice(ArrayRef 
InputFiles,
   case Triple::nvptx64:
 return nvptx::link(InputFiles, LinkerArgs, TheTriple, Arch);
   case Triple::amdgcn:
-// TODO: AMDGCN linking support.
+return amdgcn::link(InputFiles, LinkerArgs, TheTriple, Arch);
   case Triple::x86:
   case Triple::x86_64:
 // TODO: x86 linking support.



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D116545: [OpenMP] Add support for extracting device code in linker wrapper

2022-01-31 Thread Joseph Huber via Phabricator via cfe-commits
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rGb8239af0eeed: [OpenMP] Add support for extracting device 
code in linker wrapper (authored by jhuber6).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D116545/new/

https://reviews.llvm.org/D116545

Files:
  clang/tools/clang-linker-wrapper/CMakeLists.txt
  clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp

Index: clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
===
--- clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
+++ clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
@@ -5,23 +5,41 @@
 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 //
 //===-===//
-///
+//
+// This tool works as a wrapper over a linking job. This tool is used to create
+// linked device images for offloading. It scans the linker's input for embedded
+// device offloading data stored in sections `.llvm.offloading..`
+// and extracts it as a temporary file. The extracted device files will then be
+// passed to a device linking job to create a final device image.
+//
 //===-===//
 
 #include "clang/Basic/Version.h"
+#include "llvm/BinaryFormat/Magic.h"
+#include "llvm/Bitcode/BitcodeWriter.h"
+#include "llvm/IR/Constants.h"
+#include "llvm/IR/Module.h"
+#include "llvm/IRReader/IRReader.h"
 #include "llvm/Object/Archive.h"
+#include "llvm/Object/ArchiveWriter.h"
+#include "llvm/Object/Binary.h"
+#include "llvm/Object/ObjectFile.h"
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Support/Errc.h"
+#include "llvm/Support/FileOutputBuffer.h"
 #include "llvm/Support/FileSystem.h"
+#include "llvm/Support/InitLLVM.h"
 #include "llvm/Support/MemoryBuffer.h"
 #include "llvm/Support/Path.h"
 #include "llvm/Support/Program.h"
 #include "llvm/Support/Signals.h"
+#include "llvm/Support/SourceMgr.h"
 #include "llvm/Support/StringSaver.h"
 #include "llvm/Support/WithColor.h"
 #include "llvm/Support/raw_ostream.h"
 
 using namespace llvm;
+using namespace llvm::object;
 
 static cl::opt Help("h", cl::desc("Alias for -help"), cl::Hidden);
 
@@ -30,16 +48,42 @@
 static cl::OptionCategory
 ClangLinkerWrapperCategory("clang-linker-wrapper options");
 
+static cl::opt StripSections(
+"strip-sections", cl::ZeroOrMore,
+cl::desc("Strip offloading sections from the host object file."),
+cl::init(true), cl::cat(ClangLinkerWrapperCategory));
+
 static cl::opt LinkerUserPath("linker-path",
cl::desc("Path of linker binary"),
cl::cat(ClangLinkerWrapperCategory));
 
-// Do not parse linker options
+// Do not parse linker options.
 static cl::list
 LinkerArgs(cl::Sink, cl::desc("..."));
 
-static Error runLinker(std::string LinkerPath,
-   SmallVectorImpl &Args) {
+/// Path of the current binary.
+static std::string LinkerExecutable;
+
+/// Magic section string that marks the existence of offloading data. The
+/// section string will be formatted as `.llvm.offloading..`.
+#define OFFLOAD_SECTION_MAGIC_STR ".llvm.offloading"
+
+struct DeviceFile {
+  DeviceFile(StringRef TheTriple, StringRef Arch, StringRef Filename)
+  : TheTriple(TheTriple), Arch(Arch), Filename(Filename) {}
+
+  const Triple TheTriple;
+  const std::string Arch;
+  const std::string Filename;
+};
+
+namespace {
+
+Expected>
+extractFromBuffer(std::unique_ptr Buffer,
+  SmallVectorImpl &DeviceFiles);
+
+Error runLinker(std::string &LinkerPath, SmallVectorImpl &Args) {
   std::vector LinkerArgs;
   LinkerArgs.push_back(LinkerPath);
   for (auto &Arg : Args)
@@ -50,11 +94,301 @@
   return Error::success();
 }
 
-static void PrintVersion(raw_ostream &OS) {
+void PrintVersion(raw_ostream &OS) {
   OS << clang::getClangToolFullVersion("clang-linker-wrapper") << '\n';
 }
 
+void removeFromCompilerUsed(Module &M, GlobalValue &Value) {
+  GlobalVariable *GV = M.getGlobalVariable("llvm.compiler.used");
+  Type *Int8PtrTy = Type::getInt8PtrTy(M.getContext());
+  Constant *ValueToRemove =
+  ConstantExpr::getPointerBitCastOrAddrSpaceCast(&Value, Int8PtrTy);
+  SmallPtrSet InitAsSet;
+  SmallVector Init;
+  if (GV) {
+if (GV->hasInitializer()) {
+  auto *CA = cast(GV->getInitializer());
+  for (auto &Op : CA->operands()) {
+Constant *C = cast_or_null(Op);
+if (C != ValueToRemove && InitAsSet.insert(C).second)
+  Init.push_back(C);
+  }
+}
+GV->eraseFromParent();
+  }
+
+  if (Init.empty())
+return;
+
+  ArrayType *ATy = ArrayType::get(Int8PtrTy, Init.size());
+  GV = new llvm::GlobalVariable(M, ATy, false, GlobalValue::AppendingLinkage,
+ConstantArray

[clang] cb7cfae - [OpenMP] Add extra flag handling to linker wrapper

2022-01-31 Thread Joseph Huber via cfe-commits

Author: Joseph Huber
Date: 2022-01-31T23:11:41-05:00
New Revision: cb7cfaec71850d0a6bc6e00729f0ef6107c51419

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

LOG: [OpenMP] Add extra flag handling to linker wrapper

This patch adds support for a few extra flags in the linker wrapper,
such as debugging flags, verbose output, and passing arguments to ptxas. We also
now forward pass remarks to the LLVM backend so they will show up in the LTO
passes.

Depends on D117049

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

Added: 


Modified: 
clang/lib/Driver/ToolChains/Clang.cpp
clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp

Removed: 




diff  --git a/clang/lib/Driver/ToolChains/Clang.cpp 
b/clang/lib/Driver/ToolChains/Clang.cpp
index dd631339635a3..1ddffcdc24f48 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -8215,6 +8215,37 @@ void LinkerWrapper::ConstructJob(Compilation &C, const 
JobAction &JA,
   Linker->ConstructJob(C, JA, Output, Inputs, Args, LinkingOutput);
   const auto &LinkCommand = C.getJobs().getJobs().back();
 
+  CmdArgs.push_back("-host-triple");
+  CmdArgs.push_back(Args.MakeArgString(getToolChain().getTripleString()));
+  if (Args.hasArg(options::OPT_v))
+CmdArgs.push_back("-v");
+
+  // Add debug information if present.
+  if (const Arg *A = Args.getLastArg(options::OPT_g_Group)) {
+const Option &Opt = A->getOption();
+if (Opt.matches(options::OPT_gN_Group)) {
+  if (Opt.matches(options::OPT_gline_directives_only) ||
+  Opt.matches(options::OPT_gline_tables_only))
+CmdArgs.push_back("-gline-directives-only");
+} else
+  CmdArgs.push_back("-g");
+  }
+
+  for (const auto &A : Args.getAllArgValues(options::OPT_Xcuda_ptxas))
+CmdArgs.push_back(Args.MakeArgString("-ptxas-option=" + A));
+
+  // Forward remarks passes to the LLVM backend in the wrapper.
+  if (const Arg *A = Args.getLastArg(options::OPT_Rpass_EQ))
+CmdArgs.push_back(
+Args.MakeArgString(Twine("-pass-remarks=") + A->getValue()));
+  if (const Arg *A = Args.getLastArg(options::OPT_Rpass_missed_EQ))
+CmdArgs.push_back(
+Args.MakeArgString(Twine("-pass-remarks-missed=") + A->getValue()));
+  if (const Arg *A = Args.getLastArg(options::OPT_Rpass_analysis_EQ))
+CmdArgs.push_back(
+Args.MakeArgString(Twine("-pass-remarks-analysis=") + A->getValue()));
+
+  // Add the linker arguments to be forwarded by the wrapper.
   CmdArgs.push_back("-linker-path");
   CmdArgs.push_back(LinkCommand->getExecutable());
   CmdArgs.push_back("--");
@@ -8224,7 +8255,7 @@ void LinkerWrapper::ConstructJob(Compilation &C, const 
JobAction &JA,
   const char *Exec =
   
Args.MakeArgString(getToolChain().GetProgramPath("clang-linker-wrapper"));
 
-  // Replace the executable and arguments associated with the link job to the
+  // Replace the executable and arguments of the link job with the
   // wrapper.
   LinkCommand->replaceExecutable(Exec);
   LinkCommand->replaceArguments(CmdArgs);

diff  --git a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp 
b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
index 63afbd834cb7a..88dd1eed17110 100644
--- a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
+++ b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
@@ -48,6 +48,12 @@ using namespace llvm::object;
 
 static cl::opt Help("h", cl::desc("Alias for -help"), cl::Hidden);
 
+enum DebugKind {
+  NoDebugInfo,
+  DirectivesOnly,
+  FullDebugInfo,
+};
+
 // Mark all our options with this category, everything else (except for -help)
 // will be hidden.
 static cl::OptionCategory
@@ -58,29 +64,53 @@ static cl::opt StripSections(
 cl::desc("Strip offloading sections from the host object file."),
 cl::init(true), cl::cat(ClangLinkerWrapperCategory));
 
-static cl::opt LinkerUserPath("linker-path",
+static cl::opt LinkerUserPath("linker-path", cl::Required,
cl::desc("Path of linker binary"),

cl::cat(ClangLinkerWrapperCategory));
 
 static cl::opt
-TargetFeatures("target-feature", cl::desc("Target features for triple"),
+TargetFeatures("target-feature", cl::ZeroOrMore,
+   cl::desc("Target features for triple"),
cl::cat(ClangLinkerWrapperCategory));
 
-static cl::opt OptLevel("opt-level",
+static cl::opt OptLevel("opt-level", cl::ZeroOrMore,
  cl::desc("Optimization level for LTO"),
  cl::init("O2"),
  cl::cat(ClangLinkerWrapperCategory));
 
 static cl::opt
-BitcodeLibrary("target-library",
+BitcodeLibrary("target

[clang] f28c315 - [OpenMP] Add support for embedding bitcode images in wrapper tool

2022-01-31 Thread Joseph Huber via cfe-commits

Author: Joseph Huber
Date: 2022-01-31T23:11:41-05:00
New Revision: f28c3153ee6d93aa07610682519bdf0ea93755b4

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

LOG: [OpenMP] Add support for embedding bitcode images in wrapper tool

Summary;
This patch adds support for embedding device images in the linker
wrapper tool. This will be used for performing JIT functionality in the
future.

Depends on D117048

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

Added: 


Modified: 
clang/lib/Driver/ToolChains/Clang.cpp
clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp

Removed: 




diff  --git a/clang/lib/Driver/ToolChains/Clang.cpp 
b/clang/lib/Driver/ToolChains/Clang.cpp
index 7cc47b74ca916..dd631339635a3 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -8217,6 +8217,7 @@ void LinkerWrapper::ConstructJob(Compilation &C, const 
JobAction &JA,
 
   CmdArgs.push_back("-linker-path");
   CmdArgs.push_back(LinkCommand->getExecutable());
+  CmdArgs.push_back("--");
   for (const char *LinkArg : LinkCommand->getArguments())
 CmdArgs.push_back(LinkArg);
 

diff  --git a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp 
b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
index 27f4bdf153c53..63afbd834cb7a 100644
--- a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
+++ b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
@@ -76,12 +76,18 @@ static cl::opt
cl::desc("Path for the target bitcode library"),
cl::cat(ClangLinkerWrapperCategory));
 
+static cl::opt EmbedBC(
+"target-embed-bc", cl::ZeroOrMore,
+cl::desc("Embed linked bitcode instead of an executable device image."),
+cl::init(false), cl::cat(ClangLinkerWrapperCategory));
+
 // Do not parse linker options.
 static cl::list
-HostLinkerArgs(cl::Sink, cl::desc("..."));
+HostLinkerArgs(cl::Positional,
+   cl::desc("..."));
 
 /// Path of the current binary.
-static std::string LinkerExecutable;
+static const char *LinkerExecutable;
 
 /// Temporary files created by the linker wrapper.
 static SmallVector TempFiles;
@@ -411,8 +417,8 @@ extractFromArchive(const Archive &Library,
 
   std::unique_ptr Buffer =
   MemoryBuffer::getMemBuffer(Library.getMemoryBufferRef(), false);
-  if (Error Err = writeArchive(TempFile, Members, true, Library.kind(),
-true, Library.isThin(), std::move(Buffer)))
+  if (Error Err = writeArchive(TempFile, Members, true, Library.kind(), true,
+   Library.isThin(), std::move(Buffer)))
 return std::move(Err);
 
   return static_cast(TempFile);
@@ -489,7 +495,7 @@ Expected assemble(StringRef InputFile, Triple 
TheTriple,
   return static_cast(TempFile);
 }
 
-Expected link(ArrayRef InputFiles,
+Expected link(ArrayRef InputFiles,
ArrayRef LinkerArgs, Triple TheTriple,
StringRef Arch) {
   // NVPTX uses the nvlink binary to link device object files.
@@ -520,7 +526,7 @@ Expected link(ArrayRef InputFiles,
   CmdArgs.push_back(Arg);
 
   // Add extracted input files.
-  for (auto Input : InputFiles)
+  for (StringRef Input : InputFiles)
 CmdArgs.push_back(Input);
 
   if (sys::ExecuteAndWait(*NvlinkPath, CmdArgs))
@@ -530,7 +536,7 @@ Expected link(ArrayRef InputFiles,
 }
 } // namespace nvptx
 
-Expected linkDevice(ArrayRef InputFiles,
+Expected linkDevice(ArrayRef InputFiles,
  ArrayRef LinkerArgs,
  Triple TheTriple, StringRef Arch) {
   switch (TheTriple.getArch()) {
@@ -597,8 +603,10 @@ CodeGenOpt::Level getCGOptLevel(unsigned OptLevel) {
   llvm_unreachable("Invalid optimization level");
 }
 
-std::unique_ptr createLTO(const Triple &TheTriple, StringRef Arch,
-bool WholeProgram) {
+template >
+std::unique_ptr createLTO(
+const Triple &TheTriple, StringRef Arch, bool WholeProgram,
+ModuleHook Hook = [](size_t, const Module &) { return true; }) {
   lto::Config Conf;
   lto::ThinBackend Backend;
   // TODO: Handle index-only thin-LTO
@@ -617,7 +625,7 @@ std::unique_ptr createLTO(const Triple 
&TheTriple, StringRef Arch,
   Conf.PTO.LoopVectorization = Conf.OptLevel > 1;
   Conf.PTO.SLPVectorization = Conf.OptLevel > 1;
 
-  // TODO: Handle outputting bitcode using a module hook.
+  Conf.PostInternalizeModuleHook = Hook;
   if (TheTriple.isNVPTX())
 Conf.CGFileType = CGFT_AssemblyFile;
   else
@@ -637,11 +645,11 @@ bool isValidCIdentifier(StringRef S) {
  [](char C) { return C == '_' || isAlnum(C); });
 }
 
-Expected> linkBitcodeFiles(ArrayRef 
InputFiles,
- 

[clang] 3762111 - [OpenMP] Link the bitcode library late for device LTO

2022-01-31 Thread Joseph Huber via cfe-commits

Author: Joseph Huber
Date: 2022-01-31T23:11:41-05:00
New Revision: 3762111aa9608fce12c4f938bfef2b38aed766dd

URL: 
https://github.com/llvm/llvm-project/commit/3762111aa9608fce12c4f938bfef2b38aed766dd
DIFF: 
https://github.com/llvm/llvm-project/commit/3762111aa9608fce12c4f938bfef2b38aed766dd.diff

LOG: [OpenMP] Link the bitcode library late for device LTO

Summary:
This patch adds support for linking the OpenMP device bitcode library
late when doing LTO. This simply passes it in as an additional device
file when doing the final device linking phase with LTO. This has the
advantage that we don't link it multiple times, and the device
references do not get inlined and prevent us from doing needed OpenMP
optimizations when we have visiblity of the whole module.
Fix some failings where the implicit conversion of an Error to an
Expected triggered the deleted copy constructor.

Depends on D116675

Differential revision: https://reviews.llvm.org/D117048

Added: 


Modified: 
clang/lib/Driver/ToolChains/AMDGPUOpenMP.cpp
clang/lib/Driver/ToolChains/Clang.cpp
clang/lib/Driver/ToolChains/Cuda.cpp
clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp

Removed: 




diff  --git a/clang/lib/Driver/ToolChains/AMDGPUOpenMP.cpp 
b/clang/lib/Driver/ToolChains/AMDGPUOpenMP.cpp
index 6899f9360da5d..d7cf41e4b6605 100644
--- a/clang/lib/Driver/ToolChains/AMDGPUOpenMP.cpp
+++ b/clang/lib/Driver/ToolChains/AMDGPUOpenMP.cpp
@@ -285,6 +285,10 @@ void AMDGPUOpenMPToolChain::addClangTargetOptions(
   if (DriverArgs.hasArg(options::OPT_nogpulib))
 return;
 
+  // Link the bitcode library late if we're using device LTO.
+  if (getDriver().isUsingLTO(/* IsOffload */ true))
+return;
+
   std::string BitcodeSuffix;
   if (DriverArgs.hasFlag(options::OPT_fopenmp_target_new_runtime,
  options::OPT_fno_openmp_target_new_runtime, true))

diff  --git a/clang/lib/Driver/ToolChains/Clang.cpp 
b/clang/lib/Driver/ToolChains/Clang.cpp
index 8d300a9618705..7cc47b74ca916 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -8164,6 +8164,34 @@ void LinkerWrapper::ConstructJob(Compilation &C, const 
JobAction &JA,
   "-target-feature=" + TC->getTripleString() + "=" + *(FeatureIt + 
1)));
 }
 
+// Pass in the bitcode library to be linked during LTO.
+for (auto TI = OpenMPTCRange.first, TE = OpenMPTCRange.second; TI != TE;
+ ++TI) {
+  const ToolChain *TC = TI->second;
+  const Driver &D = TC->getDriver();
+  const ArgList &TCArgs = C.getArgsForToolChain(TC, "", 
Action::OFK_OpenMP);
+  StringRef Arch = TCArgs.getLastArgValue(options::OPT_march_EQ);
+
+  std::string BitcodeSuffix;
+  if (TCArgs.hasFlag(options::OPT_fopenmp_target_new_runtime,
+ options::OPT_fno_openmp_target_new_runtime, true))
+BitcodeSuffix += "new-";
+  if (TC->getTriple().isNVPTX())
+BitcodeSuffix += "nvptx-";
+  else if (TC->getTriple().isAMDGPU())
+BitcodeSuffix += "amdgpu-";
+  BitcodeSuffix += Arch;
+
+  ArgStringList BitcodeLibrary;
+  addOpenMPDeviceRTL(D, TCArgs, BitcodeLibrary, BitcodeSuffix,
+ TC->getTriple());
+
+  if (!BitcodeLibrary.empty())
+CmdArgs.push_back(
+Args.MakeArgString("-target-library=" + TC->getTripleString() +
+   "-" + Arch + "=" + BitcodeLibrary.back()));
+}
+
 // Pass in the optimization level to use for LTO.
 if (const Arg *A = Args.getLastArg(options::OPT_O_Group)) {
   StringRef OOpt;

diff  --git a/clang/lib/Driver/ToolChains/Cuda.cpp 
b/clang/lib/Driver/ToolChains/Cuda.cpp
index 7324339efaa62..4a9f6d4c4e3e4 100644
--- a/clang/lib/Driver/ToolChains/Cuda.cpp
+++ b/clang/lib/Driver/ToolChains/Cuda.cpp
@@ -744,6 +744,10 @@ void CudaToolChain::addClangTargetOptions(
   return;
 }
 
+// Link the bitcode library late if we're using device LTO.
+if (getDriver().isUsingLTO(/* IsOffload */ true))
+  return;
+
 std::string BitcodeSuffix;
 if (DriverArgs.hasFlag(options::OPT_fopenmp_target_new_runtime,
options::OPT_fno_openmp_target_new_runtime, true))

diff  --git a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp 
b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
index f44bc46a62440..27f4bdf153c53 100644
--- a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
+++ b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
@@ -68,9 +68,14 @@ static cl::opt
 
 static cl::opt OptLevel("opt-level",
  cl::desc("Optimization level for LTO"),
- cl::init("O0"),
+ cl::init("O2"),
  cl::cat(ClangLinkerWrapperCategory));
 
+static cl::opt
+BitcodeLibrary("target-l

[clang] c732c3d - [OpenMP] Initial Implementation of LTO and bitcode linking in linker wrapper

2022-01-31 Thread Joseph Huber via cfe-commits

Author: Joseph Huber
Date: 2022-01-31T23:11:41-05:00
New Revision: c732c3df749b3d5a127708c05656a226ed5753ab

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

LOG: [OpenMP] Initial Implementation of LTO and bitcode linking in linker 
wrapper

This patch implements the fist support for handling LTO in the
offloading pipeline. The flag `-foffload-lto` is used to control if
bitcode is embedded into the device. If bitcode is found in the device,
the extracted files will be sent to the LTO pipeline to be linked and
sent to the backend. This implementation does not separately link the
device bitcode libraries yet.

Depends on D116675

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

Added: 


Modified: 
clang/lib/Driver/Driver.cpp
clang/lib/Driver/ToolChains/Clang.cpp
clang/tools/clang-linker-wrapper/CMakeLists.txt
clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp

Removed: 




diff  --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp
index b8ef960f30459..3bfddeefc7b2b 100644
--- a/clang/lib/Driver/Driver.cpp
+++ b/clang/lib/Driver/Driver.cpp
@@ -4083,7 +4083,7 @@ Action *Driver::BuildOffloadingActions(Compilation &C,
 
 auto TC = ToolChains.begin();
 for (Action *&A : DeviceActions) {
-  A = ConstructPhaseAction(C, Args, Phase, A);
+  A = ConstructPhaseAction(C, Args, Phase, A, Action::OFK_OpenMP);
 
   if (isa(A)) {
 HostAction->setCannotBeCollapsedWithNextDependentAction();
@@ -4203,6 +4203,12 @@ Action *Driver::ConstructPhaseAction(
   Args.hasArg(options::OPT_S) ? types::TY_LTO_IR : types::TY_LTO_BC;
   return C.MakeAction(Input, Output);
 }
+if (isUsingLTO(/* IsOffload */ true) &&
+TargetDeviceOffloadKind == Action::OFK_OpenMP) {
+  types::ID Output =
+  Args.hasArg(options::OPT_S) ? types::TY_LTO_IR : types::TY_LTO_BC;
+  return C.MakeAction(Input, Output);
+}
 if (Args.hasArg(options::OPT_emit_llvm) ||
 (TargetDeviceOffloadKind == Action::OFK_HIP &&
  Args.hasFlag(options::OPT_fgpu_rdc, options::OPT_fno_gpu_rdc,

diff  --git a/clang/lib/Driver/ToolChains/Clang.cpp 
b/clang/lib/Driver/ToolChains/Clang.cpp
index 59139eca54ee9..8d300a9618705 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -4619,7 +4619,7 @@ void Clang::ConstructJob(Compilation &C, const JobAction 
&JA,
 if (JA.getType() == types::TY_LLVM_BC)
   CmdArgs.push_back("-emit-llvm-uselists");
 
-if (IsUsingLTO) {
+if (IsUsingLTO && !Args.hasArg(options::OPT_fopenmp_new_driver)) {
   // Only AMDGPU supports device-side LTO.
   if (IsDeviceOffloadAction && !Triple.isAMDGPU()) {
 D.Diag(diag::err_drv_unsupported_opt_for_target)
@@ -8150,6 +8150,39 @@ void LinkerWrapper::ConstructJob(Compilation &C, const 
JobAction &JA,
  const char *LinkingOutput) const {
   ArgStringList CmdArgs;
 
+  if (getToolChain().getDriver().isUsingLTO(/* IsOffload */ true)) {
+// Pass in target features for each toolchain.
+auto OpenMPTCRange = C.getOffloadToolChains();
+for (auto TI = OpenMPTCRange.first, TE = OpenMPTCRange.second; TI != TE;
+ ++TI) {
+  const ToolChain *TC = TI->second;
+  const ArgList &TCArgs = C.getArgsForToolChain(TC, "", 
Action::OFK_OpenMP);
+  ArgStringList FeatureArgs;
+  TC->addClangTargetOptions(TCArgs, FeatureArgs, Action::OFK_OpenMP);
+  auto FeatureIt = llvm::find(FeatureArgs, "-target-feature");
+  CmdArgs.push_back(Args.MakeArgString(
+  "-target-feature=" + TC->getTripleString() + "=" + *(FeatureIt + 
1)));
+}
+
+// Pass in the optimization level to use for LTO.
+if (const Arg *A = Args.getLastArg(options::OPT_O_Group)) {
+  StringRef OOpt;
+  if (A->getOption().matches(options::OPT_O4) ||
+  A->getOption().matches(options::OPT_Ofast))
+OOpt = "3";
+  else if (A->getOption().matches(options::OPT_O)) {
+OOpt = A->getValue();
+if (OOpt == "g")
+  OOpt = "1";
+else if (OOpt == "s" || OOpt == "z")
+  OOpt = "2";
+  } else if (A->getOption().matches(options::OPT_O0))
+OOpt = "0";
+  if (!OOpt.empty())
+CmdArgs.push_back(Args.MakeArgString(Twine("-opt-level=O") + OOpt));
+}
+  }
+
   // Construct the link job so we can wrap around it.
   Linker->ConstructJob(C, JA, Output, Inputs, Args, LinkingOutput);
   const auto &LinkCommand = C.getJobs().getJobs().back();

diff  --git a/clang/tools/clang-linker-wrapper/CMakeLists.txt 
b/clang/tools/clang-linker-wrapper/CMakeLists.txt
index 4e64a5f164ac6..3e6d67fed4cff 100644
--- a/clang/tools/clang-linker-wrapper/CMakeLists.txt
+++ b/clang/tools/clang-linker-wrapper/CMakeLists

[clang] 0e82c75 - [OpenMP] Search for static libraries in offload linker tool

2022-01-31 Thread Joseph Huber via cfe-commits

Author: Joseph Huber
Date: 2022-01-31T23:11:41-05:00
New Revision: 0e82c7553be95343f1cb62946eb0b84bd26f1563

URL: 
https://github.com/llvm/llvm-project/commit/0e82c7553be95343f1cb62946eb0b84bd26f1563
DIFF: 
https://github.com/llvm/llvm-project/commit/0e82c7553be95343f1cb62946eb0b84bd26f1563.diff

LOG: [OpenMP] Search for static libraries in offload linker tool

This patch adds support for searching through the linker library paths
to identify static libraries that may contain device code. If device
code is present it will be extracted. This should ideally fully support
static linking with OpenMP offloading.

Depends on D116627

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

Added: 


Modified: 
clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp

Removed: 




diff  --git a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp 
b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
index 0072ce7a0080b..de51e3683632e 100644
--- a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
+++ b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
@@ -65,7 +65,9 @@ static cl::list
 /// Path of the current binary.
 static std::string LinkerExecutable;
 
+/// Temporary files created by the linker wrapper.
 static SmallVector TempFiles;
+
 /// Magic section string that marks the existence of offloading data. The
 /// section string will be formatted as `.llvm.offloading..`.
 #define OFFLOAD_SECTION_MAGIC_STR ".llvm.offloading."
@@ -551,6 +553,44 @@ Expected wrapDeviceImage(StringRef ImageFile) 
{
   return static_cast(ObjectFile);
 }
 
+Optional findFile(StringRef Dir, const Twine &Name) {
+  SmallString<128> Path;
+  // TODO: Parse `--sysroot` somewhere and use it here.
+  sys::path::append(Path, Dir, Name);
+  if (sys::fs::exists(Path))
+return static_cast(Path);
+  return None;
+}
+
+Optional findFromSearchPaths(StringRef Name,
+  ArrayRef SearchPaths) {
+  for (StringRef Dir : SearchPaths)
+if (Optional File = findFile(Dir, Name))
+  return File;
+  return None;
+}
+
+Optional searchLibraryBaseName(StringRef Name,
+ArrayRef SearchPaths) {
+  for (StringRef Dir : SearchPaths) {
+if (Optional File = findFile(Dir, "lib" + Name + ".a"))
+  return File;
+  }
+  return None;
+}
+
+/// Search for static libraries in the linker's library path given input like
+/// `-lfoo` or `-l:libfoo.a`.
+Optional searchLibrary(StringRef Input,
+ArrayRef SearchPaths) {
+  if (!Input.startswith("-l"))
+return None;
+  StringRef Name = Input.drop_front(2);
+  if (Name.startswith(":"))
+return findFromSearchPaths(Name.drop_front(), SearchPaths);
+  return searchLibraryBaseName(Name, SearchPaths);
+}
+
 } // namespace
 
 int main(int argc, const char **argv) {
@@ -581,16 +621,26 @@ int main(int argc, const char **argv) {
   for (const std::string &Arg : HostLinkerArgs)
 LinkerArgs.push_back(Arg);
 
+  SmallVector LibraryPaths;
+  for (const StringRef Arg : LinkerArgs)
+if (Arg.startswith("-L"))
+  LibraryPaths.push_back(Arg.drop_front(2));
+
   // Try to extract device code from the linker input and replace the linker
   // input with a new file that has the device section stripped.
   SmallVector DeviceFiles;
   for (std::string &Arg : LinkerArgs) {
-if (sys::path::extension(Arg) == ".o" ||
-sys::path::extension(Arg) == ".a") {
+// Search for static libraries in the library link path.
+std::string Filename = Arg;
+if (Optional Library = searchLibrary(Arg, LibraryPaths))
+  Filename = *Library;
+
+if (sys::path::extension(Filename) == ".o" ||
+sys::path::extension(Filename) == ".a") {
   ErrorOr> BufferOrErr =
-  MemoryBuffer::getFileOrSTDIN(Arg);
+  MemoryBuffer::getFileOrSTDIN(Filename);
   if (std::error_code EC = BufferOrErr.getError())
-return reportError(createFileError(Arg, EC));
+return reportError(createFileError(Filename, EC));
 
   auto NewFileOrErr =
   extractFromBuffer(std::move(*BufferOrErr), DeviceFiles);



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] d074458 - [Clang] Initial support for linking offloading code in tool

2022-01-31 Thread Joseph Huber via cfe-commits

Author: Joseph Huber
Date: 2022-01-31T23:11:41-05:00
New Revision: d0744585f9ea842ca97855b2e002fddc893c4a92

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

LOG: [Clang] Initial support for linking offloading code in tool

This patch adds the initial support for linking NVPTX offloading code
using the clang-linker-wrapper tool. This uses the extracted device
files and runs `nvlink` on them. Currently this is then passed to the
existing toolchain for creating linkable OpenMP offloading programs
using `clang-offload-wrapper` and compiling it manually using `llc`.
More work is required to support LTO, Bitcode linking, AMDGPU, and x86
offloading.

Depends on D116545

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

Added: 


Modified: 
clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp

Removed: 




diff  --git a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp 
b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
index bbbdffc534a66..0072ce7a0080b 100644
--- a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
+++ b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
@@ -28,6 +28,7 @@
 #include "llvm/Support/Errc.h"
 #include "llvm/Support/FileOutputBuffer.h"
 #include "llvm/Support/FileSystem.h"
+#include "llvm/Support/Host.h"
 #include "llvm/Support/InitLLVM.h"
 #include "llvm/Support/MemoryBuffer.h"
 #include "llvm/Support/Path.h"
@@ -59,22 +60,26 @@ static cl::opt LinkerUserPath("linker-path",
 
 // Do not parse linker options.
 static cl::list
-LinkerArgs(cl::Sink, cl::desc("..."));
+HostLinkerArgs(cl::Sink, cl::desc("..."));
 
 /// Path of the current binary.
 static std::string LinkerExecutable;
 
+static SmallVector TempFiles;
 /// Magic section string that marks the existence of offloading data. The
 /// section string will be formatted as `.llvm.offloading..`.
-#define OFFLOAD_SECTION_MAGIC_STR ".llvm.offloading"
+#define OFFLOAD_SECTION_MAGIC_STR ".llvm.offloading."
 
+/// Information for a device offloading file extracted from the host.
 struct DeviceFile {
   DeviceFile(StringRef TheTriple, StringRef Arch, StringRef Filename)
   : TheTriple(TheTriple), Arch(Arch), Filename(Filename) {}
 
-  const Triple TheTriple;
+  const std::string TheTriple;
   const std::string Arch;
   const std::string Filename;
+
+  operator std::string() const { return TheTriple + "-" + Arch; }
 };
 
 namespace {
@@ -83,6 +88,16 @@ Expected>
 extractFromBuffer(std::unique_ptr Buffer,
   SmallVectorImpl &DeviceFiles);
 
+static StringRef getDeviceFileExtension(StringRef DeviceTriple,
+bool IsBitcode = false) {
+  Triple TheTriple(DeviceTriple);
+  if (TheTriple.isAMDGPU() || IsBitcode)
+return "bc";
+  if (TheTriple.isNVPTX())
+return "cubin";
+  return "o";
+}
+
 Error runLinker(std::string &LinkerPath, SmallVectorImpl &Args) {
   std::vector LinkerArgs;
   LinkerArgs.push_back(LinkerPath);
@@ -150,9 +165,12 @@ extractFromBinary(const ObjectFile &Obj,
 
 if (Expected Contents = Sec.getContents()) {
   SmallString<128> TempFile;
+  StringRef DeviceExtension = getDeviceFileExtension(
+  DeviceTriple, identify_magic(*Contents) == file_magic::bitcode);
   if (std::error_code EC = sys::fs::createTemporaryFile(
-  Prefix + "-device-" + DeviceTriple, Extension, TempFile))
+  Prefix + "-device-" + DeviceTriple, DeviceExtension, TempFile))
 return createFileError(TempFile, EC);
+  TempFiles.push_back(static_cast(TempFile));
 
   Expected> OutputOrErr =
   FileOutputBuffer::create(TempFile, Sec.getSize());
@@ -173,10 +191,7 @@ extractFromBinary(const ObjectFile &Obj,
 
   // We will use llvm-strip to remove the now unneeded section containing the
   // offloading code.
-  ErrorOr StripPath = sys::findProgramByName(
-  "llvm-strip", sys::path::parent_path(LinkerExecutable));
-  if (!StripPath)
-StripPath = sys::findProgramByName("llvm-strip");
+  ErrorOr StripPath = sys::findProgramByName("llvm-strip");
   if (!StripPath)
 return createStringError(StripPath.getError(),
  "Unable to find 'llvm-strip' in path");
@@ -185,6 +200,7 @@ extractFromBinary(const ObjectFile &Obj,
   if (std::error_code EC =
   sys::fs::createTemporaryFile(Prefix + "-host", Extension, TempFile))
 return createFileError(TempFile, EC);
+  TempFiles.push_back(static_cast(TempFile));
 
   SmallVector StripArgs;
   StripArgs.push_back(*StripPath);
@@ -237,9 +253,12 @@ extractFromBitcode(std::unique_ptr Buffer,
 
 StringRef Contents = CDS->getAsString();
 SmallString<128> TempFile;
+StringRef DeviceExtension = getDeviceFileExtension(
+DeviceTriple, identify_magic(Contents) == file_magi

[clang] b8239af - [OpenMP] Add support for extracting device code in linker wrapper

2022-01-31 Thread Joseph Huber via cfe-commits

Author: Joseph Huber
Date: 2022-01-31T23:11:41-05:00
New Revision: b8239af0eeedfa67d9fac28d20685b7a09e2935a

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

LOG: [OpenMP] Add support for extracting device code in linker wrapper

This patchs add support for extracting device offloading code from the
linker's input files. If the file contains a section with the name
`.llvm.offloading..` it will be extracted to a new
temporary file to be linked. Addtionally, the host file containing it
will have the section stripped so it does not remain in the executable
once linked.

Depends on D116544

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

Added: 


Modified: 
clang/tools/clang-linker-wrapper/CMakeLists.txt
clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp

Removed: 




diff  --git a/clang/tools/clang-linker-wrapper/CMakeLists.txt 
b/clang/tools/clang-linker-wrapper/CMakeLists.txt
index 17c50b6d80cad..4e64a5f164ac6 100644
--- a/clang/tools/clang-linker-wrapper/CMakeLists.txt
+++ b/clang/tools/clang-linker-wrapper/CMakeLists.txt
@@ -1,4 +1,4 @@
-set(LLVM_LINK_COMPONENTS BitWriter Core Object Support)
+set(LLVM_LINK_COMPONENTS BitWriter Core BinaryFormat IRReader Object Support)
 
 if(NOT CLANG_BUILT_STANDALONE)
   set(tablegen_deps intrinsics_gen)

diff  --git a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp 
b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
index 5cadb3dbb1e98..bbbdffc534a66 100644
--- a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
+++ b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
@@ -5,23 +5,41 @@
 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 //
 //===-===//
-///
+//
+// This tool works as a wrapper over a linking job. This tool is used to create
+// linked device images for offloading. It scans the linker's input for 
embedded
+// device offloading data stored in sections `.llvm.offloading..`
+// and extracts it as a temporary file. The extracted device files will then be
+// passed to a device linking job to create a final device image.
+//
 //===-===//
 
 #include "clang/Basic/Version.h"
+#include "llvm/BinaryFormat/Magic.h"
+#include "llvm/Bitcode/BitcodeWriter.h"
+#include "llvm/IR/Constants.h"
+#include "llvm/IR/Module.h"
+#include "llvm/IRReader/IRReader.h"
 #include "llvm/Object/Archive.h"
+#include "llvm/Object/ArchiveWriter.h"
+#include "llvm/Object/Binary.h"
+#include "llvm/Object/ObjectFile.h"
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Support/Errc.h"
+#include "llvm/Support/FileOutputBuffer.h"
 #include "llvm/Support/FileSystem.h"
+#include "llvm/Support/InitLLVM.h"
 #include "llvm/Support/MemoryBuffer.h"
 #include "llvm/Support/Path.h"
 #include "llvm/Support/Program.h"
 #include "llvm/Support/Signals.h"
+#include "llvm/Support/SourceMgr.h"
 #include "llvm/Support/StringSaver.h"
 #include "llvm/Support/WithColor.h"
 #include "llvm/Support/raw_ostream.h"
 
 using namespace llvm;
+using namespace llvm::object;
 
 static cl::opt Help("h", cl::desc("Alias for -help"), cl::Hidden);
 
@@ -30,16 +48,42 @@ static cl::opt Help("h", cl::desc("Alias for -help"), 
cl::Hidden);
 static cl::OptionCategory
 ClangLinkerWrapperCategory("clang-linker-wrapper options");
 
+static cl::opt StripSections(
+"strip-sections", cl::ZeroOrMore,
+cl::desc("Strip offloading sections from the host object file."),
+cl::init(true), cl::cat(ClangLinkerWrapperCategory));
+
 static cl::opt LinkerUserPath("linker-path",
cl::desc("Path of linker binary"),

cl::cat(ClangLinkerWrapperCategory));
 
-// Do not parse linker options
+// Do not parse linker options.
 static cl::list
 LinkerArgs(cl::Sink, cl::desc("..."));
 
-static Error runLinker(std::string LinkerPath,
-   SmallVectorImpl &Args) {
+/// Path of the current binary.
+static std::string LinkerExecutable;
+
+/// Magic section string that marks the existence of offloading data. The
+/// section string will be formatted as `.llvm.offloading..`.
+#define OFFLOAD_SECTION_MAGIC_STR ".llvm.offloading"
+
+struct DeviceFile {
+  DeviceFile(StringRef TheTriple, StringRef Arch, StringRef Filename)
+  : TheTriple(TheTriple), Arch(Arch), Filename(Filename) {}
+
+  const Triple TheTriple;
+  const std::string Arch;
+  const std::string Filename;
+};
+
+namespace {
+
+Expected>
+extractFromBuffer(std::unique_ptr Buffer,
+  SmallVectorImpl &DeviceFiles);
+
+Error runLinker(std::string &LinkerPath, SmallVectorImpl &Args) {
   std::vector LinkerArgs;
   LinkerArgs.push_back(LinkerPath

[PATCH] D117049: [OpenMP] Add support for embedding bitcode images in wrapper tool

2022-01-31 Thread Shilei Tian via Phabricator via cfe-commits
tianshilei1992 accepted this revision.
tianshilei1992 added a comment.
This revision is now accepted and ready to land.

I'm gonna accept it for now to make it in 14. We can always come back later if 
we see anything wrong.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D117049/new/

https://reviews.llvm.org/D117049

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D117049: [OpenMP] Add support for embedding bitcode images in wrapper tool

2022-01-31 Thread Joseph Huber via Phabricator via cfe-commits
jhuber6 updated this revision to Diff 404785.
jhuber6 added a comment.

Removing clang flag because LTO won't be supported when these land.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D117049/new/

https://reviews.llvm.org/D117049

Files:
  clang/lib/Driver/ToolChains/Clang.cpp
  clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp

Index: clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
===
--- clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
+++ clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
@@ -76,12 +76,18 @@
cl::desc("Path for the target bitcode library"),
cl::cat(ClangLinkerWrapperCategory));
 
+static cl::opt EmbedBC(
+"target-embed-bc", cl::ZeroOrMore,
+cl::desc("Embed linked bitcode instead of an executable device image."),
+cl::init(false), cl::cat(ClangLinkerWrapperCategory));
+
 // Do not parse linker options.
 static cl::list
-HostLinkerArgs(cl::Sink, cl::desc("..."));
+HostLinkerArgs(cl::Positional,
+   cl::desc("..."));
 
 /// Path of the current binary.
-static std::string LinkerExecutable;
+static const char *LinkerExecutable;
 
 /// Temporary files created by the linker wrapper.
 static SmallVector TempFiles;
@@ -411,8 +417,8 @@
 
   std::unique_ptr Buffer =
   MemoryBuffer::getMemBuffer(Library.getMemoryBufferRef(), false);
-  if (Error Err = writeArchive(TempFile, Members, true, Library.kind(),
-true, Library.isThin(), std::move(Buffer)))
+  if (Error Err = writeArchive(TempFile, Members, true, Library.kind(), true,
+   Library.isThin(), std::move(Buffer)))
 return std::move(Err);
 
   return static_cast(TempFile);
@@ -489,7 +495,7 @@
   return static_cast(TempFile);
 }
 
-Expected link(ArrayRef InputFiles,
+Expected link(ArrayRef InputFiles,
ArrayRef LinkerArgs, Triple TheTriple,
StringRef Arch) {
   // NVPTX uses the nvlink binary to link device object files.
@@ -520,7 +526,7 @@
   CmdArgs.push_back(Arg);
 
   // Add extracted input files.
-  for (auto Input : InputFiles)
+  for (StringRef Input : InputFiles)
 CmdArgs.push_back(Input);
 
   if (sys::ExecuteAndWait(*NvlinkPath, CmdArgs))
@@ -530,7 +536,7 @@
 }
 } // namespace nvptx
 
-Expected linkDevice(ArrayRef InputFiles,
+Expected linkDevice(ArrayRef InputFiles,
  ArrayRef LinkerArgs,
  Triple TheTriple, StringRef Arch) {
   switch (TheTriple.getArch()) {
@@ -597,8 +603,10 @@
   llvm_unreachable("Invalid optimization level");
 }
 
-std::unique_ptr createLTO(const Triple &TheTriple, StringRef Arch,
-bool WholeProgram) {
+template >
+std::unique_ptr createLTO(
+const Triple &TheTriple, StringRef Arch, bool WholeProgram,
+ModuleHook Hook = [](size_t, const Module &) { return true; }) {
   lto::Config Conf;
   lto::ThinBackend Backend;
   // TODO: Handle index-only thin-LTO
@@ -617,7 +625,7 @@
   Conf.PTO.LoopVectorization = Conf.OptLevel > 1;
   Conf.PTO.SLPVectorization = Conf.OptLevel > 1;
 
-  // TODO: Handle outputting bitcode using a module hook.
+  Conf.PostInternalizeModuleHook = Hook;
   if (TheTriple.isNVPTX())
 Conf.CGFileType = CGFT_AssemblyFile;
   else
@@ -637,11 +645,11 @@
  [](char C) { return C == '_' || isAlnum(C); });
 }
 
-Expected> linkBitcodeFiles(ArrayRef InputFiles,
- const Triple &TheTriple,
- StringRef Arch) {
+Error linkBitcodeFiles(SmallVectorImpl &InputFiles,
+   const Triple &TheTriple, StringRef Arch) {
   SmallVector, 4> SavedBuffers;
   SmallVector, 4> BitcodeFiles;
+  SmallVector NewInputFiles;
   StringMap UsedInRegularObj;
 
   // Search for bitcode files in the input and create an LTO input file. If it
@@ -660,6 +668,7 @@
   if (!ObjFile)
 return ObjFile.takeError();
 
+  NewInputFiles.push_back(File.str());
   for (auto &Sym : (*ObjFile)->symbols()) {
 Expected Name = Sym.getName();
 if (!Name)
@@ -679,12 +688,36 @@
   }
 
   if (BitcodeFiles.empty())
-return None;
+return Error::success();
+
+  auto HandleError = [&](std::error_code EC) {
+logAllUnhandledErrors(errorCodeToError(EC),
+  WithColor::error(errs(), LinkerExecutable));
+exit(1);
+  };
+
+  // LTO Module hook to output bitcode without running the backend.
+  auto LinkOnly = [&](size_t Task, const Module &M) {
+SmallString<128> TempFile;
+if (std::error_code EC = sys::fs::createTemporaryFile(
+"jit-" + TheTriple.getTriple(), "bc", TempFile))
+  HandleError(EC);
+std::error_code EC;
+raw_fd_ostream LinkedBitcode(TempFile, EC, sys::fs::OF_None);

[PATCH] D118155: [OpenMP] Improve symbol resolution for OpenMP Offloading LTO

2022-01-31 Thread Joseph Huber via Phabricator via cfe-commits
jhuber6 added inline comments.



Comment at: clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp:777-781
+// Record if we've seen these symbols in any object or shared 
libraries.
+if ((*ObjFile)->isRelocatableObject()) {
+  UsedInRegularObj[*Name] = true;
+} else
+  UsedInSharedLib[*Name] = true;

tianshilei1992 wrote:
> 
Fixed.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D118155/new/

https://reviews.llvm.org/D118155

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D118155: [OpenMP] Improve symbol resolution for OpenMP Offloading LTO

2022-01-31 Thread Shilei Tian via Phabricator via cfe-commits
tianshilei1992 added a comment.

LG with one nit.




Comment at: clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp:777-781
+// Record if we've seen these symbols in any object or shared 
libraries.
+if ((*ObjFile)->isRelocatableObject()) {
+  UsedInRegularObj[*Name] = true;
+} else
+  UsedInSharedLib[*Name] = true;




Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D118155/new/

https://reviews.llvm.org/D118155

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D118198: [OpenMP] Remove call to 'clang-offload-wrapper' binary

2022-01-31 Thread Shilei Tian via Phabricator via cfe-commits
tianshilei1992 accepted this revision.
tianshilei1992 added a comment.
This revision is now accepted and ready to land.

LG


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D118198/new/

https://reviews.llvm.org/D118198

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D117246: [OpenMP] Add support for linking AMDGPU images

2022-01-31 Thread Shilei Tian via Phabricator via cfe-commits
tianshilei1992 added a comment.

@JonChesterfield can we just accept the patch and make it in 14? If we later 
find anything broken, we could have bug fix.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D117246/new/

https://reviews.llvm.org/D117246

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D117156: [OpenMP] Add extra flag handling to linker wrapper

2022-01-31 Thread Joseph Huber via Phabricator via cfe-commits
jhuber6 marked an inline comment as done.
jhuber6 added inline comments.



Comment at: clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp:98
+static cl::opt
+PtxasOption("ptxas-option", cl::ZeroOrMore,
+cl::desc("Argument to pass to the ptxas invocation"),

tianshilei1992 wrote:
> `ptxas-args`?
changed.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D117156/new/

https://reviews.llvm.org/D117156

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D117156: [OpenMP] Add extra flag handling to linker wrapper

2022-01-31 Thread Shilei Tian via Phabricator via cfe-commits
tianshilei1992 accepted this revision.
tianshilei1992 added a comment.
This revision is now accepted and ready to land.

LG. One nit.




Comment at: clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp:98
+static cl::opt
+PtxasOption("ptxas-option", cl::ZeroOrMore,
+cl::desc("Argument to pass to the ptxas invocation"),

`ptxas-args`?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D117156/new/

https://reviews.llvm.org/D117156

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D118197: [OpenMP] Replace sysmtem call to `llc` with target machine

2022-01-31 Thread Shilei Tian via Phabricator via cfe-commits
tianshilei1992 accepted this revision.
tianshilei1992 added a comment.
This revision is now accepted and ready to land.

LG


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D118197/new/

https://reviews.llvm.org/D118197

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D116975: [OpenMP] Initial Implementation of LTO and bitcode linking in linker wrapper

2022-01-31 Thread Joseph Huber via Phabricator via cfe-commits
jhuber6 added inline comments.



Comment at: clang/lib/Driver/ToolChains/Clang.cpp:8154
+for (auto TI = OpenMPTCRange.first, TE = OpenMPTCRange.second; TI != TE;
+ ++TI) {
+  const ToolChain *TC = TI->second;

jdoerfert wrote:
> Nit: maybe `for (auto &TI : make_range(OpenMPTCRange.first, 
> OpenMPTCRange.second))`
Will do, forgot about that helper.



Comment at: clang/lib/Driver/ToolChains/Clang.cpp:8180
+CmdArgs.push_back(Args.MakeArgString(Twine("-opt-level=O") + OOpt));
+}
+  }

jdoerfert wrote:
> I thought there is a helper somewhere that does this translation, isn't there?
There might be, but I didn't find an easy one. I copied this from the GNU 
toolchain's handling of the LTO arguments.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D116975/new/

https://reviews.llvm.org/D116975

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D117048: [OpenMP] Link the bitcode library late for device LTO

2022-01-31 Thread Joseph Huber via Phabricator via cfe-commits
jhuber6 added inline comments.



Comment at: clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp:75
+static cl::opt
+BitcodeLibrary("target-library",
+   cl::desc("Path for the target bitcode library"),

tianshilei1992 wrote:
> `target-library` is not the common name we call it. Maybe 
> `device-runtime-library`?
I think I called it `bitcode-library` later, since technically it's just any 
bitcode file that's linked in along LTO.



Comment at: clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp:986
+  if (!BitcodeLibrary.empty()) {
+// FIXME: Hacky workaround to avoid a backend crash at O0.
+if (OptLevel[1] - '0' == 0)

tianshilei1992 wrote:
> Is this still needed now?
It still breaks, but I removed it later. My ordering for these patches is a bit 
of a mess, sorry.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D117048/new/

https://reviews.llvm.org/D117048

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D118670: [NFC][AIX]Disable failed tests due to aggressive byval alignment warning on AIX

2022-01-31 Thread Hubert Tong via Phabricator via cfe-commits
hubert.reinterpretcast added inline comments.



Comment at: clang/test/SemaTemplate/instantiate-attr.cpp:1
 // RUN: %clang_cc1 -fsyntax-only -verify %s
 // expected-no-diagnostics

Add `-Wno-aix-compat` instead?



Comment at: clang/test/SemaTemplate/instantiate-attr.cpp:3-6
+
+// Byval alignment warning is emitted too aggressively on AIX.
+// XFAIL: aix
+

See above suggestion. Please try applying to the other tests too.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D118670/new/

https://reviews.llvm.org/D118670

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D117048: [OpenMP] Link the bitcode library late for device LTO

2022-01-31 Thread Shilei Tian via Phabricator via cfe-commits
tianshilei1992 accepted this revision.
tianshilei1992 added a comment.
This revision is now accepted and ready to land.

LGTM with two nits.




Comment at: clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp:75
+static cl::opt
+BitcodeLibrary("target-library",
+   cl::desc("Path for the target bitcode library"),

`target-library` is not the common name we call it. Maybe 
`device-runtime-library`?



Comment at: clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp:986
+  if (!BitcodeLibrary.empty()) {
+// FIXME: Hacky workaround to avoid a backend crash at O0.
+if (OptLevel[1] - '0' == 0)

Is this still needed now?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D117048/new/

https://reviews.llvm.org/D117048

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D116975: [OpenMP] Initial Implementation of LTO and bitcode linking in linker wrapper

2022-01-31 Thread Johannes Doerfert via Phabricator via cfe-commits
jdoerfert accepted this revision.
jdoerfert added a comment.
This revision is now accepted and ready to land.

LG, as part of the patch set, and given the runtime test for coverage.

Some nits.




Comment at: clang/lib/Driver/ToolChains/Clang.cpp:8154
+for (auto TI = OpenMPTCRange.first, TE = OpenMPTCRange.second; TI != TE;
+ ++TI) {
+  const ToolChain *TC = TI->second;

Nit: maybe `for (auto &TI : make_range(OpenMPTCRange.first, 
OpenMPTCRange.second))`



Comment at: clang/lib/Driver/ToolChains/Clang.cpp:8180
+CmdArgs.push_back(Args.MakeArgString(Twine("-opt-level=O") + OOpt));
+}
+  }

I thought there is a helper somewhere that does this translation, isn't there?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D116975/new/

https://reviews.llvm.org/D116975

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D116627: [Clang] Initial support for linking offloading code in tool

2022-01-31 Thread Johannes Doerfert via Phabricator via cfe-commits
jdoerfert accepted this revision.
jdoerfert added a comment.
This revision is now accepted and ready to land.

LG, as part of the patch set, and given the runtime test for coverage.

Some nits are left for later though.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D116627/new/

https://reviews.llvm.org/D116627

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D116545: [OpenMP] Add support for extracting device code in linker wrapper

2022-01-31 Thread Johannes Doerfert via Phabricator via cfe-commits
jdoerfert accepted this revision.
jdoerfert added a comment.
This revision is now accepted and ready to land.

LG, as part of the patch set, and given the runtime test for coverage.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D116545/new/

https://reviews.llvm.org/D116545

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D54943: [clang-tidy] implement const-transformation for cppcoreguidelines-const-correctness

2022-01-31 Thread Florin Iucha via Phabricator via cfe-commits
0x8000- added inline comments.



Comment at: 
clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines-const-correctness-transform-pointer-as-values.cpp:12
+  // CHECK-MESSAGES: [[@LINE-1]]:3: warning: variable 'p_local0' of type 
'double *' can be declared 'const'
+  // CHECK-FIXES: const
+}

LegalizeAdulthood wrote:
> Shouldn't this validate that the `const` was placed in the correct position?
> e.g. `const double *` is a different meaning from `double *const`
> 
> Apply to all the other `CHECK-FIXES` as well
Can we have the checker merged in first, then we can worry about the automatic 
fixer?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D54943/new/

https://reviews.llvm.org/D54943

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D118596: [clang][dataflow] Enable comparison of distinct values in Environment

2022-01-31 Thread Yitzhak Mandelbaum via Phabricator via cfe-commits
ymandel accepted this revision.
ymandel added a comment.
This revision is now accepted and ready to land.

Please mention the (breaking) API changes to `runDataflowAnalysis`, etc. in the 
patch description.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D118596/new/

https://reviews.llvm.org/D118596

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] 275c562 - Disable -Wmissing-prototypes for internal linkage functions that aren't explicitly marked "static"

2022-01-31 Thread David Blaikie via cfe-commits

Author: David Blaikie
Date: 2022-01-31T17:34:51-08:00
New Revision: 275c56226d7fbd6a4d554807374f78d323aa0c1c

URL: 
https://github.com/llvm/llvm-project/commit/275c56226d7fbd6a4d554807374f78d323aa0c1c
DIFF: 
https://github.com/llvm/llvm-project/commit/275c56226d7fbd6a4d554807374f78d323aa0c1c.diff

LOG: Disable -Wmissing-prototypes for internal linkage functions that aren't 
explicitly marked "static"

Some functions can end up non-externally visible despite not being
declared "static" or in an unnamed namespace in C++ - such as by having
parameters that are of non-external types.

Such functions aren't mistakenly intended to be defining some function
that needs a declaration. They could be maybe more legible (except for
the `operator new` example) with an explicit static, but that's a
stylistic thing outside what should be addressed by a warning.

Added: 


Modified: 
clang/lib/Sema/SemaDecl.cpp
clang/test/SemaCXX/warn-missing-prototypes.cpp

Removed: 




diff  --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index e747ffc6f2ac1..cbd9df4d6a7b7 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -14196,6 +14196,9 @@ ShouldWarnAboutMissingPrototype(const FunctionDecl *FD,
   if (!FD->isGlobal())
 return false;
 
+  if (!FD->isExternallyVisible())
+return false;
+
   // Don't warn about C++ member functions.
   if (isa(FD))
 return false;

diff  --git a/clang/test/SemaCXX/warn-missing-prototypes.cpp 
b/clang/test/SemaCXX/warn-missing-prototypes.cpp
index e8637e5a90eab..2880514ee02b7 100644
--- a/clang/test/SemaCXX/warn-missing-prototypes.cpp
+++ b/clang/test/SemaCXX/warn-missing-prototypes.cpp
@@ -44,3 +44,16 @@ void j() = delete;
 extern void k() {} // expected-warning {{no previous prototype for function 
'k'}}
 // expected-note@-1{{declare 'static' if the function is not intended to be 
used outside of this translation unit}}
 // CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-2]]:{{.*}}-[[@LINE-2]]:{{.*}}}:"{{.*}}"
+
+namespace {
+struct anon { };
+}
+
+// No warning because this has internal linkage despite not being declared
+// explicitly 'static', owing to the internal linkage parameter.
+void l(anon) {
+}
+
+void *operator new(decltype(sizeof(3)) size, const anon &) throw() {
+  return nullptr;
+}



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D118670: [NFC][AIX]Disable failed tests due to aggressive byval alignment warning on AIX

2022-01-31 Thread Steven Wan via Phabricator via cfe-commits
stevewan created this revision.
stevewan requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

These tests emit unexpected diagnostics on AIX because the byval alignment 
warning is emitted too aggressively. https://reviews.llvm.org/D118350 is 
supposed to provide a proper fix to the problem, but for the time being disable 
the tests to unblock.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D118670

Files:
  clang/test/Analysis/padding_c.c
  clang/test/Analysis/padding_cpp.cpp
  clang/test/CXX/drs/dr6xx.cpp
  clang/test/SemaTemplate/instantiate-attr.cpp


Index: clang/test/SemaTemplate/instantiate-attr.cpp
===
--- clang/test/SemaTemplate/instantiate-attr.cpp
+++ clang/test/SemaTemplate/instantiate-attr.cpp
@@ -1,5 +1,9 @@
 // RUN: %clang_cc1 -fsyntax-only -verify %s
 // expected-no-diagnostics
+
+// Byval alignment warning is emitted too aggressively on AIX.
+// XFAIL: aix
+
 template 
 struct A {
   char a __attribute__((aligned(16)));
Index: clang/test/CXX/drs/dr6xx.cpp
===
--- clang/test/CXX/drs/dr6xx.cpp
+++ clang/test/CXX/drs/dr6xx.cpp
@@ -4,6 +4,9 @@
 // RUN: %clang_cc1 -std=c++17 %s -verify -fexceptions -fcxx-exceptions 
-pedantic-errors -fno-spell-checking
 // RUN: %clang_cc1 -std=c++20 %s -verify -fexceptions -fcxx-exceptions 
-pedantic-errors -fno-spell-checking
 
+// Byval alignment warning is emitted too aggressively on AIX.
+// XFAIL: aix
+
 namespace std {
   struct type_info {};
   __extension__ typedef __SIZE_TYPE__ size_t;
Index: clang/test/Analysis/padding_cpp.cpp
===
--- clang/test/Analysis/padding_cpp.cpp
+++ clang/test/Analysis/padding_cpp.cpp
@@ -1,5 +1,8 @@
 // RUN: %clang_analyze_cc1 -std=c++14 -analyzer-checker=optin.performance 
-analyzer-config optin.performance.Padding:AllowedPad=2 -verify %s
 
+// Byval alignment warning is emitted too aggressively on AIX.
+// XFAIL: aix
+
 // Make sure that the C cases still work fine, even when compiled as C++.
 #include "padding_c.c"
 
Index: clang/test/Analysis/padding_c.c
===
--- clang/test/Analysis/padding_c.c
+++ clang/test/Analysis/padding_c.c
@@ -12,6 +12,9 @@
 // CHECK-PAD-NEGATIVE-VALUE-SAME: 'optin.performance.Padding:AllowedPad', that
 // CHECK-PAD-NEGATIVE-VALUE-SAME: expects a non-negative value
 
+// Byval alignment warning is emitted too aggressively on AIX.
+// XFAIL: aix
+
 #if __has_include()
 #include 
 #endif


Index: clang/test/SemaTemplate/instantiate-attr.cpp
===
--- clang/test/SemaTemplate/instantiate-attr.cpp
+++ clang/test/SemaTemplate/instantiate-attr.cpp
@@ -1,5 +1,9 @@
 // RUN: %clang_cc1 -fsyntax-only -verify %s
 // expected-no-diagnostics
+
+// Byval alignment warning is emitted too aggressively on AIX.
+// XFAIL: aix
+
 template 
 struct A {
   char a __attribute__((aligned(16)));
Index: clang/test/CXX/drs/dr6xx.cpp
===
--- clang/test/CXX/drs/dr6xx.cpp
+++ clang/test/CXX/drs/dr6xx.cpp
@@ -4,6 +4,9 @@
 // RUN: %clang_cc1 -std=c++17 %s -verify -fexceptions -fcxx-exceptions -pedantic-errors -fno-spell-checking
 // RUN: %clang_cc1 -std=c++20 %s -verify -fexceptions -fcxx-exceptions -pedantic-errors -fno-spell-checking
 
+// Byval alignment warning is emitted too aggressively on AIX.
+// XFAIL: aix
+
 namespace std {
   struct type_info {};
   __extension__ typedef __SIZE_TYPE__ size_t;
Index: clang/test/Analysis/padding_cpp.cpp
===
--- clang/test/Analysis/padding_cpp.cpp
+++ clang/test/Analysis/padding_cpp.cpp
@@ -1,5 +1,8 @@
 // RUN: %clang_analyze_cc1 -std=c++14 -analyzer-checker=optin.performance -analyzer-config optin.performance.Padding:AllowedPad=2 -verify %s
 
+// Byval alignment warning is emitted too aggressively on AIX.
+// XFAIL: aix
+
 // Make sure that the C cases still work fine, even when compiled as C++.
 #include "padding_c.c"
 
Index: clang/test/Analysis/padding_c.c
===
--- clang/test/Analysis/padding_c.c
+++ clang/test/Analysis/padding_c.c
@@ -12,6 +12,9 @@
 // CHECK-PAD-NEGATIVE-VALUE-SAME: 'optin.performance.Padding:AllowedPad', that
 // CHECK-PAD-NEGATIVE-VALUE-SAME: expects a non-negative value
 
+// Byval alignment warning is emitted too aggressively on AIX.
+// XFAIL: aix
+
 #if __has_include()
 #include 
 #endif
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] 7aaf024 - [BitcodeWriter] Fix cases of some functions

2022-01-31 Thread Fangrui Song via cfe-commits

Author: Fangrui Song
Date: 2022-01-31T16:46:11-08:00
New Revision: 7aaf024dac719d6cc73e15074ec8c9ad804229ad

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

LOG: [BitcodeWriter] Fix cases of some functions

`WriteIndexToFile` is used by external projects so I do not touch it.

Added: 


Modified: 
clang/lib/CodeGen/BackendUtil.cpp
lld/ELF/LTO.cpp
llvm/include/llvm/Bitcode/BitcodeWriter.h
llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
llvm/lib/LTO/LTO.cpp
llvm/lib/LTO/LTOBackend.cpp
llvm/lib/LTO/ThinLTOCodeGenerator.cpp
llvm/lib/Transforms/IPO/ThinLTOBitcodeWriter.cpp
llvm/lib/Transforms/IPO/WholeProgramDevirt.cpp
llvm/tools/gold/gold-plugin.cpp
llvm/tools/llvm-as/llvm-as.cpp
llvm/tools/llvm-lto/llvm-lto.cpp

Removed: 




diff  --git a/clang/lib/CodeGen/BackendUtil.cpp 
b/clang/lib/CodeGen/BackendUtil.cpp
index da7fed843e0b4..a4d330c0ba935 100644
--- a/clang/lib/CodeGen/BackendUtil.cpp
+++ b/clang/lib/CodeGen/BackendUtil.cpp
@@ -1746,7 +1746,7 @@ void clang::EmbedBitcode(llvm::Module *M, const 
CodeGenOptions &CGOpts,
  llvm::MemoryBufferRef Buf) {
   if (CGOpts.getEmbedBitcode() == CodeGenOptions::Embed_Off)
 return;
-  llvm::EmbedBitcodeInModule(
+  llvm::embedBitcodeInModule(
   *M, Buf, CGOpts.getEmbedBitcode() != CodeGenOptions::Embed_Marker,
   CGOpts.getEmbedBitcode() != CodeGenOptions::Embed_Bitcode,
   CGOpts.CmdArgs);

diff  --git a/lld/ELF/LTO.cpp b/lld/ELF/LTO.cpp
index 5b7ac6a5e9256..88fcd8baf9c91 100644
--- a/lld/ELF/LTO.cpp
+++ b/lld/ELF/LTO.cpp
@@ -292,7 +292,7 @@ static void thinLTOCreateEmptyIndexFiles() {
 
 ModuleSummaryIndex m(/*HaveGVs*/ false);
 m.setSkipModuleByDistributedBackend();
-WriteIndexToFile(m, *os);
+writeIndexToFile(m, *os);
 if (config->thinLTOEmitImportsFiles)
   openFile(path + ".imports");
   }

diff  --git a/llvm/include/llvm/Bitcode/BitcodeWriter.h 
b/llvm/include/llvm/Bitcode/BitcodeWriter.h
index 7ad2d37a2a354..96f25fce8ddb2 100644
--- a/llvm/include/llvm/Bitcode/BitcodeWriter.h
+++ b/llvm/include/llvm/Bitcode/BitcodeWriter.h
@@ -139,7 +139,7 @@ class raw_ostream;
   ///
   /// ModHash is for use in ThinLTO incremental build, generated while the IR
   /// bitcode file writing.
-  void WriteThinLinkBitcodeToFile(const Module &M, raw_ostream &Out,
+  void writeThinLinkBitcodeToFile(const Module &M, raw_ostream &Out,
   const ModuleSummaryIndex &Index,
   const ModuleHash &ModHash);
 
@@ -148,7 +148,7 @@ class raw_ostream;
   /// writing the combined index file for ThinLTO. When writing a subset of the
   /// index for a distributed backend, provide the \p ModuleToSummariesForIndex
   /// map.
-  void WriteIndexToFile(const ModuleSummaryIndex &Index, raw_ostream &Out,
+  void writeIndexToFile(const ModuleSummaryIndex &Index, raw_ostream &Out,
 const std::map
 *ModuleToSummariesForIndex = nullptr);
 
@@ -161,7 +161,7 @@ class raw_ostream;
   /// If EmbedCmdline is set, the command line is also exported in
   /// the corresponding section (__LLVM,_cmdline / .llvmcmd) - even if CmdArgs
   /// were empty.
-  void EmbedBitcodeInModule(Module &M, MemoryBufferRef Buf, bool EmbedBitcode,
+  void embedBitcodeInModule(Module &M, MemoryBufferRef Buf, bool EmbedBitcode,
 bool EmbedCmdline,
 const std::vector &CmdArgs);
 

diff  --git a/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp 
b/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
index eb4e09ea3a265..4bba0b3566751 100644
--- a/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
+++ b/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
@@ -4669,7 +4669,7 @@ void IndexBitcodeWriter::write() {
 // where it will be written in a new bitcode block. This is used when
 // writing the combined index file for ThinLTO. When writing a subset of the
 // index for a distributed backend, provide a \p ModuleToSummariesForIndex map.
-void llvm::WriteIndexToFile(
+void llvm::writeIndexToFile(
 const ModuleSummaryIndex &Index, raw_ostream &Out,
 const std::map *ModuleToSummariesForIndex) {
   SmallVector Buffer;
@@ -4829,7 +4829,7 @@ void BitcodeWriter::writeThinLinkBitcode(const Module &M,
 // Write the specified thin link bitcode file to the given raw output stream,
 // where it will be written in a new bitcode block. This is used when
 // writing the per-module index file for ThinLTO.
-void llvm::WriteThinLinkBitcodeToFile(const Module &M, raw_ostream &Out,
+void llvm::writeThinLinkBitcodeToFile(const Module &M, raw_ostream &Out,
   const ModuleSummaryIndex &Index,
   const ModuleHash &ModHash) 

[PATCH] D118652: Cleanup header dependencies in LLVMCore

2022-01-31 Thread Mehdi AMINI via Phabricator via cfe-commits
mehdi_amini added a comment.

I'm not sure how it'd help clients of the released version of LLVM to delay, 
the development branch and the release seems fairly disconnected to me from 
this point of view.
(what can help are deprecating APIs with instructions about how to update so 
that they can update to the new API while targeting the current release)


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D118652/new/

https://reviews.llvm.org/D118652

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D116541: [OpenMP] Introduce new flag to change offloading driver pipeline

2022-01-31 Thread Joseph Huber via Phabricator via cfe-commits
jhuber6 added a comment.

In D116541#3285927 , @thakis wrote:

> Still failing: http://45.33.8.238/macm1/26873/step_7.txt

It seems what's happening here is that we are building the host.bc twice, this 
will work fine but isn't ideal. I prevent this manually by checking the cache 
if one of the jobs was already created, but for some reason that doesn't seem 
to be happening here. I'll need to figure out how to reproduce this.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D116541/new/

https://reviews.llvm.org/D116541

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D116542: [OpenMP] Add a flag for embedding a file into the module

2022-01-31 Thread Fangrui Song via Phabricator via cfe-commits
MaskRay added a comment.

In D116542#3285985 , @jhuber6 wrote:

> In D116542#3285983 , @MaskRay wrote:
>
>> @jhuber6 Please don't do 4a780aa13ee5e1c8268de54ef946200a270127b9 
>> .. OK, 
>> I was late.
>>
>> See D118666  for the proper fix.
>>
>> I'd be better to revert this relevant changes if that doesn't make you feel 
>> back.
>> I can prepare the revert.
>
> That's fine, I put it here originally because it was grouped with another 
> similar function. But it's likely that one should be moved as well.

Thanks:) The issue should be resolved by 
85dfe19b36ba6e9657612e072c9183ce168fdbbc 
. No need 
to revert.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D116542/new/

https://reviews.llvm.org/D116542

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D118666: [ModuleUtils] Move EmbedBufferInModule to LLVMTransformsUtils

2022-01-31 Thread Fangrui Song via Phabricator via cfe-commits
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rG85dfe19b36ba: [ModuleUtils] Move EmbedBufferInModule to 
LLVMTransformsUtils (authored by MaskRay).

Changed prior to commit:
  https://reviews.llvm.org/D118666?vs=404764&id=404766#toc

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D118666/new/

https://reviews.llvm.org/D118666

Files:
  clang/lib/CodeGen/BackendUtil.cpp
  llvm/include/llvm/Bitcode/BitcodeWriter.h
  llvm/include/llvm/Transforms/Utils/ModuleUtils.h
  llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
  llvm/lib/Transforms/Utils/ModuleUtils.cpp
  utils/bazel/llvm-project-overlay/llvm/BUILD.bazel

Index: utils/bazel/llvm-project-overlay/llvm/BUILD.bazel
===
--- utils/bazel/llvm-project-overlay/llvm/BUILD.bazel
+++ utils/bazel/llvm-project-overlay/llvm/BUILD.bazel
@@ -904,7 +904,6 @@
 "include/llvm/Bitcode/BitcodeWriter.h",
 "include/llvm/Bitcode/BitcodeWriterPass.h",
 "include/llvm/Bitcode/LLVMBitCodes.h",
-"include/llvm/Transforms/Utils/ModuleUtils.h",
 ],
 copts = llvm_copts,
 deps = [
Index: llvm/lib/Transforms/Utils/ModuleUtils.cpp
===
--- llvm/lib/Transforms/Utils/ModuleUtils.cpp
+++ llvm/lib/Transforms/Utils/ModuleUtils.cpp
@@ -264,3 +264,16 @@
   CI->addFnAttr(
   Attribute::get(M->getContext(), MappingsAttrName, Buffer.str()));
 }
+
+void llvm::embedBufferInModule(Module &M, MemoryBufferRef Buf,
+   StringRef SectionName) {
+  // Embed the buffer into the module.
+  Constant *ModuleConstant = ConstantDataArray::get(
+  M.getContext(), makeArrayRef(Buf.getBufferStart(), Buf.getBufferSize()));
+  GlobalVariable *GV = new GlobalVariable(
+  M, ModuleConstant->getType(), true, GlobalValue::PrivateLinkage,
+  ModuleConstant, "llvm.embedded.object");
+  GV->setSection(SectionName);
+
+  appendToCompilerUsed(M, GV);
+}
Index: llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
===
--- llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
+++ llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
@@ -4973,52 +4973,3 @@
   llvm::ConstantArray::get(ATy, UsedArray), "llvm.compiler.used");
   NewUsed->setSection("llvm.metadata");
 }
-
-static void appendToCompilerUsed(Module &M, ArrayRef Values) {
-  GlobalVariable *GV = M.getGlobalVariable("llvm.compiler.used");
-  SmallPtrSet InitAsSet;
-  SmallVector Init;
-  if (GV) {
-if (GV->hasInitializer()) {
-  auto *CA = cast(GV->getInitializer());
-  for (auto &Op : CA->operands()) {
-Constant *C = cast_or_null(Op);
-if (InitAsSet.insert(C).second)
-  Init.push_back(C);
-  }
-}
-GV->eraseFromParent();
-  }
-
-  Type *Int8PtrTy = llvm::Type::getInt8PtrTy(M.getContext());
-  for (auto *V : Values) {
-Constant *C = ConstantExpr::getPointerBitCastOrAddrSpaceCast(V, Int8PtrTy);
-if (InitAsSet.insert(C).second)
-  Init.push_back(C);
-  }
-
-  if (Init.empty())
-return;
-
-  ArrayType *ATy = ArrayType::get(Int8PtrTy, Init.size());
-  GV = new llvm::GlobalVariable(M, ATy, false, GlobalValue::AppendingLinkage,
-ConstantArray::get(ATy, Init),
-"llvm.compiler.used");
-  GV->setSection("llvm.metadata");
-}
-
-void llvm::EmbedBufferInModule(llvm::Module &M, llvm::MemoryBufferRef Buf,
-   StringRef SectionName) {
-  ArrayRef ModuleData =
-  ArrayRef(Buf.getBufferStart(), Buf.getBufferSize());
-
-  // Embed the buffer into the module.
-  llvm::Constant *ModuleConstant =
-  llvm::ConstantDataArray::get(M.getContext(), ModuleData);
-  llvm::GlobalVariable *GV = new llvm::GlobalVariable(
-  M, ModuleConstant->getType(), true, llvm::GlobalValue::PrivateLinkage,
-  ModuleConstant, "llvm.embedded.object");
-  GV->setSection(SectionName);
-
-  appendToCompilerUsed(M, GV);
-}
Index: llvm/include/llvm/Transforms/Utils/ModuleUtils.h
===
--- llvm/include/llvm/Transforms/Utils/ModuleUtils.h
+++ llvm/include/llvm/Transforms/Utils/ModuleUtils.h
@@ -15,6 +15,7 @@
 
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/ADT/StringRef.h"
+#include "llvm/Support/MemoryBuffer.h"
 #include  // for std::pair
 
 namespace llvm {
@@ -106,6 +107,10 @@
 /// unique identifier for this module, so we return the empty string.
 std::string getUniqueModuleId(Module *M);
 
+/// Embed the memory buffer \p Buf into the module \p M as a global using the
+/// specified section name.
+void embedBufferInModule(Module &M, MemoryBufferRef Buf, StringRef SectionName);
+
 class CallInst;
 namespace VFABI {
 /// Overwrite the Vector Function ABI variants attribute with the names provide
Index: llvm/includ

[clang] 85dfe19 - [ModuleUtils] Move EmbedBufferInModule to LLVMTransformsUtils

2022-01-31 Thread Fangrui Song via cfe-commits

Author: Fangrui Song
Date: 2022-01-31T16:33:57-08:00
New Revision: 85dfe19b36ba6e9657612e072c9183ce168fdbbc

URL: 
https://github.com/llvm/llvm-project/commit/85dfe19b36ba6e9657612e072c9183ce168fdbbc
DIFF: 
https://github.com/llvm/llvm-project/commit/85dfe19b36ba6e9657612e072c9183ce168fdbbc.diff

LOG: [ModuleUtils] Move EmbedBufferInModule to LLVMTransformsUtils

D116542 adds EmbedBufferInModule which introduces a layer violation
(https://llvm.org/docs/CodingStandards.html#library-layering).
See 2d5f857a1eaf5f7a806d12953c79b96ed8952da8 for detail.

EmbedBufferInModule does not use BitcodeWriter functionality and should be moved
LLVMTransformsUtils. While here, change the function case to the prevailing
convention.

It seems that EmbedBufferInModule just follows the steps of
EmbedBitcodeInModule. EmbedBitcodeInModule calls WriteBitcodeToFile but has IR
update operations which ideally should be refactored to another library.

Reviewed By: jhuber6

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

Added: 


Modified: 
clang/lib/CodeGen/BackendUtil.cpp
llvm/include/llvm/Bitcode/BitcodeWriter.h
llvm/include/llvm/Transforms/Utils/ModuleUtils.h
llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
llvm/lib/Transforms/Utils/ModuleUtils.cpp
utils/bazel/llvm-project-overlay/llvm/BUILD.bazel

Removed: 




diff  --git a/clang/lib/CodeGen/BackendUtil.cpp 
b/clang/lib/CodeGen/BackendUtil.cpp
index 956352dea146d..da7fed843e0b4 100644
--- a/clang/lib/CodeGen/BackendUtil.cpp
+++ b/clang/lib/CodeGen/BackendUtil.cpp
@@ -84,6 +84,7 @@
 #include "llvm/Transforms/Utils/CanonicalizeAliases.h"
 #include "llvm/Transforms/Utils/Debugify.h"
 #include "llvm/Transforms/Utils/EntryExitInstrumenter.h"
+#include "llvm/Transforms/Utils/ModuleUtils.h"
 #include "llvm/Transforms/Utils/NameAnonGlobals.h"
 #include "llvm/Transforms/Utils/SymbolRewriter.h"
 #include 
@@ -1775,6 +1776,6 @@ void clang::EmbedObject(llvm::Module *M, const 
CodeGenOptions &CGOpts,
 
 SmallString<128> SectionName(
 {".llvm.offloading.", std::get<1>(FilenameAndSection)});
-llvm::EmbedBufferInModule(*M, **ObjectOrErr, SectionName);
+llvm::embedBufferInModule(*M, **ObjectOrErr, SectionName);
   }
 }

diff  --git a/llvm/include/llvm/Bitcode/BitcodeWriter.h 
b/llvm/include/llvm/Bitcode/BitcodeWriter.h
index 56e74cfb418da..7ad2d37a2a354 100644
--- a/llvm/include/llvm/Bitcode/BitcodeWriter.h
+++ b/llvm/include/llvm/Bitcode/BitcodeWriter.h
@@ -165,11 +165,6 @@ class raw_ostream;
 bool EmbedCmdline,
 const std::vector &CmdArgs);
 
-  /// Embeds the memory buffer \p Buf into the module \p M as a global using 
the
-  /// section name \p SectionName.
-  void EmbedBufferInModule(Module &M, MemoryBufferRef Buf,
-   StringRef SectionName);
-
 } // end namespace llvm
 
 #endif // LLVM_BITCODE_BITCODEWRITER_H

diff  --git a/llvm/include/llvm/Transforms/Utils/ModuleUtils.h 
b/llvm/include/llvm/Transforms/Utils/ModuleUtils.h
index 9bbe8ea7e1e8e..8d459972336ba 100644
--- a/llvm/include/llvm/Transforms/Utils/ModuleUtils.h
+++ b/llvm/include/llvm/Transforms/Utils/ModuleUtils.h
@@ -15,6 +15,7 @@
 
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/ADT/StringRef.h"
+#include "llvm/Support/MemoryBuffer.h"
 #include  // for std::pair
 
 namespace llvm {
@@ -106,6 +107,10 @@ void filterDeadComdatFunctions(
 /// unique identifier for this module, so we return the empty string.
 std::string getUniqueModuleId(Module *M);
 
+/// Embed the memory buffer \p Buf into the module \p M as a global using the
+/// specified section name.
+void embedBufferInModule(Module &M, MemoryBufferRef Buf, StringRef 
SectionName);
+
 class CallInst;
 namespace VFABI {
 /// Overwrite the Vector Function ABI variants attribute with the names provide

diff  --git a/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp 
b/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
index 7ebe10e9b4455..eb4e09ea3a265 100644
--- a/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
+++ b/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
@@ -4973,52 +4973,3 @@ void llvm::EmbedBitcodeInModule(llvm::Module &M, 
llvm::MemoryBufferRef Buf,
   llvm::ConstantArray::get(ATy, UsedArray), "llvm.compiler.used");
   NewUsed->setSection("llvm.metadata");
 }
-
-static void appendToCompilerUsed(Module &M, ArrayRef Values) {
-  GlobalVariable *GV = M.getGlobalVariable("llvm.compiler.used");
-  SmallPtrSet InitAsSet;
-  SmallVector Init;
-  if (GV) {
-if (GV->hasInitializer()) {
-  auto *CA = cast(GV->getInitializer());
-  for (auto &Op : CA->operands()) {
-Constant *C = cast_or_null(Op);
-if (InitAsSet.insert(C).second)
-  Init.push_back(C);
-  }
-}
-GV->eraseFromParent();
-  }
-
-  Type *Int8PtrTy = llvm::Type::getInt8PtrTy(M.getContext());
-  for (auto *V : Values) {
-Constant *C = ConstantExpr::getPointerBitCastOrAddrSpace

[PATCH] D110869: [X86] Implement -fzero-call-used-regs option

2022-01-31 Thread Craig Topper via Phabricator via cfe-commits
craig.topper added inline comments.



Comment at: llvm/test/CodeGen/X86/zero-call-used-regs-fmod.ll:15
+; CHECK-NEXT:xorps %xmm1, %xmm1
+; CHECK-NEXT:popq %rax
+; CHECK-NEXT:.cfi_def_cfa_offset 8

void wrote:
> craig.topper wrote:
> > Is it ok that this popq popped garbage into the %rax after you cleared it? 
> > It's not the return value of the function. That's xmm0. This is just a popq 
> > because its a shorter encoding than `sub 8, %esp`
> It's getting the value back from the `pushq %rax` instruction, isn't it? We 
> shouldn't be clearing it, though. I'll see if I can fix that.
There was a mistake in my previous comment. The popq is being used in place of 
`add 8, %rsp`

In this case it probably is the value from the pushq. But %rax isn't a function 
argument so it is effectively garbage data. This is 
X86FrameLowering::emitSPUpdate scavenging %rax to use a pushq/popq instead of 
subtracting or adding to %rsp.

I think this is being done to align the stack for the call to fmod, but I'm not 
sure.

I think we can use a pushq like this to allocate stack space that gets 
overwritten before the popq.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D110869/new/

https://reviews.llvm.org/D110869

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D118666: [ModuleUtils] Move EmbedBufferInModule to LLVMTransformsUtils

2022-01-31 Thread Fangrui Song via Phabricator via cfe-commits
MaskRay added inline comments.



Comment at: llvm/include/llvm/Bitcode/BitcodeWriter.h:168
 
-  /// Embeds the memory buffer \p Buf into the module \p M as a global using 
the
-  /// section name \p SectionName.

Note that the convention is to use imperative sentences. `Embed`


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D118666/new/

https://reviews.llvm.org/D118666

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D118666: [ModuleUtils] Move EmbedBufferInModule to LLVMTransformsUtils

2022-01-31 Thread Joseph Huber via Phabricator via cfe-commits
jhuber6 accepted this revision.
jhuber6 added a comment.
This revision is now accepted and ready to land.

LGTM, thanks for fixing this.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D118666/new/

https://reviews.llvm.org/D118666

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D116542: [OpenMP] Add a flag for embedding a file into the module

2022-01-31 Thread Joseph Huber via Phabricator via cfe-commits
jhuber6 added a comment.

In D116542#3285983 , @MaskRay wrote:

> @jhuber6 Please don't do 4a780aa13ee5e1c8268de54ef946200a270127b9 
> .. OK, I 
> was late.
>
> See D118666  for the proper fix.
>
> I'd be better to revert this relevant changes if that doesn't make you feel 
> back.
> I can prepare the revert.

That's fine, I put it here originally because it was grouped with another 
similar function. But it's likely that one should be moved as well.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D116542/new/

https://reviews.llvm.org/D116542

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D116542: [OpenMP] Add a flag for embedding a file into the module

2022-01-31 Thread Fangrui Song via Phabricator via cfe-commits
MaskRay added a comment.

@jhuber6 Please don't do 4a780aa13ee5e1c8268de54ef946200a270127b9 
.. OK, I 
was late.

See D118666  for the proper fix.

I'd be better to revert this relevant changes if that doesn't make you feel 
back.
I can prepare the revert.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D116542/new/

https://reviews.llvm.org/D116542

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D118666: [ModuleUtils] Move EmbedBufferInModule to LLVMTransformsUtils

2022-01-31 Thread Fangrui Song via Phabricator via cfe-commits
MaskRay created this revision.
MaskRay added reviewers: jhuber6, JonChesterfield.
Herald added subscribers: ormris, hiraditya, mgorny.
MaskRay requested review of this revision.
Herald added projects: clang, LLVM.
Herald added subscribers: llvm-commits, cfe-commits.

D116542  adds EmbedBufferInModule which 
introduces a layer violation
(https://llvm.org/docs/CodingStandards.html#library-layering).
See 2d5f857a1eaf5f7a806d12953c79b96ed8952da8 
 for 
detail.

EmbedBufferInModule does not use BitcodeWriter functionality and should be moved
LLVMTransformsUtils. While here, change the function case to the prevailing
convention.

It seems that EmbedBufferInModule just follows the steps of
EmbedBitcodeInModule. EmbedBitcodeInModule calls WriteBitcodeToFile but has IR
update operations which ideally should be refactored to another library.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D118666

Files:
  clang/lib/CodeGen/BackendUtil.cpp
  llvm/include/llvm/Bitcode/BitcodeWriter.h
  llvm/include/llvm/Transforms/Utils/ModuleUtils.h
  llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
  llvm/lib/Bitcode/Writer/CMakeLists.txt
  llvm/lib/Transforms/Utils/ModuleUtils.cpp
  utils/bazel/llvm-project-overlay/llvm/BUILD.bazel

Index: utils/bazel/llvm-project-overlay/llvm/BUILD.bazel
===
--- utils/bazel/llvm-project-overlay/llvm/BUILD.bazel
+++ utils/bazel/llvm-project-overlay/llvm/BUILD.bazel
@@ -904,7 +904,6 @@
 "include/llvm/Bitcode/BitcodeWriter.h",
 "include/llvm/Bitcode/BitcodeWriterPass.h",
 "include/llvm/Bitcode/LLVMBitCodes.h",
-"include/llvm/Transforms/Utils/ModuleUtils.h",
 ],
 copts = llvm_copts,
 deps = [
Index: llvm/lib/Transforms/Utils/ModuleUtils.cpp
===
--- llvm/lib/Transforms/Utils/ModuleUtils.cpp
+++ llvm/lib/Transforms/Utils/ModuleUtils.cpp
@@ -264,3 +264,19 @@
   CI->addFnAttr(
   Attribute::get(M->getContext(), MappingsAttrName, Buffer.str()));
 }
+
+void llvm::embedBufferInModule(llvm::Module &M, llvm::MemoryBufferRef Buf,
+   StringRef SectionName) {
+  ArrayRef ModuleData =
+  ArrayRef(Buf.getBufferStart(), Buf.getBufferSize());
+
+  // Embed the buffer into the module.
+  llvm::Constant *ModuleConstant =
+  llvm::ConstantDataArray::get(M.getContext(), ModuleData);
+  llvm::GlobalVariable *GV = new llvm::GlobalVariable(
+  M, ModuleConstant->getType(), true, llvm::GlobalValue::PrivateLinkage,
+  ModuleConstant, "llvm.embedded.object");
+  GV->setSection(SectionName);
+
+  appendToCompilerUsed(M, GV);
+}
Index: llvm/lib/Bitcode/Writer/CMakeLists.txt
===
--- llvm/lib/Bitcode/Writer/CMakeLists.txt
+++ llvm/lib/Bitcode/Writer/CMakeLists.txt
@@ -11,7 +11,6 @@
   Analysis
   Core
   MC
-  TransformUtils
   Object
   Support
   )
Index: llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
===
--- llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
+++ llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
@@ -69,7 +69,6 @@
 #include "llvm/Support/MathExtras.h"
 #include "llvm/Support/SHA1.h"
 #include "llvm/Support/raw_ostream.h"
-#include "llvm/Transforms/Utils/ModuleUtils.h"
 #include 
 #include 
 #include 
@@ -4974,19 +4973,3 @@
   llvm::ConstantArray::get(ATy, UsedArray), "llvm.compiler.used");
   NewUsed->setSection("llvm.metadata");
 }
-
-void llvm::EmbedBufferInModule(llvm::Module &M, llvm::MemoryBufferRef Buf,
-   StringRef SectionName) {
-  ArrayRef ModuleData =
-  ArrayRef(Buf.getBufferStart(), Buf.getBufferSize());
-
-  // Embed the buffer into the module.
-  llvm::Constant *ModuleConstant =
-  llvm::ConstantDataArray::get(M.getContext(), ModuleData);
-  llvm::GlobalVariable *GV = new llvm::GlobalVariable(
-  M, ModuleConstant->getType(), true, llvm::GlobalValue::PrivateLinkage,
-  ModuleConstant, "llvm.embedded.object");
-  GV->setSection(SectionName);
-
-  appendToCompilerUsed(M, GV);
-}
Index: llvm/include/llvm/Transforms/Utils/ModuleUtils.h
===
--- llvm/include/llvm/Transforms/Utils/ModuleUtils.h
+++ llvm/include/llvm/Transforms/Utils/ModuleUtils.h
@@ -15,6 +15,7 @@
 
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/ADT/StringRef.h"
+#include "llvm/Support/MemoryBuffer.h"
 #include  // for std::pair
 
 namespace llvm {
@@ -106,6 +107,10 @@
 /// unique identifier for this module, so we return the empty string.
 std::string getUniqueModuleId(Module *M);
 
+/// Embed the memory buffer \p Buf into the module \p M as a global using the
+/// section name \p SectionName.
+void embedBufferInModule(Module &M, MemoryBufferRef Buf, StringRef SectionName);
+
 class CallInst

[PATCH] D110869: [X86] Implement -fzero-call-used-regs option

2022-01-31 Thread Bill Wendling via Phabricator via cfe-commits
void marked 3 inline comments as done.
void added inline comments.



Comment at: llvm/include/llvm/CodeGen/MachineRegisterInfo.h:232-240
+  /// Returns true if a register can be used as an argument to a function.
+  bool isArgumentRegister(const MachineFunction &MF, MCRegister Reg) const;
+
+  /// Returns true if a register is a fixed register.
+  bool isFixedRegister(const MachineFunction &MF, MCRegister Reg) const;
+
+  /// Returns true if a register is a general purpose register.

nickdesaulniers wrote:
> are these methods on `MachineRegisterInfo` used anywhere? It looks like only 
> the ones on `TargetRegisterInfo` are, IIUC?
> 
> Oh, are these related to the .td additions? Something seems asymmetric with 
> these three. Like perhaps we only need `isFixedRegister` here?
`isFixedRegister` is there :-)

I added these to MRI for completeness. It looks like all such predicates are 
reflected in both places.



Comment at: llvm/lib/CodeGen/PrologEpilogInserter.cpp:1269
+  const TargetFrameLowering &TFI = *MF.getSubtarget().getFrameLowering();
+  for (auto RestoreBlock : RestoreBlocks)
+TFI.emitZeroCallUsedRegs(RegsToZero, *RestoreBlock);

nickdesaulniers wrote:
> ```
> for (MachineBasicBlock *RestoreBlock : RestoreBlocks)
> ```
I prefer `auto` unless absolutely necessary (or to avoid confusion).


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D110869/new/

https://reviews.llvm.org/D110869

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D116541: [OpenMP] Introduce new flag to change offloading driver pipeline

2022-01-31 Thread Joseph Huber via Phabricator via cfe-commits
jhuber6 added a comment.

In D116541#3285927 , @thakis wrote:

> Still failing: http://45.33.8.238/macm1/26873/step_7.txt

Weird, can you show me what `-fopenmp -fopenmp-targets=nvptx64 
-fopenmp-new-driver -ccc-print-bindings` looks like there? I'm not sure why but 
it doesn't seem to be getting one of the expected arguments but I'm not sure 
how to reproduce it.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D116541/new/

https://reviews.llvm.org/D116541

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D110869: [X86] Implement -fzero-call-used-regs option

2022-01-31 Thread Bill Wendling via Phabricator via cfe-commits
void updated this revision to Diff 404763.
void added a comment.

Remove attribute from "main" function.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D110869/new/

https://reviews.llvm.org/D110869

Files:
  clang/include/clang/Basic/Attr.td
  clang/include/clang/Basic/AttrDocs.td
  clang/include/clang/Basic/CodeGenOptions.def
  clang/include/clang/Driver/Options.td
  clang/lib/CodeGen/CGCall.cpp
  clang/lib/CodeGen/CodeGenFunction.cpp
  clang/lib/Driver/ToolChains/Clang.cpp
  clang/lib/Sema/SemaDeclAttr.cpp
  clang/test/CodeGen/zero-call-used-regs.c
  clang/test/Misc/pragma-attribute-supported-attributes-list.test
  clang/test/Sema/zero_call_used_regs.c
  llvm/include/llvm/CodeGen/MachineRegisterInfo.h
  llvm/include/llvm/CodeGen/TargetFrameLowering.h
  llvm/include/llvm/CodeGen/TargetRegisterInfo.h
  llvm/include/llvm/Support/CodeGen.h
  llvm/include/llvm/Target/Target.td
  llvm/lib/CodeGen/MachineRegisterInfo.cpp
  llvm/lib/CodeGen/PrologEpilogInserter.cpp
  llvm/lib/Target/X86/X86FrameLowering.cpp
  llvm/lib/Target/X86/X86FrameLowering.h
  llvm/lib/Target/X86/X86RegisterInfo.cpp
  llvm/lib/Target/X86/X86RegisterInfo.h
  llvm/lib/Target/X86/X86RegisterInfo.td
  llvm/test/CodeGen/X86/zero-call-used-regs-fmod.ll
  llvm/test/CodeGen/X86/zero-call-used-regs.ll
  llvm/utils/TableGen/CodeGenRegisters.cpp
  llvm/utils/TableGen/CodeGenRegisters.h
  llvm/utils/TableGen/RegisterInfoEmitter.cpp

Index: llvm/utils/TableGen/RegisterInfoEmitter.cpp
===
--- llvm/utils/TableGen/RegisterInfoEmitter.cpp
+++ llvm/utils/TableGen/RegisterInfoEmitter.cpp
@@ -1176,6 +1176,10 @@
  << "unsigned RegUnit) const override;\n"
  << "  ArrayRef getRegMaskNames() const override;\n"
  << "  ArrayRef getRegMasks() const override;\n"
+ << "  bool isGeneralPurposeRegister(const MachineFunction &, "
+ << "MCRegister) const override;\n"
+ << "  bool isFixedRegister(const MachineFunction &, "
+ << "MCRegister) const override;\n"
  << "  /// Devirtualized TargetFrameLowering.\n"
  << "  static const " << TargetName << "FrameLowering *getFrameLowering(\n"
  << "  const MachineFunction &MF);\n"
@@ -1620,6 +1624,36 @@
   }
   OS << "}\n\n";
 
+  const std::list &RegCategories =
+  RegBank.getRegCategories();
+  OS << "bool " << ClassName << "::\n"
+ << "isGeneralPurposeRegister(const MachineFunction &MF, "
+ << "MCRegister PhysReg) const {\n"
+ << "  return\n";
+  for (const auto &Category : RegCategories)
+if (Category.getName() == "GeneralPurposeRegisters") {
+  for (const auto *RC : Category.getClasses())
+OS << "  " << RC->getQualifiedName()
+   << "RegClass.contains(PhysReg) ||\n";
+  break;
+}
+  OS << "  false;\n";
+  OS << "}\n\n";
+
+  OS << "bool " << ClassName << "::\n"
+ << "isFixedRegister(const MachineFunction &MF, "
+ << "MCRegister PhysReg) const {\n"
+ << "  return\n";
+  for (const auto &Category : RegCategories)
+if (Category.getName() == "FixedRegisters") {
+  for (const auto *RC : Category.getClasses())
+OS << "  " << RC->getQualifiedName()
+   << "RegClass.contains(PhysReg) ||\n";
+  break;
+}
+  OS << "  false;\n";
+  OS << "}\n\n";
+
   OS << "ArrayRef " << ClassName
  << "::getRegMaskNames() const {\n";
   if (!CSRSets.empty()) {
Index: llvm/utils/TableGen/CodeGenRegisters.h
===
--- llvm/utils/TableGen/CodeGenRegisters.h
+++ llvm/utils/TableGen/CodeGenRegisters.h
@@ -476,6 +476,26 @@
 static void computeSubClasses(CodeGenRegBank&);
   };
 
+  // Register categories are used when we need to deterine the category a
+  // register falls into (GPR, vector, fixed, etc.) without having to know
+  // specific information about the target architecture.
+  class CodeGenRegisterCategory {
+Record *TheDef;
+std::string Name;
+std::list Classes;
+
+  public:
+CodeGenRegisterCategory(CodeGenRegBank &, Record *R);
+CodeGenRegisterCategory(CodeGenRegisterCategory &) = delete;
+
+// Return the Record that defined this class, or NULL if the class was
+// created by TableGen.
+Record *getDef() const { return TheDef; }
+
+std::string getName() const { return Name; }
+std::list getClasses() const { return Classes; }
+  };
+
   // Register units are used to model interference and register pressure.
   // Every register is assigned one or more register units such that two
   // registers overlap if and only if they have a register unit in common.
@@ -559,6 +579,13 @@
 typedef std::map RCKeyMap;
 RCKeyMap Key2RC;
 
+// Register categories.
+std::list RegCategories;
+DenseMap Def2RCat;
+typedef std::map
+RCatKeyMap;
+RCatKeyMap Key2RCat;
+
 // Remember each unique set of register units. Initially, this contains a
 // unique set for 

[PATCH] D110869: [X86] Implement -fzero-call-used-regs option

2022-01-31 Thread Bill Wendling via Phabricator via cfe-commits
void updated this revision to Diff 404761.
void added a comment.

Remove "zero-call-used-regs" attribute in front-end if it's "main".


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D110869/new/

https://reviews.llvm.org/D110869

Files:
  clang/include/clang/Basic/Attr.td
  clang/include/clang/Basic/AttrDocs.td
  clang/include/clang/Basic/CodeGenOptions.def
  clang/include/clang/Driver/Options.td
  clang/lib/CodeGen/CGCall.cpp
  clang/lib/CodeGen/CodeGenFunction.cpp
  clang/lib/Driver/ToolChains/Clang.cpp
  clang/lib/Sema/SemaDeclAttr.cpp
  clang/test/CodeGen/zero-call-used-regs.c
  clang/test/Misc/pragma-attribute-supported-attributes-list.test
  clang/test/Sema/zero_call_used_regs.c
  llvm/include/llvm/CodeGen/MachineRegisterInfo.h
  llvm/include/llvm/CodeGen/TargetFrameLowering.h
  llvm/include/llvm/CodeGen/TargetRegisterInfo.h
  llvm/include/llvm/Support/CodeGen.h
  llvm/include/llvm/Target/Target.td
  llvm/lib/CodeGen/MachineRegisterInfo.cpp
  llvm/lib/CodeGen/PrologEpilogInserter.cpp
  llvm/lib/Target/X86/X86FrameLowering.cpp
  llvm/lib/Target/X86/X86FrameLowering.h
  llvm/lib/Target/X86/X86RegisterInfo.cpp
  llvm/lib/Target/X86/X86RegisterInfo.h
  llvm/lib/Target/X86/X86RegisterInfo.td
  llvm/test/CodeGen/X86/zero-call-used-regs-fmod.ll
  llvm/test/CodeGen/X86/zero-call-used-regs.ll
  llvm/utils/TableGen/CodeGenRegisters.cpp
  llvm/utils/TableGen/CodeGenRegisters.h
  llvm/utils/TableGen/RegisterInfoEmitter.cpp

Index: llvm/utils/TableGen/RegisterInfoEmitter.cpp
===
--- llvm/utils/TableGen/RegisterInfoEmitter.cpp
+++ llvm/utils/TableGen/RegisterInfoEmitter.cpp
@@ -1176,6 +1176,10 @@
  << "unsigned RegUnit) const override;\n"
  << "  ArrayRef getRegMaskNames() const override;\n"
  << "  ArrayRef getRegMasks() const override;\n"
+ << "  bool isGeneralPurposeRegister(const MachineFunction &, "
+ << "MCRegister) const override;\n"
+ << "  bool isFixedRegister(const MachineFunction &, "
+ << "MCRegister) const override;\n"
  << "  /// Devirtualized TargetFrameLowering.\n"
  << "  static const " << TargetName << "FrameLowering *getFrameLowering(\n"
  << "  const MachineFunction &MF);\n"
@@ -1620,6 +1624,36 @@
   }
   OS << "}\n\n";
 
+  const std::list &RegCategories =
+  RegBank.getRegCategories();
+  OS << "bool " << ClassName << "::\n"
+ << "isGeneralPurposeRegister(const MachineFunction &MF, "
+ << "MCRegister PhysReg) const {\n"
+ << "  return\n";
+  for (const auto &Category : RegCategories)
+if (Category.getName() == "GeneralPurposeRegisters") {
+  for (const auto *RC : Category.getClasses())
+OS << "  " << RC->getQualifiedName()
+   << "RegClass.contains(PhysReg) ||\n";
+  break;
+}
+  OS << "  false;\n";
+  OS << "}\n\n";
+
+  OS << "bool " << ClassName << "::\n"
+ << "isFixedRegister(const MachineFunction &MF, "
+ << "MCRegister PhysReg) const {\n"
+ << "  return\n";
+  for (const auto &Category : RegCategories)
+if (Category.getName() == "FixedRegisters") {
+  for (const auto *RC : Category.getClasses())
+OS << "  " << RC->getQualifiedName()
+   << "RegClass.contains(PhysReg) ||\n";
+  break;
+}
+  OS << "  false;\n";
+  OS << "}\n\n";
+
   OS << "ArrayRef " << ClassName
  << "::getRegMaskNames() const {\n";
   if (!CSRSets.empty()) {
Index: llvm/utils/TableGen/CodeGenRegisters.h
===
--- llvm/utils/TableGen/CodeGenRegisters.h
+++ llvm/utils/TableGen/CodeGenRegisters.h
@@ -476,6 +476,26 @@
 static void computeSubClasses(CodeGenRegBank&);
   };
 
+  // Register categories are used when we need to deterine the category a
+  // register falls into (GPR, vector, fixed, etc.) without having to know
+  // specific information about the target architecture.
+  class CodeGenRegisterCategory {
+Record *TheDef;
+std::string Name;
+std::list Classes;
+
+  public:
+CodeGenRegisterCategory(CodeGenRegBank &, Record *R);
+CodeGenRegisterCategory(CodeGenRegisterCategory &) = delete;
+
+// Return the Record that defined this class, or NULL if the class was
+// created by TableGen.
+Record *getDef() const { return TheDef; }
+
+std::string getName() const { return Name; }
+std::list getClasses() const { return Classes; }
+  };
+
   // Register units are used to model interference and register pressure.
   // Every register is assigned one or more register units such that two
   // registers overlap if and only if they have a register unit in common.
@@ -559,6 +579,13 @@
 typedef std::map RCKeyMap;
 RCKeyMap Key2RC;
 
+// Register categories.
+std::list RegCategories;
+DenseMap Def2RCat;
+typedef std::map
+RCatKeyMap;
+RCatKeyMap Key2RCat;
+
 // Remember each unique set of register units. Initially, this conta

[PATCH] D110869: [X86] Implement -fzero-call-used-regs option

2022-01-31 Thread Bill Wendling via Phabricator via cfe-commits
void marked 2 inline comments as done.
void added inline comments.



Comment at: llvm/lib/CodeGen/PrologEpilogInserter.cpp:1182
+
+  if (!F.hasFnAttribute("zero-call-used-regs") ||
+  // No need to zero call regs in main().

craig.topper wrote:
> Should the frontend avoid putting the attribute on "main" instead of making a 
> special case in the backend?
Sure.



Comment at: llvm/test/CodeGen/X86/zero-call-used-regs-fmod.ll:15
+; CHECK-NEXT:xorps %xmm1, %xmm1
+; CHECK-NEXT:popq %rax
+; CHECK-NEXT:.cfi_def_cfa_offset 8

craig.topper wrote:
> Is it ok that this popq popped garbage into the %rax after you cleared it? 
> It's not the return value of the function. That's xmm0. This is just a popq 
> because its a shorter encoding than `sub 8, %esp`
It's getting the value back from the `pushq %rax` instruction, isn't it? We 
shouldn't be clearing it, though. I'll see if I can fix that.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D110869/new/

https://reviews.llvm.org/D110869

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D110869: [X86] Implement -fzero-call-used-regs option

2022-01-31 Thread Nick Desaulniers via Phabricator via cfe-commits
nickdesaulniers added inline comments.



Comment at: llvm/lib/Target/X86/X86RegisterInfo.cpp:621
 
+BitVector X86RegisterInfo::getArgumentRegs(const MachineFunction &MF) const {
+  const X86Subtarget &Subtarget = MF.getSubtarget();

nickdesaulniers wrote:
> pengfei wrote:
> > Can we get this info from calling conversion?
> > Different calling conversions may use different argument registers.
> when working on the ppc issue recently (https://reviews.llvm.org/D116424), I 
> recall there being an existing check for whether a register was callee saved.
> 
> RegisterClassInfo::getLastCalleeSavedAlias
> 
> Can that be reused here or in any of the below?
> 
> Do we need to zero stack slots when arguments are passed on the stack (after 
> exhausting the "argument registers?"
oops, my last comment here was a draft that I meant to delete before submitting 
all my previous comments. Feel free to ignore.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D110869/new/

https://reviews.llvm.org/D110869

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D116541: [OpenMP] Introduce new flag to change offloading driver pipeline

2022-01-31 Thread Nico Weber via Phabricator via cfe-commits
thakis added a comment.

Still failing: http://45.33.8.238/macm1/26873/step_7.txt


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D116541/new/

https://reviews.llvm.org/D116541

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D110869: [X86] Implement -fzero-call-used-regs option

2022-01-31 Thread Nick Desaulniers via Phabricator via cfe-commits
nickdesaulniers added a comment.

Neat use of tablegen; will take me a bit to wrap my head around it. I'll give 
this a shot on some kernel builds first thing tomorrow!




Comment at: llvm/include/llvm/CodeGen/MachineRegisterInfo.h:232-240
+  /// Returns true if a register can be used as an argument to a function.
+  bool isArgumentRegister(const MachineFunction &MF, MCRegister Reg) const;
+
+  /// Returns true if a register is a fixed register.
+  bool isFixedRegister(const MachineFunction &MF, MCRegister Reg) const;
+
+  /// Returns true if a register is a general purpose register.

are these methods on `MachineRegisterInfo` used anywhere? It looks like only 
the ones on `TargetRegisterInfo` are, IIUC?

Oh, are these related to the .td additions? Something seems asymmetric with 
these three. Like perhaps we only need `isFixedRegister` here?



Comment at: llvm/lib/CodeGen/MachineRegisterInfo.cpp:655-668
+bool MachineRegisterInfo::isArgumentRegister(const MachineFunction &MF,
+ MCRegister Reg) const {
+  return getTargetRegisterInfo()->isArgumentRegister(MF, Reg);
+}
+
+bool MachineRegisterInfo::isFixedRegister(const MachineFunction &MF,
+  MCRegister Reg) const {

are these methods on `MachineRegisterInfo` used anywhere? It looks like only 
the ones on `TargetRegisterInfo` are, IIUC?



Comment at: llvm/lib/CodeGen/PrologEpilogInserter.cpp:1216-1218
+llvm::for_each(MF, [&](const MachineBasicBlock &MBB) {
+  llvm::for_each(MBB, [&](const MachineInstr &MI) {
+llvm::for_each(MI.operands(), [&](MachineOperand MO) {

while I'm definitely a fan of the functional abstractions in 
llvm/include/llvm/ADT/STLExtras.h, `llvm::for_each` is perhaps my least 
favorite (when compared to `llvm::all_of`, `llvm::any_of`, or `llvm::none_of`). 
Consider using just a range-for here (and below) if it would be more concise.



Comment at: llvm/lib/CodeGen/PrologEpilogInserter.cpp:1257
+
+  for (auto MO : MI.operands()) {
+if (!MO.isReg())

```
for (const MachineOperand &MO : MI.operands()) {
```



Comment at: llvm/lib/CodeGen/PrologEpilogInserter.cpp:1269
+  const TargetFrameLowering &TFI = *MF.getSubtarget().getFrameLowering();
+  for (auto RestoreBlock : RestoreBlocks)
+TFI.emitZeroCallUsedRegs(RegsToZero, *RestoreBlock);

```
for (MachineBasicBlock *RestoreBlock : RestoreBlocks)
```



Comment at: llvm/lib/Target/X86/X86RegisterInfo.cpp:621
 
+BitVector X86RegisterInfo::getArgumentRegs(const MachineFunction &MF) const {
+  const X86Subtarget &Subtarget = MF.getSubtarget();

pengfei wrote:
> Can we get this info from calling conversion?
> Different calling conversions may use different argument registers.
when working on the ppc issue recently (https://reviews.llvm.org/D116424), I 
recall there being an existing check for whether a register was callee saved.

RegisterClassInfo::getLastCalleeSavedAlias

Can that be reused here or in any of the below?

Do we need to zero stack slots when arguments are passed on the stack (after 
exhausting the "argument registers?"


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D110869/new/

https://reviews.llvm.org/D110869

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D110869: [X86] Implement -fzero-call-used-regs option

2022-01-31 Thread Craig Topper via Phabricator via cfe-commits
craig.topper added inline comments.



Comment at: llvm/lib/CodeGen/PrologEpilogInserter.cpp:1182
+
+  if (!F.hasFnAttribute("zero-call-used-regs") ||
+  // No need to zero call regs in main().

Should the frontend avoid putting the attribute on "main" instead of making a 
special case in the backend?



Comment at: llvm/test/CodeGen/X86/zero-call-used-regs-fmod.ll:15
+; CHECK-NEXT:xorps %xmm1, %xmm1
+; CHECK-NEXT:popq %rax
+; CHECK-NEXT:.cfi_def_cfa_offset 8

Is it ok that this popq popped garbage into the %rax after you cleared it? It's 
not the return value of the function. That's xmm0. This is just a popq because 
its a shorter encoding than `sub 8, %esp`


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D110869/new/

https://reviews.llvm.org/D110869

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D116542: [OpenMP] Add a flag for embedding a file into the module

2022-01-31 Thread Joseph Huber via Phabricator via cfe-commits
jhuber6 added a comment.

In D116542#3285857 , @cmtice wrote:

> This change introduces a circular dependency: BitcodeWriters now depends on 
> TransformUtils, but TransformUtils also depends on BitcodeWriters.  This 
> appears to be a layering violation.

Might explain why it wasn't included before, should I just copy the function I 
here and remove the dependency?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D116542/new/

https://reviews.llvm.org/D116542

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D116542: [OpenMP] Add a flag for embedding a file into the module

2022-01-31 Thread Caroline Tice via Phabricator via cfe-commits
cmtice added a comment.

This change introduces a circular dependency: BitcodeWriters now depends on 
TransformUtils, but TransformUtils also depends on BitcodeWriters.  This 
appears to be a layering violation.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D116542/new/

https://reviews.llvm.org/D116542

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D110869: [X86] Implement -fzero-call-used-regs option

2022-01-31 Thread Craig Topper via Phabricator via cfe-commits
craig.topper added inline comments.



Comment at: llvm/lib/Target/X86/X86RegisterInfo.td:647
+def GeneralPurposeRegisters : RegisterCategory<[GR64, GR32, GR16, GR8]>;
+def FixedRegisters : RegisterCategory<[DEBUG_REG, CONTROL_REG, CCR, FPCCR,
+   DFCCR, TILE, VK1PAIR, VK2PAIR, VK4PAIR,

Should we still have this comment from the non-tablegen implementation "The 
following may not be "fixed" registers, but we don't want them anyway." applied 
to the VK*PAIRS and TILE?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D110869/new/

https://reviews.llvm.org/D110869

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D118542: [Clang][OpenMPIRBuilder] Fix off-by-one error when dividing by stepsize.

2022-01-31 Thread Michael Kruse via Phabricator via cfe-commits
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rG8a9e4f245b66: [Clang][OpenMPIRBuilder] Fix off-by-one error 
when dividing by stepsize. (authored by Meinersbur).

Changed prior to commit:
  https://reviews.llvm.org/D118542?vs=404300&id=404755#toc

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D118542/new/

https://reviews.llvm.org/D118542

Files:
  clang/lib/Sema/SemaOpenMP.cpp
  clang/test/OpenMP/irbuilder_for_unsigned.c
  clang/test/OpenMP/irbuilder_for_unsigned_down.c
  clang/test/OpenMP/irbuilder_unroll_full.c
  clang/test/OpenMP/irbuilder_unroll_heuristic.c
  clang/test/OpenMP/irbuilder_unroll_partial_factor.c
  clang/test/OpenMP/irbuilder_unroll_partial_factor_for.c
  clang/test/OpenMP/irbuilder_unroll_partial_heuristic.c
  clang/test/OpenMP/irbuilder_unroll_partial_heuristic_constant_for.c
  clang/test/OpenMP/irbuilder_unroll_partial_heuristic_runtime_for.c
  clang/test/OpenMP/irbuilder_unroll_unroll_partial_factor.c
  clang/test/OpenMP/irbuilder_unroll_unroll_partial_heuristic.c

Index: clang/test/OpenMP/irbuilder_unroll_unroll_partial_heuristic.c
===
--- clang/test/OpenMP/irbuilder_unroll_unroll_partial_heuristic.c
+++ clang/test/OpenMP/irbuilder_unroll_unroll_partial_heuristic.c
@@ -154,7 +154,10 @@
 // CHECK-NEXT:%[[TMP7:.+]] = load i32, i32* %[[DOTSTART]], align 4
 // CHECK-NEXT:%[[SUB:.+]] = sub nsw i32 %[[TMP6]], %[[TMP7]]
 // CHECK-NEXT:%[[TMP8:.+]] = load i32, i32* %[[DOTSTEP]], align 4
-// CHECK-NEXT:%[[DIV:.+]] = udiv i32 %[[SUB]], %[[TMP8]]
+// CHECK-NEXT:%[[SUB1:.+]] = sub i32 %[[TMP8]], 1
+// CHECK-NEXT:%[[ADD:.+]] = add i32 %[[SUB]], %[[SUB1]]
+// CHECK-NEXT:%[[TMP9:.+]] = load i32, i32* %[[DOTSTEP]], align 4
+// CHECK-NEXT:%[[DIV:.+]] = udiv i32 %[[ADD]], %[[TMP9]]
 // CHECK-NEXT:br label %[[COND_END:.+]]
 // CHECK-EMPTY:
 // CHECK-NEXT:  [[COND_FALSE]]:
@@ -162,8 +165,8 @@
 // CHECK-EMPTY:
 // CHECK-NEXT:  [[COND_END]]:
 // CHECK-NEXT:%[[COND:.+]] = phi i32 [ %[[DIV]], %[[COND_TRUE]] ], [ 0, %[[COND_FALSE]] ]
-// CHECK-NEXT:%[[TMP9:.+]] = load i32*, i32** %[[DISTANCE_ADDR]], align 8
-// CHECK-NEXT:store i32 %[[COND]], i32* %[[TMP9]], align 4
+// CHECK-NEXT:%[[TMP10:.+]] = load i32*, i32** %[[DISTANCE_ADDR]], align 8
+// CHECK-NEXT:store i32 %[[COND]], i32* %[[TMP10]], align 4
 // CHECK-NEXT:ret void
 // CHECK-NEXT:  }
 
Index: clang/test/OpenMP/irbuilder_unroll_unroll_partial_factor.c
===
--- clang/test/OpenMP/irbuilder_unroll_unroll_partial_factor.c
+++ clang/test/OpenMP/irbuilder_unroll_unroll_partial_factor.c
@@ -173,7 +173,10 @@
 // CHECK-NEXT:%[[TMP7:.+]] = load i32, i32* %[[DOTSTART]], align 4
 // CHECK-NEXT:%[[SUB:.+]] = sub nsw i32 %[[TMP6]], %[[TMP7]]
 // CHECK-NEXT:%[[TMP8:.+]] = load i32, i32* %[[DOTSTEP]], align 4
-// CHECK-NEXT:%[[DIV:.+]] = udiv i32 %[[SUB]], %[[TMP8]]
+// CHECK-NEXT:%[[SUB1:.+]] = sub i32 %[[TMP8]], 1
+// CHECK-NEXT:%[[ADD:.+]] = add i32 %[[SUB]], %[[SUB1]]
+// CHECK-NEXT:%[[TMP9:.+]] = load i32, i32* %[[DOTSTEP]], align 4
+// CHECK-NEXT:%[[DIV:.+]] = udiv i32 %[[ADD]], %[[TMP9]]
 // CHECK-NEXT:br label %[[COND_END:.+]]
 // CHECK-EMPTY:
 // CHECK-NEXT:  [[COND_FALSE]]:
@@ -181,8 +184,8 @@
 // CHECK-EMPTY:
 // CHECK-NEXT:  [[COND_END]]:
 // CHECK-NEXT:%[[COND:.+]] = phi i32 [ %[[DIV]], %[[COND_TRUE]] ], [ 0, %[[COND_FALSE]] ]
-// CHECK-NEXT:%[[TMP9:.+]] = load i32*, i32** %[[DISTANCE_ADDR]], align 8
-// CHECK-NEXT:store i32 %[[COND]], i32* %[[TMP9]], align 4
+// CHECK-NEXT:%[[TMP10:.+]] = load i32*, i32** %[[DISTANCE_ADDR]], align 8
+// CHECK-NEXT:store i32 %[[COND]], i32* %[[TMP10]], align 4
 // CHECK-NEXT:ret void
 // CHECK-NEXT:  }
 
Index: clang/test/OpenMP/irbuilder_unroll_partial_heuristic_runtime_for.c
===
--- clang/test/OpenMP/irbuilder_unroll_partial_heuristic_runtime_for.c
+++ clang/test/OpenMP/irbuilder_unroll_partial_heuristic_runtime_for.c
@@ -206,7 +206,10 @@
 // CHECK-NEXT:%[[TMP10:.+]] = load i32, i32* %[[DOTSTART]], align 4
 // CHECK-NEXT:%[[SUB:.+]] = sub nsw i32 %[[TMP9]], %[[TMP10]]
 // CHECK-NEXT:%[[TMP11:.+]] = load i32, i32* %[[DOTSTEP]], align 4
-// CHECK-NEXT:%[[DIV:.+]] = udiv i32 %[[SUB]], %[[TMP11]]
+// CHECK-NEXT:%[[SUB1:.+]] = sub i32 %[[TMP11]], 1
+// CHECK-NEXT:%[[ADD:.+]] = add i32 %[[SUB]], %[[SUB1]]
+// CHECK-NEXT:%[[TMP12:.+]] = load i32, i32* %[[DOTSTEP]], align 4
+// CHECK-NEXT:%[[DIV:.+]] = udiv i32 %[[ADD]], %[[TMP12]]
 // CHECK-NEXT:br label %[[COND_END:.+]]
 // CHECK-EMPTY:
 // CHECK-NEXT:  [[COND_FALSE]]:
@@ -214,8 +217,8 @@
 // CHECK-EMPTY:
 // CHECK-NEXT:  [[COND_END]]:
 // CHECK-NEXT:%[[COND:.+]] = phi i32 [ %[[DIV]],

[clang] 8a9e4f2 - [Clang][OpenMPIRBuilder] Fix off-by-one error when dividing by stepsize.

2022-01-31 Thread Michael Kruse via cfe-commits

Author: Michael Kruse
Date: 2022-01-31T17:28:52-06:00
New Revision: 8a9e4f245b66b90839bdf34e91cb0901e3260dad

URL: 
https://github.com/llvm/llvm-project/commit/8a9e4f245b66b90839bdf34e91cb0901e3260dad
DIFF: 
https://github.com/llvm/llvm-project/commit/8a9e4f245b66b90839bdf34e91cb0901e3260dad.diff

LOG: [Clang][OpenMPIRBuilder] Fix off-by-one error when dividing by stepsize.

When the stepsize does not evenly divide the range's end, round-up to ensure 
that that last multiple of the stepsize before the reaching the upper boud is 
reached. For instance, the trip count of

for (int i = 0; i < 7; i+=5)

is two (i=0 and i=5), not (7-0)/5 == 1.

Reviewed By: peixin

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

Added: 
clang/test/OpenMP/irbuilder_for_unsigned_down.c

Modified: 
clang/lib/Sema/SemaOpenMP.cpp
clang/test/OpenMP/irbuilder_for_unsigned.c
clang/test/OpenMP/irbuilder_unroll_full.c
clang/test/OpenMP/irbuilder_unroll_heuristic.c
clang/test/OpenMP/irbuilder_unroll_partial_factor.c
clang/test/OpenMP/irbuilder_unroll_partial_factor_for.c
clang/test/OpenMP/irbuilder_unroll_partial_heuristic.c
clang/test/OpenMP/irbuilder_unroll_partial_heuristic_constant_for.c
clang/test/OpenMP/irbuilder_unroll_partial_heuristic_runtime_for.c
clang/test/OpenMP/irbuilder_unroll_unroll_partial_factor.c
clang/test/OpenMP/irbuilder_unroll_unroll_partial_heuristic.c

Removed: 




diff  --git a/clang/lib/Sema/SemaOpenMP.cpp b/clang/lib/Sema/SemaOpenMP.cpp
index ae91a6470471a..a500ad4f02209 100644
--- a/clang/lib/Sema/SemaOpenMP.cpp
+++ b/clang/lib/Sema/SemaOpenMP.cpp
@@ -5327,6 +5327,8 @@ static CapturedStmt *buildDistanceFunc(Sema &Actions, 
QualType LogicalTy,
 
 IntegerLiteral *Zero = IntegerLiteral::Create(
 Ctx, llvm::APInt(Ctx.getIntWidth(LogicalTy), 0), LogicalTy, {});
+IntegerLiteral *One = IntegerLiteral::Create(
+Ctx, llvm::APInt(Ctx.getIntWidth(LogicalTy), 1), LogicalTy, {});
 Expr *Dist;
 if (Rel == BO_NE) {
   // When using a != comparison, the increment can be +1 or -1. This can be
@@ -5381,18 +5383,25 @@ static CapturedStmt *buildDistanceFunc(Sema &Actions, 
QualType LogicalTy,
 
   if (Rel == BO_LE || Rel == BO_GE) {
 // Add one to the range if the relational operator is inclusive.
-Range = AssertSuccess(Actions.BuildBinOp(
-nullptr, {}, BO_Add, Range,
-Actions.ActOnIntegerConstant(SourceLocation(), 1).get()));
+Range =
+AssertSuccess(Actions.BuildBinOp(nullptr, {}, BO_Add, Range, One));
   }
 
-  // Divide by the absolute step amount.
+  // Divide by the absolute step amount. If the range is not a multiple of
+  // the step size, rounding-up the effective upper bound ensures that the
+  // last iteration is included.
+  // Note that the rounding-up may cause an overflow in a temporry that
+  // could be avoided, but would have occured in a C-style for-loop as 
well.
   Expr *Divisor = BuildVarRef(NewStep);
   if (Rel == BO_GE || Rel == BO_GT)
 Divisor =
 AssertSuccess(Actions.BuildUnaryOp(nullptr, {}, UO_Minus, 
Divisor));
+  Expr *DivisorMinusOne =
+  AssertSuccess(Actions.BuildBinOp(nullptr, {}, BO_Sub, Divisor, One));
+  Expr *RangeRoundUp = AssertSuccess(
+  Actions.BuildBinOp(nullptr, {}, BO_Add, Range, DivisorMinusOne));
   Dist = AssertSuccess(
-  Actions.BuildBinOp(nullptr, {}, BO_Div, Range, Divisor));
+  Actions.BuildBinOp(nullptr, {}, BO_Div, RangeRoundUp, Divisor));
 
   // If there is not at least one iteration, the range contains garbage. 
Fix
   // to zero in this case.

diff  --git a/clang/test/OpenMP/irbuilder_for_unsigned.c 
b/clang/test/OpenMP/irbuilder_for_unsigned.c
index a7e70391c027b..928d99b9bfc72 100644
--- a/clang/test/OpenMP/irbuilder_for_unsigned.c
+++ b/clang/test/OpenMP/irbuilder_for_unsigned.c
@@ -123,14 +123,17 @@ extern "C" void workshareloop_unsigned(float *a, float 
*b, float *c, float *d) {
 // CHECK-NEXT:[[TMP7:%.*]] = load i32, i32* [[DOTSTART]], align 4
 // CHECK-NEXT:[[SUB:%.*]] = sub i32 [[TMP6]], [[TMP7]]
 // CHECK-NEXT:[[TMP8:%.*]] = load i32, i32* [[DOTSTEP]], align 4
-// CHECK-NEXT:[[DIV:%.*]] = udiv i32 [[SUB]], [[TMP8]]
+// CHECK-NEXT:[[SUB1:%.*]] = sub i32 [[TMP8]], 1
+// CHECK-NEXT:[[ADD:%.*]] = add i32 [[SUB]], [[SUB1]]
+// CHECK-NEXT:[[TMP9:%.*]] = load i32, i32* [[DOTSTEP]], align 4
+// CHECK-NEXT:[[DIV:%.*]] = udiv i32 [[ADD]], [[TMP9]]
 // CHECK-NEXT:br label [[COND_END:%.*]]
 // CHECK:   cond.false:
 // CHECK-NEXT:br label [[COND_END]]
 // CHECK:   cond.end:
 // CHECK-NEXT:[[COND:%.*]] = phi i32 [ [[DIV]], [[COND_TRUE]] ], [ 0, 
[[COND_FALSE]] ]
-// CHECK-NEXT:[[TMP9:%.*]] = load i32*, i32** [[DISTANCE_ADDR]], align 8
-// CHECK-NEXT:store i32 [[COND]],

[PATCH] D110869: [X86] Implement -fzero-call-used-regs option

2022-01-31 Thread Bill Wendling via Phabricator via cfe-commits
void updated this revision to Diff 404754.
void added a comment.

Generate the "isGeneralPurposeRegister" and "isFixedRegister" predicates from
the .td file.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D110869/new/

https://reviews.llvm.org/D110869

Files:
  clang/include/clang/Basic/Attr.td
  clang/include/clang/Basic/AttrDocs.td
  clang/include/clang/Basic/CodeGenOptions.def
  clang/include/clang/Driver/Options.td
  clang/lib/CodeGen/CGCall.cpp
  clang/lib/Driver/ToolChains/Clang.cpp
  clang/lib/Sema/SemaDeclAttr.cpp
  clang/test/CodeGen/zero-call-used-regs.c
  clang/test/Misc/pragma-attribute-supported-attributes-list.test
  clang/test/Sema/zero_call_used_regs.c
  llvm/include/llvm/CodeGen/MachineRegisterInfo.h
  llvm/include/llvm/CodeGen/TargetFrameLowering.h
  llvm/include/llvm/CodeGen/TargetRegisterInfo.h
  llvm/include/llvm/Support/CodeGen.h
  llvm/include/llvm/Target/Target.td
  llvm/lib/CodeGen/MachineRegisterInfo.cpp
  llvm/lib/CodeGen/PrologEpilogInserter.cpp
  llvm/lib/Target/X86/X86FrameLowering.cpp
  llvm/lib/Target/X86/X86FrameLowering.h
  llvm/lib/Target/X86/X86RegisterInfo.cpp
  llvm/lib/Target/X86/X86RegisterInfo.h
  llvm/lib/Target/X86/X86RegisterInfo.td
  llvm/test/CodeGen/X86/zero-call-used-regs-fmod.ll
  llvm/test/CodeGen/X86/zero-call-used-regs.ll
  llvm/utils/TableGen/CodeGenRegisters.cpp
  llvm/utils/TableGen/CodeGenRegisters.h
  llvm/utils/TableGen/RegisterInfoEmitter.cpp

Index: llvm/utils/TableGen/RegisterInfoEmitter.cpp
===
--- llvm/utils/TableGen/RegisterInfoEmitter.cpp
+++ llvm/utils/TableGen/RegisterInfoEmitter.cpp
@@ -1176,6 +1176,10 @@
  << "unsigned RegUnit) const override;\n"
  << "  ArrayRef getRegMaskNames() const override;\n"
  << "  ArrayRef getRegMasks() const override;\n"
+ << "  bool isGeneralPurposeRegister(const MachineFunction &, "
+ << "MCRegister) const override;\n"
+ << "  bool isFixedRegister(const MachineFunction &, "
+ << "MCRegister) const override;\n"
  << "  /// Devirtualized TargetFrameLowering.\n"
  << "  static const " << TargetName << "FrameLowering *getFrameLowering(\n"
  << "  const MachineFunction &MF);\n"
@@ -1620,6 +1624,36 @@
   }
   OS << "}\n\n";
 
+  const std::list &RegCategories =
+  RegBank.getRegCategories();
+  OS << "bool " << ClassName << "::\n"
+ << "isGeneralPurposeRegister(const MachineFunction &MF, "
+ << "MCRegister PhysReg) const {\n"
+ << "  return\n";
+  for (const auto &Category : RegCategories)
+if (Category.getName() == "GeneralPurposeRegisters") {
+  for (const auto *RC : Category.getClasses())
+OS << "  " << RC->getQualifiedName()
+   << "RegClass.contains(PhysReg) ||\n";
+  break;
+}
+  OS << "  false;\n";
+  OS << "}\n\n";
+
+  OS << "bool " << ClassName << "::\n"
+ << "isFixedRegister(const MachineFunction &MF, "
+ << "MCRegister PhysReg) const {\n"
+ << "  return\n";
+  for (const auto &Category : RegCategories)
+if (Category.getName() == "FixedRegisters") {
+  for (const auto *RC : Category.getClasses())
+OS << "  " << RC->getQualifiedName()
+   << "RegClass.contains(PhysReg) ||\n";
+  break;
+}
+  OS << "  false;\n";
+  OS << "}\n\n";
+
   OS << "ArrayRef " << ClassName
  << "::getRegMaskNames() const {\n";
   if (!CSRSets.empty()) {
Index: llvm/utils/TableGen/CodeGenRegisters.h
===
--- llvm/utils/TableGen/CodeGenRegisters.h
+++ llvm/utils/TableGen/CodeGenRegisters.h
@@ -476,6 +476,26 @@
 static void computeSubClasses(CodeGenRegBank&);
   };
 
+  // Register categories are used when we need to deterine the category a
+  // register falls into (GPR, vector, fixed, etc.) without having to know
+  // specific information about the target architecture.
+  class CodeGenRegisterCategory {
+Record *TheDef;
+std::string Name;
+std::list Classes;
+
+  public:
+CodeGenRegisterCategory(CodeGenRegBank &, Record *R);
+CodeGenRegisterCategory(CodeGenRegisterCategory &) = delete;
+
+// Return the Record that defined this class, or NULL if the class was
+// created by TableGen.
+Record *getDef() const { return TheDef; }
+
+std::string getName() const { return Name; }
+std::list getClasses() const { return Classes; }
+  };
+
   // Register units are used to model interference and register pressure.
   // Every register is assigned one or more register units such that two
   // registers overlap if and only if they have a register unit in common.
@@ -559,6 +579,13 @@
 typedef std::map RCKeyMap;
 RCKeyMap Key2RC;
 
+// Register categories.
+std::list RegCategories;
+DenseMap Def2RCat;
+typedef std::map
+RCatKeyMap;
+RCatKeyMap Key2RCat;
+
 // Remember each unique set of register units. Initially, this contains a
 // un

[PATCH] D91607: [clang][Sparc] Fix __builtin_extract_return_addr etc.

2022-01-31 Thread Eli Friedman via Phabricator via cfe-commits
efriedma added a comment.

In D91607#3283350 , @ro wrote:

> In D91607#3280808 , @efriedma wrote:
>
>> Testcase?
>
> I thought the 3 testcases adjusted in D91608 
>  to use `__builtin_extract_return_addr` and 
> fixed by this patch were enough.  Otherwise, should I just augment 
> `clang/test/CodeGen/builtins-sparc.c` or better create a new test?

I'd prefer to have coverage in the clang regression tests, so developers can 
catch regressions and easily check the expected codegen.  builtins-sparc.c is 
fine.

>> Do you need to ptrtoint/inttoptr?  I would expect that the address is an 
>> `i8*`, so you can just GEP an appropriate number of bytes.
>
> TBH, I know practically nothing about LLVM codegen, so I rely heavily on 
> guidance.  IIRC this patch was developed by searching for similar code in 
> `TargetInfo.cpp` and modifying it until it did what I needed.  Is this the 
> place to read on GEP ?

That's a good starting place for understanding the complexity of GEP... but you 
don't need that much here. Here, we just want to pass a single index; that's 
equivalent to pointer addition in C.

You should be able to just drop the calls to CreatePtrToInt and CreateIntToPtr, 
and replace CreateAdd with CreateGEP.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D91607/new/

https://reviews.llvm.org/D91607

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D118596: [clang][dataflow] Enable comparison of distinct values in Environment

2022-01-31 Thread Stanislav Gatev via Phabricator via cfe-commits
sgatev updated this revision to Diff 404743.
sgatev added a comment.

Reformat code.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D118596/new/

https://reviews.llvm.org/D118596

Files:
  clang/include/clang/Analysis/FlowSensitive/DataflowAnalysis.h
  clang/include/clang/Analysis/FlowSensitive/DataflowEnvironment.h
  clang/include/clang/Analysis/FlowSensitive/TypeErasedDataflowAnalysis.h
  clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp
  clang/lib/Analysis/FlowSensitive/TypeErasedDataflowAnalysis.cpp
  clang/unittests/Analysis/FlowSensitive/TestingSupport.h
  clang/unittests/Analysis/FlowSensitive/TypeErasedDataflowAnalysisTest.cpp

Index: clang/unittests/Analysis/FlowSensitive/TypeErasedDataflowAnalysisTest.cpp
===
--- clang/unittests/Analysis/FlowSensitive/TypeErasedDataflowAnalysisTest.cpp
+++ clang/unittests/Analysis/FlowSensitive/TypeErasedDataflowAnalysisTest.cpp
@@ -19,6 +19,7 @@
 #include "clang/Analysis/FlowSensitive/DataflowLattice.h"
 #include "clang/Analysis/FlowSensitive/Value.h"
 #include "clang/Tooling/Tooling.h"
+#include "llvm/ADT/Optional.h"
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/ADT/SmallSet.h"
 #include "llvm/ADT/StringRef.h"
@@ -49,53 +50,35 @@
 using ::testing::UnorderedElementsAre;
 
 template 
-class AnalysisCallback : public ast_matchers::MatchFinder::MatchCallback {
-public:
-  AnalysisCallback(AnalysisT (*MakeAnalysis)(ASTContext &))
-  : MakeAnalysis(MakeAnalysis) {}
-  void run(const ast_matchers::MatchFinder::MatchResult &Result) override {
-assert(BlockStates.empty());
-
-const auto *Func = Result.Nodes.getNodeAs("func");
-assert(Func != nullptr);
-
-Stmt *Body = Func->getBody();
-assert(Body != nullptr);
-
-auto CFCtx = llvm::cantFail(
-ControlFlowContext::build(nullptr, Body, Result.Context));
-
-AnalysisT Analysis = MakeAnalysis(*Result.Context);
-DataflowAnalysisContext DACtx;
-Environment Env(DACtx);
-BlockStates = runDataflowAnalysis(CFCtx, Analysis, Env);
-  }
-
-  AnalysisT (*MakeAnalysis)(ASTContext &);
-  std::vector<
-  llvm::Optional>>
-  BlockStates;
-};
-
-template 
-std::vector>>
+llvm::Expected>>>
 runAnalysis(llvm::StringRef Code, AnalysisT (*MakeAnalysis)(ASTContext &)) {
   std::unique_ptr AST =
   tooling::buildASTFromCodeWithArgs(Code, {"-std=c++11"});
 
-  AnalysisCallback Callback(MakeAnalysis);
-  ast_matchers::MatchFinder Finder;
-  Finder.addMatcher(
-  ast_matchers::functionDecl(ast_matchers::hasName("target")).bind("func"),
-  &Callback);
-  Finder.matchAST(AST->getASTContext());
+  auto *Func = selectFirst(
+  "func", match(functionDecl(ast_matchers::hasName("target")).bind("func"),
+AST->getASTContext()));
+  assert(Func != nullptr);
+
+  Stmt *Body = Func->getBody();
+  assert(Body != nullptr);
+
+  auto CFCtx = llvm::cantFail(
+  ControlFlowContext::build(nullptr, Body, &AST->getASTContext()));
 
-  return Callback.BlockStates;
+  AnalysisT Analysis = MakeAnalysis(AST->getASTContext());
+  DataflowAnalysisContext DACtx;
+  Environment Env(DACtx);
+
+  return runDataflowAnalysis(CFCtx, Analysis, Env);
 }
 
 TEST(DataflowAnalysisTest, NoopAnalysis) {
-  auto BlockStates = runAnalysis(
-  "void target() {}", [](ASTContext &C) { return NoopAnalysis(C, false); });
+  auto BlockStates = llvm::cantFail(
+  runAnalysis("void target() {}", [](ASTContext &C) {
+return NoopAnalysis(C, false);
+  }));
   EXPECT_EQ(BlockStates.size(), 2u);
   EXPECT_TRUE(BlockStates[0].hasValue());
   EXPECT_TRUE(BlockStates[1].hasValue());
@@ -132,18 +115,15 @@
 };
 
 TEST(DataflowAnalysisTest, NonConvergingAnalysis) {
-  auto BlockStates = runAnalysis(
-  R"(
+  std::string Code = R"(
 void target() {
   while(true) {}
 }
-  )",
-  [](ASTContext &C) { return NonConvergingAnalysis(C); });
-  EXPECT_EQ(BlockStates.size(), 4u);
-  EXPECT_TRUE(BlockStates[0].hasValue());
-  EXPECT_TRUE(BlockStates[1].hasValue());
-  EXPECT_TRUE(BlockStates[2].hasValue());
-  EXPECT_TRUE(BlockStates[3].hasValue());
+  )";
+  auto Res = runAnalysis(
+  Code, [](ASTContext &C) { return NonConvergingAnalysis(C); });
+  EXPECT_EQ(llvm::toString(Res.takeError()),
+"maximum number of iterations reached");
 }
 
 struct FunctionCallLattice {
@@ -353,6 +333,18 @@
 }
   }
 
+  bool compareEquivalent(QualType Type, const Value &Val1,
+ const Environment &Env1, const Value &Val2,
+ const Environment &Env2) final {
+// Nothing to say about a value that does not model an `OptionalInt`.
+if (!Type->isRecordType() ||
+Type->getAsCXXRecordDecl()->getQualifiedNameAsString() != "OptionalInt")
+  return true;
+
+return cast(&Val1)->getProperty("has_value") ==
+   cast(&Val2)->getProperty("has_value");
+  }
+
   bool merge(QualType Type, const Value

[clang] 623b66e - [Clang][NFC] Added testcase from #49549

2022-01-31 Thread Dávid Bolvanský via cfe-commits

Author: Dávid Bolvanský
Date: 2022-01-31T23:45:56+01:00
New Revision: 623b66eded4b1ab2fbb962d3841899458bac6693

URL: 
https://github.com/llvm/llvm-project/commit/623b66eded4b1ab2fbb962d3841899458bac6693
DIFF: 
https://github.com/llvm/llvm-project/commit/623b66eded4b1ab2fbb962d3841899458bac6693.diff

LOG: [Clang][NFC] Added testcase from #49549

The issue is fixed in trunk, so add testcase to avoid regression in the future.

Added: 


Modified: 
clang/test/SemaCXX/attr-likelihood.cpp

Removed: 




diff  --git a/clang/test/SemaCXX/attr-likelihood.cpp 
b/clang/test/SemaCXX/attr-likelihood.cpp
index f7503fed49b93..642d62fa89826 100644
--- a/clang/test/SemaCXX/attr-likelihood.cpp
+++ b/clang/test/SemaCXX/attr-likelihood.cpp
@@ -159,4 +159,18 @@ constexpr int constexpr_function() {
   [[likely]] return 0;
 }
 static_assert(constexpr_function() == 0);
+
+constexpr double pow(double x, long long n) noexcept {
+if (n > 0) [[likely]]
+return x * pow(x, n - 1);
+else [[unlikely]]
+return 1;
+}
+constexpr long long fact(long long n) noexcept {
+if (n > 1) [[likely]]
+return n * fact(n - 1);
+else [[unlikely]]
+return 1;
+}
+
 #endif



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D115960: Revert D109159 "[amdgpu] Enable selection of `s_cselect_b64`."

2022-01-31 Thread Fangrui Song via Phabricator via cfe-commits
MaskRay added a comment.

Please abandon this revision.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D115960/new/

https://reviews.llvm.org/D115960

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D115393: [InstrProf][NFC] Refactor Profile kind into a bitset enum.

2022-01-31 Thread Snehasish Kumar via Phabricator via cfe-commits
snehasish added inline comments.



Comment at: llvm/include/llvm/ProfileData/InstrProfReader.h:495
 
+  InstrProfKind getProfileKind() const override {
+InstrProfKind ProfileKind = InstrProfKind::Unknown;

mtrofin wrote:
> This looks a lot like line 290, can it be refactored (or am I missing 
> something?)
Since these were in a header but spread across different inherited classes of 
the same base I moved these to the .cpp and refactored it into a common static 
function in https://reviews.llvm.org/D118656. Thanks!



Comment at: llvm/lib/ProfileData/InstrProfWriter.cpp:337
   uint64_t CSSummarySize = 0;
-  if (ProfileKind == PF_IRLevelWithCS) {
+  if (static_cast(ProfileKind & InstrProfKind::CS)) {
 CSSummaryOffset = OS.tell();

mtrofin wrote:
> consider adding helper APIs for test/set?
I'm not sure it will help much but I'll take a look in the patch to rename the 
enum types to more descriptive names.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D115393/new/

https://reviews.llvm.org/D115393

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D118571: [clang][WebAssembly] Imply -fno-threadsafe-static when threading is disabled

2022-01-31 Thread Sam Clegg via Phabricator via cfe-commits
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rG45ad3467b708: [clang][WebAssembly] Imply 
-fno-threadsafe-static when threading is disabled (authored by sbc100).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D118571/new/

https://reviews.llvm.org/D118571

Files:
  clang/lib/Basic/Targets/WebAssembly.cpp
  clang/test/CodeGenCXX/static-init-wasm.cpp


Index: clang/test/CodeGenCXX/static-init-wasm.cpp
===
--- clang/test/CodeGenCXX/static-init-wasm.cpp
+++ clang/test/CodeGenCXX/static-init-wasm.cpp
@@ -1,6 +1,6 @@
-// RUN: %clang_cc1 -emit-llvm -triple=wasm32-unknown-unknown -o - %s \
+// RUN: %clang_cc1 -emit-llvm -triple=wasm32-unknown-unknown -target-feature 
+atomics -o - %s \
 // RUN:   | FileCheck %s -check-prefix=WEBASSEMBLY32
-// RUN: %clang_cc1 -emit-llvm -triple=wasm64-unknown-unknown -o - %s \
+// RUN: %clang_cc1 -emit-llvm -triple=wasm64-unknown-unknown -target-feature 
+atomics -o - %s \
 // RUN:   | FileCheck %s -check-prefix=WEBASSEMBLY64
 
 // Test that we don't create common blocks.
@@ -52,3 +52,17 @@
 // WEBASSEMBLY64: call noundef %struct.A* @_ZN1AC1Ev(%struct.A* {{[^,]*}} 
@theA)
 // WEBASSEMBLY64: define internal void @_GLOBAL__sub_I_static_init_wasm.cpp() 
#3 {
 // WEBASSEMBLY64: call void @__cxx_global_var_init()
+
+// RUN: %clang_cc1 -emit-llvm -triple=wasm32-unknown-unknown -o - %s \
+// RUN:   | FileCheck %s -check-prefix=NOATOMICS
+// RUN: %clang_cc1 -emit-llvm -triple=wasm64-unknown-unknown -o - %s \
+// RUN:   | FileCheck %s -check-prefix=NOATOMICS
+
+// NOATOMICS-LABEL: @_Z1gv()
+// NOATOMICS:   %[[R0:.+]] = load i8, i8* @_ZGVZ1gvE1a, align 1
+// NOATOMICS-NEXT:  %guard.uninitialized = icmp eq i8 %[[R0]], 0
+// NOATOMICS-NEXT:  br i1 %guard.uninitialized, label %[[CHECK:.+]], label 
%[[END:.+]],
+// NOATOMICS:   [[CHECK]]:
+// NOATOMICS-NOT:   __cxa_guard_acquire
+// NOATOMICS:   [[END]]:
+// NOATOMICS-NEXT:  ret void
Index: clang/lib/Basic/Targets/WebAssembly.cpp
===
--- clang/lib/Basic/Targets/WebAssembly.cpp
+++ clang/lib/Basic/Targets/WebAssembly.cpp
@@ -260,6 +260,7 @@
   if (!HasAtomics) {
 Opts.POSIXThreads = false;
 Opts.setThreadModel(LangOptions::ThreadModelKind::Single);
+Opts.ThreadsafeStatics = false;
   }
 }
 


Index: clang/test/CodeGenCXX/static-init-wasm.cpp
===
--- clang/test/CodeGenCXX/static-init-wasm.cpp
+++ clang/test/CodeGenCXX/static-init-wasm.cpp
@@ -1,6 +1,6 @@
-// RUN: %clang_cc1 -emit-llvm -triple=wasm32-unknown-unknown -o - %s \
+// RUN: %clang_cc1 -emit-llvm -triple=wasm32-unknown-unknown -target-feature +atomics -o - %s \
 // RUN:   | FileCheck %s -check-prefix=WEBASSEMBLY32
-// RUN: %clang_cc1 -emit-llvm -triple=wasm64-unknown-unknown -o - %s \
+// RUN: %clang_cc1 -emit-llvm -triple=wasm64-unknown-unknown -target-feature +atomics -o - %s \
 // RUN:   | FileCheck %s -check-prefix=WEBASSEMBLY64
 
 // Test that we don't create common blocks.
@@ -52,3 +52,17 @@
 // WEBASSEMBLY64: call noundef %struct.A* @_ZN1AC1Ev(%struct.A* {{[^,]*}} @theA)
 // WEBASSEMBLY64: define internal void @_GLOBAL__sub_I_static_init_wasm.cpp() #3 {
 // WEBASSEMBLY64: call void @__cxx_global_var_init()
+
+// RUN: %clang_cc1 -emit-llvm -triple=wasm32-unknown-unknown -o - %s \
+// RUN:   | FileCheck %s -check-prefix=NOATOMICS
+// RUN: %clang_cc1 -emit-llvm -triple=wasm64-unknown-unknown -o - %s \
+// RUN:   | FileCheck %s -check-prefix=NOATOMICS
+
+// NOATOMICS-LABEL: @_Z1gv()
+// NOATOMICS:   %[[R0:.+]] = load i8, i8* @_ZGVZ1gvE1a, align 1
+// NOATOMICS-NEXT:  %guard.uninitialized = icmp eq i8 %[[R0]], 0
+// NOATOMICS-NEXT:  br i1 %guard.uninitialized, label %[[CHECK:.+]], label %[[END:.+]],
+// NOATOMICS:   [[CHECK]]:
+// NOATOMICS-NOT:   __cxa_guard_acquire
+// NOATOMICS:   [[END]]:
+// NOATOMICS-NEXT:  ret void
Index: clang/lib/Basic/Targets/WebAssembly.cpp
===
--- clang/lib/Basic/Targets/WebAssembly.cpp
+++ clang/lib/Basic/Targets/WebAssembly.cpp
@@ -260,6 +260,7 @@
   if (!HasAtomics) {
 Opts.POSIXThreads = false;
 Opts.setThreadModel(LangOptions::ThreadModelKind::Single);
+Opts.ThreadsafeStatics = false;
   }
 }
 
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] 45ad346 - [clang][WebAssembly] Imply -fno-threadsafe-static when threading is disabled

2022-01-31 Thread Sam Clegg via cfe-commits

Author: Sam Clegg
Date: 2022-01-31T14:26:09-08:00
New Revision: 45ad3467b708618841869cb529a814001d2295f9

URL: 
https://github.com/llvm/llvm-project/commit/45ad3467b708618841869cb529a814001d2295f9
DIFF: 
https://github.com/llvm/llvm-project/commit/45ad3467b708618841869cb529a814001d2295f9.diff

LOG: [clang][WebAssembly] Imply -fno-threadsafe-static when threading is 
disabled

When we don't enable atomics we completely disabled threading in
which case there is no point in generating thread safe code for
static initialization.

This should always be safe because, in WebAssembly, it is not
possible to link object compiled without the atomics feature into a
mutli-threaded program.

See https://github.com/emscripten-core/emscripten/pull/16152

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

Added: 


Modified: 
clang/lib/Basic/Targets/WebAssembly.cpp
clang/test/CodeGenCXX/static-init-wasm.cpp

Removed: 




diff  --git a/clang/lib/Basic/Targets/WebAssembly.cpp 
b/clang/lib/Basic/Targets/WebAssembly.cpp
index 4cba861f61d2c..2309997eb77b2 100644
--- a/clang/lib/Basic/Targets/WebAssembly.cpp
+++ b/clang/lib/Basic/Targets/WebAssembly.cpp
@@ -260,6 +260,7 @@ void WebAssemblyTargetInfo::adjust(DiagnosticsEngine &Diags,
   if (!HasAtomics) {
 Opts.POSIXThreads = false;
 Opts.setThreadModel(LangOptions::ThreadModelKind::Single);
+Opts.ThreadsafeStatics = false;
   }
 }
 

diff  --git a/clang/test/CodeGenCXX/static-init-wasm.cpp 
b/clang/test/CodeGenCXX/static-init-wasm.cpp
index 647a61b84721e..0a066de616fa9 100644
--- a/clang/test/CodeGenCXX/static-init-wasm.cpp
+++ b/clang/test/CodeGenCXX/static-init-wasm.cpp
@@ -1,6 +1,6 @@
-// RUN: %clang_cc1 -emit-llvm -triple=wasm32-unknown-unknown -o - %s \
+// RUN: %clang_cc1 -emit-llvm -triple=wasm32-unknown-unknown -target-feature 
+atomics -o - %s \
 // RUN:   | FileCheck %s -check-prefix=WEBASSEMBLY32
-// RUN: %clang_cc1 -emit-llvm -triple=wasm64-unknown-unknown -o - %s \
+// RUN: %clang_cc1 -emit-llvm -triple=wasm64-unknown-unknown -target-feature 
+atomics -o - %s \
 // RUN:   | FileCheck %s -check-prefix=WEBASSEMBLY64
 
 // Test that we don't create common blocks.
@@ -52,3 +52,17 @@ A theA;
 // WEBASSEMBLY64: call noundef %struct.A* @_ZN1AC1Ev(%struct.A* {{[^,]*}} 
@theA)
 // WEBASSEMBLY64: define internal void @_GLOBAL__sub_I_static_init_wasm.cpp() 
#3 {
 // WEBASSEMBLY64: call void @__cxx_global_var_init()
+
+// RUN: %clang_cc1 -emit-llvm -triple=wasm32-unknown-unknown -o - %s \
+// RUN:   | FileCheck %s -check-prefix=NOATOMICS
+// RUN: %clang_cc1 -emit-llvm -triple=wasm64-unknown-unknown -o - %s \
+// RUN:   | FileCheck %s -check-prefix=NOATOMICS
+
+// NOATOMICS-LABEL: @_Z1gv()
+// NOATOMICS:   %[[R0:.+]] = load i8, i8* @_ZGVZ1gvE1a, align 1
+// NOATOMICS-NEXT:  %guard.uninitialized = icmp eq i8 %[[R0]], 0
+// NOATOMICS-NEXT:  br i1 %guard.uninitialized, label %[[CHECK:.+]], label 
%[[END:.+]],
+// NOATOMICS:   [[CHECK]]:
+// NOATOMICS-NOT:   __cxa_guard_acquire
+// NOATOMICS:   [[END]]:
+// NOATOMICS-NEXT:  ret void



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D118297: [clang] add Diag -Wasm-volatile for implied volatile asm stmts

2022-01-31 Thread Segher Boessenkool via Phabricator via cfe-commits
segher added a comment.

In D118297#3285571 , @MaskRay wrote:

> The `volatile` qualifier is implied but makes the intention explicit. I agree 
> that the user should not be punished (-Wasm-volatile) by writing `asm 
> volatile("int3")` instead of `asm("int3")`.

Not punished no.  But it is useful education (like all the other "statement 
without effect"
etc. warnings).


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D118297/new/

https://reviews.llvm.org/D118297

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D118652: Cleanup header dependencies in LLVMCore

2022-01-31 Thread Fangrui Song via Phabricator via cfe-commits
MaskRay added a comment.

> It is very likely to break downstream code unless they took a lot of care in 
> avoiding hidden ehader dependencies, something the LLVM codebase doesn't do 
> that well :-/

release/14.x will branch soon. Will it be useful to deliberately miss the 
branch point so that downstream code (with some folks testing trunk llvm) has 
more time to fix or will that cause headache when llvm-project developers back 
port fixes to release/14.x?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D118652/new/

https://reviews.llvm.org/D118652

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D118652: Cleanup header dependencies in LLVMCore

2022-01-31 Thread Fangrui Song via Phabricator via cfe-commits
MaskRay added a comment.

Thanks for working on this. I spot checked some files. In general it looks 
great.

One file replaces a forward declaration with `#include "llvm/IR/BasicBlock.h"`.
Some files add `#include "llvm/Support/ToolOutputFile.h"`.
One files adds `class FunctionPass;`

Would you mind landing these addition changes separately to make the include 
removal part more targeted?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D118652/new/

https://reviews.llvm.org/D118652

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D118297: [clang] add Diag -Wasm-volatile for implied volatile asm stmts

2022-01-31 Thread Fangrui Song via Phabricator via cfe-commits
MaskRay added a comment.

The `volatile` qualifier is implied but makes the intention explicit. I agree 
that the user should not be punished (-Wasm-volatile) by writing `asm 
volatile("int3")` instead of `asm("int3")`.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D118297/new/

https://reviews.llvm.org/D118297

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D117391: [AST] Ignore implicit nodes in CastExpr::getConversionFunction

2022-01-31 Thread David Rector via Phabricator via cfe-commits
davrec added inline comments.



Comment at: clang/lib/AST/Expr.cpp:1946-1947
   for (const CastExpr *E = this; E; E = dyn_cast(SubExpr)) {
 SubExpr = skipImplicitTemporary(E->getSubExpr());
+SubExpr = SubExpr->IgnoreImplicit();
 

aaron.ballman wrote:
> `IgnoreImplicit()` calls `IgnoreImplicitSingleStep()` eventually, and that 
> call does the same work as `skipImplicitTemporary()`, so I think the end 
> result here should be the same.
As I look at this a second time, I just realized...calling IgnoreImplicit here 
mean that the loop only ever runs one iteration, since IgnoreImplicit 
presumably skips over ImplicitCastExprs.  While I liked how Kim did revision 
initially because it seemed to handle the constructor conversions similarly to 
their handling of getSubExprAsWritten() above, now I think something different 
is needed here.

Proposed alternative:
Right now skipIimplicitTemporary does what IgnoreImplicit does *except* skip 
over a) ImplicitCastExprs and b) FullExprs (= ConstantExprs or 
ExprWithCleanups).

Kim has identified that we need to skip over at least ConstantExprs at least in 
this case (i.e. the most conservative possible fix would be to skip over 
ConstantExprs just before the cast in line 1950).

But perhaps the better solution, to forestall future bugs, is to skip over 
FullExprs in skipImplicitTemporary, so that it skips over everything 
IgnoreImplicit does except ImplicitCastExprs.  (And, some documentation should 
be added to `skipImplicitTemporary` to that effect, to aid future maintenance.)

I can't see offhand how the other uses of skipImplicitTemporary would be 
negatively affected by additionally skipping over FullExprs.

Aaron what do you think?  Kim can you verify this alternative would also solve 
the problem without breaking any tests?



Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D117391/new/

https://reviews.llvm.org/D117391

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D71966: [Wdocumentation][RFC] Improve identifier's of \param

2022-01-31 Thread Aaron Puchert via Phabricator via cfe-commits
aaronpuchert added a comment.
Herald added a project: clang-tools-extra.

@Mordante, do you have plans to get back to this? Otherwise I'd like to pick it 
up.

I'd probably concentrate on multiple parameter support for now, and delegate 
identifier validation, typo correction for variadic functions, and the 
undocumented parameter warning to separate changes.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D71966/new/

https://reviews.llvm.org/D71966

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D116541: [OpenMP] Introduce new flag to change offloading driver pipeline

2022-01-31 Thread Joseph Huber via Phabricator via cfe-commits
jhuber6 added a comment.

In D116541#3285455 , @thakis wrote:

> Looks like this breaks tests on macOS: 
> http://45.33.8.238/macm1/26856/step_7.txt
>
> Please take a look and revert for now if it takes a while to fix (maybe just 
> needs an explicit triple?)

Not sure what I expected when I hard-coded the host-triple in the test. I 
pushed a change in rGb79e2a1ccd3b 
, can you 
check it again?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D116541/new/

https://reviews.llvm.org/D116541

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] b79e2a1 - [OpenMP] Remove hard-coded triple in new driver test

2022-01-31 Thread Joseph Huber via cfe-commits

Author: Joseph Huber
Date: 2022-01-31T16:46:51-05:00
New Revision: b79e2a1ccd3b0c8e388fe35ba162c2db498dd882

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

LOG: [OpenMP] Remove hard-coded triple in new driver test

Summary:
Previously this test used a hard-coded triple value in the check lines
wihch failed on other architectures. This patch changes that to accept
any host triple.

Added: 


Modified: 
clang/test/Driver/openmp-offload-gpu.c

Removed: 




diff  --git a/clang/test/Driver/openmp-offload-gpu.c 
b/clang/test/Driver/openmp-offload-gpu.c
index b031740a7d92..a9fc3e7b33a4 100644
--- a/clang/test/Driver/openmp-offload-gpu.c
+++ b/clang/test/Driver/openmp-offload-gpu.c
@@ -355,11 +355,11 @@
 // RUN:  -fopenmp-new-driver -no-canonical-prefixes 
-ccc-print-bindings %s -o openmp-offload-gpu 2>&1 \
 // RUN:   | FileCheck -check-prefix=NEW_DRIVER %s
 
-// NEW_DRIVER: "x86_64-unknown-linux-gnu" - "clang", inputs: 
["[[HOST_INPUT:.+]]"], output: "[[HOST_BC:.+]]" 
+// NEW_DRIVER: "[[HOST_TRIPLE:.+]]" - "clang", inputs: ["[[HOST_INPUT:.+]]"], 
output: "[[HOST_BC:.+]]" 
 // NEW_DRIVER: "nvptx64-nvidia-cuda" - "clang", inputs: 
["[[DEVICE_INPUT:.+]]", "[[HOST_BC]]"], output: "[[DEVICE_ASM:.+]]"
 // NEW_DRIVER: "nvptx64-nvidia-cuda" - "NVPTX::Assembler", inputs: 
["[[DEVICE_ASM]]"], output: "[[DEVICE_OBJ:.+]]" 
-// NEW_DRIVER: "x86_64-unknown-linux-gnu" - "clang", inputs: ["[[HOST_BC]]", 
"[[DEVICE_OBJ]]"], output: "[[HOST_OBJ:.+]]" 
-// NEW_DRIVER: "x86_64-unknown-linux-gnu" - "[[LINKER:.+]]", inputs: 
["[[HOST_OBJ]]"], output: "openmp-offload-gpu"
+// NEW_DRIVER: "[[HOST_TRIPLE:.+]]" - "clang", inputs: ["[[HOST_BC]]", 
"[[DEVICE_OBJ]]"], output: "[[HOST_OBJ:.+]]" 
+// NEW_DRIVER: "[[HOST_TRIPLE:.+]]" - "[[LINKER:.+]]", inputs: 
["[[HOST_OBJ]]"], output: "openmp-offload-gpu"
 
 // RUN:   %clang -### -fopenmp=libomp -fopenmp-targets=nvptx64-nvidia-cuda 
-Xopenmp-target=nvptx64-nvida-cuda -march=sm_70 \
 // RUN:  
--libomptarget-nvptx-bc-path=%S/Inputs/libomptarget/libomptarget-new-nvptx-test.bc
 \



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D118619: [clang][CodeGen][NFC] Remove unused CodeGenModule fields

2022-01-31 Thread Itay Bookstein via Phabricator via cfe-commits
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rG2a868802a372: [clang][CodeGen][NFC] Remove unused 
CodeGenModule fields (authored by ibookstein).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D118619/new/

https://reviews.llvm.org/D118619

Files:
  clang/lib/CodeGen/CodeGenModule.h


Index: clang/lib/CodeGen/CodeGenModule.h
===
--- clang/lib/CodeGen/CodeGenModule.h
+++ clang/lib/CodeGen/CodeGenModule.h
@@ -394,13 +394,6 @@
   llvm::MapVector MangledDeclNames;
   llvm::StringMap Manglings;
 
-  // An ordered map of canonical GlobalDecls paired with the cpu-index for
-  // cpu-specific name manglings.
-  llvm::MapVector, StringRef>
-  CPUSpecificMangledDeclNames;
-  llvm::StringMap, llvm::BumpPtrAllocator>
-  CPUSpecificManglings;
-
   /// Global annotations.
   std::vector Annotations;
 


Index: clang/lib/CodeGen/CodeGenModule.h
===
--- clang/lib/CodeGen/CodeGenModule.h
+++ clang/lib/CodeGen/CodeGenModule.h
@@ -394,13 +394,6 @@
   llvm::MapVector MangledDeclNames;
   llvm::StringMap Manglings;
 
-  // An ordered map of canonical GlobalDecls paired with the cpu-index for
-  // cpu-specific name manglings.
-  llvm::MapVector, StringRef>
-  CPUSpecificMangledDeclNames;
-  llvm::StringMap, llvm::BumpPtrAllocator>
-  CPUSpecificManglings;
-
   /// Global annotations.
   std::vector Annotations;
 
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] 2a86880 - [clang][CodeGen][NFC] Remove unused CodeGenModule fields

2022-01-31 Thread Itay Bookstein via cfe-commits

Author: Itay Bookstein
Date: 2022-01-31T23:45:53+02:00
New Revision: 2a868802a37261105ea3baeaba405c41cb4b5675

URL: 
https://github.com/llvm/llvm-project/commit/2a868802a37261105ea3baeaba405c41cb4b5675
DIFF: 
https://github.com/llvm/llvm-project/commit/2a868802a37261105ea3baeaba405c41cb4b5675.diff

LOG: [clang][CodeGen][NFC] Remove unused CodeGenModule fields

Signed-off-by: Itay Bookstein 

Reviewed By: erichkeane

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

Added: 


Modified: 
clang/lib/CodeGen/CodeGenModule.h

Removed: 




diff  --git a/clang/lib/CodeGen/CodeGenModule.h 
b/clang/lib/CodeGen/CodeGenModule.h
index e803022508a4..1fcd5d4d808a 100644
--- a/clang/lib/CodeGen/CodeGenModule.h
+++ b/clang/lib/CodeGen/CodeGenModule.h
@@ -394,13 +394,6 @@ class CodeGenModule : public CodeGenTypeCache {
   llvm::MapVector MangledDeclNames;
   llvm::StringMap Manglings;
 
-  // An ordered map of canonical GlobalDecls paired with the cpu-index for
-  // cpu-specific name manglings.
-  llvm::MapVector, StringRef>
-  CPUSpecificMangledDeclNames;
-  llvm::StringMap, llvm::BumpPtrAllocator>
-  CPUSpecificManglings;
-
   /// Global annotations.
   std::vector Annotations;
 



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D118619: [clang][CodeGen][NFC] Remove unused CodeGenModule fields

2022-01-31 Thread Itay Bookstein via Phabricator via cfe-commits
ibookstein added a comment.

Thanks!
CI failures seem to be in completely unrelated libarcher, which are shared by 
other runs in the past few hours; landing this, then.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D118619/new/

https://reviews.llvm.org/D118619

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D118652: Cleanup header dependencies in LLVMCore

2022-01-31 Thread serge via Phabricator via cfe-commits
serge-sans-paille created this revision.
serge-sans-paille added reviewers: RKSimon, lenary, mehdi_amini, MaskRay.
Herald added subscribers: jeroen.dobbelaere, ormris, foad, dexonsmith, wenlei, 
jdoerfert, kerbowa, pengfei, arphaman, steven_wu, hiraditya, nhaehnle, jvesely, 
arsenm.
Herald added a reviewer: deadalnix.
Herald added a reviewer: bollu.
serge-sans-paille requested review of this revision.
Herald added projects: clang, LLVM.
Herald added subscribers: llvm-commits, cfe-commits.

Based on the output of include-what-you-use.

This is a big chunk of changes. It is very likely to break downstream code
unless they took a lot of care in avoiding hidden ehader dependencies, something
the LLVM codebase doesn't do that well :-/

I've tried to summarize the biggest change below:

- llvm/include/llvm-c/Core.h: no longer includes llvm-c/ErrorHandling.h
- llvm/IR/DIBuilder.h no longer includes llvm/IR/DebugInfo.h
- llvm/IR/IRBuilder.h no longer includes llvm/IR/IntrinsicInst.h
- llvm/IR/LLVMRemarkStreamer.h no longer includes llvm/Support/ToolOutputFile.h
- llvm/IR/LegacyPassManager.h no longer include llvm/Pass.h
- llvm/IR/Type.h no longer includes llvm/ADT/SmallPtrSet.h
- llvm/IR/PassManager.h no longer includes llvm/Pass.h nor llvm/Support/Debug.h

And the usual count of preprocessed lines:
$ clang++ -E  -Iinclude -I../llvm/include ../llvm/lib/IR/*.cpp -std=c++14 
-fno-rtti -fno-exceptions | wc -l
before: 6400831
after:  6189948

200k lines less to process is no that bad ;-)

Discourse thread on the topic: 
https://llvm.discourse.group/t/include-what-you-use-include-cleanup


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D118652

Files:
  clang/lib/CodeGen/CGStmtOpenMP.cpp
  llvm/examples/IRTransforms/InitializePasses.h
  llvm/include/llvm-c/Core.h
  llvm/include/llvm-c/DebugInfo.h
  llvm/include/llvm/Analysis/AliasAnalysisEvaluator.h
  llvm/include/llvm/Analysis/CycleAnalysis.h
  llvm/include/llvm/Analysis/MLInlineAdvisor.h
  llvm/include/llvm/Analysis/MemoryDependenceAnalysis.h
  llvm/include/llvm/Analysis/SparsePropagation.h
  llvm/include/llvm/CodeGen/ReplaceWithVeclib.h
  llvm/include/llvm/IR/AbstractCallSite.h
  llvm/include/llvm/IR/Attributes.h
  llvm/include/llvm/IR/CFG.h
  llvm/include/llvm/IR/DIBuilder.h
  llvm/include/llvm/IR/DebugInfoMetadata.h
  llvm/include/llvm/IR/DiagnosticInfo.h
  llvm/include/llvm/IR/Dominators.h
  llvm/include/llvm/IR/IRBuilder.h
  llvm/include/llvm/IR/IRPrintingPasses.h
  llvm/include/llvm/IR/Instruction.h
  llvm/include/llvm/IR/Instructions.h
  llvm/include/llvm/IR/LLVMContext.h
  llvm/include/llvm/IR/LLVMRemarkStreamer.h
  llvm/include/llvm/IR/LegacyPassManager.h
  llvm/include/llvm/IR/MDBuilder.h
  llvm/include/llvm/IR/Metadata.h
  llvm/include/llvm/IR/ModuleSummaryIndex.h
  llvm/include/llvm/IR/PassManager.h
  llvm/include/llvm/IR/PassTimingInfo.h
  llvm/include/llvm/IR/ReplaceConstant.h
  llvm/include/llvm/IR/SSAContext.h
  llvm/include/llvm/IR/Statepoint.h
  llvm/include/llvm/IR/Type.h
  llvm/include/llvm/IR/Use.h
  llvm/include/llvm/Transforms/AggressiveInstCombine/AggressiveInstCombine.h
  llvm/include/llvm/Transforms/IPO/AlwaysInliner.h
  llvm/include/llvm/Transforms/IPO/ForceFunctionAttrs.h
  llvm/include/llvm/Transforms/IPO/InferFunctionAttrs.h
  llvm/include/llvm/Transforms/IPO/SampleProfile.h
  llvm/include/llvm/Transforms/Instrumentation/AddressSanitizer.h
  llvm/include/llvm/Transforms/Instrumentation/BoundsChecking.h
  llvm/include/llvm/Transforms/Instrumentation/HWAddressSanitizer.h
  llvm/include/llvm/Transforms/Instrumentation/MemProfiler.h
  llvm/include/llvm/Transforms/Scalar/Scalarizer.h
  llvm/include/llvm/Transforms/Scalar/WarnMissedTransforms.h
  llvm/include/llvm/Transforms/Utils/AssumeBundleBuilder.h
  llvm/include/llvm/Transforms/Utils/Debugify.h
  llvm/include/llvm/Transforms/Utils/InjectTLIMappings.h
  llvm/include/llvm/Transforms/Vectorize/LoadStoreVectorizer.h
  llvm/lib/IR/Attributes.cpp
  llvm/lib/IR/AutoUpgrade.cpp
  llvm/lib/IR/BasicBlock.cpp
  llvm/lib/IR/Comdat.cpp
  llvm/lib/IR/ConstantFold.cpp
  llvm/lib/IR/Constants.cpp
  llvm/lib/IR/Core.cpp
  llvm/lib/IR/DIBuilder.cpp
  llvm/lib/IR/DataLayout.cpp
  llvm/lib/IR/DebugInfo.cpp
  llvm/lib/IR/DebugInfoMetadata.cpp
  llvm/lib/IR/DebugLoc.cpp
  llvm/lib/IR/DiagnosticInfo.cpp
  llvm/lib/IR/Dominators.cpp
  llvm/lib/IR/Function.cpp
  llvm/lib/IR/Globals.cpp
  llvm/lib/IR/IRBuilder.cpp
  llvm/lib/IR/InlineAsm.cpp
  llvm/lib/IR/Instruction.cpp
  llvm/lib/IR/IntrinsicInst.cpp
  llvm/lib/IR/LLVMContext.cpp
  llvm/lib/IR/LLVMContextImpl.cpp
  llvm/lib/IR/LLVMContextImpl.h
  llvm/lib/IR/LLVMRemarkStreamer.cpp
  llvm/lib/IR/LegacyPassManager.cpp
  llvm/lib/IR/Metadata.cpp
  llvm/lib/IR/Module.cpp
  llvm/lib/IR/ModuleSummaryIndex.cpp
  llvm/lib/IR/Operator.cpp
  llvm/lib/IR/OptBisect.cpp
  llvm/lib/IR/PassManager.cpp
  llvm/lib/IR/ProfileSummary.cpp
  llvm/lib/IR/PseudoProbe.cpp
  llvm/lib/IR/ReplaceConstant.cpp
  llvm/lib/IR/SSAContext.cpp
  llvm/lib/IR/SafepointIRVerifier.c

[PATCH] D116541: [OpenMP] Introduce new flag to change offloading driver pipeline

2022-01-31 Thread Nico Weber via Phabricator via cfe-commits
thakis added a comment.

Looks like this breaks tests on macOS: http://45.33.8.238/macm1/26856/step_7.txt

Please take a look and revert for now if it takes a while to fix (maybe just 
needs an explicit triple?)


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D116541/new/

https://reviews.llvm.org/D116541

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D113795: Comment Sema: Eliminate or factor out DeclInfo inspection (NFC)

2022-01-31 Thread Aaron Puchert via Phabricator via cfe-commits
aaronpuchert updated this revision to Diff 404693.
aaronpuchert added a comment.

Rename `checkDecl` to `hasDeclThat`, which should hopefully address most issues.

We leave `isFunctionPointerVarDecl` inlined for now, since it's probably wrong 
anyway. (I think we should look for fields.)


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D113795/new/

https://reviews.llvm.org/D113795

Files:
  clang/include/clang/AST/CommentSema.h
  clang/lib/AST/CommentSema.cpp

Index: clang/lib/AST/CommentSema.cpp
===
--- clang/lib/AST/CommentSema.cpp
+++ clang/lib/AST/CommentSema.cpp
@@ -86,6 +86,12 @@
   }
 }
 
+/// \returns \c true if the declaration that this comment is attached to
+/// is a pointer to function/method/block type or has such a type.
+static bool involvesFunctionType(const DeclInfo &Info) {
+  return Info.involvesFunctionType();
+}
+
 ParamCommandComment *Sema::actOnParamCommandStart(
   SourceLocation LocBegin,
   SourceLocation LocEnd,
@@ -95,7 +101,7 @@
   new (Allocator) ParamCommandComment(LocBegin, LocEnd, CommandID,
   CommandMarker);
 
-  if (!involvesFunctionType())
+  if (!hasDeclThat(involvesFunctionType))
 Diag(Command->getLocation(),
  diag::warn_doc_param_not_attached_to_a_function_decl)
   << CommandMarker
@@ -105,23 +111,30 @@
 }
 
 void Sema::checkFunctionDeclVerbatimLine(const BlockCommandComment *Comment) {
+  if (ThisDeclInfo)
+inspectThisDecl();
   unsigned DiagSelect;
   switch (Comment->getCommandID()) {
   case CommandTraits::KCI_function:
   case CommandTraits::KCI_functiongroup:
-if (isAnyFunctionDecl() || isFunctionTemplateDecl())
+if (ThisDeclInfo && ThisDeclInfo->getKind() == DeclInfo::FunctionKind &&
+(isa(ThisDeclInfo->CurrentDecl) ||
+ isa(ThisDeclInfo->CurrentDecl)))
   return;
 DiagSelect = 0;
 break;
   case CommandTraits::KCI_method:
   case CommandTraits::KCI_methodgroup:
-if (isObjCMethodDecl())
+if (ThisDeclInfo && ThisDeclInfo->getKind() == DeclInfo::FunctionKind &&
+isa(ThisDeclInfo->CurrentDecl))
   return;
 DiagSelect = 1;
 break;
   case CommandTraits::KCI_callback:
-if (isFunctionPointerVarDecl())
-  return;
+if (ThisDeclInfo && ThisDeclInfo->getKind() == DeclInfo::VariableKind)
+  if (const auto *VD = dyn_cast(ThisDeclInfo->CurrentDecl))
+if (VD->getType()->isFunctionPointerType())
+  return;
 DiagSelect = 2;
 break;
   default:
@@ -133,35 +146,80 @@
   << Comment->getSourceRange();
 }
 
+static bool isClassOrStructDeclImpl(const Decl *D) {
+  if (auto *record = dyn_cast_or_null(D))
+return !record->isUnion();
+
+  return false;
+}
+
+/// \return \c true if the declaration that this comment is attached to
+/// declares either struct, class or tag typedef.
+static bool isClassOrStructOrTagTypedefDecl(const DeclInfo &Info) {
+  if (!Info.CurrentDecl)
+return false;
+
+  if (isClassOrStructDeclImpl(Info.CurrentDecl))
+return true;
+
+  if (auto *ThisTypedefDecl = dyn_cast(Info.CurrentDecl)) {
+auto UnderlyingType = ThisTypedefDecl->getUnderlyingType();
+if (auto ThisElaboratedType = dyn_cast(UnderlyingType)) {
+  auto DesugaredType = ThisElaboratedType->desugar();
+  if (auto *DesugaredTypePtr = DesugaredType.getTypePtrOrNull()) {
+if (auto *ThisRecordType = dyn_cast(DesugaredTypePtr)) {
+  return isClassOrStructDeclImpl(ThisRecordType->getAsRecordDecl());
+}
+  }
+}
+  }
+
+  return false;
+}
+
+static bool isObjCInterfaceDecl(const DeclInfo &Info) {
+  return isa_and_nonnull(Info.CurrentDecl);
+}
+
+static bool isObjCProtocolDecl(const DeclInfo &Info) {
+  return isa_and_nonnull(Info.CurrentDecl);
+}
+
 void Sema::checkContainerDeclVerbatimLine(const BlockCommandComment *Comment) {
-  switch (Comment->getCommandID()) {
+  if (ThisDeclInfo) {
+inspectThisDecl();
+switch (Comment->getCommandID()) {
 case CommandTraits::KCI_class:
-  if (isClassOrStructOrTagTypedefDecl() || isClassTemplateDecl())
+  if (isClassOrStructOrTagTypedefDecl(*ThisDeclInfo) ||
+  isa_and_nonnull(ThisDeclInfo->CurrentDecl))
 return;
   // Allow @class command on @interface declarations.
   // FIXME. Currently, \class and @class are indistinguishable. So,
   // \class is also allowed on an @interface declaration
-  if (Comment->getCommandMarker() && isObjCInterfaceDecl())
+  if (Comment->getCommandMarker() && isObjCInterfaceDecl(*ThisDeclInfo))
 return;
   break;
 case CommandTraits::KCI_interface:
-  if (isObjCInterfaceDecl())
+  if (isObjCInterfaceDecl(*ThisDeclInfo))
 return;
   break;
 case CommandTraits::KCI_protocol:
-  if (isObjCProtocolDecl())
+  if (isObjCProtocolDecl(*This

  1   2   3   >