-
Notifications
You must be signed in to change notification settings - Fork 130
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[CIR][CUDA] initial support for __constant__ variables #1436
Conversation
getValueFromLangAS: implement cuda constant fix shouldEmitCUDAGlobalVar - copies og emitGlobalVarDefinition: set constant for cudaconstant
- also fixed __device__ check
You can test this locally with the following command:git-clang-format --diff 2ab0704058381f1c8c79d67e6304b0f5d771b4c8 76901bbf6b0c8b770f5e81d563f2cbdb4daa2dfd --extensions cpp -- clang/lib/CIR/CodeGen/CIRGenModule.cpp clang/lib/CIR/Dialect/IR/CIRAttrs.cpp View the diff from clang-format here.diff --git a/clang/lib/CIR/CodeGen/CIRGenModule.cpp b/clang/lib/CIR/CodeGen/CIRGenModule.cpp
index e9afbdf9d5..8b3896a7ba 100644
--- a/clang/lib/CIR/CodeGen/CIRGenModule.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenModule.cpp
@@ -518,10 +518,10 @@ bool CIRGenModule::shouldEmitCUDAGlobalVar(const VarDecl *global) const {
// size and host-side address in order to provide access to
// their device-side incarnations.
return !langOpts.CUDAIsDevice || global->hasAttr<CUDADeviceAttr>() ||
- global->hasAttr<CUDAConstantAttr>() ||
- global->hasAttr<CUDASharedAttr>() ||
- global->getType()->isCUDADeviceBuiltinSurfaceType() ||
- global->getType()->isCUDADeviceBuiltinTextureType();
+ global->hasAttr<CUDAConstantAttr>() ||
+ global->hasAttr<CUDASharedAttr>() ||
+ global->getType()->isCUDADeviceBuiltinSurfaceType() ||
+ global->getType()->isCUDADeviceBuiltinTextureType();
}
void CIRGenModule::emitGlobal(GlobalDecl gd) {
@@ -1448,7 +1448,7 @@ void CIRGenModule::emitGlobalVarDefinition(const clang::VarDecl *d,
// TODO(cir): If it is safe to mark the global 'constant', do so now.
gv.setConstant((d->hasAttr<CUDAConstantAttr>() && langOpts.CUDAIsDevice) ||
(!needsGlobalCtor && !needsGlobalDtor &&
- isTypeConstant(d->getType(), true, true)));
+ isTypeConstant(d->getType(), true, true)));
// If it is in a read-only section, mark it 'constant'.
if (const SectionAttr *sa = d->getAttr<SectionAttr>())
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for working on this! Some comments inline.
__constant__ int b; | ||
|
||
// CIR-DEVICE: cir.global constant external addrspace(offload_constant) @b ={{.*}} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please add a checker for the corresponding LLVM IR?
The CI check for Check Code Formatting has failed. Please format your code using clang-format or adjust it manually. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Per @PikachuHyA comments
Currently `__shared__` and `__constant__` variables are ignored by CodeGen. This patch fixes this. (It is also fixed in #1436 .) Device and constant variables should be marked as `externally_initialized`, as they might be initialized by host, rather than on device. We can't identify which variables are device ones at lowering stage, so this patch adds a new attribute for it in CodeGen. Similar to `__global__` functions, global variables on device corresponds to "shadow" variables on host, and they must be registered to their counterpart. I added a `CUDAShadowNameAttr` in this patch for later use, but I didn't insert code to actually generate it.
#1438 only implements __device__ and __shared__variables, but not __constant__ variables which this PR does @bcardosolopes. See #1444 |
This PR adds support for __constant__ variables on devices.