Skip to content

[Attr] Extend uses of elementtype attribute #6397

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

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions clang/include/clang/Basic/CodeGenOptions.def
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ CODEGENOPT(HIPCorrectlyRoundedDivSqrt, 1, 1) ///< -fno-hip-fp32-correctly-rounde
CODEGENOPT(SYCLFp32PrecSqrt, 1, 0) ///< -fsycl-fp32-prec-sqrt
CODEGENOPT(UniqueInternalLinkageNames, 1, 0) ///< Internal Linkage symbols get unique names.
CODEGENOPT(SplitMachineFunctions, 1, 0) ///< Split machine functions using profile information.
CODEGENOPT(EnableElementTypeAttrs, 1, 0) ///< Enable emitting `elementtype` attributes on function parameters

/// When false, this attempts to generate code as if the result of an
/// overflowing conversion matches the overflowing behavior of a target's native
Expand Down
7 changes: 7 additions & 0 deletions clang/include/clang/Driver/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -5832,6 +5832,13 @@ defm opaque_pointers : BoolOption<"",
PosFlag<SetTrue, [], "Enable">,
NegFlag<SetFalse, [], "Disable">,
BothFlags<[], " opaque pointers">>;
defm element_type_attrs : BoolOption<"",
"element-type-attrs",
CodeGenOpts<"EnableElementTypeAttrs">,
DefaultFalse,
PosFlag<SetTrue, [], "Enable">,
NegFlag<SetFalse, [], "Disable">,
BothFlags<[], " emitting `elementtype` attributes on function parameters">>;
def discard_value_names : Flag<["-"], "discard-value-names">,
HelpText<"Discard value names in LLVM IR">,
MarshallingInfoFlag<CodeGenOpts<"DiscardValueNames">>;
Expand Down
17 changes: 17 additions & 0 deletions clang/lib/CodeGen/CGCall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "CGBlocks.h"
#include "CGCXXABI.h"
#include "CGCleanup.h"
#include "CGOpenCLRuntime.h"
#include "CGRecordLayout.h"
#include "CodeGenFunction.h"
#include "CodeGenModule.h"
Expand Down Expand Up @@ -1938,6 +1939,9 @@ void CodeGenModule::getDefaultFunctionAttributes(StringRef Name,
FuncAttrs.addAttribute(llvm::Attribute::Convergent);
}

