Skip to content

Commit 12a6de9

Browse files
committed
Merge remote-tracking branch 'origin/main' into aballman-wg14-n3409
2 parents c6a0b3f + 5668c7b commit 12a6de9

File tree

49 files changed

+1438
-348
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+1438
-348
lines changed

clang/lib/AST/ByteCode/Compiler.cpp

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5167,6 +5167,12 @@ bool Compiler<Emitter>::visitReturnStmt(const ReturnStmt *RS) {
51675167
}
51685168

51695169
template <class Emitter> bool Compiler<Emitter>::visitIfStmt(const IfStmt *IS) {
5170+
auto visitChildStmt = [&](const Stmt *S) -> bool {
5171+
LocalScope<Emitter> SScope(this);
5172+
if (!visitStmt(S))
5173+
return false;
5174+
return SScope.destroyLocals();
5175+
};
51705176
if (auto *CondInit = IS->getInit())
51715177
if (!visitStmt(CondInit))
51725178
return false;
@@ -5175,7 +5181,22 @@ template <class Emitter> bool Compiler<Emitter>::visitIfStmt(const IfStmt *IS) {
51755181
if (!visitDeclStmt(CondDecl))
51765182
return false;
51775183

5178-
// Compile condition.
5184+
// Save ourselves compiling some code and the jumps, etc. if the condition is
5185+
// stataically known to be either true or false. We could look at more cases
5186+
// here, but I think all the ones that actually happen are using a
5187+
// ConstantExpr.
5188+
if (const auto *CE = dyn_cast_if_present<ConstantExpr>(IS->getCond());
5189+
CE && CE->hasAPValueResult() &&
5190+
CE->getResultAPValueKind() == APValue::ValueKind::Int) {
5191+
APSInt Value = CE->getResultAsAPSInt();
5192+
if (Value.getBoolValue())
5193+
return visitChildStmt(IS->getThen());
5194+
else if (const Stmt *Else = IS->getElse())
5195+
return visitChildStmt(Else);
5196+
return true;
5197+
}
5198+
5199+
// Otherwise, compile the condition.
51795200
if (IS->isNonNegatedConsteval()) {
51805201
if (!this->emitIsConstantContext(IS))
51815202
return false;
@@ -5194,35 +5215,20 @@ template <class Emitter> bool Compiler<Emitter>::visitIfStmt(const IfStmt *IS) {
51945215
LabelTy LabelEnd = this->getLabel();
51955216
if (!this->jumpFalse(LabelElse))
51965217
return false;
5197-
{
5198-
LocalScope<Emitter> ThenScope(this);
5199-
if (!visitStmt(IS->getThen()))
5200-
return false;
5201-
if (!ThenScope.destroyLocals())
5202-
return false;
5203-
}
5218+
if (!visitChildStmt(IS->getThen()))
5219+
return false;
52045220
if (!this->jump(LabelEnd))
52055221
return false;
52065222
this->emitLabel(LabelElse);
5207-
{
5208-
LocalScope<Emitter> ElseScope(this);
5209-
if (!visitStmt(Else))
5210-
return false;
5211-
if (!ElseScope.destroyLocals())
5212-
return false;
5213-
}
5223+
if (!visitChildStmt(Else))
5224+
return false;
52145225
this->emitLabel(LabelEnd);
52155226
} else {
52165227
LabelTy LabelEnd = this->getLabel();
52175228
if (!this->jumpFalse(LabelEnd))
52185229
return false;
5219-
{
5220-
LocalScope<Emitter> ThenScope(this);
5221-
if (!visitStmt(IS->getThen()))
5222-
return false;
5223-
if (!ThenScope.destroyLocals())
5224-
return false;
5225-
}
5230+
if (!visitChildStmt(IS->getThen()))
5231+
return false;
52265232
this->emitLabel(LabelEnd);
52275233
}
52285234

clang/lib/AST/TypePrinter.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2093,6 +2093,9 @@ void TypePrinter::printAttributedAfter(const AttributedType *T,
20932093
case attr::ArmMveStrictPolymorphism:
20942094
OS << "__clang_arm_mve_strict_polymorphism";
20952095
break;
2096+
case attr::ExtVectorType:
2097+
OS << "ext_vector_type";
2098+
break;
20962099
}
20972100
OS << "))";
20982101
}

clang/test/Driver/hip-partial-link.hip

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,11 @@
6262
// STATIC: Found undefined HIP fatbin symbol: __hip_fatbin_[[ID2:[0-9a-f]+]]
6363
// STATIC: Found undefined HIP gpubin handle symbol: __hip_gpubin_handle_[[ID1]]
6464
// STATIC: Found undefined HIP gpubin handle symbol: __hip_gpubin_handle_[[ID2]]
65-
// STATIC: "{{.*}}/clang-offload-bundler" {{.*}}-unbundle
66-
// STATIC: "{{.*}}/lld" -flavor gnu -m elf64_amdgpu
67-
// STATIC: "{{.*}}/clang-offload-bundler"
68-
// STATIC: "{{.*}}/clang{{.*}}" -target x86_64-unknown-linux-gnu
69-
// STATIC: "{{.*}}/llvm-ar"
65+
// STATIC: "{{.*[/\\]}}clang-offload-bundler" {{.*}}-unbundle
66+
// STATIC: "{{.*[/\\]}}lld" -flavor gnu -m elf64_amdgpu
67+
// STATIC: "{{.*[/\\]}}clang-offload-bundler"
68+
// STATIC: "{{.*[/\\]}}clang{{.*}}" -target x86_64-unknown-linux-gnu
69+
// STATIC: "{{.*[/\\]}}llvm-ar"
7070

7171
// RUN: %clang -v --target=x86_64-unknown-linux-gnu --no-offload-new-driver \
7272
// RUN: --hip-link -no-hip-rt -fgpu-rdc --offload-arch=gfx906 \

clang/test/Driver/linker-wrapper.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ __attribute__((visibility("protected"), used)) int x;
119119

120120
// HIP: clang{{.*}} -o [[IMG_GFX90A:.+]] --target=amdgcn-amd-amdhsa -mcpu=gfx90a
121121
// HIP: clang{{.*}} -o [[IMG_GFX908:.+]] --target=amdgcn-amd-amdhsa -mcpu=gfx908
122-
// HIP: clang-offload-bundler{{.*}}-type=o -bundle-align=4096 -compress -compression-level=6 -targets=host-x86_64-unknown-linux-gnu,hip-amdgcn-amd-amdhsa--gfx90a,hip-amdgcn-amd-amdhsa--gfx908 -input=/dev/null -input=[[IMG_GFX90A]] -input=[[IMG_GFX908]] -output={{.*}}.hipfb
122+
// HIP: clang-offload-bundler{{.*}}-type=o -bundle-align=4096 -compress -compression-level=6 -targets=host-x86_64-unknown-linux-gnu,hip-amdgcn-amd-amdhsa--gfx90a,hip-amdgcn-amd-amdhsa--gfx908 -input={{/dev/null|NUL}} -input=[[IMG_GFX90A]] -input=[[IMG_GFX908]] -output={{.*}}.hipfb
123123

124124
// RUN: clang-offload-packager -o %t.out \
125125
// RUN: --image=file=%t.elf.o,kind=openmp,triple=amdgcn-amd-amdhsa,arch=gfx908 \
@@ -210,7 +210,7 @@ __attribute__((visibility("protected"), used)) int x;
210210
// RUN: %t.o -o a.out 2>&1 | FileCheck %s --check-prefix=RELOCATABLE-LINK-HIP
211211

212212
// RELOCATABLE-LINK-HIP: clang{{.*}} -o {{.*}}.img --target=amdgcn-amd-amdhsa
213-
// RELOCATABLE-LINK-HIP: clang-offload-bundler{{.*}} -type=o -bundle-align=4096 -targets=host-x86_64-unknown-linux-gnu,hip-amdgcn-amd-amdhsa--gfx90a -input=/dev/null -input={{.*}} -output={{.*}}
213+
// RELOCATABLE-LINK-HIP: clang-offload-bundler{{.*}} -type=o -bundle-align=4096 -targets=host-x86_64-unknown-linux-gnu,hip-amdgcn-amd-amdhsa--gfx90a -input={{/dev/null|NUL}} -input={{.*}} -output={{.*}}
214214
// RELOCATABLE-LINK-HIP: /usr/bin/ld.lld{{.*}}-r
215215
// RELOCATABLE-LINK-HIP: llvm-objcopy{{.*}}a.out --remove-section .llvm.offloading
216216

clang/tools/amdgpu-arch/AMDGPUArch.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ static cl::opt<bool> Help("h", cl::desc("Alias for -help"), cl::Hidden);
2121
// Mark all our options with this category.
2222
static cl::OptionCategory AMDGPUArchCategory("amdgpu-arch options");
2323

24+
cl::opt<bool> Verbose("verbose", cl::desc("Enable verbose output"),
25+
cl::init(false), cl::cat(AMDGPUArchCategory));
26+
2427
static void PrintVersion(raw_ostream &OS) {
2528
OS << clang::getClangToolFullVersion("amdgpu-arch") << '\n';
2629
}

clang/tools/amdgpu-arch/AMDGPUArchByHIP.cpp

Lines changed: 127 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,24 @@
1111
//
1212
//===----------------------------------------------------------------------===//
1313

14+
#include "llvm/ADT/STLExtras.h"
15+
#include "llvm/Support/CommandLine.h"
16+
#include "llvm/Support/ConvertUTF.h"
1417
#include "llvm/Support/DynamicLibrary.h"
1518
#include "llvm/Support/Error.h"
19+
#include "llvm/Support/FileSystem.h"
20+
#include "llvm/Support/Path.h"
21+
#include "llvm/Support/Process.h"
22+
#include "llvm/Support/Program.h"
23+
#include "llvm/Support/VersionTuple.h"
1624
#include "llvm/Support/raw_ostream.h"
25+
#include <algorithm>
26+
#include <string>
27+
#include <vector>
28+
29+
#ifdef _WIN32
30+
#include <windows.h>
31+
#endif
1732

1833
using namespace llvm;
1934

@@ -31,16 +46,124 @@ typedef hipError_t (*hipGetDeviceCount_t)(int *);
3146
typedef hipError_t (*hipDeviceGet_t)(int *, int);
3247
typedef hipError_t (*hipGetDeviceProperties_t)(hipDeviceProp_t *, int);
3348

34-
int printGPUsByHIP() {
49+
extern cl::opt<bool> Verbose;
50+
3551
#ifdef _WIN32
36-
constexpr const char *DynamicHIPPath = "amdhip64.dll";
52+
static std::vector<std::string> getSearchPaths() {
53+
std::vector<std::string> Paths;
54+
55+
// Get the directory of the current executable
56+
if (auto MainExe = sys::fs::getMainExecutable(nullptr, nullptr);
57+
!MainExe.empty())
58+
Paths.push_back(sys::path::parent_path(MainExe).str());
59+
60+
// Get the system directory
61+
wchar_t SystemDirectory[MAX_PATH];
62+
if (GetSystemDirectoryW(SystemDirectory, MAX_PATH) > 0) {
63+
std::string Utf8SystemDir;
64+
if (convertUTF16ToUTF8String(
65+
ArrayRef<UTF16>(reinterpret_cast<const UTF16 *>(SystemDirectory),
66+
wcslen(SystemDirectory)),
67+
Utf8SystemDir))
68+
Paths.push_back(Utf8SystemDir);
69+
}
70+
71+
// Get the Windows directory
72+
wchar_t WindowsDirectory[MAX_PATH];
73+
if (GetWindowsDirectoryW(WindowsDirectory, MAX_PATH) > 0) {
74+
std::string Utf8WindowsDir;
75+
if (convertUTF16ToUTF8String(
76+
ArrayRef<UTF16>(reinterpret_cast<const UTF16 *>(WindowsDirectory),
77+
wcslen(WindowsDirectory)),
78+
Utf8WindowsDir))
79+
Paths.push_back(Utf8WindowsDir);
80+
}
81+
82+
// Get the current working directory
83+
SmallVector<char, 256> CWD;
84+
if (sys::fs::current_path(CWD))
85+
Paths.push_back(std::string(CWD.begin(), CWD.end()));
86+
87+
// Get the PATH environment variable
88+
if (std::optional<std::string> PathEnv = sys::Process::GetEnv("PATH")) {
89+
SmallVector<StringRef, 16> PathList;
90+
StringRef(*PathEnv).split(PathList, sys::EnvPathSeparator);
91+
for (auto &Path : PathList)
92+
Paths.push_back(Path.str());
93+
}
94+
95+
return Paths;
96+
}
97+
98+
// Custom comparison function for dll name
99+
static bool compareVersions(StringRef A, StringRef B) {
100+
auto ParseVersion = [](StringRef S) -> VersionTuple {
101+
unsigned Pos = S.find_last_of('_');
102+
StringRef VerStr = (Pos == StringRef::npos) ? S : S.substr(Pos + 1);
103+
VersionTuple Vt;
104+
(void)VersionTuple::parse(VerStr, Vt);
105+
return Vt;
106+
};
107+
108+
VersionTuple VtA = ParseVersion(A);
109+
VersionTuple VtB = ParseVersion(B);
110+
return VtA > VtB;
111+
}
112+
#endif
113+
114+
// On Windows, prefer amdhip64_n.dll where n is ROCm major version and greater
115+
// value of n takes precedence. If amdhip64_n.dll is not found, fall back to
116+
// amdhip64.dll. The reason is that a normal driver installation only has
117+
// amdhip64_n.dll but we do not know what n is since this program may be used
118+
// with a future version of HIP runtime.
119+
//
120+
// On Linux, always use default libamdhip64.so.
121+
static std::pair<std::string, bool> findNewestHIPDLL() {
122+
#ifdef _WIN32
123+
StringRef HipDLLPrefix = "amdhip64_";
124+
StringRef HipDLLSuffix = ".dll";
125+
126+
std::vector<std::string> SearchPaths = getSearchPaths();
127+
std::vector<std::string> DLLNames;
128+
129+
for (const auto &Dir : SearchPaths) {
130+
std::error_code EC;
131+
for (sys::fs::directory_iterator DirIt(Dir, EC), DirEnd;
132+
DirIt != DirEnd && !EC; DirIt.increment(EC)) {
133+
StringRef Filename = sys::path::filename(DirIt->path());
134+
if (Filename.starts_with(HipDLLPrefix) &&
135+
Filename.ends_with(HipDLLSuffix))
136+
DLLNames.push_back(sys::path::convert_to_slash(DirIt->path()));
137+
}
138+
if (!DLLNames.empty())
139+
break;
140+
}
141+
142+
if (DLLNames.empty())
143+
return {"amdhip64.dll", true};
144+
145+
llvm::sort(DLLNames, compareVersions);
146+
return {DLLNames[0], false};
37147
#else
38-
constexpr const char *DynamicHIPPath = "libamdhip64.so";
148+
// On Linux, fallback to default shared object
149+
return {"libamdhip64.so", true};
39150
#endif
151+
}
152+
153+
int printGPUsByHIP() {
154+
auto [DynamicHIPPath, IsFallback] = findNewestHIPDLL();
155+
156+
if (Verbose) {
157+
if (IsFallback)
158+
outs() << "Using default HIP runtime: " << DynamicHIPPath << '\n';
159+
else
160+
outs() << "Found HIP runtime: " << DynamicHIPPath << '\n';
161+
}
40162

41163
std::string ErrMsg;
42164
auto DynlibHandle = std::make_unique<llvm::sys::DynamicLibrary>(
43-
llvm::sys::DynamicLibrary::getPermanentLibrary(DynamicHIPPath, &ErrMsg));
165+
llvm::sys::DynamicLibrary::getPermanentLibrary(DynamicHIPPath.c_str(),
166+
&ErrMsg));
44167
if (!DynlibHandle->isValid()) {
45168
llvm::errs() << "Failed to load " << DynamicHIPPath << ": " << ErrMsg
46169
<< '\n';

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -603,9 +603,11 @@ Expected<StringRef> writeOffloadFile(const OffloadFile &File) {
603603

604604
StringRef Prefix =
605605
sys::path::stem(Binary.getMemoryBufferRef().getBufferIdentifier());
606-
607-
auto TempFileOrErr = createOutputFile(
608-
Prefix + "-" + Binary.getTriple() + "-" + Binary.getArch(), "o");
606+
SmallString<128> Filename;
607+
(Prefix + "-" + Binary.getTriple() + "-" + Binary.getArch())
608+
.toVector(Filename);
609+
llvm::replace(Filename, ':', '-');
610+
auto TempFileOrErr = createOutputFile(Filename, "o");
609611
if (!TempFileOrErr)
610612
return TempFileOrErr.takeError();
611613

0 commit comments

Comments
 (0)