Skip to content

Commit ecc8ac3

Browse files
author
Anastasia Stulova
committed
[OpenCL] Fix pipe type printing in arg info metadata
Pipe element type spelling for arg info metadata should follow the same behavior as normal type spelling. We should only use the canonical type spelling in the base type field. This patch also removed duplication in type handling. Tags: #clang Differential Revision: https://reviews.llvm.org/D96151
1 parent 981a38b commit ecc8ac3

File tree

2 files changed

+56
-69
lines changed

2 files changed

+56
-69
lines changed

clang/lib/CodeGen/CodeGenModule.cpp

+37-68
Original file line numberDiff line numberDiff line change
@@ -1475,6 +1475,37 @@ void CodeGenModule::GenOpenCLArgMetadata(llvm::Function *Fn,
14751475
QualType ty = parm->getType();
14761476
std::string typeQuals;
14771477

1478+
// Get image and pipe access qualifier:
1479+
if (ty->isImageType() || ty->isPipeType()) {
1480+
const Decl *PDecl = parm;
1481+
if (auto *TD = dyn_cast<TypedefType>(ty))
1482+
PDecl = TD->getDecl();
1483+
const OpenCLAccessAttr *A = PDecl->getAttr<OpenCLAccessAttr>();
1484+
if (A && A->isWriteOnly())
1485+
accessQuals.push_back(llvm::MDString::get(VMContext, "write_only"));
1486+
else if (A && A->isReadWrite())
1487+
accessQuals.push_back(llvm::MDString::get(VMContext, "read_write"));
1488+
else
1489+
accessQuals.push_back(llvm::MDString::get(VMContext, "read_only"));
1490+
} else
1491+
accessQuals.push_back(llvm::MDString::get(VMContext, "none"));
1492+
1493+
// Get argument name.
1494+
argNames.push_back(llvm::MDString::get(VMContext, parm->getName()));
1495+
1496+
auto getTypeSpelling = [&](QualType Ty) {
1497+
auto typeName = Ty.getUnqualifiedType().getAsString(Policy);
1498+
1499+
if (Ty.isCanonical()) {
1500+
StringRef typeNameRef = typeName;
1501+
// Turn "unsigned type" to "utype"
1502+
if (typeNameRef.consume_front("unsigned "))
1503+
return std::string("u") + typeNameRef.str();
1504+
}
1505+
1506+
return typeName;
1507+
};
1508+
14781509
if (ty->isPointerType()) {
14791510
QualType pointeeTy = ty->getPointeeType();
14801511

@@ -1484,26 +1515,10 @@ void CodeGenModule::GenOpenCLArgMetadata(llvm::Function *Fn,
14841515
ArgInfoAddressSpace(pointeeTy.getAddressSpace()))));
14851516

14861517
// Get argument type name.
1487-
std::string typeName =
1488-
pointeeTy.getUnqualifiedType().getAsString(Policy) + "*";
1489-
1490-
// Turn "unsigned type" to "utype"
1491-
std::string::size_type pos = typeName.find("unsigned");
1492-
if (pointeeTy.isCanonical() && pos != std::string::npos)
1493-
typeName.erase(pos + 1, 8);
1494-
1495-
argTypeNames.push_back(llvm::MDString::get(VMContext, typeName));
1496-
1518+
std::string typeName = getTypeSpelling(pointeeTy) + "*";
14971519
std::string baseTypeName =
1498-
pointeeTy.getUnqualifiedType().getCanonicalType().getAsString(
1499-
Policy) +
1500-
"*";
1501-
1502-
// Turn "unsigned type" to "utype"
1503-
pos = baseTypeName.find("unsigned");
1504-
if (pos != std::string::npos)
1505-
baseTypeName.erase(pos + 1, 8);
1506-
1520+
getTypeSpelling(pointeeTy.getCanonicalType()) + "*";
1521+
argTypeNames.push_back(llvm::MDString::get(VMContext, typeName));
15071522
argBaseTypeNames.push_back(
15081523
llvm::MDString::get(VMContext, baseTypeName));
15091524

