Skip to content

Commit

Permalink
Refactor Access Qualifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
NikitaRudenkoIntel authored and AlexeySachkov committed Sep 9, 2020
1 parent 52ec14a commit 518ebe8
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 13 deletions.
2 changes: 1 addition & 1 deletion lib/SPIRV/OCLTypeToSPIRV.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ void OCLTypeToSPIRV::adaptFunctionArguments(Function *F) {
continue;
if (STName.startswith(kSPR2TypeName::ImagePrefix)) {
auto Ty = STName.str();
auto AccStr = getAccessQualifier(Ty);
auto AccStr = getAccessQualifierFullName(Ty);
addAdaptedType(&*Arg, getOrCreateOpaquePtrType(
M, mapOCLTypeNameToSPIRV(Ty, AccStr)));
Changed = true;
Expand Down
15 changes: 14 additions & 1 deletion lib/SPIRV/SPIRVInternal.h
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,13 @@ const static char WriteOnly[] = "write_only";
const static char ReadWrite[] = "read_write";
} // namespace kAccessQualName

namespace kAccessQualPostfix {
const static char ReadOnly[] = "_ro";
const static char WriteOnly[] = "_wo";
const static char ReadWrite[] = "_rw";
const static char Type[] = "_t";
} // namespace kAccessQualPostfix

namespace kMangledName {
const static char Sampler[] = "11ocl_sampler";
const static char AtomicPrefixIncoming[] = "U7_Atomic";
Expand Down Expand Up @@ -882,7 +889,13 @@ std::string mapOCLTypeNameToSPIRV(StringRef Name, StringRef Acc = "");
bool hasAccessQualifiedName(StringRef TyName);

/// Get access qualifier from the type name.
StringRef getAccessQualifier(StringRef TyName);
SPIRVAccessQualifierKind getAccessQualifier(StringRef TyName);

/// Get access qualifier from the type name.
StringRef getAccessQualifierPostfix(SPIRVAccessQualifierKind Access);

/// Get access qualifier from the type name.
StringRef getAccessQualifierFullName(StringRef TyName);

bool eraseUselessFunctions(Module *M);

Expand Down
44 changes: 33 additions & 11 deletions lib/SPIRV/SPIRVUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1452,22 +1452,44 @@ std::string mangleBuiltin(StringRef UniqName, ArrayRef<Type *> ArgTypes,

/// Check if access qualifier is encoded in the type Name.
bool hasAccessQualifiedName(StringRef TyName) {
if (TyName.endswith("_ro_t") || TyName.endswith("_wo_t") ||
TyName.endswith("_rw_t"))
return true;
return false;
if (TyName.size() < 5)
return false;
auto Acc = TyName.substr(TyName.size() - 5, 3);
return llvm::StringSwitch<bool>(Acc)
.Case(kAccessQualPostfix::ReadOnly, true)
.Case(kAccessQualPostfix::WriteOnly, true)
.Case(kAccessQualPostfix::ReadWrite, true)
.Default(false);
}

SPIRVAccessQualifierKind getAccessQualifier(StringRef TyName) {
return SPIRSPIRVAccessQualifierMap::map(
getAccessQualifierFullName(TyName).str());
}

StringRef getAccessQualifierPostfix(SPIRVAccessQualifierKind Access) {
switch (Access) {
case AccessQualifierReadOnly:
return kAccessQualPostfix::ReadOnly;
case AccessQualifierWriteOnly:
return kAccessQualPostfix::WriteOnly;
case AccessQualifierReadWrite:
return kAccessQualPostfix::ReadWrite;
default:
assert(false && "Unrecognized access qualifier!");
return kAccessQualPostfix::ReadWrite;
}
}

/// Get access qualifier from the type Name.
StringRef getAccessQualifier(StringRef TyName) {
StringRef getAccessQualifierFullName(StringRef TyName) {
assert(hasAccessQualifiedName(TyName) &&
"Type is not qualified with access.");
auto Acc = TyName.substr(TyName.size() - 4, 2);
auto Acc = TyName.substr(TyName.size() - 5, 3);
return llvm::StringSwitch<StringRef>(Acc)
.Case("ro", "read_only")
.Case("wo", "write_only")
.Case("rw", "read_write")
.Default("");
.Case(kAccessQualPostfix::ReadOnly, kAccessQualName::ReadOnly)
.Case(kAccessQualPostfix::WriteOnly, kAccessQualName::WriteOnly)
.Case(kAccessQualPostfix::ReadWrite, kAccessQualName::ReadWrite);
}

/// Translates OpenCL image type names to SPIR-V.
Expand All @@ -1476,7 +1498,7 @@ Type *getSPIRVImageTypeFromOCL(Module *M, Type *ImageTy) {
auto ImageTypeName = ImageTy->getPointerElementType()->getStructName();
StringRef Acc = kAccessQualName::ReadOnly;
if (hasAccessQualifiedName(ImageTypeName))
Acc = getAccessQualifier(ImageTypeName);
Acc = getAccessQualifierFullName(ImageTypeName);
return getOrCreateOpaquePtrType(M, mapOCLTypeNameToSPIRV(ImageTypeName, Acc));
}

Expand Down

0 comments on commit 518ebe8

Please sign in to comment.