Skip to content

[HLSL] Cleanup TargetInfo handling #90694

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

Merged
merged 2 commits into from
May 2, 2024
Merged
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
10 changes: 1 addition & 9 deletions clang/lib/AST/ASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1612,15 +1612,7 @@ const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
case BuiltinType::Float16:
return Target->getHalfFormat();
case BuiltinType::Half:
// For HLSL, when the native half type is disabled, half will be treat as
// float.
if (getLangOpts().HLSL)
if (getLangOpts().NativeHalfType)
return Target->getHalfFormat();
else
return Target->getFloatFormat();
else
return Target->getHalfFormat();
return Target->getHalfFormat();
case BuiltinType::Float: return Target->getFloatFormat();
case BuiltinType::Double: return Target->getDoubleFormat();
case BuiltinType::Ibm128:
Expand Down
10 changes: 10 additions & 0 deletions clang/lib/Basic/TargetInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,16 @@ void TargetInfo::adjust(DiagnosticsEngine &Diags, LangOptions &Opts) {
LongDoubleAlign = 64;
}

// HLSL explicitly defines the sizes and formats of some data types, and we
// need to conform to those regardless of what architecture you are targeting.
if (Opts.HLSL) {
LongWidth = LongAlign = 64;
if (!Opts.NativeHalfType) {
HalfFormat = &llvm::APFloat::IEEEsingle();
HalfWidth = HalfAlign = 32;
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Just as a note, we don't actually use this value anywhere in the HLSL code generation paths (yet), which is why it wasn't causing problems that we overrode the format returned by the AST context, but didn't override these values.

Copy link
Member

Choose a reason for hiding this comment

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

Is there a test case we can add for this?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, and I totally glossed over that this fixes a bug. Without this line sizeof(half) returns the wrong value.

}
}

if (Opts.OpenCL) {
// OpenCL C requires specific widths for types, irrespective of
// what these normally are for the target.
Expand Down
1 change: 0 additions & 1 deletion clang/lib/Basic/Targets/DirectX.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ class LLVM_LIBRARY_VISIBILITY DirectXTargetInfo : public TargetInfo {
: TargetInfo(Triple) {
TLSSupported = false;
VLASupported = false;
LongWidth = LongAlign = 64;
AddrSpaceMap = &DirectXAddrSpaceMap;
UseAddrSpaceMapMangling = true;
HasLegalHalfType = true;
Expand Down
11 changes: 11 additions & 0 deletions clang/test/SemaHLSL/Types/Arithmetic/half_size.hlsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.4-library -verify %s
// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.4-library -verify -fnative-half-type %s
// RUN: %clang_cc1 -triple spirv-linux-vulkan-library -verify %s
// RUN: %clang_cc1 -triple spirv-linux-vulkan-library -verify -fnative-half-type %s

// expected-no-diagnostics
#ifdef __HLSL_ENABLE_16_BIT
_Static_assert(sizeof(half) == 2, "half is 2 bytes");
#else
_Static_assert(sizeof(half) == 4, "half is 4 bytes");
#endif
Loading