|
13 | 13 | //===----------------------------------------------------------------------===// |
14 | 14 |
|
15 | 15 | #include "CIRGenCUDARuntime.h" |
| 16 | +#include "CIRGenCXXABI.h" |
16 | 17 | #include "CIRGenFunction.h" |
17 | 18 | #include "mlir/IR/Operation.h" |
18 | 19 | #include "clang/Basic/Cuda.h" |
|
23 | 24 | using namespace clang; |
24 | 25 | using namespace clang::CIRGen; |
25 | 26 |
|
| 27 | +static std::unique_ptr<MangleContext> initDeviceMC(CIRGenModule &cgm) { |
| 28 | + // If the host and device have different C++ ABIs, mark it as the device |
| 29 | + // mangle context so that the mangling needs to retrieve the additional |
| 30 | + // device lambda mangling number instead of the regular host one. |
| 31 | + if (cgm.getASTContext().getAuxTargetInfo() && |
| 32 | + cgm.getASTContext().getTargetInfo().getCXXABI().isMicrosoft() && |
| 33 | + cgm.getASTContext().getAuxTargetInfo()->getCXXABI().isItaniumFamily()) { |
| 34 | + return std::unique_ptr<MangleContext>( |
| 35 | + cgm.getASTContext().createDeviceMangleContext( |
| 36 | + *cgm.getASTContext().getAuxTargetInfo())); |
| 37 | + } |
| 38 | + |
| 39 | + return std::unique_ptr<MangleContext>(cgm.getASTContext().createMangleContext( |
| 40 | + cgm.getASTContext().getAuxTargetInfo())); |
| 41 | +} |
| 42 | + |
26 | 43 | CIRGenCUDARuntime::~CIRGenCUDARuntime() {} |
27 | 44 |
|
28 | | -CIRGenCUDARuntime::CIRGenCUDARuntime(CIRGenModule &cgm) : cgm(cgm) { |
| 45 | +CIRGenCUDARuntime::CIRGenCUDARuntime(CIRGenModule &cgm) |
| 46 | + : cgm(cgm), deviceMC(initDeviceMC(cgm)) { |
29 | 47 | if (cgm.getLangOpts().OffloadViaLLVM) |
30 | 48 | llvm_unreachable("NYI"); |
31 | 49 | else if (cgm.getLangOpts().HIP) |
@@ -289,6 +307,39 @@ mlir::Operation *CIRGenCUDARuntime::getKernelHandle(cir::FuncOp fn, |
289 | 307 | return globalOp; |
290 | 308 | } |
291 | 309 |
|
| 310 | +std::string CIRGenCUDARuntime::getDeviceSideName(const NamedDecl *nd) { |
| 311 | + GlobalDecl gd; |
| 312 | + // nd could be either a kernel or a variable. |
| 313 | + if (auto *fd = dyn_cast<FunctionDecl>(nd)) |
| 314 | + gd = GlobalDecl(fd, KernelReferenceKind::Kernel); |
| 315 | + else |
| 316 | + gd = GlobalDecl(nd); |
| 317 | + std::string deviceSideName; |
| 318 | + MangleContext *mc; |
| 319 | + if (cgm.getLangOpts().CUDAIsDevice) |
| 320 | + mc = &cgm.getCXXABI().getMangleContext(); |
| 321 | + else |
| 322 | + mc = deviceMC.get(); |
| 323 | + if (mc->shouldMangleDeclName(nd)) { |
| 324 | + SmallString<256> buffer; |
| 325 | + llvm::raw_svector_ostream out(buffer); |
| 326 | + mc->mangleName(gd, out); |
| 327 | + deviceSideName = std::string(out.str()); |
| 328 | + } else |
| 329 | + deviceSideName = std::string(nd->getIdentifier()->getName()); |
| 330 | + |
| 331 | + // Make unique name for device side static file-scope variable for HIP. |
| 332 | + if (cgm.getASTContext().shouldExternalize(nd) && |
| 333 | + cgm.getLangOpts().GPURelocatableDeviceCode) { |
| 334 | + SmallString<256> buffer; |
| 335 | + llvm::raw_svector_ostream out(buffer); |
| 336 | + out << deviceSideName; |
| 337 | + cgm.printPostfixForExternalizedDecl(out, nd); |
| 338 | + deviceSideName = std::string(out.str()); |
| 339 | + } |
| 340 | + return deviceSideName; |
| 341 | +} |
| 342 | + |
292 | 343 | void CIRGenCUDARuntime::internalizeDeviceSideVar( |
293 | 344 | const VarDecl *d, cir::GlobalLinkageKind &linkage) { |
294 | 345 | if (cgm.getLangOpts().GPURelocatableDeviceCode) |
|
0 commit comments