Skip to content

Commit 53ebe94

Browse files
committed
[CIR][CUDA] Add initial support for name-mangling CUDA declarations
1 parent d329c96 commit 53ebe94

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

clang/lib/CIR/CodeGen/CIRGenModule.cpp

+19-5
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,6 @@ void CIRGenModule::emitGlobal(GlobalDecl GD) {
516516

517517
assert(!Global->hasAttr<IFuncAttr>() && "NYI");
518518
assert(!Global->hasAttr<CPUDispatchAttr>() && "NYI");
519-
assert(!langOpts.CUDA && "NYI");
520519

521520
if (langOpts.OpenMP) {
522521
// If this is OpenMP, check if it is legal to emit this global normally.
@@ -559,6 +558,7 @@ void CIRGenModule::emitGlobal(GlobalDecl GD) {
559558
return;
560559
}
561560
} else {
561+
assert(!langOpts.CUDA && "NYI");
562562
const auto *VD = cast<VarDecl>(Global);
563563
assert(VD->isFileVarDecl() && "Cannot emit local var decl as global.");
564564
if (VD->isThisDeclarationADefinition() != VarDecl::Definition &&
@@ -2291,7 +2291,7 @@ static std::string getMangledNameImpl(CIRGenModule &CGM, GlobalDecl GD,
22912291
assert(0 && "NYI");
22922292
} else if (FD && FD->hasAttr<CUDAGlobalAttr>() &&
22932293
GD.getKernelReferenceKind() == KernelReferenceKind::Stub) {
2294-
assert(0 && "NYI");
2294+
Out << "__device_stub__" << II->getName();
22952295
} else {
22962296
Out << II->getName();
22972297
}
@@ -2309,7 +2309,23 @@ static std::string getMangledNameImpl(CIRGenModule &CGM, GlobalDecl GD,
23092309
if (const auto *FD = dyn_cast<FunctionDecl>(ND)) {
23102310
assert(!FD->isMultiVersion() && "NYI");
23112311
}
2312-
assert(!CGM.getLangOpts().GPURelocatableDeviceCode && "NYI");
2312+
2313+
// we need to generate a unique mangled name if relocatable-device-code
2314+
// is specified, so we postfix it with CUID, which is unique.
2315+
if (CGM.getASTContext().shouldExternalize(ND) &&
2316+
CGM.getLangOpts().GPURelocatableDeviceCode &&
2317+
CGM.getLangOpts().CUDAIsDevice) {
2318+
// ptxas does not allow '.' in symbol names. On the other hand, HIP prefers
2319+
// postfix beginning with '.' since the symbol name can be demangled.
2320+
if (CGM.getLangOpts().HIP)
2321+
Out << (isa<VarDecl>(ND) ? ".static." : ".intern.");
2322+
else
2323+
Out << (isa<VarDecl>(ND) ? "__static__" : "__intern__");
2324+
2325+
// TODO: generate a unique id with other means
2326+
assert(!CGM.getLangOpts().CUID.empty() && "NYI");
2327+
Out << CGM.getASTContext().getCUIDHash();
2328+
}
23132329

23142330
return std::string(Out.str());
23152331
}
@@ -2325,8 +2341,6 @@ StringRef CIRGenModule::getMangledName(GlobalDecl GD) {
23252341
}
23262342
}
23272343

2328-
assert(!langOpts.CUDAIsDevice && "NYI");
2329-
23302344
// Keep the first result in the case of a mangling collision.
23312345
const auto *ND = cast<NamedDecl>(GD.getDecl());
23322346
std::string MangledName = getMangledNameImpl(*this, GD, ND);

0 commit comments

Comments
 (0)