66 Commits

Author SHA1 Message Date
Helena Kotas
9a82f742b4
[HLSL][NFC] Refactor HLSLExternalSemaSource (#131032)
Moving builder classes into separate files
`HLSLBuiltinTypeDeclBuilder.cpp`/`.h`, changing a some
`HLSLExternalSemaSource` methods to private and removing unused methods.

This is a prep work before we start adding more builtin types and
methods, like textures, resource constructors or matrices. For example
constructors could make use of the `BuiltinTypeMethodBuilder`, but this
helper class was defined in `HLSLExternalSemaSource.cpp` after the
method that creates a constructor. Rather than reshuffling the code one
big source file I am moving the builders into a separate cpp & header
file and placing the helper classes declarations up top.

Currently the new header only exposes `BuiltinTypeDeclBuilder` to
`HLSLExternalSemaSource`. In the future but we might decide to expose
more helper classes as needed.
2025-03-24 14:56:05 -07:00
Helena Kotas
b3c5031b07
[HLSL] Remove HLSLResource attribute (#130342)
Fixes #104862
2025-03-17 10:00:07 -07:00
Helena Kotas
7a4b3b4927
[HLSL] Add Load(int) method on structured buffers (#120663)
Adds `T Load(int)` method on `StructuredBuffer`, `RWStructuredBuffer`
and `RasterizerOrderedStructuredBuffer`.

Uses the existing `addLoadMethods` in HLSLExternalSemalSource so most of
this change is just tests.

Fixes #112977
2024-12-20 12:52:38 -08:00
Joshua Batista
6a01ac7d06
[HLSL] Add concepts for Structured buffers (#119643)
This PR adds concept validation to structured buffers, in the same way
that it was done for typed buffers (like RWBuffer) in
https://github.com/llvm/llvm-project/pull/116413.
This PR should also be responsible for introducing rejection of 0 size
elements for structured buffers.
Fixes https://github.com/llvm/llvm-project/issues/117406
2024-12-19 15:04:39 -08:00
Mariya Podchishchaeva
fbbf1bed74
[HLSL][NFC] Fix static analyzer concerns (#120090)
Class BuiltinTypeMethodBuilder has a user-defined destructor so likely
compiler generated special functions may behave incorrectly. Delete
explicitly copy constructor and copy assignment operator to avoid
potential errors.
2024-12-17 14:37:48 +01:00
Justin Bogner
de401599b1
[HLSL] Add RWBuffer::Load(Index) (#117018)
This method is the same as `operator[]`, except that it returns a value
instead of a reference.
2024-12-16 09:31:24 -08:00
Joshua Batista
e7e42ef116
[NFC] Cleanup comments in HLSLExternalSemaSource.cpp (#119444)
Cleaning up some comments that @bogner pointed out were unpolished.
2024-12-10 13:49:26 -08:00
Helena Kotas
274637d7e5
[HLSL] Implement Append and Consume methods on Append/ConsumeStructuredBuffer (#118536)
The methods are using existing clang builtins
`__builtin_hlsl_buffer_update_counter` and
`__builtin_hlsl_resource_getpointer` to update the buffer counter and
then load or store the value.

Fixes #112968
2024-12-09 15:15:37 -08:00
joaosaffran
1df28554bd
[HLSL] Add ByteAddressBuffer, RWByteAddressBuffer and RasterizerOrderedByteAddressBuffer definitions to HLSLExternalSemaSource #113477 (#116699)
This is the first one in a series of PRs adding the requirements for
#58654

This PR adds `ByteAddressBuffer`, `RWByteAddressBuffer ` and
`RasterizerOrderedByteAddressBuffer ` definitions as well as their
handle lowering to `dx.RawBuffer`.

closes #58654

---------

Co-authored-by: Joao Saffran <jderezende@microsoft.com>
2024-12-06 12:19:39 -08:00
Justin Bogner
bd92e46204
[HLSL] Implement RWBuffer::operator[] via __builtin_hlsl_resource_getpointer (#117017)
This introduces `__builtin_hlsl_resource_getpointer`, which lowers to
`llvm.dx.resource.getpointer` and is used to implement indexing into
resources.

This will only work through the backend for typed buffers at this point,
but the changes to structured buffers should be correct as far as the
frontend is concerned.

Note: We probably want this to return a reference in the HLSL device
address space, but for now we're just using address space 0. Creating a
device address space and updating this code can be done later as
necessary.

Fixes #95956
2024-12-02 14:03:31 -08:00
Justin Bogner
9fde1a498f
[HLSL] Forward arguments in BuiltinTypeMethodBuilder::callBuiltin. NFC (#117789)
Introduce BuiltinTypeMethodBuilder::PlaceHolder values and use them in
the callBuiltin method in order to specify how we want to forward
arguments and pass the resource handle to builtins.
2024-11-26 14:40:53 -08:00
Helena Kotas
cac978331f
[HLSL] Add Increment/DecrementCounter methods to structured buffers (#117608)
Introduces `__builtin_hlsl_buffer_update_counter` clang buildin that is
used to implement the `IncrementCounter` and `DecrementCounter` methods
on `RWStructuredBuffer` and `RasterizerOrderedStructuredBuffer` (see
Note).

The builtin is translated to LLVM intrisic `llvm.dx.bufferUpdateCounter`
or `llvm.spv.bufferUpdateCounter`.

Introduces `BuiltinTypeMethodBuilder` helper in `HLSLExternalSemaSource`
that enables adding methods to builtin types using builder pattern like
this:
```
   BuiltinTypeMethodBuilder(Sema, RecordBuilder, "MethodName", ReturnType)
       .addParam("param_name", Type, InOutModifier)
       .callBuiltin("buildin_name", { BuiltinParams })
       .finalizeMethod();
```

Fixes #113513

[First version](llvm/llvm-project#114148) of this PR was reverted
because of build break.
2024-11-25 16:10:48 -08:00
Congcong Cai
cbdd14ee9d
[clang][NFC]add static for internal linkage function (#117482)
Detected by misc-use-internal-linkage
2024-11-25 06:48:33 +08:00
Helena Kotas
dc4c8de179
Revert "[HLSL] Add Increment/DecrementCounter methods to structured buffers (#114148)" (#117448)
This reverts commit 94bde8cdc39ff7e9c59ee0cd5edda882955242aa.
2024-11-23 12:02:07 -08:00
Helena Kotas
94bde8cdc3
[HLSL] Add Increment/DecrementCounter methods to structured buffers (#114148)
Introduces `__builtin_hlsl_buffer_update_counter` clang buildin that is
used to implement the `IncrementCounter` and `DecrementCounter` methods
on `RWStructuredBuffer` and `RasterizerOrderedStructuredBuffer` (see
Note).

The builtin is translated to LLVM intrisic `llvm.dx.bufferUpdateCounter`
or `llvm.spv.bufferUpdateCounter`.

Introduces `BuiltinTypeMethodBuilder` helper in `HLSLExternalSemaSource`
that enables adding methods to builtin types using builder pattern like
this:
```
   BuiltinTypeMethodBuilder(Sema, RecordBuilder, "MethodName", ReturnType)
       .addParam("param_name", Type, InOutModifier)
       .callBuiltin("buildin_name", { BuiltinParams })
       .finalizeMethod();
```

Fixes #113513
2024-11-23 09:33:38 -08:00
Joshua Batista
ee0ca4e81f
[HLSL] Add implicit resource element type concepts to AST (#116413)
This PR is step one on the journey to implement resource element type
validation via C++20 concepts. The PR sets up the infrastructure for
injecting implicit concept decls / concept specialization expressions
into the AST, which will then be evaluated after template arguments are
instantiated. This is not meant to be a complete implementation of the
desired validation for HLSL,
there are a couple of missing elements:

We need the __builtin_hlsl_is_typed_resource_element_compatible builtin
to be implemented.
We need other constraints, like is_intangible
We need to put the first 2 points together, and construct a finalized
constraint expression, which should differ between typed and raw buffers
This is just an initial PR that puts some of the core infrastructure in
place.

This PR is an edit of #112600, so that new tests that were put into main
don't fail
Fixes #75676
2024-11-22 09:47:29 -08:00
Justin Bogner
2186a008c9
[HLSL] Fix placement of const for resource operator[] (#116698)
We had an incorrect AST here - We want `const T &`, not `T &const`.
2024-11-19 10:33:54 -08:00
Justin Bogner
4bd982d528
[HLSL] Fix resource kind for RasterizerOrderedStructuredBuffer (#116700)
This is a kind of StructuredBuffer, so it should be "Raw" and not
"Typed".
2024-11-19 01:32:37 -08:00
Justin Bogner
4818dd33d8
[HLSL] Rename resource's "h" member to "__handle". NFC (#116696)
This makes it clearer that the handle is an implementation detail by
using a name that's reserved.
2024-11-19 01:26:52 -08:00
Joshua Batista
c1f6cb7463
Revert "[HLSL] Add implicit resource element type concepts to AST" (#116305)
Reverts llvm/llvm-project#112600
2024-11-14 17:10:58 -08:00
Joshua Batista
478c24b5f8
[HLSL] Add implicit resource element type concepts to AST (#112600)
This PR is step one on the journey to implement resource element type
validation via C++20 concepts. The PR sets up the infrastructure for
injecting implicit concept decls / concept specialization expressions
into the AST, which will then be evaluated after template arguments are
instantiated. This is not meant to be a complete implementation of the
desired validation for HLSL,
there are a couple of missing elements:
1. We need the __builtin_hlsl_is_typed_resource_element_compatible
builtin to be implemented.
2. We need other constraints, like is_intangible
3. We need to put the first 2 points together, and construct a finalized
constraint expression, which should differ between typed and raw buffers

This is just an initial PR that puts some of the core infrastructure in
place.
2024-11-14 17:00:58 -08:00
Helena Kotas
d413335ccf
[HLSL] Add Append/ConsumeStructuredBuffer definitions to HLSLExternalSemaSource (#113643)
Adds `AppendStructuredBuffer` and `ConsumeStructuredBuffer` definition
to HLSLExternalSemaSource. Adds separate tests for the AST shape and
element types, and adds constructor/handle.fromBinding test case to
shared test file for structured buffers.

These buffers do not have any subscript operators. Append and Consume
methods will be added later in llvm/llvm-project#112968.

Fixes #112777
2024-11-08 17:38:39 -08:00
Helena Kotas
39f2bae240
[HLSL] Add RasterizerOrderedStructuredBuffer definition to HLSLExternalSemaSource (#113648)
Adds `RasterizerOrderedStructuredBuffer` definition to
HLSLExternalSemaSource. Adds separate tests for the AST shape and
element types. Adds constructor/handle.fromBinding and subscript test
cases to shared test file for structured buffers. Additional methods
will be added later.

Fixes #112776
2024-11-06 10:28:04 -08:00
Helena Kotas
19b26d3769
[HLSL][NFC] Cleanup - removed unused function, includes and param, fix typos (#113649) 2024-11-04 22:29:02 -08:00
Helena Kotas
af872d5473
[HLSL] Add RWStructuredBuffer definition to HLSLExternalSemaSource (#113477)
Add tests for RWStructuredBuffer class definition. Use shared test files
for all structured buffers' constructor and subscript tests. Keep AST
and element-type tests separate for each buffer type because they longer
and failures would be harder to match.

Fixes #112775
2024-10-24 10:34:04 -07:00
Helena Kotas
7a1036935f
[HLSL] Change StructuredBuffer resource class to SRV (#113397)
Change `StructuredBuffer` resource class to SRV (read-only) to match DXC.

Part of llvm/llvm-project#112775
2024-10-23 10:44:48 -07:00
Helena Kotas
3b4512074e
[HLSL] Make HLSLAttributedResourceType canonical and add code paths to convert HLSL types to DirectX target types (#110327)
Translates `RWBuffer` and `StructuredBuffer` resources buffer types to
DirectX target types `dx.TypedBuffer` and `dx.RawBuffer`.

Includes a change of `HLSLAttributesResourceType` from 'sugar' type to
full canonical type. This is required for codegen and other clang
infrastructure to work property on HLSL resource types.

Fixes #95952 (part 2/2)
2024-10-15 13:38:15 -07:00
Helena Kotas
e20bf28987
[HLSL] Replace element_type* handles in HLSLExternalSemaSource with __hlsl_resource_t builtin type (#110079)
Replace `element_type*` handles in HLSLExternalSemaSource with
`__hlsl_resource_t` builtin type.

The handle used to be defined as `element_type*` which was used by the
provisional subscript operator implementation. Now that the handle is
`__hlsl_resource_t` the subscript placeholder implementation was updated
to add `element_type* e;` field to the resource struct. and return a
reference to that. This field is just a temporary workaround until the
indexing is implemented properly in llvm/llvm-project#95956, at which
point the field will be removed. This seemed like a better solution than
disabling many of the existing tests that already use the `[]` operator.
One test has to be disabled nevertheless because an error based on
interactions of const and template instantiation (potential bug that can
be investigated once indexing is implemented the right way).

Fixes #84824
2024-09-29 20:41:54 -07:00
Helena Kotas
90b7fe42d8
[HLSL] Remove __builtin_hlsl_create_handle (#109910)
The `__builtin_hlsl_create_handle` called from the constructor of
resource buffer class was supposed to initialize the resource handle
based on resource type and registry binding information. It is not
possible to do though that because the registry binding information is
not accessible from the constructor during codegen.

Instead, the handle should be initialized to an empty or null handle
with something like `__builtin_hlsl_create_null_handle`. This PR is
removing `__builtin_hlsl_create_handle` first and the
`__builtin_hlsl_create_null_handle` will be added to the constructor
once the handle type changes to `__hlsl_resource_t` and
HLSLAttributeResourceType is updated to be a canonical type, which will
allow the initialization assignment.

The actual handle initialization based on the registry binding will be
implemented part 2/2 of llvm/llvm-project#105076 once the dependent
tasks are completed.

Part 1/2 of llvm/llvm-project#105076.
2024-09-26 13:56:49 -07: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
b8239e1201
[HLSL] Add StructuredBuffer to external sema source (#106316)
This PR adds `StructuredBuffer` to `HLSLExternalSemaSource.cpp`, by
copying the logic from RWBuffer but just replacing the name with
StructuredBuffer. The change now allows StructuredBuffers to be defined
in HLSL, though they function the same as RWBuffers.

Further work to apply the appropriate attributes that distinguish
StructuredBuffers from other Buffer types will be deferred.
This improves our position on
https://github.com/llvm/llvm-project/issues/106189
2024-09-11 13:03:00 -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
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
Joshua Batista
eae1d65f34
[HLSL] Split out the ROV attribute from the resource attribute, make it a new spellable attribute. (#102414)
Much like #98193, this PR takes some more data out of the resource
attribute, specifically the ROV data.
This PR introduces a new attribute called HLSLROVAttr, which contains
data on whether or not the decl the attribute applies to is an ROV.
Tests were added to ensure the attribute is found on the AST.
This attribute may take any boolean condition as an argument. If the
condition is true, then the object the attribute applies to "is" an ROV.
Fixes #https://github.com/llvm/llvm-project/issues/102392
2024-08-14 11:07:25 -07:00
Joshua Batista
ac319a8dd8
Attach resource attributes to handle within record, instead of record (#101433)
This PR attaches the resource attributes, `HLSLResourceAttr` and
`HLSLResourceClassAttr`, to the handle contained within such resource
classes like `RWBuffer`. CodeGen will now search for fields within HLSL
resource decls, and emit previous data based on attributes on the handle
member. An AST-dump test was added to verify that the resource attribute
is attached to the handle within the resource record decl.
Fixes #98556

---------

Co-authored-by: Justin Bogner <mail@justinbogner.com>
2024-08-02 17:29:36 -07:00
Justin Bogner
d269071d85
[HLSL] Remove hlsl::Resource (#98938)
This was added in an effort to support resource types before we created
the HLSLResource builtin type, but it isn't needed.
2024-07-16 10:36:21 -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
Matheus Izvekov
2bde13cda1
[clang] NFCI: use TemplateArgumentLoc for NTTP DefaultArgument (#92852)
This is an enabler for https://github.com/llvm/llvm-project/pull/92855

This allows an NTTP default argument to be set as an arbitrary
TemplateArgument, not just an expression.
This allows template parameter packs to have default arguments in the
AST, even though the language proper doesn't support the syntax for it.

This allows NTTP default arguments to be other kinds of arguments, like
packs, integral constants, and such.
2024-05-22 12:18:44 -03:00
Matheus Izvekov
e42b799bb2
[clang] NFCI: use TemplateArgumentLoc for type-param DefaultArgument (#92854)
This is an enabler for a future patch.

This allows an type-parameter default argument to be set as an arbitrary
TemplateArgument, not just a type.
This allows template parameter packs to have default arguments in the
AST, even though the language proper doesn't support the syntax for it.

This will be used in a later patch which synthesizes template parameter
lists with arbitrary default arguments taken from template
specializations.

There are a few places we used SubsType, because we only had a type, now
we use SubstTemplateArgument.
SubstTemplateArgument was missing arguments for setting Instantiation
location and entity names.
Adding those is needed so we don't regress in diagnostics.
2024-05-21 20:27:50 -03:00
Krystian Stasiowski
8009bbec59
Reapply "[Clang][Sema] Diagnose class member access expressions naming non-existent members of the current instantiation prior to instantiation in the absence of dependent base classes (#84050)" (#90152)
Reapplies #84050, addressing a bug which cases a crash when an
expression with the type of the current instantiation is used as the
_postfix-expression_ in a class member access expression (arrow form).
2024-04-30 14:25:09 -04:00
Pranav Kant
0c6e1ca1c7 Revert "[Clang][Sema] Diagnose class member access expressions naming non-existent members of the current instantiation prior to instantiation in the absence of dependent base classes (#84050)"
This reverts commit a8fd0d029dca7d17eee72d0445223c2fe1ee7758.
2024-04-26 00:18:08 +00:00
Krystian Stasiowski
a8fd0d029d
[Clang][Sema] Diagnose class member access expressions naming non-existent members of the current instantiation prior to instantiation in the absence of dependent base classes (#84050)
Consider the following:
```cpp
template<typename T>
struct A
{
    auto f()
    {
        return this->x;
    }
};
```
Although `A` has no dependent base classes and the lookup context for
`x` is the current instantiation, we currently do not diagnose the
absence of a member `x` until `A<T>::f` is instantiated. This patch
moves the point of diagnosis for such expressions to occur at the point
of definition (i.e. prior to instantiation).
2024-04-25 14:50:53 -04:00
Justin Bogner
02e02b9a8f
[HLSL] Define RasterizerOrderedBuffer resource
Define HLSL's RasterizerOrderedBuffer resource type through the
external sema source. This doesn't fully work as is, but defining it
allows us to exercise the ROV logic in the DirectX backend from HLSL
rather than having to manually edit metadata, so it's useful for
further testing and development.

Pull Request: https://github.com/llvm/llvm-project/pull/74897
2023-12-09 12:33:32 -08:00
Justin Bogner
7a13e410fd
[DirectX] Move ROV info into HLSL metadata. NFC
Pull Request: https://github.com/llvm/llvm-project/pull/74896
2023-12-09 10:42:45 -08:00
Justin Bogner
c502a81b43 [HLSL] Add helpers to simplify HLSL resource type declarations. NFC
A few changes to HLSLExternalSemaSource and its BuiltinTypeDeclBuilder
to make defining buffer types less verbose. This will make it a lot
easier to see what the differences between the various buffer types
are once we start introducing more of them.

Pull Request: https://github.com/llvm/llvm-project/pull/73967
2023-12-07 17:31:35 -08:00
Justin Bogner
61329bdddd
[HLSL] RWBuffer<T> should not have a default parameter
RWBuffer doesn't have a default type in dxc's implementation, so it
shouldn't have one in clang either.

Reviewers: llvm-beanz, python3kgae

Reviewed By: python3kgae, llvm-beanz

Pull Request: https://github.com/llvm/llvm-project/pull/71265
2023-12-06 15:27:27 -08:00
Vlad Serebrennikov
edd690b02e
[clang][NFC] Refactor TagTypeKind (#71160)
This patch converts TagTypeKind into scoped enum. Among other benefits,
this allows us to forward-declare it where necessary.
2023-11-03 21:45:39 +04:00
Justin Bogner
1c6c01fbde
[Attributes][HLSL] Teach EnumArgument to refer to an external enum (#70835)
Rather than write a bunch of logic to shepherd between enums with the
same sets of values, add the ability for EnumArgument to refer to an
external enum in the first place.
2023-11-01 11:24:48 -07:00
Corentin Jabot
af4751738d [C++] Implement "Deducing this" (P0847R7)
This patch implements P0847R7 (partially),
CWG2561 and CWG2653.

Reviewed By: aaron.ballman, #clang-language-wg

Differential Revision: https://reviews.llvm.org/D140828
2023-10-02 14:33:02 +02:00