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
8 changes: 8 additions & 0 deletions llvm/include/llvm/Support/ScopedPrinter.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,14 @@ std::string enumToString(T Value, ArrayRef<EnumEntry<TEnum>> EnumValues) {
return utohexstr(Value, true);
}

template <typename T, typename TEnum>
Copy link
Collaborator

Choose a reason for hiding this comment

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

It is probably useful to add a doxygen comment block above this API explaining that it returns an empty string if the enum doesn't have a direct mapping.

StringRef enumToStringRef(T Value, ArrayRef<EnumEntry<TEnum>> EnumValues) {
for (const EnumEntry<TEnum> &EnumItem : EnumValues)
if (EnumItem.Value == Value)
return EnumItem.AltName;
return "";
}

class LLVM_ABI ScopedPrinter {
public:
enum class ScopedPrinterKind {
Expand Down
41 changes: 11 additions & 30 deletions llvm/lib/Frontend/HLSL/HLSLRootSignature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,6 @@ namespace llvm {
namespace hlsl {
namespace rootsig {

template <typename T>
static std::optional<StringRef> getEnumName(const T Value,
ArrayRef<EnumEntry<T>> Enums) {
for (const auto &EnumItem : Enums)
if (EnumItem.Value == Value)
return EnumItem.Name;
return std::nullopt;
}

template <typename T>
static raw_ostream &printEnum(raw_ostream &OS, const T Value,
ArrayRef<EnumEntry<T>> Enums) {
auto MaybeName = getEnumName(Value, Enums);
if (MaybeName)
OS << *MaybeName;
return OS;
}

template <typename T>
static raw_ostream &printFlags(raw_ostream &OS, const T Value,
ArrayRef<EnumEntry<T>> Flags) {
Expand All @@ -46,9 +28,9 @@ static raw_ostream &printFlags(raw_ostream &OS, const T Value,
if (FlagSet)
OS << " | ";

auto MaybeFlag = getEnumName(T(Bit), Flags);
if (MaybeFlag)
OS << *MaybeFlag;
StringRef MaybeFlag = enumToStringRef(T(Bit), Flags);
if (0 < MaybeFlag.size())
Copy link
Contributor

Choose a reason for hiding this comment

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

this is a very strange way to write this check. Using an std::optional lets you keep the more typical if (MaybeFlag)

Copy link
Contributor

Choose a reason for hiding this comment

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

(this applies anywhere you have if (0 < MaybeFlag.size()))

Copy link
Collaborator

Choose a reason for hiding this comment

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

Alternatively the current code could be written as if( !MaybeFlag.empty())

I don't have strong feelings one way or another but empty strings for optional strings is pretty common.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah fair enough. Checking for empty() is also fine

OS << MaybeFlag;
else
OS << "invalid: " << Bit;

Expand All @@ -70,43 +52,42 @@ static const EnumEntry<RegisterType> RegisterNames[] = {
};

static raw_ostream &operator<<(raw_ostream &OS, const Register &Reg) {
printEnum(OS, Reg.ViewType, ArrayRef(RegisterNames));
OS << Reg.Number;
OS << enumToStringRef(Reg.ViewType, ArrayRef(RegisterNames)) << Reg.Number;

return OS;
}

static raw_ostream &operator<<(raw_ostream &OS,
const llvm::dxbc::ShaderVisibility &Visibility) {
printEnum(OS, Visibility, dxbc::getShaderVisibility());
OS << enumToStringRef(Visibility, dxbc::getShaderVisibility());

return OS;
}

static raw_ostream &operator<<(raw_ostream &OS,
const llvm::dxbc::SamplerFilter &Filter) {
printEnum(OS, Filter, dxbc::getSamplerFilters());
OS << enumToStringRef(Filter, dxbc::getSamplerFilters());

return OS;
}

static raw_ostream &operator<<(raw_ostream &OS,
const dxbc::TextureAddressMode &Address) {
printEnum(OS, Address, dxbc::getTextureAddressModes());
OS << enumToStringRef(Address, dxbc::getTextureAddressModes());

return OS;
}

static raw_ostream &operator<<(raw_ostream &OS,
const dxbc::ComparisonFunc &CompFunc) {
printEnum(OS, CompFunc, dxbc::getComparisonFuncs());
OS << enumToStringRef(CompFunc, dxbc::getComparisonFuncs());

return OS;
}

static raw_ostream &operator<<(raw_ostream &OS,
const dxbc::StaticBorderColor &BorderColor) {
printEnum(OS, BorderColor, dxbc::getStaticBorderColors());
OS << enumToStringRef(BorderColor, dxbc::getStaticBorderColors());

return OS;
}
Expand All @@ -119,8 +100,8 @@ static const EnumEntry<dxil::ResourceClass> ResourceClassNames[] = {
};

static raw_ostream &operator<<(raw_ostream &OS, const ClauseType &Type) {
printEnum(OS, dxil::ResourceClass(llvm::to_underlying(Type)),
ArrayRef(ResourceClassNames));
OS << enumToStringRef(dxil::ResourceClass(llvm::to_underlying(Type)),
ArrayRef(ResourceClassNames));

return OS;
}
Expand Down
25 changes: 10 additions & 15 deletions llvm/lib/Frontend/HLSL/RootSignatureMetadata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,6 @@ static const EnumEntry<dxil::ResourceClass> ResourceClassNames[] = {
{"Sampler", dxil::ResourceClass::Sampler},
};

static std::optional<StringRef> getResourceName(dxil::ResourceClass Class) {
for (const auto &ClassEnum : ResourceClassNames)
if (ClassEnum.Value == Class)
return ClassEnum.Name;
return std::nullopt;
}

namespace {

// We use the OverloadVisit with std::visit to ensure the compiler catches if a
Expand Down Expand Up @@ -133,10 +126,11 @@ MDNode *MetadataBuilder::BuildRootConstants(const RootConstants &Constants) {

MDNode *MetadataBuilder::BuildRootDescriptor(const RootDescriptor &Descriptor) {
IRBuilder<> Builder(Ctx);
std::optional<StringRef> ResName =
getResourceName(dxil::ResourceClass(to_underlying(Descriptor.Type)));
assert(ResName && "Provided an invalid Resource Class");
SmallString<7> Name({"Root", *ResName});
StringRef ResName =
enumToStringRef(dxil::ResourceClass(to_underlying(Descriptor.Type)),
ArrayRef(ResourceClassNames));
assert(0 < ResName.size() && "Provided an invalid Resource Class");
SmallString<7> Name({"Root", ResName});
Metadata *Operands[] = {
MDString::get(Ctx, Name),
ConstantAsMetadata::get(
Expand Down Expand Up @@ -174,11 +168,12 @@ MDNode *MetadataBuilder::BuildDescriptorTable(const DescriptorTable &Table) {
MDNode *MetadataBuilder::BuildDescriptorTableClause(
const DescriptorTableClause &Clause) {
IRBuilder<> Builder(Ctx);
std::optional<StringRef> ResName =
getResourceName(dxil::ResourceClass(to_underlying(Clause.Type)));
assert(ResName && "Provided an invalid Resource Class");
StringRef ResName =
enumToStringRef(dxil::ResourceClass(to_underlying(Clause.Type)),
ArrayRef(ResourceClassNames));
assert(0 < ResName.size() && "Provided an invalid Resource Class");
Metadata *Operands[] = {
MDString::get(Ctx, *ResName),
MDString::get(Ctx, ResName),
ConstantAsMetadata::get(Builder.getInt32(Clause.NumDescriptors)),
ConstantAsMetadata::get(Builder.getInt32(Clause.Reg.Number)),
ConstantAsMetadata::get(Builder.getInt32(Clause.Space)),
Expand Down