39 Commits

Author SHA1 Message Date
Joshua Batista
905de9b0fe
[HLSL] Add testing for space parameter on global constants (#106782)
The space parameter in the register binding annotation may not be used
for global constants. There was previously no diagnostic emitted when
this case occurred. This PR adds a diagnostic when this case occurs, and
tests these cases.
A new file was made to specifically test the space parameter, so some
cases in `\clang\test\SemaHLSL\resource_binding_attr_error.hlsl` were
moved over to this new file.
Fixes #104521
2024-09-17 14:57:41 -07:00
Aaron Ballman
cb98fd977a Fix use of an uninitialized variable; NFC intended
This was causing debug builds with Visual Studio to fail a number of
HLSL tests.
2024-09-17 09:39:19 -04:00
Helena Kotas
5df1b79372
[HLSL] Add [[hlsl::raw_buffer]] attribute (#107954)
This PR introduces new HLSL resource type attribute
`[[hlsl::raw_buffer]]`. Presence of this attribute on a resource handle
means that the resource does not require typed element access. The
attribute will be used on resource handles that represent buffers like
`StructuredBuffer` or `ByteAddressBuffer` and in DXIL it will be
translated to target extension type `dx.RawBuffer`.

Fixes #107907
2024-09-16 19:35:24 -07:00
Helena Kotas
0a7a1ef220
[HLSL] Add [[hlsl::contained_type()]] attribute (#108456)
Introducing a new HLSL resource type attribute `[[contained_type(T)]]`
which describes the "contained type" of a buffer or resource type.
Specifically, the attribute will be used on the resource handle in
templated buffer types like so:

template <typename T> struct RWBuffer {
  __hlsl_resource_t [[hlsl::contained_type(T)]] [[hlsl::resource_class(UAV)]] h;
};

Fixes #104855
2024-09-16 13:39:13 -07:00
Joshua Batista
2d47a0baba
Add step builtins and step HLSL function to DirectX and SPIR-V backend (#106471)
This PR adds the step intrinsic and an HLSL function that uses it.
The SPIRV backend is also implemented.

Used https://github.com/llvm/llvm-project/pull/102683 as a reference.
Fixes https://github.com/llvm/llvm-project/issues/99157
2024-09-12 10:26:38 -07:00
Helena Kotas
61372fc5db
[HLSL] Warn on duplicate is_rov attribute; remove unnecessary parentheses (#107973)
We should issue a warning whenever a duplicate resource type attribute
is found. Currently we do that only for `resource_class`. This PR fixes
that by checking for duplicate `is_rov` attributes as well.

Also removes unnecessary parenthesis on `is_rov`.
2024-09-10 12:52:02 -07:00
Tim Gymnich
dce50397b5
[clang][HLSL] Add sign intrinsic part 3 (#101989)
partially fixes #70078

### Changes
- Implemented `sign` clang builtin
- Linked `sign` clang builtin with `hlsl_intrinsics.h`
- Added sema checks for `sign` to `CheckHLSLBuiltinFunctionCall` in
`SemaChecking.cpp`
- Add codegen for `sign` to `EmitHLSLBuiltinExpr` in `CGBuiltin.cpp`
- Add codegen tests to `clang/test/CodeGenHLSL/builtins/sign.hlsl`
- Add sema tests to `clang/test/SemaHLSL/BuiltIns/sign-errors.hlsl`

### Related PRs
- https://github.com/llvm/llvm-project/pull/101987
- https://github.com/llvm/llvm-project/pull/101988

### Discussion
- Should there be a `usign` intrinsic that handles the unsigned cases?
2024-09-09 23:35:45 -04:00
Sarah Spall
0f349b7a9c
[HLSL] Implement support for HLSL intrinsic - select (#107129)
Implement support for HLSL intrinsic select.
This would close issue #75377
2024-09-09 11:07:20 -07:00
Helena Kotas
8e35c86977
[HLSL] Apply resource attributes to the resource type rather than the handle member (#107160)
Converts existing resource attributes `[[hlsl::resource_class(..)]]` and
`[[is_rov]]` from declaration attributes to type attributes.

During type attribute processing all HLSL resource type attributes are
validated and collected by `SemaHLSL`
(`SemaHLSL::handleResourceTypeAttr`). At the end of the declaration they
are be combined into a single `HLSLAttributedResourceType` instance
(`SemaHLSL::ProcessResourceTypeAttributes`) that wraps the original type
and stores all of the necessary information about the resource.

`SemaHLSL` will also need to short-term-store the `TypeLoc` information
for the newly created type that will be grabbed by `TypeSpecLocFiller`
soon after it is created.

Updates all places that expected resource attributes on declarations
like resource binding diagnostic, builtin types in
HLSLExternalSemaSource, or codegen.

Also includes implementation of
`TreeTransform<Derived>::TransformHLSLAttributedResourceType` that
enables the use of attributed resource types inside templates.

Fixes #104861
Part 2/2
2024-09-05 21:50:00 -07:00
Helena Kotas
41c11ea2af
[HLSL] Remove variables that are used only in assert (#107299)
Changes the assert to test the same condition without using the
variables.

This change is done in response to a comment
[here](https://github.com/llvm/llvm-project/pull/106657#issuecomment-2327493439).
2024-09-04 21:15:48 -07:00
Helena Kotas
9efe377307
[HLSL] Implement '__builtin_hlsl_is_intangible' type trait (#104544)
Implements `__builtin_hlsl_is_intangible` type trait.

HLSL intangible types are special implementation-defined types such as
resource handles or samplers. Any class that is an array of intangible
type or contains base class or members of intangible types is also an
intangible type.

Fixes #[102954](https://github.com/llvm/llvm-project/issues/102954)
2024-09-04 16:03:13 -07:00
Kazu Hirata
b2dabd2b06 [Sema] Fix warnings
This patch fixes:

  clang/lib/Sema/SemaHLSL.cpp:838:12: error: unused variable
  'TheVarDecl' [-Werror,-Wunused-variable]

  clang/lib/Sema/SemaHLSL.cpp:840:19: error: unused variable
  'CBufferOrTBuffer' [-Werror,-Wunused-variable]
2024-09-03 14:31:11 -07:00
Helena Kotas
334d1238aa
[HLSL] Adjust resource binding diagnostic flags code (#106657)
Adjust register binding diagnostic flags code in a couple of ways:
- Store the resource class in the Flags struct to avoid duplicated
scanning for HLSLResourceClassAttribute
- Avoid unnecessary indirection when converting resource class to
register type
- Remove recursion and reduce duplicated code

Also fixes a case where struct with an array was incorrectly diagnosed
unfit for `c` register binding.

This will also simplify work that is needed to be done in this area for
llvm/llvm-project#104861.
2024-09-03 14:11:06 -07:00
Kazu Hirata
59a3b41568
[ADT] Deprecate DenseMap::getOrInsertDefault (#107040)
This patch deprecates DenseMap::getOrInsertDefault in favor of
DenseMap::operator[], which does the same thing, has been around
longer, and is also a household name as part of std::map and
std::unordered_map.

Note that DenseMap provides several equivalent ways to insert or
default-construct a key-value pair:

- operator[Key]
- try_emplace(Key).first->second
- getOrInsertDefault(Key)
- FindAndConstruct(Key).second
2024-09-03 08:19:45 -07:00
Chris B
89fb8490a9
[HLSL] Implement output parameter (#101083)
HLSL output parameters are denoted with the `inout` and `out` keywords
in the function declaration. When an argument to an output parameter is
constructed a temporary value is constructed for the argument.

For `inout` pamameters the argument is initialized via copy-initialization
from the argument lvalue expression to the parameter type. For `out`
parameters the argument is not initialized before the call.

In both cases on return of the function the temporary value is written
back to the argument lvalue expression through an implicit assignment
binary operator with casting as required.

This change introduces a new HLSLOutArgExpr ast node which represents
the output argument behavior. The OutArgExpr has three defined children:
- An OpaqueValueExpr of the argument lvalue expression.
- An OpaqueValueExpr of the copy-initialized parameter.
- A BinaryOpExpr assigning the first with the value of the second.

Fixes #87526

---------

Co-authored-by: Damyan Pepper <damyanp@microsoft.com>
Co-authored-by: John McCall <rjmccall@gmail.com>
2024-08-31 10:59:08 -05:00
Xiang Li
e41579a31f
[HLSL] AST support for WaveSize attribute. (#101240)
First step for support WaveSize attribute in
 https://microsoft.github.io/DirectX-Specs/d3d/HLSL_SM_6_6_WaveSize.html
and
https://microsoft.github.io/hlsl-specs/proposals/0013-wave-size-range.html

A new attribute HLSLWaveSizeAttr was supported in the AST.

Implement both the wave size and the wave size range, rather than
separately which might require more work.

For #70118
2024-08-31 11:23:34 -04:00
Helena Kotas
e00e9a3f82
[HLSL] Add HLSLAttributedResourceType (#106181)
Introducing `HLSLAttributedResourceType` - a new type that is similar to
`AttributedType` but with additional data specific to HLSL resources.
`AttributeType` currently only stores an attribute kind and no
additional data from the type attribute parameters. This does not really
work for HLSL resources since its type attributes contain non-boolean
values that need to be retained as well.

For example:

```
template <typename T> class RWBuffer {
  __hlsl_resource_t  [[hlsl::resource_class(uav)]] [[hlsl::is_rov]] handle;
};
```

The data `HLSLAttributedResourceType` needs to eventually store are:
- resource class (SRV, UAV, CBuffer, Sampler)
- texture dimension(1-3)
- flags is_rov, is_array, is_feedback and is_multisample
- contained type

All of these values except contained type will be stored in
`HLSLAttributedResourceType::Attributes` struct and accessed
individually via the fields. There is also `Data` alias that covers all
of these values as a `unsigned` which is used for hashing and the AST
type serialization.

During type attribute processing all HLSL type attributes will be
validated and collected by SemaHLSL (by
`SemaHLSL::handleResourceTypeAttr`) and in the end combined into a
single `HLSLAttributedResourceType` instance (in
`SemaHLSL::ProcessResourceTypeAttributes`). `SemaHLSL` will also need to
short-term store the `TypeLoc` information for the new type that will be
grabbed by `TypeSpecLocFiller` soon after the type is created.

Part 1/2 of #104861
2024-08-29 21:42:20 -07:00
Chris B
4bab0387e9
[HLSL] Add __builtin_hlsl_is_scalarized_layout_compatible (#102227)
HLSL tends to rely pretty aggressively on scalarization occuring in the
complier, which allows for some relaxed language behaviors when types
are fully sclarized to equivalent scalar representations.

This change adds a new queryable trait builtin for scalarized layout
compatability.

Resolves #100614

---------

Co-authored-by: Aaron Ballman <aaron@aaronballman.com>
2024-08-26 12:31:45 -05:00
Haojian Wu
d86349cf40 Fix some warnings in SemaHLSL.cpp. 2024-08-23 20:02:37 +02:00
Joshua Batista
ebc4a66e9b
Implement resource binding type prefix mismatch diagnostic infrastructure (#97103)
There are currently no diagnostics being emitted for when a resource is
bound to a register with an incorrect binding type prefix. For example,
a CBuffer type resource should be bound with a a binding type prefix of
'b', but if instead the prefix is 'u', no errors will be emitted. This
PR implements such diagnostics. The focus of this PR is to implement
both the flag setting and diagnostic emisison steps specified in the
relevant spec: https://github.com/microsoft/hlsl-specs/pull/230
The relevant issue is: https://github.com/llvm/llvm-project/issues/57886
This is a continuation / refresh of this PR:
https://github.com/llvm/llvm-project/pull/87578
2024-08-23 10:47:05 -07:00
S. Bharadwaj Yadavalli
6a38e19c92
[HLSL] Implement support for HLSL intrinsic - saturate (#104619)
Implement support for HLSL intrinsic saturate.
Implement DXIL codegen for the intrinsic saturate by lowering it to DXIL
Op dx.saturate.
Implement SPIRV codegen by transforming saturate(x) to clamp(x, 0.0f,
1.0f).

Add tests for DXIL and SPIRV CodeGen.
2024-08-20 12:46:33 -04:00
Joshua Batista
1b2d11de93
Add normalize builtins and normalize HLSL function to DirectX and SPIR-V backend (#102683)
This PR adds the normalize intrinsic and an HLSL function that uses it.
The SPIRV backend is also implemented.

Used https://github.com/llvm/llvm-project/pull/101256 as a reference,
along with https://github.com/llvm/llvm-project/pull/102243
Fixes https://github.com/llvm/llvm-project/issues/99139
2024-08-13 15:15:19 -07:00
Joshua Batista
ed5b0e1e69
Add length builtins and length HLSL function to DirectX Backend (#101256)
This PR adds the length intrinsic and an HLSL function that uses it.
The SPIRV implementation is left for a future PR.
This PR addresses #99134, though some SPIR-V changes still need to be
made to complete the task. Below is how this PR addresses #99134.
- "Implement `length` clang builtin" was done by defining `HLSLL ength`
in Builtins.td
- "Link `length` clang builtin with hlsl_intrinsics.h" was done by using
the alias attribute to make `length` an alias of
`__builtin_hlsl_elementwise_length` in hlsl_intrinsics.h
- "Add sema checks for `length` to `CheckHLSLBuiltinFunctionCall` in
`SemaChecking.cpp` " was done, but in this case not in SemaChecking.cpp,
rather SemaHLSL.cpp. A case was added to the builtin to check for
semantic failures, and set `TheCall` up to have the right return type.
- "Add codegen for `length` to `EmitHLSLBuiltinExpr` in `CGBuiltin.cpp`"
was done. For scalars, fabs is emitted, otherwise, length is emitted.
- "Add codegen tests to `clang/test/CodeGenHLSL/builtins/length.hlsl`
was done to test that `length` in HLSL emits the right intrinsic.
- "Add sema tests to `clang/test/SemaHLSL/BuiltIns/length-errors.hlsl`"
was done to test for diagnostics emitted in SemaHLSL.cpp
- "Create the `int_dx_length` intrinsic in `IntrinsicsDirectX.td`" was
done. Specifying return types and parameter types was difficult, but
`idot` was used for reference, and `llvm\include\llvm\IR\Intrinsics.td`
contains all the ways to express return / parameter types.
- "Create an intrinsic expansion of `int_dx_length` in
`llvm/lib/Target/DirectX/DXILIntrinsicExpansion.cpp`" was done, and was
mostly derived by looking at `TranslateLength` in `HLOperationLower.cpp`
in the DXC codebase.
- "Create the `length.ll` and `length_errors.ll` tests in
`llvm/test/CodeGen/DirectX/`" was done by taking the DXIL output of
`clang/test/CodeGenHLSL/builtins/length.hlsl` and running `opt -S
-dxil-intrinsic-expansion` and ` opt -S -dxil-op-lower` on it, checking
for how the length intrinsic was either expanded or lowered.
- "Create the `int_spv_length` intrinsic in `IntrinsicsSPIRV.td`" was
done by copying `IntrinsicsDirectX.td`.

---------

Co-authored-by: Justin Bogner <mail@justinbogner.com>
2024-08-02 21:16:24 -07:00
Farzon Lotfi
96e6255e8b
[HLSL] cleanup builtin names elementwise usage (#101543)
Remove elementwise description for builtins that don't perform
elementwise operations.
2024-08-02 00:10:28 -04:00
Helena Kotas
cc7aef97bb
[HLSL][NFC] Initialize DiagnoseHLSLAvailability members (#100778)
Improves readability and removes static verifier trigger.
2024-07-30 10:45:15 -07:00
Joshua Batista
5dc371e289
[HLSL] Split out resource class data from resource attribute (#98419)
The ability to spell out and specify the resource class is necessary for
testing various resource binding behaviors. Though it is not intended
for users to use this in customized HLSL source code, the ability to
specify the resource class via an attribute is immensely helpful for
writing thorough tests.
This PR introduces a new attribute, hlsl::resource_attribute, that can
only be applied on structs. This attribute only has 1 required argument,
and must be one of:
```
SRV
UAV
CBuffer
Sampler
```
By applying this attribute to a struct, the struct will have the
`HLSLResourceClassAttr` attribute attached to it in the AST
representation, which provides information on the type of resource class
the struct is meant to be.

The resource class data that was originally contained within the
`HLSLResourceAttr` attribute has been removed in favor of this new
attribute, and so certain ast-dump tests need to be modified so that the
same information can be represented via 2 attributes instead of one.

Fixes #98193

---------

Co-authored-by: Damyan Pepper <damyanp@microsoft.com>
2024-07-12 13:10:34 -07:00
Helena Kotas
5196a91b08
[HLSL] Run availability diagnostic on exported functions (#97352)
Implements availability diagnostic on `export` functions. 

For shader libraries the HLSL availability diagnostic should run on all
entry points and export functions. Now that the `export` keyword is
implemented (llvm/llvm-project#96823), we can detect which functions are
exported and run the diagnostic on them.

Exported functions can be nested in namespaces and in export
declarations so we need to scan not just the current translation unit
but also namespace and export declarations contexts.

Fixes #92073
2024-07-02 15:21:11 -07:00
Joshua Batista
c50ef30cce
[ParserHLSL] Attempt to parse HLSL annotations on Field Decls. (#96346)
`MaybeParseHLSLAnnotations` should be run on Field Decls instead of just
assuming that any colon after a field decl is a bitfield. In the case
that HLSL is the language, the code after the colon may be an
annotation. This PR gives the parser a chance to parse the subsequent
text as if it was an HLSL annotation.

The burden of parsing is now on the HLSL parser, but the actual work
needs to be done in handling every case of an hlsl annotation on a field
decl. SV_DispatchThreadID was straightforward enough to implement in
this PR, and tests have been added that the annotation appears as an
attribute in the AST.

Previously, the `hlsl_annotations_on_struct_members.hlsl` test would
result in an error shown below on the line that declares variable `a` in
struct Eg9:
error: use of undeclared identifier
     'SV_DispatchThreadID'
This is because the annotation is parsed as if it was a c++ bit field,
and an identifier
that represents an integer is expected, but not found.

This test ensures that hlsl annotations are parsed when parsing struct
decls.
This test not only ensures we make progress by moving the validation
error from the realm of
C++ and expecting bitfields, to HLSL and a specialized error for the
recognized annotation, but also
validates that the parser does parse the annotation and adds an
attribute to the field decl in the AST.

Fixes https://github.com/llvm/llvm-project/issues/57889
2024-06-28 01:53:54 -07:00
Vlad Serebrennikov
f1c54d7210
[clang][NFC] Move more things out of SemaChecking.cpp (#96641)
This patch moves some functions out of `SemaChecking.cpp`. ObjC-, HLSL-,
OpenCL-related functions are affected.

This patch continues the effort of splitting `Sema` into parts.
Additional context can be found in
https://github.com/llvm/llvm-project/pull/84184 and
https://github.com/llvm/llvm-project/pull/92682.
2024-06-27 18:57:34 +04:00
Helena Kotas
30efdce77e
[HLSL] Strict Availability Diagnostics (#93860)
Implements HLSL availability diagnostics' strict mode.

HLSL availability diagnostics emits errors or warning when unavailable
shader APIs are used. Unavailable shader APIs are APIs that are exposed
in HLSL code but are not available in the target shader stage or shader
model version.

In the strict mode the compiler emits an error when an unavailable API
is found in any function regardless of whether it is reachable from the
shader entry point or not. This mode is enabled by
``-fhlsl-strict-availability``.

See HLSL Availability Diagnostics design doc
[here](https://github.com/llvm/llvm-project/blob/main/clang/docs/HLSL/AvailabilityDiagnostics.rst)
for more details.

Fixes #90096
2024-06-18 10:34:28 -07:00
Helena Kotas
5d87ba1c1f
[HLSL] Use llvm::Triple::EnvironmentType instead of HLSLShaderAttr::ShaderType (#93847)
`HLSLShaderAttr::ShaderType` enum is a subset of
`llvm::Triple::EnvironmentType`. We can use
`llvm::Triple::EnvironmentType` directly and avoid converting one enum
to another.
2024-06-07 21:30:04 -07:00
Vlad Serebrennikov
6b755b0cf4
[clang] Split up SemaDeclAttr.cpp (#93966)
This patch moves language- and target-specific functions out of
`SemaDeclAttr.cpp`. As a consequence, `SemaAVR`, `SemaM68k`,
`SemaMSP430`, `SemaOpenCL`, `SemaSwift` were created (but they are not
the only languages and targets affected).

Notable things are that `Sema.h` actually grew a bit, because of
templated helpers that rely on `Sema` that I had to make available from
outside of `SemaDeclAttr.cpp`. I also had to left CUDA-related in
`SemaDeclAttr.cpp`, because it looks like HIP is building up on top of
CUDA attributes.

This is a follow-up to #93179 and continuation of efforts to split
`Sema` up. Additional context can be found in #84184 and #92682.
2024-06-05 09:46:37 +04:00
Helena Kotas
8890209ead
[HLSL] Default and Relaxed Availability Diagnostics (#92704)
Implements HLSL availability diagnostics' default and relaxed mode.

HLSL availability diagnostics emits errors or warning when unavailable
shader APIs are used. Unavailable shader APIs are APIs that are exposed
in HLSL code but are not available in the target shader stage or shader
model version.

In the default mode the compiler emits an error when an unavailable API
is found in a code that is reachable from the shader entry point
function. In the future this check will also extended to exported
library functions (#92073). The relaxed diagnostic mode is the same
except the compiler emits a warning. This mode is enabled by
``-Wno-error=hlsl-availability``.

See HLSL Availability Diagnostics design doc
[here](https://github.com/llvm/llvm-project/blob/main/clang/docs/HLSL/AvailabilityDiagnostics.rst)
for more details.

Fixes #90095
2024-05-29 21:52:20 -07:00
Xiang Li
41f0574c46
[HLSL] reenable add packoffset in AST (#91474)
This reapplies
c5509fedc5
"[HLSL] Support packoffset attribute in AST
(https://github.com/llvm/llvm-project/pull/89836)" with a fix for the
test failure caused by missing -fnative-half-type.

Since we have to parse the attribute manually in ParseHLSLAnnotations,
we could create the ParsedAttribute with an integer offset parameter
instead of string. This approach avoids parsing the string if the offset
is saved as a string in HLSLPackOffsetAttr.

For #57914
2024-05-09 09:47:07 -07:00
Xiang Li
9c09b0840e
Revert "[HLSL] Support packoffset attribute in AST (#89836)" (#91473)
This reverts commit c5509fedc5757fffece385d9d068e36b26793ade.
2024-05-08 09:48:04 -04:00
Xiang Li
c5509fedc5
[HLSL] Support packoffset attribute in AST (#89836)
Add HLSLPackOffsetAttr to save packoffset in AST.

Since we have to parse the attribute manually in ParseHLSLAnnotations,
we could create the ParsedAttribute with a integer offset parameter
instead of string. This approach avoids parsing the string if the offset
is saved as a string in HLSLPackOffsetAttr.

For #57914.
2024-05-08 05:26:34 -07:00
Vlad Serebrennikov
b45c9c313c
[clang][NFC] Move more functions to SemaHLSL (#88354)
A follow-up to #87912. I'm moving more HLSL-related functions from
`Sema` to `SemaHLSL`. I'm also dropping `HLSL` from their names in the
process.
2024-04-12 07:44:58 +04:00
Vlad Serebrennikov
d345f6a253
[clang] Introduce SemaHLSL (#87912)
This patch introduces `SemaHLSL` class, and moves some HLSL-related
functions there. No functional changes intended.

Removing "HLSL" from function names inside `SemaHLSL` is left for a
subsequent PR by HLSL contributors, if they deem that desirable.

This is a part of the effort to split `Sema` into smaller manageable
parts, and follows the example of OpenACC. See #82217, #84184, #87634
for additional context.
2024-04-08 21:32:38 +04:00
Xiang Li
782ac2182c [HLSL] Support cbuffer/tbuffer for hlsl.
This is first part for support cbuffer/tbuffer.

The format for cbuffer/tbuffer is
BufferType [Name] [: register(b#)] { VariableDeclaration [: packoffset(c#.xyzw)]; ... };

More details at https://docs.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-constants

New keyword 'cbuffer' and 'tbuffer' are added.
New AST node HLSLBufferDecl is added.
Build AST for simple cbuffer/tbuffer without attribute support.

The special thing is variables declared inside cbuffer is exposed into global scope.
So isTransparentContext should return true for HLSLBuffer.

Reviewed By: aaron.ballman

Differential Revision: https://reviews.llvm.org/D129883
2022-09-21 10:07:43 -07:00