aboutsummaryrefslogtreecommitdiff
path: root/clang/lib/CodeGen/CGHLSLRuntime.cpp
AgeCommit message (Collapse)AuthorFilesLines
2025-02-15[HLSL] Implement HLSL intialization list support (#123141)Chris B1-0/+32
This PR implements HLSL's initialization list behvaior as specified in the draft language specifcation under [*Decl.Init.Agg*](https://microsoft.github.io/hlsl-specs/specs/hlsl.html#Decl.Init.Agg). This behavior is a bit unusual for C/C++ because intermediate braces in initializer lists are ignored and a whole array of additional conversions occur unintuitively to how initializaiton works in C. The implementaiton in this PR generates a valid C/C++ initialization list AST for the HLSL initializer so that there are no changes required to Clang's CodeGen to support this. This design will also allow us to use Clang's rewrite to convert HLSL initializers to valid C/C++ initializers that are equivalent. It does have the downside that it will generate often redundant accesses during codegen. The IR optimizer is extremely good at eliminating those so this will have no impact on the final executable performance. There is some opportunity for optimizing the initializer list generation that we could consider in subsequent commits. One notable opportunity would be to identify aggregate objects that occur in the same place in both initializers and do not require converison, those aggregates could be initialized as aggregates rather than fully scalarized. Closes #56067 --------- Co-authored-by: Finn Plummer <50529406+inbelic@users.noreply.github.com> Co-authored-by: Helena Kotas <hekotas@microsoft.com> Co-authored-by: Justin Bogner <mail@justinbogner.com>
2025-02-12[DirectX] Set Shader Flag DisableOptimizations (#126813)S. Bharadwaj Yadavalli1-6/+0
- Set the shader flag `DisableOptimizations` based on `optnone` attribute of shader entry functions. - Add DXIL Metadata Analysis pass as pre-requisite for Shader Flags pass to obtain entry function information collected therein. - Named module metadata `dx.disable_optimizations` is intended to indicate disabling optimizations (`-O0`) via commandline flag. However, its intent is fulfilled by `optnone` attribute of shader entry functions as implemented in a recent change, and thus not needed. Delete generation of named metadata and corresponding test file `disable_opt.ll`. - Add tests to verify correctness of setting shader flag. Closes #112263
2025-02-11[HLSL] Appropriately set function attribute optnone (#125937)S. Bharadwaj Yadavalli1-0/+7
When optimization is disabled, set `optnone` attribute all module entry functions. Updated test in accordance with the change. Closes #124796
2025-01-24[HLSL] Introduce address space `hlsl_constant(2)` for constant buffer ↵Helena Kotas1-16/+1
declarations (#123411) Introduces a new address space `hlsl_constant(2)` for constant buffer declarations. This address space is applied to declarations inside `cbuffer` block. Later on, it will also be applied to `ConstantBuffer<T>` syntax and the default `$Globals` constant buffer. Clang codegen translates constant buffer declarations to global variables and loads from `hlsl_constant(2)` address space. More work coming soon will include addition of metadata that will map these globals to individual constant buffers and enable their transformation to appropriate constant buffer load intrinsics later on in an LLVM pass. Fixes #123406
2025-01-22[HLSL] Fix global resource initialization (#123394)Helena Kotas1-67/+63
Create separate resource initialization function for each resource and add them to CodeGenModule's `CXXGlobalInits` list. Fixes #120636 and addresses this [comment ](https://github.com/llvm/llvm-project/pull/119755/files#r1894093603).
2025-01-04[HLSL][SPIR-V] implement SV_GroupID semantic lowering (#121521)Zhengxing li1-1/+1
The HLSL SV_GroupID semantic attribute is lowered into @llvm.spv.group.id intrinsic in LLVM IR for SPIR-V target. In the SPIR-V backend, this is now translated to a `WorkgroupId` builtin variable. Fixes #118700 which's a follow-up work to #70120
2024-12-17[HLSL] Fix call convention mismatch for ctor/dtor (#118651)Nathan Gauër1-4/+8
Before this patch, there was a calling-convention mismatch between the constructors and the actual call emitted for the entrypoint wrapper. Such mismatch causes the InstCombine pass to replace this call with an `unreachable`, breaking the whole function. Signed-off-by: Nathan Gauër <brioche@google.com>
2024-12-10[HLSL] Implement SV_GroupThreadId semantic (#117781)Zhengxing li1-0/+5
Support HLSL SV_GroupThreadId attribute. For `directx` target, translate it into `dx.thread.id.in.group` in clang codeGen and lower `dx.thread.id.in.group` to `dx.op.threadIdInGroup` in LLVM DirectX backend. For `spir-v` target, translate it into `spv.thread.id.in.group` in clang codeGen and lower `spv.thread.id.in.group` to a `LocalInvocationId` builtin variable in LLVM SPIR-V backend. Fixes: #70122
2024-11-26[HLSL] Implement SV_GroupID semantic (#115911)Zhengxing li1-0/+4
Support SV_GroupID attribute. Translate it into dx.group.id in clang codeGen. Fixes: #70120
2024-10-30[HLSL] Remove old resource annotations for UAVs and SRVs (#114139)Helena Kotas1-0/+10
UAVs and SRVs have already been converted to use LLVM target types and we can disable generating of the !hlsl.uavs and !hlsl.srvs! annotations. This will enable adding tests for structured buffers with user defined types that this old resource annotations code does not handle (it crashes). Part 1 of #114126
2024-10-28[HLSL][SPIRV] Add convergence tokens to entry point wrapper (#112757)Steven Perron1-4/+37
Inlining currently assumes that either all function use controled convergence or none of them do. This is why we need to have the entry point wrapper use controled convergence. https://github.com/llvm/llvm-project/blob/c85611e8583e6392d56075ebdfa60893b6284813/llvm/lib/Transforms/Utils/InlineFunction.cpp#L2431-L2439
2024-10-17[HLSL] Add handle initialization for simple resource declarations (#111207)Helena Kotas1-0/+90
Adds `@_init_resource_bindings()` function to module initialization that includes `handle.fromBinding` intrinsic calls for simple resource declarations. Arrays of resources or resources inside user defined types are not supported yet. While this unblocks our progress on [Compile a runnable shader from clang](https://github.com/llvm/wg-hlsl/issues/7) milestone, this is probably not the way we would like to handle resource binding initialization going forward. Ideally, it should be done via the resource class constructors in order to support dynamic resource binding or unbounded arrays if resources. Depends on PRs #110327 and #111203. Part 1 of #105076
2024-10-01[HLSL][SPIRV] Fix calling convention for call in entry function. (#110542)Steven Perron1-1/+1
Fix the calling convention used for the call in the entry point wrapper. No calling convention is currently set. It can easily use the calling convention of the function that is being called. Without this, there is a mismatch in the calling convention between the call site and the callee. This is undefined behaviour.
2024-09-17[HLSL] generate hlsl.wavesize attribute (#107176)Xiang Li1-0/+7
Generate function attribute hlsl.wavesize from [WaveSize]. For #70118
2024-09-17Reapply "[HLSL] set alwaysinline on HLSL functions (#106588)"Thurston Dang1-0/+1
This reverts commit 4a63f4d301c0e044073e1b1f8f110015ec1778a1. It was reverted because of a buildbot breakage, but the fix-forward has landed (https://github.com/llvm/llvm-project/pull/109023).
2024-09-17Revert "[HLSL] set alwaysinline on HLSL functions (#106588)"Thurston Dang1-1/+0
This reverts commit a729e706de3fc6ebee49ede3c50afb47f2e29191. Reason:bBuildbot failure (https://lab.llvm.org/buildbot/#/builders/25/builds/2541): 'Clang :: CodeGenHLSL/builtins/StructuredBuffer-subscript.hlsl' failed
2024-09-17[HLSL] set alwaysinline on HLSL functions (#106588)Greg Roth1-0/+1
HLSL inlines all its functions by default. This uses the alwaysinline attribute to make the alwaysinliner pass inline any function not explicitly marked noinline by the user or autogeneration. The alwayslinline marking takes place in `SetLLVMFunctionAttributesForDefinitions` where all other inlining interactions are determined. The outermost entry function is marked noinline because there's no reason to inline it. Any user calls to an entry function will instead call the internal mangled version of the entry function. Adds tests for function and constructor inlining and augments some existing tests to verify correct inlining of implicitly created functions as well. Incidentally restore RUN line that I believe was mistakenly removed as part of #88918 Fixes #89282
2024-09-05[HLSL] Apply resource attributes to the resource type rather than the handle ↵Helena Kotas1-4/+5
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-08-14[HLSL] Split out the ROV attribute from the resource attribute, make it a ↵Joshua Batista1-1/+1
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-13[HLSL] Mark exported functions with "hlsl.export" attribute (#102275)Helena Kotas1-0/+8
Marks exported functions with `"hlsl.export"` attribute. This information will be later used by DXILFinalizeLinkage pass (coming soon) to determine which functions should have internal linkage in the final DXIL code. Related to #llvm/llvm-project#92071
2024-08-05[HLSL] Implement intangible AST type (#97362)Helena Kotas1-0/+11
HLSL has a set of intangible types which are described in in the [draft HLSL Specification (**[Basic.types]**)](https://microsoft.github.io/hlsl-specs/specs/hlsl.pdf): There are special implementation-defined types such as handle types, which fall into a category of standard intangible types. Intangible types are types that have no defined object representation or value representation, as such the size is unknown at compile time. A class type T is an intangible class type if it contains an base classes or members of intangible class type, standard intangible type, or arrays of such types. Standard intangible types and intangible class types are collectively called intangible types([9](https://microsoft.github.io/hlsl-specs/specs/hlsl.html#Intangible)). This PR implements one standard intangible type `__hlsl_resource_t` and sets up the infrastructure that will make it easier to add more in the future, such as samplers or raytracing payload handles. The HLSL intangible types are declared in `clang/include/clang/Basic/HLSLIntangibleTypes.def` and this file is included with related macro definition in most places that require edits when a new type is added. The new types are added as keywords and not typedefs to make sure they cannot be redeclared, and they can only be declared in builtin implicit headers. The `__hlsl_resource_t` type represents a handle to a memory resource and it is going to be used in builtin HLSL buffer types like this: template <typename T> class RWBuffer { [[hlsl::contained_type(T)]] [[hlsl::is_rov(false)]] [[hlsl::resource_class(uav)]] __hlsl_resource_t Handle; }; Part 1/3 of llvm/llvm-project#90631. --------- Co-authored-by: Justin Bogner <mail@justinbogner.com>
2024-08-02Attach resource attributes to handle within record, instead of record (#101433)Joshua Batista1-10/+14
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-07-12[HLSL] Split out resource class data from resource attribute (#98419)Joshua Batista1-5/+6
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-06-24Revert "[IR][NFC] Update IRBuilder to use InsertPosition (#96497)"Stephen Tozer1-1/+1
Reverts the above commit, as it updates a common header function and did not update all callsites: https://lab.llvm.org/buildbot/#/builders/29/builds/382 This reverts commit 6481dc57612671ebe77fe9c34214fba94e1b3b27.
2024-06-24[IR][NFC] Update IRBuilder to use InsertPosition (#96497)Stephen Tozer1-1/+1
Uses the new InsertPosition class (added in #94226) to simplify some of the IRBuilder interface, and removes the need to pass a BasicBlock alongside a BasicBlock::iterator, using the fact that we can now get the parent basic block from the iterator even if it points to the sentinel. This patch removes the BasicBlock argument from each constructor or call to setInsertPoint. This has no functional effect, but later on as we look to remove the `Instruction *InsertBefore` argument from instruction-creation (discussed [here](https://discourse.llvm.org/t/psa-instruction-constructors-changing-to-iterator-only-insertion/77845)), this will simplify the process by allowing us to deprecate the InsertPosition constructor directly and catch all the cases where we use instructions rather than iterators.
2024-06-07[HLSL] Use llvm::Triple::EnvironmentType instead of ↵Helena Kotas1-1/+1
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-04-04[HLSL][DXIL][SPIRV] Implementation of an abstraction for intrinsic selection ↵Farzon Lotfi1-14/+6
of HLSL backends (#87171) Start of #83882 - `Builtins.td` - add the `hlsl` `all` elementwise builtin. - `CGBuiltin.cpp` - Show a use case for CGHLSLUtils via an `all` intrinsic codegen. - `CGHLSLRuntime.cpp` - move `thread_id` to use CGHLSLUtils. - `CGHLSLRuntime.h` - Create a macro to help pick the right intrinsic for the backend. - `hlsl_intrinsics.h` - Add the `all` api. - `SemaChecking.cpp` - Add `all` builtin type checking - `IntrinsicsDirectX.td` - Add the `all` `dx` intrinsic - `IntrinsicsSPIRV.td` - Add the `all` `spv` intrinsic Work still needed: - `SPIRVInstructionSelector.cpp` - Add an implementation of `OpAll` for `spv_all` intrinsic
2024-03-04[HLSL][SPIR-V] Add SV_DispatchThreadID semantic support (#82536)Natalie Chouinard1-2/+14
Add SPIR-V backend support for the HLSL SV_DispatchThreadID semantic attribute, which is lowered to a @llvm.dx.thread.id intrinsic in LLVM IR. In the SPIR-V backend, this is now correctly translated to a `GlobalInvocationId` builtin variable. Fixes #82534
2023-12-18[HLSL][DirectX] Move handling of resource element types into the frontendJustin Bogner1-9/+54
Rather than shepherding a type name all the way to the backend as a string and attempting to parse it, get the element type out of the AST and store that in the resource annotation metadata directly. Pull Request: https://github.com/llvm/llvm-project/pull/75674
2023-12-09[DirectX] Move ROV info into HLSL metadata. NFCJustin Bogner1-3/+6
Pull Request: https://github.com/llvm/llvm-project/pull/74896
2023-11-01[Attributes][HLSL] Teach EnumArgument to refer to an external enum (#70835)Justin Bogner1-56/+3
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-02-14Recommit: [NFC][IR] Make Module::getGlobalList() privateVasileios Porpodas1-1/+1
This reverts commit cb5f239363a3c94db5425c105fcd45e77d2a16a9.
2023-02-14Revert "[NFC][IR] Make Module::getGlobalList() private"Vasileios Porpodas1-1/+1
This reverts commit ed3e3ee9e30dfbffd2170a770a49b36a7f444916.
2023-02-14[NFC][IR] Make Module::getGlobalList() privateVasileios Porpodas1-1/+1
This patch adds several missing GlobalList modifier functions, like removeGlobalVariable(), eraseGlobalVariable() and insertGlobalVariable(). There is no longer need to access the list directly so it also makes getGlobalList() private. Differential Revision: https://reviews.llvm.org/D144027
2022-10-21[HLSL] Remove unused frontend-generated IDChris Bieneman1-2/+1
As @python3kgae pointed out we're going to want to assign these IDs after optimization so that we can remove unused resrouces. This patch just removes the unused ID value from the frontend metadata, clang code generation, and updates associated test cases. Reviewed By: python3kgae Differential Revision: https://reviews.llvm.org/D136271
2022-10-20[NFC] [DirectX backend] move ResourceClass into llvm.Xiang Li1-9/+11
Move ResourceClass into llvm/Frontend/HLSL/HLSLResource.h so it could be shared between clang and DirectX backend. Reviewed By: beanz Differential Revision: https://reviews.llvm.org/D136134
2022-10-18[HLSL] Add SV_DispatchThreadIDXiang Li1-3/+29
Support SV_DispatchThreadID attribute. Translate it into dx.thread.id in clang codeGen. Reviewed By: beanz, aaron.ballman Differential Revision: https://reviews.llvm.org/D133983
2022-10-17[NFC] use llvm_unreachable instead of return on switch which all cases are ↵Xiang Li1-1/+1
covered.
2022-10-17[NFC] Fix warning on no return after switch.Xiang Li1-0/+1
2022-10-17[HLSL] CodeGen hlsl resource binding.Xiang Li1-39/+114
''register(ID, space)'' like register(t3, space1) will be translated into i32 3, i32 1 as the last 2 operands for resource annotation metadata. NamedMetadata for CBuffers and SRVs are added as "hlsl.srvs" and "hlsl.cbufs". Reviewed By: beanz Differential Revision: https://reviews.llvm.org/D130951
2022-10-14[NFC] [HLSL] Move common metadata to LLVMFrontendChris Bieneman1-3/+3
This change pulls some code from the DirectX backend into a new LLVMFrontendHLSL library to share utility data structures between the HLSL code generation in Clang and the backend in LLVM. This is a small refactoring as a first start to get code into the right structure and get the library built and dependencies correct. Fixes #58000 (https://github.com/llvm/llvm-project/issues/58000) Reviewed By: python3kgae Differential Revision: https://reviews.llvm.org/D135110
2022-10-13[HLSL] Simplify code and fix unused variable warnings. NFC.Benjamin Kramer1-7/+3
2022-10-12[HLSL] CodeGen hlsl cbuffer/tbuffer.Xiang Li1-0/+135
cbuffer A { float a; float b; } will be translated to a global variable. Something like struct CB_Ty { float a; float b; }; CB_Ty A; And all use of a and b will be replaced with A.a and A.b. Only support none-legacy cbuffer layout now. CodeGen for Resource binding will be in separate patch. In the separate patch, resource binding will map the resource information to the global variable. Reviewed By: efriedma Differential Revision: https://reviews.llvm.org/D130131
2022-10-06[HLSL] Remove global ctor/dtor variable for non-lib profile.Xiang Li1-0/+10
After generated call for ctor/dtor for entry, global variable for ctor/dtor are useless. Remove them for non-lib profiles. Lib profile still need these in case export function used the global variable which require ctor/dtor. Reviewed By: efriedma Differential Revision: https://reviews.llvm.org/D133993
2022-09-22[HLSL] clang codeGen for HLSLNumThreadsAttrXiang Li1-0/+8
Translate HLSLNumThreadsAttr into function attribute with name "dx.numthreads" and value format as "x,y,z". Reviewed By: beanz Differential Revision: https://reviews.llvm.org/D131799
2022-09-21[NFC] Add GitHub issues to HLSL FIXME commentsChris Bieneman1-0/+2
In order to make this easier to track I've filed issues for each of the HLSL FIXME comments that I can find. I may have missed some, but I want this to be the new default mode.
2022-09-13[HLSL]Add -O and -Od option for dxc mode.Xiang Li1-0/+7
Two new dxc mode options -O and -Od are added for dxc mode. -O is just alias of existing cc1 -O option. -Od will be lowered into -O0 and -dxc-opt-disable. -dxc-opt-disable is cc1 option added to for build ShaderFlags. Reviewed By: beanz Differential Revision: https://reviews.llvm.org/D128845
2022-09-13[HLSL] Call global destructors from entriesChris Bieneman1-7/+21
HLSL doesn't have a C++ runtime that supports `atexit` registration. To enable global destructors we instead rely on the `llvm.global_dtor` mechanism. This change disables `atexit` generation for HLSL and updates the HLSL code generation to call global destructors on the exit from entry functions. Depends on D132977. Reviewed By: aaron.ballman Differential Revision: https://reviews.llvm.org/D133518
2022-09-09[HLSL] Call global constructors inside entryChris Bieneman1-0/+39
HLSL doesn't have a runtime loader model that supports global construction by a loader or runtime initializer. To allow us to leverage global constructors with minimal code generation impact we put calls to the global constructors inside the generated entry function. Differential Revision: https://reviews.llvm.org/D132977
2022-08-26[HLSL] Move DXIL validation version out of ModuleFlagsXiang Li1-4/+6
Put DXIL validation version into separate NamedMetadata to avoid update ModuleFlags. Currently DXIL validation version is saved in ModuleFlags in clang codeGen. Then in DirectX backend, the data will be extracted from ModuleFlags and cause rebuild of ModuleFlags. This patch will build NamedMetadata for DXIL validation version and remove the code to rebuild ModuleFlags. Reviewed By: beanz Differential Revision: https://reviews.llvm.org/D130207