Skip to content

Commit f83d025

Browse files
anominosAdUhTkJm
authored andcommitted
constvar + address space stuff (llvm#2)
* emit const attr for cuda getValueFromLangAS: implement cuda constant fix shouldEmitCUDAGlobalVar - copies og emitGlobalVarDefinition: set constant for cudaconstant * emitGlobal: move shouldEmitCUDAGlobalVar to first check - matches og * tests: add constant to global-vars.cu - also fixed __device__ check * implement const * fix address space for offload_constant * Fix address space values for NVPTX * add testcase * test: enable const test in addrspace-lowering.cu * remove duplicated case
1 parent 7b699b4 commit f83d025

File tree

5 files changed

+32
-9
lines changed

5 files changed

+32
-9
lines changed

clang/lib/CIR/CodeGen/CIRGenModule.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -569,12 +569,12 @@ bool CIRGenModule::shouldEmitCUDAGlobalVar(const VarDecl *global) const {
569569
// size and host-side address in order to provide access to
570570
// their device-side incarnations.
571571

572-
if (global->hasAttr<CUDAConstantAttr>() ||
573-
global->getType()->isCUDADeviceBuiltinTextureType()) {
572+
if (global->getType()->isCUDADeviceBuiltinTextureType()) {
574573
llvm_unreachable("NYI");
575574
}
576575

577576
return !langOpts.CUDAIsDevice || global->hasAttr<CUDADeviceAttr>() ||
577+
global->hasAttr<CUDAConstantAttr>() ||
578578
global->hasAttr<CUDASharedAttr>() ||
579579
global->getType()->isCUDADeviceBuiltinSurfaceType();
580580
}
@@ -1492,7 +1492,7 @@ void CIRGenModule::emitGlobalVarDefinition(const clang::VarDecl *d,
14921492
// __shared__ variables is not marked as externally initialized,
14931493
// because they must not be initialized.
14941494
if (linkage != cir::GlobalLinkageKind::InternalLinkage &&
1495-
(d->hasAttr<CUDADeviceAttr>() ||
1495+
(d->hasAttr<CUDADeviceAttr>() || d->hasAttr<CUDAConstantAttr>() ||
14961496
d->getType()->isCUDADeviceBuiltinSurfaceType())) {
14971497
gv->setAttr(CUDAExternallyInitializedAttr::getMnemonic(),
14981498
CUDAExternallyInitializedAttr::get(&getMLIRContext()));
@@ -1505,8 +1505,9 @@ void CIRGenModule::emitGlobalVarDefinition(const clang::VarDecl *d,
15051505
emitter->finalize(gv);
15061506

15071507
// TODO(cir): If it is safe to mark the global 'constant', do so now.
1508-
gv.setConstant(!needsGlobalCtor && !needsGlobalDtor &&
1509-
isTypeConstant(d->getType(), true, true));
1508+
gv.setConstant((d->hasAttr<CUDAConstantAttr>() && langOpts.CUDAIsDevice) ||
1509+
(!needsGlobalCtor && !needsGlobalDtor &&
1510+
isTypeConstant(d->getType(), true, true)));
15101511

15111512
// If it is in a read-only section, mark it 'constant'.
15121513
if (const SectionAttr *sa = d->getAttr<SectionAttr>())

clang/lib/CIR/Dialect/IR/CIRAttrs.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -652,13 +652,12 @@ AddressSpaceAttr::getValueFromLangAS(clang::LangAS langAS) {
652652
case LangAS::cuda_device:
653653
return Kind::offload_global;
654654
case LangAS::opencl_constant:
655+
case LangAS::cuda_constant:
655656
return Kind::offload_constant;
656657
case LangAS::opencl_private:
657658
return Kind::offload_private;
658659
case LangAS::opencl_generic:
659660
return Kind::offload_generic;
660-
case LangAS::cuda_constant:
661-
return Kind::offload_constant;
662661
case LangAS::opencl_global_device:
663662
case LangAS::opencl_global_host:
664663
case LangAS::sycl_global:

clang/lib/CIR/Dialect/Transforms/TargetLowering/Targets/NVPTX.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ class NVPTXTargetLoweringInfo : public TargetLoweringInfo {
5252
case Kind::offload_global:
5353
return 1;
5454
case Kind::offload_constant:
55-
return 2;
56-
case Kind::offload_generic:
5755
return 4;
56+
case Kind::offload_generic:
57+
return 0;
5858
default:
5959
cir_cconv_unreachable("Unknown CIR address space for this target");
6060
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include "../Inputs/cuda.h"
2+
3+
// RUN: %clang_cc1 -triple nvptx64-nvidia-cuda -fclangir \
4+
// RUN: -fcuda-is-device -emit-llvm -target-sdk-version=12.3 \
5+
// RUN: %s -o %t.ll
6+
// RUN: FileCheck --check-prefix=LLVM-DEVICE --input-file=%t.ll %s
7+
8+
9+
__shared__ int a;
10+
11+
// LLVM-DEVICE: @a = addrspace(3) {{.*}}
12+
13+
__device__ int b;
14+
15+
// LLVM-DEVICE: @b = addrspace(1) {{.*}}
16+
17+
__constant__ int c;
18+
19+
// LLVM-DEVICE: @c = addrspace(4) {{.*}}

clang/test/CIR/CodeGen/CUDA/global-vars.cu

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,7 @@ __device__ int a;
1717
__shared__ int shared;
1818
// CIR-DEVICE: cir.global external addrspace(offload_local) @shared = #cir.undef
1919
// LLVM-DEVICE: @shared = addrspace(3) global i32 undef, align 4
20+
21+
__constant__ int b;
22+
// CIR-DEVICE: cir.global constant external addrspace(offload_constant) @b = #cir.int<0> : !s32i {alignment = 4 : i64, cu.externally_initialized = #cir.cu.externally_initialized}
23+
// LLVM-DEVICE: @b = addrspace(4) externally_initialized constant i32 0, align 4

0 commit comments

Comments
 (0)