clang 24.0.0git
CIRGenCUDANV.cpp
Go to the documentation of this file.
1//========- CIRGenCUDANV.cpp - Interface to NVIDIA CUDA Runtime -----=========//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This provides a class for CUDA code generation targeting the NVIDIA CUDA
10// runtime library.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CIRGenCUDARuntime.h"
15#include "CIRGenCXXABI.h"
16#include "CIRGenFunction.h"
17#include "CIRGenModule.h"
18#include "mlir/IR/Operation.h"
20#include "clang/AST/Attrs.inc"
21#include "clang/AST/Decl.h"
24#include "clang/Basic/Cuda.h"
27#include "llvm/Support/Casting.h"
28
29using namespace clang;
30using namespace clang::CIRGen;
31
32namespace {
33
34class CIRGenNVCUDARuntime : public CIRGenCUDARuntime {
35protected:
36 StringRef prefix;
37
38 // Map a device stub function to a symbol for identifying kernel in host
39 // code. For CUDA, the symbol for identifying the kernel is the same as the
40 // device stub function. For HIP, they are different.
41 llvm::StringMap<mlir::Operation *> kernelHandles;
42
43 // Map a kernel handle to the kernel stub.
44 llvm::DenseMap<mlir::Operation *, mlir::Operation *> kernelStubs;
45
46 struct VarInfo {
47 cir::GlobalOp var;
48 const VarDecl *d;
49 cir::CUDADeviceVarKind flags;
50 };
51 llvm::SmallVector<VarInfo, 16> deviceVars;
52
53 // Mangle context for device.
54 std::unique_ptr<MangleContext> deviceMC;
55
56private:
57 void emitDeviceStubBodyNew(CIRGenFunction &cgf, cir::FuncOp fn,
58 FunctionArgList &args);
59 mlir::Value prepareKernelArgs(CIRGenFunction &cgf, mlir::Location loc,
60 FunctionArgList &args);
61 mlir::Operation *getKernelHandle(cir::FuncOp fn, GlobalDecl gd) override;
62
63 mlir::Operation *getKernelStub(mlir::Operation *handle) override {
64 auto it = kernelStubs.find(handle);
65 assert(it != kernelStubs.end());
66 return it->second;
67 }
68 std::string addPrefixToName(StringRef funcName) const;
69 std::string addUnderscoredPrefixToName(StringRef funcName) const;
70
71public:
72 CIRGenNVCUDARuntime(CIRGenModule &cgm);
73 ~CIRGenNVCUDARuntime();
74
75 void emitDeviceStub(CIRGenFunction &cgf, cir::FuncOp fn,
76 FunctionArgList &args) override;
77
78 void handleVarRegistration(const VarDecl *vd, cir::GlobalOp var) override;
79 void finalizeModule() override;
80 void handleGlobalReplace(cir::GlobalOp oldGV, cir::GlobalOp newGV) override;
81
82 void internalizeDeviceSideVar(const VarDecl *d,
83 cir::GlobalLinkageKind &linkage) override;
84
85 std::string getDeviceSideName(const NamedDecl *nd) override;
86
87 void registerDeviceVar(const VarDecl *vd, cir::GlobalOp &var, bool isExtern,
88 bool isConstant) {
89 // Attach the device var attribute to the GlobalOp
90 auto &builder = cgm.getBuilder();
91 var->setAttr(cir::CUDAVarRegistrationInfoAttr::getMnemonic(),
92 cir::CUDAVarRegistrationInfoAttr::get(
93 builder.getContext(),
94 getDeviceSideName(cast<NamedDecl>(vd)),
95 cir::CUDADeviceVarKind::Variable, isExtern, isConstant,
96 vd->hasAttr<HIPManagedAttr>()));
97 deviceVars.push_back({
98 var,
99 vd,
100 cir::CUDADeviceVarKind::Variable,
101 });
102 }
103
104 void registerDeviceSurf(const VarDecl *vd, cir::GlobalOp &var,
105 bool isExtern) {
106 auto &builder = cgm.getBuilder();
107
108 var->setAttr(cir::CUDAVarRegistrationInfoAttr::getMnemonic(),
109 cir::CUDAVarRegistrationInfoAttr::get(
110 builder.getContext(),
111 getDeviceSideName(cast<NamedDecl>(vd)),
112 cir::CUDADeviceVarKind::Surface, isExtern,
113 /*isConstant=*/false,
114 /*isManaged=*/false));
115
116 deviceVars.push_back({
117 var,
118 vd,
119 cir::CUDADeviceVarKind::Surface,
120 });
121 }
122};
123
124} // namespace
125
126std::string CIRGenNVCUDARuntime::addPrefixToName(StringRef funcName) const {
127 return (prefix + funcName).str();
128}
129
130std::string
131CIRGenNVCUDARuntime::addUnderscoredPrefixToName(StringRef funcName) const {
132 return ("__" + prefix + funcName).str();
133}
134
135CIRGenNVCUDARuntime::CIRGenNVCUDARuntime(CIRGenModule &cgm)
136 : CIRGenCUDARuntime(cgm),
137 deviceMC(cgm.getASTContext().cudaNVInitDeviceMC()) {
138 if (cgm.getLangOpts().OffloadViaLLVM)
139 cgm.errorNYI("CIRGenNVCUDARuntime: Offload via LLVM");
140 else if (cgm.getLangOpts().HIP)
141 prefix = "hip";
142 else
143 prefix = "cuda";
144}
145
146mlir::Value CIRGenNVCUDARuntime::prepareKernelArgs(CIRGenFunction &cgf,
147 mlir::Location loc,
148 FunctionArgList &args) {
149 CIRGenBuilderTy &builder = cgm.getBuilder();
150
151 // Build void *args[] and populate with the addresses of kernel arguments.
152 auto voidPtrArrayTy = cir::ArrayType::get(cgm.voidPtrTy, args.size());
153 mlir::Value kernelArgs =
154 builder.createAlloca(loc, cir::PointerType::get(voidPtrArrayTy),
155 "kernel_args", CharUnits::fromQuantity(16));
156
157 mlir::Value kernelArgsDecayed =
158 builder.createCast(cir::CastKind::array_to_ptrdecay, kernelArgs,
159 cir::PointerType::get(cgm.voidPtrTy));
160
161 for (const auto &[i, arg] : llvm::enumerate(args)) {
162 mlir::Value index =
163 builder.getConstInt(loc, llvm::APInt(/*numBits=*/32, i));
164 mlir::Value storePos =
165 builder.createPtrStride(loc, kernelArgsDecayed, index);
166 mlir::Value argAddr = cgf.getAddrOfLocalVar(arg).getPointer();
167 mlir::Value argAsVoid = builder.createBitcast(argAddr, cgm.voidPtrTy);
168
169 builder.CIRBaseBuilderTy::createStore(loc, argAsVoid, storePos);
170 }
171
172 return kernelArgsDecayed;
173}
174
175// CUDA 9.0+ uses new way to launch kernels. Parameters are packed in a local
176// array and kernels are launched using cudaLaunchKernel().
177void CIRGenNVCUDARuntime::emitDeviceStubBodyNew(CIRGenFunction &cgf,
178 cir::FuncOp fn,
179 FunctionArgList &args) {
180
181 // This requires arguments to be sent to kernels in a different way.
182 if (cgm.getLangOpts().OffloadViaLLVM)
183 cgm.errorNYI("CIRGenNVCUDARuntime: Offload via LLVM");
184
185 CIRGenBuilderTy &builder = cgm.getBuilder();
186 mlir::Location loc = fn.getLoc();
187
188 // For [cuda|hip]LaunchKernel, we must add another layer of indirection
189 // to arguments. For example, for function `add(int a, float b)`,
190 // we need to pass it as `void *args[2] = { &a, &b }`.
191 mlir::Value kernelArgs = prepareKernelArgs(cgf, loc, args);
192
193 // Lookup cudaLaunchKernel/hipLaunchKernel function.
194 // HIP kernel launching API name depends on -fgpu-default-stream option. For
195 // the default value 'legacy', it is hipLaunchKernel. For 'per-thread',
196 // it is hipLaunchKernel_spt.
197 // cudaError_t cudaLaunchKernel(const void *func, dim3 gridDim, dim3 blockDim,
198 // void **args, size_t sharedMem,
199 // cudaStream_t stream);
200 // hipError_t hipLaunchKernel[_spt](const void *func, dim3 gridDim,
201 // dim3 blockDim, void **args,
202 // size_t sharedMem, hipStream_t stream);
203 TranslationUnitDecl *tuDecl = cgm.getASTContext().getTranslationUnitDecl();
204 DeclContext *dc = TranslationUnitDecl::castToDeclContext(tuDecl);
205
206 // The default stream is usually stream 0 (the legacy default stream).
207 // For per-thread default stream, we need a different LaunchKernel function.
208 std::string kernelLaunchAPI = "LaunchKernel";
209 if (cgm.getLangOpts().GPUDefaultStream ==
210 LangOptions::GPUDefaultStreamKind::PerThread) {
211 if (cgm.getLangOpts().HIP)
212 kernelLaunchAPI += "_spt";
213 else if (cgm.getLangOpts().CUDA)
214 kernelLaunchAPI += "_ptsz";
215 }
216
217 std::string launchKernelName = addPrefixToName(kernelLaunchAPI);
218 const IdentifierInfo &launchII =
219 cgm.getASTContext().Idents.get(launchKernelName);
220 FunctionDecl *cudaLaunchKernelFD = nullptr;
221 for (NamedDecl *result : dc->lookup(&launchII)) {
222 if (FunctionDecl *fd = dyn_cast<FunctionDecl>(result))
223 cudaLaunchKernelFD = fd;
224 }
225
226 if (cudaLaunchKernelFD == nullptr) {
227 cgm.error(cgf.curFuncDecl->getLocation(),
228 "Can't find declaration for " + launchKernelName);
229 return;
230 }
231
232 // Use this function to retrieve arguments for cudaLaunchKernel:
233 // int __[cuda|hip]PopCallConfiguration(dim3 *gridDim, dim3 *blockDim, size_t
234 // *sharedMem, cudaStream_t *stream)
235 //
236 // Here [cuda|hip]Stream_t, while also being the 6th argument of
237 // [cuda|hip]LaunchKernel, is a pointer to some opaque struct.
238
239 mlir::Type dim3Ty = cgf.getTypes().convertType(
240 cudaLaunchKernelFD->getParamDecl(1)->getType());
241 mlir::Type streamTy = cgf.getTypes().convertType(
242 cudaLaunchKernelFD->getParamDecl(5)->getType());
243
244 mlir::Value gridDim =
245 builder.createAlloca(loc, cir::PointerType::get(dim3Ty), "grid_dim",
247 mlir::Value blockDim =
248 builder.createAlloca(loc, cir::PointerType::get(dim3Ty), "block_dim",
250 mlir::Value sharedMem = builder.createAlloca(
251 loc, cir::PointerType::get(cgm.sizeTy), "shared_mem", cgm.getSizeAlign());
252 mlir::Value stream = builder.createAlloca(
253 loc, cir::PointerType::get(streamTy), "stream", cgm.getPointerAlign());
254
255 cir::FuncOp popConfig = cgm.createRuntimeFunction(
256 cir::FuncType::get({gridDim.getType(), blockDim.getType(),
257 sharedMem.getType(), stream.getType()},
258 cgm.sInt32Ty),
259 addUnderscoredPrefixToName("PopCallConfiguration"));
260 cgf.emitRuntimeCall(loc, popConfig, {gridDim, blockDim, sharedMem, stream});
261
262 // Now emit the call to cudaLaunchKernel
263 // [cuda|hip]Error_t [cuda|hip]LaunchKernel(const void *func, dim3 gridDim,
264 // dim3 blockDim,
265 // void **args, size_t sharedMem,
266 // [cuda|hip]Stream_t stream);
267
268 // We now either pick the function or the stub global for cuda, hip
269 // respectively.
270 mlir::Value kernel = [&]() -> mlir::Value {
271 if (cir::GlobalOp globalOp = llvm::dyn_cast_or_null<cir::GlobalOp>(
272 kernelHandles[fn.getSymName()])) {
273 cir::PointerType kernelTy = cir::PointerType::get(globalOp.getSymType());
274 mlir::Value kernelVal = cir::GetGlobalOp::create(builder, loc, kernelTy,
275 globalOp.getSymName());
276 mlir::Value func = builder.createBitcast(kernelVal, cgm.voidPtrTy);
277 return func;
278 }
279 if (cir::FuncOp funcOp = llvm::dyn_cast_or_null<cir::FuncOp>(
280 kernelHandles[fn.getSymName()])) {
281 cir::PointerType kernelTy =
282 cir::PointerType::get(funcOp.getFunctionType());
283 mlir::Value kernelVal =
284 cir::GetGlobalOp::create(builder, loc, kernelTy, funcOp.getSymName());
285 mlir::Value func = builder.createBitcast(kernelVal, cgm.voidPtrTy);
286 return func;
287 }
288 llvm_unreachable("Expected stub handle to be cir::GlobalOp or FuncOp");
289 }();
290
291 CallArgList launchArgs;
292 launchArgs.add(RValue::get(kernel),
293 cudaLaunchKernelFD->getParamDecl(0)->getType());
294 launchArgs.add(
296 cudaLaunchKernelFD->getParamDecl(1)->getType());
297 launchArgs.add(
299 cudaLaunchKernelFD->getParamDecl(2)->getType());
300 launchArgs.add(RValue::get(kernelArgs),
301 cudaLaunchKernelFD->getParamDecl(3)->getType());
302 launchArgs.add(
303 RValue::get(builder.CIRBaseBuilderTy::createLoad(loc, sharedMem)),
304 cudaLaunchKernelFD->getParamDecl(4)->getType());
305 launchArgs.add(RValue::get(builder.CIRBaseBuilderTy::createLoad(loc, stream)),
306 cudaLaunchKernelFD->getParamDecl(5)->getType());
307
308 mlir::Type launchTy =
309 cgm.getTypes().convertType(cudaLaunchKernelFD->getType());
310 mlir::Operation *cudaKernelLauncherFn = cgm.createRuntimeFunction(
311 cast<cir::FuncType>(launchTy), launchKernelName);
312 const CIRGenFunctionInfo &callInfo =
313 cgm.getTypes().arrangeFunctionDeclaration(cudaLaunchKernelFD);
314 cgf.emitCall(callInfo, CIRGenCallee::forDirect(cudaKernelLauncherFn),
315 ReturnValueSlot(), launchArgs);
316
318 !cgf.getLangOpts().HIP)
319 cgm.errorNYI("MSVC CUDA stub handling");
320}
321
322void CIRGenNVCUDARuntime::emitDeviceStub(CIRGenFunction &cgf, cir::FuncOp fn,
323 FunctionArgList &args) {
324
325 if (auto globalOp =
326 llvm::dyn_cast<cir::GlobalOp>(kernelHandles[fn.getSymName()])) {
327 CIRGenBuilderTy &builder = cgm.getBuilder();
328 mlir::Type fnPtrTy = globalOp.getSymType();
329 auto sym = mlir::FlatSymbolRefAttr::get(fn.getSymNameAttr());
330 auto gv = cir::GlobalViewAttr::get(fnPtrTy, sym);
331
332 globalOp->setAttr("initial_value", gv);
333 globalOp->removeAttr("sym_visibility");
334 globalOp->setAttr("alignment", builder.getI64IntegerAttr(
336 }
337
338 // CUDA 9.0 changed the way to launch kernels.
340 CudaFeature::CUDA_USES_NEW_LAUNCH) ||
341 (cgm.getLangOpts().HIP && cgm.getLangOpts().HIPUseNewLaunchAPI) ||
342 cgm.getLangOpts().OffloadViaLLVM)
343 emitDeviceStubBodyNew(cgf, fn, args);
344 else
345 cgm.errorNYI("Emit Stub Body Legacy");
346}
347
349 return new CIRGenNVCUDARuntime(cgm);
350}
351
352CIRGenNVCUDARuntime::~CIRGenNVCUDARuntime() {}
353
354mlir::Operation *CIRGenNVCUDARuntime::getKernelHandle(cir::FuncOp fn,
355 GlobalDecl gd) {
356
357 // Check if we already have a kernel handle for this function
358 auto it = kernelHandles.find(fn.getSymName());
359 if (it != kernelHandles.end()) {
360 mlir::Operation *oldHandle = it->second;
361 // Here we know that the fn did not change. Return it
362 if (kernelStubs[oldHandle] == fn)
363 return oldHandle;
364
365 // We've found the function name, but F itself has changed, so we need to
366 // update the references.
367 if (cgm.getLangOpts().HIP) {
368 // For HIP compilation the handle itself does not change, so we only need
369 // to update the Stub value.
370 kernelStubs[oldHandle] = fn;
371 return oldHandle;
372 }
373 // For non-HIP compilation, erase the old Stub and fall-through to creating
374 // new entries.
375 kernelStubs.erase(oldHandle);
376 }
377
378 // If not targeting HIP, store the function itself
379 if (!cgm.getLangOpts().HIP) {
380 kernelHandles[fn.getSymName()] = fn;
381 kernelStubs[fn] = fn;
382 return fn;
383 }
384
385 // Create a new CIR global variable to represent the kernel handle
386 CIRGenBuilderTy &builder = cgm.getBuilder();
387 StringRef globalName = cgm.getMangledName(
388 gd.getWithKernelReferenceKind(KernelReferenceKind::Kernel));
389 cir::PointerType fnPtrTy = builder.getPointerTo(fn.getFunctionType());
390 cir::GlobalOp globalOp =
391 cgm.createGlobalOp(fn.getLoc(), globalName, fnPtrTy, /*isConstant=*/true);
392
393 globalOp->setAttr("alignment", builder.getI64IntegerAttr(
395
396 // Store references
397 kernelHandles[fn.getSymName()] = globalOp;
398 kernelStubs[globalOp] = fn;
399
400 return globalOp;
401}
402
403void CIRGenNVCUDARuntime::internalizeDeviceSideVar(
404 const VarDecl *d, cir::GlobalLinkageKind &linkage) {
405 if (cgm.getLangOpts().GPURelocatableDeviceCode)
406 cgm.errorNYI(d->getSourceRange(),
407 "internalizeDeviceSideVar: GPU Relocatable Device Code (RDC)");
408
409 // __shared__ variables are odd. Shadows do get created, but
410 // they are not registered with the CUDA runtime, so they
411 // can't really be used to access their device-side
412 // counterparts. It's not clear yet whether it's nvcc's bug or
413 // a feature, but we've got to do the same for compatibility.
414 if (d->hasAttr<CUDADeviceAttr>() || d->hasAttr<CUDAConstantAttr>() ||
415 d->hasAttr<CUDASharedAttr>()) {
416 linkage = cir::GlobalLinkageKind::InternalLinkage;
417 }
418
421 cgm.errorNYI(d->getSourceRange(),
422 "internalizeDeviceSideVar: CUDA Surface/Texture support");
423}
424
425std::string CIRGenNVCUDARuntime::getDeviceSideName(const NamedDecl *nd) {
426 GlobalDecl gd;
427 // nd could be either a kernel or a variable.
428 if (auto *fd = dyn_cast<FunctionDecl>(nd))
429 gd = GlobalDecl(fd, KernelReferenceKind::Kernel);
430 else
431 gd = GlobalDecl(nd);
432 std::string deviceSideName;
433 MangleContext *mc;
434 if (cgm.getLangOpts().CUDAIsDevice)
435 mc = &cgm.getCXXABI().getMangleContext();
436 else
437 mc = deviceMC.get();
438 if (mc->shouldMangleDeclName(nd)) {
439 SmallString<256> buffer;
440 llvm::raw_svector_ostream out(buffer);
441 mc->mangleName(gd, out);
442 deviceSideName = std::string(out.str());
443 } else
444 deviceSideName = std::string(nd->getIdentifier()->getName());
445
446 // Make unique name for device side static file-scope variable for HIP.
447 if (cgm.getASTContext().shouldExternalize(nd) &&
448 cgm.getLangOpts().GPURelocatableDeviceCode) {
449 SmallString<256> buffer;
450 llvm::raw_svector_ostream out(buffer);
451 out << deviceSideName;
453 deviceSideName = std::string(out.str());
454 }
455 return deviceSideName;
456}
457
458void CIRGenNVCUDARuntime::handleVarRegistration(const VarDecl *vd,
459 cir::GlobalOp var) {
460 if (vd->hasAttr<CUDADeviceAttr>() || vd->hasAttr<CUDAConstantAttr>()) {
461 // Shadow variables and their properties must be registered with CUDA
462 // runtime. Skip Extern global variables, which will be registered in
463 // the TU where they are defined.
464 //
465 // Don't register a C++17 inline variable. The local symbol can be
466 // discarded and referencing a discarded local symbol from outside the
467 // comdat (__cuda_register_globals) is disallowed by the ELF spec.
468 //
469 // HIP managed variables need to be always recorded in device and host
470 // compilations for transformation.
471 //
472 // HIP managed variables and variables in CUDADeviceVarODRUsedByHost are
473 // added to llvm.compiler-used, therefore they are safe to be registered.
474 if ((!vd->hasExternalStorage() && !vd->isInline()) ||
475 cgm.getASTContext().CUDADeviceVarODRUsedByHost.contains(vd) ||
476 vd->hasAttr<HIPManagedAttr>()) {
477 registerDeviceVar(vd, var, !vd->hasDefinition(),
478 vd->hasAttr<CUDAConstantAttr>());
479 }
480 } else if (vd->getType()->isCUDADeviceBuiltinSurfaceType()) {
481 // Builtin surfaces and their template arguments are also registered
482 // with CUDA runtime.
483 if (!vd->hasExternalStorage())
484 registerDeviceSurf(vd, var, !vd->hasDefinition());
485
486 } else if (vd->getType()->isCUDADeviceBuiltinTextureType()) {
487 cgm.errorNYI(vd->getSourceRange(),
488 "handleVarRegistration: Texture registration");
489 }
490}
491
492void CIRGenNVCUDARuntime::handleGlobalReplace(cir::GlobalOp oldGV,
493 cir::GlobalOp newGV) {
494 for (auto &info : deviceVars) {
495 if (info.var == oldGV)
496 info.var = newGV;
497 }
498}
499
500void CIRGenNVCUDARuntime::finalizeModule() {
501 if (!cgm.getLangOpts().CUDAIsDevice)
502 return;
503
504 // Mark ODR-used device variables as compiler used to prevent them from being
505 // eliminated by optimization. This is necessary for device variables
506 // ODR-used by host functions. Sema correctly marks them as ODR-used no
507 // matter whether they are ODR-used by device or host functions.
508 //
509 // We do not need to do this if the variable has used attribute since it
510 // has already been added.
511 //
512 // Static device variables have been externalized at this point, therefore
513 // variables with private or internal linkage need not be added.
514 for (auto &&info : deviceVars) {
515 auto kind = info.flags;
516 bool isDecl = info.var.isDeclaration();
517 bool isLocalLinkage = cir::isLocalLinkage(info.var.getLinkage());
518 bool isVarOrSurfaceOrTexture = (kind == cir::CUDADeviceVarKind::Variable ||
519 kind == cir::CUDADeviceVarKind::Surface ||
520 kind == cir::CUDADeviceVarKind::Texture);
521 bool isUsed = info.d->isUsed();
522 bool hasUsedAttr = info.d->hasAttr<UsedAttr>();
523 if (!isDecl && !isLocalLinkage && isVarOrSurfaceOrTexture && isUsed &&
524 !hasUsedAttr) {
525 if (auto globalValue = mlir::dyn_cast<cir::CIRGlobalValueInterface>(
526 info.var.getOperation())) {
527 cgm.addCompilerUsedGlobal(globalValue);
528 }
529 }
530 }
531}
Defines the clang::ASTContext interface.
Provides definitions for the various language-specific address spaces.
*collection of selector each with an associated kind and an ordered *collection of selectors A selector has a kind
__CUDA_BUILTIN_VAR __cuda_builtin_blockDim_t blockDim
__CUDA_BUILTIN_VAR __cuda_builtin_gridDim_t gridDim
mlir::Value createCast(mlir::Location loc, cir::CastKind kind, mlir::Value src, mlir::Type newTy)
cir::PtrStrideOp createPtrStride(mlir::Location loc, mlir::Value base, mlir::Value stride)
cir::PointerType getPointerTo(mlir::Type ty)
mlir::Value createAlloca(mlir::Location loc, cir::PointerType addrType, llvm::StringRef name, mlir::IntegerAttr alignment, mlir::Value dynAllocSize)
mlir::Value createBitcast(mlir::Value src, mlir::Type newTy)
TranslationUnitDecl * getTranslationUnitDecl() const
bool shouldExternalize(const Decl *D) const
Whether a C++ static variable or CUDA/HIP kernel should be externalized.
IdentifierTable & Idents
Definition ASTContext.h:808
llvm::SetVector< const VarDecl * > CUDADeviceVarODRUsedByHost
Keep track of CUDA/HIP device-side variables ODR-used by host code.
const TargetInfo & getTargetInfo() const
Definition ASTContext.h:927
mlir::Value getPointer() const
Definition Address.h:98
cir::ConstantOp getConstInt(mlir::Location loc, llvm::APSInt intVal)
clang::MangleContext & getMangleContext()
Gets the mangle context.
static CIRGenCallee forDirect(mlir::Operation *funcPtr, const CIRGenCalleeInfo &abstractInfo=CIRGenCalleeInfo())
Definition CIRGenCall.h:92
CIRGenTypes & getTypes() const
const clang::LangOptions & getLangOpts() const
const clang::Decl * curFuncDecl
Address getAddrOfLocalVar(const clang::VarDecl *vd)
Return the address of a local variable.
RValue emitCall(const CIRGenFunctionInfo &funcInfo, const CIRGenCallee &callee, ReturnValueSlot returnValue, const CallArgList &args, cir::CIRCallOpInterface *callOp, mlir::Location loc)
mlir::Value emitRuntimeCall(mlir::Location loc, cir::FuncOp callee, llvm::ArrayRef< mlir::Value > args={}, mlir::NamedAttrList attrs={})
This class organizes the cross-function state that is used while generating CIR code.
llvm::StringRef getMangledName(clang::GlobalDecl gd)
DiagnosticBuilder errorNYI(SourceLocation, llvm::StringRef)
Helpers to emit "not yet implemented" error diagnostics.
clang::ASTContext & getASTContext() const
CIRGenBuilderTy & getBuilder()
const clang::TargetInfo & getTarget() const
void error(SourceLocation loc, llvm::StringRef error)
Emit a general error that something can't be done.
cir::FuncOp createRuntimeFunction(cir::FuncType ty, llvm::StringRef name, mlir::NamedAttrList extraAttrs={}, bool isLocal=false, bool assumeConvergent=false)
const clang::LangOptions & getLangOpts() const
void printPostfixForExternalizedDecl(llvm::raw_ostream &os, const Decl *d)
Print the postfix for externalized static variable or kernels for single source offloading languages ...
cir::GlobalOp createGlobalOp(mlir::Location loc, llvm::StringRef name, mlir::Type t, bool isConstant=false, mlir::ptr::MemorySpaceAttrInterface addrSpace={}, mlir::Operation *insertPoint=nullptr)
void addCompilerUsedGlobal(cir::CIRGlobalValueInterface gv)
Add a global value to the llvmCompilerUsed list.
CIRGenCXXABI & getCXXABI() const
const CIRGenFunctionInfo & arrangeFunctionDeclaration(const clang::FunctionDecl *fd)
Free functions are functions that are compatible with an ordinary C function pointer type.
mlir::Type convertType(clang::QualType type)
Convert a Clang type into a mlir::Type.
void add(RValue rvalue, clang::QualType type)
Definition CIRGenCall.h:239
Type for representing both the decl and type of parameters to a function.
Definition CIRGenCall.h:193
static RValue get(mlir::Value v)
Definition CIRGenValue.h:83
static RValue getAggregate(Address addr, bool isVolatile=false)
Convert an Address to an RValue.
QuantityType getQuantity() const
getQuantity - Get the raw integer representation of this quantity.
Definition CharUnits.h:185
static CharUnits fromQuantity(QuantityType Quantity)
fromQuantity - Construct a CharUnits quantity from a raw integer type.
Definition CharUnits.h:63
lookup_result lookup(DeclarationName Name) const
lookup - Find the declarations (if any) with the given Name in this context.
SourceLocation getLocation() const
Definition DeclBase.h:447
bool hasAttr() const
Definition DeclBase.h:585
const ParmVarDecl * getParamDecl(unsigned i) const
Definition Decl.h:2837
GlobalDecl - represents a global declaration.
Definition GlobalDecl.h:57
GlobalDecl getWithKernelReferenceKind(KernelReferenceKind Kind)
Definition GlobalDecl.h:203
StringRef getName() const
Return the actual identifier string.
IdentifierInfo & get(StringRef Name)
Return the identifier token info for the specified named identifier.
GPUDefaultStreamKind GPUDefaultStream
The default stream kind used for HIP kernel launching.
bool shouldMangleDeclName(const NamedDecl *D)
Definition Mangle.cpp:129
void mangleName(GlobalDecl GD, raw_ostream &)
Definition Mangle.cpp:245
IdentifierInfo * getIdentifier() const
Get the identifier that names this declaration, if there is one.
Definition Decl.h:295
bool isMicrosoft() const
Is this ABI an MSVC-compatible ABI?
TargetCXXABI getCXXABI() const
Get the C++ ABI currently in use.
const llvm::VersionTuple & getSDKVersion() const
static DeclContext * castToDeclContext(const TranslationUnitDecl *D)
Definition Decl.h:151
bool isCUDADeviceBuiltinSurfaceType() const
Check if the type is the CUDA device builtin surface type.
Definition Type.cpp:5478
bool isCUDADeviceBuiltinTextureType() const
Check if the type is the CUDA device builtin texture type.
Definition Type.cpp:5487
QualType getType() const
Definition Decl.h:723
SourceRange getSourceRange() const override LLVM_READONLY
Source range that this declaration covers.
Definition Decl.cpp:2171
bool isInline() const
Whether this variable is (C++1z) inline.
Definition Decl.h:1575
bool hasExternalStorage() const
Returns true if a variable has extern or private_extern storage.
Definition Decl.h:1238
DefinitionKind hasDefinition(ASTContext &) const
Check whether this variable is defined in this translation unit.
Definition Decl.cpp:2356
static bool isLocalLinkage(GlobalLinkageKind linkage)
Definition CIROpsEnums.h:51
CIRGenCUDARuntime * createNVCUDARuntime(CIRGenModule &cgm)
constexpr Variable var(Literal L)
Returns the variable of L.
Definition CNFFormula.h:64
@ Address
A pointer to a ValueDecl.
Definition Primitives.h:28
The JSON file list parser is used to communicate input to InstallAPI.
if(T->getSizeExpr()) TRY_TO(TraverseStmt(const_cast< Expr * >(T -> getSizeExpr())))
bool CudaFeatureEnabled(llvm::VersionTuple, CudaFeature)
Definition Cuda.cpp:172
U cast(CodeGen::Address addr)
Definition Address.h:327
clang::CharUnits getPointerAlign() const
clang::CharUnits getSizeAlign() const
cir::PointerType voidPtrTy
void* in address space 0