xref: /openbsd-src/gnu/llvm/clang/docs/HLSL/HLSLSupport.rst (revision 12c855180aad702bbcca06e0398d774beeafb155)
1*12c85518Srobert============
2*12c85518SrobertHLSL Support
3*12c85518Srobert============
4*12c85518Srobert
5*12c85518Srobert.. contents::
6*12c85518Srobert   :local:
7*12c85518Srobert
8*12c85518SrobertIntroduction
9*12c85518Srobert============
10*12c85518Srobert
11*12c85518SrobertHLSL Support is under active development in the Clang codebase. This document
12*12c85518Srobertdescribes the high level goals of the project, the guiding principles, as well
13*12c85518Srobertas some idiosyncrasies of the HLSL language and how we intend to support them in
14*12c85518SrobertClang.
15*12c85518Srobert
16*12c85518SrobertProject Goals
17*12c85518Srobert=============
18*12c85518Srobert
19*12c85518SrobertThe long term goal of this project is to enable Clang to function as a
20*12c85518Srobertreplacement for the `DirectXShaderCompiler (DXC)
21*12c85518Srobert<https://github.com/microsoft/DirectXShaderCompiler/>`_ in all its supported
22*12c85518Srobertuse cases. Accomplishing that goal will require Clang to be able to process most
23*12c85518Srobertexisting HLSL programs with a high degree of source compatibility.
24*12c85518Srobert
25*12c85518SrobertNon-Goals
26*12c85518Srobert---------
27*12c85518Srobert
28*12c85518SrobertHLSL ASTs do not need to be compatible between DXC and Clang. We do not expect
29*12c85518Srobertidentical code generation or that features will resemble DXC's implementation or
30*12c85518Srobertarchitecture. In fact, we explicitly expect to deviate from DXC's implementation
31*12c85518Srobertin key ways.
32*12c85518Srobert
33*12c85518SrobertGuiding Principles
34*12c85518Srobert==================
35*12c85518Srobert
36*12c85518SrobertThis document lacks details for architectural decisions that are not yet
37*12c85518Srobertfinalized. Our top priorities are quality, maintainability, and flexibility. In
38*12c85518Srobertaccordance with community standards we are expecting a high level of test
39*12c85518Srobertcoverage, and we will engineer our solutions with long term maintenance in mind.
40*12c85518SrobertWe are also working to limit modifications to the Clang C++ code paths and
41*12c85518Srobertshare as much functionality as possible.
42*12c85518Srobert
43*12c85518SrobertArchitectural Direction
44*12c85518Srobert=======================
45*12c85518Srobert
46*12c85518SrobertHLSL support in Clang is expressed as C++ minus unsupported C and C++ features.
47*12c85518SrobertThis is different from how other Clang languages are implemented. Most languages
48*12c85518Srobertin Clang are additive on top of C.
49*12c85518Srobert
50*12c85518SrobertHLSL is not a formally or fully specified language, and while our goals require
51*12c85518Sroberta high level of source compatibility, implementations can vary and we have some
52*12c85518Srobertflexibility to be more or less permissive in some cases. For modern HLSL DXC is
53*12c85518Srobertthe reference implementation.
54*12c85518Srobert
55*12c85518SrobertThe HLSL effort prioritizes following similar patterns for other languages,
56*12c85518Srobertdrivers, runtimes and targets. Specifically, We will maintain separation between
57*12c85518SrobertHSLS-specific code and the rest of Clang as much as possible following patterns
58*12c85518Srobertin use in Clang code today (i.e. ParseHLSL.cpp, SemaHLSL.cpp, CGHLSL*.cpp...).
59*12c85518SrobertWe will use inline checks on language options where the code is simple and
60*12c85518Srobertisolated, and prefer HLSL-specific implementation files for any code of
61*12c85518Srobertreasonable complexity.
62*12c85518Srobert
63*12c85518SrobertIn places where the HLSL language is in conflict with C and C++, we will seek to
64*12c85518Srobertmake minimally invasive changes guarded under the HLSL language options. We will
65*12c85518Srobertseek to make HLSL language support as minimal a maintenance burden as possible.
66*12c85518Srobert
67*12c85518SrobertDXC Driver
68*12c85518Srobert----------
69*12c85518Srobert
70*12c85518SrobertA DXC driver mode will provide command-line compatibility with DXC, supporting
71*12c85518SrobertDXC's options and flags. The DXC driver is HLSL-specific and will create an
72*12c85518SrobertHLSLToolchain which will provide the basis to support targeting both DirectX and
73*12c85518SrobertVulkan.
74*12c85518Srobert
75*12c85518SrobertParser
76*12c85518Srobert------
77*12c85518Srobert
78*12c85518SrobertFollowing the examples of other parser extensions HLSL will add a ParseHLSL.cpp
79*12c85518Srobertfile to contain the implementations of HLSL-specific extensions to the Clang
80*12c85518Srobertparser. The HLSL grammar shares most of its structure with C and C++, so we will
81*12c85518Srobertuse the existing C/C++ parsing code paths.
82*12c85518Srobert
83*12c85518SrobertSema
84*12c85518Srobert----
85*12c85518Srobert
86*12c85518SrobertHLSL's Sema implementation will also provide an ``ExternalSemaSource``. In DXC,
87*12c85518Srobertan ``ExternalSemaSource`` is used to provide definitions for HLSL built-in data
88*12c85518Sroberttypes and built-in templates. Clang is already designed to allow an attached
89*12c85518Srobert``ExternalSemaSource`` to lazily complete data types, which is a **huge**
90*12c85518Srobertperformance win for HLSL.
91*12c85518Srobert
92*12c85518SrobertIf precompiled headers are used when compiling HLSL, the ``ExternalSemaSource``
93*12c85518Srobertwill be a ``MultiplexExternalSemaSource`` which includes both the ``ASTReader``
94*12c85518Srobertand ``HLSLExternalSemaSource``. For Built-in declarations that are already
95*12c85518Srobertcompleted in the serialized AST, the ``HLSLExternalSemaSource`` will reuse the
96*12c85518Srobertexisting declarations and not introduce new declarations. If the built-in types
97*12c85518Srobertare not completed in the serialized AST, the ``HLSLExternalSemaSource`` will
98*12c85518Srobertcreate new declarations and connect the de-serialized decls as the previous
99*12c85518Srobertdeclaration.
100*12c85518Srobert
101*12c85518SrobertCodeGen
102*12c85518Srobert-------
103*12c85518Srobert
104*12c85518SrobertLike OpenCL, HLSL relies on capturing a lot of information into IR metadata.
105*12c85518Srobert*hand wave* *hand wave* *hand wave* As a design principle here we want our IR to
106*12c85518Srobertbe idiomatic Clang IR as much as possible. We will use IR attributes wherever we
107*12c85518Srobertcan, and use metadata as sparingly as possible. One example of a difference from
108*12c85518SrobertDXC already implemented in Clang is the use of target triples to communicate
109*12c85518Srobertshader model versions and shader stages.
110*12c85518Srobert
111*12c85518SrobertOur HLSL CodeGen implementation should also have an eye toward generating IR
112*12c85518Srobertthat will map directly to targets other than DXIL. While IR itself is generally
113*12c85518Srobertnot re-targetable, we want to share the Clang CodeGen implementation for HLSL
114*12c85518Srobertwith other GPU graphics targets like SPIR-V and possibly other GPU and even CPU
115*12c85518Sroberttargets.
116*12c85518Srobert
117*12c85518SrobertHLSL Language
118*12c85518Srobert=============
119*12c85518Srobert
120*12c85518SrobertThe HLSL language is insufficiently documented, and not formally specified.
121*12c85518SrobertDocumentation is available on `Microsoft's website
122*12c85518Srobert<https://docs.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl>`_.
123*12c85518SrobertThe language syntax is similar enough to C and C++ that carefully written C and
124*12c85518SrobertC++ code is valid HLSL. HLSL has some key differences from C & C++ which we will
125*12c85518Srobertneed to handle in Clang.
126*12c85518Srobert
127*12c85518SrobertHLSL is not a conforming or valid extension or superset of C or C++. The
128*12c85518Srobertlanguage has key incompatibilities with C and C++, both syntactically and
129*12c85518Srobertsemantically.
130*12c85518Srobert
131*12c85518SrobertAn Aside on GPU Languages
132*12c85518Srobert-------------------------
133*12c85518Srobert
134*12c85518SrobertDue to HLSL being a GPU targeted language HLSL is a Single Program Multiple Data
135*12c85518Srobert(SPMD) language relying on the implicit parallelism provided by GPU hardware.
136*12c85518SrobertSome language features in HLSL enable programmers to take advantage of the
137*12c85518Srobertparallel nature of GPUs in a hardware abstracted language.
138*12c85518Srobert
139*12c85518SrobertHLSL also prohibits some features of C and C++ which can have catastrophic
140*12c85518Srobertperformance or are not widely supportable on GPU hardware or drivers. As an
141*12c85518Srobertexample, register spilling is often excessively expensive on GPUs, so HLSL
142*12c85518Srobertrequires all functions to be inlined during code generation, and does not
143*12c85518Srobertsupport a runtime calling convention.
144*12c85518Srobert
145*12c85518SrobertPointers & References
146*12c85518Srobert---------------------
147*12c85518Srobert
148*12c85518SrobertHLSL does not support referring to values by address. Semantically all variables
149*12c85518Srobertare value-types and behave as such. HLSL disallows the pointer dereference
150*12c85518Srobertoperators (unary ``*``, and ``->``), as well as the address of operator (unary
151*12c85518Srobert&). While HLSL disallows pointers and references in the syntax, HLSL does use
152*12c85518Srobertreference types in the AST, and we intend to use pointer decay in the AST in
153*12c85518Srobertthe Clang implementation.
154*12c85518Srobert
155*12c85518SrobertHLSL ``this`` Keyword
156*12c85518Srobert---------------------
157*12c85518Srobert
158*12c85518SrobertHLSL does support member functions, and (in HLSL 2021) limited operator
159*12c85518Srobertoverloading. With member function support, HLSL also has a ``this`` keyword. The
160*12c85518Srobert``this`` keyword is an example of one of the places where HLSL relies on
161*12c85518Srobertreferences in the AST, because ``this`` is a reference.
162*12c85518Srobert
163*12c85518SrobertBitshifts
164*12c85518Srobert---------
165*12c85518Srobert
166*12c85518SrobertIn deviation from C, HLSL bitshifts are defined to mask the shift count by the
167*12c85518Srobertsize of the type. In DXC, the semantics of LLVM IR were altered to accommodate
168*12c85518Srobertthis, in Clang we intend to generate the mask explicitly in the IR. In cases
169*12c85518Srobertwhere the shift value is constant, this will be constant folded appropriately,
170*12c85518Srobertin other cases we can clean it up in the DXIL target.
171*12c85518Srobert
172*12c85518SrobertNon-short Circuiting Logical Operators
173*12c85518Srobert--------------------------------------
174*12c85518Srobert
175*12c85518SrobertIn HLSL 2018 and earlier, HLSL supported logical operators (and the ternary
176*12c85518Srobertoperator) on vector types. This behavior required that operators not short
177*12c85518Srobertcircuit. The non-short circuiting behavior applies to all data types until HLSL
178*12c85518Srobert2021. In HLSL 2021, logical and ternary operators do not support vector types
179*12c85518Srobertinstead builtin functions ``and``, ``or`` and ``select`` are available, and
180*12c85518Srobertoperators short circuit matching C behavior.
181*12c85518Srobert
182*12c85518SrobertPrecise Qualifier
183*12c85518Srobert-----------------
184*12c85518Srobert
185*12c85518SrobertHLSL has a ``precise`` qualifier that behaves unlike anything else in the C
186*12c85518Srobertlanguage. The support for this qualifier in DXC is buggy, so our bar for
187*12c85518Srobertcompatibility is low.
188*12c85518Srobert
189*12c85518SrobertThe ``precise`` qualifier applies in the inverse direction from normal
190*12c85518Srobertqualifiers. Rather than signifying that the declaration containing ``precise``
191*12c85518Srobertqualifier be precise, it signifies that the operations contributing to the
192*12c85518Srobertdeclaration's value be ``precise``. Additionally, ``precise`` is a misnomer:
193*12c85518Srobertvalues attributed as ``precise`` comply with IEEE-754 floating point semantics,
194*12c85518Srobertand are prevented from optimizations which could decrease *or increase*
195*12c85518Srobertprecision.
196*12c85518Srobert
197*12c85518SrobertDifferences in Templates
198*12c85518Srobert------------------------
199*12c85518Srobert
200*12c85518SrobertHLSL uses templates to define builtin types and methods, but disallowed
201*12c85518Srobertuser-defined templates until HLSL 2021. HLSL also allows omitting empty template
202*12c85518Srobertparameter lists when all template parameters are defaulted. This is an ambiguous
203*12c85518Srobertsyntax in C++, but Clang detects the case and issues a diagnostic. This makes
204*12c85518Srobertsupporting the case in Clang minimally invasive.
205*12c85518Srobert
206*12c85518SrobertVector Extensions
207*12c85518Srobert-----------------
208*12c85518Srobert
209*12c85518SrobertHLSL uses the OpenCL vector extensions, and also provides C++-style constructors
210*12c85518Srobertfor vectors that are not supported by Clang.
211*12c85518Srobert
212*12c85518SrobertStandard Library
213*12c85518Srobert----------------
214*12c85518Srobert
215*12c85518SrobertHLSL does not support the C or C++ standard libraries. Like OpenCL, HLSL
216*12c85518Srobertdescribes its own library of built in types, complex data types, and functions.
217*12c85518Srobert
218*12c85518SrobertUnsupported C & C++ Features
219*12c85518Srobert----------------------------
220*12c85518Srobert
221*12c85518SrobertHLSL does not support all features of C and C++. In implementing HLSL in Clang
222*12c85518Srobertuse of some C and C++ features will produce diagnostics under HLSL, and others
223*12c85518Srobertwill be supported as language extensions. In general, any C or C++ feature that
224*12c85518Srobertcan be supported by the DXIL and SPIR-V code generation targets could be treated
225*12c85518Srobertas a clang HLSL extension. Features that cannot be lowered to DXIL or SPIR-V,
226*12c85518Srobertmust be diagnosed as errors.
227*12c85518Srobert
228*12c85518SrobertHLSL does not support the following C features:
229*12c85518Srobert
230*12c85518Srobert* Pointers
231*12c85518Srobert* References
232*12c85518Srobert* ``goto`` or labels
233*12c85518Srobert* Variable Length Arrays
234*12c85518Srobert* ``_Complex`` and ``_Imaginary``
235*12c85518Srobert* C Threads or Atomics (or Obj-C blocks)
236*12c85518Srobert* ``union`` types `(in progress for HLSL 202x) <https://github.com/microsoft/DirectXShaderCompiler/pull/4132>`_
237*12c85518Srobert* Most features C11 and later
238*12c85518Srobert
239*12c85518SrobertHLSL does not support the following C++ features:
240*12c85518Srobert
241*12c85518Srobert* RTTI
242*12c85518Srobert* Exceptions
243*12c85518Srobert* Multiple inheritance
244*12c85518Srobert* Access specifiers
245*12c85518Srobert* Anonymous or inline namespaces
246*12c85518Srobert* ``new`` & ``delete`` operators in all of their forms (array, placement, etc)
247*12c85518Srobert* Constructors and destructors
248*12c85518Srobert* Any use of the ``virtual`` keyword
249*12c85518Srobert* Most features C++11 and later
250