Skip to content

Commit 39172bc

Browse files
authored
[HLSL] Cleanup TargetInfo handling (#90694)
We had some odd places where we set target behaviors. We were setting the long size in target-specific code, but it should be language-based. We were not setting the Half float type semantics correctly, and instead were overriding the query in the AST context. This change it moves existing code to the right places in the Target so that as we continue working on target and language feature they are controlled in the right places. It also fixes a bug where the size of `half` was computed incorrectly when native half types are not supported.
1 parent 0c2a508 commit 39172bc

File tree

4 files changed

+22
-10
lines changed

4 files changed

+22
-10
lines changed

clang/lib/AST/ASTContext.cpp

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1612,15 +1612,7 @@ const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
16121612
case BuiltinType::Float16:
16131613
return Target->getHalfFormat();
16141614
case BuiltinType::Half:
1615-
// For HLSL, when the native half type is disabled, half will be treat as
1616-
// float.
1617-
if (getLangOpts().HLSL)
1618-
if (getLangOpts().NativeHalfType)
1619-
return Target->getHalfFormat();
1620-
else
1621-
return Target->getFloatFormat();
1622-
else
1623-
return Target->getHalfFormat();
1615+
return Target->getHalfFormat();
16241616
case BuiltinType::Float: return Target->getFloatFormat();
16251617
case BuiltinType::Double: return Target->getDoubleFormat();
16261618
case BuiltinType::Ibm128:

clang/lib/Basic/TargetInfo.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,16 @@ void TargetInfo::adjust(DiagnosticsEngine &Diags, LangOptions &Opts) {
406406
LongDoubleAlign = 64;
407407
}
408408

409+
// HLSL explicitly defines the sizes and formats of some data types, and we
410+
// need to conform to those regardless of what architecture you are targeting.
411+
if (Opts.HLSL) {
412+
LongWidth = LongAlign = 64;
413+
if (!Opts.NativeHalfType) {
414+
HalfFormat = &llvm::APFloat::IEEEsingle();
415+
HalfWidth = HalfAlign = 32;
416+
}
417+
}
418+
409419
if (Opts.OpenCL) {
410420
// OpenCL C requires specific widths for types, irrespective of
411421
// what these normally are for the target.

clang/lib/Basic/Targets/DirectX.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ class LLVM_LIBRARY_VISIBILITY DirectXTargetInfo : public TargetInfo {
5353
: TargetInfo(Triple) {
5454
TLSSupported = false;
5555
VLASupported = false;
56-
LongWidth = LongAlign = 64;
5756
AddrSpaceMap = &DirectXAddrSpaceMap;
5857
UseAddrSpaceMapMangling = true;
5958
HasLegalHalfType = true;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.4-library -verify %s
2+
// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.4-library -verify -fnative-half-type %s
3+
// RUN: %clang_cc1 -triple spirv-linux-vulkan-library -verify %s
4+
// RUN: %clang_cc1 -triple spirv-linux-vulkan-library -verify -fnative-half-type %s
5+
6+
// expected-no-diagnostics
7+
#ifdef __HLSL_ENABLE_16_BIT
8+
_Static_assert(sizeof(half) == 2, "half is 2 bytes");
9+
#else
10+
_Static_assert(sizeof(half) == 4, "half is 4 bytes");
11+
#endif

0 commit comments

Comments
 (0)