// TODO: NoUnwind attribute should be added for other GPU modes OpenCL, HIP,
// SYCL, OpenMP offload. AFAIK, none of them support exceptions in device
// code.
if (getLangOpts().CUDA && getLangOpts().CUDAIsDevice) {
// Exceptions aren't supported in CUDA device code.
FuncAttrs.addAttribute(llvm::Attribute::NoUnwind);
Expand Down Expand Up @@ -2450,6 +2454,19 @@ void CodeGenModule::ConstructAttributeList(StringRef Name,
Attrs.addAttribute(llvm::Attribute::NoUndef);
}

if (CodeGenOpts.EnableElementTypeAttrs) {
if (auto PtrType = ParamType->getAs<PointerType>())
Attrs.addAttribute(llvm::Attribute::get(
getLLVMContext(), llvm::Attribute::ElementType,
getTypes().ConvertTypeForMem(PtrType->getPointeeType())));
else if (ParamType->isOpenCLSpecificType()) {
llvm::Type *IRType = getOpenCLRuntime().getOpenCLSpecificPointeeType(
ParamType.getTypePtr());
Attrs.addAttribute(llvm::Attribute::get(
getLLVMContext(), llvm::Attribute::ElementType, IRType));
}
}

// 'restrict' -> 'noalias' is done in EmitFunctionProlog when we
// have the corresponding parameter variable. It doesn't make
// sense to do it here because parameters are so messed up.
Expand Down
113 changes: 60 additions & 53 deletions clang/lib/CodeGen/CGOpenCLRuntime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,44 @@ void CGOpenCLRuntime::EmitWorkGroupLocalVarDecl(CodeGenFunction &CGF,
return CGF.EmitStaticVarDecl(D, llvm::GlobalValue::InternalLinkage);
}

llvm::StringRef CGOpenCLRuntime::getOpenCLTypeName(const Type *T) {
assert(T->isOpenCLSpecificType() && "Not an OpenCL specific type!");

switch (cast<BuiltinType>(T)->getKind()) {
default:
llvm_unreachable("Unexpected opencl builtin type!");
return {};
#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
case BuiltinType::Id: \
return "opencl." #ImgType "_" #Suffix "_t";
#include "clang/Basic/OpenCLImageTypes.def"
#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
case BuiltinType::Sampled##Id: \
return "spirv.SampledImage." #ImgType "_" #Suffix "_t";
#define IMAGE_WRITE_TYPE(Type, Id, Ext)
#define IMAGE_READ_WRITE_TYPE(Type, Id, Ext)
#include "clang/Basic/OpenCLImageTypes.def"
case BuiltinType::OCLSampler:
return "opencl.sampler_t";
case BuiltinType::OCLEvent:
return "opencl.event_t";
case BuiltinType::OCLClkEvent:
return "opencl.clk_event_t";
case BuiltinType::OCLQueue:
return "opencl.queue_t";
case BuiltinType::OCLReserveID:
return "opencl.reserve_id_t";
#define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
case BuiltinType::Id: \
return "opencl." #ExtType;
#include "clang/Basic/OpenCLExtensionTypes.def"
}
}

llvm::Type *CGOpenCLRuntime::convertOpenCLSpecificType(const Type *T) {
assert(T->isOpenCLSpecificType() &&
"Not an OpenCL specific type!");

llvm::LLVMContext& Ctx = CGM.getLLVMContext();
uint32_t AddrSpc = CGM.getContext().getTargetAddressSpace(
CGM.getContext().getOpenCLTypeAddrSpace(T));

if (CGM.getTriple().isNVPTX()) {
switch (cast<BuiltinType>(T)->getKind()) {
default:
Expand All @@ -59,51 +89,32 @@ llvm::Type *CGOpenCLRuntime::convertOpenCLSpecificType(const Type *T) {
}
}

switch (cast<BuiltinType>(T)->getKind()) {
default:
llvm_unreachable("Unexpected opencl builtin type!");
return nullptr;
#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
case BuiltinType::Id: \
return getPointerType(T, "opencl." #ImgType "_" #Suffix "_t");
#include "clang/Basic/OpenCLImageTypes.def"
#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
case BuiltinType::Sampled##Id: \
return llvm::PointerType::get( \
llvm::StructType::create(Ctx, "spirv.SampledImage." #ImgType \
"_" #Suffix "_t"), \
AddrSpc);
#define IMAGE_WRITE_TYPE(Type, Id, Ext)
#define IMAGE_READ_WRITE_TYPE(Type, Id, Ext)
#include "clang/Basic/OpenCLImageTypes.def"
case BuiltinType::OCLSampler:
return getSamplerType(T);
case BuiltinType::OCLEvent:
return getPointerType(T, "opencl.event_t");
case BuiltinType::OCLClkEvent:
return getPointerType(T, "opencl.clk_event_t");
case BuiltinType::OCLQueue:
return getPointerType(T, "opencl.queue_t");
case BuiltinType::OCLReserveID:
return getPointerType(T, "opencl.reserve_id_t");
#define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
case BuiltinType::Id: \
return getPointerType(T, "opencl." #ExtType);
#include "clang/Basic/OpenCLExtensionTypes.def"
}
return getPointerToOpaqueType(
getOpenCLTypeName(T), CGM.getContext().getTargetAddressSpace(
CGM.getContext().getOpenCLTypeAddrSpace(T)));
}

llvm::Type *CGOpenCLRuntime::getOpenCLSpecificPointeeType(const Type *T) {
return getOpaqueType(getOpenCLTypeName(T));
}

llvm::Type *CGOpenCLRuntime::getOpaqueType(StringRef Name) {
auto I = OpaqueTypes.find(Name);
if (I != OpaqueTypes.end())
return I->second;

auto *T = llvm::StructType::create(CGM.getLLVMContext(), Name);
OpaqueTypes[Name] = T;
return T;
}

llvm::PointerType *CGOpenCLRuntime::getPointerType(const Type *T,
StringRef Name) {
llvm::PointerType *CGOpenCLRuntime::getPointerToOpaqueType(StringRef Name,
uint32_t AS) {
auto I = CachedTys.find(Name);
if (I != CachedTys.end())
return I->second;

llvm::LLVMContext &Ctx = CGM.getLLVMContext();
uint32_t AddrSpc = CGM.getContext().getTargetAddressSpace(
CGM.getContext().getOpenCLTypeAddrSpace(T));
auto *PTy =
llvm::PointerType::get(llvm::StructType::create(Ctx, Name), AddrSpc);
auto *PTy = llvm::PointerType::get(getOpaqueType(Name), AS);
CachedTys[Name] = PTy;
return PTy;
}
Expand All @@ -118,20 +129,16 @@ llvm::Type *CGOpenCLRuntime::getPipeType(const PipeType *T) {
llvm::Type *CGOpenCLRuntime::getPipeType(const PipeType *T, StringRef Name,
llvm::Type *&PipeTy) {
if (!PipeTy)
PipeTy = llvm::PointerType::get(llvm::StructType::create(
CGM.getLLVMContext(), Name),
CGM.getContext().getTargetAddressSpace(
CGM.getContext().getOpenCLTypeAddrSpace(T)));
PipeTy = llvm::PointerType::get(
getOpaqueType(Name), CGM.getContext().getTargetAddressSpace(
CGM.getContext().getOpenCLTypeAddrSpace(T)));
return PipeTy;
}

llvm::PointerType *CGOpenCLRuntime::getSamplerType(const Type *T) {
if (!SamplerTy)
SamplerTy = llvm::PointerType::get(llvm::StructType::create(
CGM.getLLVMContext(), "opencl.sampler_t"),
CGM.getContext().getTargetAddressSpace(
CGM.getContext().getOpenCLTypeAddrSpace(T)));
return SamplerTy;
return getPointerToOpaqueType(
"opencl.sampler_t", CGM.getContext().getTargetAddressSpace(
CGM.getContext().getOpenCLTypeAddrSpace(T)));
}

llvm::Value *CGOpenCLRuntime::getPipeElemSize(const Expr *PipeArg) {
Expand Down
11 changes: 7 additions & 4 deletions clang/lib/CodeGen/CGOpenCLRuntime.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ class CGOpenCLRuntime {
CodeGenModule &CGM;
llvm::Type *PipeROTy;
llvm::Type *PipeWOTy;
llvm::PointerType *SamplerTy;
llvm::StringMap<llvm::PointerType *> CachedTys;
llvm::StringMap<llvm::Type *> OpaqueTypes;

/// Structure for enqueued block information.
struct EnqueuedBlockInfo {
Expand All @@ -53,11 +53,13 @@ class CGOpenCLRuntime {

virtual llvm::Type *getPipeType(const PipeType *T, StringRef Name,
llvm::Type *&PipeTy);
llvm::PointerType *getPointerType(const Type *T, StringRef Name);
llvm::Type *getOpaqueType(llvm::StringRef Name);
llvm::PointerType *getPointerToOpaqueType(llvm::StringRef Name, uint32_t AS);
llvm::StringRef getOpenCLTypeName(const Type *T);

public:
CGOpenCLRuntime(CodeGenModule &CGM) : CGM(CGM),
PipeROTy(nullptr), PipeWOTy(nullptr), SamplerTy(nullptr) {}
CGOpenCLRuntime(CodeGenModule &CGM)
: CGM(CGM), PipeROTy(nullptr), PipeWOTy(nullptr) {}
virtual ~CGOpenCLRuntime();

/// Emit the IR required for a work-group-local variable declaration, and add
Expand All @@ -66,6 +68,7 @@ class CGOpenCLRuntime {
virtual void EmitWorkGroupLocalVarDecl(CodeGenFunction &CGF,
const VarDecl &D);

virtual llvm::Type *getOpenCLSpecificPointeeType(const Type *T);
virtual llvm::Type *convertOpenCLSpecificType(const Type *T);

virtual llvm::Type *getPipeType(const PipeType *T);
Expand Down
14 changes: 7 additions & 7 deletions llvm/lib/IR/Verifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1949,11 +1949,11 @@ void Verifier::verifyFunctionAttrs(FunctionType *FT, AttributeList Attrs,
if (!IsIntrinsic) {
Check(!ArgAttrs.hasAttribute(Attribute::ImmArg),
"immarg attribute only applies to intrinsics", V);
if (!IsInlineAsm)
Check(!ArgAttrs.hasAttribute(Attribute::ElementType),
"Attribute 'elementtype' can only be applied to intrinsics"
" and inline asm.",
V);
// if (!IsInlineAsm)
// Check(!ArgAttrs.hasAttribute(Attribute::ElementType),
// "Attribute 'elementtype' can only be applied to intrinsics"
// " and inline asm.",
// V);
}

verifyParameterAttrs(ArgAttrs, Ty, V);
Expand Down Expand Up @@ -2455,8 +2455,8 @@ void Verifier::visitFunction(const Function &F) {
Check(!Attrs.hasFnAttr(Attribute::Builtin),
"Attribute 'builtin' can only be applied to a callsite.", &F);

Check(!Attrs.hasAttrSomewhere(Attribute::ElementType),
"Attribute 'elementtype' can only be applied to a callsite.", &F);
// Check(!Attrs.hasAttrSomewhere(Attribute::ElementType),
// "Attribute 'elementtype' can only be applied to a callsite.", &F);

// Check that this function meets the restrictions on this calling convention.
// Sometimes varargs is used for perfectly forwarding thunks, so some of these
Expand Down
2 changes: 0 additions & 2 deletions llvm/test/Verifier/elementtype.ll
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,11 @@ define void @type_mismatch2() {
ret void
}

; CHECK: Attribute 'elementtype' can only be applied to intrinsics and inline asm.
define void @not_intrinsic() {
call void @some_function(i32* elementtype(i32) null)
ret void
}

; CHECK: Attribute 'elementtype' can only be applied to a callsite.
define void @llvm.not_call(i32* elementtype(i32)) {
ret void
}
Expand Down