Skip to content
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

Closed
wants to merge 3 commits into from

Conversation

anominos
Copy link

@anominos anominos commented Mar 3, 2025

This PR adds support for __constant__ variables on devices.

anominos added 3 commits March 3, 2025 19:20
getValueFromLangAS: implement cuda constant

fix shouldEmitCUDAGlobalVar
- copies og

emitGlobalVarDefinition: set constant for cudaconstant
- also fixed __device__ check
@anominos anominos marked this pull request as ready for review March 3, 2025 20:51
Copy link

github-actions bot commented Mar 5, 2025

⚠️ C/C++ code formatter, clang-format found issues in your code. ⚠️

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>())

@PikachuHyA PikachuHyA self-requested a review March 5, 2025 02:04
Copy link
Collaborator

@PikachuHyA PikachuHyA left a 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 ={{.*}}
Copy link
Collaborator

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?

@PikachuHyA
Copy link
Collaborator

The CI check for Check Code Formatting has failed. Please format your code using clang-format or adjust it manually.

Copy link
Member

@bcardosolopes bcardosolopes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Per @PikachuHyA comments

bcardosolopes pushed a commit that referenced this pull request Mar 5, 2025
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.
@bcardosolopes
Copy link
Member

Abandoning this PR in favor of #1438, since it's a bit more complete - thanks for the PR @anominos

@anominos
Copy link
Author

anominos commented Mar 6, 2025

#1438 only implements __device__ and __shared__variables, but not __constant__ variables which this PR does @bcardosolopes. See #1444

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants