Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions clang/lib/CIR/CodeGen/CIRGenDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -433,12 +433,15 @@ CIRGenModule::getOrCreateStaticVarDecl(const VarDecl &d,
mlir::Type lty = getTypes().convertTypeForMem(ty);
assert(!cir::MissingFeatures::addressSpace());

if (d.hasAttr<LoaderUninitializedAttr>() || d.hasAttr<CUDASharedAttr>())
mlir::Attribute init = nullptr;
if (d.hasAttr<LoaderUninitializedAttr>())
errorNYI(d.getSourceRange(),
"getOrCreateStaticVarDecl: LoaderUninitializedAttr");
assert(!cir::MissingFeatures::addressSpace());
else if (ty.getAddressSpace() != LangAS::opencl_local &&
Comment thread
ZakyHermawan marked this conversation as resolved.
Outdated
!d.hasAttr<CUDASharedAttr>())
init = builder.getZeroInitAttr(convertType(ty));

mlir::Attribute init = builder.getZeroInitAttr(convertType(ty));
assert(!cir::MissingFeatures::addressSpace());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What's missing here? Is it the AS argument to createVersionedGlobal()?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

createVersionedGlobal did not requrie AddressSpace as argument.
Do the assert statement still necessary ?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

That is being added in #179082, so it will depend on the order in which these patches are merged. I don't think the assertion is needed since that's already in progress, but it's also fine to leave it.


cir::GlobalOp gv = builder.createVersionedGlobal(
getModule(), getLoc(d.getLocation()), name, lty, false, linkage);
Expand Down Expand Up @@ -665,11 +668,6 @@ void CIRGenFunction::emitStaticVarDecl(const VarDecl &d,

var.setAlignment(alignment.getAsAlign().value());

// There are a lot of attributes that need to be handled here. Until
Comment thread
ZakyHermawan marked this conversation as resolved.
// we start to support them, we just report an error if there are any.
if (d.hasAttrs())
cgm.errorNYI(d.getSourceRange(), "static var with attrs");

if (cgm.getCodeGenOpts().KeepPersistentStorageVariables)
cgm.errorNYI(d.getSourceRange(), "static var keep persistent storage");

Expand Down
53 changes: 49 additions & 4 deletions clang/lib/CIR/CodeGen/CIRGenModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -797,6 +797,22 @@ CIRGenModule::getOrCreateCIRGlobal(StringRef mangledName, mlir::Type ty,
"external const declaration with initializer");
}

// TODO(cir): if this method is used to handle functions we must have
Comment thread
ZakyHermawan marked this conversation as resolved.
Outdated
// something closer to GlobalValue::isDeclaration instead of checking for
// initializer.
if (gv.isDeclaration()) {
Comment thread
ZakyHermawan marked this conversation as resolved.
Outdated
// TODO(cir): set target attributes

// External HIP managed variables needed to be recorded for transformation
// in both device and host compilations.
// External HIP managed variables needed to be recorded for transformation
// in both device and host compilations.
if (getLangOpts().CUDA && d && d->hasAttr<HIPManagedAttr>() &&
d->hasExternalStorage())
llvm_unreachable("NYI");
Comment thread
ZakyHermawan marked this conversation as resolved.
Outdated
}

// TODO(cir): address space cast when needed for DAddrSpace.
Comment thread
ZakyHermawan marked this conversation as resolved.
Outdated
return gv;
}

Expand Down Expand Up @@ -947,10 +963,6 @@ void CIRGenModule::emitGlobalVarDefinition(const clang::VarDecl *vd,
errorNYI(vd->getSourceRange(), "annotate global variable");
}

if (langOpts.CUDA) {
Comment thread
ZakyHermawan marked this conversation as resolved.
errorNYI(vd->getSourceRange(), "CUDA global variable");
}

// Set initializer and finalize emission
CIRGenModule::setInitializer(gv, init);
if (emitter)
Expand Down Expand Up @@ -1563,6 +1575,39 @@ CIRGenModule::getAddrOfConstantStringFromLiteral(const StringLiteral *s,
return builder.getGlobalViewAttr(ptrTy, gv);
}

