Skip to content

Commit 9ba844e

Browse files
[ELF][LLDB] Add an nvsass triple (#159459)
When handling CUDA ELF files via objdump or LLDB, the ELF parser in LLVM needs to distinguish if an ELF file is sass or not, which requires a triple for sass to exist in llvm. This patch includes all the necessary changes for LLDB and objdump to correctly identify these files with the correct triple.
1 parent fc73ef4 commit 9ba844e

File tree

9 files changed

+37
-10
lines changed

9 files changed

+37
-10
lines changed

lldb/include/lldb/Utility/ArchSpec.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,8 @@ class ArchSpec {
236236

237237
eCore_wasm32,
238238

239+
eCore_nvsass,
240+
239241
kNumCores,
240242

241243
kCore_invalid,
@@ -282,8 +284,10 @@ class ArchSpec {
282284
kCore_mips64el_last = eCore_mips64r6el,
283285

284286
kCore_mips_first = eCore_mips32,
285-
kCore_mips_last = eCore_mips64r6el
287+
kCore_mips_last = eCore_mips64r6el,
286288

289+
kCore_nvsass_first = eCore_nvsass,
290+
kCore_nvsass_last = eCore_nvsass,
287291
};
288292

289293
/// Default constructor.

lldb/source/Utility/ArchSpec.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,9 @@ static constexpr const CoreDefinition g_core_definitions[] = {
248248

249249
{eByteOrderLittle, 4, 1, 4, llvm::Triple::wasm32, ArchSpec::eCore_wasm32,
250250
"wasm32"},
251+
252+
{eByteOrderLittle, 8, 4, 4, llvm::Triple::nvsass, ArchSpec::eCore_nvsass,
253+
"nvsass"},
251254
};
252255

253256
// Ensure that we have an entry in the g_core_definitions for each core. If you
@@ -412,6 +415,7 @@ static const ArchDefinitionEntry g_elf_arch_entries[] = {
412415
{ArchSpec::eCore_riscv64, llvm::ELF::EM_RISCV, ArchSpec::eRISCVSubType_riscv64}, // riscv64
413416
{ArchSpec::eCore_loongarch32, llvm::ELF::EM_LOONGARCH, ArchSpec::eLoongArchSubType_loongarch32}, // loongarch32
414417
{ArchSpec::eCore_loongarch64, llvm::ELF::EM_LOONGARCH, ArchSpec::eLoongArchSubType_loongarch64}, // loongarch64
418+
{ArchSpec::eCore_nvsass, llvm::ELF::EM_CUDA, }, // nvsass
415419
};
416420
// clang-format on
417421

llvm/include/llvm/Object/ELFObjectFile.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class LLVM_ABI ELFObjectFileBase : public ObjectFile {
6969
SubtargetFeatures getLoongArchFeatures() const;
7070

7171
StringRef getAMDGPUCPUName() const;
72-
StringRef getNVPTXCPUName() const;
72+
StringRef getCUDACPUName() const;
7373

7474
protected:
7575
ELFObjectFileBase(unsigned int Type, MemoryBufferRef Source);
@@ -1431,9 +1431,7 @@ template <class ELFT> Triple::ArchType ELFObjectFile<ELFT>::getArch() const {
14311431
}
14321432

14331433
case ELF::EM_CUDA: {
1434-
if (EF.getHeader().e_ident[ELF::EI_CLASS] == ELF::ELFCLASS32)
1435-
return Triple::nvptx;
1436-
return Triple::nvptx64;
1434+
return Triple::nvsass;
14371435
}
14381436

14391437
case ELF::EM_BPF:

llvm/include/llvm/TargetParser/Triple.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ class Triple {
110110
renderscript32, // 32-bit RenderScript
111111
renderscript64, // 64-bit RenderScript
112112
ve, // NEC SX-Aurora Vector Engine
113+
nvsass, // NVIDIA SASS
113114
LastArchType = ve
114115
};
115116
enum SubArchType {
@@ -905,6 +906,8 @@ class Triple {
905906

906907
bool isAMDGPU() const { return getArch() == Triple::r600 || isAMDGCN(); }
907908

909+
bool isNVSASS() const { return getArch() == Triple::nvsass; }
910+
908911
/// Tests whether the target is Thumb (little and big endian).
909912
bool isThumb() const {
910913
return getArch() == Triple::thumb || getArch() == Triple::thumbeb;
@@ -1273,7 +1276,9 @@ class Triple {
12731276
LLVM_ABI bool isCompatibleWith(const Triple &Other) const;
12741277

12751278
/// Test whether the target triple is for a GPU.
1276-
bool isGPU() const { return isSPIRV() || isNVPTX() || isAMDGPU(); }
1279+
bool isGPU() const {
1280+
return isSPIRV() || isNVPTX() || isAMDGPU() || isNVSASS();
1281+
}
12771282

12781283
/// Merge target triples.
12791284
LLVM_ABI std::string merge(const Triple &Other) const;

llvm/lib/Object/ELFObjectFile.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ std::optional<StringRef> ELFObjectFileBase::tryGetCPUName() const {
438438
case ELF::EM_AMDGPU:
439439
return getAMDGPUCPUName();
440440
case ELF::EM_CUDA:
441-
return getNVPTXCPUName();
441+
return getCUDACPUName();
442442
case ELF::EM_PPC:
443443
case ELF::EM_PPC64:
444444
return StringRef("future");
@@ -620,7 +620,7 @@ StringRef ELFObjectFileBase::getAMDGPUCPUName() const {
620620
}
621621
}
622622

623-
StringRef ELFObjectFileBase::getNVPTXCPUName() const {
623+
StringRef ELFObjectFileBase::getCUDACPUName() const {
624624
assert(getEMachine() == ELF::EM_CUDA);
625625
unsigned SM = getEIdentABIVersion() == ELF::ELFABIVERSION_CUDA_V1
626626
? getPlatformFlags() & ELF::EF_CUDA_SM

llvm/lib/Object/ObjectFile.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ Triple ObjectFile::makeTriple() const {
139139
TheTriple.setObjectFormat(Triple::GOFF);
140140
} else if (TheTriple.isAMDGPU()) {
141141
TheTriple.setVendor(Triple::AMD);
142-
} else if (TheTriple.isNVPTX()) {
142+
} else if (TheTriple.isNVPTX() || TheTriple.isNVSASS()) {
143143
TheTriple.setVendor(Triple::NVIDIA);
144144
}
145145

llvm/lib/TargetParser/TargetDataLayout.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,7 @@ std::string Triple::computeDataLayout(StringRef ABIName) const {
618618
case Triple::shave:
619619
case Triple::renderscript32:
620620
case Triple::renderscript64:
621+
case Triple::nvsass:
621622
// These are all virtual ISAs with no LLVM backend, and therefore no fixed
622623
// LLVM data layout.
623624
return "";

llvm/lib/TargetParser/Triple.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ StringRef Triple::getArchTypeName(ArchType Kind) {
5454
case msp430: return "msp430";
5555
case nvptx64: return "nvptx64";
5656
case nvptx: return "nvptx";
57+
case nvsass:
58+
return "nvsass";
5759
case ppc64: return "powerpc64";
5860
case ppc64le: return "powerpc64le";
5961
case ppc: return "powerpc";
@@ -242,6 +244,9 @@ StringRef Triple::getArchTypePrefix(ArchType Kind) {
242244
case wasm32:
243245
case wasm64: return "wasm";
244246

247+
case nvsass:
248+
return "nvsass";
249+
245250
case riscv32:
246251
case riscv64:
247252
case riscv32be:
@@ -486,6 +491,7 @@ Triple::ArchType Triple::getArchTypeForLLVMName(StringRef Name) {
486491
.Case("xcore", xcore)
487492
.Case("nvptx", nvptx)
488493
.Case("nvptx64", nvptx64)
494+
.Case("nvsass", nvsass)
489495
.Case("amdil", amdil)
490496
.Case("amdil64", amdil64)
491497
.Case("hsail", hsail)
@@ -627,6 +633,7 @@ static Triple::ArchType parseArch(StringRef ArchName) {
627633
.Case("xcore", Triple::xcore)
628634
.Case("nvptx", Triple::nvptx)
629635
.Case("nvptx64", Triple::nvptx64)
636+
.Case("nvsass", Triple::nvsass)
630637
.Case("amdil", Triple::amdil)
631638
.Case("amdil64", Triple::amdil64)
632639
.Case("hsail", Triple::hsail)
@@ -985,6 +992,7 @@ static Triple::ObjectFormatType getDefaultFormat(const Triple &T) {
985992
case Triple::msp430:
986993
case Triple::nvptx64:
987994
case Triple::nvptx:
995+
case Triple::nvsass:
988996
case Triple::ppc64le:
989997
case Triple::ppcle:
990998
case Triple::r600:
@@ -1745,6 +1753,9 @@ unsigned Triple::getArchPointerBitWidth(llvm::Triple::ArchType Arch) {
17451753
case llvm::Triple::mips64:
17461754
case llvm::Triple::mips64el:
17471755
case llvm::Triple::nvptx64:
1756+
// nvsass can represent both 32- and 64-bit pointers, but assume
1757+
// 64-bit for the triple
1758+
case llvm::Triple::nvsass:
17481759
case llvm::Triple::ppc64:
17491760
case llvm::Triple::ppc64le:
17501761
case llvm::Triple::renderscript64:
@@ -1823,6 +1834,7 @@ Triple Triple::get32BitArchVariant() const {
18231834
case Triple::mips:
18241835
case Triple::mipsel:
18251836
case Triple::nvptx:
1837+
case Triple::nvsass:
18261838
case Triple::ppc:
18271839
case Triple::ppcle:
18281840
case Triple::r600:
@@ -1910,6 +1922,7 @@ Triple Triple::get64BitArchVariant() const {
19101922
case Triple::mips64:
19111923
case Triple::mips64el:
19121924
case Triple::nvptx64:
1925+
case Triple::nvsass:
19131926
case Triple::ppc64:
19141927
case Triple::ppc64le:
19151928
case Triple::renderscript64:
@@ -1980,6 +1993,7 @@ Triple Triple::getBigEndianArchVariant() const {
19801993
case Triple::msp430:
19811994
case Triple::nvptx64:
19821995
case Triple::nvptx:
1996+
case Triple::nvsass:
19831997
case Triple::r600:
19841998
case Triple::renderscript32:
19851999
case Triple::renderscript64:
@@ -2095,6 +2109,7 @@ bool Triple::isLittleEndian() const {
20952109
case Triple::msp430:
20962110
case Triple::nvptx64:
20972111
case Triple::nvptx:
2112+
case Triple::nvsass:
20982113
case Triple::ppcle:
20992114
case Triple::ppc64le:
21002115
case Triple::r600:

llvm/unittests/Object/ELFObjectFileTest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ TEST(ELFObjectFileTest, CheckOSAndTriple) {
295295
{ELF::EM_X86_64, ELF::ELFOSABI_AIX, "x86_64--aix"},
296296
{ELF::EM_X86_64, ELF::ELFOSABI_FREEBSD, "x86_64--freebsd"},
297297
{ELF::EM_X86_64, ELF::ELFOSABI_OPENBSD, "x86_64--openbsd"},
298-
{ELF::EM_CUDA, ELF::ELFOSABI_CUDA, "nvptx64-nvidia-cuda"}};
298+
{ELF::EM_CUDA, ELF::ELFOSABI_CUDA, "nvsass-nvidia-cuda"}};
299299
for (auto [Machine, OS, Triple] : Formats) {
300300
const DataForTest D(ELF::ELFCLASS64, ELF::ELFDATA2LSB, Machine, OS,
301301
ELF::EF_AMDGPU_MACH_AMDGCN_LAST);

0 commit comments

Comments
 (0)