@@ -1525,30 +1540,9 @@ void CodeGenModule::GenOpenCLArgMetadata(llvm::Function *Fn,
15251540
llvm::ConstantAsMetadata::get(CGF->Builder.getInt32(AddrSpc)));
15261541

15271542
// Get argument type name.
1528-
std::string typeName;
1529-
if (isPipe)
1530-
typeName = ty.getCanonicalType()
1531-
->castAs<PipeType>()
1532-
->getElementType()
1533-
.getAsString(Policy);
1534-
else
1535-
typeName = ty.getUnqualifiedType().getAsString(Policy);
1536-
1537-
// Turn "unsigned type" to "utype"
1538-
std::string::size_type pos = typeName.find("unsigned");
1539-
if (ty.isCanonical() && pos != std::string::npos)
1540-
typeName.erase(pos + 1, 8);
1541-
1542-
std::string baseTypeName;
1543-
if (isPipe)
1544-
baseTypeName = ty.getCanonicalType()
1545-
->castAs<PipeType>()
1546-
->getElementType()
1547-
.getCanonicalType()
1548-
.getAsString(Policy);
1549-
else
1550-
baseTypeName =
1551-
ty.getUnqualifiedType().getCanonicalType().getAsString(Policy);
1543+
ty = isPipe ? ty->castAs<PipeType>()->getElementType() : ty;
1544+
std::string typeName = getTypeSpelling(ty);
1545+
std::string baseTypeName = getTypeSpelling(ty.getCanonicalType());
15521546

15531547
// Remove access qualifiers on images
15541548
// (as they are inseparable from type in clang implementation,
@@ -1560,38 +1554,13 @@ void CodeGenModule::GenOpenCLArgMetadata(llvm::Function *Fn,
15601554
}
15611555

15621556
argTypeNames.push_back(llvm::MDString::get(VMContext, typeName));
1563-
1564-
// Turn "unsigned type" to "utype"
1565-
pos = baseTypeName.find("unsigned");
1566-
if (pos != std::string::npos)
1567-
baseTypeName.erase(pos + 1, 8);
1568-
15691557
argBaseTypeNames.push_back(
15701558
llvm::MDString::get(VMContext, baseTypeName));
15711559

15721560
if (isPipe)
15731561
typeQuals = "pipe";
15741562
}
1575-
15761563
argTypeQuals.push_back(llvm::MDString::get(VMContext, typeQuals));
1577-
1578-
// Get image and pipe access qualifier:
1579-
if (ty->isImageType() || ty->isPipeType()) {
1580-
const Decl *PDecl = parm;
1581-
if (auto *TD = dyn_cast<TypedefType>(ty))
1582-
PDecl = TD->getDecl();
1583-
const OpenCLAccessAttr *A = PDecl->getAttr<OpenCLAccessAttr>();
1584-
if (A && A->isWriteOnly())
1585-
accessQuals.push_back(llvm::MDString::get(VMContext, "write_only"));
1586-
else if (A && A->isReadWrite())
1587-
accessQuals.push_back(llvm::MDString::get(VMContext, "read_write"));
1588-
else
1589-
accessQuals.push_back(llvm::MDString::get(VMContext, "read_only"));
1590-
} else
1591-
accessQuals.push_back(llvm::MDString::get(VMContext, "none"));
1592-
1593-
// Get argument name.
1594-
argNames.push_back(llvm::MDString::get(VMContext, parm->getName()));
15951564
}
15961565