LangAS CIRGenModule::getGlobalVarAddressSpace(const VarDecl *d) {
if (langOpts.OpenCL) {
LangAS as = d ? d->getType().getAddressSpace() : LangAS::opencl_global;
assert(as == LangAS::opencl_global || as == LangAS::opencl_global_device ||
as == LangAS::opencl_global_host || as == LangAS::opencl_constant ||
as == LangAS::opencl_local || as >= LangAS::FirstTargetAddressSpace);
return as;
}

if (langOpts.SYCLIsDevice &&
(!d || d->getType().getAddressSpace() == LangAS::Default))
llvm_unreachable("NYI");
Comment thread
ZakyHermawan marked this conversation as resolved.
Outdated

if (langOpts.CUDA && langOpts.CUDAIsDevice) {
if (d) {
if (d->hasAttr<CUDAConstantAttr>())
return LangAS::cuda_constant;
if (d->hasAttr<CUDASharedAttr>())
return LangAS::cuda_shared;
if (d->hasAttr<CUDADeviceAttr>())
return LangAS::cuda_device;
if (d->getType().isConstQualified())
return LangAS::cuda_constant;
}
return LangAS::cuda_device;
}

if (langOpts.OpenMP)
llvm_unreachable("NYI");
Comment thread
ZakyHermawan marked this conversation as resolved.
Outdated

return getTargetCIRGenInfo().getGlobalVarAddressSpace(*this, d);
}

