Skip to content

Commit 4200419

Browse files
authored
[HLSL] Add NativeInt16Type langopt to control whether short type is supported. Enabled by default for all but HLSL. (llvm#165584)
Add a new langopt NativeInt16Type to control support for 16 bit integers. Enable by default for all languages but HLSL. HLSL defines uint16_t and int16_t as a typedef of short. If -enable-16bit-types is not used, the typedefs don't exist so int16_t and uint16_t can't be used. However, short was still allowed. This change will produce an error 'unknown type name short' if -enable-16bit-types isn't used. Update failing tests. Add new test. Closes llvm#81779
1 parent 7cd3be4 commit 4200419

File tree

159 files changed

+299
-241
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

159 files changed

+299
-241
lines changed

clang/include/clang/Basic/LangOptions.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ LANGOPT(OpenCLGenericAddressSpace, 1, 0, NotCompatible, "OpenCL generic keyword"
216216
LANGOPT(OpenCLPipes , 1, 0, NotCompatible, "OpenCL pipes language constructs and built-ins")
217217
LANGOPT(NativeHalfType , 1, 0, NotCompatible, "Native half type support")
218218
LANGOPT(NativeHalfArgsAndReturns, 1, 0, NotCompatible, "Native half args and returns")
219+
LANGOPT(NativeInt16Type , 1, 1, NotCompatible, "Native int 16 type support")
219220
LANGOPT(CUDA , 1, 0, NotCompatible, "CUDA")
220221
LANGOPT(HIP , 1, 0, NotCompatible, "HIP")
221222
LANGOPT(OpenMP , 32, 0, NotCompatible, "OpenMP support and version of OpenMP (31, 40 or 45)")

clang/include/clang/Driver/Options.td

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8626,6 +8626,11 @@ def fobjc_subscripting_legacy_runtime : Flag<["-"], "fobjc-subscripting-legacy-r
86268626
def vtordisp_mode_EQ : Joined<["-"], "vtordisp-mode=">,
86278627
HelpText<"Control vtordisp placement on win32 targets">,
86288628
MarshallingInfoInt<LangOpts<"VtorDispMode">, "1">;
8629+
def fnative_int16_type : Flag<["-"], "fnative-int16-type">,
8630+
HelpText<"Use 16 bit integer types">,
8631+
// This option is implied unless we are in HLSL lang mode
8632+
ImpliedByAnyOf<[!strconcat("!", hlsl.KeyPath)]>,
8633+
MarshallingInfoFlag<LangOpts<"NativeInt16Type">>;
86298634
def fnative_half_type: Flag<["-"], "fnative-half-type">,
86308635
HelpText<"Use the native half type for __fp16 instead of promoting to float">,
86318636
MarshallingInfoFlag<LangOpts<"NativeHalfType">>,
@@ -9518,7 +9523,7 @@ def emit_pristine_llvm : DXCFlag<"emit-pristine-llvm">,
95189523
HelpText<"Emit pristine LLVM IR from the frontend by not running any LLVM passes at all."
95199524
"Same as -S + -emit-llvm + -disable-llvm-passes.">;
95209525
def fcgl : DXCFlag<"fcgl">, Alias<emit_pristine_llvm>;
9521-
def enable_16bit_types : DXCFlag<"enable-16bit-types">, Alias<fnative_half_type>,
9526+
def enable_16bit_types : DXCFlag<"enable-16bit-types">,
95229527
HelpText<"Enable 16-bit types and disable min precision types."
95239528
"Available in HLSL 2018 and shader model 6.2.">;
95249529
def fdx_rootsignature_version :

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3708,6 +3708,7 @@ static void RenderHLSLOptions(const ArgList &Args, ArgStringList &CmdArgs,
37083708
options::OPT_emit_obj,
37093709
options::OPT_disable_llvm_passes,
37103710
options::OPT_fnative_half_type,
3711+
options::OPT_fnative_int16_type,
37113712
options::OPT_hlsl_entrypoint,
37123713
options::OPT_fdx_rootsignature_define,
37133714
options::OPT_fdx_rootsignature_version,

clang/lib/Driver/ToolChains/HLSL.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,15 @@ HLSLToolChain::TranslateArgs(const DerivedArgList &Args, StringRef BoundArch,
498498
continue;
499499
}
500500

501+
if (A->getOption().getID() == options::OPT_enable_16bit_types) {
502+
// Translate -enable-16bit-types into -fnative-half-type and
503+
// -fnative-int16-type
504+
DAL->AddFlagArg(nullptr, Opts.getOption(options::OPT_fnative_half_type));
505+
DAL->AddFlagArg(nullptr, Opts.getOption(options::OPT_fnative_int16_type));
506+
A->claim();
507+
continue;
508+
}
509+
501510
DAL->append(A);
502511
}
503512

clang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4600,7 +4600,8 @@ bool CompilerInvocation::ParseLangArgs(LangOptions &Opts, ArgList &Args,
46004600
// Validate that if fnative-half-type is given, that
46014601
// the language standard is at least hlsl2018, and that
46024602
// the target shader model is at least 6.2.
4603-
if (Args.getLastArg(OPT_fnative_half_type)) {
4603+
if (Args.getLastArg(OPT_fnative_half_type) ||
4604+
Args.getLastArg(OPT_fnative_int16_type)) {
46044605
const LangStandard &Std =
46054606
LangStandard::getLangStandardForKind(Opts.LangStd);
46064607
if (!(Opts.LangStd >= LangStandard::lang_hlsl2018 &&
@@ -4614,12 +4615,16 @@ bool CompilerInvocation::ParseLangArgs(LangOptions &Opts, ArgList &Args,
46144615
Diags.Report(diag::err_drv_hlsl_bad_shader_unsupported)
46154616
<< VulkanEnv << T.getOSName() << T.str();
46164617
}
4617-
if (Args.getLastArg(OPT_fnative_half_type)) {
4618+
if (Args.getLastArg(OPT_fnative_half_type) ||
4619+
Args.getLastArg(OPT_fnative_int16_type)) {
4620+
const char *Str = Args.getLastArg(OPT_fnative_half_type)
4621+
? "-fnative-half-type"
4622+
: "-fnative-int16-type";
46184623
const LangStandard &Std =
46194624
LangStandard::getLangStandardForKind(Opts.LangStd);
46204625
if (!(Opts.LangStd >= LangStandard::lang_hlsl2018))
46214626
Diags.Report(diag::err_drv_hlsl_16bit_types_unsupported)
4622-
<< "-fnative-half-type" << false << Std.getName();
4627+
<< Str << false << Std.getName();
46234628
}
46244629
} else {
46254630
llvm_unreachable("expected DXIL or SPIR-V target");

clang/lib/Frontend/InitPreprocessor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ static void InitializeStandardPredefinedMacros(const TargetInfo &TI,
399399
Builder.defineMacro("__HLSL_202y",
400400
Twine((unsigned)LangOptions::HLSLLangStd::HLSL_202y));
401401

402-
if (LangOpts.NativeHalfType)
402+
if (LangOpts.NativeHalfType && LangOpts.NativeInt16Type)
403403
Builder.defineMacro("__HLSL_ENABLE_16_BIT", "1");
404404

405405
// Shader target information

clang/lib/Parse/ParseDecl.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4248,6 +4248,13 @@ void Parser::ParseDeclarationSpecifiers(
42484248

42494249
// type-specifier
42504250
case tok::kw_short:
4251+
if (!getLangOpts().NativeInt16Type) {
4252+
Diag(Tok, diag::err_unknown_typename) << Tok.getName();
4253+
DS.SetTypeSpecError();
4254+
DS.SetRangeEnd(Tok.getLocation());
4255+
ConsumeToken();
4256+
goto DoneWithDeclSpec;
4257+
}
42514258
isInvalid = DS.SetTypeSpecWidth(TypeSpecifierWidth::Short, Loc, PrevSpec,
42524259
DiagID, Policy);
42534260
break;

clang/test/AST/HLSL/packoffset.hlsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %clang_cc1 -triple dxil-unknown-shadermodel6.3-library -S -finclude-default-header -fnative-half-type -ast-dump -x hlsl %s | FileCheck %s
1+
// RUN: %clang_cc1 -triple dxil-unknown-shadermodel6.3-library -S -finclude-default-header -fnative-half-type -fnative-int16-type -ast-dump -x hlsl %s | FileCheck %s
22

33

44
// CHECK: HLSLBufferDecl {{.*}} cbuffer A

clang/test/AST/HLSL/vk.spec-constant.usage.hlsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %clang_cc1 -finclude-default-header -triple spirv-unknown-vulkan-compute -x hlsl -ast-dump -o - %s | FileCheck %s
1+
// RUN: %clang_cc1 -finclude-default-header -fnative-int16-type -triple spirv-unknown-vulkan-compute -x hlsl -ast-dump -o - %s | FileCheck %s
22

33
// CHECK: VarDecl {{.*}} bool_const 'const hlsl_private bool' static cinit
44
// CHECK-NEXT: CallExpr {{.*}} 'bool'

clang/test/CodeGenHLSL/BasicFeatures/StructElementwiseCast.hlsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %clang_cc1 -finclude-default-header -fnative-half-type -triple dxil-pc-shadermodel6.3-library -x hlsl -emit-llvm -disable-llvm-passes -o - %s | FileCheck %s
1+
// RUN: %clang_cc1 -finclude-default-header -fnative-half-type -fnative-int16-type -triple dxil-pc-shadermodel6.3-library -x hlsl -emit-llvm -disable-llvm-passes -o - %s | FileCheck %s
22

33
struct S {
44
int X;

0 commit comments

Comments
 (0)