15971566
Fn->setMetadata("kernel_arg_addr_space",

clang/test/CodeGenOpenCL/kernel-arg-info.cl

+19-1
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ typedef write_only image1d_t WOImage;
8585
typedef read_write image1d_t RWImage;
8686
kernel void foo7(ROImage ro, WOImage wo, RWImage rw) {
8787
}
88+
8889
// CHECK: define{{.*}} spir_kernel void @foo7{{[^!]+}}
8990
// CHECK: !kernel_arg_addr_space ![[MD71:[0-9]+]]
9091
// CHECK: !kernel_arg_access_qual ![[MD72:[0-9]+]]
@@ -94,6 +95,18 @@ kernel void foo7(ROImage ro, WOImage wo, RWImage rw) {
9495
// CHECK-NOT: !kernel_arg_name
9596
// ARGINFO: !kernel_arg_name ![[MD76:[0-9]+]]
9697

98+
typedef unsigned char uchar;
99+
typedef uchar uchar2 __attribute__((ext_vector_type(2)));
100+
kernel void foo8(pipe int p1, pipe uchar p2, pipe uchar2 p3, const pipe uchar p4, write_only pipe uchar p5) {}
101+
// CHECK: define{{.*}} spir_kernel void @foo8{{[^!]+}}
102+
// CHECK: !kernel_arg_addr_space ![[PIPE_AS_QUAL:[0-9]+]]
103+
// CHECK: !kernel_arg_access_qual ![[PIPE_ACCESS_QUAL:[0-9]+]]
104+
// CHECK: !kernel_arg_type ![[PIPE_TY:[0-9]+]]
105+
// CHECK: !kernel_arg_base_type ![[PIPE_BASE_TY:[0-9]+]]
106+
// CHECK: !kernel_arg_type_qual ![[PIPE_QUAL:[0-9]+]]
107+
// CHECK-NOT: !kernel_arg_name
108+
// ARGINFO: !kernel_arg_name ![[PIPE_ARG_NAMES:[0-9]+]]
109+
97110
// CHECK: ![[MD11]] = !{i32 1, i32 1, i32 1, i32 1, i32 2, i32 2, i32 1, i32 1, i32 1, i32 1, i32 3, i32 3, i32 3, i32 3, i32 3, i32 3, i32 3, i32 3, i32 0, i32 0, i32 0, i32 0}
98111
// CHECK: ![[MD12]] = !{!"none", !"none", !"none", !"none", !"none", !"none", !"none", !"none", !"none", !"none", !"none", !"none", !"none", !"none", !"none", !"none", !"none", !"none", !"none", !"none", !"none", !"none"}
99112
// CHECK: ![[MD13]] = !{!"int*", !"int*", !"int*", !"int*", !"int*", !"int*", !"int*", !"int*", !"int*", !"int*", !"int*", !"int*", !"int*", !"int*", !"int*", !"int*", !"int*", !"int*", !"int", !"int", !"int", !"int"}
@@ -127,4 +140,9 @@ kernel void foo7(ROImage ro, WOImage wo, RWImage rw) {
127140
// CHECK: ![[MD74]] = !{!"image1d_t", !"image1d_t", !"image1d_t"}
128141
// CHECK: ![[MD75]] = !{!"", !"", !""}
129142
// ARGINFO: ![[MD76]] = !{!"ro", !"wo", !"rw"}
130-
143+
// CHECK: ![[PIPE_AS_QUAL]] = !{i32 1, i32 1, i32 1, i32 1, i32 1}
144+
// CHECK: ![[PIPE_ACCESS_QUAL]] = !{!"read_only", !"read_only", !"read_only", !"read_only", !"write_only"}
145+
// CHECK: ![[PIPE_TY]] = !{!"int", !"uchar", !"uchar2", !"uchar", !"uchar"}
146+
// CHECK: ![[PIPE_BASE_TY]] = !{!"int", !"uchar", !"uchar __attribute__((ext_vector_type(2)))", !"uchar", !"uchar"}
147+
// CHECK: ![[PIPE_QUAL]] = !{!"pipe", !"pipe", !"pipe", !"pipe", !"pipe"}
148+
// ARGINFO: ![[PIPE_ARG_NAMES]] = !{!"p1", !"p2", !"p3", !"p4", !"p5"}

0 commit comments

Comments
 (0)