// TODO(cir): this could be a common AST helper for both CIR and LLVM codegen.
LangAS CIRGenModule::getLangTempAllocaAddressSpace() const {
if (getLangOpts().OpenCL)
Expand Down
10 changes: 10 additions & 0 deletions clang/lib/CIR/CodeGen/CIRGenModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,16 @@ class CIRGenModule : public CIRGenTypeCache {
getAddrOfConstantStringFromLiteral(const StringLiteral *s,
llvm::StringRef name = ".str");

/// Return the AST address space of the underlying global variable for D, as
/// determined by its declaration. Normally this is the same as the address
/// space of D's type, but in CUDA, address spaces are associated with
/// declarations, not types. If D is nullptr, return the default address
/// space for global variable.
///
/// For languages without explicit address spaces, if D has default address
/// space, target-specific global or constant address space may be returned.
LangAS getGlobalVarAddressSpace(const VarDecl *d);
Comment thread
ZakyHermawan marked this conversation as resolved.
Outdated

/// Returns the address space for temporary allocations in the language. This
/// ensures that the allocated variable's address space matches the
/// expectations of the AST, rather than using the target's allocation address
Expand Down
9 changes: 9 additions & 0 deletions clang/lib/CIR/CodeGen/TargetInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,12 @@ bool TargetCIRGenInfo::isNoProtoCallVariadic(
// For everything else, we just prefer false unless we opt out.
return false;
}

clang::LangAS
TargetCIRGenInfo::getGlobalVarAddressSpace(CIRGenModule &cgm,
const clang::VarDecl *d) const {
assert(!cgm.getLangOpts().OpenCL &&
!(cgm.getLangOpts().CUDA && cgm.getLangOpts().CUDAIsDevice) &&
"Address space agnostic languages only");
return d ? d->getType().getAddressSpace() : LangAS::Default;
}
7 changes: 7 additions & 0 deletions clang/lib/CIR/CodeGen/TargetInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ class TargetCIRGenInfo {
/// Returns ABI info helper for the target.
const ABIInfo &getABIInfo() const { return *info; }

/// Get target favored AST address space of a global variable for languages
/// other than OpenCL and CUDA.
/// If \p d is nullptr, returns the default target favored address space
/// for global variable.
virtual clang::LangAS getGlobalVarAddressSpace(CIRGenModule &cgm,
const clang::VarDecl *d) const;

/// Get the address space for alloca.
virtual mlir::ptr::MemorySpaceAttrInterface getCIRAllocaAddressSpace() const {
return cir::LangAddressSpaceAttr::get(&info->cgt.getMLIRContext(),
Expand Down
78 changes: 78 additions & 0 deletions clang/test/CIR/CodeGenCUDA/address-spaces.cu
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#include "Inputs/cuda.h"

// RUN: %clang_cc1 -triple nvptx64-nvidia-cuda -fclangir \
// RUN: -fcuda-is-device -emit-cir -target-sdk-version=12.3 \
// RUN: -I%S/Inputs/ %s -o %t.cir
// RUN: FileCheck --check-prefix=CIR-DEVICE --input-file=%t.cir %s

// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir \
// RUN: -x cuda -emit-cir -target-sdk-version=12.3 \
// RUN: -I%S/Inputs/ %s -o %t.cir
// RUN: FileCheck --check-prefix=CIR-HOST --input-file=%t.cir %s

// RUN: %clang_cc1 -triple nvptx64-nvidia-cuda -fclangir \
// RUN: -fcuda-is-device -emit-llvm -target-sdk-version=12.3 \
// RUN: -I%S/Inputs/ %s -o %t.ll
// RUN: FileCheck --check-prefix=LLVM-DEVICE --input-file=%t.ll %s

// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir \
// RUN: -x cuda -emit-llvm -target-sdk-version=12.3 \
// RUN: -I%S/Inputs/ %s -o %t.ll
// RUN: FileCheck --check-prefix=LLVM-HOST --input-file=%t.ll %s

// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu \
// RUN: -x cuda -emit-llvm -target-sdk-version=12.3 \
// RUN: -I%S/Inputs/ %s -o %t.ll
// RUN: FileCheck --check-prefix=OGCG-HOST --input-file=%t.ll %s

// RUN: %clang_cc1 -triple nvptx64-nvidia-cuda \
// RUN: -fcuda-is-device -emit-llvm -target-sdk-version=12.3 \
// RUN: -I%S/Inputs/ %s -o %t.ll
// RUN: FileCheck --check-prefix=OGCG-DEVICE --input-file=%t.ll %s

__global__ void fn() {
int i = 0;
__shared__ int j;
j = i;
}

// CIR-DEVICE: cir.global "private" internal dso_local @_ZZ2fnvE1j : !s32i
// CIR-DEVICE: cir.func {{.*}}@_Z2fnv() {{.*}} {
// CIR-DEVICE: %[[I:.*]] = cir.alloca !s32i, !cir.ptr<!s32i>, ["i", init]
// CIR-DEVICE: %[[ZERO:.*]] = cir.const #cir.int<0> : !s32i
// CIR-DEVICE: cir.store {{.*}}%[[ZERO]], %[[I]] : !s32i, !cir.ptr<!s32i>
// CIR-DEVICE: %[[J:.*]] = cir.get_global @_ZZ2fnvE1j : !cir.ptr<!s32i>
// CIR-DEVICE: %[[VAL:.*]] = cir.load {{.*}}%[[I]] : !cir.ptr<!s32i>, !s32i
// CIR-DEVICE: cir.store {{.*}}%[[VAL]], %[[J]] : !s32i, !cir.ptr<!s32i>
// CIR-DEVICE: cir.return

// CIR-HOST: cir.func private dso_local @__cudaPopCallConfiguration
// CIR-HOST: cir.func private dso_local @cudaLaunchKernel
// CIR-HOST: cir.func {{.*}}@_Z17__device_stub__fnv()

// LLVM-DEVICE: @_ZZ2fnvE1j = internal global i32 undef, align 4
// LLVM-DEVICE: define dso_local void @_Z2fnv()
// LLVM-DEVICE: %[[ALLOCA:.*]] = alloca i32, i64 1, align 4
// LLVM-DEVICE: store i32 0, ptr %[[ALLOCA]], align 4
// LLVM-DEVICE: %[[VAL:.*]] = load i32, ptr %[[ALLOCA]], align 4
// LLVM-DEVICE: store i32 %[[VAL]], ptr @_ZZ2fnvE1j, align 4
// LLVM-DEVICE: ret void

// LLVM-HOST: %struct.dim3 = type { i32, i32, i32 }
// LLVM-HOST: declare {{.*}}i32 @__cudaPopCallConfiguration(ptr, ptr, ptr, ptr)
// LLVM-HOST: declare {{.*}}i32 @cudaLaunchKernel(ptr, %struct.dim3, %struct.dim3, ptr, i64, ptr)
// LLVM-HOST: define dso_local void @_Z17__device_stub__fnv()

// OGCG-HOST: define dso_local void @_Z17__device_stub__fnv()
// OGCG-HOST: entry:
// OGCG-HOST: call i32 @__cudaPopCallConfiguration
// OGCG-HOST: call {{.*}}i32 @cudaLaunchKernel

// OGCG-DEVICE: @_ZZ2fnvE1j = internal addrspace(3) global i32 undef, align 4
// OGCG-DEVICE: define dso_local ptx_kernel void @_Z2fnv()
// OGCG-DEVICE: entry:
// OGCG-DEVICE: %[[I:.*]] = alloca i32, align 4
// OGCG-DEVICE: store i32 0, ptr %[[I]], align 4
// OGCG-DEVICE: %[[VAL:.*]] = load i32, ptr %[[I]], align 4
// OGCG-DEVICE: store i32 %[[VAL]], ptr addrspacecast (ptr addrspace(3) @_ZZ2fnvE1j to ptr), align 4
// OGCG-DEVICE: ret void
47 changes: 47 additions & 0 deletions clang/test/CIR/CodeGenCUDA/global-vars.cu
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include "Inputs/cuda.h"

// RUN: %clang_cc1 -triple nvptx64-nvidia-cuda -fclangir \
// RUN: -fcuda-is-device -emit-cir -target-sdk-version=12.3 \
// RUN: -I%S/Inputs/ %s -o %t.cir
// RUN: FileCheck --check-prefix=CIR-DEVICE --input-file=%t.cir %s

// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir \
// RUN: -x cuda -emit-cir -target-sdk-version=12.3 \
// RUN: -I%S/Inputs/ %s -o %t.cir
// RUN: FileCheck --check-prefix=CIR-HOST --input-file=%t.cir %s

// RUN: %clang_cc1 -triple nvptx64-nvidia-cuda -fclangir \
// RUN: -fcuda-is-device -emit-llvm -target-sdk-version=12.3 \
// RUN: -I%S/Inputs/ %s -o %t.ll
// RUN: FileCheck --check-prefix=LLVM-DEVICE --input-file=%t.ll %s

// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir \
// RUN: -x cuda -emit-llvm -target-sdk-version=12.3 \
// RUN: -I%S/Inputs/ %s -o %t.ll
// RUN: FileCheck --check-prefix=LLVM-HOST --input-file=%t.ll %s

// RUN: %clang_cc1 -triple nvptx64-nvidia-cuda \
// RUN: -fcuda-is-device -emit-llvm -target-sdk-version=12.3 \
// RUN: -I%S/Inputs/ %s -o %t.ll
// RUN: FileCheck --check-prefix=OGCG-DEVICE --input-file=%t.ll %s

// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu \
// RUN: -x cuda -emit-llvm -target-sdk-version=12.3 \
// RUN: -I%S/Inputs/ %s -o %t.ll
// RUN: FileCheck --check-prefix=OGCG-HOST --input-file=%t.ll %s

__shared__ int a;
// CIR-DEVICE: cir.global external [[SHARED:@.*]] = #cir.int<0> : !s32i {alignment = 4 : i64}
// CIR-HOST: cir.global external [[SHARED_HOST:@.*]] = #cir.int<0> : !s32i {alignment = 4 : i64}
// LLVM-DEVICE: @[[SHARED_LL:.*]] = global i32 0, align 4
// LLVM-HOST: @[[SHARED_LH:.*]] = global i32 0, align 4
// OGCG-DEVICE: @[[SHARED_OD:.*]] = addrspace(3) global i32 undef, align 4
// OGCG-HOST: @[[SHARED_OH:.*]] = internal global i32 undef, align 4

__device__ int b;
// CIR-DEVICE: cir.global external [[DEV:@.*]] = #cir.int<0> : !s32i {alignment = 4 : i64}
// CIR-HOST: cir.global external [[DEV_HOST:@.*]] = #cir.int<0> : !s32i {alignment = 4 : i64}
// LLVM-DEVICE: @[[DEV_LD:.*]] = global i32 0, align 4
// LLVM-HOST: @[[DEV_LH:.*]] = global i32 0, align 4
// OGCG-HOST: @[[DEV_OH:.*]] = internal global i32 undef, align 4
// OGCG-DEVICE: @[[DEV_OD:.*]] = addrspace(1) externally_initialized global i32 0, align 4