aboutsummaryrefslogtreecommitdiff
path: root/clang/lib/CodeGen/CGHLSLRuntime.cpp
AgeCommit message (Collapse)AuthorFilesLines
3 days[HLSL][SPIR-V] Add `-fspv-enable-maximal-reconvergence` flag to clang dxc. ↵Lucie Choi1-0/+4
(#163474) Implement the frontend change to support maximal reconvergence feature. The next work is to generate the corresponding SPIR-V instructions (`OpExtension` and `OpExecutionMode`) based on the llvm ir added in this CL `"enable-maximal-reconvergence"="true"`. Addresses https://github.com/llvm/llvm-project/issues/136930
9 days[SPIRV][HLSL] Add Sema and CodeGen for implicit typed buffer counters (#162291)Steven Perron1-3/+13
This commit implements the Sema and CodeGen portions of the typed buffer counter proposal described in the HLSL WG proposal 0023. This change introduces the necessary Sema and CodeGen logic to handle implicit counter variables for typed buffers. This includes: - Extending `HLSLResourceBindingAttr` to store the implicit counter binding order ID. - Introducing the `__builtin_hlsl_resource_counterhandlefromimplicitbinding` builtin. - Updating `SemaHLSL` to correctly initialize global resource declarations and resource arrays with implicit counter buffers. - Adding CodeGen support for the new builtin, which generates a `llvm.spv.resource.counterhandlefromimplicitbinding` intrinsic for the SPIR-V target and aliases the main resource handle for the DXIL target. - Adding and updating tests to verify the new functionality. Fixes #137032
2025-09-30[HLSL][NFC] Add helper struct to simplify dealing with resource binding ↵Helena Kotas1-64/+31
attributes (#161254) Add new `ResourceBindingAttrs` struct that holds resource binding attributes `HLSLResourceBindingAttr` and `HLSLVkBindingAttr` and provides helper methods to simplify dealing with resource bindings. This code is placed in the AST library to be shared between Sema and CodeGen. This change has been done in preparation of a third binding attribute coming soon to represent `[[vk::counter_binding()]]`. This new attribute and more helper member functions will be added to `ResourceBindingAttrs` and will be used in both Sema and in CodeGen to implement resource counter initialization.
2025-09-17Add parentheses to clarify operator ordering in asserts. NFC. (#159333)Martin Storsjö1-3/+3
This avoids the following kind of warning with GCC: ../tools/llvm-lipo/llvm-lipo.cpp: In function ‘void printInfo(llvm::LLVMContext&, llvm::ArrayRef<llvm::object::OwningBinary<llvm::object::Binary> >)’: ../tools/llvm-lipo/llvm-lipo.cpp:464:34: warning: suggest parentheses around ‘& ’ within ‘||’ [-Wparentheses] 464 | Binary->isArchive() && "expected MachO binary"); | ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
2025-09-16[HLSL] Use static create methods to initialize resources in arrays (#157005)Helena Kotas1-83/+80
Use static methods `__createFromBinding` and `__createFromImplicitBinding` to initialize resources in resource arrays instead of calling resource constructors with binding information, per proposal update https://github.com/llvm/wg-hlsl/pull/336. Test updates include the use of the `llvm-cxxfilt` tool which takes care of demangling of function names for a more readable test baseline.
2025-09-11Reapply "[HLSL] Rewrite semantics parsing" (#157718) (#158044)Nathan Gauër1-19/+53
This is a re-land of #152537 now that #157841 is merged.
2025-09-09Revert "[HLSL] Rewrite semantics parsing" (#157718)Nathan Gauër1-53/+19
Reverts llvm/llvm-project#152537 Broke the build in some cases. Need to investigate more for a proper solution.
2025-09-09[HLSL][DirectX] Add support for `rootsig` as a target environment (#156373)Finn Plummer1-7/+20
This pr implements support for a root signature as a target, as specified [here](https://github.com/llvm/wg-hlsl/blob/main/proposals/0029-root-signature-driver-options.md#target-root-signature-version). This is implemented in the following steps: 1. Add `rootsignature` as a shader model environment type and define `rootsig` as a `target_profile`. Only valid as versions 1.0 and 1.1 2. Updates `HLSLFrontendAction` to invoke a special handling of constructing the `ASTContext` if we are considering an `hlsl` file and with a `rootsignature` target 3. Defines the special handling to minimally instantiate the `Parser` and `Sema` to insert the `RootSignatureDecl` 4. Updates `CGHLSLRuntime` to emit the constructed root signature decl as part of `dx.rootsignatures` with a `null` entry function 5. Updates `DXILRootSignature` to handle emitting a root signature without an entry function 6. Updates `ToolChains/HLSL` to invoke `only-section=RTS0` to strip any other generated information Resolves: https://github.com/llvm/llvm-project/issues/150286. ##### Implementation Considerations Ideally we could invoke this as part of `clang-dxc` without the need of a source file. However, the initialization of the `Parser` and `Lexer` becomes quite complicated to handle this. Technically, we could avoid generating any of the extra information that is removed in step 6. However, it seems better to re-use the logic in `llvm-objcopy` without any need for additional custom logic in `DXILRootSignature`.
2025-09-09[HLSL] Fix OpaqueValueExpr handling in InitListExpr (#156750)Steven Perron1-2/+18
The OpaqueValueVisitor was not correctly traversing the AST to find all OpaqueValueExprs. This resulted in some expressions not being correctly initialized. This change fixes the visitor to correctly traverse the AST. Fixes https://github.com/llvm/llvm-project/issues/156786
2025-09-09[HLSL] Rewrite semantics parsing (#152537)Nathan Gauër1-19/+53
This is the first PR to implement the semantics proposal: https://github.com/llvm/wg-hlsl/blob/main/proposals/0031-semantics.md This PR focuses on the changes required to handle user semantics, but tried to be almost NFC. What changes is the error messages as the semantics case is not kept when reporting error messages. You might notice the SV_GroupIndex semantic is not properly validated as are others. This is an existing behavior that we'll need to fix, but wanted to keep this separated from this rewrite to stay as-close as an NFC as possible. The next PR will add support on the different kinds of I/O we can have using semantics (input, inout param, structs).
2025-09-02[HLSL][NFC] Add assert to verify implicit binding resource attribute exists ↵Helena Kotas1-0/+1
(#156094) Adds assert as requested in https://github.com/llvm/llvm-project/pull/152454#discussion_r2304509802.
2025-09-02[HLSL] Codegen for indexing of sub-arrays of multi-dimensional resource ↵Helena Kotas1-34/+112
arrays (#154248) Adds support for accessing sub-arrays from fixed-size multi-dimensional global resource arrays. Enables indexing into globally scoped, fixed-size resource arrays that have multiple dimensions when the result is a smaller resource array. For example: ``` RWBuffer<float> GlobalArray[4][2]; void main() { RWBuffer<float> SubArray[2] = GlobalArray[3]; ... } ``` The initialization logic is handled during codegen when the ArraySubscriptExpr AST node is processed. When a global resource array is indexed and the result type is a sub-array of the larger array, a local array of the resource type is created and all elements in the array are initialized with a constructor call for the corresponding resource record type and binding. Closes #145426
2025-08-28[HLSL][DirectX] Remove uniformity bit from resource initialization ↵Helena Kotas1-9/+3
intrinsics (#155332) Removes uniformity bit from resource initialization intrinsics `llvm.{dx|spv}.resource.handlefrombinding` and `llvm.{dx|spv}.resource.handlefromimplicitbinding`. The flag currently always set to `false`. It should be derived from resource analysis and not provided by codegen. Closes #135452
2025-08-27[clang] NFC: reintroduce clang/include/clang/AST/Type.h (#155050)Matheus Izvekov1-1/+1
This reintroduces `Type.h`, having earlier been renamed to `TypeBase.h`, as a redirection to `TypeBase.h`, and redirects most users to include the former instead. This is a preparatory patch for being able to provide inline definitions for `Type` methods which would otherwise cause a circular dependency with `Decl{,CXX}.h`. Doing these operations into their own NFC patch helps the git rename detection logic work, preserving the history. This patch makes clang just a little slower to build (~0.17%), just because it makes more code indirectly include `DeclCXX.h`.
2025-08-27[clang] NFC: rename clang/include/clang/AST/Type.h to TypeBase.h (#155049)Matheus Izvekov1-1/+1
This is a preparatory patch, to be able to provide inline definitions for `Type` functions which depend on `Decl{,CXX}.h`. As the latter also depends on `Type.h`, this would not be possible without some reorganizing. Splitting this rename into its own patch allows git to track this as a rename, and preserve all git history, and not force any code reformatting. A later NFC patch will reintroduce `Type.h` as redirection to `TypeBase.h`, rewriting most places back to directly including `Type.h` instead of `TypeBase.h`, leaving only a handful of places where this is necessary. Then yet a later patch will exploit this by making more stuff inline.
2025-08-18[HLSL] Global resource arrays element access (#152454)Helena Kotas1-12/+195
Adds support for accessing individual resources from fixed-size global resource arrays. Design proposal: https://github.com/llvm/wg-hlsl/blob/main/proposals/0028-resource-arrays.md Enables indexing into globally scoped, fixed-size resource arrays to retrieve individual resources. The initialization logic is primarily handled during codegen. When a global resource array is indexed, the codegen translates the `ArraySubscriptExpr` AST node into a constructor call for the corresponding resource record type and binding. To support this behavior, Sema needs to ensure that: - The constructor for the specific resource type is instantiated. - An implicit binding attribute is added to resource arrays that lack explicit bindings (#152452). Closes #145424
2025-08-09[clang] Improve nested name specifier AST representation (#147835)Matheus Izvekov1-2/+1
This is a major change on how we represent nested name qualifications in the AST. * The nested name specifier itself and how it's stored is changed. The prefixes for types are handled within the type hierarchy, which makes canonicalization for them super cheap, no memory allocation required. Also translating a type into nested name specifier form becomes a no-op. An identifier is stored as a DependentNameType. The nested name specifier gains a lightweight handle class, to be used instead of passing around pointers, which is similar to what is implemented for TemplateName. There is still one free bit available, and this handle can be used within a PointerUnion and PointerIntPair, which should keep bit-packing aficionados happy. * The ElaboratedType node is removed, all type nodes in which it could previously apply to can now store the elaborated keyword and name qualifier, tail allocating when present. * TagTypes can now point to the exact declaration found when producing these, as opposed to the previous situation of there only existing one TagType per entity. This increases the amount of type sugar retained, and can have several applications, for example in tracking module ownership, and other tools which care about source file origins, such as IWYU. These TagTypes are lazily allocated, in order to limit the increase in AST size. This patch offers a great performance benefit. It greatly improves compilation time for [stdexec](https://github.com/NVIDIA/stdexec). For one datapoint, for `test_on2.cpp` in that project, which is the slowest compiling test, this patch improves `-c` compilation time by about 7.2%, with the `-fsyntax-only` improvement being at ~12%. This has great results on compile-time-tracker as well: ![image](https://github.com/user-attachments/assets/700dce98-2cab-4aa8-97d1-b038c0bee831) This patch also further enables other optimziations in the future, and will reduce the performance impact of template specialization resugaring when that lands. It has some other miscelaneous drive-by fixes. About the review: Yes the patch is huge, sorry about that. Part of the reason is that I started by the nested name specifier part, before the ElaboratedType part, but that had a huge performance downside, as ElaboratedType is a big performance hog. I didn't have the steam to go back and change the patch after the fact. There is also a lot of internal API changes, and it made sense to remove ElaboratedType in one go, versus removing it from one type at a time, as that would present much more churn to the users. Also, the nested name specifier having a different API avoids missing changes related to how prefixes work now, which could make existing code compile but not work. How to review: The important changes are all in `clang/include/clang/AST` and `clang/lib/AST`, with also important changes in `clang/lib/Sema/TreeTransform.h`. The rest and bulk of the changes are mostly consequences of the changes in API. PS: TagType::getDecl is renamed to `getOriginalDecl` in this patch, just for easier to rebasing. I plan to rename it back after this lands. Fixes #136624 Fixes https://github.com/llvm/llvm-project/issues/43179 Fixes https://github.com/llvm/llvm-project/issues/68670 Fixes https://github.com/llvm/llvm-project/issues/92757
2025-08-07[HLSL] Add `isHLSLResourceRecordArray` method to `clang::Type` (#152450)Helena Kotas1-8/+1
Adds the `isHLSLResourceRecordArray()` method to the `Type` class. This method returns `true` if the `Type` represents an array of HLSL resource records. Defining this method on `Type` makes it accessible from both sema and codegen.
2025-08-05[HLSL][SPIRV] Add vk::binding attribute (#150957)Steven Perron1-4/+33
The vk::binding attribute allows users to explicitly set the set and binding for a resource in SPIR-V without chaning the "register" attribute, which will be used when targeting DXIL. Fixes https://github.com/llvm/llvm-project/issues/136894
2025-07-09[SPIRV] Add more id and range builtIns (#143909)Victor Lomuller1-3/+13
The patch adds intrinsics and lowering logic for GlobalSize, GlobalOffset, SubgroupMaxSize, NumWorkgroups, WorkgroupSize, WorkgroupId, LocalInvocationId, GlobalInvocationId, SubgroupSize, NumSubgroups, SubgroupId and SubgroupLocalInvocationId SPIR-V builtins. The patch also extend spv_thread_id, spv_group_id and spv_thread_id_in_group to return anyint rather than i32. This allows the intrinsics to support the opencl environment. For each of the intrinsics, new clang builtins were added as well as a binding for the SPIR-V "friendly" format. The original format doesn't define such binding (uses global variables) but it is not possible to express the Input SC which is normally required by the environement specs, and using builtin functions is the most usual approach for other backend and programming models.
2025-07-04[NFC][HLSL][RootSignature] Split up `HLSLRootSignatureUtils` (#146124)Finn Plummer1-1/+1
This pr breaks-up `HLSLRootSignatureUtils` into separate orthogonal and meaningful libraries. This prevents it ending up as a dumping grounds of many different parts. - Creates a library `RootSignatureMetadata` to contain helper functions for interacting the root signatures in their metadata representation - Create a library `RootSignatureValidations` to contain helper functions that will validate various values of root signatures - Move the serialization of root signature elements to `HLSLRootSignature` Resolves: https://github.com/llvm/llvm-project/issues/145946
2025-06-26Clarify some code based on static analysis complaints; NFC (#145679)Aaron Ballman1-2/+2
In one case, we have a null pointer check that's unnecessary because the only caller of the function already asserts the value is non-null. In the other case, we've got an anti-pattern of `is` followed by `get`. The logic was easier to repair by changing `get` to `cast`. Neither case is a functional change. Fixes #145525
2025-06-24[HLSL][RootSignature] Add `fdx-rootsignature-version` option to specify root ↵Finn Plummer1-9/+10
signature version (#144813) This pr provides the ability to specify the root signature version as a compiler option and to retain this in the root signature decl. It also updates the methods to serialize the version when dumping the declaration and to output the version when generating the metadata. - Update `DXContainer.hI` to define the root signature versions - Update `Options.td` and `LangOpts.h` to define the `fdx-rootsignature-version` compiler option - Update `Options.td` to provide an alias `force-rootsig-ver` in clang-dxc - Update `Decl.[h|cpp]` and `SeamHLSL.cpp` so that `RootSignatureDecl` will retain its version type - Updates `CGHLSLRuntime.cpp` to generate the extra metadata field - Add tests to illustrate Resolves https://github.com/llvm/llvm-project/issues/126557. Note: this does not implement validation based on versioning. https://github.com/llvm/llvm-project/issues/129940 is required to retrieve the version and use it for validations.
2025-06-20[HLSL] Emit a version in the dx.rootsignatures metadata (#145113)Justin Bogner1-3/+8
In #144957 the backend was updated to expect a version in the metadata, but since the frontend wasn't updated this breaks compilation. This is a somewhat temporary fix to that until #144813 lands.
2025-06-18[HLSL][SPIR-V] Fix LinkageAttribute emission for BuiltIn (#144701)Nathan Gauër1-0/+1
BuiltIn variables were missing the visibility attribute, which caused the Linkage capability to be emitted by the backend.
2025-06-16[HLSL] Use hidden visibility for external linkage. (#140292)Steven Perron1-8/+0
Implements https://github.com/llvm/wg-hlsl/blob/main/proposals/0026-symbol-visibility.md. The change is to stop using the `hlsl.export` attribute. Instead, symbols with "program linkage" in HLSL will have export linkage with default visibility, and symbols with "external linkage" in HLSL will have export linkage with hidden visibility.
2025-06-15[clang] Remove unused includes (NFC) (#144285)Kazu Hirata1-1/+0
These are identified by misc-include-cleaner. I've filtered out those that break builds. Also, I'm staying away from llvm-config.h, config.h, and Compiler.h, which likely cause platform- or compiler-specific build failures.
2025-06-13[HLSL][SPIRV] Use resource names (#143412)Steven Perron1-41/+8
The SPIR-V backend does not have access to the original name of a resource in the source, so it tries to create a name. This leads to some problems with reflection. That is why start to pass the name of the resource from Clang to the SPIR-V backend. Fixes #138533
2025-06-11[HLSL][SPIR-V] Handle SV_Position builtin in PS (#141759)Nathan Gauër1-10/+32
This commit is using the same mechanism as vk::ext_builtin_input to implement the SV_Position semantic input. The HLSL signature is not yet ready for DXIL, hence this commit only implements the SPIR-V side. This is incomplete as it doesn't allow the semantic on hull/domain and other shaders, but it's a first step to validate the overall input/output semantic logic. Fixes https://github.com/llvm/llvm-project/issues/136969
2025-06-04[HLSL][SPIR-V] Implement vk::ext_builtin_input attribute (#138530)Nathan Gauër1-0/+14
This variable attribute is used in HLSL to add Vulkan specific builtins in a shader. The attribute is documented here: https://github.com/microsoft/hlsl-specs/blob/17727e88fd1cb09013cb3a144110826af05f4dd5/proposals/0011-inline-spirv.md Those variable, even if marked as `static` are externally initialized by the pipeline/driver/GPU. This is handled by moving them to a specific address space `hlsl_input`, also added by this commit. The design for input variables in Clang can be found here: https://github.com/llvm/wg-hlsl/blob/355771361ef69259fef39a65caef8bff9cb4046d/proposals/0019-spirv-input-builtin.md Co-authored-by: Justin Bogner <mail@justinbogner.com>
2025-06-03[NFC][RootSignature] Move RootSignature util functions (#142491)Finn Plummer1-0/+1
`HLSLRootSignature.h` was originally created to hold the struct definitions of an `llvm::hlsl::rootsig::RootElement` and some helper functions for it. However, there many users of the structs that don't require any of the helper methods. This requires us to link the `FrontendHLSL` library, where we otherwise wouldn't need to. For instance: - This [revert](https://github.com/llvm/llvm-project/pull/142005) was required as it requires linking to the unrequired `FrontendHLSL` library - As part of the change required here: https://github.com/llvm/llvm-project/issues/126557. We will want to add an `HLSLRootSignatureVersion` enum. Ideally this could live with the root signature struct defs, but we don't want to link the helper objects into `clang/Basic/TargetOptions.h` This change allows the struct definitions to be kept in a single header file and to then have the `FrontendHLSL` library only be linked when required.
2025-05-27[DirectX] Add resource name argument to llvm.dx.handlefrom[implicit]binding ↵Helena Kotas1-16/+56
intrinsics (#139991) Adds resource name argument to `llvm.dx.handlefrombinding` and `llvm.dx.handlefromimplicitbinding` intrinsics. SPIR-V currently does not seem to need the resource names so this change only affects DirectX binding intrinsics. Part 2/4 of https://github.com/llvm/llvm-project/issues/105059
2025-05-15[HLSL] Implicit resource binding for cbuffers (#139022)Helena Kotas1-21/+29
Constant buffers defined with the `cbuffer` keyword do not have a constructor. Instead, the call to initialize the resource handle based on its binding is generated in codegen. This change adds initialization of `cbuffer` handles that have implicit binding. Closes #139617
2025-05-15[HLSL][RootSignature] Add metadata generation for descriptor tables (#139633)Finn Plummer1-0/+21
- prereq: Modify `RootSignatureAttr` to hold a reference to the owned declaration - Define and implement `MetadataBuilder` in `HLSLRootSignature` - Integrate and invoke the builder in `CGHLSLRuntime.cpp` to generate the Root Signature for any associated entry functions - Add tests to demonstrate functionality in `RootSignature.hlsl` Resolves https://github.com/llvm/llvm-project/issues/126584 Note: this is essentially just https://github.com/llvm/llvm-project/pull/125131 rebased onto the new approach of constructing a root signature decl, instead of holding the elements in `AdditionalMembers`.
2025-05-08[HLSL][NFC] Rename isImplicit() to hasRegisterStot() on ↵Helena Kotas1-1/+1
HLSLResourceBindingAttr (#138964) Renaming because the name `isImplicit` is ambiguous. It can mean implicit attribute or implicit binding.
2025-05-04[clang] Remove unused local variables (NFC) (#138453)Kazu Hirata1-1/+0
2025-04-30[HLSL] Allow resource annotations to specify only register space (#135287)Helena Kotas1-1/+1
Specifying only `space` in a `register` annotation means the compiler should implicitly assign a register slot to the resource from the provided virtual register space. Closes #133346
2025-04-29[HLSL] Resource initialization by constructors (#135120)Helena Kotas1-47/+28
- Adds resource constructor that takes explicit binding for all resource classes. - Updates implementation of default resource constructor to initialize resource handle to `poison`. - Removes initialization of resource classes from Codegen. - Initialization of `cbuffer` still needs to happen in `CGHLSLRuntime` because it does not have a corresponding resource class type. - Adds `ImplicitCastExpr` for builtin function calls. Sema adds these automatically when a method of a template class is instantiated, but some resource classes like `ByteAddressBuffer` are not templates so they need to have this added explicitly. Design proposal: llvm/wg-hlsl#197 Closes #134154
2025-04-10[DirectX] Implement Shader Flags Analysis for ResMayNotAlias (#131070)Deric C.1-0/+3
Fixes #112270 Completed ACs: - `-res-may-alias` clang-dxc command-line option added - It inserts and sets a module metadata flag `dx.resmayalias` to 1 - Shader flag set appropriately: - The flag IS NOT set if DXIL Version <= 1.6 OR the command-line option `-res-may-alias` is specified - Otherwise the flag IS set when: - DXIL Version > 1.7 AND function uses UAVs, OR - DXIL Version <= 1.7 AND UAVs present globally - Add tests - Tests for Shader Models 6.6, 6.7, and 6.8 corresponding to DXIL Versions 1.6, 1.7, and 1.8 - Tests (`res-may-alias-0.ll`/`res-may-alias-1.ll`) for when the module metadata flag `dx.resmayalias` is set to 0 or 1 respectively - A frontend test (`res-may-alias.hlsl`) for testing that that the command-line option `-res-may-alias` inserts `dx.resmayalias` module metadata correctly
2025-04-09[DirectX] Implement UseNativeLowPrecision shader flag analysis (#134288)Deric C.1-0/+8
Fixes #112267 Implement the shader flag analysis to set the UseNativeLowPrecision DXIL module flag. The flag is only able to be set when the command-line flag `-enable-16bit-types` is passed to clang-dxc, or equivalently `-fnative-half-type` is passed to clang. When the command-line flag is passed, a module metadata flag called "dx.nativelowprec" is set to 1. The DXILShaderFlags shader flags analysis checks that the module metadata flag "dx.nativelowprec" is set to 1 and the DXIL Version is 1.2 or greater before setting the UseNativeLowPrecision DXIL module flag.
2025-03-31[IRBuilder] Add new overload for CreateIntrinsic (#131942)Rahul Joshi1-2/+2
Add a new `CreateIntrinsic` overload with no `Types`, useful for creating calls to non-overloaded intrinsics that don't need additional mangling.
2025-03-21[HLSL] Add support for SV_GroupIndex in SPIR-V (#130672)Cassandra Beckley1-3/+3
Lower the `SV_GroupIndex` semantic as the `llvm.spv.flattened.thread.id.in.group` intrinsic. Depends on #130670. --------- Co-authored-by: Steven Perron <stevenperron@google.com>
2025-03-20[HLSL] Buffer handle globals should not be constants (#130231)Justin Bogner1-7/+6
If these are constants their initializers will be removed by InstCombine. Change them to not be constants and initialize them with poison.
2025-03-14[HLSL] Remove old resource annotations (#130338)Helena Kotas1-129/+0
Fixes #114126
2025-03-12[HLSL] Implement explicit layout for default constant buffer ($Globals) ↵Helena Kotas1-7/+38
(#128991) Processes `HLSLResourceBindingAttr` attributes that represent `register(c#)` annotations on default constant buffer declarations and applies its value to the buffer layout. Any default buffer declarations without an explicit `register(c#)` annotation are placed after the elements with explicit layout. This PR also adds a test case for a `cbuffer` that does not have `packoffset` on all declarations. Same layout rules apply here as well. Fixes #126791
2025-03-05[HLSL] Fix resource wrapper declaration (#129100)Steven Perron1-7/+2
The resource wrapper should have internal linkage because it contains a handle to the global resource, and it not the actual global. Makeing this changed exposed that we were zeroinitializing the resouce, which is a problem. The handle cannot be zeroinitialized. This is changed to use poison instead. Fixes https://github.com/llvm/llvm-project/issues/122767. --------- Co-authored-by: Helena Kotas <hekotas@microsoft.com>
2025-02-25[HLSL] Implement default constant buffer $Globals (2nd attempt) (#128589)Helena Kotas1-1/+1
All variable declarations in the global scope that are not resources, static or empty are implicitly added to implicit constant buffer `$Globals`. They are created in `hlsl_constant` address space and collected in an implicit `HLSLBufferDecl` node that is added to the AST at the end of the translation unit. Codegen is the same as for explicit constant buffers. Fixes #123801 This is a second attempt to implement this feature. The first attempt had to be reverted because of memory leaks. The problem was adding a `SmallVector` member on `HLSLBufferDecl` node to represent a list of default buffer declarations. When this vector needed to grow, it allocated memory that was never released, because all memory used by AST nodes must be allocated by `ASTContext` allocator and is released all at once. Destructors on AST nodes are never called. It this change the list of default buffer declarations is collected in a `SmallVector` instance on `SemaHLSL`. The `HLSLBufDecl` representing `$Globals` is created at the end of the translation unit when the number of declarations is known, and the list is copied into an array allocated by the `ASTContext` allocator.
2025-02-20Revert "[HLSL] Implement default constant buffer `$Globals`" (#128112)Helena Kotas1-1/+1
Reverts llvm/llvm-project#125807 Reverting this change because of failing tests.
2025-02-20[HLSL] Implement default constant buffer `$Globals` (#125807)Helena Kotas1-1/+1
All variable declarations in the global scope that are not resources, static or empty are implicitly added to implicit constant buffer `$Globals`. They are created in `hlsl_constant` address space and collected in an implicit `HLSLBufferDecl` node that is added to the AST at the end of the translation unit. Codegen is the same as for explicit constant buffers. Fixes #123801
2025-02-20[HLSL] Constant Buffers CodeGen (#124886)Helena Kotas1-127/+189
Translates `cbuffer` declaration blocks to `target("dx.CBuffer")` type. Creates global variables in `hlsl_constant` address space for all `cbuffer` constant and adds metadata describing which global constant belongs to which constant buffer. For explicit constant buffer layout information an explicit layout type `target("dx.Layout")` is used. This might change in the future. The constant globals are temporary and will be removed in upcoming pass that will translate `load` instructions in the `hlsl_constant` address space to constant buffer load intrinsics calls off a CBV handle (#124630, #112992). See [Constant buffer design doc](https://github.com/llvm/wg-hlsl/pull/94) for more details. Fixes #113514, #106596